lima,panfrost: Move lima_tiling.c/h to /src/panfrost
[mesa.git] / src / panfrost / shared / pan_tiling.c
1 /*
2 * Copyright (c) 2011-2013 Luc Verhaegen <libv@skynet.be>
3 * Copyright (c) 2018 Alyssa Rosenzweig <alyssa@rosenzweig.io>
4 * Copyright (c) 2018 Vasily Khoruzhick <anarsoul@gmail.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "pan_tiling.h"
28
29 uint32_t space_filler[16][16] = {
30 { 0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85, },
31 { 3, 2, 7, 6, 19, 18, 23, 22, 67, 66, 71, 70, 83, 82, 87, 86, },
32 { 12, 13, 8, 9, 28, 29, 24, 25, 76, 77, 72, 73, 92, 93, 88, 89, },
33 { 15, 14, 11, 10, 31, 30, 27, 26, 79, 78, 75, 74, 95, 94, 91, 90, },
34 { 48, 49, 52, 53, 32, 33, 36, 37, 112, 113, 116, 117, 96, 97, 100, 101, },
35 { 51, 50, 55, 54, 35, 34, 39, 38, 115, 114, 119, 118, 99, 98, 103, 102, },
36 { 60, 61, 56, 57, 44, 45, 40, 41, 124, 125, 120, 121, 108, 109, 104, 105, },
37 { 63, 62, 59, 58, 47, 46, 43, 42, 127, 126, 123, 122, 111, 110, 107, 106, },
38 { 192, 193, 196, 197, 208, 209, 212, 213, 128, 129, 132, 133, 144, 145, 148, 149, },
39 { 195, 194, 199, 198, 211, 210, 215, 214, 131, 130, 135, 134, 147, 146, 151, 150, },
40 { 204, 205, 200, 201, 220, 221, 216, 217, 140, 141, 136, 137, 156, 157, 152, 153, },
41 { 207, 206, 203, 202, 223, 222, 219, 218, 143, 142, 139, 138, 159, 158, 155, 154, },
42 { 240, 241, 244, 245, 224, 225, 228, 229, 176, 177, 180, 181, 160, 161, 164, 165, },
43 { 243, 242, 247, 246, 227, 226, 231, 230, 179, 178, 183, 182, 163, 162, 167, 166, },
44 { 252, 253, 248, 249, 236, 237, 232, 233, 188, 189, 184, 185, 172, 173, 168, 169, },
45 { 255, 254, 251, 250, 239, 238, 235, 234, 191, 190, 187, 186, 175, 174, 171, 170, },
46 };
47
48 static void
49 panfrost_store_tiled_image_bpp4(void *dst, const void *src,
50 const struct pipe_box *box,
51 uint32_t dst_stride,
52 uint32_t src_stride)
53 {
54 for (int y = box->y, src_y = 0; src_y < box->height; ++y, ++src_y) {
55 int block_y = y & ~0x0f;
56 int rem_y = y & 0x0F;
57 int block_start_s = block_y * dst_stride;
58 int source_start = src_y * src_stride;
59
60 for (int x = box->x, src_x = 0; src_x < box->width; ++x, ++src_x) {
61 int block_x_s = (x >> 4) * 256;
62 int rem_x = x & 0x0F;
63
64 int index = space_filler[rem_y][rem_x];
65 const uint32_t *source = src + source_start + 4 * src_x;
66 uint32_t *dest = dst + block_start_s + 4 * (block_x_s + index);
67
68 *dest = *source;
69 }
70 }
71 }
72
73 static void
74 panfrost_store_tiled_image_generic(void *dst, const void *src,
75 const struct pipe_box *box,
76 uint32_t dst_stride,
77 uint32_t src_stride,
78 uint32_t bpp)
79 {
80 for (int y = box->y, src_y = 0; src_y < box->height; ++y, ++src_y) {
81 int block_y = y & ~0x0f;
82 int rem_y = y & 0x0F;
83 int block_start_s = block_y * dst_stride;
84 int source_start = src_y * src_stride;
85
86 for (int x = box->x, src_x = 0; src_x < box->width; ++x, ++src_x) {
87 int block_x_s = (x >> 4) * 256;
88 int rem_x = x & 0x0F;
89
90 int index = space_filler[rem_y][rem_x];
91 const uint8_t *src8 = src;
92 const uint8_t *source = &src8[source_start + bpp * src_x];
93 uint8_t *dest = dst + block_start_s + bpp * (block_x_s + index);
94
95 for (int b = 0; b < bpp; ++b)
96 dest[b] = source[b];
97 }
98 }
99 }
100
101 static void
102 panfrost_load_tiled_image_bpp4(void *dst, const void *src,
103 const struct pipe_box *box,
104 uint32_t dst_stride,
105 uint32_t src_stride)
106 {
107 for (int y = box->y, dest_y = 0; dest_y < box->height; ++y, ++dest_y) {
108 int block_y = y & ~0x0f;
109 int rem_y = y & 0x0F;
110 int block_start_s = block_y * src_stride;
111 int dest_start = dest_y * dst_stride;
112
113 for (int x = box->x, dest_x = 0; dest_x < box->width; ++x, ++dest_x) {
114 int block_x_s = (x >> 4) * 256;
115 int rem_x = x & 0x0F;
116
117 int index = space_filler[rem_y][rem_x];
118 uint32_t *dest = dst + dest_start + 4 * dest_x;
119 const uint32_t *source = src + block_start_s + 4 * (block_x_s + index);
120
121 *dest = *source;
122 }
123 }
124 }
125
126 static void
127 panfrost_load_tiled_image_generic(void *dst, const void *src,
128 const struct pipe_box *box,
129 uint32_t dst_stride,
130 uint32_t src_stride,
131 uint32_t bpp)
132 {
133 for (int y = box->y, dest_y = 0; dest_y < box->height; ++y, ++dest_y) {
134 int block_y = y & ~0x0f;
135 int rem_y = y & 0x0F;
136 int block_start_s = block_y * src_stride;
137 int dest_start = dest_y * dst_stride;
138
139 for (int x = box->x, dest_x = 0; dest_x < box->width; ++x, ++dest_x) {
140 int block_x_s = (x >> 4) * 256;
141 int rem_x = x & 0x0F;
142
143 int index = space_filler[rem_y][rem_x];
144 uint8_t *dst8 = dst;
145 uint8_t *dest = &dst8[dest_start + bpp * dest_x];
146 const uint8_t *source = src + block_start_s + bpp * (block_x_s + index);
147
148 for (int b = 0; b < bpp; ++b)
149 dest[b] = source[b];
150 }
151 }
152 }
153
154 void
155 panfrost_store_tiled_image(void *dst, const void *src,
156 const struct pipe_box *box,
157 uint32_t dst_stride,
158 uint32_t src_stride,
159 uint32_t bpp)
160 {
161 switch (bpp) {
162 case 4:
163 panfrost_store_tiled_image_bpp4(dst, src, box, dst_stride, src_stride);
164 break;
165 default:
166 panfrost_store_tiled_image_generic(dst, src, box, dst_stride, src_stride, bpp);
167 }
168 }
169
170 void
171 panfrost_load_tiled_image(void *dst, const void *src,
172 const struct pipe_box *box,
173 uint32_t dst_stride,
174 uint32_t src_stride,
175 uint32_t bpp)
176 {
177 switch (bpp) {
178 case 4:
179 panfrost_load_tiled_image_bpp4(dst, src, box, dst_stride, src_stride);
180 break;
181 default:
182 panfrost_load_tiled_image_generic(dst, src, box, dst_stride, src_stride, bpp);
183 }
184 }