71 if (active_mtl_shader ==
nullptr || !active_mtl_shader->
is_valid()) {
74 "MTLImmediate::end -- cannot perform draw as active shader is NULL or invalid (likely "
75 "unimplemented) (shader %p '%s')",
93 context_->main_command_buffer.unfold_pending_debug_groups();
94 [rec pushDebugGroup:[NSString stringWithFormat:
@"immEnd(Shader:%s)",
114 if (interface.vertex_buffer_mask() == 0) {
115 MTL_LOG_ERROR(
"MTLImmediate::end Not enough buffer slot to bind attributes.");
116 BLI_assert_unreachable();
125 const ShaderInput *input = interface.attr_get(i);
126 BLI_assert(input != nullptr);
127 StringRefNull input_name(interface.name_at_offset(input->name_offset));
129 GPUVertAttr *attr = nullptr;
132 for (uint32_t a_idx = 0; a_idx < this->vertex_format.attr_len && attr == nullptr; a_idx++) {
133 GPUVertAttr *candidate = &this->vertex_format.attrs[a_idx];
135 for (uint32_t n_idx = 0; n_idx < candidate->name_len; n_idx++) {
136 StringRefNull name = GPU_vertformat_attr_name_get(
137 &this->vertex_format, candidate, n_idx);
138 if (name == input_name) {
145 if (attr ==
nullptr) {
146 MTL_LOG_ERROR(
"MTLImmediate::end Could not find matching attribute '%s' in Vertex Format!",
158 pso_attr.
offset = attr->offset;
167 desc.vertex_descriptor.buffer_layouts[0].step_function = MTLVertexStepFunctionPerVertex;
168 desc.vertex_descriptor.buffer_layouts[0].step_rate = 1;
170 desc.vertex_descriptor.buffer_layouts[0].buffer_slot = 0;
178 uchar *buffer_data =
reinterpret_cast<uchar *
>(current_allocation_.data);
191 rps.bind_vertex_buffer(
192 current_allocation_.metal_buffer, current_allocation_.buffer_offset, imm_buffer_slot);
194 MTLPrimitiveType mtl_prim_type = gpu_prim_type_to_metal(this->
prim_type);
196 if (context_->ensure_render_pipeline_state(mtl_prim_type)) {
209 bool rendered =
false;
210 if (mtl_needs_topology_emulation(this->
prim_type)) {
219 uint32_t fan_index_count = num_triangles * 3;
226 context_->get_scratch_buffer_manager().scratch_buffer_allocate_range_aligned(
231 for (
int i = 0; i < num_triangles; i++) {
232 index_buffer[a++] = 0;
233 index_buffer[a++] = i + 1;
234 index_buffer[a++] = i + 2;
248 context_->ensure_depth_stencil_state();
251 [rec drawIndexedPrimitives:MTLPrimitiveTypeTriangle
252 indexCount:fan_index_count
253 indexType:MTLIndexTypeUInt32
254 indexBuffer:index_buffer_mtl
255 indexBufferOffset:index_buffer_offset];
256 context_->main_command_buffer.register_draw_counters(fan_index_count);
270 MTLPrimitiveType primitive_type = metal_primitive_type_;
274 context_->ensure_depth_stencil_state();
281 [rec drawPrimitives:primitive_type vertexStart:0 vertexCount:vertex_count];
282 context_->main_command_buffer.register_draw_counters(vertex_count);
291 context_->get_scratch_buffer_manager().unbind_as_ssbo();
304 if (current_allocation_.metal_buffer) {
305 [current_allocation_.metal_buffer release];
306 current_allocation_.metal_buffer = nil;
static MTLContext * get()