Finish separating graphics_common and graphics_surface + add platform_math, so that we can compile graphics_common to wasm in orca

This commit is contained in:
Martin Fouilleul 2023-04-26 14:56:02 +02:00
parent ca1caf8537
commit b27dc615d1
20 changed files with 215 additions and 140 deletions

View File

@ -191,7 +191,7 @@ int main()
1./frameTime);
mg_surface_prepare(surface);
mg_flush(surface);
mg_render(surface, canvas);
mg_surface_present(surface);
mem_arena_clear(mem_scratch());

View File

@ -292,7 +292,7 @@ int main()
f64 startFlushTime = mp_get_time(MP_CLOCK_MONOTONIC);
mg_surface_prepare(surface);
mg_flush(surface);
mg_render(surface, canvas);
f64 startPresentTime = mp_get_time(MP_CLOCK_MONOTONIC);
mg_surface_present(surface);

View File

@ -228,7 +228,7 @@ int main()
frameTime,
1./frameTime);
mg_flush(surface);
mg_render(surface, canvas);
mg_surface_present(surface);
mp_input_next_frame(&inputState);

View File

@ -11,7 +11,7 @@
#include<EGL/egl.h>
#include<EGL/eglext.h>
#include"mp_app_internal.h"
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"gl_loader.h"
#if PLATFORM_MACOS

View File

@ -9,7 +9,7 @@
#ifndef __EGL_SURFACE_H_
#define __EGL_SURFACE_H_
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"mp_app.h"
mg_surface_data* mg_egl_surface_create_for_window(mp_window window);

View File

@ -6,7 +6,7 @@
* @revision:
*
*****************************************************************/
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"macro_helpers.h"
#include"glsl_shaders.h"
#include"gl_api.h"

View File

@ -94,7 +94,7 @@ MP_API bool mg_is_surface_api_available(mg_surface_api api);
//------------------------------------------------------------------------------------------
typedef struct mg_surface { u64 h; } mg_surface;
MP_API mg_surface mg_surface_nil();
MP_API mg_surface mg_surface_nil(void);
MP_API bool mg_surface_is_nil(mg_surface surface);
MP_API mg_surface mg_surface_create_for_window(mp_window window, mg_surface_api api);
@ -178,15 +178,15 @@ typedef struct mg_text_extents
MP_API mg_canvas mg_canvas_nil(void);
MP_API bool mg_canvas_is_nil(mg_canvas canvas);
MP_API mg_canvas mg_canvas_create();
MP_API mg_canvas mg_canvas_create(void);
MP_API void mg_canvas_destroy(mg_canvas canvas);
mg_canvas mg_canvas_set_current(mg_canvas canvas);
MP_API void mg_flush(mg_surface surface); //TODO change to mg_canvas_render(mg_surface surface, mg_canvas canvas);
MP_API void mg_render(mg_surface surface, mg_canvas canvas);
//------------------------------------------------------------------------------------------
//NOTE(martin): fonts
//------------------------------------------------------------------------------------------
MP_API mg_font mg_font_nil();
MP_API mg_font mg_font_nil(void);
MP_API mg_font mg_font_create_from_memory(u32 size, byte* buffer, u32 rangeCount, unicode_range* ranges);
MP_API void mg_font_destroy(mg_font font);
@ -214,7 +214,7 @@ MP_API mp_rect mg_text_bounding_box(mg_font font, f32 fontSize, str8 text);
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------
MP_API mg_image mg_image_nil();
MP_API mg_image mg_image_nil(void);
MP_API bool mg_image_is_nil(mg_image a);
MP_API mg_image mg_image_create(mg_surface surface, u32 width, u32 height);
@ -256,10 +256,10 @@ void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn);
MP_API void mg_viewport(mp_rect viewPort);
MP_API void mg_matrix_push(mg_mat2x3 matrix);
MP_API void mg_matrix_pop();
MP_API void mg_matrix_pop(void);
MP_API void mg_clip_push(f32 x, f32 y, f32 w, f32 h);
MP_API void mg_clip_pop();
MP_API void mg_clip_pop(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): graphics attributes setting/getting
@ -277,25 +277,25 @@ MP_API void mg_set_text_flip(bool flip);
MP_API void mg_set_image(mg_image image);
MP_API void mg_set_image_source_region(mp_rect region);
MP_API mg_color mg_get_color();
MP_API f32 mg_get_width();
MP_API f32 mg_get_tolerance();
MP_API mg_joint_type mg_get_joint();
MP_API f32 mg_get_max_joint_excursion();
MP_API mg_cap_type mg_get_cap();
MP_API mg_font mg_get_font();
MP_API f32 mg_get_font_size();
MP_API bool mg_get_text_flip();
MP_API mg_color mg_get_color(void);
MP_API f32 mg_get_width(void);
MP_API f32 mg_get_tolerance(void);
MP_API mg_joint_type mg_get_joint(void);
MP_API f32 mg_get_max_joint_excursion(void);
MP_API mg_cap_type mg_get_cap(void);
MP_API mg_font mg_get_font(void);
MP_API f32 mg_get_font_size(void);
MP_API bool mg_get_text_flip(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): path construction
//------------------------------------------------------------------------------------------
MP_API vec2 mg_get_position();
MP_API vec2 mg_get_position(void);
MP_API void mg_move_to(f32 x, f32 y);
MP_API void mg_line_to(f32 x, f32 y);
MP_API void mg_quadratic_to(f32 x1, f32 y1, f32 x2, f32 y2);
MP_API void mg_cubic_to(f32 x1, f32 y1, f32 x2, f32 y2, f32 x3, f32 y3);
MP_API void mg_close_path();
MP_API void mg_close_path(void);
MP_API mp_rect mg_glyph_outlines(str32 glyphIndices);
MP_API void mg_codepoints_outlines(str32 string);
@ -304,9 +304,9 @@ MP_API void mg_text_outlines(str8 string);
//------------------------------------------------------------------------------------------
//NOTE(martin): clear/fill/stroke
//------------------------------------------------------------------------------------------
MP_API void mg_clear();
MP_API void mg_fill();
MP_API void mg_stroke();
MP_API void mg_clear(void);
MP_API void mg_fill(void);
MP_API void mg_stroke(void);
//------------------------------------------------------------------------------------------
//NOTE(martin): 'fast' shapes primitives

