Tue, 11 Apr 2023 22:51:12 +0300
BasicShader is no longer a class
--- a/src/gl/basicshaderprogram.cpp Tue Apr 11 22:39:18 2023 +0300 +++ b/src/gl/basicshaderprogram.cpp Tue Apr 11 22:51:12 2023 +0300 @@ -18,94 +18,77 @@ #include "src/gl/basicshaderprogram.h" -BasicShader::BasicShader() : - buffer{QOpenGLBuffer::VertexBuffer}, - vertexShader{QOpenGLShader::Vertex}, - fragmentShader{QOpenGLShader::Fragment} -{ -} - -BasicShader::~BasicShader() -{ - if (this->isInitialized) { - this->teardown(); - } -} - -void BasicShader::initialize( +void gl::initialize_basic_shader( + gl::basic_shader* shader, const char* vertexShaderSource, const char* fragmentShaderSource, QOpenGLBuffer::UsagePattern usagePattern, const std::vector<GLAttributeSpec>& attributeSpecs) { - if (not this->isInitialized) - { - this->isInitialized = true; - this->program = std::make_unique<gl::ShaderProgram>(); - gl::buildShaders(this->program.get(), vertexShaderSource, fragmentShaderSource); - this->program->bind(); - this->buffer.create(); - this->buffer.bind(); - this->buffer.setUsagePattern(usagePattern); - this->vertexArrayObject.create(); - this->vertexArrayObject.bind(); - for (std::size_t i = 0; i < attributeSpecs.size(); ++i) { - const auto& spec = attributeSpecs[i]; - const int attr = narrow<int>(signed_cast(i)); - this->program->enableAttributeArray(attr); - this->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride); - } - this->vertexArrayObject.release(); - this->buffer.release(); - this->program->release(); - gl::checkForGLErrors(nullptr); + shader->program = std::make_unique<gl::ShaderProgram>(); + gl::buildShaders(shader->program.get(), vertexShaderSource, fragmentShaderSource); + shader->program->bind(); + shader->buffer.create(); + shader->buffer.bind(); + shader->buffer.setUsagePattern(usagePattern); + shader->vertexArrayObject.create(); + shader->vertexArrayObject.bind(); + for (std::size_t i = 0; i < attributeSpecs.size(); ++i) { + const auto& spec = attributeSpecs[i]; + const int attr = narrow<int>(signed_cast(i)); + shader->program->enableAttributeArray(attr); + shader->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride); } + shader->vertexArrayObject.release(); + shader->buffer.release(); + shader->program->release(); + gl::checkForGLErrors(nullptr); } -void BasicShader::setMvpMatrix(const glm::mat4& newMvpMatrix) +void gl::set_shader_matrix_uniform( + gl::basic_shader* shader, + const char* name, + const glm::mat4& value) { - this->setUniformMatrix("mvp", newMvpMatrix); -} - -void BasicShader::setUniformMatrix(const char* name, const glm::mat4& value) -{ - Q_ASSERT(this->isInitialized); - this->program->bind(); - this->program->setUniformMatrix(name, value); - this->program->release(); + Q_ASSERT(shader->program != nullptr); + shader->program->bind(); + shader->program->setUniformMatrix(name, value); + shader->program->release(); gl::checkForGLErrors(nullptr); } -void BasicShader::setUniformVector(const char* name, const glm::vec4& value) +void gl::set_shader_vector_uniform( + gl::basic_shader* shader, + const char* name, + const glm::vec4& value) { - Q_ASSERT(this->isInitialized); - this->program->bind(); - this->program->setUniformVector(name, value); - this->program->release(); + Q_ASSERT(shader->program != nullptr); + shader->program->bind(); + shader->program->setUniformVector(name, value); + shader->program->release(); gl::checkForGLErrors(nullptr); } -void BasicShader::bufferData(const void* data, std::size_t count, std::size_t size) +void gl::buffer_shader_data( + gl::basic_shader* shader, + const void* data, + std::size_t count, + std::size_t size) { - this->buffer.bind(); - this->buffer.allocate(data, narrow<int>(signed_cast(count * size))); - this->buffer.release(); - this->vertexCount = narrow<int>(signed_cast(count)); + shader->buffer.bind(); + shader->buffer.allocate(data, narrow<int>(signed_cast(count * size))); + shader->buffer.release(); + shader->vertexCount = narrow<int>(signed_cast(count)); } -void BasicShader::draw(GLenum drawMode) +void gl::draw_shader( + gl::basic_shader* shader, + GLenum drawMode) { - this->program->bind(); - this->vertexArrayObject.bind(); - glDrawArrays(drawMode, 0, this->vertexCount); - this->vertexArrayObject.release(); - this->program->release(); + shader->program->bind(); + shader->vertexArrayObject.bind(); + glDrawArrays(drawMode, 0, shader->vertexCount); + shader->vertexArrayObject.release(); + shader->program->release(); gl::checkForGLErrors(nullptr); } - -void BasicShader::teardown() -{ - this->vertexArrayObject.destroy(); - this->buffer.destroy(); - this->program.reset(); -}
--- a/src/gl/basicshaderprogram.h Tue Apr 11 22:39:18 2023 +0300 +++ b/src/gl/basicshaderprogram.h Tue Apr 11 22:51:12 2023 +0300 @@ -9,30 +9,43 @@ int stride; }; -//! @brief A collection of a single VAO with a single VBO, -//! a vertex shader and a fragment shader. -class BasicShader final +namespace gl { - bool isInitialized = false; - QOpenGLBuffer buffer; - QOpenGLShader vertexShader; - QOpenGLShader fragmentShader; - std::unique_ptr<gl::ShaderProgram> program = nullptr; - QOpenGLVertexArrayObject vertexArrayObject; - int vertexCount = 0; -public: - BasicShader(); - ~BasicShader(); - Q_DISABLE_COPY(BasicShader) - void initialize( + struct basic_shader; + void initialize_basic_shader( + gl::basic_shader* shader, const char* vertexShaderSource, const char* fragmentShaderSource, QOpenGLBuffer::UsagePattern usagePattern, const std::vector<GLAttributeSpec>& attributeSpecs); - void setUniformMatrix(const char* name, const glm::mat4& value); - void setUniformVector(const char* name, const glm::vec4& value); - void setMvpMatrix(const glm::mat4& newMvpMatrix); - void bufferData(const void* data, std::size_t count, std::size_t size); - void draw(GLenum drawMode); - void teardown(); + void set_shader_matrix_uniform( + gl::basic_shader* shader, + const char* name, + const glm::mat4& value); + void set_shader_vector_uniform( + gl::basic_shader* shader, + const char* name, + const glm::vec4& value); + void buffer_shader_data( + gl::basic_shader* shader, + const void* data, + std::size_t count, + std::size_t size); + void draw_shader( + gl::basic_shader* shader, + GLenum drawMode); +} + +using BasicShader = gl::basic_shader; + +//! @brief A collection of a single VAO with a single VBO, +//! a vertex shader and a fragment shader. +struct gl::basic_shader +{ + QOpenGLBuffer buffer{QOpenGLBuffer::VertexBuffer}; + QOpenGLShader vertexShader{QOpenGLShader::Vertex}; + QOpenGLShader fragmentShader{QOpenGLShader::Fragment}; + std::unique_ptr<gl::ShaderProgram> program = nullptr; + QOpenGLVertexArrayObject vertexArrayObject; + int vertexCount = 0; };
--- a/src/layers/axeslayer.cpp Tue Apr 11 22:39:18 2023 +0300 +++ b/src/layers/axeslayer.cpp Tue Apr 11 22:51:12 2023 +0300 @@ -69,7 +69,7 @@ {{0, 0, 0}, {0, 0, 0.5}}, }; constexpr int stride = sizeof(VertexType); - this->shader.initialize( + gl::initialize_basic_shader(&this->shader, ::vertexShaderSource, ::fragmentShaderSource, QOpenGLBuffer::StaticDraw, @@ -87,7 +87,7 @@ .stride = stride, }, }); - this->shader.bufferData(&data[0], countof(data), sizeof data[0]); + gl::buffer_shader_data(&this->shader, &data[0], countof(data), sizeof data[0]); } void AxesLayer::overpaint(QPainter* painter) @@ -152,11 +152,11 @@ glLineWidth(5); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); - this->shader.draw(GL_LINES); + gl::draw_shader(&this->shader, GL_LINES); glDisable(GL_LINE_SMOOTH); } void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix) { - this->shader.setMvpMatrix(mvpMatrix); + gl::set_shader_matrix_uniform(&this->shader, "mvp", mvpMatrix); }
--- a/src/layers/gridlayer.cpp Tue Apr 11 22:39:18 2023 +0300 +++ b/src/layers/gridlayer.cpp Tue Apr 11 22:51:12 2023 +0300 @@ -71,7 +71,7 @@ { this->gridMatrix = newGridMatrix; if (this->isInitialized) { - this->shader.setUniformMatrix("grid", newGridMatrix); + gl::set_shader_matrix_uniform(&this->shader, "grid", newGridMatrix); } } @@ -79,7 +79,7 @@ { this->gridColor = gl::colorToVector4(newGridColor); if (this->isInitialized) { - this->shader.setUniformVector("gridColor", this->gridColor); + gl::set_shader_vector_uniform(&this->shader, "gridColor", this->gridColor); } } @@ -90,7 +90,8 @@ void GridLayer::initializeGL() { - this->shader.initialize( + gl::initialize_basic_shader( + &this->shader, ::vertexShaderSource, ::fragmentShaderSource, QOpenGLBuffer::StaticDraw, @@ -105,10 +106,10 @@ ); this->isInitialized = true; constexpr auto data = calcGridData<50>(); - this->shader.setUniformVector("gridColor", this->gridColor); + gl::set_shader_vector_uniform(&this->shader, "gridColor", this->gridColor); this->setGridMatrix(this->gridMatrix); this->settingsChanged(); - this->shader.bufferData(data.data(), data.size(), sizeof data[0]); + gl::buffer_shader_data(&this->shader, data.data(), data.size(), sizeof data[0]); } void GridLayer::paintGL() @@ -118,12 +119,12 @@ glLineStipple(1, 0x8888); glEnable(GL_LINE_STIPPLE); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - this->shader.draw(GL_LINES); + gl::draw_shader(&this->shader, GL_LINES); glDisable(GL_BLEND); glDisable(GL_LINE_STIPPLE); } void GridLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix) { - this->shader.setMvpMatrix(mvpMatrix); + gl::set_shader_matrix_uniform(&this->shader, "mvp", mvpMatrix); }