1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2020 Valve Corporation
3bf215546Sopenharmony_ci * SPDX-License-Identifier: MIT
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Authors:
6bf215546Sopenharmony_ci *    Jonathan Marek <jonathan@marek.ca>
7bf215546Sopenharmony_ci */
8bf215546Sopenharmony_ci
9bf215546Sopenharmony_ci#ifndef TU_UTIL_H
10bf215546Sopenharmony_ci#define TU_UTIL_H
11bf215546Sopenharmony_ci
12bf215546Sopenharmony_ci#include "tu_common.h"
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_ci#include "util/u_math.h"
15bf215546Sopenharmony_ci#include "util/format/u_format_pack.h"
16bf215546Sopenharmony_ci#include "util/format/u_format_zs.h"
17bf215546Sopenharmony_ci#include "compiler/shader_enums.h"
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_ci#include "vk_util.h"
20bf215546Sopenharmony_ci
21bf215546Sopenharmony_ci/* Whenever we generate an error, pass it through this function. Useful for
22bf215546Sopenharmony_ci * debugging, where we can break on it. Only call at error site, not when
23bf215546Sopenharmony_ci * propagating errors. Might be useful to plug in a stack trace here.
24bf215546Sopenharmony_ci */
25bf215546Sopenharmony_ci
26bf215546Sopenharmony_ciVkResult
27bf215546Sopenharmony_ci__vk_startup_errorf(struct tu_instance *instance,
28bf215546Sopenharmony_ci                    VkResult error,
29bf215546Sopenharmony_ci                    bool force_print,
30bf215546Sopenharmony_ci                    const char *file,
31bf215546Sopenharmony_ci                    int line,
32bf215546Sopenharmony_ci                    const char *format,
33bf215546Sopenharmony_ci                    ...) PRINTFLIKE(6, 7);
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci/* Prints startup errors if TU_DEBUG=startup is set or on a debug driver
36bf215546Sopenharmony_ci * build.
37bf215546Sopenharmony_ci */
38bf215546Sopenharmony_ci#define vk_startup_errorf(instance, error, format, ...) \
39bf215546Sopenharmony_ci   __vk_startup_errorf(instance, error, \
40bf215546Sopenharmony_ci                       instance->debug_flags & TU_DEBUG_STARTUP, \
41bf215546Sopenharmony_ci                       __FILE__, __LINE__, format, ##__VA_ARGS__)
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_civoid
44bf215546Sopenharmony_ci__tu_finishme(const char *file, int line, const char *format, ...)
45bf215546Sopenharmony_ci   PRINTFLIKE(3, 4);
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci/**
48bf215546Sopenharmony_ci * Print a FINISHME message, including its source location.
49bf215546Sopenharmony_ci */
50bf215546Sopenharmony_ci#define tu_finishme(format, ...)                                             \
51bf215546Sopenharmony_ci   do {                                                                      \
52bf215546Sopenharmony_ci      static bool reported = false;                                          \
53bf215546Sopenharmony_ci      if (!reported) {                                                       \
54bf215546Sopenharmony_ci         __tu_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__);           \
55bf215546Sopenharmony_ci         reported = true;                                                    \
56bf215546Sopenharmony_ci      }                                                                      \
57bf215546Sopenharmony_ci   } while (0)
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci#define tu_stub()                                                            \
60bf215546Sopenharmony_ci   do {                                                                      \
61bf215546Sopenharmony_ci      tu_finishme("stub %s", __func__);                                      \
62bf215546Sopenharmony_ci   } while (0)
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_civoid
65bf215546Sopenharmony_citu_framebuffer_tiling_config(struct tu_framebuffer *fb,
66bf215546Sopenharmony_ci                             const struct tu_device *device,
67bf215546Sopenharmony_ci                             const struct tu_render_pass *pass);
68bf215546Sopenharmony_ci
69bf215546Sopenharmony_ci#define TU_STAGE_MASK ((1 << MESA_SHADER_STAGES) - 1)
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci#define tu_foreach_stage(stage, stage_bits)                                  \
72bf215546Sopenharmony_ci   for (gl_shader_stage stage,                                               \
73bf215546Sopenharmony_ci        __tmp = (gl_shader_stage)((stage_bits) &TU_STAGE_MASK);              \
74bf215546Sopenharmony_ci        stage = __builtin_ffs(__tmp) - 1, __tmp; __tmp &= ~(1 << (stage)))
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_cistatic inline enum a3xx_msaa_samples
77bf215546Sopenharmony_citu_msaa_samples(uint32_t samples)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci   assert(__builtin_popcount(samples) == 1);
80bf215546Sopenharmony_ci   return util_logbase2(samples);
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic inline uint32_t
84bf215546Sopenharmony_citu6_stage2opcode(gl_shader_stage stage)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   if (stage == MESA_SHADER_FRAGMENT || stage == MESA_SHADER_COMPUTE)
87bf215546Sopenharmony_ci      return CP_LOAD_STATE6_FRAG;
88bf215546Sopenharmony_ci   return CP_LOAD_STATE6_GEOM;
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_cistatic inline enum a6xx_state_block
92bf215546Sopenharmony_citu6_stage2texsb(gl_shader_stage stage)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci   return SB6_VS_TEX + stage;
95bf215546Sopenharmony_ci}
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cistatic inline enum a6xx_state_block
98bf215546Sopenharmony_citu6_stage2shadersb(gl_shader_stage stage)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   return SB6_VS_SHADER + stage;
101bf215546Sopenharmony_ci}
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_cistatic inline enum a3xx_rop_code
104bf215546Sopenharmony_citu6_rop(VkLogicOp op)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   /* note: hw enum matches the VK enum, but with the 4 bits reversed */
107bf215546Sopenharmony_ci   static const uint8_t lookup[] = {
108bf215546Sopenharmony_ci      [VK_LOGIC_OP_CLEAR]           = ROP_CLEAR,
109bf215546Sopenharmony_ci      [VK_LOGIC_OP_AND]             = ROP_AND,
110bf215546Sopenharmony_ci      [VK_LOGIC_OP_AND_REVERSE]     = ROP_AND_REVERSE,
111bf215546Sopenharmony_ci      [VK_LOGIC_OP_COPY]            = ROP_COPY,
112bf215546Sopenharmony_ci      [VK_LOGIC_OP_AND_INVERTED]    = ROP_AND_INVERTED,
113bf215546Sopenharmony_ci      [VK_LOGIC_OP_NO_OP]           = ROP_NOOP,
114bf215546Sopenharmony_ci      [VK_LOGIC_OP_XOR]             = ROP_XOR,
115bf215546Sopenharmony_ci      [VK_LOGIC_OP_OR]              = ROP_OR,
116bf215546Sopenharmony_ci      [VK_LOGIC_OP_NOR]             = ROP_NOR,
117bf215546Sopenharmony_ci      [VK_LOGIC_OP_EQUIVALENT]      = ROP_EQUIV,
118bf215546Sopenharmony_ci      [VK_LOGIC_OP_INVERT]          = ROP_INVERT,
119bf215546Sopenharmony_ci      [VK_LOGIC_OP_OR_REVERSE]      = ROP_OR_REVERSE,
120bf215546Sopenharmony_ci      [VK_LOGIC_OP_COPY_INVERTED]   = ROP_COPY_INVERTED,
121bf215546Sopenharmony_ci      [VK_LOGIC_OP_OR_INVERTED]     = ROP_OR_INVERTED,
122bf215546Sopenharmony_ci      [VK_LOGIC_OP_NAND]            = ROP_NAND,
123bf215546Sopenharmony_ci      [VK_LOGIC_OP_SET]             = ROP_SET,
124bf215546Sopenharmony_ci   };
125bf215546Sopenharmony_ci   assert(op < ARRAY_SIZE(lookup));
126bf215546Sopenharmony_ci   return lookup[op];
127bf215546Sopenharmony_ci}
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_cistatic inline bool
130bf215546Sopenharmony_citu6_primtype_line(enum pc_di_primtype type)
131bf215546Sopenharmony_ci{
132bf215546Sopenharmony_ci    switch(type) {
133bf215546Sopenharmony_ci    case DI_PT_LINELIST:
134bf215546Sopenharmony_ci    case DI_PT_LINESTRIP:
135bf215546Sopenharmony_ci    case DI_PT_LINE_ADJ:
136bf215546Sopenharmony_ci    case DI_PT_LINESTRIP_ADJ:
137bf215546Sopenharmony_ci       return true;
138bf215546Sopenharmony_ci    default:
139bf215546Sopenharmony_ci       return false;
140bf215546Sopenharmony_ci    }
141bf215546Sopenharmony_ci}
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_cistatic inline bool
144bf215546Sopenharmony_citu6_primtype_patches(enum pc_di_primtype type)
145bf215546Sopenharmony_ci{
146bf215546Sopenharmony_ci   return type >= DI_PT_PATCHES0 && type <= DI_PT_PATCHES31;
147bf215546Sopenharmony_ci}
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_cistatic inline enum pc_di_primtype
150bf215546Sopenharmony_citu6_primtype(VkPrimitiveTopology topology)
151bf215546Sopenharmony_ci{
152bf215546Sopenharmony_ci   static const uint8_t lookup[] = {
153bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_POINT_LIST]                    = DI_PT_POINTLIST,
154bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_LINE_LIST]                     = DI_PT_LINELIST,
155bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP]                    = DI_PT_LINESTRIP,
156bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST]                 = DI_PT_TRILIST,
157bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP]                = DI_PT_TRISTRIP,
158bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN]                  = DI_PT_TRIFAN,
159bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY]      = DI_PT_LINE_ADJ,
160bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY]     = DI_PT_LINESTRIP_ADJ,
161bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY]  = DI_PT_TRI_ADJ,
162bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY] = DI_PT_TRISTRIP_ADJ,
163bf215546Sopenharmony_ci      /* Return PATCH0 and update in tu_pipeline_builder_parse_tessellation */
164bf215546Sopenharmony_ci      [VK_PRIMITIVE_TOPOLOGY_PATCH_LIST]                    = DI_PT_PATCHES0,
165bf215546Sopenharmony_ci   };
166bf215546Sopenharmony_ci   assert(topology < ARRAY_SIZE(lookup));
167bf215546Sopenharmony_ci   return lookup[topology];
168bf215546Sopenharmony_ci}
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_cistatic inline enum adreno_compare_func
171bf215546Sopenharmony_citu6_compare_func(VkCompareOp op)
172bf215546Sopenharmony_ci{
173bf215546Sopenharmony_ci   return (enum adreno_compare_func) op;
174bf215546Sopenharmony_ci}
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_cistatic inline enum adreno_stencil_op
177bf215546Sopenharmony_citu6_stencil_op(VkStencilOp op)
178bf215546Sopenharmony_ci{
179bf215546Sopenharmony_ci   return (enum adreno_stencil_op) op;
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistatic inline enum adreno_rb_blend_factor
183bf215546Sopenharmony_citu6_blend_factor(VkBlendFactor factor)
184bf215546Sopenharmony_ci{
185bf215546Sopenharmony_ci   static const uint8_t lookup[] = {
186bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ZERO]                    = FACTOR_ZERO,
187bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE]                     = FACTOR_ONE,
188bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_SRC_COLOR]               = FACTOR_SRC_COLOR,
189bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_SRC_COLOR]     = FACTOR_ONE_MINUS_SRC_COLOR,
190bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_DST_COLOR]               = FACTOR_DST_COLOR,
191bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_DST_COLOR]     = FACTOR_ONE_MINUS_DST_COLOR,
192bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_SRC_ALPHA]               = FACTOR_SRC_ALPHA,
193bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA]     = FACTOR_ONE_MINUS_SRC_ALPHA,
194bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_DST_ALPHA]               = FACTOR_DST_ALPHA,
195bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_DST_ALPHA]     = FACTOR_ONE_MINUS_DST_ALPHA,
196bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_CONSTANT_COLOR]          = FACTOR_CONSTANT_COLOR,
197bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR]= FACTOR_ONE_MINUS_CONSTANT_COLOR,
198bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_CONSTANT_ALPHA]          = FACTOR_CONSTANT_ALPHA,
199bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA]= FACTOR_ONE_MINUS_CONSTANT_ALPHA,
200bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_SRC_ALPHA_SATURATE]      = FACTOR_SRC_ALPHA_SATURATE,
201bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_SRC1_COLOR]              = FACTOR_SRC1_COLOR,
202bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_SRC1_COLOR]    = FACTOR_ONE_MINUS_SRC1_COLOR,
203bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_SRC1_ALPHA]              = FACTOR_SRC1_ALPHA,
204bf215546Sopenharmony_ci      [VK_BLEND_FACTOR_ONE_MINUS_SRC1_ALPHA]    = FACTOR_ONE_MINUS_SRC1_ALPHA,
205bf215546Sopenharmony_ci   };
206bf215546Sopenharmony_ci   assert(factor < ARRAY_SIZE(lookup));
207bf215546Sopenharmony_ci   return lookup[factor];
208bf215546Sopenharmony_ci}
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_cistatic inline enum a3xx_rb_blend_opcode
211bf215546Sopenharmony_citu6_blend_op(VkBlendOp op)
212bf215546Sopenharmony_ci{
213bf215546Sopenharmony_ci   return (enum a3xx_rb_blend_opcode) op;
214bf215546Sopenharmony_ci}
215bf215546Sopenharmony_ci
216bf215546Sopenharmony_cistatic inline enum a6xx_tex_type
217bf215546Sopenharmony_citu6_tex_type(VkImageViewType type, bool storage)
218bf215546Sopenharmony_ci{
219bf215546Sopenharmony_ci   switch (type) {
220bf215546Sopenharmony_ci   default:
221bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_1D:
222bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_1D_ARRAY:
223bf215546Sopenharmony_ci      return A6XX_TEX_1D;
224bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_2D:
225bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_2D_ARRAY:
226bf215546Sopenharmony_ci      return A6XX_TEX_2D;
227bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_3D:
228bf215546Sopenharmony_ci      return A6XX_TEX_3D;
229bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_CUBE:
230bf215546Sopenharmony_ci   case VK_IMAGE_VIEW_TYPE_CUBE_ARRAY:
231bf215546Sopenharmony_ci      return storage ? A6XX_TEX_2D : A6XX_TEX_CUBE;
232bf215546Sopenharmony_ci   }
233bf215546Sopenharmony_ci}
234bf215546Sopenharmony_ci
235bf215546Sopenharmony_cistatic inline enum a6xx_tex_clamp
236bf215546Sopenharmony_citu6_tex_wrap(VkSamplerAddressMode address_mode)
237bf215546Sopenharmony_ci{
238bf215546Sopenharmony_ci   uint8_t lookup[] = {
239bf215546Sopenharmony_ci      [VK_SAMPLER_ADDRESS_MODE_REPEAT]                = A6XX_TEX_REPEAT,
240bf215546Sopenharmony_ci      [VK_SAMPLER_ADDRESS_MODE_MIRRORED_REPEAT]       = A6XX_TEX_MIRROR_REPEAT,
241bf215546Sopenharmony_ci      [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE]         = A6XX_TEX_CLAMP_TO_EDGE,
242bf215546Sopenharmony_ci      [VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER]       = A6XX_TEX_CLAMP_TO_BORDER,
243bf215546Sopenharmony_ci      [VK_SAMPLER_ADDRESS_MODE_MIRROR_CLAMP_TO_EDGE]  = A6XX_TEX_MIRROR_CLAMP,
244bf215546Sopenharmony_ci   };
245bf215546Sopenharmony_ci   assert(address_mode < ARRAY_SIZE(lookup));
246bf215546Sopenharmony_ci   return lookup[address_mode];
247bf215546Sopenharmony_ci}
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_cistatic inline enum a6xx_tex_filter
250bf215546Sopenharmony_citu6_tex_filter(VkFilter filter, unsigned aniso)
251bf215546Sopenharmony_ci{
252bf215546Sopenharmony_ci   switch (filter) {
253bf215546Sopenharmony_ci   case VK_FILTER_NEAREST:
254bf215546Sopenharmony_ci      return A6XX_TEX_NEAREST;
255bf215546Sopenharmony_ci   case VK_FILTER_LINEAR:
256bf215546Sopenharmony_ci      return aniso ? A6XX_TEX_ANISO : A6XX_TEX_LINEAR;
257bf215546Sopenharmony_ci   case VK_FILTER_CUBIC_EXT:
258bf215546Sopenharmony_ci      return A6XX_TEX_CUBIC;
259bf215546Sopenharmony_ci   default:
260bf215546Sopenharmony_ci      unreachable("illegal texture filter");
261bf215546Sopenharmony_ci      break;
262bf215546Sopenharmony_ci   }
263bf215546Sopenharmony_ci}
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_cistatic inline enum a6xx_reduction_mode
266bf215546Sopenharmony_citu6_reduction_mode(VkSamplerReductionMode reduction_mode)
267bf215546Sopenharmony_ci{
268bf215546Sopenharmony_ci   return (enum a6xx_reduction_mode) reduction_mode;
269bf215546Sopenharmony_ci}
270bf215546Sopenharmony_ci
271bf215546Sopenharmony_cistatic inline enum a6xx_depth_format
272bf215546Sopenharmony_citu6_pipe2depth(VkFormat format)
273bf215546Sopenharmony_ci{
274bf215546Sopenharmony_ci   switch (format) {
275bf215546Sopenharmony_ci   case VK_FORMAT_D16_UNORM:
276bf215546Sopenharmony_ci      return DEPTH6_16;
277bf215546Sopenharmony_ci   case VK_FORMAT_X8_D24_UNORM_PACK32:
278bf215546Sopenharmony_ci   case VK_FORMAT_D24_UNORM_S8_UINT:
279bf215546Sopenharmony_ci      return DEPTH6_24_8;
280bf215546Sopenharmony_ci   case VK_FORMAT_D32_SFLOAT:
281bf215546Sopenharmony_ci   case VK_FORMAT_D32_SFLOAT_S8_UINT:
282bf215546Sopenharmony_ci   case VK_FORMAT_S8_UINT:
283bf215546Sopenharmony_ci      return DEPTH6_32;
284bf215546Sopenharmony_ci   default:
285bf215546Sopenharmony_ci      return ~0;
286bf215546Sopenharmony_ci   }
287bf215546Sopenharmony_ci}
288bf215546Sopenharmony_ci
289bf215546Sopenharmony_cistatic inline enum a6xx_polygon_mode
290bf215546Sopenharmony_citu6_polygon_mode(VkPolygonMode mode)
291bf215546Sopenharmony_ci{
292bf215546Sopenharmony_ci   switch (mode) {
293bf215546Sopenharmony_ci   case VK_POLYGON_MODE_POINT:
294bf215546Sopenharmony_ci      return POLYMODE6_POINTS;
295bf215546Sopenharmony_ci   case VK_POLYGON_MODE_LINE:
296bf215546Sopenharmony_ci      return POLYMODE6_LINES;
297bf215546Sopenharmony_ci   case VK_POLYGON_MODE_FILL:
298bf215546Sopenharmony_ci      return POLYMODE6_TRIANGLES;
299bf215546Sopenharmony_ci   default:
300bf215546Sopenharmony_ci      unreachable("bad polygon mode");
301bf215546Sopenharmony_ci   }
302bf215546Sopenharmony_ci}
303bf215546Sopenharmony_ci
304bf215546Sopenharmony_cistruct bcolor_entry {
305bf215546Sopenharmony_ci   uint32_t fp32[4];
306bf215546Sopenharmony_ci   uint64_t ui16;
307bf215546Sopenharmony_ci   uint64_t si16;
308bf215546Sopenharmony_ci   uint64_t fp16;
309bf215546Sopenharmony_ci   uint16_t rgb565;
310bf215546Sopenharmony_ci   uint16_t rgb5a1;
311bf215546Sopenharmony_ci   uint16_t rgba4;
312bf215546Sopenharmony_ci   uint8_t __pad0[2];
313bf215546Sopenharmony_ci   uint32_t ui8;
314bf215546Sopenharmony_ci   uint32_t si8;
315bf215546Sopenharmony_ci   uint32_t rgb10a2;
316bf215546Sopenharmony_ci   uint32_t z24; /* also s8? */
317bf215546Sopenharmony_ci   uint64_t srgb;
318bf215546Sopenharmony_ci   uint8_t  __pad1[56];
319bf215546Sopenharmony_ci} __attribute__((aligned(128)));
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci/* vulkan does not want clamping of integer clear values, differs from u_format
322bf215546Sopenharmony_ci * see spec for VkClearColorValue
323bf215546Sopenharmony_ci */
324bf215546Sopenharmony_cistatic inline void
325bf215546Sopenharmony_cipack_int8(uint32_t *dst, const uint32_t *val)
326bf215546Sopenharmony_ci{
327bf215546Sopenharmony_ci   *dst = (val[0] & 0xff) |
328bf215546Sopenharmony_ci          (val[1] & 0xff) << 8 |
329bf215546Sopenharmony_ci          (val[2] & 0xff) << 16 |
330bf215546Sopenharmony_ci          (val[3] & 0xff) << 24;
331bf215546Sopenharmony_ci}
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_cistatic inline void
334bf215546Sopenharmony_cipack_int10_2(uint32_t *dst, const uint32_t *val)
335bf215546Sopenharmony_ci{
336bf215546Sopenharmony_ci   *dst = (val[0] & 0x3ff) |
337bf215546Sopenharmony_ci          (val[1] & 0x3ff) << 10 |
338bf215546Sopenharmony_ci          (val[2] & 0x3ff) << 20 |
339bf215546Sopenharmony_ci          (val[3] & 0x3)   << 30;
340bf215546Sopenharmony_ci}
341bf215546Sopenharmony_ci
342bf215546Sopenharmony_cistatic inline void
343bf215546Sopenharmony_cipack_int16(uint32_t *dst, const uint32_t *val)
344bf215546Sopenharmony_ci{
345bf215546Sopenharmony_ci   dst[0] = (val[0] & 0xffff) |
346bf215546Sopenharmony_ci            (val[1] & 0xffff) << 16;
347bf215546Sopenharmony_ci   dst[1] = (val[2] & 0xffff) |
348bf215546Sopenharmony_ci            (val[3] & 0xffff) << 16;
349bf215546Sopenharmony_ci}
350bf215546Sopenharmony_ci
351bf215546Sopenharmony_cistatic inline void
352bf215546Sopenharmony_citu6_pack_border_color(struct bcolor_entry *bcolor, const VkClearColorValue *val, bool is_int)
353bf215546Sopenharmony_ci{
354bf215546Sopenharmony_ci   memcpy(bcolor->fp32, val, 4 * sizeof(float));
355bf215546Sopenharmony_ci   if (is_int) {
356bf215546Sopenharmony_ci      pack_int16((uint32_t*) &bcolor->fp16, val->uint32);
357bf215546Sopenharmony_ci      return;
358bf215546Sopenharmony_ci   }
359bf215546Sopenharmony_ci#define PACK_F(x, type) util_format_##type##_pack_rgba_float \
360bf215546Sopenharmony_ci   ( (uint8_t*) (&bcolor->x), 0, val->float32, 0, 1, 1)
361bf215546Sopenharmony_ci   PACK_F(ui16, r16g16b16a16_unorm);
362bf215546Sopenharmony_ci   PACK_F(si16, r16g16b16a16_snorm);
363bf215546Sopenharmony_ci   PACK_F(fp16, r16g16b16a16_float);
364bf215546Sopenharmony_ci   PACK_F(rgb565, r5g6b5_unorm);
365bf215546Sopenharmony_ci   PACK_F(rgb5a1, r5g5b5a1_unorm);
366bf215546Sopenharmony_ci   PACK_F(rgba4, r4g4b4a4_unorm);
367bf215546Sopenharmony_ci   PACK_F(ui8, r8g8b8a8_unorm);
368bf215546Sopenharmony_ci   PACK_F(si8, r8g8b8a8_snorm);
369bf215546Sopenharmony_ci   PACK_F(rgb10a2, r10g10b10a2_unorm);
370bf215546Sopenharmony_ci   util_format_z24x8_unorm_pack_z_float((uint8_t*) &bcolor->z24,
371bf215546Sopenharmony_ci                                        0, val->float32, 0, 1, 1);
372bf215546Sopenharmony_ci   PACK_F(srgb, r16g16b16a16_float); /* TODO: clamp? */
373bf215546Sopenharmony_ci#undef PACK_F
374bf215546Sopenharmony_ci}
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_civoid
377bf215546Sopenharmony_citu_dbg_log_gmem_load_store_skips(struct tu_device *device);
378bf215546Sopenharmony_ci
379bf215546Sopenharmony_ci#define perf_debug(device, fmt, ...) do {                               \
380bf215546Sopenharmony_ci   if (unlikely((device)->instance->debug_flags & TU_DEBUG_PERF))       \
381bf215546Sopenharmony_ci      mesa_log(MESA_LOG_WARN, (MESA_LOG_TAG), (fmt), ##__VA_ARGS__);    \
382bf215546Sopenharmony_ci} while(0)
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci#endif /* TU_UTIL_H */
385