From 396dd824285d7fb524fcc8ad8ca4cddddf2efb6d Mon Sep 17 00:00:00 2001 From: Mike Erwin Date: Thu, 4 Aug 2016 15:59:38 -0400 Subject: OpenGL: add simple shaders for 2D drawing The first two of several new simple built-in shaders (will test these before adding more). These are intended for the new immediate mode API, but you can use them just like any built-in GPUShader. Due to limitations on different platforms, shaders need to work with GLSL versions 120, 130 and 150. Final Blender 2.8 will be pure #version 150. --- source/blender/gpu/CMakeLists.txt | 5 ++++ source/blender/gpu/GPU_shader.h | 4 +++ source/blender/gpu/intern/gpu_shader.c | 34 ++++++++++++++++++++++ .../shaders/gpu_shader_2D_smooth_color_frag.glsl | 13 +++++++++ .../shaders/gpu_shader_2D_smooth_color_vert.glsl | 18 ++++++++++++ .../shaders/gpu_shader_2D_uniform_color_frag.glsl | 13 +++++++++ .../shaders/gpu_shader_2D_uniform_color_vert.glsl | 11 +++++++ 7 files changed, 98 insertions(+) create mode 100644 source/blender/gpu/shaders/gpu_shader_2D_smooth_color_frag.glsl create mode 100644 source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl create mode 100644 source/blender/gpu/shaders/gpu_shader_2D_uniform_color_frag.glsl create mode 100644 source/blender/gpu/shaders/gpu_shader_2D_uniform_color_vert.glsl (limited to 'source') diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 5708352c72e..6bdac68831d 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -100,6 +100,11 @@ set(SRC intern/gpu_private.h ) +data_to_c_simple(shaders/gpu_shader_2D_uniform_color_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_2D_uniform_color_frag.glsl SRC) +data_to_c_simple(shaders/gpu_shader_2D_smooth_color_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_2D_smooth_color_frag.glsl SRC) + data_to_c_simple(shaders/gpu_shader_geometry.glsl SRC) data_to_c_simple(shaders/gpu_shader_smoke_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_smoke_vert.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 762329ee077..395015248f9 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -89,6 +89,10 @@ typedef enum GPUBuiltinShader { GPU_SHADER_SEP_GAUSSIAN_BLUR = 1, GPU_SHADER_SMOKE = 2, GPU_SHADER_SMOKE_FIRE = 3, + + /* for simple drawing */ + GPU_SHADER_2D_UNIFORM_COLOR, + GPU_SHADER_2D_SMOOTH_COLOR, } GPUBuiltinShader; GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader); diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index df1213b01e2..c3663f2d818 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -45,6 +45,11 @@ #define MAX_EXT_DEFINE_LENGTH 1024 /* Non-generated shaders */ +extern char datatoc_gpu_shader_2D_uniform_color_vert_glsl[]; +extern char datatoc_gpu_shader_2D_uniform_color_frag_glsl[]; +extern char datatoc_gpu_shader_2D_smooth_color_vert_glsl[]; +extern char datatoc_gpu_shader_2D_smooth_color_frag_glsl[]; + extern char datatoc_gpu_shader_smoke_vert_glsl[]; extern char datatoc_gpu_shader_smoke_frag_glsl[]; extern char datatoc_gpu_shader_vsm_store_vert_glsl[]; @@ -69,6 +74,9 @@ static struct GPUShadersGlobal { GPUShader *smoke_fire; /* cache for shader fx. Those can exist in combinations so store them here */ GPUShader *fx_shaders[MAX_FX_SHADERS * 2]; + /* for simple drawing */ + GPUShader *uniform_color_2D; + GPUShader *smooth_color_2D; } shaders; } GG = {{NULL}}; @@ -622,6 +630,22 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) NULL, NULL, "#define USE_FIRE;\n", 0, 0, 0); retval = GG.shaders.smoke_fire; break; + case GPU_SHADER_2D_UNIFORM_COLOR: + if (!GG.shaders.uniform_color_2D) + GG.shaders.uniform_color_2D = GPU_shader_create( + datatoc_gpu_shader_2D_uniform_color_vert_glsl, + datatoc_gpu_shader_2D_uniform_color_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.uniform_color_2D; + break; + case GPU_SHADER_2D_SMOOTH_COLOR: + if (!GG.shaders.smooth_color_2D) + GG.shaders.smooth_color_2D = GPU_shader_create( + datatoc_gpu_shader_2D_smooth_color_vert_glsl, + datatoc_gpu_shader_2D_smooth_color_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.smooth_color_2D; + break; } if (retval == NULL) @@ -733,6 +757,16 @@ void GPU_shader_free_builtin_shaders(void) GG.shaders.smoke_fire = NULL; } + if (GG.shaders.uniform_color_2D) { + GPU_shader_free(GG.shaders.uniform_color_2D); + GG.shaders.uniform_color_2D = NULL; + } + + if (GG.shaders.smooth_color_2D) { + GPU_shader_free(GG.shaders.smooth_color_2D); + GG.shaders.smooth_color_2D = NULL; + } + for (i = 0; i < 2 * MAX_FX_SHADERS; ++i) { if (GG.shaders.fx_shaders[i]) { GPU_shader_free(GG.shaders.fx_shaders[i]); diff --git a/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_frag.glsl new file mode 100644 index 00000000000..89d23e277cb --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_frag.glsl @@ -0,0 +1,13 @@ + +#if __VERSION__ == 120 + varying vec4 finalColor; + #define fragColor gl_FragColor +#else + noperspective in vec4 finalColor; + out vec4 fragColor; +#endif + +void main() +{ + fragColor = finalColor; +} diff --git a/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl new file mode 100644 index 00000000000..e74485bfa4f --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_smooth_color_vert.glsl @@ -0,0 +1,18 @@ + +#if __VERSION__ == 120 + attribute vec2 pos; + attribute vec4 color; + + varying vec4 finalColor; +#else + in vec2 pos; + in vec4 color; + + noperspective out vec4 finalColor; +#endif + +void main() +{ + gl_Position = gl_ModelViewMatrix * vec4(pos, 0.0, 1.0); + finalColor = color; +} diff --git a/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_frag.glsl b/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_frag.glsl new file mode 100644 index 00000000000..af200bf8661 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_frag.glsl @@ -0,0 +1,13 @@ + +uniform vec4 color; + +#if __VERSION__ == 120 + #define fragColor gl_FragColor +#else + out vec4 fragColor; +#endif + +void main() +{ + fragColor = color; +} diff --git a/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_vert.glsl b/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_vert.glsl new file mode 100644 index 00000000000..68d9941195b --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_2D_uniform_color_vert.glsl @@ -0,0 +1,11 @@ + +#if __VERSION__ == 120 + attribute vec2 pos; +#else + in vec2 pos; +#endif + +void main() +{ + gl_Position = gl_ModelViewMatrix * vec4(pos, 0.0, 1.0); +} -- cgit v1.2.3