Skip to content

Commit e366db8

Browse files
committed
Fixed maxRecursionDepth bug that caused hanging
1 parent afada0b commit e366db8

File tree

3 files changed

+17
-20
lines changed

3 files changed

+17
-20
lines changed

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/D3D12RaytracingSakuraForestSER.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const wchar_t* D3D12RaytracingSakuraForestSER::c_floorClosestHitShaderName = L"F
3838
const wchar_t* D3D12RaytracingSakuraForestSER::c_trunkClosestHitShaderName = L"TrunkClosestHitShader";
3939
const wchar_t* D3D12RaytracingSakuraForestSER::c_leavesClosestHitShaderName = L"LeavesClosestHitShader";
4040
const wchar_t* D3D12RaytracingSakuraForestSER::c_bushClosestHitShaderName = L"BushClosestHitShader";
41-
const wchar_t* D3D12RaytracingSakuraForestSER::c_tcubeClosestHitShaderName = L"TCubeClosestHitShader";
41+
const wchar_t* D3D12RaytracingSakuraForestSER::c_refCubeClosestHitShaderName = L"TCubeClosestHitShader";
4242
const wchar_t* D3D12RaytracingSakuraForestSER::c_missShaderName = L"MyMissShader";
4343

4444
#define PRINT(text) OutputDebugStringA(text);
@@ -227,7 +227,7 @@ void D3D12RaytracingSakuraForestSER::InitializeScene()
227227
// Initialize the view and projection inverse matrices.
228228
// m_eye currently at the middle of the forest
229229
m_eye = { 0.0f, 2.2f, -2.0f, 1.0f };
230-
m_at = { 1.0f, 1.65f, -6.0f, 1.0f };
230+
m_at = { 1.0f, 2.5f, -6.0f, 1.0f };
231231
XMVECTOR right = { 1.0f, 0.0f, 0.0f, 0.0f };
232232

233233
XMVECTOR direction = XMVector4Normalize(m_at - m_eye);
@@ -596,7 +596,7 @@ void D3D12RaytracingSakuraForestSER::CreateRaytracingPipelineStateObject()
596596
lib->DefineExport(c_trunkClosestHitShaderName);
597597
lib->DefineExport(c_leavesClosestHitShaderName);
598598
lib->DefineExport(c_bushClosestHitShaderName);
599-
lib->DefineExport(c_tcubeClosestHitShaderName);
599+
lib->DefineExport(c_refCubeClosestHitShaderName);
600600
lib->DefineExport(c_missShaderName);
601601
}
602602

@@ -623,10 +623,10 @@ void D3D12RaytracingSakuraForestSER::CreateRaytracingPipelineStateObject()
623623
bushHitGroup->SetHitGroupExport(c_bushHitGroupName);
624624
bushHitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES);
625625

626-
auto tcubeHitGroup = raytracingPipeline.CreateSubobject<CD3DX12_HIT_GROUP_SUBOBJECT>();
627-
tcubeHitGroup->SetClosestHitShaderImport(c_tcubeClosestHitShaderName);
628-
tcubeHitGroup->SetHitGroupExport(c_reflectiveCubeHitGroupName);
629-
tcubeHitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES);
626+
auto refCubeHitGroup = raytracingPipeline.CreateSubobject<CD3DX12_HIT_GROUP_SUBOBJECT>();
627+
refCubeHitGroup->SetClosestHitShaderImport(c_refCubeClosestHitShaderName);
628+
refCubeHitGroup->SetHitGroupExport(c_reflectiveCubeHitGroupName);
629+
refCubeHitGroup->SetHitGroupType(D3D12_HIT_GROUP_TYPE_TRIANGLES);
630630

631631
// Shader config
632632
// Defines the maximum sizes in bytes for the ray payload and attribute structure.
@@ -1258,7 +1258,7 @@ void D3D12RaytracingSakuraForestSER::BuildShaderTables()
12581258
void* trunkHitGroupShaderIdentifier;
12591259
void* leavesHitGroupShaderIdentifier;
12601260
void* bushHitGroupShaderIdentifier;
1261-
void* tcubeHitGroupShaderIdentifier;
1261+
void* refCubeHitGroupShaderIdentifier;
12621262

12631263
auto GetShaderIdentifiers = [&](auto* stateObjectProperties)
12641264
{
@@ -1268,7 +1268,7 @@ void D3D12RaytracingSakuraForestSER::BuildShaderTables()
12681268
trunkHitGroupShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_trunkHitGroupName);
12691269
leavesHitGroupShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_leavesHitGroupName);
12701270
bushHitGroupShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_bushHitGroupName);
1271-
tcubeHitGroupShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_reflectiveCubeHitGroupName);
1271+
refCubeHitGroupShaderIdentifier = stateObjectProperties->GetShaderIdentifier(c_reflectiveCubeHitGroupName);
12721272
};
12731273

