diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index 63ee8dcf4d..2677e3a82c 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -68,6 +68,10 @@ #include "shader.h" #include "shaderMode.h" +#ifdef CGGL +#include "Cg/CgGL.h" +#endif + #include TypeHandle CLP(GraphicsStateGuardian)::_type_handle; @@ -279,8 +283,6 @@ CLP(GraphicsStateGuardian)(const FrameBufferProperties &properties) : GraphicsStateGuardian(properties, CS_yup_right) { _error_count = 0; - _shader_mode = (ShaderMode *)NULL; - _shader_context = (CLP(ShaderContext) *)NULL; } //////////////////////////////////////////////////////////////////// @@ -857,6 +859,16 @@ reset() { _error_count = 0; +#ifdef CGGL + _cg_context = cgCreateContext(); + if (_cg_context != 0) { + _cg_vprofile = cgGLGetLatestProfile(CG_GL_VERTEX); + _cg_fprofile = cgGLGetLatestProfile(CG_GL_FRAGMENT); + } else { + cerr << "Warning: could not create Cg context.\n"; + } +#endif + report_my_gl_errors(); } @@ -1932,7 +1944,7 @@ release_geom(GeomContext *gc) { //////////////////////////////////////////////////////////////////// ShaderContext *CLP(GraphicsStateGuardian):: prepare_shader(Shader *shader) { - return new CLP(ShaderContext)(shader); + return new CLP(ShaderContext)(this, shader); } //////////////////////////////////////////////////////////////////// @@ -4750,6 +4762,12 @@ finish_modify_state() { //////////////////////////////////////////////////////////////////// void CLP(GraphicsStateGuardian):: free_pointers() { +#ifdef CGGL + if (_cg_context) { + cgDeleteContext(_cg_context); + _cg_context = 0; + } +#endif } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.h b/panda/src/glstuff/glGraphicsStateGuardian_src.h index 2d5a17a0ac..68ecd61b76 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.h +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.h @@ -41,6 +41,10 @@ #include "shader.h" #include "shaderMode.h" +#ifdef CGGL +#include "Cg/CgGL.h" +#endif + class PlaneNode; class Light; @@ -385,6 +389,12 @@ public: typedef pvector DeletedDisplayLists; DeletedDisplayLists _deleted_display_lists; +#ifdef CGGL + CGcontext _cg_context; + CGprofile _cg_vprofile; + CGprofile _cg_fprofile; +#endif + static PStatCollector _load_display_list_pcollector; static PStatCollector _primitive_batches_display_list_pcollector; static PStatCollector _vertices_display_list_pcollector; diff --git a/panda/src/glstuff/glShaderContext_src.I b/panda/src/glstuff/glShaderContext_src.I index 247c91e58b..823f1e0cdd 100755 --- a/panda/src/glstuff/glShaderContext_src.I +++ b/panda/src/glstuff/glShaderContext_src.I @@ -42,3 +42,4 @@ unbind() { INLINE void CLP(ShaderContext):: rebind(ShaderMode *oldmode, ShaderMode *newmode) { } + diff --git a/panda/src/glstuff/glShaderContext_src.cxx b/panda/src/glstuff/glShaderContext_src.cxx index 41c3bc773b..03d80fc45b 100755 --- a/panda/src/glstuff/glShaderContext_src.cxx +++ b/panda/src/glstuff/glShaderContext_src.cxx @@ -24,12 +24,44 @@ TypeHandle CLP(ShaderContext)::_type_handle; // Description: xyz //////////////////////////////////////////////////////////////////// CLP(ShaderContext):: -CLP(ShaderContext)(Shader *s) : ShaderContext(s) { - string header, body; +CLP(ShaderContext)(CLP(GraphicsStateGuardian) *gsg, Shader *s) : ShaderContext(s) { + string header; + _gsg = gsg; + _valid = false; s->parse_init(); s->parse_line(header, true, true); - s->parse_rest(body); - cerr << "Compiling shader. Language=" << header << " Body=\n" << body << "\n"; + +#ifdef CGGL + _cg_vprogram = 0; + _cg_fprogram = 0; + + if (header == "Cg") { + if (_gsg->_cg_context == 0) { + cerr << "Cannot compile shader, no Cg context\n"; + return; + } + string commentary, vs, fs; + s->parse_upto(commentary, "---*---", false); + s->parse_upto(vs, "---*---", false); + s->parse_upto(fs, "---*---", false); + + _cg_vprogram = cgCreateProgram(_gsg->_cg_context, CG_SOURCE, + vp.c_str(), _gsg->_cg_vprofile, + "main", (char**)NULL); + _cg_fprogram = cgCreateProgram(_gsg->_cg_context, CG_SOURCE, + fp.c_str(), _gsg->_cg_fprofile, + "main", (char**)NULL); + + if ((_cg_vprogram==0)||(_cg_fprogram == 0)) { + if (_cg_vprogram != 0) cgDestroyProgram(_cg_vprogram); + if (_cg_fprogram != 0) cgDestroyProgram(_cg_fprogram); + cerr << "Invalid Cg program" << s->_file << "\n"; + return; + } + } +#endif + + cerr << "Unrecognized shader language " << header << "\n"; } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/glstuff/glShaderContext_src.h b/panda/src/glstuff/glShaderContext_src.h index eefa33d54b..4765021d7f 100755 --- a/panda/src/glstuff/glShaderContext_src.h +++ b/panda/src/glstuff/glShaderContext_src.h @@ -18,6 +18,11 @@ #include "pandabase.h" #include "shaderContext.h" +#ifdef CGGL +#include "Cg/CgGL.h" +#endif + +class CLP(GraphicsStateGuardian); //////////////////////////////////////////////////////////////////// // Class : GLShaderContext @@ -26,13 +31,23 @@ class EXPCL_GL CLP(ShaderContext): public ShaderContext { public: - CLP(ShaderContext)(Shader *shader); + CLP(ShaderContext)(CLP(GraphicsStateGuardian) *gsg, Shader *shader); ~CLP(ShaderContext)(); INLINE void bind(ShaderMode *mode); INLINE void unbind(); INLINE void rebind(ShaderMode *oldmode, ShaderMode *newmode); + bool _valid; + +private: + CLP(GraphicsStateGuardian) *_gsg; + +#ifdef CGGL + CGprogram _cg_vprogram; + CGprogram _cg_fprogram; +#endif + public: static TypeHandle get_class_type() { return _type_handle; diff --git a/panda/src/gobj/shader.cxx b/panda/src/gobj/shader.cxx index 481f937489..cafee49bf4 100755 --- a/panda/src/gobj/shader.cxx +++ b/panda/src/gobj/shader.cxx @@ -88,12 +88,13 @@ parse_line(string &result, bool lt, bool rt) { //////////////////////////////////////////////////////////////////// void Shader:: parse_upto(string &result, string pattern, bool include) { + GlobPattern endpat(pattern); int start = _parse; int last = _parse; - while (true) { + while (_parse < _text.size()) { string t; parse_line(t, true, true); - if (t == pattern) break; + if (endpat.matches(t)) break; last = _parse; } if (include) { @@ -276,4 +277,3 @@ void Shader:: register_with_read_factory() { // IMPLEMENT ME } -