turnip: Add support for polygon fill modes.
[mesa.git] / src / freedreno / vulkan / tu_util.h
1 /*
2 * Copyright 2020 Valve Corporation
3 * SPDX-License-Identifier: MIT
4 *
5 * Authors:
6 * Jonathan Marek <jonathan@marek.ca>
7 */
8
9 #ifndef TU_UTIL_H
10 #define TU_UTIL_H
11
12 #include <assert.h>
13 #include <stdint.h>
14
15 #include "util/macros.h"
16 #include "util/u_math.h"
17 #include "compiler/shader_enums.h"
18
19 #include "adreno_common.xml.h"
20 #include "adreno_pm4.xml.h"
21 #include "a6xx.xml.h"
22
23 #include <vulkan/vulkan.h>
24
25 static inline gl_shader_stage
26 vk_to_mesa_shader_stage(VkShaderStageFlagBits vk_stage)
27 {
28 assert(__builtin_popcount(vk_stage) == 1);
29 return util_logbase2(vk_stage);
30 }
31
32 static inline VkShaderStageFlagBits
33 mesa_to_vk_shader_stage(gl_shader_stage mesa_stage)
34 {
35 return 1 << mesa_stage;
36 }
37
38 #define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
39
40 #define tu_foreach_stage(stage, stage_bits) \
41 for (gl_shader_stage stage, \
42 __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK); \
43 stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
44
45 static inline enum a3xx_msaa_samples
46 tu_msaa_samples(VkSampleCountFlagBits samples)
47 {
48 assert(__builtin_popcount(samples) == 1);
49 return util_logbase2(samples);
50 }
51
52 static inline uint32_t
53 tu6_stage2opcode(gl_shader_stage stage)
54 {
55 if (stage == MESA_SHADER_FRAGMENT || stage == MESA_SHADER_COMPUTE)
56 return CP_LOAD_STATE6_FRAG;
57 return CP_LOAD_STATE6_GEOM;
58 }
59
60 static inline enum a6xx_state_block
61 tu6_stage2texsb(gl_shader_stage stage)
62 {
63 return SB6_VS_TEX + stage;
64 }
65
66 static inline enum a6xx_state_block
67 tu6_stage2shadersb(gl_shader_stage stage)
68 {
69 return SB6_VS_SHADER + stage;
70 }
71
72 static inline enum a3xx_rop_code
73 tu6_rop(VkLogicOp op)
74 {
75 /* note: hw enum matches the VK enum, but with the 4 bits reversed */
76 static const uint8_t lookup[] = {
77 [VK_LOGIC_OP_CLEAR] = ROP_CLEAR,
78 [VK_LOGIC_OP_AND] = ROP_AND,
79 [VK_LOGIC_OP_AND_REVERSE] = ROP_AND_REVERSE,
80 [VK_LOGIC_OP_COPY] = ROP_COPY,
81 [VK_LOGIC_OP_AND_INVERTED] = ROP_AND_INVERTED,
82 [VK_LOGIC_OP_NO_OP] = ROP_NOOP,
83 [VK_LOGIC_OP_XOR] = ROP_XOR,
84 [VK_LOGIC_OP_OR] = ROP_OR,
85 [VK_LOGIC_OP_NOR] = ROP_NOR,
86 [VK_LOGIC_OP_EQUIVALENT] = ROP_EQUIV,
87 [VK_LOGIC_OP_INVERT] = ROP_INVERT,
88 [VK_LOGIC_OP_OR_REVERSE] = ROP_OR_REVERSE,
89 [VK_LOGIC_OP_COPY_INVERTED] = ROP_COPY_INVERTED,
90 [VK_LOGIC_OP_OR_INVERTED] = ROP_OR_INVERTED,
91 [VK_LOGIC_OP_NAND] = ROP_NAND,
92 [VK_LOGIC_OP_SET] = ROP_SET,
93 };
94 assert(op < ARRAY_SIZE(lookup));
95 return lookup[op];
96 }
97
98 static inline enum pc_di_primtype
99 tu6_primtype(VkPrimitiveTopology topology)
100 {
101 static const uint8_t lookup[] = {
102 [VK_PRIMITIVE_TOPOLOGY_POINT_LIST] = DI_PT_POINTLIST,
103 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST] = DI_PT_LINELIST,
104 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP] = DI_PT_LINESTRIP,
105 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST] = DI_PT_TRILIST,
106 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP] = DI_PT_TRISTRIP,
107 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN] = DI_PT_TRIFAN,
108 [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY] = DI_PT_LINE_ADJ,
109 [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY] = DI_PT_LINESTRIP_ADJ,
110 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY] = DI_PT_TRI_ADJ,
111 [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = DI_PT_TRISTRIP_ADJ,
112 /* Return PATCH0 and update in tu_pipeline_builder_parse_tessellation */
113 [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST] = DI_PT_PATCHES0,
114 };
115 assert(topology < ARRAY_SIZE(lookup));
116 return lookup[topology];
117 }
118
119 static inline enum adreno_compare_func
120 tu6_compare_func(VkCompareOp op)
121 {
122 return (enum adreno_compare_func) op;
123 }
124
125 static inline enum adreno_stencil_op
126 tu6_stencil_op(VkStencilOp op)
127 {
128 return (enum adreno_stencil_op) op;
129 }
130
131 static inline enum adreno_rb_blend_factor
132 tu6_blend_factor(VkBlendFactor factor)
133 {
134 static const uint8_t lookup[] = {
135 [VK_BLEND_FACTOR_ZERO] = FACTOR_ZERO,
136 [VK_BLEND_FACTOR_ONE] = FACTOR_ONE,
137 [VK_BLEND_FACTOR_SRC_COLOR] = FACTOR_SRC_COLOR,
138 [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR] = FACTOR_ONE_MINUS_SRC_COLOR,
139 [VK_BLEND_FACTOR_DST_COLOR] = FACTOR_DST_COLOR,
140 [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR] = FACTOR_ONE_MINUS_DST_COLOR,
141 [VK_BLEND_FACTOR_SRC_ALPHA] = FACTOR_SRC_ALPHA,
142 [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA] = FACTOR_ONE_MINUS_SRC_ALPHA,
143 [VK_BLEND_FACTOR_DST_ALPHA] = FACTOR_DST_ALPHA,
144 [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA] = FACTOR_ONE_MINUS_DST_ALPHA,
145 [VK_BLEND_FACTOR_CONSTANT_COLOR] = FACTOR_CONSTANT_COLOR,
146 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR]= FACTOR_ONE_MINUS_CONSTANT_COLOR,
147 [VK_BLEND_FACTOR_CONSTANT_ALPHA] = FACTOR_CONSTANT_ALPHA,
148 [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA]= FACTOR_ONE_MINUS_CONSTANT_ALPHA,
149 [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE] = FACTOR_SRC_ALPHA_SATURATE,
150 [VK_BLEND_FACTOR_SRC1_COLOR] = FACTOR_SRC1_COLOR,
151 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR] = FACTOR_ONE_MINUS_SRC1_COLOR,
152 [VK_BLEND_FACTOR_SRC1_ALPHA] = FACTOR_SRC1_ALPHA,
153 [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA] = FACTOR_ONE_MINUS_SRC1_ALPHA,
154 };
155 assert(factor < ARRAY_SIZE(lookup));
156 return lookup[factor];
157 }
158
159 static inline enum a3xx_rb_blend_opcode
160 tu6_blend_op(VkBlendOp op)
161 {
162 return (enum a3xx_rb_blend_opcode) op;
163 }
164
165 static inline enum a6xx_tex_type
166 tu6_tex_type(VkImageViewType type, bool storage)
167 {
168 switch (type) {
169 default:
170 case VK_IMAGE_VIEW_TYPE_1D:
171 case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
172 return A6XX_TEX_1D;
173 case VK_IMAGE_VIEW_TYPE_2D:
174 case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
175 return A6XX_TEX_2D;
176 case VK_IMAGE_VIEW_TYPE_3D:
177 return A6XX_TEX_3D;
178 case VK_IMAGE_VIEW_TYPE_CUBE:
179 case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
180 return storage ? A6XX_TEX_2D : A6XX_TEX_CUBE;
181 }
182 }
183
184 static inline enum a6xx_tex_clamp
185 tu6_tex_wrap(VkSamplerAddressMode address_mode)
186 {
187 uint8_t lookup[] = {
188 [VK_SAMPLER_ADDRESS_MODE_REPEAT] = A6XX_TEX_REPEAT,
189 [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT] = A6XX_TEX_MIRROR_REPEAT,
190 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE] = A6XX_TEX_CLAMP_TO_EDGE,
191 [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER] = A6XX_TEX_CLAMP_TO_BORDER,
192 [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE] = A6XX_TEX_MIRROR_CLAMP,
193 };
194 assert(address_mode < ARRAY_SIZE(lookup));
195 return lookup[address_mode];
196 }
197
198 static inline enum a6xx_tex_filter
199 tu6_tex_filter(VkFilter filter, unsigned aniso)
200 {
201 switch (filter) {
202 case VK_FILTER_NEAREST:
203 return A6XX_TEX_NEAREST;
204 case VK_FILTER_LINEAR:
205 return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;
206 case VK_FILTER_CUBIC_EXT:
207 return A6XX_TEX_CUBIC;
208 default:
209 unreachable("illegal texture filter");
210 break;
211 }
212 }
213
214 static inline enum a6xx_reduction_mode
215 tu6_reduction_mode(VkSamplerReductionMode reduction_mode)
216 {
217 return (enum a6xx_reduction_mode) reduction_mode;
218 }
219
220 static inline enum a6xx_depth_format
221 tu6_pipe2depth(VkFormat format)
222 {
223 switch (format) {
224 case VK_FORMAT_D16_UNORM:
225 return DEPTH6_16;
226 case VK_FORMAT_X8_D24_UNORM_PACK32:
227 case VK_FORMAT_D24_UNORM_S8_UINT:
228 return DEPTH6_24_8;
229 case VK_FORMAT_D32_SFLOAT:
230 case VK_FORMAT_S8_UINT:
231 return DEPTH6_32;
232 default:
233 return ~0;
234 }
235 }
236
237 static inline enum a6xx_polygon_mode
238 tu6_polygon_mode(VkPolygonMode mode)
239 {
240 switch (mode) {
241 case VK_POLYGON_MODE_POINT:
242 return POLYMODE6_POINTS;
243 case VK_POLYGON_MODE_LINE:
244 return POLYMODE6_LINES;
245 case VK_POLYGON_MODE_FILL:
246 return POLYMODE6_TRIANGLES;
247 default:
248 unreachable("bad polygon mode");
249 }
250 }
251
252 #endif /* TU_UTIL_H */