feat: add gfx pipeline abstraction with render targets

Add cbt/gfx layer (pipeline + render_target) inspired by Sokol but
C++-friendly with RAII and pipeline_desc. Supports building full
graphics pipelines (VS/IA/raster/PS/OM), render-to-texture (FBO),
and post-processing steps (scene -> RT -> fullscreen quad with
sampler + vignette).

- Depth/state/viewport/texture cleanup for reliable scene switching.
- Updated cube/sphere to demonstrate (sphere uses RT+post).
- Vulkan backend easy via PIMPL in impl (same public API).
- Fixed depth test, viewport restore, and state leakage on switch.

Followed coding conventions (snake_case, trailing returns, east const,
include order, no ; after ns/class, etc.).
This commit is contained in:
2026-05-06 00:43:00 +02:00
parent 98673b57ff
commit 5ec8cfc735
7 changed files with 565 additions and 163 deletions
+44 -59
View File
@@ -1,8 +1,8 @@
#include <array>
#include <span>
#include "glad/glad.h"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "scenes/cube.hpp"
@@ -13,17 +13,17 @@ cube::cube() {
}
auto cube::init() -> bool {
if (!build_shader()) {
if (!build_pipeline()) {
return false;
}
build_mesh();
glEnable(GL_DEPTH_TEST);
return true;
}
auto cube::update(float) -> void {}
auto cube::render(int width, int height) -> void {
glViewport(0, 0, width, height);
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration<float>(now - m_start).count();
@@ -35,18 +35,33 @@ auto cube::render(int width, int height) -> void {
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -3.0f});
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.5f, 0.3f});
m_prog.use();
glUniformMatrix4fv(m_loc_proj, 1, GL_FALSE, glm::value_ptr(proj));
glUniformMatrix4fv(m_loc_view, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(m_loc_model, 1, GL_FALSE, glm::value_ptr(model));
m_vao.bind();
glDrawArrays(GL_TRIANGLES, 0, 36);
m_vao.unbind();
m_prog.unuse();
m_pipeline.draw(model, view, proj);
}
auto cube::build_mesh() -> void {
auto cube::build_pipeline() -> bool {
char const* vert_src = R"glsl(
#version 410 core
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec3 a_color;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
out vec3 v_color;
void main() {
gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);
v_color = a_color;
}
)glsl";
char const* frag_src = R"glsl(
#version 410 core
in vec3 v_color;
out vec4 frag_color;
void main() {
frag_color = vec4(v_color, 1.0);
}
)glsl";
std::array<float, 36 * 6> vertices = {
// front face (cyan)
0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f,
@@ -97,52 +112,22 @@ auto cube::build_mesh() -> void {
-0.5f, 0.5f, -0.5f, 1.0f, 0.5f, 0.0f,
};
m_vbo.upload(vertices.data(), vertices.size() * sizeof(float));
gfx::pipeline_desc desc{
.vertex_data = std::as_bytes(std::span{vertices}),
.attributes = {
{.location = 0, .num_components = 3, .offset = 0},
{.location = 1, .num_components = 3, .offset = 12},
},
.vertex_stride = 24,
.vertex_shader_src = vert_src,
.fragment_shader_src = frag_src,
.depth_test = true,
.primitive = gfx::primitive_type::triangles
// no index data
};
m_vao.bind();
m_vbo.bind();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float),
reinterpret_cast<void*>(3 * sizeof(float)));
m_vbo.unbind();
m_vao.unbind();
}
auto cube::build_shader() -> bool {
char const* vert_src = R"glsl(
#version 410 core
layout(location = 0) in vec3 a_pos;
layout(location = 1) in vec3 a_color;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
out vec3 v_color;
void main() {
gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);
v_color = a_color;
}
)glsl";
char const* frag_src = R"glsl(
#version 410 core
in vec3 v_color;
out vec4 frag_color;
void main() {
frag_color = vec4(v_color, 1.0);
}
)glsl";
if (!m_prog.compile_vertex(vert_src) || !m_prog.compile_fragment(frag_src) || !m_prog.link()) {
return false;
}
m_loc_proj = glGetUniformLocation(m_prog.id(), "u_proj");
m_loc_view = glGetUniformLocation(m_prog.id(), "u_view");
m_loc_model = glGetUniformLocation(m_prog.id(), "u_model");
return true;
m_pipeline = gfx::pipeline{desc};
return m_pipeline.valid();
}
}