Skip to content

Commit 956f7c8

Browse files
committed
Removed star nest in the crevices workload, separated star nest into hlsli file
1 parent a46ddec commit 956f7c8

File tree

5 files changed

+124
-146
lines changed

5 files changed

+124
-146
lines changed

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/D3D12RaytracingSakuraForestSER.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<ClInclude Include="ObjModelLoader.h" />
169169
<ClInclude Include="RaytracingHlslCompat.h" />
170170
<ClInclude Include="SharedCode.h" />
171+
<ClInclude Include="Star.hlsli" />
171172
<ClInclude Include="StepTimer.h" />
172173
<ClInclude Include="Win32Application.h" />
173174
<ClInclude Include="D3D12RaytracingSakuraForestSER.h" />

Samples/Desktop/D3D12Raytracing/src/D3D12RaytracingSakuraForestSER/D3D12RaytracingSakuraForestSER.vcxproj.filters

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@
4545
<ClInclude Include="DeviceResources.h">
4646
<Filter>Header Files\Util</Filter>
4747
</ClInclude>
48-
<ClInclude Include="D3D12RaytracingSakuraScene.h">
49-
<Filter>Header Files</Filter>
50-
</ClInclude>
5148
<ClInclude Include="DirectXRaytracingHelper.h">
5249
<Filter>Header Files</Filter>
5350
</ClInclude>
@@ -59,6 +56,7 @@
5956
<ClInclude Include="HlslCompat.h">
6057
<Filter>Assets\Shaders</Filter>
6158
</ClInclude>
59+
<ClInclude Include="D3D12RaytracingSakuraForestSER.h" />
6260
</ItemGroup>
6361
<ItemGroup>
6462
<ClCompile Include="stdafx.cpp">
@@ -76,16 +74,15 @@
7674
<ClCompile Include="DeviceResources.cpp">
7775
<Filter>Source Files\Util</Filter>
7876
</ClCompile>
79-
<ClCompile Include="D3D12RaytracingSakuraScene.cpp">
80-
<Filter>Source Files</Filter>
81-
</ClCompile>
8277
<ClCompile Include="ObjModelLoader.cpp" />
78+
<ClCompile Include="D3D12RaytracingSakuraForestSER.cpp" />
8379
</ItemGroup>
8480
<ItemGroup>
8581
<None Include="readme.md" />
8682
<None Include="packages.config" />
8783
</ItemGroup>
8884
<ItemGroup>
8985
<FxCompile Include="Raytracing.hlsl" />
86+
<FxCompile Include="Star.hlsl" />
9087
</ItemGroup>
9188
</Project>

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

Lines changed: 11 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define RAYTRACING_HLSL
1515
#define HLSL
1616
#include "RaytracingHlslCompat.h"
17+
#include "Star.hlsli"
1718

1819
using namespace dx;
1920
RaytracingAccelerationStructure Scene : register(t0, space0);
@@ -42,8 +43,7 @@ SamplerState BushSampler : register(s1);
4243

4344

4445
typedef BuiltInTriangleIntersectionAttributes MyAttributes;
45-
46-
46+
4747

4848
// Struct defines the payload used during ray tracing
4949
struct [raypayload] RayPayload
@@ -53,14 +53,6 @@ struct [raypayload] RayPayload
5353
uint reflectHint : write(caller) : read(closesthit, caller);
5454
};
5555

56-
struct [raypayload] ShadowRayPayload
57-
{
58-
bool hit : write(closesthit, miss) : read(caller);
59-
};
60-
61-
62-
63-
6456

6557
// Retrieve hit world position.
6658
float3 HitWorldPosition()
@@ -216,55 +208,12 @@ float4 TraceRadianceRay(in Ray ray, in int currentRayRecursionDepth)
216208

