Advertisement
Guest User

create mipmaps

a guest
Dec 1st, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. #version 450
  2. #extension GL_ARB_shading_language_include : require
  3. #extension GL_ARB_bindless_texture : require
  4.  
  5. #include "/buffer_blocks.glsl"
  6.  
  7. const uint g_UINT8_MAX = 0x000000FF;
  8. const uint g_UINT10_MAX = 0x000003FF;
  9. const float g_invUint8_max = 1.0 / float(g_UINT8_MAX);
  10. const float g_invUint10_max = 1.0 / float(g_UINT10_MAX);
  11.  
  12. layout(location = 0) uniform int u_sourceLevel;
  13. layout(location = 1) uniform int u_textureIndex;
  14.  
  15. layout(binding = 0, r32ui) uniform coherent uimage3D u_dstMip;
  16.  
  17. U_VOXELHANDLE_BLOCK(4);
  18.  
  19. void main()
  20. {
  21.     ivec3 dstSize = imageSize(u_dstMip);
  22.  
  23.     uint x = gl_VertexID;
  24.     uint z = gl_InstanceID;
  25.  
  26.     for(uint y = 0; y < dstSize.y; ++y)
  27.     {
  28.         ivec3 dstPos = ivec3(x, y, z);
  29.         ivec3 srcPos = dstPos * 2;
  30.  
  31.         uint srcColor = texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(0, 0, 0), u_sourceLevel).r;
  32.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(1, 0, 0), u_sourceLevel).r;
  33.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(0, 1, 0), u_sourceLevel).r;
  34.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(1, 1, 0), u_sourceLevel).r;
  35.  
  36.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(0, 0, 1), u_sourceLevel).r;
  37.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(1, 0, 1), u_sourceLevel).r;
  38.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(0, 1, 1), u_sourceLevel).r;
  39.         srcColor += texelFetch(u_voxelTexture[u_textureIndex], srcPos + ivec3(1, 1, 1), u_sourceLevel).r;
  40.  
  41.         vec3 tmpColor;
  42.         tmpColor.x = float(srcColor & g_UINT10_MAX);
  43.         tmpColor.y = float((srcColor >> 10) & g_UINT10_MAX);
  44.         tmpColor.z = float((srcColor >> 20) & g_UINT10_MAX);
  45.         tmpColor = clamp(tmpColor, vec3(0.0), vec3(g_UINT10_MAX));
  46.  
  47.         tmpColor = tmpColor * g_invUint8_max / 8.0;
  48.        
  49.         tmpColor = round(tmpColor * g_UINT8_MAX);
  50.        
  51.         uint outColor = uint(tmpColor.x) | (uint(tmpColor.y) << 10) | (uint(tmpColor.z) << 20);
  52.  
  53.         imageStore(u_dstMip, dstPos, uvec4(outColor));
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement