Browse Source

Added ShaderGenerator

Josh Yelon 18 years ago
parent
commit
5d0c234484

+ 3 - 0
panda/src/pgraph/Sources.pp

@@ -102,6 +102,7 @@
     sequenceNode.I sequenceNode.h \
     sequenceNode.I sequenceNode.h \
     shadeModelAttrib.I shadeModelAttrib.h \
     shadeModelAttrib.I shadeModelAttrib.h \
     shaderAttrib.I shaderAttrib.h \
     shaderAttrib.I shaderAttrib.h \
+    shaderGenerator.I shaderGenerator.h \
     shaderInput.I shaderInput.h \
     shaderInput.I shaderInput.h \
     shaderPool.I shaderPool.h \
     shaderPool.I shaderPool.h \
     showBoundsEffect.I showBoundsEffect.h \
     showBoundsEffect.I showBoundsEffect.h \
@@ -211,6 +212,7 @@
     sequenceNode.cxx \
     sequenceNode.cxx \
     shadeModelAttrib.cxx \
     shadeModelAttrib.cxx \
     shaderAttrib.cxx \
     shaderAttrib.cxx \
+    shaderGenerator.cxx \
     shaderInput.cxx \
     shaderInput.cxx \
     shaderPool.cxx \
     shaderPool.cxx \
     showBoundsEffect.cxx \
     showBoundsEffect.cxx \
@@ -317,6 +319,7 @@
     sequenceNode.I sequenceNode.h \
     sequenceNode.I sequenceNode.h \
     shadeModelAttrib.I shadeModelAttrib.h \
     shadeModelAttrib.I shadeModelAttrib.h \
     shaderAttrib.I shaderAttrib.h \
     shaderAttrib.I shaderAttrib.h \
+    shaderGenerator.I shaderGenerator.h \
     shaderInput.I shaderInput.h \
     shaderInput.I shaderInput.h \
     shaderPool.I shaderPool.h \
     shaderPool.I shaderPool.h \
     showBoundsEffect.I showBoundsEffect.h \
     showBoundsEffect.I showBoundsEffect.h \

+ 1 - 0
panda/src/pgraph/pgraph_composite4.cxx

@@ -12,6 +12,7 @@
 #include "shaderInput.cxx"
 #include "shaderInput.cxx"
 #include "shaderAttrib.cxx"
 #include "shaderAttrib.cxx"
 #include "shaderPool.cxx"
 #include "shaderPool.cxx"
+#include "shaderGenerator.cxx"
 #include "showBoundsEffect.cxx"
 #include "showBoundsEffect.cxx"
 #include "spotlight.cxx"
 #include "spotlight.cxx"
 #include "stateMunger.cxx"
 #include "stateMunger.cxx"

+ 19 - 20
panda/src/pgraph/renderState.I

@@ -601,26 +601,6 @@ compare_to(const Attribute &other) const {
   return _override - other._override;
   return _override - other._override;
 }
 }
 
 
-////////////////////////////////////////////////////////////////////
-//     Function: RenderState::get_generated_shader
-//       Access: Public
-//  Description: 
-////////////////////////////////////////////////////////////////////
-INLINE ShaderAttrib *RenderState::
-get_generated_shader() const {
-  return (ShaderAttrib*)(RenderAttrib*)_generated_shader;
-}
-
-////////////////////////////////////////////////////////////////////
-//     Function: RenderState::set_generated_shader
-//       Access: Public
-//  Description: 
-////////////////////////////////////////////////////////////////////
-INLINE void RenderState::
-set_generated_shader(ShaderAttrib *attr) {
-  _generated_shader = (RenderAttrib*)attr;
-}
-
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: RenderState::flush_level
 //     Function: RenderState::flush_level
 //       Access: Public, Static
 //       Access: Public, Static
