Skip to content

Commit

Permalink
[Script/LuaXr] Added Lua bindings for XR features
Browse files Browse the repository at this point in the history
- Const'd XR rendering functions that could be
  • Loading branch information
Razakhel committed Nov 13, 2024
1 parent bf23058 commit 90a35a2
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 17 deletions.
14 changes: 8 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,14 @@ else ()
message(STATUS "[RaZ] Lua scripting DISABLED")
endif ()

if (NOT RAZ_PLATFORM_MAC AND NOT RAZ_USE_EMSCRIPTEN AND RAZ_USE_WINDOW)
# XR currently isn't available with macOS or Emscripten and requires windowing capabilities
# TODO: requiring windowing currently makes XR unavailable from the GUI editor, as it handles the windowing part on its own
target_link_libraries(RaZ PRIVATE OpenXR)
else ()
list(REMOVE_ITEM RAZ_FILES "${PROJECT_SOURCE_DIR}/src/RaZ/Script/LuaXr.cpp")
endif ()

# The target is always linked, as the include files & their macros should be unconditionally available for ease of use; Tracy macros do something only
# if the expected definition is available, which only is if the related CMake option is set
# It also always compiles the implementation file, but making it conditional would be much more of a hassle. This may change in the future
Expand All @@ -483,12 +491,6 @@ endif ()

target_link_libraries(RaZ PRIVATE fastgltf simdjson stb)

if (NOT RAZ_PLATFORM_MAC AND NOT RAZ_USE_EMSCRIPTEN AND RAZ_USE_WINDOW)
# XR currently isn't available with macOS or Emscripten and requires windowing capabilities
# TODO: this makes XR unavailable from the GUI editor
target_link_libraries(RaZ PRIVATE OpenXR)
endif ()

# Compiling RaZ's sources
target_sources(RaZ PRIVATE ${RAZ_FILES})

Expand Down
2 changes: 1 addition & 1 deletion include/RaZ/Render/RenderSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class RenderSystem final : public System {
std::optional<Cubemap> m_cubemap {};

#if defined(RAZ_USE_XR)
XrSystem* m_xrSystem {};
const XrSystem* m_xrSystem {};
#endif
};

Expand Down
1 change: 1 addition & 0 deletions include/RaZ/Script/LuaWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class LuaWrapper {
static void registerUtilsTypes();
static void registerVectorTypes();
static void registerWindowTypes();
static void registerXrTypes();

static sol::state& getState();
};
Expand Down
8 changes: 5 additions & 3 deletions include/RaZ/XR/XrSession.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include "RaZ/Math/Angle.hpp"

#include <functional>

using XrSession = struct XrSession_T*;
struct XrInstance_T;
struct XrSpace_T;
Expand Down Expand Up @@ -39,7 +41,7 @@ class XrSession {
bool renderFrame(const std::vector<XrViewConfigurationView>& viewConfigViews,
unsigned int viewConfigType,
unsigned int environmentBlendMode,
const ViewRenderFunc& viewRenderFunc);
const ViewRenderFunc& viewRenderFunc) const;

~XrSession();

Expand All @@ -59,8 +61,8 @@ class XrSession {
bool renderLayer(RenderLayerInfo& layerInfo,
const std::vector<XrViewConfigurationView>& viewConfigViews,
unsigned int viewConfigType,
const ViewRenderFunc& viewRenderFunc);
void copyToSwapchains(const Texture2D& colorBuffer, const Texture2D& depthBuffer, uint32_t colorSwapchainImage, uint32_t depthSwapchainImage);
const ViewRenderFunc& viewRenderFunc) const;
void copyToSwapchains(const Texture2D& colorBuffer, const Texture2D& depthBuffer, uint32_t colorSwapchainImage, uint32_t depthSwapchainImage) const;

