radeonsi: update copyrights
[mesa.git] / src / gallium / drivers / radeonsi / si_test_dma.c
1 /*
2 * Copyright 2016 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 /* This file implements randomized SDMA texture blit tests. */
27
28 #include "si_pipe.h"
29 #include "util/u_surface.h"
30 #include "util/rand_xor.h"
31
32 static uint64_t seed_xorshift128plus[2];
33
34 #define RAND_NUM_SIZE 8
35
36 /* The GPU blits are emulated on the CPU using these CPU textures. */
37
38 struct cpu_texture {
39 uint8_t *ptr;
40 uint64_t size;
41 uint64_t layer_stride;
42 unsigned stride;
43 };
44
45 static void alloc_cpu_texture(struct cpu_texture *tex,
46 struct pipe_resource *templ, int bpp)
47 {
48 tex->stride = align(templ->width0 * bpp, RAND_NUM_SIZE);
49 tex->layer_stride = (uint64_t)tex->stride * templ->height0;
50 tex->size = tex->layer_stride * templ->array_size;
51 tex->ptr = malloc(tex->size);
52 assert(tex->ptr);
53 }
54
55 static void set_random_pixels(struct pipe_context *ctx,
56 struct pipe_resource *tex,
57 struct cpu_texture *cpu)
58 {
59 struct pipe_transfer *t;
60 uint8_t *map;
61 int x,y,z;
62
63 map = pipe_transfer_map_3d(ctx, tex, 0, PIPE_TRANSFER_WRITE,
64 0, 0, 0, tex->width0, tex->height0,
65 tex->array_size, &t);
66 assert(map);
67
68 for (z = 0; z < tex->array_size; z++) {
69 for (y = 0; y < tex->height0; y++) {
70 uint64_t *ptr = (uint64_t*)
71 (map + t->layer_stride*z + t->stride*y);
72 uint64_t *ptr_cpu = (uint64_t*)
73 (cpu->ptr + cpu->layer_stride*z + cpu->stride*y);
74 unsigned size = cpu->stride / RAND_NUM_SIZE;
75
76 assert(t->stride % RAND_NUM_SIZE == 0);
77 assert(cpu->stride % RAND_NUM_SIZE == 0);
78
79 for (x = 0; x < size; x++) {
80 *ptr++ = *ptr_cpu++ =
81 rand_xorshift128plus(seed_xorshift128plus);
82 }
83 }
84 }
85
86 pipe_transfer_unmap(ctx, t);
87 }
88
89 static bool compare_textures(struct pipe_context *ctx,
90 struct pipe_resource *tex,
91 struct cpu_texture *cpu, int bpp)
92 {
93 struct pipe_transfer *t;
94 uint8_t *map;
95 int y,z;
96 bool pass = true;
97
98 map = pipe_transfer_map_3d(ctx, tex, 0, PIPE_TRANSFER_READ,
99 0, 0, 0, tex->width0, tex->height0,
100 tex->array_size, &t);
101 assert(map);
102
103 for (z = 0; z < tex->array_size; z++) {
104 for (y = 0; y < tex->height0; y++) {
105 uint8_t *ptr = map + t->layer_stride*z + t->stride*y;
106 uint8_t *cpu_ptr = cpu->ptr +
107 cpu->layer_stride*z + cpu->stride*y;
108
109 if (memcmp(ptr, cpu_ptr, tex->width0 * bpp)) {
110 pass = false;
111 goto done;
112 }
113 }
114 }
115 done:
116 pipe_transfer_unmap(ctx, t);
117 return pass;
118 }
119
120 static enum pipe_format get_format_from_bpp(int bpp)
121 {
122 switch (bpp) {
123 case 1:
124 return PIPE_FORMAT_R8_UINT;
125 case 2:
126 return PIPE_FORMAT_R16_UINT;
127 case 4:
128 return PIPE_FORMAT_R32_UINT;
129 case 8:
130 return PIPE_FORMAT_R32G32_UINT;
131 case 16:
132 return PIPE_FORMAT_R32G32B32A32_UINT;
133 default:
134 assert(0);
135 return PIPE_FORMAT_NONE;
136 }
137 }
138
139 static const char *array_mode_to_string(struct si_screen *sscreen,
140 struct radeon_surf *surf)
141 {
142 if (sscreen->info.chip_class >= GFX9) {
143 switch (surf->u.gfx9.surf.swizzle_mode) {
144 case 0:
145 return " LINEAR";
146 case 21:
147 return " 4KB_S_X";
148 case 22:
149 return " 4KB_D_X";
150 case 25:
151 return "64KB_S_X";
152 case 26:
153 return "64KB_D_X";
154 default:
155 printf("Unhandled swizzle mode = %u\n",
156 surf->u.gfx9.surf.swizzle_mode);
157 return " UNKNOWN";
158 }
159 } else {
160 switch (surf->u.legacy.level[0].mode) {
161 case RADEON_SURF_MODE_LINEAR_ALIGNED:
162 return "LINEAR_ALIGNED";
163 case RADEON_SURF_MODE_1D:
164 return "1D_TILED_THIN1";
165 case RADEON_SURF_MODE_2D:
166 return "2D_TILED_THIN1";
167 default:
168 assert(0);
169 return " UNKNOWN";
170 }
171 }
172 }
173
174 static unsigned generate_max_tex_side(unsigned max_tex_side)
175 {
176 switch (rand() % 4) {
177 case 0:
178 /* Try to hit large sizes in 1/4 of the cases. */
179 return max_tex_side;
180 case 1:
181 /* Try to hit 1D tiling in 1/4 of the cases. */
182 return 128;
183 default:
184 /* Try to hit common sizes in 2/4 of the cases. */
185 return 2048;
186 }
187 }
188
189 void si_test_dma(struct si_screen *sscreen)
190 {
191 struct pipe_screen *screen = &sscreen->b;
192 struct pipe_context *ctx = screen->context_create(screen, NULL, 0);
193 struct si_context *sctx = (struct si_context*)ctx;
194 uint64_t max_alloc_size;
195 unsigned i, iterations, num_partial_copies, max_levels, max_tex_side;
196 unsigned num_pass = 0, num_fail = 0;
197
198 max_levels = screen->get_param(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS);
199 max_tex_side = 1 << (max_levels - 1);
200
201 /* Max 128 MB allowed for both textures. */
202 max_alloc_size = 128 * 1024 * 1024;
203
204 /* the seed for random test parameters */
205 srand(0x9b47d95b);
206 /* the seed for random pixel data */
207 s_rand_xorshift128plus(seed_xorshift128plus, false);
208
209 iterations = 1000000000; /* just kill it when you are bored */
210 num_partial_copies = 30;
211
212 /* These parameters are randomly generated per test:
213 * - whether to do one whole-surface copy or N partial copies per test
214 * - which tiling modes to use (LINEAR_ALIGNED, 1D, 2D)
215 * - which texture dimensions to use
216 * - whether to use VRAM (all tiling modes) and GTT (staging, linear
217 * only) allocations
218 * - random initial pixels in src
219 * - generate random subrectangle copies for partial blits
220 */
221 for (i = 0; i < iterations; i++) {
222 struct pipe_resource tsrc = {}, tdst = {}, *src, *dst;
223 struct r600_texture *rdst;
224 struct r600_texture *rsrc;
225 struct cpu_texture src_cpu, dst_cpu;
226 unsigned bpp, max_width, max_height, max_depth, j, num;
227 unsigned gfx_blits = 0, dma_blits = 0, max_tex_side_gen;
228 unsigned max_tex_layers;
229 bool pass;
230 bool do_partial_copies = rand() & 1;
231
232 /* generate a random test case */
233 tsrc.target = tdst.target = PIPE_TEXTURE_2D_ARRAY;
234 tsrc.depth0 = tdst.depth0 = 1;
235
236 bpp = 1 << (rand() % 5);
237 tsrc.format = tdst.format = get_format_from_bpp(bpp);
238
239 max_tex_side_gen = generate_max_tex_side(max_tex_side);
240 max_tex_layers = rand() % 4 ? 1 : 5;
241
242 tsrc.width0 = (rand() % max_tex_side_gen) + 1;
243 tsrc.height0 = (rand() % max_tex_side_gen) + 1;
244 tsrc.array_size = (rand() % max_tex_layers) + 1;
245
246 /* Have a 1/4 chance of getting power-of-two dimensions. */
247 if (rand() % 4 == 0) {
248 tsrc.width0 = util_next_power_of_two(tsrc.width0);
249 tsrc.height0 = util_next_power_of_two(tsrc.height0);
250 }
251
252 if (!do_partial_copies) {
253 /* whole-surface copies only, same dimensions */
254 tdst = tsrc;
255 } else {
256 max_tex_side_gen = generate_max_tex_side(max_tex_side);
257 max_tex_layers = rand() % 4 ? 1 : 5;
258
259 /* many partial copies, dimensions can be different */
260 tdst.width0 = (rand() % max_tex_side_gen) + 1;
261 tdst.height0 = (rand() % max_tex_side_gen) + 1;
262 tdst.array_size = (rand() % max_tex_layers) + 1;
263
264 /* Have a 1/4 chance of getting power-of-two dimensions. */
265 if (rand() % 4 == 0) {
266 tdst.width0 = util_next_power_of_two(tdst.width0);
267 tdst.height0 = util_next_power_of_two(tdst.height0);
268 }
269 }
270
271 /* check texture sizes */
272 if ((uint64_t)tsrc.width0 * tsrc.height0 * tsrc.array_size * bpp +
273 (uint64_t)tdst.width0 * tdst.height0 * tdst.array_size * bpp >
274 max_alloc_size) {
275 /* too large, try again */
276 i--;
277 continue;
278 }
279
280 /* VRAM + the tiling mode depends on dimensions (3/4 of cases),
281 * or GTT + linear only (1/4 of cases)
282 */
283 tsrc.usage = rand() % 4 ? PIPE_USAGE_DEFAULT : PIPE_USAGE_STAGING;
284 tdst.usage = rand() % 4 ? PIPE_USAGE_DEFAULT : PIPE_USAGE_STAGING;
285
286 /* Allocate textures (both the GPU and CPU copies).
287 * The CPU will emulate what the GPU should be doing.
288 */
289 src = screen->resource_create(screen, &tsrc);
290 dst = screen->resource_create(screen, &tdst);
291 assert(src);
292 assert(dst);
293 rdst = (struct r600_texture*)dst;
294 rsrc = (struct r600_texture*)src;
295 alloc_cpu_texture(&src_cpu, &tsrc, bpp);
296 alloc_cpu_texture(&dst_cpu, &tdst, bpp);
297
298 printf("%4u: dst = (%5u x %5u x %u, %s), "
299 " src = (%5u x %5u x %u, %s), bpp = %2u, ",
300 i, tdst.width0, tdst.height0, tdst.array_size,
301 array_mode_to_string(sscreen, &rdst->surface),
302 tsrc.width0, tsrc.height0, tsrc.array_size,
303 array_mode_to_string(sscreen, &rsrc->surface), bpp);
304 fflush(stdout);
305
306 /* set src pixels */
307 set_random_pixels(ctx, src, &src_cpu);
308
309 /* clear dst pixels */
310 si_clear_buffer(ctx, dst, 0, rdst->surface.surf_size, 0, true);
311 memset(dst_cpu.ptr, 0, dst_cpu.layer_stride * tdst.array_size);
312
313 /* preparation */
314 max_width = MIN2(tsrc.width0, tdst.width0);
315 max_height = MIN2(tsrc.height0, tdst.height0);
316 max_depth = MIN2(tsrc.array_size, tdst.array_size);
317
318 num = do_partial_copies ? num_partial_copies : 1;
319 for (j = 0; j < num; j++) {
320 int width, height, depth;
321 int srcx, srcy, srcz, dstx, dsty, dstz;
322 struct pipe_box box;
323 unsigned old_num_draw_calls = sctx->b.num_draw_calls;
324 unsigned old_num_dma_calls = sctx->b.num_dma_calls;
325
326 if (!do_partial_copies) {
327 /* copy whole src to dst */
328 width = max_width;
329 height = max_height;
330 depth = max_depth;
331
332 srcx = srcy = srcz = dstx = dsty = dstz = 0;
333 } else {
334 /* random sub-rectangle copies from src to dst */
335 depth = (rand() % max_depth) + 1;
336 srcz = rand() % (tsrc.array_size - depth + 1);
337 dstz = rand() % (tdst.array_size - depth + 1);
338
339 /* special code path to hit the tiled partial copies */
340 if (!rsrc->surface.is_linear &&
341 !rdst->surface.is_linear &&
342 rand() & 1) {
343 if (max_width < 8 || max_height < 8)
344 continue;
345 width = ((rand() % (max_width / 8)) + 1) * 8;
346 height = ((rand() % (max_height / 8)) + 1) * 8;
347
348 srcx = rand() % (tsrc.width0 - width + 1) & ~0x7;
349 srcy = rand() % (tsrc.height0 - height + 1) & ~0x7;
350
351 dstx = rand() % (tdst.width0 - width + 1) & ~0x7;
352 dsty = rand() % (tdst.height0 - height + 1) & ~0x7;
353 } else {
354 /* just make sure that it doesn't divide by zero */
355 assert(max_width > 0 && max_height > 0);
356
357 width = (rand() % max_width) + 1;
358 height = (rand() % max_height) + 1;
359
360 srcx = rand() % (tsrc.width0 - width + 1);
361 srcy = rand() % (tsrc.height0 - height + 1);
362
363 dstx = rand() % (tdst.width0 - width + 1);
364 dsty = rand() % (tdst.height0 - height + 1);
365 }
366
367 /* special code path to hit out-of-bounds reads in L2T */
368 if (rsrc->surface.is_linear &&
369 !rdst->surface.is_linear &&
370 rand() % 4 == 0) {
371 srcx = 0;
372 srcy = 0;
373 srcz = 0;
374 }
375 }
376
377 /* GPU copy */
378 u_box_3d(srcx, srcy, srcz, width, height, depth, &box);
379 sctx->b.dma_copy(ctx, dst, 0, dstx, dsty, dstz, src, 0, &box);
380
381 /* See which engine was used. */
382 gfx_blits += sctx->b.num_draw_calls > old_num_draw_calls;
383 dma_blits += sctx->b.num_dma_calls > old_num_dma_calls;
384
385 /* CPU copy */
386 util_copy_box(dst_cpu.ptr, tdst.format, dst_cpu.stride,
387 dst_cpu.layer_stride,
388 dstx, dsty, dstz, width, height, depth,
389 src_cpu.ptr, src_cpu.stride,
390 src_cpu.layer_stride,
391 srcx, srcy, srcz);
392 }
393
394 pass = compare_textures(ctx, dst, &dst_cpu, bpp);
395 if (pass)
396 num_pass++;
397 else
398 num_fail++;
399
400 printf("BLITs: GFX = %2u, DMA = %2u, %s [%u/%u]\n",
401 gfx_blits, dma_blits, pass ? "pass" : "fail",
402 num_pass, num_pass+num_fail);
403
404 /* cleanup */
405 pipe_resource_reference(&src, NULL);
406 pipe_resource_reference(&dst, NULL);
407 free(src_cpu.ptr);
408 free(dst_cpu.ptr);
409 }
410
411 ctx->destroy(ctx);
412 exit(0);
413 }