1
0
mirror of https://github.com/XProger/OpenLara.git synced 2025-08-16 01:54:38 +02:00

#15 WebAssembly support

This commit is contained in:
XProger
2018-05-09 20:42:52 +03:00
parent 42eab0cea2
commit 8f2b80c7a8
7 changed files with 56 additions and 29 deletions

View File

@@ -208,6 +208,8 @@ struct ShaderCache {
Shader *getShader(Core::Pass pass, Shader::Type type, int fx) { Shader *getShader(Core::Pass pass, Shader::Type type, int fx) {
Shader *shader = shaders[pass][type][fx]; Shader *shader = shaders[pass][type][fx];
#ifndef FFP #ifndef FFP
if (shader == NULL)
LOG("! NULL shader: %d %d %d\n", int(pass), int(type), int(fx));
ASSERT(shader != NULL); ASSERT(shader != NULL);
#endif #endif
return shader; return shader;
@@ -217,7 +219,8 @@ struct ShaderCache {
Core::pass = pass; Core::pass = pass;
Shader *shader = getShader(pass, type, fx); Shader *shader = getShader(pass, type, fx);
shader->bind(); if (shader)
shader->bind();
Core::setAlphaTest((fx & FX_ALPHA_TEST) != 0); Core::setAlphaTest((fx & FX_ALPHA_TEST) != 0);
} }

View File

@@ -923,10 +923,12 @@ namespace GAPI {
} }
void setAlphaTest(bool enable) { void setAlphaTest(bool enable) {
#ifdef FFP
if (enable) if (enable)
glEnable(GL_ALPHA_TEST); glEnable(GL_ALPHA_TEST);
else else
glDisable(GL_ALPHA_TEST); glDisable(GL_ALPHA_TEST);
#endif
} }
void setCullMode(int rsMask) { void setCullMode(int rsMask) {
@@ -1014,10 +1016,8 @@ namespace GAPI {
if (mask & RS_BLEND) if (mask & RS_BLEND)
setBlendMode(state & RS_BLEND); setBlendMode(state & RS_BLEND);
#ifdef FFP
if (mask & RS_DISCARD) if (mask & RS_DISCARD)
setAlphaTest((state & RS_DISCARD) != 0); setAlphaTest((state & RS_DISCARD) != 0);
#endif
} }
} }

View File

