TiledDeferredLighting.bsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "$ENGINE$\GBuffer.bslinc"
  2. #include "$ENGINE$\LightingCommon.bslinc"
  3. Parameters =
  4. {
  5. Sampler2D gGBufferASamp : alias("gGBufferATex");
  6. Sampler2D gGBufferBSamp : alias("gGBufferBTex");
  7. Sampler2D gDepthBufferSamp : alias("gDepthBufferTex");
  8. Texture2D gGBufferATex : auto("GBufferA");
  9. Texture2D gGBufferBTex : auto("GBufferB");
  10. Texture2D gDepthBufferTex : auto("GBufferDepth");
  11. };
  12. Technique
  13. : inherits("GBuffer")
  14. : inherits("LightingCommon") =
  15. {
  16. Language = "HLSL11";
  17. Pass =
  18. {
  19. Compute =
  20. {
  21. SamplerState gGBufferASamp : register(s0);
  22. SamplerState gGBufferBSamp : register(s1);
  23. SamplerState gDepthBufferSamp : register(s2);
  24. Texture2D gGBufferATex : register(t0);
  25. Texture2D gGBufferBTex : register(t1);
  26. Texture2D gDepthBufferTex : register(t2);
  27. SurfaceData decodeGBuffer(float4 GBufferAData, float4 GBufferBData, float deviceZ)
  28. {
  29. SurfaceData output;
  30. output.albedo.xyz = GBufferAData.xyz;
  31. output.albedo.w = 1.0f;
  32. output.worldNormal = GBufferBData * float4(2, 2, 2, 1) - float4(1, 1, 1, 0);
  33. output.worldNormal.xyz = normalize(output.worldNormal.xyz);
  34. output.depth = convertFromDeviceZ(deviceZ);
  35. return output;
  36. }
  37. SurfaceData getGBufferData(float2 uv)
  38. {
  39. float4 GBufferAData = gGBufferATex.SampleLevel(gGBufferASamp, uv, 0);
  40. float4 GBufferBData = gGBufferBTex.SampleLevel(gGBufferBSamp, uv, 0);
  41. float deviceZ = gDepthBufferTex.SampleLevel(gDepthBufferSamp, uv, 0).r;
  42. return decodeGBuffer(GBufferAData, GBufferBData, deviceZ);
  43. }
  44. SurfaceData getGBufferData(uint2 pixelPos)
  45. {
  46. float4 GBufferAData = gGBufferATex.Load(int3(pixelPos, 0));
  47. float4 GBufferBData = gGBufferBTex.Load(int3(pixelPos, 0));
  48. float deviceZ = gDepthBufferTex.Load(int3(pixelPos, 0)).r;
  49. return decodeGBuffer(GBufferAData, GBufferBData, deviceZ);
  50. }
  51. StructuredBuffer<LightData> gDirLights : register(t3);
  52. StructuredBuffer<LightData> gPointLights : register(t4);
  53. StructuredBuffer<LightData> gSpotLights : register(t5);
  54. RWTexture2D<float4> gOutput : register(u0);
  55. cbuffer Params : register(b0)
  56. {
  57. // x - directional, y - point, z - spot
  58. uint3 gNumLightsPerType;
  59. }
  60. [numthreads(TILE_SIZE, TILE_SIZE, 1)]
  61. void main(
  62. uint3 groupId : SV_GroupID,
  63. uint3 groupThreadId : SV_GroupThreadID,
  64. uint3 dispatchThreadId : SV_DispatchThreadID,
  65. uint threadIndex : SV_GroupIndex)
  66. {
  67. uint2 pixelPos = dispatchThreadId.xy + gViewportRectangle.xy;
  68. SurfaceData surfaceData = getGBufferData(pixelPos);
  69. float3 lightAccumulator = 0;
  70. float alpha = 0.0f;
  71. if(surfaceData.worldNormal.w > 0.0f)
  72. {
  73. float2 screenUv = ((float2)(gViewportRectangle.xy + pixelPos) + 0.5f) / (float2)gViewportRectangle.zw;
  74. float2 clipSpacePos = (screenUv - gClipToUVScaleOffset.zw) / gClipToUVScaleOffset.xy;
  75. // x, y are now in clip space, z, w are in view space
  76. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  77. // z, w eliminated (since they are already in view space)
  78. // Note: Multiply by depth should be avoided if using ortographic projection
  79. float4 mixedSpacePos = float4(clipSpacePos.xy * -surfaceData.depth, surfaceData.depth, 1);
  80. float4 worldPosition4D = mul(gMatScreenToWorld, mixedSpacePos);
  81. float3 worldPosition = worldPosition4D.xyz / worldPosition4D.w;
  82. for(uint i = 0; i < gNumLightsPerType.x; i++)
  83. lightAccumulator += getDirLightContibution(surfaceData, gDirLights[i]);
  84. for(uint i = 0; i < gNumLightsPerType.y; i++)
  85. lightAccumulator += getPointLightContribution(worldPosition, surfaceData, gPointLights[i]);
  86. for(uint i = 0; i < gNumLightsPerType.z; i++)
  87. lightAccumulator += getSpotLightContribution(worldPosition, surfaceData, gSpotLights[i]);
  88. alpha = 1.0f;
  89. }
  90. float3 diffuse = surfaceData.albedo.xyz / PI; // TODO - Add better lighting model later
  91. uint2 viewportMax = gViewportRectangle.xy + gViewportRectangle.zw;
  92. // Ignore pixels out of valid range
  93. if (all(dispatchThreadId.xy < viewportMax))
  94. gOutput[pixelPos] = float4(gOutput[pixelPos].xyz + diffuse * lightAccumulator, alpha);
  95. }
  96. };
  97. };
  98. };
  99. Technique
  100. : inherits("GBuffer")
  101. : inherits("LightingCommon") =
  102. {
  103. Language = "GLSL";
  104. Pass =
  105. {
  106. Compute =
  107. {
  108. layout (local_size_x = TILE_SIZE, local_size_y = TILE_SIZE) in;
  109. layout(binding = 0) uniform sampler2D gGBufferATex;
  110. layout(binding = 1) uniform sampler2D gGBufferBTex;
  111. layout(binding = 2) uniform sampler2D gDepthBufferTex;
  112. SurfaceData decodeGBuffer(vec4 GBufferAData, vec4 GBufferBData, float deviceZ)
  113. {
  114. SurfaceData surfaceData;
  115. surfaceData.albedo.xyz = GBufferAData.xyz;
  116. surfaceData.albedo.w = 1.0f;
  117. surfaceData.worldNormal = GBufferBData * vec4(2, 2, 2, 1) - vec4(1, 1, 1, 0);
  118. surfaceData.worldNormal.xyz = normalize(surfaceData.worldNormal.xyz);
  119. surfaceData.depth = convertFromDeviceZ(deviceZ);
  120. return surfaceData;
  121. }
  122. SurfaceData getGBufferData(vec2 uv)
  123. {
  124. vec4 GBufferAData = textureLod(gGBufferATex, uv, 0);
  125. vec4 GBufferBData = textureLod(gGBufferBTex, uv, 0);
  126. float deviceZ = textureLod(gDepthBufferTex, uv, 0).r;
  127. return decodeGBuffer(GBufferAData, GBufferBData, deviceZ);
  128. }
  129. SurfaceData getGBufferData(ivec2 pixelPos)
  130. {
  131. vec4 GBufferAData = texelFetch(gGBufferATex, pixelPos, 0);
  132. vec4 GBufferBData = texelFetch(gGBufferBTex, pixelPos, 0);
  133. float deviceZ = texelFetch(gDepthBufferTex, pixelPos, 0).r;
  134. return decodeGBuffer(GBufferAData, GBufferBData, deviceZ);
  135. }
  136. layout(std430, binding = 3) buffer gDirLights
  137. {
  138. LightData[] gDirLightsData;
  139. };
  140. layout(std430, binding = 4) buffer gPointLights
  141. {
  142. LightData[] gPointLightsData;
  143. };
  144. layout(std430, binding = 5) buffer gSpotLights
  145. {
  146. LightData[] gSpotLightsData;
  147. };
  148. layout(binding = 6, rgba16f) uniform image2D gOutput;
  149. layout(binding = 7, std140) uniform Params
  150. {
  151. // x - directional, y - point, z - spot
  152. uvec3 gNumLightsPerType;
  153. };
  154. void main()
  155. {
  156. ivec2 pixelPos = ivec2(gl_GlobalInvocationID.xy) + gViewportRectangle.xy;
  157. SurfaceData surfaceData = getGBufferData(pixelPos);
  158. float alpha = 0.0f;
  159. vec3 lightAccumulator = vec3(0, 0, 0);
  160. if(surfaceData.worldNormal.w > 0.0f)
  161. {
  162. vec2 screenUv = (vec2(gViewportRectangle.xy + pixelPos) + 0.5f) / vec2(gViewportRectangle.zw);
  163. vec2 clipSpacePos = (screenUv - gClipToUVScaleOffset.zw) / gClipToUVScaleOffset.xy;
  164. // x, y are now in clip space, z, w are in view space
  165. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  166. // z, w eliminated (since they are already in view space)
  167. // Note: Multiply by depth should be avoided if using ortographic projection
  168. vec4 mixedSpacePos = vec4(clipSpacePos.xy * -surfaceData.depth, surfaceData.depth, 1);
  169. vec4 worldPosition4D = gMatScreenToWorld * mixedSpacePos;
  170. vec3 worldPosition = worldPosition4D.xyz / worldPosition4D.w;
  171. for(uint i = 0; i < gNumLightsPerType.x; i++)
  172. lightAccumulator += getDirLightContibution(surfaceData, gDirLightsData[i]);
  173. for(uint i = 0; i < gNumLightsPerType.y; i++)
  174. lightAccumulator += getPointLightContribution(worldPosition, surfaceData, gPointLightsData[i]);
  175. for(uint i = 0; i < gNumLightsPerType.z; i++)
  176. lightAccumulator += getSpotLightContribution(worldPosition, surfaceData, gSpotLightsData[i]);
  177. alpha = 1.0f;
  178. }
  179. vec3 diffuse = surfaceData.albedo.xyz / PI; // TODO - Add better lighting model later
  180. uvec2 viewportMax = gViewportRectangle.xy + gViewportRectangle.zw;
  181. // Ignore pixels out of valid range
  182. if (all(lessThan(gl_GlobalInvocationID.xy, viewportMax)))
  183. {
  184. vec4 existingValue = imageLoad(gOutput, pixelPos);
  185. imageStore(gOutput, pixelPos, vec4(diffuse * lightAccumulator + existingValue.xyz, alpha));
  186. }
  187. }
  188. };
  189. };
  190. };