From 47431fab6676ce5c4cd1ba994da73b1edac2d3e4 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 19 May 2020 15:16:35 +0300 Subject: [PATCH] Automatically add uniforms for texunits The uniform statement now uses existing ProgramData if it exists, to avoid inadvertently erasing the texunit uniforms. --- source/renderpass.cpp | 16 ++++++++++++++-- source/renderpass.h | 4 ++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/source/renderpass.cpp b/source/renderpass.cpp index be5bb170..4e3b1f13 100644 --- a/source/renderpass.cpp +++ b/source/renderpass.cpp @@ -52,6 +52,17 @@ RenderPass::~RenderPass() delete texturing; } +void RenderPass::ensure_private_shader_data() +{ + if(!shprog) + throw invalid_operation("RenderPass::ensure_private_shader_data"); + + if(!shdata) + shdata = new ProgramData(shprog); + else if(shdata.refcount() > 1) + shdata = new ProgramData(*shdata); +} + void RenderPass::set_shader_program(const Program *prog, const ProgramData *data) { shprog = prog; @@ -153,12 +164,13 @@ void RenderPass::Loader::texunit_named(unsigned i, const string &n) { texunit(i); obj.tex_names[n] = i; + obj.ensure_private_shader_data(); + obj.shdata->uniform(n, static_cast(i)); } void RenderPass::Loader::uniforms() { - if(!obj.shprog) - throw invalid_operation("RenderPass::Loader::uniforms"); + obj.ensure_private_shader_data(); RefPtr shd = new ProgramData(obj.shprog); load_sub(*shd); obj.shdata = shd.release(); diff --git a/source/renderpass.h b/source/renderpass.h index 76c74720..66768772 100644 --- a/source/renderpass.h +++ b/source/renderpass.h @@ -71,6 +71,10 @@ public: RenderPass &operator=(const RenderPass &); ~RenderPass(); +private: + void ensure_private_shader_data(); + +public: void set_shader_program(const Program *, const ProgramData *); const Program *get_shader_program() const { return shprog; } const ProgramData *get_shader_data() const { return shdata.get(); } -- 2.45.2