@@ -716,6 +716,8 @@ struct Level : IGame {
inventory.toggle(0, Inventory::PAGE_OPTION); inventory.toggle(0, Inventory::PAGE_OPTION);
} }
setClipParams(1.0f, NO_CLIP_PLANE);
effect = TR::Effect::NONE; effect = TR::Effect::NONE;
cube360 = NULL; cube360 = NULL;
@@ -2403,8 +2405,7 @@ struct Level : IGame {
player = players[view]; player = players[view];
camera = player->camera; camera = player->camera;
params->clipHeight = NO_CLIP_PLANE; setClipParams(1.0f, NO_CLIP_PLANE);
params->clipSign = 1.0f;
params->waterHeight = params->clipHeight; params->waterHeight = params->clipHeight;
if (shadow) { if (shadow) {

View File

@@ -15,7 +15,12 @@ AddEncoding gzip .gz
Header set Content-Encoding: gzip Header set Content-Encoding: gzip
</FilesMatch> </FilesMatch>
<FilesMatch "\.wasm\.gz$">
ForceType application/octet-stream
Header set Content-Encoding: gzip
</FilesMatch>
RewriteEngine on RewriteEngine on
RewriteCond %{HTTP:Accept-Encoding} gzip RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.+)\.(js|data|mem)$ $1.$2.gz [L] RewriteRule ^(.+)\.(js|data|mem|wasm)$ $1.$2.gz [L]

View File

@@ -0,0 +1,8 @@
@echo off
cls
set SRC=main.cpp ../../libs/stb_vorbis/stb_vorbis.c ../../libs/tinf/tinflate.c
set PROJ=OpenLara_wasm
set FLAGS=-s WASM=1 -O3 -ffast-math -Wno-deprecated-register --llvm-opts 2 -fmax-type-align=2 -std=c++11 -Wall -I../../
echo.
call em++ %SRC% %FLAGS% -o %PROJ%.js --preload-file ./level/1/TITLE.PSX --preload-file ./audio/1/dummy --preload-file ./audio/2/dummy --preload-file ./audio/3/dummy --preload-file ./level/2/dummy --preload-file ./level/3/dummy
gzip.exe -9 -f %PROJ%.data %PROJ%.js %PROJ%.wasm %PROJ%.js.mem load-wasm-worker.js

View File

@@ -85,7 +85,7 @@
var AudioContext = window.AudioContext || window.webkitAudioContext; var AudioContext = window.AudioContext || window.webkitAudioContext;
if (!AudioContext) return; if (!AudioContext) return;
audioContext = new (window.AudioContext || window.webkitAudioContext)(); audioContext = new (window.AudioContext || window.webkitAudioContext)();
var count = 2048; var count = 4096;
var rate = 44100 / audioContext.sampleRate; var rate = 44100 / audioContext.sampleRate;
var framesCount = Math.ceil(count * rate); var framesCount = Math.ceil(count * rate);
var frames = Module._malloc(framesCount * 4); // interleaved short L, R var frames = Module._malloc(framesCount * 4); // interleaved short L, R
@@ -103,13 +103,14 @@
proc.connect(audioContext.destination); proc.connect(audioContext.destination);
} }
// for iOS to unlock audio context after user interaction // unlock audio context after user interaction
var userAction = function() { var userAction = function() {
if (audioContext) { if (audioContext && audioContext.state == "suspended") {
audioContext.resume(); audioContext.resume().then(function() {
window.removeEventListener('touchstart', userAction, false); window.removeEventListener('touchstart', userAction, false);
window.removeEventListener('click', userAction, false); window.removeEventListener('click', userAction, false);
console.log("resume playback"); console.log("resume playback");
});
} }
}; };
window.addEventListener('touchstart', userAction, false); window.addEventListener('touchstart', userAction, false);
@@ -133,26 +134,33 @@
<input type="button" value="Browse Level" onclick="document.getElementById('browseFile').click();" /> (.PHD, .PSX, .TR2) <input type="button" value="Browse Level" onclick="document.getElementById('browseFile').click();" /> (.PHD, .PSX, .TR2)
<p style="margin:8px"> <p style="margin:8px">
OpenLara on <a target="_blank" href="https://github.com/XProger/OpenLara">github</a> & <a target="_blank" href="https://www.facebook.com/OpenLaraTR">facebook</a><br> OpenLara on <a target="_blank" href="https://github.com/XProger/OpenLara">github</a> & <a target="_blank" href="https://www.facebook.com/OpenLaraTR">facebook</a><br>
<br><i>last update: 8.05.2018</i><br> <br><i>last update: 9.05.2018</i><br>
</p> </p>
</span> </span>
<script> <script>
(function() {
var memoryInitializer = 'OpenLara.js.mem';
if (typeof Module['locateFile'] === 'function') {
memoryInitializer = Module['locateFile'](memoryInitializer);
} else if (Module['memoryInitializerPrefixURL']) {
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
}
var xhr = Module['memoryInitializerRequest'] = new XMLHttpRequest();
xhr.open('GET', memoryInitializer, true);
xhr.responseType = 'arraybuffer';
xhr.send(null);
})();
var script = document.createElement('script'); var script = document.createElement('script');
script.src = "OpenLara.js";
if ('WebAssembly' in window) {
console.log("Start WebAssembly!");
script.src = "OpenLara_wasm.js";
} else {
console.log("Start asm.js!");
script.src = "OpenLara.js";
(function() {
var memoryInitializer = 'OpenLara.js.mem';
if (typeof Module['locateFile'] === 'function') {
memoryInitializer = Module['locateFile'](memoryInitializer);
} else if (Module['memoryInitializerPrefixURL']) {
memoryInitializer = Module['memoryInitializerPrefixURL'] + memoryInitializer;
}
var xhr = Module['memoryInitializerRequest'] = new XMLHttpRequest();
xhr.open('GET', memoryInitializer, true);
xhr.responseType = 'arraybuffer';
xhr.send(null);
})();
}
document.body.appendChild(script); document.body.appendChild(script);
window.onbeforeunload = function () { // Ctrl+W window.onbeforeunload = function () { // Ctrl+W

View File

@@ -133,7 +133,9 @@ struct Shader {
} }
void init() { void init() {
bind(); Core::active.shader = this;
glUseProgram(ID);
for (int st = 0; st < sMAX; st++) { for (int st = 0; st < sMAX; st++) {
GLint idx = glGetUniformLocation(ID, (GLchar*)SamplerName[st]); GLint idx = glGetUniformLocation(ID, (GLchar*)SamplerName[st]);
if (idx != -1) if (idx != -1)