Skip to content

Commit

Permalink
Clean up renderer.
Browse files Browse the repository at this point in the history
  • Loading branch information
AnalogFeelings committed Aug 7, 2024
1 parent 84fd95a commit e246cc8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 74 deletions.
117 changes: 50 additions & 67 deletions src/glrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ auto GLRenderer::InitRenderer(INT viewportWidth, INT viewportHeight, CONST SETTI
glViewport(0, 0, this->ViewportWidth, this->ViewportHeight);

glGenVertexArrays(1, &this->QuadVao);
glObjectLabel(GL_VERTEX_ARRAY, this->QuadVao, 11, "Screen VAO");
this->LabelObject(GL_VERTEX_ARRAY, this->QuadVao, "Screen VAO");

glGenBuffers(1, &this->QuadVbo);
glObjectLabel(GL_BUFFER, this->QuadVbo, 11, "Screen VBO");
this->LabelObject(GL_BUFFER, this->QuadVbo, "Screen VBO");

glGenBuffers(1, &this->QuadEbo);
glObjectLabel(GL_BUFFER, this->QuadEbo, 11, "Screen EBO");
this->LabelObject(GL_BUFFER, this->QuadEbo, "Screen EBO");

glBindVertexArray(this->QuadVao);

Expand All @@ -106,83 +106,39 @@ auto GLRenderer::InitRenderer(INT viewportWidth, INT viewportHeight, CONST SETTI
// Generate framebuffers.
if (!settings.BufferAPath.empty())
{
glGenFramebuffers(1, &this->BufferAFramebuffer);
glObjectLabel(GL_FRAMEBUFFER, this->BufferAFramebuffer, 20, "BufferA Framebuffer");

glGenTextures(1, &this->BufferATexture);
glObjectLabel(GL_TEXTURE, this->BufferATexture, 16, "BufferA Texture");

glBindFramebuffer(GL_FRAMEBUFFER, this->BufferAFramebuffer);
glBindTexture(GL_TEXTURE_2D, this->BufferATexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewportWidth, viewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->BufferATexture, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
BOOL result = this->GenerateFramebuffer(&this->BufferAFramebuffer, &this->BufferATexture);
if (!result)
return FALSE;

this->LabelObject(GL_FRAMEBUFFER, this->BufferAFramebuffer, "BufferA Framebuffer");
this->LabelObject(GL_TEXTURE, this->BufferATexture, "BufferA Texture");
}
if (!settings.BufferBPath.empty())
{
glGenFramebuffers(1, &this->BufferBFramebuffer);
glObjectLabel(GL_FRAMEBUFFER, this->BufferBFramebuffer, 20, "BufferB Framebuffer");

glGenTextures(1, &this->BufferBTexture);
glObjectLabel(GL_TEXTURE, this->BufferBTexture, 16, "BufferB Texture");

glBindFramebuffer(GL_FRAMEBUFFER, this->BufferBFramebuffer);
glBindTexture(GL_TEXTURE_2D, this->BufferBTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewportWidth, viewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->BufferBTexture, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
BOOL result = this->GenerateFramebuffer(&this->BufferBFramebuffer, &this->BufferBTexture);
if (!result)
return FALSE;

this->LabelObject(GL_FRAMEBUFFER, this->BufferBFramebuffer, "BufferB Framebuffer");
this->LabelObject(GL_TEXTURE, this->BufferBTexture, "BufferB Texture");
}
if (!settings.BufferCPath.empty())
{
glGenFramebuffers(1, &this->BufferCFramebuffer);
glObjectLabel(GL_FRAMEBUFFER, this->BufferCFramebuffer, 20, "BufferC Framebuffer");

glGenTextures(1, &this->BufferCTexture);
glObjectLabel(GL_TEXTURE, this->BufferCTexture, 16, "BufferC Texture");

glBindFramebuffer(GL_FRAMEBUFFER, this->BufferCFramebuffer);
glBindTexture(GL_TEXTURE_2D, this->BufferCTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewportWidth, viewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->BufferCTexture, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
BOOL result = this->GenerateFramebuffer(&this->BufferCFramebuffer, &this->BufferCTexture);
if (!result)
return FALSE;

this->LabelObject(GL_FRAMEBUFFER, this->BufferCFramebuffer, "BufferC Framebuffer");
this->LabelObject(GL_TEXTURE, this->BufferCTexture, "BufferC Texture");
}
if (!settings.BufferDPath.empty())
{
glGenFramebuffers(1, &this->BufferDFramebuffer);
glObjectLabel(GL_FRAMEBUFFER, this->BufferDFramebuffer, 20, "BufferD Framebuffer");

glGenTextures(1, &this->BufferDTexture);
glObjectLabel(GL_TEXTURE, this->BufferDTexture, 16, "BufferD Texture");

glBindFramebuffer(GL_FRAMEBUFFER, this->BufferDFramebuffer);
glBindTexture(GL_TEXTURE_2D, this->BufferDTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, viewportWidth, viewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, this->BufferDTexture, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
BOOL result = this->GenerateFramebuffer(&this->BufferDFramebuffer, &this->BufferDTexture);
if (!result)
return FALSE;

this->LabelObject(GL_FRAMEBUFFER, this->BufferDFramebuffer, "BufferD Framebuffer");
this->LabelObject(GL_TEXTURE, this->BufferDTexture, "BufferD Texture");
}

UINT vertexSize;
Expand Down Expand Up @@ -457,6 +413,33 @@ auto GLRenderer::SetUniformValues(std::shared_ptr<Shader> target, CONST PUNIFORM
target->SetVector4Uniform("iDate", uniforms->Year, uniforms->Month, uniforms->Day, uniforms->Seconds);
}

auto GLRenderer::GenerateFramebuffer(PUINT targetFramebuffer, PUINT targetTexture) -> BOOL
{
glGenFramebuffers(1, targetFramebuffer);
glGenTextures(1, targetTexture);

glBindFramebuffer(GL_FRAMEBUFFER, *targetFramebuffer);
glBindTexture(GL_TEXTURE_2D, *targetTexture);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, this->ViewportWidth, this->ViewportHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *targetTexture, 0);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return FALSE;

return TRUE;
}

auto GLRenderer::LabelObject(UINT type, UINT object, PCSTR label) -> VOID
{
UINT stringLength = std::strlen(label);

glObjectLabel(type, object, stringLength, label);
}

auto GLRenderer::LoadFileFromResource(INT resourceId, UINT& size, PCSTR& data) -> BOOL
{
HMODULE moduleHandle = ::GetModuleHandle(nullptr);
Expand Down
14 changes: 8 additions & 6 deletions src/include/glrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ typedef struct
class GLRenderer
{
public:
std::shared_ptr<Shader> QuadShader;
std::shared_ptr<Shader> BufferAShader;
std::shared_ptr<Shader> BufferBShader;
std::shared_ptr<Shader> BufferCShader;
std::shared_ptr<Shader> BufferDShader;

auto InitContext(HWND hWnd, HDC& deviceContext, HGLRC& glRenderContext) -> BOOL;
auto InitRenderer(INT viewportWidth, INT viewportHeight, CONST SETTINGS& settings) -> BOOL;

auto DoRender(HDC deviceContext) -> VOID;
~GLRenderer();

private:
std::shared_ptr<Shader> QuadShader;
std::shared_ptr<Shader> BufferAShader;
std::shared_ptr<Shader> BufferBShader;
std::shared_ptr<Shader> BufferCShader;
std::shared_ptr<Shader> BufferDShader;

INT ViewportWidth = 0;
INT ViewportHeight = 0;
INT FrameCount = 0;
Expand Down Expand Up @@ -87,6 +87,8 @@ class GLRenderer
UINT Channel3Texture = 0;

auto SetUniformValues(std::shared_ptr<Shader> target, CONST PUNIFORMS uniforms) -> VOID;
auto GenerateFramebuffer(PUINT targetFramebuffer, PUINT targetTexture) -> BOOL;
auto LabelObject(UINT type, UINT object, PCSTR label) -> VOID;
auto LoadFileFromResource(INT resourceId, UINT& size, PCSTR& data) -> BOOL;
auto GuaranteeNullTermination(UINT size, CONST PCSTR& data) -> std::string;
auto LoadFileFromDisk(CONST std::string& filename) -> std::string;
Expand Down
2 changes: 1 addition & 1 deletion src/include/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
#include <GL/glew.h>
#include <string>
#include <sstream>
#include <boost/regex.hpp>
#include <memory>
#include <unordered_map>

#define SHADER_LOG_SIZE 1024
#define UNIFORM_BUFFER_SIZE 16
Expand Down

0 comments on commit e246cc8

Please sign in to comment.