Skip to content

Commit

Permalink
[ShaderUniform] Added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Oct 29, 2023
1 parent c84ed93 commit b6e01b6
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 117 deletions.
48 changes: 10 additions & 38 deletions source/client/graphics/ChunkRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,9 @@
#include "Texture.hpp"

ChunkRenderer::ChunkRenderer(const TextureAtlas &textureAtlas) : m_textureAtlas(textureAtlas) {
m_renderDistance = bgfx::createUniform("u_renderDistance", bgfx::UniformType::Vec4);
m_fogColor = bgfx::createUniform("u_fogColor", bgfx::UniformType::Vec4);
m_mipLevel = bgfx::createUniform("u_mipLevel", bgfx::UniformType::Vec4);
}

ChunkRenderer::~ChunkRenderer() {
if (bgfx::isValid(m_mipLevel)) {
bgfx::destroy(m_mipLevel);
m_mipLevel.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_fogColor)) {
bgfx::destroy(m_fogColor);
m_fogColor.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_renderDistance)) {
bgfx::destroy(m_renderDistance);
m_renderDistance.idx = bgfx::kInvalidHandle;
}
m_renderDistance.init("u_renderDistance", bgfx::UniformType::Vec4);
m_fogColor.init("u_fogColor", bgfx::UniformType::Vec4);
m_mipLevel.init("u_mipLevel", bgfx::UniformType::Vec4);
}

inline static bool bbIntersects(const glm::vec3 &a0, const glm::vec3 &a1, const glm::vec3 &b0, const glm::vec3 &b1) {
Expand Down Expand Up @@ -297,25 +280,15 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const

bgfx::setViewMode(states.view, bgfx::ViewMode::DepthDescending);

float renderDistance[4] = {(float)Config::renderDistance * CHUNK_WIDTH, 0.f, 0.f, 0.f};
bgfx::setUniform(m_renderDistance, renderDistance);
m_renderDistance.setValue(Config::renderDistance * CHUNK_WIDTH);

if (currentSky) {
float fogColor[4] = {
currentSky->fogColor().r,
currentSky->fogColor().g,
currentSky->fogColor().b,
currentSky->fogColor().a
};
bgfx::setUniform(m_fogColor, fogColor);
}
if (currentSky)
m_fogColor.setValue(currentSky->fogColor());

for (u8 layer = 0 ; layer < ChunkMeshLayer::Count ; ++layer) {
float mipLevel[4] = {0.f,
(layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora)
? 0.f : (float)Config::mipmapLevels,
0.f, 0.f
};
float maxMipLevel = 0.f;
if (layer == ChunkMeshLayer::NoMipMap || layer == ChunkMeshLayer::Flora)
maxMipLevel = Config::mipmapLevels;

if (layer == ChunkMeshLayer::Flora || layer == ChunkMeshLayer::Liquid)
states.isCullFaceEnabled = false;
Expand All @@ -326,7 +299,7 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const
std::size_t verticesCount = std::get<0>(it)->getVerticesCount(layer);
if (verticesCount == 0) continue;

bgfx::setUniform(m_mipLevel, mipLevel);
m_mipLevel.setValue(0.f, maxMipLevel);

target.beginDrawing(states);

Expand All @@ -345,4 +318,3 @@ void ChunkRenderer::drawChunks(RenderTarget &target, RenderStates states, const
}
}
}

8 changes: 4 additions & 4 deletions source/client/graphics/ChunkRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <functional>

#include "RenderStates.hpp"
#include "ShaderUniform.hpp"
#include "Sky.hpp"
#include "TextureAtlas.hpp"

Expand All @@ -45,7 +46,6 @@ class ChunkRenderer {

public:
ChunkRenderer(const TextureAtlas &textureAtlas);
~ChunkRenderer();

void draw(RenderTarget &target, RenderStates states, const ChunkMap &chunks, Camera &camera, const Sky *currentSky) const;

Expand All @@ -60,9 +60,9 @@ class ChunkRenderer {
OnChunkDeletionRequestedCallback m_onChunkDeletionRequested;
OnMeshingRequestedCallback m_onMeshingRequested;

bgfx::UniformHandle m_renderDistance = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_fogColor = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_mipLevel = BGFX_INVALID_HANDLE;
ShaderUniform m_renderDistance;
ShaderUniform m_fogColor;
ShaderUniform m_mipLevel;
};

#endif // CHUNKRENDERER_HPP_
43 changes: 8 additions & 35 deletions source/client/graphics/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ Framebuffer::Framebuffer() {

m_vbo.init(quad, sizeof(quad));

m_colorTextureSampler = bgfx::createUniform("s_colorTexture", bgfx::UniformType::Sampler);
m_depthTextureSampler = bgfx::createUniform("s_depthTexture", bgfx::UniformType::Sampler);
m_colorTextureSampler.init("s_colorTexture", bgfx::UniformType::Sampler);
m_depthTextureSampler.init("s_depthTexture", bgfx::UniformType::Sampler);

m_effectTypeUniform = bgfx::createUniform("u_effectType", bgfx::UniformType::Vec4);
m_depthFogColorUniform = bgfx::createUniform("u_depthFogColor", bgfx::UniformType::Vec4);
m_effectTypeUniform.init("u_effectType", bgfx::UniformType::Vec4);
m_depthFogColorUniform.init("u_depthFogColor", bgfx::UniformType::Vec4);
}

Framebuffer::Framebuffer(u16 width, u16 height) : Framebuffer() {
Expand Down Expand Up @@ -95,26 +95,6 @@ void Framebuffer::free() {
bgfx::destroy(m_handle);
m_handle.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_depthFogColorUniform)) {
bgfx::destroy(m_depthFogColorUniform);
m_depthFogColorUniform.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_effectTypeUniform)) {
bgfx::destroy(m_effectTypeUniform);
m_effectTypeUniform.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_depthTextureSampler)) {
bgfx::destroy(m_depthTextureSampler);
m_depthTextureSampler.idx = bgfx::kInvalidHandle;
}

