From: Mikko Rasa Date: Thu, 19 Dec 2013 21:10:15 +0000 (+0200) Subject: Automatically determine texture units for effects X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=db2fd517dcd215e9d16345287d413f2a64f5ba03;p=libs%2Fgl.git Automatically determine texture units for effects At the moment this breaks such effects in fixed-function mode on platforms that support shader-only texture units. Will devise a fix later. --- diff --git a/source/environmentmap.cpp b/source/environmentmap.cpp index be0bf410..c789fe48 100644 --- a/source/environmentmap.cpp +++ b/source/environmentmap.cpp @@ -28,7 +28,6 @@ EnvironmentMap::EnvironmentMap(unsigned s, Renderable &r, Renderable &e): camera.set_aspect(1); camera.set_depth_clip(0.1, 100); - shdata.uniform("environment", 4); } void EnvironmentMap::setup_frame() const @@ -72,6 +71,12 @@ void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const if(!enabled_passes.count(tag)) return renderer.render(renderable, tag); + Renderer::Push _push_rend(renderer); + + unsigned unit = renderer.allocate_effect_texunit(); + shdata.uniform("environment", static_cast(unit)); + env_tex.bind_to(unit); + const Matrix &view_matrix = renderer.get_camera()->get_matrix(); // XXX The camera should maybe have store its own object matrix float env_mdata[9]; @@ -86,13 +91,10 @@ void EnvironmentMap::render(Renderer &renderer, const Tag &tag) const env_mdata[8] = view_matrix[10]; shdata.uniform_matrix3("env_eye_matrix", env_mdata); - env_tex.bind_to(4); - - Renderer::Push _push_rend(renderer); renderer.add_shader_data(shdata); renderer.render(renderable, tag); - env_tex.unbind_from(4); + env_tex.unbind_from(unit); } } // namespace GL diff --git a/source/renderer.cpp b/source/renderer.cpp index b6ece6de..af0d0873 100644 --- a/source/renderer.cpp +++ b/source/renderer.cpp @@ -11,6 +11,7 @@ #include "renderer.h" #include "texture.h" #include "texturing.h" +#include "texunit.h" #include "vertexarray.h" #include "windingtest.h" @@ -74,6 +75,11 @@ void Renderer::set_texturing(const Texturing *t) state->texture = 0; } +unsigned Renderer::allocate_effect_texunit() +{ + return --state->lowest_effect_texunit; +} + void Renderer::set_material(const Material *m) { state->material = m; @@ -291,6 +297,7 @@ void Renderer::apply_state() Renderer::State::State(): texture(0), texturing(0), + lowest_effect_texunit(TexUnit::get_n_units()), material(0), lighting(0), shprog(0), diff --git a/source/renderer.h b/source/renderer.h index f736447e..1be1a454 100644 --- a/source/renderer.h +++ b/source/renderer.h @@ -65,6 +65,7 @@ private: { const Texture *texture; const Texturing *texturing; + unsigned lowest_effect_texunit; const Material *material; const Lighting *lighting; Matrix lighting_matrix; @@ -110,6 +111,7 @@ public: void set_texture(const Texture *); void set_texturing(const Texturing *); + unsigned allocate_effect_texunit(); void set_material(const Material *); void set_lighting(const Lighting *); diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index bd87d262..8151a7cf 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -33,7 +33,6 @@ ShadowMap::ShadowMap(unsigned s, const Renderable &r, const Light &l): fbo.require_complete(); set_darkness(0.7); - set_texture_unit(3); } void ShadowMap::set_target(const Vector3 &t, float r) @@ -58,15 +57,6 @@ void ShadowMap::set_depth_bias(float b) depth_bias = b; } -void ShadowMap::set_texture_unit(unsigned u) -{ - unit = u; - - int i = unit; - shdata.uniform("shadow", i); - shdata.uniform("shadow_unit", i); -} - void ShadowMap::setup_frame() const { if(rendered) @@ -131,6 +121,13 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const if(!enabled_passes.count(tag)) return renderer.render(renderable, tag); + Renderer::Push _push_rend(renderer); + + unsigned unit = renderer.allocate_effect_texunit(); + int iunit = unit; + shdata.uniform("shadow", iunit); + shdata.uniform("shadow_unit", iunit); + depth_buf.bind_to(unit); TexGen tg_s, tg_t, tg_r; tg_s.set_plane(Vector4(shadow_matrix(0, 0), shadow_matrix(0, 1), shadow_matrix(0, 2), shadow_matrix(0, 3))); @@ -147,7 +144,6 @@ void ShadowMap::render(Renderer &renderer, const Tag &tag) const else shdata.uniform("shd_eye_matrix", shadow_matrix); - Renderer::Push _push_rend(renderer); renderer.add_shader_data(shdata); renderer.render(renderable, tag); diff --git a/source/shadowmap.h b/source/shadowmap.h index 4903afeb..57adf298 100644 --- a/source/shadowmap.h +++ b/source/shadowmap.h @@ -28,7 +28,6 @@ private: mutable Matrix light_matrix; mutable Matrix view_matrix; mutable Matrix shadow_matrix; - unsigned unit; Texture2D depth_buf; Vector3 target; float radius; @@ -55,11 +54,6 @@ public: thin objects from casting shadows on nearby sufraces. */ void set_depth_bias(float); - /** Sets the texture unit to bind the shadow map to during the rendering - phase. Provided to shaders through the shadow and shadow_unit uniforms. - The default is texture unit 3. */ - void set_texture_unit(unsigned); - virtual void setup_frame() const; virtual void finish_frame() const;