Feature/graphic/rhi - #67
Conversation
…om GL - Add include/gkit/graphic/config.hpp with backend-agnostic enums (ClearFlags/CompareFunc/BlendFunc/BlendEquation/CullFaceMode/FrontFace/ StencilOp/TextureType) + SCR_WIDTH/SCR_HEIGHT - Rewrite opengl/config.hpp as GL mapping layer: to_gl_* converter funcs - Update Renderer/StateManager/Texture/test_window to use frontend enums - StateManager applies states via to_gl_* mapping instead of raw cast
…ckend classes - Add frontend abstract classes in include/gkit/graphic/: Buffer, VertexBuffer, IndexBuffer, VertexArray, Shader, Texture, FrameBuffer, RenderBuffer, VertexBufferLayout, config - opengl:: classes now inherit frontend interfaces; GL handles stay private - Move Shader to opengl::Shader (gkit::graphic::Shader is now abstract) - Move VertexBufferLayout to frontend; delete opengl::buffer:: namespace - Renderer::draw takes frontend abstract types - Update CMakeLists and test_window to new layout
…ckend - Add frontend RenderDevice interface (resource factory + render entry) - Add Backend enum and create_device() single switch point - Add opengl::Device implementing RenderDevice - create_texture left unimplemented pending resource module - Wire new sources into CMakeLists
- Renderer holds unique_ptr<RenderDevice>, init(Backend) selects backend - clear/draw/draw_instance delegate to device; get_device() accessor - test_window creates resources via device factory, calls renderer.init() - clang-tidy: rename Buffer::size_ -> size, drop default arg on virtual create_vertex_buffer (base class), update all references - clang-format: normalize all graphic headers/sources to project style
- Replace Chinese comments in graphic frontend/backend headers, sources, and test with English equivalents - Remove default arg on opengl::Device::create_vertex_buffer override (google-default-arguments)
…ngle test - Texture: create a real GL texture with storage for TextureFramebuffer type so the FBO color attachment is valid (fixes black screenTexture output) - Renderer: get_device() lazily creates the default OpenGL device, so callers no longer have to call init() first to avoid a null dereference; clear/draw delegate through it as well - test_window: replace the textured square with a centered colored triangle (position + color), add color_triangle.shader; drop main_texture usage
- Convert /// @brief one-liners and '/** @brief X */' single-line blocks to three-line doxygen blocks across graphic headers - Fix block indentation to match clang-format output (double-tab style), removing stray blank lines inside comment blocks - StateManager.hpp: drop redundant opengl/config.hpp include; move it to StateManager.cpp where to_gl_* is used - test_window.cpp: replace unused StateManager include with opengl/config.hpp for viewport; drop unused state_manager variable
feature/graphic/RHI 分支注意事项
1. 架构现状(一句话)graphic 模块已拆成 前端抽象层( 2. 临时占位(重要,别当正式实现用)
3. 使用指南3.1 创建资源:走 device 工厂(新方式)auto& renderer = gkit::graphic::Renderer::instance();
renderer.init(); // 可省略,get_device() 会自动默认 OpenGL
auto& device = renderer.get_device();
auto vao = device.create_vertex_array();
auto vbo = device.create_vertex_buffer(data, size, /*dynamic=*/false);
auto ibo = device.create_index_buffer(indices, count);
auto shader = device.create_shader("test/graphic/xxx.shader");
auto fbo = device.create_frame_buffer(w, h);
auto rbo = device.create_render_buffer(w, h);资源由 3.2 渲染renderer.clear(gkit::graphic::ClearFlags::All);
shader->bind();
renderer.draw(*vao, *ibo, *shader);3.3 纹理(临时路径)// 临时: 帧缓冲纹理直接构造(注意是 opengl 层类型)
gkit::graphic::opengl::Texture fbo_texture(" ", gkit::graphic::TextureType::TextureFramebuffer);
fbo->attach_color_texture(fbo_texture, 0);4. 关键 API 变化(从 main 迁移注意)
5. 容易踩的坑5.1
|
- Add move ctor/assign to opengl VertexBuffer/IndexBuffer/VertexArray/ FrameBuffer/RenderBuffer (lost during refactor; user-defined dtors suppress implicit moves, making them non-movable) - Call base-class move in Shader/Texture move ctor/assign - Buffer + frontend VertexBuffer/IndexBuffer: explicit move with self-move-assignment guard (this != &other), so the whole chain from Buffer down to opengl backends is protected
… Device - opengl VertexBuffer/IndexBuffer/VertexArray/FrameBuffer/RenderBuffer/Shader: constructors now private; opengl::Device is a friend, so resources can only be created through the device factory, preventing frontend code from bypassing the RHI abstraction - opengl::Texture constructor stays public as a temporary exception while the resource module is not ready (see RHI design doc §4.4) - Device factory uses direct new + unique_ptr instead of make_unique (friend does not propagate through make_unique's internal new)
- Move include/gkit/graphic/opengl/*.hpp to src/graphic/opengl/ so backend headers are co-located with their .cpp and no longer ship in the public include directory (only frontend abstractions remain in include/gkit/graphic) - Update includes to 'graphic/opengl/X.hpp' matching the src-tree layout used by e.g. core/input/cache.hpp
- Move src/graphic/opengl/ to src/graphic/backend/opengl/ so multiple graphics API backends (opengl, future vulkan) live under one backend/ directory instead of cluttering the graphic module root - Update includes to graphic/backend/opengl/... and CMake source paths
…re/graphic/RHI
| virtual auto set_uniform_vec_4f(const std::string& name, const float* vec4) -> void = 0; | ||
| virtual auto set_uniform_vec_3f(const std::string& name, const float* vec3) -> void = 0; | ||
| virtual auto set_uniform_mat_4f(const std::string& name, const float* mat4) -> void = 0; | ||
| virtual auto set_uniform_mat_3f(const std::string& name, const float* mat3) -> void = 0; |
There was a problem hiding this comment.
考虑使用 math 模块的数学类型而不是不安全的裸指针传参
…ointers - Shader::set_uniform_vec_4f/vec_3f now take math::Vector4/Vector3 - Shader::set_uniform_mat_4f/mat_3f now take math::Matrix4/Matrix3 - Data extraction (mat.data(), &vec.x) happens inside the backend setter, so callers pass the math object directly without touching raw pointers - Add Matrix3::data() (column-major, symmetric with Matrix4) - Frontend Shader.hpp now includes gkit/math headers (graphic depends on math)
这是仿照主流引擎的设计,渲染瓶颈在DrawCall上,虚函数的开销与DrawCall开销不是一个量级,没必要过度设计 |
CoraBlack
left a comment
There was a problem hiding this comment.
nice code, nice commits
Summary
Lays the foundation for a Render Hardware Interface (RHI) in the graphic module: a frontend abstraction layer (
gkit::graphic) decoupled from any concrete graphics API, with OpenGL moved behind a backend device implementation. This makes adding a Vulkan (or other) backend a drop-in exercise rather than a rewrite, and prepares the renderer for a future render queue / draw-sorting pass.What changed
1. Backend-agnostic enums (
include/gkit/graphic/config.hpp)ClearFlags,CompareFunc,BlendFunc,BlendEquation,CullFaceMode,FrontFace,StencilOp,TextureType, andSCR_WIDTH/SCR_HEIGHTout of the OpenGL layer.opengl/config.hppbecame a thin mapping layer (to_gl_*converters) used only by the OpenGL backend.2. Frontend abstract interfaces (
include/gkit/graphic/)Buffer(withVertexBuffer/IndexBufferderivations),VertexArray,Shader,Texture,FrameBuffer,RenderBuffer,VertexBufferLayout.StorageBuffer/UniformBuffer.Textureis designed as a thin binding to the resource module (data ownership stays inresource); the currentopengl::Textureremains a deprecated placeholder until the resource module lands.3. Backend refactor (
include/gkit/graphic/opengl/,src/graphic/opengl/)Shadermoved fromgkit::graphic::Shadertoopengl::Shader(the frontendShaderis now abstract).opengl::buffer::sub-namespace;VertexBufferLayoutpromoted to a frontend concept.4. RenderDevice factory (
RenderDevice.hpp,opengl/Device.hpp,create_device.cpp)RenderDeviceabstract class: resource factory (create_vertex_buffer,create_index_buffer,create_shader, …) plus render entry points (clear,draw,draw_instance).Backendenum +create_device(Backend)is the single backend-selection switch point.opengl::Deviceimplements the factory for the OpenGL backend.5. Renderer integration (
Renderer.hpp/cpp)Rendererowns astd::unique_ptr<RenderDevice>, selected viainit(Backend).get_device()now lazily creates the default OpenGL device, so callers no longer risk a null dereference by callingget_device()beforeinit().6. Fixes & test updates
opengl::Texture: framebuffer textures now allocate a real GL texture with storage, fixing the blackscreenTextureoutput when rendering to an FBO.test_window.cpp: replaced the textured square with a centered colored triangle (position + color attributes); newcolor_triangle.shader.Commits
ac8cae485db70062e047ea3a549e1001bfc3344d6f