Blender V5.3
mtl_pso_descriptor_state.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#pragma once
9
10#include "BLI_math_bits.hh"
11#include "GPU_batch.hh"
12#include "GPU_vertex_format.hh"
13
14#include <Metal/Metal.h>
15
16#include "BLI_vector.hh"
17
19#include "gpu_shader_private.hh"
20
21namespace blender::gpu {
22
28 ::MTLVertexFormat format;
29 int offset;
32
34 {
35 return (format == other.format) && (offset == other.offset) &&
36 (buffer_index == other.buffer_index) &&
38 }
39
40 uint64_t hash() const
41 {
42 return uint64_t((uint64_t(this->format) ^ (this->offset << 4) ^ (this->buffer_index << 8) ^
43 (this->format_conversion_mode << 12)));
44 }
45
46 void reset()
47 {
48 format = MTLVertexFormatInvalid;
49 offset = 0;
50 buffer_index = 0;
52 }
53};
54
56 ::MTLVertexStepFunction step_function;
58 int stride;
60
62 {
63 return (step_function == other.step_function) && (step_rate == other.step_rate) &&
64 (stride == other.stride) && (buffer_slot == other.buffer_slot);
65 }
66
67 uint64_t hash() const
68 {
69 return uint64_t(uint64_t(this->step_function) ^ (this->step_rate << 4) ^ (this->stride << 8) ^
70 (uint64_t(this->buffer_slot) << 32));
71 }
72
73 void reset()
74 {
75 step_function = MTLVertexStepFunctionPerVertex;
76 step_rate = 1;
77 stride = 0;
78 }
79};
80
82 /* Core Vertex Attributes. */
88 ::MTLPrimitiveTopologyClass prim_topology_class;
89
90 bool operator==(const MTLVertexDescriptor &other) const
91 {
92 if ((this->max_attribute_value != other.max_attribute_value) ||
93 (this->total_attributes != other.total_attributes) ||
94 (this->num_vert_buffers != other.num_vert_buffers))
95 {
96 return false;
97 }
98 if (this->prim_topology_class != other.prim_topology_class) {
99 return false;
100 };
101
102 for (const int a : IndexRange(this->max_attribute_value + 1)) {
103 if (!(this->attributes[a] == other.attributes[a])) {
104 return false;
105 }
106 }
107
108 for (const int b : IndexRange(ARRAY_SIZE(this->buffer_layouts))) {
109 if (!(this->buffer_layouts[b] == other.buffer_layouts[b])) {
110 return false;
111 }
112 }
113
114 /* NOTE: No need to compare SSBO attributes, as these will match attribute bindings for the
115 * given shader. These are simply extra pre-resolved properties we want to include in the
116 * cache. */
117 return true;
118 }
119
121 {
122 uint64_t hash = uint64_t(this->max_attribute_value ^ this->num_vert_buffers);
123 for (const int a : IndexRange(this->max_attribute_value + 1)) {
124 hash ^= this->attributes[a].hash() << a;
125 }
126
127 for (const int b : IndexRange(this->num_vert_buffers)) {
128 hash ^= this->buffer_layouts[b].hash() << (b + 10);
129 }
130 return hash;
131 }
132};
133
136
142
144 {
145 return values == other.values;
146 }
147
149 {
151 uint seed = 0xFF;
152 for (const shader::SpecializationConstant::Value &value : values) {
153 seed = seed << 1;
154 hash ^= seed ^ value.u;
155 }
156 return hash;
157 }
158};
159
160/* Metal Render Pipeline State Descriptor -- All unique information which feeds PSO creation. */
162 /* This state descriptor will contain ALL parameters which generate a unique PSO.
163 * We will then use this state-object to efficiently look-up or create a
164 * new PSO for the current shader.
165 *
166 * Unlike the 'MTLContextGlobalShaderPipelineState', this struct contains a subset of
167 * parameters used to distinguish between unique PSOs. This struct is hash-able and only
168 * contains those parameters which are required by PSO generation. Non-unique state such as
169 * bound resources is not tracked here, as it does not require a unique PSO permutation if
170 * changed. */
171
172 /* Input Vertex Descriptor. */
174
175 /* Render Target attachment state.
176 * Assign to #MTLPixelFormatInvalid if not used. */
182
183 /* Render Pipeline State affecting PSO creation. */
185 MTLBlendOperation alpha_blend_op;
186 MTLBlendOperation rgb_blend_op;
188 MTLBlendFactor dest_rgb_blend_factor;
190 MTLBlendFactor src_rgb_blend_factor;
191
192 /* Global color write mask as this cannot be specified per attachment. */
193 MTLColorWriteMask color_write_mask;
194
195 /* Clip distance enablement. */
197
198 /* Point size required by point primitives. */
199 float point_size = 0.0f;
200
201 /* Specialization constants map. */
203
204 /* Comparison Operator for caching. */
206 {
207 if (!(vertex_descriptor == other.vertex_descriptor)) {
208 return false;
209 }
210
212 return false;
213 }
214
220 (rgb_blend_op != other.rgb_blend_op) ||
227 (point_size != other.point_size))
228 {
229 return false;
230 }
231
232 /* Attachments can be skipped, so num_color_attachments will not define the range. */
233 for (const int c : IndexRange(GPU_FB_MAX_COLOR_ATTACHMENT)) {
235 return false;
236 }
237 }
238
240 return false;
241 }
242
243 return true;
244 }
245
247 {
248 /* NOTE(Metal): Current setup aims to minimize overlap of parameters
249 * which are more likely to be different, to ensure earlier hash
250 * differences without having to fallback to comparisons.
251 * Though this could likely be further improved to remove
252 * has collisions. */
253
254 uint64_t hash = this->vertex_descriptor.hash();
255 hash ^= uint64_t(this->num_color_attachments) << 16; /* up to 6 (3 bits). */
256 hash ^= uint64_t(this->depth_attachment_format) << 18; /* up to 555 (9 bits). */
257 hash ^= uint64_t(this->stencil_attachment_format) << 20; /* up to 555 (9 bits). */
258 hash ^= (*((uint64_t *)&this->vertex_descriptor.prim_topology_class)); /* Up to 3 (2 bits). */
259
260 /* Only include elements in Hash if they are needed - avoids variable null assignments
261 * influencing hash. */
262 if (this->num_color_attachments > 0) {
263 hash ^= uint64_t(this->color_write_mask) << 22; /* 4 bit bit-mask. */
264 hash ^= uint64_t(this->alpha_blend_op) << 26; /* Up to 4 (3 bits). */
265 hash ^= uint64_t(this->rgb_blend_op) << 29; /* Up to 4 (3 bits). */
266 hash ^= uint64_t(this->dest_alpha_blend_factor) << 32; /* Up to 18 (5 bits). */
267 hash ^= uint64_t(this->dest_rgb_blend_factor) << 37; /* Up to 18 (5 bits). */
268 hash ^= uint64_t(this->src_alpha_blend_factor) << 42; /* Up to 18 (5 bits). */
269 hash ^= uint64_t(this->src_rgb_blend_factor) << 47; /* Up to 18 (5 bits). */
270 hash ^= uint64_t(this->color_attachment_mask) << 47; /* 8 bit bit-mask. */
271
273 hash ^= uint64_t(this->color_attachment_format[c]) << (c + 52); /* Up to 555 (9 bits). */
274 }
275 }
276
277 hash |= uint64_t((this->blending_enabled && (this->num_color_attachments > 0)) ? 1 : 0) << 62;
278 hash ^= uint64_t(float_as_uint(this->point_size));
279
280 /* Clipping plane enablement. */
282
283 /* Specialization constants. We can treat the raw bytes as uint. */
285
286 return hash;
287 }
288
289 /* Reset the Vertex Descriptor to default. */
291 {
295 vertex_descriptor.prim_topology_class = MTLPrimitiveTopologyClassUnspecified;
296 for (int i = 0; i < GPU_VERT_ATTR_MAX_LEN; i++) {
298 }
299 }
300};
301
302/* Metal Compute Pipeline State Descriptor containing all unique information which feeds PSO
303 * creation. */
305
306 /* Specialization constants map. */
308
314
315 /* Comparison Operator for caching. */
317 {
319 }
320
322 {
323 return specialization_state.hash();
324 }
325};
326
327} // namespace blender::gpu
constexpr IndexRange()=default
#define GPU_FB_MAX_COLOR_ATTACHMENT
unsigned __int64 uint64_t
Definition BKE_text.h:91
unsigned int uint
Definition BKE_text.h:55
MINLINE unsigned int float_as_uint(float f)
unsigned char uchar
Definition BKE_text.h:58
unsigned char uint8_t
Definition BKE_text.h:79
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool operator==(const MTLComputePipelineStateDescriptor &other) const
MTLComputePipelineStateDescriptor(Vector< shader::SpecializationConstant::Value > values)
MTLPixelFormat color_attachment_format[GPU_FB_MAX_COLOR_ATTACHMENT]
bool operator==(const MTLRenderPipelineStateDescriptor &other) const
bool operator==(const MTLVertexAttributeDescriptorPSO &other) const
bool operator==(const MTLVertexBufferLayoutDescriptorPSO &other) const
::MTLPrimitiveTopologyClass prim_topology_class
MTLVertexAttributeDescriptorPSO attributes[GPU_VERT_ATTR_MAX_LEN]
MTLVertexBufferLayoutDescriptorPSO buffer_layouts[GPU_BATCH_VBO_MAX_LEN]
bool operator==(const MTLVertexDescriptor &other) const
SpecializationStateDescriptor(Vector< shader::SpecializationConstant::Value > source)
bool operator==(const SpecializationStateDescriptor &other) const
Vector< shader::SpecializationConstant::Value > values
int size() const
Definition vector.hh:130