diff --git a/CWasm.sublime-project b/CWasm.sublime-project index 4ae5efd..0e16827 100644 --- a/CWasm.sublime-project +++ b/CWasm.sublime-project @@ -39,7 +39,6 @@ "CWASM_ENABLE_DBG_LEVEL_INFO", "CWASM_ENABLE_DBG_LEVEL_WARNING", "CWASM_ENABLE_DBG_LEVEL_WARNING", - "CWASM_NO_STB_IMAGE", "DbgLevel_Count", "DbgLevel_Debug", "DbgLevel_Error", @@ -444,7 +443,6 @@ "void jsGlBindBuffer(GlEnum bufferType, GlId bufferId)", "void jsGlBindTexture(GlEnum target, GlId textureId)", "void jsGlBindVertexArray(GlId vaoId)", - "void jsGlBlendEquation(GlEnum equation)", "void jsGlBlendFunc(GlEnum srcFactor, GlEnum dstFactor)", "void jsGlBlendFuncSeparate(GlEnum srcRGB, GlEnum dstRGB, GlEnum srcAlpha, GlEnum dstAlpha)", "void jsGlBufferData(GlEnum bufferType, u32 dataLength, const void* dataPntr, GlEnum usageHint)", @@ -464,7 +462,6 @@ "void jsGlFrontFace(GlEnum cullMode)", "void jsGlGenerateMipmap(GlEnum target)", "void jsGlLinkProgram(GlId programId)", - "void jsGlPixelStorei(GlEnum parameter, int value)", "void jsGlShaderSource(GlId shaderId, int sourceLength, const char* sourcePntr)", "void jsGlTexImage2D(GlEnum target, GlEnum level, GlEnum internalFormat, int width, int height, int border, GlEnum format, GlEnum type, int dataLength, const void* dataPntr)", "void jsGlTexParameteri(GlEnum target, GlEnum parameter, int value)", @@ -489,6 +486,7 @@ "void jsGlUniformMatrix4fv(GlId locationId, const mat4* valuesPntr)", "void jsGlUseProgram(GlId programId)", "void jsGlVertexAttribPointer(GlEnum attribLocation, int componentCount, GlEnum componentType, bool normalized, int stride, int offset)", + "void jsSetLabel(const char* labelIdStr, const char* labelStr)", "void* AllocMemUnaligned(Arena* arena, u32 numBytes)", "void* ReallocMemAligned(Arena* arena, void* oldPntr, u32 oldSize, u32 newSize, u32 newAlignment)", ], diff --git a/cwasm.c b/cwasm.c index 069f3cd..5eb1f6f 100644 --- a/cwasm.c +++ b/cwasm.c @@ -29,6 +29,7 @@ Description: #include "stb_image.h" #endif +#include "cwasm_misc_js_imports.h" #include "cwasm_webgl_js_imports.h" #include "cwasm_webgl_constants.h" @@ -37,6 +38,35 @@ void InitializeCWasm(u32 scratchArenasSize) InitGlobalArenas(scratchArenasSize); } +static inline void SetLabel(const char* labelName, const char* displayStr) { jsSetLabel(labelName, displayStr); } +void SetLabelPrint(const char* labelName, const char* formatStr, ...) +{ + char* displayStr = nullptr; + va_list args; + va_start(args, formatStr); + int length = vsnprintf(displayStr, 0, formatStr, args); //Measure first + Assert(length >= 0); + va_end(args); + if (length == 0) { SetLabel(labelName, ""); return; } + + ScratchBegin(scratch); + { + displayStr = AllocArray(char, scratch, length+1); + if (displayStr == nullptr) { SetLabel(labelName, ""); return; } + + va_start(args, formatStr); + vsnprintf(displayStr, (size_t)(length+1), formatStr, args); //Real printf + va_end(args); + displayStr[length] = '\0'; + + SetLabel(labelName, displayStr); + } + ScratchEnd(scratch); +} + +// +--------------------------------------------------------------+ +// | Memory Allocation Functions for Javascript | +// +--------------------------------------------------------------+ WASM_EXPORT(cAllocMem) void* cAllocMem(Arena* arenaPntr, int numBytes, int alignment) { NotNull(arenaPntr); diff --git a/cwasm_misc_js_imports.h b/cwasm_misc_js_imports.h new file mode 100644 index 0000000..fb50c20 --- /dev/null +++ b/cwasm_misc_js_imports.h @@ -0,0 +1,12 @@ +/* +File: cwasm_misc_js_imports.h +Author: Taylor Robbins +Date: 09\02\2025 +*/ + +#ifndef _CWASM_MISC_JS_IMPORTS_H +#define _CWASM_MISC_JS_IMPORTS_H + +MAYBE_EXTERN_C void jsSetLabel(const char* labelIdStr, const char* labelStr); + +#endif // _CWASM_MISC_JS_IMPORTS_H diff --git a/data/index.html b/data/index.html index 5c66f56..eebd4a6 100644 --- a/data/index.html +++ b/data/index.html @@ -7,13 +7,14 @@
-

