From 3ebd70f5dbaba0040a7b194780c6906bc7f32d96 Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Sat, 14 Nov 2020 17:17:40 +0700 Subject: [PATCH] refactor: Combine features and general local uniforms --- src/gui/control/control.cpp | 2 +- src/render/pipeline/world.cpp | 6 +++--- src/render/shaders.cpp | 27 +++++---------------------- src/render/shaders.h | 27 +++------------------------ src/scene/modelnodescenenode.cpp | 14 +++++++------- 5 files changed, 19 insertions(+), 57 deletions(-) diff --git a/src/gui/control/control.cpp b/src/gui/control/control.cpp index 7b7fe960..ee948456 100644 --- a/src/gui/control/control.cpp +++ b/src/gui/control/control.cpp @@ -249,7 +249,7 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g transform = glm::scale(transform, glm::vec3(w, h, 1.0f)); LocalUniforms locals; - locals.features.discardEnabled = _discardEnabled; + locals.general.discardEnabled = _discardEnabled; locals.general.model = move(transform); locals.general.discardColor = glm::vec4(_discardColor, 1.0f); diff --git a/src/render/pipeline/world.cpp b/src/render/pipeline/world.cpp index 262213ec..39282303 100644 --- a/src/render/pipeline/world.cpp +++ b/src/render/pipeline/world.cpp @@ -79,7 +79,7 @@ void WorldRenderPipeline::render() const { transform = glm::scale(transform, glm::vec3(w, h, 1.0f)); LocalUniforms locals; - locals.features.blurEnabled = true; + locals.general.blurEnabled = true; locals.general.model = move(transform); locals.general.blurResolution = glm::vec2(w, h); locals.general.blurDirection = glm::vec2(1.0f, 0.0f); @@ -110,7 +110,7 @@ void WorldRenderPipeline::render() const { transform = glm::scale(transform, glm::vec3(w, h, 1.0f)); LocalUniforms locals; - locals.features.blurEnabled = true; + locals.general.blurEnabled = true; locals.general.model = move(transform); locals.general.blurResolution = glm::vec2(_opts.width, _opts.height); locals.general.blurDirection = glm::vec2(0.0f, 1.0f); @@ -133,7 +133,7 @@ void WorldRenderPipeline::render() const { transform = glm::scale(transform, glm::vec3(w, h, 1.0f)); LocalUniforms locals; - locals.features.bloomEnabled = true; + locals.general.bloomEnabled = true; locals.general.model = move(transform); Shaders::instance().activate(ShaderProgram::GUIBloom, locals); diff --git a/src/render/shaders.cpp b/src/render/shaders.cpp index 8d84e3f1..d858c008 100644 --- a/src/render/shaders.cpp +++ b/src/render/shaders.cpp @@ -50,7 +50,7 @@ struct Light { float radius; }; -layout(std140) uniform Features { +layout(std140) uniform General { bool uLightmapEnabled; bool uEnvmapEnabled; bool uBumpyShinyEnabled; @@ -61,9 +61,7 @@ layout(std140) uniform Features { bool uBlurEnabled; bool uBloomEnabled; bool uDiscardEnabled; -}; -layout(std140) uniform General { uniform mat4 uModel; uniform vec4 uColor; uniform float uAlpha; @@ -336,7 +334,6 @@ void Shaders::initGL() { initProgram(ShaderProgram::ModelWhite, ShaderName::VertexModel, ShaderName::FragmentWhite); initProgram(ShaderProgram::ModelModel, ShaderName::VertexModel, ShaderName::FragmentModel); - glGenBuffers(1, &_featuresUbo); glGenBuffers(1, &_generalUbo); glGenBuffers(1, &_lightingUbo); glGenBuffers(1, &_skeletalUbo); @@ -345,13 +342,11 @@ void Shaders::initGL() { glUseProgram(program.second); _activeOrdinal = program.second; - uint32_t featuresBlockIdx = glGetUniformBlockIndex(_activeOrdinal, "Features"); - if (featuresBlockIdx != GL_INVALID_INDEX) { - glUniformBlockBinding(_activeOrdinal, featuresBlockIdx, kFeaturesBindingPointIndex); - } uint32_t generalBlockIdx = glGetUniformBlockIndex(_activeOrdinal, "General"); if (generalBlockIdx != GL_INVALID_INDEX) { glUniformBlockBinding(_activeOrdinal, generalBlockIdx, kGeneralBindingPointIndex); + //GLint blockSize; + //glGetActiveUniformBlockiv(_activeOrdinal, generalBlockIdx, GL_UNIFORM_BLOCK_DATA_SIZE, &blockSize); } uint32_t lightingBlockIdx = glGetUniformBlockIndex(_activeOrdinal, "Lighting"); if (lightingBlockIdx != GL_INVALID_INDEX) { @@ -431,10 +426,6 @@ void Shaders::deinitGL() { glDeleteBuffers(1, &_generalUbo); _generalUbo = 0; } - if (_featuresUbo) { - glDeleteBuffers(1, &_featuresUbo); - _featuresUbo = 0; - } for (auto &pair :_programs) { glDeleteProgram(pair.second); } @@ -466,22 +457,14 @@ unsigned int Shaders::getOrdinal(ShaderProgram program) const { } void Shaders::setLocalUniforms(const LocalUniforms &locals) { - static unordered_map features; - - auto maybeFeatures = features.find(_activeProgram); - if (maybeFeatures == features.end() || maybeFeatures->second != locals.features) { - glBindBufferBase(GL_UNIFORM_BUFFER, kFeaturesBindingPointIndex, _featuresUbo); - glBufferData(GL_UNIFORM_BUFFER, sizeof(FeatureUniforms), &locals.features, GL_STATIC_DRAW); - features[_activeProgram] = locals.features; - } glBindBufferBase(GL_UNIFORM_BUFFER, kGeneralBindingPointIndex, _generalUbo); glBufferData(GL_UNIFORM_BUFFER, sizeof(GeneralUniforms), &locals.general, GL_STATIC_DRAW); - if (locals.features.skeletalEnabled) { + if (locals.general.skeletalEnabled) { glBindBufferBase(GL_UNIFORM_BUFFER, kSkeletalBindingPointIndex, _skeletalUbo); glBufferData(GL_UNIFORM_BUFFER, sizeof(SkeletalUniforms), locals.skeletal.get(), GL_STATIC_DRAW); } - if (locals.features.lightingEnabled) { + if (locals.general.lightingEnabled) { glBindBufferBase(GL_UNIFORM_BUFFER, kLightingBindingPointIndex, _lightingUbo); glBufferData(GL_UNIFORM_BUFFER, sizeof(LightingUniforms), locals.lighting.get(), GL_STATIC_DRAW); } diff --git a/src/render/shaders.h b/src/render/shaders.h index f3b91f1b..36d59442 100644 --- a/src/render/shaders.h +++ b/src/render/shaders.h @@ -57,7 +57,7 @@ struct GlobalUniforms { glm::vec3 cameraPosition { 0.0f }; }; -struct FeatureUniforms { +struct GeneralUniforms { int lightmapEnabled { false }; int envmapEnabled { false }; int bumpyShinyEnabled { false }; @@ -68,31 +68,12 @@ struct FeatureUniforms { int blurEnabled { false }; int bloomEnabled { false }; int discardEnabled { false }; + char padding1[8]; - bool operator==(const FeatureUniforms &other) { - return - lightmapEnabled == other.lightmapEnabled && - envmapEnabled == other.envmapEnabled && - bumpyShinyEnabled == other.bumpyShinyEnabled && - bumpmapEnabled == other.bumpmapEnabled && - skeletalEnabled == other.skeletalEnabled && - lightingEnabled == other.lightingEnabled && - selfIllumEnabled == other.selfIllumEnabled && - blurEnabled == other.blurEnabled && - bloomEnabled == other.bloomEnabled && - discardEnabled == other.discardEnabled; - } - - bool operator!=(const FeatureUniforms &other) { - return !operator==(other); - } -}; - -struct GeneralUniforms { glm::mat4 model { 1.0f }; glm::vec4 color { 1.0f }; float alpha { 1.0f }; - char padding[12]; + char padding2[12]; glm::vec4 selfIllumColor { 1.0f }; glm::vec4 discardColor { 0.0f }; glm::vec2 blurResolution { 0.0f }; @@ -120,7 +101,6 @@ struct LightingUniforms { }; struct LocalUniforms { - FeatureUniforms features; GeneralUniforms general; std::shared_ptr skeletal; std::shared_ptr lighting; @@ -160,7 +140,6 @@ private: // Uniform buffer objects - uint32_t _featuresUbo { 0 }; uint32_t _generalUbo { 0 }; uint32_t _lightingUbo { 0 }; uint32_t _skeletalUbo { 0 }; diff --git a/src/scene/modelnodescenenode.cpp b/src/scene/modelnodescenenode.cpp index f2bd0bec..ee4569e3 100644 --- a/src/scene/modelnodescenenode.cpp +++ b/src/scene/modelnodescenenode.cpp @@ -72,19 +72,19 @@ void ModelNodeSceneNode::renderSingle() const { locals.general.alpha = _modelSceneNode->alpha() * _modelNode->alpha(); if (mesh->hasEnvmapTexture()) { - locals.features.envmapEnabled = true; + locals.general.envmapEnabled = true; } if (mesh->hasLightmapTexture()) { - locals.features.lightmapEnabled = true; + locals.general.lightmapEnabled = true; } if (mesh->hasBumpyShinyTexture()) { - locals.features.bumpyShinyEnabled = true; + locals.general.bumpyShinyEnabled = true; } if (mesh->hasBumpmapTexture()) { - locals.features.bumpmapEnabled = true; + locals.general.bumpmapEnabled = true; } if (skeletal) { - locals.features.skeletalEnabled = true; + locals.general.skeletalEnabled = true; locals.skeletal = Shaders::instance().skeletalUniforms(); locals.skeletal->absTransform = _modelNode->absoluteTransform(); locals.skeletal->absTransformInv = _modelNode->absoluteTransformInverse(); @@ -103,7 +103,7 @@ void ModelNodeSceneNode::renderSingle() const { } } if (_modelNode->isSelfIllumEnabled()) { - locals.features.selfIllumEnabled = true; + locals.general.selfIllumEnabled = true; locals.general.selfIllumColor = glm::vec4(_modelNode->selfIllumColor(), 1.0f); } int lightCount = 0; @@ -111,7 +111,7 @@ void ModelNodeSceneNode::renderSingle() const { if (_modelSceneNode->isLightingEnabled()) { const vector &lights = _modelSceneNode->lightsAffectedBy(); - locals.features.lightingEnabled = true; + locals.general.lightingEnabled = true; locals.lighting = Shaders::instance().lightingUniforms(); locals.lighting->ambientLightColor = glm::vec4(_sceneGraph->ambientLightColor(), 1.0f); locals.lighting->lightCount = static_cast(lights.size());