Browse Source

Work around black text issue in OpenGL ES 2 renderer

rdb 9 years ago
parent
commit
591ce04ab1

+ 14 - 0
panda/src/display/graphicsStateGuardian.cxx

@@ -1239,6 +1239,20 @@ fetch_specified_part(Shader::ShaderMatInput part, InternalName *name,
       return &LMatrix4::ident_mat();
     }
   }
+  case Shader::SMO_tex_is_alpha_i: {
+    // This is a hack so we can support both F_alpha and other
+    // formats in the default shader, to fix font rendering in GLES2
+    const TextureAttrib *ta;
+    if (_target_rs->get_attrib(ta) &&
+        index < ta->get_num_on_stages()) {
+      TextureStage *ts = ta->get_on_stage(index);
+      PN_stdfloat v = (ta->get_on_texture(ts)->get_format() == Texture::F_alpha);
+      t = LMatrix4(0,0,0,0,0,0,0,0,0,0,0,0,v,v,v,0);
+      return &t;
+    } else {
+      return &LMatrix4::zeros_mat();
+    }
+  }
   case Shader::SMO_plane_x: {
     const NodePath &np = _target_shader->get_shader_input_nodepath(name);
     nassertr(!np.is_empty(), &LMatrix4::zeros_mat());

+ 13 - 8
panda/src/glstuff/glGraphicsStateGuardian_src.cxx

@@ -155,14 +155,14 @@ static const string default_vshader =
   "attribute vec4 p3d_Color;\n"
   "attribute vec2 p3d_MultiTexCoord0;\n"
   "varying vec2 texcoord;\n"
-  "varying vec4 color;\n"
+  "varying lowp vec4 color;\n"
 #endif
   "uniform mat4 p3d_ModelViewProjectionMatrix;\n"
   "uniform vec4 p3d_ColorScale;\n"
   "void main(void) {\n"
   "  gl_Position = p3d_ModelViewProjectionMatrix * p3d_Vertex;\n"
   "  texcoord = p3d_MultiTexCoord0;\n"
-  "  color = p3d_Color * p3d_ColorScale;\n"
+  "  color = p3d_Color;\n"
   "}\n";
 
 static const string default_fshader =
@@ -171,18 +171,23 @@ static const string default_fshader =
   "in vec2 texcoord;\n"
   "in vec4 color;\n"
   "out vec4 p3d_FragColor;"
+  "uniform sampler2D p3d_Texture0;\n"
+  "uniform vec4 p3d_TexAlphaOnly;\n"
 #else
   "precision mediump float;\n"
   "varying vec2 texcoord;\n"
-  "varying vec4 color;\n"
+  "varying lowp vec4 color;\n"
+  "uniform lowp sampler2D p3d_Texture0;\n"
+  "uniform lowp vec4 p3d_TexAlphaOnly;\n"
 #endif
-  "uniform sampler2D p3d_Texture0;\n"
   "void main(void) {\n"
 #ifndef OPENGLES
   "  p3d_FragColor = texture(p3d_Texture0, texcoord);\n"
-  "  p3d_FragColor *= color;\n"
+  "  p3d_FragColor += p3d_TexAlphaOnly;\n" // Hack for text rendering
+  "  p3d_FragColor = color;\n"
 #else
   "  gl_FragColor = texture2D(p3d_Texture0, texcoord).bgra;\n"
+  "  gl_FragColor += p3d_TexAlphaOnly;\n" // Hack for text rendering
   "  gl_FragColor *= color;\n"
 #endif
   "}\n";
@@ -11549,9 +11554,9 @@ upload_texture(CLP(TextureContext) *gtc, bool force, bool uses_mipmaps) {
     // texture formats?
     switch (tex->get_format()) {
     case Texture::F_alpha:
-      glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_ONE);
-      glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_ONE);
-      glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_ONE);
+      glTexParameteri(target, GL_TEXTURE_SWIZZLE_R, GL_ZERO);
+      glTexParameteri(target, GL_TEXTURE_SWIZZLE_G, GL_ZERO);
+      glTexParameteri(target, GL_TEXTURE_SWIZZLE_B, GL_ZERO);
       glTexParameteri(target, GL_TEXTURE_SWIZZLE_A, GL_RED);
       break;
 

+ 20 - 0
panda/src/glstuff/glShaderContext_src.cxx

@@ -1198,6 +1198,22 @@ reflect_uniform(int i, char *name_buffer, GLsizei name_buflen) {
       _slider_table_size = param_size;
       return;
     }
+    if (noprefix == "TexAlphaOnly") {
+      Shader::ShaderMatSpec bind;
+      bind._id = arg_id;
+      bind._func = Shader::SMF_first;
+      bind._index = 0;
+      bind._part[0] = Shader::SMO_tex_is_alpha_i;
+      bind._arg[0] = NULL;
+      bind._dep[0] = Shader::SSD_general | Shader::SSD_texture | Shader::SSD_frame;
+      bind._part[1] = Shader::SMO_identity;
+      bind._arg[1] = NULL;
+      bind._dep[1] = Shader::SSD_NONE;
+      bind._piece = Shader::SMP_row3;
+      _shader->_mat_spec.push_back(bind);
+      _shader->_mat_deps |= bind._dep[0] | bind._dep[1];
+      return;
+    }
     GLCAT.error() << "Unrecognized uniform name '" << param_name << "'!\n";
     return;
 
@@ -1850,6 +1866,10 @@ set_state_and_transform(const RenderState *target_rs,
         target_rs->get_attrib(TexMatrixAttrib::get_class_slot())) {
       altered |= Shader::SSD_tex_matrix;
     }
+    if (_state_rs->get_attrib(TextureAttrib::get_class_slot()) !=
+        target_rs->get_attrib(TextureAttrib::get_class_slot())) {
+      altered |= Shader::SSD_texture;
+    }
     _state_rs = target_rs;
   }
 

+ 3 - 0
panda/src/gobj/shader.cxx

@@ -509,6 +509,9 @@ cp_dependency(ShaderMatInput inp) {
       (inp == SMO_apiclip_to_apiview)) {
     dep |= SSD_projection;
   }
+  if (inp == SMO_tex_is_alpha_i) {
+    dep |= SSD_texture | SSD_frame;
+  }
 
   return dep;
 }

+ 4 - 0
panda/src/gobj/shader.h

@@ -201,6 +201,9 @@ public:
     // Additional properties for PBR materials
     SMO_attr_material2,
 
+    // Hack for text rendering.  Don't use in user shaders.
+    SMO_tex_is_alpha_i,
+
     SMO_INVALID
   };
 
@@ -297,6 +300,7 @@ public:
     SSD_tex_matrix    = 0x200,
     SSD_frame         = 0x400,
     SSD_projection    = 0x800,
+    SSD_texture      = 0x1000,
   };
 
   enum ShaderBug {