From cde115a055c8ba72a2645af003369e2470d3f39e Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Thu, 6 Apr 2006 16:44:38 +0000 Subject: [PATCH] Got hardware shadows in Cg working, for real this time --- panda/src/display/graphicsOutput.cxx | 4 +- panda/src/express/trueClock.cxx | 2 +- .../glstuff/glGraphicsStateGuardian_src.cxx | 7 +- panda/src/glstuff/glShaderContext_src.cxx | 71 +++++++++---------- panda/src/glstuff/glShaderContext_src.h | 2 +- 5 files changed, 40 insertions(+), 46 deletions(-) diff --git a/panda/src/display/graphicsOutput.cxx b/panda/src/display/graphicsOutput.cxx index a9d4120799..15a325ab3c 100644 --- a/panda/src/display/graphicsOutput.cxx +++ b/panda/src/display/graphicsOutput.cxx @@ -596,12 +596,12 @@ create_texture_card_vdata(int x, int y) vertex.add_data3f(Vertexf::rfu(-1.0f, 0.0f, -1.0f)); vertex.add_data3f(Vertexf::rfu( 1.0f, 0.0f, 1.0f)); vertex.add_data3f(Vertexf::rfu( 1.0f, 0.0f, -1.0f)); - + texcoord.add_data2f( 0.0f, yhi); texcoord.add_data2f( 0.0f, 0.0f); texcoord.add_data2f( xhi, yhi); texcoord.add_data2f( xhi, 0.0f); - + normal.add_data3f(LVector3f::back()); normal.add_data3f(LVector3f::back()); normal.add_data3f(LVector3f::back()); diff --git a/panda/src/express/trueClock.cxx b/panda/src/express/trueClock.cxx index 9882cf1658..3c24e2e9e3 100644 --- a/panda/src/express/trueClock.cxx +++ b/panda/src/express/trueClock.cxx @@ -237,7 +237,7 @@ correct_time(double time) { // A step backward in the high-precision clock, or more than a // small jump on only one of the clocks, is cause for alarm. - express_cat.warning() + express_cat.debug() << "Clock error detected; elapsed time " << time_delta << "s on high-resolution counter, and " << tod_delta << "s on time-of-day clock.\n"; diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index 07fa836357..9cf5ec904b 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -6247,12 +6247,13 @@ specify_texture(Texture *tex) { GLP(TexParameteri)(target, GL_TEXTURE_MAG_FILTER, get_texture_filter_type(magfilter, true)); - if (_supports_shadow_filter) { - if (tex->get_format() == Texture::F_depth_component) { + if (tex->get_format() == Texture::F_depth_component) { + GLP(TexParameteri)(target, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); + if (_supports_shadow_filter) { if ((tex->get_magfilter() == Texture::FT_shadow) || (tex->get_minfilter() == Texture::FT_shadow)) { GLP(TexParameteri)(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); - GLP(TexParameteri)(target, GL_TEXTURE_COMPARE_FUNC_ARB, GL_GEQUAL); + GLP(TexParameteri)(target, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); } else { GLP(TexParameteri)(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); GLP(TexParameteri)(target, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); diff --git a/panda/src/glstuff/glShaderContext_src.cxx b/panda/src/glstuff/glShaderContext_src.cxx index ec1f40f683..2239165601 100755 --- a/panda/src/glstuff/glShaderContext_src.cxx +++ b/panda/src/glstuff/glShaderContext_src.cxx @@ -79,6 +79,7 @@ CLP(ShaderContext)(ShaderExpansion *s, GSG *gsg) : ShaderContext(s) { _cg_profile[SHADER_type_frag] = CG_PROFILE_UNKNOWN; _cg_program[SHADER_type_vert] = (CGprogram)0; _cg_program[SHADER_type_frag] = (CGprogram)0; + _cg_program[2] = (CGprogram)0; if (header == "//Cg") { // Create the Cg context. @@ -248,15 +249,32 @@ try_cg_compile(ShaderExpansion *s, GSG *gsg) return false; } -#if DEBUG_GL_SHADER - // DEBUG: output the generated program - { - const char *program; - program = cgGetProgramString (_cg_program[1], CG_COMPILED_PROGRAM); - - GLCAT.debug() << program << "\n"; + bool success = true; + CGparameter parameter; + for (int progindex=0; progindex<2; progindex++) { + int nvtx = _var_spec.size(); + for (parameter = cgGetFirstLeafParameter(_cg_program[progindex],CG_PROGRAM); + parameter != 0; + parameter = cgGetNextLeafParameter(parameter)) { + CGenum vbl = cgGetParameterVariability(parameter); + if ((vbl==CG_VARYING)||(vbl==CG_UNIFORM)) { + success &= compile_parameter(parameter, + cgGetParameterName(parameter), + cg_type_to_panda_type(cgGetParameterType(parameter)), + cg_dir_to_panda_dir(cgGetParameterDirection(parameter)), + (vbl == CG_VARYING), + GLCAT.get_safe_ptr()); + } + } + if ((progindex == SHADER_type_frag) && (nvtx != _var_spec.size())) { + GLCAT.error() << "Cannot use vtx parameters in an fshader\n"; + success = false; + } + } + if (!success) { + release_resources(); + return false; } -#endif // The following code is present to work around a bug in the Cg compiler. // It does not generate correct code for shadow map lookups when using arbfp1. @@ -269,7 +287,8 @@ try_cg_compile(ShaderExpansion *s, GSG *gsg) bool anyshadow = false; memset(shadowunit, 0, sizeof(shadowunit)); vector_string lines; - tokenize(cgGetProgramString(_cg_program[1], CG_COMPILED_PROGRAM), lines, "\n"); + tokenize(cgGetProgramString(_cg_program[SHADER_type_frag], + CG_COMPILED_PROGRAM), lines, "\n"); // figure out which texture units contain shadow maps. for (int lineno=0; lineno<(int)lines.size(); lineno++) { if (lines[lineno].compare(0,21,"#var sampler2DSHADOW ")) { @@ -318,10 +337,10 @@ try_cg_compile(ShaderExpansion *s, GSG *gsg) for (int lineno=1; lineno<(int)lines.size(); lineno++) { result += (lines[lineno] + "\n"); } - cgDestroyProgram(_cg_program[1]); - _cg_program[1] = + _cg_program[2] = _cg_program[SHADER_type_frag]; + _cg_program[SHADER_type_frag] = cgCreateProgram(_cg_context, CG_OBJECT, result.c_str(), - _cg_profile[1], "fshader", (const char**)NULL); + _cg_profile[SHADER_type_frag], "fshader", (const char**)NULL); report_cg_compile_errors(s->get_name(), _cg_context); if (_cg_program[SHADER_type_frag]==0) { release_resources(); @@ -330,33 +349,6 @@ try_cg_compile(ShaderExpansion *s, GSG *gsg) } } - bool success = true; - CGparameter parameter; - for (int progindex=0; progindex<2; progindex++) { - int nvtx = _var_spec.size(); - for (parameter = cgGetFirstLeafParameter(_cg_program[progindex],CG_PROGRAM); - parameter != 0; - parameter = cgGetNextLeafParameter(parameter)) { - CGenum vbl = cgGetParameterVariability(parameter); - if ((vbl==CG_VARYING)||(vbl==CG_UNIFORM)) { - success &= compile_parameter(parameter, - cgGetParameterName(parameter), - cg_type_to_panda_type(cgGetParameterType(parameter)), - cg_dir_to_panda_dir(cgGetParameterDirection(parameter)), - (vbl == CG_VARYING), - GLCAT.get_safe_ptr()); - } - } - if ((progindex == SHADER_type_frag) && (nvtx != _var_spec.size())) { - GLCAT.error() << "Cannot use vtx parameters in an fshader\n"; - success = false; - } - } - if (!success) { - release_resources(); - return false; - } - cgGLLoadProgram(_cg_program[SHADER_type_vert]); cgGLLoadProgram(_cg_program[SHADER_type_frag]); @@ -392,6 +384,7 @@ release_resources() { _cg_profile[SHADER_type_frag] = (CGprofile)0; _cg_program[SHADER_type_vert] = (CGprogram)0; _cg_program[SHADER_type_frag] = (CGprogram)0; + _cg_program[2] = (CGprogram)0; } #endif } diff --git a/panda/src/glstuff/glShaderContext_src.h b/panda/src/glstuff/glShaderContext_src.h index 350c544dd5..806d2a3fc7 100755 --- a/panda/src/glstuff/glShaderContext_src.h +++ b/panda/src/glstuff/glShaderContext_src.h @@ -56,7 +56,7 @@ private: #ifdef HAVE_CGGL CGcontext _cg_context; CGprofile _cg_profile[2]; - CGprogram _cg_program[2]; + CGprogram _cg_program[3]; void report_cg_compile_errors(const string &file, CGcontext ctx); bool try_cg_compile(ShaderExpansion *s, GSG *gsg);