Skip to content

Commit

Permalink
Fixed HLSL shaders (mostly ray tracing related)
Browse files Browse the repository at this point in the history
Updated HLSL compile script
  • Loading branch information
SaschaWillems committed Oct 13, 2023
1 parent cb836dd commit 66dce3c
Show file tree
Hide file tree
Showing 14 changed files with 51 additions and 26 deletions.
4 changes: 3 additions & 1 deletion shaders/hlsl/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def isExe(path):
if(hlsl_file.find('.vert') != -1):
profile = 'vs_6_1'
elif(hlsl_file.find('.frag') != -1):
profile = 'ps_6_1'
profile = 'ps_6_4'
elif(hlsl_file.find('.comp') != -1):
profile = 'cs_6_1'
elif(hlsl_file.find('.geom') != -1):
Expand Down Expand Up @@ -71,6 +71,8 @@ def isExe(path):
'-fspv-extension=SPV_KHR_multiview',
'-fspv-extension=SPV_KHR_shader_draw_parameters',
'-fspv-extension=SPV_EXT_descriptor_indexing',
'-fspv-extension=SPV_KHR_ray_query',
'-fspv-extension=SPV_KHR_fragment_shading_rate',
target,
hlsl_file,
'-Fo', spv_out])
8 changes: 4 additions & 4 deletions shaders/hlsl/raytracingbasic/closesthit.rchit
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2020 Google LLC

struct Attribute
struct Attributes
{
float2 attribs;
float2 bary;
};

struct Payload
Expand All @@ -11,8 +11,8 @@ struct Payload
};

[shader("closesthit")]
void main(inout Payload p, in float2 attribs)
void main(inout Payload p, in Attributes attribs)
{
const float3 barycentricCoords = float3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
p.hitValue = barycentricCoords;
}
Binary file modified shaders/hlsl/raytracingbasic/closesthit.rchit.spv
Binary file not shown.
9 changes: 7 additions & 2 deletions shaders/hlsl/raytracingcallable/closesthit.rchit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Sascha Willems
// Copyright 2021-2023 Sascha Willems

struct Payload
{
Expand All @@ -10,8 +10,13 @@ struct CallData
float3 outColor;
};

struct Attributes
{
float2 bary;
};

[shader("closesthit")]
void main(inout Payload p, in float2 attribs)
void main(inout Payload p, in Attributes attribs)
{
// Execute the callable shader indexed by the current geometry being hit
// For our sample this means that the first callable shader in the SBT is invoked for the first triangle, the second callable shader for the second triangle, etc.
Expand Down
Binary file modified shaders/hlsl/raytracingcallable/closesthit.rchit.spv
Binary file not shown.
9 changes: 7 additions & 2 deletions shaders/hlsl/raytracingreflections/closesthit.rchit
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ struct RayPayload
float reflector;
};

struct Attributes
{
float2 bary;
};

RaytracingAccelerationStructure topLevelAS : register(t0);
struct UBO
{
Expand Down Expand Up @@ -50,7 +55,7 @@ Vertex unpack(uint index)
}

[shader("closesthit")]
void main(inout RayPayload rayPayload, in float2 attribs)
void main(inout RayPayload rayPayload, in Attributes attribs)
{
uint PrimitiveID = PrimitiveIndex();
int3 index = int3(indices[3 * PrimitiveID], indices[3 * PrimitiveID + 1], indices[3 * PrimitiveID + 2]);
Expand All @@ -60,7 +65,7 @@ void main(inout RayPayload rayPayload, in float2 attribs)
Vertex v2 = unpack(index.z);

// Interpolate normal
const float3 barycentricCoords = float3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
float3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);

// Basic lighting
Expand Down
Binary file modified shaders/hlsl/raytracingreflections/closesthit.rchit.spv
Binary file not shown.
6 changes: 3 additions & 3 deletions shaders/hlsl/raytracingsbtdata/closesthit.rchit
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2020 Google LLC

struct Attribute
struct Attributes
{
float2 attribs;
float2 bary;
};

