Blender V4.3
mtl_index_buffer.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
9#pragma once
10
11#include "GPU_index_buffer.hh"
12#include "MEM_guardedalloc.h"
13#include "mtl_context.hh"
14#include <Cocoa/Cocoa.h>
15#include <Metal/Metal.h>
16#include <QuartzCore/QuartzCore.h>
17
18namespace blender::gpu {
19
20class MTLIndexBuf : public IndexBuf {
21 friend class MTLBatch;
22 friend class MTLDrawList;
23 friend class MTLStorageBuf; /* For bind as SSBO resource access. */
24
25 private:
26 /* Metal buffer resource. */
27 gpu::MTLBuffer *ibo_ = nullptr;
28 uint64_t alloc_size_ = 0;
29
30 /* SSBO wrapper for bind_as_ssbo support. */
31 MTLStorageBuf *ssbo_wrapper_ = nullptr;
32
33#ifndef NDEBUG
34 /* Flags whether point index buffer has been compacted
35 * to remove false restart indices. */
36 bool point_restarts_stripped_ = false;
37#endif
38
39 /* Optimized index buffers.
40 * NOTE(Metal): This optimization encodes a new index buffer following
41 * #TriangleList topology. Parsing of Index buffers is more optimal
42 * when not using restart-compatible primitive topology types. */
43 GPUPrimType optimized_primitive_type_;
44 gpu::MTLBuffer *optimized_ibo_ = nullptr;
45 uint32_t emulated_v_count = 0;
46 void free_optimized_buffer();
47
48 /* Flags whether an index buffer can be optimized.
49 * For index buffers which are partially modified
50 * on the host, or by the GPU, optimization cannot be performed. */
51 bool can_optimize_ = true;
52
53 public:
55
56 void bind_as_ssbo(uint32_t binding) override;
57 void read(uint32_t *data) const override;
58
59 void upload_data() override;
60 void update_sub(uint32_t start, uint32_t len, const void *data) override;
61
62 /* #get_index_buffer can conditionally return an optimized index buffer of a
63 * differing format, if it is concluded that optimization is preferred
64 * for the given inputs.
65 * Index buffer optimization is used to replace restart-compatible
66 * primitive types with non-restart-compatible ones such as #TriangleList and
67 * #LineList. This improves GPU execution for these types significantly, while
68 * only incurring a small performance penalty.
69 *
70 * This is also used to emulate unsupported topology types
71 * such as triangle fan. */
72 id<MTLBuffer> get_index_buffer(GPUPrimType &in_out_primitive_type, uint &in_out_v_count);
73 void flag_can_optimize(bool can_optimize);
74
75 static MTLIndexType gpu_index_type_to_metal(GPUIndexBufType type)
76 {
77 return (type == GPU_INDEX_U16) ? MTLIndexTypeUInt16 : MTLIndexTypeUInt32;
78 }
79
80 private:
81 void strip_restart_indices() override;
82
83 MEM_CXX_CLASS_ALLOC_FUNCS("MTLIndexBuf")
84};
85
86} // namespace blender::gpu
unsigned int uint
GPUPrimType
Read Guarded memory(de)allocation.
static MTLIndexType gpu_index_type_to_metal(GPUIndexBufType type)
void read(uint32_t *data) const override
id< MTLBuffer > get_index_buffer(GPUPrimType &in_out_primitive_type, uint &in_out_v_count)
void bind_as_ssbo(uint32_t binding) override
void update_sub(uint32_t start, uint32_t len, const void *data) override
void flag_can_optimize(bool can_optimize)
int len
unsigned int uint32_t
Definition stdint.h:80
unsigned __int64 uint64_t
Definition stdint.h:90