🠟 The canvas is below 🠟

+

🠟 The canvas is below 🠟

Your browser does not support the HTML5 canvas tag. +
-

🠝 The canvas is above 🠝

+

🠝 The canvas is above 🠝

diff --git a/data/main.css b/data/main.css index f0763b0..862543b 100644 --- a/data/main.css +++ b/data/main.css @@ -14,6 +14,17 @@ body display: inline-block; } +#label_container +{ + display: flex; +} +#label_container > p +{ + margin-left: 20px; + margin-top: 0px; + margin-bottom: 0px; +} + canvas { border: 1px solid black; diff --git a/data/main.js b/data/main.js index 6fd64f9..f9fe589 100644 --- a/data/main.js +++ b/data/main.js @@ -3,6 +3,7 @@ import { WASM_MEMORY_PAGE_SIZE, appGlobals } from './globals.js' import { loadWasmModule, wasmPntrToJsString, wasmPntrAndLengthToJsString } from './wasm_functions.js' import { jsStdFunctions } from './std_functions.js' import { jsGlFunctions } from './gl_functions.js' +import { jsMiscFunctions } from './misc_functions.js' function AcquireCanvas(canvasWidth, canvasHeight) { @@ -23,24 +24,11 @@ function AcquireCanvas(canvasWidth, canvasHeight) appGlobals.canvas = canvas; } -async function LoadWasmModule(wasmFilePath, initialWasmPageCount) +async function MainLoop() { appGlobals.textDecoder = new TextDecoder("utf-8"); appGlobals.textEncoder = new TextEncoder("utf-8"); - appGlobals.wasmModule = await loadWasmModule(wasmFilePath, { ...jsStdFunctions, ...jsGlFunctions }); - appGlobals.memDataView = new DataView(new Uint8Array(appGlobals.wasmModule.exports.memory.buffer).buffer); - let memorySize = appGlobals.wasmModule.exports.memory.buffer.byteLength; - let numMemoryPagesAfterLoad = memorySize / WASM_MEMORY_PAGE_SIZE; - if ((memorySize % WASM_MEMORY_PAGE_SIZE) != 0) - { - console.warn("memorySize (" + memorySize + ") is not a multiple of WASM_MEMORY_PAGE_SIZE (" + WASM_MEMORY_PAGE_SIZE + ")"); - numMemoryPagesAfterLoad++; - } - appGlobals.wasmModule.exports.init_mem(numMemoryPagesAfterLoad); -} - -async function MainLoop() -{ + console.log("Initializing WebGL Canvas..."); AcquireCanvas(600, 400); @@ -50,7 +38,8 @@ async function MainLoop() appGlobals.glContext = canvasContextGl; console.log("Loading WASM Module..."); - await LoadWasmModule("app.wasm", 4); + appGlobals.wasmModule = await loadWasmModule("app.wasm", { ...jsStdFunctions, ...jsGlFunctions, ...jsMiscFunctions }); + appGlobals.memDataView = new DataView(new Uint8Array(appGlobals.wasmModule.exports.memory.buffer).buffer); let initSuccess = appGlobals.wasmModule.exports.App_Initialize(); diff --git a/data/misc_functions.js b/data/misc_functions.js new file mode 100644 index 0000000..7cea6d9 --- /dev/null +++ b/data/misc_functions.js @@ -0,0 +1,40 @@ + +import { appGlobals } from './globals.js' +import { wasmPntrToJsString, wasmPntrAndLengthToJsString } from './wasm_functions.js' + +export function jsSetLabel(labelIdStrPntr, labelStrPntr) +{ + let labelIdStr = wasmPntrToJsString(labelIdStrPntr); + let labelStr = wasmPntrToJsString(labelStrPntr); + var labelContainer = document.getElementById("label_container"); + if (labelContainer != null) + { + var foundExistingLabel = false; + var existingLabels = labelContainer.getElementsByClassName("wasm_label"); + if (existingLabels != null) + { + for (var lIndex = 0; lIndex < existingLabels.length; lIndex++) + { + if (existingLabels[lIndex].id == labelIdStr) + { + existingLabels[lIndex].textContent = labelStr; + foundExistingLabel = true; + break; + } + } + } + if (!foundExistingLabel) + { + const newLabel = document.createElement("p"); + newLabel.className = "wasm_label"; + newLabel.id = labelIdStr; + newLabel.textContent = labelStr; + labelContainer.appendChild(newLabel); + } + } + else { console.error("Failed to file container with id \"label_container\"!"); } +} + +export let jsMiscFunctions = { + jsSetLabel: jsSetLabel, +}; diff --git a/std/src/std_mem.c b/std/src/std_mem.c index 447e310..6f4a4b3 100644 --- a/std/src/std_mem.c +++ b/std/src/std_mem.c @@ -9,20 +9,19 @@ Description: extern unsigned char __heap_base; -static void* heapBaseAddress = 0; +static void* heapBaseAddress = nullptr; static size_t stdCurrentHeapSize = 0; static size_t stdCurrentPageCount = 0; -WASM_EXPORT(init_mem) void init_mem(size_t numInitialPages) -{ - heapBaseAddress = (void*)&__heap_base; - stdCurrentPageCount = numInitialPages; -} - //These are our own std-like functions for interacting with WebAssembly memory void* grow_mem(size_t numBytes) { - assert_msg(heapBaseAddress != nullptr, "growmem called before initmem! Make sure initmem is being called by the Javascript code!"); + if (heapBaseAddress == nullptr) + { + stdCurrentPageCount = __builtin_wasm_memory_size(0); + heapBaseAddress = (void*)&__heap_base; + } + size_t memorySizeNeeded = ((size_t)heapBaseAddress) + stdCurrentHeapSize; size_t numPagesNeeded = (memorySizeNeeded / WASM_MEMORY_PAGE_SIZE) + (((memorySizeNeeded % WASM_MEMORY_PAGE_SIZE) > 0) ? 1 : 0); if (numPagesNeeded > WASM_MEMORY_MAX_NUM_PAGES) diff --git a/std/stdlib.h b/std/stdlib.h index c14d59f..1c02164 100644 --- a/std/stdlib.h +++ b/std/stdlib.h @@ -26,7 +26,6 @@ _Noreturn void abort_msg(const char* message); // void* aligned_alloc(size_t numBytes, size_t alignmentSize); //These are our own std-like functions for interacting with WebAssembly memory -WASM_EXPORT(init_mem) void init_mem(size_t numInitialPages); void* grow_mem(size_t numBytes); size_t get_mem(); diff --git a/test_app.c b/test_app.c index 89b4901..49bcd3c 100644 --- a/test_app.c +++ b/test_app.c @@ -158,6 +158,11 @@ WASM_EXPORT(App_Initialize) bool App_Initialize() PrintLine_D("GL_VERSION: \"%s\"", jsGlGetParameterString(scratch, GL_VERSION)); PrintLine_D("GL_VENDOR: \"%s\"", jsGlGetParameterString(scratch, GL_VENDOR)); + #if 1 + SetLabelPrint("glVersionLabel", "GL_VERSION: %s", jsGlGetParameterString(scratch, GL_VERSION)); + SetLabelPrint("glVendorLabel", "GL_VENDOR: %s", jsGlGetParameterString(scratch, GL_VENDOR)); + #endif + jsGlEnable(GL_BLEND); jsGlBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); jsGlBlendEquation(GL_FUNC_ADD); @@ -401,6 +406,9 @@ WASM_EXPORT(App_UpdateAndRender) bool App_UpdateAndRender(r64 programTimeR64) jsGlDrawArrays(GL_TRIANGLES, 0, 6); } + SetLabelPrint("offsetLabel", "Offset: %f", OscillateBy(programTime, -1.0f, 1.0f, 15000, 0)); + SetLabelPrint("memoryLabel", "Mem: %u pages", stdCurrentPageCount); + #if 1 { r32 offsetX = -0.4f;