bc4.glsl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #[versions]
  2. unsigned = "";
  3. signed = "#define SNORM";
  4. #[compute]
  5. #version 450
  6. #include "CrossPlatformSettings_piece_all.glsl"
  7. #include "UavCrossPlatform_piece_all.glsl"
  8. #VERSION_DEFINES
  9. shared float2 g_minMaxValues[4u * 4u * 4u];
  10. shared uint2 g_mask[4u * 4u];
  11. layout(binding = 0) uniform sampler2D srcTex;
  12. layout(binding = 1, rg32ui) uniform restrict writeonly uimage2D dstTexture;
  13. layout(push_constant, std430) uniform Params {
  14. uint p_channelIdx;
  15. uint p_padding[3];
  16. }
  17. params;
  18. layout(local_size_x = 4, //
  19. local_size_y = 4, //
  20. local_size_z = 4) in;
  21. /// Each block is 16 pixels
  22. /// Each thread works on 4 pixels
  23. /// Therefore each block needs 4 threads, generating 8 masks
  24. /// At the end these 8 masks get merged into 2 and results written to output
  25. ///
  26. /// **Q: Why 4 pixels per thread? Why not 1 pixel per thread? Why not 2? Why not 16?**
  27. ///
  28. /// A: It's a sweetspot.
  29. /// - Very short threads cannot fill expensive GPUs with enough work (dispatch bound)
  30. /// - Lots of threads means lots of synchronization (e.g. evaluating min/max, merging masks)
  31. /// overhead, and also more LDS usage which reduces occupancy.
  32. /// - Long threads (e.g. 1 thread per block) misses parallelism opportunities
  33. void main() {
  34. float minVal, maxVal;
  35. float4 srcPixel;
  36. const uint blockThreadId = gl_LocalInvocationID.x;
  37. const uint2 pixelsToLoadBase = gl_GlobalInvocationID.yz << 2u;
  38. for (uint i = 0u; i < 4u; ++i) {
  39. const uint2 pixelsToLoad = pixelsToLoadBase + uint2(i, blockThreadId);
  40. const float4 value = OGRE_Load2D(srcTex, int2(pixelsToLoad), 0).xyzw;
  41. srcPixel[i] = params.p_channelIdx == 0 ? value.x : (params.p_channelIdx == 1 ? value.y : value.w);
  42. srcPixel[i] *= 255.0f;
  43. }
  44. minVal = min3(srcPixel.x, srcPixel.y, srcPixel.z);
  45. maxVal = max3(srcPixel.x, srcPixel.y, srcPixel.z);
  46. minVal = min(minVal, srcPixel.w);
  47. maxVal = max(maxVal, srcPixel.w);
  48. const uint minMaxIdxBase = (gl_LocalInvocationID.z << 4u) + (gl_LocalInvocationID.y << 2u);
  49. const uint maskIdxBase = (gl_LocalInvocationID.z << 2u) + gl_LocalInvocationID.y;
  50. g_minMaxValues[minMaxIdxBase + blockThreadId] = float2(minVal, maxVal);
  51. g_mask[maskIdxBase] = uint2(0u, 0u);
  52. memoryBarrierShared();
  53. barrier();
  54. // Have all 4 threads in the block grab the min/max value by comparing what all 4 threads uploaded
  55. for (uint i = 0u; i < 4u; ++i) {
  56. minVal = min(g_minMaxValues[minMaxIdxBase + i].x, minVal);
  57. maxVal = max(g_minMaxValues[minMaxIdxBase + i].y, maxVal);
  58. }
  59. // determine bias and emit color indices
  60. // given the choice of maxVal/minVal, these indices are optimal:
  61. // http://fgiesen.wordpress.com/2009/12/15/dxt5-alpha-block-index-determination/
  62. float dist = maxVal - minVal;
  63. float dist4 = dist * 4.0f;
  64. float dist2 = dist * 2.0f;
  65. float bias = (dist < 8) ? (dist - 1) : (trunc(dist * 0.5f) + 2);
  66. bias -= minVal * 7;
  67. uint mask0 = 0u, mask1 = 0u;
  68. for (uint i = 0u; i < 4u; ++i) {
  69. float a = srcPixel[i] * 7.0f + bias;
  70. int ind = 0;
  71. // select index. this is a "linear scale" lerp factor between 0 (val=min) and 7 (val=max).
  72. if (a >= dist4) {
  73. ind = 4;
  74. a -= dist4;
  75. }
  76. if (a >= dist2) {
  77. ind += 2;
  78. a -= dist2;
  79. }
  80. if (a >= dist)
  81. ind += 1;
  82. // turn linear scale into DXT index (0/1 are extremal pts)
  83. ind = -ind & 7;
  84. ind ^= (2 > ind) ? 1 : 0;
  85. // write index
  86. const uint bits = 16u + ((blockThreadId << 2u) + i) * 3u;
  87. if (bits < 32u) {
  88. mask0 |= uint(ind) << bits;
  89. if (bits + 3u > 32u) {
  90. mask1 |= uint(ind) >> (32u - bits);
  91. }
  92. } else {
  93. mask1 |= uint(ind) << (bits - 32u);
  94. }
  95. }
  96. if (mask0 != 0u)
  97. atomicOr(g_mask[maskIdxBase].x, mask0);
  98. if (mask1 != 0u)
  99. atomicOr(g_mask[maskIdxBase].y, mask1);
  100. memoryBarrierShared();
  101. barrier();
  102. if (blockThreadId == 0u) {
  103. // Save data
  104. uint2 outputBytes;
  105. #ifdef SNORM
  106. outputBytes.x =
  107. packSnorm4x8(float4(maxVal * (1.0f / 255.0f) * 2.0f - 1.0f,
  108. minVal * (1.0f / 255.0f) * 2.0f - 1.0f, 0.0f, 0.0f));
  109. #else
  110. outputBytes.x = packUnorm4x8(
  111. float4(maxVal * (1.0f / 255.0f), minVal * (1.0f / 255.0f), 0.0f, 0.0f));
  112. #endif
  113. outputBytes.x |= g_mask[maskIdxBase].x;
  114. outputBytes.y = g_mask[maskIdxBase].y;
  115. uint2 dstUV = gl_GlobalInvocationID.yz;
  116. imageStore(dstTexture, int2(dstUV), uint4(outputBytes.xy, 0u, 0u));
  117. }
  118. }