Skip to content

Commit

Permalink
Added config object to engine initialization (google#2057) (google#5470)
Browse files Browse the repository at this point in the history
  • Loading branch information
BStringhamVRSK committed May 11, 2022
1 parent a572a10 commit 13d14fe
Show file tree
Hide file tree
Showing 40 changed files with 274 additions and 110 deletions.
16 changes: 15 additions & 1 deletion filament/backend/include/backend/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class UTILS_PUBLIC Platform {
uintptr_t image = 0;
};

class DriverConfig {
private:
size_t mHandleArenaSize; // size of handle arena in bytes

public:
DriverConfig(const DriverConfig* srcConfig);
DriverConfig(uint32_t handleArenaSizeInMB = 0);

const size_t getHandleArenaSize() const noexcept { return mHandleArenaSize; }

private:
void init(uint32_t handleArenaSizeInMB);
};

virtual ~Platform() noexcept;

/**
Expand All @@ -56,7 +70,7 @@ class UTILS_PUBLIC Platform {
*
* @return nullptr on failure, or a pointer to the newly created driver.
*/
virtual backend::Driver* createDriver(void* sharedContext) noexcept = 0;
virtual backend::Driver* createDriver(void* sharedContext, const DriverConfig& driverConfig) noexcept = 0;

/**
* Processes the platform's event queue when called from its primary event-handling thread.
Expand Down
4 changes: 0 additions & 4 deletions filament/backend/include/private/backend/BackendUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@

#include <stddef.h>

#ifndef FILAMENT_MIN_COMMAND_BUFFERS_SIZE_IN_MB
# define FILAMENT_MIN_COMMAND_BUFFERS_SIZE_IN_MB 1
#endif

namespace filament {
namespace backend {

Expand Down
2 changes: 1 addition & 1 deletion filament/backend/include/private/backend/MetalPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class MetalPlatform : public DefaultPlatform {
public:
~MetalPlatform() override;

Driver* createDriver(void* sharedContext) noexcept override;
Driver* createDriver(void* sharedContext, const Platform::DriverConfig& driverConfig) noexcept override;
int getOSVersion() const noexcept override { return 0; }

/**
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/include/private/backend/OpenGLPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class OpenGLPlatform : public DefaultPlatform {
* Derived classes can use this to instantiate the default OpenGLDriver backend.
* This is typically called from your implementation of createDriver()
*/
static Driver* createDefaultDriver(OpenGLPlatform* platform, void* sharedContext);
static Driver* createDefaultDriver(OpenGLPlatform* platform, void* sharedContext, const DriverConfig& driverConfig);

public:
~OpenGLPlatform() noexcept override;
Expand Down
21 changes: 21 additions & 0 deletions filament/backend/src/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,32 @@ filament::backend::DefaultPlatform* createDefaultMetalPlatform();
#include "noop/PlatformNoop.h"

namespace filament {

namespace backend {

// this generates the vtable in this translation unit
Platform::~Platform() noexcept = default;

Platform::DriverConfig::DriverConfig(const DriverConfig* srcConfig) {
if (srcConfig != nullptr) {
*this = *srcConfig;
}
else {
init(0);
}
}

Platform::DriverConfig::DriverConfig(uint32_t handleArenaSizeInMB)
{
init(handleArenaSizeInMB);
}

void Platform::DriverConfig::init(uint32_t handleArenaSizeInMB) {
constexpr size_t MB = 1024 * 1024;

mHandleArenaSize = handleArenaSizeInMB * MB;
}

// Creates the platform-specific Platform object. The caller takes ownership and is
// responsible for destroying it. Initialization of the backend API is deferred until
// createDriver(). The passed-in backend hint is replaced with the resolved backend.
Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/metal/MetalDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ struct UniformBufferState;
#endif

class MetalDriver final : public DriverBase {
explicit MetalDriver(MetalPlatform* platform) noexcept;
explicit MetalDriver(MetalPlatform* platform, const Platform::DriverConfig& driverConfig) noexcept;
~MetalDriver() noexcept override;
Dispatcher getDispatcher() const noexcept final;

public:
static Driver* create(MetalPlatform* platform);
static Driver* create(MetalPlatform* platform, const Platform::DriverConfig& driverConfig);
static size_t getHandleArenaSize(const Platform::DriverConfig& driverConfig) noexcept;

private:

Expand Down
18 changes: 12 additions & 6 deletions filament/backend/src/metal/MetalDriver.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,30 @@
namespace filament {
namespace backend {

Driver* MetalDriverFactory::create(MetalPlatform* const platform) {
return MetalDriver::create(platform);
Driver* MetalDriverFactory::create(MetalPlatform* const platform, const Platform::DriverConfig& driverConfig) {
return MetalDriver::create(platform, driverConfig);
}

UTILS_NOINLINE
Driver* MetalDriver::create(MetalPlatform* const platform) {
Driver* MetalDriver::create(MetalPlatform* const platform, const Platform::DriverConfig& driverConfig) {
assert_invariant(platform);
return new MetalDriver(platform);
return new MetalDriver(platform, driverConfig);
}

size_t MetalDriver::getHandleArenaSize(const Platform::DriverConfig& driverConfig) noexcept {
size_t configSize = driverConfig.getHandleArenaSize();
size_t defaultSize = FILAMENT_METAL_HANDLE_ARENA_SIZE_IN_MB * 1024U * 1024U;
return configSize > defaultSize ? configSize : defaultSize;
}

Dispatcher MetalDriver::getDispatcher() const noexcept {
return ConcreteDispatcher<MetalDriver>::make();
}

MetalDriver::MetalDriver(MetalPlatform* platform) noexcept
MetalDriver::MetalDriver(MetalPlatform* platform, const Platform::DriverConfig& driverConfig) noexcept
: mPlatform(*platform),
mContext(new MetalContext),
mHandleAllocator("Handles", FILAMENT_METAL_HANDLE_ARENA_SIZE_IN_MB * 1024U * 1024U) {
mHandleAllocator("Handles", getHandleArenaSize(driverConfig)) {
mContext->driver = this;

mContext->device = mPlatform.createDevice();
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/metal/MetalDriverFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Driver;

class MetalDriverFactory {
public:
static Driver* create(MetalPlatform* platform);
static Driver* create(MetalPlatform* platform, const Platform::DriverConfig& driverConfig);
};

} // namespace backend
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/metal/MetalPlatform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@

MetalPlatform::~MetalPlatform() = default;

Driver* MetalPlatform::createDriver(void* sharedContext) noexcept {
return MetalDriverFactory::create(this);
Driver* MetalPlatform::createDriver(void* sharedContext, const Platform::DriverConfig& driverConfig) noexcept {
return MetalDriverFactory::create(this, driverConfig);
}

id<MTLDevice> MetalPlatform::createDevice() noexcept {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/noop/PlatformNoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace filament::backend {

Driver* PlatformNoop::createDriver(void* const sharedGLContext) noexcept {
Driver* PlatformNoop::createDriver(void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept {
return NoopDriver::create();
}

Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/noop/PlatformNoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class PlatformNoop final : public DefaultPlatform {

protected:

Driver* createDriver(void* sharedContext) noexcept override;
Driver* createDriver(void* sharedContext, const Platform::DriverConfig& driverConfig) noexcept override;
};

} // namespace filament
Expand Down
19 changes: 13 additions & 6 deletions filament/backend/src/opengl/OpenGLDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ using namespace utils;
namespace filament::backend {

Driver* OpenGLDriverFactory::create(
OpenGLPlatform* const platform, void* const sharedGLContext) noexcept {
return OpenGLDriver::create(platform, sharedGLContext);
OpenGLPlatform* const platform, void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept {
return OpenGLDriver::create(platform, sharedGLContext, driverConfig);
}

using namespace GLUtils;
Expand All @@ -73,7 +73,7 @@ using namespace GLUtils;

UTILS_NOINLINE
Driver* OpenGLDriver::create(
OpenGLPlatform* const platform, void* const sharedGLContext) noexcept {
OpenGLPlatform* const platform, void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept {
assert_invariant(platform);
OpenGLPlatform* const ec = platform;

Expand Down Expand Up @@ -136,10 +136,17 @@ Driver* OpenGLDriver::create(
}
}

OpenGLDriver* const driver = new OpenGLDriver(ec);
OpenGLDriver* const driver = new OpenGLDriver(ec, driverConfig);
return driver;
}

size_t OpenGLDriver::getHandleArenaSize(const Platform::DriverConfig& driverConfig) noexcept {
size_t configSize = driverConfig.getHandleArenaSize();
size_t defaultSize = FILAMENT_OPENGL_HANDLE_ARENA_SIZE_IN_MB * 1024U * 1024U;
return configSize > defaultSize ? configSize : defaultSize;
}


// ------------------------------------------------------------------------------------------------

OpenGLDriver::DebugMarker::DebugMarker(OpenGLDriver& driver, const char* string) noexcept
Expand All @@ -153,8 +160,8 @@ OpenGLDriver::DebugMarker::~DebugMarker() noexcept {

// ------------------------------------------------------------------------------------------------

OpenGLDriver::OpenGLDriver(OpenGLPlatform* platform) noexcept
: mHandleAllocator("Handles", FILAMENT_OPENGL_HANDLE_ARENA_SIZE_IN_MB * 1024U * 1024U), // TODO: set the amount in configuration
OpenGLDriver::OpenGLDriver(OpenGLPlatform* platform, const Platform::DriverConfig& driverConfig) noexcept
: mHandleAllocator("Handles", getHandleArenaSize(driverConfig)),
mSamplerMap(32),
mPlatform(*platform) {

Expand Down
5 changes: 3 additions & 2 deletions filament/backend/src/opengl/OpenGLDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ class OpenGLBlitter;
class OpenGLTimerQueryInterface;

class OpenGLDriver final : public DriverBase {
inline explicit OpenGLDriver(OpenGLPlatform* platform) noexcept;
inline explicit OpenGLDriver(OpenGLPlatform* platform, const Platform::DriverConfig& driverConfig) noexcept;
~OpenGLDriver() noexcept final;
Dispatcher getDispatcher() const noexcept final;

public:
static Driver* create(OpenGLPlatform* platform, void* sharedGLContext) noexcept;
static Driver* create(OpenGLPlatform* platform, void* sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept;
static size_t getHandleArenaSize(const Platform::DriverConfig& driverConfig) noexcept;

class DebugMarker {
OpenGLDriver& driver;
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/opengl/OpenGLDriverFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Driver;

class OpenGLDriverFactory {
public:
static Driver* create(OpenGLPlatform* platform, void* sharedGLContext) noexcept;
static Driver* create(OpenGLPlatform* platform, void* sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept;
};

} // namespace filament::backend
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/opengl/OpenGLPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ namespace filament::backend {

OpenGLPlatform::~OpenGLPlatform() noexcept = default;

Driver* OpenGLPlatform::createDefaultDriver(OpenGLPlatform* platform, void* sharedContext) {
return OpenGLDriverFactory::create(platform, sharedContext);
Driver* OpenGLPlatform::createDefaultDriver(OpenGLPlatform* platform, void* sharedContext, const Platform::DriverConfig& driverConfig) {
return OpenGLDriverFactory::create(platform, sharedContext, driverConfig);
}

} // namespace filament::backend
4 changes: 2 additions & 2 deletions filament/backend/src/opengl/platforms/PlatformWGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ struct WGLSwapChain {
bool isHeadless = false;
};

Driver* PlatformWGL::createDriver(void* const sharedGLContext) noexcept {
Driver* PlatformWGL::createDriver(void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept {
int result = 0;
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribs = nullptr;
int pixelFormat = 0;
Expand Down Expand Up @@ -147,7 +147,7 @@ Driver* PlatformWGL::createDriver(void* const sharedGLContext) noexcept {

result = bluegl::bind();
ASSERT_POSTCONDITION(!result, "Unable to load OpenGL entry points.");
return OpenGLDriverFactory::create(this, sharedGLContext);
return OpenGLDriverFactory::create(this, sharedGLContext, driverConfig);

error:
if (tempContext) {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/opengl/platforms/PlatformWGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace filament::backend {

class PlatformWGL final : public OpenGLPlatform {
public:
Driver* createDriver(void* const sharedGLContext) noexcept override;
Driver* createDriver(void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept override;
void terminate() noexcept override;

SwapChain* createSwapChain(void* nativewindow, uint64_t& flags) noexcept override;
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/opengl/platforms/PlatformWebGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ namespace filament::backend {

using namespace backend;

Driver* PlatformWebGL::createDriver(void* const sharedGLContext) noexcept {
return OpenGLDriverFactory::create(this, sharedGLContext);
Driver* PlatformWebGL::createDriver(void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept {
return OpenGLDriverFactory::create(this, sharedGLContext, driverConfig);
}

void PlatformWebGL::terminate() noexcept {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/opengl/platforms/PlatformWebGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace filament::backend {
class PlatformWebGL final : public OpenGLPlatform {
public:

Driver* createDriver(void* const sharedGLContext) noexcept override;
Driver* createDriver(void* const sharedGLContext, const Platform::DriverConfig& driverConfig) noexcept override;
void terminate() noexcept override;

SwapChain* createSwapChain(void* nativewindow, uint64_t& flags) noexcept final override;
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/PlatformVkAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ using namespace bluevk;

namespace filament::backend {

Driver* PlatformVkAndroid::createDriver(void* const sharedContext) noexcept {
Driver* PlatformVkAndroid::createDriver(void* const sharedContext, const Platform::DriverConfig& driverConfig) noexcept {
ASSERT_PRECONDITION(sharedContext == nullptr, "Vulkan does not support shared contexts.");
static const char* requiredInstanceExtensions[] = { "VK_KHR_android_surface" };
return VulkanDriverFactory::create(this, requiredInstanceExtensions, 1);
return VulkanDriverFactory::create(this, requiredInstanceExtensions, 1, driverConfig);
}

void* PlatformVkAndroid::createVkSurfaceKHR(void* nativeWindow, void* vkinstance, uint64_t flags) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/vulkan/PlatformVkAndroid.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace filament::backend {
class PlatformVkAndroid final : public VulkanPlatform {
public:

Driver* createDriver(void* const sharedContext) noexcept override;
Driver* createDriver(void* const sharedContext, const Platform::DriverConfig& driverConfig) noexcept override;

void* createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept override;

Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/vulkan/PlatformVkCocoa.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace filament::backend {

class PlatformVkCocoa final : public VulkanPlatform {
public:
Driver* createDriver(void* sharedContext) noexcept override;
Driver* createDriver(void* sharedContext, const Platform::DriverConfig& driverConfig) noexcept override;
void* createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept override;
int getOSVersion() const noexcept override { return 0; }
};
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/PlatformVkCocoa.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@

namespace filament::backend {

Driver* PlatformVkCocoa::createDriver(void* sharedContext) noexcept {
Driver* PlatformVkCocoa::createDriver(void* sharedContext, const Platform::DriverConfig& driverConfig) noexcept {
ASSERT_PRECONDITION(sharedContext == nullptr, "Vulkan does not support shared contexts.");
static const char* requiredInstanceExtensions[] = {
"VK_MVK_macos_surface", // TODO: replace with VK_EXT_metal_surface
};
return VulkanDriverFactory::create(this, requiredInstanceExtensions, 1);
return VulkanDriverFactory::create(this, requiredInstanceExtensions, 1, driverConfig);
}

void* PlatformVkCocoa::createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion filament/backend/src/vulkan/PlatformVkCocoaTouch.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace filament::backend {

class PlatformVkCocoaTouch final : public VulkanPlatform {
public:
Driver* createDriver(void* const sharedContext) noexcept override;
Driver* createDriver(void* const sharedContext, const Platform::DriverConfig& driverConfig) noexcept override;
void* createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept override;
int getOSVersion() const noexcept override { return 0; }
};
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/PlatformVkCocoaTouch.mm
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

namespace filament::backend {

Driver* PlatformVkCocoaTouch::createDriver(void* const sharedContext) noexcept {
Driver* PlatformVkCocoaTouch::createDriver(void* const sharedContext, const Platform::DriverConfig& driverConfig) noexcept {
ASSERT_PRECONDITION(sharedContext == nullptr, "Vulkan does not support shared contexts.");
static const char* requestedExtensions[] = {"VK_MVK_ios_surface"};
return VulkanDriverFactory::create(this, requestedExtensions, 1);
return VulkanDriverFactory::create(this, requestedExtensions, 1, driverConfig);
}

void* PlatformVkCocoaTouch::createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept {
Expand Down
4 changes: 2 additions & 2 deletions filament/backend/src/vulkan/PlatformVkLinuxWayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ using namespace bluevk;

namespace filament::backend {

Driver* PlatformVkLinuxWayland::createDriver(void* const sharedContext) noexcept {
Driver* PlatformVkLinuxWayland::createDriver(void* const sharedContext, const Platform::DriverConfig& driverConfig) noexcept {
ASSERT_PRECONDITION(sharedContext == nullptr, "Vulkan does not support shared contexts.");
const char* requiredInstanceExtensions[] = {
"VK_KHR_wayland_surface",
};
return VulkanDriverFactory::create(this, requiredInstanceExtensions,
sizeof(requiredInstanceExtensions) / sizeof(requiredInstanceExtensions[0]));
sizeof(requiredInstanceExtensions) / sizeof(requiredInstanceExtensions[0]), driverConfig);
}

void* PlatformVkLinuxWayland::createVkSurfaceKHR(void* nativeWindow, void* instance, uint64_t flags) noexcept {
Expand Down
Loading

0 comments on commit 13d14fe

Please sign in to comment.