GPU: Avoid undocumented/fragile dependency on shader enumerator order

Previously this was using `GPU_SHADER_TEXT` as default value indicating
an "unset" state. This wasn't documented in the definition (and so
D16284 added a new enumerator that broke this). Plus code was assuming
this enumerator would always have the value 0 without specifying this in
the definition either.

In this case it's easy to not rely on the enum value at all, and just
use `std::optional` to add a "unset" state.

Differential Revision: https://developer.blender.org/D16303
This commit is contained in:
2022-10-20 12:51:20 +02:00
parent 8a43bfd8fd
commit c81bc09876
2 changed files with 12 additions and 7 deletions

View File

@@ -45,7 +45,7 @@ void immBindShader(GPUShader *shader)
BLI_assert(imm->shader == nullptr);
imm->shader = shader;
imm->builtin_shader_bound = GPU_SHADER_TEXT; /* Default value. */
imm->builtin_shader_bound = std::nullopt;
if (!imm->vertex_format.packed) {
VertexFormat_pack(&imm->vertex_format);
@@ -125,9 +125,12 @@ static void wide_line_workaround_start(GPUPrimType prim_type)
/* No need to change the shader. */
return;
}
if (!imm->builtin_shader_bound) {
return;
}
eGPUBuiltinShader polyline_sh;
switch (imm->builtin_shader_bound) {
switch (*imm->builtin_shader_bound) {
case GPU_SHADER_3D_CLIPPED_UNIFORM_COLOR:
polyline_sh = GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR;
break;
@@ -180,8 +183,8 @@ static void wide_line_workaround_end()
}
immUnbindProgram();
immBindBuiltinProgram(imm->prev_builtin_shader);
imm->prev_builtin_shader = GPU_SHADER_TEXT;
immBindBuiltinProgram(*imm->prev_builtin_shader);
imm->prev_builtin_shader = std::nullopt;
}
}