Changeset View
Changeset View
Standalone View
Standalone View
source/blender/gpu/intern/gpu_context.cc
| Show First 20 Lines • Show All 221 Lines • ▼ Show 20 Lines | |||||
| /* -------------------------------------------------------------------- */ | /* -------------------------------------------------------------------- */ | ||||
| /** \name Backend selection | /** \name Backend selection | ||||
| * \{ */ | * \{ */ | ||||
| /* NOTE: To enable Metal API, we need to temporarily change this to `GPU_BACKEND_METAL`. | /* NOTE: To enable Metal API, we need to temporarily change this to `GPU_BACKEND_METAL`. | ||||
| * Until a global switch is added, Metal also needs to be enabled in GHOST_ContextCGL: | * Until a global switch is added, Metal also needs to be enabled in GHOST_ContextCGL: | ||||
| * `m_useMetalForRendering = true`. */ | * `m_useMetalForRendering = true`. */ | ||||
| static eGPUBackendType g_backend_type = GPU_BACKEND_OPENGL; | static eGPUBackendType g_backend_type = GPU_BACKEND_OPENGL; | ||||
| static std::optional<eGPUBackendType> g_backend_type_override = std::nullopt; | |||||
| static std::optional<bool> g_backend_type_supported = std::nullopt; | |||||
| static GPUBackend *g_backend = nullptr; | static GPUBackend *g_backend = nullptr; | ||||
| void GPU_backend_type_selection_set(const eGPUBackendType backend) | void GPU_backend_type_selection_set(const eGPUBackendType backend) | ||||
| { | { | ||||
| g_backend_type = backend; | g_backend_type = backend; | ||||
| g_backend_type_supported = std::nullopt; | |||||
| } | } | ||||
| eGPUBackendType GPU_backend_type_selection_get() | eGPUBackendType GPU_backend_type_selection_get() | ||||
| { | { | ||||
| return g_backend_type; | return g_backend_type; | ||||
| } | } | ||||
| bool GPU_backend_supported(void) | void GPU_backend_type_selection_set_override(const eGPUBackendType backend_type) | ||||
| { | |||||
| g_backend_type_override = backend_type; | |||||
| } | |||||
| bool GPU_backend_type_selection_is_overridden(void) | |||||
| { | |||||
| return g_backend_type_override.has_value(); | |||||
| } | |||||
| bool GPU_backend_type_selection_detect(void) | |||||
| { | |||||
| blender::Vector<eGPUBackendType> backends_to_check; | |||||
| if (GPU_backend_type_selection_is_overridden()) { | |||||
| backends_to_check.append(*g_backend_type_override); | |||||
| } | |||||
| else { | |||||
| backends_to_check.append(GPU_BACKEND_OPENGL); | |||||
| } | |||||
| /* Add fallback to OpenGL when Metal backend is requested on a platform that doens't support | |||||
| * metal. */ | |||||
| if (backends_to_check[0] == GPU_BACKEND_METAL) { | |||||
| backends_to_check.append(GPU_BACKEND_OPENGL); | |||||
| } | |||||
| for (const eGPUBackendType backend_type : backends_to_check) { | |||||
| GPU_backend_type_selection_set(backend_type); | |||||
| if (GPU_backend_supported()) { | |||||
| return true; | |||||
| } | |||||
| } | |||||
| GPU_backend_type_selection_set(GPU_BACKEND_NONE); | |||||
| return false; | |||||
| } | |||||
| static bool gpu_backend_supported() | |||||
| { | { | ||||
| switch (g_backend_type) { | switch (g_backend_type) { | ||||
| case GPU_BACKEND_OPENGL: | case GPU_BACKEND_OPENGL: | ||||
| #ifdef WITH_OPENGL_BACKEND | #ifdef WITH_OPENGL_BACKEND | ||||
| return true; | return true; | ||||
| #else | #else | ||||
| return false; | return false; | ||||
| #endif | #endif | ||||
| Show All 10 Lines | #else | ||||
| return false; | return false; | ||||
| #endif | #endif | ||||
| default: | default: | ||||
| BLI_assert(false && "No backend specified"); | BLI_assert(false && "No backend specified"); | ||||
| return false; | return false; | ||||
| } | } | ||||
| } | } | ||||
| bool GPU_backend_supported() | |||||
| { | |||||
| if (!g_backend_type_supported.has_value()) { | |||||
| g_backend_type_supported = gpu_backend_supported(); | |||||
| } | |||||
| return *g_backend_type_supported; | |||||
| } | |||||
| static void gpu_backend_create() | static void gpu_backend_create() | ||||
| { | { | ||||
| BLI_assert(g_backend == nullptr); | BLI_assert(g_backend == nullptr); | ||||
| BLI_assert(GPU_backend_supported()); | BLI_assert(GPU_backend_supported()); | ||||
| switch (g_backend_type) { | switch (g_backend_type) { | ||||
| #ifdef WITH_OPENGL_BACKEND | #ifdef WITH_OPENGL_BACKEND | ||||
| case GPU_BACKEND_OPENGL: | case GPU_BACKEND_OPENGL: | ||||
| ▲ Show 20 Lines • Show All 62 Lines • Show Last 20 Lines | |||||