Browse Source

shader: Handle legacy GLSL shader inputs via the same mechanisms

This required some logic to reconstruct structs and arrays of structs, but it reduces a lot of redundancy in shader input binding
rdb 1 year ago
parent
commit
ce2290b016

File diff suppressed because it is too large
+ 279 - 1322
panda/src/glstuff/glShaderContext_src.cxx


+ 0 - 1
panda/src/glstuff/glShaderContext_src.h

@@ -41,7 +41,6 @@ public:
   void reflect_attribute(int i, char *name_buf, GLsizei name_buflen);
   void reflect_uniform_block(int i, const char *block_name,
                              char *name_buffer, GLsizei name_buflen);
-  void reflect_uniform(int i, char *name_buffer, GLsizei name_buflen);
   bool get_sampler_texture_type(int &out, GLenum param_type);
   const ShaderType *get_param_type(GLenum type);
 

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

@@ -2976,6 +2976,12 @@ r_bind_struct_members(const Parameter &param, const InternalName *name,
     maybe_light_struct = true;
     for (size_t i = 0; i < struct_type->get_num_members(); ++i) {
       const ::ShaderType::Struct::Member &member = struct_type->get_member(i);
+
+      // Skip members with void type.
+      if (member.type->get_type() == ShaderType::Void::get_class_type()) {
+        continue;
+      }
+
       ShaderMatPiece piece;
       int offset;
       if (!check_light_struct_member(member.name, member.type, piece, offset)) {
@@ -2990,6 +2996,11 @@ r_bind_struct_members(const Parameter &param, const InternalName *name,
   for (size_t i = 0; i < struct_type->get_num_members(); ++i) {
     const ::ShaderType::Struct::Member &member = struct_type->get_member(i);
 
+    // Skip members with void type.
+    if (member.type->get_type() == ShaderType::Void::get_class_type()) {
+      continue;
+    }
+
     PT(InternalName) fqname = ((InternalName *)name)->append(member.name);
 
     // Members under a GLSL light struct may need a special treatment.

+ 32 - 0
panda/src/gobj/shaderType.cxx

@@ -19,6 +19,7 @@ const char *texture_type_suffixes[] = {
 
 ShaderType::Registry *ShaderType::_registered_types = nullptr;
 TypeHandle ShaderType::_type_handle;
+TypeHandle ShaderType::Void::_type_handle;
 TypeHandle ShaderType::Scalar::_type_handle;
 TypeHandle ShaderType::Vector::_type_handle;
 TypeHandle ShaderType::Matrix::_type_handle;
@@ -28,6 +29,7 @@ TypeHandle ShaderType::Image::_type_handle;
 TypeHandle ShaderType::Sampler::_type_handle;
 TypeHandle ShaderType::SampledImage::_type_handle;
 
+const ShaderType::Void *ShaderType::void_type;
 const ShaderType::Scalar *ShaderType::bool_type;
 const ShaderType::Scalar *ShaderType::int_type;
 const ShaderType::Scalar *ShaderType::uint_type;
@@ -60,6 +62,7 @@ init_type() {
   TypedWritable::init_type();
   ::register_type(_type_handle, "ShaderType", TypedWritable::get_class_type());
 
+  ::register_type(Void::_type_handle, "ShaderType::Void", _type_handle);
   ::register_type(Scalar::_type_handle, "ShaderType::Scalar", _type_handle);
   ::register_type(Vector::_type_handle, "ShaderType::Vector", _type_handle);
   ::register_type(Matrix::_type_handle, "ShaderType::Matrix", _type_handle);
@@ -69,6 +72,7 @@ init_type() {
   ::register_type(Sampler::_type_handle, "ShaderType::Sampler", _type_handle);
   ::register_type(SampledImage::_type_handle, "ShaderType::SampledImage", _type_handle);
 
+  void_type = ShaderType::register_type(ShaderType::Void());
   bool_type = ShaderType::register_type(ShaderType::Scalar(ST_bool));
   int_type = ShaderType::register_type(ShaderType::Scalar(ST_int));
   uint_type = ShaderType::register_type(ShaderType::Scalar(ST_uint));
@@ -84,6 +88,7 @@ init_type() {
 void ShaderType::
 register_with_read_factory() {
   WritableFactory *factory = BamReader::get_factory();
+  factory->register_factory(Void::_type_handle, Void::make_from_bam);
   factory->register_factory(Scalar::_type_handle, Scalar::make_from_bam);
   factory->register_factory(Vector::_type_handle, Vector::make_from_bam);
   factory->register_factory(Matrix::_type_handle, Matrix::make_from_bam);
@@ -162,6 +167,33 @@ get_size_bytes() const {
   }
 }
 
+/**
+ *
+ */
+void ShaderType::Void::
+output(std::ostream &out) const {
+  out << "void";
+}
+
+/**
+ * Private implementation of compare_to, only called for types with the same
+ * TypeHandle.
+ */
+int ShaderType::Void::
+compare_to_impl(const ShaderType &other) const {
+  return true;
+}
+
+/**
+ * This function is called by the BamReader's factory when a new object of
+ * type ShaderType is encountered in the Bam file.  It should create the
+ * ShaderType and extract its information from the file.
+ */
+TypedWritable *ShaderType::Void::
+make_from_bam(const FactoryParams &params) {
+  return (ShaderType *)ShaderType::void_type;
+}
+
 /**
  * Returns true if this type contains the given scalar type.
  */

+ 29 - 0
panda/src/gobj/shaderType.h

@@ -61,6 +61,7 @@ private:
   static Registry *_registered_types;
 
 PUBLISHED:
+  class Void;
   class Scalar;
   class Vector;
   class Matrix;
@@ -71,6 +72,7 @@ PUBLISHED:
   class SampledImage;
 
   // Fundamental types.
+  static const ShaderType::Void *void_type;
   static const ShaderType::Scalar *bool_type;
   static const ShaderType::Scalar *int_type;
   static const ShaderType::Scalar *uint_type;
@@ -133,6 +135,33 @@ INLINE std::ostream &operator << (std::ostream &out, const ShaderType &stype) {
   return out;
 }
 
+/**
+ * The void type, invalid for most uses.
+ */
+class EXPCL_PANDA_GOBJ ShaderType::Void final : public ShaderType {
+public:
+  virtual void output(std::ostream &out) const override;
+
+private:
+  virtual int compare_to_impl(const ShaderType &other) const override;
+
+protected:
+  static TypedWritable *make_from_bam(const FactoryParams &params);
+
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  virtual TypeHandle get_type() const override {
+    return get_class_type();
+  }
+
+private:
+  static TypeHandle _type_handle;
+
+  friend class ShaderType;
+};
+
 /**
  * A numeric scalar type, like int or float.
  */

Some files were not shown because too many files changed in this diff