refactor: Set general local uniforms through a UBO

This commit is contained in:
Vsevolod Kremianskii 2020-11-14 17:01:56 +07:00
parent 4a06af4689
commit 92f6cab491
15 changed files with 101 additions and 104 deletions

View file

@ -96,9 +96,9 @@ void Console::render() const {
transform = glm::scale(transform, glm::vec3(_opts.width, height, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = glm::vec3(0.0f);
locals.alpha = 0.5f;
locals.general.model = move(transform);
locals.general.color = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f);
locals.general.alpha = 0.5f;
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}

View file

@ -100,7 +100,7 @@ void SelectionOverlay::drawReticle(Texture &texture, const glm::vec3 &screenCoor
transform = glm::scale(transform, glm::vec3(width, height, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
@ -122,9 +122,9 @@ void SelectionOverlay::drawTitleBar() const {
transform = glm::scale(transform, glm::vec3(kTitleBarWidth, barHeight, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = glm::vec3(0.0f);
locals.alpha = 0.5f;
locals.general.model = move(transform);
locals.general.color = glm::vec4(0.0f);
locals.general.alpha = 0.5f;
Shaders::instance().activate(ShaderProgram::GUIWhite, locals);

View file

@ -249,9 +249,9 @@ 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.model = move(transform);
locals.features.discardEnabled = _discardEnabled;
locals.discardColor = _discardColor;
locals.general.model = move(transform);
locals.general.discardColor = glm::vec4(_discardColor, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -288,8 +288,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::rotate(transform, glm::pi<float>(), glm::vec3(1.0f, 0.0f, 0.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -303,8 +303,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::rotate(transform, glm::half_pi<float>(), glm::vec3(0.0f, 0.0f, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -322,8 +322,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(width, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -336,8 +336,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(width, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -359,8 +359,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(border.dimension, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -373,8 +373,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(border.dimension, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -387,8 +387,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(border.dimension, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -401,8 +401,8 @@ void Control::drawBorder(const Border &border, const glm::ivec2 &offset, const g
transform = glm::scale(transform, glm::vec3(border.dimension, border.dimension, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.color = color;
locals.general.model = move(transform);
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}

View file

@ -68,8 +68,8 @@ void ImageButton::drawIcon(const glm::ivec2 &offset, const shared_ptr<Texture> &
}
LocalUniforms locals;
locals.model = transform;
locals.color = move(frameColor);
locals.general.model = transform;
locals.general.color = glm::vec4(frameColor, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}
@ -81,7 +81,7 @@ void ImageButton::drawIcon(const glm::ivec2 &offset, const shared_ptr<Texture> &
}
{
LocalUniforms locals;
locals.model = transform;
locals.general.model = transform;
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
}

View file

@ -61,7 +61,7 @@ void ScrollBar::drawUpArrow(const glm::vec2 &offset) const {
transform = glm::scale(transform, glm::vec3(_extent.width, _extent.width, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
Quad::getDefault().renderTriangles();
@ -74,7 +74,7 @@ void ScrollBar::drawDownArrow(const glm::vec2 &offset) const {
transform = glm::rotate(transform, glm::pi<float>(), glm::vec3(1.0f, 0.0f, 0.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);
Quad::getDefault().renderTriangles();

View file

@ -260,7 +260,7 @@ void GUI::drawBackground() const {
transform = glm::scale(transform, glm::vec3(_gfxOpts.width, _gfxOpts.height, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);

View file

@ -49,7 +49,7 @@ void Cursor::render() const {
transform = glm::scale(transform, glm::vec3(texture->width(), texture->height(), 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);

View file

@ -132,8 +132,8 @@ void Font::render(const string &text, const glm::mat4 &transform, const glm::vec
for (auto &glyph : text) {
LocalUniforms locals;
locals.model = textTransform;
locals.color = color;
locals.general.model = textTransform;
locals.general.color = glm::vec4(color, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);

View file

@ -66,7 +66,7 @@ void AABBMesh::render(const AABB &aabb, const glm::mat4 &transform) const {
glm::mat4 finalTransform(transform * aabb.transform());
LocalUniforms locals;
locals.model = finalTransform;
locals.general.model = finalTransform;
Shaders::instance().activate(ShaderProgram::ModelWhite, locals);
Mesh::renderLines();

View file

@ -62,7 +62,7 @@ CubeMesh::CubeMesh() {
void CubeMesh::render(const glm::mat4 &transform) const {
LocalUniforms locals;
locals.model = transform;
locals.general.model = transform;
Shaders::instance().activate(ShaderProgram::ModelWhite, locals);

View file

@ -76,7 +76,7 @@ void ControlRenderPipeline::render(const glm::ivec2 &offset) const {
Shaders::instance().setGlobalUniforms(globals);
LocalUniforms locals;
locals.model = move(transform);
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIGUI, locals);

View file

@ -79,10 +79,10 @@ void WorldRenderPipeline::render() const {
transform = glm::scale(transform, glm::vec3(w, h, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.features.blurEnabled = true;
locals.blur.resolution = glm::vec2(w, h);
locals.blur.direction = glm::vec2(1.0f, 0.0f);
locals.general.model = move(transform);
locals.general.blurResolution = glm::vec2(w, h);
locals.general.blurDirection = glm::vec2(1.0f, 0.0f);
Shaders::instance().activate(ShaderProgram::GUIBlur, locals);
@ -110,10 +110,10 @@ void WorldRenderPipeline::render() const {
transform = glm::scale(transform, glm::vec3(w, h, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.features.blurEnabled = true;
locals.blur.resolution = glm::vec2(_opts.width, _opts.height);
locals.blur.direction = glm::vec2(0.0f, 1.0f);
locals.general.model = move(transform);
locals.general.blurResolution = glm::vec2(_opts.width, _opts.height);
locals.general.blurDirection = glm::vec2(0.0f, 1.0f);
Shaders::instance().activate(ShaderProgram::GUIBlur, locals);
@ -133,8 +133,8 @@ void WorldRenderPipeline::render() const {
transform = glm::scale(transform, glm::vec3(w, h, 1.0f));
LocalUniforms locals;
locals.model = move(transform);
locals.features.bloomEnabled = true;
locals.general.model = move(transform);
Shaders::instance().activate(ShaderProgram::GUIBloom, locals);

View file

@ -34,8 +34,9 @@ namespace reone {
namespace render {
static const int kFeaturesBindingPointIndex = 1;
static const int kLightingBindingPointIndex = 2;
static const int kSkeletalBindingPointIndex = 3;
static const int kGeneralBindingPointIndex = 2;
static const int kLightingBindingPointIndex = 3;
static const int kSkeletalBindingPointIndex = 4;
static const GLchar kCommonShaderHeader[] = R"END(
#version 330
@ -62,6 +63,16 @@ layout(std140) uniform Features {
bool uDiscardEnabled;
};
layout(std140) uniform General {
uniform mat4 uModel;
uniform vec4 uColor;
uniform float uAlpha;
uniform vec4 uSelfIllumColor;
uniform vec4 uDiscardColor;
uniform vec2 uBlurResolution;
uniform vec2 uBlurDirection;
};
layout(std140) uniform Lighting {
vec4 uAmbientLightColor;
int uLightCount;
@ -78,7 +89,6 @@ layout(std140) uniform Skeletal {
static const GLchar kGUIVertexShader[] = R"END(
uniform mat4 uProjection;
uniform mat4 uView;
uniform mat4 uModel;
layout(location = 0) in vec3 aPosition;
layout(location = 2) in vec2 aTexCoords;
@ -94,7 +104,6 @@ void main() {
static const GLchar kModelVertexShader[] = R"END(
uniform mat4 uProjection;
uniform mat4 uView;
uniform mat4 uModel;
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec3 aNormal;
@ -143,23 +152,17 @@ void main() {
)END";
static const GLchar kWhiteFragmentShader[] = R"END(
uniform vec3 uColor;
uniform float uAlpha;
layout(location = 0) out vec4 fragColor;
layout(location = 1) out vec4 fragColorBright;
void main() {
fragColor = vec4(uColor, uAlpha);
fragColor = vec4(uColor.rgb, uAlpha);
fragColorBright = vec4(0.0, 0.0, 0.0, 1.0);
}
)END";
static const GLchar kGUIFragmentShader[] = R"END(
uniform sampler2D uTexture;
uniform vec3 uColor;
uniform float uAlpha;
uniform vec3 uDiscardColor;
in vec2 fragTexCoords;
@ -168,9 +171,9 @@ layout(location = 1) out vec4 fragColorBright;
void main() {
vec4 textureSample = texture(uTexture, fragTexCoords);
vec3 finalColor = uColor * textureSample.rgb;
vec3 finalColor = uColor.rgb * textureSample.rgb;
if (uDiscardEnabled && length(uDiscardColor - finalColor) < 0.01) {
if (uDiscardEnabled && length(uDiscardColor.rgb - finalColor) < 0.01) {
discard;
}
fragColor = vec4(finalColor, uAlpha * textureSample.a);
@ -188,9 +191,6 @@ uniform samplerCube uEnvmap;
uniform samplerCube uBumpyShiny;
uniform vec3 uCameraPosition;
uniform float uAlpha;
uniform vec3 uSelfIllumColor;
in vec3 fragPosition;
in vec3 fragNormal;
@ -266,7 +266,7 @@ void main() {
fragColor = vec4(lightColor * surfaceColor, finalAlpha);
if (uSelfIllumEnabled) {
vec3 color = uSelfIllumColor * diffuseSample.rgb;
vec3 color = uSelfIllumColor.rgb * diffuseSample.rgb;
fragColorBright = vec4(smoothstep(0.75, 1.0, color), 1.0);
} else {
fragColorBright = vec4(0.0, 0.0, 0.0, 1.0);
@ -276,21 +276,19 @@ void main() {
static const GLchar kGaussianBlurFragmentShader[] = R"END(
uniform sampler2D uTexture;
uniform vec2 uResolution;
uniform vec2 uDirection;
out vec4 fragColor;
void main() {
vec2 uv = vec2(gl_FragCoord.xy / uResolution);
vec2 uv = vec2(gl_FragCoord.xy / uBlurResolution);
vec4 color = vec4(0.0);
vec2 off1 = vec2(1.3846153846) * uDirection;
vec2 off2 = vec2(3.2307692308) * uDirection;
vec2 off1 = vec2(1.3846153846) * uBlurDirection;
vec2 off2 = vec2(3.2307692308) * uBlurDirection;
color += texture2D(uTexture, uv) * 0.2270270270;
color += texture2D(uTexture, uv + (off1 / uResolution)) * 0.3162162162;
color += texture2D(uTexture, uv - (off1 / uResolution)) * 0.3162162162;
color += texture2D(uTexture, uv + (off2 / uResolution)) * 0.0702702703;
color += texture2D(uTexture, uv - (off2 / uResolution)) * 0.0702702703;
color += texture2D(uTexture, uv + (off1 / uBlurResolution)) * 0.3162162162;
color += texture2D(uTexture, uv - (off1 / uBlurResolution)) * 0.3162162162;
color += texture2D(uTexture, uv + (off2 / uBlurResolution)) * 0.0702702703;
color += texture2D(uTexture, uv - (off2 / uBlurResolution)) * 0.0702702703;
fragColor = color;
}
@ -339,6 +337,7 @@ void Shaders::initGL() {
initProgram(ShaderProgram::ModelModel, ShaderName::VertexModel, ShaderName::FragmentModel);
glGenBuffers(1, &_featuresUbo);
glGenBuffers(1, &_generalUbo);
glGenBuffers(1, &_lightingUbo);
glGenBuffers(1, &_skeletalUbo);
@ -350,6 +349,10 @@ void Shaders::initGL() {
if (featuresBlockIdx != GL_INVALID_INDEX) {
glUniformBlockBinding(_activeOrdinal, featuresBlockIdx, kFeaturesBindingPointIndex);
}
uint32_t generalBlockIdx = glGetUniformBlockIndex(_activeOrdinal, "General");
if (generalBlockIdx != GL_INVALID_INDEX) {
glUniformBlockBinding(_activeOrdinal, generalBlockIdx, kGeneralBindingPointIndex);
}
uint32_t lightingBlockIdx = glGetUniformBlockIndex(_activeOrdinal, "Lighting");
if (lightingBlockIdx != GL_INVALID_INDEX) {
glUniformBlockBinding(_activeOrdinal, lightingBlockIdx, kLightingBindingPointIndex);
@ -424,6 +427,10 @@ void Shaders::deinitGL() {
glDeleteBuffers(1, &_lightingUbo);
_lightingUbo = 0;
}
if (_generalUbo) {
glDeleteBuffers(1, &_generalUbo);
_generalUbo = 0;
}
if (_featuresUbo) {
glDeleteBuffers(1, &_featuresUbo);
_featuresUbo = 0;
@ -467,9 +474,8 @@ void Shaders::setLocalUniforms(const LocalUniforms &locals) {
glBufferData(GL_UNIFORM_BUFFER, sizeof(FeatureUniforms), &locals.features, GL_STATIC_DRAW);
features[_activeProgram] = locals.features;
}
setUniform("uModel", locals.model);
setUniform("uColor", locals.color);
setUniform("uAlpha", locals.alpha);
glBindBufferBase(GL_UNIFORM_BUFFER, kGeneralBindingPointIndex, _generalUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(GeneralUniforms), &locals.general, GL_STATIC_DRAW);
if (locals.features.skeletalEnabled) {
glBindBufferBase(GL_UNIFORM_BUFFER, kSkeletalBindingPointIndex, _skeletalUbo);
@ -479,16 +485,6 @@ void Shaders::setLocalUniforms(const LocalUniforms &locals) {
glBindBufferBase(GL_UNIFORM_BUFFER, kLightingBindingPointIndex, _lightingUbo);
glBufferData(GL_UNIFORM_BUFFER, sizeof(LightingUniforms), locals.lighting.get(), GL_STATIC_DRAW);
}
if (locals.features.selfIllumEnabled) {
setUniform("uSelfIllumColor", locals.selfIllumColor);
}
if (locals.features.blurEnabled) {
setUniform("uResolution", locals.blur.resolution);
setUniform("uDirection", locals.blur.direction);
}
if (locals.features.discardEnabled) {
setUniform("uDiscardColor", locals.discardColor);
}
}
void Shaders::setUniform(const string &name, const glm::mat4 &m) {

View file

@ -43,6 +43,14 @@ enum class ShaderProgram {
ModelModel
};
struct TextureUniforms {
static constexpr int envmap { 1 };
static constexpr int lightmap { 2 };
static constexpr int bumpyShiny { 3 };
static constexpr int bumpmap { 4 };
static constexpr int bloom { 5 };
};
struct GlobalUniforms {
glm::mat4 projection { 1.0f };
glm::mat4 view { 1.0f };
@ -80,12 +88,15 @@ struct FeatureUniforms {
}
};
struct TextureUniforms {
static constexpr int envmap { 1 };
static constexpr int lightmap { 2 };
static constexpr int bumpyShiny { 3 };
static constexpr int bumpmap { 4 };
static constexpr int bloom { 5 };
struct GeneralUniforms {
glm::mat4 model { 1.0f };
glm::vec4 color { 1.0f };
float alpha { 1.0f };
char padding[12];
glm::vec4 selfIllumColor { 1.0f };
glm::vec4 discardColor { 0.0f };
glm::vec2 blurResolution { 0.0f };
glm::vec2 blurDirection { 0.0f };
};
struct SkeletalUniforms {
@ -108,22 +119,11 @@ struct LightingUniforms {
ShaderLight lights[kMaxLightCount];
};
struct GaussianBlurUniforms {
glm::vec2 resolution { 0.0f };
glm::vec2 direction { 0.0f };
};
struct LocalUniforms {
FeatureUniforms features;
GeneralUniforms general;
std::shared_ptr<SkeletalUniforms> skeletal;
std::shared_ptr<LightingUniforms> lighting;
GaussianBlurUniforms blur;
glm::mat4 model { 1.0f };
glm::vec3 color { 1.0f };
float alpha { 1.0f };
glm::vec3 selfIllumColor { 1.0f };
glm::vec3 discardColor { 0.0f };
};
class Shaders {
@ -161,6 +161,7 @@ private:
// Uniform buffer objects
uint32_t _featuresUbo { 0 };
uint32_t _generalUbo { 0 };
uint32_t _lightingUbo { 0 };
uint32_t _skeletalUbo { 0 };

View file

@ -68,8 +68,8 @@ void ModelNodeSceneNode::renderSingle() const {
bool skeletal = static_cast<bool>(skin);
LocalUniforms locals;
locals.model = _absoluteTransform;
locals.alpha = _modelSceneNode->alpha() * _modelNode->alpha();
locals.general.model = _absoluteTransform;
locals.general.alpha = _modelSceneNode->alpha() * _modelNode->alpha();
if (mesh->hasEnvmapTexture()) {
locals.features.envmapEnabled = true;
@ -104,7 +104,7 @@ void ModelNodeSceneNode::renderSingle() const {
}
if (_modelNode->isSelfIllumEnabled()) {
locals.features.selfIllumEnabled = true;
locals.selfIllumColor = _modelNode->selfIllumColor();
locals.general.selfIllumColor = glm::vec4(_modelNode->selfIllumColor(), 1.0f);
}
int lightCount = 0;