-
Notifications
You must be signed in to change notification settings - Fork 0
Add index buffer #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add index buffer #40
Changes from all commits
063d594
9bf2f61
76513d7
0027ed9
902c99e
50d313e
718fa32
d700eda
787e2ff
381d3cf
283bba2
4e45074
539e18e
08dbf2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| cmake_minimum_required(VERSION 3.30) | ||
| project(data_type LANGUAGES CXX) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 23) | ||
| add_library(data_type STATIC) | ||
|
|
||
| target_sources(data_type | ||
| PUBLIC | ||
| FILE_SET cxx_modules TYPE CXX_MODULES FILES | ||
| modules/data_type.ixx | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // data_type.ixx | ||
| // Created by sophomore on 11/23/25. | ||
| // | ||
| module; | ||
| #include<expected> | ||
| export module data_type; | ||
|
|
||
| export namespace sopho { | ||
| enum class GpuError { | ||
| UNINITIALIZED, | ||
| CREATE_DEVICE_FAILED, | ||
| CREATE_WINDOW_FAILED, | ||
| CLAIM_WINDOW_FAILED, | ||
| CREATE_GPU_BUFFER_FAILED, | ||
| CREATE_TRANSFER_BUFFER_FAILED, | ||
| CREATE_SHADER_FAILED, | ||
| CREATE_PIPELINE_FAILED, | ||
| GET_TEXTUREFORMAT_FAILED, | ||
| BUFFER_OVERFLOW, | ||
| MAP_TRANSFER_BUFFER_FAILED, | ||
| ACQUIRE_COMMAND_BUFFER_FAILED, | ||
| SUBMIT_COMMAND_FAILED, | ||
| BEGIN_COPY_PASS_FAILED, | ||
| COMPILE_VERTEX_SHADER_FAILED, | ||
| COMPILE_FRAGMENT_SHADER_FAILED, | ||
| }; | ||
|
|
||
| using TError = GpuError; | ||
| template<typename T> | ||
| using checkable = std::expected<T, TError>; | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |||||||||||||
| #include "SDL3/SDL.h" | ||||||||||||||
| #include "SDL3/SDL_gpu.h" | ||||||||||||||
| #include "SDL3/SDL_keycode.h" | ||||||||||||||
|
|
||||||||||||||
| import data_type; | ||||||||||||||
| import glsl_reflector; | ||||||||||||||
| import sdl_wrapper; | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -57,7 +57,7 @@ layout(std140, set = 1, binding = 0) uniform Camera | |||||||||||||
| void main() | ||||||||||||||
| { | ||||||||||||||
| gl_Position = uView * vec4(a_position, 1.0f); | ||||||||||||||
| v_color = vec4(1); | ||||||||||||||
| v_color = a_color; | ||||||||||||||
| })WSQ"; | ||||||||||||||
|
|
||||||||||||||
| std::string fragment_source = | ||||||||||||||
|
|
@@ -116,7 +116,7 @@ void main() | |||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // 3. Create vertex buffer. | ||||||||||||||
| auto render_data = std::move(m_gpu->create_data(pw_result.value(), 3)); | ||||||||||||||
| auto render_data = std::move(m_gpu->create_data(pw_result.value(), 4)); | ||||||||||||||
| if (!render_data) | ||||||||||||||
| { | ||||||||||||||
| SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Failed to create vertex buffer, error = %d", | ||||||||||||||
|
|
@@ -229,7 +229,7 @@ void main() | |||||||||||||
| { | ||||||||||||||
| bool changed = false; | ||||||||||||||
| auto editor_data = m_renderable->data()->vertex_view(); | ||||||||||||||
| auto ptr = editor_data.raw; | ||||||||||||||
| auto raw_ptr = editor_data.raw; | ||||||||||||||
| for (int vertex_index = 0; vertex_index < editor_data.vertex_count; ++vertex_index) | ||||||||||||||
| { | ||||||||||||||
| for (const auto& format : editor_data.layout.get_vertex_reflection().inputs) | ||||||||||||||
|
|
@@ -242,22 +242,29 @@ void main() | |||||||||||||
| { | ||||||||||||||
| case 3: | ||||||||||||||
| changed |= ImGui::DragFloat3(std::format("{}{}", format.name, vertex_index).data(), | ||||||||||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||||||||||
| reinterpret_cast<float*>(raw_ptr), 0.01f, -1.f, 1.f); | ||||||||||||||
| break; | ||||||||||||||
| case 4: | ||||||||||||||
| changed |= ImGui::DragFloat4(std::format("{}{}", format.name, vertex_index).data(), | ||||||||||||||
| reinterpret_cast<float*>(ptr), 0.01f, -1.f, 1.f); | ||||||||||||||
| reinterpret_cast<float*>(raw_ptr), 0.01f, -1.f, 1.f); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| break; | ||||||||||||||
| default: | ||||||||||||||
| break; | ||||||||||||||
| } | ||||||||||||||
| auto size = sopho::get_size(sopho::to_sdl_format(format.basic_type, format.vector_size)); | ||||||||||||||
| ptr += size; | ||||||||||||||
| raw_ptr += size; | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| auto index_view = m_renderable->data()->index_view(); | ||||||||||||||
| auto index_ptr = index_view.raw; | ||||||||||||||
| for (int index_index = 0; index_index < 2; ++index_index) | ||||||||||||||
| { | ||||||||||||||
| changed |= ImGui::InputInt3(std::format("index_{}", index_index).data(), | ||||||||||||||
| reinterpret_cast<int*>(index_ptr)); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: do not use reinterpret_cast [cppcoreguidelines-pro-type-reinterpret-cast] reinterpret_cast<int*>(index_ptr));
^ |
||||||||||||||
| index_ptr += 3 * sizeof(int); | ||||||||||||||
| } | ||||||||||||||
| if (changed) | ||||||||||||||
| { | ||||||||||||||
| auto upload_result = m_renderable->data()->upload(); | ||||||||||||||
|
|
@@ -431,10 +438,13 @@ void main() | |||||||||||||
| SDL_PushGPUVertexUniformData(commandBuffer, 0, cam.m.data(), static_cast<std::uint32_t>(sizeof(cam.m))); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| SDL_BindGPUVertexBuffers(renderPass, 0, m_renderable->data()->get_buffer_binding().data(), | ||||||||||||||
| m_renderable->data()->get_buffer_binding().size()); | ||||||||||||||
| SDL_BindGPUVertexBuffers(renderPass, 0, m_renderable->data()->get_vertex_buffer_binding().data(), | ||||||||||||||
| m_renderable->data()->get_vertex_buffer_binding().size()); | ||||||||||||||
|
|
||||||||||||||
| SDL_BindGPUIndexBuffer(renderPass, &m_renderable->data()->get_index_buffer_binding(), | ||||||||||||||
| SDL_GPU_INDEXELEMENTSIZE_32BIT); | ||||||||||||||
|
|
||||||||||||||
| SDL_DrawGPUPrimitives(renderPass, 3, 1, 0, 0); | ||||||||||||||
| SDL_DrawGPUIndexedPrimitives(renderPass, 6, 1, 0, 0, 0); | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: 6 is a magic number; consider replacing it with a named constant [cppcoreguidelines-avoid-magic-numbers] SDL_DrawGPUIndexedPrimitives(renderPass, 6, 1, 0, 0, 0);
^ |
||||||||||||||
|
|
||||||||||||||
| ImGui_ImplSDLGPU3_RenderDrawData(draw_data, commandBuffer, renderPass); | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -505,4 +515,4 @@ void main() | |||||||||||||
| * @return sopho::App* Pointer to a heap-allocated application object; the caller takes ownership and is responsible for | ||||||||||||||
| * deleting it. | ||||||||||||||
| */ | ||||||||||||||
| sopho::App* create_app(int argc, char** argv) { return new UserApp(); } | ||||||||||||||
| sopho::checkable<sopho::App*> create_app(int argc, char** argv) { return new UserApp(); } | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: function 'create_app' can be made static or moved into an anonymous namespace to enforce internal linkage [misc-use-internal-linkage]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: parameter 'argc' is unused [misc-unused-parameters]
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: parameter 'argv' is unused [misc-unused-parameters]
Suggested change
|
||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # SDL Wrapper Arch | ||
|
|
||
| ```mermaid | ||
| graph TD | ||
|
|
||
| subgraph RenderData | ||
| BufferWrapper | ||
| end | ||
|
|
||
| Renderable --> RenderProcedural | ||
| Renderable --> RenderData | ||
| RenderProcedural --> GpuWrapper | ||
| RenderData --> GpuWrapper | ||
|
|
||
| ``` |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,11 +2,12 @@ | |||||
| // Created by sophomore on 11/9/25. | ||||||
| // | ||||||
| #define SDL_MAIN_USE_CALLBACKS | ||||||
| #include <SDL3/SDL.h> | ||||||
| #include <SDL3/SDL_main.h> | ||||||
| #include "SDL3/SDL.h" | ||||||
| #include "SDL3/SDL_main.h" | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: included header SDL.h is not used directly [misc-include-cleaner]
Suggested change
|
||||||
| import sdl_wrapper; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. warning: module 'sdl_wrapper' not found [clang-diagnostic-error] import sdl_wrapper;
^ |
||||||
| import data_type; | ||||||
|
|
||||||
| extern sopho::App* create_app(int argc, char** argv); | ||||||
| extern sopho::checkable<sopho::App*> create_app(int argc, char** argv); | ||||||
|
|
||||||
| /** | ||||||
| * @brief Initializes the SDL video subsystem, constructs the application, and invokes its initialization. | ||||||
|
|
@@ -26,8 +27,8 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char** argv) | |||||
| auto app = create_app(argc, argv); | ||||||
| if (!app) | ||||||
| return SDL_APP_FAILURE; | ||||||
| *appstate = app; | ||||||
| return app->init(argc, argv); | ||||||
| *appstate = app.value(); | ||||||
| return app.value()->init(argc, argv); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,16 +12,22 @@ namespace sopho | |
| { | ||
| export class RenderData | ||
| { | ||
| BufferWrapper m_buffer; | ||
| BufferWrapper m_vertex_buffer; | ||
| BufferWrapper m_index_buffer; | ||
| VertexLayout m_layouts{}; | ||
| size_t m_vertex_count{}; | ||
| std::vector<SDL_GPUBufferBinding> m_bindings{}; | ||
| SDL_GPUBufferBinding m_index_binding{}; | ||
|
|
||
| public: | ||
| explicit RenderData(BufferWrapper&& buffer_wrapper, const VertexLayout& layouts, size_t vertex_count) : | ||
| m_buffer(std::move(buffer_wrapper)), m_layouts(layouts), m_vertex_count(vertex_count) | ||
| explicit RenderData(BufferWrapper&& vertex_buffer_wrapper, BufferWrapper&& index_buffer_wrapper, | ||
| const VertexLayout& layouts, size_t vertex_count) : | ||
| m_vertex_buffer(std::move(vertex_buffer_wrapper)), m_index_buffer(std::move(index_buffer_wrapper)), | ||
| m_layouts(layouts), m_vertex_count(vertex_count) | ||
| { | ||
| m_bindings.emplace_back(m_buffer.gpu_buffer(), 0); | ||
| m_bindings.emplace_back(m_vertex_buffer.gpu_buffer(), 0); | ||
| m_index_binding.buffer = m_index_buffer.gpu_buffer(); | ||
| m_index_binding.offset = 0; | ||
| } | ||
|
|
||
| public: | ||
|
|
@@ -31,19 +37,32 @@ namespace sopho | |
| size_t vertex_count{}; | ||
| std::byte* raw{}; | ||
| }; | ||
| struct IndexView | ||
| { | ||
| size_t index_count{}; | ||
| std::byte* raw{}; | ||
| }; | ||
|
Comment on lines
+40
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IndexView uses vertex_count, which can miscount indices
Consider tracking index count separately and wiring it through the constructor, e.g.: - VertexLayout m_layouts{};
- size_t m_vertex_count{};
+ VertexLayout m_layouts{};
+ size_t m_vertex_count{};
+ size_t m_index_count{};
- explicit RenderData(BufferWrapper&& vertex_buffer_wrapper, BufferWrapper&& index_buffer_wrapper,
- const VertexLayout& layouts, size_t vertex_count) :
- m_vertex_buffer(std::move(vertex_buffer_wrapper)), m_index_buffer(std::move(index_buffer_wrapper)),
- m_layouts(layouts), m_vertex_count(vertex_count)
+ explicit RenderData(BufferWrapper&& vertex_buffer_wrapper, BufferWrapper&& index_buffer_wrapper,
+ const VertexLayout& layouts, size_t vertex_count, size_t index_count) :
+ m_vertex_buffer(std::move(vertex_buffer_wrapper)), m_index_buffer(std::move(index_buffer_wrapper)),
+ m_layouts(layouts), m_vertex_count(vertex_count), m_index_count(index_count)
@@
- auto index_view() { return IndexView{.index_count = m_vertex_count, .raw = m_index_buffer.cpu_buffer()}; }
+ auto index_view() { return IndexView{.index_count = m_index_count, .raw = m_index_buffer.cpu_buffer()}; }You’d then adjust call sites to pass the true index count. Also applies to: 56-56 🤖 Prompt for AI Agents |
||
| RenderData(const RenderData&) = delete; | ||
| RenderData& operator=(const RenderData&) = delete; | ||
| RenderData(RenderData&&) = default; | ||
| RenderData& operator=(RenderData&&) = default; | ||
| auto& buffer() { return m_buffer; } | ||
| auto& get_buffer_binding() { return m_bindings; } | ||
| auto& buffer() { return m_vertex_buffer; } | ||
| auto& get_vertex_buffer_binding() { return m_bindings; } | ||
| auto& get_index_buffer_binding() { return m_index_binding; } | ||
| auto vertex_view() | ||
| { | ||
| return VertexView{.layout = m_layouts, .vertex_count = m_vertex_count, .raw = m_buffer.cpu_buffer()}; | ||
| return VertexView{.layout = m_layouts, .vertex_count = m_vertex_count, .raw = m_vertex_buffer.cpu_buffer()}; | ||
| } | ||
| auto index_view() { return IndexView{.index_count = m_vertex_count, .raw = m_index_buffer.cpu_buffer()}; } | ||
| auto upload() | ||
| { | ||
| return m_buffer.upload(); | ||
| auto result = m_vertex_buffer.upload(); | ||
| if (!result) | ||
| { | ||
| return result; | ||
| } | ||
| result = m_index_buffer.upload(); | ||
| return result; | ||
| } | ||
| }; | ||
| } // namespace sopho | ||
Uh oh!
There was an error while loading. Please reload this page.