diff options
| -rw-r--r-- | src/rendering/include/engine/rendering/rendering.h | 13 | ||||
| -rw-r--r-- | src/rendering/src/gl.c | 4 | ||||
| -rw-r--r-- | src/rendering/src/rendering.c | 200 |
3 files changed, 68 insertions, 149 deletions
diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index d2d17d5..2246601 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -15,9 +15,8 @@ extern "C" { #include <cglm/cglm.h> /* Definitions */ -#define RGBA(_r, _g, _b, _a) \ - ((Engine_color){.r = _r, .g = _g, .b = _b, .a = _a}) -#define RGB(_r, _g, _b) RGBA(_r, _g, _b, 0xFF) +#define RGBA(_r, _g, _b, _a) ((Engine_color){.r = _r, .g = _g, .b = _b, .a = _a}) +#define RGB(_r, _g, _b) RGBA(_r, _g, _b, 0xFF) /* Types */ /* TODO: Cleanup these types. */ @@ -43,7 +42,8 @@ typedef struct { typedef enum { Shader_Error, - Shader_Program, /* Collection of shaders */ + + Shader_Program, Shader_Vertex, Shader_Tessellation, Shader_Geometry, @@ -70,7 +70,7 @@ typedef enum { typedef struct { // The backend ID, ie. glGenBuffer(numBufferObjects, &this->buffername) u32 buffername; - // The size of data = count * size_elem + // Buffer size of `data`. To get the size of the actual data, size_elem * count isize size; // Number of elements isize count; @@ -153,8 +153,7 @@ typedef struct { } Camera; -const -usize ShaderBufferDataType_size(ShaderBufferDataType t); +const usize ShaderBufferDataType_size(ShaderBufferDataType t); /* Rendering functions */ void render_begin(Window* w); diff --git a/src/rendering/src/gl.c b/src/rendering/src/gl.c index 5cde439..7a04dbb 100644 --- a/src/rendering/src/gl.c +++ b/src/rendering/src/gl.c @@ -200,9 +200,11 @@ RenderObject RenderObject_new( /* For each buffer in the shader, */ /* The shader should be generalied, */ for (usize i = 0; i < num_buffers; i++) { + const isize sz = buffers[i].size_elem * buffers[i].count; + gl->GenBuffers(1, &(buffers[i].buffername)); gl->BindBuffer(GL_ARRAY_BUFFER, buffers[i].buffername); - gl->BufferData(GL_ARRAY_BUFFER, buffers[i].size, buffers[i].data, GL_STATIC_DRAW); + gl->BufferData(GL_ARRAY_BUFFER, sz, buffers[i].data, GL_STATIC_DRAW); } o.shader = *shader; diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index b0f8d5d..0c1052a 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -73,14 +73,32 @@ int renderbatch_new(RenderBatch* renderbatch, isize count) { return 0; } -// Add a render object to the render batch +// Appends the data in src onto dst. More space for `data` is allocated if +// necessary, in which case a pointer to the new ShaderBuffer is returned. +ShaderBuffer* shaderbuffer_cat(ShaderBuffer* dst, ShaderBuffer *restrict src) { + if (dst->datatype != src->datatype) { + ERROR("Failed to concatenate shader buffers, incompatible datatypes: %d != %d", dst->datatype, src->datatype); + } + if (dst->components != src->components) { + ERROR("Failed to concatenate shader buffers, incompatible number of components: %d != %d", dst->components, src->components); + } + + // Assume that we single-handedly control the pointer to the data, copy and + // free the stuff. + // + return dst; +} + +// Add a render object to the render batch. +// In reality we only add the buffer stuff to each of the batchs' renderobject +// buffers int renderbatch_add(RenderBatch* renderbatch, RenderObject* obj) { if (renderbatch == NULL) { ERROR("renderbatch was null!"); return -1; } - // Check if it is initialized + // Check if we have initialized models array if (renderbatch->models == NULL) { const isize sz = 8 * sizeof(RenderObject); renderbatch->models = malloc(8 * sizeof(RenderObject)); @@ -94,11 +112,40 @@ int renderbatch_add(RenderBatch* renderbatch, RenderObject* obj) { renderbatch->msize = sz; } - // Initialize renderobject buffer - if (renderbatch->renderobj.buffer == NULL) { - renderbatch->renderobj.buffer = malloc(sizeof(ShaderBuffer)); + // If this is the first object, copy all its rendering params + if (renderbatch->mcount == 0) { + renderbatch->renderobj.shader = obj->shader; + renderbatch->renderobj.texture = obj->texture; + renderbatch->renderobj.buffer_len = obj->buffer_len; + renderbatch->renderobj.buffer = calloc(obj->buffer_len, sizeof(ShaderBuffer)); + + for (isize i = 0; i < renderbatch->renderobj.buffer_len; i++) { + const isize sz = obj->buffer[i].size_elem * obj->buffer[i].count; + + renderbatch->renderobj.buffer[i].count += obj->buffer[i].count; + renderbatch->renderobj.buffer[i].buffername = 0; + + // Copy meta data + renderbatch->renderobj.buffer[i].components = obj->buffer[i].components; + renderbatch->renderobj.buffer[i].datatype = obj->buffer[i].datatype; + renderbatch->renderobj.buffer[i].size_elem = obj->buffer[i].size_elem; + + // Round up to nearest multiple of 4096 byte + const isize newsz = (1 + (sz / 4096)) * 4096; + renderbatch->renderobj.buffer[i].size = newsz; + renderbatch->renderobj.buffer[i].data = malloc(newsz); + memcpy(renderbatch->renderobj.buffer[i].data, obj->buffer[i].data, sz); + } + + } else if (renderbatch->renderobj.buffer_len != obj->buffer_len) { + ERROR("Mismatch in number of shader buffers in render objects! Assumed %d but got %d", renderbatch->renderobj.buffer_len, obj->buffer_len); + + // Append all ShaderBuffers to their appropriate buffers. + } else for (isize i = 0; i < renderbatch->renderobj.buffer_len; i++) { + shaderbuffer_cat(&renderbatch->renderobj.buffer[i], &obj->buffer[i]); } + /* // If this is the first object, copy all its rendering params if (renderbatch->mcount == 0) { // Copy the first objects shaderstuff @@ -108,16 +155,17 @@ int renderbatch_add(RenderBatch* renderbatch, RenderObject* obj) { renderbatch->renderobj.texture = obj->texture; renderbatch->renderobj.buffer_len = obj->buffer_len; } + */ - if (renderbatch->mcount != 0) { - renderbatch->renderobj.buffer_len += obj->buffer_len; - } + //if (renderbatch->mcount != 0) { + //renderbatch->renderobj.buffer_len += obj->buffer_len; + //} renderbatch->mcount++; return 0; } -// renderbatch_refresh: Copy all models in the renderbatch to the batchs model. +// renderbatch_refresh: Copy all models in the renderbatch to the batchs' model. int renderbatch_refresh(RenderBatch* renderbatch) { isize offset = 0; for (isize i = 0; i < renderbatch->mcount; i++) { @@ -130,137 +178,6 @@ int renderbatch_refresh(RenderBatch* renderbatch) { } - -//RenderBatch renderobject_extend(RenderObject b, RenderObject *restrict object) { -//} -//RenderBatch renderbatch_add(RenderBatch b, RenderObject *restrict object) { -// -// if (b.models == NULL) { -// b = renderbatch_new(64); -// } -// -// // Check to see if we have room for the new object -// if ((b.mcount + 1) * sizeof(RenderObject) > b.msize) { -// // grow the size -// b.models = realloc(b.models, b.msize * 2); -// b.msize *= 2; -// } -// -// // Add the model to the tracking list -// b.models[b.mcount] = *object; -// b.mcount++; -// -// // Update the vertex buffer -// if (b.vertices == NULL) { -// isize newsz = sizeof(f32) * count; -// b.vertices = malloc(newsz); -// b.vsize = newsz; -// -// } else if (sizeof(f32) * (b.vcount + count) > b.vsize) { -// // Grow the vertex buffer size -// isize newsz = b.vsize * 2; -// while (newsz < sizeof(f32) * count + b.vsize) { -// newsz *= 2; -// } -// -// b.vertices = realloc(&b.vertices, newsz); -// b.vsize = newsz; -// } -// -// // Copy the new buffer data -// memcpy(&b.vertices[b.vcount], vertices, count * sizeof(f32)); -// b.vcount += count; -// -// const GladGLContext *restrict gl = GLOBAL_PLATFORM->window->context; -// gl->BindBuffer(GL_ARRAY_BUFFER, b.vertexbuffer); -// gl->BufferData(GL_ARRAY_BUFFER, b.vcount, b.vertices, GL_DYNAMIC_DRAW); -// gl->VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0); -// -// return b; -//} -// -//RenderBatch renderbatch_add_ex(RenderBatch b, -// Shader* shader, -// u32 texture, -// const isize num, const isize *count, f32** vertices, ShaderBuffer *restrict buffers, usize num_buffers) { -// /* -// * !!!README!!! TODO FIXME -// * it just occurred to me.. -// * -// * We already do the hard work in renderobject, and can maybe suffice to use -// * that, and use all of the renderbatch code to squish together the vertices -// * and other buffers together to a single buffer? -// * -// * So we will have a bunch of pointers to the render objects, -// * then create a "batched" render object, with the data -// * -// * -// * -// * -// * */ -// -// RenderModel *mm = NULL; -// -// if (b.models == NULL) { -// b = renderbatch_new(num); -// } -// -// if ((b.mcount + num) * sizeof(RenderModel) > b.msize) { -// // grow the size -// b.models = realloc(b.models, (b.mcount + num) * sizeof(RenderModel)); -// b.msize += num * sizeof(RenderModel); -// } -// -// // Add the models to the tracking list -// isize vcount = 0; -// for (isize i = 0; i < num; i++) { -// b.models[b.mcount + i] = (RenderModel){.count = count[i], .vertices = vertices[i]}; -// vcount = count[i]; -// } -// -// b.mcount += num; -// -// // Update the vertex buffer -// if (b.vertices == NULL) { -// isize newsz = sizeof(f32) * vcount; -// b.vertices = malloc(newsz); -// b.vsize = newsz; -// -// } else if (sizeof(f32) * (b.vcount + vcount) > b.vsize) { -// // Grow the vertex buffer size by doubling it (a couple of times) -// isize newsz = b.vsize * 2; -// while (newsz < sizeof(f32) * vcount + b.vsize) { -// newsz *= 2; -// } -// -// b.vertices = realloc(&b.vertices, newsz); -// b.vsize = newsz; -// } -// -// // Copy the new buffer data -// for (isize i = 0; i < num; i++) { -// memcpy(&b.vertices[b.vcount], vertices[i], count[i] * sizeof(f32)); -// b.vcount += count[i]; -// } -// -// const GladGLContext *restrict gl = GLOBAL_PLATFORM->window->context; -// gl->BindBuffer(GL_ARRAY_BUFFER, b.vertexbuffer); -// gl->BufferData(GL_ARRAY_BUFFER, b.vcount, b.vertices, GL_DYNAMIC_DRAW); -// gl->VertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0); -// -// return b; -//} -// -//void renderbatch_update(RenderBatch b, f32** vertices) { -// // TODO fixup this mess -// isize acc = 0; -// for (isize i = 0; i < b.mcount; i++) { -// const isize sz = b.models[i].count * sizeof(f32); -// memcpy(&b.vertices[acc], b.models[i].vertices, sz); -// acc += sz; -// } -//} - /* Implementations */ /* Clear the screen, @@ -346,7 +263,8 @@ void render_present(Window* w) { // Draw the model ! // TODO: Use DrawElements and an index buffer! - gl->DrawArrays(GL_TRIANGLES, 0, o->buffer->size); // Starting from vertex 0; 3 vertices total -> 1 triangle + const isize sz = o->buffer->count * o->buffer->size_elem; + gl->DrawArrays(GL_TRIANGLES, 0, sz); // Starting from vertex 0; 3 vertices total -> 1 triangle for (usize i = 0; i < o->buffer_len; i++) { gl->DisableVertexAttribArray(i); |
