40 this->draw_advanced(v_first, v_count, i_first, i_count);
45 this->draw_advanced_indirect(indirect_buf, offset);
48void MTLBatch::MTLVertexDescriptorCache::vertex_descriptor_cache_init(
MTLContext *ctx)
51 this->vertex_descriptor_cache_clear();
55void MTLBatch::MTLVertexDescriptorCache::vertex_descriptor_cache_clear()
58 cache_context_ =
nullptr;
61void MTLBatch::MTLVertexDescriptorCache::vertex_descriptor_cache_ensure()
63 if (this->cache_context_ !=
nullptr) {
67 batch_->flag &= ~GPU_BATCH_DIRTY;
68 this->vertex_descriptor_cache_clear();
73 if (cache_context_ ==
nullptr) {
78MTLBatch::VertexDescriptorShaderInterfacePair *MTLBatch::MTLVertexDescriptorCache::find(
79 const ShaderInterface *interface)
81 this->vertex_descriptor_cache_ensure();
83 if (cache_[i].interface == interface && cache_[i].cache_life_index == cache_life_index_) {
90bool MTLBatch::MTLVertexDescriptorCache::insert(
91 MTLBatch::VertexDescriptorShaderInterfacePair &
data)
93 vertex_descriptor_cache_ensure();
95 if (cache_[i].interface ==
nullptr || cache_[i].cache_life_index != cache_life_index_) {
97 cache_[i].cache_life_index = cache_life_index_;
105VertBufBinding MTLBatch::prepare_vertex_binding(MTLVertBuf *verts,
106 MTLRenderPipelineStateDescriptor &desc,
107 const MTLShaderInterface &interface,
112 const GPUVertFormat *
format = &verts->format;
114 int buffer_stride =
format->stride;
116 VertBufBinding binding{-1, -1};
117 int attribute_offset = 0;
122 const GPUVertAttr *a = &
format->attrs[a_idx];
124 if (
format->deinterleaved) {
125 attribute_offset += ((a_idx == 0) ? 0 :
format->attrs[a_idx - 1].type.size()) *
127 buffer_stride = a->type.size();
130 attribute_offset = a->offset;
135 for (
uint32_t n_idx = 0; n_idx < a->name_len; n_idx++) {
137 const ShaderInput *input = interface.attr_get(name);
139 if (input ==
nullptr || input->location == -1) {
144 if ((~attr_mask) & (1 << input->location)) {
146 " -- [Batch] Skipping attribute with input location %d (As one is already bound)",
152 attr_mask &= ~(1 << input->location);
156 if (binding.desc_id == -1) {
157 if (buffer_mask == 0) {
158 MTL_LOG_DEBUG(
" -- [Batch] Error: Not enough available slot to bind vertex buffer");
162 binding.desc_id = desc.vertex_descriptor.num_vert_buffers++;
165 auto &buffer_layout = desc.vertex_descriptor.buffer_layouts[binding.desc_id];
166 buffer_layout.step_function = MTLVertexStepFunctionPerVertex;
167 buffer_layout.step_rate = 1;
168 buffer_layout.stride = buffer_stride;
169 buffer_layout.buffer_slot = binding.slot_id;
171 MTL_LOG_DEBUG(
" -- [Batch] Adding source vertex buffer (Index: %d, Stride: %d)",
177 desc.vertex_descriptor.buffer_layouts[binding.desc_id].stride = buffer_stride;
181 MTLVertexAttributeDescriptorPSO &pso_attr =
182 desc.vertex_descriptor.attributes[input->location];
185 pso_attr.offset = attribute_offset;
186 pso_attr.buffer_index = binding.slot_id;
187 pso_attr.format_conversion_mode =
is_fetch_float(a->type.format) ?
191 desc.vertex_descriptor.max_attribute_value =
192 ((input->location) > desc.vertex_descriptor.max_attribute_value) ?
194 desc.vertex_descriptor.max_attribute_value;
195 desc.vertex_descriptor.total_attributes++;
201 " -- Batch Attribute(%d): ORIG Vert format: %d, Vert "
202 "components: %d, Fetch Mode %d --> FINAL FORMAT: %d",
204 (
int)a->
type.comp_type(),
205 (
int)a->type.comp_len(),
206 (
int)a->type.fetch_mode(),
207 (
int)pso_attr.format);
210 " -- [Batch] matching vertex attribute '%s' (Attribute Index: %d, Buffer index: "
239 if (active_shader_ ==
nullptr || !active_shader_->is_valid()) {
248 MTLVertBuf *buffers[GPU_BATCH_VBO_MAX_LEN] = {
nullptr};
249 int binding_slot[GPU_BATCH_VBO_MAX_LEN] = {0};
254 if (mtl_elem !=
nullptr) {
264 prepare_vertex_descriptor_and_bindings(
265 {buffers, GPU_BATCH_VBO_MAX_LEN}, {binding_slot, GPU_BATCH_VBO_MAX_LEN}, num_buffers);
269 for (
int i = 0; i < num_buffers; i++) {
285 [rec pushDebugGroup:[NSString stringWithFormat:
@"Batch(Shader:%s)",
286 active_shader_->name_get().c_str()]];
293 MTLPrimitiveType mtl_prim_type = gpu_prim_type_to_metal(this->prim_type);
295 MTL_LOG_ERROR(
"Failed to prepare and apply render pipeline state.");
301 for (
int i = 0; i < num_buffers; i++) {
337 uint16_t attr_mask = interface.enabled_attr_mask_;
338 uint32_t buffer_mask = interface.vertex_buffer_mask();
357 VertexDescriptorShaderInterfacePair *pair = this->vao_cache.find(&interface);
360 attr_mask = pair->attr_mask;
361 num_buffers = pair->num_buffers;
363 for (
int bid = 0; bid < GPU_BATCH_VBO_MAX_LEN; ++bid) {
364 if (pair->bufferIds[bid].used) {
365 buffers[bid] =
static_cast<MTLVertBuf *
>(this->verts[pair->bufferIds[bid].id]);
366 buffer_slots[bid] = pair->bufferIds[bid].slot;
371 VertexDescriptorShaderInterfacePair pair{};
372 pair.interface = &interface;
374 for (
int i = 0; i < GPU_BATCH_VBO_MAX_LEN; ++i) {
375 pair.bufferIds[i].id = -1;
376 pair.bufferIds[i].used = 0;
382 for (
int v = 0;
v < GPU_BATCH_VBO_MAX_LEN;
v++) {
383 if (mtl_verts[
v] !=
nullptr) {
384 MTL_LOG_DEBUG(
" -- [Batch] Checking bindings for bound vertex buffer %p", mtl_verts[
v]);
385 VertBufBinding bindings = this->prepare_vertex_binding(
386 static_cast<MTLVertBuf *
>(this->verts[
v]), desc, interface, attr_mask, buffer_mask);
387 if (bindings.desc_id >= 0) {
388 buffers[bindings.desc_id] =
static_cast<MTLVertBuf *
>(this->verts[
v]);
389 buffer_slots[bindings.desc_id] = bindings.slot_id;
390 pair.bufferIds[bindings.desc_id].id =
v;
391 pair.bufferIds[bindings.desc_id].used = 1;
392 pair.bufferIds[bindings.desc_id].slot = bindings.slot_id;
393 num_buffers =
max_ii(bindings.desc_id + 1, num_buffers);
399 pair.attr_mask = attr_mask;
401 pair.num_buffers = num_buffers;
402 if (!this->vao_cache.insert(pair)) {
404 "Cache is full (Size: %d), vertex descriptor will not be cached",
410#if MTL_DEBUG_SHADER_ATTRIBUTES == 1
411 if (attr_mask != 0) {
413 for (
int i = 0; i < active_shader_->get_interface()->get_total_attributes(); i++) {
414 const MTLShaderInputAttribute &attr = active_shader_->get_interface()->get_attribute(i);
415 if (attr_mask & (1 << attr.location)) {
417 "Warning: Missing expected attribute '%s' with location: %u in shader %s (attr "
419 active_shader_->get_interface()->name_at_offset(attr.name_offset),
421 active_shader_->name_get(),
427 MTLVertexFormatInvalid);
434void MTLBatch::draw_advanced(
int v_first,
int v_count,
int i_first,
int i_count)
440 id<MTLRenderCommandEncoder> rec = this->
bind();
448 MTLIndexBuf *mtl_elem =
static_cast<MTLIndexBuf *
>(
reinterpret_cast<IndexBuf *
>(this->elem));
449 MTLPrimitiveType mtl_prim_type = gpu_prim_type_to_metal(this->prim_type);
452 if (mtl_elem ==
nullptr) {
454 if (mtl_needs_topology_emulation(this->prim_type)) {
457 uint32_t emulated_v_count = v_count;
458 id<MTLBuffer> generated_index_buffer = this->get_emulated_toplogy_buffer(emulated_prim_type,
462 MTLPrimitiveType emulated_mtl_prim_type = gpu_prim_type_to_metal(emulated_prim_type);
466 [rec setCullMode:MTLCullModeNone];
468 if (generated_index_buffer != nil) {
469 BLI_assert(emulated_mtl_prim_type == MTLPrimitiveTypeTriangle ||
470 emulated_mtl_prim_type == MTLPrimitiveTypeLine);
471 if (emulated_mtl_prim_type == MTLPrimitiveTypeTriangle) {
474 if (emulated_mtl_prim_type == MTLPrimitiveTypeLine) {
479 ctx->ensure_depth_stencil_state();
481 [rec drawIndexedPrimitives:emulated_mtl_prim_type
482 indexCount:emulated_v_count
483 indexType:MTLIndexTypeUInt32
484 indexBuffer:generated_index_buffer
486 instanceCount:i_count
488 baseInstance:i_first];
492 "Cannot draw batch -- Emulated Topology mode: %u not yet supported",
498 ctx->ensure_depth_stencil_state();
501 [rec drawPrimitives:mtl_prim_type
504 instanceCount:i_count
505 baseInstance:i_first];
507 ctx->main_command_buffer.register_draw_counters(v_count * i_count);
513 uint32_t base_index = mtl_elem->index_base_;
515 uint32_t v_first_ofs = ((v_first + mtl_elem->index_start_) * index_size);
517 "Index offset is not 2/4-byte aligned as per METAL spec");
523 uint index_count = v_count;
525 id<MTLBuffer> index_buffer = mtl_elem->get_index_buffer(final_prim_type, index_count);
526 mtl_prim_type = gpu_prim_type_to_metal(final_prim_type);
529 if (index_buffer != nil) {
532 ctx->ensure_depth_stencil_state();
535 [rec drawIndexedPrimitives:mtl_prim_type
536 indexCount:index_count
538 indexBuffer:index_buffer
539 indexBufferOffset:v_first_ofs
540 instanceCount:i_count
541 baseVertex:base_index
542 baseInstance:i_first];
543 ctx->main_command_buffer.register_draw_counters(index_count * i_count);
546 BLI_assert_msg(
false,
"Index buffer does not have backing Metal buffer");
554void MTLBatch::draw_advanced_indirect(StorageBuf *indirect_buf,
intptr_t offset)
556 this->draw_advanced_indirect_internal(indirect_buf, 1, offset, 0);
564 this->draw_advanced_indirect_internal(indirect_buf, count, offset, stride);
567void MTLBatch::draw_advanced_indirect_internal(
StorageBuf *indirect_buf,
578 CLOG_ERROR(&
LOG,
"Failed to open Render Command encoder for DRAW INDIRECT");
589 if (mtl_indirect_buf == nil) {
599 "TriangleFan is not supported in Metal for Indirect draws.");
602 MTLIndexBuf *mtl_elem =
static_cast<MTLIndexBuf *
>(
reinterpret_cast<IndexBuf *
>(this->elem));
603 MTLPrimitiveType mtl_prim_type = gpu_prim_type_to_metal(this->prim_type);
605 if (mtl_needs_topology_emulation(this->prim_type)) {
606 BLI_assert_msg(
false,
"Metal Topology emulation unsupported for draw indirect.\n");
616 MTLIndexType index_type;
618 id<MTLBuffer> index_buffer = nil;
619 if (mtl_elem !=
nullptr) {
620 uint index_count = 0;
625 mtl_elem->flag_can_optimize(
false);
627 index_buffer = mtl_elem->get_index_buffer(final_prim_type, index_count);
628 mtl_prim_type = gpu_prim_type_to_metal(final_prim_type);
631 if (index_buffer == nil) {
632 BLI_assert_msg(
false,
"Index buffer does not have backing Metal buffer");
644 for (
int i = 0; i < count; i++) {
645 intptr_t cmd_offset = offset + i * stride;
646 if (mtl_elem ==
nullptr) {
647 [rec drawPrimitives:mtl_prim_type
648 indirectBuffer:mtl_indirect_buf
649 indirectBufferOffset:cmd_offset];
652 [rec drawIndexedPrimitives:mtl_prim_type
654 indexBuffer:index_buffer
656 indirectBuffer:mtl_indirect_buf
657 indirectBufferOffset:cmd_offset];
672id<MTLBuffer> MTLBatch::get_emulated_toplogy_buffer(
GPUPrimType &in_out_prim_type,
681 switch (input_prim_type) {
685 BLI_assert_msg(
false,
"Optimal primitive types should not reach here.");
690 BLI_assert_msg(
false,
"Adjacency primitive types should not reach here.");
708 if (this->emulated_topology_buffer_ !=
nullptr &&
709 (emulated_topology_type_ != input_prim_type || topology_buffer_input_v_count_ != v_count))
713 emulated_topology_buffer_->free();
714 emulated_topology_buffer_ =
nullptr;
718 if (this->emulated_topology_buffer_ ==
nullptr) {
721 switch (input_prim_type) {
724 output_prim_count = v_count - 1;
727 output_prim_count = v_count;
731 output_prim_count = v_count - 2;
734 BLI_assert_msg(
false,
"Cannot generate optimized topology buffer for other types.");
740 uint32_t buffer_bytes = output_IB_elems * 4;
750 bool winding_clockwise =
false;
753 switch (input_prim_type) {
757 for (line = 0; line < output_prim_count - 1; line++) {
758 data[line * 2 + 0] = line + 0;
759 data[line * 2 + 1] = line + 1;
762 data[line * 2 + 0] = line + 0;
763 data[line * 2 + 1] = 0;
768 for (
int triangle = 0; triangle < output_prim_count; triangle++) {
769 data[triangle * 3 + 0] = 0;
770 data[triangle * 3 + 1] = triangle + 1;
771 data[triangle * 3 + 2] = triangle + 2;
776 BLI_assert_msg(
false,
"Other primitive types do not require emulation.");
781 this->emulated_topology_buffer_->flush();
783 topology_buffer_input_v_count_ = v_count;
784 topology_buffer_output_v_count_ = output_IB_elems;
785 emulated_topology_type_ = input_prim_type;
789 in_out_v_count = topology_buffer_output_v_count_;
790 in_out_prim_type = output_prim_type;
791 return (emulated_topology_buffer_) ? emulated_topology_buffer_->get_metal_buffer() : nil;
#define CLOG_ERROR(clg_ref,...)
#define CLOG_DEBUG(clg_ref,...)
#define CLOG_WARN(clg_ref,...)
#define BLI_assert_msg(a, msg)
SocketType::Type type() const
StateManager * state_manager
void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override
void multi_draw_indirect(StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride) override
id< MTLRenderCommandEncoder > bind()
void draw(int v_first, int v_count, int i_first, int i_count) override
void unbind(id< MTLRenderCommandEncoder > rec)
gpu::MTLBuffer * allocate(uint64_t size, bool cpu_visible)
MTLFrameBuffer * get_active_framebuffer()
void register_draw_counters(int vertex_submission)
MTLRenderPassState & get_render_pass_state()
void unfold_pending_debug_groups()
bool ensure_render_pipeline_state(MTLPrimitiveType prim_type)
id< MTLRenderCommandEncoder > ensure_begin_render_pass()
static MTLContext * get()
MTLCommandBufferManager main_command_buffer
static MTLBufferPool * get_global_memory_manager()
void ensure_depth_stencil_state()
static MTLIndexType gpu_index_type_to_metal(GPUIndexBufType type)
void upload_data() override
void bind_vertex_buffer(id< MTLBuffer > buffer, uint64_t buffer_offset, uint index)
MTLRenderPipelineStateDescriptor & get_pipeline_descriptor()
id< MTLBuffer > get_metal_buffer()
#define GPU_VAO_STATIC_LEN
#define MTL_LOG_WARNING(info,...)
#define MTL_LOG_DEBUG(info,...)
#define MTL_LOG_ERROR(info,...)
MTLVertexFormat gpu_vertex_format_to_metal(VertAttrType vert_format)
bool is_fetch_float(VertAttrType attr_type)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
@ GPU_PRIM_LINE_STRIP_ADJ
MINLINE int max_ii(int a, int b)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE unsigned int bitscan_forward_clear_uint(unsigned int *a)
BMesh const char void * data
std::string format(const std::string &fmt, Args &&...args)
void reset_vertex_descriptor()
MTLVertexDescriptor vertex_descriptor
MTLVertexAttributeDescriptorPSO attributes[GPU_VERT_ATTR_MAX_LEN]