Blender V4.3
mtl_primitive.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#pragma once
12
13#include "BLI_assert.h"
14
15#include "GPU_primitive.hh"
16
17#include <Metal/Metal.h>
18
19namespace blender::gpu {
20
22static inline MTLPrimitiveTopologyClass mtl_prim_type_to_topology_class(MTLPrimitiveType prim_type)
23{
24 switch (prim_type) {
25 case MTLPrimitiveTypePoint:
26 return MTLPrimitiveTopologyClassPoint;
27 case MTLPrimitiveTypeLine:
28 case MTLPrimitiveTypeLineStrip:
29 return MTLPrimitiveTopologyClassLine;
30 case MTLPrimitiveTypeTriangle:
31 case MTLPrimitiveTypeTriangleStrip:
32 return MTLPrimitiveTopologyClassTriangle;
33 }
34 return MTLPrimitiveTopologyClassUnspecified;
35}
36
37static inline MTLPrimitiveType gpu_prim_type_to_metal(GPUPrimType prim_type)
38{
39 switch (prim_type) {
40 case GPU_PRIM_POINTS:
41 return MTLPrimitiveTypePoint;
42 case GPU_PRIM_LINES:
44 return MTLPrimitiveTypeLine;
48 return MTLPrimitiveTypeLineStrip;
49 case GPU_PRIM_TRIS:
52 return MTLPrimitiveTypeTriangle;
54 return MTLPrimitiveTypeTriangleStrip;
55 case GPU_PRIM_NONE:
56 return MTLPrimitiveTypePoint;
57 };
58}
59
60/* Certain primitive types are not supported in Metal, and require emulation.
61 * `GPU_PRIM_LINE_LOOP` and `GPU_PRIM_TRI_FAN` required index buffer patching.
62 * Adjacency types do not need emulation as the input structure is the same,
63 * and access is controlled from the vertex shader through SSBO vertex fetch.
64 * -- These Adj cases are only used in geometry shaders in OpenGL. */
65static inline bool mtl_needs_topology_emulation(GPUPrimType prim_type)
66{
67
68 BLI_assert(prim_type != GPU_PRIM_NONE);
69 switch (prim_type) {
72 return true;
73 default:
74 return false;
75 }
76 return false;
77}
78
79static inline bool mtl_vertex_count_fits_primitive_type(uint32_t vertex_count,
80 MTLPrimitiveType prim_type)
81{
82 if (vertex_count == 0) {
83 return false;
84 }
85
86 switch (prim_type) {
87 case MTLPrimitiveTypeLineStrip:
88 return (vertex_count > 1);
89 case MTLPrimitiveTypeLine:
90 return (vertex_count % 2 == 0);
91 case MTLPrimitiveTypePoint:
92 return (vertex_count > 0);
93 case MTLPrimitiveTypeTriangle:
94 return (vertex_count % 3 == 0);
95 case MTLPrimitiveTypeTriangleStrip:
96 return (vertex_count > 2);
97 }
98 BLI_assert(false);
99 return false;
100}
101
102} // namespace blender::gpu
#define BLI_assert(a)
Definition BLI_assert.h:50
GPUPrimType
@ GPU_PRIM_TRI_FAN
@ GPU_PRIM_LINE_LOOP
@ GPU_PRIM_LINE_STRIP_ADJ
@ GPU_PRIM_TRIS_ADJ
@ GPU_PRIM_NONE
@ GPU_PRIM_LINES
@ GPU_PRIM_POINTS
@ GPU_PRIM_LINES_ADJ
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_TRIS
static MTLPrimitiveType gpu_prim_type_to_metal(GPUPrimType prim_type)
static MTLPrimitiveTopologyClass mtl_prim_type_to_topology_class(MTLPrimitiveType prim_type)
static bool mtl_vertex_count_fits_primitive_type(uint32_t vertex_count, MTLPrimitiveType prim_type)
static bool mtl_needs_topology_emulation(GPUPrimType prim_type)
unsigned int uint32_t
Definition stdint.h:80