-
Notifications
You must be signed in to change notification settings - Fork 491
Expand file tree
/
Copy pathGpuShaderUtils.h
More file actions
290 lines (223 loc) · 11.2 KB
/
Copy pathGpuShaderUtils.h
File metadata and controls
290 lines (223 loc) · 11.2 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#ifndef INCLUDED_OCIO_GPUSHADERUTILS_H
#define INCLUDED_OCIO_GPUSHADERUTILS_H
#include <sstream>
#include <OpenColorIO/OpenColorIO.h>
namespace OCIO_NAMESPACE
{
// Helper class to create shader programs
class GpuShaderText
{
public:
// Helper class to create shader lines
class GpuShaderLine
{
public:
GpuShaderLine() = delete;
GpuShaderLine(const GpuShaderLine &) = default;
~GpuShaderLine();
GpuShaderLine& operator<<(const char * str);
GpuShaderLine& operator<<(float value);
GpuShaderLine& operator<<(double value);
GpuShaderLine& operator<<(unsigned value);
GpuShaderLine& operator<<(int value);
GpuShaderLine& operator<<(const std::string & str);
GpuShaderLine& operator=(const GpuShaderLine & rhs);
friend class GpuShaderText;
private:
explicit GpuShaderLine(GpuShaderText * text);
// The ShaderText instance associated with this line.
// Note: Not owned.
GpuShaderText * m_text = nullptr;
};
public:
GpuShaderText() = delete;
explicit GpuShaderText(GpuLanguage lang);
// Create a new GpuShaderLine instance and associate it with the GpuShaderText object.
GpuShaderLine newLine();
// Get the shader string produced so far
std::string string() const;
//
// Indentation helper functions
//
void setIndent(unsigned indent);
void indent();
void dedent();
//
// Basic types.
//
std::string constKeyword() const;
std::string floatKeyword() const;
std::string floatKeywordConst() const;
std::string floatDecl(const std::string& name) const;
std::string intKeyword() const;
std::string intKeywordConst() const;
std::string intDecl(const std::string& name) const;
// Cast a float expression to int using language-appropriate syntax.
std::string intCast(const std::string& expr) const;
std::string colorDecl(const std::string& name) const;
std::string vectorCompareExpression(const std::string& lhs, const std::string& op, const std::string& rhs);
//
// Scalar & arrays helper functions.
//
// Declare a float variable.
std::string declareVarStr(const std::string & name, float v);
void declareVar(const std::string & name, float v);
void declareVarConst(const std::string & name, float v);
// Declare a bool variable.
std::string declareVarStr(const std::string & name, bool v);
void declareVar(const std::string & name, bool v);
void declareVarConst(const std::string & name, bool v);
// Declare a float array variable.
void declareFloatArrayConst(const std::string & name, int size, const float * v);
// Declare a int array variable.
void declareIntArrayConst(const std::string & name, int size, const int * v);
//
// Float2 helper functions
//
std::string float2Keyword() const;
// Get the string for creating constant vector with three elements
std::string float2Const(const std::string& x, const std::string& y) const;
std::string float2Decl(const std::string& name) const;
//
// Float3 helper functions
//
// Get the keyword for declaring/using vectors with three elements
std::string float3Keyword() const;
// Get the string for creating constant vector with three elements
std::string float3Const(float x, float y, float z) const;
std::string float3Const(double x, double y, double z) const;
// Get the string for creating constant vector with three elements
std::string float3Const(const std::string& x, const std::string& y, const std::string& z) const;
// Get the string for creating constant vector with three elements
std::string float3Const(float v) const;
std::string float3Const(double v) const;
// Get the string for creating constant vector with three elements
std::string float3Const(const std::string& v) const;
// Get the declaration for a vector with three elements
std::string float3Decl(const std::string& name) const;
// Declare and initialize a vector with three elements
void declareFloat3(const std::string& name, float x, float y, float z);
void declareFloat3(const std::string& name, const Float3 & vec3);
void declareFloat3(const std::string& name, double x, double y, double z);
// Declare and initialize a vector with three elements
void declareFloat3(const std::string& name,
const std::string& x, const std::string& y, const std::string& z);
//
// Float4 helper functions
//
// Get the keyword for declaring/using vectors with four elements
std::string float4Keyword() const;
// Get the string for creating constant vector with four elements
std::string float4Const(float x, float y, float z, float w) const;
std::string float4Const(double x, double y, double z, double w) const;
// Get the string for creating constant vector with four elements
std::string float4Const(const std::string& x, const std::string& y,
const std::string& z, const std::string& w) const;
// Get the string for creating constant vector with four elements
std::string float4Const(float v) const;
// Get the string for creating constant vector with four elements
std::string float4Const(const std::string& v) const;
// Get the declaration for a vector with four elements
std::string float4Decl(const std::string& name) const;
// Declare and initialize a vector with four elements
void declareFloat4(const std::string& name,
float x, float y, float z, float w);
void declareFloat4(const std::string& name,
double x, double y, double z, double w);
void declareFloat4(const std::string& name,
const std::string& x, const std::string& y,
const std::string& z, const std::string& w);
//
// Texture helpers
//
static std::string getSamplerName(const std::string& textureName);
// Declare the global texture and sampler information for a 1D texture.
void declareTex1D(const std::string& textureName, unsigned descriptorSetIndex, unsigned textureIndex);
// Declare the global texture and sampler information for a 2D texture.
void declareTex2D(const std::string& textureName, unsigned descriptorSetIndex, unsigned textureIndex);
// Declare the global texture and sampler information for a 3D texture.
void declareTex3D(const std::string& textureName, unsigned descriptorSetIndex, unsigned textureIndex);
// Get the texture lookup call for a 1D texture.
std::string sampleTex1D(const std::string& textureName, const std::string& coords) const;
// Get the texture lookup call for a 2D texture.
std::string sampleTex2D(const std::string& textureName, const std::string& coords) const;
// Get the texture lookup call for a 3D texture.
std::string sampleTex3D(const std::string& textureName, const std::string& coords) const;
//
// Uniform helpers
//
void declareUniformFloat(const std::string & uniformName);
void declareUniformBool(const std::string & uniformName);
void declareUniformFloat3(const std::string & uniformName);
void declareUniformArrayFloat(const std::string & uniformName, unsigned int size);
void declareUniformArrayInt(const std::string & uniformName, unsigned int size);
//
// Matrix multiplication helpers
//
// Get the string for multiplying a 3x3 matrix and a three-elements vector
std::string mat3fMul(const float * m3x3, const std::string & vecName) const;
std::string mat3fMul(const double * m3x3, const std::string & vecName) const;
// Get the string for multiplying a 4x4 matrix and a four-elements vector
std::string mat4fMul(const float * m4x4, const std::string & vecName) const;
std::string mat4fMul(const double * m4x4, const std::string & vecName) const;
//
// Special function helpers
//
// Get the string for linearly interpolating two quantities.
std::string lerp(const std::string& x, const std::string& y,
const std::string& a) const;
// Get the string for creating a three or four-elements 'greater than' comparison
// Each element i in the resulting vector is 1 if a>b, or 0 otherwise.
std::string float3GreaterThan(const std::string& a, const std::string& b) const;
std::string float4GreaterThan(const std::string& a, const std::string& b) const;
// Get the string for creating a three or four-elements 'greater than or equal' comparison
// Each element i in the resulting vector is 1 if a>=b, or 0 otherwise.
std::string float3GreaterThanEqual(const std::string& a, const std::string& b) const;
std::string float4GreaterThanEqual(const std::string& a, const std::string& b) const;
// Get the string for taking the four-quadrant arctangent
// (similar to atan(y/x) but takes into account the signs of the arguments).
std::string atan2(const std::string& y, const std::string& x) const;
// Get the string for taking the sign of a vector.
std::string sign(const std::string & v) const;
//Add a cast to bool for shading languages that don't support implicit casts from int to bool.
//It is required to use this function when doing boolean operations on a bool uniform to be
//compatible with all shading languages.
std::string castToBool(const std::string& v) const;
friend class GpuShaderLine;
private:
// Flush the current shader line to the shader text
// This includes the leading indentation and the trailing newline
// to the current line before adding it to the shader text, and
// resets the current line.
void flushLine();
private:
// Shader language to use in the various shader text builder methods.
GpuLanguage m_lang;
// String stream containing the current shader text.
std::ostringstream m_ossText;
// In order to avoid repeated allocations of an output string stream
// for multiple shader lines, create a single ostringstream instance
// on the shader text and just reset it after a line has been added
// to the text. This should not pose a racing problem since we're only
// creating a single line at a time for a given shader text.
// String stream containing the current shader line.
std::ostringstream m_ossLine;
// Indentation level to use for the next line.
unsigned m_indent;
};
// Create a resource name prepending the prefix of the shaderCreator to base.
std::string BuildResourceName(GpuShaderCreatorRcPtr & shaderCreator, const std::string & prefix,
const std::string & base);
//
// Math functions used by multiple GPU renderers.
//
// Convert scene-linear values to "grading log".
void AddLinToLogShader(GpuShaderCreatorRcPtr & shaderCreator, GpuShaderText & st);
void AddLinToLogShaderChannelBlue(GpuShaderCreatorRcPtr & shaderCreator, GpuShaderText & st);
// Convert "grading log" values to scene-linear.
void AddLogToLinShader(GpuShaderCreatorRcPtr & shaderCreator, GpuShaderText & st);
void AddLogToLinShaderChannelBlue(GpuShaderCreatorRcPtr & shaderCreator, GpuShaderText & st);
} // namespace OCIO_NAMESPACE
#endif