From 58cad839b601ee61f6c2a5c4fdfdd5d221e315ae Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 28 Mar 2025 14:44:57 -0500 Subject: [PATCH] fix crash --- scripts/modules/sdl_render.js | 305 +--------------------------------- source/jsffi.c | 2 +- source/prosperon.c | 6 +- tests/window.js | 6 +- 4 files changed, 13 insertions(+), 306 deletions(-) diff --git a/scripts/modules/sdl_render.js b/scripts/modules/sdl_render.js index f1d738f7..850e4af2 100644 --- a/scripts/modules/sdl_render.js +++ b/scripts/modules/sdl_render.js @@ -105,16 +105,6 @@ sprite_pipeline.target = { depth: "d32 float s8" }; -var appy = {}; -appy.inputs = {}; -if (os.platform() === "macos") { - appy.inputs["S-q"] = os.exit; -} - -appy.inputs["M-f4"] = os.exit; - -controller.player[0].control(appy); - prosperon.window = prosperon.engine_start(config); var driver = "vulkan" @@ -123,258 +113,31 @@ switch(os.platform()) { driver = "vulkan" break case "Windows": -// driver = "direct3d12" - driver = "vulkan" + driver = "direct3d12" break case "macOS": driver = "metal" break } -render._main = prosperon.window.make_gpu(false,driver) +render._main = prosperon.window.make_renderer(driver) prosperon.gpu = render._main render._main.window = prosperon.window -render._main.claim_window(prosperon.window) -render._main.set_swapchain('sdr', 'vsync') -var whiteimage = {} -whiteimage.surface = graphics.make_surface([1,1]) -whiteimage.surface.rect({x:0,y:0,width:1,height:1}, [1,1,1,1]) -whiteimage.texture = render._main.load_texture(whiteimage.surface) +//var imgui = use('imgui') +//if (imgui) imgui.init(render._main, prosperon.window) -var imgui = use('imgui') -if (imgui) imgui.init(render._main, prosperon.window) +var imgui var unit_transform = os.make_transform(); -var cur = {}; -cur.images = []; -cur.samplers = []; - -var tbuffer; -function full_upload(buffers) { - var cmds = render._main.acquire_cmd_buffer(); - tbuffer = render._main.upload(cmds, buffers, tbuffer); - cmds.submit(); -} - -full_upload[prosperon.DOC] = `Acquire a command buffer and upload the provided data buffers to the GPU, then submit. - -:param buffers: An array of data buffers to be uploaded. -:return: None -` - -function bind_pipeline(pass, pipeline) { - make_pipeline(pipeline) - pass.bind_pipeline(pipeline.gpu) - pass.pipeline = pipeline; -} - -bind_pipeline[prosperon.DOC] = `Ensure the specified pipeline is created on the GPU and bind it to the given render pass. - -:param pass: The current render pass to bind the pipeline to. -:param pipeline: The pipeline object containing shader and state info. -:return: None -` - -var main_pass; - var cornflower = [62/255,96/255,113/255,1]; -function get_pipeline_ubo_slot(pipeline, name) { - if (!pipeline.vertex.reflection.ubos) return; - for (var i = 0; i < pipeline.vertex.reflection.ubos.length; i++) { - var ubo = pipeline.vertex.reflection.ubos[i]; - if (ubo.name.endsWith(name)) - return i; - } - return undefined; -} - -get_pipeline_ubo_slot[prosperon.DOC] = `Return the index of a uniform buffer block within the pipeline's vertex reflection data by name suffix. - -:param pipeline: The pipeline whose vertex reflection is inspected. -:param name: A string suffix to match against the uniform buffer block name. -:return: The integer index of the matching UBO, or undefined if not found. -` - -function transpose4x4(val) { - var out = []; - out[0] = val[0]; out[1] = val[4]; out[2] = val[8]; out[3] = val[12]; - out[4] = val[1]; out[5] = val[5]; out[6] = val[9]; out[7] = val[13]; - out[8] = val[2]; out[9] = val[6]; out[10] = val[10];out[11] = val[14]; - out[12] = val[3];out[13] = val[7];out[14] = val[11];out[15] = val[15]; - return out; -} - -transpose4x4[prosperon.DOC] = `Return a new 4x4 matrix array that is the transpose of the passed matrix. - -:param val: An array of length 16 representing a 4x4 matrix in row-major format. -:return: A new array of length 16 representing the transposed matrix. -` - -function ubo_obj_to_array(pipeline, name, obj) { - var ubo; - for (var i = 0; i < pipeline.vertex.reflection.ubos.length; i++) { - ubo = pipeline.vertex.reflection.ubos[i]; - if (ubo.name.endsWith(name)) break; - } - var type = pipeline.vertex.reflection.types[ubo.type]; - var len = 0; - for (var mem of type.members) - len += type_to_byte_count(mem.type); - - var buf = new ArrayBuffer(len); - var view = new DataView(buf); - - for (var mem of type.members) { - var val = obj[mem.name]; - if (!val) throw new Error (`Could not find ${mem.name} on supplied object`); - - if (mem.name === 'model') - val = transpose4x4(val.array()); - - for (var i = 0; i < val.length; i++) - view.setFloat32(mem.offset + i*4, val[i],true); - } - return buf; -} - -ubo_obj_to_array[prosperon.DOC] = `Construct an ArrayBuffer containing UBO data from the provided object, matching the pipeline's reflection info. - -:param pipeline: The pipeline whose vertex reflection is read for UBO structure. -:param name: The name suffix that identifies the target UBO in the reflection data. -:param obj: An object whose properties match the UBO members. -:return: An ArrayBuffer containing packed UBO data. -` - -function type_to_byte_count(type) { - switch (type) { - case 'float': return 4; - case 'vec2': return 8; - case 'vec3': return 12; - case 'vec4': return 16; - case 'mat4': return 64; - default: throw new Error("Unknown or unsupported float-based type: " + type); - } -} - -type_to_byte_count[prosperon.DOC] = `Return the byte size for known float-based types. - -:param type: A string type identifier (e.g., 'float', 'vec2', 'vec3', 'vec4', 'mat4'). -:return: Integer number of bytes. -` - var sprite_model_ubo = { model: unit_transform, color: [1,1,1,1] }; -var shader_cache = {}; -var shader_times = {}; - -function make_pipeline(pipeline) { - if (pipeline.hasOwnProperty("gpu")) return; // this pipeline has already been made - - if (typeof pipeline.vertex === 'string') - pipeline.vertex = make_shader(pipeline.vertex); - if (typeof pipeline.fragment === 'string') - pipeline.fragment = make_shader(pipeline.fragment) - - // 1) Reflection data for vertex shader - var refl = pipeline.vertex.reflection - if (!refl || !refl.inputs || !Array.isArray(refl.inputs)) { - pipeline.gpu = render._main.make_pipeline(pipeline); - return; - } - - var inputs = refl.inputs - var buffer_descriptions = [] - var attributes = [] - - // 2) Build buffer + attribute for each reflection input - for (var i = 0; i < inputs.length; i++) { - var inp = inputs[i] - var typeStr = inp.type - var nameStr = (inp.name || "").toUpperCase() - var pitch = 4 - var fmt = "float1" - - if (typeStr == "vec2") { - pitch = 8 - fmt = "float2" - } else if (typeStr == "vec3") { - pitch = 12 - fmt = "float3" - } else if (typeStr == "vec4") { - if (nameStr.indexOf("COLOR") >= 0) { - pitch = 16 - fmt = "color" - } else { - pitch = 16 - fmt = "float4" - } - } - - buffer_descriptions.push({ - slot: i, - pitch: pitch, - input_rate: "vertex", - instance_step_rate: 0, - name:inp.name.split(".").pop() - }) - - attributes.push({ - location: inp.location, - buffer_slot: i, - format: fmt, - offset: 0 - }) - } - - pipeline.vertex_buffer_descriptions = buffer_descriptions - pipeline.vertex_attributes = attributes - - pipeline.gpu = render._main.make_pipeline(pipeline); -} - -make_pipeline[prosperon.DOC] = `Create and store a GPU pipeline object if it has not already been created. - -:param pipeline: An object describing the pipeline state, shaders, and reflection data. -:return: None -` - -var shader_type; - -function make_shader(sh_file) { - var file = `shaders/${shader_type}/${sh_file}.${shader_type}` - if (shader_cache[file]) return shader_cache[file] - var refl = json.decode(io.slurp(`shaders/reflection/${sh_file}.json`)) - - var shader = { - code: io.slurpbytes(file), - format: shader_type, - stage: sh_file.endsWith("vert") ? "vertex" : "fragment", - num_samplers: refl.separate_samplers ? refl.separate_samplers.length : 0, - num_textures: 0, - num_storage_buffers: refl.separate_storage_buffers ? refl.separate_storage_buffers.length : 0, - num_uniform_buffers: refl.ubos ? refl.ubos.length : 0, - entrypoint: shader_type === "msl" ? "main0" : "main" - } - - shader.gpu = render._main.make_shader(shader) - shader.reflection = refl; - shader_cache[file] = shader - shader.file = sh_file - return shader -} - -make_shader[prosperon.DOC] = `Load and compile a shader from disk, caching the result. Reflective metadata is also loaded. - -:param sh_file: The base filename (without extension) of the shader to compile. -:return: A shader object with GPU and reflection data attached. -` - // helpful render devices. width and height in pixels; diagonal in inches. render.device = { pc: { width: 1920, height: 1080 }, @@ -433,64 +196,6 @@ var std_sampler = { compare: false }; -function upload_model(model) { - var bufs = []; - for (var i in model) { - if (typeof model[i] !== 'object') continue; - bufs.push(model[i]); - } - render._main.upload(this, bufs); -} - -upload_model[prosperon.DOC] = `Upload all buffer-like properties of the given model to the GPU. - -:param model: An object whose buffer properties are to be uploaded. -:return: None -` - -function bind_model(pass, pipeline, model) { - var buffers = pipeline.vertex_buffer_descriptions; - var bufs = []; - if (buffers) - for (var b of buffers) { - if (b.name in model) bufs.push(model[b.name]) - else throw Error (`could not find buffer ${b.name} on model`); - } - pass.bind_buffers(0,bufs); - pass.bind_index_buffer(model.indices); -} - -bind_model[prosperon.DOC] = `Bind the model's vertex and index buffers for the given pipeline and render pass. - -:param pass: The current render pass. -:param pipeline: The pipeline object with vertex buffer descriptions. -:param model: The model object containing matching buffers and an index buffer. -:return: None -` - -function bind_mat(pass, pipeline, mat) { - var imgs = []; - var refl = pipeline.fragment.reflection; - if (refl.separate_images) { - for (var i of refl.separate_images) { - if (i.name in mat) { - var tex = mat[i.name]; - imgs.push({texture:tex.texture, sampler:tex.sampler}); - } else - throw Error (`could not find all necessary images: ${i.name}`) - } - pass.bind_samplers(false, 0,imgs); - } -} - -bind_mat[prosperon.DOC] = `Bind the material images and samplers needed by the pipeline's fragment shader. - -:param pass: The current render pass. -:param pipeline: The pipeline whose fragment shader reflection indicates required textures. -:param mat: An object mapping the required image names to {texture, sampler}. -:return: None -` - function group_sprites_by_texture(sprites, mesh) { if (sprites.length === 0) return; for (var i = 0; i < sprites.length; i++) { diff --git a/source/jsffi.c b/source/jsffi.c index 0265a354..1cdcc2e7 100644 --- a/source/jsffi.c +++ b/source/jsffi.c @@ -2961,7 +2961,7 @@ JSC_CCALL(renderer_geometry, if (!SDL_RenderGeometryRaw(r, tex, trans_pos, pos_stride,colordata,color_stride,uvdata, uv_stride, vertices, idxdata, count, indices_stride)) ret = JS_ThrowReferenceError(js, "Error rendering geometry: %s",SDL_GetError()); - free(trans_pos); + free(trans_pos); JS_FreeValue(js,pos); JS_FreeValue(js,color); diff --git a/source/prosperon.c b/source/prosperon.c index f55b3581..598d76f8 100644 --- a/source/prosperon.c +++ b/source/prosperon.c @@ -1364,7 +1364,7 @@ int main(int argc, char **argv) io_actor = create_actor(2, io_argv); /* Create the initial actor from the main command line. */ - char **margv = malloc(sizeof(char *) * argc + 2); + char **margv = malloc(sizeof(char *) * (argc + 2)); for (int i = 0; i < argc; i++) margv[i] = strdup(argv[i]); margv[argc] = "--main"; margv[argc+1] = "1"; @@ -1390,8 +1390,8 @@ int main(int argc, char **argv) if (event.type == queue_event) goto QUEUE; -// WotaBuffer wb = event2wota(&event); -// send_message(io_actor->id, wb.data); + WotaBuffer wb = event2wota(&event); + send_message(io_actor->id, wb.data); continue; QUEUE: diff --git a/tests/window.js b/tests/window.js index 26431214..e5a2631d 100644 --- a/tests/window.js +++ b/tests/window.js @@ -21,7 +21,7 @@ prosperon.win = prosperon.engine_start({ url: "https://prosperon.dev" }) -var ren = prosperon.win.make_renderer("metal") +var ren = prosperon.win.make_renderer("vulkan") function loop() { ren.draw_color([1,1,1,1]) @@ -50,5 +50,7 @@ $_.send(ioguy, { $_.receiver(e => { if (e.type === 'quit') - os.quit() + os.exit() + else + console.log(json.encode(e)) })