Blender V5.3
mtl_batch.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
11
12#pragma once
13
14#include "GPU_batch.hh"
15#include "MEM_guardedalloc.h"
16#include "mtl_index_buffer.hh"
17#include "mtl_primitive.hh"
18#include "mtl_shader.hh"
19#include "mtl_vertex_buffer.hh"
20
21namespace blender::gpu {
22
23class MTLContext;
25
26#define GPU_VAO_STATIC_LEN 64
27
29 /* Slot in the buffer layout descriptor. -1 if buffer is not needed. */
31 /* Binding point this buffer needs to be bound at. */
33};
34
35/* TODO(fclem): This needs to be revisited as the complexity of this code is off the chart.
36 * There are multiple copies of the same information in multiple place. */
42
43class MTLBatch : public Batch {
44
45 /* Vertex Bind-state Caching for a given shader interface used with the Batch. */
46 struct VertexDescriptorShaderInterfacePair {
47 MTLVertexDescriptor vertex_descriptor{};
48 const ShaderInterface *interface = nullptr;
49 uint16_t attr_mask = 0;
50 int num_buffers = 0;
51 VertexBufferID bufferIds[GPU_BATCH_VBO_MAX_LEN] = {};
52 /* Cache life index compares a cache entry with the active MTLBatch state.
53 * This is initially set to the cache life index of MTLBatch. If the batch has been modified,
54 * this index is incremented to cheaply invalidate existing cache entries. */
55 uint32_t cache_life_index = 0;
56 };
57
58 class MTLVertexDescriptorCache {
59
60 private:
61 MTLBatch *batch_;
62
63 VertexDescriptorShaderInterfacePair cache_[GPU_VAO_STATIC_LEN] = {};
64 MTLContext *cache_context_ = nullptr;
65 uint32_t cache_life_index_ = 0;
66
67 public:
68 MTLVertexDescriptorCache(MTLBatch *batch) : batch_(batch) {};
69 VertexDescriptorShaderInterfacePair *find(const ShaderInterface *interface);
70 bool insert(VertexDescriptorShaderInterfacePair &data);
71
72 private:
73 void vertex_descriptor_cache_init(MTLContext *ctx);
74 void vertex_descriptor_cache_clear();
75 void vertex_descriptor_cache_ensure();
76 };
77
78 private:
79 MTLShader *active_shader_ = nullptr;
80 MTLVertexDescriptorCache vao_cache = {this};
81
82 /* Topology emulation. */
83 gpu::MTLBuffer *emulated_topology_buffer_ = nullptr;
84 GPUPrimType emulated_topology_type_;
85 uint32_t topology_buffer_input_v_count_ = 0;
86 uint32_t topology_buffer_output_v_count_ = 0;
87
88 public:
89 MTLBatch() = default;
90 ~MTLBatch() override = default;
91
92 void draw(int v_first, int v_count, int i_first, int i_count) override;
93 void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override;
94 void multi_draw_indirect(StorageBuf *indirect_buf,
95 int count,
96 intptr_t offset,
97 intptr_t stride) override;
98
99 /* Returns an initialized RenderComandEncoder for drawing if all is good.
100 * Otherwise, nil. */
103
104 /* Convenience getters. */
106 {
107 return static_cast<MTLIndexBuf *>(elem);
108 }
109 MTLVertBuf *verts_(const int index) const
110 {
111 return static_cast<MTLVertBuf *>(verts[index]);
112 }
114 {
115 return active_shader_;
116 }
117
118 private:
119 void draw_advanced(int v_first, int v_count, int i_first, int i_count);
120 void draw_advanced_indirect(StorageBuf *indirect_buf, intptr_t offset);
121 void draw_advanced_indirect_internal(StorageBuf *indirect_buf,
122 int count,
123 intptr_t offset,
124 intptr_t stride);
125
126 VertBufBinding prepare_vertex_binding(MTLVertBuf *verts,
128 const MTLShaderInterface &interface,
129 uint16_t &attr_mask,
130 uint32_t &buffer_mask);
131
132 id<MTLBuffer> get_emulated_toplogy_buffer(GPUPrimType &in_out_prim_type, uint32_t &v_count);
133
134 void prepare_vertex_descriptor_and_bindings(MutableSpan<MTLVertBuf *> buffers,
135 MutableSpan<int> buffer_slots,
136 int &num_buffers);
137
138 MEM_CXX_CLASS_ALLOC_FUNCS("MTLBatch");
139};
140
141} // namespace blender::gpu
Read Blender memory allocation and freeing.
__forceinline const float4 insert(const float4 &a, const float b)
Definition binning.cpp:31
MTLShader * active_shader_get() const
Definition mtl_batch.hh:113
void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override
Definition mtl_batch.mm:43
void multi_draw_indirect(StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride) override
Definition mtl_batch.mm:559
id< MTLRenderCommandEncoder > bind()
Definition mtl_batch.mm:223
MTLIndexBuf * elem_() const
Definition mtl_batch.hh:105
MTLVertBuf * verts_(const int index) const
Definition mtl_batch.hh:109
~MTLBatch() override=default
void unbind(id< MTLRenderCommandEncoder > rec)
Definition mtl_batch.mm:318
#define GPU_VAO_STATIC_LEN
Definition mtl_batch.hh:26
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
_W64 int intptr_t
Definition BKE_text.h:119