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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2005 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup gpu
*/
#include "MEM_guardedalloc.h"
#include "BLI_math_base.h"
#include "BLI_math_vector.h"
#include "BLI_path_util.h"
#include "BLI_string.h"
#include "BLI_string_utils.h"
#include "BLI_utildefines.h"
#include "BLI_vector.hh"
#include "BKE_appdir.h"
#include "BKE_global.h"
#include "DNA_space_types.h"
#include "GPU_extensions.h"
#include "GPU_matrix.h"
#include "GPU_platform.h"
#include "GPU_shader.h"
#include "GPU_texture.h"
#include "GPU_uniformbuffer.h"
#include "gpu_backend.hh"
#include "gpu_context_private.hh"
#include "gpu_shader_private.hh"
extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[];
using namespace blender;
using namespace blender::gpu;
/* -------------------------------------------------------------------- */
/** \name Debug functions
* \{ */
void Shader::print_errors(Span<const char *> sources, char *log)
{
const bool pretty = true;
const char line_prefix[] = " | ";
char *sources_combined = BLI_string_join_arrayN((const char **)sources.data(), sources.size());
if (pretty) {
fprintf(stderr, "\n \033[1mShader Compilation Log : \033[0m%s\n", this->name);
}
else {
fprintf(stderr, "\n Shader Compilation Log : %s\n", this->name);
}
char *log_line = log, *line_end;
char *error_line_number_end;
int error_line, error_char, last_error_line = -2, last_error_char = -1;
bool found_line_id = false;
while ((line_end = strchr(log_line, '\n'))) {
/* Skip empty lines. */
if (line_end == log_line) {
log_line++;
continue;
}
/* Skip ERROR: or WARNING:. */
const char *prefix[] = {"ERROR", "WARNING"};
for (int i = 0; i < ARRAY_SIZE(prefix); i++) {
if (STREQLEN(log_line, prefix[i], strlen(prefix[i]))) {
log_line += strlen(prefix[i]);
break;
}
}
error_line = error_char = -1;
if (ELEM(log_line[0], '0', ':') && ELEM(log_line[1], ':', '(', ' ')) {
error_line = (int)strtol(log_line + 2, &error_line_number_end, 10);
/* Try to fetch the error caracter (not always available). */
if (ELEM(error_line_number_end[0], '(', ':')) {
error_char = (int)strtol(error_line_number_end + 1, NULL, 10);
}
}
if ((error_line == -1)) {
found_line_id = false;
}
const char *src_line = sources_combined;
/* Some drivers use (source:line) instead of (line:char) */
if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_UNIX, GPU_DRIVER_OFFICIAL) && (error_line != -1) &&
(error_char != -1)) {
int error_source = error_line;
if (error_source < sources.size()) {
src_line = sources[error_source];
error_line = error_char;
error_char = -1;
}
}
/* Separate from previous block. */
if (last_error_line != error_line) {
fprintf(stderr, "\n");
}
/* Print line from the source file that is producing the error. */
if ((error_line != -1) && (error_line != last_error_line || error_char != last_error_char)) {
const char *src_line_end = src_line;
found_line_id = false;
/* error_line is 1 based in this case. */
int src_line_index = 1;
while ((src_line_end = strchr(src_line, '\n'))) {
if (src_line_index == error_line) {
found_line_id = true;
break;
}
/* Continue to next line. */
src_line = src_line_end + 1;
src_line_index++;
}
/* Print error source. */
if (found_line_id) {
fprintf(stderr, "%5d | ", src_line_index);
fwrite(src_line, (src_line_end + 1) - src_line, 1, stderr);
/* Print char offset. */
fprintf(stderr, line_prefix);
if (error_char != -1) {
for (int i = 0; i < error_char; i++) {
fprintf(stderr, " ");
}
fprintf(stderr, "^");
}
fprintf(stderr, "\n");
}
}
fprintf(stderr, line_prefix);
if (found_line_id) {
/* Skip to message. Avoid redundant info. */
const char *keywords[] = {"error", "warning"};
for (int i = 0; i < ARRAY_SIZE(keywords); i++) {
/* Avoid searching following lines. */
line_end[0] = '\0';
if (strstr(log_line, keywords[i])) {
log_line = strstr(log_line, keywords[i]);
if (pretty) {
if (STREQ(keywords[i], "error")) {
fprintf(stderr, "\033[31;1mError\033[0m ");
}
else if (STREQ(keywords[i], "warning")) {
fprintf(stderr, "\033[33;1mWarning\033[0m ");
}
log_line += strlen(keywords[i]);
}
break;
}
}
line_end[0] = '\n';
}
/* Print the error itself. */
if (pretty) {
fprintf(stderr, "\033[2m");
}
fwrite(log_line, (line_end + 1) - log_line, 1, stderr);
if (pretty) {
fprintf(stderr, "\033[0m");
}
/* Continue to next line. */
log_line = line_end + 1;
last_error_line = error_line;
last_error_char = error_char;
}
fprintf(stderr, "\n\n");
MEM_freeN(sources_combined);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Creation / Destruction
* \{ */
Shader::Shader(const char *sh_name)
{
BLI_strncpy(this->name, sh_name, sizeof(this->name));
}
Shader::~Shader()
{
if (this->interface) {
GPU_shaderinterface_discard(this->interface);
}
}
static void standard_defines(Vector<const char *> &sources)
{
BLI_assert(sources.size() == 0);
/* Version needs to be first. Exact values will be added by implementation. */
sources.append("version");
/* some useful defines to detect GPU type */
if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
sources.append("#define GPU_ATI\n");
}
else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY)) {
sources.append("#define GPU_NVIDIA\n");
}
else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
sources.append("#define GPU_INTEL\n");
}
/* some useful defines to detect OS type */
if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_WIN, GPU_DRIVER_ANY)) {
sources.append("#define OS_WIN\n");
}
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_MAC, GPU_DRIVER_ANY)) {
sources.append("#define OS_MAC\n");
}
else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
sources.append("#define OS_UNIX\n");
}
if (GPU_crappy_amd_driver()) {
sources.append("#define GPU_DEPRECATED_AMD_DRIVER\n");
}
}
GPUShader *GPU_shader_create_ex(const char *vertcode,
const char *fragcode,
const char *geomcode,
const char *libcode,
const char *defines,
const eGPUShaderTFBType tf_type,
const char **tf_names,
const int tf_count,
const char *shname)
{
/* At least a vertex shader and a fragment shader are required. */
BLI_assert((fragcode != NULL) && (vertcode != NULL));
Shader *shader = GPUBackend::get()->shader_alloc(shname);
if (vertcode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_VERTEX_SHADER\n");
sources.append("#define IN_OUT out\n");
if (geomcode) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
if (defines) {
sources.append(defines);
}
sources.append(vertcode);
shader->vertex_shader_from_glsl(sources);
}
if (fragcode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_FRAGMENT_SHADER\n");
sources.append("#define IN_OUT in\n");
if (geomcode) {
sources.append("#define USE_GEOMETRY_SHADER\n");
}
if (defines) {
sources.append(defines);
}
if (libcode) {
sources.append(libcode);
}
sources.append(fragcode);
shader->fragment_shader_from_glsl(sources);
}
if (geomcode) {
Vector<const char *> sources;
standard_defines(sources);
sources.append("#define GPU_GEOMETRY_SHADER\n");
if (defines) {
sources.append(defines);
}
sources.append(geomcode);
shader->geometry_shader_from_glsl(sources);
}
if (tf_names != NULL && tf_count > 0) {
BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
shader->transform_feedback_names_set(Span<const char *>(tf_names, tf_count), tf_type);
}
if (!shader->finalize()) {
delete shader;
return NULL;
};
return static_cast<GPUShader *>(shader);
}
void GPU_shader_free(GPUShader *shader)
{
delete static_cast<Shader *>(shader);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Creation utils
* \{ */
GPUShader *GPU_shader_create(const char *vertcode,
const char *fragcode,
const char *geomcode,
const char *libcode,
const char *defines,
const char *shname)
{
return GPU_shader_create_ex(
vertcode, fragcode, geomcode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, shname);
}
GPUShader *GPU_shader_create_from_python(const char *vertcode,
const char *fragcode,
const char *geomcode,
const char *libcode,
const char *defines)
{
char *libcodecat = NULL;
if (libcode == NULL) {
libcode = datatoc_gpu_shader_colorspace_lib_glsl;
}
else {
libcode = libcodecat = BLI_strdupcat(libcode, datatoc_gpu_shader_colorspace_lib_glsl);
}
GPUShader *sh = GPU_shader_create_ex(
vertcode, fragcode, geomcode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, NULL);
MEM_SAFE_FREE(libcodecat);
return sh;
}
static const char *string_join_array_maybe_alloc(const char **str_arr, bool *r_is_alloc)
{
bool is_alloc = false;
if (str_arr == NULL) {
*r_is_alloc = false;
return NULL;
}
/* Skip empty strings (avoid alloc if we can). */
while (str_arr[0] && str_arr[0][0] == '\0') {
str_arr++;
}
int i;
for (i = 0; str_arr[i]; i++) {
if (i != 0 && str_arr[i][0] != '\0') {
is_alloc = true;
}
}
*r_is_alloc = is_alloc;
if (is_alloc) {
return BLI_string_join_arrayN(str_arr, i);
}
return str_arr[0];
}
/**
* Use via #GPU_shader_create_from_arrays macro (avoids passing in param).
*
* Similar to #DRW_shader_create_with_lib with the ability to include libs for each type of shader.
*
* It has the advantage that each item can be conditionally included
* without having to build the string inline, then free it.
*
* \param params: NULL terminated arrays of strings.
*
* Example:
* \code{.c}
* sh = GPU_shader_create_from_arrays({
* .vert = (const char *[]){shader_lib_glsl, shader_vert_glsl, NULL},
* .geom = (const char *[]){shader_geom_glsl, NULL},
* .frag = (const char *[]){shader_frag_glsl, NULL},
* .defs = (const char *[]){"#define DEFINE\n", test ? "#define OTHER_DEFINE\n" : "", NULL},
* });
* \endcode
*/
struct GPUShader *GPU_shader_create_from_arrays_impl(
const struct GPU_ShaderCreateFromArray_Params *params)
{
struct {
const char *str;
bool is_alloc;
} str_dst[4] = {{0}};
const char **str_src[4] = {params->vert, params->frag, params->geom, params->defs};
for (int i = 0; i < ARRAY_SIZE(str_src); i++) {
str_dst[i].str = string_join_array_maybe_alloc(str_src[i], &str_dst[i].is_alloc);
}
GPUShader *sh = GPU_shader_create(
str_dst[0].str, str_dst[1].str, str_dst[2].str, NULL, str_dst[3].str, __func__);
for (int i = 0; i < ARRAY_SIZE(str_dst); i++) {
if (str_dst[i].is_alloc) {
MEM_freeN((void *)str_dst[i].str);
}
}
return sh;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Binding
* \{ */
void GPU_shader_bind(GPUShader *gpu_shader)
{
Shader *shader = static_cast<Shader *>(gpu_shader);
GPUContext *ctx = GPU_context_active_get();
if (ctx->shader != shader) {
ctx->shader = shader;
shader->bind();
GPU_matrix_bind(shader);
GPU_shader_set_srgb_uniform(shader);
}
if (GPU_matrix_dirty_get()) {
GPU_matrix_bind(shader);
}
}
void GPU_shader_unbind(void)
{
#ifndef NDEBUG
GPUContext *ctx = GPU_context_active_get();
if (ctx->shader) {
static_cast<Shader *>(ctx->shader)->unbind();
}
ctx->shader = NULL;
#endif
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Transform feedback
*
* TODO(fclem) Should be replaced by compute shaders.
* \{ */
bool GPU_shader_transform_feedback_enable(GPUShader *shader, GPUVertBuf *vertbuf)
{
return static_cast<Shader *>(shader)->transform_feedback_enable(vertbuf);
}
void GPU_shader_transform_feedback_disable(GPUShader *shader)
{
static_cast<Shader *>(shader)->transform_feedback_disable();
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Uniforms / Resource location
* \{ */
int GPU_shader_get_uniform(GPUShader *shader, const char *name)
{
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(shader->interface, name);
return uniform ? uniform->location : -1;
}
int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
{
return GPU_shaderinterface_uniform_builtin(shader->interface,
static_cast<GPUUniformBuiltin>(builtin));
}
int GPU_shader_get_builtin_block(GPUShader *shader, int builtin)
{
return GPU_shaderinterface_block_builtin(shader->interface,
static_cast<GPUUniformBlockBuiltin>(builtin));
}
int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
{
const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
return ubo ? ubo->location : -1;
}
int GPU_shader_get_uniform_block_binding(GPUShader *shader, const char *name)
{
const GPUShaderInput *ubo = GPU_shaderinterface_ubo(shader->interface, name);
return ubo ? ubo->binding : -1;
}
int GPU_shader_get_texture_binding(GPUShader *shader, const char *name)
{
const GPUShaderInput *tex = GPU_shaderinterface_uniform(shader->interface, name);
return tex ? tex->binding : -1;
}
int GPU_shader_get_attribute(GPUShader *shader, const char *name)
{
const GPUShaderInput *attr = GPU_shaderinterface_attr(shader->interface, name);
return attr ? attr->location : -1;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Getters
* \{ */
/* Clement : Temp */
int GPU_shader_get_program(GPUShader *UNUSED(shader))
{
/* TODO fixme */
return (int)0;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Uniforms setters
* \{ */
void GPU_shader_uniform_vector(
GPUShader *shader, int loc, int len, int arraysize, const float *value)
{
static_cast<Shader *>(shader)->uniform_float(loc, len, arraysize, value);
}
void GPU_shader_uniform_vector_int(
GPUShader *shader, int loc, int len, int arraysize, const int *value)
{
static_cast<Shader *>(shader)->uniform_int(loc, len, arraysize, value);
}
void GPU_shader_uniform_int(GPUShader *shader, int location, int value)
{
GPU_shader_uniform_vector_int(shader, location, 1, 1, &value);
}
void GPU_shader_uniform_float(GPUShader *shader, int location, float value)
{
GPU_shader_uniform_vector(shader, location, 1, 1, &value);
}
#define GET_UNIFORM \
const GPUShaderInput *uniform = GPU_shaderinterface_uniform(sh->interface, name); \
BLI_assert(uniform);
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
{
GET_UNIFORM
GPU_shader_uniform_int(sh, uniform->location, value);
}
void GPU_shader_uniform_1b(GPUShader *sh, const char *name, bool value)
{
GPU_shader_uniform_1i(sh, name, value ? 1 : 0);
}
void GPU_shader_uniform_2f(GPUShader *sh, const char *name, float x, float y)
{
const float data[2] = {x, y};
GPU_shader_uniform_2fv(sh, name, data);
}
void GPU_shader_uniform_3f(GPUShader *sh, const char *name, float x, float y, float z)
{
const float data[3] = {x, y, z};
GPU_shader_uniform_3fv(sh, name, data);
}
void GPU_shader_uniform_4f(GPUShader *sh, const char *name, float x, float y, float z, float w)
{
const float data[4] = {x, y, z, w};
GPU_shader_uniform_4fv(sh, name, data);
}
void GPU_shader_uniform_1f(GPUShader *sh, const char *name, float x)
{
GET_UNIFORM
GPU_shader_uniform_float(sh, uniform->location, x);
}
void GPU_shader_uniform_2fv(GPUShader *sh, const char *name, const float data[2])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 2, 1, data);
}
void GPU_shader_uniform_3fv(GPUShader *sh, const char *name, const float data[3])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 3, 1, data);
}
void GPU_shader_uniform_4fv(GPUShader *sh, const char *name, const float data[4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 4, 1, data);
}
void GPU_shader_uniform_mat4(GPUShader *sh, const char *name, const float data[4][4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 16, 1, (const float *)data);
}
void GPU_shader_uniform_2fv_array(GPUShader *sh, const char *name, int len, const float (*val)[2])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 2, len, (const float *)val);
}
void GPU_shader_uniform_4fv_array(GPUShader *sh, const char *name, int len, const float (*val)[4])
{
GET_UNIFORM
GPU_shader_uniform_vector(sh, uniform->location, 4, len, (const float *)val);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name sRGB Rendering Workaround
*
* The viewport overlay frame-buffer is sRGB and will expect shaders to output display referred
* Linear colors. But other frame-buffers (i.e: the area frame-buffers) are not sRGB and require
* the shader output color to be in sRGB space
* (assumed display encoded color-space as the time of writing).
* For this reason we have a uniform to switch the transform on and off depending on the current
* frame-buffer color-space.
* \{ */
static int g_shader_builtin_srgb_transform = 0;
void GPU_shader_set_srgb_uniform(GPUShader *shader)
{
int32_t loc = GPU_shaderinterface_uniform_builtin(shader->interface, GPU_UNIFORM_SRGB_TRANSFORM);
if (loc != -1) {
GPU_shader_uniform_vector_int(shader, loc, 1, 1, &g_shader_builtin_srgb_transform);
}
}
void GPU_shader_set_framebuffer_srgb_target(int use_srgb_to_linear)
{
g_shader_builtin_srgb_transform = use_srgb_to_linear;
}
/** \} */
|