Advertisement
Zgragselus

VoxelMipmap3.hlsl

Feb 10th, 2022
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. cbuffer InputDimensions : register(b0)
  2. {
  3. uint3 dimensions;
  4. }
  5.  
  6. cbuffer InputMiplevels : register(b1)
  7. {
  8. uint srcMiplevel;
  9. uint miplevels;
  10. float texelSize;
  11. }
  12.  
  13. SamplerState srcSampler : register(s0);
  14. Texture3D<float4> srcLevel : register(t0);
  15. RWTexture3D<float4> mipLevel1 : register(u0);
  16. RWTexture3D<float4> mipLevel2 : register(u1);
  17. RWTexture3D<float4> mipLevel3 : register(u2);
  18.  
  19. groupshared float4 tmp[512];
  20.  
  21. void StoreColor(uint idx, float4 color)
  22. {
  23. tmp[idx] = color;
  24. }
  25.  
  26. float4 LoadColor(uint idx)
  27. {
  28. return tmp[idx];
  29. }
  30.  
  31. float HasVoxel(float4 color)
  32. {
  33. return color.a > 0.0f ? 1.0f : 0.0f;
  34. }
  35.  
  36. // Naive version to generate two mipmap levels at most from the source one
  37. //
  38. // Runs in 4x4x4 workgroup
  39. [numthreads(8, 8, 8)]
  40. void GenerateMipmaps(uint GI : SV_GroupIndex, uint3 DTid : SV_DispatchThreadID)
  41. {
  42. // Each thread in workgroup loads single voxel and stores it on groupshared memory
  43. float4 src0;
  44. float4 src1;
  45. float4 src2;
  46. float4 src3;
  47. float4 src4;
  48. float4 src5;
  49. float4 src6;
  50. float4 src7;
  51. float3 uvw = (DTid.xyz + 0.5f) * texelSize;
  52. src0 = srcLevel.SampleLevel(srcSampler, uvw, (float)srcMiplevel);
  53. StoreColor(GI, src0);
  54. GroupMemoryBarrierWithGroupSync();
  55.  
  56. // 001001001b
  57. if ((GI & 0x49) == 0)
  58. {
  59. src1 = LoadColor(GI + 0x01);
  60. src2 = LoadColor(GI + 0x08);
  61. src3 = LoadColor(GI + 0x09);
  62. src4 = LoadColor(GI + 0x40);
  63. src5 = LoadColor(GI + 0x41);
  64. src6 = LoadColor(GI + 0x48);
  65. src7 = LoadColor(GI + 0x49);
  66.  
  67. // Perform mipmapping function
  68. float div = HasVoxel(src0) + HasVoxel(src1) + HasVoxel(src2) + HasVoxel(src3) + HasVoxel(src4) + HasVoxel(src5) + HasVoxel(src6) + HasVoxel(src7);
  69.  
  70. if (div == 0.0f)
  71. {
  72. src0 = 0.0f;
  73. }
  74. else
  75. {
  76. src0 = (src0 + src1 + src2 + src3 + src4 + src5 + src6 + src7) / div;
  77. }
  78.  
  79. // Store value + write into shared memory
  80. mipLevel1[DTid / 2] = src0;
  81. StoreColor(GI, src0);
  82. }
  83.  
  84. // If we are going to generate just 1 mipmap, return here
  85. if (miplevels == 1)
  86. {
  87. return;
  88. }
  89.  
  90. GroupMemoryBarrierWithGroupSync();
  91.  
  92. // 011011011b
  93. if ((GI & 0xDB) == 0)
  94. {
  95. // Load all 8 colors from shared memory
  96. src1 = LoadColor(GI + 0x02);
  97. src2 = LoadColor(GI + 0x10);
  98. src3 = LoadColor(GI + 0x12);
  99. src4 = LoadColor(GI + 0x80);
  100. src5 = LoadColor(GI + 0x82);
  101. src6 = LoadColor(GI + 0x90);
  102. src7 = LoadColor(GI + 0x92);
  103.  
  104. // Perform mipmapping function
  105. float div = HasVoxel(src0) + HasVoxel(src1) + HasVoxel(src2) + HasVoxel(src3) + HasVoxel(src4) + HasVoxel(src5) + HasVoxel(src6) + HasVoxel(src7);
  106.  
  107. if (div == 0.0f)
  108. {
  109. src0 = 0.0f;
  110. }
  111. else
  112. {
  113. src0 = (src0 + src1 + src2 + src3 + src4 + src5 + src6 + src7) / div;
  114. }
  115.  
  116. // Store value + write into shared memory
  117. mipLevel2[DTid / 4] = src0;
  118. StoreColor(GI, src0);
  119. }
  120.  
  121. // If we are going to generate just 1 mipmap, return here
  122. if (miplevels == 2)
  123. {
  124. return;
  125. }
  126.  
  127. GroupMemoryBarrierWithGroupSync();
  128.  
  129. // For first thread in workgroup only
  130. if (GI == 0)
  131. {
  132. // Load all 8 colors from shared memory
  133. src1 = LoadColor(GI + 0x04);
  134. src2 = LoadColor(GI + 0x20);
  135. src3 = LoadColor(GI + 0x24);
  136. src4 = LoadColor(GI + 0x100);
  137. src5 = LoadColor(GI + 0x104);
  138. src6 = LoadColor(GI + 0x120);
  139. src7 = LoadColor(GI + 0x124);
  140.  
  141. // Perform mipmapping function
  142. float div = HasVoxel(src0) + HasVoxel(src1) + HasVoxel(src2) + HasVoxel(src3) + HasVoxel(src4) + HasVoxel(src5) + HasVoxel(src6) + HasVoxel(src7);
  143.  
  144. if (div == 0.0f)
  145. {
  146. src0 = 0.0f;
  147. }
  148. else
  149. {
  150. src0 = (src0 + src1 + src2 + src3 + src4 + src5 + src6 + src7) / div;
  151. }
  152.  
  153. // Store value
  154. mipLevel3[DTid / 8] = src0;
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement