Browse Source

shader: Fix problems with mismatched shader input num components

rdb 2 years ago
parent
commit
182e262027
2 changed files with 53 additions and 7 deletions
  1. 52 6
      panda/src/gobj/shader.cxx
  2. 1 1
      panda/src/pgraphnodes/shaderGenerator.cxx

+ 52 - 6
panda/src/gobj/shader.cxx

@@ -2153,12 +2153,17 @@ bind_parameter(const Parameter &param) {
         if (!expect_float_vector(name, type, 3, 4)) {
           return false;
         }
-        bind._piece = SMP_row3;
         bind._func = SMF_first;
         bind._part[0] = SMO_attr_pointparams;
         bind._arg[0] = nullptr;
         bind._part[1] = SMO_identity;
         bind._arg[1] = nullptr;
+
+        if (type->as_vector()->get_num_components() == 3) {
+          bind._piece = Shader::SMP_row3x3;
+        } else {
+          bind._piece = Shader::SMP_row3;
+        }
       }
       else {
         return report_parameter_error(name, type, "unrecognized parameter name");
@@ -2176,13 +2181,18 @@ bind_parameter(const Parameter &param) {
       }
       ShaderMatSpec bind;
       bind._id = param;
-      bind._piece = SMP_row3;
       bind._func = SMF_first;
       bind._part[0] = SMO_alight_x;
       bind._arg[0] = InternalName::make(pieces[1]);
       bind._part[1] = SMO_identity;
       bind._arg[1] = nullptr;
 
+      if (type->as_vector()->get_num_components() == 3) {
+        bind._piece = Shader::SMP_row3x3;
+      } else {
+        bind._piece = Shader::SMP_row3;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }
@@ -2273,7 +2283,6 @@ bind_parameter(const Parameter &param) {
       }
       ShaderMatSpec bind;
       bind._id = param;
-      bind._piece = SMP_row3;
       bind._func = SMF_first;
       bind._part[0] = SMO_texscale_i;
       bind._arg[0] = nullptr;
@@ -2281,6 +2290,12 @@ bind_parameter(const Parameter &param) {
       bind._arg[1] = nullptr;
       bind._index = atoi(pieces[1].c_str());
 
+      if (type->as_vector()->get_num_components() == 3) {
+        bind._piece = Shader::SMP_row3x3;
+      } else {
+        bind._piece = Shader::SMP_row3;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }
@@ -2292,7 +2307,6 @@ bind_parameter(const Parameter &param) {
       }
       ShaderMatSpec bind;
       bind._id = param;
-      bind._piece = SMP_row3;
       bind._func = SMF_first;
       bind._part[0] = SMO_texcolor_i;
       bind._arg[0] = nullptr;
@@ -2300,6 +2314,12 @@ bind_parameter(const Parameter &param) {
       bind._arg[1] = nullptr;
       bind._index = atoi(pieces[1].c_str());
 
+      if (type->as_vector()->get_num_components() == 3) {
+        bind._piece = Shader::SMP_row3x3;
+      } else {
+        bind._piece = Shader::SMP_row3;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }
@@ -2311,7 +2331,6 @@ bind_parameter(const Parameter &param) {
       }
       ShaderMatSpec bind;
       bind._id = param;
-      bind._piece = SMP_row3;
       bind._func = SMF_first;
       bind._part[0] = SMO_texconst_i;
       bind._arg[0] = nullptr;
@@ -2319,6 +2338,12 @@ bind_parameter(const Parameter &param) {
       bind._arg[1] = nullptr;
       bind._index = atoi(pieces[1].c_str());
 
+      if (type->as_vector()->get_num_components() == 3) {
+        bind._piece = Shader::SMP_row3x3;
+      } else {
+        bind._piece = Shader::SMP_row3;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }
@@ -2456,12 +2481,18 @@ bind_parameter(const Parameter &param) {
       }
       ShaderMatSpec bind;
       bind._id = param;
-      bind._piece = SMP_row3;
       bind._func = SMF_first;
       bind._part[0] = SMO_texpad_x;
       bind._arg[0] = InternalName::make(pieces[1]);
       bind._part[1] = SMO_identity;
       bind._arg[1] = nullptr;
+
+      if (type->as_vector()->get_num_components() == 3) {
+        bind._piece = Shader::SMP_row3x3;
+      } else {
+        bind._piece = Shader::SMP_row3;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }
@@ -2479,6 +2510,21 @@ bind_parameter(const Parameter &param) {
       bind._arg[0] = InternalName::make(pieces[1]);
       bind._part[1] = SMO_identity;
       bind._arg[1] = nullptr;
+
+      switch (type->as_vector()->get_num_components()) {
+      case 2:
+        bind._piece = Shader::SMP_row3x2;
+        break;
+
+      case 3:
+        bind._piece = Shader::SMP_row3x3;
+        break;
+
+      case 4:
+        bind._piece = Shader::SMP_row3;
+        break;
+      }
+
       cp_add_mat_spec(bind);
       return true;
     }

+ 1 - 1
panda/src/pgraphnodes/shaderGenerator.cxx

@@ -1212,7 +1212,7 @@ synthesize_shader(const RenderState *rs, const GeomVertexAnimationSpec &anim) {
     text << "\t l_eye_position = float4(mul(trans_model_to_view, vtx_position), 1);\n";
   }
   else if (need_point_size) {
-    text << "\t float4 l_eye_position = mul(trans_model_to_view, vtx_position);\n";
+    text << "\t float3 l_eye_position = mul(trans_model_to_view, vtx_position);\n";
   }
   if (need_point_size) {
     text << "\t l_point_size = attr_pointparams.y + attr_pointparams.z / length(l_eye_position.xyz);\n";