[glutils] Add helper functions for setting up shaders
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
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::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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user