Files
test2/intern/opensubdiv/internal/evaluator/gpu_patch_table.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
4.2 KiB
C++
Raw Normal View History

SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
/* SPDX-FileCopyrightText: 2025 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "gpu_patch_table.hh"
#include "opensubdiv/far/patchTable.h"
#include "opensubdiv/osd/cpuPatchTable.h"
using namespace OpenSubdiv::Osd;
namespace blender::opensubdiv {
GPUPatchTable *GPUPatchTable::Create(PatchTable const *far_patch_table, void * /*deviceContext*/)
{
GPUPatchTable *instance = new GPUPatchTable();
if (instance->allocate(far_patch_table))
return instance;
delete instance;
return nullptr;
}
static void discard_buffer(GPUStorageBuf **buffer)
{
if (*buffer != nullptr) {
GPU_storagebuf_free(*buffer);
*buffer = nullptr;
}
}
static void discard_list(std::vector<GPUStorageBuf *> &buffers)
{
while (!buffers.empty()) {
GPUStorageBuf *buffer = buffers.back();
buffers.pop_back();
GPU_storagebuf_free(buffer);
}
}
GPUPatchTable::~GPUPatchTable()
{
discard_buffer(&_patchIndexBuffer);
discard_buffer(&_patchParamBuffer);
discard_buffer(&_varyingIndexBuffer);
discard_list(_fvarIndexBuffers);
discard_list(_fvarParamBuffers);
}
bool GPUPatchTable::allocate(PatchTable const *far_patch_table)
{
CpuPatchTable patch_table(far_patch_table);
/* Patch array */
size_t num_patch_arrays = patch_table.GetNumPatchArrays();
_patchArrays.assign(patch_table.GetPatchArrayBuffer(),
patch_table.GetPatchArrayBuffer() + num_patch_arrays);
/* Patch index buffer */
const size_t index_size = patch_table.GetPatchIndexSize();
_patchIndexBuffer = GPU_storagebuf_create_ex(
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
index_size * sizeof(int32_t),
static_cast<const void *>(patch_table.GetPatchIndexBuffer()),
GPU_USAGE_STATIC,
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
"osd_patch_index");
/* Patch param buffer */
const size_t patch_param_size = patch_table.GetPatchParamSize();
_patchParamBuffer = GPU_storagebuf_create_ex(patch_param_size * sizeof(PatchParam),
patch_table.GetPatchParamBuffer(),
GPU_USAGE_STATIC,
"osd_patch_param");
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
/* Varying patch array */
_varyingPatchArrays.assign(patch_table.GetVaryingPatchArrayBuffer(),
patch_table.GetVaryingPatchArrayBuffer() + num_patch_arrays);
/* Varying index buffer */
_varyingIndexBuffer = GPU_storagebuf_create_ex(patch_table.GetVaryingPatchIndexSize() *
sizeof(uint32_t),
patch_table.GetVaryingPatchIndexBuffer(),
GPU_USAGE_STATIC,
"osd_varying_index");
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
/* Face varying */
const int num_face_varying_channels = patch_table.GetNumFVarChannels();
_fvarPatchArrays.resize(num_face_varying_channels);
_fvarIndexBuffers.resize(num_face_varying_channels);
_fvarParamBuffers.resize(num_face_varying_channels);
for (int index = 0; index < num_face_varying_channels; index++) {
/* Face varying patch arrays */
_fvarPatchArrays[index].assign(patch_table.GetFVarPatchArrayBuffer(),
patch_table.GetFVarPatchArrayBuffer() + num_patch_arrays);
/* Face varying patch index buffer */
_fvarIndexBuffers[index] = GPU_storagebuf_create_ex(patch_table.GetFVarPatchIndexSize(index) *
sizeof(int32_t),
patch_table.GetFVarPatchIndexBuffer(index),
GPU_USAGE_STATIC,
"osd_face_varying_index");
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
/* Face varying patch param buffer */
_fvarParamBuffers[index] = GPU_storagebuf_create_ex(patch_table.GetFVarPatchParamSize(index) *
sizeof(PatchParam),
patch_table.GetFVarPatchParamBuffer(index),
GPU_USAGE_STATIC,
"osd_face_varying_params");
SubDiv: Migrate GPU subdivision to use GPU module 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. ![metal.png](/attachments/bb042c3a-1a87-4140-9958-a80da10d417b) # 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
2025-03-10 07:31:59 +01:00
}
return true;
}
} // namespace blender::opensubdiv