Blender V5.0
gpu_shader_builtin.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_global.hh"
10#include "BLI_utildefines.h"
11
12#include "GPU_capabilities.hh"
13#include "GPU_shader.hh"
14
15#include "gpu_shader_private.hh"
16
18 /* WORKAROUND: This is needed for the polyline workaround default initialization. */
19 bool init = false;
20
21 BuiltinShader(std::string info_name) : blender::gpu::StaticShader(info_name) {}
22};
23
24/* Cache of built-in shaders (each is created on first use). */
26
28{
29 switch (shader) {
30 case GPU_SHADER_TEXT:
31 return "gpu_shader_text";
33 return "gpu_shader_keyframe_shape";
35 return "gpu_shader_simple_lighting";
37 return "gpu_shader_3D_image";
39 return "gpu_shader_3D_image_scene_linear";
41 return "gpu_shader_3D_image_color";
43 return "gpu_shader_3D_image_color_scene_linear";
45 return "gpu_shader_2D_checker";
47 return "gpu_shader_2D_diag_stripes";
48 case GPU_SHADER_ICON:
49 return "gpu_shader_icon";
51 return "gpu_shader_2D_image_overlays_merge";
53 return "gpu_shader_2D_image_overlays_stereo_merge";
55 return "gpu_shader_2D_image_desaturate_color";
57 return "gpu_shader_2D_image_shuffle_color";
59 return "gpu_shader_2D_image_rect_color";
61 return "gpu_shader_icon_multi";
63 return "gpu_shader_3D_uniform_color";
65 return "gpu_shader_3D_flat_color";
67 return "gpu_shader_3D_smooth_color";
69 return "gpu_shader_3D_depth_only";
71 return "gpu_shader_3D_clipped_uniform_color";
73 return "gpu_shader_3D_polyline_uniform_color";
75 return "gpu_shader_3D_polyline_uniform_color_clipped";
77 return "gpu_shader_3D_polyline_flat_color";
79 return "gpu_shader_3D_polyline_smooth_color";
81 return "gpu_shader_3D_line_dashed_uniform_color";
83 return "gpu_shader_2D_point_uniform_size_uniform_color_aa";
85 return "gpu_shader_2D_point_uniform_size_uniform_color_outline_aa";
87 return "gpu_shader_3D_point_varying_size_varying_color";
89 return "gpu_shader_3D_point_uniform_size_uniform_color_aa";
91 return "gpu_shader_3D_point_flat_color";
93 return "gpu_shader_3D_point_uniform_color";
95 return "gpu_shader_2D_area_borders";
97 return "gpu_shader_2D_widget_base";
99 return "gpu_shader_2D_widget_base_inst";
101 return "gpu_shader_2D_widget_shadow";
103 return "gpu_shader_2D_node_socket";
105 return "gpu_shader_2D_node_socket_inst";
107 return "gpu_shader_2D_nodelink";
109 return "gpu_shader_gpencil_stroke";
111 return "gpu_shader_sequencer_strips";
113 return "gpu_shader_sequencer_thumbs";
115 return "gpu_shader_sequencer_scope_raster";
117 return "gpu_shader_sequencer_scope_resolve";
119 return "gpu_shader_sequencer_zebra";
121 return "gpu_shader_index_2d_array_points";
123 return "gpu_shader_index_2d_array_lines";
125 return "gpu_shader_index_2d_array_tris";
127 return "gpu_shader_xr_raycast";
128 default:
130 return "";
131 }
132}
133
135{
136 switch (shader) {
138 return "gpu_shader_3D_uniform_color_clipped";
140 return "gpu_shader_3D_flat_color_clipped";
142 return "gpu_shader_3D_smooth_color_clipped";
144 return "gpu_shader_3D_depth_only_clipped";
146 return "gpu_shader_3D_line_dashed_uniform_color_clipped";
148 return "gpu_shader_3D_point_uniform_size_uniform_color_aa_clipped";
150 return "gpu_shader_3D_polyline_uniform_color_clipped";
151 default:
152 BLI_assert_msg(false, "Clipped shader configuration not available.");
153 return "";
154 }
155}
156
158 GPUShaderConfig sh_cfg)
159{
161
162#ifdef __GNUC__
163# pragma GCC diagnostic push
164# pragma GCC diagnostic ignored "-Warray-bounds"
165#endif
166 BuiltinShader **sh_p = &builtin_shaders[sh_cfg][shader];
167#ifdef __GNUC__
168# pragma GCC diagnostic pop
169#endif
170
171 if (*sh_p == nullptr) {
172 if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
173 /* Common case. */
174 const char *info_name = builtin_shader_create_info_name(shader);
175 *sh_p = MEM_new<BuiltinShader>(__func__, info_name);
176 }
177 else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
178 /* In rare cases geometry shaders calculate clipping themselves. */
179 const char *info_name_clipped = builtin_shader_create_info_name_clipped(shader);
180 if (!blender::StringRefNull(info_name_clipped).is_empty()) {
181 *sh_p = MEM_new<BuiltinShader>(__func__, info_name_clipped);
182 }
183 }
184 else {
185 BLI_assert(0);
186 }
187 }
188
189 if ((*sh_p)->init == false) {
190 (*sh_p)->init = true;
191 if (ELEM(shader,
196 {
197 blender::gpu::Shader *sh = (*sh_p)->get();
198 /* Set a default value for `lineSmooth`.
199 * Ideally this value should be set by the caller. */
200 GPU_shader_bind(sh);
201 GPU_shader_uniform_1i(sh, "lineSmooth", 1);
202 /* WORKAROUND: See is_polyline declaration. */
203 sh->is_polyline = true;
204 }
205 }
206
207 return (*sh_p)->get();
208}
209
211{
213
214#ifdef __GNUC__
215# pragma GCC diagnostic push
216# pragma GCC diagnostic ignored "-Warray-bounds"
217#endif
218 BuiltinShader **sh_p = &builtin_shaders[sh_cfg][shader];
219#ifdef __GNUC__
220# pragma GCC diagnostic pop
221#endif
222
223 if (*sh_p == nullptr) {
224 if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
225 /* Common case. */
226 const char *info_name = builtin_shader_create_info_name(shader);
227 *sh_p = MEM_new<BuiltinShader>(__func__, info_name);
228 }
229 else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
230 /* In rare cases geometry shaders calculate clipping themselves. */
231 const char *info_name_clipped = builtin_shader_create_info_name_clipped(shader);
232 if (!blender::StringRefNull(info_name_clipped).is_empty()) {
233 *sh_p = MEM_new<BuiltinShader>(__func__, info_name_clipped);
234 }
235 }
236 else {
237 BLI_assert(0);
238 }
239 }
240 (*sh_p)->ensure_compile_async();
241}
242
247
249{
250 if ((G.debug & G_DEBUG_GPU) && (GPU_backend_get_type() == GPU_BACKEND_OPENGL)) {
251 /* On some system (Mesa OpenGL), doing this warm up seems to breaks something related to debug
252 * hooks and makes the Blender application hang. */
253 return;
254 }
255
257 /* The overhead of creating the subprocesses at this exact moment can create bubbles during the
258 * startup process. It is usually fast enough on OpenGL that we can skip it. */
259 return;
260 }
261 /* Ordered by first usage in default startup screen.
262 * Adding more to this list will delay the scheduling of engine shaders and increase time to
263 * first pixel. */
282}
283
285{
286 /* Make sure non is bound before deleting. */
288 for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) {
289 for (int j = 0; j < GPU_SHADER_BUILTIN_LEN; j++) {
290 MEM_SAFE_DELETE(builtin_shaders[i][j]);
291 }
292 }
293}
@ G_DEBUG_GPU
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert(a)
Definition BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define ELEM(...)
bool GPU_use_subprocess_compilation()
GPUBackendType GPU_backend_get_type()
void GPU_shader_bind(blender::gpu::Shader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(blender::gpu::Shader *sh, const char *name, int value)
void GPU_shader_unbind()
#define GPU_SHADER_BUILTIN_LEN
GPUBuiltinShader
@ GPU_SHADER_2D_DIAG_STRIPES
@ GPU_SHADER_SEQUENCER_THUMBS
@ GPU_SHADER_3D_SMOOTH_COLOR
@ GPU_SHADER_GPENCIL_STROKE
@ GPU_SHADER_INDEXBUF_TRIS
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
@ GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR
@ GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR
@ GPU_SHADER_KEYFRAME_SHAPE
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
@ GPU_SHADER_3D_POINT_FLAT_COLOR
@ GPU_SHADER_SEQUENCER_STRIPS
@ GPU_SHADER_2D_IMAGE_DESATURATE_COLOR
@ GPU_SHADER_3D_DEPTH_ONLY
@ GPU_SHADER_INDEXBUF_LINES
@ GPU_SHADER_TEXT
@ GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR
@ GPU_SHADER_2D_CHECKER
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
@ GPU_SHADER_3D_UNIFORM_COLOR
@ GPU_SHADER_2D_IMAGE_RECT_COLOR
@ GPU_SHADER_3D_FLAT_COLOR
@ GPU_SHADER_SEQUENCER_SCOPE_RASTER
@ GPU_SHADER_2D_WIDGET_BASE_INST
@ GPU_SHADER_ICON_MULTI
@ GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR
@ GPU_SHADER_3D_IMAGE
@ GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA
@ GPU_SHADER_3D_POLYLINE_FLAT_COLOR
@ GPU_SHADER_SIMPLE_LIGHTING
@ GPU_SHADER_3D_IMAGE_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_2D_NODE_SOCKET
@ GPU_SHADER_ICON
@ GPU_SHADER_2D_WIDGET_SHADOW
@ GPU_SHADER_SEQUENCER_ZEBRA
@ GPU_SHADER_3D_POINT_UNIFORM_COLOR
@ GPU_SHADER_SEQUENCER_SCOPE_RESOLVE
@ GPU_SHADER_2D_WIDGET_BASE
@ GPU_SHADER_XR_RAYCAST
@ GPU_SHADER_2D_AREA_BORDERS
@ GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR
@ GPU_SHADER_INDEXBUF_POINTS
@ GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE
@ GPU_SHADER_2D_NODE_SOCKET_INST
@ GPU_SHADER_2D_NODELINK
@ GPU_SHADER_3D_IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_3D_IMAGE_COLOR
#define GPU_SHADER_CFG_LEN
@ GPU_SHADER_CFG_DEFAULT
@ GPU_SHADER_CFG_CLIPPED
StaticShader(std::string info_name)
static BuiltinShader * builtin_shaders[GPU_SHADER_CFG_LEN][GPU_SHADER_BUILTIN_LEN]
blender::gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
void GPU_shader_builtin_warm_up()
static const char * builtin_shader_create_info_name_clipped(GPUBuiltinShader shader)
static void gpu_shader_warm_builtin_shader_async(GPUBuiltinShader shader, GPUShaderConfig sh_cfg)
void GPU_shader_free_builtin_shaders()
static const char * builtin_shader_create_info_name(GPUBuiltinShader shader)
blender::gpu::Shader * GPU_shader_get_builtin_shader_with_config(GPUBuiltinShader shader, GPUShaderConfig sh_cfg)
#define G(x, y, z)
BuiltinShader(std::string info_name)
i
Definition text_draw.cc:230