From e6a3ac7694cd05cd7f53462c8619b4acd31267cf Mon Sep 17 00:00:00 2001 From: Martin Fouilleul Date: Sun, 10 Sep 2023 15:21:11 +0200 Subject: [PATCH] oc_render uses currently selected surface --- doc/QuickStart.md | 2 +- doc/cheatsheets/cheatsheet_graphics.h | 4 ++-- samples/breakout/src/main.c | 2 +- samples/clock/src/main.c | 2 +- samples/ui/src/main.c | 2 +- sketches/atlas/main.c | 2 +- sketches/canvas/main.c | 2 +- sketches/image/main.c | 2 +- sketches/multi_surface/main.c | 4 ++-- sketches/perf_text/main.c | 2 +- sketches/render_thread/main.c | 2 +- sketches/smiley/main.c | 2 +- sketches/tiger/main.c | 2 +- sketches/ui/main.c | 2 +- src/graphics/graphics.h | 14 ++++++++------ src/graphics/graphics_common.c | 15 ++++++++++++--- src/graphics/graphics_surface.c | 9 +++++---- src/runtime.c | 15 ++------------- src/wasmbind/surface_api.json | 12 ++++++++++++ 19 files changed, 55 insertions(+), 42 deletions(-) diff --git a/doc/QuickStart.md b/doc/QuickStart.md index 9957dbc..c7e0657 100644 --- a/doc/QuickStart.md +++ b/doc/QuickStart.md @@ -65,7 +65,7 @@ oc_canvas_select(canvas); // make the canvas current //... add commands to the canvas command buffer using drawing functions oc_surface_select(surface); // select the canvas surface -oc_render(surface, canvas); // render the canvas commands into it +oc_render(canvas); // render the canvas commands into it oc_surface_present(surface); // display the result ``` diff --git a/doc/cheatsheets/cheatsheet_graphics.h b/doc/cheatsheets/cheatsheet_graphics.h index f683bac..08eae0e 100644 --- a/doc/cheatsheets/cheatsheet_graphics.h +++ b/doc/cheatsheets/cheatsheet_graphics.h @@ -16,8 +16,8 @@ oc_surface oc_surface_gles(); void oc_surface_destroy(oc_surface surface); void oc_surface_select(oc_surface surface); -void oc_surface_present(oc_surface surface); void oc_surface_deselect(void); +void oc_surface_present(oc_surface surface); oc_vec2 oc_surface_get_size(oc_surface surface); oc_vec2 oc_surface_contents_scaling(oc_surface surface); @@ -32,7 +32,7 @@ bool oc_canvas_is_nil(oc_canvas canvas); oc_canvas oc_canvas_create(void); void oc_canvas_destroy(oc_canvas canvas); oc_canvas oc_canvas_set_current(oc_canvas canvas); -void oc_render(oc_surface surface, oc_canvas canvas); +void oc_render(oc_canvas canvas); //------------------------------------------------------------------------------------------ // transform and clipping diff --git a/samples/breakout/src/main.c b/samples/breakout/src/main.c index 20c79d1..801b08a 100644 --- a/samples/breakout/src/main.c +++ b/samples/breakout/src/main.c @@ -465,7 +465,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_matrix_pop(); oc_surface_select(surface); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/samples/clock/src/main.c b/samples/clock/src/main.c index 8477858..b8b9aef 100644 --- a/samples/clock/src/main.c +++ b/samples/clock/src/main.c @@ -133,6 +133,6 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_circle_fill(centerX, centerY, 10 * uiScale); oc_surface_select(surface); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); } diff --git a/samples/ui/src/main.c b/samples/ui/src/main.c index d022a2f..0f4b153 100644 --- a/samples/ui/src/main.c +++ b/samples/ui/src/main.c @@ -355,7 +355,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void) oc_canvas_select(canvas); oc_surface_select(surface); oc_ui_draw(); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/sketches/atlas/main.c b/sketches/atlas/main.c index 6505b2f..66d7dd2 100644 --- a/sketches/atlas/main.c +++ b/sketches/atlas/main.c @@ -87,7 +87,7 @@ int main() oc_image_draw_region(image1.image, image1.rect, (oc_rect){ 100, 100, 300, 300 }); oc_image_draw_region(image2.image, image2.rect, (oc_rect){ 300, 200, 300, 300 }); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/sketches/canvas/main.c b/sketches/canvas/main.c index e8ee1ad..126c1d0 100644 --- a/sketches/canvas/main.c +++ b/sketches/canvas/main.c @@ -180,7 +180,7 @@ int main() frameTime, 1. / frameTime); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/sketches/image/main.c b/sketches/image/main.c index 2425b82..a4f7683 100644 --- a/sketches/image/main.c +++ b/sketches/image/main.c @@ -102,7 +102,7 @@ int main() oc_image_draw(image, (oc_rect){ 100, 100, 300, 300 }); oc_image_draw(image2, (oc_rect){ 300, 200, 300, 300 }); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/sketches/multi_surface/main.c b/sketches/multi_surface/main.c index ea2d2b8..d502a89 100644 --- a/sketches/multi_surface/main.c +++ b/sketches/multi_surface/main.c @@ -95,7 +95,7 @@ int main() oc_set_color_rgba(1, 0, 0, 1); oc_rectangle_fill(100, 100, 300, 150); - oc_render(surface1, canvas1); + oc_render(canvas1); //* oc_surface_select(surface2); @@ -107,7 +107,7 @@ int main() oc_set_color_rgba(0, 0, 1, 1); oc_rectangle_fill(200, 200, 300, 200); - oc_render(surface2, canvas2); + oc_render(canvas2); //*/ oc_surface_present(surface1); diff --git a/sketches/perf_text/main.c b/sketches/perf_text/main.c index 8bc8308..e739963 100644 --- a/sketches/perf_text/main.c +++ b/sketches/perf_text/main.c @@ -298,7 +298,7 @@ int main() f64 startFlushTime = oc_clock_time(OC_CLOCK_MONOTONIC); oc_surface_select(surface); - oc_render(surface, canvas); + oc_render(canvas); f64 startPresentTime = oc_clock_time(OC_CLOCK_MONOTONIC); oc_surface_present(surface); diff --git a/sketches/render_thread/main.c b/sketches/render_thread/main.c index 2ce1f86..dedb051 100644 --- a/sketches/render_thread/main.c +++ b/sketches/render_thread/main.c @@ -36,7 +36,7 @@ i32 render_thread(void* user) oc_set_color_rgba(1, 0, 0, 1); oc_rectangle_fill(100, 100, 300, 150); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); diff --git a/sketches/smiley/main.c b/sketches/smiley/main.c index bbf1bc9..683a96a 100644 --- a/sketches/smiley/main.c +++ b/sketches/smiley/main.c @@ -198,7 +198,7 @@ int main() 1. / frameTime); oc_surface_select(surface); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(oc_scratch()); diff --git a/sketches/tiger/main.c b/sketches/tiger/main.c index 5ad13c6..9e06377 100644 --- a/sketches/tiger/main.c +++ b/sketches/tiger/main.c @@ -233,7 +233,7 @@ int main() frameTime, 1. / frameTime); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_input_next_frame(&inputState); diff --git a/sketches/ui/main.c b/sketches/ui/main.c index 1c4d60f..265a798 100644 --- a/sketches/ui/main.c +++ b/sketches/ui/main.c @@ -632,7 +632,7 @@ int main() oc_ui_draw(); - oc_render(surface, canvas); + oc_render(canvas); oc_surface_present(surface); oc_arena_clear(scratch); diff --git a/src/graphics/graphics.h b/src/graphics/graphics.h index 54ccc73..9f7d6d7 100644 --- a/src/graphics/graphics.h +++ b/src/graphics/graphics.h @@ -115,9 +115,11 @@ ORCA_API oc_surface oc_surface_gles(); //DOC: create a surface for use with GL ORCA_API void oc_surface_destroy(oc_surface surface); //DOC: destroys the surface -ORCA_API void oc_surface_select(oc_surface surface); //DOC: selects the surface in the current thread before drawing +ORCA_API void oc_surface_select(oc_surface surface); //DOC: selects the surface in the current thread before drawing +ORCA_API void oc_surface_deselect(void); //DOC: deselects the current thread's previously selected surface +ORCA_API oc_surface oc_surface_get_selected(); + ORCA_API void oc_surface_present(oc_surface surface); //DOC: presents the surface to its window -ORCA_API void oc_surface_deselect(void); //DOC: deselects the current thread's previously selected surface ORCA_API oc_vec2 oc_surface_get_size(oc_surface surface); ORCA_API oc_vec2 oc_surface_contents_scaling(oc_surface surface); //DOC: returns the scaling of the surface (pixels = points * scale) @@ -211,10 +213,10 @@ typedef struct oc_text_extents ORCA_API oc_canvas oc_canvas_nil(void); //DOC: returns a nil canvas ORCA_API bool oc_canvas_is_nil(oc_canvas canvas); //DOC: true if canvas is nil -ORCA_API oc_canvas oc_canvas_create(void); //DOC: create a new canvas -ORCA_API void oc_canvas_destroy(oc_canvas canvas); //DOC: destroys canvas -ORCA_API oc_canvas oc_canvas_select(oc_canvas canvas); //DOC: selects canvas in the current thread -ORCA_API void oc_render(oc_surface surface, oc_canvas canvas); //DOC: renders all canvas commands onto surface +ORCA_API oc_canvas oc_canvas_create(void); //DOC: create a new canvas +ORCA_API void oc_canvas_destroy(oc_canvas canvas); //DOC: destroys canvas +ORCA_API oc_canvas oc_canvas_select(oc_canvas canvas); //DOC: selects canvas in the current thread +ORCA_API void oc_render(oc_canvas canvas); //DOC: renders all canvas commands onto surface //------------------------------------------------------------------------------------------ //SECTION: fonts diff --git a/src/graphics/graphics_common.c b/src/graphics/graphics_common.c index a95a48e..a9e4db2 100644 --- a/src/graphics/graphics_common.c +++ b/src/graphics/graphics_common.c @@ -219,6 +219,14 @@ void* oc_graphics_data_from_handle(oc_graphics_handle_kind kind, u64 h) return (data); } +//------------------------------------------------------------------------------------------ +//NOTE(martin): surface nil and is nil +//------------------------------------------------------------------------------------------ + +oc_surface oc_surface_nil() { return ((oc_surface){ .h = 0 }); } + +bool oc_surface_is_nil(oc_surface surface) { return (surface.h == 0); } + //------------------------------------------------------------------------------------------ //NOTE(martin): graphics canvas internal //------------------------------------------------------------------------------------------ @@ -926,13 +934,14 @@ oc_canvas oc_canvas_select(oc_canvas canvas) return (old); } -void oc_render(oc_surface surface, oc_canvas canvas) +void oc_render(oc_canvas canvas) { + oc_surface selectedSurface = oc_surface_get_selected(); oc_canvas_data* canvasData = oc_canvas_data_from_handle(canvas); - if(canvasData) + if(canvasData && !oc_surface_is_nil(selectedSurface)) { int eltCount = canvasData->path.startIndex + canvasData->path.count; - oc_surface_render_commands(surface, + oc_surface_render_commands(selectedSurface, canvasData->clearColor, canvasData->primitiveCount, canvasData->primitives, diff --git a/src/graphics/graphics_surface.c b/src/graphics/graphics_surface.c index b938d19..5a50ed3 100644 --- a/src/graphics/graphics_surface.c +++ b/src/graphics/graphics_surface.c @@ -14,6 +14,11 @@ oc_thread_local oc_surface oc_selectedSurface = { 0 }; +oc_surface oc_surface_get_selected() +{ + return (oc_selectedSurface); +} + //--------------------------------------------------------------- // typed handles functions //--------------------------------------------------------------- @@ -98,10 +103,6 @@ bool oc_is_surface_backend_available(oc_surface_api api) return (result); } -oc_surface oc_surface_nil() { return ((oc_surface){ .h = 0 }); } - -bool oc_surface_is_nil(oc_surface surface) { return (surface.h == 0); } - oc_surface oc_surface_create_for_window(oc_window window, oc_surface_api api) { if(oc_graphicsData.init) diff --git a/src/runtime.c b/src/runtime.c index 20f879d..3985516 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -836,7 +836,7 @@ i32 orca_runloop(void* user) oc_clear(); } - oc_render(app->debugOverlay.surface, app->debugOverlay.canvas); + oc_render(app->debugOverlay.canvas); oc_surface_present(app->debugOverlay.surface); oc_arena_clear(oc_scratch()); @@ -886,24 +886,13 @@ int main(int argc, char** argv) #if OC_PLATFORM_WINDOWS //NOTE(martin): on windows we set all surfaces to non-synced, and do a single "manual" wait here. // on macOS each surface is individually synced to the monitor refresh rate but don't block each other - oc_surface_swap_interval(app->debugOverlay.surface, 0); + oc_surface_swap_interval(app->debugOverlay.surface, 0); #else oc_surface_swap_interval(app->debugOverlay.surface, 1); #endif oc_surface_deselect(); - //WARN: this is a workaround to avoid stalling the first few times we acquire drawables from - // the surfaces... This should probably be fixed in the implementation of mtl_surface! - - for(int i = 0; i < 3; i++) - { - oc_surface_select(app->debugOverlay.surface); - oc_canvas_select(app->debugOverlay.canvas); - oc_render(app->debugOverlay.surface, app->debugOverlay.canvas); - oc_surface_present(app->debugOverlay.surface); - } - oc_ui_init(&app->debugOverlay.ui); //NOTE: show window and start runloop diff --git a/src/wasmbind/surface_api.json b/src/wasmbind/surface_api.json index 8065d32..fa735c2 100644 --- a/src/wasmbind/surface_api.json +++ b/src/wasmbind/surface_api.json @@ -77,6 +77,18 @@ {"name": "surface", "type": {"name": "oc_surface", "tag": "S"}}] }, +{ + "name": "oc_surface_deselect", + "cname": "oc_surface_deselect", + "ret": {"name": "void", "tag": "v"}, + "args": [] +}, +{ + "name": "oc_surface_get_selected", + "cname": "oc_surface_get_selected", + "ret": {"name": "oc_surface", "tag": "S"}, + "args": [] +}, { "name": "oc_surface_present", "cname": "oc_surface_present",