@@ -671,3 +651,22 @@ CompositionCycleDescEntry(const RenderState *obj,
   _inverted(inverted)
   _inverted(inverted)
 {
 {
 }
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: RenderState::get_generated_shader
+//       Access: Public
+//  Description: Generate a ShaderAttrib for this RenderState.  This
+//               generated ShaderAttrib can be thought of as a
+//               replacement for the regular ShaderAttrib that is a
+//               standard part of the RenderState.
+////////////////////////////////////////////////////////////////////
+INLINE const ShaderAttrib *RenderState::
+get_generated_shader() const {
+  if (_generated_shader != (RenderAttrib*)NULL) {
+    return DCAST(ShaderAttrib, _generated_shader);
+  }
+  ((RenderState*)this)->_generated_shader =
+    ShaderGenerator::synthesize_shader(this);
+  return DCAST(ShaderAttrib, _generated_shader);
+}
+

+ 1 - 0
panda/src/pgraph/renderState.cxx

@@ -2118,3 +2118,4 @@ fillin(DatagramIterator &scan, BamReader *manager) {
     _attributes.push_back(Attribute(override));
     _attributes.push_back(Attribute(override));
   }
   }
 }
 }
+

+ 5 - 4
panda/src/pgraph/renderState.h

@@ -36,6 +36,7 @@
 #include "deletedChain.h"
 #include "deletedChain.h"
 #include "simpleHashMap.h"
 #include "simpleHashMap.h"
 #include "cacheStats.h"
 #include "cacheStats.h"
+#include "shaderGenerator.h"
 
 
 class GraphicsStateGuardianBase;
 class GraphicsStateGuardianBase;
 class FogAttrib;
 class FogAttrib;
@@ -154,13 +155,13 @@ PUBLISHED:
   
   
   int get_geom_rendering(int geom_rendering) const;
   int get_geom_rendering(int geom_rendering) const;
 
 
+  INLINE const ShaderAttrib *get_generated_shader() const;
+  
 public:
 public:
   void store_into_slots(AttribSlots *slots) const;
   void store_into_slots(AttribSlots *slots) const;
   
   
   static void bin_removed(int bin_index);
   static void bin_removed(int bin_index);
-  INLINE ShaderAttrib *get_generated_shader() const;
-  INLINE void set_generated_shader(ShaderAttrib *attr);
-
+  
   INLINE static void flush_level();
   INLINE static void flush_level();
 
 
 private:
 private:
@@ -303,7 +304,7 @@ private:
   // ShaderAttrib will be synthesized by the runtime and stored here.
   // ShaderAttrib will be synthesized by the runtime and stored here.
   // I can't declare this as a ShaderAttrib because that would create
   // I can't declare this as a ShaderAttrib because that would create
   // a circular include-file dependency problem.  Aaargh.
   // a circular include-file dependency problem.  Aaargh.
-  PT(RenderAttrib) _generated_shader;
+  CPT(RenderAttrib) _generated_shader;
   
   
   // We also cache the pointer to some critical attribs stored in the
   // We also cache the pointer to some critical attribs stored in the
   // state, if they exist.
   // state, if they exist.

+ 23 - 12
panda/src/pgraph/shaderAttrib.cxx

