Debian Codegraph @

src/panfrost/shared/pan_tiling.h

raw
1 /*
2 * Copyright (c) 2011-2013 Luc Verhaegen <[email protected]>
3 * Copyright (c) 2018 Alyssa Rosenzweig
4 * Copyright (c) 2018 Vasily Khoruzhick <[email protected]>
5 * SPDX-License-Identifier: MIT
6 */
7
8 #ifndef H_PANFROST_TILING
9 #define H_PANFROST_TILING
10
11 #include <stdint.h>
12 #include "util/bitscan.h"
13 #include <util/format/u_format.h>
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 /* The depth and stencil aspects of a Z24_UNORM_S8_UINT image are interleaved,
20 * where the bottom 24 bits are depth and the top 8 bits are stencil. When
21 * copying to/from a Z24S8 tiled image, the pan_interleave_zs enum specifies
22 * whether to (de)interleave the depth/stencil aspects */
23 enum pan_interleave_zs {
24 /* Copy all aspects, no interleaving */
25 PAN_INTERLEAVE_NONE,
26 /* Copy only the depth aspect of a Z24S8 tiled image to/from linear Z24X8 */
27 PAN_INTERLEAVE_DEPTH,
28 /* Copy only the stencil aspect of a Z24S8 tiled image to/from linear S8 */
29 PAN_INTERLEAVE_STENCIL,
30 };
31
32 /**
33 * Get the appropriate pan_interleave_zs mode for copying to/from a given
34 * format.
35 *
36 * @depth Whether to copy the depth aspect
37 * @stencil Whether to copy the stencil aspect
38 */
39 enum pan_interleave_zs
40 pan_get_interleave_zs(enum pipe_format format, bool depth, bool stencil);
41
42 /**
43 * Load a rectangular region from a tiled image to a linear staging image.
44 *
45 * @dst Linear destination
46 * @src Tiled source
47 * @x Region of interest of source in pixels, aligned to block size
48 * @y Region of interest of source in pixels, aligned to block size
49 * @w Region of interest of source in pixels, aligned to block size
50 * @h Region of interest of source in pixels, aligned to block size
51 * @dst_stride Stride in bytes of linear destination
52 * @src_stride Number of bytes between adjacent rows of tiles in source.
53 * @format Format of the source and destination image
54 * @interleave How to deinterleave ZS aspects from the tiled image
55 */
56 void pan_load_tiled_image(void *dst, const void *src, unsigned x, unsigned y,
57 unsigned w, unsigned h, uint32_t dst_stride,
58 uint32_t src_stride, enum pipe_format format,
59 enum pan_interleave_zs interleave);
60
61 /**
62 * Store a linear staging image to a rectangular region of a tiled image.
63 *
64 * @dst Tiled destination
65 * @src Linear source
66 * @x Region of interest of destination in pixels, aligned to block size
67 * @y Region of interest of destination in pixels, aligned to block size
68 * @w Region of interest of destination in pixels, aligned to block size
69 * @h Region of interest of destination in pixels, aligned to block size
70 * @dst_stride Number of bytes between adjacent rows of tiles in destination.
71 * @src_stride Stride in bytes of linear source
72 * @format Format of the source and destination image
73 * @interleave How to interleave a ZS aspects to the tiled image
74 */
75 void pan_store_tiled_image(void *dst, const void *src, unsigned x, unsigned y,
76 unsigned w, unsigned h, uint32_t dst_stride,
77 uint32_t src_stride, enum pipe_format format,
78 enum pan_interleave_zs interleave);
79
80 /**
81 * Copy a rectangular region from one tiled image to another.
82 *
83 * @dst Tiled destination
84 * @src Tiled source
85 * @dst_x Region of interest of destination in pixels, aligned to block size
86 * @dst_y Region of interest of destination in pixels, aligned to block size
87 * @src_x Region of interest of source in pixels, aligned to block size
88 * @src_y Region of interest of source in pixels, aligned to block size
89 * @w Size of region of interest in pixels, aligned to block size
90 * @h Size of region of interest in pixels, aligned to block size
91 * @dst_stride Number of bytes between adjacent rows of tiles in destination.
92 * @src_stride Number of bytes between adjacent rows of tiles in source.
93 * @format Format of the source and destination image
94 */
95 void pan_copy_tiled_image(void *dst, const void *src, unsigned dst_x,
96 unsigned dst_y, unsigned src_x, unsigned src_y,
97 unsigned w, unsigned h, uint32_t dst_stride,
98 uint32_t src_stride, enum pipe_format format);
99
100 static inline
101 void *assume_pixel_aligned(void *ptr, const unsigned pixel_size)
102 {
103 if (pixel_size == 1)
104 return __builtin_assume_aligned(ptr, 1);
105 else if (pixel_size == 2)
106 return __builtin_assume_aligned(ptr, 2);
107 else if (pixel_size == 4)
108 return __builtin_assume_aligned(ptr, 4);
109 else if (pixel_size == 8)
110 return __builtin_assume_aligned(ptr, 8);
111 else if (pixel_size == 16)
112 return __builtin_assume_aligned(ptr, 16);
113 else
114 UNREACHABLE("unexpected pixel size");
115 }
116
117 static inline
118 void pan_access_image_pixel(void *dst, void *src, const unsigned pixel_size,
119 enum pan_interleave_zs interleave, bool is_store)
120 {
121 if (util_is_power_of_two_nonzero(pixel_size)) {
122 src = assume_pixel_aligned(src, pixel_size);
123 if (interleave != PAN_INTERLEAVE_STENCIL)
124 dst = assume_pixel_aligned(dst, pixel_size);
125 }
126
127 switch (interleave) {
128 case PAN_INTERLEAVE_NONE:
129 if (is_store)
130 memcpy(dst, src, pixel_size);
131 else
132 memcpy(src, dst, pixel_size);
133 break;
134 case PAN_INTERLEAVE_DEPTH:
135 /* interleave only applies to Z24S8 */
136 assert(pixel_size == 4);
137 if (is_store) {
138 uint32_t src_pixel = *(uint32_t *) src;
139 *(uint16_t *) dst = src_pixel & 0xffff;
140 *((uint8_t *) dst + 2) = (src_pixel >> 16) & 0xff;
141 } else {
142 /* The top 8 bits of Z24X8 are unused, so we can overwrite them
143 * with zeros in a single 32B write, instead of needing separate
144 * 16B and 8B writes */
145 *(uint32_t *) src = *(uint32_t *) dst & 0xffffff;
146 }
147 break;
148 case PAN_INTERLEAVE_STENCIL:
149 /* interleave only applies to Z24S8 */
150 assert(pixel_size == 4);
151 if (is_store)
152 *((uint8_t *) dst + 3) = *(uint8_t *) src;
153 else
154 *(uint8_t *) src = *((uint8_t *) dst + 3);
155 break;
156 }
157 }
158
159
160 #ifdef __cplusplus
161 } /* extern C */
162 #endif
163
164 #endif