Go to the documentation of this file.
2 " * Copyright 2019-2021 Diligent Graphics LLC\n"
3 " * Copyright 2015-2019 Egor Yusov\n"
5 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n"
6 " * you may not use this file except in compliance with the License.\n"
7 " * You may obtain a copy of the License at\n"
9 " * http://www.apache.org/licenses/LICENSE-2.0\n"
11 " * Unless required by applicable law or agreed to in writing, software\n"
12 " * distributed under the License is distributed on an \"AS IS\" BASIS,\n"
13 " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
14 " * See the License for the specific language governing permissions and\n"
15 " * limitations under the License.\n"
17 " * In no event and under no legal theory, whether in tort (including negligence), \n"
18 " * contract, or otherwise, unless required by applicable law (such as deliberate \n"
19 " * and grossly negligent acts) or agreed to in writing, shall any Contributor be\n"
20 " * liable for any damages, including any direct, indirect, special, incidental, \n"
21 " * or consequential damages of any character arising as a result of this License or \n"
22 " * out of the use or inability to use the software (including but not limited to damages \n"
23 " * for loss of goodwill, work stoppage, computer failure or malfunction, or any and \n"
24 " * all other commercial damages or losses), even if such Contributor has been advised \n"
25 " * of the possibility of such damages.\n"
28 "#ifndef _GLSL_DEFINITIONS_\n"
29 "#define _GLSL_DEFINITIONS_\n"
34 "// From GLES 3.1 spec:\n"
35 "// Except for image variables qualified with the format qualifiers r32f, r32i, and r32ui,\n"
36 "// image variables must specify either memory qualifier readonly or the memory qualifier writeonly.\n"
37 "# define IMAGE_WRITEONLY writeonly\n"
39 "# define IMAGE_WRITEONLY\n"
42 "#define float4 vec4\n"
43 "#define float3 vec3\n"
44 "#define float2 vec2\n"
46 "#define int4 ivec4\n"
47 "#define int3 ivec3\n"
48 "#define int2 ivec2\n"
50 "#define uint4 uvec4\n"
51 "#define uint3 uvec3\n"
52 "#define uint2 uvec2\n"
54 "#define bool4 bvec4\n"
55 "#define bool3 bvec3\n"
56 "#define bool2 bvec2\n"
58 "// OpenGL matrices in GLSL are always as column-major \n"
59 "// (this is not related to how they are stored)\n"
60 "#define float2x2 mat2x2\n"
61 "#define float2x3 mat3x2\n"
62 "#define float2x4 mat4x2\n"
64 "#define float3x2 mat2x3\n"
65 "#define float3x3 mat3x3\n"
66 "#define float3x4 mat4x3\n"
68 "#define float4x2 mat2x4\n"
69 "#define float4x3 mat3x4\n"
70 "#define float4x4 mat4x4\n"
71 "#define matrix mat4x4\n"
75 "#define SamplerState int\n"
76 "#define SamplerComparisonState int\n"
78 "// https://www.opengl.org/wiki/Memory_Model#Incoherent_memory_access\n"
79 "// Shared variable access uses the rules for incoherent memory access. \n"
80 "// This means that the user must perform certain synchronization in \n"
81 "// order to ensure that shared variables are visible.\n"
82 "// At the same time, shared variables are all implicitly declared coherent, \n"
83 "// so one don\'t need to (and can\'t) use that qualifier.\n"
84 "#define groupshared shared\n"
86 "#ifdef FRAGMENT_SHADER\n"
90 "# define ddx(x) (x) // GLSL compiler fails when it sees derivatives \n"
91 "# define ddy(x) (x) // in any shader but fragment\n"
94 "#define ddx_coarse ddx\n"
95 "#define ddy_coarse ddy\n"
96 "#define ddx_fine ddx\n"
97 "#define ddy_fine ddy\n"
99 "#define mul(a, b) ((a)*(b))\n"
100 "#define frac fract\n"
101 "#define atan2 atan\n"
102 "#define rsqrt inversesqrt\n"
105 "#define dst distance\n"
106 "#define countbits bitCount\n"
107 "#define firstbithigh findMSB\n"
108 "#define firstbitlow findLSB\n"
109 "#define reversebits bitfieldReverse\n"
111 "float rcp( float x ){ return 1.0 / x; }\n"
112 "vec2 rcp( vec2 x ){ return vec2(1.0,1.0) / x; }\n"
113 "vec3 rcp( vec3 x ){ return vec3(1.0,1.0,1.0) / x; }\n"
114 "vec4 rcp( vec4 x ){ return vec4(1.0,1.0,1.0,1.0) / x; }\n"
116 "float saturate( float x ){ return clamp( x, 0.0, 1.0 ); }\n"
117 "vec2 saturate( vec2 x ){ return clamp( x, vec2(0.0, 0.0), vec2(1.0, 1.0) ); }\n"
118 "vec3 saturate( vec3 x ){ return clamp( x, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0) ); }\n"
119 "vec4 saturate( vec4 x ){ return clamp( x, vec4(0.0, 0.0, 0.0, 0.0), vec4(1.0, 1.0, 1.0, 1.0) ); }\n"
121 "void sincos( float x, out float s, out float c ){ s = sin( x ); c = cos( x ); }\n"
122 "void sincos( vec2 x, out vec2 s, out vec2 c ){ s = sin( x ); c = cos( x ); }\n"
123 "void sincos( vec3 x, out vec3 s, out vec3 c ){ s = sin( x ); c = cos( x ); }\n"
124 "void sincos( vec4 x, out vec4 s, out vec4 c ){ s = sin( x ); c = cos( x ); }\n"
127 "// Bit conversion operations\n"
129 "float asfloat( float x ){ return x; }\n"
130 "vec2 asfloat( vec2 x ){ return x; }\n"
131 "vec3 asfloat( vec3 x ){ return x; }\n"
132 "vec4 asfloat( vec4 x ){ return x; }\n"
134 "float asfloat( int x ){ return intBitsToFloat(x); }\n"
135 "vec2 asfloat( ivec2 x ){ return intBitsToFloat(x); }\n"
136 "vec3 asfloat( ivec3 x ){ return intBitsToFloat(x); }\n"
137 "vec4 asfloat( ivec4 x ){ return intBitsToFloat(x); }\n"
139 "float asfloat( uint x ){ return uintBitsToFloat(x); }\n"
140 "vec2 asfloat( uvec2 x ){ return uintBitsToFloat(x); }\n"
141 "vec3 asfloat( uvec3 x ){ return uintBitsToFloat(x); }\n"
142 "vec4 asfloat( uvec4 x ){ return uintBitsToFloat(x); }\n"
145 "int asint( int x ){ return x; }\n"
146 "ivec2 asint( ivec2 x ){ return x; }\n"
147 "ivec3 asint( ivec3 x ){ return x; }\n"
148 "ivec4 asint( ivec4 x ){ return x; }\n"
150 "int asint( uint x ){ return int(x); }\n"
151 "ivec2 asint( uvec2 x ){ return ivec2(x); }\n"
152 "ivec3 asint( uvec3 x ){ return ivec3(x); }\n"
153 "ivec4 asint( uvec4 x ){ return ivec4(x); }\n"
155 "int asint( float x ){ return floatBitsToInt(x); }\n"
156 "ivec2 asint( vec2 x ){ return floatBitsToInt(x); }\n"
157 "ivec3 asint( vec3 x ){ return floatBitsToInt(x); }\n"
158 "ivec4 asint( vec4 x ){ return floatBitsToInt(x); }\n"
161 "uint asuint( uint x ){ return x; }\n"
162 "uvec2 asuint( uvec2 x ){ return x; }\n"
163 "uvec3 asuint( uvec3 x ){ return x; }\n"
164 "uvec4 asuint( uvec4 x ){ return x; }\n"
166 "uint asuint( int x ){ return uint(x); }\n"
167 "uvec2 asuint( ivec2 x ){ return uvec2(x); }\n"
168 "uvec3 asuint( ivec3 x ){ return uvec3(x); }\n"
169 "uvec4 asuint( ivec4 x ){ return uvec4(x); }\n"
171 "uint asuint( float x ){ return floatBitsToUint(x); }\n"
172 "uvec2 asuint( vec2 x ){ return floatBitsToUint(x); }\n"
173 "uvec3 asuint( vec3 x ){ return floatBitsToUint(x); }\n"
174 "uvec4 asuint( vec4 x ){ return floatBitsToUint(x); }\n"
176 "#if defined(GL_ES) && (__VERSION__>=310) || !defined(GL_ES) && (__VERSION__>=420)\n"
177 "float f16tof32( uint u1 )\n"
179 " return unpackHalf2x16( u1 ).x;\n"
181 "vec2 f16tof32( uvec2 u2 )\n"
183 " uint u2PackedHalf = (u2.x & 0x0ffffu) | ((u2.y & 0x0ffffu) << 16u);\n"
184 " return unpackHalf2x16( u2PackedHalf ); \n"
186 "vec3 f16tof32( uvec3 u3 )\n"
188 " return vec3( f16tof32( u3.xy ), f16tof32( u3.z ) );\n"
190 "vec4 f16tof32( uvec4 u4 )\n"
192 " return vec4( f16tof32( u4.xy ), f16tof32( u4.zw ) );\n"
194 "float f16tof32( int i1 ){ return f16tof32( uint ( i1 ) ); }\n"
195 "vec2 f16tof32( ivec2 i2 ){ return f16tof32( uvec2( i2 ) ); }\n"
196 "vec3 f16tof32( ivec3 i3 ){ return f16tof32( uvec3( i3 ) ); }\n"
197 "vec4 f16tof32( ivec4 i4 ){ return f16tof32( uvec4( i4 ) ); }\n"
199 "uint f32tof16( float f )\n"
201 " return packHalf2x16( vec2( f, 0.0 ) ) & 0x0ffffu;\n"
203 "uvec2 f32tof16( vec2 f2 )\n"
205 " uint u2PackedHalf = packHalf2x16( f2 );\n"
206 " return uvec2( u2PackedHalf & 0x0ffffu, u2PackedHalf >> 16u );\n"
208 "uvec3 f32tof16( vec3 f3 )\n"
210 " return uvec3( f32tof16( f3.xy ), f32tof16( f3.z ) );\n"
212 "uvec4 f32tof16( vec4 f4 )\n"
214 " return uvec4( f32tof16( f4.xy ), f32tof16( f4.zw ) );\n"
218 "// Use #define as double is not supported on GLES\n"
219 "#define asdouble(lowbits, highbits) packDouble2x32( uvec2( lowbits, highbits ) )\n"
221 "// Floating point functions\n"
223 "bool isfinite( float x )\n"
225 " return !isinf( x ) && !isnan( x );\n"
228 "bool2 isfinite( vec2 f2 )\n"
230 " return bool2( isfinite( f2.x ), isfinite( f2.y ) );\n"
233 "bool3 isfinite( vec3 f3 )\n"
235 " return bool3( isfinite( f3.xy ), isfinite( f3.z ) );\n"
238 "bool4 isfinite( vec4 f4 )\n"
240 " return bool4( isfinite( f4.xyz ), isfinite( f4.w ) );\n"
243 "float log10( float x )\n"
245 " return log( x ) / log( 10.0 );\n"
247 "vec2 log10( vec2 x )\n"
249 " float _lg10 = log( 10.0 );\n"
250 " return log( x ) / vec2(_lg10, _lg10);\n"
252 "vec3 log10( vec3 x )\n"
254 " float _lg10 = log( 10.0 );\n"
255 " return log( x ) / vec3(_lg10, _lg10, _lg10);\n"
257 "vec4 log10( vec4 x )\n"
259 " float _lg10 = log( 10.0 );\n"
260 " return log( x ) / vec4(_lg10, _lg10, _lg10, _lg10);\n"
265 "# define mad(a,b,c) ((a)*(b)+(c))\n"
271 "// Relational and logical operators\n"
272 "#define Less lessThan\n"
273 "#define LessEqual lessThanEqual\n"
274 "#define Greater greaterThan\n"
275 "#define GreaterEqual greaterThanEqual\n"
276 "#define Equal equal\n"
277 "#define NotEqual notEqual\n"
279 "bool4 And(bool4 L, bool4 R)\n"
281 " return bool4(L.x && R.x,\n"
286 "bool3 And(bool3 L, bool3 R)\n"
288 " return bool3(L.x && R.x,\n"
292 "bool2 And(bool2 L, bool2 R)\n"
294 " return bool2(L.x && R.x,\n"
297 "bool And(bool L, bool R)\n"
299 " return (L && R);\n"
303 "bool4 Or(bool4 L, bool4 R)\n"
305 " return bool4(L.x || R.x,\n"
310 "bool3 Or(bool3 L, bool3 R)\n"
312 " return bool3(L.x || R.x,\n"
316 "bool2 Or(bool2 L, bool2 R)\n"
318 " return bool2(L.x || R.x,\n"
321 "bool Or(bool L, bool R)\n"
323 " return (L || R);\n"
326 "float4 BoolToFloat( bool4 b4 )\n"
328 " return float4(b4.x ? 1.0 : 0.0,\n"
329 " b4.y ? 1.0 : 0.0,\n"
330 " b4.z ? 1.0 : 0.0,\n"
331 " b4.w ? 1.0 : 0.0);\n"
333 "float3 BoolToFloat( bool3 b3 )\n"
335 " return float3(b3.x ? 1.0 : 0.0,\n"
336 " b3.y ? 1.0 : 0.0,\n"
337 " b3.z ? 1.0 : 0.0);\n"
339 "float2 BoolToFloat( bool2 b2 )\n"
341 " return float2(b2.x ? 1.0 : 0.0,\n"
342 " b2.y ? 1.0 : 0.0);\n"
344 "float BoolToFloat( bool b )\n"
346 " return b ? 1.0 : 0.0;\n"
350 "// Synchronization functions\n"
352 "#ifdef COMPUTE_SHADER\n"
354 "// https://www.opengl.org/wiki/Memory_Model#Incoherent_memory_access\n"
356 "// MSDN: GroupMemoryBarrier() blocks execution of all threads \n"
357 "// in a group until all group SHARED accesses have been completed.\n"
358 "void GroupMemoryBarrier()\n"
360 " // OpenGL.org: groupMemoryBarrier() waits on the completion of all memory accesses \n"
361 " // performed by an invocation of a compute shader relative to the same access performed \n"
362 " // by other invocations in the same work group and then returns with no other effect.\n"
364 " // groupMemoryBarrier() acts like memoryBarrier(), ordering memory writes for all kinds \n"
365 " // of variables, but it only orders read/writes for the current work group.\n"
366 " groupMemoryBarrier();\n"
368 " // OpenGL.org: memoryBarrierShared() waits on the completion of \n"
369 " // all memory accesses resulting from the use of SHARED variables\n"
370 " // and then returns with no other effect. \n"
371 " memoryBarrierShared();\n"
374 "// MSDN: GroupMemoryBarrierWithGroupSync() blocks execution of all \n"
375 "// threads in a group until all memory accesses have been completed \n"
376 "// and all threads in the group have reached this call.\n"
377 "void GroupMemoryBarrierWithGroupSync()\n"
379 " // Issue memory barrier first!\n"
380 " GroupMemoryBarrier();\n"
384 "// MSDN: DeviceMemoryBarrier() blocks execution of all threads \n"
385 "// in a group until all device memory accesses have been completed.\n"
386 "void DeviceMemoryBarrier()\n"
388 " // Call all memory barriers except for shared memory\n"
390 " // Do we need to call groupMemoryBarrier() ????? \n"
392 " // OpenGL.org: memoryBarrierBuffer() waits on the completion of \n"
393 " // all memory accesses resulting from the use of BUFFER variables \n"
394 " // and then returns with no other effect\n"
395 " memoryBarrierBuffer();\n"
397 " // OpenGL.org: memoryBarrierImage() waits on the completion of all \n"
398 " // memory accesses resulting from the use of IMAGE variables and then \n"
399 " // returns with no other effect. \n"
400 " memoryBarrierImage();\n"
402 " // OpenGL.org: memoryBarrierAtomicCounter() waits on the completion of \n"
403 " // all accesses resulting from the use of ATOMIC COUNTERS and then returns \n"
404 " // with no other effect. \n"
405 " memoryBarrierAtomicCounter();\n"
408 "// MSDN: DeviceMemoryBarrierWithGroupSync() blocks execution of \n"
409 "// all threads in a group until all device memory accesses have \n"
410 "// been completed and all threads in the group have reached this call.\n"
411 "void DeviceMemoryBarrierWithGroupSync()\n"
413 " DeviceMemoryBarrier();\n"
417 "// MSDN: AllMemoryBarrier() blocks execution of all threads in a \n"
418 "// group until all memory accesses have been completed.\n"
419 "void AllMemoryBarrier()\n"
421 " // OpenGL.org: memoryBarrier() waits on the completion of ALL \n"
422 " // memory accesses resulting from the use of IMAGE variables or \n"
423 " // ATOMIC COUNTERS and then returns with no other effect.\n"
424 " memoryBarrier();\n"
425 " // NOTE: nothing is said about buffer memory and shared memory,\n"
426 " // so call memoryBarrierBuffer() and memoryBarrierShared() for safety\n"
428 " // OpenGL.org: memoryBarrierBuffer() waits on the completion of \n"
429 " // all memory accesses resulting from the use of BUFFER variables \n"
430 " // and then returns with no other effect\n"
431 " memoryBarrierBuffer();\n"
433 " // OpenGL.org: memoryBarrierShared() waits on the completion of \n"
434 " // all memory accesses resulting from the use of SHARED variables\n"
435 " // and then returns with no other effect. \n"
436 " memoryBarrierShared();\n"
438 " // Call all memory barrier functions. They should have no effect\n"
439 " // if everything is synchronized.\n"
441 " // OpenGL.org: memoryBarrierImage() waits on the completion of all \n"
442 " // memory accesses resulting from the use of IMAGE variables and then \n"
443 " // returns with no other effect. \n"
444 " memoryBarrierImage();\n"
446 " // OpenGL.org: memoryBarrierAtomicCounter() waits on the completion of \n"
447 " // all accesses resulting from the use of ATOMIC COUNTERS and then returns \n"
448 " // with no other effect. \n"
449 " memoryBarrierAtomicCounter();\n"
451 " // groupMemoryBarrier waits on the completion of all memory accesses performed \n"
452 " // by an invocation of a compute shader relative to the same access performed by \n"
453 " // other invocations in the same work group and then returns with no other effect.\n"
454 " groupMemoryBarrier();\n"
457 "// MSDN: AllMemoryBarrierWithGroupSync() blocks execution of all \n"
458 "// threads in a group until all memory accesses have been completed \n"
459 "// and all threads in the group have reached this call.\n"
460 "void AllMemoryBarrierWithGroupSync()\n"
462 " AllMemoryBarrier();\n"
468 "void AllMemoryBarrier(){}\n"
469 "void AllMemoryBarrierWithGroupSync(){}\n"
470 "void DeviceMemoryBarrier(){}\n"
471 "void DeviceMemoryBarrierWithGroupSync(){}\n"
472 "void GroupMemoryBarrier(){}\n"
473 "void GroupMemoryBarrierWithGroupSync(){}\n"
478 "// Type conversion functions\n"
480 "vec4 _ExpandVector( float x ){ return vec4( x, x, x, x ); }\n"
481 "vec4 _ExpandVector( vec2 f2 ){ return vec4( f2.x, f2.y, 0.0, 0.0 ); }\n"
482 "vec4 _ExpandVector( vec3 f3 ){ return vec4( f3.x, f3.y, f3.z, 0.0 ); }\n"
483 "vec4 _ExpandVector( vec4 f4 ){ return vec4( f4.x, f4.y, f4.z, f4.w ); }\n"
485 "ivec4 _ExpandVector( int x ){ return ivec4( x, x, x, x ); }\n"
486 "ivec4 _ExpandVector( ivec2 i2 ){ return ivec4( i2.x, i2.y, 0, 0 ); }\n"
487 "ivec4 _ExpandVector( ivec3 i3 ){ return ivec4( i3.x, i3.y, i3.z, 0 ); }\n"
488 "ivec4 _ExpandVector( ivec4 i4 ){ return ivec4( i4.x, i4.y, i4.z, i4.w ); }\n"
490 "uvec4 _ExpandVector( uint x ){ return uvec4( x, x, x, x ); }\n"
491 "uvec4 _ExpandVector( uvec2 u2 ){ return uvec4( u2.x, u2.y, 0u, 0u ); }\n"
492 "uvec4 _ExpandVector( uvec3 u3 ){ return uvec4( u3.x, u3.y, u3.z, 0u ); }\n"
493 "uvec4 _ExpandVector( uvec4 u4 ){ return uvec4( u4.x, u4.y, u4.z, u4.w ); }\n"
495 "bvec4 _ExpandVector( bool x ){ return bvec4( x, x, x, x ); }\n"
496 "bvec4 _ExpandVector( bvec2 b2 ){ return bvec4( b2.x, b2.y, false, false ); }\n"
497 "bvec4 _ExpandVector( bvec3 b3 ){ return bvec4( b3.x, b3.y, b3.z, false ); }\n"
498 "bvec4 _ExpandVector( bvec4 b4 ){ return bvec4( b4.x, b4.y, b4.z, b4.w ); }\n"
500 "void _ResizeVector(out vec4 outVec4, in vec4 inVec4){outVec4 = inVec4;}\n"
501 "void _ResizeVector(out vec3 outVec3, in vec4 inVec4){outVec3 = inVec4.xyz;}\n"
502 "void _ResizeVector(out vec2 outVec2, in vec4 inVec4){outVec2 = inVec4.xy;}\n"
503 "void _ResizeVector(out float outFlt, in vec4 inVec4){outFlt = inVec4.x;}\n"
505 "void _ResizeVector(out vec4 outVec4, in vec3 inVec3){outVec4 = vec4(inVec3, 0.0);}\n"
506 "void _ResizeVector(out vec3 outVec3, in vec3 inVec3){outVec3 = inVec3;}\n"
507 "void _ResizeVector(out vec2 outVec2, in vec3 inVec3){outVec2 = inVec3.xy;}\n"
508 "void _ResizeVector(out float outFlt, in vec3 inVec3){outFlt = inVec3.x;}\n"
510 "void _ResizeVector(out vec4 outVec4, in vec2 inVec2){outVec4 = vec4(inVec2, 0.0, 0.0);}\n"
511 "void _ResizeVector(out vec3 outVec3, in vec2 inVec2){outVec3 = vec3(inVec2, 0.0);}\n"
512 "void _ResizeVector(out vec2 outVec2, in vec2 inVec2){outVec2 = inVec2;}\n"
513 "void _ResizeVector(out float outFlt, in vec2 inVec2){outFlt = inVec2.x;}\n"
515 "void _ResizeVector(out vec4 outVec4, in float v){outVec4 = vec4(v, 0.0, 0.0, 0.0);}\n"
516 "void _ResizeVector(out vec3 outVec3, in float v){outVec3 = vec3(v, 0.0, 0.0);}\n"
517 "void _ResizeVector(out vec2 outVec2, in float v){outVec2 = vec2(v, 0.0);}\n"
518 "void _ResizeVector(out float outFlt, in float v){outFlt = v;}\n"
521 "void _TypeConvertStore( out float Dst, in int Src ){ Dst = float( Src ); }\n"
522 "void _TypeConvertStore( out float Dst, in uint Src ){ Dst = float( Src ); }\n"
523 "void _TypeConvertStore( out float Dst, in float Src ){ Dst = float( Src ); }\n"
524 "void _TypeConvertStore( out float Dst, in bool Src ){ Dst = Src ? 1.0 : 0.0; }\n"
526 "void _TypeConvertStore( out uint Dst, in int Src ){ Dst = uint( Src ); }\n"
527 "void _TypeConvertStore( out uint Dst, in uint Src ){ Dst = uint( Src ); }\n"
528 "void _TypeConvertStore( out uint Dst, in float Src ){ Dst = uint( Src ); }\n"
529 "void _TypeConvertStore( out uint Dst, in bool Src ){ Dst = Src ? 1u : 0u; }\n"
531 "void _TypeConvertStore( out int Dst, in int Src ){ Dst = int( Src ); }\n"
532 "void _TypeConvertStore( out int Dst, in uint Src ){ Dst = int( Src ); }\n"
533 "void _TypeConvertStore( out int Dst, in float Src ){ Dst = int( Src ); }\n"
534 "void _TypeConvertStore( out int Dst, in bool Src ){ Dst = Src ? 1 : 0; }\n"
536 "void _TypeConvertStore( out bool Dst, in int Src ){ Dst = (Src != 0); }\n"
537 "void _TypeConvertStore( out bool Dst, in uint Src ){ Dst = (Src != 0u); }\n"
538 "void _TypeConvertStore( out bool Dst, in float Src ){ Dst = (Src != 0.0); }\n"
539 "void _TypeConvertStore( out bool Dst, in bool Src ){ Dst = Src; }\n"
542 "int _ToInt( int x ) { return int(x); }\n"
543 "int _ToInt( ivec2 v ) { return int(v.x); }\n"
544 "int _ToInt( ivec3 v ) { return int(v.x); }\n"
545 "int _ToInt( ivec4 v ) { return int(v.x); }\n"
547 "int _ToInt( uint x ) { return int(x); }\n"
548 "int _ToInt( uvec2 v ) { return int(v.x); }\n"
549 "int _ToInt( uvec3 v ) { return int(v.x); }\n"
550 "int _ToInt( uvec4 v ) { return int(v.x); }\n"
552 "int _ToInt( float x ) { return int(x); }\n"
553 "int _ToInt( vec2 v ) { return int(v.x); }\n"
554 "int _ToInt( vec3 v ) { return int(v.x); }\n"
555 "int _ToInt( vec4 v ) { return int(v.x); }\n"
557 "int _ToInt( bool x ) { return x ? 1 : 0;}\n"
558 "int _ToInt( bvec2 v ) { return v.x ? 1 : 0;}\n"
559 "int _ToInt( bvec3 v ) { return v.x ? 1 : 0;}\n"
560 "int _ToInt( bvec4 v ) { return v.x ? 1 : 0;}\n"
564 "float _ToFloat( int x ) { return float(x); }\n"
565 "float _ToFloat( ivec2 v ){ return float(v.x); }\n"
566 "float _ToFloat( ivec3 v ){ return float(v.x); }\n"
567 "float _ToFloat( ivec4 v ){ return float(v.x); }\n"
569 "float _ToFloat( uint x ) { return float(x); }\n"
570 "float _ToFloat( uvec2 v ){ return float(v.x); }\n"
571 "float _ToFloat( uvec3 v ){ return float(v.x); }\n"
572 "float _ToFloat( uvec4 v ){ return float(v.x); }\n"
574 "float _ToFloat( float x ){ return float(x); }\n"
575 "float _ToFloat( vec2 v ) { return float(v.x); }\n"
576 "float _ToFloat( vec3 v ) { return float(v.x); }\n"
577 "float _ToFloat( vec4 v ) { return float(v.x); }\n"
579 "float _ToFloat( bool x ) { return x ? 1.0 : 0.0;}\n"
580 "float _ToFloat( bvec2 v ){ return v.x ? 1.0 : 0.0;}\n"
581 "float _ToFloat( bvec3 v ){ return v.x ? 1.0 : 0.0;}\n"
582 "float _ToFloat( bvec4 v ){ return v.x ? 1.0 : 0.0;}\n"
586 "uint _ToUint( int x ) { return uint(x); }\n"
587 "uint _ToUint( uint x ) { return uint(x); }\n"
588 "uint _ToUint( float x ){ return uint(x); }\n"
589 "uint _ToUint( bool x ) { return x ? 1u : 0u; }\n"
591 "bool _ToBool( int x ) { return x != 0 ? true : false; }\n"
592 "bool _ToBool( uint x ) { return x != 0u ? true : false; }\n"
593 "bool _ToBool( float x ){ return x != 0.0 ? true : false; }\n"
594 "bool _ToBool( bool x ) { return x; }\n"
596 "#define _ToVec2(x,y) vec2(_ToFloat(x), _ToFloat(y))\n"
597 "#define _ToVec3(x,y,z) vec3(_ToFloat(x), _ToFloat(y), _ToFloat(z))\n"
598 "#define _ToVec4(x,y,z,w) vec4(_ToFloat(x), _ToFloat(y), _ToFloat(z), _ToFloat(w))\n"
600 "#define _ToIvec2(x,y) ivec2(_ToInt(x), _ToInt(y))\n"
601 "#define _ToIvec3(x,y,z) ivec3(_ToInt(x), _ToInt(y), _ToInt(z))\n"
602 "#define _ToIvec4(x,y,z,w) ivec4(_ToInt(x), _ToInt(y), _ToInt(z), _ToInt(w))\n"
604 "#define _ToUvec2(x,y) uvec2(_ToUint(x), _ToUint(y))\n"
605 "#define _ToUvec3(x,y,z) uvec3(_ToUint(x), _ToUint(y), _ToUint(z))\n"
606 "#define _ToUvec4(x,y,z,w) uvec4(_ToUint(x), _ToUint(y), _ToUint(z), _ToUint(w))\n"
608 "#define _ToBvec2(x,y) bvec2(_ToBool(x), _ToBool(y))\n"
609 "#define _ToBvec3(x,y,z) bvec3(_ToBool(x), _ToBool(y), _ToBool(z))\n"
610 "#define _ToBvec4(x,y,z,w) bvec4(_ToBool(x), _ToBool(y), _ToBool(z), _ToBool(w))\n"
613 "int _ToIvec( uint u1 ){ return _ToInt( u1 ); }\n"
614 "ivec2 _ToIvec( uvec2 u2 ){ return _ToIvec2( u2.x, u2.y ); }\n"
615 "ivec3 _ToIvec( uvec3 u3 ){ return _ToIvec3( u3.x, u3.y, u3.z ); }\n"
616 "ivec4 _ToIvec( uvec4 u4 ){ return _ToIvec4( u4.x, u4.y, u4.z, u4.w ); }\n"
618 "int _ToIvec( int i1 ){ return i1; }\n"
619 "ivec2 _ToIvec( ivec2 i2 ){ return i2; }\n"
620 "ivec3 _ToIvec( ivec3 i3 ){ return i3; }\n"
621 "ivec4 _ToIvec( ivec4 i4 ){ return i4; }\n"
623 "int _ToIvec( float f1 ){ return _ToInt( f1 ); }\n"
624 "ivec2 _ToIvec( vec2 f2 ){ return _ToIvec2( f2.x, f2.y ); }\n"
625 "ivec3 _ToIvec( vec3 f3 ){ return _ToIvec3( f3.x, f3.y, f3.z ); }\n"
626 "ivec4 _ToIvec( vec4 f4 ){ return _ToIvec4( f4.x, f4.y, f4.z, f4.w ); }\n"
629 "float _ToVec( uint u1 ){ return _ToFloat(u1); }\n"
630 "vec2 _ToVec( uvec2 u2 ){ return _ToVec2( u2.x, u2.y ); }\n"
631 "vec3 _ToVec( uvec3 u3 ){ return _ToVec3( u3.x, u3.y, u3.z ); }\n"
632 "vec4 _ToVec( uvec4 u4 ){ return _ToVec4( u4.x, u4.y, u4.z, u4.w ); }\n"
634 "float _ToVec( int i1 ){ return _ToFloat(i1); }\n"
635 "vec2 _ToVec( ivec2 i2 ){ return _ToVec2( i2.x, i2.y ); }\n"
636 "vec3 _ToVec( ivec3 i3 ){ return _ToVec3( i3.x, i3.y, i3.z ); }\n"
637 "vec4 _ToVec( ivec4 i4 ){ return _ToVec4( i4.x, i4.y, i4.z, i4.w ); }\n"
639 "float _ToVec( float f1 ){ return f1; }\n"
640 "vec2 _ToVec( vec2 f2 ){ return f2; }\n"
641 "vec3 _ToVec( vec3 f3 ){ return f3; }\n"
642 "vec4 _ToVec( vec4 f4 ){ return f4; }\n"
645 "uint _ToUvec( uint u1 ){ return u1; }\n"
646 "uvec2 _ToUvec( uvec2 u2 ){ return u2; }\n"
647 "uvec3 _ToUvec( uvec3 u3 ){ return u3; }\n"
648 "uvec4 _ToUvec( uvec4 u4 ){ return u4; }\n"
650 "uint _ToUvec( int i1 ){ return _ToUint( i1 ); }\n"
651 "uvec2 _ToUvec( ivec2 i2 ){ return _ToUvec2( i2.x, i2.y ); }\n"
652 "uvec3 _ToUvec( ivec3 i3 ){ return _ToUvec3( i3.x, i3.y, i3.z ); }\n"
653 "uvec4 _ToUvec( ivec4 i4 ){ return _ToUvec4( i4.x, i4.y, i4.z, i4.w ); }\n"
655 "uint _ToUvec( float f1 ){ return _ToUint( f1 ); }\n"
656 "uvec2 _ToUvec( vec2 f2 ){ return _ToUvec2( f2.x, f2.y ); }\n"
657 "uvec3 _ToUvec( vec3 f3 ){ return _ToUvec3( f3.x, f3.y, f3.z ); }\n"
658 "uvec4 _ToUvec( vec4 f4 ){ return _ToUvec4( f4.x, f4.y, f4.z, f4.w ); }\n"
660 "// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/frexp.xhtml\n"
661 "// https://www.khronos.org/registry/OpenGL-Refpages/es3.1/html/frexp.xhtml\n"
662 "#if defined(GL_ES) && (__VERSION__>=310) || !defined(GL_ES) && (__VERSION__>=400)\n"
663 "// We have to redefine \'float frexp(float, int)\' as \'float frexp(float, float)\'\n"
664 "float _frexp(float f1, out float fexp1)\n"
667 " float sig1 = frexp(f1, iexp1);\n"
668 " fexp1 = float(iexp1);\n"
671 "vec2 _frexp(vec2 f2, out vec2 fexp2)\n"
674 " vec2 sig2 = frexp(f2, iexp2);\n"
675 " fexp2 = vec2(iexp2);\n"
678 "vec3 _frexp(vec3 f3, out vec3 fexp3)\n"
681 " vec3 sig3 = frexp(f3, iexp3);\n"
682 " fexp3 = vec3(iexp3);\n"
685 "vec4 _frexp(vec4 f4, out vec4 fexp4)\n"
688 " vec4 sig4 = frexp(f4, iexp4);\n"
689 " fexp4 = vec4(iexp4);\n"
692 "#define frexp _frexp\n"
696 "// TEXTURE FUNCTION STUB MACROS\n"
697 "// https://www.opengl.org/wiki/Sampler_(GLSL)\n"
700 "// Texture size queries\n"
701 "// https://www.opengl.org/sdk/docs/man/html/textureSize.xhtml\n"
702 "// textureSize returns the dimensions of level lod (if present) of the texture bound to sampler. \n"
703 "// The components in the return value are filled in, in order, with the width, height and depth \n"
704 "// of the texture. For the array forms, the last component of the return value is the number of \n"
705 "// layers in the texture array.\n"
707 "//#if !(defined(DESKTOP_GL) && __VERSION__ >= 430)\n"
708 "# define textureQueryLevels(x) 0 // Only supported on 4.3+\n"
711 "#define GetTex1DDimensions_1(Sampler, Width)\\\n"
713 " _TypeConvertStore( Width, textureSize(Sampler, 0) );\\\n"
716 "#define GetTex1DDimensions_3(Sampler, MipLevel, Width, NumberOfMipLevels)\\\n"
718 " _TypeConvertStore( Width, textureSize(Sampler, _ToInt(MipLevel)) ); \\\n"
719 " _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) ); \\\n"
722 "#define GetTex1DArrDimensions_2(Sampler, Width, Elements)\\\n"
724 " ivec2 i2Size = textureSize(Sampler, 0); \\\n"
725 " _TypeConvertStore( Width, i2Size.x );\\\n"
726 " _TypeConvertStore( Elements, i2Size.y );\\\n"
729 "#define GetTex1DArrDimensions_4(Sampler, MipLevel, Width, Elements, NumberOfMipLevels)\\\n"
731 " ivec2 i2Size = textureSize(Sampler, _ToInt(MipLevel)); \\\n"
732 " _TypeConvertStore( Width, i2Size.x ); \\\n"
733 " _TypeConvertStore( Elements, i2Size.y ); \\\n"
734 " _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\\\n"
737 "#define GetTex2DDimensions_2(Sampler, Width, Height)\\\n"
739 " ivec2 i2Size = textureSize(Sampler, 0); \\\n"
740 " _TypeConvertStore( Width, i2Size.x ); \\\n"
741 " _TypeConvertStore( Height, i2Size.y ); \\\n"
744 "#define GetTex2DDimensions_4(Sampler, MipLevel, Width, Height, NumberOfMipLevels)\\\n"
746 " ivec2 i2Size = textureSize(Sampler, _ToInt(MipLevel) ); \\\n"
747 " _TypeConvertStore( Width, i2Size.x ); \\\n"
748 " _TypeConvertStore( Height, i2Size.y ); \\\n"
749 " _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\\\n"
752 "#define GetTex2DArrDimensions_3(Sampler, Width, Height, Elements)\\\n"
754 " ivec3 i3Size = textureSize(Sampler, 0); \\\n"
755 " _TypeConvertStore( Width, i3Size.x ); \\\n"
756 " _TypeConvertStore( Height, i3Size.y ); \\\n"
757 " _TypeConvertStore( Elements,i3Size.z ); \\\n"
760 "#define GetTex2DArrDimensions_5(Sampler, MipLevel, Width, Height, Elements, NumberOfMipLevels)\\\n"
762 " ivec3 i3Size = textureSize(Sampler, _ToInt(MipLevel)); \\\n"
763 " _TypeConvertStore( Width, i3Size.x ); \\\n"
764 " _TypeConvertStore( Height, i3Size.y ); \\\n"
765 " _TypeConvertStore( Elements, i3Size.z ); \\\n"
766 " _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\\\n"
769 "#define GetTex3DDimensions_3(Sampler, Width, Height, Depth)\\\n"
771 " ivec3 i3Size = textureSize(Sampler, 0); \\\n"
772 " _TypeConvertStore( Width, i3Size.x ); \\\n"
773 " _TypeConvertStore( Height, i3Size.y ); \\\n"
774 " _TypeConvertStore( Depth, i3Size.z ); \\\n"
777 "#define GetTex3DDimensions_5(Sampler, MipLevel, Width, Height, Depth, NumberOfMipLevels)\\\n"
779 " ivec3 i3Size = textureSize(Sampler, _ToInt(MipLevel)); \\\n"
780 " _TypeConvertStore( Width, i3Size.x ); \\\n"
781 " _TypeConvertStore( Height, i3Size.y ); \\\n"
782 " _TypeConvertStore( Depth, i3Size.z ); \\\n"
783 " _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\\\n"
786 "#define GetTex2DMSDimensions_3(Sampler, Width, Height, NumberOfSamples)\\\n"
788 " ivec2 i2Size = textureSize(Sampler); \\\n"
789 " _TypeConvertStore( Width, i2Size.x ); \\\n"
790 " _TypeConvertStore( Height, i2Size.y ); \\\n"
791 " _TypeConvertStore( NumberOfSamples, 0 );\\\n"
794 "#define GetTex2DMSArrDimensions_4(Sampler, Width, Height, Elements, NumberOfSamples)\\\n"
796 " ivec3 i3Size = textureSize(Sampler); \\\n"
797 " _TypeConvertStore( Width, i3Size.x );\\\n"
798 " _TypeConvertStore( Height, i3Size.y );\\\n"
799 " _TypeConvertStore( Elements, i3Size.z );\\\n"
800 " _TypeConvertStore( NumberOfSamples, 0 );\\\n"
803 "#define GetTexBufferDimensions_1(Sampler, Width)\\\n"
805 " _TypeConvertStore( Width, textureSize(Sampler) );\\\n"
809 "// https://www.opengl.org/sdk/docs/man/html/imageSize.xhtml\n"
810 "// imageSize returns the dimensions of the image bound to image. The components in the \n"
811 "// return value are filled in, in order, with the width, height and depth of the image. \n"
812 "// For the array forms, the last component of the return value is the number of layers \n"
813 "// in the texture array.\n"
815 "#define GetRWTex1DDimensions_1(Tex, Width)\\\n"
817 " _TypeConvertStore( Width, imageSize(Tex) ); \\\n"
820 "#define GetRWTex1DArrDimensions_2(Tex, Width, Elements)\\\n"
822 " ivec2 i2Size = imageSize(Tex); \\\n"
823 " _TypeConvertStore( Width, i2Size.x ); \\\n"
824 " _TypeConvertStore( Elements, i2Size.y ); \\\n"
827 "#define GetRWTex2DDimensions_2(Tex, Width, Height)\\\n"
829 " ivec2 i2Size = imageSize(Tex); \\\n"
830 " _TypeConvertStore( Width, i2Size.x ); \\\n"
831 " _TypeConvertStore( Height, i2Size.y ); \\\n"
834 "#define GetRWTex2DArrDimensions_3(Tex, Width, Height, Elements)\\\n"
836 " ivec3 i3Size = imageSize(Tex); \\\n"
837 " _TypeConvertStore( Width, i3Size.x );\\\n"
838 " _TypeConvertStore( Height, i3Size.y );\\\n"
839 " _TypeConvertStore( Elements, i3Size.z );\\\n"
842 "#define GetRWTex3DDimensions_3(Tex, Width, Height, Depth)\\\n"
844 " ivec3 i3Size = imageSize(Tex); \\\n"
845 " _TypeConvertStore( Width, i3Size.x ); \\\n"
846 " _TypeConvertStore( Height, i3Size.y ); \\\n"
847 " _TypeConvertStore( Depth, i3Size.z ); \\\n"
850 "#define GetRWTexBufferDimensions_1(Tex, Width)\\\n"
852 " _TypeConvertStore( Width, imageSize(Tex) ); \\\n"
856 "// Texture sampling operations\n"
859 "// IMPORTANT NOTE ABOUT OFFSET\n"
860 "// Offset parameter to all texture sampling functions must be a constant expression.\n"
861 "// If it is not, the shader will be successfully compiled, HOWEVER the value of Offset \n"
862 "// will silently be zero. \n"
864 "// A constant expression in GLSL is defined as follows:\n"
865 "// * A literal value.\n"
866 "// * A const-qualified variable with an explicit initializer (so not a function parameter).\n"
867 "// * The result of the length() function of an array, but only if the array has an explicit size.\n"
868 "// * The result of most operators, so long as all the operands are themselves constant expressions. \n"
869 "// The operators not on this list are any assignment operators (+= and so forth), and the comma operator.\n"
870 "// * The result of a constructor for a type, but only if all of the arguments to the constructor are \n"
871 "// themselves constant expressions.\n"
872 "// * The return value of any built-in function, but only if all of the arguments to the function are \n"
873 "// themselves constant expressions. Opaque Types are never constant expressions. Note that the \n"
874 "// functions dFdx, dFdy, and fwidth will return 0, when used in a context that requires a constant \n"
875 "// expression (such as a const variable initializer).\n"
877 "// The list above does not include return value of a function, even when the value is compile-time expression.\n"
878 "// As a result, we cannot use type conversion functions for Offset parameter.\n"
880 "// In all texture sampling functions, the last component of Coords is used as Dsub and the array layer is specified \n"
881 "// in the second to last component of Coords. (The second component of Coords is unused for 1D shadow lookups.)\n"
882 "// For cube array textures, Dsub is specified as a separate parameter\n"
884 "#define SampleCmpLevel0Tex1D_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3( Coords, 0.0, CompareValue), 0.0)\n"
885 "#define SampleCmpLevel0Tex1DArr_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0)\n"
886 "#define SampleCmpLevel0Tex2D_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0)\n"
887 "#define SampleCmpLevel0Tex2DArr_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for sampler2DArrayShadow\n"
888 "#define SampleCmpLevel0TexCube_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for samplerCubeShadow\n"
889 "#define SampleCmpLevel0TexCubeArr_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for samplerCubeArrayShadow\n"
892 "#define SampleCmpLevel0Tex1D_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3( Coords, 0.0, CompareValue), 0.0, int(Offset))\n"
893 "#define SampleCmpLevel0Tex1DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0, int(Offset))\n"
894 "#define SampleCmpLevel0Tex2D_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0, ivec2((Offset).xy))\n"
895 "#define SampleCmpLevel0Tex2DArr_4(Tex, Sampler, Coords, CompareValue, Offset) 0.0 // No textureLodOffset for sampler2DArrayShadow\n"
898 "// https://www.opengl.org/sdk/docs/man/html/texture.xhtml - note: there are many mistakes on the page\n"
899 "#ifdef FRAGMENT_SHADER\n"
901 "# define Sample_2(Tex, Sampler, Coords) texture (Tex, _ToVec(Coords))\n"
902 "# define Sample_3(Tex, Sampler, Coords, Offset) textureOffset(Tex, _ToVec(Coords), Offset)\n"
903 "# define SampleBias_3(Tex, Sampler, Coords, Bias) texture (Tex, _ToVec(Coords), _ToFloat(Bias))\n"
904 "# define SampleBias_4(Tex, Sampler, Coords, Bias, Offset) textureOffset(Tex, _ToVec(Coords), Offset, _ToFloat(Bias))\n"
906 "# define SampleCmpTex1D_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3( Coords, 0.0, CompareValue))\n"
907 "# define SampleCmpTex1DArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue))\n"
908 "# define SampleCmpTex2D_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue))\n"
909 "# define SampleCmpTex2DArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue))\n"
910 "# define SampleCmpTexCube_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue))\n"
911 "# define SampleCmpTexCubeArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, (Coords).w), _ToFloat(CompareValue))\n"
913 "# define SampleCmpTex1D_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3( Coords, 0.0, CompareValue), int(Offset))\n"
914 "# define SampleCmpTex1DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), int(Offset))\n"
915 "# define SampleCmpTex2D_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), ivec2((Offset).xy))\n"
916 "# define SampleCmpTex2DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue), ivec2((Offset).xy))\n"
920 "// Derivatives are only available in fragment shader. GLSL compiler fails when it\n"
921 "// encounters texture() or textureOffset() instructions in other types of shaders. So\n"
922 "// to let the shader be compiled and to have something meaningful, replace such operations\n"
923 "// with textureLod() and textureLodOffset()\n"
925 "# define Sample_2(Tex, Sampler, Coords) textureLod (Tex, _ToVec(Coords), 0.0)\n"
926 "# define Sample_3(Tex, Sampler, Coords, Offset) textureLodOffset(Tex, _ToVec(Coords), 0.0, Offset)\n"
927 "# define SampleBias_3(Tex, Sampler, Coords, Bias) textureLod (Tex, _ToVec(Coords), 0.0 + _ToFloat(Bias))\n"
928 "# define SampleBias_4(Tex, Sampler, Coords, Bias, Offset) textureLodOffset(Tex, _ToVec(Coords), 0.0 + _ToFloat(Bias), Offset)\n"
930 "# define SampleCmpTex1D_3 SampleCmpLevel0Tex1D_3\n"
931 "# define SampleCmpTex1DArr_3 SampleCmpLevel0Tex1DArr_3\n"
932 "# define SampleCmpTex2D_3 SampleCmpLevel0Tex2D_3\n"
933 "# define SampleCmpTex2DArr_3 SampleCmpLevel0Tex2DArr_3\n"
934 "# define SampleCmpTexCube_3 SampleCmpLevel0TexCube_3\n"
935 "# define SampleCmpTexCubeArr_3 SampleCmpLevel0TexCubeArr_3\n"
937 "# define SampleCmpTex1D_4 SampleCmpLevel0Tex1D_4\n"
938 "# define SampleCmpTex1DArr_4 SampleCmpLevel0Tex1DArr_4\n"
939 "# define SampleCmpTex2D_4 SampleCmpLevel0Tex2D_4\n"
940 "# define SampleCmpTex2DArr_4 SampleCmpLevel0Tex2DArr_4\n"
944 "// https://www.opengl.org/sdk/docs/man/html/textureLod.xhtml\n"
945 "#define SampleLevel_3(Tex, Sampler, Coords, Level) textureLod (Tex, _ToVec(Coords), _ToFloat(Level))\n"
946 "#define SampleLevel_4(Tex, Sampler, Coords, Level, Offset) textureLodOffset(Tex, _ToVec(Coords), _ToFloat(Level), Offset)\n"
948 "// https://www.opengl.org/sdk/docs/man/html/textureGrad.xhtml\n"
949 "#define SampleGrad_4(Tex, Sampler, Coords, DDX, DDY) textureGrad (Tex, _ToVec(Coords), _ToVec(DDX), _ToVec(DDY))\n"
950 "#define SampleGrad_5(Tex, Sampler, Coords, DDX, DDY, Offset) textureGradOffset(Tex, _ToVec(Coords), _ToVec(DDX), _ToVec(DDY), Offset)\n"
953 "// texelFetch performs a lookup of a single texel from texture coordinate P in the texture \n"
954 "// bound to sampler. The array layer is specified in the last component of P for array forms. \n"
955 "// The lod parameter (if present) specifies the level-of-detail from which the texel will be fetched. \n"
956 "// The sample specifies which sample within the texel will be returned when reading from a multi-sample texure.\n"
958 "#define LoadTex1D_1(Tex, Location) texelFetch (Tex, _ToInt((Location).x), _ToInt((Location).y))\n"
959 "#define LoadTex1D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToInt((Location).x), _ToInt((Location).y), int(Offset))\n"
960 "#define LoadTex1DArr_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xy), _ToInt((Location).z) )\n"
961 "#define LoadTex1DArr_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xy), _ToInt((Location).z), int(Offset))\n"
962 "#define LoadTex2D_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xy), _ToInt((Location).z))\n"
963 "#define LoadTex2D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xy), _ToInt((Location).z), ivec2( (Offset).xy) )\n"
964 "#define LoadTex2DArr_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xyz), _ToInt((Location).w) )\n"
965 "#define LoadTex2DArr_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xyz), _ToInt((Location).w), ivec2( (Offset).xy))\n"
966 "#define LoadTex3D_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xyz), _ToInt((Location).w))\n"
967 "#define LoadTex3D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xyz), _ToInt((Location).w), ivec3( (Offset).xyz))\n"
968 "#define LoadTex2DMS_2(Tex, Location, Sample) texelFetch(Tex, _ToIvec( (Location).xy), _ToInt(Sample))\n"
969 "#define LoadTex2DMS_3(Tex, Location, Sample, Offset)texelFetch(Tex, _ToIvec2( (Location).x + (Offset).x, (Location).y + (Offset).y), int(Sample) ) // No texelFetchOffset for texture2DMS\n"
970 "#define LoadTex2DMSArr_2(Tex, Location, Sample) texelFetch(Tex, _ToIvec( (Location).xyz), _ToInt(Sample))\n"
971 "#define LoadTex2DMSArr_3(Tex, Location, Sample, Offset)texelFetch(Tex, _ToIvec3( (Location).x + (Offset).x, (Location).y + (Offset).y, (Location).z), int(Sample)) // No texelFetchOffset for texture2DMSArray\n"
972 "#define LoadTexBuffer_1(Tex, Location) texelFetch(Tex, _ToInt(Location))\n"
974 "//https://www.opengl.org/sdk/docs/man/html/imageLoad.xhtml\n"
975 "#define LoadRWTex1D_1(Tex, Location) imageLoad(Tex, _ToInt(Location) )\n"
976 "#define LoadRWTex1DArr_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xy) )\n"
977 "#define LoadRWTex2D_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xy) )\n"
978 "#define LoadRWTex2DArr_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xyz) )\n"
979 "#define LoadRWTex3D_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xyz) )\n"
980 "#define LoadRWTexBuffer_1(Tex, Location)imageLoad(Tex, _ToInt(Location) )\n"
982 "#define Gather_2(Tex, Sampler, Location) textureGather (Tex, _ToVec(Location))\n"
983 "#define Gather_3(Tex, Sampler, Location, Offset)textureGatherOffset(Tex, _ToVec(Location), Offset)\n"
985 "#define GatherCmp_3(Tex, Sampler, Location, CompareVal) textureGather (Tex, _ToVec(Location), _ToFloat(CompareVal))\n"
986 "#define GatherCmp_4(Tex, Sampler, Location, CompareVal, Offset)textureGatherOffset(Tex, _ToVec(Location), _ToFloat(CompareVal), Offset)\n"
988 "// Atomic operations\n"
989 "#define InterlockedAddSharedVar_2(dest, value) atomicAdd(dest, value)\n"
990 "#define InterlockedAddSharedVar_3(dest, value, orig_val) orig_val = atomicAdd(dest, value)\n"
991 "#define InterlockedAddImage_2(img, coords, value) imageAtomicAdd(img, _ToIvec(coords), value)\n"
992 "#define InterlockedAddImage_3(img, coords, value, orig_val)orig_val = imageAtomicAdd(img, _ToIvec(coords), value)\n"
994 "#define InterlockedAndSharedVar_2(dest, value) atomicAnd(dest, value)\n"
995 "#define InterlockedAndSharedVar_3(dest, value, orig_val) orig_val = atomicAnd(dest, value)\n"
996 "#define InterlockedAndImage_2(img, coords, value) imageAtomicAnd(img, _ToIvec(coords), value)\n"
997 "#define InterlockedAndImage_3(img, coords, value, orig_val)orig_val = imageAtomicAnd(img, _ToIvec(coords), value)\n"
999 "#define InterlockedMaxSharedVar_2(dest, value) atomicMax(dest, value)\n"
1000 "#define InterlockedMaxSharedVar_3(dest, value, orig_val) orig_val = atomicMax(dest, value)\n"
1001 "#define InterlockedMaxImage_2(img, coords, value) imageAtomicMax(img, _ToIvec(coords), value)\n"
1002 "#define InterlockedMaxImage_3(img, coords, value, orig_val)orig_val = imageAtomicMax(img, _ToIvec(coords), value)\n"
1004 "#define InterlockedMinSharedVar_2(dest, value) atomicMin(dest, value)\n"
1005 "#define InterlockedMinSharedVar_3(dest, value, orig_val) orig_val = atomicMin(dest, value)\n"
1006 "#define InterlockedMinImage_2(img, coords, value) imageAtomicMin(img, _ToIvec(coords), value)\n"
1007 "#define InterlockedMinImage_3(img, coords, value, orig_val)orig_val = imageAtomicMin(img, _ToIvec(coords), value)\n"
1009 "#define InterlockedOrSharedVar_2(dest, value) atomicOr(dest, value)\n"
1010 "#define InterlockedOrSharedVar_3(dest, value, orig_val) orig_val = atomicOr(dest, value)\n"
1011 "#define InterlockedOrImage_2(img, coords, value) imageAtomicOr(img, _ToIvec(coords), value)\n"
1012 "#define InterlockedOrImage_3(img, coords, value, orig_val)orig_val = imageAtomicOr(img, _ToIvec(coords), value)\n"
1014 "#define InterlockedXorSharedVar_2(dest, value) atomicXor(dest, value)\n"
1015 "#define InterlockedXorSharedVar_3(dest, value, orig_val) orig_val = atomicXor(dest, value)\n"
1016 "#define InterlockedXorImage_2(img, coords, value) imageAtomicXor(img, _ToIvec(coords), value)\n"
1017 "#define InterlockedXorImage_3(img, coords, value, orig_val)orig_val = imageAtomicXor(img, _ToIvec(coords), value)\n"
1019 "// There is actually no InterlockedExchange() with 2 arguments\n"
1020 "#define InterlockedExchangeSharedVar_2(dest, value) atomicExchange(dest, value)\n"
1021 "#define InterlockedExchangeSharedVar_3(dest, value, orig_val) orig_val = atomicExchange(dest, value)\n"
1022 "#define InterlockedExchangeImage_2(img, coords, value) imageAtomicExchange(img, _ToIvec(coords), value)\n"
1023 "#define InterlockedExchangeImage_3(img, coords, value, orig_val)orig_val = imageAtomicExchange(img, _ToIvec(coords), value)\n"
1025 "//uint imageAtomicCompSwap( image img, IVec P, nint compare, nint data);\n"
1026 "//void InterlockedCompareExchange( in R dest, in T compare_value, in T value, out T original_value);\n"
1027 "#define InterlockedCompareExchangeSharedVar_4(dest, cmp_val, value, orig_val) orig_val = atomicCompSwap(dest, cmp_val, value)\n"
1028 "#define InterlockedCompareExchangeImage_4(img, coords, cmp_val, value, orig_val) orig_val = imageAtomicCompSwap(img, _ToIvec(coords), cmp_val, value)\n"
1030 "#define InterlockedCompareStoreSharedVar_3(dest, cmp_val, value) atomicCompSwap(dest, cmp_val, value)\n"
1031 "#define InterlockedCompareStoreImage_3(img, coords, cmp_val, value)imageAtomicCompSwap(img, _ToIvec(coords), cmp_val, value)\n"
1034 "// Swizzling macros\n"
1035 "#define _SWIZZLE0\n"
1036 "#define _SWIZZLE1 .x\n"
1037 "#define _SWIZZLE2 .xy\n"
1038 "#define _SWIZZLE3 .xyz\n"
1039 "#define _SWIZZLE4 .xyzw\n"
1041 "// Helper functions\n"
1045 "#define NDC_MIN_Z 0.0 // Minimal z in the normalized device space\n"
1047 "// Note that Vulkan itself does not invert Y coordinate when transforming\n"
1048 "// normalized device Y to window space. However, we use negative viewport\n"
1049 "// height which achieves the same effect as in D3D, thererfore we need to\n"
1050 "// invert y (see comments in DeviceContextVkImpl::CommitViewports() for details)\n"
1051 "#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, -0.5, 1.0)\n"
1055 "#define NDC_MIN_Z -1.0 // Minimal z in the normalized device space\n"
1056 "#define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5)\n"
1060 "float2 NormalizedDeviceXYToTexUV( float2 f2ProjSpaceXY )\n"
1062 " return float2(0.5,0.5) + F3NDC_XYZ_TO_UVD_SCALE.xy * f2ProjSpaceXY.xy;\n"
1064 "float2 TexUVToNormalizedDeviceXY( float2 TexUV)\n"
1066 " return (TexUV.xy - float2(0.5, 0.5)) / F3NDC_XYZ_TO_UVD_SCALE.xy;\n"
1069 "float NormalizedDeviceZToDepth(float fNDC_Z)\n"
1071 " return (fNDC_Z - NDC_MIN_Z) * F3NDC_XYZ_TO_UVD_SCALE.z;\n"
1073 "float DepthToNormalizedDeviceZ(float fDepth)\n"
1075 " return fDepth / F3NDC_XYZ_TO_UVD_SCALE.z + NDC_MIN_Z;\n"
1078 "#define MATRIX_ELEMENT(mat, row, col) mat[col][row]\n"
1080 "float4x4 MatrixFromRows(float4 row0, float4 row1, float4 row2, float4 row3)\n"
1082 " return transpose(float4x4(row0, row1, row2, row3));\n"
1085 "float3x3 MatrixFromRows(float3 row0, float3 row1, float3 row2)\n"
1087 " return transpose(float3x3(row0, row1, row2));\n"
1090 "float2x2 MatrixFromRows(float2 row0, float2 row1)\n"
1092 " return transpose(float2x2(row0, row1));\n"
1095 "// ---------------------------------- Vertex shader ----------------------------------\n"
1096 "#ifdef VERTEX_SHADER\n"
1099 "out gl_PerVertex\n"
1101 " vec4 gl_Position;\n"
1105 "#define _GET_GL_VERTEX_ID(VertexId)_TypeConvertStore(VertexId, gl_VertexID)\n"
1106 "#define _GET_GL_INSTANCE_ID(InstId)_TypeConvertStore(InstId, gl_InstanceID)\n"
1107 "#define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)\n"
1112 "// --------------------------------- Fragment shader ---------------------------------\n"
1113 "#ifdef FRAGMENT_SHADER\n"
1115 "// SV_Position.w == w, while gl_FragCoord.w == 1/w\n"
1116 "#define _GET_GL_FRAG_COORD(FragCoord)_ResizeVector(FragCoord, vec4(gl_FragCoord.xyz, 1.0/gl_FragCoord.w))\n"
1117 "#define _GET_GL_FRONT_FACING(FrontFacing)_TypeConvertStore(FrontFacing, gl_FrontFacing)\n"
1118 "#define _SET_GL_FRAG_DEPTH(Depth)_TypeConvertStore(gl_FragDepth, Depth)\n"
1123 "// --------------------------------- Geometry shader ---------------------------------\n"
1124 "#ifdef GEOMETRY_SHADER\n"
1126 "// ARB_separate_shader_objects requires built-in block gl_PerVertex to be redeclared before accessing its members\n"
1127 "// declaring gl_PointSize and gl_ClipDistance causes compilation error on Android\n"
1130 " vec4 gl_Position;\n"
1131 " //float gl_PointSize;\n"
1132 " //float gl_ClipDistance[];\n"
1135 "out gl_PerVertex\n"
1137 " vec4 gl_Position;\n"
1138 " //float gl_PointSize;\n"
1139 " //float gl_ClipDistance[];\n"
1142 "#define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)\n"
1143 "#define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_in[i].gl_PrimitiveIDIn)\n"
1145 "#define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)\n"
1146 "#define _SET_GL_LAYER(Layer)_TypeConvertStore(gl_Layer,Layer)\n"
1151 "// --------------------------- Tessellation control shader ---------------------------\n"
1152 "#ifdef TESS_CONTROL_SHADER\n"
1156 " vec4 gl_Position;\n"
1157 " //float gl_PointSize;\n"
1158 " //float gl_ClipDistance[];\n"
1159 "} gl_in[gl_MaxPatchVertices];\n"
1161 "out gl_PerVertex\n"
1163 " vec4 gl_Position;\n"
1164 " //float gl_PointSize;\n"
1165 " //float gl_ClipDistance[];\n"
1168 "#define _GET_GL_INVOCATION_ID(InvocId)_TypeConvertStore(InvocId, gl_InvocationID)\n"
1169 "#define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_PrimitiveID)\n"
1170 "#define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)\n"
1172 "#define _SET_GL_POSITION(Pos)gl_out[gl_InvocationID].gl_Position=_ExpandVector(Pos)\n"
1174 "void _SetGLTessLevelOuter(float OuterLevel[2])\n"
1176 " for(int i=0; i < 2; ++i)\n"
1177 " gl_TessLevelOuter[i] = OuterLevel[i];\n"
1179 "void _SetGLTessLevelOuter(float OuterLevel[3])\n"
1181 " for(int i=0; i < 3; ++i)\n"
1182 " gl_TessLevelOuter[i] = OuterLevel[i];\n"
1184 "void _SetGLTessLevelOuter(float OuterLevel[4])\n"
1186 " for(int i=0; i < 4; ++i)\n"
1187 " gl_TessLevelOuter[i] = OuterLevel[i];\n"
1191 "void _SetGLTessLevelInner(float InnerLevel[2])\n"
1193 " gl_TessLevelInner[0] = InnerLevel[0];\n"
1194 " gl_TessLevelInner[1] = InnerLevel[1];\n"
1196 "void _SetGLTessLevelInner(float InnerLevel)\n"
1198 " gl_TessLevelInner[0] = InnerLevel;\n"
1204 "// --------------------------- Tessellation evaluation shader ---------------------------\n"
1205 "#ifdef TESS_EVALUATION_SHADER\n"
1209 " vec4 gl_Position;\n"
1210 " //float gl_PointSize;\n"
1211 " //float gl_ClipDistance[];\n"
1212 "} gl_in[gl_MaxPatchVertices];\n"
1214 "out gl_PerVertex\n"
1216 " vec4 gl_Position;\n"
1217 " //float gl_PointSize;\n"
1218 " //float gl_ClipDistance[];\n"
1221 "#define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)\n"
1223 "void _GetGLTessLevelOuter(out float OuterLevel[2])\n"
1225 " for(int i=0; i < 2; ++i)\n"
1226 " OuterLevel[i] = gl_TessLevelOuter[i];\n"
1228 "void _GetGLTessLevelOuter(out float OuterLevel[3])\n"
1230 " for(int i=0; i < 3; ++i)\n"
1231 " OuterLevel[i] = gl_TessLevelOuter[i];\n"
1233 "void _GetGLTessLevelOuter(out float OuterLevel[4])\n"
1235 " for(int i=0; i < 4; ++i)\n"
1236 " OuterLevel[i] = gl_TessLevelOuter[i];\n"
1239 "void _GetGLTessLevelInner(out float InnerLevel[2])\n"
1241 " InnerLevel[0] = gl_TessLevelInner[0];\n"
1242 " InnerLevel[1] = gl_TessLevelInner[1];\n"
1244 "void _GetGLTessLevelInner(out float InnerLevel)\n"
1246 " InnerLevel = gl_TessLevelInner[0];\n"
1249 "#define _GET_GL_TESS_COORD(TessCoord)_ResizeVector(TessCoord, gl_TessCoord)\n"
1250 "#define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_PrimitiveID)\n"
1252 "#define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)\n"
1257 "// ---------------------------------- Compute shader ----------------------------------\n"
1258 "#ifdef COMPUTE_SHADER\n"
1260 "#define _GET_GL_GLOBAL_INVOCATION_ID(Type, InvocId)InvocId=Type(gl_GlobalInvocationID)\n"
1261 "#define _GET_GL_WORK_GROUP_ID(Type, GroupId)GroupId=Type(gl_WorkGroupID)\n"
1262 "#define _GET_GL_LOCAL_INVOCATION_ID(Type, InvocId)InvocId=Type(gl_LocalInvocationID)\n"
1263 "#define _GET_GL_LOCAL_INVOCATION_INDEX(Type, InvocInd)InvocInd=Type(gl_LocalInvocationIndex)\n"
1267 "#endif // _GLSL_DEFINITIONS_\n"