[glutils] Add helper functions for setting up shaders
This commit is contained in:
parent
124c7029bd
commit
aa8200beed
34
deps/glutils/include/glutils/glutils.hpp
vendored
34
deps/glutils/include/glutils/glutils.hpp
vendored
@ -150,8 +150,38 @@ class FrameBufferBind {
|
|||||||
|
|
||||||
void bind(const FrameBuffer &fb) noexcept;
|
void bind(const FrameBuffer &fb) noexcept;
|
||||||
|
|
||||||
|
struct ShaderVarSet {
|
||||||
|
GLsizei len{};
|
||||||
|
ox::String name;
|
||||||
|
};
|
||||||
|
|
||||||
ox::Result<GLProgram> buildShaderProgram(ox::CStringView const&vert, ox::CStringView const&frag, ox::CStringView const&geo = "") noexcept;
|
struct ProgramSource {
|
||||||
|
ox::Vector<glutils::ShaderVarSet> 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<GLProgram> buildShaderProgram(ProgramSource const&src) noexcept;
|
||||||
|
|
||||||
|
ox::Result<GLProgram> buildShaderProgram(
|
||||||
|
ox::CStringView const&vert,
|
||||||
|
ox::CStringView const&frag,
|
||||||
|
ox::CStringView const&geo = "") noexcept;
|
||||||
|
|
||||||
|
void setupShaderParams(
|
||||||
|
GLProgram const&shader,
|
||||||
|
ox::Vector<ShaderVarSet> const&vars,
|
||||||
|
GLsizei vertexRowLen) noexcept;
|
||||||
|
|
||||||
|
void setupShaderParams(GLProgram const&shader, ox::Vector<ShaderVarSet> const&vars) noexcept;
|
||||||
|
|
||||||
glutils::GLVertexArray generateVertexArrayObject() noexcept;
|
glutils::GLVertexArray generateVertexArrayObject() noexcept;
|
||||||
|
|
||||||
@ -160,6 +190,8 @@ glutils::GLBuffer generateBuffer() noexcept;
|
|||||||
[[nodiscard]]
|
[[nodiscard]]
|
||||||
FrameBuffer generateFrameBuffer(int width, int height) noexcept;
|
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.
|
* Resizes a FrameBuffer, and creates if it does not already exist.
|
||||||
*/
|
*/
|
||||||
|
48
deps/glutils/src/glutils.cpp
vendored
48
deps/glutils/src/glutils.cpp
vendored
@ -88,6 +88,40 @@ static ox::Result<GLShader> buildShader(
|
|||||||
return shader;
|
return shader;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ox::Result<GLProgram> 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<ShaderVarSet> const&vars,
|
||||||
|
GLsizei vertexRowLen) noexcept {
|
||||||
|
// setup vars
|
||||||
|
for (auto lenWritten = 0LU; auto const&v : vars) {
|
||||||
|
auto const attr = static_cast<GLuint>(glGetAttribLocation(shader, v.name.c_str()));
|
||||||
|
glEnableVertexAttribArray(attr);
|
||||||
|
glVertexAttribPointer(
|
||||||
|
attr, v.len, GL_FLOAT, GL_FALSE,
|
||||||
|
vertexRowLen * static_cast<GLsizei>(sizeof(float)),
|
||||||
|
std::bit_cast<void*>(uintptr_t{lenWritten * sizeof(float)}));
|
||||||
|
lenWritten += static_cast<size_t>(v.len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setupShaderParams(GLProgram const&shader, ox::Vector<ShaderVarSet> const&vars) noexcept {
|
||||||
|
// get row len
|
||||||
|
GLsizei vertexRowLen{};
|
||||||
|
for (auto const&v : vars) {
|
||||||
|
vertexRowLen += v.len;
|
||||||
|
}
|
||||||
|
setupShaderParams(shader, vars, vertexRowLen);
|
||||||
|
}
|
||||||
|
|
||||||
ox::Result<GLProgram> buildShaderProgram(
|
ox::Result<GLProgram> buildShaderProgram(
|
||||||
ox::CStringView const&vert,
|
ox::CStringView const&vert,
|
||||||
ox::CStringView const&frag,
|
ox::CStringView const&frag,
|
||||||
@ -147,11 +181,7 @@ FrameBuffer generateFrameBuffer(int width, int height) noexcept {
|
|||||||
return fb;
|
return fb;
|
||||||
}
|
}
|
||||||
|
|
||||||
void resizeInitFrameBuffer(FrameBuffer &fb, int width, int height) noexcept {
|
void resizeFrameBuffer(FrameBuffer &fb, int width, int height) noexcept {
|
||||||
if (!fb) {
|
|
||||||
fb = generateFrameBuffer(width, height);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
width = ox::max(1, width);
|
width = ox::max(1, width);
|
||||||
height = ox::max(1, height);
|
height = ox::max(1, height);
|
||||||
fb.width = width;
|
fb.width = width;
|
||||||
@ -171,6 +201,14 @@ void resizeInitFrameBuffer(FrameBuffer &fb, int width, int height) noexcept {
|
|||||||
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
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 {
|
void resizeInitFrameBuffer(FrameBuffer &fb, ox::Size const&sz) noexcept {
|
||||||
resizeInitFrameBuffer(fb, sz.width, sz.height);
|
resizeInitFrameBuffer(fb, sz.width, sz.height);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user