From f815866f62e0f6a371c0e1cb4a385b41bcffd1ce Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 25 Feb 2023 11:07:02 -0800 Subject: Simplify buffer creation, add support for dynamic geometry. --- gfx/include/gfx/render_backend.h | 73 +++++++++---- gfx/src/render/buffer.c | 49 ++++++--- gfx/src/render/buffer.h | 13 +-- gfx/src/render/geometry.c | 214 ++++++++++++++++++++++----------------- gfx/src/render/geometry.h | 20 +--- gfx/src/render/render_backend.c | 36 +------ gfx/src/util/geometry.c | 22 ++-- gfx/src/util/scene.c | 15 ++- 8 files changed, 244 insertions(+), 198 deletions(-) diff --git a/gfx/include/gfx/render_backend.h b/gfx/include/gfx/render_backend.h index 785c4b9..ef6e4e9 100644 --- a/gfx/include/gfx/render_backend.h +++ b/gfx/include/gfx/render_backend.h @@ -38,6 +38,44 @@ typedef enum PrimitiveType { TriangleStrip } PrimitiveType; +/// Buffer usage. +typedef enum BufferUsage { BufferStatic, BufferDynamic } BufferUsage; + +/// Buffer type. +typedef enum BufferType { + BufferUntyped, + Buffer2d, + Buffer3d, + Buffer4d, + BufferU8, + BufferU16 +} BufferType; + +/// Buffer descriptor. +/// +/// 'count' is the number of elements in the array. For untyped buffers, this is +/// the size in bytes of the 'data' array. For other types, it is the number of +/// vec2s, vec3s, etc. in the corresponding array. +/// +/// The data pointers can also be null. In such a case, a buffer of the given +/// size is created with its contents uninitialized. +/// +/// TODO: Think about typed buffers (Buffer, Buffer2d, Buffer3d, BufferU8, etc). +/// Typed buffers don't work well with interleaved vertex attributes. Not sure +/// this is really worth it. +typedef struct BufferDesc { + BufferUsage usage; + BufferType type; + union { + const void* data; + const vec2* vec2s; + const vec3* vec3s; + const uint8_t* u8s; + const uint16_t* u16s; + }; + size_t count; +} BufferDesc; + /// A buffer view for vertex data (attributes or indices). /// Either 'data' or 'buffer' must be set. #define MAKE_BUFFER_VIEW(NAME, TYPE) \ @@ -78,8 +116,8 @@ MAKE_BUFFER_VIEW(BufferViewIdx16, uint16_t) /// Describes a piece of geometry. /// -/// Currently we support only 16-bit vertex indices. Might have to change this -/// to support a larger variety of 3D models. +/// Currently we support only up to 16-bit vertex indices. Might have to change +/// this to support a larger variety of 3D models. typedef struct GeometryDesc { BufferView2d positions2d; BufferView3d positions3d; @@ -265,22 +303,7 @@ void gfx_get_viewport(RenderBackend*, int* width, int* height); // ----------------------------------------------------------------------------- /// Create a buffer from raw data. -Buffer* gfx_make_buffer(RenderBackend*, const void*, size_t size_bytes); - -/// Create a buffer from 2D vertices. -Buffer* gfx_make_buffer2d(RenderBackend*, const vec2* verts, size_t count); - -/// Create a buffer from 3D vertices. -Buffer* gfx_make_buffer3d(RenderBackend*, const vec3* verts, size_t count); - -/// Create a buffer from 4D vertices. -Buffer* gfx_make_buffer4d(RenderBackend*, const vec4* verts, size_t count); - -/// Create a buffer from 8-bit unsigned integers. -Buffer* gfx_make_bufferu8(RenderBackend*, const uint8_t* vals, size_t count); - -/// Create a buffer from 16-bit unsigned integers. -Buffer* gfx_make_bufferu16(RenderBackend*, const uint16_t* vals, size_t count); +Buffer* gfx_make_buffer(RenderBackend*, const BufferDesc*); /// Destroy the buffer. void gfx_destroy_buffer(RenderBackend*, Buffer**); @@ -295,6 +318,20 @@ Geometry* gfx_make_geometry(RenderBackend*, const GeometryDesc*); /// Destroy the geometry. void gfx_destroy_geometry(RenderBackend*, Geometry**); +/// Upload new vertex data for the geometry. +/// +/// This is similar to gfx_make_geometry(), but the geometry need not be +/// entirely specified. Only the vertex attributes set in the descriptor are +/// updated. +/// +/// This function only updates vertex attributes, not indices or primitive type. +/// +/// Note that the descriptor cannot specify more vertex attributes than the +/// geometry was created with. If the size or any other attribute not handled +/// by this update function needs to be changed, then a new geometry must be +/// created. +void gfx_update_geometry(Geometry*, const GeometryDesc*); + /// Render the geometry. void gfx_render_geometry(const Geometry*); diff --git a/gfx/src/render/buffer.c b/gfx/src/render/buffer.c index 3536dcb..392777c 100644 --- a/gfx/src/render/buffer.c +++ b/gfx/src/render/buffer.c @@ -1,32 +1,51 @@ #include "buffer.h" +#include + #include #include #include -bool gfx_init_buffer(Buffer* buffer, const void* data, size_t size_bytes) { +static size_t get_buffer_size_bytes(const BufferDesc* desc) { + switch (desc->type) { + case BufferUntyped: + return desc->count; + case Buffer2d: + return desc->count * sizeof(vec2); + case Buffer3d: + return desc->count * sizeof(vec3); + case Buffer4d: + return desc->count * sizeof(vec4); + case BufferU8: + return desc->count * sizeof(uint8_t); + case BufferU16: + return desc->count * sizeof(uint16_t); + } +} + +static GLenum get_buffer_usage(BufferUsage usage) { + switch (usage) { + case BufferStatic: + return GL_STATIC_DRAW; + case BufferDynamic: + return GL_DYNAMIC_DRAW; + } + assert(false); + return GL_STATIC_DRAW; +} + +bool gfx_init_buffer(Buffer* buffer, const BufferDesc* desc) { assert(buffer); - buffer->size_bytes = size_bytes; + buffer->size_bytes = get_buffer_size_bytes(desc); + const GLenum usage = get_buffer_usage(desc->usage); glGenBuffers(1, &buffer->vbo); glBindBuffer(GL_ARRAY_BUFFER, buffer->vbo); - glBufferData(GL_ARRAY_BUFFER, size_bytes, data, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, buffer->size_bytes, desc->data, usage); glBindBuffer(GL_ARRAY_BUFFER, 0); ASSERT_GL; return true; } -bool gfx_init_buffer_2d(Buffer* buffer, const vec2* verts, size_t count) { - return gfx_init_buffer(buffer, (const void*)verts, count * sizeof(vec2)); -} - -bool gfx_init_buffer_3d(Buffer* buffer, const vec3* verts, size_t count) { - return gfx_init_buffer(buffer, (const void*)verts, count * sizeof(vec3)); -} - -bool gfx_init_buffer_4d(Buffer* buffer, const vec4* verts, size_t count) { - return gfx_init_buffer(buffer, (const void*)verts, count * sizeof(vec4)); -} - void gfx_del_buffer(Buffer* buffer) { assert(buffer); if (buffer->vbo) { diff --git a/gfx/src/render/buffer.h b/gfx/src/render/buffer.h index 5cff53a..575fbb9 100644 --- a/gfx/src/render/buffer.h +++ b/gfx/src/render/buffer.h @@ -7,22 +7,15 @@ #include #include +typedef struct BufferDesc BufferDesc; + typedef struct Buffer { GLuint vbo; size_t size_bytes; } Buffer; /// Create a buffer from raw data. -bool gfx_init_buffer(Buffer*, const void* data, size_t size_bytes); - -/// Create a buffer from 2D vertices. -bool gfx_init_buffer_2d(Buffer*, const vec2* verts, size_t count); - -/// Create a buffer from 3D vertices. -bool gfx_init_buffer_3d(Buffer*, const vec3* verts, size_t count); - -/// Create a buffer from 4D vertices. -bool gfx_init_buffer_4d(Buffer*, const vec4* verts, size_t count); +bool gfx_init_buffer(Buffer*, const BufferDesc*); /// Destroy the buffer. void gfx_del_buffer(Buffer*); diff --git a/gfx/src/render/geometry.c b/gfx/src/render/geometry.c index 7f3fc46..076a956 100644 --- a/gfx/src/render/geometry.c +++ b/gfx/src/render/geometry.c @@ -25,35 +25,38 @@ static GLenum primitive_type_to_gl(PrimitiveType type) { /// Create a buffer for the view. /// /// If the view already has a buffer, return the buffer. Otherwise create a -/// buffer from the view's data. -static const Buffer* get_or_make_buffer( - RenderBackend* render_backend, const BufferView* view) { +/// buffer from the view's data and assign it to the view. +static const Buffer* get_or_make_view_buffer( + RenderBackend* render_backend, BufferView* view) { if (view->buffer) { return view->buffer; } else { - return gfx_make_buffer(render_backend, view->data, view->size_bytes); + return view->buffer = gfx_make_buffer( + render_backend, &(BufferDesc){ + .usage = BufferStatic, + .type = BufferUntyped, + .data = view->data, + .count = view->size_bytes}); } } /// Create a buffer for the view, then configure it in the VAO. static bool configure_buffer( - RenderBackend* render_backend, const GeometryDesc* desc, - const BufferView* view, size_t num_components, size_t component_size_bytes, - GLenum component_type, GLboolean normalized, GLuint channel, - const Buffer** buffer) { + RenderBackend* render_backend, const GeometryDesc* desc, BufferView* view, + size_t num_components, size_t component_size_bytes, GLenum component_type, + GLboolean normalized, GLuint channel) { assert(render_backend); assert(desc); assert(view); - assert(buffer); assert( desc->num_verts <= view->size_bytes / (num_components * component_size_bytes)); - *buffer = get_or_make_buffer(render_backend, view); - if (!(*buffer)) { + const Buffer* buffer = get_or_make_view_buffer(render_backend, view); + if (!buffer) { return false; } - assert(view->size_bytes <= (*buffer)->size_bytes); - glBindBuffer(GL_ARRAY_BUFFER, (*buffer)->vbo); + assert(view->size_bytes <= buffer->size_bytes); + glBindBuffer(GL_ARRAY_BUFFER, buffer->vbo); glEnableVertexAttribArray(channel); if ((component_type == GL_FLOAT) || normalized) { glVertexAttribPointer( @@ -72,132 +75,130 @@ static bool configure_buffer( return true; } -bool gfx_init_geometry( - Geometry* geometry, RenderBackend* render_backend, - const GeometryDesc* desc) { - assert(geometry); +static bool configure_vertex_attributes( + RenderBackend* render_backend, GeometryDesc* desc) { assert(render_backend); assert(desc); - assert( - view_is_populated(desc->positions3d) || - view_is_populated(desc->positions2d)); - assert(desc->num_verts > 0); - - geometry->mode = primitive_type_to_gl(desc->type); - geometry->num_verts = desc->num_verts; - geometry->num_indices = desc->num_indices; - - glGenVertexArrays(1, &geometry->vao); - glBindVertexArray(geometry->vao); bool result = true; if (view_is_populated(desc->positions3d)) { - result = result && - configure_buffer( - render_backend, desc, (const BufferView*)&desc->positions3d, 3, - sizeof(float), GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL, - &geometry->positions); + result = + result && configure_buffer( + render_backend, desc, (BufferView*)&desc->positions3d, 3, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL); } else if (view_is_populated(desc->positions2d)) { - result = result && - configure_buffer( - render_backend, desc, (const BufferView*)&desc->positions2d, 2, - sizeof(float), GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL, - &geometry->positions); + result = + result && configure_buffer( + render_backend, desc, (BufferView*)&desc->positions2d, 2, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_POSITION_CHANNEL); } if (view_is_populated(desc->normals)) { result = result && configure_buffer( - render_backend, desc, (const BufferView*)&desc->normals, - 3, sizeof(float), GL_FLOAT, GL_FALSE, GFX_NORMAL_CHANNEL, - &geometry->normals); + render_backend, desc, (BufferView*)&desc->normals, 3, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_NORMAL_CHANNEL); } if (view_is_populated(desc->tangents)) { result = result && configure_buffer( - render_backend, desc, (const BufferView*)&desc->tangents, - 4, sizeof(float), GL_FLOAT, GL_FALSE, GFX_TANGENT_CHANNEL, - &geometry->tangents); + render_backend, desc, (BufferView*)&desc->tangents, 4, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_TANGENT_CHANNEL); } if (view_is_populated(desc->texcoords)) { result = result && configure_buffer( - render_backend, desc, (const BufferView*)&desc->texcoords, - 2, sizeof(float), GL_FLOAT, GL_FALSE, - GFX_TEXCOORDS_CHANNEL, &geometry->texcoords); + render_backend, desc, (BufferView*)&desc->texcoords, 2, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_TEXCOORDS_CHANNEL); } if (view_is_populated(desc->joints.u8)) { - result = - result && configure_buffer( - render_backend, desc, (const BufferView*)&desc->joints.u8, - 4, sizeof(uint8_t), GL_UNSIGNED_BYTE, GL_FALSE, - GFX_JOINTS_CHANNEL, &geometry->joints); + result = result && configure_buffer( + render_backend, desc, (BufferView*)&desc->joints.u8, + 4, sizeof(uint8_t), GL_UNSIGNED_BYTE, GL_FALSE, + GFX_JOINTS_CHANNEL); } else if (view_is_populated(desc->joints.u16)) { - result = result && - configure_buffer( - render_backend, desc, (const BufferView*)&desc->joints.u16, 4, - sizeof(uint16_t), GL_UNSIGNED_SHORT, GL_FALSE, - GFX_JOINTS_CHANNEL, &geometry->joints); + result = result && configure_buffer( + render_backend, desc, (BufferView*)&desc->joints.u16, + 4, sizeof(uint16_t), GL_UNSIGNED_SHORT, GL_FALSE, + GFX_JOINTS_CHANNEL); } // If weights are given as unsigned integers, then they are normalized when // read by the shader. if (view_is_populated(desc->weights.u8)) { - result = result && - configure_buffer( - render_backend, desc, (const BufferView*)&desc->weights.u8, 4, - sizeof(uint8_t), GL_UNSIGNED_BYTE, GL_TRUE, - GFX_WEIGHTS_CHANNEL, &geometry->weights); + result = result && configure_buffer( + render_backend, desc, (BufferView*)&desc->weights.u8, + 4, sizeof(uint8_t), GL_UNSIGNED_BYTE, GL_TRUE, + GFX_WEIGHTS_CHANNEL); } else if (view_is_populated(desc->weights.u16)) { - result = result && - configure_buffer( - render_backend, desc, (const BufferView*)&desc->weights.u16, 4, - sizeof(uint16_t), GL_UNSIGNED_SHORT, GL_TRUE, - GFX_WEIGHTS_CHANNEL, &geometry->weights); + result = result && configure_buffer( + render_backend, desc, + (BufferView*)&desc->weights.u16, 4, sizeof(uint16_t), + GL_UNSIGNED_SHORT, GL_TRUE, GFX_WEIGHTS_CHANNEL); } else if (view_is_populated(desc->weights.floats)) { result = result && configure_buffer( - render_backend, desc, (const BufferView*)&desc->weights.floats, - 4, sizeof(float), GL_FLOAT, GL_FALSE, GFX_WEIGHTS_CHANNEL, - &geometry->weights); + render_backend, desc, (BufferView*)&desc->weights.floats, 4, + sizeof(float), GL_FLOAT, GL_FALSE, GFX_WEIGHTS_CHANNEL); } - if (!result) { + glBindBuffer(GL_ARRAY_BUFFER, 0); + return result; +} + +bool gfx_init_geometry( + Geometry* geometry, RenderBackend* render_backend, + const GeometryDesc* input_desc) { + assert(geometry); + assert(render_backend); + assert(input_desc); + assert( + view_is_populated(input_desc->positions3d) || + view_is_populated(input_desc->positions2d)); + assert(input_desc->num_verts > 0); + + geometry->mode = primitive_type_to_gl(input_desc->type); + geometry->desc = *input_desc; + geometry->render_backend = render_backend; + + // We manipulate the descriptor copy below. Create a shorter name for it. + GeometryDesc* desc = &geometry->desc; + + glGenVertexArrays(1, &geometry->vao); + glBindVertexArray(geometry->vao); + + if (!configure_vertex_attributes(render_backend, desc)) { gfx_del_geometry(geometry); return false; } - glBindBuffer(GL_ARRAY_BUFFER, 0); - if (view_is_populated(desc->indices8)) { assert(desc->num_indices > 0); assert( desc->num_indices <= desc->indices8.size_bytes / sizeof(VertexIndex8)); - geometry->indices_offset_bytes = desc->indices8.offset_bytes; - geometry->indices8 = - get_or_make_buffer(render_backend, (const BufferView*)&desc->indices8); - if (!geometry->indices8) { + const Buffer* buffer = + get_or_make_view_buffer(render_backend, (BufferView*)&desc->indices8); + if (!buffer) { gfx_del_geometry(geometry); return false; } - assert(desc->indices8.size_bytes <= geometry->indices8->size_bytes); + assert(desc->indices8.size_bytes <= buffer->size_bytes); } else if (view_is_populated(desc->indices16)) { assert(desc->num_indices > 0); assert( desc->num_indices <= desc->indices16.size_bytes / sizeof(VertexIndex16)); - geometry->indices_offset_bytes = desc->indices16.offset_bytes; - geometry->indices16 = - get_or_make_buffer(render_backend, (const BufferView*)&desc->indices16); - if (!geometry->indices16) { + const Buffer* buffer = + get_or_make_view_buffer(render_backend, (BufferView*)&desc->indices16); + if (!buffer) { gfx_del_geometry(geometry); return false; } - assert(desc->indices16.size_bytes <= geometry->indices16->size_bytes); + assert(desc->indices16.size_bytes <= buffer->size_bytes); } glBindVertexArray(0); @@ -213,21 +214,50 @@ void gfx_del_geometry(Geometry* geometry) { } } +void gfx_update_geometry(Geometry* geometry, const GeometryDesc* desc) { + assert(geometry); + assert(desc); + assert(desc->positions3d.size_bytes <= geometry->desc.positions3d.size_bytes); + assert(desc->positions2d.size_bytes <= geometry->desc.positions2d.size_bytes); + assert(desc->normals.size_bytes <= geometry->desc.normals.size_bytes); + assert(desc->tangents.size_bytes <= geometry->desc.tangents.size_bytes); + assert(desc->texcoords.size_bytes <= geometry->desc.texcoords.size_bytes); + assert(desc->joints.u8.size_bytes <= geometry->desc.joints.u8.size_bytes); + assert(desc->joints.u16.size_bytes <= geometry->desc.joints.u16.size_bytes); + assert(desc->weights.u8.size_bytes <= geometry->desc.weights.u8.size_bytes); + assert(desc->weights.u16.size_bytes <= geometry->desc.weights.u16.size_bytes); + assert( + desc->weights.floats.size_bytes <= + geometry->desc.weights.floats.size_bytes); + + geometry->desc = *desc; + + glBindVertexArray(geometry->vao); + bool result = + configure_vertex_attributes(geometry->render_backend, &geometry->desc); + // Shouldn't fail since we're just uploading buffer data, not creating new + // buffers. + assert(result); + glBindVertexArray(0); +} + void gfx_render_geometry(const Geometry* geometry) { assert(geometry); assert(geometry->vao); + const GeometryDesc* desc = &geometry->desc; glBindVertexArray(geometry->vao); - if (geometry->indices8) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometry->indices8->vbo); + if (desc->indices8.buffer) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, desc->indices8.buffer->vbo); glDrawElements( - geometry->mode, geometry->num_indices, GL_UNSIGNED_BYTE, - (const void*)geometry->indices_offset_bytes); - } else if (geometry->indices16) { - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, geometry->indices16->vbo); + geometry->mode, desc->num_indices, GL_UNSIGNED_BYTE, + (const void*)desc->indices8.offset_bytes); + } else if (desc->indices16.buffer) { + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, desc->indices16.buffer->vbo); glDrawElements( - geometry->mode, geometry->num_indices, GL_UNSIGNED_SHORT, - (const void*)geometry->indices_offset_bytes); + geometry->mode, desc->num_indices, GL_UNSIGNED_SHORT, + (const void*)desc->indices16.offset_bytes); } else { - glDrawArrays(geometry->mode, 0, geometry->num_verts); + glDrawArrays(geometry->mode, 0, desc->num_verts); } + glBindVertexArray(0); } diff --git a/gfx/src/render/geometry.h b/gfx/src/render/geometry.h index fb8f923..8fb36da 100644 --- a/gfx/src/render/geometry.h +++ b/gfx/src/render/geometry.h @@ -13,22 +13,10 @@ /// the renderer assumes ownership of all rendering resources, which simplifies /// their management. typedef struct Geometry { - GLuint vao; - GLenum mode; - VertexCount num_verts; - size_t num_indices; - size_t indices_offset_bytes; - // Buffer pointers are no longer needed once the VAO is created, but we store - // them here so that we can keep track of what Geometry objects use what - // Buffer objects. - const Buffer* positions; - const Buffer* normals; - const Buffer* tangents; - const Buffer* texcoords; - const Buffer* joints; - const Buffer* weights; - const Buffer* indices8; - const Buffer* indices16; + GLuint vao; + GLenum mode; + GeometryDesc desc; + RenderBackend* render_backend; } Geometry; /// Create new geometry. diff --git a/gfx/src/render/render_backend.c b/gfx/src/render/render_backend.c index b3f832b..8feefab 100644 --- a/gfx/src/render/render_backend.c +++ b/gfx/src/render/render_backend.c @@ -100,50 +100,20 @@ void gfx_get_viewport(RenderBackend* render_backend, int* width, int* height) { // Buffers. // ----------------------------------------------------------------------------- -Buffer* gfx_make_buffer( - RenderBackend* render_backend, const void* data, size_t size_bytes) { +Buffer* gfx_make_buffer(RenderBackend* render_backend, const BufferDesc* desc) { assert(render_backend); + assert(desc); Buffer* buffer = mempool_alloc(&render_backend->buffers); if (!buffer) { return 0; } - if (!gfx_init_buffer(buffer, data, size_bytes)) { + if (!gfx_init_buffer(buffer, desc)) { mempool_free(&render_backend->buffers, &buffer); return 0; } return buffer; } -Buffer* gfx_make_buffer2d( - RenderBackend* render_backend, const vec2* verts, size_t count) { - return gfx_make_buffer( - render_backend, (const void*)verts, count * 2 * sizeof(float)); -} - -Buffer* gfx_make_buffer3d( - RenderBackend* render_backend, const vec3* verts, size_t count) { - return gfx_make_buffer( - render_backend, (const void*)verts, count * 3 * sizeof(float)); -} - -Buffer* gfx_make_buffer4d( - RenderBackend* render_backend, const vec4* verts, size_t count) { - return gfx_make_buffer( - render_backend, (const void*)verts, count * 4 * sizeof(float)); -} - -Buffer* gfx_make_bufferu8( - RenderBackend* render_backend, const uint8_t* vals, size_t count) { - return gfx_make_buffer( - render_backend, (const void*)vals, count * sizeof(uint8_t)); -} - -Buffer* gfx_make_bufferu16( - RenderBackend* render_backend, const uint16_t* vals, size_t count) { - return gfx_make_buffer( - render_backend, (const void*)vals, count * sizeof(uint16_t)); -} - void gfx_destroy_buffer(RenderBackend* render_backend, Buffer** buffer) { assert(render_backend); assert(buffer); diff --git a/gfx/src/util/geometry.c b/gfx/src/util/geometry.c index d485125..84435ce 100644 --- a/gfx/src/util/geometry.c +++ b/gfx/src/util/geometry.c @@ -2,26 +2,26 @@ #include -static void gfx_make_quad_11_positions(vec2 positions[4]) { +static void make_quad_11_positions(vec2 positions[4]) { positions[0] = vec2_make(-1, +1); positions[1] = vec2_make(-1, -1); positions[2] = vec2_make(+1, +1); positions[3] = vec2_make(+1, -1); } -static void gfx_make_quad_01_positions(vec2 positions[4]) { +static void make_quad_01_positions(vec2 positions[4]) { positions[0] = vec2_make(0, 0); positions[1] = vec2_make(1, 0); positions[2] = vec2_make(1, 1); positions[3] = vec2_make(0, 1); } -static GeometryDesc gfx_make_quad_desc(vec2 positions[4]) { - GeometryDesc desc = (GeometryDesc){0}; - desc.positions2d.data = positions; +static GeometryDesc make_quad_desc(vec2 positions[4]) { + GeometryDesc desc = (GeometryDesc){0}; + desc.positions2d.data = positions; desc.positions2d.size_bytes = 4 * sizeof(vec2); - desc.num_verts = 4; - desc.type = TriangleStrip; + desc.num_verts = 4; + desc.type = TriangleStrip; return desc; } @@ -29,8 +29,8 @@ Geometry* gfx_make_quad_11(RenderBackend* render_backend) { assert(render_backend); vec2 positions[4]; - gfx_make_quad_11_positions(positions); - const GeometryDesc geometry_desc = gfx_make_quad_desc(positions); + make_quad_11_positions(positions); + const GeometryDesc geometry_desc = make_quad_desc(positions); return gfx_make_geometry(render_backend, &geometry_desc); } @@ -38,7 +38,7 @@ Geometry* gfx_make_quad_01(RenderBackend* render_backend) { assert(render_backend); vec2 positions[4]; - gfx_make_quad_01_positions(positions); - const GeometryDesc geometry_desc = gfx_make_quad_desc(positions); + make_quad_01_positions(positions); + const GeometryDesc geometry_desc = make_quad_desc(positions); return gfx_make_geometry(render_backend, &geometry_desc); } diff --git a/gfx/src/util/scene.c b/gfx/src/util/scene.c index 40d6686..e8dd6b1 100644 --- a/gfx/src/util/scene.c +++ b/gfx/src/util/scene.c @@ -536,7 +536,12 @@ static bool load_buffers( for (cgltf_size i = 0; i < data->buffers_count; ++i) { const cgltf_buffer* buffer = &data->buffers[i]; assert(buffer->data); - buffers[i] = gfx_make_buffer(render_backend, buffer->data, buffer->size); + buffers[i] = gfx_make_buffer( + render_backend, &(BufferDesc){ + .usage = BufferStatic, + .type = BufferUntyped, + .data = buffer->data, + .count = buffer->size}); if (!buffers[i]) { return false; } @@ -557,8 +562,12 @@ static bool load_tangent_buffers( for (cgltf_size i = 0; i < num_tangent_buffers; ++i) { const cgltfTangentBuffer* buffer = &cgltf_tangent_buffers[i]; assert(buffer->data); - tangent_buffers[i] = - gfx_make_buffer(render_backend, buffer->data, buffer->size_bytes); + tangent_buffers[i] = gfx_make_buffer( + render_backend, &(BufferDesc){ + .usage = BufferStatic, + .type = BufferUntyped, + .data = buffer->data, + .count = buffer->size_bytes}); if (!tangent_buffers[i]) { return false; } -- cgit v1.2.3