Skip to content
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
1 change: 1 addition & 0 deletions Build/Linux/_deps/arcana.cpp-src
Submodule arcana.cpp-src added at c726db
1 change: 1 addition & 0 deletions Build/Linux/_deps/base-n-src
Submodule base-n-src added at 7573e7
1 change: 1 addition & 0 deletions Build/Linux/_deps/bgfx.cmake-src
Submodule bgfx.cmake-src added at 0af3c9
1 change: 1 addition & 0 deletions Build/Linux/_deps/cmakeextensions-src
Submodule cmakeextensions-src added at ea28b7
1 change: 1 addition & 0 deletions Build/Linux/_deps/glslang-src
Submodule glslang-src added at 6388a3
1 change: 1 addition & 0 deletions Build/Linux/_deps/gsl-src
Submodule gsl-src added at a35345
1 change: 1 addition & 0 deletions Build/Linux/_deps/jsruntimehost-src
Submodule jsruntimehost-src added at 796453
1 change: 1 addition & 0 deletions Build/Linux/_deps/libwebp-src
Submodule libwebp-src added at 57e324
1 change: 1 addition & 0 deletions Build/Linux/_deps/spirv-cross-src
Submodule spirv-cross-src added at 6abfcf
1 change: 1 addition & 0 deletions Build/Linux/_deps/urllib-src
Submodule urllib-src added at 1f9d2c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <X11/Xlib.h>
#undef None

namespace Babylon::Graphics
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

#include <d3d11.h>

#define GRAPHICS_BACK_BUFFER_SUPPORT