12741274
// Get shader identifiers.
@@ -1338,7 +1338,7 @@ void D3D12RaytracingSakuraForestSER::BuildShaderTables()
13381338
argument.cb = m_reflectiveCubeCB;
13391339
argument.cb.albedo = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f);
13401340
argument.cb.materialID = 1;
1341-
hitGroupShaderTable.push_back(ShaderRecord(tcubeHitGroupShaderIdentifier, shaderIdentifierSize, &argument, sizeof(argument)));
1341+
hitGroupShaderTable.push_back(ShaderRecord(refCubeHitGroupShaderIdentifier, shaderIdentifierSize, &argument, sizeof(argument)));
13421342
}
13431343

13441344
// Tree trunk shader records

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/D3D12RaytracingSakuraForestSER.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class D3D12RaytracingSakuraForestSER : public DXSample
158158
static const wchar_t* c_floorClosestHitShaderName;
159159
static const wchar_t* c_trunkClosestHitShaderName;
160160
static const wchar_t* c_leavesClosestHitShaderName;
161-
static const wchar_t* c_tcubeClosestHitShaderName;
161+
static const wchar_t* c_refCubeClosestHitShaderName;
162162
static const wchar_t* c_missShaderName;
163163
static const wchar_t* c_bushClosestHitShaderName;
164164
ComPtr<ID3D12Resource> m_missShaderTable;

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/Raytracing.hlsl

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define HLSL
1616
#include "RaytracingHlslCompat.h"
1717
#include "Star.hlsli"
18+
#define MAX_RECURSION_DEPTH 2
1819

1920
using namespace dx;
2021
RaytracingAccelerationStructure Scene : register(t0, space0);
@@ -41,7 +42,6 @@ SamplerState TrunkSampler : register(s0);
4142
Texture2D<float4> BushTexture : register(t10, space0);
4243
SamplerState BushSampler : register(s1);
4344

44-
4545
typedef BuiltInTriangleIntersectionAttributes MyAttributes;
4646

4747

@@ -166,7 +166,7 @@ void MyRaygenShader()
166166
ray.TMax = 10000.0;
167167

168168
// Initialize payload
169-
RayPayload payload = { float4(0, 0, 0, 0), 0, 0 };
169+
RayPayload payload = { float4(0, 0, 0, 0), 1, 0 };
170170

171171
if (g_sceneCB.sortMode == SORTMODE_OFF)
172172
{
@@ -190,8 +190,6 @@ void MyRaygenShader()
190190
RenderTarget[DispatchRaysIndex().xy] = payload.color;
191191
}
192192

193-
194-
195193

196194
// Fresnel reflectance - schlick approximation.
197195
float3 FresnelReflectanceSchlick(in float3 I, in float3 N, in float3 f0)
@@ -211,7 +209,8 @@ struct Ray
211209
// Trace a radiance ray into the scene and returns a shaded color.
212210
float4 TraceRadianceRay(in Ray ray, in int currentRayRecursionDepth)
213211
{
214-
if (currentRayRecursionDepth >= 1)
212+
// Stop tracing if maximum depth is reached.
213+
if (currentRayRecursionDepth >= MAX_RECURSION_DEPTH)
215214
{
216215
return float4(0, 0, 0, 0);
217216
}
@@ -220,8 +219,7 @@ float4 TraceRadianceRay(in Ray ray, in int currentRayRecursionDepth)
220219
RayDesc rayDesc;
221220
rayDesc.Origin = ray.Origin;
222221
rayDesc.Direction = ray.Direction;
223-
// Set TMin to a zero value to avoid aliasing artifacts along contact areas.
224-
// Note: make sure to enable face culling so as to avoid surface face fighting.
222+
// Set TMin to a non-zero small value to avoid aliasing issues due to floating - point errors.
225223
rayDesc.TMin = 0.001;
226224
rayDesc.TMax = 10000;
227225
RayPayload rayPayload = { float4(0, 0, 0, 0), currentRayRecursionDepth + 1, 0 };
@@ -259,7 +257,7 @@ void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
259257
float3 finalColor;
260258

261259
// Trace reflection ray if surface is dark
262-
if (all(sampled.rgb < 0.1) && payload.recursionDepth < 2)
260+
if (all(sampled.rgb < 0.1) && payload.recursionDepth < MAX_RECURSION_DEPTH)
263261
{
264262
Ray reflectionRay;
265263
reflectionRay.Origin = hitPosition + triangleNormal * 0.001f;
@@ -426,7 +424,6 @@ void TCubeClosestHitShader(inout RayPayload payload, in MyAttributes attr)
426424
[shader("miss")]
427425
void MyMissShader(inout RayPayload payload)
428426
{
429-
//float4 background = float4(1.0000f, 0.9216f, 0.9373f, 1.0f);
430427
float4 background = float4(0.05f, 0.02f, 0.08f, 1.0f);
431428

432429
// Create sky position and ray direction

0 commit comments

Comments
 (0)