Blender V5.3
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
10#include "BKE_global.hh"
11#include "BLI_utildefines.hh"
12
13#include "GPU_capabilities.hh"
14#include "GPU_shader.hh"
15
16#include "gpu_shader_private.hh"
17
18namespace blender {
19
21 /* WORKAROUND: This is needed for the polyline workaround default initialization. */
22 bool init = false;
23
24 BuiltinShader(std::string info_name) : gpu::StaticShader(info_name) {}
25};
26
27/* Cache of built-in shaders (each is created on first use). */
29
31{
32 switch (shader) {
33 case GPU_SHADER_TEXT:
34 return "gpu_shader_text";
36 return "gpu_shader_keyframe_shape";
38 return "gpu_shader_simple_lighting";
40 return "gpu_shader_3D_image";
42 return "gpu_shader_3D_image_scene_linear";
44 return "gpu_shader_3D_image_color";
46 return "gpu_shader_3D_image_color_scene_linear";
48 return "gpu_shader_2D_checker";
50 return "gpu_shader_2D_diag_stripes";
52 return "gpu_shader_2D_image_overlays_merge";
54 return "gpu_shader_2D_image_overlays_stereo_merge";
56 return "gpu_shader_2D_image_desaturate_color";
58 return "gpu_shader_2D_image_shuffle_color";
60 return "gpu_shader_2D_image_rect_color";
62 return "gpu_shader_icon";
64 return "gpu_shader_3D_uniform_color";
66 return "gpu_shader_3D_flat_color";
68 return "gpu_shader_3D_smooth_color";
70 return "gpu_shader_3D_clipped_uniform_color";
72 return "gpu_shader_polyline_uniform_color";
74 return "gpu_shader_polyline_uniform_color_clipped";
76 return "gpu_shader_polyline_flat_color";
78 return "gpu_shader_polyline_smooth_color";
80 return "gpu_shader_3D_line_dashed_uniform_color";
82 return "gpu_shader_3D_point_uniform_size_uniform_color_outline_aa";
84 return "gpu_shader_3D_point_varying_size_varying_color";
87 return "gpu_shader_3D_point_uniform_size_uniform_color_aa";
89 return "gpu_shader_3D_point_flat_color";
91 return "gpu_shader_3D_point_uniform_color";
93 return "gpu_shader_2D_area_borders";
95 return "gpu_shader_2D_rounded_corner_mask";
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_annotation";
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";
126
128 return "gpu_shader_2D_update_mipmaps_unorm_8";
130 return "gpu_shader_2D_update_mipmaps_unorm_8_layered";
132 return "gpu_shader_2D_update_mipmaps_unorm_8_8_8_8";
134 return "gpu_shader_2D_update_mipmaps_unorm_8_8_8_8_layered";
136 return "gpu_shader_2D_update_mipmaps_sfloat_16";
138 return "gpu_shader_2D_update_mipmaps_sfloat_16_layered";
140 return "gpu_shader_2D_update_mipmaps_sfloat_16_16_16_16";
142 return "gpu_shader_2D_update_mipmaps_sfloat_16_16_16_16_layered";
144 return "gpu_shader_2D_update_mipmaps_sfloat_32";
146 return "gpu_shader_2D_update_mipmaps_sfloat_32_layered";
148 return "gpu_shader_2D_update_mipmaps_sfloat_32_32_32_32";
150 return "gpu_shader_2D_update_mipmaps_sfloat_32_32_32_32_layered";
152 return "gpu_shader_2D_update_mipmaps_srgba_8_8_8_8";
154 return "gpu_shader_2D_update_mipmaps_srgba_8_8_8_8_layered";
155 default:
157 return "";
158 }
159}
160
162{
163 switch (shader) {
165 return "gpu_shader_3D_uniform_color_clipped";
167 return "gpu_shader_3D_flat_color_clipped";
169 return "gpu_shader_3D_smooth_color_clipped";
171 return "gpu_shader_3D_line_dashed_uniform_color_clipped";
173 return "gpu_shader_3D_point_uniform_size_uniform_color_aa_clipped";
175 return "gpu_shader_polyline_uniform_color_clipped";
176 default:
177 BLI_assert_msg(false, "Clipped shader configuration not available.");
178 return "";
179 }
180}
181
183 GPUShaderConfig sh_cfg)
184{
186
187#ifdef __GNUC__
188# pragma GCC diagnostic push
189# pragma GCC diagnostic ignored "-Warray-bounds"
190#endif
191 BuiltinShader **sh_p = &builtin_shaders[sh_cfg][shader];
192#ifdef __GNUC__
193# pragma GCC diagnostic pop
194#endif
195
196 if (*sh_p == nullptr) {
197 if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
198 /* Common case. */
199 const char *info_name = builtin_shader_create_info_name(shader);
200 *sh_p = MEM_new<BuiltinShader>(__func__, info_name);
201 }
202 else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
203 /* In rare cases geometry shaders calculate clipping themselves. */
204 const char *info_name_clipped = builtin_shader_create_info_name_clipped(shader);
205 if (!StringRefNull(info_name_clipped).is_empty()) {
206 *sh_p = MEM_new<BuiltinShader>(__func__, info_name_clipped);
207 }
208 }
209 else {
210 BLI_assert(0);
211 }
212 }
213
214 if ((*sh_p)->init == false) {
215 (*sh_p)->init = true;
216 if (ELEM(shader,
221 {
222 gpu::Shader *sh = (*sh_p)->get();
223 /* Set a default value for `lineSmooth`.
224 * Ideally this value should be set by the caller. */
225 GPU_shader_bind(sh);
226 GPU_shader_uniform_1i(sh, "lineSmooth", 1);
227 /* WORKAROUND: See is_polyline declaration. */
228 sh->is_polyline = true;
229 }
230 }
231
232 return (*sh_p)->get();
233}
234
236{
238
239#ifdef __GNUC__
240# pragma GCC diagnostic push
241# pragma GCC diagnostic ignored "-Warray-bounds"
242#endif
243 BuiltinShader **sh_p = &builtin_shaders[sh_cfg][shader];
244#ifdef __GNUC__
245# pragma GCC diagnostic pop
246#endif
247
248 if (*sh_p == nullptr) {
249 if (sh_cfg == GPU_SHADER_CFG_DEFAULT) {
250 /* Common case. */
251 const char *info_name = builtin_shader_create_info_name(shader);
252 *sh_p = MEM_new<BuiltinShader>(__func__, info_name);
253 }
254 else if (sh_cfg == GPU_SHADER_CFG_CLIPPED) {
255 /* In rare cases geometry shaders calculate clipping themselves. */
256 const char *info_name_clipped = builtin_shader_create_info_name_clipped(shader);
257 if (!StringRefNull(info_name_clipped).is_empty()) {
258 *sh_p = MEM_new<BuiltinShader>(__func__, info_name_clipped);
259 }
260 }
261 else {
262 BLI_assert(0);
263 }
264 }
265 (*sh_p)->ensure_compile_async();
266}
267
272
274{
275 if ((G.debug & G_DEBUG_GPU) && (GPU_backend_get_type() == GPU_BACKEND_OPENGL)) {
276 /* On some system (Mesa OpenGL), doing this warm up seems to breaks something related to debug
277 * hooks and makes the Blender application hang. */
278 return;
279 }
280
282 /* The overhead of creating the subprocesses at this exact moment can create bubbles during the
283 * startup process. It is usually fast enough on OpenGL that we can skip it. */
284 return;
285 }
286 /* Ordered by first usage in default startup screen.
287 * Adding more to this list will delay the scheduling of engine shaders and increase time to
288 * first pixel. */
308}
309
311{
312 /* Make sure non is bound before deleting. */
314 for (int i = 0; i < GPU_SHADER_CFG_LEN; i++) {
315 for (int j = 0; j < GPU_SHADER_BUILTIN_LEN; j++) {
316 MEM_SAFE_DELETE(builtin_shaders[i][j]);
317 }
318 }
319}
320
321} // namespace blender
#define GPU_SHADER_BUILTIN_LEN
#define GPU_SHADER_CFG_LEN
#define BLI_assert_unreachable()
#define ELEM(...)
#define BLI_assert_msg(a, msg)
constexpr StringRefNull()
StaticShader(std::string info_name)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_shader_builtin_warm_up()
bool GPU_use_subprocess_compilation()
void GPU_shader_free_builtin_shaders()
GPUBackendType GPU_backend_get_type()
static void gpu_shader_warm_builtin_shader_async(GPUBuiltinShader shader, GPUShaderConfig sh_cfg)
Global G
Definition blender.cc:57
static const char * builtin_shader_create_info_name_clipped(GPUBuiltinShader shader)
void GPU_shader_unbind()
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_16_16_16_16
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32_32_32_32_LAYERED
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SRGBA_8_8_8_8_LAYERED
@ GPU_SHADER_SEQUENCER_SCOPE_RASTER
@ GPU_SHADER_2D_UPDATE_MIPMAPS_UNORM_8_8_8_8
@ GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR
@ GPU_SHADER_3D_POLYLINE_FLAT_COLOR
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32_LAYERED
@ GPU_SHADER_2D_IMAGE_SHUFFLE_COLOR
@ GPU_SHADER_2D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_OUTLINE_AA
@ GPU_SHADER_2D_UPDATE_MIPMAPS_UNORM_8
@ GPU_SHADER_3D_IMAGE_COLOR_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SRGBA_8_8_8_8
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_16_16_16_16_LAYERED
@ GPU_SHADER_2D_WIDGET_BASE_INST
@ GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_16_LAYERED
@ GPU_SHADER_2D_ROUNDED_CORNER_MASK
@ GPU_SHADER_2D_IMAGE_OVERLAYS_MERGE
@ GPU_SHADER_3D_IMAGE_SCENE_LINEAR_TO_REC709_SRGB
@ GPU_SHADER_3D_POINT_UNIFORM_COLOR
@ GPU_SHADER_2D_NODE_SOCKET_INST
@ GPU_SHADER_3D_POLYLINE_SMOOTH_COLOR
@ GPU_SHADER_2D_IMAGE_OVERLAYS_STEREO_MERGE
@ GPU_SHADER_3D_LINE_DASHED_UNIFORM_COLOR
@ GPU_SHADER_3D_POINT_UNIFORM_SIZE_UNIFORM_COLOR_AA
@ GPU_SHADER_2D_UPDATE_MIPMAPS_UNORM_8_LAYERED
@ GPU_SHADER_SEQUENCER_SCOPE_RESOLVE
@ GPU_SHADER_2D_IMAGE_DESATURATE_COLOR
@ GPU_SHADER_SEQUENCER_STRIPS
@ GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR
@ GPU_SHADER_3D_POINT_VARYING_SIZE_VARYING_COLOR
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_16
@ GPU_SHADER_2D_UPDATE_MIPMAPS_UNORM_8_8_8_8_LAYERED
@ GPU_SHADER_2D_IMAGE_RECT_COLOR
@ GPU_SHADER_SEQUENCER_THUMBS
@ GPU_SHADER_2D_WIDGET_SHADOW
@ GPU_SHADER_3D_POINT_FLAT_COLOR
@ GPU_SHADER_2D_UPDATE_MIPMAPS_SFLOAT_32_32_32_32
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
static const char * builtin_shader_create_info_name(GPUBuiltinShader shader)
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
gpu::Shader * GPU_shader_get_builtin_shader_with_config(GPUBuiltinShader shader, GPUShaderConfig sh_cfg)
static BuiltinShader * builtin_shaders[GPU_SHADER_CFG_LEN][GPU_SHADER_BUILTIN_LEN]
BuiltinShader(std::string info_name)