Blender V5.3
mtl_shader_interface.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
8
9#pragma once
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_vector.hh"
14
16#include "mtl_capabilities.hh"
17#include "mtl_push_constant.hh"
19
20#include "GPU_common.hh"
21#include "GPU_common_types.hh"
22#include "GPU_texture.hh"
24#include <Metal/Metal.h>
25#include <functional>
26
27namespace blender::gpu {
28
29/* #MTLShaderInterface describes the layout and properties of a given shader,
30 * including input and output bindings, and any special properties or modes
31 * that the shader may require.
32 *
33 * -- Shader input/output bindings --
34 *
35 * We require custom data-structures for the binding information in Metal.
36 * This is because certain bindings contain and require more information to
37 * be stored than can be tracked solely within the `ShaderInput` struct.
38 * e.g. data sizes and offsets.
39 *
40 * Upon interface completion, `prepare_common_shader_inputs` is used to
41 * populate the global `ShaderInput*` array to enable correct functionality
42 * of shader binding location lookups. These returned locations act as indices
43 * into the arrays stored here in the #MTLShaderInterface, such that extraction
44 * of required information can be performed within the back-end.
45 *
46 * e.g. `int loc = GPU_shader_get_uniform(...)`
47 * `loc` will match the index into the `MTLShaderUniform uniforms_[]` array
48 * to fetch the required Metal specific information.
49 *
50 *
51 *
52 * -- Argument Buffers and Argument Encoders --
53 *
54 * We can use #ArgumentBuffers (AB's) in Metal to extend the resource bind limitations
55 * by providing bind-less support.
56 *
57 * Argument Buffers are used for sampler bindings when the builtin
58 * sampler limit of 16 is exceeded, as in all cases for Blender,
59 * each individual texture is associated with a given sampler, and this
60 * lower limit would otherwise reduce the total availability of textures
61 * used in shaders.
62 *
63 * In future, argument buffers may be extended to support other resource
64 * types, if overall bind limits are ever increased within Blender.
65 *
66 * The #ArgumentEncoder cache used to store the generated #ArgumentEncoders for a given
67 * shader permutation. The #ArgumentEncoder is the resource used to write resource binding
68 * information to a specified buffer, and is unique to the shader's resource interface.
69 */
70
75 private:
76 /* Argument buffer encoder for sampler buffer. */
77 id<MTLArgumentEncoder> arg_encoder_ = nil;
78
79 /* Bit Mask representing the free buffer slots from this interface.
80 * Used to bind the vertex and index buffers.
81 * This references the Metal Buffer bind space, not the GPU one. */
82 uint32_t vertex_buffer_mask_ = ~(0xFFFFFFFFu << MTL_MAX_BUFFER_BINDINGS);
83
84 /* Bit mask of bound top-level acceleration structure slots. */
85 uint32_t enabled_accel_mask_ = 0;
86
88
89 /* Used for texture atomic workaround. */
90 int image_names_offsets_[MTL_MAX_IMAGE_SLOTS];
91 int sampler_names_offsets_[MTL_MAX_SAMPLER_SLOTS];
92
93 /* Debug. */
94 char name[256];
95
96 public:
97 MTLShaderInterface(const char *name,
98 const shader::ShaderCreateInfo &info,
99 MTLPushConstantBuf *push_constant_buf = nullptr);
100 ~MTLShaderInterface() override;
101
103 {
104 return vertex_buffer_mask_;
105 }
106
108 {
109 return enabled_accel_mask_;
110 }
111
112 bool use_layer() const
113 {
114 return bool(shader_builtins_ & shader::BuiltinBits::LAYER);
115 }
117 {
118 return bool(shader_builtins_ & shader::BuiltinBits::VIEWPORT_INDEX);
119 }
121 {
122 return bool(shader_builtins_ & shader::BuiltinBits::USE_SAMPLER_ARG_BUFFER);
123 }
125 {
126 return bool(shader_builtins_ & shader::BuiltinBits::TEXTURE_ATOMIC);
127 }
128
129 bool is_point_shader() const
130 {
132 }
133
134 /* Name buffer fetching. */
135 const char *name_at_offset(uint32_t offset) const;
136
137 /* Interface name. */
138 const char *name_get() const
139 {
140 return this->name;
141 }
142
143 const char *image_name_get(int slot) const
144 {
145 return name_at_offset(image_names_offsets_[slot]);
146 }
147
148 const char *sampler_name_get(int slot) const
149 {
150 return name_at_offset(sampler_names_offsets_[slot]);
151 }
152
154
155 MEM_CXX_CLASS_ALLOC_FUNCS("MTLShaderInterface");
156};
157
158} // namespace blender::gpu
Read Blender memory allocation and freeing.
MTLShaderInterface(const char *name, const shader::ShaderCreateInfo &info, MTLPushConstantBuf *push_constant_buf=nullptr)
const char * sampler_name_get(int slot) const
const char * image_name_get(int slot) const
id< MTLArgumentEncoder > ensure_argument_encoder(id< MTLFunction > mtl_function)
const char * name_at_offset(uint32_t offset) const
MEM_CXX_CLASS_ALLOC_FUNCS("MTLShaderInterface")
#define MTL_MAX_SAMPLER_SLOTS
#define MTL_MAX_BUFFER_BINDINGS
#define MTL_MAX_IMAGE_SLOTS
unsigned int uint32_t
Definition BKE_text.h:81
Describe inputs & outputs, stage interfaces, resources and sources of a shader. If all data is correc...