rendering.c: fast cos from sin calculation, with lossy fast_sqrt()

This commit is contained in:
2024-07-28 14:39:23 +03:00
parent c59708d619
commit ea4d12212c
3 changed files with 154 additions and 129 deletions

View File

@ -5,6 +5,7 @@
#include "../textures.h"
#include "../rendering.h"
#include "../context.h"
#include "../util.h"
#include "quad_element_buffer.h"
#include <stb_ds.h>
@ -19,7 +20,7 @@
* before anything is really rendered
*/
/* TODO: it might make sense to infer alpha channel presence / meaningfulness for textures in atlas */
/* so that they are rendered with no blend / batched in a way to reduce overdraw automatically */
/* so that they are rendered with no blend / batched in a way to reduce overdraw automatically */
void push_sprite(char *path, t_frect rect) {
struct sprite_primitive sprite = {
.rect = rect,
@ -104,7 +105,7 @@ static void render_sprites(const struct primitive_2d primitives[],
const size_t len,
const bool reversed)
{
/* single vertex array is used for every batch with NULL glBufferData() trick at the end */
/* single vertex array is used for every batch with NULL glBufferData() trick at the end */
static GLuint vertex_array = 0;
if (vertex_array == 0)
glGenBuffers(1, &vertex_array);
@ -128,103 +129,104 @@ static void render_sprites(const struct primitive_2d primitives[],
const size_t cur = reversed ? len - i - 1: i;
const struct sprite_primitive sprite = primitives[cur].sprite;
const t_rect srcrect =
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
const t_rect srcrect =
textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key);
const float wr = (float)srcrect.w / (float)dims.w;
const float hr = (float)srcrect.h / (float)dims.h;
const float xr = (float)srcrect.x / (float)dims.w;
const float yr = (float)srcrect.y / (float)dims.h;
const float wr = (float)srcrect.w / (float)dims.w;
const float hr = (float)srcrect.h / (float)dims.h;
const float xr = (float)srcrect.x / (float)dims.w;
const float yr = (float)srcrect.y / (float)dims.h;
/* non-rotated case */
if (sprite.rotation == 0.0f) {
payload[i] = (struct sprite_primitive_payload) {
/* upper-left */
.v0 = {
sprite.rect.x,
sprite.rect.y },
.uv0 = {
xr + wr * sprite.flip_x,
yr + hr * sprite.flip_y, },
/* non-rotated case */
if (sprite.rotation == 0.0f) {
payload[i] = (struct sprite_primitive_payload) {
/* upper-left */
.v0 = {
sprite.rect.x,
sprite.rect.y },
.uv0 = {
xr + wr * sprite.flip_x,
yr + hr * sprite.flip_y, },
/* bottom-left */
.v1 = {
(sprite.rect.x),
(sprite.rect.y + sprite.rect.h) },
.uv1 = {
xr + wr * sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-left */
.v1 = {
(sprite.rect.x),
(sprite.rect.y + sprite.rect.h) },
.uv1 = {
xr + wr * sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-right */
.v2 = {
(sprite.rect.x + sprite.rect.w),
(sprite.rect.y + sprite.rect.h) },
.uv2 = {
xr + wr * !sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-right */
.v2 = {
(sprite.rect.x + sprite.rect.w),
(sprite.rect.y + sprite.rect.h) },
.uv2 = {
xr + wr * !sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* upper-right */
.v3 = {
(sprite.rect.x + sprite.rect.w),
(sprite.rect.y) },
.uv3 = {
xr + wr * !sprite.flip_x,
yr + hr * sprite.flip_y, },
/* upper-right */
.v3 = {
(sprite.rect.x + sprite.rect.w),
(sprite.rect.y) },
.uv3 = {
xr + wr * !sprite.flip_x,
yr + hr * sprite.flip_y, },
/* equal for all (flat shaded) */
.c0 = sprite.color,
.c1 = sprite.color,
.c2 = sprite.color,
.c3 = sprite.color,
};
} else {
/* rotated case */
const t_fvec2 c = frect_center(sprite.rect);
const t_fvec2 d = {
.x = (cosf(sprite.rotation + (float)M_PI_4) * sprite.rect.w) * (float)M_SQRT1_2,
.y = (sinf(sprite.rotation + (float)M_PI_4) * sprite.rect.h) * (float)M_SQRT1_2,
};
/* equal for all (flat shaded) */
.c0 = sprite.color,
.c1 = sprite.color,
.c2 = sprite.color,
.c3 = sprite.color,
};
} else {
/* rotated case */
const t_fvec2 c = frect_center(sprite.rect);
const t_fvec2 t = fast_cossine(sprite.rotation + (float)M_PI_4);
const t_fvec2 d = {
.x = t.x * sprite.rect.w * (float)M_SQRT1_2,
.y = t.y * sprite.rect.h * (float)M_SQRT1_2,
};
payload[i] = (struct sprite_primitive_payload) {
/* upper-left */
.v0 = {
c.x - d.x,
c.y - d.y },
.uv0 = {
xr + wr * sprite.flip_x,
yr + hr * sprite.flip_y, },
payload[i] = (struct sprite_primitive_payload) {
/* upper-left */
.v0 = {
c.x - d.x,
c.y - d.y },
.uv0 = {
xr + wr * sprite.flip_x,
yr + hr * sprite.flip_y, },
/* bottom-left */
.v1 = {
c.x - d.y,
c.y + d.x },
.uv1 = {
xr + wr * sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-left */
.v1 = {
c.x - d.y,
c.y + d.x },
.uv1 = {
xr + wr * sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-right */
.v2 = {
c.x + d.x,
c.y + d.y },
.uv2 = {
xr + wr * !sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* bottom-right */
.v2 = {
c.x + d.x,
c.y + d.y },
.uv2 = {
xr + wr * !sprite.flip_x,
yr + hr * !sprite.flip_y, },
/* upper-right */
.v3 = {
c.x + d.y,
c.y - d.x },
.uv3 = {
xr + wr * !sprite.flip_x,
yr + hr * sprite.flip_y, },
/* upper-right */
.v3 = {
c.x + d.y,
c.y - d.x },
.uv3 = {
xr + wr * !sprite.flip_x,
yr + hr * sprite.flip_y, },
/* equal for all (flat shaded) */
.c0 = sprite.color,
.c1 = sprite.color,
.c2 = sprite.color,
.c3 = sprite.color,
};
}
/* equal for all (flat shaded) */
.c0 = sprite.color,
.c1 = sprite.color,
.c2 = sprite.color,
.c3 = sprite.color,
};
}
}
glUnmapBuffer(GL_ARRAY_BUFFER);