struct Payload
Expand All @@ -19,7 +19,7 @@ struct SBT {
ConstantBuffer<SBT> sbt;

[shader("closesthit")]
void main(inout Payload p, in float2 attribs)
void main(inout Payload p, in Attributes attribs)
{
// Update the hit value to the hit record SBT data associated with this
// geometry ID and ray ID
Expand Down
21 changes: 11 additions & 10 deletions shaders/hlsl/raytracingshadows/closesthit.rchit
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright 2020 Google LLC

struct InPayload
struct Payload
{
[[vk::location(0)]] float3 hitValue;
[[vk::location(1)]] bool shadowed;
};

struct InOutPayload
struct Attributes
{
[[vk::location(2)]] bool shadowed;
float2 bary;
};

RaytracingAccelerationStructure topLevelAS : register(t0);
Expand Down Expand Up @@ -52,7 +53,7 @@ Vertex unpack(uint index)
}

[shader("closesthit")]
void main(in InPayload inPayload, inout InOutPayload inOutPayload, in float2 attribs)
void main(inout Payload payload, in Attributes attribs)
{
uint PrimitiveID = PrimitiveIndex();
int3 index = int3(indices[3 * PrimitiveID], indices[3 * PrimitiveID + 1], indices[3 * PrimitiveID + 2]);
Expand All @@ -62,24 +63,24 @@ void main(in InPayload inPayload, inout InOutPayload inOutPayload, in float2 att
Vertex v2 = unpack(index.z);

// Interpolate normal
const float3 barycentricCoords = float3(1.0f - attribs.x - attribs.y, attribs.x, attribs.y);
const float3 barycentricCoords = float3(1.0f - attribs.bary.x - attribs.bary.y, attribs.bary.x, attribs.bary.y);
float3 normal = normalize(v0.normal * barycentricCoords.x + v1.normal * barycentricCoords.y + v2.normal * barycentricCoords.z);

// Basic lighting
float3 lightVector = normalize(ubo.lightPos.xyz);
float dot_product = max(dot(lightVector, normal), 0.2);
inPayload.hitValue = v0.color.rgb * dot_product;
payload.hitValue = v0.color.rgb * dot_product;

RayDesc rayDesc;
rayDesc.Origin = WorldRayOrigin() + WorldRayDirection() * RayTCurrent();
rayDesc.Direction = lightVector;
rayDesc.TMin = 0.001;
rayDesc.TMax = 100.0;

inOutPayload.shadowed = true;
payload.shadowed = true;
// Offset indices to match shadow hit/miss index
TraceRay(topLevelAS, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, 0xff, 0, 0, 1, rayDesc, inOutPayload);
if (inOutPayload.shadowed) {
inPayload.hitValue *= 0.3;
TraceRay(topLevelAS, RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH | RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_SKIP_CLOSEST_HIT_SHADER, 0xff, 0, 0, 1, rayDesc, payload);
if (payload.shadowed) {
payload.hitValue *= 0.3;
}
}
Binary file modified shaders/hlsl/raytracingshadows/closesthit.rchit.spv
Binary file not shown.
10 changes: 8 additions & 2 deletions shaders/hlsl/raytracingshadows/miss.rmiss
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright 2020 Google LLC

struct Payload
{
[[vk::location(0)]] float3 hitValue;
[[vk::location(1)]] bool shadowed;
};

[shader("miss")]
void main([[vk::location(0)]] in float3 hitValue)
void main(inout Payload payload)
{
hitValue = float3(0.0, 0.0, 0.2);
payload.hitValue = float3(0.0, 0.0, 0.2);
}
Binary file modified shaders/hlsl/raytracingshadows/miss.rmiss.spv
Binary file not shown.
10 changes: 8 additions & 2 deletions shaders/hlsl/raytracingshadows/shadow.rmiss
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Copyright 2020 Google LLC

struct Payload
{
[[vk::location(0)]] float3 hitValue;
[[vk::location(1)]] bool shadowed;
};

[shader("miss")]
void main([[vk::location(2)]] in bool shadowed)
void main(inout Payload payload)
{
shadowed = false;
payload.shadowed = false;
}
Binary file modified shaders/hlsl/raytracingshadows/shadow.rmiss.spv
Binary file not shown.

0 comments on commit 66dce3c

Please sign in to comment.