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:
+44
-59
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+3
-12
@@ -5,9 +5,7 @@
|
||||
#include "glm/glm.hpp"
|
||||
|
||||
#include "cbt/scene.hpp"
|
||||
#include "cbt/opengl/buffer.hpp"
|
||||
#include "cbt/opengl/shader.hpp"
|
||||
#include "cbt/opengl/vao.hpp"
|
||||
#include "cbt/gfx.hpp"
|
||||
|
||||
namespace cbt::scenes {
|
||||
|
||||
@@ -19,18 +17,11 @@ public:
|
||||
auto render(int width, int height) -> void override;
|
||||
|
||||
private:
|
||||
opengl::shader m_prog;
|
||||
opengl::buffer m_vbo;
|
||||
opengl::vao m_vao;
|
||||
|
||||
GLint m_loc_proj = -1;
|
||||
GLint m_loc_view = -1;
|
||||
GLint m_loc_model = -1;
|
||||
gfx::pipeline m_pipeline;
|
||||
|
||||
std::chrono::steady_clock::time_point m_start;
|
||||
|
||||
auto build_mesh() -> void;
|
||||
auto build_shader() -> bool;
|
||||
auto build_pipeline() -> bool;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
+116
-76
@@ -1,10 +1,11 @@
|
||||
#include <cstddef>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <span>
|
||||
#include <array>
|
||||
|
||||
#include "glad/glad.h"
|
||||
#include "glm/gtc/matrix_transform.hpp"
|
||||
#include "glm/gtc/type_ptr.hpp"
|
||||
|
||||
#include "scenes/sphere.hpp"
|
||||
|
||||
@@ -15,11 +16,9 @@ sphere::sphere() {
|
||||
}
|
||||
|
||||
auto sphere::init() -> bool {
|
||||
if (!build_shader()) {
|
||||
if (!build_pipeline() || !build_post_pipeline()) {
|
||||
return false;
|
||||
}
|
||||
build_mesh();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -29,6 +28,12 @@ auto sphere::render(int width, int height) -> void {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration<float>(now - m_start).count();
|
||||
|
||||
m_rt.resize(width, height);
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
// Step 1: Render scene to texture (offscreen pass)
|
||||
m_rt.bind();
|
||||
glClearColor(0.15f, 0.15f, 0.2f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
@@ -37,18 +42,59 @@ auto sphere::render(int width, int height) -> void {
|
||||
auto view = glm::translate(glm::mat4{1.0f}, glm::vec3{0.0f, 0.0f, -4.0f});
|
||||
auto model = glm::rotate(glm::mat4{1.0f}, elapsed, glm::vec3{1.0f, 0.3f, 0.2f});
|
||||
|
||||
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_scene_pipeline.draw(model, view, proj);
|
||||
m_rt.unbind();
|
||||
|
||||
m_vao.bind();
|
||||
glDrawElements(GL_TRIANGLES, m_index_count, GL_UNSIGNED_INT, nullptr);
|
||||
m_vao.unbind();
|
||||
m_prog.unuse();
|
||||
// Reset viewport for screen pass (RT bind changed it)
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
// Step 2: Post-processing step (sample RT texture, apply effect, render to screen)
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
m_post_pipeline.bind_texture("u_texture", m_rt.color_id(), 0);
|
||||
m_post_pipeline.draw(glm::mat4{1.0f}, glm::mat4{1.0f}, glm::mat4{1.0f});
|
||||
}
|
||||
|
||||
auto sphere::build_mesh() -> void {
|
||||
auto sphere::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_normal;
|
||||
layout(location = 2) in vec2 a_uv;
|
||||
layout(location = 3) in vec3 a_color;
|
||||
uniform mat4 u_model;
|
||||
uniform mat4 u_view;
|
||||
uniform mat4 u_proj;
|
||||
out vec3 v_normal;
|
||||
out vec3 v_world_pos;
|
||||
out vec2 v_uv;
|
||||
out vec3 v_color;
|
||||
void main() {
|
||||
gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);
|
||||
v_normal = mat3(u_model) * a_normal;
|
||||
v_world_pos = (u_model * vec4(a_pos, 1.0)).xyz;
|
||||
v_uv = a_uv;
|
||||
v_color = a_color;
|
||||
}
|
||||
)glsl";
|
||||
|
||||
char const* frag_src = R"glsl(
|
||||
#version 410 core
|
||||
in vec3 v_normal;
|
||||
in vec3 v_world_pos;
|
||||
in vec2 v_uv;
|
||||
in vec3 v_color;
|
||||
out vec4 frag_color;
|
||||
void main() {
|
||||
vec3 light_dir = normalize(vec3(1.0, 1.0, 2.0));
|
||||
vec3 normal = normalize(v_normal);
|
||||
float diff = max(dot(normal, light_dir), 0.0);
|
||||
vec3 ambient = vec3(0.2);
|
||||
vec3 result = (ambient + diff * 0.8) * v_color;
|
||||
frag_color = vec4(result, 1.0);
|
||||
}
|
||||
)glsl";
|
||||
|
||||
struct vertex {
|
||||
glm::vec3 position;
|
||||
glm::vec3 normal;
|
||||
@@ -127,88 +173,82 @@ auto sphere::build_mesh() -> void {
|
||||
offset += div * div;
|
||||
}
|
||||
|
||||
m_index_count = static_cast<GLsizei>(indices.size());
|
||||
gfx::pipeline_desc desc{
|
||||
.vertex_data = std::as_bytes(std::span{vertices}),
|
||||
.index_data = std::as_bytes(std::span{indices}),
|
||||
.attributes = {
|
||||
{.location = 0, .num_components = 3, .offset = 0},
|
||||
{.location = 1, .num_components = 3, .offset = 12},
|
||||
{.location = 2, .num_components = 2, .offset = 24},
|
||||
{.location = 3, .num_components = 3, .offset = 32},
|
||||
},
|
||||
.vertex_stride = sizeof(vertex),
|
||||
.vertex_shader_src = vert_src,
|
||||
.fragment_shader_src = frag_src,
|
||||
.depth_test = true,
|
||||
.primitive = gfx::primitive_type::triangles,
|
||||
.index_type_ = gfx::index_type::uint32
|
||||
};
|
||||
|
||||
m_vbo.upload(vertices.data(), vertices.size() * sizeof(vertex));
|
||||
m_ebo.upload(indices.data(), indices.size() * sizeof(std::uint32_t));
|
||||
|
||||
m_vao.bind();
|
||||
m_vbo.bind();
|
||||
m_ebo.bind();
|
||||
|
||||
// location 0: position (vec3)
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vertex), nullptr);
|
||||
|
||||
// location 1: normal (vec3)
|
||||
glEnableVertexAttribArray(1);
|
||||
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(vertex),
|
||||
reinterpret_cast<void*>(3 * sizeof(float)));
|
||||
|
||||
// location 2: uv (vec2)
|
||||
glEnableVertexAttribArray(2);
|
||||
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
|
||||
reinterpret_cast<void*>(6 * sizeof(float)));
|
||||
|
||||
// location 3: color (vec3)
|
||||
glEnableVertexAttribArray(3);
|
||||
glVertexAttribPointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(vertex),
|
||||
reinterpret_cast<void*>(8 * sizeof(float)));
|
||||
|
||||
m_vao.unbind();
|
||||
m_vbo.unbind();
|
||||
m_ebo.unbind();
|
||||
m_scene_pipeline = gfx::pipeline{desc};
|
||||
return m_scene_pipeline.valid();
|
||||
}
|
||||
|
||||
auto sphere::build_shader() -> bool {
|
||||
char const* vert_src = R"glsl(
|
||||
auto sphere::build_post_pipeline() -> bool {
|
||||
char const* post_vert = R"glsl(
|
||||
#version 410 core
|
||||
layout(location = 0) in vec3 a_pos;
|
||||
layout(location = 1) in vec3 a_normal;
|
||||
layout(location = 2) in vec2 a_uv;
|
||||
layout(location = 3) in vec3 a_color;
|
||||
uniform mat4 u_model;
|
||||
uniform mat4 u_view;
|
||||
uniform mat4 u_proj;
|
||||
out vec3 v_normal;
|
||||
out vec3 v_world_pos;
|
||||
layout(location = 0) in vec2 a_pos;
|
||||
layout(location = 1) in vec2 a_uv;
|
||||
out vec2 v_uv;
|
||||
out vec3 v_color;
|
||||
void main() {
|
||||
gl_Position = u_proj * u_view * u_model * vec4(a_pos, 1.0);
|
||||
v_normal = mat3(u_model) * a_normal;
|
||||
v_world_pos = (u_model * vec4(a_pos, 1.0)).xyz;
|
||||
gl_Position = vec4(a_pos, 0.0, 1.0);
|
||||
v_uv = a_uv;
|
||||
v_color = a_color;
|
||||
}
|
||||
)glsl";
|
||||
|
||||
char const* frag_src = R"glsl(
|
||||
char const* post_frag = R"glsl(
|
||||
#version 410 core
|
||||
in vec3 v_normal;
|
||||
in vec3 v_world_pos;
|
||||
in vec2 v_uv;
|
||||
in vec3 v_color;
|
||||
uniform sampler2D u_texture;
|
||||
out vec4 frag_color;
|
||||
void main() {
|
||||
vec3 light_dir = normalize(vec3(1.0, 1.0, 2.0));
|
||||
vec3 normal = normalize(v_normal);
|
||||
float diff = max(dot(normal, light_dir), 0.0);
|
||||
vec3 ambient = vec3(0.2);
|
||||
vec3 result = (ambient + diff * 0.8) * v_color;
|
||||
frag_color = vec4(result, 1.0);
|
||||
vec3 col = texture(u_texture, v_uv).rgb;
|
||||
// simple processing: vignette only (preserves original colors from sphere faces)
|
||||
float vig = 1.0 - length(v_uv * 2.0 - 1.0) * 0.5;
|
||||
frag_color = vec4(col * vig, 1.0);
|
||||
}
|
||||
)glsl";
|
||||
|
||||
if (!m_prog.compile_vertex(vert_src) || !m_prog.compile_fragment(frag_src) || !m_prog.link()) {
|
||||
return false;
|
||||
}
|
||||
struct fs_vertex {
|
||||
glm::vec2 pos;
|
||||
glm::vec2 uv;
|
||||
};
|
||||
|
||||
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");
|
||||
std::array<fs_vertex, 4> qverts = {{
|
||||
{{-1.0f, -1.0f}, {0.0f, 0.0f}},
|
||||
{{ 1.0f, -1.0f}, {1.0f, 0.0f}},
|
||||
{{ 1.0f, 1.0f}, {1.0f, 1.0f}},
|
||||
{{-1.0f, 1.0f}, {0.0f, 1.0f}},
|
||||
}};
|
||||
std::array<std::uint32_t, 6> qinds = {0, 1, 2, 0, 2, 3};
|
||||
|
||||
return true;
|
||||
gfx::pipeline_desc desc{
|
||||
.vertex_data = std::as_bytes(std::span{qverts}),
|
||||
.index_data = std::as_bytes(std::span{qinds}),
|
||||
.attributes = {
|
||||
{.location = 0, .num_components = 2, .offset = 0},
|
||||
{.location = 1, .num_components = 2, .offset = 8},
|
||||
},
|
||||
.vertex_stride = sizeof(fs_vertex),
|
||||
.vertex_shader_src = post_vert,
|
||||
.fragment_shader_src = post_frag,
|
||||
.depth_test = false,
|
||||
.primitive = gfx::primitive_type::triangles,
|
||||
.index_type_ = gfx::index_type::uint32
|
||||
};
|
||||
|
||||
m_post_pipeline = gfx::pipeline{desc};
|
||||
return m_post_pipeline.valid();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+6
-15
@@ -5,9 +5,7 @@
|
||||
#include "glm/glm.hpp"
|
||||
|
||||
#include "cbt/scene.hpp"
|
||||
#include "cbt/opengl/buffer.hpp"
|
||||
#include "cbt/opengl/shader.hpp"
|
||||
#include "cbt/opengl/vao.hpp"
|
||||
#include "cbt/gfx.hpp"
|
||||
|
||||
namespace cbt::scenes {
|
||||
|
||||
@@ -19,21 +17,14 @@ public:
|
||||
auto render(int width, int height) -> void override;
|
||||
|
||||
private:
|
||||
opengl::shader m_prog;
|
||||
opengl::buffer m_vbo;
|
||||
opengl::buffer m_ebo{opengl::buffer_type::index};
|
||||
opengl::vao m_vao;
|
||||
|
||||
GLint m_loc_proj = -1;
|
||||
GLint m_loc_view = -1;
|
||||
GLint m_loc_model = -1;
|
||||
|
||||
GLsizei m_index_count = 0;
|
||||
gfx::pipeline m_scene_pipeline;
|
||||
gfx::pipeline m_post_pipeline;
|
||||
gfx::render_target m_rt{0, 0};
|
||||
|
||||
std::chrono::steady_clock::time_point m_start;
|
||||
|
||||
auto build_mesh() -> void;
|
||||
auto build_shader() -> bool;
|
||||
auto build_pipeline() -> bool;
|
||||
auto build_post_pipeline() -> bool;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user