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