Skip to content

Commit

Permalink
Draw: Use uniform locs for GLES, add samplers.
Browse files Browse the repository at this point in the history
So that we can initialize and bind samplers.
  • Loading branch information
unknownbrackets committed Jun 12, 2020
1 parent dcf4498 commit 64435e5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 15 deletions.
6 changes: 3 additions & 3 deletions ext/native/thin3d/GLQueueRunner.cpp
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions ext/native/thin3d/GLQueueRunner.h
Expand Up @@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions ext/native/thin3d/GLRenderManager.h
Expand Up @@ -647,7 +647,7 @@ class GLRenderManager {
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_);
Expand All @@ -659,7 +659,7 @@ class GLRenderManager {
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_);
Expand All @@ -671,7 +671,7 @@ class GLRenderManager {
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_);
Expand All @@ -683,7 +683,7 @@ class GLRenderManager {
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_);
Expand All @@ -707,7 +707,7 @@ class GLRenderManager {
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_);
Expand Down
23 changes: 18 additions & 5 deletions ext/native/thin3d/thin3d_gl.cpp
Expand Up @@ -323,6 +323,8 @@ class OpenGLPipeline : public Pipeline {

// TODO: Optimize by getting the locations first and putting in a custom struct
UniformBufferDesc dynamicUniforms;
GLint samplerLocs_[8];
std::vector<GLint> dynamicUniformLocs_;
GLRProgram *program_ = nullptr;

private:
Expand Down Expand Up @@ -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.
Expand All @@ -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");
Expand Down Expand Up @@ -1050,7 +1054,14 @@ bool OpenGLPipeline::LinkShaders() {
semantics.push_back({ SEM_POSITION, "a_position" });
semantics.push_back({ SEM_TEXCOORD0, "a_texcoord0" });
std::vector<GLRProgram::UniformLocQuery> 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<GLRProgram::Initializer> 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;
}
Expand All @@ -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;
}
}
Expand Down

0 comments on commit 64435e5

Please sign in to comment.