Used __builtin_wasm_memory_size(0) instead of having the Javascript code call init_mem with the initial page count. Added a simple <div> below the canvas that can hold any number of labels that can be filled/updated through jsSetLabel which lives in a new file misc_functions.js.

This commit is contained in:
2025-09-02 16:05:53 -07:00
parent f45c2d88e0
commit 04028b29d3
10 changed files with 117 additions and 30 deletions

View File

@@ -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)",
],

30
cwasm.c
View File

@@ -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);

12
cwasm_misc_js_imports.h Normal file
View File

@@ -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

View File

@@ -7,13 +7,14 @@
</head>
<body>
<div class="main_section">
<p>&#129055 The canvas is below &#129055</p>
<p style="margin-bottom:1px">&#129055 The canvas is below &#129055</p>
<div id="canvas_container">
<canvas>
Your browser does not support the HTML5 canvas tag.
</canvas>
<div id="label_container"> </div>
</div>
<p>&#129053 The canvas is above &#129053</p>
<p style="margin-top:1px">&#129053 The canvas is above &#129053</p>
</div>
<script async type="module" src="main.js" ></script>
</body>

View File

@@ -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;

View File

@@ -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();

40
data/misc_functions.js Normal file
View File

@@ -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,
};

View File

@@ -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)

View File

@@ -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();

View File

@@ -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;