Skip to content

Commit

Permalink
jsbindings: add JS utilities, enhance testwasm.
Browse files Browse the repository at this point in the history
  • Loading branch information
prideout committed Oct 17, 2018
1 parent d15e275 commit e00ba80
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 94 deletions.
20 changes: 17 additions & 3 deletions libs/filamentjs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@ cmake_minimum_required(VERSION 3.1)

project(filamentjs)

set(JAVASCRIPT_SRC
${CMAKE_CURRENT_SOURCE_DIR}/wasmloader.js
${CMAKE_CURRENT_SOURCE_DIR}/utilities.js)

set(CPP_SRC
jsenums.cpp
jsbindings.cpp)

# The emcc options are not documented well, the best place to find them is the source:
# https://github.com/kripken/emscripten/blob/master/src/settings.js

set(COPTS "${COPTS} -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0")

set(LOPTS "${LOPTS} --bind")
set(LOPTS "${LOPTS} --post-js ${CMAKE_CURRENT_SOURCE_DIR}/wasmloader.js")
set(LOPTS "${LOPTS} -s ALLOW_MEMORY_GROWTH=1")
set(LOPTS "${LOPTS} -s MODULARIZE_INSTANCE=1")
set(LOPTS "${LOPTS} -s EXPORT_NAME=Filament")
foreach (JS_FILENAME ${JAVASCRIPT_SRC})
set(LOPTS "${LOPTS} --post-js ${JS_FILENAME}")
endforeach()

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LOPTS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COPTS}")

add_executable(filamentjs jsenums.cpp jsbindings.cpp)
set_target_properties(filamentjs PROPERTIES OUTPUT_NAME filament)
add_executable(filamentjs ${CPP_SRC})

set_target_properties(filamentjs PROPERTIES
LINK_DEPENDS "${JAVASCRIPT_SRC}"
OUTPUT_NAME filament)

