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