diff options
author | Mitya Selivanov <automainint@guattari.tech> | 2025-04-07 00:13:00 +0200 |
---|---|---|
committer | Mitya Selivanov <automainint@guattari.tech> | 2025-04-07 00:13:00 +0200 |
commit | 27c0b38e4ebc2f2e1b1fb49e684312ce58e0b78a (patch) | |
tree | 4cb0743d2534051956d308ff1fbba6b92ffe55d4 | |
parent | e2ad36c4cd9f757b3843ac59c5c436819b70f442 (diff) | |
download | reduced_system_layer-27c0b38e4ebc2f2e1b1fb49e684312ce58e0b78a.zip |
Fix graphics cache glitch
-rwxr-xr-x | build_all.sh | 4 | ||||
-rw-r--r-- | examples/ui.c | 6 | ||||
-rwxr-xr-x | graphics.c | 12 | ||||
-rwxr-xr-x | reduced_system_layer.c | 67 |
4 files changed, 49 insertions, 40 deletions
diff --git a/build_all.sh b/build_all.sh index e4674e4..3dcbaba 100755 --- a/build_all.sh +++ b/build_all.sh @@ -32,11 +32,11 @@ mkdir -p bin/web # gcc $FLAGS_X11 -o ./bin/x11/graph ./examples/graph.c # gcc $FLAGS_X11 -o ./bin/x11/julia_set ./examples/julia_set.c # gcc $FLAGS_X11 -o ./bin/x11/sinewave ./examples/sinewave.c -# gcc $FLAGS_X11 -o ./bin/x11/ui ./examples/ui.c +gcc $FLAGS_X11 -o ./bin/x11/ui ./examples/ui.c # gcc $FLAGS_X11 -o ./bin/x11/particles ./examples/particles.c # gcc $FLAGS_X11 -o ./bin/x11/echo ./examples/echo.c -gcc $FLAGS_X11 -o ./bin/x11/screenshot ./examples/screenshot.c +# gcc $FLAGS_X11 -o ./bin/x11/screenshot ./examples/screenshot.c # clang $FLAGS_WEB -o ./bin/web/graph.wasm ./examples/graph.c # clang $FLAGS_WEB -o ./bin/web/julia_set.wasm ./examples/julia_set.c diff --git a/examples/ui.c b/examples/ui.c index a7877dc..943828d 100644 --- a/examples/ui.c +++ b/examples/ui.c @@ -221,9 +221,9 @@ void update_and_render_frame(void) { text[i] = g_platform.drop_files[0].data[i]; } - draw_text_cursor_cached((vec4_f32) { 1.f, 1.f, 1.f, 1.f }, (Box) { x0, y0, w, h }, (vec2) { 10., 10. }, text_len, text, cursor, selection); - draw_text_area_cached((vec4_f32) { 0.f, 0.f, 0.f, 1.f }, (Box) { x0 + 8, y0 - 8, w, h }, (vec2) { 10., 10. }, text_len, text); - draw_text_area_cached(color, (Box) { x0, y0, w, h }, (vec2) { 10., 10. }, text_len, text); + draw_text_cursor_cached(0, (vec4_f32) { 1.f, 1.f, 1.f, 1.f }, (Box) { x0, y0, w, h }, (vec2) { 10., 10. }, text_len, text, cursor, selection); + draw_text_area_cached(0, (vec4_f32) { 0.f, 0.f, 0.f, 1.f }, (Box) { x0 + 8, y0 - 8, w, h }, (vec2) { 10., 10. }, text_len, text); + draw_text_area_cached(0, color, (Box) { x0, y0, w, h }, (vec2) { 10., 10. }, text_len, text); render_main_window_frame(); } @@ -253,6 +253,7 @@ void perform_graphics_request(Graphics_Context context, Graphics_Request req); #ifndef GRAPHICS_IMPL_GUARD_ #define GRAPHICS_IMPL_GUARD_ +// FIXME: Re-encode for faster rendering. static u64 _bitfont[] = { 0xbc0000000000, 0xc00300000, 0x5fd5040093f24fc9, 0xa00a2c2a1a280105, 0xc000415e6f, 0x400000020be0000, 0x1c38a8400000007d, 0x40002043e1020215, 0x408102000000010, 0x9800000000020002, 0xf913e00000033, 0x53200000207c8800, 0x3654880000099, 0x54b800000f840e00, 0xe953c000001a, 0x953e000000674080, 0x1e54b800000f, 0x490000000000240, 0x88a08000000, 0x20a220050a142850, 0x6520800000, 0x912f801eab260be, 0x800034952bf0001f, 0xc850bf0000921427, 0xf00010a54afc0003, 0xd29427800002142b, 0x840007e1023f0000, 0x7d09100000217e, 0x3f000188a08fc000, 0xc30c0cfc00000810, 0x27803f101013f00f, 0xc244bf0000f214, 0x4bf0002f21427800, 0xc254a480006c24, 0x407c00102fc08100, 0xf208080f0000fa0, 0x531007d81c607c0, 0xc208288c031141, 0x83fc00046954b10, 0x180e03000000, 0x41040000000ff04, 0x8102040810000404, 0x2a54600000000101, 0x309123e0000e, 0xc912180000a22447, 0x8000062a54700007, 0xe52a4300000029f0, 0xa0000602043e0001, 0x1d48000002074, 0x1f000003610f8000, 0x13e04f800000010, 0x470000780813e00f, 0x184893e0000e224, 0x23e0001f12243000, 0x82a54100000008, 0x40780000009f0200, 0xe208080e0001f20, 0xa22007981860780, 0x82082888022282, 0x16c200004ca95320, 0x7f000004, 0x408200000086d04, 0x8204, }; @@ -1985,7 +1986,10 @@ static void copy_pixels_quick_(Pixel_Buffer dst, i64 x, i64 y, Pixel_Buffer src) static void perform_graphics_request_cached_(Pixel_Buffer dst, Graphics_Request req) { Graphics_Request_Norm_ norm = normalize_graphics_request_(req); - Graphics_Request_Id_ id = graphics_request_id_(norm.req); + if (norm.width <= 0 || norm.height <= 0) + return; + + Graphics_Request_Id_ id = graphics_request_id_(norm.req); b8 reset_slot = 1; i64 slot = -1; @@ -2012,7 +2016,7 @@ static void perform_graphics_request_cached_(Pixel_Buffer dst, Graphics_Request } if (slot == -1) { - LOG_ERROR("Graphics cache overflow."); + // Cache overflow. slot = n; } @@ -2033,13 +2037,15 @@ static void perform_graphics_request_cached_(Pixel_Buffer dst, Graphics_Request if (s->buffer_len < num_pixels) // Out of memory s->occupied = 0; - else + else { + mem_set_(s->buffer, 0, s->buffer_len * sizeof *s->buffer); perform_graphics_request_to_buffer_((Pixel_Buffer) { .width = s->width, .height = s->height, .stride = s->width, .pixels = s->buffer, }, s->req); + } } if (s->occupied) diff --git a/reduced_system_layer.c b/reduced_system_layer.c index f083ec8..77bd2d7 100755 --- a/reduced_system_layer.c +++ b/reduced_system_layer.c @@ -29,7 +29,10 @@ #/ - Pascal_Snake_Case - Type name. #/ - snake_case - Non-type name. #/ - UPPER_SNAKE_CASE - Macro or constant. -#/ - g_ prefix - Global variable name. +#/ +#/ - g_ prefix - Global variable name. +#/ - _ prefix - Name of a global variable that is not part of the user API. +#/ - _ suffix - Name of a procedure that is not part of the user API. #/ #/ Most procedures have long and descriptive names. #/ Some procedures have prefixes according to their domain. @@ -330,7 +333,7 @@ i32 main(i32 argc, c8 **argv); #define PRIMARY_SOUND_SAMPLE_RATE 44100 #endif -// FIXME: Implement dynamic primary sound buffer. +// TODO: Implement dynamic primary sound buffer. #ifndef MAX_NUM_PRIMARY_SOUND_FRAMES #define MAX_NUM_PRIMARY_SOUND_FRAMES (10 * PRIMARY_SOUND_SAMPLE_RATE * NUM_PRIMARY_SOUND_CHANNELS) #endif @@ -770,7 +773,7 @@ static i64 max2_i64_(i64 x, i64 y) { // // ================================================================ -// TEMP +// TODO: Use print formatting after we implement it. #if defined(__wasm__) void log_error_impl(i32 file_len, c8 const *file, i32 line, i32 func_len, c8 const *func, i32 text_len, c8 const *text); #define LOG_ERROR(text_, ...) \ @@ -1234,7 +1237,7 @@ static vec3_f32 rgb_f32_from_u32_(u32 c) { // ================================================================ -// TODO: Move this to separate sections. +// FIXME: Move this to separate sections. Platform g_platform = {0}; @@ -1398,12 +1401,12 @@ static void *memory_buffer_allocate_from_(i64 memory_buffer_size, u8 *memory_buf return dst; } -static u8 memory_buffer_[STATIC_MEMORY_BUFFER_SIZE] = {0}; +static u8 _memory_buffer[STATIC_MEMORY_BUFFER_SIZE] = {0}; void *memory_buffer_allocate(i64 size, i64 alignment, i64 previous_size, void *previous_data) { if (g_platform.memory_buffer == NULL || g_platform.memory_buffer_size <= 0) { - g_platform.memory_buffer_size = sizeof memory_buffer_; - g_platform.memory_buffer = memory_buffer_; + g_platform.memory_buffer_size = sizeof _memory_buffer; + g_platform.memory_buffer = _memory_buffer; } return memory_buffer_allocate_from_(g_platform.memory_buffer_size, g_platform.memory_buffer, size, alignment, previous_size, previous_data); @@ -1420,7 +1423,7 @@ void resize_dynamic_array_exact(i64 *num, void **data, i64 element_size, i64 new i64 alignment = max2_i64_(8, element_size); - void *new_data = memory_buffer_allocate( + u8 *new_data = (u8 *) memory_buffer_allocate( new_num * element_size, alignment, *num * element_size, @@ -1432,7 +1435,7 @@ void resize_dynamic_array_exact(i64 *num, void **data, i64 element_size, i64 new return; if (new_num > *num) - mem_set_(*data, 0, new_num - *num); + mem_set_(new_data + element_size * *num, 0, new_num - *num); *num = new_num; *data = new_data; @@ -1770,7 +1773,7 @@ typedef struct { Dynamic_Library_Slot_ *slots; } Dynamic_Libraries_; -static Dynamic_Libraries_ dynamic_libraries_ = {0}; +static Dynamic_Libraries_ _dynamic_libraries = {0}; void dynamic_library_open(u16 slot, c8 *name) { if (name == NULL) { @@ -1778,12 +1781,12 @@ void dynamic_library_open(u16 slot, c8 *name) { return; } - if (slot >= dynamic_libraries_.num_slots) - resize_dynamic_array_exact(&dynamic_libraries_.num_slots, (void **) &dynamic_libraries_.slots, sizeof *dynamic_libraries_.slots, (i64) slot + 1); + if (slot >= _dynamic_libraries.num_slots) + resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, (i64) slot + 1); - if (dynamic_libraries_.slots[slot].handle != NULL) { - dlclose(dynamic_libraries_.slots[slot].handle); - dynamic_libraries_.slots[slot].handle = NULL; + if (_dynamic_libraries.slots[slot].handle != NULL) { + dlclose(_dynamic_libraries.slots[slot].handle); + _dynamic_libraries.slots[slot].handle = NULL; } void *h = dlopen(name, RTLD_LAZY); @@ -1794,11 +1797,11 @@ void dynamic_library_open(u16 slot, c8 *name) { return; } - dynamic_libraries_.slots[slot].handle = h; + _dynamic_libraries.slots[slot].handle = h; } void *dynamic_library_get_proc_address(u16 slot, c8 *proc) { - if (slot >= dynamic_libraries_.num_slots) { + if (slot >= _dynamic_libraries.num_slots) { LOG_ERROR("Invalid slot: %d", (i32) slot); return NULL; } @@ -1808,12 +1811,12 @@ void *dynamic_library_get_proc_address(u16 slot, c8 *proc) { return NULL; } - if (dynamic_libraries_.slots[slot].handle == NULL) { + if (_dynamic_libraries.slots[slot].handle == NULL) { LOG_ERROR("Slot is closed: %d", (i32) slot); return NULL; } - void *proc_address = dlsym(dynamic_libraries_.slots[slot].handle, proc); + void *proc_address = dlsym(_dynamic_libraries.slots[slot].handle, proc); if (proc_address == NULL) { LOG_ERROR("Failed to get: %s", proc); @@ -1824,10 +1827,10 @@ void *dynamic_library_get_proc_address(u16 slot, c8 *proc) { } static void close_all_dynamic_libraries_(void) { - for (i64 i = 0; i < dynamic_libraries_.num_slots; ++i) - if (dynamic_libraries_.slots[i].handle != NULL) - dlclose(dynamic_libraries_.slots[i].handle); - resize_dynamic_array_exact(&dynamic_libraries_.num_slots, (void **) &dynamic_libraries_.slots, sizeof *dynamic_libraries_.slots, 0); + for (i64 i = 0; i < _dynamic_libraries.num_slots; ++i) + if (_dynamic_libraries.slots[i].handle != NULL) + dlclose(_dynamic_libraries.slots[i].handle); + resize_dynamic_array_exact(&_dynamic_libraries.num_slots, (void **) &_dynamic_libraries.slots, sizeof *_dynamic_libraries.slots, 0); } #endif // defined(__unix__) @@ -1986,7 +1989,7 @@ b8 network_open(u16 slot, Network_Address address, u16 *local_port) { } else { a6.sin6_family = AF_INET6; a6.sin6_port = htons(address.port); - memcpy(a6.sin6_addr.s6_addr, address.v6_address, sizeof a6.sin6_addr.s6_addr); + mem_cpy_(a6.sin6_addr.s6_addr, address.v6_address, sizeof a6.sin6_addr.s6_addr); } if (bind(_sockets[slot].socket, p, p_len) == -1) { @@ -2061,7 +2064,7 @@ i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc } if (remote_address != NULL) { - memset(remote_address, 0, sizeof *remote_address); + mem_set_(remote_address, 0, sizeof *remote_address); remote_address->protocol = address.protocol; if (address.protocol == IPv4_UDP) { @@ -2069,7 +2072,7 @@ i64 network_recv(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc remote_address->v4_address_as_u32 = a4.sin_addr.s_addr; } else { remote_address->port = ntohs(a6.sin6_port); - memcpy(remote_address->v6_address, a6.sin6_addr.s6_addr, sizeof remote_address->v6_address); + mem_cpy_(remote_address->v6_address, a6.sin6_addr.s6_addr, sizeof remote_address->v6_address); } } @@ -2109,7 +2112,7 @@ i64 network_send(u16 slot, Network_Address address, i64 size, u8 *data, u16 *loc a6.sin6_family = AF_INET6; a6.sin6_port = htons(address.port); - memcpy(a6.sin6_addr.s6_addr, address.v6_address, sizeof a6.sin6_addr.s6_addr); + mem_cpy_(a6.sin6_addr.s6_addr, address.v6_address, sizeof a6.sin6_addr.s6_addr); } i64 sent = sendto( @@ -2248,7 +2251,7 @@ void handle_primary_sound(void) { if (s < 0) LOG_ERROR("snd_pcm_writei failed: %s", snd_strerror(s)); - memset(_sound_ring + _sound_position, 0, num_frames * sizeof *_sound_ring); + mem_set_(_sound_ring + _sound_position, 0, num_frames * sizeof *_sound_ring); } else { i64 part_one = MAX_NUM_PRIMARY_SOUND_FRAMES - _sound_position; i64 part_two = num_frames - part_one; @@ -2261,8 +2264,8 @@ void handle_primary_sound(void) { if (s < 0) LOG_ERROR("snd_pcm_writei failed: %s", snd_strerror(s)); - memset(_sound_ring + _sound_position, 0, part_one * sizeof *_sound_ring); - memset(_sound_ring, 0, part_two * sizeof *_sound_ring); + mem_set_(_sound_ring + _sound_position, 0, part_one * sizeof *_sound_ring); + mem_set_(_sound_ring, 0, part_two * sizeof *_sound_ring); } _sound_position = (_sound_position + num_frames) % MAX_NUM_PRIMARY_SOUND_FRAMES; @@ -3207,8 +3210,8 @@ i32 handle_main_window_events(void) { if (g_platform.files_dropped) drop_files_clean_(); - memset(g_platform.key_pressed, 0, sizeof g_platform.key_pressed); - memset(_key_repeat, 0, sizeof _key_repeat); + mem_set_(g_platform.key_pressed, 0, sizeof g_platform.key_pressed); + mem_set_(_key_repeat, 0, sizeof _key_repeat); resize_dynamic_array_capacity(&g_platform.input_len, &g_platform.input_capacity, (void **) &g_platform.input, sizeof *g_platform.input, 0); |