Commit: 1b1275f0db85101345def817fee486beebaa6b9a
Author: Mike Erwin
Date:   Sat Sep 17 13:33:02 2016 +0200
Branches: blender2.8
https://developer.blender.org/rB1b1275f0db85101345def817fee486beebaa6b9a

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.

===================================================================

M       source/blender/blenfont/intern/blf.c
M       source/blender/gpu/CMakeLists.txt
M       source/blender/gpu/GPU_shader.h
M       source/blender/gpu/intern/gpu_shader.c
A       source/blender/gpu/shaders/gpu_shader_text_frag.glsl
A       source/blender/gpu/shaders/gpu_shader_text_vert.glsl

===================================================================

diff --git a/source/blender/blenfont/intern/blf.c 
b/source/blender/blenfont/intern/blf.c
index 132a0ec..ec1f3d1 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 6d3052b..a0b236d 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 196b0ec..9b3077e 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 dd576a1..fbb9c9e 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 0000000..4869a63
--- /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 0000000..8449c42
--- /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;
+}

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to