Blender already had its own copy of OpenSubDiv containing some local fixes and code-style. This code still used gl-calls. This PR updates the calls to use GPU module. This allows us to use OpenSubDiv to be usable on other backends as well. This PR was tested on OpenGL, Vulkan and Metal. Metal can be enabled, but Vulkan requires some API changes to work with loose geometry.  # Considerations **ShaderCreateInfo** intern/opensubdiv now requires access to GPU module. This to create buffers in the correct context and trigger correct dispatches. ShaderCreateInfo is used to construct the shader for cross compilation to Metal/Vulkan. However opensubdiv shader caching structures are still used. **Vertex buffers vs storage buffers** Implementation tries to keep as close to the original OSD implementation. If they used storage buffers for data, we will use GPUStorageBuf. If it uses vertex buffers, we will use gpu::VertBuf. **Evaluator const** The evaluator cannot be const anymore as the GPU module API only allows updating SSBOs when constructing. API could be improved to support updating SSBOs. Current implementation has a change to use reads out of bounds when constructing SSBOs. An API change is in the planning to remove this issue. This will be fixed in an upcoming PR. We wanted to land this PR as the visibility of the issue is not common and multiple other changes rely on this PR to land. Pull Request: https://projects.blender.org/blender/blender/pulls/135296
61 lines
2.3 KiB
C++
61 lines
2.3 KiB
C++
/* SPDX-FileCopyrightText: 2021 Blender Foundation
|
|
*
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
|
*
|
|
* Author: Sergey Sharybin. */
|
|
|
|
#include "internal/evaluator/eval_output_gpu.h"
|
|
|
|
#include "opensubdiv_evaluator.hh"
|
|
|
|
#include "gpu_patch_table.hh"
|
|
|
|
using OpenSubdiv::Osd::PatchArray;
|
|
using OpenSubdiv::Osd::PatchArrayVector;
|
|
|
|
namespace blender::opensubdiv {
|
|
|
|
static GPUStorageBuf *create_patch_array_buffer(const PatchArrayVector &patch_arrays)
|
|
{
|
|
const size_t patch_array_size = sizeof(PatchArray);
|
|
const size_t patch_array_byte_site = patch_array_size * patch_arrays.size();
|
|
const size_t patch_array_alloc_size = (patch_array_byte_site + 15) & ~0b1111;
|
|
// TODO: potential read out of bounds.
|
|
GPUStorageBuf *storage_buf = GPU_storagebuf_create_ex(
|
|
patch_array_alloc_size, patch_arrays.data(), GPU_USAGE_STATIC, "osd_patch_array");
|
|
return storage_buf;
|
|
}
|
|
|
|
GpuEvalOutput::GpuEvalOutput(const StencilTable *vertex_stencils,
|
|
const StencilTable *varying_stencils,
|
|
const std::vector<const StencilTable *> &all_face_varying_stencils,
|
|
const int face_varying_width,
|
|
const PatchTable *patch_table,
|
|
VolatileEvalOutput::EvaluatorCache *evaluator_cache)
|
|
: VolatileEvalOutput<GPUVertexBuffer,
|
|
GPUVertexBuffer,
|
|
GPUStencilTableSSBO,
|
|
GPUPatchTable,
|
|
GPUComputeEvaluator>(vertex_stencils,
|
|
varying_stencils,
|
|
all_face_varying_stencils,
|
|
face_varying_width,
|
|
patch_table,
|
|
evaluator_cache)
|
|
{
|
|
}
|
|
|
|
GPUStorageBuf *GpuEvalOutput::create_patch_arrays_buf()
|
|
{
|
|
GPUPatchTable *patch_table = getPatchTable();
|
|
return create_patch_array_buffer(patch_table->GetPatchArrays());
|
|
}
|
|
|
|
GPUStorageBuf *GpuEvalOutput::create_face_varying_patch_array_buf(const int face_varying_channel)
|
|
{
|
|
GPUPatchTable *patch_table = getFVarPatchTable(face_varying_channel);
|
|
return create_patch_array_buffer(patch_table->GetFVarPatchArrays(face_varying_channel));
|
|
}
|
|
|
|
} // namespace blender::opensubdiv
|