2021-04-20 21:54:05 +02:00
|
|
|
#include <stddef.h>
|
2021-03-31 17:33:35 +02:00
|
|
|
#include <stdint.h>
|
2019-12-28 12:16:32 +01:00
|
|
|
#include <stdio.h>
|
2021-12-16 02:31:24 +01:00
|
|
|
#include <stdbool.h>
|
2022-04-15 17:34:46 +02:00
|
|
|
#include <string.h>
|
2021-05-21 23:22:46 +02:00
|
|
|
|
2022-11-16 04:23:45 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#ifndef alignof
|
|
|
|
#define alignof _Alignof
|
|
|
|
#endif
|
|
|
|
/* max_align_t is a compiler defined type, but
|
|
|
|
** MSVC doesn't provide it, so we'll have to improvise */
|
|
|
|
typedef long double max_align_t;
|
|
|
|
#else
|
|
|
|
#include <stdalign.h>
|
|
|
|
#endif
|
|
|
|
|
2021-05-21 23:22:46 +02:00
|
|
|
#include <lauxlib.h>
|
2019-12-28 12:16:32 +01:00
|
|
|
#include "rencache.h"
|
2023-03-19 20:39:52 +01:00
|
|
|
#include "renwindow.h"
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-07 23:24:12 +02:00
|
|
|
/* a cache over the software renderer -- all drawing operations are stored as
|
|
|
|
** commands when issued. At the end of the frame we write the commands to a grid
|
|
|
|
** of hash values, take the cells that have changed since the previous frame,
|
2019-12-28 12:16:32 +01:00
|
|
|
** merge them into dirty rectangles and redraw only those regions */
|
|
|
|
|
|
|
|
#define CELLS_X 80
|
|
|
|
#define CELLS_Y 50
|
|
|
|
#define CELL_SIZE 96
|
2023-01-02 03:12:14 +01:00
|
|
|
#define CMD_BUF_RESIZE_RATE 1.2
|
|
|
|
#define CMD_BUF_INIT_SIZE (1024 * 512)
|
2023-03-04 21:23:09 +01:00
|
|
|
#define COMMAND_BARE_SIZE offsetof(Command, command)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2023-03-04 21:23:09 +01:00
|
|
|
enum CommandType { SET_CLIP, DRAW_TEXT, DRAW_RECT };
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
enum CommandType type;
|
|
|
|
uint32_t size;
|
|
|
|
/* Commands *must* always begin with a RenRect
|
|
|
|
** This is done to ensure alignment */
|
|
|
|
RenRect command[];
|
|
|
|
} Command;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RenRect rect;
|
|
|
|
} SetClipCommand;
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RenRect rect;
|
|
|
|
RenColor color;
|
2021-10-13 05:24:52 +02:00
|
|
|
RenFont *fonts[FONT_FALLBACK_MAX];
|
2021-09-25 18:55:20 +02:00
|
|
|
float text_x;
|
2022-12-26 18:49:07 +01:00
|
|
|
size_t len;
|
2023-03-04 21:23:09 +01:00
|
|
|
int8_t tab_size;
|
2022-04-15 17:34:46 +02:00
|
|
|
char text[];
|
2023-03-04 21:23:09 +01:00
|
|
|
} DrawTextCommand;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
RenRect rect;
|
|
|
|
RenColor color;
|
|
|
|
} DrawRectCommand;
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
static unsigned cells_buf1[CELLS_X * CELLS_Y];
|
|
|
|
static unsigned cells_buf2[CELLS_X * CELLS_Y];
|
|
|
|
static unsigned *cells_prev = cells_buf1;
|
|
|
|
static unsigned *cells = cells_buf2;
|
|
|
|
static RenRect rect_buf[CELLS_X * CELLS_Y / 2];
|
2023-01-02 03:12:14 +01:00
|
|
|
static bool resize_issue;
|
2019-12-28 12:16:32 +01:00
|
|
|
static RenRect screen_rect;
|
2023-01-19 04:15:26 +01:00
|
|
|
static RenRect last_clip_rect;
|
2019-12-28 12:16:32 +01:00
|
|
|
static bool show_debug;
|
|
|
|
|
2022-11-16 04:23:45 +01:00
|
|
|
static inline int rencache_min(int a, int b) { return a < b ? a : b; }
|
|
|
|
static inline int rencache_max(int a, int b) { return a > b ? a : b; }
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-05-21 23:22:46 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
/* 32bit fnv-1a hash */
|
|
|
|
#define HASH_INITIAL 2166136261
|
|
|
|
|
|
|
|
static void hash(unsigned *h, const void *data, int size) {
|
|
|
|
const unsigned char *p = data;
|
|
|
|
while (size--) {
|
|
|
|
*h = (*h ^ *p++) * 16777619;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline int cell_idx(int x, int y) {
|
|
|
|
return x + y * CELLS_X;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline bool rects_overlap(RenRect a, RenRect b) {
|
|
|
|
return b.x + b.width >= a.x && b.x <= a.x + a.width
|
|
|
|
&& b.y + b.height >= a.y && b.y <= a.y + a.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static RenRect intersect_rects(RenRect a, RenRect b) {
|
2022-11-16 04:23:45 +01:00
|
|
|
int x1 = rencache_max(a.x, b.x);
|
|
|
|
int y1 = rencache_max(a.y, b.y);
|
|
|
|
int x2 = rencache_min(a.x + a.width, b.x + b.width);
|
|
|
|
int y2 = rencache_min(a.y + a.height, b.y + b.height);
|
|
|
|
return (RenRect) { x1, y1, rencache_max(0, x2 - x1), rencache_max(0, y2 - y1) };
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static RenRect merge_rects(RenRect a, RenRect b) {
|
2022-11-16 04:23:45 +01:00
|
|
|
int x1 = rencache_min(a.x, b.x);
|
|
|
|
int y1 = rencache_min(a.y, b.y);
|
|
|
|
int x2 = rencache_max(a.x + a.width, b.x + b.width);
|
|
|
|
int y2 = rencache_max(a.y + a.height, b.y + b.height);
|
2019-12-28 12:16:32 +01:00
|
|
|
return (RenRect) { x1, y1, x2 - x1, y2 - y1 };
|
|
|
|
}
|
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
static bool expand_command_buffer(RenWindow *window_renderer) {
|
|
|
|
size_t new_size = window_renderer->command_buf_size * CMD_BUF_RESIZE_RATE;
|
2023-01-02 03:12:14 +01:00
|
|
|
if (new_size == 0) {
|
|
|
|
new_size = CMD_BUF_INIT_SIZE;
|
|
|
|
}
|
2023-06-16 16:19:52 +02:00
|
|
|
uint8_t *new_command_buf = realloc(window_renderer->command_buf, new_size);
|
2023-01-02 03:12:14 +01:00
|
|
|
if (!new_command_buf) {
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-16 16:19:52 +02:00
|
|
|
window_renderer->command_buf_size = new_size;
|
|
|
|
window_renderer->command_buf = new_command_buf;
|
2023-01-02 03:12:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
static void* push_command(RenWindow *window_renderer, enum CommandType type, int size) {
|
2023-01-02 03:12:14 +01:00
|
|
|
if (resize_issue) {
|
|
|
|
// Don't push new commands as we had problems resizing the command buffer.
|
|
|
|
// Let's wait for the next frame.
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-10-28 08:40:18 +02:00
|
|
|
size_t alignment = alignof(max_align_t) - 1;
|
2023-03-04 21:23:09 +01:00
|
|
|
size += COMMAND_BARE_SIZE;
|
2021-10-28 08:40:18 +02:00
|
|
|
size = (size + alignment) & ~alignment;
|
2023-06-16 16:19:52 +02:00
|
|
|
int n = window_renderer->command_buf_idx + size;
|
|
|
|
while (n > window_renderer->command_buf_size) {
|
|
|
|
if (!expand_command_buffer(window_renderer)) {
|
2023-04-23 10:35:40 +02:00
|
|
|
fprintf(stderr, "Warning: (" __FILE__ "): unable to resize command buffer (%zu)\n",
|
2023-06-16 16:19:52 +02:00
|
|
|
(size_t)(window_renderer->command_buf_size * CMD_BUF_RESIZE_RATE));
|
2023-01-02 03:12:14 +01:00
|
|
|
resize_issue = true;
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
2023-06-16 16:19:52 +02:00
|
|
|
Command *cmd = (Command*) (window_renderer->command_buf + window_renderer->command_buf_idx);
|
|
|
|
window_renderer->command_buf_idx = n;
|
2022-01-29 21:19:22 +01:00
|
|
|
memset(cmd, 0, size);
|
2019-12-28 12:16:32 +01:00
|
|
|
cmd->type = type;
|
|
|
|
cmd->size = size;
|
2023-03-04 21:23:09 +01:00
|
|
|
return cmd->command;
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
static bool next_command(RenWindow *window_renderer, Command **prev) {
|
2019-12-28 12:16:32 +01:00
|
|
|
if (*prev == NULL) {
|
2023-06-16 16:19:52 +02:00
|
|
|
*prev = (Command*) window_renderer->command_buf;
|
2019-12-28 12:16:32 +01:00
|
|
|
} else {
|
|
|
|
*prev = (Command*) (((char*) *prev) + (*prev)->size);
|
|
|
|
}
|
2023-06-16 16:19:52 +02:00
|
|
|
return *prev != ((Command*) (window_renderer->command_buf + window_renderer->command_buf_idx));
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void rencache_show_debug(bool enable) {
|
|
|
|
show_debug = enable;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
void rencache_set_clip_rect(RenWindow *window_renderer, RenRect rect) {
|
|
|
|
SetClipCommand *cmd = push_command(window_renderer, SET_CLIP, sizeof(SetClipCommand));
|
2023-01-19 04:15:26 +01:00
|
|
|
if (cmd) {
|
|
|
|
cmd->rect = intersect_rects(rect, screen_rect);
|
|
|
|
last_clip_rect = cmd->rect;
|
|
|
|
}
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-16 16:19:52 +02:00
|
|
|
void rencache_draw_rect(RenWindow *window_renderer, RenRect rect, RenColor color) {
|
2023-01-19 04:15:26 +01:00
|
|
|
if (rect.width == 0 || rect.height == 0 || !rects_overlap(last_clip_rect, rect)) {
|
2021-10-10 14:58:51 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-06-16 16:19:52 +02:00
|
|
|
DrawRectCommand *cmd = push_command(window_renderer, DRAW_RECT, sizeof(DrawRectCommand));
|
2020-05-14 09:52:07 +02:00
|
|
|
if (cmd) {
|
|
|
|
cmd->rect = rect;
|
|
|
|
cmd->color = color;
|
|
|
|
}
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
2023-03-09 16:48:46 +01:00
|
|
|
double rencache_draw_text(RenWindow *window_renderer, RenFont **fonts, const char *text, size_t len, double x, int y, RenColor color)
|
2021-03-31 17:33:35 +02:00
|
|
|
{
|
2023-03-09 16:48:46 +01:00
|
|
|
double width = ren_font_group_get_width(window_renderer, fonts, text, len);
|
2021-10-13 05:24:52 +02:00
|
|
|
RenRect rect = { x, y, (int)width, ren_font_group_get_height(fonts) };
|
2023-01-19 04:15:26 +01:00
|
|
|
if (rects_overlap(last_clip_rect, rect)) {
|
2022-12-26 18:49:07 +01:00
|
|
|
int sz = len + 1;
|
2023-06-16 16:19:52 +02:00
|
|
|
DrawTextCommand *cmd = push_command(window_renderer, DRAW_TEXT, sizeof(DrawTextCommand) + sz);
|
2020-05-14 09:52:07 +02:00
|
|
|
if (cmd) {
|
|
|
|
memcpy(cmd->text, text, sz);
|
|
|
|
cmd->color = color;
|
2021-10-13 05:24:52 +02:00
|
|
|
memcpy(cmd->fonts, fonts, sizeof(RenFont*)*FONT_FALLBACK_MAX);
|
2020-05-14 09:52:07 +02:00
|
|
|
cmd->rect = rect;
|
2021-09-25 18:55:20 +02:00
|
|
|
cmd->text_x = x;
|
2022-12-26 18:49:07 +01:00
|
|
|
cmd->len = len;
|
2021-10-13 05:24:52 +02:00
|
|
|
cmd->tab_size = ren_font_group_get_tab_size(fonts);
|
2020-05-14 09:52:07 +02:00
|
|
|
}
|
2020-05-14 09:43:27 +02:00
|
|
|
}
|
2021-09-25 06:35:55 +02:00
|
|
|
return x + width;
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-22 10:00:48 +02:00
|
|
|
void rencache_invalidate(void) {
|
|
|
|
memset(cells_prev, 0xff, sizeof(cells_buf1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-22 04:11:42 +01:00
|
|
|
void rencache_begin_frame(RenWindow *window_renderer) {
|
2019-12-28 12:16:32 +01:00
|
|
|
/* reset all cells if the screen width/height has changed */
|
|
|
|
int w, h;
|
2023-01-02 03:12:14 +01:00
|
|
|
resize_issue = false;
|
2023-01-22 04:11:42 +01:00
|
|
|
ren_get_size(window_renderer, &w, &h);
|
2019-12-28 12:16:32 +01:00
|
|
|
if (screen_rect.width != w || h != screen_rect.height) {
|
|
|
|
screen_rect.width = w;
|
|
|
|
screen_rect.height = h;
|
2020-05-22 10:00:48 +02:00
|
|
|
rencache_invalidate();
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
2023-01-19 04:15:26 +01:00
|
|
|
last_clip_rect = screen_rect;
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void update_overlapping_cells(RenRect r, unsigned h) {
|
|
|
|
int x1 = r.x / CELL_SIZE;
|
|
|
|
int y1 = r.y / CELL_SIZE;
|
|
|
|
int x2 = (r.x + r.width) / CELL_SIZE;
|
|
|
|
int y2 = (r.y + r.height) / CELL_SIZE;
|
|
|
|
|
|
|
|
for (int y = y1; y <= y2; y++) {
|
|
|
|
for (int x = x1; x <= x2; x++) {
|
|
|
|
int idx = cell_idx(x, y);
|
|
|
|
hash(&cells[idx], &h, sizeof(h));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void push_rect(RenRect r, int *count) {
|
|
|
|
/* try to merge with existing rectangle */
|
|
|
|
for (int i = *count - 1; i >= 0; i--) {
|
|
|
|
RenRect *rp = &rect_buf[i];
|
|
|
|
if (rects_overlap(*rp, r)) {
|
|
|
|
*rp = merge_rects(*rp, r);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* couldn't merge with previous rectangle: push */
|
|
|
|
rect_buf[(*count)++] = r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-22 04:11:42 +01:00
|
|
|
void rencache_end_frame(RenWindow *window_renderer) {
|
2019-12-28 12:16:32 +01:00
|
|
|
/* update cells from commands */
|
|
|
|
Command *cmd = NULL;
|
|
|
|
RenRect cr = screen_rect;
|
2023-06-16 16:19:52 +02:00
|
|
|
while (next_command(window_renderer, &cmd)) {
|
2023-03-04 21:23:09 +01:00
|
|
|
/* cmd->command[0] should always be the Command rect */
|
|
|
|
if (cmd->type == SET_CLIP) { cr = cmd->command[0]; }
|
|
|
|
RenRect r = intersect_rects(cmd->command[0], cr);
|
2019-12-28 12:16:32 +01:00
|
|
|
if (r.width == 0 || r.height == 0) { continue; }
|
|
|
|
unsigned h = HASH_INITIAL;
|
|
|
|
hash(&h, cmd, cmd->size);
|
|
|
|
update_overlapping_cells(r, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push rects for all cells changed from last frame, reset cells */
|
|
|
|
int rect_count = 0;
|
|
|
|
int max_x = screen_rect.width / CELL_SIZE + 1;
|
|
|
|
int max_y = screen_rect.height / CELL_SIZE + 1;
|
|
|
|
for (int y = 0; y < max_y; y++) {
|
|
|
|
for (int x = 0; x < max_x; x++) {
|
|
|
|
/* compare previous and current cell for change */
|
|
|
|
int idx = cell_idx(x, y);
|
|
|
|
if (cells[idx] != cells_prev[idx]) {
|
|
|
|
push_rect((RenRect) { x, y, 1, 1 }, &rect_count);
|
|
|
|
}
|
|
|
|
cells_prev[idx] = HASH_INITIAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* expand rects from cells to pixels */
|
|
|
|
for (int i = 0; i < rect_count; i++) {
|
|
|
|
RenRect *r = &rect_buf[i];
|
|
|
|
r->x *= CELL_SIZE;
|
|
|
|
r->y *= CELL_SIZE;
|
|
|
|
r->width *= CELL_SIZE;
|
|
|
|
r->height *= CELL_SIZE;
|
|
|
|
*r = intersect_rects(*r, screen_rect);
|
|
|
|
}
|
|
|
|
|
2023-03-19 20:39:52 +01:00
|
|
|
RenSurface rs = renwin_get_surface(window_renderer);
|
2019-12-28 12:16:32 +01:00
|
|
|
/* redraw updated regions */
|
|
|
|
for (int i = 0; i < rect_count; i++) {
|
|
|
|
/* draw */
|
|
|
|
RenRect r = rect_buf[i];
|
2023-01-22 04:11:42 +01:00
|
|
|
ren_set_clip_rect(window_renderer, r);
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
cmd = NULL;
|
2023-06-16 16:19:52 +02:00
|
|
|
while (next_command(window_renderer, &cmd)) {
|
2023-03-04 21:23:09 +01:00
|
|
|
SetClipCommand *ccmd = (SetClipCommand*)&cmd->command;
|
|
|
|
DrawRectCommand *rcmd = (DrawRectCommand*)&cmd->command;
|
|
|
|
DrawTextCommand *tcmd = (DrawTextCommand*)&cmd->command;
|
2019-12-28 12:16:32 +01:00
|
|
|
switch (cmd->type) {
|
|
|
|
case SET_CLIP:
|
2023-03-04 21:23:09 +01:00
|
|
|
ren_set_clip_rect(window_renderer, intersect_rects(ccmd->rect, r));
|
2019-12-28 12:16:32 +01:00
|
|
|
break;
|
|
|
|
case DRAW_RECT:
|
2023-03-19 20:39:52 +01:00
|
|
|
ren_draw_rect(&rs, rcmd->rect, rcmd->color);
|
2019-12-28 12:16:32 +01:00
|
|
|
break;
|
|
|
|
case DRAW_TEXT:
|
2023-03-04 21:23:09 +01:00
|
|
|
ren_font_group_set_tab_size(tcmd->fonts, tcmd->tab_size);
|
2023-03-19 20:39:52 +01:00
|
|
|
ren_draw_text(&rs, tcmd->fonts, tcmd->text, tcmd->len, tcmd->text_x, tcmd->rect.y, tcmd->color);
|
2021-03-06 18:12:02 +01:00
|
|
|
break;
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_debug) {
|
|
|
|
RenColor color = { rand(), rand(), rand(), 50 };
|
2023-03-19 20:39:52 +01:00
|
|
|
ren_draw_rect(&rs, r, color);
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* update dirty rects */
|
|
|
|
if (rect_count > 0) {
|
2023-01-22 04:11:42 +01:00
|
|
|
ren_update_rects(window_renderer, rect_buf, rect_count);
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* swap cell buffer and reset */
|
|
|
|
unsigned *tmp = cells;
|
|
|
|
cells = cells_prev;
|
|
|
|
cells_prev = tmp;
|
2023-06-16 16:19:52 +02:00
|
|
|
window_renderer->command_buf_idx = 0;
|
2019-12-28 12:16:32 +01:00
|
|
|
}
|
2021-05-05 09:32:24 +02:00
|
|
|
|