From aa8200beedd993b5cd5c8c9cefe338e601451f45 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 27 Jan 2024 23:39:03 -0600 Subject: [PATCH] [glutils] Add helper functions for setting up shaders --- deps/glutils/include/glutils/glutils.hpp | 34 ++++++++++++++++- deps/glutils/src/glutils.cpp | 48 +++++++++++++++++++++--- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/deps/glutils/include/glutils/glutils.hpp b/deps/glutils/include/glutils/glutils.hpp index 037c6b4e..da55cfa4 100644 --- a/deps/glutils/include/glutils/glutils.hpp +++ b/deps/glutils/include/glutils/glutils.hpp @@ -150,8 +150,38 @@ class FrameBufferBind { void bind(const FrameBuffer &fb) noexcept; +struct ShaderVarSet { + GLsizei len{}; + ox::String name; +}; -ox::Result buildShaderProgram(ox::CStringView const&vert, ox::CStringView const&frag, ox::CStringView const&geo = "") noexcept; +struct ProgramSource { + ox::Vector const shaderParams; + GLsizei const rowLen = [this] { + GLsizei len{}; + for (auto const&v : shaderParams) { + len += v.len; + } + return len; + }(); + ox::String const vertShader{}; + ox::String const fragShader{}; + ox::String const geomShader{}; +}; + +ox::Result buildShaderProgram(ProgramSource const&src) noexcept; + +ox::Result buildShaderProgram( + ox::CStringView const&vert, + ox::CStringView const&frag, + ox::CStringView const&geo = "") noexcept; + +void setupShaderParams( + GLProgram const&shader, + ox::Vector const&vars, + GLsizei vertexRowLen) noexcept; + +void setupShaderParams(GLProgram const&shader, ox::Vector const&vars) noexcept; glutils::GLVertexArray generateVertexArrayObject() noexcept; @@ -160,6 +190,8 @@ glutils::GLBuffer generateBuffer() noexcept; [[nodiscard]] FrameBuffer generateFrameBuffer(int width, int height) noexcept; +void resizeFrameBuffer(FrameBuffer &fb, int width, int height) noexcept; + /** * Resizes a FrameBuffer, and creates if it does not already exist. */ diff --git a/deps/glutils/src/glutils.cpp b/deps/glutils/src/glutils.cpp index bd1ca0c8..f1eaf0cf 100644 --- a/deps/glutils/src/glutils.cpp +++ b/deps/glutils/src/glutils.cpp @@ -88,6 +88,40 @@ static ox::Result buildShader( return shader; } +ox::Result buildShaderProgram(ProgramSource const&src) noexcept { + oxRequireM(program, buildShaderProgram( + src.vertShader, + src.fragShader, + src.geomShader)); + setupShaderParams(program, src.shaderParams, src.rowLen); + return std::move(program); +} + +void setupShaderParams( + GLProgram const&shader, + ox::Vector const&vars, + GLsizei vertexRowLen) noexcept { + // setup vars + for (auto lenWritten = 0LU; auto const&v : vars) { + auto const attr = static_cast(glGetAttribLocation(shader, v.name.c_str())); + glEnableVertexAttribArray(attr); + glVertexAttribPointer( + attr, v.len, GL_FLOAT, GL_FALSE, + vertexRowLen * static_cast(sizeof(float)), + std::bit_cast(uintptr_t{lenWritten * sizeof(float)})); + lenWritten += static_cast(v.len); + } +} + +void setupShaderParams(GLProgram const&shader, ox::Vector const&vars) noexcept { + // get row len + GLsizei vertexRowLen{}; + for (auto const&v : vars) { + vertexRowLen += v.len; + } + setupShaderParams(shader, vars, vertexRowLen); +} + ox::Result buildShaderProgram( ox::CStringView const&vert, ox::CStringView const&frag, @@ -147,11 +181,7 @@ FrameBuffer generateFrameBuffer(int width, int height) noexcept { return fb; } -void resizeInitFrameBuffer(FrameBuffer &fb, int width, int height) noexcept { - if (!fb) { - fb = generateFrameBuffer(width, height); - return; - } +void resizeFrameBuffer(FrameBuffer &fb, int width, int height) noexcept { width = ox::max(1, width); height = ox::max(1, height); fb.width = width; @@ -171,6 +201,14 @@ void resizeInitFrameBuffer(FrameBuffer &fb, int width, int height) noexcept { glBindRenderbuffer(GL_RENDERBUFFER, 0); } +void resizeInitFrameBuffer(FrameBuffer &fb, int width, int height) noexcept { + if (!fb) { + fb = generateFrameBuffer(width, height); + return; + } + resizeFrameBuffer(fb, width, height); +} + void resizeInitFrameBuffer(FrameBuffer &fb, ox::Size const&sz) noexcept { resizeInitFrameBuffer(fb, sz.width, sz.height); }