27 MEM_SAFE_DELETE(
data_);
48 if (vbo_ !=
nullptr) {
56 MEM_SAFE_DELETE(
data_);
60 ssbo_wrapper_ =
nullptr;
77 if (required_size_raw == 0) {
91 bool requires_reallocation = (vbo_ !=
nullptr) && (alloc_size_ != required_size);
97 if (vbo_ !=
nullptr) {
98 if (requires_reallocation || (new_data_ready && contents_in_flight_)) {
108 if (requires_reallocation) {
115 if (vbo_ ==
nullptr) {
119 static std::atomic<int> global_counter = 0;
120 int index = global_counter.fetch_add(1);
121 vbo_->set_label([NSString stringWithFormat:
@"VBO %i",
index]);
128 alloc_size_ = required_size;
129 contents_in_flight_ =
false;
133 if (new_data_ready) {
140 BLI_assert(vbo_->get_size() >= required_size_raw);
143 void *dst_data = vbo_->get_host_ptr();
144 memcpy((
uint8_t *)dst_data, this->
data_, required_size_raw);
145 vbo_->flush_range(0, required_size_raw);
150 MEM_SAFE_DELETE(
data_);
157 else if (requires_reallocation) {
176 [enc copyFromBuffer:copy_prev_buffer
178 toBuffer:copy_new_buffer
184 if (copy_new_buffer.storageMode == MTLStorageModeManaged) {
185 [enc synchronizeResource:copy_new_buffer];
190 MEM_SAFE_DELETE(
data_);
202 if (prev_vbo !=
nullptr) {
226 if ([scratch_allocation.
metal_buffer storageMode] == MTLStorageModeManaged) {
241 [enc copyFromBuffer:data_buffer
242 sourceOffset:data_buffer_offset
244 destinationOffset:start
248 if (dst_buffer.storageMode == MTLStorageModeManaged) {
249 [enc synchronizeResource:dst_buffer];
254 uint source_first_vertex,
255 uint dest_first_vertex,
260 "Copy source range exceeds the source vertex buffer bounds");
262 "Copy destination range exceeds the vertex buffer bounds");
264 BLI_assert_msg(vbo_,
"GPU_vertbuf_use() not called on this buffer");
265 BLI_assert_msg(src.vbo_,
"GPU_vertbuf_use() not called on the source buffer");
267 const size_t src_offset = size_t(source_first_vertex) * src.
format.
stride;
268 const size_t dst_offset = size_t(dest_first_vertex) *
format.
stride;
271 if (copy_size == 0) {
275 BLI_assert(src_offset + copy_size <= src.vbo_->get_size());
276 BLI_assert(dst_offset + copy_size <= vbo_->get_size());
290 [enc copyFromBuffer:src_buf
291 sourceOffset:src_offset
293 destinationOffset:dst_offset
298 if (dst_buf.storageMode == MTLStorageModeManaged) {
299 [enc synchronizeResource:dst_buf];
315 if (ssbo_wrapper_ ==
nullptr) {
318 ssbo_wrapper_->bind(binding);
329 if (buffer_texture_ !=
nullptr) {
332 if (tex_buf != buf) {
334 buffer_texture_ =
nullptr;
339 if (buffer_texture_ ==
nullptr) {
360 if (source_buffer.storageMode == MTLStorageModeManaged) {
362 [enc synchronizeResource:source_buffer];
369 void *host_ptr = vbo_->get_host_ptr();
384 [enc copyFromBuffer:source_buffer
392 if (dest_buffer.storageMode == MTLStorageModeManaged) {
393 [enc synchronizeResource:dest_buffer];
402 dst_tmp_vbo_->
free();
423 contents_in_flight_ =
true;
428#define CASE(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h) \
429 case VertAttrType::blender_enum: \
430 return MTLVertexFormat##mtl_vertex_enum;
432#define CASE_DEPRECATED(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h) \
433 case VertAttrType::blender_enum##_DEPRECATED: \
436 switch (vert_format) {
443#undef CASE_DEPRECATED
445 return MTLVertexFormatInvalid;
453 return MTLVertexFormatFloat;
455 return MTLVertexFormatFloat2;
457 return MTLVertexFormatFloat3;
459 return MTLVertexFormatFloat4;
460 case Type::float3x3_t:
461 return MTLVertexFormatInvalid;
462 case Type::float4x4_t:
463 return MTLVertexFormatInvalid;
464 case Type::float3_10_10_10_2_t:
465 return MTLVertexFormatInvalid;
467 return MTLVertexFormatUChar;
469 return MTLVertexFormatUChar2;
471 return MTLVertexFormatUChar3;
473 return MTLVertexFormatUChar4;
475 return MTLVertexFormatChar;
477 return MTLVertexFormatChar2;
479 return MTLVertexFormatChar3;
481 return MTLVertexFormatChar4;
483 return MTLVertexFormatInt;
485 return MTLVertexFormatInt2;
487 return MTLVertexFormatInt3;
489 return MTLVertexFormatInt4;
491 return MTLVertexFormatUInt;
493 return MTLVertexFormatUInt2;
495 return MTLVertexFormatUInt3;
497 return MTLVertexFormatUInt4;
499 return MTLVertexFormatUShort;
500 case Type::ushort2_t:
501 return MTLVertexFormatUShort2;
502 case Type::ushort3_t:
503 return MTLVertexFormatUShort3;
504 case Type::ushort4_t:
505 return MTLVertexFormatUShort4;
507 return MTLVertexFormatShort;
509 return MTLVertexFormatShort2;
511 return MTLVertexFormatShort3;
513 return MTLVertexFormatShort4;
515 return MTLVertexFormatInvalid;
518 return MTLVertexFormatInvalid;
#define GPU_TEXTURE_FREE_SAFE(texture)
#define BLI_assert_msg(a, msg)
#define MEM_realloc_uninitialized(vmemh, len)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
void * get_host_ptr() const
id< MTLBuffer > get_metal_buffer() const
id< MTLBlitCommandEncoder > ensure_begin_blit_encoder()
static MTLContext * get()
MTLScratchBufferManager & get_scratch_buffer_manager()
MTLCommandBufferManager main_command_buffer
static MTLBufferPool * get_global_memory_manager()
MTLTemporaryBuffer scratch_buffer_allocate_range_aligned(uint64_t alloc_size, uint alignment)
MTLStorageBuf(size_t size, GPUUsageType usage, const char *name)
id< MTLBuffer > get_vertex_buffer() const
void resize_data() override
void wrap_handle(uint64_t handle) override
void bind_as_ssbo(uint binding) override
void upload_data() override
void copy_sub(VertBuf &source_buf, uint source_first_vertex, uint dest_first_vertex, uint vertex_len) override
Copy from part of source vertex buffer into the vertex buffer.
void update_sub(uint start, uint len, const void *data) override
void read(void *data) const override
void bind_as_texture(uint binding) override
void acquire_data() override
void release_data() override
size_t size_used_get() const
size_t size_alloc_get() const
GPUUsageType get_usage_type() const
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
#define MTL_LOG_DEBUG(info,...)
#define CASE_DEPRECATED(a, b, c, blender_enum, d, e, mtl_vertex_enum, g, h)
MTLVertexFormat gpu_vertex_format_to_metal(VertAttrType vert_format)
MTLBufferRange MTLTemporaryBuffer
MTLVertexFormat gpu_type_to_metal_vertex_format(shader::Type type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MINLINE unsigned long long max_ulul(unsigned long long a, unsigned long long b)
MINLINE unsigned long long min_ulul(unsigned long long a, unsigned long long b)
unsigned __int64 uint64_t
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
gpu::Texture * GPU_texture_create_from_vertbuf(const char *name, gpu::VertBuf *vertex_buf)
@ GPU_VERTBUF_DATA_UPLOADED
void GPU_texture_bind(gpu::Texture *texture, int unit)
MINLINE uint ceil_to_multiple_u(uint a, uint b)
id< MTLBuffer > metal_buffer