TiledDeferredLighting.bsl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. #include "$ENGINE$\GBufferInput.bslinc"
  2. #include "$ENGINE$\PerCameraData.bslinc"
  3. #define USE_COMPUTE_INDICES
  4. #include "$ENGINE$\LightingCommon.bslinc"
  5. #include "$ENGINE$\ReflectionCubemapCommon.bslinc"
  6. #include "$ENGINE$\ImageBasedLighting.bslinc"
  7. Technique
  8. : inherits("GBufferInput")
  9. : inherits("PerCameraData")
  10. : inherits("LightingCommon")
  11. : inherits("ReflectionCubemapCommon")
  12. : inherits("ImageBasedLighting") =
  13. {
  14. Language = "HLSL11";
  15. Pass =
  16. {
  17. Compute =
  18. {
  19. cbuffer Params : register(b0)
  20. {
  21. // Offsets at which specific light types begin in gLights buffer
  22. // Assumed directional lights start at 0
  23. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  24. uint3 gLightOffsets;
  25. uint2 gFramebufferSize;
  26. }
  27. #if MSAA_COUNT > 1
  28. RWBuffer<float4> gOutput : register(u0);
  29. uint getLinearAddress(uint2 coord, uint sampleIndex)
  30. {
  31. return (coord.y * gFramebufferSize.x + coord.x) * MSAA_COUNT + sampleIndex;
  32. }
  33. void writeBufferSample(uint2 coord, uint sampleIndex, float4 color)
  34. {
  35. uint idx = getLinearAddress(coord, sampleIndex);
  36. gOutput[idx] = color;
  37. }
  38. #else
  39. RWTexture2D<float4> gOutput : register(u0);
  40. #endif
  41. groupshared uint sTileMinZ;
  42. groupshared uint sTileMaxZ;
  43. groupshared uint sNumLightsPerType[2];
  44. groupshared uint sTotalNumLights;
  45. float4 getLighting(float2 clipSpacePos, SurfaceData surfaceData)
  46. {
  47. // x, y are now in clip space, z, w are in view space
  48. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  49. // z, w eliminated (since they are already in view space)
  50. // Note: Multiply by depth should be avoided if using ortographic projection
  51. float4 mixedSpacePos = float4(clipSpacePos * -surfaceData.depth, surfaceData.depth, 1);
  52. float4 worldPosition4D = mul(gMatScreenToWorld, mixedSpacePos);
  53. float3 worldPosition = worldPosition4D.xyz / worldPosition4D.w;
  54. uint4 lightOffsets;
  55. lightOffsets.x = gLightOffsets[0];
  56. lightOffsets.y = 0;
  57. lightOffsets.z = sNumLightsPerType[0];
  58. lightOffsets.w = sTotalNumLights;
  59. float3 V = normalize(gViewOrigin - worldPosition);
  60. float3 N = surfaceData.worldNormal.xyz;
  61. float3 R = 2 * dot(V, N) * N - V;
  62. float3 specR = getSpecularDominantDir(N, R, surfaceData.roughness);
  63. return getDirectLighting(worldPosition, V, specR, surfaceData, lightOffsets);
  64. }
  65. [numthreads(TILE_SIZE, TILE_SIZE, 1)]
  66. void main(
  67. uint3 groupId : SV_GroupID,
  68. uint3 groupThreadId : SV_GroupThreadID,
  69. uint3 dispatchThreadId : SV_DispatchThreadID)
  70. {
  71. uint threadIndex = groupThreadId.y * TILE_SIZE + groupThreadId.x;
  72. uint2 pixelPos = dispatchThreadId.xy + gViewportRectangle.xy;
  73. // Note: To improve performance perhaps:
  74. // - Use halfZ (split depth range into two regions for better culling)
  75. // - Use parallel reduction instead of atomics
  76. // - Use AABB instead of frustum (no false positives)
  77. // - Increase tile size to 32x32 to amortize the cost of AABB calc (2x if using halfZ)
  78. // Get data for all samples, and determine per-pixel minimum and maximum depth values
  79. SurfaceData surfaceData[MSAA_COUNT];
  80. uint sampleMinZ = 0x7F7FFFFF;
  81. uint sampleMaxZ = 0;
  82. #if MSAA_COUNT > 1
  83. [unroll]
  84. for(uint i = 0; i < MSAA_COUNT; ++i)
  85. {
  86. surfaceData[i] = getGBufferData(pixelPos, i);
  87. sampleMinZ = min(sampleMinZ, asuint(-surfaceData[i].depth));
  88. sampleMaxZ = max(sampleMaxZ, asuint(-surfaceData[i].depth));
  89. }
  90. #else
  91. surfaceData[0] = getGBufferData(pixelPos);
  92. sampleMinZ = asuint(-surfaceData[0].depth);
  93. sampleMaxZ = asuint(-surfaceData[0].depth);
  94. #endif
  95. // Set initial values
  96. if(threadIndex == 0)
  97. {
  98. sTileMinZ = 0x7F7FFFFF;
  99. sTileMaxZ = 0;
  100. sNumLightsPerType[0] = 0;
  101. sNumLightsPerType[1] = 0;
  102. sTotalNumLights = 0;
  103. }
  104. GroupMemoryBarrierWithGroupSync();
  105. // Determine minimum and maximum depth values for a tile
  106. InterlockedMin(sTileMinZ, sampleMinZ);
  107. InterlockedMax(sTileMaxZ, sampleMaxZ);
  108. GroupMemoryBarrierWithGroupSync();
  109. float minTileZ = asfloat(sTileMinZ);
  110. float maxTileZ = asfloat(sTileMaxZ);
  111. // Create a frustum for the current tile
  112. // First determine a scale of the tile compared to the viewport
  113. float2 tileScale = gViewportRectangle.zw * rcp(float2(TILE_SIZE, TILE_SIZE));
  114. // Now we need to use that scale to scale down the frustum.
  115. // Assume a projection matrix:
  116. // A, 0, C, 0
  117. // 0, B, D, 0
  118. // 0, 0, Q, QN
  119. // 0, 0, -1, 0
  120. //
  121. // Where A is = 2*n / (r - l)
  122. // and C = (r + l) / (r - l)
  123. //
  124. // Q & QN are used for Z value which we don't need to scale. B & D are equivalent for the
  125. // Y value, we'll only consider the X values (A & C) from now on.
  126. //
  127. // Both and A and C are inversely proportional to the size of the frustum (r - l). Larger scale mean that
  128. // tiles are that much smaller than the viewport. This means as our scale increases, (r - l) decreases,
  129. // which means A & C as a whole increase. Therefore:
  130. // A' = A * tileScale.x
  131. // C' = C * tileScale.x
  132. // Aside from scaling, we also need to offset the frustum to the center of the tile.
  133. // For this we calculate the bias value which we add to the C & D factors (which control
  134. // the offset in the projection matrix).
  135. float2 tileBias = tileScale - 1 - groupId.xy * 2;
  136. // This will yield a bias ranging from [-(tileScale - 1), tileScale - 1]. Every second bias is skipped as
  137. // corresponds to a point in-between two tiles, overlapping existing frustums.
  138. float At = gMatProj[0][0] * tileScale.x;
  139. float Ctt = gMatProj[0][2] * tileScale.x - tileBias.x;
  140. float Bt = gMatProj[1][1] * tileScale.y;
  141. float Dtt = gMatProj[1][2] * tileScale.y + tileBias.y;
  142. // Extract left/right/top/bottom frustum planes from scaled projection matrix
  143. // Note: Do this on the CPU? Since they're shared among all entries in a tile. Plus they don't change across frames.
  144. float4 frustumPlanes[6];
  145. frustumPlanes[0] = float4(At, 0.0f, gMatProj[3][2] + Ctt, 0.0f);
  146. frustumPlanes[1] = float4(-At, 0.0f, gMatProj[3][2] - Ctt, 0.0f);
  147. frustumPlanes[2] = float4(0.0f, -Bt, gMatProj[3][2] - Dtt, 0.0f);
  148. frustumPlanes[3] = float4(0.0f, Bt, gMatProj[3][2] + Dtt, 0.0f);
  149. // Normalize
  150. [unroll]
  151. for (uint i = 0; i < 4; ++i)
  152. frustumPlanes[i] *= rcp(length(frustumPlanes[i].xyz));
  153. // Generate near/far frustum planes
  154. // Note: d gets negated in plane equation, this is why its in opposite direction than it intuitively should be
  155. frustumPlanes[4] = float4(0.0f, 0.0f, -1.0f, -minTileZ);
  156. frustumPlanes[5] = float4(0.0f, 0.0f, 1.0f, maxTileZ);
  157. // Find radial & spot lights overlapping the tile
  158. for(uint type = 0; type < 2; type++)
  159. {
  160. uint lightOffset = threadIndex + gLightOffsets[type];
  161. uint lightsEnd = gLightOffsets[type + 1];
  162. for (uint i = lightOffset; i < lightsEnd && i < MAX_LIGHTS; i += TILE_SIZE)
  163. {
  164. float4 lightPosition = mul(gMatView, float4(gLights[i].position, 1.0f));
  165. float lightRadius = gLights[i].attRadius;
  166. // Note: The cull method can have false positives. In case of large light bounds and small tiles, it
  167. // can end up being quite a lot. Consider adding an extra heuristic to check a separating plane.
  168. bool lightInTile = true;
  169. // First check side planes as this will cull majority of the lights
  170. [unroll]
  171. for (uint j = 0; j < 4; ++j)
  172. {
  173. float dist = dot(frustumPlanes[j], lightPosition);
  174. lightInTile = lightInTile && (dist >= -lightRadius);
  175. }
  176. // Make sure to do an actual branch, since it's quite likely an entire warp will have the same value
  177. [branch]
  178. if (lightInTile)
  179. {
  180. bool inDepthRange = true;
  181. // Check near/far planes
  182. [unroll]
  183. for (uint j = 4; j < 6; ++j)
  184. {
  185. float dist = dot(frustumPlanes[j], lightPosition);
  186. inDepthRange = inDepthRange && (dist >= -lightRadius);
  187. }
  188. // In tile, add to branch
  189. [branch]
  190. if (inDepthRange)
  191. {
  192. InterlockedAdd(sNumLightsPerType[type], 1U);
  193. uint idx;
  194. InterlockedAdd(sTotalNumLights, 1U, idx);
  195. gLightIndices[idx] = i;
  196. }
  197. }
  198. }
  199. }
  200. GroupMemoryBarrierWithGroupSync();
  201. // Generate world position
  202. float2 screenUv = ((float2)(gViewportRectangle.xy + pixelPos) + 0.5f) / (float2)gViewportRectangle.zw;
  203. float2 clipSpacePos = (screenUv - gClipToUVScaleOffset.zw) / gClipToUVScaleOffset.xy;
  204. uint2 viewportMax = gViewportRectangle.xy + gViewportRectangle.zw;
  205. // Ignore pixels out of valid range
  206. if (all(dispatchThreadId.xy < viewportMax))
  207. {
  208. #if MSAA_COUNT > 1
  209. float4 lighting = getLighting(clipSpacePos.xy, surfaceData[0]);
  210. writeBufferSample(pixelPos, 0, lighting);
  211. bool doPerSampleShading = needsPerSampleShading(surfaceData);
  212. if(doPerSampleShading)
  213. {
  214. [unroll]
  215. for(uint i = 1; i < MSAA_COUNT; ++i)
  216. {
  217. lighting = getLighting(clipSpacePos.xy, surfaceData[i]);
  218. writeBufferSample(pixelPos, i, lighting);
  219. }
  220. }
  221. else // Splat same information to all samples
  222. {
  223. [unroll]
  224. for(uint i = 1; i < MSAA_COUNT; ++i)
  225. writeBufferSample(pixelPos, i, lighting);
  226. }
  227. #else
  228. float4 lighting = getLighting(clipSpacePos.xy, surfaceData[0]);
  229. gOutput[pixelPos] = lighting;
  230. #endif
  231. }
  232. }
  233. };
  234. };
  235. };
  236. Technique
  237. : inherits("SurfaceData")
  238. : inherits("PerCameraData")
  239. : inherits("LightingCommon") =
  240. {
  241. Language = "GLSL";
  242. Pass =
  243. {
  244. Compute =
  245. {
  246. // Arbitrary limit, increase if needed
  247. #define MAX_LIGHTS 512
  248. layout (local_size_x = TILE_SIZE, local_size_y = TILE_SIZE) in;
  249. #if MSAA_COUNT > 1
  250. layout(binding = 1) uniform sampler2DMS gGBufferATex;
  251. layout(binding = 2) uniform sampler2DMS gGBufferBTex;
  252. layout(binding = 3) uniform sampler2DMS gGBufferCTex;
  253. layout(binding = 4) uniform sampler2DMS gDepthBufferTex;
  254. #else
  255. layout(binding = 1) uniform sampler2D gGBufferATex;
  256. layout(binding = 2) uniform sampler2D gGBufferBTex;
  257. layout(binding = 3) uniform sampler2D gGBufferCTex;
  258. layout(binding = 4) uniform sampler2D gDepthBufferTex;
  259. #endif
  260. SurfaceData decodeGBuffer(vec4 GBufferAData, vec4 GBufferBData, vec2 GBufferCData, float deviceZ)
  261. {
  262. SurfaceData surfaceData;
  263. surfaceData.albedo.xyz = GBufferAData.xyz;
  264. surfaceData.albedo.w = 1.0f;
  265. surfaceData.worldNormal = GBufferBData * vec4(2, 2, 2, 1) - vec4(1, 1, 1, 0);
  266. surfaceData.worldNormal.xyz = normalize(surfaceData.worldNormal.xyz);
  267. surfaceData.depth = convertFromDeviceZ(deviceZ);
  268. surfaceData.roughness = GBufferCData.x;
  269. surfaceData.metalness = GBufferCData.y;
  270. return surfaceData;
  271. }
  272. #if MSAA_COUNT > 1
  273. layout(binding = 5, rgba16f) uniform image2DMS gOutput;
  274. bool needsPerSampleShading(SurfaceData samples[MSAA_COUNT])
  275. {
  276. vec3 albedo = samples[0].albedo.xyz;
  277. vec3 normal = samples[0].worldNormal.xyz;
  278. float depth = samples[0].depth;
  279. for(int i = 1; i < MSAA_COUNT; i++)
  280. {
  281. vec3 otherAlbedo = samples[i].albedo.xyz;
  282. vec3 otherNormal = samples[i].worldNormal.xyz;
  283. float otherDepth = samples[i].depth;
  284. if(abs(depth - otherDepth) > 0.1f || abs(dot(abs(normal - otherNormal), vec3(1, 1, 1))) > 0.1f || abs(dot(albedo - otherAlbedo, vec3(1, 1, 1))) > 0.1f)
  285. {
  286. return true;
  287. }
  288. }
  289. return false;
  290. }
  291. SurfaceData getGBufferData(ivec2 pixelPos, int sampleIndex)
  292. {
  293. vec4 GBufferAData = texelFetch(gGBufferATex, pixelPos, sampleIndex);
  294. vec4 GBufferBData = texelFetch(gGBufferBTex, pixelPos, sampleIndex);
  295. vec2 GBufferCData = texelFetch(gGBufferCTex, pixelPos, sampleIndex).rg;
  296. float deviceZ = texelFetch(gDepthBufferTex, pixelPos, sampleIndex).r;
  297. return decodeGBuffer(GBufferAData, GBufferBData, GBufferCData, deviceZ);
  298. }
  299. #else
  300. layout(binding = 5, rgba16f) uniform image2D gOutput;
  301. SurfaceData getGBufferData(ivec2 pixelPos)
  302. {
  303. vec4 GBufferAData = texelFetch(gGBufferATex, pixelPos, 0);
  304. vec4 GBufferBData = texelFetch(gGBufferBTex, pixelPos, 0);
  305. vec2 GBufferCData = texelFetch(gGBufferCTex, pixelPos, 0).rg;
  306. float deviceZ = texelFetch(gDepthBufferTex, pixelPos, 0).r;
  307. return decodeGBuffer(GBufferAData, GBufferBData, GBufferCData, deviceZ);
  308. }
  309. #endif
  310. layout(std430, binding = 6) readonly buffer gLights
  311. {
  312. LightData gLightsData[];
  313. };
  314. layout(binding = 7, std140) uniform Params
  315. {
  316. // Offsets at which specific light types begin in gLights buffer
  317. // Assumed directional lights start at 0
  318. // x - offset to point lights, y - offset to spot lights, z - total number of lights
  319. uvec3 gLightOffsets;
  320. uvec2 gFramebufferSize;
  321. };
  322. shared uint sTileMinZ;
  323. shared uint sTileMaxZ;
  324. shared uint sNumLightsPerType[2];
  325. shared uint sTotalNumLights;
  326. shared uint sLightIndices[MAX_LIGHTS];
  327. vec4 getLighting(vec2 clipSpacePos, SurfaceData surfaceData)
  328. {
  329. // x, y are now in clip space, z, w are in view space
  330. // We multiply them by a special inverse view-projection matrix, that had the projection entries that effect
  331. // z, w eliminated (since they are already in view space)
  332. // Note: Multiply by depth should be avoided if using ortographic projection
  333. vec4 mixedSpacePos = vec4(clipSpacePos.xy * -surfaceData.depth, surfaceData.depth, 1);
  334. vec4 worldPosition4D = gMatScreenToWorld * mixedSpacePos;
  335. vec3 worldPosition = worldPosition4D.xyz / worldPosition4D.w;
  336. float alpha = 0.0f;
  337. vec3 lightAccumulator = vec3(0, 0, 0);
  338. if(surfaceData.worldNormal.w > 0.0f)
  339. {
  340. for(uint i = 0; i < gLightOffsets[0]; ++i)
  341. {
  342. LightData lightData = gLightsData[i];
  343. lightAccumulator += getDirLightContibution(surfaceData, lightData);
  344. }
  345. for (uint i = 0; i < sNumLightsPerType[0]; ++i)
  346. {
  347. uint lightIdx = sLightIndices[i];
  348. LightData lightData = gLightsData[lightIdx];
  349. lightAccumulator += getPointLightContribution(worldPosition, surfaceData, lightData);
  350. }
  351. for(uint i = sNumLightsPerType[0]; i < sTotalNumLights; ++i)
  352. {
  353. uint lightIdx = sLightIndices[i];
  354. LightData lightData = gLightsData[lightIdx];
  355. lightAccumulator += getSpotLightContribution(worldPosition, surfaceData, lightData);
  356. }
  357. lightAccumulator += surfaceData.albedo.rgb * gAmbientFactor;
  358. alpha = 1.0f;
  359. }
  360. vec3 diffuse = surfaceData.albedo.xyz / PI; // TODO - Add better lighting model later
  361. return vec4(lightAccumulator * diffuse, alpha);
  362. }
  363. void main()
  364. {
  365. uint threadIndex = gl_LocalInvocationID.y * TILE_SIZE + gl_LocalInvocationID.x;
  366. ivec2 pixelPos = ivec2(gl_GlobalInvocationID.xy) + gViewportRectangle.xy;
  367. // Get data for all samples, and determine per-pixel minimum and maximum depth values
  368. SurfaceData surfaceData[MSAA_COUNT];
  369. uint sampleMinZ = 0x7F7FFFFF;
  370. uint sampleMaxZ = 0;
  371. #if MSAA_COUNT > 1
  372. for(int i = 0; i < MSAA_COUNT; ++i)
  373. {
  374. surfaceData[i] = getGBufferData(pixelPos, i);
  375. sampleMinZ = min(sampleMinZ, floatBitsToUint(-surfaceData[i].depth));
  376. sampleMaxZ = max(sampleMaxZ, floatBitsToUint(-surfaceData[i].depth));
  377. }
  378. #else
  379. surfaceData[0] = getGBufferData(pixelPos);
  380. sampleMinZ = floatBitsToUint(-surfaceData[0].depth);
  381. sampleMaxZ = floatBitsToUint(-surfaceData[0].depth);
  382. #endif
  383. // Set initial values
  384. if(threadIndex == 0)
  385. {
  386. sTileMinZ = 0x7F7FFFFF;
  387. sTileMaxZ = 0;
  388. sNumLightsPerType[0] = 0;
  389. sNumLightsPerType[1] = 0;
  390. sTotalNumLights = 0;
  391. }
  392. groupMemoryBarrier();
  393. barrier();
  394. atomicMin(sTileMinZ, sampleMinZ);
  395. atomicMax(sTileMaxZ, sampleMaxZ);
  396. groupMemoryBarrier();
  397. barrier();
  398. float minTileZ = uintBitsToFloat(sTileMinZ);
  399. float maxTileZ = uintBitsToFloat(sTileMaxZ);
  400. // Create a frustum for the current tile
  401. // See HLSL version for an explanation of the math
  402. vec2 tileScale = gViewportRectangle.zw / vec2(TILE_SIZE, TILE_SIZE);
  403. vec2 tileBias = tileScale - 1 - gl_WorkGroupID.xy * 2;
  404. float At = gMatProj[0][0] * tileScale.x;
  405. float Ctt = gMatProj[2][0] * tileScale.x - tileBias.x;
  406. float Bt = gMatProj[1][1] * tileScale.y;
  407. float Dtt = gMatProj[2][1] * tileScale.y + tileBias.y;
  408. // Extract left/right/top/bottom frustum planes from scaled projection matrix
  409. vec4 frustumPlanes[6];
  410. frustumPlanes[0] = vec4(At, 0.0f, gMatProj[2][3] + Ctt, 0.0f);
  411. frustumPlanes[1] = vec4(-At, 0.0f, gMatProj[2][3] - Ctt, 0.0f);
  412. frustumPlanes[2] = vec4(0.0f, -Bt, gMatProj[2][3] - Dtt, 0.0f);
  413. frustumPlanes[3] = vec4(0.0f, Bt, gMatProj[2][3] + Dtt, 0.0f);
  414. // Normalize
  415. for (uint i = 0; i < 4; ++i)
  416. frustumPlanes[i] /= length(frustumPlanes[i].xyz);
  417. // Generate near/far frustum planes
  418. frustumPlanes[4] = vec4(0.0f, 0.0f, -1.0f, -minTileZ);
  419. frustumPlanes[5] = vec4(0.0f, 0.0f, 1.0f, maxTileZ);
  420. // Find radial & spot lights overlapping the tile
  421. for(uint type = 0; type < 2; type++)
  422. {
  423. uint lightOffset = threadIndex + gLightOffsets[type];
  424. uint lightsEnd = gLightOffsets[type + 1];
  425. for (uint i = lightOffset; i < lightsEnd && i < MAX_LIGHTS; i += TILE_SIZE)
  426. {
  427. LightData lightData = gLightsData[i];
  428. vec4 lightPosition = gMatView * vec4(lightData.position, 1.0f);
  429. float lightRadius = lightData.attRadius;
  430. bool lightInTile = true;
  431. // First check side planes as this will cull majority of the lights
  432. for (uint j = 0; j < 4; ++j)
  433. {
  434. float dist = dot(frustumPlanes[j], lightPosition);
  435. lightInTile = lightInTile && (dist >= -lightRadius);
  436. }
  437. if (lightInTile)
  438. {
  439. bool inDepthRange = true;
  440. // Check near/far planes
  441. for (uint j = 4; j < 6; ++j)
  442. {
  443. float dist = dot(frustumPlanes[j], lightPosition);
  444. inDepthRange = inDepthRange && (dist >= -lightRadius);
  445. }
  446. // In tile, add to branch
  447. if (inDepthRange)
  448. {
  449. atomicAdd(sNumLightsPerType[type], 1U);
  450. uint idx = atomicAdd(sTotalNumLights, 1U);
  451. sLightIndices[idx] = i;
  452. }
  453. }
  454. }
  455. }
  456. groupMemoryBarrier();
  457. barrier();
  458. vec2 screenUv = (vec2(gViewportRectangle.xy + pixelPos) + 0.5f) / vec2(gViewportRectangle.zw);
  459. vec2 clipSpacePos = (screenUv - gClipToUVScaleOffset.zw) / gClipToUVScaleOffset.xy;
  460. uvec2 viewportMax = gViewportRectangle.xy + gViewportRectangle.zw;
  461. // Ignore pixels out of valid range
  462. if (all(lessThan(gl_GlobalInvocationID.xy, viewportMax)))
  463. {
  464. #if MSAA_COUNT > 1
  465. vec4 lighting = getLighting(clipSpacePos.xy, surfaceData[0]);
  466. imageStore(gOutput, pixelPos, 0, lighting);
  467. bool doPerSampleShading = needsPerSampleShading(surfaceData);
  468. if(doPerSampleShading)
  469. {
  470. for(int i = 1; i < MSAA_COUNT; ++i)
  471. {
  472. lighting = getLighting(clipSpacePos.xy, surfaceData[i]);
  473. imageStore(gOutput, pixelPos, i, lighting);
  474. }
  475. }
  476. else // Splat same information to all samples
  477. {
  478. for(int i = 1; i < MSAA_COUNT; ++i)
  479. imageStore(gOutput, pixelPos, i, lighting);
  480. }
  481. #else
  482. vec4 lighting = getLighting(clipSpacePos.xy, surfaceData[0]);
  483. imageStore(gOutput, pixelPos, lighting);
  484. #endif
  485. }
  486. }
  487. };
  488. };
  489. };