namespace Babylon::Graphics
{
using DeviceT = ID3D11Device*;
using TextureT = ID3D11Resource*;
using TextureFormatT = DXGI_FORMAT;

struct DeviceConfiguration
{
DeviceT Device;
float DevicePixelRatio{1.f};
};
using BackBufferColorT = ID3D11RenderTargetView*;
using BackBufferDepthStencilT = ID3D11DepthStencilView*;

struct PlatformInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace Babylon::Graphics
using TextureT = ID3D12Resource*;
using TextureFormatT = DXGI_FORMAT;

struct DeviceConfiguration
{
DeviceT Device;
float DevicePixelRatio{1.f};
};

struct PlatformInfo
{
DeviceT Device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace Babylon::Graphics
using TextureT = id<MTLTexture>;
using TextureFormatT = MTLPixelFormat;

struct DeviceConfiguration
{
DeviceT Device;
float DevicePixelRatio{1.f};
};

struct PlatformInfo
{
struct MTLDevice* Device;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ namespace Babylon::Graphics
using TextureT = unsigned int;
using TextureFormatT = unsigned int;

struct DeviceConfiguration
{
DeviceT Device;
float DevicePixelRatio{1.f};
};

struct PlatformInfo
{
DeviceT Device;
Expand Down
26 changes: 26 additions & 0 deletions Core/Graphics/Include/Shared/Babylon/Graphics/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

namespace Babylon::Graphics
{
enum class DepthStencilFormat
{
// Do not create a depth/stencil texture.
None,

// Create a 32-bit depth texture with no stencil.
Depth32,

// Create a combined 24-bit depth and 8-bit stencil texture.
Depth24Stencil8,
};

struct Configuration
{
// Custom device to use instead of creating one internally.
Expand All @@ -17,6 +29,16 @@ namespace Babylon::Graphics
// The platform specific window.
WindowT Window{};

#ifdef GRAPHICS_BACK_BUFFER_SUPPORT
// Color back buffer to use instead of creating one internally.
// @remarks Only available for D3D11.
BackBufferColorT BackBufferColor{};

// Depth stencil back buffer to use instead of creating one internally.
// @remarks Only available for D3D11. DepthStencilFormat is ignored when specified.
BackBufferDepthStencilT BackBufferDepthStencil{};
#endif

// The resolution width.
size_t Width{};

Expand All @@ -28,6 +50,10 @@ namespace Babylon::Graphics

// When enabled, back buffer will be premultiplied with alpha value.
bool AlphaPremultiplied{};

// Format to use when creating the depth/stencil texture for the back buffer.
// Specify DepthStencilFormat::None to not create a depth/stencil texture.
DepthStencilFormat BackBufferDepthStencilFormat{DepthStencilFormat::Depth24Stencil8};
};

class Device;
Expand Down
52 changes: 36 additions & 16 deletions Core/Graphics/Source/DeviceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace Babylon::Graphics
m_state.Bgfx.Initialized = false;

auto& init = m_state.Bgfx.InitState;
init.callback = &m_bgfxCallback;

// Use the noop renderer if the configuration has no window and no size.
if (config.Window == WindowT{} && config.Width == 0 && config.Height == 0)
Expand All @@ -48,16 +49,42 @@ namespace Babylon::Graphics
init.type = s_bgfxRenderType;
}

//
// init.resolution
//

init.resolution.reset = BGFX_RESET_VSYNC | BGFX_RESET_MAXANISOTROPY | BGFX_RESET_FLIP_AFTER_RENDER;
init.resolution.maxFrameLatency = 1;

init.callback = &m_bgfxCallback;

init.platformData.context = config.Device;
UpdateWindow(config.Window);
UpdateSize(config.Width, config.Height);
UpdateMSAA(config.MSAASamples);
UpdateAlphaPremultiplied(config.AlphaPremultiplied);

switch (config.BackBufferDepthStencilFormat)
{
case DepthStencilFormat::None:
init.resolution.formatDepthStencil = bgfx::TextureFormat::UnknownDepth;
break;
case DepthStencilFormat::Depth32:
init.resolution.formatDepthStencil = bgfx::TextureFormat::D32;
break;
case DepthStencilFormat::Depth24Stencil8:
default:
init.resolution.formatDepthStencil = bgfx::TextureFormat::D24S8;
break;
}

//
// init.platformData
//

UpdateWindow(config.Window);
UpdateDevice(config.Device);

#ifdef GRAPHICS_BACK_BUFFER_SUPPORT
init.platformData.backBuffer = config.BackBufferColor;
init.platformData.backBufferDS = config.BackBufferDepthStencil;
#endif
}

DeviceImpl::~DeviceImpl()
Expand Down Expand Up @@ -133,18 +160,6 @@ namespace Babylon::Graphics
m_state.Bgfx.Dirty = true;
}

void DeviceImpl::UpdateDevicePixelRatio(float value)
{
std::scoped_lock lock{m_state.Mutex};
m_state.Resolution.DevicePixelRatio = value;
m_state.Bgfx.Dirty = true;
}

void DeviceImpl::SetRenderResetCallback(std::function<void()> callback)
{
m_renderResetCallback = std::move(callback);
}

void DeviceImpl::AddToJavaScript(Napi::Env env)
{
JsRuntime::NativeObject::GetFromJavaScript(env)
Expand All @@ -164,6 +179,11 @@ namespace Babylon::Graphics
return DeviceContext::Create(env, *this);
}

void DeviceImpl::SetRenderResetCallback(std::function<void()> callback)
{
m_renderResetCallback = std::move(callback);
}

void DeviceImpl::EnableRendering()
{
std::scoped_lock lock{m_state.Mutex};
Expand Down
4 changes: 2 additions & 2 deletions Core/Graphics/Source/DeviceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ namespace Babylon::Graphics
void UpdateSize(size_t width, size_t height);
void UpdateMSAA(uint8_t value);
void UpdateAlphaPremultiplied(bool enabled);
void UpdateDevicePixelRatio(float value);
void SetRenderResetCallback(std::function<void()> callback);

void AddToJavaScript(Napi::Env);
static DeviceImpl& GetFromJavaScript(Napi::Env);

Napi::Value CreateContext(Napi::Env);

void SetRenderResetCallback(std::function<void()> callback);

void EnableRendering();
void DisableRendering();

Expand Down
5 changes: 5 additions & 0 deletions Dependencies/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ set(BGFX_OPENGL_USE_EGL ON)
set(BGFX_USE_DEBUG_SUFFIX OFF)
FetchContent_MakeAvailable_With_Message(bgfx.cmake)

target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_DEFAULT_MAX_ENCODERS=2)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MAX_VERTEX_STREAMS=18)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_RESOURCE_COMMAND_BUFFER_SIZE=16)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_MIN_UNIFORM_BUFFER_SIZE=4096)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_THRESHOLD_SIZE=256)
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_UNIFORM_BUFFER_RESIZE_INCREMENT_SIZE=1024)
if(GRAPHICS_API STREQUAL "D3D11")
target_compile_definitions(bgfx PRIVATE BGFX_CONFIG_RENDERER_DIRECT3D11=1)
elseif(GRAPHICS_API STREQUAL "D3D12")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ namespace Babylon::Plugins
// Returns the height of the texture.
uint32_t Height() const;