target_link_libraries(filamentjs PRIVATE filament math utils image filameshio)
53 changes: 49 additions & 4 deletions libs/filamentjs/jsbindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <filament/Camera.h>
#include <filament/Engine.h>
#include <filament/IndexBuffer.h>
#include <filament/LightManager.h>
#include <filament/Material.h>
#include <filament/MaterialInstance.h>
#include <filament/RenderableManager.h>
Expand Down Expand Up @@ -82,6 +83,7 @@ namespace emscripten {
BIND(View)
BIND(Scene)
BIND(Camera)
BIND(LightManager)
BIND(RenderableManager)
BIND(TransformManager)
BIND(VertexBuffer)
Expand All @@ -104,11 +106,12 @@ using VertexBuilder = VertexBuffer::Builder;
using IndexBuilder = IndexBuffer::Builder;
using MatBuilder = Material::Builder;
using TexBuilder = Texture::Builder;
using LightBuilder = LightManager::Builder;

// We avoid directly exposing driver::BufferDescriptor because embind does not support move
// semantics and void* doesn't make sense to JavaScript anyway. This little wrapper class is exposed
// to JavaScript as "driver$BufferDescriptor", but clients will normally use our "Filament.Buffer"
// helper function, which is implemented in wasmloader.
// helper function (implemented in utilities.js)
struct BufferDescriptor {
// This form is used when JavaScript sends a buffer into WASM.
BufferDescriptor(val arrdata) {
Expand All @@ -132,7 +135,7 @@ struct BufferDescriptor {
};

// Exposed to JavaScript as "driver$PixelBufferDescriptor", but clients will normally use the
// "Filament.PixelBuffer" helper function, which is implemented in wasmloader.
// "Filament.PixelBuffer" helper function (implemented in utilities.js)
struct PixelBufferDescriptor {
PixelBufferDescriptor(val arrdata, driver::PixelDataFormat fmt, driver::PixelDataType dtype) {
auto byteLength = arrdata["byteLength"].as<uint32_t>();
Expand Down Expand Up @@ -286,7 +289,13 @@ class_<Camera>("Camera")
.function("setProjection", EMBIND_LAMBDA(void, (Camera* self, Camera::Projection projection,
double left, double right, double bottom, double top, double near, double far), {
self->setProjection(projection, left, right, bottom, top, near, far);
}), allow_raw_pointers());
}), allow_raw_pointers())
.function("setProjectionFov", EMBIND_LAMBDA(void, (Camera* self,
double fovInDegrees, double aspect, double near, double far, Camera::Fov direction), {
self->setProjection(fovInDegrees, aspect, near, far, direction);
}), allow_raw_pointers())
.function("setExposure", &Camera::setExposure)
.function("lookAt", &Camera::lookAt);

class_<RenderBuilder>("RenderableManager$Builder")
.function("build", EMBIND_LAMBDA(void, (RenderBuilder* builder,
Expand Down Expand Up @@ -322,6 +331,39 @@ class_<TransformManager>("TransformManager")

class_<TransformManager::Instance>("TransformManager$Instance");

class_<LightBuilder>("LightManager$Builder")
.function("build", EMBIND_LAMBDA(void, (LightBuilder* builder,
Engine* engine, utils::Entity entity), {
builder->build(*engine, entity);
}), allow_raw_pointers())
.BUILDER_FUNCTION("castShadows", LightBuilder, (LightBuilder* builder, bool enable), {
return &builder->castShadows(enable); })
.BUILDER_FUNCTION("castLight", LightBuilder, (LightBuilder* builder, bool enable), {
return &builder->castLight(enable); })
.BUILDER_FUNCTION("position", LightBuilder, (LightBuilder* builder, math::float3 value), {
return &builder->position(value); })
.BUILDER_FUNCTION("direction", LightBuilder, (LightBuilder* builder, math::float3 value), {
return &builder->direction(value); })
.BUILDER_FUNCTION("color", LightBuilder, (LightBuilder* builder, math::float3 value), {
return &builder->color(value); })
.BUILDER_FUNCTION("intensity", LightBuilder, (LightBuilder* builder, float value), {
return &builder->intensity(value); })
.BUILDER_FUNCTION("falloff", LightBuilder, (LightBuilder* builder, float value), {
return &builder->falloff(value); })
.BUILDER_FUNCTION("spotLightCone", LightBuilder,
(LightBuilder* builder, float inner, float outer), {
return &builder->spotLightCone(inner, outer); })
.BUILDER_FUNCTION("sunAngularRadius", LightBuilder,
(LightBuilder* builder, float value), { return &builder->sunAngularRadius(value); })
.BUILDER_FUNCTION("sunHaloSize", LightBuilder,
(LightBuilder* builder, float value), { return &builder->sunHaloSize(value); })
.BUILDER_FUNCTION("sunHaloFalloff", LightBuilder,
(LightBuilder* builder, float value), { return &builder->sunHaloFalloff(value); });

class_<LightManager>("LightManager")
.class_function("Builder", (LightBuilder (*)(LightManager::Type)) [] (LightManager::Type lt) {
return LightBuilder(lt); });

class_<VertexBuilder>("VertexBuffer$Builder")
.function("build", EMBIND_LAMBDA(VertexBuffer*, (VertexBuilder* builder, Engine* engine), {
return builder->build(*engine);
Expand Down Expand Up @@ -386,7 +428,10 @@ class_<MaterialInstance>("MaterialInstance")
self->setParameter(name.c_str(), value); }), allow_raw_pointers())
.function("setTextureParameter", EMBIND_LAMBDA(void,
(MaterialInstance* self, std::string name, Texture* value, TextureSampler sampler), {
self->setParameter(name.c_str(), value, sampler); }), allow_raw_pointers());
self->setParameter(name.c_str(), value, sampler); }), allow_raw_pointers())
.function("setColorParameter", EMBIND_LAMBDA(void,
(MaterialInstance* self, std::string name, RgbType type, math::float3 value), {
self->setParameter(name.c_str(), type, value); }), allow_raw_pointers());

class_<TextureSampler>("TextureSampler")
.constructor<driver::SamplerMinFilter, driver::SamplerMagFilter, driver::SamplerWrapMode>();
Expand Down
23 changes: 23 additions & 0 deletions libs/filamentjs/jsenums.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

#include <filament/Camera.h>
#include <filament/Color.h>
#include <filament/IndexBuffer.h>
#include <filament/RenderableManager.h>
#include <filament/LightManager.h>
#include <filament/Texture.h>
#include <filament/VertexBuffer.h>
#include <filament/View.h>
Expand All @@ -29,6 +31,16 @@ using namespace filament;

EMSCRIPTEN_BINDINGS(jsenums) {

enum_<RgbType>("RgbType")
.value("sRGB", RgbType::sRGB)
.value("LINEAR", RgbType::LINEAR);

enum_<RgbaType>("RgbaType")
.value("sRGB", RgbaType::sRGB)
.value("LINEAR", RgbaType::LINEAR)
.value("PREMULTIPLIED_sRGB", RgbaType::PREMULTIPLIED_sRGB)
.value("PREMULTIPLIED_LINEAR", RgbaType::PREMULTIPLIED_LINEAR);

enum_<VertexAttribute>("VertexAttribute")
.value("POSITION", POSITION)
.value("TANGENTS", TANGENTS)
Expand Down Expand Up @@ -70,6 +82,13 @@ enum_<VertexAttribute>("VertexAttribute")
.value("USHORT", IndexBuffer::IndexType::USHORT)
.value("UINT", IndexBuffer::IndexType::UINT);

enum_<LightManager::Type>("LightManager$Type")
.value("SUN", LightManager::Type::SUN)
.value("DIRECTIONAL", LightManager::Type::DIRECTIONAL)
.value("POINT", LightManager::Type::POINT)
.value("FOCUSED_SPOT", LightManager::Type::FOCUSED_SPOT)
.value("SPOT", LightManager::Type::SPOT);

enum_<RenderableManager::PrimitiveType>("RenderableManager$PrimitiveType")
.value("POINTS", RenderableManager::PrimitiveType::POINTS)
.value("LINES", RenderableManager::PrimitiveType::LINES)
Expand All @@ -85,6 +104,10 @@ enum_<VertexAttribute>("VertexAttribute")
.value("PERSPECTIVE", Camera::Projection::PERSPECTIVE)
.value("ORTHO", Camera::Projection::ORTHO);

enum_<Camera::Fov>("Camera$Fov")
.value("VERTICAL", Camera::Fov::VERTICAL)
.value("HORIZONTAL", Camera::Fov::HORIZONTAL);

enum_<Texture::Sampler>("Texture$Sampler") // aka driver::SamplerType
.value("SAMPLER_2D", Texture::Sampler::SAMPLER_2D)
.value("SAMPLER_CUBEMAP", Texture::Sampler::SAMPLER_CUBEMAP)
Expand Down
1 change: 1 addition & 0 deletions libs/filamentjs/tests/testwasm.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<body>
<canvas></canvas>
<script src="filament.js"></script>
<script src="gl-matrix-min.js"></script>
<script src="testwasm.js"></script>
</body>
</html>
Loading

0 comments on commit e00ba80

Please sign in to comment.