-
Notifications
You must be signed in to change notification settings - Fork 490
Expand file tree
/
Copy pathglsl.cpp
More file actions
591 lines (476 loc) · 14.8 KB
/
glsl.cpp
File metadata and controls
591 lines (476 loc) · 14.8 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#elif _WIN32
#include <GL/glew.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include <sstream>
#include <iostream>
#include <OpenColorIO/OpenColorIO.h>
#include "glsl.h"
namespace OCIO_NAMESPACE
{
namespace
{
bool GetGLError(std::string & error)
{
const GLenum glErr = glGetError();
if(glErr!=GL_NO_ERROR)
{
#ifdef __APPLE__
// Unfortunately no gluErrorString equivalent on Mac.
error = "OpenGL Error";
#else
error = (const char*)gluErrorString(glErr);
#endif
return true;
}
return false;
}
void CheckStatus()
{
std::string error;
if (GetGLError(error))
{
throw Exception(error.c_str());
}
}
void SetTextureParameters(GLenum textureType, Interpolation interpolation)
{
if(interpolation==INTERP_NEAREST)
{
glTexParameteri(textureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(textureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
else
{
glTexParameteri(textureType, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(textureType, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
glTexParameteri(textureType, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(textureType, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(textureType, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
void AllocateTexture3D(unsigned index, unsigned & texId,
Interpolation interpolation,
unsigned edgelen, const float * values)
{
if(values==0x0)
{
throw Exception("Missing texture data");
}
glGenTextures(1, &texId);
glActiveTexture(GL_TEXTURE0 + index);
glBindTexture(GL_TEXTURE_3D, texId);
SetTextureParameters(GL_TEXTURE_3D, interpolation);
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB32F_ARB,
edgelen, edgelen, edgelen, 0, GL_RGB, GL_FLOAT, values);
}
void AllocateTexture(unsigned index, unsigned & texId,
unsigned width, unsigned height,
GpuShaderDesc::TextureType channel,
GpuShaderDesc::TextureDimensions dimensions,
Interpolation interpolation,
const float * values)
{
if (values == nullptr)
{
throw Exception("Missing texture data.");
}
GLint internalformat = GL_RGB32F_ARB;
GLenum format = GL_RGB;
if (channel == GpuShaderCreator::TEXTURE_RED_CHANNEL)
{
internalformat = GL_R32F;
format = GL_RED;
}
glGenTextures(1, &texId);
glActiveTexture(GL_TEXTURE0 + index);
switch (dimensions)
{
case GpuShaderCreator::TEXTURE_1D:
glBindTexture(GL_TEXTURE_1D, texId);
SetTextureParameters(GL_TEXTURE_1D, interpolation);
glTexImage1D(GL_TEXTURE_1D, 0, internalformat, width, 0, format, GL_FLOAT, values);
break;
case GpuShaderCreator::TEXTURE_2D:
glBindTexture(GL_TEXTURE_2D, texId);
SetTextureParameters(GL_TEXTURE_2D, interpolation);
glTexImage2D(GL_TEXTURE_2D, 0, internalformat, width, height, 0, format, GL_FLOAT, values);
break;
default:
throw Exception("Invalid 1D LUT texture dimensions");
break;
}
}
GLuint CompileShaderText(GLenum shaderType, const char * text)
{
CheckStatus();
if(!text || !*text)
{
throw Exception("Invalid fragment shader program");
}
GLuint shader;
GLint stat;
shader = glCreateShader(shaderType);
glShaderSource(shader, 1, (const GLchar **) &text, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
if (!stat)
{
GLchar log[1000];
GLsizei len;
glGetShaderInfoLog(shader, 1000, &len, log);
std::string err("OCIO Shader program compilation failed: ");
err += log;
err += "\n";
err += text;
throw Exception(err.c_str());
}
return shader;
}
void LinkShaders(GLuint program, GLuint fragShader)
{
CheckStatus();
if (!fragShader)
{
throw Exception("Missing shader program");
}
else
{
glAttachShader(program, fragShader);
}
glLinkProgram(program);
GLint stat;
glGetProgramiv(program, GL_LINK_STATUS, &stat);
if (!stat)
{
GLchar log[1000];
GLsizei len;
glGetProgramInfoLog(program, 1000, &len, log);
std::string err("Shader link error:\n");
err += log;
throw Exception(err.c_str());
}
}
}
//////////////////////////////////////////////////////////
OpenGLBuilder::Uniform::Uniform(const std::string & name, const GpuShaderDesc::UniformData & data)
: m_name(name)
, m_data(data)
, m_handle(0)
{
}
void OpenGLBuilder::Uniform::setUp(unsigned program)
{
m_handle = glGetUniformLocation(program, m_name.c_str());
std::string error;
if (GetGLError(error))
{
std::string err("Shader parameter ");
err += m_name;
err += " not found: ";
throw Exception(err.c_str());
}
}
void OpenGLBuilder::Uniform::use()
{
// Update value.
if (m_data.m_getDouble)
{
glUniform1f(m_handle, (GLfloat)m_data.m_getDouble());
}
else if (m_data.m_getBool)
{
glUniform1f(m_handle, (GLfloat)(m_data.m_getBool()?1.0f:0.0f));
}
else if (m_data.m_getFloat3)
{
glUniform3f(m_handle, (GLfloat)m_data.m_getFloat3()[0],
(GLfloat)m_data.m_getFloat3()[1],
(GLfloat)m_data.m_getFloat3()[2]);
}
else if (m_data.m_vectorFloat.m_getSize && m_data.m_vectorFloat.m_getVector)
{
glUniform1fv(m_handle, (GLsizei)m_data.m_vectorFloat.m_getSize(),
(GLfloat*)m_data.m_vectorFloat.m_getVector());
}
else if (m_data.m_vectorInt.m_getSize && m_data.m_vectorInt.m_getVector)
{
glUniform1iv(m_handle, (GLsizei)m_data.m_vectorInt.m_getSize(),
(GLint*)m_data.m_vectorInt.m_getVector());
}
else
{
throw Exception("Uniform is not linked to any value.");
}
}
//////////////////////////////////////////////////////////
OpenGLBuilderRcPtr OpenGLBuilder::Create(const GpuShaderDescRcPtr & shaderDesc)
{
return OpenGLBuilderRcPtr(new OpenGLBuilder(shaderDesc));
}
OpenGLBuilder::OpenGLBuilder(const GpuShaderDescRcPtr & shaderDesc)
: m_shaderDesc(shaderDesc)
, m_startIndex(0)
, m_fragShader(0)
, m_program(glCreateProgram())
, m_verbose(false)
{
}
OpenGLBuilder::~OpenGLBuilder()
{
deleteAllTextures();
if(m_fragShader)
{
glDetachShader(m_program, m_fragShader);
glDeleteShader(m_fragShader);
m_fragShader = 0;
}
if(m_program)
{
glDeleteProgram(m_program);
m_program = 0;
}
}
void OpenGLBuilder::allocateAllTextures(unsigned startIndex)
{
deleteAllTextures();
// This is the first available index for the textures.
m_startIndex = startIndex;
unsigned currIndex = m_startIndex;
// Process the 3D LUT first.
const unsigned maxTexture3D = m_shaderDesc->getNum3DTextures();
for(unsigned idx=0; idx<maxTexture3D; ++idx)
{
// 1. Get the information of the 3D LUT.
const char * textureName = nullptr;
const char * samplerName = nullptr;
unsigned edgelen = 0;
GpuShaderCreator:: TextureType channel = GpuShaderCreator::TEXTURE_RGB_CHANNEL;
Interpolation interpolation = INTERP_LINEAR;
m_shaderDesc->get3DTexture(idx, textureName, samplerName, edgelen, channel, interpolation);
if(!textureName || !*textureName
|| !samplerName || !*samplerName
|| edgelen==0)
{
throw Exception("The texture data is corrupted");
}
const float * values = nullptr;
m_shaderDesc->get3DTextureValues(idx, values);
if(!values)
{
throw Exception("The texture values are missing");
}
// 2. Allocate the 3D LUT.
unsigned texId = 0;
AllocateTexture3D(currIndex, texId, interpolation, edgelen, values);
// 3. Keep the texture id & name for the later enabling.
m_textureIds.push_back(TextureId(texId, textureName, samplerName, GL_TEXTURE_3D));
currIndex++;
}
// Process the 1D LUTs.
const unsigned maxTexture2D = m_shaderDesc->getNumTextures();
for(unsigned idx=0; idx<maxTexture2D; ++idx)
{
// 1. Get the information of the 1D LUT.
const char * textureName = nullptr;
const char * samplerName = nullptr;
unsigned width = 0;
unsigned height = 0;
GpuShaderDesc::TextureType channel = GpuShaderDesc::TEXTURE_RGB_CHANNEL;
Interpolation interpolation = INTERP_LINEAR;
GpuShaderDesc::TextureDimensions dimensions = GpuShaderDesc::TEXTURE_1D;
m_shaderDesc->getTexture(idx, textureName, samplerName, width, height, channel, dimensions, interpolation);
if (!textureName || !*textureName
|| !samplerName || !*samplerName
|| width==0)
{
throw Exception("The texture data is corrupted");
}
const float * values = 0x0;
m_shaderDesc->getTextureValues(idx, values);
if(!values)
{
throw Exception("The texture values are missing");
}
// 2. Allocate the 1D LUT (a 1D or 2D texture is needed to hold large LUTs).
unsigned texId = 0;
AllocateTexture(currIndex, texId, width, height, channel, dimensions, interpolation, values);
// 3. Keep the texture id & name for the later enabling.
unsigned type = (dimensions == GpuShaderDesc::TEXTURE_1D) ? GL_TEXTURE_1D : GL_TEXTURE_2D;
m_textureIds.push_back(TextureId(texId, textureName, samplerName, type));
currIndex++;
}
}
void OpenGLBuilder::deleteAllTextures()
{
const size_t max = m_textureIds.size();
for (size_t idx=0; idx<max; ++idx)
{
const TextureId & data = m_textureIds[idx];
glDeleteTextures(1, &data.m_uid);
}
m_textureIds.clear();
}
void OpenGLBuilder::useAllTextures()
{
const size_t max = m_textureIds.size();
for (size_t idx=0; idx<max; ++idx)
{
const TextureId& data = m_textureIds[idx];
glActiveTexture((GLenum)(GL_TEXTURE0 + m_startIndex + idx));
glBindTexture(data.m_type, data.m_uid);
glUniform1i(
glGetUniformLocation(m_program,
data.m_samplerName.c_str()),
GLint(m_startIndex + idx) );
}
}
void OpenGLBuilder::linkAllUniforms()
{
deleteAllUniforms();
const unsigned maxUniforms = m_shaderDesc->getNumUniforms();
for (unsigned idx = 0; idx < maxUniforms; ++idx)
{
GpuShaderDesc::UniformData data;
const char * name = m_shaderDesc->getUniform(idx, data);
if (data.m_type == UNIFORM_UNKNOWN)
{
throw Exception("Unknown uniform type.");
}
// Transfer uniform.
m_uniforms.emplace_back(name, data);
// Connect uniform with program.
m_uniforms.back().setUp(m_program);
}
}
void OpenGLBuilder::deleteAllUniforms()
{
m_uniforms.clear();
}
void OpenGLBuilder::useAllUniforms()
{
for (auto uniform : m_uniforms)
{
uniform.use();
}
}
std::string OpenGLBuilder::getGLSLVersionString()
{
switch (m_shaderDesc->getLanguage())
{
case GPU_LANGUAGE_GLSL_1_2:
case GPU_LANGUAGE_MSL_2_0:
// That's the minimal version supported.
return "#version 120";
case GPU_LANGUAGE_GLSL_1_3:
return "#version 130";
case GPU_LANGUAGE_GLSL_4_0:
return "#version 400 core";
case GPU_LANGUAGE_GLSL_ES_1_0:
return "#version 100";
case GPU_LANGUAGE_GLSL_ES_3_0:
return "#version 300 es";
case GPU_LANGUAGE_CG:
case GPU_LANGUAGE_HLSL_DX11:
case LANGUAGE_OSL_1:
default:
// These are all impossible in OpenGL contexts.
// The shader will be unusable, so let's throw
throw Exception("Invalid shader language for OpenGLBuilder");
}
}
unsigned OpenGLBuilder::buildProgram(const std::string & clientShaderProgram, bool standaloneShader)
{
const std::string shaderCacheID = m_shaderDesc->getCacheID();
if(shaderCacheID!=m_shaderCacheID)
{
if(m_fragShader)
{
glDetachShader(m_program, m_fragShader);
glDeleteShader(m_fragShader);
}
std::ostringstream oss;
oss << getGLSLVersionString() << std::endl
<< (!standaloneShader ? m_shaderDesc->getShaderText() : "") << std::endl
<< clientShaderProgram << std::endl;
if(m_verbose)
{
std::cout << "\nGPU Shader Program:\n\n"
<< oss.str()
<< "\n\n"
<< std::flush;
}
m_fragShader = CompileShaderText(GL_FRAGMENT_SHADER, oss.str().c_str());
LinkShaders(m_program, m_fragShader);
m_shaderCacheID = shaderCacheID;
linkAllUniforms();
}
return m_program;
}
void OpenGLBuilder::useProgram()
{
glUseProgram(m_program);
}
unsigned OpenGLBuilder::getProgramHandle()
{
return m_program;
}
unsigned OpenGLBuilder::GetTextureMaxWidth()
{
// Arbitrary huge number only to find the limit.
static unsigned maxTextureSize = 256 * 1024;
CheckStatus();
unsigned w = maxTextureSize;
unsigned h = 1;
while(w>1)
{
glTexImage2D(GL_PROXY_TEXTURE_2D, 0,
GL_RGB32F_ARB,
w, h, 0,
GL_RGB, GL_FLOAT, NULL);
bool texValid = true;
GLenum glErr = GL_NO_ERROR;
while((glErr=glGetError()) != GL_NO_ERROR)
{
if(glErr==GL_INVALID_VALUE)
{
texValid = false;
}
}
#ifndef __APPLE__
//
// In case of Linux, if glTexImage2D() succeeds
// glGetTexLevelParameteriv() could fail.
//
// In case of OSX, glTexImage2D() will provide the right result,
// and glGetTexLevelParameteriv() always fails.
// So do not try glGetTexLevelParameteriv().
//
if(texValid)
{
GLint format = 0;
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0,
GL_TEXTURE_COMPONENTS, &format);
texValid = texValid && (GL_RGB32F_ARB==format);
while((glErr=glGetError()) != GL_NO_ERROR);
}
#endif
if(texValid) break;
w = w >> 1;
h = h << 1;
}
if(w==1)
{
throw Exception("Maximum texture size unknown");
}
CheckStatus();
return w;
}
} // namespace OCIO_NAMESPACE