Blender  V3.3
gpu_shader_builtin.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2005 Blender Foundation. All rights reserved. */
3 
8 #include "BLI_utildefines.h"
9 
10 #include "GPU_shader.h"
11 
12 /* Adjust these constants as needed. */
13 #define MAX_DEFINE_LENGTH 256
14 #define MAX_EXT_DEFINE_LENGTH 512
15 
16 /* Non-generated shaders */
41 
61 
63 
73 
77 
82 
86 
88 
90 
93  {
94  .lib = "",
95  .def = "#define blender_srgb_to_framebuffer_space(a) a\n",
96  },
98  {
100  .def = "#define USE_WORLD_CLIP_PLANES\n"
101  "#define blender_srgb_to_framebuffer_space(a) a\n",
102  },
103 };
104 
105 /* cache of built-in shaders (each is created on first use) */
107 
108 typedef struct {
109  const char *name;
110  const char *vert;
112  const char *geom;
113  const char *frag;
115  const char *defs;
116 
117  const char *create_info;
118  const char *clipped_create_info;
120 
122  [GPU_SHADER_TEXT] =
123  {
124  .name = "GPU_SHADER_TEXT",
125  .create_info = "gpu_shader_text",
126  },
128  {
129  .name = "GPU_SHADER_KEYFRAME_SHAPE",
130  .create_info = "gpu_shader_keyframe_shape",
131  },
133  {
134  .name = "GPU_SHADER_SIMPLE_LIGHTING",
135  .create_info = "gpu_shader_simple_lighting",
136  },
138  {
139  .name = "GPU_SHADER_3D_IMAGE",
140  .create_info = "gpu_shader_3D_image",
141  },
143  {
144  .name = "GPU_SHADER_3D_IMAGE_MODULATE_ALPHA",
145  .create_info = "gpu_shader_3D_image_modulate_alpha",
146  },
148  {
149  .name = "GPU_SHADER_2D_CHECKER",
150  .create_info = "gpu_shader_2D_checker",
151  },
152 
154  {
155  .name = "GPU_SHADER_2D_DIAG_STRIPES",
156  .create_info = "gpu_shader_2D_diag_stripes",
157  },
158 
160  {
161  .name = "GPU_SHADER_2D_UNIFORM_COLOR",
162  .create_info = "gpu_shader_2D_uniform_color",
163  },
165  {
166  .name = "GPU_SHADER_2D_FLAT_COLOR",
167  .create_info = "gpu_shader_2D_flat_color",
168  },
170  {
171  .name = "GPU_SHADER_2D_SMOOTH_COLOR",
172  .create_info = "gpu_shader_2D_smooth_color",
173  },
175  {
176  .name = "GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE",
177  .create_info = "gpu_shader_2D_image_overlays_merge",
178  },
180  {
181  .name = "GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE",
182  .create_info = "gpu_shader_2D_image_overlays_stereo_merge",
183  },
185  {
186  .name = "GPU_SHADER_2D_IMAGE",
187  .create_info = "gpu_shader_2D_image",
188  },
190  {
191  .name = "GPU_SHADER_2D_IMAGE_COLOR",
192  .create_info = "gpu_shader_2D_image_color",
193  },
195  {
196  .name = "GPU_SHADER_2D_IMAGE_DESATURATE_COLOR",
197  .create_info = "gpu_shader_2D_image_desaturate_color",
198  },
200  {
201  .name = "GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR",
202  .create_info = "gpu_shader_2D_image_shuffle_color",
203  },
205  {
206  .name = "GPU_SHADER_2D_IMAGE_RECT_COLOR",
207  .create_info = "gpu_shader_2D_image_rect_color",
208  },
210  {
211  .name = "GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR",
212  .create_info = "gpu_shader_2D_image_multi_rect_color",
213  },
214 
216  {
217  .name = "GPU_SHADER_3D_UNIFORM_COLOR",
218  .create_info = "gpu_shader_3D_uniform_color",
219  .clipped_create_info = "gpu_shader_3D_uniform_color_clipped",
220  },
222  {
223  .name = "GPU_SHADER_3D_FLAT_COLOR",
224  .create_info = "gpu_shader_3D_flat_color",
225  .clipped_create_info = "gpu_shader_3D_flat_color_clipped",
226  },
228  {
229  .name = "GPU_SHADER_3D_SMOOTH_COLOR",
230  .create_info = "gpu_shader_3D_smooth_color",
231  .clipped_create_info = "gpu_shader_3D_smooth_color_clipped",
232  },
234  {
235  .name = "GPU_SHADER_3D_DEPTH_ONLY",
236  .create_info = "gpu_shader_3D_depth_only",
237  .clipped_create_info = "gpu_shader_3D_depth_only_clipped",
238  },
240  {
241  .name = "GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR",
242  .create_info = "gpu_shader_3D_clipped_uniform_color",
243  },
244 
246  {
247  .name = "GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR",
248  .create_info = "gpu_shader_3D_polyline_uniform_color",
249  },
251  {
252  .name = "GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR",
253  .create_info = "gpu_shader_3D_polyline_uniform_color_clipped",
254  },
256  {
257  .name = "GPU_SHADER_3D_POLYLINE_FLAT_COLOR",
258  .create_info = "gpu_shader_3D_polyline_flat_color",
259  },
261  {
262  .name = "GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR",
263  .create_info = "gpu_shader_3D_polyline_smooth_color",
264  },
265 
267  {
268  .name = "GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR",
269  .create_info = "gpu_shader_2D_line_dashed_uniform_color",
270  },
272  {
273  .name = "GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR",
274  .create_info = "gpu_shader_3D_line_dashed_uniform_color",
275  .clipped_create_info = "gpu_shader_3D_line_dashed_uniform_color_clipped",
276  },
277 
279  {
280  .name = "GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA",
281  .create_info = "gpu_shader_2D_point_uniform_size_uniform_color_aa",
282  },
284  {
285  .name = "GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA",
286  .create_info = "gpu_shader_2D_point_uniform_size_uniform_color_outline_aa",
287  },
289  {
290  .name = "GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR",
291  .create_info = "gpu_shader_3D_point_fixed_size_varying_color",
292  },
294  {
295  .name = "GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR",
296  .create_info = "gpu_shader_3D_point_varying_size_varying_color",
297  },
299  {
300  .name = "GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA",
301  .create_info = "gpu_shader_3D_point_uniform_size_uniform_color_aa",
302  .clipped_create_info = "gpu_shader_3D_point_uniform_size_uniform_color_aa_clipped",
303  },
304 
306  {
307  .name = "GPU_SHADER_2D_AREA_BORDERS",
308  .create_info = "gpu_shader_2D_area_borders",
309  },
311  {
312  .name = "GPU_SHADER_2D_WIDGET_BASE",
313  .create_info = "gpu_shader_2D_widget_base",
314  },
316  {
317  .name = "GPU_SHADER_2D_WIDGET_BASE_INST",
318  .defs = "#define USE_INSTANCE\n",
319  .create_info = "gpu_shader_2D_widget_base_inst",
320  },
322  {
323  .name = "GPU_SHADER_2D_WIDGET_SHADOW",
324  .create_info = "gpu_shader_2D_widget_shadow",
325  },
327  {
328  .name = "GPU_SHADER_2D_NODELINK",
329  .create_info = "gpu_shader_2D_nodelink",
330  },
331 
333  {
334  .name = "GPU_SHADER_2D_NODELINK_INST",
335  .create_info = "gpu_shader_2D_nodelink_inst",
336  },
337 
339  {
340  .name = "GPU_SHADER_GPENCIL_STROKE",
341  .create_info = "gpu_shader_gpencil_stroke",
342  },
343 };
344 
346  eGPUShaderConfig sh_cfg)
347 {
349  BLI_assert(sh_cfg < GPU_SHADER_CFG_LEN);
350  GPUShader **sh_p = &builtin_shaders[sh_cfg][shader];
351 
352  if (*sh_p == NULL) {
353  const GPUShaderStages *stages = &builtin_shader_stages[shader];
354 
355  /* common case */
356  if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
357  if (stages->create_info != NULL) {
359  if (ELEM(shader,
364  /* Set a default value for `lineSmooth`.
365  * Ideally this value should be set by the caller. */
366  GPU_shader_bind(*sh_p);
367  GPU_shader_uniform_1i(*sh_p, "lineSmooth", 1);
368  }
369  }
370  else {
372  stages->name,
373  {
374  .vert = (const char *[]){stages->vert, NULL},
375  .geom = (const char *[]){stages->geom, NULL},
376  .frag =
377  (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL},
378  .defs = (const char *[]){stages->defs, NULL},
379  });
380  }
381  }
382  else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
383  /* Remove eventually, for now ensure support for each shader has been added. */
384  BLI_assert(ELEM(shader,
391  /* In rare cases geometry shaders calculate clipping themselves. */
392  if (stages->clipped_create_info != NULL) {
393  *sh_p = GPU_shader_create_from_info_name(stages->clipped_create_info);
394  }
395  else {
396  const char *world_clip_lib = datatoc_gpu_shader_cfg_world_clip_lib_glsl;
397  const char *world_clip_def = "#define USE_WORLD_CLIP_PLANES\n";
399  stages->name,
400  {
401  .vert = (const char *[]){world_clip_lib, stages->vert, NULL},
402  .geom = (const char *[]){stages->geom ? world_clip_lib : NULL, stages->geom, NULL},
403  .frag =
404  (const char *[]){datatoc_gpu_shader_colorspace_lib_glsl, stages->frag, NULL},
405  .defs = (const char *[]){world_clip_def, stages->defs, NULL},
406  });
407  }
408  }
409  else {
410  BLI_assert(0);
411  }
412  }
413 
414  return *sh_p;
415 }
416 
418 {
420 }
421 
423 {
424  for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) {
425  for (int j = 0; j < GPU_SHADER_BUILTIN_LEN; j++) {
426  if (builtin_shaders[i][j]) {
428  builtin_shaders[i][j] = NULL;
429  }
430  }
431  }
432 }
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define ELEM(...)
#define GPU_shader_create_from_arrays_named(name,...)
Definition: GPU_shader.h:91
struct GPUShader GPUShader
Definition: GPU_shader.h:20
void GPU_shader_uniform_1i(GPUShader *sh, const char *name, int value)
Definition: gpu_shader.cc:652
eGPUShaderConfig
Definition: GPU_shader.h:364
@ GPU_SHADER_CFG_DEFAULT
Definition: GPU_shader.h:365
@ GPU_SHADER_CFG_CLIPPED
Definition: GPU_shader.h:366
#define GPU_SHADER_BUILTIN_LEN
Definition: GPU_shader.h:361
GPUShader * GPU_shader_create_from_info_name(const char *info_name)
Definition: gpu_shader.cc:265
void GPU_shader_bind(GPUShader *shader)
Definition: gpu_shader.cc:491
eGPUBuiltinShader
Definition: GPU_shader.h:189
@ GPU_SHADER_2D_DIAG_STRIPES
Definition: GPU_shader.h:222
@ GPU_SHADER_3D_SMOOTH_COLOR
Definition: GPU_shader.h:245
@ GPU_SHADER_GPENCIL_STROKE
Definition: GPU_shader.h:352
@ GPU_SHADER_2D_NODELINK_INST
Definition: GPU_shader.h:359
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
Definition: GPU_shader.h:350
@ GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR
Definition: GPU_shader.h:270
@ GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR
Definition: GPU_shader.h:347
@ GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR
Definition: GPU_shader.h:349
@ GPU_SHADER_KEYFRAME_SHAPE
Definition: GPU_shader.h:192
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
Definition: GPU_shader.h:253
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
Definition: GPU_shader.h:310
@ GPU_SHADER_2D_IMAGE_DESATURATE_COLOR
Definition: GPU_shader.h:218
@ GPU_SHADER_3D_DEPTH_ONLY
Definition: GPU_shader.h:277
@ GPU_SHADER_TEXT
Definition: GPU_shader.h:191
@ GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR
Definition: GPU_shader.h:231
@ GPU_SHADER_3D_IMAGE_MODULATE_ALPHA
Definition: GPU_shader.h:300
@ GPU_SHADER_2D_CHECKER
Definition: GPU_shader.h:221
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
Definition: GPU_shader.h:338
@ GPU_SHADER_2D_IMAGE_MULTI_RECT_COLOR
Definition: GPU_shader.h:220
@ GPU_SHADER_2D_SMOOTH_COLOR
Definition: GPU_shader.h:215
@ GPU_SHADER_2D_UNIFORM_COLOR
Definition: GPU_shader.h:201
@ GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR
Definition: GPU_shader.h:329
@ GPU_SHADER_3D_UNIFORM_COLOR
Definition: GPU_shader.h:230
@ GPU_SHADER_2D_IMAGE_RECT_COLOR
Definition: GPU_shader.h:219
@ GPU_SHADER_3D_FLAT_COLOR
Definition: GPU_shader.h:238
@ GPU_SHADER_2D_WIDGET_BASE_INST
Definition: GPU_shader.h:356
@ GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR
Definition: GPU_shader.h:281
@ GPU_SHADER_3D_IMAGE
Definition: GPU_shader.h:291
@ GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE
Definition: GPU_shader.h:280
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA
Definition: GPU_shader.h:321
@ GPU_SHADER_2D_IMAGE
Definition: GPU_shader.h:216
@ GPU_SHADER_3D_POLYLINE_FLAT_COLOR
Definition: GPU_shader.h:262
@ GPU_SHADER_SIMPLE_LIGHTING
Definition: GPU_shader.h:193
@ GPU_SHADER_2D_IMAGE_COLOR
Definition: GPU_shader.h:217
@ GPU_SHADER_2D_WIDGET_SHADOW
Definition: GPU_shader.h:357
@ GPU_SHADER_2D_FLAT_COLOR
Definition: GPU_shader.h:208
@ GPU_SHADER_2D_WIDGET_BASE
Definition: GPU_shader.h:355
@ GPU_SHADER_2D_AREA_BORDERS
Definition: GPU_shader.h:354
@ GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR
Definition: GPU_shader.h:254
@ GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE
Definition: GPU_shader.h:279
@ GPU_SHADER_2D_NODELINK
Definition: GPU_shader.h:358
void GPU_shader_free(GPUShader *shader)
Definition: gpu_shader.cc:200
#define GPU_SHADER_CFG_LEN
Definition: GPU_shader.h:368
char datatoc_gpu_shader_3D_point_fixed_size_varying_color_vert_glsl[]
char datatoc_gpu_shader_image_desaturate_frag_glsl[]
char datatoc_gpu_shader_uniform_color_frag_glsl[]
char datatoc_gpu_shader_cfg_world_clip_lib_glsl[]
char datatoc_gpu_shader_3D_smooth_color_frag_glsl[]
Definition: draw_shader.cc:23
const struct GPUShaderConfigData GPU_shader_cfg_data[GPU_SHADER_CFG_LEN]
char datatoc_gpu_shader_3D_flat_color_vert_glsl[]
char datatoc_gpu_shader_image_modulate_alpha_frag_glsl[]
char datatoc_gpu_shader_3D_point_varying_size_varying_color_vert_glsl[]
char datatoc_gpu_shader_simple_lighting_frag_glsl[]
char datatoc_gpu_shader_2D_nodelink_frag_glsl[]
char datatoc_gpu_shader_2D_image_multi_rect_vert_glsl[]
char datatoc_gpu_shader_2D_flat_color_vert_glsl[]
char datatoc_gpu_shader_2D_line_dashed_frag_glsl[]
char datatoc_gpu_shader_depth_only_frag_glsl[]
char datatoc_gpu_shader_point_uniform_color_outline_aa_frag_glsl[]
char datatoc_gpu_shader_2D_smooth_color_uniform_alpha_vert_glsl[]
char datatoc_gpu_shader_instance_variying_size_variying_color_vert_glsl[]
GPUShader * GPU_shader_get_builtin_shader_with_config(eGPUBuiltinShader shader, eGPUShaderConfig sh_cfg)
void GPU_shader_free_builtin_shaders(void)
char datatoc_gpu_shader_gpencil_stroke_geom_glsl[]
char datatoc_gpu_shader_flat_color_alpha_test_0_frag_glsl[]
char datatoc_gpu_shader_image_varying_color_frag_glsl[]
char datatoc_gpu_shader_2D_widget_base_vert_glsl[]
char datatoc_gpu_shader_flat_id_frag_glsl[]
char datatoc_gpu_shader_gpencil_stroke_frag_glsl[]
char datatoc_gpu_shader_2D_widget_shadow_vert_glsl[]
char datatoc_gpu_shader_image_color_frag_glsl[]
char datatoc_gpu_shader_3D_normal_vert_glsl[]
char datatoc_gpu_shader_2D_point_uniform_size_aa_vert_glsl[]
static GPUShader * builtin_shaders[GPU_SHADER_CFG_LEN][GPU_SHADER_BUILTIN_LEN]
char datatoc_gpu_shader_3D_clipped_uniform_color_vert_glsl[]
char datatoc_gpu_shader_gpencil_stroke_vert_glsl[]
char datatoc_gpu_shader_3D_line_dashed_uniform_color_vert_glsl[]
char datatoc_gpu_shader_keyframe_shape_frag_glsl[]
char datatoc_gpu_shader_2D_widget_shadow_frag_glsl[]
char datatoc_gpu_shader_text_frag_glsl[]
char datatoc_gpu_shader_colorspace_lib_glsl[]
Definition: gpu_shader.cc:26
char datatoc_gpu_shader_point_uniform_color_aa_frag_glsl[]
char datatoc_gpu_shader_2D_nodelink_vert_glsl[]
char datatoc_gpu_shader_image_overlays_merge_frag_glsl[]
char datatoc_gpu_shader_2D_area_borders_frag_glsl[]
char datatoc_gpu_shader_2D_area_borders_vert_glsl[]
char datatoc_gpu_shader_2D_image_vert_glsl[]
char datatoc_gpu_shader_point_varying_color_frag_glsl[]
GPUShader * GPU_shader_get_builtin_shader(eGPUBuiltinShader shader)
char datatoc_gpu_shader_2D_image_rect_vert_glsl[]
char datatoc_gpu_shader_image_shuffle_color_frag_glsl[]
char datatoc_gpu_shader_3D_polyline_vert_glsl[]
char datatoc_gpu_shader_point_varying_color_varying_outline_aa_frag_glsl[]
char datatoc_gpu_shader_3D_point_uniform_size_aa_vert_glsl[]
char datatoc_gpu_shader_diag_stripes_frag_glsl[]
char datatoc_gpu_shader_3D_smooth_color_vert_glsl[]
char datatoc_gpu_shader_2D_point_uniform_size_outline_aa_vert_glsl[]
char datatoc_gpu_shader_image_frag_glsl[]
char datatoc_gpu_shader_text_vert_glsl[]
char datatoc_gpu_shader_2D_widget_base_frag_glsl[]
static const GPUShaderStages builtin_shader_stages[GPU_SHADER_BUILTIN_LEN]
char datatoc_gpu_shader_3D_polyline_geom_glsl[]
char datatoc_gpu_shader_keyframe_shape_vert_glsl[]
char datatoc_gpu_shader_2D_smooth_color_frag_glsl[]
char datatoc_gpu_shader_3D_image_vert_glsl[]
char datatoc_gpu_shader_3D_vert_glsl[]
char datatoc_gpu_shader_2D_smooth_color_vert_glsl[]
char datatoc_gpu_shader_flat_color_frag_glsl[]
char datatoc_gpu_shader_image_overlays_stereo_merge_frag_glsl[]
char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[]
char datatoc_gpu_shader_3D_passthrough_vert_glsl[]
char datatoc_gpu_shader_checker_frag_glsl[]
char datatoc_gpu_shader_2D_vert_glsl[]
char datatoc_gpu_shader_3D_polyline_frag_glsl[]
const char * lib
Definition: GPU_shader.h:371
const char * create_info
const char * clipped_create_info