Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: nitrogen #256

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions package/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ find_package(ReactAndroid REQUIRED CONFIG)
find_package(fbjni REQUIRED CONFIG)
find_library(LOG_LIB log)

include(${CMAKE_SOURCE_DIR}/../nitrogen/generated/android/RNF+autolinking.cmake)

# Add react-native-filament sources
add_library(
${PACKAGE_NAME}
Expand Down Expand Up @@ -127,7 +129,7 @@ target_link_libraries(
)

# Link with RNWC:
if (IS_OLD_ARCHITECTURE_ENABLED)
if(IS_OLD_ARCHITECTURE_ENABLED)
# On the old arch RNWC publishes a prefab that we need to find (and it has a different name):
find_package(react-native-worklets-core REQUIRED CONFIG)
message("RN Filament: react-native-worklets core found! Enabling Worklets support...")
Expand All @@ -136,13 +138,14 @@ if (IS_OLD_ARCHITECTURE_ENABLED)
${PACKAGE_NAME}
react-native-worklets-core::rnworklets
)
else ()
else()
target_link_libraries(
${PACKAGE_NAME}
react_codegen_rnfilament # link against the codegen generated library of rnf (needed so this module is compiled correctly)
react-native-worklets-core
${PACKAGE_NAME}
react_codegen_rnfilament # link against the codegen generated library of rnf (needed so this module is compiled correctly)
react-native-worklets-core
)
endif()

add_definitions(-DHAS_WORKLETS=1)

