Diligent Engine  v.2.4.g
GLSLDefinitions.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019-2021 Diligent Graphics LLC
3  * Copyright 2015-2019 Egor Yusov
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * In no event and under no legal theory, whether in tort (including negligence),
18  * contract, or otherwise, unless required by applicable law (such as deliberate
19  * and grossly negligent acts) or agreed to in writing, shall any Contributor be
20  * liable for any damages, including any direct, indirect, special, incidental,
21  * or consequential damages of any character arising as a result of this License or
22  * out of the use or inability to use the software (including but not limited to damages
23  * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
24  * all other commercial damages or losses), even if such Contributor has been advised
25  * of the possibility of such damages.
26  */
27 
28 #ifndef _GLSL_DEFINITIONS_
29 #define _GLSL_DEFINITIONS_
30 
31 #define GLSL
32 
33 #ifdef GL_ES
34 // From GLES 3.1 spec:
35 // Except for image variables qualified with the format qualifiers r32f, r32i, and r32ui,
36 // image variables must specify either memory qualifier readonly or the memory qualifier writeonly.
37 # define IMAGE_WRITEONLY writeonly
38 #else
39 # define IMAGE_WRITEONLY
40 #endif
41 
42 #define float4 vec4
43 #define float3 vec3
44 #define float2 vec2
45 
46 #define int4 ivec4
47 #define int3 ivec3
48 #define int2 ivec2
49 
50 #define uint4 uvec4
51 #define uint3 uvec3
52 #define uint2 uvec2
53 
54 #define bool4 bvec4
55 #define bool3 bvec3
56 #define bool2 bvec2
57 
58 // OpenGL matrices in GLSL are always as column-major
59 // (this is not related to how they are stored)
60 #define float2x2 mat2x2
61 #define float2x3 mat3x2
62 #define float2x4 mat4x2
63 
64 #define float3x2 mat2x3
65 #define float3x3 mat3x3
66 #define float3x4 mat4x3
67 
68 #define float4x2 mat2x4
69 #define float4x3 mat3x4
70 #define float4x4 mat4x4
71 #define matrix mat4x4
72 
73 #define static
74 
75 #define SamplerState int
76 #define SamplerComparisonState int
77 
78 // https://www.opengl.org/wiki/Memory_Model#Incoherent_memory_access
79 // Shared variable access uses the rules for incoherent memory access.
80 // This means that the user must perform certain synchronization in
81 // order to ensure that shared variables are visible.
82 // At the same time, shared variables are all implicitly declared coherent,
83 // so one don't need to (and can't) use that qualifier.
84 #define groupshared shared
85 
86 #ifdef FRAGMENT_SHADER
87 # define ddx dFdx
88 # define ddy dFdy
89 #else
90 # define ddx(x) (x) // GLSL compiler fails when it sees derivatives
91 # define ddy(x) (x) // in any shader but fragment
92 #endif
93 
94 #define ddx_coarse ddx
95 #define ddy_coarse ddy
96 #define ddx_fine ddx
97 #define ddy_fine ddy
98 
99 #define mul(a, b) ((a)*(b))
100 #define frac fract
101 #define atan2 atan
102 #define rsqrt inversesqrt
103 #define fmod mod
104 #define lerp mix
105 #define dst distance
106 #define countbits bitCount
107 #define firstbithigh findMSB
108 #define firstbitlow findLSB
109 #define reversebits bitfieldReverse
110 
111 float rcp( float x ){ return 1.0 / x; }
112 vec2 rcp( vec2 x ){ return vec2(1.0,1.0) / x; }
113 vec3 rcp( vec3 x ){ return vec3(1.0,1.0,1.0) / x; }
114 vec4 rcp( vec4 x ){ return vec4(1.0,1.0,1.0,1.0) / x; }
115 
116 float saturate( float x ){ return clamp( x, 0.0, 1.0 ); }
117 vec2 saturate( vec2 x ){ return clamp( x, vec2(0.0, 0.0), vec2(1.0, 1.0) ); }
118 vec3 saturate( vec3 x ){ return clamp( x, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0) ); }
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) ); }
120 
121 void sincos( float x, out float s, out float c ){ s = sin( x ); c = cos( x ); }
122 void sincos( vec2 x, out vec2 s, out vec2 c ){ s = sin( x ); c = cos( x ); }
123 void sincos( vec3 x, out vec3 s, out vec3 c ){ s = sin( x ); c = cos( x ); }
124 void sincos( vec4 x, out vec4 s, out vec4 c ){ s = sin( x ); c = cos( x ); }
125 
126 
127 // Bit conversion operations
128 
129 float asfloat( float x ){ return x; }
130 vec2 asfloat( vec2 x ){ return x; }
131 vec3 asfloat( vec3 x ){ return x; }
132 vec4 asfloat( vec4 x ){ return x; }
133 
134 float asfloat( int x ){ return intBitsToFloat(x); }
135 vec2 asfloat( ivec2 x ){ return intBitsToFloat(x); }
136 vec3 asfloat( ivec3 x ){ return intBitsToFloat(x); }
137 vec4 asfloat( ivec4 x ){ return intBitsToFloat(x); }
138 
139 float asfloat( uint x ){ return uintBitsToFloat(x); }
140 vec2 asfloat( uvec2 x ){ return uintBitsToFloat(x); }
141 vec3 asfloat( uvec3 x ){ return uintBitsToFloat(x); }
142 vec4 asfloat( uvec4 x ){ return uintBitsToFloat(x); }
143 
144 
145 int asint( int x ){ return x; }
146 ivec2 asint( ivec2 x ){ return x; }
147 ivec3 asint( ivec3 x ){ return x; }
148 ivec4 asint( ivec4 x ){ return x; }
149 
150 int asint( uint x ){ return int(x); }
151 ivec2 asint( uvec2 x ){ return ivec2(x); }
152 ivec3 asint( uvec3 x ){ return ivec3(x); }
153 ivec4 asint( uvec4 x ){ return ivec4(x); }
154 
155 int asint( float x ){ return floatBitsToInt(x); }
156 ivec2 asint( vec2 x ){ return floatBitsToInt(x); }
157 ivec3 asint( vec3 x ){ return floatBitsToInt(x); }
158 ivec4 asint( vec4 x ){ return floatBitsToInt(x); }
159 
160 
161 uint asuint( uint x ){ return x; }
162 uvec2 asuint( uvec2 x ){ return x; }
163 uvec3 asuint( uvec3 x ){ return x; }
164 uvec4 asuint( uvec4 x ){ return x; }
165 
166 uint asuint( int x ){ return uint(x); }
167 uvec2 asuint( ivec2 x ){ return uvec2(x); }
168 uvec3 asuint( ivec3 x ){ return uvec3(x); }
169 uvec4 asuint( ivec4 x ){ return uvec4(x); }
170 
171 uint asuint( float x ){ return floatBitsToUint(x); }
172 uvec2 asuint( vec2 x ){ return floatBitsToUint(x); }
173 uvec3 asuint( vec3 x ){ return floatBitsToUint(x); }
174 uvec4 asuint( vec4 x ){ return floatBitsToUint(x); }
175 
176 #if defined(GL_ES) && (__VERSION__>=310) || !defined(GL_ES) && (__VERSION__>=420)
177 float f16tof32( uint u1 )
178 {
179  return unpackHalf2x16( u1 ).x;
180 }
181 vec2 f16tof32( uvec2 u2 )
182 {
183  uint u2PackedHalf = (u2.x & 0x0ffffu) | ((u2.y & 0x0ffffu) << 16u);
184  return unpackHalf2x16( u2PackedHalf );
185 }
186 vec3 f16tof32( uvec3 u3 )
187 {
188  return vec3( f16tof32( u3.xy ), f16tof32( u3.z ) );
189 }
190 vec4 f16tof32( uvec4 u4 )
191 {
192  return vec4( f16tof32( u4.xy ), f16tof32( u4.zw ) );
193 }
194 float f16tof32( int i1 ){ return f16tof32( uint ( i1 ) ); }
195 vec2 f16tof32( ivec2 i2 ){ return f16tof32( uvec2( i2 ) ); }
196 vec3 f16tof32( ivec3 i3 ){ return f16tof32( uvec3( i3 ) ); }
197 vec4 f16tof32( ivec4 i4 ){ return f16tof32( uvec4( i4 ) ); }
198 
199 uint f32tof16( float f )
200 {
201  return packHalf2x16( vec2( f, 0.0 ) ) & 0x0ffffu;
202 }
203 uvec2 f32tof16( vec2 f2 )
204 {
205  uint u2PackedHalf = packHalf2x16( f2 );
206  return uvec2( u2PackedHalf & 0x0ffffu, u2PackedHalf >> 16u );
207 }
208 uvec3 f32tof16( vec3 f3 )
209 {
210  return uvec3( f32tof16( f3.xy ), f32tof16( f3.z ) );
211 }
212 uvec4 f32tof16( vec4 f4 )
213 {
214  return uvec4( f32tof16( f4.xy ), f32tof16( f4.zw ) );
215 }
216 #endif
217 
218 // Use #define as double is not supported on GLES
219 #define asdouble(lowbits, highbits) packDouble2x32( uvec2( lowbits, highbits ) )
220 
221 // Floating point functions
222 
223 bool isfinite( float x )
224 {
225  return !isinf( x ) && !isnan( x );
226 }
227 
228 bool2 isfinite( vec2 f2 )
229 {
230  return bool2( isfinite( f2.x ), isfinite( f2.y ) );
231 }
232 
233 bool3 isfinite( vec3 f3 )
234 {
235  return bool3( isfinite( f3.xy ), isfinite( f3.z ) );
236 }
237 
238 bool4 isfinite( vec4 f4 )
239 {
240  return bool4( isfinite( f4.xyz ), isfinite( f4.w ) );
241 }
242 
243 float log10( float x )
244 {
245  return log( x ) / log( 10.0 );
246 }
247 vec2 log10( vec2 x )
248 {
249  float _lg10 = log( 10.0 );
250  return log( x ) / vec2(_lg10, _lg10);
251 }
252 vec3 log10( vec3 x )
253 {
254  float _lg10 = log( 10.0 );
255  return log( x ) / vec3(_lg10, _lg10, _lg10);
256 }
257 vec4 log10( vec4 x )
258 {
259  float _lg10 = log( 10.0 );
260  return log( x ) / vec4(_lg10, _lg10, _lg10, _lg10);
261 }
262 
263 
264 #ifdef GL_ES
265 # define mad(a,b,c) ((a)*(b)+(c))
266 #else
267 # define mad fma
268 #endif
269 
270 
271 // Relational and logical operators
272 #define Less lessThan
273 #define LessEqual lessThanEqual
274 #define Greater greaterThan
275 #define GreaterEqual greaterThanEqual
276 #define Equal equal
277 #define NotEqual notEqual
278 #define Not not
280 {
281  return bool4(L.x && R.x,
282  L.y && R.y,
283  L.z && R.z,
284  L.w && R.w);
285 }
287 {
288  return bool3(L.x && R.x,
289  L.y && R.y,
290  L.z && R.z);
291 }
293 {
294  return bool2(L.x && R.x,
295  L.y && R.y);
296 }
297 bool And(bool L, bool R)
298 {
299  return (L && R);
300 }
301 
302 
304 {
305  return bool4(L.x || R.x,
306  L.y || R.y,
307  L.z || R.z,
308  L.w || R.w);
309 }
311 {
312  return bool3(L.x || R.x,
313  L.y || R.y,
314  L.z || R.z);
315 }
317 {
318  return bool2(L.x || R.x,
319  L.y || R.y);
320 }
321 bool Or(bool L, bool R)
322 {
323  return (L || R);
324 }
325 
327 {
328  return float4(b4.x ? 1.0 : 0.0,
329  b4.y ? 1.0 : 0.0,
330  b4.z ? 1.0 : 0.0,
331  b4.w ? 1.0 : 0.0);
332 }
334 {
335  return float3(b3.x ? 1.0 : 0.0,
336  b3.y ? 1.0 : 0.0,
337  b3.z ? 1.0 : 0.0);
338 }
340 {
341  return float2(b2.x ? 1.0 : 0.0,
342  b2.y ? 1.0 : 0.0);
343 }
344 float BoolToFloat( bool b )
345 {
346  return b ? 1.0 : 0.0;
347 }
348 
349 
350 // Synchronization functions
351 
352 #ifdef COMPUTE_SHADER
353 
354 // https://www.opengl.org/wiki/Memory_Model#Incoherent_memory_access
355 
356 // MSDN: GroupMemoryBarrier() blocks execution of all threads
357 // in a group until all group SHARED accesses have been completed.
358 void GroupMemoryBarrier()
359 {
360  // OpenGL.org: groupMemoryBarrier() waits on the completion of all memory accesses
361  // performed by an invocation of a compute shader relative to the same access performed
362  // by other invocations in the same work group and then returns with no other effect.
363 
364  // groupMemoryBarrier() acts like memoryBarrier(), ordering memory writes for all kinds
365  // of variables, but it only orders read/writes for the current work group.
366  groupMemoryBarrier();
367 
368  // OpenGL.org: memoryBarrierShared() waits on the completion of
369  // all memory accesses resulting from the use of SHARED variables
370  // and then returns with no other effect.
371  memoryBarrierShared();
372 }
373 
374 // MSDN: GroupMemoryBarrierWithGroupSync() blocks execution of all
375 // threads in a group until all memory accesses have been completed
376 // and all threads in the group have reached this call.
378 {
379  // Issue memory barrier first!
381  barrier();
382 }
383 
384 // MSDN: DeviceMemoryBarrier() blocks execution of all threads
385 // in a group until all device memory accesses have been completed.
386 void DeviceMemoryBarrier()
387 {
388  // Call all memory barriers except for shared memory
389 
390  // Do we need to call groupMemoryBarrier() ?????
391 
392  // OpenGL.org: memoryBarrierBuffer() waits on the completion of
393  // all memory accesses resulting from the use of BUFFER variables
394  // and then returns with no other effect
395  memoryBarrierBuffer();
396 
397  // OpenGL.org: memoryBarrierImage() waits on the completion of all
398  // memory accesses resulting from the use of IMAGE variables and then
399  // returns with no other effect.
400  memoryBarrierImage();
401 
402  // OpenGL.org: memoryBarrierAtomicCounter() waits on the completion of
403  // all accesses resulting from the use of ATOMIC COUNTERS and then returns
404  // with no other effect.
405  memoryBarrierAtomicCounter();
406 }
407 
408 // MSDN: DeviceMemoryBarrierWithGroupSync() blocks execution of
409 // all threads in a group until all device memory accesses have
410 // been completed and all threads in the group have reached this call.
412 {
414  barrier();
415 }
416 
417 // MSDN: AllMemoryBarrier() blocks execution of all threads in a
418 // group until all memory accesses have been completed.
419 void AllMemoryBarrier()
420 {
421  // OpenGL.org: memoryBarrier() waits on the completion of ALL
422  // memory accesses resulting from the use of IMAGE variables or
423  // ATOMIC COUNTERS and then returns with no other effect.
424  memoryBarrier();
425  // NOTE: nothing is said about buffer memory and shared memory,
426  // so call memoryBarrierBuffer() and memoryBarrierShared() for safety
427 
428  // OpenGL.org: memoryBarrierBuffer() waits on the completion of
429  // all memory accesses resulting from the use of BUFFER variables
430  // and then returns with no other effect
431  memoryBarrierBuffer();
432 
433  // OpenGL.org: memoryBarrierShared() waits on the completion of
434  // all memory accesses resulting from the use of SHARED variables
435  // and then returns with no other effect.
436  memoryBarrierShared();
437 
438  // Call all memory barrier functions. They should have no effect
439  // if everything is synchronized.
440 
441  // OpenGL.org: memoryBarrierImage() waits on the completion of all
442  // memory accesses resulting from the use of IMAGE variables and then
443  // returns with no other effect.
444  memoryBarrierImage();
445 
446  // OpenGL.org: memoryBarrierAtomicCounter() waits on the completion of
447  // all accesses resulting from the use of ATOMIC COUNTERS and then returns
448  // with no other effect.
449  memoryBarrierAtomicCounter();
450 
451  // groupMemoryBarrier waits on the completion of all memory accesses performed
452  // by an invocation of a compute shader relative to the same access performed by
453  // other invocations in the same work group and then returns with no other effect.
454  groupMemoryBarrier();
455 }
456 
457 // MSDN: AllMemoryBarrierWithGroupSync() blocks execution of all
458 // threads in a group until all memory accesses have been completed
459 // and all threads in the group have reached this call.
461 {
463  barrier();
464 }
465 
466 #else
467 
474 
475 #endif
476 
477 
478 // Type conversion functions
479 
480 vec4 _ExpandVector( float x ){ return vec4( x, x, x, x ); }
481 vec4 _ExpandVector( vec2 f2 ){ return vec4( f2.x, f2.y, 0.0, 0.0 ); }
482 vec4 _ExpandVector( vec3 f3 ){ return vec4( f3.x, f3.y, f3.z, 0.0 ); }
483 vec4 _ExpandVector( vec4 f4 ){ return vec4( f4.x, f4.y, f4.z, f4.w ); }
484 
485 ivec4 _ExpandVector( int x ){ return ivec4( x, x, x, x ); }
486 ivec4 _ExpandVector( ivec2 i2 ){ return ivec4( i2.x, i2.y, 0, 0 ); }
487 ivec4 _ExpandVector( ivec3 i3 ){ return ivec4( i3.x, i3.y, i3.z, 0 ); }
488 ivec4 _ExpandVector( ivec4 i4 ){ return ivec4( i4.x, i4.y, i4.z, i4.w ); }
489 
490 uvec4 _ExpandVector( uint x ){ return uvec4( x, x, x, x ); }
491 uvec4 _ExpandVector( uvec2 u2 ){ return uvec4( u2.x, u2.y, 0u, 0u ); }
492 uvec4 _ExpandVector( uvec3 u3 ){ return uvec4( u3.x, u3.y, u3.z, 0u ); }
493 uvec4 _ExpandVector( uvec4 u4 ){ return uvec4( u4.x, u4.y, u4.z, u4.w ); }
494 
495 bvec4 _ExpandVector( bool x ){ return bvec4( x, x, x, x ); }
496 bvec4 _ExpandVector( bvec2 b2 ){ return bvec4( b2.x, b2.y, false, false ); }
497 bvec4 _ExpandVector( bvec3 b3 ){ return bvec4( b3.x, b3.y, b3.z, false ); }
498 bvec4 _ExpandVector( bvec4 b4 ){ return bvec4( b4.x, b4.y, b4.z, b4.w ); }
499 
500 void _ResizeVector(out vec4 outVec4, in vec4 inVec4){outVec4 = inVec4;}
501 void _ResizeVector(out vec3 outVec3, in vec4 inVec4){outVec3 = inVec4.xyz;}
502 void _ResizeVector(out vec2 outVec2, in vec4 inVec4){outVec2 = inVec4.xy;}
503 void _ResizeVector(out float outFlt, in vec4 inVec4){outFlt = inVec4.x;}
504 
505 void _ResizeVector(out vec4 outVec4, in vec3 inVec3){outVec4 = vec4(inVec3, 0.0);}
506 void _ResizeVector(out vec3 outVec3, in vec3 inVec3){outVec3 = inVec3;}
507 void _ResizeVector(out vec2 outVec2, in vec3 inVec3){outVec2 = inVec3.xy;}
508 void _ResizeVector(out float outFlt, in vec3 inVec3){outFlt = inVec3.x;}
509 
510 void _ResizeVector(out vec4 outVec4, in vec2 inVec2){outVec4 = vec4(inVec2, 0.0, 0.0);}
511 void _ResizeVector(out vec3 outVec3, in vec2 inVec2){outVec3 = vec3(inVec2, 0.0);}
512 void _ResizeVector(out vec2 outVec2, in vec2 inVec2){outVec2 = inVec2;}
513 void _ResizeVector(out float outFlt, in vec2 inVec2){outFlt = inVec2.x;}
514 
515 void _ResizeVector(out vec4 outVec4, in float v){outVec4 = vec4(v, 0.0, 0.0, 0.0);}
516 void _ResizeVector(out vec3 outVec3, in float v){outVec3 = vec3(v, 0.0, 0.0);}
517 void _ResizeVector(out vec2 outVec2, in float v){outVec2 = vec2(v, 0.0);}
518 void _ResizeVector(out float outFlt, in float v){outFlt = v;}
519 
520 
521 void _TypeConvertStore( out float Dst, in int Src ){ Dst = float( Src ); }
522 void _TypeConvertStore( out float Dst, in uint Src ){ Dst = float( Src ); }
523 void _TypeConvertStore( out float Dst, in float Src ){ Dst = float( Src ); }
524 void _TypeConvertStore( out float Dst, in bool Src ){ Dst = Src ? 1.0 : 0.0; }
525 
526 void _TypeConvertStore( out uint Dst, in int Src ){ Dst = uint( Src ); }
527 void _TypeConvertStore( out uint Dst, in uint Src ){ Dst = uint( Src ); }
528 void _TypeConvertStore( out uint Dst, in float Src ){ Dst = uint( Src ); }
529 void _TypeConvertStore( out uint Dst, in bool Src ){ Dst = Src ? 1u : 0u; }
530 
531 void _TypeConvertStore( out int Dst, in int Src ){ Dst = int( Src ); }
532 void _TypeConvertStore( out int Dst, in uint Src ){ Dst = int( Src ); }
533 void _TypeConvertStore( out int Dst, in float Src ){ Dst = int( Src ); }
534 void _TypeConvertStore( out int Dst, in bool Src ){ Dst = Src ? 1 : 0; }
535 
536 void _TypeConvertStore( out bool Dst, in int Src ){ Dst = (Src != 0); }
537 void _TypeConvertStore( out bool Dst, in uint Src ){ Dst = (Src != 0u); }
538 void _TypeConvertStore( out bool Dst, in float Src ){ Dst = (Src != 0.0); }
539 void _TypeConvertStore( out bool Dst, in bool Src ){ Dst = Src; }
540 
541 
542 int _ToInt( int x ) { return int(x); }
543 int _ToInt( ivec2 v ) { return int(v.x); }
544 int _ToInt( ivec3 v ) { return int(v.x); }
545 int _ToInt( ivec4 v ) { return int(v.x); }
546 
547 int _ToInt( uint x ) { return int(x); }
548 int _ToInt( uvec2 v ) { return int(v.x); }
549 int _ToInt( uvec3 v ) { return int(v.x); }
550 int _ToInt( uvec4 v ) { return int(v.x); }
551 
552 int _ToInt( float x ) { return int(x); }
553 int _ToInt( vec2 v ) { return int(v.x); }
554 int _ToInt( vec3 v ) { return int(v.x); }
555 int _ToInt( vec4 v ) { return int(v.x); }
556 
557 int _ToInt( bool x ) { return x ? 1 : 0;}
558 int _ToInt( bvec2 v ) { return v.x ? 1 : 0;}
559 int _ToInt( bvec3 v ) { return v.x ? 1 : 0;}
560 int _ToInt( bvec4 v ) { return v.x ? 1 : 0;}
561 
562 
563 
564 float _ToFloat( int x ) { return float(x); }
565 float _ToFloat( ivec2 v ){ return float(v.x); }
566 float _ToFloat( ivec3 v ){ return float(v.x); }
567 float _ToFloat( ivec4 v ){ return float(v.x); }
568 
569 float _ToFloat( uint x ) { return float(x); }
570 float _ToFloat( uvec2 v ){ return float(v.x); }
571 float _ToFloat( uvec3 v ){ return float(v.x); }
572 float _ToFloat( uvec4 v ){ return float(v.x); }
573 
574 float _ToFloat( float x ){ return float(x); }
575 float _ToFloat( vec2 v ) { return float(v.x); }
576 float _ToFloat( vec3 v ) { return float(v.x); }
577 float _ToFloat( vec4 v ) { return float(v.x); }
578 
579 float _ToFloat( bool x ) { return x ? 1.0 : 0.0;}
580 float _ToFloat( bvec2 v ){ return v.x ? 1.0 : 0.0;}
581 float _ToFloat( bvec3 v ){ return v.x ? 1.0 : 0.0;}
582 float _ToFloat( bvec4 v ){ return v.x ? 1.0 : 0.0;}
583 
584 
585 
586 uint _ToUint( int x ) { return uint(x); }
587 uint _ToUint( uint x ) { return uint(x); }
588 uint _ToUint( float x ){ return uint(x); }
589 uint _ToUint( bool x ) { return x ? 1u : 0u; }
590 
591 bool _ToBool( int x ) { return x != 0 ? true : false; }
592 bool _ToBool( uint x ) { return x != 0u ? true : false; }
593 bool _ToBool( float x ){ return x != 0.0 ? true : false; }
594 bool _ToBool( bool x ) { return x; }
595 
596 #define _ToVec2(x,y) vec2(_ToFloat(x), _ToFloat(y))
597 #define _ToVec3(x,y,z) vec3(_ToFloat(x), _ToFloat(y), _ToFloat(z))
598 #define _ToVec4(x,y,z,w) vec4(_ToFloat(x), _ToFloat(y), _ToFloat(z), _ToFloat(w))
599 
600 #define _ToIvec2(x,y) ivec2(_ToInt(x), _ToInt(y))
601 #define _ToIvec3(x,y,z) ivec3(_ToInt(x), _ToInt(y), _ToInt(z))
602 #define _ToIvec4(x,y,z,w) ivec4(_ToInt(x), _ToInt(y), _ToInt(z), _ToInt(w))
603 
604 #define _ToUvec2(x,y) uvec2(_ToUint(x), _ToUint(y))
605 #define _ToUvec3(x,y,z) uvec3(_ToUint(x), _ToUint(y), _ToUint(z))
606 #define _ToUvec4(x,y,z,w) uvec4(_ToUint(x), _ToUint(y), _ToUint(z), _ToUint(w))
607 
608 #define _ToBvec2(x,y) bvec2(_ToBool(x), _ToBool(y))
609 #define _ToBvec3(x,y,z) bvec3(_ToBool(x), _ToBool(y), _ToBool(z))
610 #define _ToBvec4(x,y,z,w) bvec4(_ToBool(x), _ToBool(y), _ToBool(z), _ToBool(w))
611 
612 
613 int _ToIvec( uint u1 ){ return _ToInt( u1 ); }
614 ivec2 _ToIvec( uvec2 u2 ){ return _ToIvec2( u2.x, u2.y ); }
615 ivec3 _ToIvec( uvec3 u3 ){ return _ToIvec3( u3.x, u3.y, u3.z ); }
616 ivec4 _ToIvec( uvec4 u4 ){ return _ToIvec4( u4.x, u4.y, u4.z, u4.w ); }
617 
618 int _ToIvec( int i1 ){ return i1; }
619 ivec2 _ToIvec( ivec2 i2 ){ return i2; }
620 ivec3 _ToIvec( ivec3 i3 ){ return i3; }
621 ivec4 _ToIvec( ivec4 i4 ){ return i4; }
622 
623 int _ToIvec( float f1 ){ return _ToInt( f1 ); }
624 ivec2 _ToIvec( vec2 f2 ){ return _ToIvec2( f2.x, f2.y ); }
625 ivec3 _ToIvec( vec3 f3 ){ return _ToIvec3( f3.x, f3.y, f3.z ); }
626 ivec4 _ToIvec( vec4 f4 ){ return _ToIvec4( f4.x, f4.y, f4.z, f4.w ); }
627 
628 
629 float _ToVec( uint u1 ){ return _ToFloat(u1); }
630 vec2 _ToVec( uvec2 u2 ){ return _ToVec2( u2.x, u2.y ); }
631 vec3 _ToVec( uvec3 u3 ){ return _ToVec3( u3.x, u3.y, u3.z ); }
632 vec4 _ToVec( uvec4 u4 ){ return _ToVec4( u4.x, u4.y, u4.z, u4.w ); }
633 
634 float _ToVec( int i1 ){ return _ToFloat(i1); }
635 vec2 _ToVec( ivec2 i2 ){ return _ToVec2( i2.x, i2.y ); }
636 vec3 _ToVec( ivec3 i3 ){ return _ToVec3( i3.x, i3.y, i3.z ); }
637 vec4 _ToVec( ivec4 i4 ){ return _ToVec4( i4.x, i4.y, i4.z, i4.w ); }
638 
639 float _ToVec( float f1 ){ return f1; }
640 vec2 _ToVec( vec2 f2 ){ return f2; }
641 vec3 _ToVec( vec3 f3 ){ return f3; }
642 vec4 _ToVec( vec4 f4 ){ return f4; }
643 
644 
645 uint _ToUvec( uint u1 ){ return u1; }
646 uvec2 _ToUvec( uvec2 u2 ){ return u2; }
647 uvec3 _ToUvec( uvec3 u3 ){ return u3; }
648 uvec4 _ToUvec( uvec4 u4 ){ return u4; }
649 
650 uint _ToUvec( int i1 ){ return _ToUint( i1 ); }
651 uvec2 _ToUvec( ivec2 i2 ){ return _ToUvec2( i2.x, i2.y ); }
652 uvec3 _ToUvec( ivec3 i3 ){ return _ToUvec3( i3.x, i3.y, i3.z ); }
653 uvec4 _ToUvec( ivec4 i4 ){ return _ToUvec4( i4.x, i4.y, i4.z, i4.w ); }
654 
655 uint _ToUvec( float f1 ){ return _ToUint( f1 ); }
656 uvec2 _ToUvec( vec2 f2 ){ return _ToUvec2( f2.x, f2.y ); }
657 uvec3 _ToUvec( vec3 f3 ){ return _ToUvec3( f3.x, f3.y, f3.z ); }
658 uvec4 _ToUvec( vec4 f4 ){ return _ToUvec4( f4.x, f4.y, f4.z, f4.w ); }
659 
660 // https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/frexp.xhtml
661 // https://www.khronos.org/registry/OpenGL-Refpages/es3.1/html/frexp.xhtml
662 #if defined(GL_ES) && (__VERSION__>=310) || !defined(GL_ES) && (__VERSION__>=400)
663 // We have to redefine 'float frexp(float, int)' as 'float frexp(float, float)'
664 float _frexp(float f1, out float fexp1)
665 {
666  int iexp1;
667  float sig1 = frexp(f1, iexp1);
668  fexp1 = float(iexp1);
669  return sig1;
670 }
671 vec2 _frexp(vec2 f2, out vec2 fexp2)
672 {
673  ivec2 iexp2;
674  vec2 sig2 = frexp(f2, iexp2);
675  fexp2 = vec2(iexp2);
676  return sig2;
677 }
678 vec3 _frexp(vec3 f3, out vec3 fexp3)
679 {
680  ivec3 iexp3;
681  vec3 sig3 = frexp(f3, iexp3);
682  fexp3 = vec3(iexp3);
683  return sig3;
684 }
685 vec4 _frexp(vec4 f4, out vec4 fexp4)
686 {
687  ivec4 iexp4;
688  vec4 sig4 = frexp(f4, iexp4);
689  fexp4 = vec4(iexp4);
690  return sig4;
691 }
692 #define frexp _frexp
693 #endif
694 
695 
696 // TEXTURE FUNCTION STUB MACROS
697 // https://www.opengl.org/wiki/Sampler_(GLSL)
698 
699 
700 // Texture size queries
701 // https://www.opengl.org/sdk/docs/man/html/textureSize.xhtml
702 // textureSize returns the dimensions of level lod (if present) of the texture bound to sampler.
703 // The components in the return value are filled in, in order, with the width, height and depth
704 // of the texture. For the array forms, the last component of the return value is the number of
705 // layers in the texture array.
706 
707 //#if !(defined(DESKTOP_GL) && __VERSION__ >= 430)
708 # define textureQueryLevels(x) 0 // Only supported on 4.3+
709 //#endif
710 
711 #define GetTex1DDimensions_1(Sampler, Width)\
712 { \
713  _TypeConvertStore( Width, textureSize(Sampler, 0) );\
714 }
715 
716 #define GetTex1DDimensions_3(Sampler, MipLevel, Width, NumberOfMipLevels)\
717 { \
718  _TypeConvertStore( Width, textureSize(Sampler, _ToInt(MipLevel)) ); \
719  _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) ); \
720 }
721 
722 #define GetTex1DArrDimensions_2(Sampler, Width, Elements)\
723 { \
724  ivec2 i2Size = textureSize(Sampler, 0); \
725  _TypeConvertStore( Width, i2Size.x );\
726  _TypeConvertStore( Elements, i2Size.y );\
727 }
728 
729 #define GetTex1DArrDimensions_4(Sampler, MipLevel, Width, Elements, NumberOfMipLevels)\
730 { \
731  ivec2 i2Size = textureSize(Sampler, _ToInt(MipLevel)); \
732  _TypeConvertStore( Width, i2Size.x ); \
733  _TypeConvertStore( Elements, i2Size.y ); \
734  _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\
735 }
736 
737 #define GetTex2DDimensions_2(Sampler, Width, Height)\
738 { \
739  ivec2 i2Size = textureSize(Sampler, 0); \
740  _TypeConvertStore( Width, i2Size.x ); \
741  _TypeConvertStore( Height, i2Size.y ); \
742 }
743 
744 #define GetTex2DDimensions_4(Sampler, MipLevel, Width, Height, NumberOfMipLevels)\
745 { \
746  ivec2 i2Size = textureSize(Sampler, _ToInt(MipLevel) ); \
747  _TypeConvertStore( Width, i2Size.x ); \
748  _TypeConvertStore( Height, i2Size.y ); \
749  _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\
750 }
751 
752 #define GetTex2DArrDimensions_3(Sampler, Width, Height, Elements)\
753 { \
754  ivec3 i3Size = textureSize(Sampler, 0); \
755  _TypeConvertStore( Width, i3Size.x ); \
756  _TypeConvertStore( Height, i3Size.y ); \
757  _TypeConvertStore( Elements,i3Size.z ); \
758 }
759 
760 #define GetTex2DArrDimensions_5(Sampler, MipLevel, Width, Height, Elements, NumberOfMipLevels)\
761 { \
762  ivec3 i3Size = textureSize(Sampler, _ToInt(MipLevel)); \
763  _TypeConvertStore( Width, i3Size.x ); \
764  _TypeConvertStore( Height, i3Size.y ); \
765  _TypeConvertStore( Elements, i3Size.z ); \
766  _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\
767 }
768 
769 #define GetTex3DDimensions_3(Sampler, Width, Height, Depth)\
770 { \
771  ivec3 i3Size = textureSize(Sampler, 0); \
772  _TypeConvertStore( Width, i3Size.x ); \
773  _TypeConvertStore( Height, i3Size.y ); \
774  _TypeConvertStore( Depth, i3Size.z ); \
775 }
776 
777 #define GetTex3DDimensions_5(Sampler, MipLevel, Width, Height, Depth, NumberOfMipLevels)\
778 { \
779  ivec3 i3Size = textureSize(Sampler, _ToInt(MipLevel)); \
780  _TypeConvertStore( Width, i3Size.x ); \
781  _TypeConvertStore( Height, i3Size.y ); \
782  _TypeConvertStore( Depth, i3Size.z ); \
783  _TypeConvertStore( NumberOfMipLevels, textureQueryLevels(Sampler) );\
784 }
785 
786 #define GetTex2DMSDimensions_3(Sampler, Width, Height, NumberOfSamples)\
787 { \
788  ivec2 i2Size = textureSize(Sampler); \
789  _TypeConvertStore( Width, i2Size.x ); \
790  _TypeConvertStore( Height, i2Size.y ); \
791  _TypeConvertStore( NumberOfSamples, 0 );\
792 }
793 
794 #define GetTex2DMSArrDimensions_4(Sampler, Width, Height, Elements, NumberOfSamples)\
795 { \
796  ivec3 i3Size = textureSize(Sampler); \
797  _TypeConvertStore( Width, i3Size.x );\
798  _TypeConvertStore( Height, i3Size.y );\
799  _TypeConvertStore( Elements, i3Size.z );\
800  _TypeConvertStore( NumberOfSamples, 0 );\
801 }
802 
803 #define GetTexBufferDimensions_1(Sampler, Width)\
804 { \
805  _TypeConvertStore( Width, textureSize(Sampler) );\
806 }
807 
808 
809 // https://www.opengl.org/sdk/docs/man/html/imageSize.xhtml
810 // imageSize returns the dimensions of the image bound to image. The components in the
811 // return value are filled in, in order, with the width, height and depth of the image.
812 // For the array forms, the last component of the return value is the number of layers
813 // in the texture array.
814 
815 #define GetRWTex1DDimensions_1(Tex, Width)\
816 { \
817  _TypeConvertStore( Width, imageSize(Tex) ); \
818 }
819 
820 #define GetRWTex1DArrDimensions_2(Tex, Width, Elements)\
821 { \
822  ivec2 i2Size = imageSize(Tex); \
823  _TypeConvertStore( Width, i2Size.x ); \
824  _TypeConvertStore( Elements, i2Size.y ); \
825 }
826 
827 #define GetRWTex2DDimensions_2(Tex, Width, Height)\
828 { \
829  ivec2 i2Size = imageSize(Tex); \
830  _TypeConvertStore( Width, i2Size.x ); \
831  _TypeConvertStore( Height, i2Size.y ); \
832 }
833 
834 #define GetRWTex2DArrDimensions_3(Tex, Width, Height, Elements)\
835 { \
836  ivec3 i3Size = imageSize(Tex); \
837  _TypeConvertStore( Width, i3Size.x );\
838  _TypeConvertStore( Height, i3Size.y );\
839  _TypeConvertStore( Elements, i3Size.z );\
840 }
841 
842 #define GetRWTex3DDimensions_3(Tex, Width, Height, Depth)\
843 { \
844  ivec3 i3Size = imageSize(Tex); \
845  _TypeConvertStore( Width, i3Size.x ); \
846  _TypeConvertStore( Height, i3Size.y ); \
847  _TypeConvertStore( Depth, i3Size.z ); \
848 }
849 
850 #define GetRWTexBufferDimensions_1(Tex, Width)\
851 { \
852  _TypeConvertStore( Width, imageSize(Tex) ); \
853 }
854 
855 
856 // Texture sampling operations
857 
858 
859 // IMPORTANT NOTE ABOUT OFFSET
860 // Offset parameter to all texture sampling functions must be a constant expression.
861 // If it is not, the shader will be successfully compiled, HOWEVER the value of Offset
862 // will silently be zero.
863 //
864 // A constant expression in GLSL is defined as follows:
865 // * A literal value.
866 // * A const-qualified variable with an explicit initializer (so not a function parameter).
867 // * The result of the length() function of an array, but only if the array has an explicit size.
868 // * The result of most operators, so long as all the operands are themselves constant expressions.
869 // The operators not on this list are any assignment operators (+= and so forth), and the comma operator.
870 // * The result of a constructor for a type, but only if all of the arguments to the constructor are
871 // themselves constant expressions.
872 // * The return value of any built-in function, but only if all of the arguments to the function are
873 // themselves constant expressions. Opaque Types are never constant expressions. Note that the
874 // functions dFdx, dFdy, and fwidth will return 0, when used in a context that requires a constant
875 // expression (such as a const variable initializer).
876 //
877 // The list above does not include return value of a function, even when the value is compile-time expression.
878 // As a result, we cannot use type conversion functions for Offset parameter.
879 
880 // In all texture sampling functions, the last component of Coords is used as Dsub and the array layer is specified
881 // in the second to last component of Coords. (The second component of Coords is unused for 1D shadow lookups.)
882 // For cube array textures, Dsub is specified as a separate parameter
883 // mip
884 #define SampleCmpLevel0Tex1D_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3( Coords, 0.0, CompareValue), 0.0)
885 #define SampleCmpLevel0Tex1DArr_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0)
886 #define SampleCmpLevel0Tex2D_3(Tex, Sampler, Coords, CompareValue) textureLod(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0)
887 #define SampleCmpLevel0Tex2DArr_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for sampler2DArrayShadow
888 #define SampleCmpLevel0TexCube_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for samplerCubeShadow
889 #define SampleCmpLevel0TexCubeArr_3(Tex, Sampler, Coords, CompareValue) 0.0 // No textureLod for samplerCubeArrayShadow
890 
891 // mip
892 #define SampleCmpLevel0Tex1D_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3( Coords, 0.0, CompareValue), 0.0, int(Offset))
893 #define SampleCmpLevel0Tex1DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0, int(Offset))
894 #define SampleCmpLevel0Tex2D_4(Tex, Sampler, Coords, CompareValue, Offset) textureLodOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), 0.0, ivec2((Offset).xy))
895 #define SampleCmpLevel0Tex2DArr_4(Tex, Sampler, Coords, CompareValue, Offset) 0.0 // No textureLodOffset for sampler2DArrayShadow
896 
897 
898 // https://www.opengl.org/sdk/docs/man/html/texture.xhtml - note: there are many mistakes on the page
899 #ifdef FRAGMENT_SHADER
900 
901 # define Sample_2(Tex, Sampler, Coords) texture (Tex, _ToVec(Coords))
902 # define Sample_3(Tex, Sampler, Coords, Offset) textureOffset(Tex, _ToVec(Coords), Offset)
903 # define SampleBias_3(Tex, Sampler, Coords, Bias) texture (Tex, _ToVec(Coords), _ToFloat(Bias))
904 # define SampleBias_4(Tex, Sampler, Coords, Bias, Offset) textureOffset(Tex, _ToVec(Coords), Offset, _ToFloat(Bias))
905 
906 # define SampleCmpTex1D_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3( Coords, 0.0, CompareValue))
907 # define SampleCmpTex1DArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue))
908 # define SampleCmpTex2D_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue))
909 # define SampleCmpTex2DArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue))
910 # define SampleCmpTexCube_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue))
911 # define SampleCmpTexCubeArr_3(Tex, Sampler, Coords, CompareValue) texture(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, (Coords).w), _ToFloat(CompareValue))
912 
913 # define SampleCmpTex1D_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3( Coords, 0.0, CompareValue), int(Offset))
914 # define SampleCmpTex1DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), int(Offset))
915 # define SampleCmpTex2D_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec3((Coords).x, (Coords).y, CompareValue), ivec2((Offset).xy))
916 # define SampleCmpTex2DArr_4(Tex, Sampler, Coords, CompareValue, Offset) textureOffset(Tex, _ToVec4((Coords).x, (Coords).y, (Coords).z, CompareValue), ivec2((Offset).xy))
917 
918 #else
919 
920 // Derivatives are only available in fragment shader. GLSL compiler fails when it
921 // encounters texture() or textureOffset() instructions in other types of shaders. So
922 // to let the shader be compiled and to have something meaningful, replace such operations
923 // with textureLod() and textureLodOffset()
924 
925 # define Sample_2(Tex, Sampler, Coords) textureLod (Tex, _ToVec(Coords), 0.0)
926 # define Sample_3(Tex, Sampler, Coords, Offset) textureLodOffset(Tex, _ToVec(Coords), 0.0, Offset)
927 # define SampleBias_3(Tex, Sampler, Coords, Bias) textureLod (Tex, _ToVec(Coords), 0.0 + _ToFloat(Bias))
928 # define SampleBias_4(Tex, Sampler, Coords, Bias, Offset) textureLodOffset(Tex, _ToVec(Coords), 0.0 + _ToFloat(Bias), Offset)
929 
930 # define SampleCmpTex1D_3 SampleCmpLevel0Tex1D_3
931 # define SampleCmpTex1DArr_3 SampleCmpLevel0Tex1DArr_3
932 # define SampleCmpTex2D_3 SampleCmpLevel0Tex2D_3
933 # define SampleCmpTex2DArr_3 SampleCmpLevel0Tex2DArr_3
934 # define SampleCmpTexCube_3 SampleCmpLevel0TexCube_3
935 # define SampleCmpTexCubeArr_3 SampleCmpLevel0TexCubeArr_3
936 
937 # define SampleCmpTex1D_4 SampleCmpLevel0Tex1D_4
938 # define SampleCmpTex1DArr_4 SampleCmpLevel0Tex1DArr_4
939 # define SampleCmpTex2D_4 SampleCmpLevel0Tex2D_4
940 # define SampleCmpTex2DArr_4 SampleCmpLevel0Tex2DArr_4
941 
942 #endif
943 
944 // https://www.opengl.org/sdk/docs/man/html/textureLod.xhtml
945 #define SampleLevel_3(Tex, Sampler, Coords, Level) textureLod (Tex, _ToVec(Coords), _ToFloat(Level))
946 #define SampleLevel_4(Tex, Sampler, Coords, Level, Offset) textureLodOffset(Tex, _ToVec(Coords), _ToFloat(Level), Offset)
947 
948 // https://www.opengl.org/sdk/docs/man/html/textureGrad.xhtml
949 #define SampleGrad_4(Tex, Sampler, Coords, DDX, DDY) textureGrad (Tex, _ToVec(Coords), _ToVec(DDX), _ToVec(DDY))
950 #define SampleGrad_5(Tex, Sampler, Coords, DDX, DDY, Offset) textureGradOffset(Tex, _ToVec(Coords), _ToVec(DDX), _ToVec(DDY), Offset)
951 
952 
953 // texelFetch performs a lookup of a single texel from texture coordinate P in the texture
954 // bound to sampler. The array layer is specified in the last component of P for array forms.
955 // The lod parameter (if present) specifies the level-of-detail from which the texel will be fetched.
956 // The sample specifies which sample within the texel will be returned when reading from a multi-sample texure.
957 
958 #define LoadTex1D_1(Tex, Location) texelFetch (Tex, _ToInt((Location).x), _ToInt((Location).y))
959 #define LoadTex1D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToInt((Location).x), _ToInt((Location).y), int(Offset))
960 #define LoadTex1DArr_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xy), _ToInt((Location).z) )
961 #define LoadTex1DArr_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xy), _ToInt((Location).z), int(Offset))
962 #define LoadTex2D_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xy), _ToInt((Location).z))
963 #define LoadTex2D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xy), _ToInt((Location).z), ivec2( (Offset).xy) )
964 #define LoadTex2DArr_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xyz), _ToInt((Location).w) )
965 #define LoadTex2DArr_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xyz), _ToInt((Location).w), ivec2( (Offset).xy))
966 #define LoadTex3D_1(Tex, Location) texelFetch (Tex, _ToIvec( (Location).xyz), _ToInt((Location).w))
967 #define LoadTex3D_2(Tex, Location, Offset)texelFetchOffset(Tex, _ToIvec( (Location).xyz), _ToInt((Location).w), ivec3( (Offset).xyz))
968 #define LoadTex2DMS_2(Tex, Location, Sample) texelFetch(Tex, _ToIvec( (Location).xy), _ToInt(Sample))
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
970 #define LoadTex2DMSArr_2(Tex, Location, Sample) texelFetch(Tex, _ToIvec( (Location).xyz), _ToInt(Sample))
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
972 #define LoadTexBuffer_1(Tex, Location) texelFetch(Tex, _ToInt(Location))
973 
974 //https://www.opengl.org/sdk/docs/man/html/imageLoad.xhtml
975 #define LoadRWTex1D_1(Tex, Location) imageLoad(Tex, _ToInt(Location) )
976 #define LoadRWTex1DArr_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xy) )
977 #define LoadRWTex2D_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xy) )
978 #define LoadRWTex2DArr_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xyz) )
979 #define LoadRWTex3D_1(Tex, Location) imageLoad(Tex, _ToIvec((Location).xyz) )
980 #define LoadRWTexBuffer_1(Tex, Location)imageLoad(Tex, _ToInt(Location) )
981 
982 #define Gather_2(Tex, Sampler, Location) textureGather (Tex, _ToVec(Location))
983 #define Gather_3(Tex, Sampler, Location, Offset)textureGatherOffset(Tex, _ToVec(Location), Offset)
984 
985 #define GatherCmp_3(Tex, Sampler, Location, CompareVal) textureGather (Tex, _ToVec(Location), _ToFloat(CompareVal))
986 #define GatherCmp_4(Tex, Sampler, Location, CompareVal, Offset)textureGatherOffset(Tex, _ToVec(Location), _ToFloat(CompareVal), Offset)
987 
988 // Atomic operations
989 #define InterlockedAddSharedVar_2(dest, value) atomicAdd(dest, value)
990 #define InterlockedAddSharedVar_3(dest, value, orig_val) orig_val = atomicAdd(dest, value)
991 #define InterlockedAddImage_2(img, coords, value) imageAtomicAdd(img, _ToIvec(coords), value)
992 #define InterlockedAddImage_3(img, coords, value, orig_val)orig_val = imageAtomicAdd(img, _ToIvec(coords), value)
993 
994 #define InterlockedAndSharedVar_2(dest, value) atomicAnd(dest, value)
995 #define InterlockedAndSharedVar_3(dest, value, orig_val) orig_val = atomicAnd(dest, value)
996 #define InterlockedAndImage_2(img, coords, value) imageAtomicAnd(img, _ToIvec(coords), value)
997 #define InterlockedAndImage_3(img, coords, value, orig_val)orig_val = imageAtomicAnd(img, _ToIvec(coords), value)
998 
999 #define InterlockedMaxSharedVar_2(dest, value) atomicMax(dest, value)
1000 #define InterlockedMaxSharedVar_3(dest, value, orig_val) orig_val = atomicMax(dest, value)
1001 #define InterlockedMaxImage_2(img, coords, value) imageAtomicMax(img, _ToIvec(coords), value)
1002 #define InterlockedMaxImage_3(img, coords, value, orig_val)orig_val = imageAtomicMax(img, _ToIvec(coords), value)
1003 
1004 #define InterlockedMinSharedVar_2(dest, value) atomicMin(dest, value)
1005 #define InterlockedMinSharedVar_3(dest, value, orig_val) orig_val = atomicMin(dest, value)
1006 #define InterlockedMinImage_2(img, coords, value) imageAtomicMin(img, _ToIvec(coords), value)
1007 #define InterlockedMinImage_3(img, coords, value, orig_val)orig_val = imageAtomicMin(img, _ToIvec(coords), value)
1008 
1009 #define InterlockedOrSharedVar_2(dest, value) atomicOr(dest, value)
1010 #define InterlockedOrSharedVar_3(dest, value, orig_val) orig_val = atomicOr(dest, value)
1011 #define InterlockedOrImage_2(img, coords, value) imageAtomicOr(img, _ToIvec(coords), value)
1012 #define InterlockedOrImage_3(img, coords, value, orig_val)orig_val = imageAtomicOr(img, _ToIvec(coords), value)
1013 
1014 #define InterlockedXorSharedVar_2(dest, value) atomicXor(dest, value)
1015 #define InterlockedXorSharedVar_3(dest, value, orig_val) orig_val = atomicXor(dest, value)
1016 #define InterlockedXorImage_2(img, coords, value) imageAtomicXor(img, _ToIvec(coords), value)
1017 #define InterlockedXorImage_3(img, coords, value, orig_val)orig_val = imageAtomicXor(img, _ToIvec(coords), value)
1018 
1019 // There is actually no InterlockedExchange() with 2 arguments
1020 #define InterlockedExchangeSharedVar_2(dest, value) atomicExchange(dest, value)
1021 #define InterlockedExchangeSharedVar_3(dest, value, orig_val) orig_val = atomicExchange(dest, value)
1022 #define InterlockedExchangeImage_2(img, coords, value) imageAtomicExchange(img, _ToIvec(coords), value)
1023 #define InterlockedExchangeImage_3(img, coords, value, orig_val)orig_val = imageAtomicExchange(img, _ToIvec(coords), value)
1024 
1025 //uint imageAtomicCompSwap( image img, IVec P, nint compare, nint data);
1026 //void InterlockedCompareExchange( in R dest, in T compare_value, in T value, out T original_value);
1027 #define InterlockedCompareExchangeSharedVar_4(dest, cmp_val, value, orig_val) orig_val = atomicCompSwap(dest, cmp_val, value)
1028 #define InterlockedCompareExchangeImage_4(img, coords, cmp_val, value, orig_val) orig_val = imageAtomicCompSwap(img, _ToIvec(coords), cmp_val, value)
1029 
1030 #define InterlockedCompareStoreSharedVar_3(dest, cmp_val, value) atomicCompSwap(dest, cmp_val, value)
1031 #define InterlockedCompareStoreImage_3(img, coords, cmp_val, value)imageAtomicCompSwap(img, _ToIvec(coords), cmp_val, value)
1032 
1033 
1034 // Swizzling macros
1035 #define _SWIZZLE0
1036 #define _SWIZZLE1 .x
1037 #define _SWIZZLE2 .xy
1038 #define _SWIZZLE3 .xyz
1039 #define _SWIZZLE4 .xyzw
1040 
1041 // Helper functions
1042 
1043 #ifdef VULKAN
1044 
1045 #define NDC_MIN_Z 0.0 // Minimal z in the normalized device space
1046 
1047 // Note that Vulkan itself does not invert Y coordinate when transforming
1048 // normalized device Y to window space. However, we use negative viewport
1049 // height which achieves the same effect as in D3D, thererfore we need to
1050 // invert y (see comments in DeviceContextVkImpl::CommitViewports() for details)
1051 #define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, -0.5, 1.0)
1052 
1053 #else
1054 
1055 #define NDC_MIN_Z -1.0 // Minimal z in the normalized device space
1056 #define F3NDC_XYZ_TO_UVD_SCALE float3(0.5, 0.5, 0.5)
1057 
1058 #endif
1059 
1061 {
1062  return float2(0.5,0.5) + F3NDC_XYZ_TO_UVD_SCALE.xy * f2ProjSpaceXY.xy;
1063 }
1065 {
1066  return (TexUV.xy - float2(0.5, 0.5)) / F3NDC_XYZ_TO_UVD_SCALE.xy;
1067 }
1068 
1069 float NormalizedDeviceZToDepth(float fNDC_Z)
1070 {
1071  return (fNDC_Z - NDC_MIN_Z) * F3NDC_XYZ_TO_UVD_SCALE.z;
1072 }
1073 float DepthToNormalizedDeviceZ(float fDepth)
1074 {
1075  return fDepth / F3NDC_XYZ_TO_UVD_SCALE.z + NDC_MIN_Z;
1076 }
1077 
1078 #define MATRIX_ELEMENT(mat, row, col) mat[col][row]
1079 
1081 {
1082  return transpose(float4x4(row0, row1, row2, row3));
1083 }
1084 
1086 {
1087  return transpose(float3x3(row0, row1, row2));
1088 }
1089 
1091 {
1092  return transpose(float2x2(row0, row1));
1093 }
1094 
1095 // ---------------------------------- Vertex shader ----------------------------------
1096 #ifdef VERTEX_SHADER
1097 
1098 #ifndef GL_ES
1099 out gl_PerVertex
1100 {
1101  vec4 gl_Position;
1102 };
1103 #endif
1104 
1105 #define _GET_GL_VERTEX_ID(VertexId)_TypeConvertStore(VertexId, gl_VertexID)
1106 #define _GET_GL_INSTANCE_ID(InstId)_TypeConvertStore(InstId, gl_InstanceID)
1107 #define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)
1108 
1109 #endif
1110 
1111 
1112 // --------------------------------- Fragment shader ---------------------------------
1113 #ifdef FRAGMENT_SHADER
1114 
1115 // SV_Position.w == w, while gl_FragCoord.w == 1/w
1116 #define _GET_GL_FRAG_COORD(FragCoord)_ResizeVector(FragCoord, vec4(gl_FragCoord.xyz, 1.0/gl_FragCoord.w))
1117 #define _GET_GL_FRONT_FACING(FrontFacing)_TypeConvertStore(FrontFacing, gl_FrontFacing)
1118 #define _SET_GL_FRAG_DEPTH(Depth)_TypeConvertStore(gl_FragDepth, Depth)
1119 
1120 #endif
1121 
1122 
1123 // --------------------------------- Geometry shader ---------------------------------
1124 #ifdef GEOMETRY_SHADER
1125 
1126 // ARB_separate_shader_objects requires built-in block gl_PerVertex to be redeclared before accessing its members
1127 // declaring gl_PointSize and gl_ClipDistance causes compilation error on Android
1128 in gl_PerVertex
1129 {
1130  vec4 gl_Position;
1131  //float gl_PointSize;
1132  //float gl_ClipDistance[];
1133 } gl_in[];
1134 
1135 out gl_PerVertex
1136 {
1137  vec4 gl_Position;
1138  //float gl_PointSize;
1139  //float gl_ClipDistance[];
1140 };
1141 
1142 #define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)
1143 #define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_in[i].gl_PrimitiveIDIn)
1144 
1145 #define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)
1146 #define _SET_GL_LAYER(Layer)_TypeConvertStore(gl_Layer,Layer)
1147 
1148 #endif
1149 
1150 
1151 // --------------------------- Tessellation control shader ---------------------------
1152 #ifdef TESS_CONTROL_SHADER
1153 
1154 in gl_PerVertex
1155 {
1156  vec4 gl_Position;
1157  //float gl_PointSize;
1158  //float gl_ClipDistance[];
1159 } gl_in[gl_MaxPatchVertices];
1160 
1161 out gl_PerVertex
1162 {
1163  vec4 gl_Position;
1164  //float gl_PointSize;
1165  //float gl_ClipDistance[];
1166 } gl_out[];
1167 
1168 #define _GET_GL_INVOCATION_ID(InvocId)_TypeConvertStore(InvocId, gl_InvocationID)
1169 #define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_PrimitiveID)
1170 #define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)
1171 
1172 #define _SET_GL_POSITION(Pos)gl_out[gl_InvocationID].gl_Position=_ExpandVector(Pos)
1173 
1174 void _SetGLTessLevelOuter(float OuterLevel[2])
1175 {
1176  for(int i=0; i < 2; ++i)
1177  gl_TessLevelOuter[i] = OuterLevel[i];
1178 }
1179 void _SetGLTessLevelOuter(float OuterLevel[3])
1180 {
1181  for(int i=0; i < 3; ++i)
1182  gl_TessLevelOuter[i] = OuterLevel[i];
1183 }
1184 void _SetGLTessLevelOuter(float OuterLevel[4])
1185 {
1186  for(int i=0; i < 4; ++i)
1187  gl_TessLevelOuter[i] = OuterLevel[i];
1188 }
1189 
1190 
1191 void _SetGLTessLevelInner(float InnerLevel[2])
1192 {
1193  gl_TessLevelInner[0] = InnerLevel[0];
1194  gl_TessLevelInner[1] = InnerLevel[1];
1195 }
1196 void _SetGLTessLevelInner(float InnerLevel)
1197 {
1198  gl_TessLevelInner[0] = InnerLevel;
1199 }
1200 
1201 #endif
1202 
1203 
1204 // --------------------------- Tessellation evaluation shader ---------------------------
1205 #ifdef TESS_EVALUATION_SHADER
1206 
1207 in gl_PerVertex
1208 {
1209  vec4 gl_Position;
1210  //float gl_PointSize;
1211  //float gl_ClipDistance[];
1212 } gl_in[gl_MaxPatchVertices];
1213 
1214 out gl_PerVertex
1215 {
1216  vec4 gl_Position;
1217  //float gl_PointSize;
1218  //float gl_ClipDistance[];
1219 };
1220 
1221 #define _GET_GL_POSITION(Pos)_ResizeVector(Pos, gl_in[i].gl_Position)
1222 
1223 void _GetGLTessLevelOuter(out float OuterLevel[2])
1224 {
1225  for(int i=0; i < 2; ++i)
1226  OuterLevel[i] = gl_TessLevelOuter[i];
1227 }
1228 void _GetGLTessLevelOuter(out float OuterLevel[3])
1229 {
1230  for(int i=0; i < 3; ++i)
1231  OuterLevel[i] = gl_TessLevelOuter[i];
1232 }
1233 void _GetGLTessLevelOuter(out float OuterLevel[4])
1234 {
1235  for(int i=0; i < 4; ++i)
1236  OuterLevel[i] = gl_TessLevelOuter[i];
1237 }
1238 
1239 void _GetGLTessLevelInner(out float InnerLevel[2])
1240 {
1241  InnerLevel[0] = gl_TessLevelInner[0];
1242  InnerLevel[1] = gl_TessLevelInner[1];
1243 }
1244 void _GetGLTessLevelInner(out float InnerLevel)
1245 {
1246  InnerLevel = gl_TessLevelInner[0];
1247 }
1248 
1249 #define _GET_GL_TESS_COORD(TessCoord)_ResizeVector(TessCoord, gl_TessCoord)
1250 #define _GET_GL_PRIMITIVE_ID(PrimId)_TypeConvertStore(PrimId, gl_PrimitiveID)
1251 
1252 #define _SET_GL_POSITION(Pos)gl_Position=_ExpandVector(Pos)
1253 
1254 #endif
1255 
1256 
1257 // ---------------------------------- Compute shader ----------------------------------
1258 #ifdef COMPUTE_SHADER
1259 
1260 #define _GET_GL_GLOBAL_INVOCATION_ID(Type, InvocId)InvocId=Type(gl_GlobalInvocationID)
1261 #define _GET_GL_WORK_GROUP_ID(Type, GroupId)GroupId=Type(gl_WorkGroupID)
1262 #define _GET_GL_LOCAL_INVOCATION_ID(Type, InvocId)InvocId=Type(gl_LocalInvocationID)
1263 #define _GET_GL_LOCAL_INVOCATION_INDEX(Type, InvocInd)InvocInd=Type(gl_LocalInvocationIndex)
1264 
1265 #endif
1266 
1267 #endif // _GLSL_DEFINITIONS_
saturate
float saturate(float x)
Definition: GLSLDefinitions.h:116
Diligent::uint
uint32_t uint
Definition: BasicMath.hpp:1826
And
bool4 And(bool4 L, bool4 R)
Definition: GLSLDefinitions.h:279
_ToVec
float _ToVec(uint u1)
Definition: GLSLDefinitions.h:629
asuint
uint asuint(uint x)
Definition: GLSLDefinitions.h:161
isfinite
bool isfinite(float x)
Definition: GLSLDefinitions.h:223
_ToVec2
#define _ToVec2(x, y)
Definition: GLSLDefinitions.h:596
_ToIvec2
#define _ToIvec2(x, y)
Definition: GLSLDefinitions.h:600
BoolToFloat
float4 BoolToFloat(bool4 b4)
Definition: GLSLDefinitions.h:326
asfloat
float asfloat(float x)
Definition: GLSLDefinitions.h:129
_ToFloat
float _ToFloat(int x)
Definition: GLSLDefinitions.h:564
float3x3
#define float3x3
Definition: GLSLDefinitions.h:65
_ToUvec4
#define _ToUvec4(x, y, z, w)
Definition: GLSLDefinitions.h:606
_ResizeVector
void _ResizeVector(out vec4 outVec4, in vec4 inVec4)
Definition: GLSLDefinitions.h:500
GroupMemoryBarrierWithGroupSync
void GroupMemoryBarrierWithGroupSync()
Definition: GLSLDefinitions.h:473
rcp
float rcp(float x)
Definition: GLSLDefinitions.h:111
bool3
#define bool3
Definition: GLSLDefinitions.h:55
_ToVec4
#define _ToVec4(x, y, z, w)
Definition: GLSLDefinitions.h:598
bool2
#define bool2
Definition: GLSLDefinitions.h:56
DeviceMemoryBarrierWithGroupSync
void DeviceMemoryBarrierWithGroupSync()
Definition: GLSLDefinitions.h:471
MatrixFromRows
float4x4 MatrixFromRows(float4 row0, float4 row1, float4 row2, float4 row3)
Definition: GLSLDefinitions.h:1080
float2
#define float2
Definition: GLSLDefinitions.h:44
f16tof32
n n float f16tof32(uint u1)\n" "
Definition: GLSLDefinitions_inc.h:177
GroupMemoryBarrier
void GroupMemoryBarrier()
Definition: GLSLDefinitions.h:472
Or
bool4 Or(bool4 L, bool4 R)
Definition: GLSLDefinitions.h:303
AllMemoryBarrierWithGroupSync
void AllMemoryBarrierWithGroupSync()
Definition: GLSLDefinitions.h:469
_ToVec3
#define _ToVec3(x, y, z)
Definition: GLSLDefinitions.h:597
float2x2
#define float2x2
Definition: GLSLDefinitions_inc.h:60
_ToBool
bool _ToBool(int x)
Definition: GLSLDefinitions.h:591
Diligent::clamp
T clamp(T val, T _min, T _max)
Definition: BasicMath.hpp:1700
F3NDC_XYZ_TO_UVD_SCALE
#define F3NDC_XYZ_TO_UVD_SCALE
Definition: GLSLDefinitions.h:1056
f32tof16
n n uint f32tof16(float f)\n" "
Definition: GLSLDefinitions_inc.h:199
bool4
#define bool4
Definition: GLSLDefinitions.h:54
_ToUvec2
#define _ToUvec2(x, y)
Definition: GLSLDefinitions.h:604
_ToIvec3
#define _ToIvec3(x, y, z)
Definition: GLSLDefinitions.h:601
_ExpandVector
vec4 _ExpandVector(float x)
Definition: GLSLDefinitions.h:480
float4x4
#define float4x4
Definition: GLSLDefinitions.h:70
log10
float log10(float x)
Definition: GLSLDefinitions.h:243
float3
#define float3
Definition: GLSLDefinitions.h:43
NDC_MIN_Z
#define NDC_MIN_Z
Definition: GLSLDefinitions.h:1055
NormalizedDeviceZToDepth
float NormalizedDeviceZToDepth(float fNDC_Z)
Definition: GLSLDefinitions.h:1069
float4
#define float4
Definition: GLSLDefinitions.h:42
NormalizedDeviceXYToTexUV
float2 NormalizedDeviceXYToTexUV(float2 f2ProjSpaceXY)
Definition: GLSLDefinitions.h:1060
_TypeConvertStore
void _TypeConvertStore(out float Dst, in int Src)
Definition: GLSLDefinitions.h:521
sincos
void sincos(float x, out float s, out float c)
Definition: GLSLDefinitions.h:121
_ToIvec4
#define _ToIvec4(x, y, z, w)
Definition: GLSLDefinitions.h:602
TexUVToNormalizedDeviceXY
float2 TexUVToNormalizedDeviceXY(float2 TexUV)
Definition: GLSLDefinitions.h:1064
_ToUvec
uint _ToUvec(uint u1)
Definition: GLSLDefinitions.h:645
_ToIvec
int _ToIvec(uint u1)
Definition: GLSLDefinitions.h:613
asint
int asint(int x)
Definition: GLSLDefinitions.h:145
DeviceMemoryBarrier
void DeviceMemoryBarrier()
Definition: GLSLDefinitions.h:470
DepthToNormalizedDeviceZ
float DepthToNormalizedDeviceZ(float fDepth)
Definition: GLSLDefinitions.h:1073
AllMemoryBarrier
void AllMemoryBarrier()
Definition: GLSLDefinitions.h:468
_ToUint
uint _ToUint(int x)
Definition: GLSLDefinitions.h:586
_ToInt
int _ToInt(int x)
Definition: GLSLDefinitions.h:542
_ToUvec3
#define _ToUvec3(x, y, z)
Definition: GLSLDefinitions.h:605