lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gen3_render_linear_blits.c
1 /*
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>
39 #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 "ioctl_wrappers.h"
46 #include "drmtest.h"
47 #include "intel_io.h"
48 #include "intel_chipset.h"
49
50 #include "i915_reg.h"
51 #include "i915_3d.h"
52
53 #define WIDTH 512
54 #define HEIGHT 512
55
56 static uint32_t linear[WIDTH*HEIGHT];
57
58 static inline uint32_t pack_float(float f)
59 {
60         union {
61                 uint32_t dw;
62                 float f;
63         } u;
64         u.f = f;
65         return u.dw;
66 }
67
68 static uint32_t fill_reloc(struct drm_i915_gem_relocation_entry *reloc,
69                            uint32_t offset,
70                            uint32_t handle,
71                            uint32_t read_domain,
72                            uint32_t write_domain)
73 {
74         reloc->target_handle = handle;
75         reloc->delta = 0;
76         reloc->offset = offset * sizeof(uint32_t);
77         reloc->presumed_offset = 0;
78         reloc->read_domains = read_domain;
79         reloc->write_domain = write_domain;
80
81         return reloc->presumed_offset + reloc->delta;
82 }
83
84 static void
85 copy(int fd, uint32_t dst, uint32_t src)
86 {
87         uint32_t batch[1024], *b = batch;
88         struct drm_i915_gem_relocation_entry reloc[2], *r = reloc;
89         struct drm_i915_gem_exec_object2 obj[3];
90         struct drm_i915_gem_execbuffer2 exec;
91         uint32_t handle;
92         int ret;
93
94         /* invariant state */
95         *b++ = (_3DSTATE_AA_CMD |
96                 AA_LINE_ECAAR_WIDTH_ENABLE |
97                 AA_LINE_ECAAR_WIDTH_1_0 |
98                 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
99         *b++ = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
100                 IAB_MODIFY_ENABLE |
101                 IAB_MODIFY_FUNC | (BLENDFUNC_ADD << IAB_FUNC_SHIFT) |
102                 IAB_MODIFY_SRC_FACTOR | (BLENDFACT_ONE <<
103                                          IAB_SRC_FACTOR_SHIFT) |
104                 IAB_MODIFY_DST_FACTOR | (BLENDFACT_ZERO <<
105                                          IAB_DST_FACTOR_SHIFT));
106         *b++ = (_3DSTATE_DFLT_DIFFUSE_CMD);
107         *b++ = (0);
108         *b++ = (_3DSTATE_DFLT_SPEC_CMD);
109         *b++ = (0);
110         *b++ = (_3DSTATE_DFLT_Z_CMD);
111         *b++ = (0);
112         *b++ = (_3DSTATE_COORD_SET_BINDINGS |
113                 CSB_TCB(0, 0) |
114                 CSB_TCB(1, 1) |
115                 CSB_TCB(2, 2) |
116                 CSB_TCB(3, 3) |
117                 CSB_TCB(4, 4) |
118                 CSB_TCB(5, 5) | CSB_TCB(6, 6) | CSB_TCB(7, 7));
119         *b++ = (_3DSTATE_RASTER_RULES_CMD |
120                 ENABLE_POINT_RASTER_RULE |
121                 OGL_POINT_RASTER_RULE |
122                 ENABLE_LINE_STRIP_PROVOKE_VRTX |
123                 ENABLE_TRI_FAN_PROVOKE_VRTX |
124                 LINE_STRIP_PROVOKE_VRTX(1) |
125                 TRI_FAN_PROVOKE_VRTX(2) | ENABLE_TEXKILL_3D_4D | TEXKILL_4D);
126         *b++ = (_3DSTATE_MODES_4_CMD |
127                 ENABLE_LOGIC_OP_FUNC | LOGIC_OP_FUNC(LOGICOP_COPY) |
128                 ENABLE_STENCIL_WRITE_MASK | STENCIL_WRITE_MASK(0xff) |
129                 ENABLE_STENCIL_TEST_MASK | STENCIL_TEST_MASK(0xff));
130         *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | I1_LOAD_S(4) | I1_LOAD_S(5) | 2);
131         *b++ = (0x00000000);    /* Disable texture coordinate wrap-shortest */
132         *b++ = ((1 << S4_POINT_WIDTH_SHIFT) |
133                 S4_LINE_WIDTH_ONE |
134                 S4_CULLMODE_NONE |
135                 S4_VFMT_XY);
136         *b++ = (0x00000000);    /* Stencil. */
137         *b++ = (_3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT);
138         *b++ = (_3DSTATE_SCISSOR_RECT_0_CMD);
139         *b++ = (0);
140         *b++ = (0);
141         *b++ = (_3DSTATE_DEPTH_SUBRECT_DISABLE);
142         *b++ = (_3DSTATE_LOAD_INDIRECT | 0);    /* disable indirect state */
143         *b++ = (0);
144         *b++ = (_3DSTATE_STIPPLE);
145         *b++ = (0x00000000);
146         *b++ = (_3DSTATE_BACKFACE_STENCIL_OPS | BFO_ENABLE_STENCIL_TWO_SIDE | 0);
147
148         /* samler state */
149 #define TEX_COUNT 1
150         *b++ = (_3DSTATE_MAP_STATE | (3 * TEX_COUNT));
151         *b++ = ((1 << TEX_COUNT) - 1);
152         *b = fill_reloc(r++, b-batch, src, I915_GEM_DOMAIN_SAMPLER, 0); b++;
153         *b++ = (MAPSURF_32BIT | MT_32BIT_ARGB8888 |
154                 (HEIGHT - 1) << MS3_HEIGHT_SHIFT |
155                 (WIDTH - 1) << MS3_WIDTH_SHIFT);
156         *b++ = ((WIDTH-1) << MS4_PITCH_SHIFT);
157
158         *b++ = (_3DSTATE_SAMPLER_STATE | (3 * TEX_COUNT));
159         *b++ = ((1 << TEX_COUNT) - 1);
160         *b++ = (MIPFILTER_NONE << SS2_MIP_FILTER_SHIFT |
161                 FILTER_NEAREST << SS2_MAG_FILTER_SHIFT |
162                 FILTER_NEAREST << SS2_MIN_FILTER_SHIFT);
163         *b++ = (TEXCOORDMODE_WRAP << SS3_TCX_ADDR_MODE_SHIFT |
164                 TEXCOORDMODE_WRAP << SS3_TCY_ADDR_MODE_SHIFT |
165                 0 << SS3_TEXTUREMAP_INDEX_SHIFT);
166         *b++ = (0x00000000);
167
168         /* render target state */
169         *b++ = (_3DSTATE_BUF_INFO_CMD);
170         *b++ = (BUF_3D_ID_COLOR_BACK | WIDTH*4);
171         *b = fill_reloc(r++, b-batch, dst,
172                         I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER);
173         b++;
174
175         *b++ = (_3DSTATE_DST_BUF_VARS_CMD);
176         *b++ = (COLR_BUF_ARGB8888 |
177                 DSTORG_HORT_BIAS(0x8) |
178                 DSTORG_VERT_BIAS(0x8));
179
180         /* draw rect is unconditional */
181         *b++ = (_3DSTATE_DRAW_RECT_CMD);
182         *b++ = (0x00000000);
183         *b++ = (0x00000000);    /* ymin, xmin */
184         *b++ = (DRAW_YMAX(HEIGHT - 1) |
185                 DRAW_XMAX(WIDTH - 1));
186         /* yorig, xorig (relate to color buffer?) */
187         *b++ = (0x00000000);
188
189         /* texfmt */
190         *b++ = (_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(1) | I1_LOAD_S(2) | I1_LOAD_S(6) | 2);
191         *b++ = ((4 << S1_VERTEX_WIDTH_SHIFT) | (4 << S1_VERTEX_PITCH_SHIFT));
192         *b++ = (~S2_TEXCOORD_FMT(0, TEXCOORDFMT_NOT_PRESENT) |
193                 S2_TEXCOORD_FMT(0, TEXCOORDFMT_2D));
194         *b++ = (S6_CBUF_BLEND_ENABLE | S6_COLOR_WRITE_ENABLE |
195                 BLENDFUNC_ADD << S6_CBUF_BLEND_FUNC_SHIFT |
196                 BLENDFACT_ONE << S6_CBUF_SRC_BLEND_FACT_SHIFT |
197                 BLENDFACT_ZERO << S6_CBUF_DST_BLEND_FACT_SHIFT);
198
199         /* pixel shader */
200         *b++ = (_3DSTATE_PIXEL_SHADER_PROGRAM | (1 + 3*3 - 2));
201         /* decl FS_T0 */
202         *b++ = (D0_DCL |
203                 REG_TYPE(FS_T0) << D0_TYPE_SHIFT |
204                 REG_NR(FS_T0) << D0_NR_SHIFT |
205                 ((REG_TYPE(FS_T0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
206         *b++ = (0);
207         *b++ = (0);
208         /* decl FS_S0 */
209         *b++ = (D0_DCL |
210                 (REG_TYPE(FS_S0) << D0_TYPE_SHIFT) |
211                 (REG_NR(FS_S0) << D0_NR_SHIFT) |
212                 ((REG_TYPE(FS_S0) != REG_TYPE_S) ? D0_CHANNEL_ALL : 0));
213         *b++ = (0);
214         *b++ = (0);
215         /* texld(FS_OC, FS_S0, FS_T0 */
216         *b++ = (T0_TEXLD |
217                 (REG_TYPE(FS_OC) << T0_DEST_TYPE_SHIFT) |
218                 (REG_NR(FS_OC) << T0_DEST_NR_SHIFT) |
219                 (REG_NR(FS_S0) << T0_SAMPLER_NR_SHIFT));
220         *b++ = ((REG_TYPE(FS_T0) << T1_ADDRESS_REG_TYPE_SHIFT) |
221                 (REG_NR(FS_T0) << T1_ADDRESS_REG_NR_SHIFT));
222         *b++ = (0);
223
224         *b++ = (PRIM3D_RECTLIST | (3*4 - 1));
225         *b++ = pack_float(WIDTH);
226         *b++ = pack_float(HEIGHT);
227         *b++ = pack_float(WIDTH);
228         *b++ = pack_float(HEIGHT);
229
230         *b++ = pack_float(0);
231         *b++ = pack_float(HEIGHT);
232         *b++ = pack_float(0);
233         *b++ = pack_float(HEIGHT);
234
235         *b++ = pack_float(0);
236         *b++ = pack_float(0);
237         *b++ = pack_float(0);
238         *b++ = pack_float(0);
239
240         *b++ = MI_BATCH_BUFFER_END;
241         if ((b - batch) & 1)
242                 *b++ = 0;
243
244         igt_assert(b - batch <= 1024);
245         handle = gem_create(fd, 4096);
246         gem_write(fd, handle, 0, batch, (b-batch)*sizeof(batch[0]));
247
248         igt_assert(r-reloc == 2);
249
250         obj[0].handle = dst;
251         obj[0].relocation_count = 0;
252         obj[0].relocs_ptr = 0;
253         obj[0].alignment = 0;
254         obj[0].offset = 0;
255         obj[0].flags = 0;
256         obj[0].rsvd1 = 0;
257         obj[0].rsvd2 = 0;
258
259         obj[1].handle = src;
260         obj[1].relocation_count = 0;
261         obj[1].relocs_ptr = 0;
262         obj[1].alignment = 0;
263         obj[1].offset = 0;
264         obj[1].flags = 0;
265         obj[1].rsvd1 = 0;
266         obj[1].rsvd2 = 0;
267
268         obj[2].handle = handle;
269         obj[2].relocation_count = 2;
270         obj[2].relocs_ptr = (uintptr_t)reloc;
271         obj[2].alignment = 0;
272         obj[2].offset = 0;
273         obj[2].flags = 0;
274         obj[2].rsvd1 = obj[2].rsvd2 = 0;
275
276         exec.buffers_ptr = (uintptr_t)obj;
277         exec.buffer_count = 3;
278         exec.batch_start_offset = 0;
279         exec.batch_len = (b-batch)*sizeof(batch[0]);
280         exec.DR1 = exec.DR4 = 0;
281         exec.num_cliprects = 0;
282         exec.cliprects_ptr = 0;
283         exec.flags = 0;
284         i915_execbuffer2_set_context_id(exec, 0);
285         exec.rsvd2 = 0;
286
287         ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
288         while (ret && errno == EBUSY) {
289                 drmCommandNone(fd, DRM_I915_GEM_THROTTLE);
290                 ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &exec);
291         }
292         igt_assert(ret == 0);
293
294         gem_close(fd, handle);
295 }
296
297 static uint32_t
298 create_bo(int fd, uint32_t val)
299 {
300         uint32_t handle;
301         int i;
302
303         handle = gem_create(fd, sizeof(linear));
304
305         /* Fill the BO with dwords starting at val */
306         for (i = 0; i < WIDTH*HEIGHT; i++)
307                 linear[i] = val++;
308         gem_write(fd, handle, 0, linear, sizeof(linear));
309
310         return handle;
311 }
312
313 static void
314 check_bo(int fd, uint32_t handle, uint32_t val)
315 {
316         int i;
317
318         gem_read(fd, handle, 0, linear, sizeof(linear));
319         for (i = 0; i < WIDTH*HEIGHT; i++) {
320                 igt_assert_f(linear[i] == val,
321                              "Expected 0x%08x, found 0x%08x "
322                              "at offset 0x%08x\n",
323                              val, linear[i], i * 4);
324                 val++;
325         }
326 }
327
328 int main(int argc, char **argv)
329 {
330         uint32_t *handle, *start_val;
331         uint32_t start = 0;
332         int i, fd, count;
333
334         igt_simple_init(argc, argv);
335
336         fd = drm_open_any();
337
338         igt_require(IS_GEN3(intel_get_drm_devid(fd)));
339
340         count = 0;
341         if (argc > 1)
342                 count = atoi(argv[1]);
343         if (count == 0)
344                 count = 3 * gem_aperture_size(fd) / (1024*1024) / 2;
345         igt_info("Using %d 1MiB buffers\n", count);
346
347         handle = malloc(sizeof(uint32_t)*count*2);
348         start_val = handle + count;
349
350         for (i = 0; i < count; i++) {
351                 handle[i] = create_bo(fd, start);
352                 start_val[i] = start;
353                 start += 1024 * 1024 / 4;
354         }
355
356         igt_info("Verifying initialisation...\n");
357         for (i = 0; i < count; i++)
358                 check_bo(fd, handle[i], start_val[i]);
359
360         igt_info("Cyclic blits, forward...\n");
361         for (i = 0; i < count * 4; i++) {
362                 int src = i % count;
363                 int dst = (i + 1) % count;
364
365                 copy(fd, handle[dst], handle[src]);
366                 start_val[dst] = start_val[src];
367         }
368         for (i = 0; i < count; i++)
369                 check_bo(fd, handle[i], start_val[i]);
370
371         igt_info("Cyclic blits, backward...\n");
372         for (i = 0; i < count * 4; i++) {
373                 int src = (i + 1) % count;
374                 int dst = i % count;
375
376                 copy(fd, handle[dst], handle[src]);
377                 start_val[dst] = start_val[src];
378         }
379         for (i = 0; i < count; i++)
380                 check_bo(fd, handle[i], start_val[i]);
381
382         igt_info("Random blits...\n");
383         for (i = 0; i < count * 4; i++) {
384                 int src = random() % count;
385                 int dst = random() % count;
386
387                 if (src == dst)
388                         continue;
389
390                 copy(fd, handle[dst], handle[src]);
391                 start_val[dst] = start_val[src];
392         }
393         for (i = 0; i < count; i++)
394                 check_bo(fd, handle[i], start_val[i]);
395
396         return 0;
397 }