From 1a556dbbadd8b57e6ce013c8873f7fe48b88f698 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Tue, 11 Nov 2025 12:33:29 +0100 Subject: [PATCH] wasm / webgpu progress --- base/project.js | 6 +- base/sources/backends/wasm_system.c | 3 + base/sources/backends/webgpu_gpu.c | 8 +- base/sources/iron_gc.c | 1 + base/sources/libs/kong/dir.c | 15 +++- base/sources/libs/miniclib/ctype.c | 10 +++ base/sources/libs/miniclib/ctype.h | 3 + base/sources/libs/miniclib/math.c | 116 +++++++++++++++++++++++++++ base/sources/libs/miniclib/math.h | 51 ++++++------ base/sources/libs/miniclib/memory.c | 4 + base/sources/libs/miniclib/memory.h | 8 -- base/sources/libs/miniclib/stdbool.h | 2 - base/sources/libs/miniclib/stdio.c | 8 ++ base/sources/libs/miniclib/stdio.h | 10 +-- base/sources/libs/miniclib/stdlib.c | 16 ++++ base/sources/libs/miniclib/stdlib.h | 27 +++---- base/sources/libs/miniclib/string.h | 8 -- base/sources/libs/miniclib/time.h | 8 -- base/tools/make.js | 4 +- 19 files changed, 225 insertions(+), 83 deletions(-) create mode 100644 base/sources/libs/miniclib/ctype.c diff --git a/base/project.js b/base/project.js index ddb4966a..9de07035 100644 --- a/base/project.js +++ b/base/project.js @@ -40,7 +40,10 @@ project.add_assets("assets/licenses/**", {destination : "data/licenses/{name}"}) project.add_assets("assets/themes/*.json", {destination : "data/themes/{name}"}); project.add_cfiles("sources/*.c"); // project.add_cfiles("sources/iron.c"); -project.add_cfiles("sources/libs/gc.c"); +if (platform != "wasm") { + project.add_cfiles("sources/libs/gc.c"); +} + project.add_cfiles("sources/libs/kong/dir.c"); project.add_define("IRON_C_PATH=\"" + os_cwd() + "/build/iron.c" + "\""); @@ -143,6 +146,7 @@ else if (platform == "wasm") { project.add_cfiles("sources/libs/miniclib/**"); project.add_define("IRON_WASM"); project.add_define("IRON_WEBGPU"); + project.add_define("NO_GC"); project.add_include_dir("sources/libs/miniclib"); } diff --git a/base/sources/backends/wasm_system.c b/base/sources/backends/wasm_system.c index 0ec1fe2d..0376d2ca 100644 --- a/base/sources/backends/wasm_system.c +++ b/base/sources/backends/wasm_system.c @@ -34,8 +34,11 @@ bool iron_mouse_can_lock(void) { void iron_mouse_show() {} void iron_mouse_hide() {} +void iron_mouse_set_cursor(iron_cursor_t cursor_index) {} void iron_mouse_set_position(int x, int y) {} void iron_mouse_get_position(int *x, int *y) {} +void iron_keyboard_show() {} +void iron_keyboard_hide() {} __attribute__((import_module("imports"), import_name("js_time"))) int js_time(); diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index 4352d8fd..2f1b1813 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -40,9 +39,6 @@ static int current_height = 0; static WGPURenderPassColorAttachment current_color_attachment_infos[8]; static WGPURenderPassDepthStencilAttachment current_depth_attachment_info; -void iron_webgpu_create_surface(WGPUInstance instance, WGPUSurface *surface); -void gpu_create_framebuffers(int depth_buffer_bits); - static WGPUTextureFormat convert_image_format(gpu_texture_format_t format) { switch (format) { case GPU_TEXTURE_FORMAT_RGBA128: @@ -179,6 +175,9 @@ static void create_descriptors(void) { point_sampler = wgpuDeviceCreateSampler(device, &sampler_info); } +void gpu_barrier(gpu_texture_t *render_target, gpu_texture_state_t state_after) { +} + void gpu_render_target_init2(gpu_texture_t *target, int width, int height, gpu_texture_format_t format, int framebuffer_index) { target->width = width; target->height = height; @@ -254,7 +253,6 @@ void gpu_init_internal(int depth_buffer_bits, bool vsync) { instance = wgpuCreateInstance(NULL); window_depth_bits = depth_buffer_bits; window_vsync = vsync; - iron_webgpu_create_surface(instance, &surface); WGPURequestAdapterOptions options = {.compatibleSurface = surface, .powerPreference = WGPUPowerPreference_HighPerformance}; gpu = NULL; diff --git a/base/sources/iron_gc.c b/base/sources/iron_gc.c index 56a9b1e4..df619a90 100644 --- a/base/sources/iron_gc.c +++ b/base/sources/iron_gc.c @@ -6,6 +6,7 @@ #include #include #include +#include #define HEAP_SIZE 512 * 1024 * 1024 static uint8_t *heap = NULL; diff --git a/base/sources/libs/kong/dir.c b/base/sources/libs/kong/dir.c index 54ea7e12..8833718a 100644 --- a/base/sources/libs/kong/dir.c +++ b/base/sources/libs/kong/dir.c @@ -45,10 +45,23 @@ void close_dir(directory *dir) { FindClose(dir->handle); } +#elif defined(IRON_WASM) + +directory open_dir(const char *dirname) { + directory dir; + return dir; +} + +file read_next_file(directory *dir) { + file f; + return f; +} + +void close_dir(directory *dir) {} + #else #include - #include #include #include diff --git a/base/sources/libs/miniclib/ctype.c b/base/sources/libs/miniclib/ctype.c new file mode 100644 index 00000000..e7c09e82 --- /dev/null +++ b/base/sources/libs/miniclib/ctype.c @@ -0,0 +1,10 @@ + +#include "ctype.h" + +int tolower(int ch) { + return 0; +} + +int toupper(int ch) { + return 0; +} diff --git a/base/sources/libs/miniclib/ctype.h b/base/sources/libs/miniclib/ctype.h index e69de29b..43ff4fc2 100644 --- a/base/sources/libs/miniclib/ctype.h +++ b/base/sources/libs/miniclib/ctype.h @@ -0,0 +1,3 @@ + +int tolower(int ch); +int toupper(int ch); diff --git a/base/sources/libs/miniclib/math.c b/base/sources/libs/miniclib/math.c index d4cc5bbe..5380fc9d 100644 --- a/base/sources/libs/miniclib/math.c +++ b/base/sources/libs/miniclib/math.c @@ -11,6 +11,30 @@ __attribute__((import_module("imports"), import_name("js_exp"))) float js_exp( __attribute__((import_module("imports"), import_name("js_sqrt"))) float js_sqrt(float x); #endif +double fabs(double n) { + return n < 0 ? -n : n; +} + +float fabsf(float n) { + return n < 0 ? -n : n; +} + +double fmax(double x, double y) { + return 0.0; +} + +float fmaxf(float x, float y) { + return 0.0f; +} + +double fmin(double x, double y) { + return 0.0; +} + +float fminf(float x, float y) { + return 0.0f; +} + double ldexp(double x, int exp) { return 0.0; } @@ -22,6 +46,13 @@ double pow(double base, double exponent) { return 0.0; } +float powf(float base, float exponent) { +#ifdef IRON_WASM + return js_pow(base, exponent); +#endif + return 0.0f; +} + double floor(double x) { #ifdef IRON_WASM return js_floor(x); @@ -36,6 +67,34 @@ float floorf(float x) { return 0.0f; } +double ceil(double x) { + // #ifdef IRON_WASM + // return js_ceil(x); + // #endif + return 0.0; +} + +float ceilf(float x) { + // #ifdef IRON_WASM + // return js_ceil(x); + // #endif + return 0.0f; +} + +double round(double x) { + // #ifdef IRON_WASM + // return js_round(x); + // #endif + return 0.0; +} + +float roundf(float x) { + // #ifdef IRON_WASM + // return js_round(x); + // #endif + return 0.0f; +} + double sin(double x) { #ifdef IRON_WASM return js_sin(x); @@ -50,6 +109,10 @@ float sinf(float x) { return 0.0f; } +float asinf(float x) { + return 0.0f; +} + double cos(double x) { #ifdef IRON_WASM return js_cos(x); @@ -64,6 +127,14 @@ float cosf(float x) { return 0.0f; } +double acos(double x) { + return 0.0; +} + +float acosf(float x) { + return 0.0f; +} + double tan(double x) { #ifdef IRON_WASM return js_tan(x); @@ -78,6 +149,18 @@ float tanf(float x) { return 0.0f; } +float atanf(float x) { + return 0.0f; +} + +double atan2(double x, double y) { + return 0.0; +} + +float atan2f(float x, float y) { + return 0.0f; +} + double log(double x) { #ifdef IRON_WASM return js_log(x); @@ -85,6 +168,17 @@ double log(double x) { return 0.0; } +float logf(float x) { +#ifdef IRON_WASM + return js_log(x); +#endif + return 0.0f; +} + +float log2f(float x) { + return 0.0f; +} + double exp(double x) { #ifdef IRON_WASM return js_exp(x); @@ -92,9 +186,31 @@ double exp(double x) { return 0.0; } +float expf(float x) { +#ifdef IRON_WASM + return js_exp(x); +#endif + return 0.0; +} + double sqrt(double x) { #ifdef IRON_WASM return js_sqrt(x); #endif return 0.0; } + +float sqrtf(float x) { +#ifdef IRON_WASM + return js_sqrt(x); +#endif + return 0.0f; +} + +double fmod(double x, double y) { + return 0.0; +} + +int isnan(float x) { + return 0; +} diff --git a/base/sources/libs/miniclib/math.h b/base/sources/libs/miniclib/math.h index 5f45ee72..29b01e53 100644 --- a/base/sources/libs/miniclib/math.h +++ b/base/sources/libs/miniclib/math.h @@ -1,35 +1,40 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - +double fabs(double n); +float fabsf(float n); +double fmax(double x, double y); +float fmaxf(float x, float y); +double fmin(double x, double y); +float fminf(float x, float y); double ldexp(double x, int exp); - double pow(double base, double exponent); - +float powf(float base, float exponent); double floor(double x); - -float floorf(float x); - +float floorf(float x); +double ceil(double x); +float ceilf(float x); +double round(double x); +float roundf(float x); double sin(double x); - -float sinf(float x); - +float sinf(float x); +float asinf(float x); double cos(double x); - -float cosf(float x); - +float cosf(float x); +double acos(double x); +float acosf(float x); double tan(double x); - -float tanf(float x); - +float tanf(float x); +float atanf(float x); +double atan2(double x, double y); +float atan2f(float x, float y); double log(double x); - +float logf(float x); +float log2f(float x); double exp(double x); - +float expf(float x); double sqrt(double x); +float sqrtf(float x); +double fmod(double x, double y); -#ifdef __cplusplus -} -#endif +int isnan(float x); +#define NAN (0.0F / 0.0F) diff --git a/base/sources/libs/miniclib/memory.c b/base/sources/libs/miniclib/memory.c index 4e847909..23799fc4 100644 --- a/base/sources/libs/miniclib/memory.c +++ b/base/sources/libs/miniclib/memory.c @@ -28,6 +28,10 @@ malloc(size_t size) { return NULL; } +void *calloc(size_t num, size_t size) { + return NULL; +} + void *alloca(size_t size) { return NULL; } diff --git a/base/sources/libs/miniclib/memory.h b/base/sources/libs/miniclib/memory.h index d537c33f..ac5dfbf1 100644 --- a/base/sources/libs/miniclib/memory.h +++ b/base/sources/libs/miniclib/memory.h @@ -1,11 +1,3 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - #include - -#ifdef __cplusplus -} -#endif diff --git a/base/sources/libs/miniclib/stdbool.h b/base/sources/libs/miniclib/stdbool.h index 784a6d49..c34d6da8 100644 --- a/base/sources/libs/miniclib/stdbool.h +++ b/base/sources/libs/miniclib/stdbool.h @@ -1,7 +1,5 @@ #pragma once -#ifndef __cplusplus typedef _Bool bool; #define true 1 #define false 0 -#endif diff --git a/base/sources/libs/miniclib/stdio.c b/base/sources/libs/miniclib/stdio.c index e57dba25..b1678146 100644 --- a/base/sources/libs/miniclib/stdio.c +++ b/base/sources/libs/miniclib/stdio.c @@ -17,6 +17,14 @@ int fprintf(FILE *stream, const char *format, ...) { return 0; } +int sprintf(char *s, const char *format, ...) { + return 0; +} + +int snprintf(char *s, size_t n, const char *format, ...) { + return 0; +} + int vsnprintf(char *s, size_t n, const char *format, va_list arg) { return 0; } diff --git a/base/sources/libs/miniclib/stdio.h b/base/sources/libs/miniclib/stdio.h index 68f412ad..e4124d7a 100644 --- a/base/sources/libs/miniclib/stdio.h +++ b/base/sources/libs/miniclib/stdio.h @@ -3,10 +3,6 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif - #define SEEK_SET 0 #define SEEK_END 1 @@ -16,6 +12,8 @@ extern FILE *stdout, *stderr; int fprintf(FILE *stream, const char *format, ...); +int sprintf(char *s, const char *format, ...); +int snprintf(char *s, size_t n, const char *format, ...); int vsnprintf(char *s, size_t n, const char *format, va_list arg); size_t fwrite(const void *ptr, size_t size, size_t count, FILE *stream); @@ -31,7 +29,3 @@ int fseek(FILE *stream, long int offset, int origin); size_t fread(void *ptr, size_t size, size_t count, FILE *stream); int fputs(const char *str, FILE *stream); - -#ifdef __cplusplus -} -#endif diff --git a/base/sources/libs/miniclib/stdlib.c b/base/sources/libs/miniclib/stdlib.c index 6d48e2d5..e689b6c6 100644 --- a/base/sources/libs/miniclib/stdlib.c +++ b/base/sources/libs/miniclib/stdlib.c @@ -1,5 +1,9 @@ #include "stdlib.h" +int system(const char *string) { + return 0; +} + void exit(int code) { exit(code); } @@ -8,6 +12,14 @@ long int strtol(const char *str, char **endptr, int base) { return 0; } +float strtof(const char *str, char **endptr) { + return 0.0f; +} + +double atof (const char* str) { + return 0.0; +} + int abs(int n) { return n < 0 ? -n : n; } @@ -17,3 +29,7 @@ long long int llabs(long long int n) { } void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *)) {} + +int rand() { + return 0; +} diff --git a/base/sources/libs/miniclib/stdlib.h b/base/sources/libs/miniclib/stdlib.h index 671f0af9..297a4f2c 100644 --- a/base/sources/libs/miniclib/stdlib.h +++ b/base/sources/libs/miniclib/stdlib.h @@ -2,31 +2,24 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - typedef unsigned long size_t; #define EXIT_FAILURE 1 void *malloc(size_t size); - +void *calloc(size_t num, size_t size); void *alloca(size_t size); - void *realloc(void *mem, size_t size); +void free(void *mem); -void free(void *mem); - +int system(const char *string); void exit(int code); -long int strtol(const char *str, char **endptr, int base); - -int abs(int n); - +long int strtol(const char *str, char **endptr, int base); +float strtof(const char *str, char **endptr); +double atof(const char *str); +int abs(int n); long long int llabs(long long int n); +void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *)); +int rand(); -void qsort(void *base, size_t num, size_t size, int (*compar)(const void *, const void *)); - -#ifdef __cplusplus -} -#endif +#define RAND_MAX 2147483647 diff --git a/base/sources/libs/miniclib/string.h b/base/sources/libs/miniclib/string.h index f8b259be..2c88c8d3 100644 --- a/base/sources/libs/miniclib/string.h +++ b/base/sources/libs/miniclib/string.h @@ -2,10 +2,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - void *memset(void *ptr, int value, size_t num); void *memcpy(void *destination, const void *source, size_t num); @@ -40,7 +36,3 @@ wchar_t *wcsstr(wchar_t *str1, const wchar_t *str2); int wcscmp(const wchar_t *str1, const wchar_t *str2); int wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t num); - -#ifdef __cplusplus -} -#endif diff --git a/base/sources/libs/miniclib/time.h b/base/sources/libs/miniclib/time.h index d537c33f..ac5dfbf1 100644 --- a/base/sources/libs/miniclib/time.h +++ b/base/sources/libs/miniclib/time.h @@ -1,11 +1,3 @@ #pragma once -#ifdef __cplusplus -extern "C" { -#endif - #include - -#ifdef __cplusplus -} -#endif diff --git a/base/tools/make.js b/base/tools/make.js index 781ea674..a3df3764 100644 --- a/base/tools/make.js +++ b/base/tools/make.js @@ -973,8 +973,8 @@ class WasmExporter extends Exporter { constructor() { super(); this.compile_commands = new CompilerCommandsExporter(); - let compiler = "clang --target=wasm32"; - let compilerFlags = "--target=wasm32 -nostdlib -matomics -mbulk-memory"; + let compiler = "clang --target=wasm32 -nostdlib -matomics -mbulk-memory"; + let compilerFlags = ""; this.make = new MakeExporter(compiler, compiler, compilerFlags, compilerFlags, '--target=wasm32 -nostdlib -matomics -mbulk-memory "-Wl,--import-memory,--shared-memory"', '.wasm'); }