Go to the documentation of this file. 1 "#ifndef NON_POWER_OF_TWO\n"
2 "#define NON_POWER_OF_TWO 0\n"
5 "#ifndef CONVERT_TO_SRGB\n"
6 "#define CONVERT_TO_SRGB 0\n"
10 "#define IMG_FORMAT rgba8\n"
13 "layout(IMG_FORMAT) uniform writeonly image2DArray OutMip0;\n"
14 "layout(IMG_FORMAT) uniform writeonly image2DArray OutMip1;\n"
15 "layout(IMG_FORMAT) uniform writeonly image2DArray OutMip2;\n"
16 "layout(IMG_FORMAT) uniform writeonly image2DArray OutMip3;\n"
18 "uniform sampler2DArray SrcMip;\n"
22 " int SrcMipLevel; // Texture level of source mip\n"
23 " int NumMipLevels; // Number of OutMips to write: [1, 4]\n"
24 " int FirstArraySlice;\n"
26 " vec2 TexelSize; // 1.0 / OutMip1.Dimensions\n"
30 "// The reason for separating channels is to reduce bank conflicts in the\n"
31 "// local data memory controller. A large stride will cause more threads\n"
32 "// to collide on the same memory bank.\n"
33 "shared float gs_R[64];\n"
34 "shared float gs_G[64];\n"
35 "shared float gs_B[64];\n"
36 "shared float gs_A[64];\n"
38 "void StoreColor( uint Index, vec4 Color )\n"
40 " gs_R[Index] = Color.r;\n"
41 " gs_G[Index] = Color.g;\n"
42 " gs_B[Index] = Color.b;\n"
43 " gs_A[Index] = Color.a;\n"
46 "vec4 LoadColor( uint Index )\n"
48 " return vec4( gs_R[Index], gs_G[Index], gs_B[Index], gs_A[Index]);\n"
51 "float LinearToSRGB(float x)\n"
53 " // This is exactly the sRGB curve\n"
54 " //return x < 0.0031308 ? 12.92 * x : 1.055 * pow(abs(x), 1.0 / 2.4) - 0.055;\n"
56 " // This is cheaper but nearly equivalent\n"
57 " return x < 0.0031308 ? 12.92 * x : 1.13005 * sqrt(abs(x - 0.00228)) - 0.13448 * x + 0.005719;\n"
60 "vec4 PackColor(vec4 Linear)\n"
62 "#if CONVERT_TO_SRGB\n"
63 " return vec4(LinearToSRGB(Linear.r), LinearToSRGB(Linear.g), LinearToSRGB(Linear.b), Linear.a);\n"
69 "void GroupMemoryBarrierWithGroupSync()\n"
71 " // OpenGL.org: groupMemoryBarrier() waits on the completion of all memory accesses \n"
72 " // performed by an invocation of a compute shader relative to the same access performed \n"
73 " // by other invocations in the same work group and then returns with no other effect.\n"
75 " // groupMemoryBarrier() acts like memoryBarrier(), ordering memory writes for all kinds \n"
76 " // of variables, but it only orders read/writes for the current work group.\n"
77 " groupMemoryBarrier();\n"
79 " // OpenGL.org: memoryBarrierShared() waits on the completion of \n"
80 " // all memory accesses resulting from the use of SHARED variables\n"
81 " // and then returns with no other effect. \n"
82 " memoryBarrierShared();\n"
84 " // Thread execution barrier\n"
88 "layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;\n"
91 " uint LocalInd = gl_LocalInvocationIndex;\n"
92 " uvec3 GlobalInd = gl_GlobalInvocationID;\n"
94 " ivec3 SrcMipSize = textureSize(SrcMip, 0); // SrcMip is the view of the source mip level\n"
95 " bool IsValidThread = GlobalInd.x < uint(SrcMipSize.x) && GlobalInd.y < uint(SrcMipSize.y);\n"
96 " int ArraySlice = FirstArraySlice + int(GlobalInd.z);\n"
98 " vec4 Src1 = vec4(0.0, 0.0, 0.0, 0.0);\n"
99 " float fSrcMipLevel = 0.0; // SrcMip is the view of the source mip level\n"
100 " if (IsValidThread)\n"
102 " // One bilinear sample is insufficient when scaling down by more than 2x.\n"
103 " // You will slightly undersample in the case where the source dimension\n"
104 " // is odd. This is why it\'s a really good idea to only generate mips on\n"
105 " // power-of-two sized textures. Trying to handle the undersampling case\n"
106 " // will force this shader to be slower and more complicated as it will\n"
107 " // have to take more source texture samples.\n"
108 "#if NON_POWER_OF_TWO == 0\n"
109 " vec2 UV = TexelSize * (vec2(GlobalInd.xy) + vec2(0.5, 0.5));\n"
110 " Src1 = textureLod(SrcMip, vec3(UV, ArraySlice), fSrcMipLevel);\n"
111 "#elif NON_POWER_OF_TWO == 1\n"
112 " // > 2:1 in X dimension\n"
113 " // Use 2 bilinear samples to guarantee we don\'t undersample when downsizing by more than 2x\n"
114 " // horizontally.\n"
115 " vec2 UV1 = TexelSize * (vec2(GlobalInd.xy) + vec2(0.25, 0.5));\n"
116 " vec2 Off = TexelSize * vec2(0.5, 0.0);\n"
117 " Src1 = 0.5 * (textureLod(SrcMip, vec3(UV1, ArraySlice), fSrcMipLevel) +\n"
118 " textureLod(SrcMip, vec3(UV1 + Off, ArraySlice), fSrcMipLevel));\n"
119 "#elif NON_POWER_OF_TWO == 2\n"
120 " // > 2:1 in Y dimension\n"
121 " // Use 2 bilinear samples to guarantee we don\'t undersample when downsizing by more than 2x\n"
123 " vec2 UV1 = TexelSize * (vec2(GlobalInd.xy) + vec2(0.5, 0.25));\n"
124 " vec2 Off = TexelSize * vec2(0.0, 0.5);\n"
125 " Src1 = 0.5 * (textureLod(SrcMip, vec3(UV1, ArraySlice), fSrcMipLevel) +\n"
126 " textureLod(SrcMip, vec3(UV1 + Off, ArraySlice), fSrcMipLevel));\n"
127 "#elif NON_POWER_OF_TWO == 3\n"
128 " // > 2:1 in in both dimensions\n"
129 " // Use 4 bilinear samples to guarantee we don\'t undersample when downsizing by more than 2x\n"
130 " // in both directions.\n"
131 " vec2 UV1 = TexelSize * (vec2(GlobalInd.xy) + vec2(0.25, 0.25));\n"
132 " vec2 Off = TexelSize * 0.5;\n"
133 " Src1 += textureLod(SrcMip, vec3(UV1, ArraySlice), fSrcMipLevel);\n"
134 " Src1 += textureLod(SrcMip, vec3(UV1 + vec2(Off.x, 0.0), ArraySlice), fSrcMipLevel);\n"
135 " Src1 += textureLod(SrcMip, vec3(UV1 + vec2(0.0, Off.y), ArraySlice), fSrcMipLevel);\n"
136 " Src1 += textureLod(SrcMip, vec3(UV1 + vec2(Off.x, Off.y), ArraySlice), fSrcMipLevel);\n"
140 " imageStore(OutMip0, ivec3(GlobalInd.xy, ArraySlice), PackColor(Src1));\n"
143 " // A scalar (constant) branch can exit all threads coherently.\n"
144 " if (NumMipLevels == 1)\n"
147 " if (IsValidThread)\n"
149 " // Without lane swizzle operations, the only way to share data with other\n"
150 " // threads is through LDS.\n"
151 " StoreColor(LocalInd, Src1);\n"
154 " // This guarantees all LDS writes are complete and that all threads have\n"
155 " // executed all instructions so far (and therefore have issued their LDS\n"
156 " // write instructions.)\n"
157 " GroupMemoryBarrierWithGroupSync();\n"
159 " if (IsValidThread)\n"
161 " // With low three bits for X and high three bits for Y, this bit mask\n"
162 " // (binary: 001001) checks that X and Y are even.\n"
163 " if ((LocalInd & 0x9u) == 0u)\n"
165 " vec4 Src2 = LoadColor(LocalInd + 0x01u);\n"
166 " vec4 Src3 = LoadColor(LocalInd + 0x08u);\n"
167 " vec4 Src4 = LoadColor(LocalInd + 0x09u);\n"
168 " Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n"
170 " imageStore(OutMip1, ivec3(GlobalInd.xy / 2u, ArraySlice), PackColor(Src1));\n"
171 " StoreColor(LocalInd, Src1);\n"
175 " if (NumMipLevels == 2)\n"
178 " GroupMemoryBarrierWithGroupSync();\n"
180 " if( IsValidThread )\n"
182 " // This bit mask (binary: 011011) checks that X and Y are multiples of four.\n"
183 " if ((LocalInd & 0x1Bu) == 0u)\n"
185 " vec4 Src2 = LoadColor(LocalInd + 0x02u);\n"
186 " vec4 Src3 = LoadColor(LocalInd + 0x10u);\n"
187 " vec4 Src4 = LoadColor(LocalInd + 0x12u);\n"
188 " Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n"
190 " imageStore(OutMip2, ivec3(GlobalInd.xy / 4u, ArraySlice), PackColor(Src1));\n"
191 " StoreColor(LocalInd, Src1);\n"
195 " if (NumMipLevels == 3)\n"
198 " GroupMemoryBarrierWithGroupSync();\n"
200 " if( IsValidThread )\n"
202 " // This bit mask would be 111111 (X & Y multiples of 8), but only one\n"
203 " // thread fits that criteria.\n"
204 " if (LocalInd == 0u)\n"
206 " vec4 Src2 = LoadColor(LocalInd + 0x04u);\n"
207 " vec4 Src3 = LoadColor(LocalInd + 0x20u);\n"
208 " vec4 Src4 = LoadColor(LocalInd + 0x24u);\n"
209 " Src1 = 0.25 * (Src1 + Src2 + Src3 + Src4);\n"
211 " imageStore(OutMip3, ivec3(GlobalInd.xy / 8u, ArraySlice), PackColor(Src1));\n"