lib: Add MI_LOAD_REGISTER_IMM
[platform/upstream/intel-gpu-tools.git] / benchmarks / intel_upload_blit_small.c
1 /*
2  * Copyright © 2009 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  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27
28 /**
29  * Roughly simulates Mesa's current vertex buffer behavior: do a series of
30  * small pwrites on a moderately-sized buffer, then render using it.
31  *
32  * The vertex buffer uploads
33  *
34  * You might think of this like a movie player, but that wouldn't be entirely
35  * accurate, since the access patterns of the memory would be different
36  * (generally, smaller source image, upscaled, an thus different memory access
37  * pattern in both texel fetch for the stretching and the destination writes).
38  * However, some things like swfdec would be doing something like this since
39  * they compute their data in host memory and upload the full sw rendered
40  * frame.
41  */
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <assert.h>
47 #include <fcntl.h>
48 #include <inttypes.h>
49 #include <errno.h>
50 #include <sys/stat.h>
51 #include <sys/time.h>
52 #include "drm.h"
53 #include "i915_drm.h"
54 #include "drmtest.h"
55 #include "intel_bufmgr.h"
56 #include "intel_batchbuffer.h"
57 #include "intel_io.h"
58 #include "intel_chipset.h"
59
60 /* Happens to be 128k, the size of the VBOs used by i965's Mesa driver. */
61 #define OBJECT_WIDTH    256
62 #define OBJECT_HEIGHT   128
63
64 static double
65 get_time_in_secs(void)
66 {
67         struct timeval tv;
68
69         gettimeofday(&tv, NULL);
70
71         return (double)tv.tv_sec + tv.tv_usec / 1000000.0;
72 }
73
74 static void
75 do_render(drm_intel_bufmgr *bufmgr, struct intel_batchbuffer *batch,
76           drm_intel_bo *dst_bo, int width, int height)
77 {
78         uint32_t data[64];
79         drm_intel_bo *src_bo;
80         int i;
81         static uint32_t seed = 1;
82
83         src_bo = drm_intel_bo_alloc(bufmgr, "src", width * height * 4, 4096);
84
85         /* Upload some junk.  Real workloads would be doing a lot more
86          * work to generate the junk.
87          */
88         for (i = 0; i < width * height;) {
89                 int size, j;
90
91                 /* Choose a size from 1 to 64 dwords to upload.
92                  * Normal workloads have a distribution of sizes with a
93                  * large tail (something in your scene's going to have a big
94                  * pile of vertices, most likely), but I'm trying to get at
95                  * the cost of the small uploads here.
96                  */
97                 size = random() % 64 + 1;
98                 if (i + size > width * height)
99                         size = width * height - i;
100
101                 for (j = 0; j < size; j++)
102                         data[j] = seed++;
103
104                 /* Upload the junk. */
105                 drm_intel_bo_subdata(src_bo, i * 4, size * 4, data);
106
107                 i += size;
108         }
109
110         /* Render the junk to the dst. */
111         BLIT_COPY_BATCH_START(0);
112         OUT_BATCH((3 << 24) | /* 32 bits */
113                   (0xcc << 16) | /* copy ROP */
114                   (width * 4) /* dst pitch */);
115         OUT_BATCH(0); /* dst x1,y1 */
116         OUT_BATCH((height << 16) | width); /* dst x2,y2 */
117         OUT_RELOC(dst_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
118         OUT_BATCH(0); /* src x1,y1 */
119         OUT_BATCH(width * 4); /* src pitch */
120         OUT_RELOC(src_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
121         ADVANCE_BATCH();
122
123         intel_batchbuffer_flush(batch);
124
125         drm_intel_bo_unreference(src_bo);
126 }
127
128 int main(int argc, char **argv)
129 {
130         int fd;
131         int object_size = OBJECT_WIDTH * OBJECT_HEIGHT * 4;
132         double start_time, end_time;
133         drm_intel_bo *dst_bo;
134         drm_intel_bufmgr *bufmgr;
135         struct intel_batchbuffer *batch;
136         int i;
137
138         fd = drm_open_any();
139
140         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
141         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
142
143         batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
144
145         dst_bo = drm_intel_bo_alloc(bufmgr, "dst", object_size, 4096);
146
147         /* Prep loop to get us warmed up. */
148         for (i = 0; i < 20; i++) {
149                 do_render(bufmgr, batch, dst_bo, OBJECT_WIDTH, OBJECT_HEIGHT);
150         }
151         drm_intel_bo_wait_rendering(dst_bo);
152
153         /* Do the actual timing. */
154         start_time = get_time_in_secs();
155         for (i = 0; i < 1000; i++) {
156                 do_render(bufmgr, batch, dst_bo, OBJECT_WIDTH, OBJECT_HEIGHT);
157         }
158         drm_intel_bo_wait_rendering(dst_bo);
159         end_time = get_time_in_secs();
160
161         printf("%d iterations in %.03f secs: %.01f MB/sec\n", i,
162                end_time - start_time,
163                (double)i * OBJECT_WIDTH * OBJECT_HEIGHT * 4 / 1024.0 / 1024.0 /
164                (end_time - start_time));
165
166         intel_batchbuffer_free(batch);
167         drm_intel_bufmgr_destroy(bufmgr);
168
169         close(fd);
170
171         return 0;
172 }