217209
return rayPayload.color;
218210
}
219-
220-
// Trace a shadow ray and return true if it hits any geometry.
221-
bool TraceShadowRayAndReportIfHit(in Ray ray, in int currentRayRecursionDepth)
222-
{
223-
if (currentRayRecursionDepth >= 1)
224-
{
225-
return false;
226-
}
227-
228-
// Set the ray's extents.
229-
RayDesc rayDesc;
230-
rayDesc.Origin = ray.Origin;
231-
rayDesc.Direction = ray.Direction;
232-
// Set TMin to a zero value to avoid aliasing artifcats along contact areas.
233-
// Note: make sure to enable back-face culling so as to avoid surface face fighting.
234-
rayDesc.TMin = 0;
235-
rayDesc.TMax = 10000;
236-
237-
// Initialize shadow ray payload.
238-
// Set the initial value to true since closest and any hit shaders are skipped.
239-
// Shadow miss shader, if called, will set it to false.
240-
ShadowRayPayload shadowPayload = { true };
241-
TraceRay(Scene,
242-
RAY_FLAG_CULL_BACK_FACING_TRIANGLES
243-
| RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH
244-
| RAY_FLAG_FORCE_OPAQUE // ~skip any hit shaders
245-
| RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, // ~skip closest hit shaders,
246-
~0, // InstanceInclusionMask
247-
1, // RayContributionToHitGroupIndex for shadow rays
248-
2, // MultiplierForGeometryContributionToHitGroupIndex
249-
1, // MissShaderIndex for shadow rays
250-
rayDesc, shadowPayload);
251-
252-
return shadowPayload.hit;
253-
}
254211

255212

