Skip to content

Commit

Permalink
Added per barycenteric painting and camera projection into pathtracin…
Browse files Browse the repository at this point in the history
…g mode
  • Loading branch information
Nikita Krupitskas committed Aug 4, 2024
1 parent be10b0c commit 40188b1
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 52 deletions.
12 changes: 8 additions & 4 deletions Shaders/BasePassVertex.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ struct VS2RS
#endif
};

cbuffer Camera : register(b0)
cbuffer CameraParamters : register(b0)
{
float4x4 VP;
float3 CameraPosition;
float4x4 view_projection;
float4x4 view;
float4x4 projection;
float4x4 view_inverse;
float4x4 projection_inverse;
float3 camera_position;
};

cbuffer Node : register(b1)
Expand All @@ -47,7 +51,7 @@ VS2RS main(IA2VS input)
{
VS2RS output;
output.position = mul(float4(input.position, 1.0), M);
output.position = mul(VP, output.position);
output.position = mul(view_projection, output.position);

#ifdef HAS_NORMAL
output.normal = mul(float4(input.normal, 1.0), M);
Expand Down
11 changes: 9 additions & 2 deletions Shaders/HitRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ struct Attributes
float2 bary;
};


[shader("closesthit")]
void ClosestHit(inout HitInfo payload, Attributes attrib)
{
payload.colorAndDistance = float4(1, 1, 0, RayTCurrent());
float3 barycentrics = float3(1.f - attrib.bary.x - attrib.bary.y, attrib.bary.x, attrib.bary.y);

const float3 A = float3(1, 0, 0);
const float3 B = float3(0, 1, 0);
const float3 C = float3(0, 0, 1);

float3 hit_color = A * barycentrics.x + B * barycentrics.y + C * barycentrics.z;

payload.colorAndDistance = float4(hit_color, RayTCurrent());
}
17 changes: 15 additions & 2 deletions Shaders/RayGenRT.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ struct Attributes
// Raytracing output texture, accessed as a UAV
RWTexture2D<float4> gOutput : register(u0);

cbuffer CameraParameters : register(b0)
{
float4x4 view_projection;
float4x4 view;
float4x4 projection;
float4x4 view_inverse;
float4x4 projection_inverse;
float3 position;
}

// Raytracing acceleration structure, accessed as a SRV
RaytracingAccelerationStructure SceneBVH : register(t0);

Expand All @@ -40,10 +50,13 @@ void RayGen()

float2 d = (((launchIndex.xy + 0.5f) / dimensions.xy) * 2.f - 1.f);

float aspect_ratio = dimensions.x / dimensions.y;

// Define a ray, consisting of origin, direction, and the min-max distance values
RayDesc ray;
ray.Origin = float3(d.x, -d.y, 1);
ray.Direction = float3(0, 0, -1);
ray.Origin = mul(view_inverse, float4(0, 0, 0, 1));
float4 target = mul(projection_inverse, float4(d.x, -d.y, 1, 1));
ray.Direction = mul(view_inverse, float4(target.xyz, 0));
ray.TMin = 0;
ray.TMax = 100000;

Expand Down
6 changes: 6 additions & 0 deletions Yasno/RHI/GpuBuffer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

namespace ysn
{

}
10 changes: 5 additions & 5 deletions Yasno/RHI/ShaderManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ namespace ysn

LogInfo << "Compiling shader: " << fullpath.string().c_str() << "\n";

std::vector<std::wstring> arguments;
std::vector<LPCWSTR> arguments;

std::wstring shader_profile;

Expand All @@ -128,8 +128,8 @@ namespace ysn
}

// Strip reflection data and pdbs (see later)
arguments.push_back(L"-Qstrip_debug");
arguments.push_back(L"-Qstrip_reflect");
//arguments.push_back(L"-Qstrip_debug");
//arguments.push_back(L"-Qstrip_reflect");

//arguments.push_back(DXC_ARG_WARNINGS_ARE_ERRORS); //-WX
if (!parameters->disable_optimizations)
Expand Down Expand Up @@ -160,8 +160,8 @@ namespace ysn
filename.c_str(),
parameters->entry_point.c_str(),
shader_profile.c_str(),
nullptr,
0,
arguments.data(),
arguments.size(),
parameters->defines.data(),
static_cast<uint32_t>(parameters->defines.size()),
dxc_include_handler,
Expand Down
1 change: 1 addition & 0 deletions Yasno/RHI/VertexFactory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "VertexFactory.hpp"
6 changes: 6 additions & 0 deletions Yasno/RHI/VertexFactory.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once

