Skip to content

Commit

Permalink
Working on indirect draw
Browse files Browse the repository at this point in the history
  • Loading branch information
krupitskas committed Oct 20, 2024
1 parent 7fd05d1 commit 493248c
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 16 deletions.
10 changes: 10 additions & 0 deletions Shaders/HitRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@ struct Attributes
float2 bary;
};

struct VertexLayout
{

};

StructuredBuffer<VertexLayout> VertexBuffer : register(t0);
StructuredBuffer<uint> IndexBuffer : register(t1);

[shader("closesthit")]
void ClosestHit(inout HitInfo payload, Attributes attrib)
{
Texture2D albedo_texture = ResourceDescriptorHeap[3];

uint primitive_index = PrimitiveIndex();

float3 barycentrics = float3(1.f - attrib.bary.x - attrib.bary.y, attrib.bary.x, attrib.bary.y);

Expand Down
4 changes: 1 addition & 3 deletions Shaders/RayGenRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ cbuffer CameraParameters : register(b0)
// Raytracing acceleration structure, accessed as a SRV
RaytracingAccelerationStructure SceneBVH : register(t0);

ByteAddressBuffer IndicesBuffer : register(t1);
ByteAddressBuffer VerticesBuffer : register(t2);

[shader("raygeneration")]
void RayGen()
{
Expand All @@ -63,6 +60,7 @@ void RayGen()
ray.TMin = 0;
ray.TMax = 100000;


// Trace the ray
TraceRay(
// Parameter name: AccelerationStructure
Expand Down
11 changes: 10 additions & 1 deletion Yasno.sln
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
# 17
VisualStudioVersion = 17.10.35013.160
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Yasno", "Yasno\Yasno.vcxproj", "{71E86738-F503-4A57-B814-F657D688901B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Profile|x64 = Profile|x64
Profile|x86 = Profile|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{71E86738-F503-4A57-B814-F657D688901B}.Debug|x64.ActiveCfg = Debug|x64
{71E86738-F503-4A57-B814-F657D688901B}.Debug|x64.Build.0 = Debug|x64
{71E86738-F503-4A57-B814-F657D688901B}.Debug|x86.ActiveCfg = Debug|Win32
{71E86738-F503-4A57-B814-F657D688901B}.Debug|x86.Build.0 = Debug|Win32
{71E86738-F503-4A57-B814-F657D688901B}.Profile|x64.ActiveCfg = Profile|x64
{71E86738-F503-4A57-B814-F657D688901B}.Profile|x64.Build.0 = Profile|x64
{71E86738-F503-4A57-B814-F657D688901B}.Profile|x86.ActiveCfg = Profile|Win32
{71E86738-F503-4A57-B814-F657D688901B}.Profile|x86.Build.0 = Profile|Win32
{71E86738-F503-4A57-B814-F657D688901B}.Release|x64.ActiveCfg = Release|x64
{71E86738-F503-4A57-B814-F657D688901B}.Release|x64.Build.0 = Release|x64
{71E86738-F503-4A57-B814-F657D688901B}.Release|x86.ActiveCfg = Release|Win32
{71E86738-F503-4A57-B814-F657D688901B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
88 changes: 87 additions & 1 deletion Yasno/Graphics/Techniques/ForwardPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ namespace ysn
}
else
{
//command_list->DrawInstanced(primitive.vertexCount, 1, 0, 0);
//ccommand_list->DrawInstanced(primitive.vertexCount, 1, 0, 0);
}
}
else
Expand All @@ -319,4 +319,90 @@ namespace ysn
render_parameters.command_queue->ExecuteCommandList(command_list);
}


void ForwardPass::RenderIndirect(const RenderScene& render_scene, const ForwardPassRenderParameters& render_parameters)
{
auto renderer = Application::Get().GetRenderer();

wil::com_ptr<ID3D12GraphicsCommandList4> command_list = render_parameters.command_queue->GetCommandList("Forward Pass Indirect");

ID3D12DescriptorHeap* pDescriptorHeaps[] =
{
render_parameters.cbv_srv_uav_heap->GetHeapPtr(),
};
command_list->SetDescriptorHeaps(_countof(pDescriptorHeaps), pDescriptorHeaps);
command_list->RSSetViewports(1, &render_parameters.viewport);
command_list->RSSetScissorRects(1, &render_parameters.scissors_rect);
command_list->OMSetRenderTargets(1, &render_parameters.hdr_rtv_descriptor_handle.cpu, FALSE, &render_parameters.dsv_descriptor_handle.cpu);

{
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(render_parameters.shadow_map_buffer.buffer.get(),
D3D12_RESOURCE_STATE_DEPTH_WRITE,
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE);
command_list->ResourceBarrier(1, &barrier);
}

for(auto& model : render_scene.models)
{
for(int mesh_id = 0; mesh_id < model.meshes.size(); mesh_id++)
{
const Mesh& mesh = model.meshes[mesh_id];
const GpuResource& node_gpu_buffer = model.node_buffers[mesh_id];

//for(auto& primitive : mesh.primitives)
//{
// uint32_t attribute_slot = 0;
// for(const auto& [name, attribute] : primitive.attributes)
// {
// command_list->IASetVertexBuffers(attribute_slot, 1, &attribute.vertex_buffer_view);
// attribute_slot += 1;
// }

// // TODO: check for -1 as pso_id
// const std::optional<Pso> pso = renderer->GetPso(primitive.pso_id);

// if(pso.has_value())
// {
// const Material& material = model.materials[primitive.material_id];

// command_list->SetGraphicsRootSignature(pso.value().root_signature.get());
// command_list->SetPipelineState(pso.value().pso.get());

// command_list->IASetPrimitiveTopology(primitive.topology);
// command_list->SetGraphicsRootConstantBufferView(2, render_parameters.scene_parameters_gpu_buffer->GetGPUVirtualAddress());
// command_list->SetGraphicsRootConstantBufferView(3, material.gpu_material_parameters.GetGPUVirtualAddress());
// command_list->SetGraphicsRootDescriptorTable(4, render_parameters.shadow_map_buffer.srv_handle.gpu);

// command_list->SetGraphicsRootConstantBufferView(0, render_parameters.camera_gpu_buffer->GetGPUVirtualAddress());
// command_list->SetGraphicsRootConstantBufferView(1, node_gpu_buffer.GetGPUVirtualAddress());

// if (primitive.index_count)
// {
// command_list->IASetIndexBuffer(&primitive.index_buffer_view);
// command_list->DrawIndexedInstanced(primitive.index_count, 1, 0, 0, 0);
// }
// else
// {
// //command_list->DrawInstanced(primitive.vertexCount, 1, 0, 0);
// }
// }
// else
// {
// LogWarning << "Can't render primitive because it has't any pso attached\n";
// }
//}
}
}

{
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(render_parameters.shadow_map_buffer.buffer.get(),
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
D3D12_RESOURCE_STATE_DEPTH_WRITE);
command_list->ResourceBarrier(1, &barrier);
}


render_parameters.command_queue->ExecuteCommandList(command_list);
}

}
1 change: 1 addition & 0 deletions Yasno/Graphics/Techniques/ForwardPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ namespace ysn
//void Initialize();
bool CompilePrimitivePso(ysn::Primitive& primitive, std::vector<Material> materials);
void Render(const RenderScene& render_scene, const ForwardPassRenderParameters& render_parameters);
void RenderIndirect(const RenderScene& render_scene, const ForwardPassRenderParameters& render_parameters);
};
}
17 changes: 14 additions & 3 deletions Yasno/Graphics/Techniques/RaytracingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace ysn
{ {0 /*u0*/, 1 /*1 descriptor */, 0 /*use the implicit register space 0*/,
D3D12_DESCRIPTOR_RANGE_TYPE_UAV /* UAV representing the output buffer*/,
0 /*heap slot where the UAV is defined*/},
{0 /*t0*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_SRV /*Top-level acceleration structure*/, 1},
{0 /*t0*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1}, // TLAS
{1 /*t1*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1}, // VertexBuffer
{2 /*t2*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1}, // IndexBuffer
{0 /*b0*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_CBV /*Camera parameters*/, 2} });

