-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathcommon.glsl
More file actions
141 lines (112 loc) · 4.18 KB
/
common.glsl
File metadata and controls
141 lines (112 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
===========================================================================
Daemon BSD Source Code
Copyright (c) 2024-2025 Daemon Developers
All rights reserved.
This file is part of the Daemon BSD Source Code (Daemon Source Code).
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
===========================================================================
*/
/* common.glsl */
/* Common defines */
/* Allows accessing each element of a uvec4 array with a singular ID
Useful to avoid wasting memory due to alignment requirements
array must be in the form of uvec4 array[] */
#define UINT_FROM_UVEC4_ARRAY( array, id ) ( ( array )[( id ) / 4][( id ) % 4] )
#define UVEC2_FROM_UVEC4_ARRAY( array, id ) ( ( id ) % 2 == 0 ? ( array )[( id ) / 2].xy : ( array )[( id ) / 2].zw )
// Common functions
#if defined(HAVE_EXT_gpu_shader4)
#define colorPack uint
#define colorModulatePack uint
#else
#define colorPack vec4
#define colorModulatePack vec4
#endif
vec4 UnpackColor( const in colorPack packedColor )
{
#if defined(HAVE_EXT_gpu_shader4)
return unpackUnorm4x8( packedColor );
#else
return packedColor;
#endif
}
/* colorMod uint format:
colorMod << 0: color * 1
colorMod << 1: color * ( -1 )
colorMod << 2: alpha * 1
colorMod << 3: alpha * ( -1 )
colorMod << 4-10: available for future usage
colorMod << 11-31: lightFactor
colorMod float format:
colorMod[ 0 ]: color * f
colorMod[ 1 ]: lightFactor
colorMod[ 3 ]: alpha * f */
vec4 ColorModulateToColor( const in colorModulatePack colorMod )
{
#if defined(HAVE_EXT_gpu_shader4)
vec3 colorModArray = vec3( 0.0f, 1.0f, -1.0f );
uint rgbIndex = colorMod & 3u;
uint alphaIndex = ( colorMod >> 2u ) & 3u;
float rgb = colorModArray[ rgbIndex ];
float alpha = colorModArray[ alphaIndex ];
#else
float rgb = colorMod.r;
float alpha = colorMod.a;
#endif
return vec4( rgb, rgb, rgb, alpha );
}
float ColorModulateToLightFactor( const in colorModulatePack colorMod )
{
#if defined(HAVE_EXT_gpu_shader4)
return float( colorMod >> 11u ) * ( 1.0 / 16384 );
#else
return colorMod.g;
#endif
}
void ModulateColor(
const in vec4 colorModulation,
const in vec4 unpackedColor,
inout vec4 color )
{
color *= colorModulation;
color += unpackedColor;
}
void ColorModulateColor(
const in colorModulatePack colorMod,
const in colorPack packedColor,
inout vec4 color )
{
vec4 colorModulation = ColorModulateToColor( colorMod );
vec4 unpackedColor = UnpackColor( packedColor );
ModulateColor( colorModulation, unpackedColor, color );
}
void ColorModulateColor_lightFactor(
const in colorModulatePack colorMod,
const in colorPack packedColor,
inout vec4 color )
{
vec4 colorModulation = ColorModulateToColor( colorMod );
float lightFactor = ColorModulateToLightFactor( colorMod );
vec4 unpackedColor = UnpackColor( packedColor );
unpackedColor.rgb *= lightFactor;
ModulateColor( colorModulation, unpackedColor, color );
}