# Filament (local CMake project as a git submodule)
Expand Down
30 changes: 15 additions & 15 deletions package/cpp/RNFFilamentProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,30 @@ bool FilamentProxy::getHasWorklets() {

#if HAS_WORKLETS
std::shared_ptr<RNWorklet::JsiWorkletContext> FilamentProxy::createWorkletContext() {
Logger::log(TAG, "Creating Worklet Context...");
margelo::Logger::log(TAG, "Creating Worklet Context...");
auto jsDispatcher = getJSDispatcher();
auto runOnJS = [=](std::function<void()>&& function) { jsDispatcher->runAsync(std::move(function)); };
auto renderThreadDispatcher = getRenderThreadDispatcher();
auto runOnWorklet = [=](std::function<void()>&& function) { renderThreadDispatcher->runAsync(std::move(function)); };
auto& runtime = getMainJSRuntime();
auto workletContext = std::make_shared<RNWorklet::JsiWorkletContext>("FilamentRenderer", &runtime, runOnJS, runOnWorklet);
Logger::log(TAG, "Successfully created WorkletContext! Installing global Dispatcher...");
margelo::Logger::log(TAG, "Successfully created WorkletContext! Installing global Dispatcher...");

workletContext->invokeOnWorkletThread([=](RNWorklet::JsiWorkletContext*, jsi::Runtime& runtime) {
Dispatcher::installRuntimeGlobalDispatcher(runtime, renderThreadDispatcher);
Logger::log(TAG, "Successfully installed global Dispatcher in WorkletContext!");
margelo::Dispatcher::installRuntimeGlobalDispatcher(runtime, renderThreadDispatcher);
margelo::Logger::log(TAG, "Successfully installed global Dispatcher in WorkletContext!");
});

return workletContext;
}
#endif

jsi::Value FilamentProxy::getCurrentDispatcher(jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) {
return Dispatcher::getRuntimeGlobalDispatcherHolder(runtime);
return margelo::Dispatcher::getRuntimeGlobalDispatcherHolder(runtime);
}

std::future<std::shared_ptr<FilamentBuffer>> FilamentProxy::loadAssetAsync(const std::string& path) {
Logger::log(TAG, "Loading asset %s...", path.c_str());
margelo::Logger::log(TAG, "Loading asset %s...", path.c_str());
auto weakThis = std::weak_ptr<FilamentProxy>(shared<FilamentProxy>());
auto dispatcher = getBackgroundDispatcher();
return dispatcher->runAsyncAwaitable<std::shared_ptr<FilamentBuffer>>([weakThis, path]() {
Expand All @@ -82,7 +82,7 @@ std::future<std::shared_ptr<FilamentBuffer>> FilamentProxy::loadAssetAsync(const
}

std::future<std::shared_ptr<FilamentView>> FilamentProxy::findFilamentViewAsync(int id) {
Logger::log(TAG, "Finding FilamentView #%i...", id);
margelo::Logger::log(TAG, "Finding FilamentView #%i...", id);
auto weakThis = std::weak_ptr<FilamentProxy>(shared<FilamentProxy>());
auto dispatcher = getUIDispatcher();
return dispatcher->runAsyncAwaitable<std::shared_ptr<FilamentView>>([weakThis, id]() {
Expand All @@ -96,15 +96,15 @@ std::future<std::shared_ptr<FilamentView>> FilamentProxy::findFilamentViewAsync(
}

std::shared_ptr<TestHybridObject> FilamentProxy::createTestObject() {
Logger::log(TAG, "Creating TestObject...");
margelo::Logger::log(TAG, "Creating TestObject...");
return std::make_shared<TestHybridObject>();
}

std::shared_ptr<EngineWrapper> FilamentProxy::createEngine(std::optional<std::string> backend,
std::optional<std::unordered_map<std::string, int>> arguments) {
Logger::log(TAG, "Creating Engine...");
margelo::Logger::log(TAG, "Creating Engine...");

std::shared_ptr<Dispatcher> renderThread = getRenderThreadDispatcher();
std::shared_ptr<margelo::Dispatcher> renderThread = getRenderThreadDispatcher();

Engine::Config config = EngineConfigHelper::makeConfigFromUserParams(arguments);
Engine::Backend backendEnum = Engine::Backend::DEFAULT;
Expand All @@ -118,14 +118,14 @@ std::shared_ptr<EngineWrapper> FilamentProxy::createEngine(std::optional<std::st
// Make sure that the engine gets destroyed on the thread that it was created on.
// It can happen that the engine gets cleaned up by Hades (hermes GC) on a different thread.
renderThread->runAsync([engine]() {
Logger::log(TAG, "Destroying engine...");
margelo::Logger::log(TAG, "Destroying engine...");
Engine::destroy(engine);
});
});

// Get screen refresh rate
float refreshRate = getDisplayRefreshRate();
Logger::log(TAG, "Display refresh rate: %f Hz", refreshRate);
margelo::Logger::log(TAG, "Display refresh rate: %f Hz", refreshRate);

float densityPixelRatio = getDensityPixelRatio();

Expand All @@ -136,13 +136,13 @@ std::shared_ptr<EngineWrapper> FilamentProxy::createEngine(std::optional<std::st
}

std::shared_ptr<BulletWrapper> FilamentProxy::createBullet() {
Logger::log(TAG, "Creating Bullet...");
margelo::Logger::log(TAG, "Creating Bullet...");
return std::make_shared<BulletWrapper>();
}

jsi::Value FilamentProxy::createChoreographerWrapper(jsi::Runtime& runtime, const jsi::Value&, const jsi::Value*, size_t) {

Logger::log(TAG, "Creating Choreographer...");
margelo::Logger::log(TAG, "Creating Choreographer...");
std::shared_ptr<Choreographer> choreographer = createChoreographer();

ChoreographerWrapper* choreographerWrapperPtr = new ChoreographerWrapper(choreographer);
Expand All @@ -157,7 +157,7 @@ jsi::Value FilamentProxy::createChoreographerWrapper(jsi::Runtime& runtime, cons
delete ptr;
});

return JSIConverter<std::shared_ptr<ChoreographerWrapper>>::toJSI(runtime, choreographerWrapper);
return margelo::JSIConverter<std::shared_ptr<ChoreographerWrapper>>::toJSI(runtime, choreographerWrapper);
}

} // namespace margelo
10 changes: 5 additions & 5 deletions package/cpp/RNFFilamentProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace margelo {

using namespace facebook;

class FilamentProxy : public HybridObject {
class FilamentProxy : public margelo::HybridObject {
public:
explicit FilamentProxy() : HybridObject(TAG) {}

Expand All @@ -51,22 +51,22 @@ class FilamentProxy : public HybridObject {
/**
* Get the Dispatcher for the main react JS thread.
*/
virtual std::shared_ptr<Dispatcher> getJSDispatcher() = 0;
virtual std::shared_ptr<margelo::Dispatcher> getJSDispatcher() = 0;
/**
* Get the Dispatcher that is responsible for rendering to Filament.
* This is guaranteed to only use a single Thread, as opposed to a Thread-pool.
*/
virtual std::shared_ptr<Dispatcher> getRenderThreadDispatcher() = 0;
virtual std::shared_ptr<margelo::Dispatcher> getRenderThreadDispatcher() = 0;
/**
* Get the Dispatcher for the platform-default UI Thread.
* This is guaranteed to only use a single Thread, as opposed to a Thread-pool.
*/
virtual std::shared_ptr<Dispatcher> getUIDispatcher() = 0;
virtual std::shared_ptr<margelo::Dispatcher> getUIDispatcher() = 0;
/**
* Get a Dispatcher that uses a Thread-pool for background operations such as File I/O.
* This Dispatcher may use multiple Threads to run code.
*/
virtual std::shared_ptr<Dispatcher> getBackgroundDispatcher() = 0;
virtual std::shared_ptr<margelo::Dispatcher> getBackgroundDispatcher() = 0;
/**
* Get the refresh rate of the display in Hz.
* Needed for correct frame pacing and dynamic resolution calculations.
Expand Down
6 changes: 3 additions & 3 deletions package/cpp/RNFFilamentRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ namespace margelo {

using namespace facebook;

class FilamentRecorder : public HybridObject {
class FilamentRecorder : public margelo::HybridObject {
public:
using ReadyForMoreDataCallback = std::function<bool()>;

public:
explicit FilamentRecorder(std::shared_ptr<Dispatcher> renderThreadDispatcher, int width, int height, int fps, double bitRate);
explicit FilamentRecorder(std::shared_ptr<margelo::Dispatcher> renderThreadDispatcher, int width, int height, int fps, double bitRate);
~FilamentRecorder();

public:
Expand Down Expand Up @@ -77,7 +77,7 @@ class FilamentRecorder : public HybridObject {
static constexpr auto TAG = "FilamentRecorder";

protected:
std::shared_ptr<Dispatcher> _renderThreadDispatcher;
std::shared_ptr<margelo::Dispatcher> _renderThreadDispatcher;
int _width;
int _height;
int _fps;
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/RNFSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace margelo {

class Surface : public HybridObject {
class Surface : public margelo::HybridObject {
public:
explicit Surface() : HybridObject("Surface") {}

Expand Down
6 changes: 3 additions & 3 deletions package/cpp/RNFSurfaceProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace margelo {

class SurfaceProvider : public HybridObject {
class SurfaceProvider : public margelo::HybridObject {
public:
using TOnCreate = std::function<void(std::shared_ptr<Surface> surface)>;
using TOnResize = std::function<void(std::shared_ptr<Surface> surface, int width, int height)>;
Expand All @@ -32,8 +32,8 @@ class SurfaceProvider : public HybridObject {

public:
std::shared_ptr<Listener> addOnSurfaceChangedListener(Callbacks&& callbacks);
std::shared_ptr<Listener> addOnSurfaceCreatedListener(TOnCreate callback, std::shared_ptr<Dispatcher> dispatcher);
std::shared_ptr<Listener> addOnSurfaceDestroyedListener(TOnDestroy callback, std::shared_ptr<Dispatcher> dispatcher);
std::shared_ptr<Listener> addOnSurfaceCreatedListener(TOnCreate callback, std::shared_ptr<margelo::Dispatcher> dispatcher);
std::shared_ptr<Listener> addOnSurfaceDestroyedListener(TOnDestroy callback, std::shared_ptr<margelo::Dispatcher> dispatcher);

virtual std::shared_ptr<Surface> getSurfaceOrNull() = 0;
std::optional<std::shared_ptr<Surface>> getSurface();
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/bullet/RNFRigidBodyWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RigidBodyWrapper;
*/
using CollisionCallback = std::function<void(std::shared_ptr<RigidBodyWrapper>&, const std::shared_ptr<RigidBodyWrapper>)>;

class RigidBodyWrapper : public HybridObject {
class RigidBodyWrapper : public margelo::HybridObject {
public:
explicit RigidBodyWrapper(double mass, std::shared_ptr<btCollisionShape> shape, std::unique_ptr<btMotionState> motionState,
std::string id, std::optional<CollisionCallback> collisionCallback);
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/core/RNFAABBWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace margelo {

using namespace filament;

class AABBWrapper : public HybridObject {
class AABBWrapper : public margelo::HybridObject {
public:
explicit AABBWrapper(const Aabb& aabb) : HybridObject("AABBWrapper"), _aabb(aabb) {}
void loadHybridMethods() override;
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/core/RNFAmbientOcclusionOptionsWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace margelo {

using namespace filament;

class AmbientOcclusionOptionsWrapper : public HybridObject, public AmbientOcclusionOptions {
class AmbientOcclusionOptionsWrapper : public margelo::HybridObject, public AmbientOcclusionOptions {
public:
explicit AmbientOcclusionOptionsWrapper() : HybridObject("AmbientOcclusionOptionsWrapper") {}
explicit AmbientOcclusionOptionsWrapper(const AmbientOcclusionOptions& options)
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/core/RNFDynamicResolutionOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace margelo {

using namespace filament;

class DynamicResolutionOptionsWrapper : public HybridObject, public DynamicResolutionOptions {
class DynamicResolutionOptionsWrapper : public margelo::HybridObject, public DynamicResolutionOptions {
public:
explicit DynamicResolutionOptionsWrapper() : HybridObject("DynamicResolutionOptions") {}
explicit DynamicResolutionOptionsWrapper(const DynamicResolutionOptions& options)
Expand Down
4 changes: 2 additions & 2 deletions package/cpp/core/RNFEngineImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ using ManipulatorBuilder = Manipulator<float>::Builder;
// If you add a new method that you want to expose to JS, you need to add it to the EngineWrapper as well.
class EngineImpl : public std::enable_shared_from_this<EngineImpl> {
public:
explicit EngineImpl(std::shared_ptr<Dispatcher> rendererDispatcher, std::shared_ptr<Engine> engine, float displayRefreshRate,
explicit EngineImpl(std::shared_ptr<margelo::Dispatcher> rendererDispatcher, std::shared_ptr<Engine> engine, float displayRefreshRate,
float densityPixelRatio);

// First a surface provider must be set, then once we have a surface a swapchain can be created and finally the swapchain can be set
Expand Down Expand Up @@ -73,7 +73,7 @@ class EngineImpl : public std::enable_shared_from_this<EngineImpl> {
private:
std::mutex _mutex;
std::shared_ptr<Engine> _engine;
std::shared_ptr<Dispatcher> _rendererDispatcher;
std::shared_ptr<margelo::Dispatcher> _rendererDispatcher;
std::shared_ptr<SurfaceProvider> _surfaceProvider;
std::shared_ptr<Listener> _surfaceListener;
std::shared_ptr<gltfio::MaterialProvider> _materialProvider;
Expand Down
4 changes: 2 additions & 2 deletions package/cpp/core/RNFEngineWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void EngineWrapper::setSurfaceProvider(std::shared_ptr<SurfaceProvider> surfaceP
}
std::shared_ptr<SwapChainWrapper> EngineWrapper::createSwapChainForSurface(std::shared_ptr<SurfaceProvider> surfaceProvider,
bool enableTransparentRendering) {
Logger::log(TAG, "Creating swapchain for surface ...");
margelo::Logger::log(TAG, "Creating swapchain for surface ...");

std::shared_ptr<Surface> surface = surfaceProvider->getSurfaceOrNull();
if (surface == nullptr) {
Expand All @@ -74,7 +74,7 @@ std::shared_ptr<SwapChainWrapper> EngineWrapper::createSwapChainForSurface(std::
return std::make_shared<SwapChainWrapper>(swapChain);
}
std::shared_ptr<SwapChainWrapper> EngineWrapper::createSwapChainForRecorder(std::shared_ptr<FilamentRecorder> recorder) {
Logger::log(TAG, "Creating swapchain for recorder ...");
margelo::Logger::log(TAG, "Creating swapchain for recorder ...");

if (recorder == nullptr) {
throw std::invalid_argument("Recorder is null");
Expand Down
2 changes: 1 addition & 1 deletion package/cpp/core/RNFFilamentInstanceWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ using namespace gltfio;

class AnimatorWrapper;

class FilamentInstanceWrapper : public HybridObject {
class FilamentInstanceWrapper : public margelo::HybridObject {
public:
explicit FilamentInstanceWrapper(FilamentInstance* instance) : HybridObject("FilamentInstanceWrapper"), _instance(instance) {}

Expand Down
8 changes: 4 additions & 4 deletions package/cpp/core/RNFRenderableManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Texture* RenderableManagerImpl::createTextureFromBuffer(std::shared_ptr<Filament

if (texture == nullptr) {
std::string error = _textureProvider->getPushMessage();
Logger::log(TAG, "Error loading texture: %s", error.c_str());
margelo::Logger::log(TAG, "Error loading texture: %s", error.c_str());
throw std::runtime_error("Error loading texture: " + error);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ void RenderableManagerImpl::changeMaterialTextureMap(std::shared_ptr<EntityWrapp
std::shared_ptr<MaterialInstance> newInstance =
std::shared_ptr<MaterialInstance>(MaterialInstance::duplicate(materialInstance), [engine, dispatcher](MaterialInstance* instance) {
dispatcher->runAsync([engine, instance]() {
Logger::log(TAG, "Destroying material instance %p", instance);
margelo::Logger::log(TAG, "Destroying material instance %p", instance);
engine->destroy(instance);
});
});
Expand All @@ -166,7 +166,7 @@ void RenderableManagerImpl::startUpdateResourceLoading() {

// Check for textures that now have all their miplevels initialized.
while (Texture* _texture = _textureProvider->popTexture()) {
Logger::log(TAG, "%p has all its miplevels ready.", _texture);
margelo::Logger::log(TAG, "%p has all its miplevels ready.", _texture);
}
}
}
Expand Down Expand Up @@ -282,7 +282,7 @@ void RenderableManagerImpl::scaleBoundingBox(std::shared_ptr<FilamentAssetWrappe
Entity entity = entities[i];
RenderableManager::Instance renderable = renderableManager.getInstance(entity);
Box boundingBox = renderableManager.getAxisAlignedBoundingBox(renderable);
Logger::log(TAG, "#%d Bounding box: min: %f %f %f, max: %f %f %f", i, boundingBox.getMin().x, boundingBox.getMin().y,
margelo::Logger::log(TAG, "#%d Bounding box: min: %f %f %f, max: %f %f %f", i, boundingBox.getMin().x, boundingBox.getMin().y,
boundingBox.getMin().z, boundingBox.getMax().x, boundingBox.getMax().y, boundingBox.getMax().z);
// Create a new box that is twice the size
Box box = Box().set(boundingBox.getMin() * scaleFactor, boundingBox.getMax() * scaleFactor);
Expand Down
24 changes: 16 additions & 8 deletions package/cpp/core/RNFSwapChainWrapper.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#pragma once

#include "jsi/RNFPointerHolder.h"
#include "HybridSwapChainSpec.hpp"
#include "jsi/RNFPointerHolderNitro.h"

#include <filament/SwapChain.h>

namespace margelo {
using namespace filament;
using namespace nitro::RNF;

class SwapChainWrapper : public PointerHolder<SwapChain> {
class SwapChainWrapper : public HybridSwapChainSpec, public PointerHolderNitro<SwapChain> {
public:
explicit SwapChainWrapper(std::shared_ptr<SwapChain> swapChain) : PointerHolder("SwapChainWrapper", swapChain) {}
explicit SwapChainWrapper(std::shared_ptr<SwapChain> swapChain) : HybridSwapChainSpec(), PointerHolderNitro(HybridSwapChainSpec::TAG, swapChain) {}

void loadHybridMethods() override {}

std::shared_ptr<SwapChain> getSwapChain() {
return pointee();
}
std::shared_ptr<SwapChain> getSwapChain() {
return pointee();
}

bool getIsValid() override {
return PointerHolderNitro::getIsValid();
}

void release() override {
PointerHolderNitro::release();
}
};
} // namespace margelo
2 changes: 1 addition & 1 deletion package/cpp/core/math/RNFTMat44Wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace margelo {
using namespace filament;

class TMat44Wrapper : public HybridObject {
class TMat44Wrapper : public margelo::HybridObject {
public:
explicit TMat44Wrapper(math::mat4f matrix) : HybridObject("TMat44Wrapper"), _matrix(matrix) {}

Expand Down
Loading