diff --git a/base/sources/backends/data/wasm/start.js b/base/sources/backends/data/wasm/start.js index c4a5d5e1..5324c9ec 100644 --- a/base/sources/backends/data/wasm/start.js +++ b/base/sources/backends/data/wasm/start.js @@ -79,7 +79,7 @@ async function init() { let wasm_bytes = null; await fetch("./start.wasm").then(res => res.arrayBuffer()).then(buffer => wasm_bytes = new Uint8Array(buffer)); - memory = new WebAssembly.Memory({initial : 8295, maximum : 8295, shared : true}); + memory = new WebAssembly.Memory({initial : 20000, maximum : 20000, shared : true}); // * 65536 heapu8 = new Uint8Array(memory.buffer); heapu16 = new Uint16Array(memory.buffer); heapu32 = new Uint32Array(memory.buffer); @@ -105,8 +105,8 @@ async function init() { imports : { wgpuCreateInstance : function(pdescriptor) { - let inst = navigator.gpu; - return ptr_to_id(inst); + let inst = navigator.gpu; + return ptr_to_id(inst); }, // wgpuInstanceRequestAdapter : function(pinstance, poptions, pcallback_info) { // let callback_index = heapu32[(pcallback_info + 12) >> 2]; @@ -114,480 +114,477 @@ async function init() { // return 0n; // }, wgpuInstanceRequestAdapterSync : function() { - return ptr_to_id(adapter); + return ptr_to_id(adapter); }, wgpuAdapterGetInfo : function(padapter, pinfo) { - return 0; + return 0; }, wgpuAdapterInfoFreeMembers : function(padapter_info) {}, // wgpuAdapterRequestDevice : function(padapter, pdescriptor, pcallback_info) { // return 0; // }, wgpuAdapterRequestDeviceSync : function() { - return ptr_to_id(device); + return ptr_to_id(device); }, wgpuDeviceCreateTexture : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUTextureDescriptor - let desc = { - usage : read_u32(pdescriptor + 16), - dimension : "2d", - size : {width : read_u32(pdescriptor + 28), height : read_u32(pdescriptor + 32), depthOrArrayLayers : 1}, - format : id_to_texture_format(read_u32(pdescriptor + 40)), - mipLevelCount : 1, - sampleCount : 1 - }; - let texture = device.createTexture(desc); - return ptr_to_id(texture); + let device = id_to_ptr(pdevice); + // WGPUTextureDescriptor + let desc = { + usage : read_u32(pdescriptor + 16), + dimension : "2d", + size : {width : read_u32(pdescriptor + 28), height : read_u32(pdescriptor + 32), depthOrArrayLayers : 1}, + format : id_to_texture_format(read_u32(pdescriptor + 40)), + mipLevelCount : 1, + sampleCount : 1 + }; + let texture = device.createTexture(desc); + return ptr_to_id(texture); }, wgpuTextureCreateView : function(ptexture, pdescriptor) { - let texture = id_to_ptr(ptexture); - // WGPUTextureViewDescriptor - let desc = - {format : id_to_texture_format(read_u32(pdescriptor + 12)), dimension : "2d", mipLevelCount : 1, arrayLayerCount : 1, aspect : "all"}; - let view = texture.createView(desc); - return ptr_to_id(view); + let texture = id_to_ptr(ptexture); + // WGPUTextureViewDescriptor + let desc = + {format : id_to_texture_format(read_u32(pdescriptor + 12)), dimension : "2d", mipLevelCount : 1, arrayLayerCount : 1, aspect : "all"}; + let view = texture.createView(desc); + return ptr_to_id(view); }, wgpuDeviceGetQueue : function(pdevice) { - let device = id_to_ptr(pdevice); - return ptr_to_id(device.queue); + let device = id_to_ptr(pdevice); + return ptr_to_id(device.queue); }, wgpuDeviceCreateBindGroupLayout : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUBindGroupLayoutDescriptor - let desc = {entryCount : read_u32(pdescriptor + 12), entries : []}; - let pentries = read_u32(pdescriptor + 16); + let device = id_to_ptr(pdevice); + // WGPUBindGroupLayoutDescriptor + let desc = {entryCount : read_u32(pdescriptor + 12), entries : []}; + let pentries = read_u32(pdescriptor + 16); - // WGPUBindGroupLayoutEntry - for (let i = 0; i < desc.entryCount; ++i) { - let e = { - binding : read_u32(i * 88 + pentries + 4), - visibility : read_u32(i * 88 + pentries + 8), - }; + // WGPUBindGroupLayoutEntry + for (let i = 0; i < desc.entryCount; ++i) { + let e = { + binding : read_u32(i * 88 + pentries + 4), + visibility : read_u32(i * 88 + pentries + 8), + }; - if (read_u32(i * 88 + pentries + 28) != 0x00000000) { // WGPUBufferBindingType_BindingNotUsed - e.buffer = { - type : read_u32(i * 88 + pentries + 28), - hasDynamicOffset : read_u32(i * 88 + pentries + 32), - minBindingSize : read_u32(i * 88 + pentries + 36) - }; - if (e.buffer.type === 0x00000002) - e.buffer.type = "uniform"; - } + if (read_u32(i * 88 + pentries + 28) != 0x00000000) { // WGPUBufferBindingType_BindingNotUsed + e.buffer = { + type : read_u32(i * 88 + pentries + 28), + hasDynamicOffset : read_u32(i * 88 + pentries + 32), + minBindingSize : read_u32(i * 88 + pentries + 36) + }; + if (e.buffer.type === 0x00000002) + e.buffer.type = "uniform"; + } - if (read_u32(i * 88 + pentries + 52) != 0x00000000) { // WGPUSamplerBindingType_BindingNotUsed - e.sampler = {type : read_u32(i * 88 + pentries + 52)}; - if (e.sampler.type === 0x00000002) - e.sampler.type = "filtering"; - } + if (read_u32(i * 88 + pentries + 52) != 0x00000000) { // WGPUSamplerBindingType_BindingNotUsed + e.sampler = {type : read_u32(i * 88 + pentries + 52)}; + if (e.sampler.type === 0x00000002) + e.sampler.type = "filtering"; + } - if (read_u32(i * 88 + pentries + 60) != 0x00000000) { // WGPUTextureSampleType_BindingNotUsed - e.texture = { - sampleType : read_u32(i * 88 + pentries + 60), - viewDimension : "2d", // read_u32(i * 88 + pentries + 64), - multisampled : read_u32(i * 88 + pentries + 68) - }; - if (e.texture.sampleType === 0x00000002) - e.texture.sampleType = "float"; - } + if (read_u32(i * 88 + pentries + 60) != 0x00000000) { // WGPUTextureSampleType_BindingNotUsed + e.texture = { + sampleType : read_u32(i * 88 + pentries + 60), + viewDimension : "2d", // read_u32(i * 88 + pentries + 64), + multisampled : read_u32(i * 88 + pentries + 68) + }; + if (e.texture.sampleType === 0x00000002) + e.texture.sampleType = "float"; + } - desc.entries.push(e); - } - let bgl = device.createBindGroupLayout(desc); - return ptr_to_id(bgl); + desc.entries.push(e); + } + let bgl = device.createBindGroupLayout(desc); + return ptr_to_id(bgl); }, wgpuDeviceCreateBuffer : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUBufferDescriptor - let desc = {usage : read_u32(pdescriptor + 16), size : read_u32(pdescriptor + 24), mappedAtCreation : read_u32(pdescriptor + 32)}; - let buffer = device.createBuffer(desc); - return ptr_to_id(buffer); + let device = id_to_ptr(pdevice); + // WGPUBufferDescriptor + let desc = {usage : read_u32(pdescriptor + 16), size : read_u32(pdescriptor + 24), mappedAtCreation : read_u32(pdescriptor + 32)}; + let buffer = device.createBuffer(desc); + return ptr_to_id(buffer); }, wgpuBufferGetMappedRange : function(pbuffer, offset, size) { - let buffer = id_to_ptr(pbuffer); - let ptr = instance.exports.malloc(size); - let ab = buffer.getMappedRange(offset, size); - let u8 = new Uint8Array(ab); - for (let i = 0; i < u8.length; ++i) { - heapu8[ptr + i] = u8[i]; - } - return ptr; + let buffer = id_to_ptr(pbuffer); + let ptr = instance.exports.malloc(size); + let ab = buffer.getMappedRange(offset, size); + let u8 = new Uint8Array(ab); + for (let i = 0; i < u8.length; ++i) { + heapu8[ptr + i] = u8[i]; + } + return ptr; }, wgpuBufferUnmap : function(pbuffer) { - let buffer = id_to_ptr(pbuffer); - buffer.unmap(); + let buffer = id_to_ptr(pbuffer); + buffer.unmap(); }, wgpuBufferUnmap2 : function(pbuffer, pdata, start, count) { - let buffer = id_to_ptr(pbuffer); - let ab = buffer.getMappedRange(start, count); - let u8 = new Uint8Array(ab); - for (let i = 0; i < u8.length; ++i) { - u8[i] = heapu8[pdata + i]; - } - buffer.unmap(); + let buffer = id_to_ptr(pbuffer); + let ab = buffer.getMappedRange(start, count); + let u8 = new Uint8Array(ab); + for (let i = 0; i < u8.length; ++i) { + u8[i] = heapu8[pdata + i]; + } + buffer.unmap(); }, wgpuDeviceCreateCommandEncoder : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUCommandBufferDescriptor - let desc = null; - let encoder = device.createCommandEncoder(desc); - return ptr_to_id(encoder); + let device = id_to_ptr(pdevice); + // WGPUCommandBufferDescriptor + let desc = null; + let encoder = device.createCommandEncoder(desc); + return ptr_to_id(encoder); }, wgpuCommandEncoderCopyBufferToTexture : function(pcommand_encoder, psource, pdestination, pcopysize) { - let encoder = id_to_ptr(pcommand_encoder); - // WGPUTexelCopyBufferInfo - let source = { - layout : {bytesPerRow : read_u32(psource + 8), rowsPerImage : read_u32(psource + 12)}, - buffer : id_to_ptr(read_u32(psource + 16)) - }; - // WGPUTexelCopyTextureInfo - let destination = {texture : id_to_ptr(read_u32(pdestination))}; - // WGPUExtent3D - let copysize = {width : read_u32(pcopysize), height : read_u32(pcopysize + 4), depthOrArrayLayers : read_u32(pcopysize + 8)}; - encoder.copyBufferToTexture(source, destination, copysize); + let encoder = id_to_ptr(pcommand_encoder); + // WGPUTexelCopyBufferInfo + let source = {bytesPerRow : read_u32(psource + 8), rowsPerImage : read_u32(psource + 12), buffer : id_to_ptr(read_u32(psource + 16))}; + // WGPUTexelCopyTextureInfo + let destination = {texture : id_to_ptr(read_u32(pdestination))}; + // WGPUExtent3D + let copysize = {width : read_u32(pcopysize), height : read_u32(pcopysize + 4), depthOrArrayLayers : read_u32(pcopysize + 8)}; + encoder.copyBufferToTexture(source, destination, copysize); }, wgpuCommandEncoderFinish : function(pcommand_encoder, pdescriptor) { - let encoder = id_to_ptr(pcommand_encoder); - // WGPUCommandBufferDescriptor - let desc = null; - let command_buffer = encoder.finish(desc); - return ptr_to_id(command_buffer); + let encoder = id_to_ptr(pcommand_encoder); + // WGPUCommandBufferDescriptor + let desc = null; + let command_buffer = encoder.finish(desc); + return ptr_to_id(command_buffer); }, wgpuQueueSubmit : function(pqueue, command_buffer_count, pcommand_buffers) { - let queue = id_to_ptr(pqueue); - let command_buffers = []; - for (let i = 0; i < command_buffer_count; i++) { - let c = id_to_ptr(read_u32(pcommand_buffers + i * 4)); - command_buffers.push(c); - } - queue.submit(command_buffers); + let queue = id_to_ptr(pqueue); + let command_buffers = []; + for (let i = 0; i < command_buffer_count; i++) { + let c = id_to_ptr(read_u32(pcommand_buffers + i * 4)); + command_buffers.push(c); + } + queue.submit(command_buffers); }, wgpuBufferRelease : function(pbuffer) {}, wgpuDeviceCreateSampler : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUSamplerDescriptor - let desc = { - addressModeU : "repeat", - addressModeV : "repeat", - addressModeW : "repeat", - magFilter : "linear", - minFilter : "linear", - mipmapFilter : "linear", - maxAnisotropy : 1 - }; - let sampler = device.createSampler(desc); - return ptr_to_id(sampler); + let device = id_to_ptr(pdevice); + // WGPUSamplerDescriptor + let desc = { + addressModeU : "repeat", + addressModeV : "repeat", + addressModeW : "repeat", + magFilter : "linear", + minFilter : "linear", + mipmapFilter : "linear", + maxAnisotropy : 1 + }; + let sampler = device.createSampler(desc); + return ptr_to_id(sampler); }, wgpuSurfaceGetCapabilities : function(psurface, padapter, pcapabilities) { - return 0; + return 0; }, wgpuSurfaceCapabilitiesFreeMembers : function(pcapabilities) {}, wgpuBufferDestroy : function(pbuffer) { - let buffer = id_to_ptr(pbuffer); - buffer.destroy(); + let buffer = id_to_ptr(pbuffer); + buffer.destroy(); }, wgpuSurfaceGetCurrentTexture : function(psurface, psurface_texture) { - let surface = id_to_ptr(psurface) || context; - let texture = surface.getCurrentTexture(); - // WGPUSurfaceTexture - heapu32[psurface_texture / 4 + 1] = ptr_to_id(texture); + let surface = id_to_ptr(psurface) || context; + let texture = surface.getCurrentTexture(); + // WGPUSurfaceTexture + heapu32[psurface_texture / 4 + 1] = ptr_to_id(texture); }, wgpuCommandEncoderBeginRenderPass : function(pcommand_encoder, pdescriptor) { - let encoder = id_to_ptr(pcommand_encoder); + let encoder = id_to_ptr(pcommand_encoder); - // WGPURenderPassDescriptor - let desc = {colorAttachmentCount : read_u32(pdescriptor + 12), colorAttachments : []}; - let pcas = read_u32(pdescriptor + 16); - for (let i = 0; i < desc.colorAttachmentCount; ++i) { - // WGPURenderPassColorAttachment - let ca = {view : id_to_ptr(read_u32(pcas + 4 + i * 28)), loadOp : "clear", storeOp : "store", clearValue : [ 0.0, 0.0, 0.0, 0.0 ]}; - desc.colorAttachments.push(ca); - } + // WGPURenderPassDescriptor + let desc = {colorAttachmentCount : read_u32(pdescriptor + 12), colorAttachments : []}; + let pcas = read_u32(pdescriptor + 16); + for (let i = 0; i < desc.colorAttachmentCount; ++i) { + // WGPURenderPassColorAttachment + let ca = {view : id_to_ptr(read_u32(pcas + 4 + i * 28)), loadOp : "clear", storeOp : "store", clearValue : [ 0.0, 0.0, 0.0, 0.0 ]}; + desc.colorAttachments.push(ca); + } - // WGPURenderPassDepthStencilAttachment - let pdsa = read_u32(pdescriptor + 20); - if (pdsa !== 0) { - desc.depthStencilAttachment = {view : id_to_ptr(read_u32(pdsa + 4)), depthLoadOp : "clear", depthStoreOp : "store", depthClearValue : 1.0}; - } + // WGPURenderPassDepthStencilAttachment + let pdsa = read_u32(pdescriptor + 20); + if (pdsa !== 0) { + desc.depthStencilAttachment = {view : id_to_ptr(read_u32(pdsa + 4)), depthLoadOp : "clear", depthStoreOp : "store", depthClearValue : 1.0}; + } - let render_pass = encoder.beginRenderPass(desc); - return ptr_to_id(render_pass); + let render_pass = encoder.beginRenderPass(desc); + return ptr_to_id(render_pass); }, wgpuRenderPassEncoderSetViewport : function(prender_pass_encoder, x, y, width, height, min_depth, max_depth) { - let render_pass = id_to_ptr(prender_pass_encoder); - render_pass.setViewport(x, y, width, height, min_depth, max_depth); + let render_pass = id_to_ptr(prender_pass_encoder); + render_pass.setViewport(x, y, width, height, min_depth, max_depth); }, wgpuRenderPassEncoderSetScissorRect : function(prender_pass_encoder, x, y, width, height) { - let render_pass = id_to_ptr(prender_pass_encoder); - render_pass.setScissorRect(x, y, width, height); + let render_pass = id_to_ptr(prender_pass_encoder); + render_pass.setScissorRect(x, y, width, height); }, wgpuRenderPassEncoderEnd : function(prender_pass_encoder) { - let render_pass = id_to_ptr(prender_pass_encoder); - render_pass.end(); + let render_pass = id_to_ptr(prender_pass_encoder); + render_pass.end(); }, wgpuRenderPassEncoderRelease : function(prender_pass_encoder) {}, wgpuCommandBufferRelease : function(commandBufferPtr) {}, wgpuCommandEncoderRelease : function(pcommand_encoder) {}, wgpuSurfacePresent : function(psurface) { - return 0; + return 0; }, wgpuRenderPassEncoderDrawIndexed : function(prender_pass_encoder, index_count, instance_count, first_index, base_vertex, first_instance) { - let render_pass = id_to_ptr(prender_pass_encoder); - render_pass.drawIndexed(index_count, instance_count, first_index, base_vertex, first_instance); + let render_pass = id_to_ptr(prender_pass_encoder); + render_pass.drawIndexed(index_count, instance_count, first_index, base_vertex, first_instance); }, wgpuRenderPassEncoderSetPipeline : function(prender_pass_encoder, ppipeline) { - let render_pass = id_to_ptr(prender_pass_encoder); - let pipeline = id_to_ptr(ppipeline); - render_pass.setPipeline(pipeline); + let render_pass = id_to_ptr(prender_pass_encoder); + let pipeline = id_to_ptr(ppipeline); + render_pass.setPipeline(pipeline); }, wgpuRenderPassEncoderSetVertexBuffer : function(prender_pass_encoder, slot, pbuffer, offset, size) { - let render_pass = id_to_ptr(prender_pass_encoder); - let buffer = id_to_ptr(pbuffer); - render_pass.setVertexBuffer(slot, buffer, Number(offset), Number(size)); + let render_pass = id_to_ptr(prender_pass_encoder); + let buffer = id_to_ptr(pbuffer); + render_pass.setVertexBuffer(slot, buffer, Number(offset), Number(size)); }, wgpuRenderPassEncoderSetIndexBuffer : function(prender_pass_encoder, pbuffer, format, offset, size) { - let render_pass = id_to_ptr(prender_pass_encoder); - let buffer = id_to_ptr(pbuffer); - render_pass.setIndexBuffer(buffer, 'uint32', Number(offset), Number(size)); + let render_pass = id_to_ptr(prender_pass_encoder); + let buffer = id_to_ptr(pbuffer); + render_pass.setIndexBuffer(buffer, 'uint32', Number(offset), Number(size)); }, wgpuDeviceCreateBindGroup : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUBindGroupDescriptor - let desc = {layout : id_to_ptr(read_u32(pdescriptor + 12)), entryCount : read_u32(pdescriptor + 16), entries : []}; - let pentries = read_u32(pdescriptor + 20); - for (let i = 0; i < desc.entryCount; i++) { - // WGPUBindGroupEntry - let e = { - binding : read_u32(pentries + 4 + i * 40), - }; + let device = id_to_ptr(pdevice); + // WGPUBindGroupDescriptor + let desc = {layout : id_to_ptr(read_u32(pdescriptor + 12)), entryCount : read_u32(pdescriptor + 16), entries : []}; + let pentries = read_u32(pdescriptor + 20); + for (let i = 0; i < desc.entryCount; i++) { + // WGPUBindGroupEntry + let e = { + binding : read_u32(pentries + 4 + i * 40), + }; - if (read_u32(pentries + 8 + i * 40) !== 0) { - e.resource = { - buffer : id_to_ptr(read_u32(pentries + 8 + i * 40)), - offset : read_u32(pentries + 16 + i * 40), - size : read_u32(pentries + 24 + i * 40) - } - } - if (read_u32(pentries + 32 + i * 40) !== 0) { - e.resource = id_to_ptr(read_u32(pentries + 32 + i * 40)); - } - if (read_u32(pentries + 36 + i * 40) !== 0) { - e.resource = id_to_ptr(read_u32(pentries + 36 + i * 40)); - } + if (read_u32(pentries + 8 + i * 40) !== 0) { + e.resource = { + buffer : id_to_ptr(read_u32(pentries + 8 + i * 40)), + offset : read_u32(pentries + 16 + i * 40), + size : read_u32(pentries + 24 + i * 40) + } + } + if (read_u32(pentries + 32 + i * 40) !== 0) { + e.resource = id_to_ptr(read_u32(pentries + 32 + i * 40)); + } + if (read_u32(pentries + 36 + i * 40) !== 0) { + e.resource = id_to_ptr(read_u32(pentries + 36 + i * 40)); + } - desc.entries.push(e); - } - let bg = device.createBindGroup(desc); - return ptr_to_id(bg); + desc.entries.push(e); + } + let bg = device.createBindGroup(desc); + return ptr_to_id(bg); }, wgpuRenderPassEncoderSetBindGroup : function(prender_pass_encoder, group_index, pgroup, dynamic_offset_count, pdynamic_offsets) { - let render_pass = id_to_ptr(prender_pass_encoder); - let group = id_to_ptr(pgroup); - let dynamic_offsets = []; - for (let i = 0; i < dynamic_offset_count; i++) { - let doff = read_u32(pdynamic_offsets + i * 4); - dynamic_offsets.push(doff); - } - render_pass.setBindGroup(group_index, group, dynamic_offsets); + let render_pass = id_to_ptr(prender_pass_encoder); + let group = id_to_ptr(pgroup); + let dynamic_offsets = []; + for (let i = 0; i < dynamic_offset_count; i++) { + let doff = read_u32(pdynamic_offsets + i * 4); + dynamic_offsets.push(doff); + } + render_pass.setBindGroup(group_index, group, dynamic_offsets); }, wgpuBindGroupRelease : function(pbind_group) {}, wgpuRenderPipelineRelease : function(prender_pipeline) {}, wgpuPipelineLayoutRelease : function(ppipeline_layout) {}, wgpuDeviceCreatePipelineLayout : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUPipelineLayoutDescriptor - let desc = {bindGroupLayoutCount : read_u32(pdescriptor + 12), bindGroupLayouts : []}; - let pbgl = read_u32(pdescriptor + 16); - for (let i = 0; i < desc.bindGroupLayoutCount; ++i) { - // WGPUBindGroupLayout - let bgl = id_to_ptr(read_u32(pbgl + i * 4)); - desc.bindGroupLayouts.push(bgl); - } - let pl = device.createPipelineLayout(desc); - return ptr_to_id(pl); + let device = id_to_ptr(pdevice); + // WGPUPipelineLayoutDescriptor + let desc = {bindGroupLayoutCount : read_u32(pdescriptor + 12), bindGroupLayouts : []}; + let pbgl = read_u32(pdescriptor + 16); + for (let i = 0; i < desc.bindGroupLayoutCount; ++i) { + // WGPUBindGroupLayout + let bgl = id_to_ptr(read_u32(pbgl + i * 4)); + desc.bindGroupLayouts.push(bgl); + } + let pl = device.createPipelineLayout(desc); + return ptr_to_id(pl); }, wgpuDeviceCreateShaderModule : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); - // WGPUShaderSourceWGSL - let pwgsl = read_u32(pdescriptor); - let wgsl = {chain : {sType : read_u32(pwgsl + 4)}, code : {data : read_u32(pwgsl + 8), length : read_u32(pwgsl + 12)}}; - // WGPUShaderModuleDescriptor - let desc = {code : read_string_n(wgsl.code.data, wgsl.code.length)}; - let sm = device.createShaderModule(desc); - return ptr_to_id(sm); + let device = id_to_ptr(pdevice); + // WGPUShaderSourceWGSL + let pwgsl = read_u32(pdescriptor); + let wgsl = {chain : {sType : read_u32(pwgsl + 4)}, code : {data : read_u32(pwgsl + 8), length : read_u32(pwgsl + 12)}}; + // WGPUShaderModuleDescriptor + let desc = {code : read_string_n(wgsl.code.data, wgsl.code.length)}; + let sm = device.createShaderModule(desc); + return ptr_to_id(sm); }, wgpuDeviceCreateRenderPipeline : function(pdevice, pdescriptor) { - let device = id_to_ptr(pdevice); + let device = id_to_ptr(pdevice); - // WGPUFragmentState - let pfrag = read_u32(pdescriptor + 92); - let frag = {module : id_to_ptr(read_u32(pfrag + 4)), entryPoint : "main", targetCount : 1, targets : []}; - let ptragets = read_u32(pfrag + 28); - for (let i = 0; i < frag.targetCount; ++i) { - // WGPUColorTargetState - let t = { - format : id_to_texture_format(read_u32(ptragets + 4 + i * 24)), - blend : { - color : {operation : "add", srcFactor : "one", dstFactor : "zero"}, - alpha : {operation : "add", srcFactor : "one", dstFactor : "zero"} - }, - writeMask : read_u32(ptragets + 16 + i * 24) - }; - frag.targets.push(t); - } + // WGPUFragmentState + let pfrag = read_u32(pdescriptor + 92); + let frag = {module : id_to_ptr(read_u32(pfrag + 4)), entryPoint : "main", targetCount : 1, targets : []}; + let ptragets = read_u32(pfrag + 28); + for (let i = 0; i < frag.targetCount; ++i) { + // WGPUColorTargetState + let t = { + format : id_to_texture_format(read_u32(ptragets + 4 + i * 24)), + blend : { + color : {operation : "add", srcFactor : "one", dstFactor : "zero"}, + alpha : {operation : "add", srcFactor : "one", dstFactor : "zero"} + }, + writeMask : read_u32(ptragets + 16 + i * 24) + }; + frag.targets.push(t); + } - // WGPUDepthStencilState - let pds = read_u32(pdescriptor + 72); - let ds = {format : id_to_texture_format(read_u32(pds + 4)), depthWriteEnabled : read_u32(pds + 8), depthCompare : read_u32(pds + 12)}; - if (ds.depthCompare === 0x00000002) - ds.depthCompare = "less"; - if (ds.depthCompare === 0x00000008) - ds.depthCompare = "always"; + // WGPUDepthStencilState + let pds = read_u32(pdescriptor + 72); + let ds = {format : id_to_texture_format(read_u32(pds + 4)), depthWriteEnabled : read_u32(pds + 8), depthCompare : read_u32(pds + 12)}; + if (ds.depthCompare === 0x00000002) + ds.depthCompare = "less"; + if (ds.depthCompare === 0x00000008) + ds.depthCompare = "always"; - // WGPURenderPipelineDescriptor - let desc = { - layout : id_to_ptr(read_u32(pdescriptor + 12)), - // WGPUVertexState - vertex : {module : id_to_ptr(read_u32(pdescriptor + 20)), entryPoint : "main", bufferCount : 1, buffers : []}, - primitive : {topology : "triangle-list", frontFace : "ccw", cullMode : "none"}, - depthStencil : ds, - fragment : frag, - }; + // WGPURenderPipelineDescriptor + let desc = { + layout : id_to_ptr(read_u32(pdescriptor + 12)), + // WGPUVertexState + vertex : {module : id_to_ptr(read_u32(pdescriptor + 20)), entryPoint : "main", bufferCount : 1, buffers : []}, + primitive : {topology : "triangle-list", frontFace : "ccw", cullMode : "none"}, + depthStencil : ds, + fragment : frag, + }; - let pbuffers = read_u32(pdescriptor + 44); - for (let i = 0; i < desc.vertex.bufferCount; ++i) { - // WGPUVertexBufferLayout - let b = {arrayStride : read_u32(pbuffers + 8 + i * 24), attributeCount : read_u32(pbuffers + 16 + i * 24), attributes : []}; - let pattributes = read_u32(pbuffers + 20 + i * 24); - for (let i = 0; i < b.attributeCount; ++i) { - // WGPUVertexAttribute - let a = { - format : id_to_vertex_format(read_u32(pattributes + 4 + i * 20)), - offset : read_u32(pattributes + 8 + i * 20), - shaderLocation : read_u32(pattributes + 16 + i * 20) - }; - b.attributes.push(a); - } - desc.vertex.buffers.push(b); - } + let pbuffers = read_u32(pdescriptor + 44); + for (let i = 0; i < desc.vertex.bufferCount; ++i) { + // WGPUVertexBufferLayout + let b = {arrayStride : read_u32(pbuffers + 8 + i * 24), attributeCount : read_u32(pbuffers + 16 + i * 24), attributes : []}; + let pattributes = read_u32(pbuffers + 20 + i * 24); + for (let i = 0; i < b.attributeCount; ++i) { + // WGPUVertexAttribute + let a = { + format : id_to_vertex_format(read_u32(pattributes + 4 + i * 24)), + offset : read_u32(pattributes + 8 + i * 24), + shaderLocation : read_u32(pattributes + 16 + i * 24) + }; + b.attributes.push(a); + } + desc.vertex.buffers.push(b); + } - let rp = device.createRenderPipeline(desc); - return ptr_to_id(rp); + let rp = device.createRenderPipeline(desc); + return ptr_to_id(rp); }, wgpuShaderModuleRelease : function(pshader_module) {}, wgpuQueueWriteBuffer : function(pqueue, pbuffer, buffer_offset, pdata, size) { - let queue = id_to_ptr(pqueue); - let buffer = id_to_ptr(pbuffer); - let data = heapu8.subarray(pdata, pdata + size); - queue.writeBuffer(buffer, buffer_offset, data); + let queue = id_to_ptr(pqueue); + let buffer = id_to_ptr(pbuffer); + let data = heapu8.subarray(pdata, pdata + size); + queue.writeBuffer(buffer, Number(buffer_offset), data); }, wgpuTextureDestroy : function(ptexture) { - let texture = id_to_ptr(ptexture); - texture.destroy(); + let texture = id_to_ptr(ptexture); + texture.destroy(); }, wgpuTextureRelease : function(ptexture) {}, wgpuTextureViewRelease : function(ptexture_view) {}, wgpuCommandEncoderCopyBufferToBuffer : function(pcommand_encoder, psource, source_offset, pdestination, destination_offset, size) { - let encoder = id_to_ptr(pcommand_encoder); - let source = id_to_ptr(psource); - let destination = id_to_ptr(pdestination); - encoder.copyBufferToBuffer(source, Number(source_offset), destination, Number(destination_offset), Number(size)); + let encoder = id_to_ptr(pcommand_encoder); + let source = id_to_ptr(psource); + let destination = id_to_ptr(pdestination); + encoder.copyBufferToBuffer(source, Number(source_offset), destination, Number(destination_offset), Number(size)); }, wgpuSurfaceConfigure : function(psurface, pconfig) { - let surface = id_to_ptr(psurface) || context; - // WGPUSurfaceConfiguration - let config = { - device : id_to_ptr(read_u32(pconfig + 4)), - format : id_to_texture_format(read_u32(pconfig + 8)), - usage : read_u32(pconfig + 16), - width : read_u32(pconfig + 24), - height : read_u32(pconfig + 28), - viewFormatCount : read_u32(pconfig + 32), - viewFormats : [], - alphaMode : read_u32(pconfig + 40), - presentMode : read_u32(pconfig + 44) - }; - if (config.alphaMode === 0x00000001) - config.alphaMode = "opaque"; - if (config.presentMode === 0x00000001) - config.presentMode = "fifo"; - if (config.presentMode === 0x00000004) - config.presentMode = "mailbox"; - let pview_formats = read_u32(pconfig + 36); - for (let i = 0; i < config.viewFormatCount; ++i) { - let f = id_to_texture_format(read_u32(pview_formats + i * 4)); - config.viewFormats.push(f); - } - surface.configure(config); + let surface = id_to_ptr(psurface) || context; + // WGPUSurfaceConfiguration + let config = { + device : id_to_ptr(read_u32(pconfig + 4)), + format : id_to_texture_format(read_u32(pconfig + 8)), + usage : read_u32(pconfig + 16), + width : read_u32(pconfig + 24), + height : read_u32(pconfig + 28), + viewFormatCount : read_u32(pconfig + 32), + viewFormats : [], + alphaMode : read_u32(pconfig + 40), + presentMode : read_u32(pconfig + 44) + }; + if (config.alphaMode === 0x00000001) + config.alphaMode = "opaque"; + if (config.presentMode === 0x00000001) + config.presentMode = "fifo"; + if (config.presentMode === 0x00000004) + config.presentMode = "mailbox"; + let pview_formats = read_u32(pconfig + 36); + for (let i = 0; i < config.viewFormatCount; ++i) { + let f = id_to_texture_format(read_u32(pview_formats + i * 4)); + config.viewFormats.push(f); + } + surface.configure(config); }, - js_fprintf : function(format) { - console.log(read_string(format)); + js_printf : function(format) { + console.log(read_string(format)); }, js_fopen : function(filename) { - let req = new XMLHttpRequest(); - req.open("GET", read_string(filename), false); - req.overrideMimeType("text/plain; charset=x-user-defined"); - req.send(); - let str = req.response; - file_buffer_pos = 0; - file_buffer = new ArrayBuffer(str.length); - let buf_view = new Uint8Array(file_buffer); - for (let i = 0; i < str.length; ++i) { - buf_view[i] = str.charCodeAt(i); - } - return 1; + let req = new XMLHttpRequest(); + req.open("GET", read_string(filename), false); + req.overrideMimeType("text/plain; charset=x-user-defined"); + req.send(); + let str = req.response; + file_buffer_pos = 0; + file_buffer = new ArrayBuffer(str.length); + let buf_view = new Uint8Array(file_buffer); + for (let i = 0; i < str.length; ++i) { + buf_view[i] = str.charCodeAt(i); + } + return 1; }, js_ftell : function(stream) { - return file_buffer_pos; + return file_buffer_pos; }, js_fseek : function(stream, offset, origin) { - file_buffer_pos = offset; - if (origin == 1) - file_buffer_pos += file_buffer.byteLength; // SEEK_END - return 0; + file_buffer_pos = offset; + if (origin == 1) + file_buffer_pos += file_buffer.byteLength; // SEEK_END + return 0; }, js_fread : function(ptr, size, count, stream) { - let buf_view = new Uint8Array(file_buffer); - for (let i = 0; i < count; ++i) { - heapu8[ptr + i] = buf_view[file_buffer_pos++]; - } - return count; + let buf_view = new Uint8Array(file_buffer); + for (let i = 0; i < count; ++i) { + heapu8[ptr + i] = buf_view[file_buffer_pos++]; + } + return count; }, js_time : function() { - return window.performance.now(); + return window.performance.now(); }, js_pow : function(x) { - return Math.pow(x); + return Math.pow(x); }, js_floor : function(x) { - return Math.floor(x); + return Math.floor(x); }, js_sin : function(x) { - return Math.sin(x); + return Math.sin(x); }, js_cos : function(x) { - return Math.cos(x); + return Math.cos(x); }, js_tan : function(x) { - return Math.tan(x); + return Math.tan(x); }, js_log : function(base, exponent) { - return Math.log(base, exponent); + return Math.log(base, exponent); }, js_exp : function(x) { - return Math.exp(x); + return Math.exp(x); }, js_sqrt : function(x) { - return Math.sqrt(x); + return Math.sqrt(x); }, js_eval : function(str) { - (1, eval)(read_string(str)); + (1, eval)(read_string(str)); }, - js_canvas_w: function() { + js_canvas_w : function() { return canvas.width; }, - js_canvas_h: function() { + js_canvas_h : function() { return canvas.height; } } diff --git a/base/sources/backends/webgpu_gpu.c b/base/sources/backends/webgpu_gpu.c index ccd4985d..58548c80 100644 --- a/base/sources/backends/webgpu_gpu.c +++ b/base/sources/backends/webgpu_gpu.c @@ -102,6 +102,10 @@ static WGPUBlendFactor convert_blend_factor(gpu_blend_t factor) { } } +static int bytes_per_row_align(int bpr) { + return (bpr + 255) & ~255; +} + static void create_descriptors(void) { WGPUBindGroupLayoutEntry bindings[18]; memset(bindings, 0, sizeof(bindings)); @@ -156,7 +160,7 @@ static void create_descriptors(void) { wgpuBufferUnmap(dummy_upload); WGPUCommandEncoder dummy_encoder = wgpuDeviceCreateCommandEncoder(device, NULL); - WGPUTexelCopyBufferInfo src = {.layout = {.bytesPerRow = 4, .rowsPerImage = 1}, .buffer = dummy_upload}; + WGPUTexelCopyBufferInfo src = {.layout = {.bytesPerRow = bytes_per_row_align(4), .rowsPerImage = 1}, .buffer = dummy_upload}; WGPUTexelCopyTextureInfo dst = {.texture = dummy_texture}; WGPUExtent3D extent = {1, 1, 1}; wgpuCommandEncoderCopyBufferToTexture(dummy_encoder, &src, &dst, &extent); @@ -442,7 +446,7 @@ void gpu_get_render_target_pixels(gpu_texture_t *render_target, uint8_t *data) { WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, NULL); WGPUTexelCopyTextureInfo src = {.texture = render_target->impl.texture}; - WGPUTexelCopyBufferInfo dst = {.layout = {.bytesPerRow = render_target->width * gpu_texture_format_size(render_target->format), + WGPUTexelCopyBufferInfo dst = {.layout = {.bytesPerRow = bytes_per_row_align(render_target->width * gpu_texture_format_size(render_target->format)), .rowsPerImage = render_target->height}, .buffer = readback_buffer}; WGPUExtent3D extent = {(uint32_t)render_target->width, (uint32_t)render_target->height, 1}; @@ -488,7 +492,7 @@ static WGPUBindGroup get_descriptor_set(WGPUBuffer buffer) { void gpu_set_constant_buffer(gpu_buffer_t *buffer, int offset, size_t size) { WGPUBindGroup bind_group = get_descriptor_set(buffer->impl.buf); // uint32_t offsets[1] = {(uint32_t)offset}; - uint32_t offsets[1] = {(uint32_t)0}; + uint32_t offsets[1] = {(uint32_t)0}; wgpuRenderPassEncoderSetBindGroup(render_pass_encoder, 0, bind_group, 1, offsets); wgpuBindGroupRelease(bind_group); } @@ -643,7 +647,7 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, upload_buffer_size = new_upload_buffer_size; WGPUBufferDescriptor upload_desc = { .size = upload_buffer_size, - .usage = WGPUBufferUsage_CopySrc, + .usage = WGPUBufferUsage_CopySrc | WGPUBufferUsage_CopyDst, .mappedAtCreation = false, }; upload_buffer = wgpuDeviceCreateBuffer(device, &upload_desc); @@ -662,7 +666,7 @@ void gpu_texture_init_from_bytes(gpu_texture_t *texture, void *data, int width, texture->impl.texture = wgpuDeviceCreateTexture(device, &image_info); WGPUCommandEncoder encoder = wgpuDeviceCreateCommandEncoder(device, NULL); - WGPUTexelCopyBufferInfo src = {.layout = {.bytesPerRow = width * bpp, .rowsPerImage = height}, .buffer = upload_buffer}; + WGPUTexelCopyBufferInfo src = {.layout = {.bytesPerRow = bytes_per_row_align(width * bpp), .rowsPerImage = height}, .buffer = upload_buffer}; WGPUTexelCopyTextureInfo dst = {.texture = texture->impl.texture}; WGPUExtent3D extent = {(uint32_t)width, (uint32_t)height, 1}; wgpuCommandEncoderCopyBufferToTexture(encoder, &src, &dst, &extent); @@ -708,15 +712,15 @@ void gpu_vertex_buffer_init(gpu_buffer_t *buffer, int count, gpu_vertex_structur WGPUBufferDescriptor desc = {.size = buffer->count * buffer->stride, .usage = WGPUBufferUsage_Vertex, .mappedAtCreation = true}; buffer->impl.buf = wgpuDeviceCreateBuffer(device, &desc); - buffer->impl.temp = malloc(buffer->count * buffer->stride); + buffer->impl.mem = malloc(buffer->count * buffer->stride); } void *gpu_vertex_buffer_lock(gpu_buffer_t *buffer) { - return buffer->impl.temp; + return buffer->impl.mem; } void gpu_vertex_buffer_unlock(gpu_buffer_t *buffer) { - wgpuBufferUnmap2(buffer->impl.buf, buffer->impl.temp, 0, buffer->count * buffer->stride); + wgpuBufferUnmap2(buffer->impl.buf, buffer->impl.mem, 0, buffer->count * buffer->stride); } void gpu_index_buffer_init(gpu_buffer_t *buffer, int count) { @@ -725,15 +729,15 @@ void gpu_index_buffer_init(gpu_buffer_t *buffer, int count) { WGPUBufferDescriptor desc = {.size = buffer->count * buffer->stride, .usage = WGPUBufferUsage_Index, .mappedAtCreation = true}; buffer->impl.buf = wgpuDeviceCreateBuffer(device, &desc); - buffer->impl.temp = malloc(buffer->count * buffer->stride); + buffer->impl.mem = malloc(buffer->count * buffer->stride); } void *gpu_index_buffer_lock(gpu_buffer_t *buffer) { - return buffer->impl.temp; + return buffer->impl.mem; } void gpu_index_buffer_unlock(gpu_buffer_t *buffer) { - wgpuBufferUnmap2(buffer->impl.buf, buffer->impl.temp, 0, buffer->count * buffer->stride); + wgpuBufferUnmap2(buffer->impl.buf, buffer->impl.mem, 0, buffer->count * buffer->stride); } void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) { @@ -743,18 +747,17 @@ void gpu_constant_buffer_init(gpu_buffer_t *buffer, int size) { // WGPUBufferDescriptor desc = {.size = size, .usage = WGPUBufferUsage_Uniform, .mappedAtCreation = true}; WGPUBufferDescriptor desc = {.size = size, .usage = WGPUBufferUsage_Uniform, .mappedAtCreation = false}; buffer->impl.buf = wgpuDeviceCreateBuffer(device, &desc); - buffer->impl.temp = malloc(buffer->count * buffer->stride); + buffer->impl.mem = malloc(buffer->count * buffer->stride); } void gpu_constant_buffer_lock(gpu_buffer_t *buffer, int start, int count) { - // buffer->impl.start = start; - buffer->impl.start = 0; - buffer->impl.count = count; - buffer->data = &buffer->impl.temp[start]; + // buffer->start = start; + buffer->count = count; + buffer->data = &buffer->impl.mem[start]; } void gpu_constant_buffer_unlock(gpu_buffer_t *buffer) { - // wgpuBufferUnmap2(buffer->impl.buf, buffer->data, buffer->impl.start, buffer->impl.count); + // wgpuBufferUnmap2(buffer->impl.buf, buffer->data, buffer->start, buffer->count); buffer->data = NULL; } diff --git a/base/sources/backends/webgpu_gpu.h b/base/sources/backends/webgpu_gpu.h index 717a1051..a63aff8e 100644 --- a/base/sources/backends/webgpu_gpu.h +++ b/base/sources/backends/webgpu_gpu.h @@ -3,9 +3,7 @@ typedef struct { WGPUBuffer buf; - void *temp; - int start; - int count; + void *mem; } gpu_buffer_impl_t; typedef struct { diff --git a/base/sources/iron.h b/base/sources/iron.h index 488ee13f..88629f5b 100644 --- a/base/sources/iron.h +++ b/base/sources/iron.h @@ -244,6 +244,8 @@ int kickstart(int argc, char **argv) { char *bindir = "/"; #elif defined(IRON_IOS) char *bindir = ""; +#elif defined(IRON_WASM) + char *bindir = "/"; #else char *bindir = argv[0]; #endif @@ -352,7 +354,7 @@ unsigned char *iron_deflate_raw(unsigned char *data, int data_len, int *out_len, #include #endif -void _update(void *data) { +void _update() { #ifdef IRON_WINDOWS if (in_background && ++paused_frames > 3) { Sleep(1); @@ -735,7 +737,7 @@ void _iron_init(iron_window_options_t *ops) { void _iron_set_update_callback(void (*callback)(void)) { iron_update = callback; - iron_set_update_callback(_update, NULL); + iron_set_update_callback(_update); } void _iron_set_drop_files_callback(void (*callback)(char *)) { diff --git a/base/sources/iron_armpack.c b/base/sources/iron_armpack.c index 18487723..8d903be0 100644 --- a/base/sources/iron_armpack.c +++ b/base/sources/iron_armpack.c @@ -6,16 +6,20 @@ #include #include -static const int PTR_SIZE = 8; -static uint32_t di; // Decoded index -static uint32_t ei; // Encoded index -static uint32_t bottom; // Decoded bottom -static uint8_t *decoded; -static uint8_t *encoded; -static uint32_t capacity; -static uint32_t array_count; -static uint32_t string_length; -static void read_store(); +#ifdef IRON_WASM +#define PTR_SIZE 4 +#else +#define PTR_SIZE 8 +#endif +static uint32_t di; // Decoded index +static uint32_t ei; // Encoded index +static uint32_t bottom; // Decoded bottom +static uint8_t *decoded; +static uint8_t *encoded; +static uint32_t capacity; +static uint32_t array_count; +static uint32_t string_length; +static void read_store(); static inline uint64_t pad(int di, int n) { return (n - (di % n)) % n; @@ -52,13 +56,13 @@ static void store_f32(float f32) { static void store_ptr(uint32_t ptr) { di += pad(di, PTR_SIZE); - *(uint64_t *)(decoded + di) = (uint64_t)decoded + (uint64_t)ptr; + *(uintptr_t *)(decoded + di) = (uintptr_t)decoded + (uintptr_t)ptr; di += PTR_SIZE; } static void store_ptr_abs(void *ptr) { di += pad(di, PTR_SIZE); - *(uint64_t *)(decoded + di) = (uint64_t)ptr; + *(uintptr_t *)(decoded + di) = (uintptr_t)ptr; di += PTR_SIZE; } @@ -181,7 +185,7 @@ static uint32_t traverse(int di, bool count_arrays) { len += traverse(0, count_arrays); } } - len += 32; // buffer ptr, length, capacity + align? + len += PTR_SIZE + 4 + 4 + 16; // buffer ptr, length, capacity + align if (!count_arrays) { len = 0; } @@ -334,7 +338,7 @@ static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t f ei += length; length += pad(length, PTR_SIZE); arrays_length += length; - arrays_length += 8 + 8 + 4 + 4; + arrays_length += PTR_SIZE + 4 + 4; } } ei = _ei; @@ -345,7 +349,7 @@ static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t f array_count = count; for (uint32_t i = 0; i < count; ++i) { - /*uint8_t flag =*/ read_u8(); + /*uint8_t flag =*/read_u8(); read_store_array(read_i32()); } } @@ -365,8 +369,8 @@ static void read_store_array(uint32_t count) { // Store in any/i32/../_array_t f array_count = count; for (uint32_t i = 0; i < count; ++i) { - di = pad(di, PTR_SIZE) + di; - /*uint8_t flag =*/ read_u8(); + di = pad(di, PTR_SIZE) + di; + /*uint8_t flag =*/read_u8(); read_store_map(read_i32()); } } diff --git a/base/sources/iron_gc.c b/base/sources/iron_gc.c index c19d539d..9698fadf 100644 --- a/base/sources/iron_gc.c +++ b/base/sources/iron_gc.c @@ -3,40 +3,16 @@ #ifdef NO_GC -#include #include #include -#include - -#define HEAP_SIZE 512 * 1024 * 1024 -static uint8_t *heap = NULL; -static size_t heap_top = 0; - -#ifdef IRON_WASM -__attribute__((import_module("imports"), import_name("js_fprintf"))) void js_fprintf(const char *format); -#endif void *gc_alloc(size_t size) { -#ifdef HEAP_SIZE - size_t old_top = heap_top; - heap_top += size; -#ifdef IRON_WASM - if (heap_top >= HEAP_SIZE) { - js_fprintf("gc_alloc: out of memory"); - } -#endif - return &heap[old_top]; -#else return calloc(size, 1); -#endif } void gc_array(void *ptr, uint32_t *length) {} - void gc_leaf(void *ptr) {} - void gc_root(void *ptr) {} - void gc_unroot(void *ptr) {} void *gc_cut(void *ptr, size_t pos, size_t size) { @@ -44,36 +20,17 @@ void *gc_cut(void *ptr, size_t pos, size_t size) { } void *gc_realloc(void *ptr, size_t size) { -#ifdef HEAP_SIZE - void *new_ptr = gc_alloc(size); - if (ptr != NULL) { - memcpy(new_ptr, ptr, size); - } - return new_ptr; -#else return realloc(ptr, size); -#endif } void gc_free(void *ptr) { -#ifdef HEAP_SIZE -#else free(ptr); -#endif } void gc_pause() {} - void gc_resume() {} - void gc_run() {} - -void gc_start(void *bos) { -#ifdef HEAP_SIZE - heap = (uint8_t *)calloc(HEAP_SIZE, 1); -#endif -} - +void gc_start(void *bos) {} void gc_stop() {} #else diff --git a/base/sources/iron_system.c b/base/sources/iron_system.c index cea04a42..f7a495c9 100644 --- a/base/sources/iron_system.c +++ b/base/sources/iron_system.c @@ -20,6 +20,11 @@ typedef enum { } iron_log_level_t; void iron_log_args(iron_log_level_t level, const char *format, va_list args) { +#ifdef IRON_WASM + printf(format); + return; +#endif + #ifdef IRON_ANDROID va_list args_android_copy; va_copy(args_android_copy, args); @@ -82,8 +87,7 @@ double iron_time(void) { } #endif -static void (*update_callback)(void *) = NULL; -static void *update_callback_data = NULL; +static void (*update_callback)(void) = NULL; static void (*foreground_callback)(void *) = NULL; static void *foreground_callback_data = NULL; static void (*background_callback)(void *) = NULL; @@ -107,9 +111,8 @@ static void *paste_callback_data = NULL; bool with_autoreleasepool(bool (*f)(void)); #endif -void iron_set_update_callback(void (*callback)(void *), void *data) { - update_callback = callback; - update_callback_data = data; +void iron_set_update_callback(void (*callback)(void)) { + update_callback = callback; } void iron_set_foreground_callback(void (*callback)(void *), void *data) { @@ -159,7 +162,7 @@ void iron_set_paste_callback(void (*callback)(char *, void *), void *data) { void iron_internal_update_callback(void) { if (update_callback != NULL) { - update_callback(update_callback_data); + update_callback(); } } @@ -349,7 +352,7 @@ static void (*mouse_release_callback)(int /*button*/, int /*x*/, int /*y*/, void static void *mouse_release_callback_data = NULL; static void (*mouse_move_callback)(int /*x*/, int /*y*/, int /*movementX*/, int /*movementY*/, void * /*data*/) = NULL; static void *mouse_move_callback_data = NULL; -static void (*mouse_scroll_callback)(float /*delta*/, void * /*data*/) = NULL; +static void (*mouse_scroll_callback)(float /*delta*/, void * /*data*/) = NULL; static void *mouse_scroll_callback_data = NULL; void iron_mouse_set_press_callback(void (*value)(int /*button*/, int /*x*/, int /*y*/, void * /*data*/), void *data) { diff --git a/base/sources/iron_system.h b/base/sources/iron_system.h index 78f63517..4a74d5ad 100644 --- a/base/sources/iron_system.h +++ b/base/sources/iron_system.h @@ -96,7 +96,7 @@ void iron_exec_async(const char *path, char *argv[]); extern volatile int iron_exec_async_done; void iron_copy_to_clipboard(const char *text); -void iron_set_update_callback(void (*callback)(void *), void *data); +void iron_set_update_callback(void (*callback)(void)); void iron_set_foreground_callback(void (*callback)(void *), void *data); void iron_set_resume_callback(void (*callback)(void *), void *data); void iron_set_pause_callback(void (*callback)(void *), void *data); diff --git a/base/sources/libs/miniclib/memory.c b/base/sources/libs/miniclib/memory.c index a06d7021..62d61e6f 100644 --- a/base/sources/libs/miniclib/memory.c +++ b/base/sources/libs/miniclib/memory.c @@ -1,9 +1,9 @@ #include "stdlib.h" +#include "stdio.h" +#include "string.h" #ifdef IRON_WASM -__attribute__((import_module("imports"), import_name("js_fprintf"))) void js_fprintf(const char *format); - -#define HEAP_SIZE 1024 * 1024 * 512 +#define HEAP_SIZE 1024 * 1024 * 512 * 2 static unsigned char heap[HEAP_SIZE]; static size_t heap_top = 4; #endif @@ -21,7 +21,7 @@ malloc(size_t size) { size_t old_top = heap_top; heap_top += size; if (heap_top >= HEAP_SIZE) { - js_fprintf("malloc: out of memory"); + printf("malloc: out of memory"); } return &heap[old_top]; #endif @@ -29,7 +29,13 @@ malloc(size_t size) { } void *calloc(size_t num, size_t size) { +#ifdef IRON_WASM + void *ptr = malloc(num * size); + memset(ptr, 0, num * size); + return ptr; +#else return NULL; +#endif } void *alloca(size_t size) { @@ -37,7 +43,15 @@ void *alloca(size_t size) { } void *realloc(void *mem, size_t size) { +#ifdef IRON_WASM + void *new_ptr = malloc(size); + if (mem != NULL) { + memcpy(new_ptr, mem, size); + } + return new_ptr; +#else return NULL; +#endif } void free(void *mem) {} diff --git a/base/sources/libs/miniclib/stdio.c b/base/sources/libs/miniclib/stdio.c index b1678146..79490139 100644 --- a/base/sources/libs/miniclib/stdio.c +++ b/base/sources/libs/miniclib/stdio.c @@ -1,7 +1,7 @@ #include "stdio.h" #ifdef IRON_WASM -__attribute__((import_module("imports"), import_name("js_fprintf"))) void js_fprintf(const char *format); +__attribute__((import_module("imports"), import_name("js_printf"))) void js_printf(const char *format); __attribute__((import_module("imports"), import_name("js_fopen"))) FILE *js_fopen(const char *filename); __attribute__((import_module("imports"), import_name("js_ftell"))) long int js_ftell(FILE *stream); __attribute__((import_module("imports"), import_name("js_fseek"))) int js_fseek(FILE *stream, long int offset, int origin); @@ -10,9 +10,16 @@ __attribute__((import_module("imports"), import_name("js_fread"))) size_t js_f FILE *stdout = NULL, *stderr = NULL; +int printf(const char *format, ...) { +#ifdef IRON_WASM + js_printf(format); +#endif + return 0; +} + int fprintf(FILE *stream, const char *format, ...) { #ifdef IRON_WASM - js_fprintf(format); + js_printf(format); #endif return 0; } diff --git a/base/sources/libs/miniclib/stdio.h b/base/sources/libs/miniclib/stdio.h index e4124d7a..bfb10be2 100644 --- a/base/sources/libs/miniclib/stdio.h +++ b/base/sources/libs/miniclib/stdio.h @@ -10,22 +10,15 @@ typedef int FILE; 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); - -FILE *fopen(const char *filename, const char *mode); - -int fclose(FILE *stream); - +int printf(const char *format, ...); +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); +FILE *fopen(const char *filename, const char *mode); +int fclose(FILE *stream); long int ftell(FILE *stream); - -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); +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); diff --git a/base/tests/cube/sources/main.ts b/base/tests/cube/sources/main.ts index 111ae56b..c1a951a4 100644 --- a/base/tests/cube/sources/main.ts +++ b/base/tests/cube/sources/main.ts @@ -68,11 +68,26 @@ function scene_ready() { sys_notify_on_update(spin_cube); } -function spin_cube() { +function spin_cube(_: any) { let cube: object_t = scene_get_child("Cube"); transform_rotate(cube.transform, vec4_create(0, 0, 1), 0.01); } +//// +type config_t = { + server: string; +}; +let config_raw: config_t; +function strings_check_internet_connection(): string { + return ""; +} +function console_error(s: string) {} +function console_info(s: string) {} +function plugin_embed() {} function tr(id: string, vars: map_t = null): string { return id; } +let pipes_offset: i32; +function pipes_get_constant_location(s: string): i32 { + return 0; +} diff --git a/base/tools/amake/ashader.c b/base/tools/amake/ashader.c index b5156f98..bcc2cc05 100644 --- a/base/tools/amake/ashader.c +++ b/base/tools/amake/ashader.c @@ -106,6 +106,7 @@ void kong_compile(char *shader_lang, const char *from, const char *to) { analyze(); if (strcmp(shader_lang, "wgsl") == 0) { + transform(TRANSFORM_FLAG_ONE_COMPONENT_SWIZZLE); char *vs; char *fs; wgsl_export2(&vs, &fs); diff --git a/base/tools/make.js b/base/tools/make.js index b1762a03..c39354d0 100644 --- a/base/tools/make.js +++ b/base/tools/make.js @@ -948,7 +948,7 @@ class WasmExporter extends Exporter { this.make = new MakeExporter(compiler, compiler, compilerFlags, compilerFlags, // '--target=wasm32 -nostdlib -matomics -mbulk-memory "-Wl,--import-memory,--shared-memory,--allow-undefined,--no-entry"', '.wasm'); - '--target=wasm32 -nostdlib -matomics -mbulk-memory "-Wl,--import-memory,--shared-memory,--allow-undefined,--no-entry,--stack-first,--initial-memory=543621120,--max-memory=543621120,-z,stack-size=6553600"', '.wasm'); + '--target=wasm32 -nostdlib -matomics -mbulk-memory "-Wl,--import-memory,--shared-memory,--allow-undefined,--no-entry,--stack-first,--initial-memory=1310720000,--max-memory=1310720000,-z,stack-size=6553600"', '.wasm'); } export_solution(project) {