@@ -288,13 +288,17 @@ get_shader_input_nodepath(InternalName *id) const {
   static NodePath resfail;
   static NodePath resfail;
   Inputs::const_iterator i = _inputs.find(id);
   Inputs::const_iterator i = _inputs.find(id);
   if (i == _inputs.end()) {
   if (i == _inputs.end()) {
-    pgraph_cat.error() << "Shader input " << id->get_name() << " is not present.\n";
-    nassertr(false, resfail);
+    ostringstream strm;
+    strm << "Shader input " << id->get_name() << " is not present.\n";
+    nassert_raise(strm.str());
+    return resfail;
   } else {
   } else {
     const ShaderInput *p = (*i).second;
     const ShaderInput *p = (*i).second;
     if (p->get_value_type() != ShaderInput::M_nodepath) {
     if (p->get_value_type() != ShaderInput::M_nodepath) {
-      pgraph_cat.error() << "Shader input " << id->get_name() << " is not a nodepath.\n";
-      nassertr(false, resfail);
+      ostringstream strm;
+      strm << "Shader input " << id->get_name() << " is not a nodepath.\n";
+      nassert_raise(strm.str());
+      return resfail;
     }
     }
     return p->get_nodepath();
     return p->get_nodepath();
   }
   }
@@ -314,14 +318,16 @@ get_shader_input_vector(InternalName *id) const {
   static LVector4f resfail(0,0,0,0);
   static LVector4f resfail(0,0,0,0);
   Inputs::const_iterator i = _inputs.find(id);
   Inputs::const_iterator i = _inputs.find(id);
   if (i == _inputs.end()) {
   if (i == _inputs.end()) {
-    pgraph_cat.error() << "Shader input " << id->get_name() << " is not present.\n";
-    nassertr(false, resfail);
+    ostringstream strm;
+    strm << "Shader input " << id->get_name() << " is not present.\n";
+    nassert_raise(strm.str());
     return resfail;
     return resfail;
   } else {
   } else {
     const ShaderInput *p = (*i).second;
     const ShaderInput *p = (*i).second;
     if (p->get_value_type() != ShaderInput::M_vector) {
     if (p->get_value_type() != ShaderInput::M_vector) {
-      pgraph_cat.error() << "Shader input " << id->get_name() << " is not a vector.\n";
-      nassertr(false, resfail);
+      ostringstream strm;
+      strm << "Shader input " << id->get_name() << " is not a vector.\n";
+      nassert_raise(strm.str());
       return resfail;
       return resfail;
     }
     }
     return p->get_vector();
     return p->get_vector();
@@ -338,14 +344,16 @@ Texture *ShaderAttrib::
 get_shader_input_texture(InternalName *id) const {
 get_shader_input_texture(InternalName *id) const {
   Inputs::const_iterator i = _inputs.find(id);
   Inputs::const_iterator i = _inputs.find(id);
   if (i == _inputs.end()) {
   if (i == _inputs.end()) {
-    pgraph_cat.error() << "Shader input " << id->get_name() << " is not present.\n";
-    nassertr(false, NULL);
+    ostringstream strm;
+    strm << "Shader input " << id->get_name() << " is not present.\n";
+    nassert_raise(strm.str());
     return NULL;
     return NULL;
   } else {
   } else {
     const ShaderInput *p = (*i).second;
     const ShaderInput *p = (*i).second;
     if (p->get_value_type() != ShaderInput::M_texture) {
     if (p->get_value_type() != ShaderInput::M_texture) {
-      pgraph_cat.error() << "Shader input " << id->get_name() << " is not a texture.\n";
-      nassertr(false, NULL);
+      ostringstream strm;
+      strm <<  "Shader input " << id->get_name() << " is not a texture.\n";
+      nassert_raise(strm.str());
       return NULL;
       return NULL;
     }
     }
     return p->get_texture();
     return p->get_texture();
@@ -418,6 +426,9 @@ compare_to_impl(const RenderAttrib *other) const {
   if (this->_shader_priority != that->_shader_priority) {
   if (this->_shader_priority != that->_shader_priority) {
     return (this->_shader_priority < that->_shader_priority) ? -1 : 1;
     return (this->_shader_priority < that->_shader_priority) ? -1 : 1;
   }
   }
+  if (this->_auto_shader != that->_auto_shader) {
+    return (this->_auto_shader < that->_auto_shader) ? -1 : 1;
+  }
   if (this->_has_shader != that->_has_shader) {
   if (this->_has_shader != that->_has_shader) {
     return (this->_has_shader < that->_has_shader) ? -1 : 1;
     return (this->_has_shader < that->_has_shader) ? -1 : 1;
   }
   }