2 * Copyright © 2011 Intel Corporation
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:
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
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
24 * Chris Wilson <chris@chris-wilson.co.uk>
28 /** @file gen3_linear_render_blits.c
30 * This is a test of doing many blits, with a working set
31 * larger than the aperture size.
33 * The goal is to simply ensure the basics work.
46 #include <sys/ioctl.h>
50 #include "intel_gpu_tools.h"
58 static inline uint32_t pack_float(float f)
69 gem_aperture_size(int fd)
71 struct drm_i915_gem_get_aperture aperture;
73 aperture.aper_size = 512*1024*1024;
74 (void)drmIoctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
75 return aperture.aper_size;
78 static uint32_t fill_reloc(struct drm_i915_gem_relocation_entry *reloc,
82 uint32_t write_domain)
84 reloc->target_handle = handle;
86 reloc->offset = offset * sizeof(uint32_t);
87 reloc->presumed_offset = 0;
88 reloc->read_domains = read_domain;
89 reloc->write_domain = write_domain;
91 return reloc->presumed_offset + reloc->delta;
96 uint32_t dst, int dst_tiling,
97 uint32_t src, int src_tiling,
100 uint32_t batch[1024], *b = batch;
101 struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
102 struct drm_i915_gem_exec_object2 obj[3];
103 struct drm_i915_gem_execbuffer2 exec;
105 uint32_t tiling_bits;
108 /* invariant state */
109 *b++ = (_3DSTATE_AA_CMD |
110 AA_LINE_ECAAR_WIDTH_ENABLE |
111 AA_LINE_ECAAR_WIDTH_1_0 |
112 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
113 *b++ = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
115 IAB_MODIFY_FUNC | (BLENDFUNC_ADD << IAB_FUNC_SHIFT) |
116 IAB_MODIFY_SRC_FACTOR |
117 (BLENDFACT_ONE << IAB_SRC_FACTOR_SHIFT) |
118 IAB_MODIFY_DST_FACTOR |
119 (BLENDFACT_ZERO << IAB_DST_FACTOR_SHIFT));
120 *b++ = (_3DSTATE_DFLT_DIFFUSE_CMD);
122 *b++ = (_3DSTATE_DFLT_SPEC_CMD);
124 *b++ = (_3DSTATE_DFLT_Z_CMD);
126 *b++ = (_3DSTATE_COORD_SET_BINDINGS |
135 *b++ = (_3DSTATE_RASTER_RULES_CMD |
136 ENABLE_POINT_RASTER_RULE |
137 OGL_POINT_RASTER_RULE |
138 ENABLE_LINE_STRIP_PROVOKE_VRTX |
139 ENABLE_TRI_FAN_PROVOKE_VRTX |
140 LINE_STRIP_PROVOKE_VRTX(1) |
141 TRI_FAN_PROVOKE_VRTX(2) |
142 ENABLE_TEXKILL_3D_4D |
144 *b++ = (_3DSTATE_MODES_4_CMD |
145 ENABLE_LOGIC_OP_FUNC | LOGIC_OP_FUNC(LOGICOP_COPY) |
146 ENABLE_STENCIL_WRITE_MASK | STENCIL_WRITE_MASK(0xff) |
147 ENABLE_STENCIL_TEST_MASK | STENCIL_TEST_MASK(0xff));
148 *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | I1_LOAD_S(4) | I1_LOAD_S(5) | 2);
149 *b++ = (0x00000000); /* Disable texture coordinate wrap-shortest */
150 *b++ = ((1 << S4_POINT_WIDTH_SHIFT) |
154 *b++ = (0x00000000); /* Stencil. */
155 *b++ = (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
156 *b++ = (_3DSTATE_SCISSOR_RECT_0_CMD);
159 *b++ = (_3DSTATE_DEPTH_SUBRECT_DISABLE);
160 *b++ = (_3DSTATE_LOAD_INDIRECT | 0); /* disable indirect state */
162 *b++ = (_3DSTATE_STIPPLE);
164 *b++ = (_3DSTATE_BACKFACE_STENCIL_OPS | BFO_ENABLE_STENCIL_TWO_SIDE | 0);
168 tiling_bits = MS3_USE_FENCE_REGS;
171 if (src_tiling != I915_TILING_NONE)
172 tiling_bits = MS3_TILED_SURFACE;
173 if (src_tiling == I915_TILING_Y)
174 tiling_bits |= MS3_TILE_WALK;
178 *b++ = (_3DSTATE_MAP_STATE | (3 * TEX_COUNT));
179 *b++ = ((1 << TEX_COUNT) - 1);
180 *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_SAMPLER, 0); b++;
181 *b++ = (MAPSURF_32BIT | MT_32BIT_ARGB8888 | tiling_bits |
182 (HEIGHT - 1) << MS3_HEIGHT_SHIFT |
183 (WIDTH - 1) << MS3_WIDTH_SHIFT);
184 *b++ = ((WIDTH-1) << MS4_PITCH_SHIFT);
186 *b++ = (_3DSTATE_SAMPLER_STATE | (3 * TEX_COUNT));
187 *b++ = ((1 << TEX_COUNT) - 1);
188 *b++ = (MIPFILTER_NONE << SS2_MIP_FILTER_SHIFT |
189 FILTER_NEAREST << SS2_MAG_FILTER_SHIFT |
190 FILTER_NEAREST << SS2_MIN_FILTER_SHIFT);
191 *b++ = (TEXCOORDMODE_WRAP << SS3_TCX_ADDR_MODE_SHIFT |
192 TEXCOORDMODE_WRAP << SS3_TCY_ADDR_MODE_SHIFT |
193 0 << SS3_TEXTUREMAP_INDEX_SHIFT);
196 /* render target state */
198 tiling_bits = BUF_3D_USE_FENCE;
201 if (dst_tiling != I915_TILING_NONE)
202 tiling_bits = BUF_3D_TILED_SURFACE;
203 if (dst_tiling == I915_TILING_Y)
204 tiling_bits |= BUF_3D_TILE_WALK_Y;
206 *b++ = (_3DSTATE_BUF_INFO_CMD);
207 *b++ = (BUF_3D_ID_COLOR_BACK | tiling_bits | WIDTH*4);
208 *b = fill_reloc(r++, b-batch, dst,
209 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
212 *b++ = (_3DSTATE_DST_BUF_VARS_CMD);
213 *b++ = (COLR_BUF_ARGB8888 |
214 DSTORG_HORT_BIAS(0x8) |
215 DSTORG_VERT_BIAS(0x8));
217 /* draw rect is unconditional */
218 *b++ = (_3DSTATE_DRAW_RECT_CMD);
220 *b++ = (0x00000000); /* ymin, xmin */
221 *b++ = (DRAW_YMAX(HEIGHT - 1) |
222 DRAW_XMAX(WIDTH - 1));
223 /* yorig, xorig (relate to color buffer?) */
227 *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(1) | I1_LOAD_S(2) | I1_LOAD_S(6) | 2);
228 *b++ = ((4 << S1_VERTEX_WIDTH_SHIFT) | (4 << S1_VERTEX_PITCH_SHIFT));
229 *b++ = (~S2_TEXCOORD_FMT(0, TEXCOORDFMT_NOT_PRESENT) |
230 S2_TEXCOORD_FMT(0, TEXCOORDFMT_2D));
231 *b++ = (S6_CBUF_BLEND_ENABLE | S6_COLOR_WRITE_ENABLE |
232 BLENDFUNC_ADD << S6_CBUF_BLEND_FUNC_SHIFT |
233 BLENDFACT_ONE << S6_CBUF_SRC_BLEND_FACT_SHIFT |
234 BLENDFACT_ZERO << S6_CBUF_DST_BLEND_FACT_SHIFT);
237 *b++ = (_3DSTATE_PIXEL_SHADER_PROGRAM | (1 + 3*3 - 2));
240 REG_TYPE(FS_T0) << D0_TYPE_SHIFT |
241 REG_NR(FS_T0) << D0_NR_SHIFT |
242 ((REG_TYPE(FS_T0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
247 (REG_TYPE(FS_S0) << D0_TYPE_SHIFT) |
248 (REG_NR(FS_S0) << D0_NR_SHIFT) |
249 ((REG_TYPE(FS_S0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
252 /* texld(FS_OC, FS_S0, FS_T0 */
254 (REG_TYPE(FS_OC) << T0_DEST_TYPE_SHIFT) |
255 (REG_NR(FS_OC) << T0_DEST_NR_SHIFT) |
256 (REG_NR(FS_S0) << T0_SAMPLER_NR_SHIFT));
257 *b++ = ((REG_TYPE(FS_T0) << T1_ADDRESS_REG_TYPE_SHIFT) |
258 (REG_NR(FS_T0) << T1_ADDRESS_REG_NR_SHIFT));
261 *b++ = (PRIM3D_RECTLIST | (3*4 - 1));
262 *b++ = pack_float(WIDTH);
263 *b++ = pack_float(HEIGHT);
264 *b++ = pack_float(WIDTH);
265 *b++ = pack_float(HEIGHT);
267 *b++ = pack_float(0);
268 *b++ = pack_float(HEIGHT);
269 *b++ = pack_float(0);
270 *b++ = pack_float(HEIGHT);
272 *b++ = pack_float(0);
273 *b++ = pack_float(0);
274 *b++ = pack_float(0);
275 *b++ = pack_float(0);
277 *b++ = MI_BATCH_BUFFER_END;
281 assert(b - batch <= 1024);
282 handle = gem_create(fd, 4096);
283 gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
285 assert(r-reloc == 2);
289 tiling_bits = EXEC_OBJECT_NEEDS_FENCE;
292 obj[0].relocation_count = 0;
293 obj[0].relocs_ptr = 0;
294 obj[0].alignment = 0;
296 obj[0].flags = tiling_bits;
301 obj[1].relocation_count = 0;
302 obj[1].relocs_ptr = 0;
303 obj[1].alignment = 0;
305 obj[1].flags = tiling_bits;
309 obj[2].handle = handle;
310 obj[2].relocation_count = 2;
311 obj[2].relocs_ptr = (uintptr_t)reloc;
312 obj[2].alignment = 0;
315 obj[2].rsvd1 = obj[2].rsvd2 = 0;
317 exec.buffers_ptr = (uintptr_t)obj;
318 exec.buffer_count = 3;
319 exec.batch_start_offset = 0;
320 exec.batch_len = (b-batch)*sizeof(batch[0]);
321 exec.DR1 = exec.DR4 = 0;
322 exec.num_cliprects = 0;
323 exec.cliprects_ptr = 0;
325 exec.rsvd1 = exec.rsvd2 = 0;
327 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
328 while (ret && errno == EBUSY) {
329 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
330 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
334 gem_close(fd, handle);
337 static void blt_copy(int fd, uint32_t dst, uint32_t src)
339 uint32_t batch[1024], *b = batch;
340 struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
341 struct drm_i915_gem_exec_object2 obj[3];
342 struct drm_i915_gem_execbuffer2 exec;
346 *b++ = (XY_SRC_COPY_BLT_CMD |
347 XY_SRC_COPY_BLT_WRITE_ALPHA |
348 XY_SRC_COPY_BLT_WRITE_RGB);
349 *b++ = 3 << 24 | 0xcc << 16 | WIDTH * 4;
351 *b++ = HEIGHT << 16 | WIDTH;
352 *b = fill_reloc(r++, b-batch, dst,
353 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER); b++;
356 *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_RENDER, 0); b++;
358 *b++ = MI_BATCH_BUFFER_END;
362 assert(b - batch <= 1024);
363 handle = gem_create(fd, 4096);
364 gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
366 assert(r-reloc == 2);
369 obj[0].relocation_count = 0;
370 obj[0].relocs_ptr = 0;
371 obj[0].alignment = 0;
373 obj[0].flags = EXEC_OBJECT_NEEDS_FENCE;
378 obj[1].relocation_count = 0;
379 obj[1].relocs_ptr = 0;
380 obj[1].alignment = 0;
382 obj[1].flags = EXEC_OBJECT_NEEDS_FENCE;
386 obj[2].handle = handle;
387 obj[2].relocation_count = 2;
388 obj[2].relocs_ptr = (uintptr_t)reloc;
389 obj[2].alignment = 0;
392 obj[2].rsvd1 = obj[2].rsvd2 = 0;
394 exec.buffers_ptr = (uintptr_t)obj;
395 exec.buffer_count = 3;
396 exec.batch_start_offset = 0;
397 exec.batch_len = (b-batch)*sizeof(batch[0]);
398 exec.DR1 = exec.DR4 = 0;
399 exec.num_cliprects = 0;
400 exec.cliprects_ptr = 0;
402 exec.rsvd1 = exec.rsvd2 = 0;
404 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
405 while (ret && errno == EBUSY) {
406 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
407 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
411 gem_close(fd, handle);
417 uint32_t dst, int dst_tiling,
418 uint32_t src, int src_tiling)
421 switch (random() % 3) {
422 case 0: render_copy(fd, dst, dst_tiling, src, src_tiling, 0); break;
423 case 1: render_copy(fd, dst, dst_tiling, src, src_tiling, 1); break;
424 case 2: if (dst_tiling == I915_TILING_Y || src_tiling == I915_TILING_Y)
426 blt_copy(fd, dst, src);
432 create_bo(int fd, uint32_t val, int tiling)
438 handle = gem_create(fd, WIDTH*HEIGHT*4);
439 gem_set_tiling(fd, handle, tiling, WIDTH*4);
441 /* Fill the BO with dwords starting at val */
442 v = gem_mmap(fd, handle, WIDTH*HEIGHT*4, PROT_READ | PROT_WRITE);
444 for (i = 0; i < WIDTH*HEIGHT; i++)
446 munmap(v, WIDTH*HEIGHT*4);
452 check_bo(int fd, uint32_t handle, uint32_t val)
457 v = gem_mmap(fd, handle, WIDTH*HEIGHT*4, PROT_READ);
459 for (i = 0; i < WIDTH*HEIGHT; i++) {
461 fprintf(stderr, "Expected 0x%08x, found 0x%08x "
462 "at offset 0x%08x\n",
468 munmap(v, WIDTH*HEIGHT*4);
471 int main(int argc, char **argv)
473 uint32_t *handle, *tiling, *start_val;
479 if (!IS_GEN3(intel_get_drm_devid(fd))) {
480 printf("gen3-only test, doing nothing\n");
486 count = atoi(argv[1]);
488 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
489 printf("Using %d 1MiB buffers\n", count);
491 handle = malloc(sizeof(uint32_t)*count*3);
492 tiling = handle + count;
493 start_val = tiling + count;
495 for (i = 0; i < count; i++) {
496 handle[i] = create_bo(fd, start, tiling[i] = i % 3);
497 start_val[i] = start;
498 start += 1024 * 1024 / 4;
501 printf("Verifying initialisation..."); fflush(stdout);
502 for (i = 0; i < count; i++)
503 check_bo(fd, handle[i], start_val[i]);
506 printf("Cyclic blits, forward..."); fflush(stdout);
507 for (i = 0; i < count * 32; i++) {
509 int dst = (i + 1) % count;
511 copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
512 start_val[dst] = start_val[src];
514 printf("verifying..."); fflush(stdout);
515 for (i = 0; i < count; i++)
516 check_bo(fd, handle[i], start_val[i]);
519 printf("Cyclic blits, backward..."); fflush(stdout);
520 for (i = 0; i < count * 32; i++) {
521 int src = (i + 1) % count;
524 copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
525 start_val[dst] = start_val[src];
527 printf("verifying..."); fflush(stdout);
528 for (i = 0; i < count; i++)
529 check_bo(fd, handle[i], start_val[i]);
532 printf("Random blits..."); fflush(stdout);
533 for (i = 0; i < count * 32; i++) {
534 int src = random() % count;
535 int dst = random() % count;
538 dst = random() % count;
540 copy(fd, handle[dst], tiling[dst], handle[src], tiling[src]);
541 start_val[dst] = start_val[src];
543 printf("verifying..."); fflush(stdout);
544 for (i = 0; i < count; i++)
545 check_bo(fd, handle[i], start_val[i]);