Blender V5.3
mtl_uniform_buffer.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_global.hh"
10
11#include "BLI_string.hh"
12
13#include "gpu_backend.hh"
15
16#include "mtl_backend.hh"
17#include "mtl_context.hh"
18#include "mtl_debug.hh"
19#include "mtl_storage_buffer.hh"
20#include "mtl_uniform_buffer.hh"
21
22namespace blender::gpu {
23
24MTLUniformBuf::MTLUniformBuf(size_t size, const char *name) : UniformBuf(size, name) {}
25
27{
28 if (metal_buffer_ != nullptr) {
29 metal_buffer_->free();
30 metal_buffer_ = nullptr;
31 }
32
33 /* Ensure UBO is not bound to active CTX.
34 * UBO bindings are reset upon Context-switch so we do not need
35 * to check deactivated context's. */
37 if (ctx) {
39 if (slot.bound && slot.ubo == this) {
40 slot.bound = false;
41 slot.ubo = nullptr;
42 }
43 }
44 }
45
46 if (ssbo_wrapper_) {
47 delete ssbo_wrapper_;
48 ssbo_wrapper_ = nullptr;
49 }
50}
51
52void MTLUniformBuf::update(const void *data)
53{
54 BLI_assert(this);
56
57 /* Free existing allocation.
58 * The previous UBO resource will be tracked by the memory manager,
59 * in case dependent GPU work is still executing. */
60 if (metal_buffer_ != nullptr) {
61 metal_buffer_->free();
62 metal_buffer_ = nullptr;
63 }
64
65 /* Allocate MTL buffer */
67 BLI_assert(ctx);
68 BLI_assert(ctx->device);
70
71 if (data) {
73 size_in_bytes_, true, data);
74 }
75 else {
77 }
78
79#ifndef NDEBUG
80 static std::atomic<int> global_counter = 0;
81 int index = global_counter.fetch_add(1);
82 metal_buffer_->set_label([NSString stringWithFormat:@"UBO %i %s", index, name_]);
83#endif
84
85 BLI_assert(metal_buffer_ != nullptr);
86 BLI_assert(metal_buffer_->get_metal_buffer() != nil);
87}
88
90{
91 /* TODO(fclem): Avoid another allocation and just do the clear on the GPU if possible. */
92 void *clear_data = calloc(1, size_in_bytes_);
93 this->update(clear_data);
94 free(clear_data);
95}
96
97void MTLUniformBuf::bind(int slot)
98{
99 if (slot < 0) {
100 MTL_LOG_WARNING("Failed to bind UBO %p. uniform location %d invalid.", this, slot);
101 return;
102 }
103
105
106 /* Bind current UBO to active context. */
108 BLI_assert(ctx);
109
110 MTLUniformBufferBinding &ctx_ubo_bind_slot = ctx->pipeline_state.ubo_bindings[slot];
111 ctx_ubo_bind_slot.ubo = this;
112 ctx_ubo_bind_slot.bound = true;
113
114 bind_slot_ = slot;
115 bound_ctx_ = ctx;
116
117 /* Check if we have any deferred data to upload. */
118 if (data_ != nullptr) {
119 this->update(data_);
120 MEM_SAFE_DELETE_VOID(data_);
121 }
122
123 /* Ensure there is at least an empty dummy buffer. */
124 if (metal_buffer_ == nullptr) {
125 this->update(nullptr);
126 }
127}
128
130{
131 if (slot < 0) {
132 MTL_LOG_WARNING("Failed to bind UBO %p as SSBO. uniform location %d invalid.", this, slot);
133 return;
134 }
135
136 /* We need to ensure data is actually allocated if using as an SSBO, as resource may be written
137 * to. */
138 if (metal_buffer_ == nullptr) {
139 /* Check if we have any deferred data to upload. */
140 if (data_ != nullptr) {
141 this->update(data_);
142 MEM_SAFE_DELETE_VOID(data_);
143 }
144 else {
145 this->clear_to_zero();
146 }
147 }
148
149 /* Create MTLStorageBuffer to wrap this resource and use conventional binding. */
150 if (ssbo_wrapper_ == nullptr) {
151 ssbo_wrapper_ = new MTLStorageBuf(this, size_in_bytes_);
152 }
153
154 ssbo_wrapper_->bind(slot);
155}
156
158{
159 /* Unbind in debug mode to validate missing binds.
160 * Otherwise, only perform a full unbind upon destruction
161 * to ensure no lingering references. */
162#ifndef NDEBUG
163 if (true)
164#else
165 if (G.debug & G_DEBUG_GPU)
166#endif
167 {
168 if (bound_ctx_ != nullptr && bind_slot_ > -1) {
169 MTLUniformBufferBinding &ctx_ubo_bind_slot =
170 bound_ctx_->pipeline_state.ubo_bindings[bind_slot_];
171 if (ctx_ubo_bind_slot.bound && ctx_ubo_bind_slot.ubo == this) {
172 ctx_ubo_bind_slot.bound = false;
173 ctx_ubo_bind_slot.ubo = nullptr;
174 }
175 }
176 }
177
178 /* Reset bind index. */
179 bind_slot_ = -1;
180 bound_ctx_ = nullptr;
181}
182
184{
185 BLI_assert(this);
186 if (metal_buffer_ != nullptr) {
187 metal_buffer_->debug_ensure_used();
188 return metal_buffer_->get_metal_buffer();
189 }
190 return nil;
191}
192
194{
195 BLI_assert(this);
196 return size_in_bytes_;
197}
198
199} // namespace blender::gpu
#define UNUSED_VARS_NDEBUG(...)
gpu::MTLBuffer * allocate_with_data(uint64_t size, bool cpu_visible, const void *data=nullptr)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
static MTLContext * get()
MTLContextGlobalShaderPipelineState pipeline_state
static MTLBufferPool * get_global_memory_manager()
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
MTLUniformBuf(size_t size, const char *name)
void bind(int slot) override
void bind_as_ssbo(int slot) override
void update(const void *data) override
UniformBuf(size_t size, const char *name)
#define MTL_MAX_BUFFER_BINDINGS
#define MTL_LOG_WARNING(info,...)
Definition mtl_debug.hh:39
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
Global G
Definition blender.cc:57
BMesh const char void * data
std::array< MTLUniformBufferBinding, MTL_MAX_UBO > ubo_bindings