return rsc.Generate(renderer->GetDevice().get(), true);
Expand Down Expand Up @@ -210,7 +212,10 @@ namespace ysn
return pBuffer;
}

bool RaytracingPass::CreateShaderBindingTable(std::shared_ptr<ysn::DxRenderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context, wil::com_ptr<ID3D12Resource> camera_buffer)
bool RaytracingPass::CreateShaderBindingTable(std::shared_ptr<ysn::DxRenderer> renderer,
wil::com_ptr<ID3D12Resource> scene_color,
RaytracingContext& rtx_context,
wil::com_ptr<ID3D12Resource> camera_buffer)
{
// The SBT helper class collects calls to Add*Program. If called several
// times, the helper must be emptied before re-adding shaders.
Expand Down Expand Up @@ -283,7 +288,12 @@ namespace ysn
return true;
}

void RaytracingPass::Execute(std::shared_ptr<ysn::DxRenderer> renderer, wil::com_ptr<ID3D12GraphicsCommandList4> command_list, uint32_t width, uint32_t height, wil::com_ptr<ID3D12Resource> scene_color, wil::com_ptr<ID3D12Resource> camera_buffer)
void RaytracingPass::Execute(std::shared_ptr<ysn::DxRenderer> renderer,
wil::com_ptr<ID3D12GraphicsCommandList4> command_list,
uint32_t width,
uint32_t height,
wil::com_ptr<ID3D12Resource> scene_color,
wil::com_ptr<ID3D12Resource> camera_buffer)
{
ID3D12DescriptorHeap* ppHeaps[] = { renderer->GetCbvSrvUavDescriptorHeap()->GetHeapPtr() };
command_list->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps);
Expand Down Expand Up @@ -339,6 +349,7 @@ namespace ysn
// We can then do the actual copy, before transitioning the render target
// buffer into a render target, that will be then used to display the image
transition = CD3DX12_RESOURCE_BARRIER::Transition(scene_color.get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_RENDER_TARGET); // TODO(rtx): This transition is done in Tonemap, so it's duplicatr