namespace ysn
{

}
1 change: 1 addition & 0 deletions Yasno/System/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
#include <System/Assert.hpp>
#include <System/GlobalDefines.hpp>
#include <System/Logger.hpp>
#include <System/Math.hpp>
32 changes: 22 additions & 10 deletions Yasno/Techniques/RaytracingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <RHI/DXRHelper.h>
#include <RHI/DescriptorHeap.hpp>
#include <RHI/RayTracing.hpp>
#include <Yasno/Camera.hpp>

namespace ysn
{
Expand All @@ -19,12 +20,11 @@ namespace ysn
nv_helpers_dx12::RootSignatureGenerator rsc;

rsc.AddHeapRangesParameter(
{ { 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 /*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 /*b0*/, 1, 0, D3D12_DESCRIPTOR_RANGE_TYPE_CBV /*Camera parameters*/, 2} });

return rsc.Generate(renderer->GetDevice().get(), true);
}
Expand All @@ -45,15 +45,15 @@ namespace ysn
return rsc.Generate(renderer->GetDevice().get(), true);
}

bool RaytracingPass::Initialize(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context)
bool RaytracingPass::Initialize(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context, wil::com_ptr<ID3D12Resource> camera_buffer)
{
if (!CreateRaytracingPipeline(renderer))
{
LogFatal << "Can't create raytracing pass pipeline\n";
return false;
}

if (!CreateShaderBindingTable(renderer, scene_color, rtx_context))
if (!CreateShaderBindingTable(renderer, scene_color, rtx_context, camera_buffer))
{
LogFatal << "Can't create raytracing pass shader binding table\n";
return false;
Expand Down Expand Up @@ -210,7 +210,7 @@ namespace ysn
return pBuffer;
}

bool RaytracingPass::CreateShaderBindingTable(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context)
bool RaytracingPass::CreateShaderBindingTable(std::shared_ptr<ysn::D3D12Renderer> 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 All @@ -219,6 +219,7 @@ namespace ysn
// The pointer to the beginning of the heap is the only parameter required by shaders without root parameters
//const auto srvUavHeapHandle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle();

// First SRV again scene
const auto scene_color_uav_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle();
{
D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc = {};
Expand All @@ -228,8 +229,19 @@ namespace ysn
renderer->GetDevice()->CreateUnorderedAccessView(scene_color.get(), nullptr, &uavDesc, scene_color_uav_handle.cpu);
}

// Second SRV - TLAS
rtx_context.CreateTlasSrv(renderer);

// Third SRV - Camera
{
const auto camera_srv_handle = renderer->GetCbvSrvUavDescriptorHeap()->GetNewHandle();

D3D12_CONSTANT_BUFFER_VIEW_DESC cbv_desc = {};
cbv_desc.BufferLocation = camera_buffer->GetGPUVirtualAddress();
cbv_desc.SizeInBytes = GpuCamera::GetGpuSize();
renderer->GetDevice()->CreateConstantBufferView(&cbv_desc, camera_srv_handle.cpu);
}

// The helper treats both root parameter pointers and heap pointers as void*,
// while DX12 uses the
// D3D12_GPU_DESCRIPTOR_HANDLE to define heap pointers. The pointer in this
Expand Down Expand Up @@ -271,7 +283,7 @@ namespace ysn
return true;
}

void RaytracingPass::Execute(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12GraphicsCommandList4> command_list, uint32_t width, uint32_t height, wil::com_ptr<ID3D12Resource> scene_color)
void RaytracingPass::Execute(std::shared_ptr<ysn::D3D12Renderer> 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
6 changes: 3 additions & 3 deletions Yasno/Techniques/RaytracingPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ namespace ysn
class RaytracingPass
{
public:
bool Initialize(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context);
bool Initialize(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context, wil::com_ptr<ID3D12Resource> camera_buffer);
bool CreateRaytracingPipeline(std::shared_ptr<ysn::D3D12Renderer> renderer);
bool CreateShaderBindingTable(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context);
bool CreateShaderBindingTable(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12Resource> scene_color, RaytracingContext& rtx_context, wil::com_ptr<ID3D12Resource> camera_buffer);

void Execute(std::shared_ptr<ysn::D3D12Renderer> renderer, wil::com_ptr<ID3D12GraphicsCommandList4> command_list, uint32_t width, uint32_t height, wil::com_ptr<ID3D12Resource> scene_color);
void Execute(std::shared_ptr<ysn::D3D12Renderer> 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);

wil::com_ptr<ID3D12RootSignature> CreateRayGenSignature(std::shared_ptr<ysn::D3D12Renderer> renderer);
wil::com_ptr<ID3D12RootSignature> CreateMissSignature(std::shared_ptr<ysn::D3D12Renderer> renderer);
Expand Down
3 changes: 3 additions & 0 deletions Yasno/Yasno.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
<ClCompile Include="RHI\RayTracing.cpp" />
<ClCompile Include="RHI\ShaderManager.cpp" />
<ClCompile Include="RHI\GpuTexture.cpp" />
<ClCompile Include="RHI\VertexFactory.cpp" />
<ClCompile Include="System\Application.cpp" />
<ClCompile Include="System\Assert.cpp" />
<ClCompile Include="System\ConsoleVariables.cpp" />
Expand Down Expand Up @@ -306,6 +307,7 @@
<ClInclude Include="RHI\D3D12Renderer.hpp" />
<ClInclude Include="RHI\DescriptorHeap.hpp" />
<ClInclude Include="RHI\DXRHelper.h" />
<ClInclude Include="RHI\GpuBuffer.hpp" />
<ClInclude Include="RHI\GpuMarker.hpp" />
<ClInclude Include="RHI\nv_helpers_dx12\BottomLevelASGenerator.h" />
<ClInclude Include="RHI\nv_helpers_dx12\RaytracingPipelineGenerator.h" />
Expand All @@ -316,6 +318,7 @@
<ClInclude Include="RHI\ShaderManager.hpp" />
<ClInclude Include="RHI\GpuTexture.hpp" />
<ClInclude Include="resource.h" />
<ClInclude Include="RHI\VertexFactory.hpp" />
<ClInclude Include="System\Application.hpp" />
<ClInclude Include="System\Assert.hpp" />
<ClInclude Include="System\ConsoleVariables.hpp" />
Expand Down
9 changes: 9 additions & 0 deletions Yasno/Yasno.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
<ClCompile Include="Graphics\Material.cpp">
<Filter>Source\Graphics</Filter>
</ClCompile>
<ClCompile Include="RHI\VertexFactory.cpp">
<Filter>Source\RHI</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Yasno\Camera.hpp">
Expand Down Expand Up @@ -313,6 +316,12 @@
<ClInclude Include="Graphics\Material.hpp">
<Filter>Source\Graphics</Filter>
</ClInclude>
<ClInclude Include="RHI\GpuBuffer.hpp">
<Filter>Source\RHI</Filter>
</ClInclude>
<ClInclude Include="RHI\VertexFactory.hpp">
<Filter>Source\RHI</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\Shaders\BasePassPixelGray.hlsl">
Expand Down
7 changes: 7 additions & 0 deletions Yasno/Yasno/Camera.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#include "Camera.hpp"

#include <d3d12.h>

#include <SimpleMath.h>

namespace ysn
{
using namespace DirectX;
using namespace DirectX::SimpleMath;

uint64_t GpuCamera::GetGpuSize()
{
return ysn::AlignPow2(sizeof(GpuCamera), D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT);
}

Camera::Camera()
{
m_ProjectionMatrix = XMMatrixIdentity();
Expand Down
13 changes: 13 additions & 0 deletions Yasno/Yasno/Camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@

namespace ysn
{
struct alignas(16) GpuCamera
{
DirectX::XMFLOAT4X4 view_projection;
DirectX::XMFLOAT4X4 view;
DirectX::XMFLOAT4X4 projection;
DirectX::XMFLOAT4X4 view_inverse;
DirectX::XMFLOAT4X4 projection_inverse;
DirectX::XMFLOAT3 position;
uint32_t pad;

static uint64_t GetGpuSize();
};

struct Camera
{
Camera();
Expand Down
Loading

0 comments on commit 40188b1

Please sign in to comment.