From 66dce3c9916d0a1e1ba0c6b5450491919b0d3133 Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Fri, 13 Oct 2023 17:26:51 +0200 Subject: [PATCH] Fixed HLSL shaders (mostly ray tracing related) Updated HLSL compile script --- shaders/hlsl/compile.py | 4 +++- shaders/hlsl/raytracingbasic/closesthit.rchit | 8 +++---- .../hlsl/raytracingbasic/closesthit.rchit.spv | Bin 1144 -> 600 bytes .../hlsl/raytracingcallable/closesthit.rchit | 9 ++++++-- .../raytracingcallable/closesthit.rchit.spv | Bin 624 -> 624 bytes .../raytracingreflections/closesthit.rchit | 9 ++++++-- .../closesthit.rchit.spv | Bin 3468 -> 3756 bytes .../hlsl/raytracingsbtdata/closesthit.rchit | 6 ++--- .../hlsl/raytracingshadows/closesthit.rchit | 21 +++++++++--------- .../raytracingshadows/closesthit.rchit.spv | Bin 4032 -> 4084 bytes shaders/hlsl/raytracingshadows/miss.rmiss | 12 +++++++--- shaders/hlsl/raytracingshadows/miss.rmiss.spv | Bin 672 -> 460 bytes shaders/hlsl/raytracingshadows/shadow.rmiss | 12 +++++++--- .../hlsl/raytracingshadows/shadow.rmiss.spv | Bin 608 -> 416 bytes 14 files changed, 53 insertions(+), 28 deletions(-) diff --git a/shaders/hlsl/compile.py b/shaders/hlsl/compile.py index eea29bf3..3923c618 100644 --- a/shaders/hlsl/compile.py +++ b/shaders/hlsl/compile.py @@ -43,7 +43,7 @@ for root, dirs, files in os.walk(dir_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): @@ -71,6 +71,8 @@ for root, dirs, files in os.walk(dir_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]) diff --git a/shaders/hlsl/raytracingbasic/closesthit.rchit b/shaders/hlsl/raytracingbasic/closesthit.rchit index 2a8921ce..98655f73 100644 --- a/shaders/hlsl/raytracingbasic/closesthit.rchit +++ b/shaders/hlsl/raytracingbasic/closesthit.rchit @@ -1,8 +1,8 @@ // Copyright 2020 Google LLC -struct Attribute +struct Attributes { - float2 attribs; + float2 bary; }; struct Payload @@ -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; } diff --git a/shaders/hlsl/raytracingbasic/closesthit.rchit.spv b/shaders/hlsl/raytracingbasic/closesthit.rchit.spv index 95890dd79693ee41f7859fe0afe7b25b97cc7e3b..58768f06333105863a74d35aaf990b4b0fbd5a37 100644 GIT binary patch literal 600 zcmYk3K}*9x5QWFYBwDq#R@<9YJbD*FRJ;jZqC&4>t3?B~N}38eDfrj?7lOaTi{Se< z5!_{VX5PFvJDY^Teqzy*nYC@8lhw7rUb|*3i|r;IWtSJ%S>dXzEZl8AyB9O`JX9S^ z>|@)E_|fGv3V{qps2$0_2#9Q1+bng}^vT_+bL?|L_K=q&H(gAKBWch-`#AFY^Rg`R z@uHl}O{yA$J9b5dr#JUG&zt-F|2%zFW7UVU196EHnc3UfK>3OyFna9jWKOW5qE-1X zy$=j-OCByW!sUHnaM$EN#Rcn$u`a(S&W1ekanlbiJ;~8V^pB=?Zk9q($mgW33dWV+LnZwiNEGwX#5T* zKGrJhYH5+(XYcdAw~A(Cbvj}yB4*`h(%~!El_8hLdr6HQL`X zvsva@H5cvpM6P@rCAnm^2mba-nhdn5H zD=uKuWHKJTd{_3U_iy95Vs09zy|u$MUOT`u9o>I?xjJHz9atp_;B%wWzfbX3w}?3> z$325}S$E*(WX;9Jv6_~juIc0OqNZoyy_!A&|4`E>;VzK-pHj0x*0abTAnQ5!Pq4(@ zIky1Dstly{lYPRN9&@Z|*wQT=R+&t+8h_i3e!}=-U zl5`8+^2)8vDU&W0dSK_IpT%CiG$A&?9Z#|bin*&un492<}p24a@UrcBxb QAhSh*_%8z^g9Q)+0Kw)FEdT%j delta 152 zcmeys@_}VS24lm-OnpYCi8IX^nI~RU7G!2%V2}f15O8N;VPKie$~aq*3CO!A!oXk+ z6yXN4L8>l5`8+^2%jAQM&W0dSK_IpTs$vJyLLeC+jsuzsVuR#Ca%@0a7{s4!%A_p- PGFudg|1vN#SO75q#(@zu diff --git a/shaders/hlsl/raytracingreflections/closesthit.rchit b/shaders/hlsl/raytracingreflections/closesthit.rchit index 643bfbe9..7159ecfd 100644 --- a/shaders/hlsl/raytracingreflections/closesthit.rchit +++ b/shaders/hlsl/raytracingreflections/closesthit.rchit @@ -8,6 +8,11 @@ struct RayPayload float reflector; }; +struct Attributes +{ + float2 bary; +}; + RaytracingAccelerationStructure topLevelAS : register(t0); struct UBO { @@ -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]); @@ -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 diff --git a/shaders/hlsl/raytracingreflections/closesthit.rchit.spv b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>IcPAfr&_rK{_&xU{F~rM%l^gF?e-M}vQ57Rn|{0k=t_Y(jB diff --git a/shaders/hlsl/raytracingsbtdata/closesthit.rchit b/shaders/hlsl/raytracingsbtdata/closesthit.rchit index 1b306450..0a670e95 100644 --- a/shaders/hlsl/raytracingsbtdata/closesthit.rchit +++ b/shaders/hlsl/raytracingsbtdata/closesthit.rchit @@ -1,8 +1,8 @@ // Copyright 2020 Google LLC -struct Attribute +struct Attributes { - float2 attribs; + float2 bary; }; struct Payload @@ -19,7 +19,7 @@ struct SBT { ConstantBuffer 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 diff --git a/shaders/hlsl/raytracingshadows/closesthit.rchit b/shaders/hlsl/raytracingshadows/closesthit.rchit index 6dba8bc9..6e644ca9 100644 --- a/shaders/hlsl/raytracingshadows/closesthit.rchit +++ b/shaders/hlsl/raytracingshadows/closesthit.rchit @@ -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); @@ -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]); @@ -62,13 +63,13 @@ 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(); @@ -76,10 +77,10 @@ void main(in InPayload inPayload, inout InOutPayload inOutPayload, in float2 att 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; } } diff --git a/shaders/hlsl/raytracingshadows/closesthit.rchit.spv b/shaders/hlsl/raytracingshadows/closesthit.rchit.spv index 983aafe1e48e01e90fa5ac79474532c0ed17924e..4f6ff0a6a96777f96a4e735db0359842bfd86f92 100644 GIT binary patch delta 569 zcmXw0y-LGi6un8)q{XHnEr^R1G$3^GXVJm78N{_BxH){KfffptHbpu}UqCt-|LaRg zUcmPd+#KB$tsr<#`ZeKj?mhQEc`1FC+?2WINZJsQygWU-Q)6UF0bX4VJ6+Wj8!vr6 zt#){#I)i}rTxVs!rJCLIph-O~mMnnJbbT615xyUWy>?>|27T7fH0C!{Z%Afc4y>XJ zGwWL7!qz?FiRoB}v$ZxMuLG+{-vYm(H`iN&zYSgi%!E_PSZd7O0ly-*7`@w7qY!(J zV`R$+EF(~f4l*UL3Tqz!C=Q delta 522 zcmY*W%Syvg6g)|qq+(Jj7Nr{_Xu(n!S~t3|skrLOir~&gq=g1b3pS;=kp6(UFuwf? z$q)D+f_wLhRuG)IRMZ=WnS0LMxhKi1_vpEXzF>=`iAYB79^8>OG-VpQTBjZyx4S_D z+QP#cS1XM6gLc13ZQ_{(hkv{#ygAfrot=L48=*2-=jyeBMt9I;G!jzcfP*MH3J-f? z&cN!T5dIHH?sYr0ZKyd`(4NP+Rq!)HIoq5V>fS}@S@g=PCZdDsT?0SCTaI5WU&|#$ z+t%{SXmF?sPykBU=fQrwk@m7xsLgTI7POO`r1z{s1vha(9%4HKJ_l^zAnPdY#}iYf zA){k}?p?eoXS*2JK=O{HOA4${B(Zk|u=at;sn2kZ?|TiL0!zRMy&n(x4R_OX1JF|i Z;4i9xI)eFuZUGuNK7ShbU!uu6@C~hGMr!~7 diff --git a/shaders/hlsl/raytracingshadows/miss.rmiss b/shaders/hlsl/raytracingshadows/miss.rmiss index b142967a..e4193639 100644 --- a/shaders/hlsl/raytracingshadows/miss.rmiss +++ b/shaders/hlsl/raytracingshadows/miss.rmiss @@ -1,7 +1,13 @@ // Copyright 2020 Google LLC -[shader("miss")] -void main([[vk::location(0)]] in float3 hitValue) +struct Payload { - hitValue = float3(0.0, 0.0, 0.2); + [[vk::location(0)]] float3 hitValue; + [[vk::location(1)]] bool shadowed; +}; + +[shader("miss")] +void main(inout Payload payload) +{ + payload.hitValue = float3(0.0, 0.0, 0.2); } \ No newline at end of file diff --git a/shaders/hlsl/raytracingshadows/miss.rmiss.spv b/shaders/hlsl/raytracingshadows/miss.rmiss.spv index fbbb81219decaa52c8abc45cd3c487e50cca65c5..999bf108d81774d293519ea502354c45948ca29d 100644 GIT binary patch literal 460 zcmYk1&1%9>5QWFY#Glqb6uT>yF73JqrLGHhkydaUE=Fp=s#sfb<u3-S%$_4+W< Ji#wp|5l6{GBpLt! literal 672 zcmY+B-Acni5QV2n(`wb$THE>uCU~jzQUtO103w2r+pvge!4?U%;GHkmJADK3IlS@b zJGD#WhRK|hb7pq38wLkevyz$BE#a-zw7}js&1zP$>#H=oOtaiQXXD)63`TcyhBKe4 z8W!0Uukg?fMiN4vh&&0599ai^KNzR(;ptX-WaWSU$f~x>_eYnzVQ=E{-oo9+OB;&` zt;0G0n_$7qXqv(6+Fx~^SHSXk&QIG?feWt}_%b*y@EZ8Mz*oR81-=T_0KWg4I0JZI zhkg^DH^4tchqKbYF(FO3NoaY#0e*YE=y%auV0rZaN8FD%$3Lr4oxCx=Hu(cVdW%82 z5QV3SiB(&JRO+rM?tB0dimnAWse;>ZX`}|Miodw>>G~Rd4mU!-BN2MT z+%t3TIWrUAYZoneFte&f+&VS$>|8S|TWFJbnv90yWaCcBZsR_)zNRz6T@j%l@PC;2Qbo$yVAKw`~m5PAwd8D literal 608 zcmY+B-AV#c6opsnXjy5d_5&3o-4%ElK@>ef1cA45$S5zgn8N6;m+LK}=jf)NZ9A zz2P+;h2hvjktZQf3L+}n&VaQd5$lGUvqo~>;Yo`RdWW2yaxRy zBCmsgh!!TFd%&GJH;9x;*U2B;tM{6Cfa>6Bla1}!B>Ow;>A-i%*1Uf*>*2e1pCfMF u8Y51Z`+U-OgKV#B{14pk6z-negzL{Z*P406o-_J(z}BtVZ~nze$HXrp>nEH5