Bläddra i källkod

Merge pull request #1907 from kainino0x/separate-web-and-emscripten

Separate GLSLANG_WEB (min-size build) and Emscripten options
John Kessenich 6 år sedan
förälder
incheckning
f7a48b153f

+ 28 - 33
CMakeLists.txt

@@ -28,9 +28,10 @@ option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
 
 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
 
-option(ENABLE_GLSLANG_WEB "Reduces glslang to minumum needed for web use" OFF)
-option(ENABLE_EMSCRIPTEN_SINGLE_FILE "If using emscripten, enables SINGLE_FILE build" OFF)
-option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE "If using emscripten, builds to run on Node instead of Web" OFF)
+option(ENABLE_GLSLANG_WEB "Reduces glslang to minimum needed for web use" OFF)
+option(ENABLE_GLSLANG_WEB_DEVEL "For ENABLE_GLSLANG_WEB builds, enables compilation error messages" OFF)
+option(ENABLE_EMSCRIPTEN_SINGLE_FILE "If using Emscripten, enables SINGLE_FILE build" OFF)
+option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE "If using Emscripten, builds to run on Node instead of Web" OFF)
 
 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL "Enables HLSL input support" ON "NOT ENABLE_GLSLANG_WEB" OFF)
 
@@ -70,6 +71,9 @@ endif(ENABLE_HLSL)
 
 if(ENABLE_GLSLANG_WEB)
     add_definitions(-DGLSLANG_WEB)
+    if(ENABLE_GLSLANG_WEB_DEVEL)
+        add_definitions(-DGLSLANG_WEB_DEVEL)
+    endif(ENABLE_GLSLANG_WEB_DEVEL)
 endif(ENABLE_GLSLANG_WEB)
 
 if(WIN32)
@@ -98,38 +102,29 @@ elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
     add_compile_options(/GR-) # Disable RTTI
 endif()
 
-if(ENABLE_GLSLANG_WEB)
-    if(EMSCRIPTEN)
-        add_compile_options(-Os -fno-exceptions)
-        add_compile_options("SHELL: -s WASM=1")
-        add_compile_options("SHELL: -s WASM_OBJECT_FILES=0")
-        add_link_options(-Os)
-        add_link_options("SHELL: -s FILESYSTEM=0")
-        add_link_options("SHELL: --llvm-lto 1")
-        add_link_options("SHELL: --closure 1")
-        add_link_options("SHELL: -s ALLOW_MEMORY_GROWTH=1")
-
-        add_link_options("SHELL: -s MODULARIZE=1")
-        if(ENABLE_EMSCRIPTEN_SINGLE_FILE)
-            add_link_options("SHELL: -s SINGLE_FILE=1")
-        endif(ENABLE_EMSCRIPTEN_SINGLE_FILE)
-
-        if(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE)
-            add_link_options("SHELL: -s ENVIRONMENT=node")
-            add_link_options("SHELL: -s BINARYEN_ASYNC_COMPILATION=0")
+if(EMSCRIPTEN)
+    add_compile_options(-Os -fno-exceptions)
+    add_compile_options("SHELL: -s WASM=1")
+    add_compile_options("SHELL: -s WASM_OBJECT_FILES=0")
+    add_link_options(-Os)
+    add_link_options("SHELL: -s FILESYSTEM=0")
+    add_link_options("SHELL: --llvm-lto 1")
+    add_link_options("SHELL: --closure 1")
+    add_link_options("SHELL: -s ALLOW_MEMORY_GROWTH=1")
+
+    if(ENABLE_EMSCRIPTEN_SINGLE_FILE)
+        add_link_options("SHELL: -s SINGLE_FILE=1")
+    endif(ENABLE_EMSCRIPTEN_SINGLE_FILE)
+else()
+    if(ENABLE_GLSLANG_WEB)
+        if(MSVC)
+            add_compile_options(/Os /GR-)
         else()
