From 64435e56b1ad783d5dbedd252dfb97667cb9f423 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 17 May 2020 10:34:11 -0700 Subject: [PATCH] Draw: Use uniform locs for GLES, add samplers. So that we can initialize and bind samplers. --- ext/native/thin3d/GLQueueRunner.cpp | 6 +++--- ext/native/thin3d/GLQueueRunner.h | 4 ++-- ext/native/thin3d/GLRenderManager.h | 10 +++++----- ext/native/thin3d/thin3d_gl.cpp | 23 ++++++++++++++++++----- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/ext/native/thin3d/GLQueueRunner.cpp b/ext/native/thin3d/GLQueueRunner.cpp index 7be3fd09c2..c629c73d91 100644 --- a/ext/native/thin3d/GLQueueRunner.cpp +++ b/ext/native/thin3d/GLQueueRunner.cpp @@ -929,9 +929,9 @@ void GLQueueRunner::PerformRenderPass(const GLRStep &step) { case GLRRenderCommand::UNIFORMMATRIX: { assert(curProgram); - int loc = c.uniform4.loc ? *c.uniform4.loc : -1; - if (c.uniform4.name) { - loc = curProgram->GetUniformLoc(c.uniform4.name); + int loc = c.uniformMatrix4.loc ? *c.uniformMatrix4.loc : -1; + if (c.uniformMatrix4.name) { + loc = curProgram->GetUniformLoc(c.uniformMatrix4.name); } if (loc >= 0) { glUniformMatrix4fv(loc, 1, false, c.uniformMatrix4.m); diff --git a/ext/native/thin3d/GLQueueRunner.h b/ext/native/thin3d/GLQueueRunner.h index 06ba82af31..37838f8383 100644 --- a/ext/native/thin3d/GLQueueRunner.h +++ b/ext/native/thin3d/GLQueueRunner.h @@ -117,13 +117,13 @@ struct GLRRenderData { } drawIndexed; struct { const char *name; // if null, use loc - GLint *loc; // NOTE: This is a pointer so we can immediately use things that are "queried" during program creation. + const GLint *loc; // NOTE: This is a pointer so we can immediately use things that are "queried" during program creation. GLint count; float v[4]; } uniform4; struct { const char *name; // if null, use loc - GLint *loc; + const GLint *loc; float m[16]; } uniformMatrix4; struct { diff --git a/ext/native/thin3d/GLRenderManager.h b/ext/native/thin3d/GLRenderManager.h index eac80eac6f..40cbcb0d27 100644 --- a/ext/native/thin3d/GLRenderManager.h +++ b/ext/native/thin3d/GLRenderManager.h @@ -647,7 +647,7 @@ public: curRenderStep_->commands.push_back(data); } - void SetUniformI(GLint *loc, int count, const int *udata) { + void SetUniformI(const GLint *loc, int count, const int *udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); #ifdef _DEBUG assert(curProgram_); @@ -659,7 +659,7 @@ public: curRenderStep_->commands.push_back(data); } - void SetUniformI1(GLint *loc, int udata) { + void SetUniformI1(const GLint *loc, int udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); #ifdef _DEBUG assert(curProgram_); @@ -671,7 +671,7 @@ public: curRenderStep_->commands.push_back(data); } - void SetUniformF(GLint *loc, int count, const float *udata) { + void SetUniformF(const GLint *loc, int count, const float *udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); #ifdef _DEBUG assert(curProgram_); @@ -683,7 +683,7 @@ public: curRenderStep_->commands.push_back(data); } - void SetUniformF1(GLint *loc, const float udata) { + void SetUniformF1(const GLint *loc, const float udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); #ifdef _DEBUG assert(curProgram_); @@ -707,7 +707,7 @@ public: curRenderStep_->commands.push_back(data); } - void SetUniformM4x4(GLint *loc, const float *udata) { + void SetUniformM4x4(const GLint *loc, const float *udata) { _dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == GLRStepType::RENDER); #ifdef _DEBUG assert(curProgram_); diff --git a/ext/native/thin3d/thin3d_gl.cpp b/ext/native/thin3d/thin3d_gl.cpp index dd3aa97fb3..f35f5daece 100644 --- a/ext/native/thin3d/thin3d_gl.cpp +++ b/ext/native/thin3d/thin3d_gl.cpp @@ -323,6 +323,8 @@ public: // TODO: Optimize by getting the locations first and putting in a custom struct UniformBufferDesc dynamicUniforms; + GLint samplerLocs_[8]; + std::vector dynamicUniformLocs_; GLRProgram *program_ = nullptr; private: @@ -964,6 +966,10 @@ Pipeline *OpenGLContext::CreateGraphicsPipeline(const PipelineDesc &desc) { return nullptr; } } + if (desc.uniformDesc) { + pipeline->dynamicUniforms = *desc.uniformDesc; + pipeline->dynamicUniformLocs_.resize(desc.uniformDesc->uniforms.size()); + } ILOG("Linking shaders."); if (pipeline->LinkShaders()) { // Build the rest of the virtual pipeline object. @@ -976,8 +982,6 @@ Pipeline *OpenGLContext::CreateGraphicsPipeline(const PipelineDesc &desc) { pipeline->blend->AddRef(); pipeline->raster->AddRef(); pipeline->inputLayout->AddRef(); - if (desc.uniformDesc) - pipeline->dynamicUniforms = *desc.uniformDesc; return pipeline; } else { ELOG("Failed to create pipeline - shaders failed to link"); @@ -1050,7 +1054,14 @@ bool OpenGLPipeline::LinkShaders() { semantics.push_back({ SEM_POSITION, "a_position" }); semantics.push_back({ SEM_TEXCOORD0, "a_texcoord0" }); std::vector queries; + queries.push_back({ &samplerLocs_[0], "sampler0" }); + queries.push_back({ &samplerLocs_[1], "sampler1" }); + for (size_t i = 0; i < dynamicUniforms.uniforms.size(); ++i) { + queries.push_back({ &dynamicUniformLocs_[i], dynamicUniforms.uniforms[i].name }); + } std::vector initialize; + initialize.push_back({ &samplerLocs_[0], 0, 0 }); + initialize.push_back({ &samplerLocs_[1], 0, 1 }); program_ = render_->CreateProgram(linkShaders, semantics, queries, initialize, false); return true; } @@ -1070,17 +1081,19 @@ void OpenGLContext::UpdateDynamicUniformBuffer(const void *ub, size_t size) { Crash(); } - for (auto &uniform : curPipeline_->dynamicUniforms.uniforms) { + for (size_t i = 0; i < curPipeline_->dynamicUniforms.uniforms.size(); ++i) { + const auto &uniform = curPipeline_->dynamicUniforms.uniforms[i]; + const GLint &loc = curPipeline_->dynamicUniformLocs_[i]; const float *data = (const float *)((uint8_t *)ub + uniform.offset); switch (uniform.type) { case UniformType::FLOAT1: case UniformType::FLOAT2: case UniformType::FLOAT3: case UniformType::FLOAT4: - renderManager_.SetUniformF(uniform.name, 1 + (int)uniform.type - (int)UniformType::FLOAT1, data); + renderManager_.SetUniformF(&loc, 1 + (int)uniform.type - (int)UniformType::FLOAT1, data); break; case UniformType::MATRIX4X4: - renderManager_.SetUniformM4x4(uniform.name, data); + renderManager_.SetUniformM4x4(&loc, data); break; } }