Browse Source

Fixes some bugs in cg parameter handling

Josh Yelon 18 years ago
parent
commit
32a6c8fd95

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

@@ -117,6 +117,7 @@ bind(GSG *gsg) {
     cgGLBindProgram(_cg_vprogram);
     cgGLBindProgram(_cg_vprogram);
     cgGLEnableProfile(cgGetProgramProfile(_cg_fprogram));
     cgGLEnableProfile(cgGetProgramProfile(_cg_fprogram));
     cgGLBindProgram(_cg_fprogram);
     cgGLBindProgram(_cg_fprogram);
+    cg_report_errors();
   }
   }
 #endif
 #endif
 }
 }
@@ -132,6 +133,7 @@ unbind() {
   if (_cg_context != 0) {
   if (_cg_context != 0) {
     cgGLDisableProfile(cgGetProgramProfile(_cg_vprogram));
     cgGLDisableProfile(cgGetProgramProfile(_cg_vprogram));
     cgGLDisableProfile(cgGetProgramProfile(_cg_fprogram));
     cgGLDisableProfile(cgGetProgramProfile(_cg_fprogram));
+    cg_report_errors();
   }
   }
 #endif
 #endif
 }
 }
@@ -179,6 +181,7 @@ issue_parameters(GSG *gsg, bool altered) {
       }
       }
     }
     }
   }
   }
+  cg_report_errors();
 #endif
 #endif
 }
 }
 
 
@@ -196,8 +199,10 @@ disable_shader_vertex_arrays(GSG *gsg) {
 
 
   for (int i=0; i<(int)_expansion->_var_spec.size(); i++) {
   for (int i=0; i<(int)_expansion->_var_spec.size(); i++) {
     CGparameter p = _cg_parameter_map[_expansion->_var_spec[i]._id._seqno];
     CGparameter p = _cg_parameter_map[_expansion->_var_spec[i]._id._seqno];
+    if (p == 0) continue;
     cgGLDisableClientState(p);
     cgGLDisableClientState(p);
   }
   }
+  cg_report_errors();
 #endif
 #endif
 }
 }
 
 
