OpenSubdiv: Switch away from GLSL on Apple

Use MTLPatchShaderSource to provide the patch basis shader source on
all Apple platforms. The immediate advantage of this change is ability
to use GPU subdivision on iOS. Another advantage is that it moves us
further away from frameworks which got deprecated by Apple and it might
save us some headache in the future.

Also tweak backend-specific defines to match definitions from OpenSubdiv.
The annoying difference is that OSD_PATCH_BASIS_METAL is defined by the
OpenSubdiv as 1 in the very beginning of the base code, which is not done
for the OSD_PATCH_BASIS_GLSL is not defined by the OpenSubdiv at all.

Ref #143445

---

TODO:
- [X] Check it works correctly on macOS
- [x] Check it works correctly on Linux

Pull Request: https://projects.blender.org/blender/blender/pulls/143462
This commit is contained in:
Sergey Sharybin
2025-07-28 16:54:10 +02:00
committed by Sergey Sharybin
parent 63954843c2
commit 07bf1bd87b
3 changed files with 24 additions and 5 deletions

View File

@@ -6,7 +6,11 @@
#include "opensubdiv_evaluator_capi.hh"
#include <opensubdiv/osd/glslPatchShaderSource.h>
#ifdef __APPLE__
# include <opensubdiv/osd/mtlPatchShaderSource.h>
#else
# include <opensubdiv/osd/glslPatchShaderSource.h>
#endif
#include "MEM_guardedalloc.h"
@@ -34,7 +38,11 @@ const char *openSubdiv_getGLSLPatchBasisSource()
/* Using a global string to avoid dealing with memory allocation/ownership. */
static std::string patch_basis_source;
if (patch_basis_source.empty()) {
#ifdef __APPLE__
patch_basis_source = OpenSubdiv::Osd::MTLPatchShaderSource::GetPatchBasisShaderSource();
#else
patch_basis_source = OpenSubdiv::Osd::GLSLPatchShaderSource::GetPatchBasisShaderSource();
#endif
}
return patch_basis_source.c_str();
}

View File

@@ -9,7 +9,6 @@
#include <opensubdiv/far/error.h>
#include <opensubdiv/far/patchDescriptor.h>
#include <opensubdiv/far/stencilTable.h>
#include <opensubdiv/osd/glslPatchShaderSource.h>
#include <cassert>
#include <cmath>
@@ -336,8 +335,13 @@ static GPUShader *compile_eval_stencil_shader(BufferDescriptor const &srcDesc,
using namespace blender::gpu::shader;
ShaderCreateInfo info("opensubdiv_compute_eval");
info.local_group_size(workGroupSize, 1, 1);
/* Ensure the basis code has access to proper backend specification define: it is not guaranteed
* that the code provided by OpenSubdiv specifies it. For example, it doesn't for GLSL but it
* does for Metal. Additionally, for Metal OpenSubdiv defines OSD_PATCH_BASIS_METAL as 1, so do
* the same here to avoid possible warning about value being re-defined. */
if (GPU_backend_get_type() == GPU_BACKEND_METAL) {
info.define("OSD_PATCH_BASIS_METAL");
info.define("OSD_PATCH_BASIS_METAL", "1");
}
else {
info.define("OSD_PATCH_BASIS_GLSL");
@@ -438,8 +442,13 @@ static GPUShader *compile_eval_patches_shader(BufferDescriptor const &srcDesc,
using namespace blender::gpu::shader;
ShaderCreateInfo info("opensubdiv_compute_eval");
info.local_group_size(workGroupSize, 1, 1);
/* Ensure the basis code has access to proper backend specification define: it is not guaranteed
* that the code provided by OpenSubdiv specifies it. For example, it doesn't for GLSL but it
* does for Metal. Additionally, for Metal OpenSubdiv defines OSD_PATCH_BASIS_METAL as 1, so do
* the same here to avoid possible warning about value being re-defined. */
if (GPU_backend_get_type() == GPU_BACKEND_METAL) {
info.define("OSD_PATCH_BASIS_METAL");
info.define("OSD_PATCH_BASIS_METAL", "1");
}
else {
info.define("OSD_PATCH_BASIS_GLSL");