256213
[shader("closesthit")]
257214
void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
258215
{
259216
float3 hitPosition = HitWorldPosition();
260-
261-
// Shadow component - trace for all floor pixels, not just reflective ones
262-
// Ray shadowRay = { hitPosition + float3(0, 0.001f, 0), normalize(g_sceneCB.lightPosition.xyz - hitPosition) };
263-
// bool shadowRayHit = TraceShadowRayAndReportIfHit(shadowRay, payload.recursionDepth);
264-
265-
// Calculate shadow factor
266-
// float shadowFactor = shadowRayHit ? 0.3f : 1.0f; // 0.3 for ambient when in shadow
267-
268217
uint baseIndex = PrimitiveIndex() * 3;
269218
uint offset = baseIndex * 4;
270219
uint3 indices = IndicesCube.Load3(offset);
@@ -280,8 +229,8 @@ void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
280229
float3 vertexNormals[3] =
281230
{
282231
VerticesCube[indices.x].normal,
283-
VerticesCube[indices.y].normal,
284-
VerticesCube[indices.z].normal
232+
VerticesCube[indices.y].normal,
233+
VerticesCube[indices.z].normal
285234
};
286235

287236
triangleNormal = HitAttribute(vertexNormals, attr);
@@ -298,52 +247,7 @@ void FloorClosestHitShader(inout RayPayload payload, in MyAttributes attr)
298247
float4 reflectionColor = TraceRadianceRay(reflectionRay, payload.recursionDepth);
299248
float3 fresnel = FresnelReflectanceSchlick(WorldRayDirection(), triangleNormal, baseColor);
300249
float3 refColor = lerp(baseColor, reflectionColor.rgb, fresnel) * 1.7f;
301-
302-
// Star Nest by Pablo Roman Andrioli
303-
// Volumetric fractal clouds effect
304-
const int FractalSampleSteps = 270;
305-
const int FractalIterations = 50;
306-
const float SampleSpacing = 0.05;
307-
const float FractalOffset = 0.53;
308-
309-
float fractalDensity = 0;
310-
float fadeFactor = 1.0;
311-
312-
for (int s = 0; s < FractalSampleSteps; ++s)
313-
{
314-
float3 p = hitPosition + triangleNormal * (s * SampleSpacing);
315-
p = abs(fmod(p, 2.0) - 1.0);
316-
317-
float pa = 0;
318-
float a = 0;
319-
for (int i = 0; i < FractalIterations; ++i)
320-
{
321-
float invLen2 = 1.0 / dot(p, p);
322-
p = abs(p) * invLen2 - FractalOffset;
323-
float lenP = length(p);
324-
a += abs(lenP - pa);
325-
pa = lenP;
326-
}
327-
328-
fractalDensity += a * fadeFactor;
329-
fadeFactor *= 0.9;
330-
}
331-
332-
// Normalize & smooth fractal density
333-
fractalDensity = saturate(fractalDensity / (FractalSampleSteps * FractalIterations * 0.12));
334-
float d = smoothstep(0.1, 0.9, fractalDensity);
335-
336-
// Cloud color ramp & light glow
337-
float3 blue = float3(0.4, 0.6, 1.0); // Soft blue
338-
float3 pink = float3(1.0, 0.6, 0.8); // Soft pink
339-
float3 cloud = lerp(blue, pink, d);
340-
341-
float3 L = normalize(g_sceneCB.lightPosition.xyz - hitPosition);
342-
float lightD = saturate(dot(triangleNormal, L));
343-
cloud *= lerp(0.8 + 0.2 * lightD, 4.0, d); // lightD ranges [0.8,1.0]
344-
345-
// Composite reflection with clouds
346-
finalColor = lerp(refColor, cloud, d * 1.3);
250+
finalColor = refColor;
347251
}
348252
else
349253
{
@@ -504,52 +408,19 @@ void MyMissShader(inout RayPayload payload)
504408
float3 rayOrigin = WorldRayOrigin();
505409
float3 rayDir = normalize(WorldRayDirection());
506410
float3 skyPosition = rayDir * 10.0f;
507-
508-
// Star Nest by Pablo Roman Andrioli
509-
// Volumetric fractal clouds effect
510-
const int FractalSampleSteps = 200;
511-
const int FractalIterations = 50;
512-
const float SampleSpacing = 0.05;
513-
const float FractalOffset = 0.53;
514-
515-
float fractalDensity = 0;
516-
float fadeFactor = 1.0;
517-
518-
for (int s = 0; s < FractalSampleSteps; ++s)
519-
{
520-
float3 p = skyPosition + rayDir * (s * SampleSpacing);
521-
p = abs(fmod(p, 2.0) - 1.0);
522-
523-
float pa = 0;
524-
float a = 0;
525-
for (int i = 0; i < FractalIterations; ++i)
526-
{
527-
float invLen2 = 1.0 / dot(p, p);
528-
p = abs(p) * invLen2 - FractalOffset;
529-
float lenP = length(p);
530-
a += abs(lenP - pa);
531-
pa = lenP;
532-
}
533-
534-
fractalDensity += a * fadeFactor;
535-
fadeFactor *= 0.9;
536-
}
537-
538-
// Normalize & smooth fractal density
539-
fractalDensity = saturate(fractalDensity / (FractalSampleSteps * FractalIterations * 0.12));
540-
float d = smoothstep(0.1, 0.9, fractalDensity);
411+
float fractalDensity = CalculateStarNest(skyPosition, rayDir);
541412

542-
// Cloud color ramp & light glow
413+
// Create soft purple light glow
543414
float3 blue = float3(0.4, 0.6, 1.0); // Soft blue
544415
float3 pink = float3(1.0, 0.6, 0.8); // Soft pink
545-
float3 cloud = lerp(blue, pink, d);
416+
float3 cloud = lerp(blue, pink, fractalDensity);
546417

547418
float3 L = normalize(g_sceneCB.lightPosition.xyz - skyPosition);
548-
float lightD = saturate(dot(rayDir, L)); // Use ray direction as "normal" for sky
549-
cloud *= lerp(0.8 + 0.2 * lightD, 4.0, d);
419+
float lightD = saturate(dot(rayDir, L));
420+
cloud *= lerp(0.8 + 0.2 * lightD, 4.0, fractalDensity);
550421

551422
// Composite background with clouds
552-
float3 finalColor = lerp(background.rgb, cloud, d * 1.3);
423+
float3 finalColor = lerp(background.rgb, cloud, fractalDensity * 1.3);
553424
payload.color = float4(finalColor, 1.0f);
554425
}
555426

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#ifndef STAR_NEST_HLSL
2+
#define STAR_NEST_HLSL
3+
4+
// Star Nest by Pablo Roman Andrioli
5+
// Volumetric fractal clouds effect
6+
float3 CalculateStarNest(float3 rayOrigin, float3 rayDir)
7+
{
8+
float3 skyPosition = rayDir * 10.0f;
9+
10+
const int FractalSampleSteps = 200;
11+
const int FractalIterations = 50;
12+
const float SampleSpacing = 0.05;
13+
const float FractalOffset = 0.53;
14+
15+
float fractalDensity = 0;
16+
float fadeFactor = 1.0;
17+
18+
for (int s = 0; s < FractalSampleSteps; ++s)
19+
{
20+
float3 p = skyPosition + rayDir * (s * SampleSpacing);
21+
p = abs(fmod(p, 2.0) - 1.0);
22+
23+
float pa = 0;
24+
float a = 0;
25+
for (int i = 0; i < FractalIterations; ++i)
26+
{
27+
float invLen2 = 1.0 / dot(p, p);
28+
p = abs(p) * invLen2 - FractalOffset;
29+
float lenP = length(p);
30+
a += abs(lenP - pa);
31+
pa = lenP;
32+
}
33+
34+
fractalDensity += a * fadeFactor;
35+
fadeFactor *= 0.9;
36+
}
37+
38+
// Normalize & smooth fractal density
39+
fractalDensity = saturate(fractalDensity / (FractalSampleSteps * FractalIterations * 0.12));
40+
float d = smoothstep(0.1, 0.9, fractalDensity);
41+
42+
// Cloud color ramp
43+
float3 blue = float3(0.4, 0.6, 1.0); // Soft blue
44+
float3 pink = float3(1.0, 0.6, 0.8); // Soft pink
45+
float3 cloud = lerp(blue, pink, d);
46+
47+
// Return cloud color and density (w component)
48+
return float4(cloud, d).xyz;
49+
}
50+
51+
#endif // STAR_NEST_HLSL
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//*********************************************************
2+
//
3+
// Copyright (c) Microsoft. All rights reserved.
4+
// This code is licensed under the MIT License (MIT).
5+
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
6+
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
7+
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
8+
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
9+
//
10+
// Portions of this code are based on work by Pablo Roman Andrioli.
11+
// Star Nest.
12+
// Licensed under the MIT License.
13+
//
14+
//*********************************************************
15+
16+
17+
#ifndef STAR_NEST_H
18+
#define STAR_NEST_H
19+
20+
float3 CalculateStarNest(float3 skyPosition, float3 rayDir)
21+
{
22+
// Volumetric fractal clouds effect
23+
const int FractalSampleSteps = 200;
24+
const int FractalIterations = 50;
25+
const float SampleSpacing = 0.05;
26+
const float FractalOffset = 0.53;
27+
28+
float fractalDensity = 0;
29+
float fadeFactor = 1.0;
30+
31+
for (int s = 0; s < FractalSampleSteps; ++s)
32+
{
33+
float3 p = skyPosition + rayDir * (s * SampleSpacing);
34+
p = abs(fmod(p, 2.0) - 1.0);
35+
36+
float pa = 0;
37+
float a = 0;
38+
for (int i = 0; i < FractalIterations; ++i)
39+
{
40+
float invLen2 = 1.0 / dot(p, p);
41+
p = abs(p) * invLen2 - FractalOffset;
42+
float lenP = length(p);
43+
a += abs(lenP - pa);
44+
pa = lenP;
45+
}
46+
47+
fractalDensity += a * fadeFactor;
48+
fadeFactor *= 0.9;
49+
}
50+
// Normalize & smooth fractal density
51+
fractalDensity = saturate(fractalDensity / (FractalSampleSteps * FractalIterations * 0.12));
52+
float smoothedDensity = smoothstep(0.1, 0.9, fractalDensity);
53+
54+
return smoothedDensity;
55+
}
56+
57+
58+
#endif // STAR_NEST_H

0 commit comments

Comments
 (0)