window_size_title_api #61

Merged
MartinFouilleul merged 8 commits from window_size_title_api into main 2023-08-25 13:29:28 +00:00
15 changed files with 580 additions and 452 deletions

View File

@ -19,6 +19,9 @@ f32 minf(f32 a, f32 b);
ORCA_EXPORT void oc_on_init(void)
{
oc_runtime_window_set_title(OC_STR8("clock"));
oc_runtime_window_set_size((oc_vec2){ .x = 400, .y = 400 });
surface = oc_surface_canvas();
canvas = oc_canvas_create();

View File

@ -615,6 +615,8 @@ ORCA_EXPORT void oc_on_init()
{
oc_log_info("Hello, world (from C)");
oc_runtime_window_set_title(OC_STR8("fluid"));
surface = oc_surface_gles();
oc_surface_select(surface);

View File

@ -37,6 +37,8 @@ void compile_shader(GLuint shader, const char* source)
ORCA_EXPORT void oc_on_init(void)
{
oc_runtime_window_set_title(OC_STR8("triangle"));
surface = oc_surface_gles();
oc_surface_select(surface);

View File

@ -69,6 +69,8 @@ oc_str8 loadFile(oc_arena* arena, oc_str8 filename)
ORCA_EXPORT void oc_on_init(void)
{
oc_runtime_window_set_title(OC_STR8("pong"));
surface = oc_surface_canvas();
canvas = oc_canvas_create();

View File

@ -10,6 +10,8 @@ oc_arena textArena = { 0 };
ORCA_EXPORT void oc_on_init(void)
{
oc_runtime_window_set_title(OC_STR8("ui"));
surface = oc_surface_canvas();
canvas = oc_canvas_create();
oc_ui_init(&ui);

View File

@ -397,6 +397,7 @@ def gen_all_bindings():
)
bindgen("core", "src/wasmbind/core_api.json",
guest_stubs="src/wasmbind/core_api_stubs.c",
wasm3_bindings="src/wasmbind/core_api_bind_gen.c",
)

View File

@ -15,8 +15,7 @@
#include "util/utf8.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
//--------------------------------------------------------------------
@ -324,8 +323,12 @@ extern "C"
ORCA_API void oc_window_hide(oc_window window);
ORCA_API void oc_window_show(oc_window window);
ORCA_API void oc_window_set_title(oc_window window, oc_str8 title);
ORCA_API bool oc_window_is_minimized(oc_window window);
ORCA_API bool oc_window_is_maximized(oc_window window);
ORCA_API void oc_window_minimize(oc_window window);
ORCA_API void oc_window_maximize(oc_window window);
ORCA_API void oc_window_restore(oc_window window);
ORCA_API bool oc_window_has_focus(oc_window window);
@ -402,6 +405,10 @@ extern "C"
void ORCA_IMPORT(oc_request_quit)(void);
void ORCA_IMPORT(oc_runtime_window_set_title)(oc_str8 title);
void ORCA_IMPORT(oc_runtime_window_set_size)(oc_vec2 size);
#endif // !defined(OC_PLATFORM_ORCA) || !(OC_PLATFORM_ORCA)
#ifdef __cplusplus

View File

@ -1471,6 +1471,29 @@ bool oc_window_is_focused(oc_window window)
}
}
void oc_window_set_title(oc_window window, oc_str8 title)
{
dispatch_block_t block = ^{
@autoreleasepool
{
oc_window_data* windowData = oc_window_ptr_from_handle(window);
if(windowData)
{
windowData->osx.nsWindow.title = [[NSString alloc] initWithBytes:title.ptr length:title.len encoding:NSUTF8StringEncoding];
}
}
};
if([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}
bool oc_window_is_minimized(oc_window window)
{
@autoreleasepool
@ -1634,9 +1657,9 @@ oc_rect oc_window_get_content_rect(oc_window window)
void oc_window_set_content_rect(oc_window window, oc_rect rect)
{
dispatch_block_t block = ^{
@autoreleasepool
{
oc_window_data* windowData = oc_window_ptr_from_handle(window);
if(windowData)
{
@ -1652,6 +1675,16 @@ void oc_window_set_content_rect(oc_window window, oc_rect rect)
[windowData->osx.nsWindow setFrame:frameRect display:YES];
}
}
};
if([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}
//--------------------------------------------------------------------

View File

@ -789,6 +789,20 @@ void oc_window_show(oc_window window)
}
}
void oc_window_set_title(oc_window window, oc_str8 title)
{
oc_window_data* windowData = oc_window_ptr_from_handle(window);
if(windowData)
{
oc_arena_scope scratch = oc_scratch_begin();
const char* titleCString = oc_str8_to_cstring(scratch.arena, title);
SetWindowText(windowData->win32.hWnd, titleCString);
oc_scratch_end(scratch);
}
}
bool oc_window_is_minimized(oc_window window)
{
oc_window_data* windowData = oc_window_ptr_from_handle(window);

View File

@ -984,6 +984,8 @@ void oc_mtl_render_batch(oc_mtl_canvas_backend* backend,
[blitEncoder endEncoding];
//NOTE: path setup pass
if(pathCount > 0)
{
id<MTLComputeCommandEncoder> pathEncoder = [surface->commandBuffer computeCommandEncoder];
pathEncoder.label = @"path pass";
[pathEncoder setComputePipelineState:backend->pathPipeline];
@ -1004,15 +1006,18 @@ void oc_mtl_render_batch(oc_mtl_canvas_backend* backend,
[pathEncoder dispatchThreads:pathGridSize threadsPerThreadgroup:pathGroupSize];
[pathEncoder endEncoding];
//NOTE: segment setup pass
id<MTLComputeCommandEncoder> segmentEncoder = [surface->commandBuffer computeCommandEncoder];
segmentEncoder.label = @"segment pass";
[segmentEncoder setComputePipelineState:backend->segmentPipeline];
}
int tileOpMax = [backend->tileOpBuffer length] / sizeof(oc_mtl_tile_op);
int segmentMax = [backend->segmentBuffer length] / sizeof(oc_mtl_segment);
//NOTE: segment setup pass
if(eltCount > 0)
{
id<MTLComputeCommandEncoder> segmentEncoder = [surface->commandBuffer computeCommandEncoder];
segmentEncoder.label = @"segment pass";
[segmentEncoder setComputePipelineState:backend->segmentPipeline];
[segmentEncoder setBytes:&eltCount length:sizeof(int) atIndex:0];
[segmentEncoder setBuffer:backend->elementBuffer[backend->bufferIndex] offset:elementBufferOffset atIndex:1];
[segmentEncoder setBuffer:backend->segmentCountBuffer offset:0 atIndex:2];
@ -1033,8 +1038,11 @@ void oc_mtl_render_batch(oc_mtl_canvas_backend* backend,
[segmentEncoder dispatchThreads:segmentGridSize threadsPerThreadgroup:segmentGroupSize];
[segmentEncoder endEncoding];
}
//NOTE: backprop pass
if(pathCount > 0)
{
id<MTLComputeCommandEncoder> backpropEncoder = [surface->commandBuffer computeCommandEncoder];
backpropEncoder.label = @"backprop pass";
[backpropEncoder setComputePipelineState:backend->backpropPipeline];
@ -1049,8 +1057,11 @@ void oc_mtl_render_batch(oc_mtl_canvas_backend* backend,
[backpropEncoder dispatchThreads:backpropGridSize threadsPerThreadgroup:backpropGroupSize];
[backpropEncoder endEncoding];
}
//NOTE: merge pass
if(pathCount > 0)
{
id<MTLComputeCommandEncoder> mergeEncoder = [surface->commandBuffer computeCommandEncoder];
mergeEncoder.label = @"merge pass";
[mergeEncoder setComputePipelineState:backend->mergePipeline];
@ -1074,6 +1085,7 @@ void oc_mtl_render_batch(oc_mtl_canvas_backend* backend,
[mergeEncoder dispatchThreads:mergeGridSize threadsPerThreadgroup:mergeGroupSize];
[mergeEncoder endEncoding];
}
//NOTE: raster pass
id<MTLComputeCommandEncoder> rasterEncoder = [surface->commandBuffer computeCommandEncoder];

View File

@ -96,6 +96,7 @@
//NOTE: macos application layer and graphics backends are defined in orca.m
#elif OC_PLATFORM_ORCA
#include "app/orca_app.c"
#include "wasmbind/core_api_stubs.c"
#include "graphics/graphics_common.c"
#include "graphics/orca_surface_stubs.c"
#else

View File

@ -66,6 +66,37 @@ oc_runtime_env* oc_runtime_env_get()
return (&__orcaApp.runtime);
}
void* oc_runtime_ptr_to_native(oc_runtime* orca, void* wasmPtr, u32 length)
{
// We can't use the runtime's memory pointer directly because wasm3 embeds a
// header at the beginning of the block we give it.
u64 bufferIndex = (u64)wasmPtr & 0xffffffff;
u32 memSize = 0;
char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0);
if(bufferIndex + length < memSize)
{
char* nativePtr = memory + bufferIndex;
return nativePtr;
}
return NULL;
}
void oc_runtime_window_set_title(oc_str8 title)
{
title.ptr = oc_runtime_ptr_to_native(oc_runtime_get(), title.ptr, title.len);
if(title.ptr)
{
oc_window_set_title(__orcaApp.window, title);
}
}
void oc_runtime_window_set_size(oc_vec2 size)
{
oc_window_set_content_size(__orcaApp.window, size);
}
void oc_runtime_log(oc_log_level level,
int fileLen,
char* file,

View File

@ -125,4 +125,6 @@ typedef struct oc_runtime
oc_runtime* oc_runtime_get();
oc_runtime_env* oc_runtime_env_get();
void* oc_runtime_ptr_to_native(oc_runtime* runtime, void* wasmPtr, u32 length);
#endif //__RUNTIME_H_

View File

@ -14,19 +14,12 @@ oc_io_cmp oc_runtime_io_wait_single_req(oc_io_req* wasmReq)
oc_io_cmp cmp = { 0 };
oc_io_req req = *wasmReq;
//NOTE: convert the req->buffer wasm pointer to a native pointer
// for some reason, wasm3 memory doesn't start at the beginning of the block we give it.
u64 bufferIndex = (u64)req.buffer & 0xffffffff;
u32 memSize = 0;
char* memory = (char*)m3_GetMemory(orca->runtime.m3Runtime, &memSize, 0);
if(bufferIndex + req.size > memSize)
void* buffer = oc_runtime_ptr_to_native(orca, req.buffer, req.size);
if(buffer)
{
cmp.error = OC_IO_ERR_ARG;
}
else
{
req.buffer = memory + bufferIndex;
req.buffer = buffer;
if(req.op == OC_IO_OPEN_AT)
{
@ -39,5 +32,10 @@ oc_io_cmp oc_runtime_io_wait_single_req(oc_io_req* wasmReq)
}
cmp = oc_io_wait_single_req_with_table(&req, &orca->fileTable);
}
else
{
cmp.error = OC_IO_ERR_ARG;
}
return (cmp);
}

View File

@ -69,5 +69,23 @@
"cname": "oc_request_quit",
"ret": {"name": "void", "tag": "v"},
"args": []
},
{
"name": "oc_runtime_window_set_title",
"cname": "oc_runtime_window_set_title",
"ret": {"name": "void", "tag": "v"},
"args": [
{ "name": "title",
"type": {"name": "oc_str8", "tag": "S"}}
]
},
{
"name": "oc_runtime_window_set_size",
"cname": "oc_runtime_window_set_size",
"ret": {"name": "void", "tag": "v"},
"args": [
{ "name": "size",
"type": {"name": "oc_vec2", "tag": "S"}}
]
}
]