if (bgfx::isValid(m_colorTextureSampler)) {
bgfx::destroy(m_colorTextureSampler);
m_colorTextureSampler.idx = bgfx::kInvalidHandle;
}
}

void Framebuffer::loadShader(const std::string &name) {
Expand All @@ -127,19 +107,12 @@ void Framebuffer::prepareDraw() const {
}

void Framebuffer::draw() const {
bgfx::setTexture(0, m_colorTextureSampler, m_textures[0]);
bgfx::setTexture(1, m_depthTextureSampler, m_textures[1]);
bgfx::setTexture(0, m_colorTextureSampler.handle(), m_textures[0]);
bgfx::setTexture(1, m_depthTextureSampler.handle(), m_textures[1]);

float effectType[4] = {(float)GameConfig::currentScreenEffect, GameConfig::fogDepth, 0.f, 0.f};
bgfx::setUniform(m_effectTypeUniform, effectType);
m_effectTypeUniform.setValue(GameConfig::currentScreenEffect, GameConfig::fogDepth);

float fogColor[4] = {
GameConfig::fogColor.r,
GameConfig::fogColor.g,
GameConfig::fogColor.b,
GameConfig::fogColor.a,
};
bgfx::setUniform(m_depthFogColorUniform, fogColor);
m_depthFogColorUniform.setValue(GameConfig::fogColor);

m_vbo.enable();

Expand Down
9 changes: 5 additions & 4 deletions source/client/graphics/Framebuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#define FRAMEBUFFER_HPP_

#include "Shader.hpp"
#include "ShaderUniform.hpp"
#include "Texture.hpp"
#include "VertexBuffer.hpp"

Expand All @@ -49,11 +50,11 @@ class Framebuffer : public gk::NonCopyable {
void draw() const;

private:
bgfx::UniformHandle m_colorTextureSampler = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_depthTextureSampler = BGFX_INVALID_HANDLE;
ShaderUniform m_colorTextureSampler;
ShaderUniform m_depthTextureSampler;

bgfx::UniformHandle m_effectTypeUniform = BGFX_INVALID_HANDLE;
bgfx::UniformHandle m_depthFogColorUniform = BGFX_INVALID_HANDLE;
ShaderUniform m_effectTypeUniform;
ShaderUniform m_depthFogColorUniform;

bgfx::TextureHandle m_textures[2] = {BGFX_INVALID_HANDLE, BGFX_STATE_BLEND_INV_SRC_ALPHA};

Expand Down
80 changes: 80 additions & 0 deletions source/client/graphics/ShaderUniform.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#include <gk/core/Exception.hpp>

#include "ShaderUniform.hpp"

ShaderUniform::ShaderUniform(ShaderUniform &&shaderUniform) {
m_handle = shaderUniform.m_handle;
shaderUniform.m_handle.idx = bgfx::kInvalidHandle;
}

ShaderUniform::~ShaderUniform() {
free();
}

ShaderUniform &ShaderUniform::operator=(ShaderUniform &&shaderUniform) {
m_handle = shaderUniform.m_handle;
shaderUniform.m_handle.idx = bgfx::kInvalidHandle;

return *this;
}

void ShaderUniform::init(const char *uniformName, bgfx::UniformType::Enum type, uint16_t num) {
if (bgfx::isValid(m_handle))
throw EXCEPTION("Trying to reuse an uniform named", (m_name ? m_name : "?"), "with name", (uniformName ? uniformName : "?"));

m_name = uniformName;
m_handle = bgfx::createUniform(m_name, type, num);
}

void ShaderUniform::free() {
if (bgfx::isValid(m_handle))
bgfx::destroy(m_handle);
}

void ShaderUniform::setValue(float x, float y, float z, float w) const {
float value[4] = {x, y, z, w};
bgfx::setUniform(m_handle, value);
}

void ShaderUniform::setValue(const gk::Color &color, bool needsRounding) const {
if (!needsRounding) {
float value[4] = {color.r, color.g, color.b, color.a};
bgfx::setUniform(m_handle, value);
}
else {
float value[4] = {
color.r255() / 255.f,
color.g255() / 255.f,
color.b255() / 255.f,
color.a255() / 255.f
};

bgfx::setUniform(m_handle, value);
}
}
60 changes: 60 additions & 0 deletions source/client/graphics/ShaderUniform.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* =====================================================================================
*
* OpenMiner
*
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <[email protected]>
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
*
* This file is part of OpenMiner.
*
* OpenMiner is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* OpenMiner is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
#ifndef SHADERUNIFORM_HPP_
#define SHADERUNIFORM_HPP_

#include <bgfx/bgfx.h>

#include <gk/utils/NonCopyable.hpp>

namespace gk {
class Color;
}

class ShaderUniform : public gk::NonCopyable {
public:
ShaderUniform() = default;
ShaderUniform(ShaderUniform &&);
~ShaderUniform();

ShaderUniform &operator=(ShaderUniform &&);

void init(const char *uniformName, bgfx::UniformType::Enum type, uint16_t num = 1);
void free();

void setValue(float x, float y = 0.f, float z = 0.f, float w = 0.f) const;
void setValue(const gk::Color &color, bool needsRounding = false) const;

const bgfx::UniformHandle &handle() const { return m_handle; }

private:
const char *m_name = nullptr;

bgfx::UniformHandle m_handle = BGFX_INVALID_HANDLE;
};

#endif // SHADERUNIFORM_HPP_
Loading

0 comments on commit b6e01b6

Please sign in to comment.