make deferred space triangles work

This commit is contained in:
veclav talica 2024-10-19 20:02:39 +03:00
parent a7b09b9f39
commit c49789f1f4

View File

@ -104,6 +104,9 @@ typedef struct {
GLsizei element_count;
GLsizei range_start, range_end;
/* could be either `element_count` with supplied `element_buffer`, or this, but not both */
GLsizei primitive_count;
double depth_range_low, depth_range_high;
} DeferredCommandDraw;
@ -228,8 +231,7 @@ static void issue_deferred_draw_commands(void) {
/* TODO: don't assume a single vertex array ? */
SDL_assert(command.vertices.arity != 0);
SDL_assert(command.vertices.buffer);
SDL_assert(command.element_count != 0);
SDL_assert(command.element_buffer);
SDL_assert((command.element_buffer && command.element_count != 0) || command.primitive_count != 0);
glBindBuffer(GL_ARRAY_BUFFER, command.vertices.buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, command.element_buffer);
@ -274,6 +276,8 @@ static void issue_deferred_draw_commands(void) {
textures_bind(&ctx.texture_cache, command.texture_key);
}
if (command.element_buffer) {
SDL_assert(command.element_count != 0);
if (command.range_start == command.range_end)
glDrawElements(GL_TRIANGLES, command.element_count, GL_UNSIGNED_SHORT, NULL);
else
@ -283,6 +287,10 @@ static void issue_deferred_draw_commands(void) {
command.element_count,
GL_UNSIGNED_SHORT,
NULL);
} else {
SDL_assert(command.primitive_count != 0);
glDrawArrays(GL_TRIANGLES, 0, command.primitive_count);
}
/* state clearing */
@ -735,37 +743,36 @@ void finally_draw_uncolored_space_traingle_batch(const MeshBatch *batch,
const TextureKey texture_key,
const VertexBuffer buffer)
{
const size_t primitives_len = arrlenu(batch->primitives);
DeferredCommandDraw command = {0};
textures_bind(&ctx.texture_cache, texture_key);
command.vertices = (AttributeArrayPointer) {
.arity = 3,
.type = GL_FLOAT,
.stride = offsetof(struct UncoloredSpaceTrianglePayload, v1),
.offset = offsetof(struct UncoloredSpaceTrianglePayload, v0),
.buffer = buffer
};
use_texture_mode(textures_get_mode(&ctx.texture_cache, texture_key));
command.texcoords = (AttributeArrayPointer) {
.arity = 2,
.type = GL_FLOAT,
.stride = offsetof(struct UncoloredSpaceTrianglePayload, v1),
.offset = offsetof(struct UncoloredSpaceTrianglePayload, uv0),
.buffer = buffer
};
glBindBuffer(GL_ARRAY_BUFFER, buffer);
command.textured = true;
command.texture_key = texture_key;
/* vertex specification*/
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,
GL_FLOAT,
offsetof(struct UncoloredSpaceTrianglePayload, v1),
(void *)offsetof(struct UncoloredSpaceTrianglePayload, v0));
const GLuint primitives_len = arrlenu(batch->primitives);
command.primitive_count = 3 * primitives_len;
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClientActiveTexture(GL_TEXTURE0);
glTexCoordPointer(2,
GL_FLOAT,
offsetof(struct UncoloredSpaceTrianglePayload, v1),
(void *)offsetof(struct UncoloredSpaceTrianglePayload, uv0));
DeferredCommand final_command = {
.type = DEFERRED_COMMAND_TYPE_DRAW,
.draw = command
};
/* commit for drawing */
glDrawArrays(GL_TRIANGLES, 0, 3 * (GLint)primitives_len);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
/* invalidate the buffer immediately */
glBufferData(GL_ARRAY_BUFFER, 0, NULL, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
arrpush(deferred_commands, final_command);
}