anv: Add func anv_get_isl_format()
[mesa.git] / src / vulkan / isl_priv.h
1 /*
2 * Copyright 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #pragma once
25
26 #include <assert.h>
27
28 #include "brw_device_info.h"
29 #include "mesa/main/imports.h"
30 #include "util/macros.h"
31
32 #include "isl.h"
33
34 #define isl_finishme(format, ...) \
35 __isl_finishme(__FILE__, __LINE__, format, ##__VA_ARGS__)
36
37 void PRINTFLIKE(3, 4) UNUSED
38 __isl_finishme(const char *file, int line, const char *fmt, ...);
39
40 #define MIN(a, b) ((a) < (b) ? (a) : (b))
41 #define MAX(a, b) ((a) > (b) ? (a) : (b))
42
43 static inline uint32_t
44 ffs(uint32_t n) {
45 return __builtin_ffs(n);
46 }
47
48 static inline bool
49 isl_is_pow2(uintmax_t n)
50 {
51 return !(n & (n - 1));
52 }
53
54 /**
55 * Alignment must be a power of 2.
56 */
57 static inline bool
58 isl_is_aligned(uintmax_t n, uintmax_t a)
59 {
60 assert(isl_is_pow2(a));
61 return (n & (a - 1)) == 0;
62 }
63
64 /**
65 * Alignment must be a power of 2.
66 */
67 static inline uintmax_t
68 isl_align(uintmax_t n, uintmax_t a)
69 {
70 assert(isl_is_pow2(a));
71 return (n + a - 1) & ~(a - 1);
72 }
73
74 static inline uintmax_t
75 isl_align_npot(uintmax_t n, uintmax_t a)
76 {
77 assert(a > 0);
78 return ((n + a - 1) / a) * a;
79 }
80
81 /**
82 * Alignment must be a power of 2.
83 */
84 static inline uintmax_t
85 isl_align_div(uintmax_t n, uintmax_t a)
86 {
87 return isl_align(n, a) / a;
88 }
89
90 static inline uintmax_t
91 isl_align_div_npot(uintmax_t n, uintmax_t a)
92 {
93 return isl_align_npot(n, a) / a;
94 }
95
96 /**
97 * Log base 2, rounding towards zero.
98 */
99 static inline uint32_t
100 isl_log2u(uint32_t n)
101 {
102 assert(n != 0);
103 return 31 - __builtin_clz(n);
104 }
105
106 static inline uint32_t
107 isl_minify(uint32_t n, uint32_t levels)
108 {
109 if (unlikely(n == 0))
110 return 0;
111 else
112 return MAX(n >> levels, 1);
113 }
114
115 static inline struct isl_extent3d
116 isl_extent3d_sa_to_el(enum isl_format fmt, struct isl_extent3d extent_sa)
117 {
118 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
119
120 assert(extent_sa.w % fmtl->bw == 0);
121 assert(extent_sa.h % fmtl->bh == 0);
122 assert(extent_sa.d % fmtl->bd == 0);
123
124 return (struct isl_extent3d) {
125 .w = extent_sa.w / fmtl->bw,
126 .h = extent_sa.h / fmtl->bh,
127 .d = extent_sa.d / fmtl->bd,
128 };
129 }
130
131 static inline struct isl_extent3d
132 isl_extent3d_el_to_sa(enum isl_format fmt, struct isl_extent3d extent_el)
133 {
134 const struct isl_format_layout *fmtl = isl_format_get_layout(fmt);
135
136 return (struct isl_extent3d) {
137 .w = extent_el.w * fmtl->bw,
138 .h = extent_el.h * fmtl->bh,
139 .d = extent_el.d * fmtl->bd,
140 };
141 }