GPUShader: Add selection id shader

This is to separate id drawing from standard color drawing.
This commit is contained in:
Clément Foucault 2018-12-22 21:02:30 +01:00
parent 943852c0dc
commit 9d19ff9076
5 changed files with 64 additions and 0 deletions

View File

@ -218,6 +218,9 @@ data_to_c_simple(shaders/gpu_shader_2D_edituvs_edges_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_edituvs_faces_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_2D_edituvs_stretch_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_3D_selection_id_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_selection_id_frag.glsl SRC)
data_to_c_simple(shaders/gpu_shader_edges_front_back_persp_vert.glsl SRC)
data_to_c_simple(shaders/gpu_shader_edges_front_back_persp_geom.glsl SRC)
data_to_c_simple(shaders/gpu_shader_edges_front_back_persp_legacy_vert.glsl SRC)

View File

@ -352,6 +352,9 @@ typedef enum GPUBuiltinShader {
GPU_SHADER_2D_UV_EDGES_SMOOTH,
GPU_SHADER_2D_UV_FACES,
GPU_SHADER_2D_UV_FACES_STRETCH,
/* Selection */
GPU_SHADER_3D_FLAT_SELECT_ID,
GPU_SHADER_3D_UNIFORM_SELECT_ID,
GPU_NUM_BUILTIN_SHADERS /* (not an actual shader) */
} GPUBuiltinShader;

View File

@ -146,6 +146,9 @@ extern char datatoc_gpu_shader_2D_edituvs_edges_vert_glsl[];
extern char datatoc_gpu_shader_2D_edituvs_faces_vert_glsl[];
extern char datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl[];
extern char datatoc_gpu_shader_3D_selection_id_vert_glsl[];
extern char datatoc_gpu_shader_selection_id_frag_glsl[];
extern char datatoc_gpu_shader_2D_line_dashed_uniform_color_vert_glsl[];
extern char datatoc_gpu_shader_2D_line_dashed_frag_glsl[];
extern char datatoc_gpu_shader_2D_line_dashed_geom_glsl[];
@ -940,6 +943,13 @@ static const GPUShaderStages builtin_shader_stages[GPU_NUM_BUILTIN_SHADERS] = {
{ datatoc_gpu_shader_2D_edituvs_stretch_vert_glsl,
datatoc_gpu_shader_2D_smooth_color_frag_glsl },
[GPU_SHADER_3D_FLAT_SELECT_ID] =
{ datatoc_gpu_shader_3D_selection_id_vert_glsl,
datatoc_gpu_shader_selection_id_frag_glsl },
[GPU_SHADER_3D_UNIFORM_SELECT_ID] =
{ datatoc_gpu_shader_3D_selection_id_vert_glsl,
datatoc_gpu_shader_selection_id_frag_glsl },
[GPU_SHADER_GPENCIL_STROKE] =
{ datatoc_gpu_shader_gpencil_stroke_vert_glsl,
datatoc_gpu_shader_gpencil_stroke_frag_glsl,
@ -1007,6 +1017,9 @@ static const char *gpu_shader_get_builtin_shader_defines(
case GPU_SHADER_2D_UV_EDGES_SMOOTH:
return "#define SMOOTH_COLOR\n";
case GPU_SHADER_3D_UNIFORM_SELECT_ID:
return "#define UNIFORM_ID\n";
default:
return NULL;
}

View File

@ -0,0 +1,24 @@
uniform mat4 ModelViewProjectionMatrix;
in vec3 pos;
#ifndef UNIFORM_ID
uniform uint offset;
in uint color;
flat out vec4 id;
#endif
void main()
{
#ifndef UNIFORM_ID
id = vec4(
(((color + offset) ) & uint(0xFF)) * (1.0f / 255.0f),
(((color + offset) >> 8) & uint(0xFF)) * (1.0f / 255.0f),
(((color + offset) >> 16) & uint(0xFF)) * (1.0f / 255.0f),
(((color + offset) >> 24) ) * (1.0f / 255.0f));
#endif
gl_Position = ModelViewProjectionMatrix * vec4(pos, 1.0);
}

View File

@ -0,0 +1,21 @@
#ifdef UNIFORM_ID
uniform uint id;
#else
flat in vec4 id;
#endif
out vec4 fragColor;
void main()
{
#ifdef UNIFORM_ID
fragColor = vec4(
((id ) & uint(0xFF)) * (1.0f / 255.0f),
((id >> 8) & uint(0xFF)) * (1.0f / 255.0f),
((id >> 16) & uint(0xFF)) * (1.0f / 255.0f),
((id >> 24) ) * (1.0f / 255.0f));
#else
fragColor = id;
#endif
}