View File

@ -7,18 +7,20 @@
*
*****************************************************************/
#define _USE_MATH_DEFINES //NOTE: necessary for MSVC
#include<math.h>
#include"platform.h"
#include"platform_math.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include"stb_truetype.h"
#if !PLATFORM_ORCA
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
#define STB_TRUETYPE_IMPLEMENTATION
#include"stb_truetype.h"
#endif
#include"platform_log.h"
#include"platform_assert.h"
#include"graphics_internal.h"
#include"graphics_common.h"
typedef struct mg_glyph_map_entry
{
@ -83,7 +85,7 @@ typedef struct mg_handle_slot
} mg_handle_slot;
static const u32 MG_HANDLES_MAX_COUNT = 512;
enum { MG_HANDLES_MAX_COUNT = 512 };
typedef struct mg_data
{
@ -402,6 +404,8 @@ mg_font_data* mg_font_data_from_handle(mg_font handle)
return(data);
}
#if !PLATFORM_ORCA
mg_font mg_font_create_from_memory(u32 size, byte* buffer, u32 rangeCount, unicode_range* ranges)
{
if(!__mgData.init)
@ -594,6 +598,8 @@ void mg_font_destroy(mg_font fontHandle)
mg_handle_recycle(fontHandle.h);
}
}
#endif // !PLATFORM_ORCA
str32 mg_font_get_glyph_indices_from_font_data(mg_font_data* fontData, str32 codePoints, str32 backing)
{
@ -865,6 +871,11 @@ mg_canvas_data* mg_canvas_data_from_handle(mg_canvas handle)
mg_canvas mg_canvas_create()
{
if(!__mgData.init)
{
mg_init();
}
mg_canvas canvasHandle = mg_canvas_nil();
mg_canvas_data* canvas = list_pop_entry(&__mgData.canvasFreeList, mg_canvas_data, freeListElt);
if(!canvas)
@ -873,8 +884,14 @@ mg_canvas mg_canvas_create()
}
if(canvas)
{
memset(canvas, 0, sizeof(mg_canvas_data));
canvas->textFlip = false;
canvas->path = (mg_path_descriptor){0};
canvas->matrixStackSize = 0;
canvas->clipStackSize = 0;
canvas->primitiveCount = 0;
canvas->clearColor = (mg_color){0, 0, 0, 0};
canvas->attributes = (mg_attributes){0};
canvas->attributes.color = (mg_color){0, 0, 0, 1};
canvas->attributes.tolerance = 1;
canvas->attributes.width = 10;
@ -910,23 +927,22 @@ mg_canvas mg_canvas_set_current(mg_canvas canvas)
return(old);
}
void mg_flush(mg_surface surface)
void mg_render(mg_surface surface, mg_canvas canvas)
{
mg_canvas_data* canvas = __mgCurrentCanvas;
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(canvas && surfaceData && surfaceData->backend)
mg_canvas_data* canvasData = mg_canvas_data_from_handle(canvas);
if(canvasData)
{
int eltCount = canvas->path.startIndex + canvas->path.count;
surfaceData->backend->render(surfaceData->backend,
canvas->clearColor,
canvas->primitiveCount,
canvas->primitives,
eltCount,
canvas->pathElements);
int eltCount = canvasData->path.startIndex + canvasData->path.count;
mg_surface_render_commands(surface,
canvasData->clearColor,
canvasData->primitiveCount,
canvasData->primitives,
eltCount,
canvasData->pathElements);
canvas->primitiveCount = 0;
canvas->path.startIndex = 0;
canvas->path.count = 0;
canvasData->primitiveCount = 0;
canvasData->path.startIndex = 0;
canvasData->path.count = 0;
}
}
@ -1580,6 +1596,8 @@ mg_image mg_image_create_from_rgba8(mg_surface surface, u32 width, u32 height, u
return(image);
}
#if !PLATFORM_ORCA
mg_image mg_image_create_from_data(mg_surface surface, str8 data, bool flip)
{
mg_image image = mg_image_nil();
@ -1613,6 +1631,8 @@ mg_image mg_image_create_from_file(mg_surface surface, str8 path, bool flip)
return(image);
}
#endif // !PLATFORM_ORCA
void mg_image_draw_region(mg_image image, mp_rect srcRegion, mp_rect dstRegion)
{
@ -1712,6 +1732,8 @@ mg_image_region mg_image_atlas_alloc_from_rgba8(mg_rect_atlas* atlas, mg_image b
return(imageRgn);
}
#if !PLATFORM_ORCA
mg_image_region mg_image_atlas_alloc_from_data(mg_rect_atlas* atlas, mg_image backingImage, str8 data, bool flip)
{
mg_image_region imageRgn = {0};
@ -1744,6 +1766,7 @@ mg_image_region mg_image_atlas_alloc_from_file(mg_rect_atlas* atlas, mg_image ba
}
return(imageRgn);
}
#endif // !PLATFORM_ORCA
void mg_image_atlas_recycle(mg_rect_atlas* atlas, mg_image_region imageRgn)
{

82
src/graphics_common.h Normal file
View File

@ -0,0 +1,82 @@
/************************************************************//**
*
* @file: graphics_common.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_COMMON_H_
#define __GRAPHICS_COMMON_H_
#include"graphics.h"
//------------------------------------------------------------------------
// canvas structs
//------------------------------------------------------------------------
typedef enum { MG_PATH_MOVE,
MG_PATH_LINE,
MG_PATH_QUADRATIC,
MG_PATH_CUBIC } mg_path_elt_type;
typedef struct mg_path_elt
{
mg_path_elt_type type;
vec2 p[3];
} mg_path_elt;
typedef struct mg_path_descriptor
{
u32 startIndex;
u32 count;
vec2 startPoint;
} mg_path_descriptor;
typedef struct mg_attributes
{
f32 width;
f32 tolerance;
mg_color color;
mg_joint_type joint;
f32 maxJointExcursion;
mg_cap_type cap;
mg_font font;
f32 fontSize;
mg_image image;
mp_rect srcRegion;
mg_mat2x3 transform;
mp_rect clip;
} mg_attributes;
typedef enum { MG_CMD_FILL,
MG_CMD_STROKE,
MG_CMD_JUMP
} mg_primitive_cmd;
typedef struct mg_primitive
{
mg_primitive_cmd cmd;
mg_attributes attributes;
union
{
mg_path_descriptor path;
mp_rect rect;
u32 jump;
};
} mg_primitive;
void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements);
#endif //__GRAPHICS_COMMON_H_

View File

@ -6,8 +6,7 @@
*
*****************************************************************/
#include"graphics_internal.h"
#include"graphics_surface.h"
//---------------------------------------------------------------
// typed handles functions
@ -327,6 +326,25 @@ void mg_surface_host_connect(mg_surface handle, mg_surface_id remoteID)
}
}
void mg_surface_render_commands(mg_surface surface,
mg_color clearColor,
u32 primitiveCount,
mg_primitive* primitives,
u32 eltCount,
mg_path_elt* elements)
{
mg_surface_data* surfaceData = mg_surface_data_from_handle(surface);
if(surfaceData && surfaceData->backend)
{
surfaceData->backend->render(surfaceData->backend,
clearColor,
primitiveCount,
primitives,
eltCount,
elements);
}
}
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------

View File

@ -1,15 +1,14 @@
/************************************************************//**
*
* @file: graphics_internal.h
* @file: graphics_surface.h
* @author: Martin Fouilleul
* @date: 23/01/2023
* @revision:
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __GRAPHICS_INTERNAL_H_
#define __GRAPHICS_INTERNAL_H_
#ifndef __GRAPHICS_SURFACE_H_
#define __GRAPHICS_SURFACE_H_
#include"graphics.h"
#include"graphics_common.h"
#include"mp_app_internal.h"
#ifdef __cplusplus
@ -127,79 +126,6 @@ typedef void (*mg_canvas_backend_image_upload_region_proc)(mg_canvas_backend* ba
mp_rect region,
u8* pixels);
//------------------------------------------------------------------------
// canvas structs
//------------------------------------------------------------------------
typedef enum { MG_PATH_MOVE,
MG_PATH_LINE,
MG_PATH_QUADRATIC,
MG_PATH_CUBIC } mg_path_elt_type;
typedef struct mg_path_elt
{
mg_path_elt_type type;
vec2 p[3];
} mg_path_elt;
typedef struct mg_path_descriptor
{
u32 startIndex;
u32 count;
vec2 startPoint;
} mg_path_descriptor;
typedef struct mg_attributes
{
f32 width;
f32 tolerance;
mg_color color;
mg_joint_type joint;
f32 maxJointExcursion;
mg_cap_type cap;
mg_font font;
f32 fontSize;
mg_image image;
mp_rect srcRegion;
mg_mat2x3 transform;
mp_rect clip;
} mg_attributes;
typedef struct mg_rounded_rect
{
f32 x;
f32 y;
f32 w;
f32 h;
f32 r;
} mg_rounded_rect;
typedef enum { MG_CMD_FILL,
MG_CMD_STROKE,
MG_CMD_JUMP
} mg_primitive_cmd;
typedef struct mg_primitive
{
mg_primitive_cmd cmd;
mg_attributes attributes;
union
{
mg_path_descriptor path;
mp_rect rect;
mg_rounded_rect roundedRect;
u32 jump;
};
} mg_primitive;
typedef void (*mg_canvas_backend_render_proc)(mg_canvas_backend* backend,
mg_color clearColor,
u32 primitiveCount,
@ -229,4 +155,4 @@ typedef struct mg_canvas_backend
} // extern "C"
#endif
#endif //__GRAPHICS_INTERNAL_H_
#endif //__GRAPHICS_SURFACE_H_

View File

@ -10,7 +10,7 @@
#import<QuartzCore/CAMetalLayer.h>
#include<simd/simd.h>
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"macro_helpers.h"
#include"osx_app.h"

View File

@ -9,7 +9,7 @@
#ifndef __MTL_SURFACE_H_
#define __MTL_SURFACE_H_
#include"graphics_internal.h"
#include"graphics_surface.h"
#ifdef __OBJC__
#import<Metal/Metal.h>

View File

@ -11,7 +11,7 @@
#import<QuartzCore/CAMetalLayer.h>
#include<simd/simd.h>
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"macro_helpers.h"
#include"osx_app.h"

View File

@ -18,7 +18,7 @@
#include"macro_helpers.h"
#include"platform_log.h"
#include"platform_clock.h"
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"mp_app.c"

View File

@ -0,0 +1,26 @@
/************************************************************//**
*
* @file: platform_math.h
* @author: Martin Fouilleul
* @date: 26/04/2023
*
*****************************************************************/
#ifndef __PLATFORM_MATH_H_
#define __PLATFORM_MATH_H_
#include"platform.h"
#if !PLATFORM_ORCA
#include<math.h>
#else
#define M_PI 3.14159265358979323846
double fabs(double x);
double sqrt(double sqrt);
double cos(double x);
double sin(double x);
#endif
#endif //__PLATFORM_MATH_H_

View File

@ -404,7 +404,7 @@ MP_API void ui_begin_frame(vec2 size, ui_style* defaultStyle, ui_style_mask mask
MP_API void ui_end_frame(void);
MP_API void ui_draw(void);
#define ui_frame(s, m) defer_loop(ui_begin_frame((s), (m)), ui_end_frame())
#define ui_frame(size, style, mask) defer_loop(ui_begin_frame((size), (style), (mask)), ui_end_frame())
//-------------------------------------------------------------------------------------
// Box keys

View File

@ -7,7 +7,7 @@
*
*****************************************************************/
#include"win32_app.h"
#include"graphics_internal.h"
#include"graphics_surface.h"
#include"gl_loader.h"
#include<GL/wglext.h>

View File

@ -9,7 +9,7 @@
#ifndef __WGL_SURFACE_H_
#define __WGL_SURFACE_H_
#include"graphics_internal.h"
#include"graphics_surface.h"
mg_surface_data* mg_wgl_surface_create_for_window(mp_window window);

View File

@ -863,7 +863,7 @@ MP_API str8 mp_clipboard_copy_string(str8 backing)
// win32 surfaces
//--------------------------------------------------------------------------------
#include"graphics_internal.h"
#include"graphics_surface.h"
vec2 mg_win32_surface_contents_scaling(mg_surface_data* surface)
{