working gpu renderer

This commit is contained in:
2025-07-29 15:15:18 -05:00
parent 09c3d5cc4e
commit e5c19e7e80
7 changed files with 738 additions and 2003 deletions

View File

@@ -153,8 +153,6 @@ void actor_free(cell_rt *actor)
JS_FreeAtom(js, actor->actor_sym);
SDL_RemoveTimer(actor->ar);
for (int i = 0; i < arrlen(actor->js_swapchains); i++)
JS_FreeValue(js, actor->js_swapchains[i]);
/* Free timer callbacks stored in actor */
for (int i = 0; i < hmlen(actor->timers); i++) {
@@ -162,7 +160,6 @@ void actor_free(cell_rt *actor)
}
hmfree(actor->timers);
arrfree(actor->js_swapchains);
arrfree(actor->module_registry);
/* Free all letters in the queue */

View File

@@ -65,7 +65,6 @@ mi_heap_t *heap;
void *init_wota;
ModuleEntry *module_registry;
JSValue *js_swapchains;
/* Protects JSContext usage */
SDL_Mutex *mutex; /* for everything else */

View File

@@ -1436,34 +1436,6 @@ JSC_CCALL(os_power_state,
return JS_NULL;
)
JSC_CCALL(os_cull_sprites,
ret = JS_NewArray(js);
int n = 0;
JSValue sprites = argv[0];
shader_globals info = {0}; // TODO: get this as a JS object
rect camera_rect = {0};
camera_rect.x = info.camera_pos_world.x - info.render_size.x/2.0;
camera_rect.y = info.camera_pos_world.y - info.render_size.y/2.0;
camera_rect.w = info.render_size.x;
camera_rect.h = info.render_size.y;
int len = JS_ArrayLength(js,sprites);
for (int i = 0; i < len; i++) {
JSValue sub = JS_GetPropertyUint32(js,sprites,i);
transform *t;
JS_GETATOM(js,t,sub,transform,transform)
rect sprite = transform2rect(t);
if (SDL_HasRectIntersectionFloat(&sprite, &camera_rect)) {
JS_SetPropertyUint32(js,ret,n,JS_DupValue(js,sub));
n++;
}
JS_FreeValue(js,sub);
}
)
static const JSCFunctionListEntry js_util_funcs[] = {
MIST_FUNC_DEF(os, guid, 0),
MIST_FUNC_DEF(os, insertion_sort, 2),
@@ -1524,7 +1496,6 @@ static const JSCFunctionListEntry js_graphics_funcs[] = {
MIST_FUNC_DEF(os, make_texture, 1),
MIST_FUNC_DEF(os, make_gif, 1),
MIST_FUNC_DEF(os, make_aseprite, 1),
MIST_FUNC_DEF(os, cull_sprites, 2),
MIST_FUNC_DEF(os, make_font, 2),
MIST_FUNC_DEF(os, make_line_prim, 5),
MIST_FUNC_DEF(graphics, hsl_to_rgb, 3),

View File

@@ -56,24 +56,6 @@ struct lrtb {
typedef struct lrtb lrtb;
typedef struct text_vert text_vert;
// Shader globals structure for camera transformations
#pragma pack(push, 1)
typedef struct shader_globals {
HMM_Mat4 world_to_projection;
HMM_Mat4 projection_to_world;
HMM_Mat4 world_to_view;
HMM_Mat4 view_to_projection;
HMM_Vec3 camera_pos_world;
HMM_Vec3 camera_dir_world;
float viewport_min_z;
float viewport_max_z;
HMM_Vec2 viewport_size;
HMM_Vec2 viewport_offset;
HMM_Vec2 render_size;
float time;
} shader_globals;
#pragma pack(pop)
// Common macros for property access
#define JS_GETPROP(JS, TARGET, VALUE, PROP, TYPE) {\
JSValue __##PROP##__v = JS_GetPropertyStr(JS,VALUE,#PROP); \

View File

@@ -971,6 +971,34 @@ JSC_CCALL(geometry_tilemap_to_data,
free(index_data);
)
static void print_buffers(float *xy_data, float *uv_data, SDL_FColor *color_data, uint16_t *index_data, int vertex_count, int index_count)
{
printf("=== GEOMETRY BUFFERS ===\n");
printf("XY Data (%d vertices):\n", vertex_count);
for (int i = 0; i < vertex_count; i++) {
printf(" Vertex %d: x=%.2f, y=%.2f\n", i, xy_data[i*2], xy_data[i*2+1]);
}
printf("\nUV Data:\n");
for (int i = 0; i < vertex_count; i++) {
printf(" Vertex %d: u=%.4f, v=%.4f\n", i, uv_data[i*2], uv_data[i*2+1]);
}
printf("\nColor Data:\n");
for (int i = 0; i < vertex_count; i++) {
printf(" Vertex %d: r=%.2f, g=%.2f, b=%.2f, a=%.2f\n",
i, color_data[i].r, color_data[i].g, color_data[i].b, color_data[i].a);
}
printf("\nIndex Data (%d indices):\n", index_count);
for (int i = 0; i < index_count; i += 3) {
printf(" Triangle %d: %d, %d, %d\n", i/3, index_data[i], index_data[i+1], index_data[i+2]);
}
printf("========================\n");
}
JSC_CCALL(geometry_sprites_to_data,
JSValue sprites_array = argv[0];
if (!JS_IsArray(js, sprites_array)) {
@@ -1318,6 +1346,108 @@ JSC_CCALL(geometry_renderitem_clear,
return JS_NULL;
)
JSC_CCALL(geometry_array_blob,
JSValue arr = argv[0];
size_t len = JS_ArrayLength(js,arr);
float data[len];
for (int i = 0; i < len; i++) {
JSValue val = JS_GetPropertyUint32(js,arr,i);
data[i] = js2number(js, val);
JS_FreeValue(js,val);
}
return js_new_blob_stoned_copy(js, data, sizeof(float)*len);
)
JSC_CCALL(geometry_make_quad_indices,
int quads;
if (JS_ToInt32(js, &quads, argv[0]) < 0) {
return JS_ThrowTypeError(js, "quads must be a number");
}
if (quads < 0) {
return JS_ThrowTypeError(js, "quads must be >= 0");
}
int index_count = quads * 6;
uint16_t *index_data = malloc(index_count * sizeof(uint16_t));
int index_idx = 0;
for (int i = 0; i < quads; i++) {
uint16_t base_idx = i * 4;
index_data[index_idx++] = base_idx + 0;
index_data[index_idx++] = base_idx + 1;
index_data[index_idx++] = base_idx + 2;
index_data[index_idx++] = base_idx + 1;
index_data[index_idx++] = base_idx + 3;
index_data[index_idx++] = base_idx + 2;
}
JSValue result = js_new_blob_stoned_copy(js, index_data, index_count * sizeof(uint16_t));
free(index_data);
return result;
)
JSC_CCALL(geometry_make_rect_quad,
rect r = js2rect(js, argv[0]);
// Optional UV rect (default to 0,0,1,1)
rect uv = {0, 0, 1, 1};
if (argc > 1 && !JS_IsNull(argv[1])) {
uv = js2rect(js, argv[1]);
}
// Optional color (default to white)
SDL_FColor color = {1.0f, 1.0f, 1.0f, 1.0f};
if (argc > 2 && !JS_IsNull(argv[2])) {
HMM_Vec4 c = js2color(js, argv[2]);
color.r = c.r;
color.g = c.g;
color.b = c.b;
color.a = c.a;
}
// Allocate buffers for 1 quad (4 vertices)
int vertex_count = 4;
int index_count = 6;
float xy_data[vertex_count*2];
float uv_data[vertex_count*2];
SDL_FColor color_data[vertex_count];
// Set vertex positions (4 corners)
xy_data[0] = r.x; xy_data[1] = r.y; // bottom-left
xy_data[2] = r.x + r.w; xy_data[3] = r.y; // bottom-right
xy_data[4] = r.x; xy_data[5] = r.y + r.h; // top-left
xy_data[6] = r.x + r.w; xy_data[7] = r.y + r.h; // top-right
// Set UV coordinates
uv_data[0] = uv.x; uv_data[1] = uv.y + uv.h; // bottom-left
uv_data[2] = uv.x + uv.w; uv_data[3] = uv.y + uv.h; // bottom-right
uv_data[4] = uv.x; uv_data[5] = uv.y; // top-left
uv_data[6] = uv.x + uv.w; uv_data[7] = uv.y; // top-right
// Set colors
for (int i = 0; i < 4; i++) {
color_data[i] = color;
}
// Create result object
ret = JS_NewObject(js);
JSValue xy_blob = js_new_blob_stoned_copy(js, xy_data, vertex_count * 2 * sizeof(float));
JSValue uv_blob = js_new_blob_stoned_copy(js, uv_data, vertex_count * 2 * sizeof(float));
JSValue color_blob = js_new_blob_stoned_copy(js, color_data, vertex_count * sizeof(SDL_FColor));
JS_SetPropertyStr(js, ret, "xy", xy_blob);
JS_SetPropertyStr(js, ret, "xy_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "uv", uv_blob);
JS_SetPropertyStr(js, ret, "uv_stride", JS_NewInt32(js, 2 * sizeof(float)));
JS_SetPropertyStr(js, ret, "color", color_blob);
JS_SetPropertyStr(js, ret, "color_stride", JS_NewInt32(js, sizeof(SDL_FColor)));
JS_SetPropertyStr(js, ret, "num_vertices", JS_NewInt32(js, vertex_count));
)
static const JSCFunctionListEntry js_geometry_funcs[] = {
MIST_FUNC_DEF(geometry, rect_intersection, 2),
MIST_FUNC_DEF(geometry, rect_intersects, 2),
@@ -1339,6 +1469,9 @@ static const JSCFunctionListEntry js_geometry_funcs[] = {
MIST_FUNC_DEF(geometry, renderitem_push, 3),
MIST_FUNC_DEF(geometry, renderitem_sort, 0),
MIST_FUNC_DEF(geometry, renderitem_clear, 0),
MIST_FUNC_DEF(geometry, array_blob, 2),
MIST_FUNC_DEF(geometry, make_quad_indices, 1),
MIST_FUNC_DEF(geometry, make_rect_quad, 3),
};
JSValue js_geometry_use(JSContext *js) {

File diff suppressed because it is too large Load Diff