Skip to content

Commit

Permalink
Zephyr: Renderer: handle changes in vertex and index count
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed May 2, 2024
1 parent 910c383 commit 91da51e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/next/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ find_package(SDL2 REQUIRED)

add_executable(zephyr-next ${SOURCES} ${HEADERS})

target_link_libraries(zephyr-next PRIVATE zephyr)
target_link_libraries(zephyr-next PRIVATE zephyr nlohmann_json)
target_include_directories(zephyr-next PRIVATE include)
target_include_directories(zephyr-next PRIVATE src)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ namespace zephyr {
class RenderGeometry {
public:
virtual ~RenderGeometry() = default;

[[nodiscard]] virtual size_t GetNumberOfVertices() const = 0;
[[nodiscard]] virtual size_t GetNumberOfIndices() const = 0;
};

struct RenderObject {
Expand Down
8 changes: 8 additions & 0 deletions zephyr/renderer/src/backend/opengl/render_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ namespace zephyr {
return render_geometry;
}

[[nodiscard]] size_t GetNumberOfVertices() const override {
return m_number_of_vertices;
}

[[nodiscard]] size_t GetNumberOfIndices() const override {
return m_number_of_indices;
}

void UpdateIndices(std::span<const u8> data) {
// @todo: validation
glNamedBufferSubData(m_gl_ibo.value(), 0u, (GLsizeiptr)data.size_bytes(), data.data());
Expand Down
13 changes: 9 additions & 4 deletions zephyr/renderer/src/engine/geometry_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ namespace zephyr {
const Geometry* geometry = upload_task.geometry;
RenderGeometry* render_geometry = m_render_geometry_table[geometry];

if(!render_geometry) {
render_geometry = m_render_backend->CreateRenderGeometry(
upload_task.layout, upload_task.number_of_vertices, upload_task.number_of_indices);
m_render_geometry_table[upload_task.geometry] = render_geometry;
const size_t new_number_of_vertices = upload_task.number_of_vertices;
const size_t new_number_of_indices = upload_task.number_of_indices;

if(!render_geometry || new_number_of_vertices != render_geometry->GetNumberOfVertices() || new_number_of_indices != render_geometry->GetNumberOfIndices()) {
if(render_geometry) {
m_render_backend->DestroyRenderGeometry(render_geometry);
}
render_geometry = m_render_backend->CreateRenderGeometry(upload_task.layout, new_number_of_vertices, new_number_of_indices);
m_render_geometry_table[geometry] = render_geometry;
}

m_render_backend->UpdateRenderGeometryVertices(render_geometry, upload_task.raw_vbo_data);
Expand Down

0 comments on commit 91da51e

Please sign in to comment.