Skip to content

Commit

Permalink
Make basic functionality work as a JS library
Browse files Browse the repository at this point in the history
  • Loading branch information
kainino0x committed Oct 25, 2017
1 parent bcadbc1 commit 1035e48
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Expand Up @@ -2,6 +2,10 @@ cmake_minimum_required(VERSION 2.8.12)
project(shaderc)
enable_testing()

if(EMSCRIPTEN)
set(CMAKE_CXX_STANDARD 11)
endif()

message(STATUS "Shaderc: build type is \"${CMAKE_BUILD_TYPE}\".")

option(SHADERC_SKIP_INSTALL "Skip installation" ${SHADERC_SKIP_INSTALL})
Expand Down
8 changes: 8 additions & 0 deletions libshaderc/CMakeLists.txt
Expand Up @@ -99,3 +99,11 @@ if(${SHADERC_ENABLE_TESTS})
target_link_libraries(shaderc_c_smoke_test PRIVATE shaderc)
add_test(NAME shaderc_c_smoke_test COMMAND shaderc_c_smoke_test)
endif()

if(EMSCRIPTEN)
add_executable(shaderc.js shaderc.js.cpp)
target_link_libraries(shaderc.js shaderc_combined)
set_target_properties(shaderc.js PROPERTIES
OUTPUT_NAME "shaderc"
LINK_FLAGS "--bind")
endif()
46 changes: 46 additions & 0 deletions libshaderc/shaderc.js.cpp
@@ -0,0 +1,46 @@
#include "include/shaderc/shaderc.hpp"
#include <emscripten/bind.h>

using namespace emscripten;
using namespace shaderc;

EMSCRIPTEN_BINDINGS(shaderc) {
class_<SpvCompilationResult>("SpvCompilationResult")
.function("GetErrorMessage", &SpvCompilationResult::GetErrorMessage)
.function("GetBinary",
optional_override([](SpvCompilationResult& self) {
size_t length = self.end() - self.begin();
return val(typed_memory_view(length, self.begin()));
}))
;

class_<CompileOptions>("CompileOptions")
.constructor<>()
;

class_<Compiler>("Compiler")
.constructor<>()
.function("IsValid", &Compiler::IsValid)
//.function("CompileGlslToSpv", &Compiler::CompileGlslToSpv)
.function("CompileGlslToSpv",
optional_override([](Compiler& self,
const std::string& source_text,
shaderc_shader_kind shader_kind,
const std::string& input_file_name,
const std::string& entry_point_name,
const CompileOptions& options) {
return self.CompileGlslToSpv(
source_text.c_str(),
source_text.length(),
shader_kind,
input_file_name.c_str(),
entry_point_name.c_str(),
options);
}))
;

enum_<shaderc_shader_kind>("shader_kind")
.value("vertex", shaderc_vertex_shader)
.value("fragment", shaderc_fragment_shader)
;
}
22 changes: 22 additions & 0 deletions shaderc-test.html
@@ -0,0 +1,22 @@
<script src="shaderc.js"></script>
<script>
var source = "#version 450\nvoid main() {}";
console.log(source);

var compiler, opts, result, binary;
setTimeout(() => {

let S = Module;
compiler = new S.Compiler();
console.log("compiler =", compiler);
opts = new S.CompileOptions();
console.log("opts =", opts);

var result = compiler.CompileGlslToSpv(source, S.shader_kind.vertex, "", "main", opts);
console.log("result =", result);

console.log(result.GetErrorMessage());
let binary = result.GetBinary();
console.log("binary =", binary.length, binary);
}, 200);
</script>

0 comments on commit 1035e48

Please sign in to comment.