taisei/src/renderer/gles20/index_buffer.c
Andrei Alexeyev b6978178b1
memory: use custom memory allocation wrappers
Introduces wrappers around memory allocation functions in `memory.h`
that should be used instead of the standard C ones.

These never return NULL and, with the exception of `mem_realloc()`,
zero-initialize the allocated memory like `calloc()` does.

All allocations made with the memory.h API must be deallocated with
`mem_free()`. Although standard `free()` will work on some platforms,
it's not portable (currently it won't work on Windows). Likewise,
`mem_free()` must not be used to free foreign allocations.

The standard C allocation functions are now diagnosed as deprecated.
They are, however, available with the `libc_` prefix in case interfacing
with foreign APIs is required. So far they are only used to implement
`memory.h`.

Perhaps the most important change is the introduction of the `ALLOC()`,
`ALLOC_ARRAY()`, and `ALLOC_FLEX()` macros. They take a type as a
parameter, and allocate enough memory with the correct alignment for
that type. That includes overaligned types as well. In most
circumstances you should prefer to use these macros. See the `memory.h`
header for some usage examples.
2023-01-18 13:23:22 +01:00

66 lines
1.9 KiB
C

/*
* This software is licensed under the terms of the MIT License.
* See COPYING for further information.
* ---
* Copyright (c) 2011-2019, Lukas Weber <laochailan@web.de>.
* Copyright (c) 2012-2019, Andrei Alexeyev <akari@taisei-project.org>.
*/
#include "taisei.h"
#include "index_buffer.h"
IndexBuffer *gles20_index_buffer_create(uint index_size, size_t max_elements) {
assert(index_size == sizeof(gles20_ibo_index_t));
auto ibuf = ALLOC_FLEX(IndexBuffer, max_elements * sizeof(gles20_ibo_index_t));
snprintf(ibuf->debug_label, sizeof(ibuf->debug_label), "Fake IBO at %p", (void*)ibuf);
ibuf->num_elements = max_elements;
return ibuf;
}
size_t gles20_index_buffer_get_capacity(IndexBuffer *ibuf) {
return ibuf->num_elements;
}
uint gles20_index_buffer_get_index_size(IndexBuffer *ibuf) {
return sizeof(gles20_ibo_index_t);
}
const char *gles20_index_buffer_get_debug_label(IndexBuffer *ibuf) {
return ibuf->debug_label;
}
void gles20_index_buffer_set_debug_label(IndexBuffer *ibuf, const char *label) {
if(label) {
strlcpy(ibuf->debug_label, label, sizeof(ibuf->debug_label));
} else {
snprintf(ibuf->debug_label, sizeof(ibuf->debug_label), "Fake IBO at %p", (void*)ibuf);
}
}
void gles20_index_buffer_set_offset(IndexBuffer *ibuf, size_t offset) {
ibuf->offset = offset;
}
size_t gles20_index_buffer_get_offset(IndexBuffer *ibuf) {
return ibuf->offset;
}
void gles20_index_buffer_add_indices(IndexBuffer *ibuf, size_t data_size, void *data) {
gles20_ibo_index_t *indices = data;
attr_unused size_t num_indices = data_size / sizeof(gles20_ibo_index_t);
assert(ibuf->offset + num_indices - 1 < ibuf->num_elements);
memcpy(ibuf->elements + ibuf->offset, indices, data_size);
ibuf->offset += num_indices;
}
void gles20_index_buffer_destroy(IndexBuffer *ibuf) {
mem_free(ibuf);
}
void gles20_index_buffer_flush(IndexBuffer *ibuf) {
}
void gles20_index_buffer_invalidate(IndexBuffer *ibuf) {
ibuf->offset = 0;
}