summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <lag1924@gmail.com>2021-11-21 17:26:31 +0100
committerLaG1924 <lag1924@gmail.com>2021-11-21 17:45:48 +0100
commit0148fe0915c2c6f0357609c9b3e1c99d713fcd2b (patch)
tree8d1d50e046b4fd1ddfc688934e795fb0774acb0d /src
parentAdded gbuffer visualization (diff)
downloadAltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar.gz
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar.bz2
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar.lz
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar.xz
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.tar.zst
AltCraft-0148fe0915c2c6f0357609c9b3e1c99d713fcd2b.zip
Diffstat (limited to '')
-rw-r--r--src/Render.cpp1
-rw-r--r--src/RenderConfigs.cpp145
-rw-r--r--src/RenderConfigs.hpp60
3 files changed, 140 insertions, 66 deletions
diff --git a/src/Render.cpp b/src/Render.cpp
index 148ec27..fadec80 100644
--- a/src/Render.cpp
+++ b/src/Render.cpp
@@ -138,6 +138,7 @@ void Render::PrepareToRendering() {
size_t scaledW = width * resolutionScale, scaledH = height * resolutionScale;
gbuffer = std::make_unique<Gbuffer>(scaledW, scaledH, scaledW, scaledH);
+ gbuffer->SetRenderBuff(renderBuff);
auto gal = Gal::GetImplementation();
gal->GetDefaultFramebuffer()->SetViewport(0, 0, width, height);
diff --git a/src/RenderConfigs.cpp b/src/RenderConfigs.cpp
index 2867bc6..d62d204 100644
--- a/src/RenderConfigs.cpp
+++ b/src/RenderConfigs.cpp
@@ -2,6 +2,81 @@
#include "AssetManager.hpp"
+std::string LoadShaderCode(std::string_view assetPath) {
+ auto gal = Gal::GetImplementation();
+ auto codeAsset = AssetManager::GetAssetByAssetName(std::string(assetPath));
+ auto codePtr = reinterpret_cast<const char*>(codeAsset->data.data());
+ return std::string(codePtr, codePtr + codeAsset->data.size());
+}
+
+std::shared_ptr<Gal::Shader> LoadVertexShader(std::string_view assetPath) {
+ auto gal = Gal::GetImplementation();
+ return gal->LoadVertexShader(LoadShaderCode(assetPath));
+}
+
+std::shared_ptr<Gal::Shader> LoadPixelShader(std::string_view assetPath) {
+ auto gal = Gal::GetImplementation();
+ return gal->LoadPixelShader(LoadShaderCode(assetPath));
+}
+
+PostProcess::PostProcess(
+ std::shared_ptr<Gal::Shader> pixelShader,
+ std::vector<std::pair<std::string_view, std::shared_ptr<Gal::Texture>>> inputTextures,
+ std::vector<std::pair<std::string_view, Gal::Type>> inputParameters,
+ size_t width,
+ size_t height,
+ Gal::Format format,
+ Gal::Filtering filtering) {
+ auto gal = Gal::GetImplementation();
+
+ auto texConf = gal->CreateTexture2DConfig(width, height, format);
+ texConf->SetMinFilter(filtering);
+ texConf->SetMaxFilter(filtering);
+
+ result = gal->BuildTexture(texConf);
+
+ auto fbConf = gal->CreateFramebufferConfig();
+ fbConf->SetTexture(0, result);
+
+ framebuffer = gal->BuildFramebuffer(fbConf);
+ framebuffer->SetViewport(0, 0, width, height);
+
+ auto fbPPC = gal->CreatePipelineConfig();
+ fbPPC->SetTarget(framebuffer);
+ for (auto&& [name, texture] : inputTextures) {
+ fbPPC->AddStaticTexture(name, texture);
+ }
+ for (auto&& [name, type] : inputParameters) {
+ fbPPC->AddShaderParameter(name, type);
+ }
+ fbPPC->SetVertexShader(LoadVertexShader("/altcraft/shaders/vert/pp"));
+ fbPPC->SetPixelShader(pixelShader);
+ auto fbBufferBB = fbPPC->BindVertexBuffer({
+ {"pos", Gal::Type::Vec2},
+ {"uvPos", Gal::Type::Vec2}
+ });
+
+ pipeline = gal->BuildPipeline(fbPPC);
+
+ fbBuffer = gal->CreateBuffer();
+ constexpr float quadVertices[] = {
+ // pos // uv
+ -1.0f, 1.0f, 0.0f, 1.0f,
+ -1.0f, -1.0f, 0.0f, 0.0f,
+ 1.0f, -1.0f, 1.0f, 0.0f,
+
+ -1.0f, 1.0f, 0.0f, 1.0f,
+ 1.0f, -1.0f, 1.0f, 0.0f,
+ 1.0f, 1.0f, 1.0f, 1.0f
+ };
+ auto quadPtr = reinterpret_cast<const std::byte*>(quadVertices);
+ fbBuffer->SetData({ quadPtr, quadPtr + sizeof(quadVertices) });
+
+ pipelineInstance = pipeline->CreateInstance({
+ {fbBufferBB, fbBuffer}
+ });
+}
+
Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) {
auto gal = Gal::GetImplementation();
@@ -40,60 +115,24 @@ Gbuffer::Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH) {
geomFramebuffer = gal->BuildFramebuffer(geomFbConf);
geomFramebuffer->SetViewport(0, 0, geomW, geomH);
- auto finalColorConf = gal->CreateTexture2DConfig(lightW, lightH, Gal::Format::R8G8B8A8);
- finalColorConf->SetMinFilter(Gal::Filtering::Bilinear);
- finalColorConf->SetMaxFilter(Gal::Filtering::Bilinear);
- finalColor = gal->BuildTexture(finalColorConf);
-
- auto lightFbConf = gal->CreateFramebufferConfig();
- lightFbConf->SetTexture(0, finalColor);
-
- lightFramebuffer = gal->BuildFramebuffer(lightFbConf);
- lightFramebuffer->SetViewport(0, 0, lightW, lightH);
-
- std::string vertexSource, pixelSource;
- {
- auto vertAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/vert/light");
- vertexSource = std::string((char*)vertAsset->data.data(), (char*)vertAsset->data.data() + vertAsset->data.size());
-
- auto pixelAsset = AssetManager::GetAssetByAssetName("/altcraft/shaders/frag/light");
- pixelSource = std::string((char*)pixelAsset->data.data(), (char*)pixelAsset->data.data() + pixelAsset->data.size());
- }
-
- auto lightPPC = gal->CreatePipelineConfig();
- lightPPC->SetTarget(lightFramebuffer);
- lightPPC->AddStaticTexture("color", color);
- lightPPC->AddStaticTexture("addColor", addColor);
- lightPPC->AddStaticTexture("normal", normal);
- lightPPC->AddStaticTexture("light", light);
- lightPPC->AddStaticTexture("depthStencil", depthStencil);
- lightPPC->AddShaderParameter("dayTime", Gal::Type::Float);
- lightPPC->AddShaderParameter("renderBuff", Gal::Type::Int32);
-
- lightPPC->SetVertexShader(gal->LoadVertexShader(vertexSource));
- lightPPC->SetPixelShader(gal->LoadPixelShader(pixelSource));
-
- auto lightBB = lightPPC->BindVertexBuffer({
- {"pos", Gal::Type::Vec2},
- {"uvPos", Gal::Type::Vec2}
- });
-
- lightPipeline = gal->BuildPipeline(lightPPC);
-
- constexpr float quadVertices[] = {
- // pos // uv
- -1.0f, 1.0f, 0.0f, 1.0f,
- -1.0f, -1.0f, 0.0f, 0.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
+ std::vector<std::pair<std::string_view, std::shared_ptr<Gal::Texture>>> lightingTexture = {
+ {"color", color},
+ {"addColor", addColor},
+ {"normal", normal},
+ {"light", light},
+ {"depthStencil", depthStencil},
+ };
- -1.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, -1.0f, 1.0f, 0.0f,
- 1.0f, 1.0f, 1.0f, 1.0f
+ std::vector<std::pair<std::string_view, Gal::Type>> lightingParameters = {
+ {"dayTime", Gal::Type::Float},
+ {"renderBuff", Gal::Type::Int32},
};
- lightBuffer = gal->CreateBuffer();
- lightBuffer->SetData({ reinterpret_cast<const std::byte*>(quadVertices), reinterpret_cast<const std::byte*>(quadVertices) + sizeof(quadVertices) });
- lightPipelineInstance = lightPipeline->CreateInstance({
- {lightBB, lightBuffer}
- });
+ lightingPass = std::make_unique<PostProcess>(LoadPixelShader("/altcraft/shaders/frag/light"),
+ lightingTexture,
+ lightingParameters,
+ lightW,
+ lightH,
+ Gal::Format::R8G8B8A8,
+ Gal::Filtering::Bilinear);
}
diff --git a/src/RenderConfigs.hpp b/src/RenderConfigs.hpp
index 9b535ca..793dbb2 100644
--- a/src/RenderConfigs.hpp
+++ b/src/RenderConfigs.hpp
@@ -2,11 +2,49 @@
#include "Gal.hpp"
+std::shared_ptr<Gal::Shader> LoadVertexShader(std::string_view assetPath);
+
+std::shared_ptr<Gal::Shader> LoadPixelShader(std::string_view assetPath);
+
+class PostProcess {
+ std::shared_ptr<Gal::Framebuffer> framebuffer;
+ std::shared_ptr<Gal::Buffer> fbBuffer;
+ std::shared_ptr<Gal::Pipeline> pipeline;
+ std::shared_ptr<Gal::PipelineInstance> pipelineInstance;
+ std::shared_ptr<Gal::Texture> result;
+public:
+
+ PostProcess(
+ std::shared_ptr<Gal::Shader> pixelShader,
+ std::vector<std::pair<std::string_view, std::shared_ptr<Gal::Texture>>> inputTextures,
+ std::vector<std::pair<std::string_view, Gal::Type>> inputParameters,
+ size_t width,
+ size_t height,
+ Gal::Format format,
+ Gal::Filtering filtering);
+
+ void Clear() {
+ framebuffer->Clear();
+ }
+
+ void Render() {
+ pipeline->Activate();
+ pipelineInstance->Activate();
+ pipelineInstance->Render(0, 6);
+ }
+
+ template<typename T>
+ void SetShaderParameter(std::string_view name, T value) {
+ pipeline->SetShaderParameter(name, value);
+ }
+
+ std::shared_ptr<Gal::Texture> GetResultTexture() {
+ return result;
+ }
+};
+
class Gbuffer {
- std::shared_ptr<Gal::Framebuffer> lightFramebuffer;
- std::shared_ptr<Gal::Buffer> lightBuffer;
- std::shared_ptr<Gal::Pipeline> lightPipeline;
- std::shared_ptr<Gal::PipelineInstance> lightPipelineInstance;
+ std::unique_ptr<PostProcess> lightingPass;
std::shared_ptr<Gal::Texture> color; //RGB - color
std::shared_ptr<Gal::Texture> normal; //RGB - normal
std::shared_ptr<Gal::Texture> addColor; //RGB - addColor
@@ -14,8 +52,6 @@ class Gbuffer {
std::shared_ptr<Gal::Texture> depthStencil;
std::shared_ptr<Gal::Framebuffer> geomFramebuffer;
- std::shared_ptr<Gal::Texture> finalColor;
-
public:
Gbuffer(size_t geomW, size_t geomH, size_t lightW, size_t lightH);
@@ -24,22 +60,20 @@ public:
}
std::shared_ptr<Gal::Texture> GetFinalTexture() {
- return finalColor;
+ return lightingPass->GetResultTexture();
}
void Render() {
- lightPipeline->Activate();
- lightPipelineInstance->Activate();
- lightPipelineInstance->Render(0, 6);
+ lightingPass->Render();
}
void Clear() {
geomFramebuffer->Clear();
- lightFramebuffer->Clear();
+ lightingPass->Clear();
}
void SetDayTime(float dayTime) {
- lightPipeline->SetShaderParameter("dayTime", dayTime);
+ lightingPass->SetShaderParameter("dayTime", dayTime);
}
int GetMaxRenderBuffers() {
@@ -47,6 +81,6 @@ public:
}
void SetRenderBuff(int renderBuff) {
- lightPipeline->SetShaderParameter("renderBuff", renderBuff);
+ lightingPass->SetShaderParameter("renderBuff", renderBuff);
}
};