cs_assao_postprocess_importance_map_b.sc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2018 Attila Kocsis. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "bgfx_compute.sh"
  6. #include "uniforms.sh"
  7. IMAGE2D_WO(s_target, r8, 0);
  8. SAMPLER2D(s_importanceMap, 1);
  9. BUFFER_RW(s_loadCounter, uint, 2);
  10. CONST(float cSmoothenImportance) = 1.0;
  11. // Shaders below only needed for adaptive quality level
  12. NUM_THREADS(8, 8, 1)
  13. void main()
  14. {
  15. uvec2 dtID = uvec2(gl_GlobalInvocationID.xy);
  16. uvec2 dim = imageSize(s_target).xy;
  17. if (all(lessThan(dtID.xy, dim) ) )
  18. {
  19. vec2 inUV = (dtID.xy+vec2(0.5,0.5)) * u_quarterResPixelSize;
  20. float centre = texture2DLod(s_importanceMap, inUV, 0.0 ).x;
  21. //return centre;
  22. vec2 halfPixel = u_quarterResPixelSize * 0.5f;
  23. vec4 vals;
  24. vals.x = texture2DLod(s_importanceMap, inUV + vec2( -halfPixel.x, -halfPixel.y * 3 ), 0.0 ).x;
  25. vals.y = texture2DLod(s_importanceMap, inUV + vec2( +halfPixel.x * 3, -halfPixel.y ), 0.0 ).x;
  26. vals.z = texture2DLod(s_importanceMap, inUV + vec2( +halfPixel.x, +halfPixel.y * 3 ), 0.0 ).x;
  27. vals.w = texture2DLod(s_importanceMap, inUV + vec2( -halfPixel.x * 3, +halfPixel.y ), 0.0 ).x;
  28. float avgVal = dot( vals, vec4( 0.25, 0.25, 0.25, 0.25 ) );
  29. vals.xy = max( vals.xy, vals.zw );
  30. float maxVal = max( centre, max( vals.x, vals.y ) );
  31. float retVal = mix( maxVal, avgVal, cSmoothenImportance );
  32. // sum the average; to avoid overflowing we assume max AO resolution is not bigger than 16384x16384; so quarter res (used here) will be 4096x4096, which leaves us with 8 bits per pixel
  33. uint sum = uint(saturate(retVal) * 255.0 + 0.5);
  34. // save every 9th to avoid InterlockedAdd congestion - since we're blurring, this is good enough; compensated by multiplying LoadCounterAvgDiv by 9
  35. #if BGFX_SHADER_LANGUAGE_GLSL
  36. if( ((dtID.x % 3) + ((dim.y-1-dtID.y) % 3)) == 0 )
  37. #else
  38. if( ((dtID.x % 3) + (dtID.y % 3)) == 0 )
  39. #endif
  40. atomicAdd(s_loadCounter[0], sum );
  41. imageStore(s_target, ivec2(dtID.xy), retVal.xxxx);
  42. }
  43. }