Skip to content

Commit

Permalink
Bring RTX back
Browse files Browse the repository at this point in the history
  • Loading branch information
krupitskas committed Oct 17, 2024
1 parent 8751abf commit 0e87783
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 103 deletions.
3 changes: 3 additions & 0 deletions Yasno/Graphics/Primitive.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace ysn
std::string name;

uint32_t vertex_count = 0;
//uint64_t vertex_offset_in_bytes = 0;

DXGI_FORMAT format;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;
Expand All @@ -29,6 +30,8 @@ namespace ysn
D3D12_INDEX_BUFFER_VIEW index_buffer_view;

uint32_t index_count = 0;
//uint64_t index_offset_in_bytes = 0;

int material_id = -1;

std::unordered_map<std::string, Attribute> attributes;
Expand Down
72 changes: 35 additions & 37 deletions Yasno/Renderer/RaytracingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Renderer/nv_helpers_dx12/BottomLevelASGenerator.h>
#include <Renderer/DXRHelper.h>
#include <System/GltfLoader.hpp>
#include <System/Application.hpp>

// TODO(postrtx): remove this
ID3D12Resource* CreateBuffer(ID3D12Device* m_device, uint64_t size, D3D12_RESOURCE_FLAGS flags, D3D12_RESOURCE_STATES initState, const D3D12_HEAP_PROPERTIES& heapProps)
Expand Down Expand Up @@ -40,12 +41,9 @@ ysn::AccelerationStructureBuffers ysn::RaytracingContext::CreateBottomLevelAS(wi
for (const auto& buffer : vertex_buffers)
{
bottomLevelAS.AddVertexBuffer(
buffer.vertex_buffer.get(),
buffer.vertex_offset_in_bytes,
buffer.vertex_buffer_view,
buffer.index_buffer_view,
buffer.vertex_count,
buffer.vertex_stride,
buffer.index_buffer.get(),
buffer.index_offset_in_bytes,
buffer.index_count,
0,
0);
Expand Down Expand Up @@ -151,50 +149,50 @@ void ysn::RaytracingContext::CreateTlasSrv(std::shared_ptr<ysn::DxRenderer> rend

// Combine the BLAS and TLAS builds to construct the entire acceleration structure required to raytrace the scene

/*

void ysn::RaytracingContext::CreateAccelerationStructures(wil::com_ptr<ID3D12Device5> device,
wil::com_ptr<ID3D12GraphicsCommandList4> command_list,
ysn::ModelRenderContext* gltf_model_context)
void ysn::RaytracingContext::CreateAccelerationStructures(wil::com_ptr<ID3D12GraphicsCommandList4> command_list,const RenderScene& render_scene)
{
std::vector<BLASVertexInput> vertex_buffers;
std::shared_ptr<DxRenderer> renderer = Application::Get().GetRenderer();

// Vertex buffer
// Amout of vertices
// Sizeof vertex
for (const auto& mesh : gltf_model_context->Meshes)
for (auto& model : render_scene.models)
{
for (const auto& primitive : mesh.primitives)
for(int i = 0; i < model.meshes.size(); i++)
{
BLASVertexInput blas_input;
const ysn::Mesh& mesh = model.meshes[i];
const ysn::NodeTransform& transform = model.transforms[i];

blas_input.vertex_buffer = primitive.vertex_buffer;
blas_input.vertex_offset_in_bytes = primitive.vertex_offset_in_bytes;
blas_input.vertex_count = primitive.vertex_count;
blas_input.vertex_stride = primitive.vertex_stride;
blas_input.index_buffer = primitive.index_buffer;
blas_input.index_offset_in_bytes = primitive.index_offset_in_bytes;
blas_input.index_count = primitive.index_count;
for (auto& primitive : mesh.primitives)
{
std::vector<BLASVertexInput> vertex_buffers;

vertex_buffers.push_back(blas_input);
}
}
BLASVertexInput blas_input;

const Attribute& attribute = primitive.attributes.at("POSITION");

blas_input.vertex_count = attribute.vertex_count;
blas_input.index_count = primitive.index_count;

blas_input.vertex_buffer_view = attribute.vertex_buffer_view;
blas_input.index_buffer_view = primitive.index_buffer_view;

// Build the bottom AS from the Triangle vertex buffer
AccelerationStructureBuffers bottomLevelBuffers = CreateBottomLevelAS(device, command_list, vertex_buffers);
vertex_buffers.push_back(blas_input);

// Just one instance for now
instances = { { bottomLevelBuffers.result, DirectX::XMMatrixIdentity() } };
CreateTopLevelAS(device, command_list, instances);
// Build the bottom AS from the Triangle vertex buffer
AccelerationStructureBuffers bottomLevelBuffers = CreateBottomLevelAS(renderer->GetDevice(), command_list, vertex_buffers);
instances.emplace_back(bottomLevelBuffers.result, transform.transform);

// Store the AS buffers. The rest of the buffers will be released once we exit
// the function
blas_res = bottomLevelBuffers.result;
blas_res.push_back(bottomLevelBuffers.result);

#ifndef YSN_RELEASE
bottomLevelBuffers.result->SetName(L"BLAS");
#endif
}
}
}

CreateTopLevelAS(renderer->GetDevice(), command_list, instances);

#ifndef YSN_RELEASE
blas_res->SetName(L"BLAS");
tlas_buffers.result->SetName(L"TLAS");
#endif
}
*/
24 changes: 6 additions & 18 deletions Yasno/Renderer/RaytracingContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,32 @@ namespace ysn

struct BLASVertexInput
{
wil::com_ptr<ID3D12Resource> vertex_buffer;
uint64_t vertex_offset_in_bytes;
uint32_t vertex_count;
uint32_t vertex_stride;
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view;
D3D12_INDEX_BUFFER_VIEW index_buffer_view;

wil::com_ptr<ID3D12Resource> index_buffer;
uint64_t index_offset_in_bytes;
uint32_t vertex_count;
uint32_t index_count;
};

struct RaytracingContext
{
/// Create the acceleration structure of an instance
/// vertex_buffers : pair of buffer and vertex count
AccelerationStructureBuffers CreateBottomLevelAS(
wil::com_ptr<ID3D12Device5> device,
wil::com_ptr<ID3D12GraphicsCommandList4> command_list,
std::vector<BLASVertexInput> vertex_buffers); // sizeof(Vertex)
std::vector<BLASVertexInput> vertex_buffers);

/// Create the main acceleration structure that holds
/// all instances of the scene
/// \param instances : pair of BLAS and transform
void CreateTopLevelAS(
wil::com_ptr<ID3D12Device5> device,
wil::com_ptr<ID3D12GraphicsCommandList4> command_list,
const std::vector<std::pair<wil::com_ptr<ID3D12Resource>, DirectX::XMMATRIX>>& instances);

void CreateTlasSrv(std::shared_ptr<ysn::DxRenderer> renderer);

/// Create all acceleration structures, bottom and top
//void CreateAccelerationStructures(
// wil::com_ptr<ID3D12Device5> device,
// wil::com_ptr<ID3D12GraphicsCommandList4> command_list,
// ysn::ModelRenderContext* gltf_model_context);
void CreateAccelerationStructures(wil::com_ptr<ID3D12GraphicsCommandList4> command_list, const RenderScene& render_scene);

nv_helpers_dx12::TopLevelASGenerator tlas_generator;

wil::com_ptr<ID3D12Resource> blas_res;
std::vector<wil::com_ptr<ID3D12Resource>> blas_res;

AccelerationStructureBuffers tlas_buffers;
std::vector<std::pair<wil::com_ptr<ID3D12Resource>, DirectX::XMMATRIX>> instances;
Expand Down
46 changes: 24 additions & 22 deletions Yasno/Renderer/nv_helpers_dx12/BottomLevelASGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ namespace nv_helpers_dx12
//--------------------------------------------------------------------------------------------------
// Add a vertex buffer in GPU memory into the acceleration structure. The
// vertices are supposed to be represented by 3 float32 value
void BottomLevelASGenerator::AddVertexBuffer(
ID3D12Resource* vertexBuffer, // Buffer containing the vertex coordinates possibly interleaved with other vertex data
UINT64 vertexOffsetInBytes, // Offset of the first vertex in the vertex buffer
uint32_t vertexCount, // Number of vertices to consider in the buffer
UINT vertexSizeInBytes, // Size of a vertex including all its other data used to stride in the buffer
ID3D12Resource* transformBuffer, // Buffer containing a 4x4 transform matrix in GPU memory, to be applied to the vertices. This buffer cannot be nullptr
UINT64 transformOffsetInBytes, // Offset of the transform matrix in the transform buffer
bool isOpaque /* = true */ // If true, the geometry is considered opaque optimizing the search for a closest hit
)
{
AddVertexBuffer(
vertexBuffer, vertexOffsetInBytes, vertexCount, vertexSizeInBytes, nullptr, 0, 0, transformBuffer, transformOffsetInBytes, isOpaque);
}
//void BottomLevelASGenerator::AddVertexBuffer(
// ID3D12Resource* vertexBuffer, // Buffer containing the vertex coordinates possibly interleaved with other vertex data
// UINT64 vertexOffsetInBytes, // Offset of the first vertex in the vertex buffer
// uint32_t vertexCount, // Number of vertices to consider in the buffer
// UINT vertexSizeInBytes, // Size of a vertex including all its other data used to stride in the buffer
// ID3D12Resource* transformBuffer, // Buffer containing a 4x4 transform matrix in GPU memory, to be applied to the vertices. This buffer cannot be nullptr
// UINT64 transformOffsetInBytes, // Offset of the transform matrix in the transform buffer
// bool isOpaque /* = true */ // If true, the geometry is considered opaque optimizing the search for a closest hit
//)
//{
// AddVertexBuffer(
// vertexBuffer, vertexOffsetInBytes, vertexCount, vertexSizeInBytes, nullptr, 0, 0, transformBuffer, transformOffsetInBytes, isOpaque);
//}

//--------------------------------------------------------------------------------------------------
// Add a vertex buffer along with its index buffer in GPU memory into the
Expand All @@ -36,12 +36,14 @@ namespace nv_helpers_dx12
// - 3xfloat32 format
// - 32-bit indices
void BottomLevelASGenerator::AddVertexBuffer(
ID3D12Resource* vertexBuffer, // Buffer containing the vertex coordinates possibly interleaved with other vertex data
UINT64 vertexOffsetInBytes, // Offset of the first vertex in the vertex buffer
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view,
D3D12_INDEX_BUFFER_VIEW index_buffer_view,
//ID3D12Resource* vertexBuffer, // Buffer containing the vertex coordinates possibly interleaved with other vertex data
//UINT64 vertexOffsetInBytes, // Offset of the first vertex in the vertex buffer
uint32_t vertexCount, // Number of vertices to consider in the buffer
UINT vertexSizeInBytes, // Size of a vertex including all its other data used to stride in the buffer
ID3D12Resource* indexBuffer, // Buffer containing the vertex indices describing the triangles
UINT64 indexOffsetInBytes, // Offset of the first index in the index buffer
//UINT vertexSizeInBytes, // Size of a vertex including all its other data used to stride in the buffer
//ID3D12Resource* indexBuffer, // Buffer containing the vertex indices describing the triangles
//UINT64 indexOffsetInBytes, // Offset of the first index in the index buffer
uint32_t indexCount, // Number of indices to consider in the buffer
ID3D12Resource* transformBuffer, // Buffer containing a 4x4 transform matrix in GPU memory, to be applied to the vertices. This buffer cannot be nullptr
UINT64 transformOffsetInBytes, // Offset of the transform matrix in the transform buffer
Expand All @@ -52,12 +54,12 @@ namespace nv_helpers_dx12
// opaque triangles, with 3xf32 vertex coordinates and 32-bit indices
D3D12_RAYTRACING_GEOMETRY_DESC descriptor = {};
descriptor.Type = D3D12_RAYTRACING_GEOMETRY_TYPE_TRIANGLES;
descriptor.Triangles.VertexBuffer.StartAddress = vertexBuffer->GetGPUVirtualAddress() + vertexOffsetInBytes;
descriptor.Triangles.VertexBuffer.StrideInBytes = vertexSizeInBytes;
descriptor.Triangles.VertexBuffer.StartAddress = vertex_buffer_view.BufferLocation;// vertexBuffer->GetGPUVirtualAddress() + vertexOffsetInBytes;
descriptor.Triangles.VertexBuffer.StrideInBytes = vertex_buffer_view.StrideInBytes; // vertexSizeInBytes;
descriptor.Triangles.VertexCount = vertexCount;
descriptor.Triangles.VertexFormat = DXGI_FORMAT_R32G32B32_FLOAT;
descriptor.Triangles.IndexBuffer = indexBuffer ? (indexBuffer->GetGPUVirtualAddress() + indexOffsetInBytes) : 0;
descriptor.Triangles.IndexFormat = indexBuffer ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_UNKNOWN; // TODO: DXGI_FORMAT_R32_UINT provide
descriptor.Triangles.IndexBuffer = index_buffer_view.BufferLocation;// indexBuffer ? (indexBuffer->GetGPUVirtualAddress() + indexOffsetInBytes) : 0;
descriptor.Triangles.IndexFormat = DXGI_FORMAT_R16_UINT;// indexBuffer ? DXGI_FORMAT_R16_UINT : DXGI_FORMAT_UNKNOWN; // TODO: DXGI_FORMAT_R32_UINT provide
descriptor.Triangles.IndexCount = indexCount;
descriptor.Triangles.Transform3x4 = transformBuffer ? (transformBuffer->GetGPUVirtualAddress() + transformOffsetInBytes) : 0;
descriptor.Flags = isOpaque ? D3D12_RAYTRACING_GEOMETRY_FLAG_OPAQUE : D3D12_RAYTRACING_GEOMETRY_FLAG_NONE;
Expand Down
12 changes: 7 additions & 5 deletions Yasno/Renderer/nv_helpers_dx12/BottomLevelASGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,14 @@ namespace nv_helpers_dx12
/// The vertices are supposed to be represented by 3 float32 value, and the indices are 32-bit
/// unsigned ints
void AddVertexBuffer(
ID3D12Resource* vertexBuffer, /// Buffer containing the vertex coordinates possibly interleaved with other vertex data
UINT64 vertexOffsetInBytes, /// Offset of the first vertex in the vertex buffer
D3D12_VERTEX_BUFFER_VIEW vertex_buffer_view,
D3D12_INDEX_BUFFER_VIEW index_buffer_view,
//ID3D12Resource* vertexBuffer, /// Buffer containing the vertex coordinates possibly interleaved with other vertex data
//UINT64 vertexOffsetInBytes, /// Offset of the first vertex in the vertex buffer
uint32_t vertexCount, /// Number of vertices to consider in the buffer
UINT vertexSizeInBytes, /// Size of a vertex including all its other data used to stride in the buffer
ID3D12Resource* indexBuffer, /// Buffer containing the vertex indices describing the triangles
UINT64 indexOffsetInBytes, /// Offset of the first index in the index buffer
//UINT vertexSizeInBytes, /// Size of a vertex including all its other data used to stride in the buffer
//ID3D12Resource* indexBuffer, /// Buffer containing the vertex indices describing the triangles
//UINT64 indexOffsetInBytes, /// Offset of the first index in the index buffer
uint32_t indexCount, /// Number of indices to consider in the buffer
ID3D12Resource* transformBuffer, /// Buffer containing a 4x4 transform matrix in GPU memory, to be applied to the vertices. This buffer cannot be nullptr
UINT64 transformOffsetInBytes, /// Offset of the transform matrix in the transform buffer
Expand Down
2 changes: 1 addition & 1 deletion Yasno/System/GltfLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ static void BuildIndexBuffer(ysn::Primitive& primitive, const ysn::Model& model,
primitive.index_count = static_cast<uint32_t>(gltf_accessor.count);

//pRenderPrimitive->index_buffer = pBuffers[gltf_buffer_view.buffer];
//pRenderPrimitive->index_offset_in_bytes = gltf_buffer_view.byteOffset + gltf_accessor.byteOffset;
//primitive->index_offset_in_bytes = gltf_buffer_view.byteOffset + gltf_accessor.byteOffset;
}
else
{
Expand Down
33 changes: 13 additions & 20 deletions Yasno/Yasno/Yasno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,18 @@ namespace ysn

wil::com_ptr<ID3D12GraphicsCommandList4> command_list = command_queue->GetCommandList("Load Content");

// TODO(last): Another command list?
bool load_result = false;
//LoadGLTFModel(&m_gltf_draw_context, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), Application::Get().GetRenderer(), command_list);
{
LoadingParameters loading_parameters;
loading_parameters.model_modifier = XMMatrixScaling(0.01f, 0.01f, 0.01f);
load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/Sponza/Sponza.gltf"), loading_parameters);
}
//{
// LoadingParameters loading_parameters;
// load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), loading_parameters);
//}
//LoadGLTFModel(&m_gltf_draw_context, GetVirtualFilesystemPath(L"Assets/BoomBoxWithAxes/glTF/BoomBoxWithAxes.gltf"), Application::Get().GetRenderer(), command_list);
{
LoadingParameters loading_parameters;
load_result = LoadGltfFromFile(m_render_scene, GetVirtualFilesystemPath(L"Assets/DamagedHelmet/DamagedHelmet.gltf"), loading_parameters);
}

//LoadGLTFModel(&m_gltf_draw_context, GetVirtualFilesystemPath(L"Assets/BoomBoxWithAxes/glTF/BoomBoxWithAxes.gltf"), Application::Get().GetRenderer(), command_list);
//{
// LoadingParameters loading_parameters;
// loading_parameters.model_modifier = XMMatrixScaling(0.01f, 0.01f, 0.01f);
Expand All @@ -291,6 +289,8 @@ namespace ysn
}
}

m_raytracing_context.CreateAccelerationStructures(command_list, m_render_scene);

if (!m_tonemap_pass.Initialize())
{
LogFatal << "Can't initialize tonemap pass\n";
Expand Down Expand Up @@ -333,9 +333,6 @@ namespace ysn
m_backbuffer_uav_descriptor_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle();
m_depth_dsv_descriptor_handle = renderer->GetDsvDescriptorHeap()->GetNewHandle();

// TODO(return)
//m_raytracing_context.CreateAccelerationStructures(Application::Get().GetRenderer()->GetDevice(), command_list, &m_render_scene);

if (!CreateGpuCameraBuffer())
{
LogFatal << "Yasno app can't create GPU camera buffer\n";
Expand Down Expand Up @@ -366,11 +363,11 @@ namespace ysn
// Setup techniques
m_shadow_pass.Initialize(Application::Get().GetRenderer());

//if (!m_ray_tracing_pass.Initialize(Application::Get().GetRenderer(), m_scene_color_buffer, m_raytracing_context, m_camera_gpu_buffer))
//{
// LogFatal << "Can't initialize raytracing pass\n";
// return false;
//}
if (!m_ray_tracing_pass.Initialize(Application::Get().GetRenderer(), m_scene_color_buffer, m_raytracing_context, m_camera_gpu_buffer))
{
LogFatal << "Can't initialize raytracing pass\n";
return false;
}

// Setup camera
m_render_scene.camera = std::make_shared<ysn::Camera>();
Expand Down Expand Up @@ -810,9 +807,7 @@ namespace ysn
}
else
{
wil::com_ptr<ID3D12GraphicsCommandList4> command_list = command_queue->GetCommandList("Raytracing");

PIXBeginEvent(command_list.get(), PIX_COLOR_DEFAULT, "RTX Pass");
wil::com_ptr<ID3D12GraphicsCommandList4> command_list = command_queue->GetCommandList("RTX Pass");

// Clear the render targets.
{
Expand All @@ -828,8 +823,6 @@ namespace ysn

m_ray_tracing_pass.Execute(Application::Get().GetRenderer(), command_list, GetClientWidth(), GetClientHeight(), m_scene_color_buffer, m_camera_gpu_buffer);

PIXEndEvent(command_list.get());

command_queue->ExecuteCommandList(command_list);
}

Expand Down

0 comments on commit 0e87783

Please sign in to comment.