refactor: Combine features and general local uniforms

This commit is contained in:
Vsevolod Kremianskii 2020-11-14 17:17:40 +07:00
parent 92f6cab491
commit 3ebd70f5db
5 changed files with 19 additions and 57 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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<ShaderProgram, FeatureUniforms> 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);
}

View file

@ -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<SkeletalUniforms> skeletal;
std::shared_ptr<LightingUniforms> 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 };

View file

@ -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<LightSceneNode *> &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<int>(lights.size());