Blender V5.3
mtl_command_buffer.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "mtl_capabilities.hh"
12#include "mtl_common.hh"
13#include "mtl_debug.hh"
14#include "mtl_memory.hh"
15#include "mtl_texture.hh"
16
17/* Metal profiling tools complain about redundant bindings. Using our own tracking mechanism we can
18 * avoid these redundant binds. Set to 0 to turn off this feature. */
19#define MTL_ENABLE_REDUNDANT_BINDING_OPTIMIZATION 1
20/* Avoid using the offset only update and force rebind even if buffer is the same. */
21#define MTL_FORCE_BUFFER_REBIND 0
22
23namespace blender::gpu {
24
25/* Combined sampler state configuration for Argument Buffer caching. */
28 /* MTLSamplerState permutations between 0..256 - slightly more than a byte. */
31
32 bool operator==(const MTLSamplerArray &other) const
33 {
34 if (this->num_samplers != other.num_samplers) {
35 return false;
36 }
37 return (memcmp(this->mtl_sampler_flags,
39 sizeof(MTLSamplerState) * this->num_samplers) == 0);
40 }
41
42 uint32_t hash() const
43 {
44 uint32_t hash = this->num_samplers;
45 for (int i = 0; i < this->num_samplers; i++) {
46 hash ^= uint32_t(this->mtl_sampler_flags[i]) << (i % 3);
47 }
48 return hash;
49 }
50};
51
52/* Structs containing information on current binding state for textures and samplers. */
56
67
68/* Caching of CommandEncoder Vertex/Fragment buffer bindings. */
73
74/* Caching of CommandEncoder textures bindings. */
78
79/* Cached of CommandEncoder sampler states. */
85
86/* Thin wrappers over to allow overloading of objective-C methods that have different names per
87 * encoder. */
88
91
93
94 void set_buffer_offset(size_t offset, int index);
95 void set_buffer(id<MTLBuffer> buf, size_t offset, int index);
96 void set_bytes(const void *bytes, size_t length, int index);
97 void set_texture(id<MTLTexture> tex, int index);
98 void set_sampler(id<MTLSamplerState> sampler_state, int index);
100 void use_resource(id<MTLResource> resource, MTLResourceUsage usage);
101};
102
105
107
108 void set_buffer_offset(size_t offset, int index);
109 void set_buffer(id<MTLBuffer> buf, size_t offset, int index);
110 void set_bytes(const void *bytes, size_t length, int index);
111 void set_texture(id<MTLTexture> tex, int index);
112 void set_sampler(id<MTLSamplerState> sampler_state, int index);
114 void use_resource(id<MTLResource> resource, MTLResourceUsage usage);
115};
116
119
121
122 void set_buffer_offset(size_t offset, int index);
123 void set_buffer(id<MTLBuffer> buf, size_t offset, int index);
124 void set_bytes(const void *bytes, size_t length, int index);
125 void set_texture(id<MTLTexture> tex, int index);
126 void set_sampler(id<MTLSamplerState> sampler_state, int index);
128 void use_resource(id<MTLResource> resource, MTLResourceUsage usage);
129};
130
131/* Class to remove redundant resource bindings. */
132template<typename CommandEncoderT> struct MTLBindingCache {
133 /* Indexed by final backend bindings, not by shader interface bindings. */
134 std::array<MTLBufferBindingCached, MTL_MAX_BUFFER_BINDINGS> buffer_bindings = {};
135 std::array<MTLTextureBindingCached, MTL_MAX_TEXTURE_SLOTS> texture_bindings = {};
136 std::array<MTLSamplerStateBindingCached, MTL_MAX_TEXTURE_SLOTS> sampler_state_bindings = {};
137
138 void bind_buffer(CommandEncoderT enc, id<MTLBuffer> buf, size_t offset, uint index);
139 void bind_bytes(CommandEncoderT enc,
140 MTLScratchBufferManager &scratch_buffer,
141 const void *bytes,
142 size_t length,
143 uint index);
144 void bind_texture(CommandEncoderT enc, id<MTLTexture> tex, uint index);
145 void bind_sampler(CommandEncoderT enc,
146 gpu::MTLSamplerArray &sampler_array,
147 id<MTLSamplerState> sampler_state,
148 gpu::MTLSamplerState binding_state,
149 bool use_samplers_argument_buffer,
150 uint index);
151};
152
153template<typename CommandEncoderT>
155 id<MTLBuffer> buf,
156 size_t offset,
157 uint index)
158{
159 BLI_assert(buf != nil);
160 BLI_assert(index >= 0);
163
164#if MTL_ENABLE_REDUNDANT_BINDING_OPTIMIZATION
165 if (binding.metal_buffer == buf && binding.offset == offset) {
166 return;
167 }
168#endif
169
170 if (binding.metal_buffer == buf && MTL_FORCE_BUFFER_REBIND == 0) {
171 enc.set_buffer_offset(offset, index);
172 }
173 else {
174 enc.set_buffer(buf, offset, index);
175 }
176
177 binding.metal_buffer = buf;
178 binding.offset = offset;
179}
180
181template<typename CommandEncoderT>
183 MTLScratchBufferManager &scratch_buffer,
184 const void *bytes,
185 size_t length,
186 uint index)
187{
188 /* Bytes are always updated as source data may have changed. */
190 BLI_assert(length > 0);
191 BLI_assert(bytes != nullptr);
193
195 /* We have run over the setBytes limit, bind buffer instead. */
197 memcpy(range.data, bytes, length);
198 this->bind_buffer(enc, range.metal_buffer, range.buffer_offset, index);
199 return;
200 }
201
202 enc.set_bytes(bytes, length, index);
203
204 binding.metal_buffer = nil;
205 binding.offset = -1;
206}
207
208template<typename CommandEncoderT>
210 id<MTLTexture> tex,
211 uint index)
212{
213 BLI_assert(tex != nil);
214 BLI_assert(index >= 0);
217
218#if MTL_ENABLE_REDUNDANT_BINDING_OPTIMIZATION
219 if (binding.metal_texture == tex) {
220 return;
221 }
222#endif
223
224 enc.set_texture(tex, index);
225
226 binding.metal_texture = tex;
227}
228
229template<typename CommandEncoderT>
231 gpu::MTLSamplerArray &sampler_array,
232 id<MTLSamplerState> sampler_state,
233 gpu::MTLSamplerState binding_state,
234 bool use_samplers_argument_buffer,
235 uint index)
236{
237 BLI_assert(index >= 0);
240
241#if MTL_ENABLE_REDUNDANT_BINDING_OPTIMIZATION
242 /* If sampler state has not changed for the given slot, we do not need to fetch. */
243 if (binding.sampler_state != nil && binding.binding_state == binding_state &&
244 !use_samplers_argument_buffer)
245 {
246 return;
247 }
248#endif
249
250 /* Flag last binding type. */
251 binding.is_arg_buffer_binding = use_samplers_argument_buffer;
252
253 /* Always assign to argument buffer samplers binding array - Efficiently ensures the value in
254 * the samplers array is always up to date. */
255 sampler_array.mtl_sampler[index] = sampler_state;
256 sampler_array.mtl_sampler_flags[index] = binding_state;
257
258 if (use_samplers_argument_buffer) {
259 /* TODO(fclem): Always updating the argument buffer is something that was done before the huge
260 * Metal backend refactor. Maybe we can move all the update inside this if clause. */
261 return;
262 }
263
264 /* Update binding and cached state. */
265 enc.set_sampler(sampler_state, index);
266
267 binding.binding_state = binding_state;
268 binding.sampler_state = sampler_state;
269}
270
271} // namespace blender::gpu
MTLTemporaryBuffer scratch_buffer_allocate_range_aligned(uint64_t alloc_size, uint alignment)
float length(VecOp< float, D >) RET
#define MTL_MAX_BUFFER_BINDINGS
#define MTL_MAX_TEXTURE_SLOTS
#define MTL_MAX_SET_BYTES_SIZE
#define MTL_FORCE_BUFFER_REBIND
MTLBufferRange MTLTemporaryBuffer
const MTLSamplerState DEFAULT_SAMPLER_STATE
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
unsigned int uint32_t
Definition BKE_text.h:81
void bind_buffer(CommandEncoderT enc, id< MTLBuffer > buf, size_t offset, uint index)
void bind_texture(CommandEncoderT enc, id< MTLTexture > tex, uint index)
std::array< MTLBufferBindingCached, MTL_MAX_BUFFER_BINDINGS > buffer_bindings
void bind_sampler(CommandEncoderT enc, gpu::MTLSamplerArray &sampler_array, id< MTLSamplerState > sampler_state, gpu::MTLSamplerState binding_state, bool use_samplers_argument_buffer, uint index)
std::array< MTLSamplerStateBindingCached, MTL_MAX_TEXTURE_SLOTS > sampler_state_bindings
std::array< MTLTextureBindingCached, MTL_MAX_TEXTURE_SLOTS > texture_bindings
void bind_bytes(CommandEncoderT enc, MTLScratchBufferManager &scratch_buffer, const void *bytes, size_t length, uint index)
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_bytes(const void *bytes, size_t length, int index)
void set_buffer_offset(size_t offset, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void set_texture(id< MTLTexture > tex, int index)
id< MTLComputeCommandEncoder > enc
MTLComputeCommandEncoder(id< MTLComputeCommandEncoder > encoder)
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_texture(id< MTLTexture > tex, int index)
void set_buffer_offset(size_t offset, int index)
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_bytes(const void *bytes, size_t length, int index)
MTLFragmentCommandEncoder(id< MTLRenderCommandEncoder > encoder)
MTLSamplerState mtl_sampler_flags[MTL_MAX_TEXTURE_SLOTS]
bool operator==(const MTLSamplerArray &other) const
id< MTLSamplerState > mtl_sampler[MTL_MAX_TEXTURE_SLOTS]
bool operator==(MTLSamplerBinding const &other) const
id< MTLSamplerState > get_mtl_sampler(MTLContext &ctx)
void set_bytes(const void *bytes, size_t length, int index)
void set_buffer_offset(size_t offset, int index)
void use_resource(id< MTLResource > resource, MTLResourceUsage usage)
void set_acceleration_structure(id< MTLAccelerationStructure > accel, int index)
void set_sampler(id< MTLSamplerState > sampler_state, int index)
void set_buffer(id< MTLBuffer > buf, size_t offset, int index)
void set_texture(id< MTLTexture > tex, int index)
MTLVertexCommandEncoder(id< MTLRenderCommandEncoder > encoder)