::XrSession m_handle {};
XrInstance m_instance {};
Expand Down
2 changes: 1 addition & 1 deletion include/RaZ/XR/XrSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class XrSystem final : public System {
void recoverViewConfigurations();
void recoverEnvironmentBlendModes();
void initializeSession();
bool renderFrame(const ViewRenderFunc& viewRenderFunc);
bool renderFrame(const ViewRenderFunc& viewRenderFunc) const;
bool processSessionStateChanged(const XrEventDataSessionStateChanged& sessionStateChanged);

XrContext m_context;
Expand Down
4 changes: 4 additions & 0 deletions src/RaZ/Script/LuaCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "RaZ/Render/RenderSystem.hpp"
#include "RaZ/Script/LuaWrapper.hpp"
#include "RaZ/Utils/TypeUtils.hpp"
#include "RaZ/XR/XrSystem.hpp"

#define SOL_ALL_SAFETIES_ON 1
#include "sol/sol.hpp"
Expand Down Expand Up @@ -72,6 +73,9 @@ void LuaWrapper::registerCoreTypes() {
WindowSetting, uint8_t>
#endif
);
#if defined(RAZ_USE_XR)
world["addXrSystem"] = &World::addSystem<XrSystem, const std::string&>;
#endif
world["addEntity"] = sol::overload([] (World& w) { return &w.addEntity(); },
PickOverload<bool>(&World::addEntity));
world["removeEntity"] = &World::removeEntity;
Expand Down
6 changes: 4 additions & 2 deletions src/RaZ/Script/LuaRenderSystem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "RaZ/Render/MeshRenderer.hpp"
#include "RaZ/Render/RenderSystem.hpp"
#include "RaZ/Script/LuaWrapper.hpp"
#include "RaZ/Utils/TypeUtils.hpp"
Expand Down Expand Up @@ -38,6 +37,9 @@ void LuaWrapper::registerRenderSystemTypes() {
renderSystem["hasCubemap"] = &RenderSystem::hasCubemap;
renderSystem["getCubemap"] = [] (const RenderSystem& r) { return &r.getCubemap(); };
renderSystem["setCubemap"] = [] (RenderSystem& r, Cubemap& c) { r.setCubemap(std::move(c)); };
#if defined(RAZ_USE_XR)
renderSystem["enableXr"] = [] (RenderSystem& r, XrSystem* x) { r.enableXr(*x); };
#endif
#if !defined(RAZ_NO_WINDOW)
renderSystem["createWindow"] = sol::overload([] (RenderSystem& r, unsigned int w, unsigned int h) { r.createWindow(w, h); },
[] (RenderSystem& r, unsigned int w, unsigned int h, const std::string& t) { r.createWindow(w, h, t); },
Expand All @@ -50,7 +52,7 @@ void LuaWrapper::registerRenderSystemTypes() {
renderSystem["updateLights"] = &RenderSystem::updateLights;
renderSystem["updateShaders"] = &RenderSystem::updateShaders;
renderSystem["updateMaterials"] = sol::overload([] (const RenderSystem& r) { r.updateMaterials(); },
PickOverload<const MeshRenderer&>(&RenderSystem::updateMaterials));
[] (const RenderSystem& r, const MeshRenderer* m) { r.updateMaterials(*m); });
renderSystem["saveToImage"] = sol::overload([] (const RenderSystem& r, const FilePath& p) { r.saveToImage(p); },
[] (const RenderSystem& r, const FilePath& p, TextureFormat f) { r.saveToImage(p, f); },
PickOverload<const FilePath&, TextureFormat, PixelDataType>(&RenderSystem::saveToImage));
Expand Down
3 changes: 3 additions & 0 deletions src/RaZ/Script/LuaWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ void LuaWrapper::registerTypes() {
#if !defined(RAZ_NO_WINDOW)
registerWindowTypes();
#endif
#if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(RAZ_NO_WINDOW)
registerXrTypes();
#endif

Logger::debug("[LuaWrapper] Registered types");

Expand Down
21 changes: 21 additions & 0 deletions src/RaZ/Script/LuaXr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "RaZ/XR/XrSystem.hpp"
#include "RaZ/Script/LuaWrapper.hpp"

#define SOL_ALL_SAFETIES_ON 1
#include "sol/sol.hpp"

namespace Raz {

void LuaWrapper::registerXrTypes() {
sol::state& state = getState();

{
sol::usertype<XrSystem> xrSystem = state.new_usertype<XrSystem>("XrSystem",
sol::constructors<XrSystem(const std::string&)>(),
sol::base_classes, sol::bases<System>());
xrSystem["getOptimalViewWidth"] = &XrSystem::getOptimalViewWidth;
xrSystem["getOptimalViewHeight"] = &XrSystem::getOptimalViewHeight;
}
}

} // namespace Raz
6 changes: 3 additions & 3 deletions src/RaZ/XR/XrSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void XrSession::end() {
bool XrSession::renderFrame(const std::vector<XrViewConfigurationView>& viewConfigViews,
unsigned int viewConfigType,
unsigned int environmentBlendMode,
const ViewRenderFunc& viewRenderFunc) {
const ViewRenderFunc& viewRenderFunc) const {
ZoneScopedN("XrSession::renderFrame");

if (!m_isRunning)
Expand Down Expand Up @@ -400,7 +400,7 @@ void XrSession::createSwapchainImages(XrSwapchain swapchain, SwapchainType swapc
bool XrSession::renderLayer(RenderLayerInfo& layerInfo,
const std::vector<XrViewConfigurationView>& viewConfigViews,
unsigned int viewConfigType,
const ViewRenderFunc& viewRenderFunc) {
const ViewRenderFunc& viewRenderFunc) const {
ZoneScopedN("XrSession::renderLayer");

std::vector<XrView> views(m_swapchainImages.size(), { XR_TYPE_VIEW });
Expand Down Expand Up @@ -500,7 +500,7 @@ bool XrSession::renderLayer(RenderLayerInfo& layerInfo,
return true;
}

void XrSession::copyToSwapchains(const Texture2D& colorBuffer, const Texture2D& depthBuffer, uint32_t colorSwapchainImage, uint32_t depthSwapchainImage) {
void XrSession::copyToSwapchains(const Texture2D& colorBuffer, const Texture2D& depthBuffer, uint32_t colorSwapchainImage, uint32_t depthSwapchainImage) const {
// https://docs.gl/gl4/glCopyImageSubData *could* be a viable and more direct solution, but expects both textures to have compatible internal formats
// (https://registry.khronos.org/OpenGL/specs/gl/glspec46.core.pdf#page=295), which we simply cannot have any guarantee of

Expand Down
2 changes: 1 addition & 1 deletion src/RaZ/XR/XrSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void XrSystem::initializeSession() {
m_session.createSwapchains(m_viewConfigViews);
}

bool XrSystem::renderFrame(const ViewRenderFunc& viewRenderFunc) {
bool XrSystem::renderFrame(const ViewRenderFunc& viewRenderFunc) const {
return m_session.renderFrame(m_viewConfigViews, m_viewConfigType, m_environmentBlendMode, viewRenderFunc);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/src/RaZ/Script/LuaRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ TEST_CASE("LuaRender RenderSystem", "[script][lua][render]") {
renderSystem:updateLights()
renderSystem:updateShaders()
renderSystem:updateMaterials()
renderSystem:updateMaterials(MeshRenderer.new())
renderSystem:saveToImage(FilePath.new("téstÊxpørt.jpg"))
renderSystem:saveToImage(FilePath.new("téstÊxpørt.hdr"), TextureFormat.DEPTH)
renderSystem:saveToImage(FilePath.new("téstÊxpørt.png"), TextureFormat.RGBA, PixelDataType.UBYTE)
Expand All @@ -626,6 +627,8 @@ TEST_CASE("LuaRender RenderSystem", "[script][lua][render]") {
renderSystem:createWindow(1, 1, "Test", WindowSetting.INVISIBLE, 1)
)"));
#endif

// Enabling XR rendering cannot be tested, as it requires having available XR device & runtime
}

TEST_CASE("LuaRender Shader", "[script][lua][render]") {
Expand Down

0 comments on commit 90a35a2

Please sign in to comment.