blob: 506d6d241f1bd2e0185eb9bb657c4d726914b987 [file] [log] [blame]
Chris Wilson20b69032011-06-05 11:20:34 +01001/*
2 * Copyright © 2011 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Chris Wilson <chris@chris-wilson.co.uk>
25 *
26 */
27
28/** @file gen3_linear_render_blits.c
29 *
30 * This is a test of doing many blits, with a working set
31 * larger than the aperture size.
32 *
33 * The goal is to simply ensure the basics work.
34 */
35
36#include <stdlib.h>
37#include <stdio.h>
38#include <string.h>
Chris Wilson20b69032011-06-05 11:20:34 +010039#include <fcntl.h>
40#include <inttypes.h>
41#include <errno.h>
42#include <sys/stat.h>
43#include <sys/time.h>
44#include "drm.h"
45#include "i915_drm.h"
46#include "drmtest.h"
47#include "intel_gpu_tools.h"
48
49#include "i915_reg.h"
50#include "i915_3d.h"
51
52#define WIDTH 512
53#define HEIGHT 512
54
55static uint32_t linear[WIDTH*HEIGHT];
56
57static inline uint32_t pack_float(float f)
58{
59 union {
60 uint32_t dw;
61 float f;
62 } u;
63 u.f = f;
64 return u.dw;
65}
66
Chris Wilson20b69032011-06-05 11:20:34 +010067static uint32_t fill_reloc(struct drm_i915_gem_relocation_entry *reloc,
68 uint32_t offset,
69 uint32_t handle,
70 uint32_t read_domain,
71 uint32_t write_domain)
72{
73 reloc->target_handle = handle;
74 reloc->delta = 0;
75 reloc->offset = offset * sizeof(uint32_t);
76 reloc->presumed_offset = 0;
77 reloc->read_domains = read_domain;
78 reloc->write_domain = write_domain;
79
80 return reloc->presumed_offset + reloc->delta;
81}
82
83static void
84copy(int fd, uint32_t dst, uint32_t src)
85{
86 uint32_t batch[1024], *b = batch;
87 struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
88 struct drm_i915_gem_exec_object2 obj[3];
89 struct drm_i915_gem_execbuffer2 exec;
90 uint32_t handle;
91 int ret;
92
93 /* invariant state */
94 *b++ = (_3DSTATE_AA_CMD |
95 AA_LINE_ECAAR_WIDTH_ENABLE |
96 AA_LINE_ECAAR_WIDTH_1_0 |
97 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
98 *b++ = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
99 IAB_MODIFY_ENABLE |
100 IAB_MODIFY_FUNC | (BLENDFUNC_ADD << IAB_FUNC_SHIFT) |
101 IAB_MODIFY_SRC_FACTOR | (BLENDFACT_ONE <<
102 IAB_SRC_FACTOR_SHIFT) |
103 IAB_MODIFY_DST_FACTOR | (BLENDFACT_ZERO <<
104 IAB_DST_FACTOR_SHIFT));
105 *b++ = (_3DSTATE_DFLT_DIFFUSE_CMD);
106 *b++ = (0);
107 *b++ = (_3DSTATE_DFLT_SPEC_CMD);
108 *b++ = (0);
109 *b++ = (_3DSTATE_DFLT_Z_CMD);
110 *b++ = (0);
111 *b++ = (_3DSTATE_COORD_SET_BINDINGS |
112 CSB_TCB(0, 0) |
113 CSB_TCB(1, 1) |
114 CSB_TCB(2, 2) |
115 CSB_TCB(3, 3) |
116 CSB_TCB(4, 4) |
117 CSB_TCB(5, 5) | CSB_TCB(6, 6) | CSB_TCB(7, 7));
118 *b++ = (_3DSTATE_RASTER_RULES_CMD |
119 ENABLE_POINT_RASTER_RULE |
120 OGL_POINT_RASTER_RULE |
121 ENABLE_LINE_STRIP_PROVOKE_VRTX |
122 ENABLE_TRI_FAN_PROVOKE_VRTX |
123 LINE_STRIP_PROVOKE_VRTX(1) |
124 TRI_FAN_PROVOKE_VRTX(2) | ENABLE_TEXKILL_3D_4D | TEXKILL_4D);
125 *b++ = (_3DSTATE_MODES_4_CMD |
126 ENABLE_LOGIC_OP_FUNC | LOGIC_OP_FUNC(LOGICOP_COPY) |
127 ENABLE_STENCIL_WRITE_MASK | STENCIL_WRITE_MASK(0xff) |
128 ENABLE_STENCIL_TEST_MASK | STENCIL_TEST_MASK(0xff));
129 *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | I1_LOAD_S(4) | I1_LOAD_S(5) | 2);
130 *b++ = (0x00000000); /* Disable texture coordinate wrap-shortest */
131 *b++ = ((1 << S4_POINT_WIDTH_SHIFT) |
132 S4_LINE_WIDTH_ONE |
133 S4_CULLMODE_NONE |
134 S4_VFMT_XY);
135 *b++ = (0x00000000); /* Stencil. */
136 *b++ = (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
137 *b++ = (_3DSTATE_SCISSOR_RECT_0_CMD);
138 *b++ = (0);
139 *b++ = (0);
140 *b++ = (_3DSTATE_DEPTH_SUBRECT_DISABLE);
141 *b++ = (_3DSTATE_LOAD_INDIRECT | 0); /* disable indirect state */
142 *b++ = (0);
143 *b++ = (_3DSTATE_STIPPLE);
144 *b++ = (0x00000000);
145 *b++ = (_3DSTATE_BACKFACE_STENCIL_OPS | BFO_ENABLE_STENCIL_TWO_SIDE | 0);
146
147 /* samler state */
148#define TEX_COUNT 1
149 *b++ = (_3DSTATE_MAP_STATE | (3 * TEX_COUNT));
150 *b++ = ((1 << TEX_COUNT) - 1);
151 *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_SAMPLER, 0); b++;
152 *b++ = (MAPSURF_32BIT | MT_32BIT_ARGB8888 |
153 (HEIGHT - 1) << MS3_HEIGHT_SHIFT |
154 (WIDTH - 1) << MS3_WIDTH_SHIFT);
155 *b++ = ((WIDTH-1) << MS4_PITCH_SHIFT);
156
157 *b++ = (_3DSTATE_SAMPLER_STATE | (3 * TEX_COUNT));
158 *b++ = ((1 << TEX_COUNT) - 1);
159 *b++ = (MIPFILTER_NONE << SS2_MIP_FILTER_SHIFT |
160 FILTER_NEAREST << SS2_MAG_FILTER_SHIFT |
161 FILTER_NEAREST << SS2_MIN_FILTER_SHIFT);
162 *b++ = (TEXCOORDMODE_WRAP << SS3_TCX_ADDR_MODE_SHIFT |
163 TEXCOORDMODE_WRAP << SS3_TCY_ADDR_MODE_SHIFT |
164 0 << SS3_TEXTUREMAP_INDEX_SHIFT);
165 *b++ = (0x00000000);
166
167 /* render target state */
168 *b++ = (_3DSTATE_BUF_INFO_CMD);
169 *b++ = (BUF_3D_ID_COLOR_BACK | WIDTH*4);
170 *b = fill_reloc(r++, b-batch, dst,
171 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
172 b++;
173
174 *b++ = (_3DSTATE_DST_BUF_VARS_CMD);
175 *b++ = (COLR_BUF_ARGB8888 |
176 DSTORG_HORT_BIAS(0x8) |
177 DSTORG_VERT_BIAS(0x8));
178
179 /* draw rect is unconditional */
180 *b++ = (_3DSTATE_DRAW_RECT_CMD);
181 *b++ = (0x00000000);
182 *b++ = (0x00000000); /* ymin, xmin */
183 *b++ = (DRAW_YMAX(HEIGHT - 1) |
184 DRAW_XMAX(WIDTH - 1));
185 /* yorig, xorig (relate to color buffer?) */
186 *b++ = (0x00000000);
187
188 /* texfmt */
189 *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(1) | I1_LOAD_S(2) | I1_LOAD_S(6) | 2);
190 *b++ = ((4 << S1_VERTEX_WIDTH_SHIFT) | (4 << S1_VERTEX_PITCH_SHIFT));
191 *b++ = (~S2_TEXCOORD_FMT(0, TEXCOORDFMT_NOT_PRESENT) |
192 S2_TEXCOORD_FMT(0, TEXCOORDFMT_2D));
193 *b++ = (S6_CBUF_BLEND_ENABLE | S6_COLOR_WRITE_ENABLE |
194 BLENDFUNC_ADD << S6_CBUF_BLEND_FUNC_SHIFT |
195 BLENDFACT_ONE << S6_CBUF_SRC_BLEND_FACT_SHIFT |
196 BLENDFACT_ZERO << S6_CBUF_DST_BLEND_FACT_SHIFT);
197
198 /* pixel shader */
199 *b++ = (_3DSTATE_PIXEL_SHADER_PROGRAM | (1 + 3*3 - 2));
200 /* decl FS_T0 */
201 *b++ = (D0_DCL |
202 REG_TYPE(FS_T0) << D0_TYPE_SHIFT |
203 REG_NR(FS_T0) << D0_NR_SHIFT |
204 ((REG_TYPE(FS_T0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
205 *b++ = (0);
206 *b++ = (0);
207 /* decl FS_S0 */
208 *b++ = (D0_DCL |
209 (REG_TYPE(FS_S0) << D0_TYPE_SHIFT) |
210 (REG_NR(FS_S0) << D0_NR_SHIFT) |
211 ((REG_TYPE(FS_S0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
212 *b++ = (0);
213 *b++ = (0);
214 /* texld(FS_OC, FS_S0, FS_T0 */
215 *b++ = (T0_TEXLD |
216 (REG_TYPE(FS_OC) << T0_DEST_TYPE_SHIFT) |
217 (REG_NR(FS_OC) << T0_DEST_NR_SHIFT) |
218 (REG_NR(FS_S0) << T0_SAMPLER_NR_SHIFT));
219 *b++ = ((REG_TYPE(FS_T0) << T1_ADDRESS_REG_TYPE_SHIFT) |
220 (REG_NR(FS_T0) << T1_ADDRESS_REG_NR_SHIFT));
221 *b++ = (0);
222
223 *b++ = (PRIM3D_RECTLIST | (3*4 - 1));
224 *b++ = pack_float(WIDTH);
225 *b++ = pack_float(HEIGHT);
226 *b++ = pack_float(WIDTH);
227 *b++ = pack_float(HEIGHT);
228
229 *b++ = pack_float(0);
230 *b++ = pack_float(HEIGHT);
231 *b++ = pack_float(0);
232 *b++ = pack_float(HEIGHT);
233
234 *b++ = pack_float(0);
235 *b++ = pack_float(0);
236 *b++ = pack_float(0);
237 *b++ = pack_float(0);
238
239 *b++ = MI_BATCH_BUFFER_END;
240 if ((b - batch) & 1)
241 *b++ = 0;
242
Daniel Vetter83440952013-08-13 12:35:58 +0200243 igt_assert(b - batch <= 1024);
Chris Wilson20b69032011-06-05 11:20:34 +0100244 handle = gem_create(fd, 4096);
Daniel Vetter319638b2012-01-10 15:31:11 +0100245 gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
Chris Wilson20b69032011-06-05 11:20:34 +0100246
Daniel Vetter83440952013-08-13 12:35:58 +0200247 igt_assert(r-reloc == 2);
Chris Wilson20b69032011-06-05 11:20:34 +0100248
249 obj[0].handle = dst;
250 obj[0].relocation_count = 0;
251 obj[0].relocs_ptr = 0;
252 obj[0].alignment = 0;
253 obj[0].offset = 0;
254 obj[0].flags = 0;
255 obj[0].rsvd1 = 0;
256 obj[0].rsvd2 = 0;
257
258 obj[1].handle = src;
259 obj[1].relocation_count = 0;
260 obj[1].relocs_ptr = 0;
261 obj[1].alignment = 0;
262 obj[1].offset = 0;
263 obj[1].flags = 0;
264 obj[1].rsvd1 = 0;
265 obj[1].rsvd2 = 0;
266
267 obj[2].handle = handle;
268 obj[2].relocation_count = 2;
269 obj[2].relocs_ptr = (uintptr_t)reloc;
270 obj[2].alignment = 0;
271 obj[2].offset = 0;
272 obj[2].flags = 0;
273 obj[2].rsvd1 = obj[2].rsvd2 = 0;
274
275 exec.buffers_ptr = (uintptr_t)obj;
276 exec.buffer_count = 3;
277 exec.batch_start_offset = 0;
278 exec.batch_len = (b-batch)*sizeof(batch[0]);
279 exec.DR1 = exec.DR4 = 0;
280 exec.num_cliprects = 0;
281 exec.cliprects_ptr = 0;
282 exec.flags = 0;
Ben Widawsky5a28ef82012-03-18 18:42:44 -0700283 i915_execbuffer2_set_context_id(exec, 0);
284 exec.rsvd2 = 0;
Chris Wilson20b69032011-06-05 11:20:34 +0100285
286 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
287 while (ret && errno == EBUSY) {
288 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
289 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
290 }
Daniel Vetter83440952013-08-13 12:35:58 +0200291 igt_assert(ret == 0);
Chris Wilson20b69032011-06-05 11:20:34 +0100292
293 gem_close(fd, handle);
294}
295
296static uint32_t
297create_bo(int fd, uint32_t val)
298{
299 uint32_t handle;
300 int i;
301
302 handle = gem_create(fd, sizeof(linear));
303
304 /* Fill the BO with dwords starting at val */
305 for (i = 0; i < WIDTH*HEIGHT; i++)
306 linear[i] = val++;
Daniel Vetter319638b2012-01-10 15:31:11 +0100307 gem_write(fd, handle, 0, linear, sizeof(linear));
Chris Wilson20b69032011-06-05 11:20:34 +0100308
309 return handle;
310}
311
312static void
313check_bo(int fd, uint32_t handle, uint32_t val)
314{
315 int i;
316
Daniel Vetterbd5cf9a2012-01-10 15:37:53 +0100317 gem_read(fd, handle, 0, linear, sizeof(linear));
Chris Wilson20b69032011-06-05 11:20:34 +0100318 for (i = 0; i < WIDTH*HEIGHT; i++) {
319 if (linear[i] != val) {
320 fprintf(stderr, "Expected 0x%08x, found 0x%08x "
321 "at offset 0x%08x\n",
322 val, linear[i], i * 4);
323 abort();
324 }
325 val++;
326 }
327}
328
329int main(int argc, char **argv)
330{
331 uint32_t *handle, *start_val;
332 uint32_t start = 0;
333 int i, fd, count;
334
335 fd = drm_open_any();
336
Daniel Vetter21ec8c72011-09-12 20:56:13 +0200337 if (!IS_GEN3(intel_get_drm_devid(fd))) {
338 printf("gen3-only test, doing nothing\n");
Daniel Vetter19d69952011-09-13 11:05:13 +0200339 return 77;
Daniel Vetter21ec8c72011-09-12 20:56:13 +0200340 }
341
Chris Wilson20b69032011-06-05 11:20:34 +0100342 count = 0;
343 if (argc > 1)
344 count = atoi(argv[1]);
345 if (count == 0)
346 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
347 printf("Using %d 1MiB buffers\n", count);
348
349 handle = malloc(sizeof(uint32_t)*count*2);
350 start_val = handle + count;
351
352 for (i = 0; i < count; i++) {
353 handle[i] = create_bo(fd, start);
354 start_val[i] = start;
355 start += 1024 * 1024 / 4;
356 }
357
358 printf("Verifying initialisation...\n");
359 for (i = 0; i < count; i++)
360 check_bo(fd, handle[i], start_val[i]);
361
362 printf("Cyclic blits, forward...\n");
363 for (i = 0; i < count * 4; i++) {
364 int src = i % count;
365 int dst = (i + 1) % count;
366
367 copy(fd, handle[dst], handle[src]);
368 start_val[dst] = start_val[src];
369 }
370 for (i = 0; i < count; i++)
371 check_bo(fd, handle[i], start_val[i]);
372
373 printf("Cyclic blits, backward...\n");
374 for (i = 0; i < count * 4; i++) {
375 int src = (i + 1) % count;
376 int dst = i % count;
377
378 copy(fd, handle[dst], handle[src]);
379 start_val[dst] = start_val[src];
380 }
381 for (i = 0; i < count; i++)
382 check_bo(fd, handle[i], start_val[i]);
383
384 printf("Random blits...\n");
385 for (i = 0; i < count * 4; i++) {
386 int src = random() % count;
387 int dst = random() % count;
388
389 if (src == dst)
390 continue;
391
392 copy(fd, handle[dst], handle[src]);
393 start_val[dst] = start_val[src];
394 }
395 for (i = 0; i < count; i++)
396 check_bo(fd, handle[i], start_val[i]);
397
398 return 0;
399}