commit 305614525c729f70fa18fe26ebc6e223cd06860a
parent 4abcf6af784f6f74e3a974bb7efd9dcca0cef09f
Author: amin <dev@aminmesbah.com>
Date: Wed, 17 Jul 2019 02:25:23 +0000
Use stack allocator in wasm platform file loading
And finally get rid of that crazy huge stack buffer!
FossilOrigin-Name: e0d80e116e43d2bff03c95976a567ffc85faced474f3d2da5b8ed23c38a7bf21
Diffstat:
4 files changed, 38 insertions(+), 21 deletions(-)
diff --git a/src/platform_wasm.c b/src/platform_wasm.c
@@ -2,7 +2,13 @@
#include "platform.h"
#include "platform_wasm.h"
-extern unsigned char __heap_base;
+// wasm-ld memory layout:
+// a b
+// data | stack | heap
+// a = __data_end
+// b = __heap_base
+extern u8 __heap_base;
+extern u8 __data_end;
// There's no standardized, nondeprecated way to get a numeric ID of a keyboard
// key, so I guess we have to do that ourselves.
@@ -29,10 +35,6 @@ global_variable struct GameMemory game_memory = {0};
global_variable struct GameInput game_input = {0};
global_variable i32 g_width = PLATFORM_SCR_WIDTH;
global_variable i32 g_height = PLATFORM_SCR_HEIGHT;
-// TODO: For the love of God, replace this with an allocator!
-// NOTE: If this buffer is too small, the browser will freeze up
-global_variable u8 g_mem_buffer[200000] = {0};
-global_variable i32 g_mem_buffer_i = 0;
global_variable u32 previous_time = 0;
i32 str_length(const char *str)
@@ -117,22 +119,20 @@ PLATFORM_MEMORY_FREE(wasm_memory_free)
// do nothing
}
-// TODO: robustly return file length
PLATFORM_READ_ENTIRE_FILE(wasm_read_entire_file)
{
- g_mem_buffer_i++;
- u8 *buf = &g_mem_buffer[g_mem_buffer_i];
- i32 file_data_length = js_read_entire_file((i32)file_path, str_length(file_path), buf);
- if(file_data_length)
- {
- *out_num_bytes = file_data_length;
- g_mem_buffer_i += file_data_length;
- g_mem_buffer[g_mem_buffer_i] = '\0';
- wasm_print("Succeeded in reading file.");
- }
- else
+ i32 num_bytes_in_file = js_get_file_size((i32)file_path, str_length(file_path));
+
+ if (out_num_bytes)
{
- wasm_print("Failed to read file.");
+ *out_num_bytes = num_bytes_in_file;
}
+
+ u8 *buf = mem_st_alloc_buffer(a, u8, num_bytes_in_file + 1);
+ assert(buf);
+
+ js_read_entire_file((i32)file_path, str_length(file_path), buf);
+ buf[num_bytes_in_file] = '\0';
+ wasm_print("Succeeded in reading file.");
return buf;
}
diff --git a/src/platform_wasm.h b/src/platform_wasm.h
@@ -5,5 +5,6 @@ PLATFORM_READ_ENTIRE_FILE(wasm_read_entire_file);
PLATFORM_MEMORY_FREE(wasm_memory_free);
// Functions implemented in the JS loader
+i32 js_get_file_size(i32 file_path, i32 name_len);
i32 js_read_entire_file(i32 file_path, i32 name_len, u8 *file_data);
i32 js_print(i32 string, i32 len);
diff --git a/src/platform_wasm_js_symbols.txt b/src/platform_wasm_js_symbols.txt
@@ -42,5 +42,6 @@ webglUniformMatrix4fv
webglUseProgram
webglVertexAttribPointer
webglViewport
+js_get_file_size
js_read_entire_file
js_print
diff --git a/src/platform_wasm_loader.js b/src/platform_wasm_loader.js
@@ -204,7 +204,24 @@ imports["webglVertexAttribPointer"] = function(index, size, type, normalized, st
imports["webglViewport"] = function(x, y, width, height) {
gl.viewport(x, y, width, height);
}
-imports["js_read_entire_file"] = function(name, name_len, out_buf, out_len) {
+imports["js_get_file_size"] = function(name, name_len) {
+ let file_name = utf8decoder.decode(memory.subarray(name, name + name_len))
+ let file_size = 0;
+ if (file_name == "shader/main_f.glsl") {
+ var file = files[1];
+ } else if (file_name == "shader/main_v.glsl") {
+ var file = files[2];
+ } else if (file_name == "assets/tile0.tga") {
+ var file = files[3];
+ } else if (file_name == "assets/tileset0.tga") {
+ var file = files[4];
+ } else {
+ return 0;
+ }
+ file_size = file.byteLength;
+ return file_size;
+}
+imports["js_read_entire_file"] = function(name, name_len, out_buf) {
let file_name = utf8decoder.decode(memory.subarray(name, name + name_len))
let file_size = 0;
if (file_name == "shader/main_f.glsl") {
@@ -219,10 +236,8 @@ imports["js_read_entire_file"] = function(name, name_len, out_buf, out_len) {
return 0;
}
let arr = memory.subarray(out_buf, out_buf + file.byteLength);
- file_size = arr.length * arr.BYTES_PER_ELEMENT;
let s = String.fromCharCode.apply(null, arr);
arr.set(new Uint8Array(file));
- return file_size;
}
imports["js_print"] = function(s, len) {
let arr = memory.subarray(s, s + len);