diff options
author | Jason Fielder <jason_apple> | 2022-03-22 14:38:28 +0300 |
---|---|---|
committer | Clément Foucault <foucault.clem@gmail.com> | 2022-03-22 14:54:34 +0300 |
commit | 309ea314858a9b7892ea2c8a6fe55ab2a1028697 (patch) | |
tree | ad153ceb5e7e90c5cac65b59b99bb2d708577dba /source/blender/gpu/intern/gpu_context.cc | |
parent | 913b6b9ec1b1f3cf83b21956b06e2f6d3341c78c (diff) |
Metal: Initial Implementation of Metal Backend for GPU Module.
Adding WITH_METAL option to CMAKE to guard compilation for macOS only. Implemented stub METALBackend to mirror GPUBackend interface and added capabilities initialisation, along with API initialisation paths.
Global rendering coordination commands added to backend with GPU_render_begin and GPU_render_end() commands globally wrapping GPU work. This is required for Metal to ensure temporary resources are generated within an NSAutoReleasePool and freed accordingly.
Authored by Apple: Michael Parkin-White, Vil Harvey, Marco Giordano, Michael Jones, Morteza Mostajabodaveh, Jason Fielder
Ref T96261
Reviewed By: fclem
Maniphest Tasks: T96261
Differential Revision: https://developer.blender.org/D14293
Diffstat (limited to 'source/blender/gpu/intern/gpu_context.cc')
-rw-r--r-- | source/blender/gpu/intern/gpu_context.cc | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/source/blender/gpu/intern/gpu_context.cc b/source/blender/gpu/intern/gpu_context.cc index 60e95e09a99..5b40d3850a5 100644 --- a/source/blender/gpu/intern/gpu_context.cc +++ b/source/blender/gpu/intern/gpu_context.cc @@ -13,7 +13,9 @@ */ /* TODO: Create cmake option. */ -#define WITH_OPENGL_BACKEND 1 +#if WITH_OPENGL + #define WITH_OPENGL_BACKEND 1 +#endif #include "BLI_assert.h" #include "BLI_utildefines.h" @@ -32,6 +34,9 @@ # include "gl_backend.hh" # include "gl_context.hh" #endif +#ifdef WITH_METAL_BACKEND +# include "mtl_backend.hh" +#endif #include <mutex> #include <vector> @@ -141,21 +146,73 @@ void GPU_context_main_unlock() /** \} */ /* -------------------------------------------------------------------- */ +/** \name GPU Begin/end work blocks + * + * Used to explicitly define a per-frame block within which GPU work will happen. + * Used for global autoreleasepool flushing in Metal + * \{ */ + +void GPU_render_begin() { + GPUBackend* backend = GPUBackend::get(); + BLI_assert(backend); + backend->render_begin(); +} +void GPU_render_end() { + GPUBackend* backend = GPUBackend::get(); + BLI_assert(backend); + backend->render_end(); +} +void GPU_render_step() { + GPUBackend* backend = GPUBackend::get(); + BLI_assert(backend); + backend->render_step(); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ /** \name Backend selection * \{ */ static GPUBackend *g_backend; +bool GPU_backend_supported(eGPUBackendType type) +{ + switch (type) { + case GPU_BACKEND_OPENGL: +#ifdef WITH_OPENGL_BACKEND + return true; +#else + return false; +#endif + case GPU_BACKEND_METAL: +#ifdef WITH_METAL_BACKEND + return MTLBackend::metal_is_supported(); +#else + return false; +#endif + default: + BLI_assert(false && "No backend specified"); + return false; + } +} + void GPU_backend_init(eGPUBackendType backend_type) { BLI_assert(g_backend == nullptr); + BLI_assert(GPU_backend_supported(backend_type)); switch (backend_type) { -#if WITH_OPENGL_BACKEND +#ifdef WITH_OPENGL_BACKEND case GPU_BACKEND_OPENGL: g_backend = new GLBackend; break; #endif +#ifdef WITH_METAL_BACKEND + case GPU_BACKEND_METAL: + g_backend = new MTLBackend; + break; +#endif default: BLI_assert(0); break; @@ -172,9 +229,18 @@ void GPU_backend_exit() eGPUBackendType GPU_backend_get_type() { + +#ifdef WITH_OPENGL_BACKEND if (g_backend && dynamic_cast<GLBackend *>(g_backend) != nullptr) { return GPU_BACKEND_OPENGL; } +#endif + +#ifdef WITH_METAL_BACKEND + if (g_backend && dynamic_cast<MTLBackend *>(g_backend) != nullptr) { + return GPU_BACKEND_METAL; + } +#endif return GPU_BACKEND_NONE; } |