From 10f4c6bb21e089fc724f813fcf843d15fbeba2f3 Mon Sep 17 00:00:00 2001 From: rdb Date: Fri, 13 Jan 2023 17:01:20 +0100 Subject: [PATCH] shader: Add texconst_i shader input for Cg shaders to access TexGen constant --- panda/src/display/graphicsStateGuardian.cxx | 12 ++++++++++ panda/src/glstuff/glCgShaderContext_src.cxx | 4 ++++ panda/src/glstuff/glShaderContext_src.cxx | 4 ++++ panda/src/gobj/shader.cxx | 26 +++++++++++++++++++++ panda/src/gobj/shader.h | 4 ++++ 5 files changed, 50 insertions(+) diff --git a/panda/src/display/graphicsStateGuardian.cxx b/panda/src/display/graphicsStateGuardian.cxx index b1f0961658..b06e520fd3 100644 --- a/panda/src/display/graphicsStateGuardian.cxx +++ b/panda/src/display/graphicsStateGuardian.cxx @@ -1175,6 +1175,18 @@ fetch_specified_part(Shader::ShaderMatInput part, InternalName *name, return &LMatrix4::zeros_mat(); } } + case Shader::SMO_texconst_i: { + const TexGenAttrib *tga; + const TextureAttrib *ta; + if (_target_rs->get_attrib(ta) && _target_rs->get_attrib(tga) && + index < ta->get_num_on_stages()) { + LVecBase3 value = tga->get_constant_value(ta->get_on_stage(index)); + t.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, value[0], value[1], value[2], 1); + return &t; + } else { + return &LMatrix4::ident_mat(); + } + } case Shader::SMO_tex_is_alpha_i: { // This is a hack so we can support both F_alpha and other formats in the // default shader, to fix font rendering in GLES2 diff --git a/panda/src/glstuff/glCgShaderContext_src.cxx b/panda/src/glstuff/glCgShaderContext_src.cxx index 4a71b074c6..5d2886cf39 100644 --- a/panda/src/glstuff/glCgShaderContext_src.cxx +++ b/panda/src/glstuff/glCgShaderContext_src.cxx @@ -483,6 +483,10 @@ set_state_and_transform(const RenderState *target_rs, target_rs->get_attrib(TexMatrixAttrib::get_class_slot())) { altered |= Shader::SSD_tex_matrix; } + if (state_rs->get_attrib(TexGenAttrib::get_class_slot()) != + target_rs->get_attrib(TexGenAttrib::get_class_slot())) { + altered |= Shader::SSD_tex_gen; + } _state_rs = target_rs; } diff --git a/panda/src/glstuff/glShaderContext_src.cxx b/panda/src/glstuff/glShaderContext_src.cxx index 79db239f35..44a22b8c71 100644 --- a/panda/src/glstuff/glShaderContext_src.cxx +++ b/panda/src/glstuff/glShaderContext_src.cxx @@ -2131,6 +2131,10 @@ set_state_and_transform(const RenderState *target_rs, target_rs->get_attrib(TextureAttrib::get_class_slot())) { altered |= Shader::SSD_texture; } + if (state_rs->get_attrib(TexGenAttrib::get_class_slot()) != + target_rs->get_attrib(TexGenAttrib::get_class_slot())) { + altered |= Shader::SSD_tex_gen; + } _state_rs = target_rs; } diff --git a/panda/src/gobj/shader.cxx b/panda/src/gobj/shader.cxx index e01c123c66..95235ba5a8 100644 --- a/panda/src/gobj/shader.cxx +++ b/panda/src/gobj/shader.cxx @@ -494,6 +494,9 @@ cp_dependency(ShaderMatInput inp) { if (inp == SMO_tex_is_alpha_i || inp == SMO_texcolor_i) { dep |= SSD_texture | SSD_frame; } + if (inp == SMO_texconst_i) { + dep |= SSD_tex_gen; + } return dep; } @@ -1218,6 +1221,29 @@ compile_parameter(ShaderArgInfo &p, int *arg_dim) { return true; } + if (pieces[0] == "texconst") { + if ((!cp_errchk_parameter_words(p,2))|| + (!cp_errchk_parameter_in(p)) || + (!cp_errchk_parameter_uniform(p))|| + (!cp_errchk_parameter_float(p,3,4))) { + return false; + } + ShaderMatSpec bind; + bind._id = p._id; + bind._piece = SMP_row3; + bind._func = SMF_first; + bind._part[0] = SMO_texconst_i; + bind._arg[0] = nullptr; + bind._part[1] = SMO_identity; + bind._arg[1] = nullptr; + bind._index = atoi(pieces[1].c_str()); + + cp_optimize_mat_spec(bind); + _mat_spec.push_back(bind); + _mat_deps |= bind._dep[0] | bind._dep[1]; + return true; + } + if (pieces[0] == "plane") { if ((!cp_errchk_parameter_words(p,2))|| (!cp_errchk_parameter_in(p)) || diff --git a/panda/src/gobj/shader.h b/panda/src/gobj/shader.h index d2035a26ea..3dc9777b26 100644 --- a/panda/src/gobj/shader.h +++ b/panda/src/gobj/shader.h @@ -214,6 +214,9 @@ public: // Color of an M_blend texture stage. SMO_texcolor_i, + // Constant value of the TexGenAttrib of stage i. + SMO_texconst_i, + SMO_INVALID }; @@ -321,6 +324,7 @@ public: SSD_projection = 0x800, SSD_texture = 0x1000, SSD_view_transform= 0x2000, + SSD_tex_gen = 0x4000, }; enum ShaderBug {