big rendering overhaul (cleaning and api abstraction)

This commit is contained in:
veclavtalica
2025-01-14 23:20:54 +03:00
parent b7cb37c06a
commit 5059802d09
25 changed files with 290 additions and 424 deletions

View File

@ -3,7 +3,6 @@
#include "twn_engine_context_c.h"
#include "twn_camera_c.h"
#include "twn_types.h"
#include "twn_util_c.h"
#include "twn_vec.h"
#include "twn_deferred_commands.h"
@ -11,15 +10,19 @@
#include <stb_ds.h>
#include <stddef.h>
#include <math.h>
#include <tgmath.h>
DeferredCommand *deferred_commands;
/* TODO: have a default initialized one */
/* TODO: with buffered render, don't we use camera of wrong frame right now ? */
Matrix4 camera_projection_matrix;
Matrix4 camera_look_at_matrix;
double depth_range_low, depth_range_high;
void render_queue_clear(void) {
text_cache_reset_arena(&ctx.text_cache);
@ -205,8 +208,6 @@ TWN_API void draw_quad(char const *texture,
static void render_2d(void) {
use_2d_pipeline();
const size_t render_queue_len = arrlenu(ctx.render_queue_2d);
struct Render2DInvocation {
@ -362,9 +363,6 @@ static void render_space(void) {
/* nothing to do, abort */
/* as space pipeline isn't used we can have fewer changes and initialization costs */
if (hmlenu(ctx.uncolored_mesh_batches) != 0 || hmlenu(ctx.billboard_batches) != 0) {
use_space_pipeline();
apply_fog();
for (size_t i = 0; i < hmlenu(ctx.uncolored_mesh_batches); ++i) {
finally_draw_uncolored_space_traingle_batch(&ctx.uncolored_mesh_batches[i].value,
ctx.uncolored_mesh_batches[i].key);
@ -373,8 +371,6 @@ static void render_space(void) {
for (size_t i = 0; i < hmlenu(ctx.billboard_batches); ++i) {
finally_draw_billboard_batch(&ctx.billboard_batches[i].value, ctx.billboard_batches[i].key);
}
pop_fog();
}
render_skybox(); /* after everything else, as to use depth buffer for early z rejection */
@ -439,15 +435,8 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
void set_depth_range(double low, double high) {
DeferredCommand const command = {
.type = DEFERRED_COMMAND_TYPE_DEPTH_RANGE,
.depth_range = {
.low = low,
.high = high
}
};
arrpush(deferred_commands, command);
depth_range_low = low;
depth_range_high = high;
}
@ -468,44 +457,9 @@ void clear_draw_buffer(void) {
}
void use_texture_mode(TextureMode mode) {
DeferredCommand const command = {
.type = DEFERRED_COMMAND_TYPE_USE_TEXTURE_MODE,
.use_texture_mode = { mode }
};
arrpush(deferred_commands, command);
}
void use_2d_pipeline(void) {
DeferredCommand const command = {
.type = DEFERRED_COMMAND_TYPE_USE_PIPIELINE,
.use_pipeline = { PIPELINE_2D }
};
arrpush(deferred_commands, command);
}
void use_space_pipeline(void) {
DeferredCommand const command = {
.type = DEFERRED_COMMAND_TYPE_USE_PIPIELINE,
.use_pipeline = { PIPELINE_SPACE }
};
arrpush(deferred_commands, command);
}
void issue_deferred_draw_commands(void) {
for (size_t i = 0; i < arrlenu(deferred_commands); ++i) {
switch (deferred_commands[i].type) {
case DEFERRED_COMMAND_TYPE_DEPTH_RANGE: {
finally_set_depth_range(deferred_commands[i].depth_range);
break;
}
case DEFERRED_COMMAND_TYPE_CLEAR: {
finally_clear_draw_buffer(deferred_commands[i].clear);
break;
@ -521,37 +475,6 @@ void issue_deferred_draw_commands(void) {
break;
}
case DEFERRED_COMMAND_TYPE_USE_PIPIELINE: {
switch (deferred_commands[i].use_pipeline.pipeline) {
case PIPELINE_2D:
finally_use_2d_pipeline();
break;
case PIPELINE_SPACE:
finally_use_space_pipeline();
break;
case PIPELINE_NO:
default:
SDL_assert(false);
}
break;
}
case DEFERRED_COMMAND_TYPE_USE_TEXTURE_MODE: {
finally_use_texture_mode(deferred_commands[i].use_texture_mode.mode);
break;
}
case DEFERRED_COMMAND_TYPE_APPLY_FOG: {
finally_apply_fog(deferred_commands[i].apply_fog);
break;
}
case DEFERRED_COMMAND_TYPE_POP_FOG: {
finally_pop_fog();
break;
}
default:
SDL_assert(false);
}