Advertisement
Zgragselus

VoxelMipmap2.hlsl

Feb 10th, 2022
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 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.  
  18. groupshared float4 tmp[64];
  19.  
  20. void StoreColor(uint idx, float4 color)
  21. {
  22. tmp[idx] = color;
  23. }
  24.  
  25. float4 LoadColor(uint idx)
  26. {
  27. return tmp[idx];
  28. }
  29.  
  30. float HasVoxel(float4 color)
  31. {
  32. return color.a > 0.0f ? 1.0f : 0.0f;
  33. }
  34.  
  35. // Naive version to generate two mipmap levels at most from the source one
  36. //
  37. // Runs in 4x4x4 workgroup
  38. [numthreads(4, 4, 4)]
  39. void GenerateMipmaps(uint GI : SV_GroupIndex, uint3 DTid : SV_DispatchThreadID)
  40. {
  41. // Each thread in workgroup loads single voxel and stores it on groupshared memory
  42. float4 src0;
  43. float4 src1;
  44. float4 src2;
  45. float4 src3;
  46. float4 src4;
  47. float4 src5;
  48. float4 src6;
  49. float4 src7;
  50. float3 uvw = (DTid.xyz + 0.5f) * texelSize;
  51. src0 = srcLevel.SampleLevel(srcSampler, uvw, (float)srcMiplevel);
  52. StoreColor(GI, src0);
  53. GroupMemoryBarrierWithGroupSync();
  54.  
  55. // Every first thread in 2x2x2 sub-group
  56. if ((GI & 0x15) == 0)
  57. {
  58. // Load all 8 colors from shared memory
  59. src1 = LoadColor(GI + 0x01);
  60. src2 = LoadColor(GI + 0x04);
  61. src3 = LoadColor(GI + 0x05);
  62. src4 = LoadColor(GI + 0x10);
  63. src5 = LoadColor(GI + 0x11);
  64. src6 = LoadColor(GI + 0x14);
  65. src7 = LoadColor(GI + 0x15);
  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. // For first thread in workgroup only
  93. if (GI == 0)
  94. {
  95. // Load all 8 colors from shared memory
  96. src1 = LoadColor(GI + 0x02);
  97. src2 = LoadColor(GI + 0x08);
  98. src3 = LoadColor(GI + 0x0A);
  99. src4 = LoadColor(GI + 0x10);
  100. src5 = LoadColor(GI + 0x12);
  101. src6 = LoadColor(GI + 0x18);
  102. src7 = LoadColor(GI + 0x1A);
  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
  117. mipLevel2[DTid / 4] = src0;
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement