Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gpu_shader.cc « intern « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03b7d5402f50c82bb399327e89c30b98a174a358 (plain)
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
 * 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 "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_shader_private.h"

extern "C" char datatoc_gpu_shader_colorspace_lib_glsl[];

/* Adjust these constants as needed. */
#define MAX_DEFINE_LENGTH 256
#define MAX_EXT_DEFINE_LENGTH 512

#ifndef NDEBUG
static uint g_shaderid = 0;
#endif

/* -------------------------------------------------------------------- */
/** \name Convenience functions
 * \{ */

static void shader_print_errors(const char *task, const char *log, const char **code, int totcode)
{
  int line = 1;

  fprintf(stderr, "GPUShader: %s error:\n", task);

  for (int i = 0; i < totcode; i++) {
    const char *c, *pos, *end = code[i] + strlen(code[i]);

    if (G.debug & G_DEBUG) {
      fprintf(stderr, "===== shader string %d ====\n", i + 1);

      c = code[i];
      while ((c < end) && (pos = strchr(c, '\n'))) {
        fprintf(stderr, "%2d  ", line);
        fwrite(c, (pos + 1) - c, 1, stderr);
        c = pos + 1;
        line++;
      }

      fprintf(stderr, "%s", c);
    }
  }

  fprintf(stderr, "%s\n", log);
}

static const char *gpu_shader_version(void)
{
  return "#version 330\n";
}

static void gpu_shader_standard_extensions(char defines[MAX_EXT_DEFINE_LENGTH])
{
  /* enable extensions for features that are not part of our base GLSL version
   * don't use an extension for something already available!
   */

  if (GLEW_ARB_texture_gather) {
    /* There is a bug on older Nvidia GPU where GL_ARB_texture_gather
     * is reported to be supported but yield a compile error (see T55802). */
    if (!GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY) || GLEW_VERSION_4_0) {
      strcat(defines, "#extension GL_ARB_texture_gather: enable\n");

      /* Some drivers don't agree on GLEW_ARB_texture_gather and the actual support in the
       * shader so double check the preprocessor define (see T56544). */
      if (!GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY) && !GLEW_VERSION_4_0) {
        strcat(defines, "#ifdef GL_ARB_texture_gather\n");
        strcat(defines, "#  define GPU_ARB_texture_gather\n");
        strcat(defines, "#endif\n");
      }
      else {
        strcat(defines, "#define GPU_ARB_texture_gather\n");
      }
    }
  }
  if (GLEW_ARB_texture_query_lod) {
    /* a #version 400 feature, but we use #version 330 maximum so use extension */
    strcat(defines, "#extension GL_ARB_texture_query_lod: enable\n");
  }
  if (GLEW_ARB_shader_draw_parameters) {
    strcat(defines, "#extension GL_ARB_shader_draw_parameters : enable\n");
    strcat(defines, "#define GPU_ARB_shader_draw_parameters\n");
  }
  if (GPU_arb_texture_cube_map_array_is_supported()) {
    strcat(defines, "#extension GL_ARB_texture_cube_map_array : enable\n");
    strcat(defines, "#define GPU_ARB_texture_cube_map_array\n");
  }
}

static void gpu_shader_standard_defines(char defines[MAX_DEFINE_LENGTH])
{
  /* some useful defines to detect GPU type */
  if (GPU_type_matches(GPU_DEVICE_ATI, GPU_OS_ANY, GPU_DRIVER_ANY)) {
    strcat(defines, "#define GPU_ATI\n");
    if (GPU_crappy_amd_driver()) {
      strcat(defines, "#define GPU_DEPRECATED_AMD_DRIVER\n");
    }
  }
  else if (GPU_type_matches(GPU_DEVICE_NVIDIA, GPU_OS_ANY, GPU_DRIVER_ANY)) {
    strcat(defines, "#define GPU_NVIDIA\n");
  }
  else if (GPU_type_matches(GPU_DEVICE_INTEL, GPU_OS_ANY, GPU_DRIVER_ANY)) {
    strcat(defines, "#define GPU_INTEL\n");
  }

  /* some useful defines to detect OS type */
  if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_WIN, GPU_DRIVER_ANY)) {
    strcat(defines, "#define OS_WIN\n");
  }
  else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_MAC, GPU_DRIVER_ANY)) {
    strcat(defines, "#define OS_MAC\n");
  }
  else if (GPU_type_matches(GPU_DEVICE_ANY, GPU_OS_UNIX, GPU_DRIVER_ANY)) {
    strcat(defines, "#define OS_UNIX\n");
  }

  float derivatives_factors[2];
  GPU_get_dfdy_factors(derivatives_factors);
  if (derivatives_factors[0] == 1.0f) {
    strcat(defines, "#define DFDX_SIGN 1.0\n");
  }
  else {
    strcat(defines, "#define DFDX_SIGN -1.0\n");
  }

  if (derivatives_factors[1] == 1.0f) {
    strcat(defines, "#define DFDY_SIGN 1.0\n");
  }
  else {
    strcat(defines, "#define DFDY_SIGN -1.0\n");
  }
}

#define DEBUG_SHADER_NONE ""
#define DEBUG_SHADER_VERTEX "vert"
#define DEBUG_SHADER_FRAGMENT "frag"
#define DEBUG_SHADER_GEOMETRY "geom"

/**
 * Dump GLSL shaders to disk
 *
 * This is used for profiling shader performance externally and debug if shader code is correct.
 * If called with no code, it simply bumps the shader index, so different shaders for the same
 * program share the same index.
 */
static void gpu_dump_shaders(const char **code, const int num_shaders, const char *extension)
{
  if ((G.debug & G_DEBUG_GPU_SHADERS) == 0) {
    return;
  }

  /* We use the same shader index for shaders in the same program.
   * So we call this function once before calling for the individual shaders. */
  static int shader_index = 0;
  if (code == NULL) {
    shader_index++;
    BLI_assert(STREQ(DEBUG_SHADER_NONE, extension));
    return;
  }

  /* Determine the full path of the new shader. */
  char shader_path[FILE_MAX];

  char file_name[512] = {'\0'};
  sprintf(file_name, "%04d.%s", shader_index, extension);

  BLI_join_dirfile(shader_path, sizeof(shader_path), BKE_tempdir_session(), file_name);

  /* Write shader to disk. */
  FILE *f = fopen(shader_path, "w");
  if (f == NULL) {
    printf("Error writing to file: %s\n", shader_path);
  }
  for (int j = 0; j < num_shaders; j++) {
    fprintf(f, "%s", code[j]);
  }
  fclose(f);
  printf("Shader file written to disk: %s\n", shader_path);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Creation / Destruction
 * \{ */

GPUShader *GPU_shader_create(const char *vertexcode,
                             const char *fragcode,
                             const char *geocode,
                             const char *libcode,
                             const char *defines,
                             const char *shname)
{
  return GPU_shader_create_ex(
      vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, shname);
}

GPUShader *GPU_shader_create_from_python(const char *vertexcode,
                                         const char *fragcode,
                                         const char *geocode,
                                         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(
      vertexcode, fragcode, geocode, libcode, defines, GPU_SHADER_TFB_NONE, NULL, 0, NULL);

  MEM_SAFE_FREE(libcodecat);
  return sh;
}

GPUShader *GPU_shader_load_from_binary(const char *binary,
                                       const int binary_format,
                                       const int binary_len,
                                       const char *shname)
{
  BLI_assert(GL_ARB_get_program_binary);
  int success;
  int program = glCreateProgram();

  glProgramBinary(program, binary_format, binary, binary_len);
  glGetProgramiv(program, GL_LINK_STATUS, &success);

  if (success) {
    glUseProgram(program);

    GPUShader *shader = (GPUShader *)MEM_callocN(sizeof(*shader), __func__);
    shader->interface = GPU_shaderinterface_create(program);
    shader->program = program;

#ifndef NDEBUG
    BLI_snprintf(shader->name, sizeof(shader->name), "%s_%u", shname, g_shaderid++);
#else
    UNUSED_VARS(shname);
#endif

    return shader;
  }

  glDeleteProgram(program);
  return NULL;
}

GPUShader *GPU_shader_create_ex(const char *vertexcode,
                                const char *fragcode,
                                const char *geocode,
                                const char *libcode,
                                const char *defines,
                                const eGPUShaderTFBType tf_type,
                                const char **tf_names,
                                const int tf_count,
                                const char *shname)
{
  GLint status;
  GLchar log[5000];
  GLsizei length = 0;
  GPUShader *shader;
  char standard_defines[MAX_DEFINE_LENGTH] = "";
  char standard_extensions[MAX_EXT_DEFINE_LENGTH] = "";

  shader = (GPUShader *)MEM_callocN(sizeof(GPUShader), "GPUShader");
  gpu_dump_shaders(NULL, 0, DEBUG_SHADER_NONE);

#ifndef NDEBUG
  BLI_snprintf(shader->name, sizeof(shader->name), "%s_%u", shname, g_shaderid++);
#else
  UNUSED_VARS(shname);
#endif

  /* At least a vertex shader and a fragment shader are required. */
  BLI_assert((fragcode != NULL) && (vertexcode != NULL));

  if (vertexcode) {
    shader->vertex = glCreateShader(GL_VERTEX_SHADER);
  }
  if (fragcode) {
    shader->fragment = glCreateShader(GL_FRAGMENT_SHADER);
  }
  if (geocode) {
    shader->geometry = glCreateShader(GL_GEOMETRY_SHADER);
  }

  shader->program = glCreateProgram();

  if (!shader->program || (vertexcode && !shader->vertex) || (fragcode && !shader->fragment) ||
      (geocode && !shader->geometry)) {
    fprintf(stderr, "GPUShader, object creation failed.\n");
    GPU_shader_free(shader);
    return NULL;
  }

  gpu_shader_standard_defines(standard_defines);
  gpu_shader_standard_extensions(standard_extensions);

  if (vertexcode) {
    const char *source[7];
    /* custom limit, may be too small, beware */
    int num_source = 0;

    source[num_source++] = gpu_shader_version();
    source[num_source++] =
        "#define GPU_VERTEX_SHADER\n"
        "#define IN_OUT out\n";
    source[num_source++] = standard_extensions;
    source[num_source++] = standard_defines;

    if (geocode) {
      source[num_source++] = "#define USE_GEOMETRY_SHADER\n";
    }
    if (defines) {
      source[num_source++] = defines;
    }
    source[num_source++] = vertexcode;

    gpu_dump_shaders(source, num_source, DEBUG_SHADER_VERTEX);

    glAttachShader(shader->program, shader->vertex);
    glShaderSource(shader->vertex, num_source, source, NULL);

    glCompileShader(shader->vertex);
    glGetShaderiv(shader->vertex, GL_COMPILE_STATUS, &status);

    if (!status) {
      glGetShaderInfoLog(shader->vertex, sizeof(log), &length, log);
      shader_print_errors("compile", log, source, num_source);

      GPU_shader_free(shader);
      return NULL;
    }
  }

  if (fragcode) {
    const char *source[8];
    int num_source = 0;

    source[num_source++] = gpu_shader_version();
    source[num_source++] =
        "#define GPU_FRAGMENT_SHADER\n"
        "#define IN_OUT in\n";
    source[num_source++] = standard_extensions;
    source[num_source++] = standard_defines;

    if (geocode) {
      source[num_source++] = "#define USE_GEOMETRY_SHADER\n";
    }
    if (defines) {
      source[num_source++] = defines;
    }
    if (libcode) {
      source[num_source++] = libcode;
    }
    source[num_source++] = fragcode;

    gpu_dump_shaders(source, num_source, DEBUG_SHADER_FRAGMENT);

    glAttachShader(shader->program, shader->fragment);
    glShaderSource(shader->fragment, num_source, source, NULL);

    glCompileShader(shader->fragment);
    glGetShaderiv(shader->fragment, GL_COMPILE_STATUS, &status);

    if (!status) {
      glGetShaderInfoLog(shader->fragment, sizeof(log), &length, log);
      shader_print_errors("compile", log, source, num_source);

      GPU_shader_free(shader);
      return NULL;
    }
  }

  if (geocode) {
    const char *source[6];
    int num_source = 0;

    source[num_source++] = gpu_shader_version();
    source[num_source++] = "#define GPU_GEOMETRY_SHADER\n";
    source[num_source++] = standard_extensions;
    source[num_source++] = standard_defines;

    if (defines) {
      source[num_source++] = defines;
    }
    source[num_source++] = geocode;

    gpu_dump_shaders(source, num_source, DEBUG_SHADER_GEOMETRY);

    glAttachShader(shader->program, shader->geometry);
    glShaderSource(shader->geometry, num_source, source, NULL);

    glCompileShader(shader->geometry);
    glGetShaderiv(shader->geometry, GL_COMPILE_STATUS, &status);

    if (!status) {
      glGetShaderInfoLog(shader->geometry, sizeof(log), &length, log);
      shader_print_errors("compile", log, source, num_source);

      GPU_shader_free(shader);
      return NULL;
    }
  }

  if (tf_names != NULL) {
    glTransformFeedbackVaryings(shader->program, tf_count, tf_names, GL_INTERLEAVED_ATTRIBS);
    /* Primitive type must be setup */
    BLI_assert(tf_type != GPU_SHADER_TFB_NONE);
    shader->feedback_transform_type = tf_type;
  }

  glLinkProgram(shader->program);
  glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
  if (!status) {
    glGetProgramInfoLog(shader->program, sizeof(log), &length, log);
    /* print attached shaders in pipeline order */
    if (defines) {
      shader_print_errors("linking", log, &defines, 1);
    }
    if (vertexcode) {
      shader_print_errors("linking", log, &vertexcode, 1);
    }
    if (geocode) {
      shader_print_errors("linking", log, &geocode, 1);
    }
    if (libcode) {
      shader_print_errors("linking", log, &libcode, 1);
    }
    if (fragcode) {
      shader_print_errors("linking", log, &fragcode, 1);
    }

    GPU_shader_free(shader);
    return NULL;
  }

  glUseProgram(shader->program);
  shader->interface = GPU_shaderinterface_create(shader->program);

  return shader;
}

#undef DEBUG_SHADER_GEOMETRY
#undef DEBUG_SHADER_FRAGMENT
#undef DEBUG_SHADER_VERTEX
#undef DEBUG_SHADER_NONE

void GPU_shader_free(GPUShader *shader)
{
#if 0 /* Would be nice to have, but for now the Deferred compilation \
       * does not have a GPUContext. */
  BLI_assert(GPU_context_active_get() != NULL);
#endif
  BLI_assert(shader);

  if (shader->vertex) {
    glDeleteShader(shader->vertex);
  }
  if (shader->geometry) {
    glDeleteShader(shader->geometry);
  }
  if (shader->fragment) {
    glDeleteShader(shader->fragment);
  }
  if (shader->program) {
    glDeleteProgram(shader->program);
  }

  if (shader->interface) {
    GPU_shaderinterface_discard(shader->interface);
  }

  MEM_freeN(shader);
}

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 *shader)
{
  BLI_assert(shader && shader->program);

  glUseProgram(shader->program);
  GPU_matrix_bind(shader->interface);
  GPU_shader_set_srgb_uniform(shader->interface);
}

void GPU_shader_unbind(void)
{
  glUseProgram(0);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Transform feedback
 * \{ */

bool GPU_shader_transform_feedback_enable(GPUShader *shader, uint vbo_id)
{
  if (shader->feedback_transform_type == GPU_SHADER_TFB_NONE) {
    return false;
  }

  glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vbo_id);

  switch (shader->feedback_transform_type) {
    case GPU_SHADER_TFB_POINTS:
      glBeginTransformFeedback(GL_POINTS);
      return true;
    case GPU_SHADER_TFB_LINES:
      glBeginTransformFeedback(GL_LINES);
      return true;
    case GPU_SHADER_TFB_TRIANGLES:
      glBeginTransformFeedback(GL_TRIANGLES);
      return true;
    default:
      return false;
  }
}

void GPU_shader_transform_feedback_disable(GPUShader *UNUSED(shader))
{
  glEndTransformFeedback();
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Uniforms / Resource location
 * \{ */

int GPU_shader_get_uniform(GPUShader *shader, const char *name)
{
  BLI_assert(shader && shader->program);
  const GPUShaderInput *uniform = GPU_shaderinterface_uniform(shader->interface, name);
  return uniform ? uniform->location : -1;
}

int GPU_shader_get_builtin_uniform(GPUShader *shader, int builtin)
{
  BLI_assert(shader && shader->program);
  return GPU_shaderinterface_uniform_builtin(shader->interface,
                                             static_cast<GPUUniformBuiltin>(builtin));
}

int GPU_shader_get_builtin_block(GPUShader *shader, int builtin)
{
  BLI_assert(shader && shader->program);
  return GPU_shaderinterface_block_builtin(shader->interface,
                                           static_cast<GPUUniformBlockBuiltin>(builtin));
}

int GPU_shader_get_uniform_block(GPUShader *shader, const char *name)
{
  BLI_assert(shader && shader->program);
  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)
{
  BLI_assert(shader && shader->program);
  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)
{
  BLI_assert(shader && shader->program);
  const GPUShaderInput *tex = GPU_shaderinterface_uniform(shader->interface, name);
  return tex ? tex->binding : -1;
}

int GPU_shader_get_attribute(GPUShader *shader, const char *name)
{
  BLI_assert(shader && shader->program);
  const GPUShaderInput *attr = GPU_shaderinterface_attr(shader->interface, name);
  return attr ? attr->location : -1;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Getters
 * \{ */

/* Clement : Temp */
int GPU_shader_get_program(GPUShader *shader)
{
  return (int)shader->program;
}

char *GPU_shader_get_binary(GPUShader *shader, uint *r_binary_format, int *r_binary_len)
{
  BLI_assert(GLEW_ARB_get_program_binary);
  char *r_binary;
  int binary_len = 0;

  glGetProgramiv(shader->program, GL_PROGRAM_BINARY_LENGTH, &binary_len);
  r_binary = (char *)MEM_mallocN(binary_len, __func__);
  glGetProgramBinary(shader->program, binary_len, NULL, r_binary_format, r_binary);

  if (r_binary_len) {
    *r_binary_len = binary_len;
  }

  return r_binary;
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Uniforms setters
 * \{ */

void GPU_shader_uniform_float(GPUShader *UNUSED(shader), int location, float value)
{
  if (location == -1) {
    return;
  }

  glUniform1f(location, value);
}

void GPU_shader_uniform_vector(
    GPUShader *UNUSED(shader), int location, int length, int arraysize, const float *value)
{
  if (location == -1 || value == NULL) {
    return;
  }

  switch (length) {
    case 1:
      glUniform1fv(location, arraysize, value);
      break;
    case 2:
      glUniform2fv(location, arraysize, value);
      break;
    case 3:
      glUniform3fv(location, arraysize, value);
      break;
    case 4:
      glUniform4fv(location, arraysize, value);
      break;
    case 9:
      glUniformMatrix3fv(location, arraysize, 0, value);
      break;
    case 16:
      glUniformMatrix4fv(location, arraysize, 0, value);
      break;
    default:
      BLI_assert(0);
      break;
  }
}

void GPU_shader_uniform_int(GPUShader *UNUSED(shader), int location, int value)
{
  if (location == -1) {
    return;
  }

  glUniform1i(location, value);
}

void GPU_shader_uniform_vector_int(
    GPUShader *UNUSED(shader), int location, int length, int arraysize, const int *value)
{
  if (location == -1) {
    return;
  }

  switch (length) {
    case 1:
      glUniform1iv(location, arraysize, value);
      break;
    case 2:
      glUniform2iv(location, arraysize, value);
      break;
    case 3:
      glUniform3iv(location, arraysize, value);
      break;
    case 4:
      glUniform4iv(location, arraysize, value);
      break;
    default:
      BLI_assert(0);
      break;
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \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(const GPUShaderInterface *interface)
{
  int32_t loc = GPU_shaderinterface_uniform_builtin(interface, GPU_UNIFORM_SRGB_TRANSFORM);
  if (loc != -1) {
    glUniform1i(loc, 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;
}

/** \} */