From bcf0229978462c590534bd1e29f710ea82569653 Mon Sep 17 00:00:00 2001 From: rdb Date: Mon, 9 Feb 2015 15:57:47 +0100 Subject: [PATCH] Expose p3d_ClipPlane array to GLSL --- panda/src/display/graphicsStateGuardian.cxx | 28 ++++++++++++++++--- panda/src/display/graphicsStateGuardian.h | 3 +- .../glstuff/glGraphicsStateGuardian_src.cxx | 5 ++++ panda/src/glstuff/glShaderContext_src.cxx | 19 +++++++++++++ panda/src/gobj/shader.cxx | 5 +++- panda/src/gobj/shader.h | 4 +++ 6 files changed, 58 insertions(+), 6 deletions(-) diff --git a/panda/src/display/graphicsStateGuardian.cxx b/panda/src/display/graphicsStateGuardian.cxx index 01030d1294..6dda4cb69a 100644 --- a/panda/src/display/graphicsStateGuardian.cxx +++ b/panda/src/display/graphicsStateGuardian.cxx @@ -936,13 +936,13 @@ fetch_specified_value(Shader::ShaderMatSpec &spec, int altered) { LVecBase3 v; if (altered & spec._dep[0]) { - const LMatrix4 *t = fetch_specified_part(spec._part[0], spec._arg[0], spec._cache[0]); + const LMatrix4 *t = fetch_specified_part(spec._part[0], spec._arg[0], spec._cache[0], spec._index); if (t != &spec._cache[0]) { spec._cache[0] = *t; } } if (altered & spec._dep[1]) { - const LMatrix4 *t = fetch_specified_part(spec._part[1], spec._arg[1], spec._cache[1]); + const LMatrix4 *t = fetch_specified_part(spec._part[1], spec._arg[1], spec._cache[1], spec._index); if (t != &spec._cache[1]) { spec._cache[1] = *t; } @@ -987,8 +987,9 @@ fetch_specified_value(Shader::ShaderMatSpec &spec, int altered) { // Description: See fetch_specified_value //////////////////////////////////////////////////////////////////// const LMatrix4 *GraphicsStateGuardian:: -fetch_specified_part(Shader::ShaderMatInput part, InternalName *name, LMatrix4 &t) { - switch(part) { +fetch_specified_part(Shader::ShaderMatInput part, InternalName *name, + LMatrix4 &t, int index) { + switch (part) { case Shader::SMO_identity: { return &LMatrix4::ident_mat(); } @@ -1223,6 +1224,25 @@ fetch_specified_part(Shader::ShaderMatInput part, InternalName *name, LMatrix4 & t = LMatrix4(0,0,0,0,0,0,0,0,0,0,0,0,p[0],p[1],p[2],p[3]); return &t; } + case Shader::SMO_apiview_clipplane_i: { + const ClipPlaneAttrib *cpa = DCAST(ClipPlaneAttrib, _target_rs->get_attrib_def(ClipPlaneAttrib::get_class_slot())); + if (index >= cpa->get_num_on_planes()) { + return &LMatrix4::zeros_mat(); + } + + const NodePath &plane = cpa->get_on_plane(index); + nassertr(!plane.is_empty(), &LMatrix4::zeros_mat()); + const PlaneNode *plane_node; + DCAST_INTO_R(plane_node, plane.node(), &LMatrix4::zeros_mat()); + + CPT(TransformState) transform = + get_scene()->get_cs_world_transform()->compose( + plane.get_transform(_scene_setup->get_scene_root().get_parent())); + + LPlane xformed_plane = plane_node->get_plane() * transform->get_mat(); + t.set_row(3, xformed_plane); + return &t; + } case Shader::SMO_mat_constant_x: { return &_target_shader->get_shader_input_matrix(name, t); } diff --git a/panda/src/display/graphicsStateGuardian.h b/panda/src/display/graphicsStateGuardian.h index 31282aa820..b546a61945 100644 --- a/panda/src/display/graphicsStateGuardian.h +++ b/panda/src/display/graphicsStateGuardian.h @@ -255,7 +255,8 @@ public: virtual void clear(DrawableRegion *clearable); const LMatrix4 *fetch_specified_value(Shader::ShaderMatSpec &spec, int altered); - const LMatrix4 *fetch_specified_part(Shader::ShaderMatInput input, InternalName *name, LMatrix4 &t); + const LMatrix4 *fetch_specified_part(Shader::ShaderMatInput input, InternalName *name, + LMatrix4 &t, int index); const Shader::ShaderPtrData *fetch_ptr_parameter(const Shader::ShaderPtrSpec& spec); virtual void prepare_display_region(DisplayRegionPipelineReader *dr); diff --git a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx index 36f1264f51..0d0c019b65 100644 --- a/panda/src/glstuff/glGraphicsStateGuardian_src.cxx +++ b/panda/src/glstuff/glGraphicsStateGuardian_src.cxx @@ -8811,6 +8811,11 @@ set_state_and_transform(const RenderState *target, //PStatGPUTimer timer(this, _draw_set_state_clip_plane_pcollector); do_issue_clip_plane(); _state_mask.set_bit(clip_plane_slot); +#ifndef OPENGLES_1 + if (_current_shader_context) { + _current_shader_context->issue_parameters(Shader::SSD_clip_planes); + } +#endif } int color_slot = ColorAttrib::get_class_slot(); diff --git a/panda/src/glstuff/glShaderContext_src.cxx b/panda/src/glstuff/glShaderContext_src.cxx index 399d583aab..e6a0d4551e 100755 --- a/panda/src/glstuff/glShaderContext_src.cxx +++ b/panda/src/glstuff/glShaderContext_src.cxx @@ -412,6 +412,25 @@ CLP(ShaderContext)(CLP(GraphicsStateGuardian) *glgsg, Shader *s) : ShaderContext s->_mat_spec.push_back(bind); continue; } + if (noprefix == "ClipPlane") { + for (int i = 0; i < param_size; ++i) { + Shader::ShaderMatSpec bind; + bind._id = arg_id; + bind._id._seqno = seqno++; + bind._piece = Shader::SMP_row3; + bind._func = Shader::SMF_first; + bind._index = i; + bind._part[0] = Shader::SMO_apiview_clipplane_i; + bind._arg[0] = NULL; + bind._dep[0] = Shader::SSD_general | Shader::SSD_clip_planes; + bind._part[1] = Shader::SMO_identity; + bind._arg[1] = NULL; + bind._dep[1] = Shader::SSD_NONE; + s->_mat_spec.push_back(bind); + _glsl_parameter_map.push_back(p + i); + } + continue; + } if (noprefix == "LightModel.ambient") { Shader::ShaderMatSpec bind; bind._id = arg_id; diff --git a/panda/src/gobj/shader.cxx b/panda/src/gobj/shader.cxx index c74c694bdd..2eb4a01fa9 100755 --- a/panda/src/gobj/shader.cxx +++ b/panda/src/gobj/shader.cxx @@ -424,7 +424,6 @@ cp_dependency(ShaderMatInput inp) { (inp == SMO_mat_constant_x) || (inp == SMO_vec_constant_x) || (inp == SMO_vec_constant_x_attrib) || - (inp == SMO_clipplane_x) || (inp == SMO_view_x_to_view) || (inp == SMO_view_to_view_x) || (inp == SMO_apiview_x_to_view) || @@ -444,6 +443,10 @@ cp_dependency(ShaderMatInput inp) { (inp == SMO_light_product_i_specular)) { dep |= (SSD_light | SSD_material); } + if ((inp == SMO_clipplane_x) || + (inp == SMO_apiview_clipplane_i)) { + dep |= SSD_clip_planes; + } return dep; } diff --git a/panda/src/gobj/shader.h b/panda/src/gobj/shader.h index 9ed5411c84..4b25009228 100755 --- a/panda/src/gobj/shader.h +++ b/panda/src/gobj/shader.h @@ -183,6 +183,9 @@ public: SMO_light_product_i_diffuse, SMO_light_product_i_specular, + // SMO_clipplane_x is world coords, GLSL needs eye coords + SMO_apiview_clipplane_i, + SMO_INVALID }; @@ -261,6 +264,7 @@ public: SSD_shaderinputs = 0x020, SSD_fog = 0x040, SSD_light = 0x080, + SSD_clip_planes = 0x100, }; enum ShaderBug {