diff options
author | Mike Erwin <significant.bit@gmail.com> | 2016-09-17 14:33:02 +0300 |
---|---|---|
committer | Mike Erwin <significant.bit@gmail.com> | 2016-09-17 14:33:48 +0300 |
commit | 1b1275f0db85101345def817fee486beebaa6b9a (patch) | |
tree | 1c6338f84ca852f587e213cea609a3f026984886 /source/blender | |
parent | c3034afa586e3c5009f852e42c6a46000daa2551 (diff) |
add GPU_SHADER_TEXT for font rendering
With USE_GLSL enabled, GPU_basic_shader(TEXTURE|COLOR) always rendered black. New shader uses a solid color + alpha channel of texture (which in our case is a font glyph). See fragment shader for details.
I prefer this approah -- multiple shaders that each do one thing well (and are easy to read/write/understand), instead of one shader that can do many things given the right options.
Diffstat (limited to 'source/blender')
-rw-r--r-- | source/blender/blenfont/intern/blf.c | 7 | ||||
-rw-r--r-- | source/blender/gpu/CMakeLists.txt | 3 | ||||
-rw-r--r-- | source/blender/gpu/GPU_shader.h | 2 | ||||
-rw-r--r-- | source/blender/gpu/intern/gpu_shader.c | 18 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_text_frag.glsl | 14 | ||||
-rw-r--r-- | source/blender/gpu/shaders/gpu_shader_text_vert.glsl | 16 |
6 files changed, 57 insertions, 3 deletions
diff --git a/source/blender/blenfont/intern/blf.c b/source/blender/blenfont/intern/blf.c index 132a0ec3808..ec1f3d1f1f1 100644 --- a/source/blender/blenfont/intern/blf.c +++ b/source/blender/blenfont/intern/blf.c @@ -56,7 +56,7 @@ #include "IMB_colormanagement.h" #ifndef BLF_STANDALONE -#include "GPU_basic_shader.h" +#include "GPU_shader.h" #endif #include "blf_internal_types.h" @@ -501,7 +501,7 @@ static void blf_draw_gl__start(FontBLF *font, GLint *mode) glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); #ifndef BLF_STANDALONE - GPU_basic_shader_bind(GPU_SHADER_TEXTURE_2D | GPU_SHADER_USE_COLOR); + GPU_shader_bind(GPU_shader_get_builtin_shader(GPU_SHADER_TEXT)); #endif /* Save the current matrix mode. */ @@ -544,8 +544,9 @@ static void blf_draw_gl__end(GLint mode) glMatrixMode(mode); #ifndef BLF_STANDALONE - GPU_basic_shader_bind(GPU_SHADER_USE_COLOR); + GPU_shader_unbind(); #endif + glDisable(GL_BLEND); } diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 6d3052b1190..a0b236d67a9 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -130,6 +130,9 @@ data_to_c_simple(shaders/gpu_shader_3D_flat_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_vert.glsl SRC) data_to_c_simple(shaders/gpu_shader_3D_smooth_color_frag.glsl SRC) +data_to_c_simple(shaders/gpu_shader_text_vert.glsl SRC) +data_to_c_simple(shaders/gpu_shader_text_frag.glsl SRC) + data_to_c_simple(shaders/gpu_shader_geometry.glsl SRC) data_to_c_simple(shaders/gpu_shader_fire_frag.glsl SRC) data_to_c_simple(shaders/gpu_shader_smoke_frag.glsl SRC) diff --git a/source/blender/gpu/GPU_shader.h b/source/blender/gpu/GPU_shader.h index 196b0ec71f8..9b3077e9abd 100644 --- a/source/blender/gpu/GPU_shader.h +++ b/source/blender/gpu/GPU_shader.h @@ -90,6 +90,8 @@ typedef enum GPUBuiltinShader { GPU_SHADER_SMOKE = 2, GPU_SHADER_SMOKE_FIRE = 3, + GPU_SHADER_TEXT, + /* for simple 2D drawing */ GPU_SHADER_2D_UNIFORM_COLOR, GPU_SHADER_2D_FLAT_COLOR, diff --git a/source/blender/gpu/intern/gpu_shader.c b/source/blender/gpu/intern/gpu_shader.c index dd576a1fcf7..fbb9c9ed899 100644 --- a/source/blender/gpu/intern/gpu_shader.c +++ b/source/blender/gpu/intern/gpu_shader.c @@ -58,6 +58,9 @@ extern char datatoc_gpu_shader_3D_flat_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_smooth_color_vert_glsl[]; extern char datatoc_gpu_shader_3D_smooth_color_frag_glsl[]; +extern char datatoc_gpu_shader_text_vert_glsl[]; +extern char datatoc_gpu_shader_text_frag_glsl[]; + extern char datatoc_gpu_shader_fire_frag_glsl[]; extern char datatoc_gpu_shader_smoke_vert_glsl[]; extern char datatoc_gpu_shader_smoke_frag_glsl[]; @@ -83,6 +86,8 @@ 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 drawing text */ + GPUShader *text; /* for simple 2D drawing */ GPUShader *uniform_color_2D; GPUShader *flat_color_2D; @@ -610,6 +615,14 @@ GPUShader *GPU_shader_get_builtin_shader(GPUBuiltinShader shader) NULL, NULL, NULL, 0, 0, 0); retval = GG.shaders.smoke_fire; break; + case GPU_SHADER_TEXT: + if (!GG.shaders.text) + GG.shaders.text = GPU_shader_create( + datatoc_gpu_shader_text_vert_glsl, + datatoc_gpu_shader_text_frag_glsl, + NULL, NULL, NULL, 0, 0, 0); + retval = GG.shaders.text; + break; case GPU_SHADER_2D_UNIFORM_COLOR: if (!GG.shaders.uniform_color_2D) GG.shaders.uniform_color_2D = GPU_shader_create( @@ -777,6 +790,11 @@ void GPU_shader_free_builtin_shaders(void) GG.shaders.smoke_fire = NULL; } + if (GG.shaders.text) { + GPU_shader_free(GG.shaders.text); + GG.shaders.text = NULL; + } + if (GG.shaders.uniform_color_2D) { GPU_shader_free(GG.shaders.uniform_color_2D); GG.shaders.uniform_color_2D = NULL; diff --git a/source/blender/gpu/shaders/gpu_shader_text_frag.glsl b/source/blender/gpu/shaders/gpu_shader_text_frag.glsl new file mode 100644 index 00000000000..4869a6360fa --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_text_frag.glsl @@ -0,0 +1,14 @@ + +flat varying vec4 color; +varying vec2 texcoord; + +uniform sampler2D glyph; + +void main() +{ + // input color replaces texture color + gl_FragColor.rgb = color.rgb; + + // modulate input alpha & texture alpha + gl_FragColor.a = color.a * texture2D(glyph, texcoord).a; +} diff --git a/source/blender/gpu/shaders/gpu_shader_text_vert.glsl b/source/blender/gpu/shaders/gpu_shader_text_vert.glsl new file mode 100644 index 00000000000..8449c4229b3 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_shader_text_vert.glsl @@ -0,0 +1,16 @@ + +// TODO(merwin): +// - use modern GLSL +// - uniform color, not per vertex +// - generic attrib inputs (2D pos, tex coord) + +flat varying vec4 color; +varying vec2 texcoord; + +void main() +{ + gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; + + color = gl_Color; + texcoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).st; +} |