Allow command buffer to be expanded (#1297)

This commit is contained in:
Guldoman 2023-01-02 03:12:14 +01:00 committed by GitHub
parent 4e272c33de
commit 2638e9636b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 8 deletions

View File

@ -26,7 +26,8 @@
#define CELLS_X 80 #define CELLS_X 80
#define CELLS_Y 50 #define CELLS_Y 50
#define CELL_SIZE 96 #define CELL_SIZE 96
#define COMMAND_BUF_SIZE (1024 * 512) #define CMD_BUF_RESIZE_RATE 1.2
#define CMD_BUF_INIT_SIZE (1024 * 512)
#define COMMAND_BARE_SIZE offsetof(Command, text) #define COMMAND_BARE_SIZE offsetof(Command, text)
enum { SET_CLIP, DRAW_TEXT, DRAW_RECT }; enum { SET_CLIP, DRAW_TEXT, DRAW_RECT };
@ -48,7 +49,9 @@ static unsigned cells_buf2[CELLS_X * CELLS_Y];
static unsigned *cells_prev = cells_buf1; static unsigned *cells_prev = cells_buf1;
static unsigned *cells = cells_buf2; static unsigned *cells = cells_buf2;
static RenRect rect_buf[CELLS_X * CELLS_Y / 2]; static RenRect rect_buf[CELLS_X * CELLS_Y / 2];
static char command_buf[COMMAND_BUF_SIZE]; size_t command_buf_size = 0;
uint8_t *command_buf = NULL;
static bool resize_issue;
static int command_buf_idx; static int command_buf_idx;
static RenRect screen_rect; static RenRect screen_rect;
static bool show_debug; static bool show_debug;
@ -96,16 +99,38 @@ static RenRect merge_rects(RenRect a, RenRect b) {
return (RenRect) { x1, y1, x2 - x1, y2 - y1 }; return (RenRect) { x1, y1, x2 - x1, y2 - y1 };
} }
static bool expand_command_buffer() {
size_t new_size = command_buf_size * CMD_BUF_RESIZE_RATE;
if (new_size == 0) {
new_size = CMD_BUF_INIT_SIZE;
}
uint8_t *new_command_buf = realloc(command_buf, new_size);
if (!new_command_buf) {
return false;
}
command_buf_size = new_size;
command_buf = new_command_buf;
return true;
}
static Command* push_command(int type, int size) { static Command* push_command(int type, int size) {
size_t alignment = alignof(max_align_t) - 1; if (resize_issue) {
size = (size + alignment) & ~alignment; // Don't push new commands as we had problems resizing the command buffer.
Command *cmd = (Command*) (command_buf + command_buf_idx); // Let's wait for the next frame.
int n = command_buf_idx + size;
if (n > COMMAND_BUF_SIZE) {
fprintf(stderr, "Warning: (" __FILE__ "): exhausted command buffer\n");
return NULL; return NULL;
} }
size_t alignment = alignof(max_align_t) - 1;
size = (size + alignment) & ~alignment;
int n = command_buf_idx + size;
while (n > command_buf_size) {
if (!expand_command_buffer()) {
fprintf(stderr, "Warning: (" __FILE__ "): unable to resize command buffer (%ld)\n",
(size_t)(command_buf_size * CMD_BUF_RESIZE_RATE));
resize_issue = true;
return NULL;
}
}
Command *cmd = (Command*) (command_buf + command_buf_idx);
command_buf_idx = n; command_buf_idx = n;
memset(cmd, 0, size); memset(cmd, 0, size);
cmd->type = type; cmd->type = type;
@ -175,6 +200,7 @@ void rencache_invalidate(void) {
void rencache_begin_frame() { void rencache_begin_frame() {
/* reset all cells if the screen width/height has changed */ /* reset all cells if the screen width/height has changed */
int w, h; int w, h;
resize_issue = false;
ren_get_size(&w, &h); ren_get_size(&w, &h);
if (screen_rect.width != w || h != screen_rect.height) { if (screen_rect.width != w || h != screen_rect.height) {
screen_rect.width = w; screen_rect.width = w;

View File

@ -507,8 +507,13 @@ void ren_draw_rect(RenRect rect, RenColor color) {
/*************** Window Management ****************/ /*************** Window Management ****************/
void ren_free_window_resources() { void ren_free_window_resources() {
extern uint8_t *command_buf;
extern size_t command_buf_size;
renwin_free(&window_renderer); renwin_free(&window_renderer);
SDL_FreeSurface(draw_rect_surface); SDL_FreeSurface(draw_rect_surface);
free(command_buf);
command_buf = NULL;
command_buf_size = 0;
} }
void ren_init(SDL_Window *win) { void ren_init(SDL_Window *win) {