// Returns the underlying texture.
Graphics::TextureT Get() const;

// Adds this texture to the graphics context of the given N-API environment.
// NOTE: Must call from the JavaScript thread.
Napi::Promise AddToContextAsync(Napi::Env) const;
Napi::Promise AddToContextAsync(Napi::Env, std::optional<uint16_t> layerIndex = {}) const;

// Updates to a new texture.
// Texture attributes (width, height, format, etc.) must match.
// NOTE: Must call from the Graphics thread.
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT> = {});
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT> = {}, std::optional<uint16_t> layerIndex = {});

private:
class Impl;
Expand Down
6 changes: 4 additions & 2 deletions Plugins/ExternalTexture/Source/ExternalTexture_Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Babylon::Plugins
uint16_t Height() const { return m_info.Height; }
bgfx::TextureFormat::Enum Format() const { return m_info.Format; }
bool HasMips() const { return m_info.MipLevels != 1; }
uint16_t NumLayers() const { return m_info.NumLayers; }
uint64_t Flags() const { return m_info.Flags; }

void AddHandle(bgfx::TextureHandle handle)
Expand Down Expand Up @@ -62,13 +63,13 @@ namespace Babylon::Plugins
return BGFX_TEXTURE_NONE;
}

void UpdateHandles(uintptr_t ptr)
void UpdateHandles(Graphics::TextureT ptr, std::optional<uint16_t> layerIndex)
{
std::scoped_lock lock{m_mutex};

for (auto handle : m_handles)
{
if (bgfx::overrideInternal(handle, ptr) == 0)
if (bgfx::overrideInternal(handle, uintptr_t(ptr), layerIndex.value_or(0)) == 0)
{
assert(!"Failed to override texture");
}
Expand All @@ -80,6 +81,7 @@ namespace Babylon::Plugins
uint16_t Width{};
uint16_t Height{};
uint16_t MipLevels{};
uint16_t NumLayers{};
bgfx::TextureFormat::Enum Format{bgfx::TextureFormat::Unknown};
uint64_t Flags{};
};
Expand Down
9 changes: 5 additions & 4 deletions Plugins/ExternalTexture/Source/ExternalTexture_D3D11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ namespace Babylon::Plugins
public:
// Implemented in ExternalTexture_Shared.h
Impl(Graphics::TextureT, std::optional<Graphics::TextureFormatT>);
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT>);
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT>, std::optional<uint16_t>);

uintptr_t Ptr() const
Graphics::TextureT Get() const
{
return reinterpret_cast<uintptr_t>(m_ptr.get());
return m_ptr.get();
}

private:
Expand All @@ -185,6 +185,7 @@ namespace Babylon::Plugins
info.Width = static_cast<uint16_t>(desc.Width);
info.Height = static_cast<uint16_t>(desc.Height);
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);
info.NumLayers = static_cast<uint16_t>(desc.ArraySize);

if ((desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0)
{
Expand Down Expand Up @@ -213,7 +214,7 @@ namespace Babylon::Plugins
}
}

void Assign(Graphics::TextureT ptr)
void Set(Graphics::TextureT ptr)
{
m_ptr.copy_from(ptr);
}
Expand Down
9 changes: 5 additions & 4 deletions Plugins/ExternalTexture/Source/ExternalTexture_D3D12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ namespace Babylon::Plugins
public:
// Implemented in ExternalTexture_Shared.h
Impl(Graphics::TextureT, std::optional<Graphics::TextureFormatT>);
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT>);
void Update(Graphics::TextureT, std::optional<Graphics::TextureFormatT>, std::optional<uint16_t>);

uintptr_t Ptr() const
Graphics::TextureT Get() const
{
return reinterpret_cast<uintptr_t>(m_ptr.get());
return m_ptr.get();
}

private:
Expand All @@ -179,6 +179,7 @@ namespace Babylon::Plugins
info.Width = static_cast<uint16_t>(desc.Width);
info.Height = static_cast<uint16_t>(desc.Height);
info.MipLevels = static_cast<uint16_t>(desc.MipLevels);
info.NumLayers = static_cast<uint16_t>(desc.DepthOrArraySize);

if ((desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET) != 0)
{
Expand Down Expand Up @@ -207,7 +208,7 @@ namespace Babylon::Plugins
}
}

void Assign(Graphics::TextureT ptr)
void Set(Graphics::TextureT ptr)
{
m_ptr.copy_from(ptr);
}
Expand Down
Loading