Blender V5.0
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 uint32_t id : 16;
30 uint32_t used : 1;
31};
32
33class MTLBatch : public Batch {
34
35 /* Vertex Bind-state Caching for a given shader interface used with the Batch. */
36 struct VertexDescriptorShaderInterfacePair {
37 MTLVertexDescriptor vertex_descriptor{};
38 const ShaderInterface *interface = nullptr;
39 uint16_t attr_mask{};
40 int num_buffers{};
42 /* Cache life index compares a cache entry with the active MTLBatch state.
43 * This is initially set to the cache life index of MTLBatch. If the batch has been modified,
44 * this index is incremented to cheaply invalidate existing cache entries. */
45 uint32_t cache_life_index = 0;
46 };
47
48 class MTLVertexDescriptorCache {
49
50 private:
51 MTLBatch *batch_;
52
53 VertexDescriptorShaderInterfacePair cache_[GPU_VAO_STATIC_LEN] = {};
54 MTLContext *cache_context_ = nullptr;
55 uint32_t cache_life_index_ = 0;
56
57 public:
58 MTLVertexDescriptorCache(MTLBatch *batch) : batch_(batch) {};
59 VertexDescriptorShaderInterfacePair *find(const ShaderInterface *interface);
60 bool insert(VertexDescriptorShaderInterfacePair &data);
61
62 private:
63 void vertex_descriptor_cache_init(MTLContext *ctx);
64 void vertex_descriptor_cache_clear();
65 void vertex_descriptor_cache_ensure();
66 };
67
68 private:
69 MTLShader *active_shader_ = nullptr;
70 MTLVertexDescriptorCache vao_cache = {this};
71
72 /* Topology emulation. */
73 gpu::MTLBuffer *emulated_topology_buffer_ = nullptr;
74 GPUPrimType emulated_topology_type_;
75 uint32_t topology_buffer_input_v_count_ = 0;
76 uint32_t topology_buffer_output_v_count_ = 0;
77
78 public:
79 MTLBatch() = default;
80 ~MTLBatch() override = default;
81
82 void draw(int v_first, int v_count, int i_first, int i_count) override;
83 void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override;
84 void multi_draw_indirect(StorageBuf * /*indirect_buf*/,
85 int /*count*/,
86 intptr_t /*offset*/,
87 intptr_t /*stride*/) override
88 {
89 /* TODO(Metal): Support indirect draw commands. */
90 }
91
92 /* Returns an initialized RenderComandEncoder for drawing if all is good.
93 * Otherwise, nil. */
94 id<MTLRenderCommandEncoder> bind();
95 void unbind(id<MTLRenderCommandEncoder> rec);
96
97 /* Convenience getters. */
99 {
100 return static_cast<MTLIndexBuf *>(elem);
101 }
102 MTLVertBuf *verts_(const int index) const
103 {
104 return static_cast<MTLVertBuf *>(verts[index]);
105 }
107 {
108 return active_shader_;
109 }
110
111 private:
112 void draw_advanced(int v_first, int v_count, int i_first, int i_count);
113 void draw_advanced_indirect(StorageBuf *indirect_buf, intptr_t offset);
114 int prepare_vertex_binding(MTLVertBuf *verts,
117 uint16_t &attr_mask);
118
119 id<MTLBuffer> get_emulated_toplogy_buffer(GPUPrimType &in_out_prim_type, uint32_t &v_count);
120
121 void prepare_vertex_descriptor_and_bindings(MTLVertBuf **buffers, int &num_buffers);
122
123 MEM_CXX_CLASS_ALLOC_FUNCS("MTLBatch");
124};
125
126} // namespace blender::gpu
void BLI_kdtree_nd_ insert(KDTree *tree, int index, const float co[KD_DIMS]) ATTR_NONNULL(1
static constexpr int GPU_BATCH_VBO_MAX_LEN
Definition GPU_batch.hh:33
GPUPrimType
Read Guarded memory(de)allocation.
BMesh const char void * data
MTLShader * active_shader_get() const
Definition mtl_batch.hh:106
void draw_indirect(StorageBuf *indirect_buf, intptr_t offset) override
Definition mtl_batch.mm:40
id< MTLRenderCommandEncoder > bind()
Definition mtl_batch.mm:277
MTLIndexBuf * elem_() const
Definition mtl_batch.hh:98
void multi_draw_indirect(StorageBuf *, int, intptr_t, intptr_t) override
Definition mtl_batch.hh:84
void draw(int v_first, int v_count, int i_first, int i_count) override
Definition mtl_batch.mm:35
MTLVertBuf * verts_(const int index) const
Definition mtl_batch.hh:102
~MTLBatch() override=default
void unbind(id< MTLRenderCommandEncoder > rec)
Definition mtl_batch.mm:389
static float verts[][3]
struct @021025263243242147216143265077100330027142264337::@225245033123204053237120173316075113304004012000 batch
#define GPU_VAO_STATIC_LEN
Definition mtl_batch.hh:26
char * buffers[2]