command_list->ResourceBarrier(1, &transition);
}
}
26 changes: 18 additions & 8 deletions Yasno/Yasno/Yasno.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,17 @@ namespace ysn

bool load_result = false;

//{
// 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);
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;
Expand Down Expand Up @@ -654,6 +654,8 @@ namespace ysn
{
ImGui::Begin(CONTROLS_NAME);

ImGui::Checkbox("Indirect", &m_is_indirect);

if (ImGui::CollapsingHeader("Lighting"), ImGuiTreeNodeFlags_DefaultOpen)
{
static float color[3] = { m_render_scene.directional_light.color.x, m_render_scene.directional_light.color.y, m_render_scene.directional_light.color.z };
Expand Down Expand Up @@ -807,7 +809,15 @@ namespace ysn
render_parameters.shadow_map_buffer = m_shadow_pass.shadow_map_buffer;
render_parameters.scene_parameters_gpu_buffer = m_scene_parameters_gpu_buffer;

m_forward_pass.Render(m_render_scene, render_parameters);
if (m_is_indirect)
{
m_forward_pass.RenderIndirect(m_render_scene, render_parameters);
}
else
{
m_forward_pass.Render(m_render_scene, render_parameters);
}

}
}
else
Expand Down
1 change: 1 addition & 0 deletions Yasno/Yasno/Yasno.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace ysn
wil::com_ptr<ID3D12Resource> m_scene_parameters_gpu_buffer;

bool m_is_raster = true;
bool m_is_indirect = false;
bool m_is_raster_pressed = false;

bool m_is_content_loaded = false;
Expand Down

0 comments on commit 493248c

Please sign in to comment.