@@ -220,6 +225,7 @@ update_shader_vertex_arrays(CLP(ShaderContext) *prev, GSG *gsg,
   if (_cg_context == 0) {
   if (_cg_context == 0) {
     return true;
     return true;
   }
   }
+  cg_report_errors();
 
 
 #ifdef SUPPORT_IMMEDIATE_MODE
 #ifdef SUPPORT_IMMEDIATE_MODE
   if (gsg->_use_sender) {
   if (gsg->_use_sender) {
@@ -233,6 +239,7 @@ update_shader_vertex_arrays(CLP(ShaderContext) *prev, GSG *gsg,
     int nvarying = _expansion->_var_spec.size();
     int nvarying = _expansion->_var_spec.size();
     for (int i=0; i<nvarying; i++) {
     for (int i=0; i<nvarying; i++) {
       CGparameter p = _cg_parameter_map[_expansion->_var_spec[i]._id._seqno];
       CGparameter p = _cg_parameter_map[_expansion->_var_spec[i]._id._seqno];
+      if (p == 0) continue;
       InternalName *name = _expansion->_var_spec[i]._name;
       InternalName *name = _expansion->_var_spec[i]._name;
       int texslot = _expansion->_var_spec[i]._append_uv;
       int texslot = _expansion->_var_spec[i]._append_uv;
       if (texslot >= 0) {
       if (texslot >= 0) {
@@ -264,6 +271,7 @@ update_shader_vertex_arrays(CLP(ShaderContext) *prev, GSG *gsg,
       }
       }
     }
     }
   }
   }
+  cg_report_errors();
 #endif // HAVE_CG
 #endif // HAVE_CG
   return true;
   return true;
 }
 }
@@ -282,6 +290,7 @@ disable_shader_texture_bindings(GSG *gsg) {
 
 
   for (int i=0; i<(int)_expansion->_tex_spec.size(); i++) {
   for (int i=0; i<(int)_expansion->_tex_spec.size(); i++) {
     CGparameter p = _cg_parameter_map[_expansion->_tex_spec[i]._id._seqno];
     CGparameter p = _cg_parameter_map[_expansion->_tex_spec[i]._id._seqno];
+    if (p == 0) continue;
     int texunit = cgGetParameterResourceIndex(p);
     int texunit = cgGetParameterResourceIndex(p);
     gsg->_glActiveTexture(GL_TEXTURE0 + texunit);
     gsg->_glActiveTexture(GL_TEXTURE0 + texunit);
     GLP(Disable)(GL_TEXTURE_1D);
     GLP(Disable)(GL_TEXTURE_1D);
@@ -295,6 +304,7 @@ disable_shader_texture_bindings(GSG *gsg) {
     // This is probably faster - but maybe not as safe?
     // This is probably faster - but maybe not as safe?
     // cgGLDisableTextureParameter(p);
     // cgGLDisableTextureParameter(p);
   }
   }
+  cg_report_errors();
 #endif
 #endif
 }
 }
 
 
@@ -319,6 +329,7 @@ update_shader_texture_bindings(CLP(ShaderContext) *prev, GSG *gsg) {
 
 
   for (int i=0; i<(int)_expansion->_tex_spec.size(); i++) {
   for (int i=0; i<(int)_expansion->_tex_spec.size(); i++) {
     CGparameter p = _cg_parameter_map[_expansion->_tex_spec[i]._id._seqno];
     CGparameter p = _cg_parameter_map[_expansion->_tex_spec[i]._id._seqno];
+    if (p == 0) continue;
     Texture *tex = 0;
     Texture *tex = 0;
     InternalName *id = _expansion->_tex_spec[i]._name;
     InternalName *id = _expansion->_tex_spec[i]._name;
     if (id != 0) {
     if (id != 0) {
@@ -358,6 +369,21 @@ update_shader_texture_bindings(CLP(ShaderContext) *prev, GSG *gsg) {
 
 
     gsg->apply_texture(tc);
     gsg->apply_texture(tc);
   }
   }
+  cg_report_errors();
 #endif
 #endif
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: GLShaderContext::cg_report_errors
+//       Access: Public
+//  Description: Report any Cg errors that were not previously caught.
+////////////////////////////////////////////////////////////////////
+#ifdef HAVE_CG
+void CLP(ShaderContext)::
+cg_report_errors() {
+  CGerror err = cgGetError();
+  if (err != CG_NO_ERROR) {
+    GLCAT.error() << _expansion->get_name() << " " << cgGetErrorString(err) << "\n";
+  }
+}
+#endif

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

@@ -55,6 +55,7 @@ private:
   CGprogram _cg_vprogram;
   CGprogram _cg_vprogram;
   CGprogram _cg_fprogram;
   CGprogram _cg_fprogram;
   pvector <CGparameter> _cg_parameter_map;
   pvector <CGparameter> _cg_parameter_map;
+  void cg_report_errors();
 #endif
 #endif
 
 
   void release_resources(void);
   void release_resources(void);

+ 23 - 0
panda/src/gobj/shaderExpansion.cxx

@@ -770,6 +770,20 @@ cg_release_resources() {
     _cg_context = 0;
     _cg_context = 0;
   }
   }
 }
 }
+
+////////////////////////////////////////////////////////////////////
+//     Function: ShaderExpansion::cg_report_errors
+//       Access: Private
+//  Description: xyz
+////////////////////////////////////////////////////////////////////
+void ShaderExpansion::
+cg_report_errors() {
+  CGerror err = cgGetError();
+  if (err != CG_NO_ERROR) {
+    gobj_cat.error() << _name << " " << cgGetErrorString(err) << "\n";
+  }
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: ShaderExpansion::cg_compile_entry_point
 //     Function: ShaderExpansion::cg_compile_entry_point
 //       Access: Private
 //       Access: Private
@@ -785,6 +799,8 @@ cg_compile_entry_point(char *entry, const ShaderCaps &caps, bool fshader)
   int active = fshader ? caps._active_fprofile : caps._active_vprofile;
   int active = fshader ? caps._active_fprofile : caps._active_vprofile;
   int ultimate = fshader ? caps._ultimate_fprofile : caps._ultimate_vprofile;
   int ultimate = fshader ? caps._ultimate_fprofile : caps._ultimate_vprofile;
 
 
+  cg_report_errors();
+
   if (fshader && caps._bug_list.count(SBUG_ati_draw_buffers)) {
   if (fshader && caps._bug_list.count(SBUG_ati_draw_buffers)) {
     compiler_args[nargs++] = "-po";
     compiler_args[nargs++] = "-po";
     compiler_args[nargs++] = "ATI_draw_buffers";
     compiler_args[nargs++] = "ATI_draw_buffers";
@@ -824,6 +840,7 @@ cg_compile_entry_point(char *entry, const ShaderCaps &caps, bool fshader)
   if (prog != 0) {
   if (prog != 0) {
     cgDestroyProgram(prog);
     cgDestroyProgram(prog);
   }
   }
+
   return 0;
   return 0;
 }
 }
 
 
@@ -1110,11 +1127,17 @@ cg_compile_for(const ShaderCaps &caps,
     const ShaderArgId &id = _tex_spec[i]._id;
     const ShaderArgId &id = _tex_spec[i]._id;
     CGprogram prog = (id._fshader) ? _cg_fprogram : _cg_vprogram;
     CGprogram prog = (id._fshader) ? _cg_fprogram : _cg_vprogram;
     map[id._seqno] = cgGetNamedParameter(prog, id._name.c_str());
     map[id._seqno] = cgGetNamedParameter(prog, id._name.c_str());
+    if (cgGetParameterBaseResource(map[id._seqno]) == CG_UNDEFINED) {
+      map[id._seqno] = 0;
+    }
   }
   }
   for (int i=0; i<n_var; i++) {
   for (int i=0; i<n_var; i++) {
     const ShaderArgId &id = _var_spec[i]._id;
     const ShaderArgId &id = _var_spec[i]._id;
     CGprogram prog = (id._fshader) ? _cg_fprogram : _cg_vprogram;
     CGprogram prog = (id._fshader) ? _cg_fprogram : _cg_vprogram;
     map[id._seqno] = cgGetNamedParameter(prog, id._name.c_str());
     map[id._seqno] = cgGetNamedParameter(prog, id._name.c_str());
+    if (cgGetParameterBaseResource(map[id._seqno]) == CG_UNDEFINED) {
+      map[id._seqno] = 0;
+    }
   }
   }
   
   
   // Transfer ownership of the compiled shader.
   // Transfer ownership of the compiled shader.

+ 1 - 0
panda/src/gobj/shaderExpansion.h

@@ -239,6 +239,7 @@ public:
   bool          cg_analyze_shader(const ShaderCaps &caps);
   bool          cg_analyze_shader(const ShaderCaps &caps);
   bool          cg_compile_shader(const ShaderCaps &caps);
   bool          cg_compile_shader(const ShaderCaps &caps);
   void          cg_release_resources();
   void          cg_release_resources();
+  void          cg_report_errors();
   
   
   ShaderCaps _cg_last_caps;
   ShaderCaps _cg_last_caps;
   CGcontext  _cg_context;
   CGcontext  _cg_context;