-            add_link_options("SHELL: -s ENVIRONMENT=web,worker")
-            add_link_options("SHELL: -s EXPORT_ES6=1")
+            add_compile_options(-Os -fno-exceptions)
+            add_link_options(-Os)
         endif()
-    else()
-      if(MSVC)
-        add_compile_options(/Os /GR-)
-      else()
-        add_compile_options(-Os -fno-exceptions)
-        add_link_options(-Os)
-      endif()
-    endif(EMSCRIPTEN)
-endif(ENABLE_GLSLANG_WEB)
+    endif(ENABLE_GLSLANG_WEB)
+endif(EMSCRIPTEN)
 
 # Request C++11
 if(${CMAKE_VERSION} VERSION_LESS 3.1)

+ 12 - 9
README.md

@@ -166,27 +166,30 @@ when executed from the glslang subdirectory of the glslang repository.
 With no arguments it builds the full grammar, and with a "web" argument,
 the web grammar subset (see more about the web subset in the next section).
 
-### WASM for the Web
+### Building to WASM for the Web and Node
 
 Use the steps in [Build Steps](#build-steps), with the following notes/exceptions:
 * For building the web subset of core glslang:
   + execute `updateGrammar web` from the glslang subdirectory
     (or if using your own scripts, `m4` needs a `-DGLSLANG_WEB` argument)
-  + turn off the CMAKE options for `BUILD_TESTING`, `ENABLE_OPT`, and `INSTALL_GTEST`,
-    while turning on `ENABLE_GLSLANG_WEB`
+  + set `-DENABLE_HLSL=OFF -DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF`
+  + turn on `-DENABLE_GLSLANG_WEB=ON`
+  + optionally, for GLSL compilation error messages, turn on `-DENABLE_GLSLANG_WEB_DEVEL=ON`
 * `emsdk` needs to be present in your executable search path, *PATH* for
   Bash-like enivironments
   + [Instructions located
     here](https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install)
-* Do not checkout SPIRV-Tools into `External`
-  + Does not work correctly with emscripten out of the box and we don't want it
-    in the build anyway. *TBD* Have build ignore SPIRV-Tools for web build
-* Wrap call to `cmake` using `emconfigure` with ENABLE_GLSLANG_WEB=ON:
-  + e.g. For Linux, `emconfigure cmake -DCMAKE_BUILD_TYPE=Release
-    -DENABLE_GLSLANG_WEB=ON -DCMAKE_INSTALL_PREFIX="$(pwd)/install" ..`
+* Wrap cmake call: `emcmake cmake`
 * To get a fully minimized build, make sure to use `brotli` to compress the .js
   and .wasm files
 
+Example:
+
+```sh
+emcmake cmake -DCMAKE_BUILD_TYPE=Release -DENABLE_GLSLANG_WEB=ON \
+    -DENABLE_HLSL=OFF -DBUILD_TESTING=OFF -DENABLE_OPT=OFF -DINSTALL_GTEST=OFF ..
+```
+
 Testing
 -------
 

+ 4 - 13
glslang/CMakeLists.txt

@@ -6,6 +6,10 @@ else(WIN32)
     message("unknown platform")
 endif(WIN32)
 
+if(EMSCRIPTEN OR ENABLE_GLSLANG_WEB)
+    add_subdirectory(OSDependent/Web)
+endif(EMSCRIPTEN OR ENABLE_GLSLANG_WEB)
+
 set(SOURCES
     MachineIndependent/glslang.m4
     MachineIndependent/glslang.y
@@ -114,16 +118,3 @@ if(ENABLE_GLSLANG_INSTALL)
         install(FILES ${file} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/glslang/${dir})
     endforeach()
 endif(ENABLE_GLSLANG_INSTALL)
-
-if(ENABLE_GLSLANG_WEB)
-    add_executable(glslang.js glslang.js.cpp)
-    glslang_set_link_args(glslang.js)
-    target_link_libraries(glslang.js glslang SPIRV)
-    if(EMSCRIPTEN)
-        set_target_properties(glslang.js PROPERTIES
-          OUTPUT_NAME "glslang"
-          SUFFIX ".js"
-          LINK_FLAGS "--bind -s EXPORT_NAME=\"glslangModule\"")
-        em_link_pre_js(glslang.js ${CMAKE_CURRENT_SOURCE_DIR}/glslang.pre.js)
-    endif(EMSCRIPTEN)
-endif(ENABLE_GLSLANG_WEB)

+ 24 - 0
glslang/OSDependent/Web/CMakeLists.txt

@@ -0,0 +1,24 @@
+add_executable(glslang.js "glslang.js.cpp")
+glslang_set_link_args(glslang.js)
+target_link_libraries(glslang.js glslang SPIRV)
+if(EMSCRIPTEN)
+    set_target_properties(glslang.js PROPERTIES
+        OUTPUT_NAME "glslang"
+        SUFFIX ".js")
+    em_link_pre_js(glslang.js "${CMAKE_CURRENT_SOURCE_DIR}/glslang.pre.js")
+
+    target_link_options(glslang.js PRIVATE
+        "SHELL:--bind -s MODULARIZE=1")
+    if(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE)
+        target_link_options(glslang.js PRIVATE
+            "SHELL:-s ENVIRONMENT=node -s BINARYEN_ASYNC_COMPILATION=0")
+    else()
+        target_link_options(glslang.js PRIVATE
+            "SHELL:-s ENVIRONMENT=web,worker")
+    endif()
+
+    if(NOT ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE)
+        add_custom_command(TARGET glslang.js POST_BUILD
+            COMMAND cat ${CMAKE_CURRENT_SOURCE_DIR}/glslang.after.js >> ${CMAKE_CURRENT_BINARY_DIR}/glslang.js)
+    endif()
+endif(EMSCRIPTEN)

+ 26 - 0
glslang/OSDependent/Web/glslang.after.js

@@ -0,0 +1,26 @@
+export default (() => {
+    const initialize = () => {
+        return new Promise(resolve => {
+            Module({
+                locateFile() {
+                    const i = import.meta.url.lastIndexOf('/')
+                    return import.meta.url.substring(0, i) + '/glslang.wasm';
+                },
+                onRuntimeInitialized() {
+                    resolve({
+                        compileGLSLZeroCopy: this.compileGLSLZeroCopy,
+                        compileGLSL: this.compileGLSL,
+                    });
+                },
+            });
+        });
+    };
+
+    let instance;
+    return () => {
+        if (!instance) {
+            instance = initialize();
+        }
+        return instance;
+    };
+})();

+ 5 - 6
glslang/glslang.js.cpp → glslang/OSDependent/Web/glslang.js.cpp

@@ -35,17 +35,16 @@
 
 #include <cstdio>
 #include <cstdint>
+#include <memory>
 
 #ifdef __EMSCRIPTEN__
 #include <emscripten.h>
-#endif  // __EMSCRIPTEN__
-#include <memory>
+#endif
 
-#include "../SPIRV/GlslangToSpv.h"
-#include "../SPIRV/doc.h"
-#include "./../glslang/Public/ShaderLang.h"
+#include "../../../SPIRV/GlslangToSpv.h"
+#include "../../../glslang/Public/ShaderLang.h"
 
-#ifndef EMSCRIPTEN_KEEPALIVE
+#ifndef __EMSCRIPTEN__
 #define EMSCRIPTEN_KEEPALIVE
 #endif
 

+ 0 - 0
glslang/glslang.pre.js → glslang/OSDependent/Web/glslang.pre.js


+ 1 - 1
glslang/updateGrammar

@@ -1,4 +1,4 @@
-#!/usr/bin/bash
+#!/bin/bash
 
 if [ "$1" = 'web' ]
 then