TiledDeferredLighting.bsl 19 KB

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