packaging: only on x86* arch
[platform/upstream/intel-gpu-tools.git] / lib / rendercopy_gen8.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <sys/ioctl.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <assert.h>
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <errno.h>
10 #include <sys/stat.h>
11 #include <sys/time.h>
12
13 #include <drm.h>
14 #include <i915_drm.h>
15
16 #include "drmtest.h"
17 #include "intel_bufmgr.h"
18 #include "intel_batchbuffer.h"
19 #include "intel_io.h"
20 #include "rendercopy.h"
21 #include "gen8_render.h"
22 #include "intel_reg.h"
23 #include "igt_aux.h"
24
25 #include <intel_aub.h>
26
27 #define VERTEX_SIZE (3*4)
28
29 #if DEBUG_RENDERCPY
30 static void dump_batch(struct intel_batchbuffer *batch) {
31         int fd = open("/tmp/i965-batchbuffers.dump", O_WRONLY | O_CREAT,  0666);
32         if (fd != -1) {
33                 write(fd, batch->buffer, 4096);
34                 fd = close(fd);
35         }
36 }
37 #else
38 #define dump_batch(x) do { } while(0)
39 #endif
40
41 struct {
42         uint32_t cc_state;
43         uint32_t blend_state;
44 } cc;
45
46 struct {
47         uint32_t cc_state;
48         uint32_t sf_clip_state;
49 } viewport;
50
51 /* see shaders/ps/blit.g7a */
52 static const uint32_t ps_kernel[][4] = {
53 #if 1
54    { 0x0060005a, 0x21403ae8, 0x3a0000c0, 0x008d0040 },
55    { 0x0060005a, 0x21603ae8, 0x3a0000c0, 0x008d0080 },
56    { 0x0060005a, 0x21803ae8, 0x3a0000d0, 0x008d0040 },
57    { 0x0060005a, 0x21a03ae8, 0x3a0000d0, 0x008d0080 },
58    { 0x02800031, 0x2e0022e8, 0x0e000140, 0x08840001 },
59    { 0x05800031, 0x200022e0, 0x0e000e00, 0x90031000 },
60 #else
61    /* Write all -1 */
62    { 0x00600001, 0x2e000608, 0x00000000, 0x3f800000 },
63    { 0x00600001, 0x2e200608, 0x00000000, 0x3f800000 },
64    { 0x00600001, 0x2e400608, 0x00000000, 0x3f800000 },
65    { 0x00600001, 0x2e600608, 0x00000000, 0x3f800000 },
66    { 0x00600001, 0x2e800608, 0x00000000, 0x3f800000 },
67    { 0x00600001, 0x2ea00608, 0x00000000, 0x3f800000 },
68    { 0x00600001, 0x2ec00608, 0x00000000, 0x3f800000 },
69    { 0x00600001, 0x2ee00608, 0x00000000, 0x3f800000 },
70    { 0x05800031, 0x200022e0, 0x0e000e00, 0x90031000 },
71 #endif
72 };
73
74 /* AUB annotation support */
75 #define MAX_ANNOTATIONS 33
76 struct annotations_context {
77         drm_intel_aub_annotation annotations[MAX_ANNOTATIONS];
78         int index;
79         uint32_t offset;
80 } aub_annotations;
81
82 static void annotation_init(struct annotations_context *ctx)
83 {
84         /* ctx->annotations is an array keeping a list of annotations of the
85          * batch buffer ordered by offset. ctx->annotations[0] is thus left
86          * for the command stream and will be filled just before executing
87          * the batch buffer with annotations_add_batch() */
88         ctx->index = 1;
89 }
90
91 static void add_annotation(drm_intel_aub_annotation *a,
92                            uint32_t type, uint32_t subtype,
93                            uint32_t ending_offset)
94 {
95         a->type = type;
96         a->subtype = subtype;
97         a->ending_offset = ending_offset;
98 }
99
100 static void annotation_add_batch(struct annotations_context *ctx, size_t size)
101 {
102         add_annotation(&ctx->annotations[0], AUB_TRACE_TYPE_BATCH, 0, size);
103 }
104
105 static void annotation_add_state(struct annotations_context *ctx,
106                                  uint32_t state_type,
107                                  uint32_t start_offset,
108                                  size_t   size)
109 {
110         igt_assert(ctx->index < MAX_ANNOTATIONS);
111
112         add_annotation(&ctx->annotations[ctx->index++],
113                        AUB_TRACE_TYPE_NOTYPE, 0,
114                        start_offset);
115         add_annotation(&ctx->annotations[ctx->index++],
116                        AUB_TRACE_TYPE(state_type),
117                        AUB_TRACE_SUBTYPE(state_type),
118                        start_offset + size);
119 }
120
121 static void annotation_flush(struct annotations_context *ctx,
122                              struct intel_batchbuffer *batch)
123 {
124         if (!igt_aub_dump_enabled())
125                 return;
126
127         drm_intel_bufmgr_gem_set_aub_annotations(batch->bo,
128                                                  ctx->annotations,
129                                                  ctx->index);
130 }
131
132 static uint32_t
133 batch_used(struct intel_batchbuffer *batch)
134 {
135         return batch->ptr - batch->buffer;
136 }
137
138 static uint32_t
139 batch_align(struct intel_batchbuffer *batch, uint32_t align)
140 {
141         uint32_t offset = batch_used(batch);
142         offset = ALIGN(offset, align);
143         batch->ptr = batch->buffer + offset;
144         return offset;
145 }
146
147 static void *
148 batch_alloc(struct intel_batchbuffer *batch, uint32_t size, uint32_t align)
149 {
150         uint32_t offset = batch_align(batch, align);
151         batch->ptr += size;
152         return memset(batch->buffer + offset, 0, size);
153 }
154
155 static uint32_t
156 batch_offset(struct intel_batchbuffer *batch, void *ptr)
157 {
158         return (uint8_t *)ptr - batch->buffer;
159 }
160
161 static uint32_t
162 batch_copy(struct intel_batchbuffer *batch, const void *ptr, uint32_t size, uint32_t align)
163 {
164         return batch_offset(batch, memcpy(batch_alloc(batch, size, align), ptr, size));
165 }
166
167 static void
168 gen6_render_flush(struct intel_batchbuffer *batch,
169                   drm_intel_context *context, uint32_t batch_end)
170 {
171         int ret;
172
173         ret = drm_intel_bo_subdata(batch->bo, 0, 4096, batch->buffer);
174         if (ret == 0)
175                 ret = drm_intel_gem_bo_context_exec(batch->bo, context,
176                                                     batch_end, 0);
177         igt_assert(ret == 0);
178 }
179
180 /* Mostly copy+paste from gen6, except height, width, pitch moved */
181 static uint32_t
182 gen8_bind_buf(struct intel_batchbuffer *batch, struct igt_buf *buf,
183               uint32_t format, int is_dst) {
184         struct gen8_surface_state *ss;
185         uint32_t write_domain, read_domain, offset;
186         int ret;
187
188         if (is_dst) {
189                 write_domain = read_domain = I915_GEM_DOMAIN_RENDER;
190         } else {
191                 write_domain = 0;
192                 read_domain = I915_GEM_DOMAIN_SAMPLER;
193         }
194
195         ss = batch_alloc(batch, sizeof(*ss), 64);
196         offset = batch_offset(batch, ss);
197         annotation_add_state(&aub_annotations, AUB_TRACE_SURFACE_STATE,
198                              offset, sizeof(*ss));
199
200         ss->ss0.surface_type = GEN6_SURFACE_2D;
201         ss->ss0.surface_format = format;
202         ss->ss0.render_cache_read_write = 1;
203         ss->ss0.vertical_alignment = 1; /* align 4 */
204         ss->ss0.horizontal_alignment = 1; /* align 4 */
205         if (buf->tiling == I915_TILING_X)
206                 ss->ss0.tiled_mode = 2;
207         else if (buf->tiling == I915_TILING_Y)
208                 ss->ss0.tiled_mode = 3;
209
210         ss->ss8.base_addr = buf->bo->offset;
211
212         ret = drm_intel_bo_emit_reloc(batch->bo,
213                                       batch_offset(batch, ss) + 8 * 4,
214                                       buf->bo, 0,
215                                       read_domain, write_domain);
216         igt_assert(ret == 0);
217
218         ss->ss2.height = igt_buf_height(buf) - 1;
219         ss->ss2.width  = igt_buf_width(buf) - 1;
220         ss->ss3.pitch  = buf->stride - 1;
221
222         ss->ss7.shader_chanel_select_r = 4;
223         ss->ss7.shader_chanel_select_g = 5;
224         ss->ss7.shader_chanel_select_b = 6;
225         ss->ss7.shader_chanel_select_a = 7;
226
227         return offset;
228 }
229
230 static uint32_t
231 gen8_bind_surfaces(struct intel_batchbuffer *batch,
232                    struct igt_buf *src,
233                    struct igt_buf *dst)
234 {
235         uint32_t *binding_table, offset;
236
237         binding_table = batch_alloc(batch, 8, 32);
238         offset = batch_offset(batch, binding_table);
239         annotation_add_state(&aub_annotations, AUB_TRACE_BINDING_TABLE,
240                              offset, 8);
241
242         binding_table[0] =
243                 gen8_bind_buf(batch, dst, GEN6_SURFACEFORMAT_B8G8R8A8_UNORM, 1);
244         binding_table[1] =
245                 gen8_bind_buf(batch, src, GEN6_SURFACEFORMAT_B8G8R8A8_UNORM, 0);
246
247         return offset;
248 }
249
250 /* Mostly copy+paste from gen6, except wrap modes moved */
251 static uint32_t
252 gen8_create_sampler(struct intel_batchbuffer *batch) {
253         struct gen8_sampler_state *ss;
254         uint32_t offset;
255
256         ss = batch_alloc(batch, sizeof(*ss), 64);
257         offset = batch_offset(batch, ss);
258         annotation_add_state(&aub_annotations, AUB_TRACE_SAMPLER_STATE,
259                              offset, sizeof(*ss));
260
261         ss->ss0.min_filter = GEN6_MAPFILTER_NEAREST;
262         ss->ss0.mag_filter = GEN6_MAPFILTER_NEAREST;
263         ss->ss3.r_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
264         ss->ss3.s_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
265         ss->ss3.t_wrap_mode = GEN6_TEXCOORDMODE_CLAMP;
266
267         /* I've experimented with non-normalized coordinates and using the LD
268          * sampler fetch, but couldn't make it work. */
269         ss->ss3.non_normalized_coord = 0;
270
271         return offset;
272 }
273
274 static uint32_t
275 gen8_fill_ps(struct intel_batchbuffer *batch,
276              const uint32_t kernel[][4],
277              size_t size)
278 {
279         uint32_t offset;
280
281         offset = batch_copy(batch, kernel, size, 64);
282         annotation_add_state(&aub_annotations, AUB_TRACE_KERNEL_INSTRUCTIONS,
283                              offset, size);
284
285         return offset;
286 }
287
288 /*
289  * gen7_fill_vertex_buffer_data populate vertex buffer with data.
290  *
291  * The vertex buffer consists of 3 vertices to construct a RECTLIST. The 4th
292  * vertex is implied (automatically derived by the HW). Each element has the
293  * destination offset, and the normalized texture offset (src). The rectangle
294  * itself will span the entire subsurface to be copied.
295  *
296  * see gen6_emit_vertex_elements
297  */
298 static uint32_t
299 gen7_fill_vertex_buffer_data(struct intel_batchbuffer *batch,
300                              struct igt_buf *src,
301                              uint32_t src_x, uint32_t src_y,
302                              uint32_t dst_x, uint32_t dst_y,
303                              uint32_t width, uint32_t height)
304 {
305         void *start;
306         uint32_t offset;
307
308         batch_align(batch, 8);
309         start = batch->ptr;
310
311         emit_vertex_2s(batch, dst_x + width, dst_y + height);
312         emit_vertex_normalized(batch, src_x + width, igt_buf_width(src));
313         emit_vertex_normalized(batch, src_y + height, igt_buf_height(src));
314
315         emit_vertex_2s(batch, dst_x, dst_y + height);
316         emit_vertex_normalized(batch, src_x, igt_buf_width(src));
317         emit_vertex_normalized(batch, src_y + height, igt_buf_height(src));
318
319         emit_vertex_2s(batch, dst_x, dst_y);
320         emit_vertex_normalized(batch, src_x, igt_buf_width(src));
321         emit_vertex_normalized(batch, src_y, igt_buf_height(src));
322
323         offset = batch_offset(batch, start);
324         annotation_add_state(&aub_annotations, AUB_TRACE_VERTEX_BUFFER,
325                              offset, 3 * VERTEX_SIZE);
326         return offset;
327 }
328
329 /*
330  * gen6_emit_vertex_elements - The vertex elements describe the contents of the
331  * vertex buffer. We pack the vertex buffer in a semi weird way, conforming to
332  * what gen6_rendercopy did. The most straightforward would be to store
333  * everything as floats.
334  *
335  * see gen7_fill_vertex_buffer_data() for where the corresponding elements are
336  * packed.
337  */
338 static void
339 gen6_emit_vertex_elements(struct intel_batchbuffer *batch) {
340         /*
341          * The VUE layout
342          *    dword 0-3: pad (0, 0, 0. 0)
343          *    dword 4-7: position (x, y, 0, 1.0),
344          *    dword 8-11: texture coordinate 0 (u0, v0, 0, 1.0)
345          */
346         OUT_BATCH(GEN6_3DSTATE_VERTEX_ELEMENTS | (3 * 2 + 1 - 2));
347
348         /* Element state 0. These are 4 dwords of 0 required for the VUE format.
349          * We don't really know or care what they do.
350          */
351         OUT_BATCH(0 << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
352                   GEN6_SURFACEFORMAT_R32G32B32A32_FLOAT << VE0_FORMAT_SHIFT |
353                   0 << VE0_OFFSET_SHIFT); /* we specify 0, but it's really does not exist */
354         OUT_BATCH(GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_0_SHIFT |
355                   GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_1_SHIFT |
356                   GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
357                   GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_3_SHIFT);
358
359         /* Element state 1 - Our "destination" vertices. These are passed down
360          * through the pipeline, and eventually make it to the pixel shader as
361          * the offsets in the destination surface. It's packed as the 16
362          * signed/scaled because of gen6 rendercopy. I see no particular reason
363          * for doing this though.
364          */
365         OUT_BATCH(0 << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
366                   GEN6_SURFACEFORMAT_R16G16_SSCALED << VE0_FORMAT_SHIFT |
367                   0 << VE0_OFFSET_SHIFT); /* offsets vb in bytes */
368         OUT_BATCH(GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
369                   GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
370                   GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
371                   GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
372
373         /* Element state 2. Last but not least we store the U,V components as
374          * normalized floats. These will be used in the pixel shader to sample
375          * from the source buffer.
376          */
377         OUT_BATCH(0 << VE0_VERTEX_BUFFER_INDEX_SHIFT | VE0_VALID |
378                   GEN6_SURFACEFORMAT_R32G32_FLOAT << VE0_FORMAT_SHIFT |
379                   4 << VE0_OFFSET_SHIFT);       /* offset vb in bytes */
380         OUT_BATCH(GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_0_SHIFT |
381                   GEN6_VFCOMPONENT_STORE_SRC << VE1_VFCOMPONENT_1_SHIFT |
382                   GEN6_VFCOMPONENT_STORE_0 << VE1_VFCOMPONENT_2_SHIFT |
383                   GEN6_VFCOMPONENT_STORE_1_FLT << VE1_VFCOMPONENT_3_SHIFT);
384 }
385
386 /*
387  * gen8_emit_vertex_buffer emit the vertex buffers command
388  *
389  * @batch
390  * @offset - bytw offset within the @batch where the vertex buffer starts.
391  */
392 static void gen8_emit_vertex_buffer(struct intel_batchbuffer *batch,
393                                     uint32_t offset) {
394         OUT_BATCH(GEN6_3DSTATE_VERTEX_BUFFERS | (1 + (4 * 1) - 2));
395         OUT_BATCH(0 << VB0_BUFFER_INDEX_SHIFT | /* VB 0th index */
396                   GEN7_VB0_BUFFER_ADDR_MOD_EN | /* Address Modify Enable */
397                   VERTEX_SIZE << VB0_BUFFER_PITCH_SHIFT);
398         OUT_RELOC(batch->bo, I915_GEM_DOMAIN_VERTEX, 0, offset);
399         OUT_BATCH(3 * VERTEX_SIZE);
400 }
401
402 static uint32_t
403 gen6_create_cc_state(struct intel_batchbuffer *batch)
404 {
405         struct gen6_color_calc_state *cc_state;
406         uint32_t offset;
407
408         cc_state = batch_alloc(batch, sizeof(*cc_state), 64);
409         offset = batch_offset(batch, cc_state);
410         annotation_add_state(&aub_annotations, AUB_TRACE_CC_STATE,
411                              offset, sizeof(*cc_state));
412
413         return offset;
414 }
415
416 static uint32_t
417 gen8_create_blend_state(struct intel_batchbuffer *batch)
418 {
419         struct gen8_blend_state *blend;
420         int i;
421         uint32_t offset;
422
423         blend = batch_alloc(batch, sizeof(*blend), 64);
424         offset = batch_offset(batch, blend);
425         annotation_add_state(&aub_annotations, AUB_TRACE_BLEND_STATE,
426                              offset, sizeof(*blend));
427
428         for (i = 0; i < 16; i++) {
429                 blend->bs[i].dest_blend_factor = GEN6_BLENDFACTOR_ZERO;
430                 blend->bs[i].source_blend_factor = GEN6_BLENDFACTOR_ONE;
431                 blend->bs[i].color_blend_func = GEN6_BLENDFUNCTION_ADD;
432                 blend->bs[i].pre_blend_color_clamp = 1;
433                 blend->bs[i].color_buffer_blend = 0;
434         }
435
436         return offset;
437 }
438
439 static uint32_t
440 gen6_create_cc_viewport(struct intel_batchbuffer *batch)
441 {
442         struct gen6_cc_viewport *vp;
443         uint32_t offset;
444
445         vp = batch_alloc(batch, sizeof(*vp), 32);
446         offset = batch_offset(batch, vp);
447         annotation_add_state(&aub_annotations, AUB_TRACE_CC_VP_STATE,
448                              offset, sizeof(*vp));
449
450         /* XXX I don't understand this */
451         vp->min_depth = -1.e35;
452         vp->max_depth = 1.e35;
453
454         return offset;
455 }
456
457 static uint32_t
458 gen7_create_sf_clip_viewport(struct intel_batchbuffer *batch) {
459         /* XXX these are likely not needed */
460         struct gen7_sf_clip_viewport *scv_state;
461         uint32_t offset;
462
463         scv_state = batch_alloc(batch, sizeof(*scv_state), 64);
464         offset = batch_offset(batch, scv_state);
465         annotation_add_state(&aub_annotations, AUB_TRACE_CLIP_VP_STATE,
466                              offset, sizeof(*scv_state));
467
468         scv_state->guardband.xmin = 0;
469         scv_state->guardband.xmax = 1.0f;
470         scv_state->guardband.ymin = 0;
471         scv_state->guardband.ymax = 1.0f;
472
473         return offset;
474 }
475
476 static uint32_t
477 gen6_create_scissor_rect(struct intel_batchbuffer *batch)
478 {
479         struct gen6_scissor_rect *scissor;
480         uint32_t offset;
481
482         scissor = batch_alloc(batch, sizeof(*scissor), 64);
483         offset = batch_offset(batch, scissor);
484         annotation_add_state(&aub_annotations, AUB_TRACE_SCISSOR_STATE,
485                              offset, sizeof(*scissor));
486
487         return offset;
488 }
489
490 static void
491 gen8_emit_sip(struct intel_batchbuffer *batch) {
492         OUT_BATCH(GEN6_STATE_SIP | (3 - 2));
493         OUT_BATCH(0);
494         OUT_BATCH(0);
495 }
496
497 static void
498 gen7_emit_push_constants(struct intel_batchbuffer *batch) {
499         OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_VS);
500         OUT_BATCH(0);
501         OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_HS);
502         OUT_BATCH(0);
503         OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_DS);
504         OUT_BATCH(0);
505         OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_GS);
506         OUT_BATCH(0);
507         OUT_BATCH(GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_PS);
508         OUT_BATCH(0);
509 }
510
511 static void
512 gen8_emit_state_base_address(struct intel_batchbuffer *batch) {
513         OUT_BATCH(GEN6_STATE_BASE_ADDRESS | (16 - 2));
514
515         /* general */
516         OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
517         OUT_BATCH(0);
518
519         /* stateless data port */
520         OUT_BATCH(0 | BASE_ADDRESS_MODIFY);
521
522         /* surface */
523         OUT_RELOC(batch->bo, I915_GEM_DOMAIN_SAMPLER, 0, BASE_ADDRESS_MODIFY);
524
525         /* dynamic */
526         OUT_RELOC(batch->bo, I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION,
527                   0, BASE_ADDRESS_MODIFY);
528
529         /* indirect */
530         OUT_BATCH(0);
531         OUT_BATCH(0);
532
533         /* instruction */
534         OUT_RELOC(batch->bo, I915_GEM_DOMAIN_INSTRUCTION, 0, BASE_ADDRESS_MODIFY);
535
536         /* general state buffer size */
537         OUT_BATCH(0xfffff000 | 1);
538         /* dynamic state buffer size */
539         OUT_BATCH(1 << 12 | 1);
540         /* indirect object buffer size */
541         OUT_BATCH(0xfffff000 | 1);
542         /* intruction buffer size */
543         OUT_BATCH(1 << 12 | 1);
544 }
545
546 static void
547 gen7_emit_urb(struct intel_batchbuffer *batch) {
548         /* XXX: Min valid values from mesa */
549         const int vs_entries = 64;
550         const int vs_size = 2;
551         const int vs_start = 2;
552
553         OUT_BATCH(GEN7_3DSTATE_URB_VS);
554         OUT_BATCH(vs_entries | ((vs_size - 1) << 16) | (vs_start << 25));
555         OUT_BATCH(GEN7_3DSTATE_URB_GS);
556         OUT_BATCH(vs_start << 25);
557         OUT_BATCH(GEN7_3DSTATE_URB_HS);
558         OUT_BATCH(vs_start << 25);
559         OUT_BATCH(GEN7_3DSTATE_URB_DS);
560         OUT_BATCH(vs_start << 25);
561 }
562
563 static void
564 gen8_emit_cc(struct intel_batchbuffer *batch) {
565         OUT_BATCH(GEN7_3DSTATE_BLEND_STATE_POINTERS);
566         OUT_BATCH(cc.blend_state | 1);
567
568         OUT_BATCH(GEN6_3DSTATE_CC_STATE_POINTERS);
569         OUT_BATCH(cc.cc_state | 1);
570 }
571
572 static void
573 gen8_emit_multisample(struct intel_batchbuffer *batch) {
574         OUT_BATCH(GEN8_3DSTATE_MULTISAMPLE);
575         OUT_BATCH(0);
576
577         OUT_BATCH(GEN6_3DSTATE_SAMPLE_MASK);
578         OUT_BATCH(1);
579 }
580
581 static void
582 gen8_emit_vs(struct intel_batchbuffer *batch) {
583         OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_VS);
584         OUT_BATCH(0);
585
586         OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_VS);
587         OUT_BATCH(0);
588
589         OUT_BATCH(GEN6_3DSTATE_CONSTANT_VS | (11 - 2));
590         OUT_BATCH(0);
591         OUT_BATCH(0);
592         OUT_BATCH(0);
593         OUT_BATCH(0);
594         OUT_BATCH(0);
595         OUT_BATCH(0);
596         OUT_BATCH(0);
597         OUT_BATCH(0);
598         OUT_BATCH(0);
599         OUT_BATCH(0);
600
601         OUT_BATCH(GEN6_3DSTATE_VS | (9-2));
602         OUT_BATCH(0);
603         OUT_BATCH(0);
604         OUT_BATCH(0);
605         OUT_BATCH(0);
606         OUT_BATCH(0);
607         OUT_BATCH(0);
608         OUT_BATCH(0);
609         OUT_BATCH(0);
610 }
611
612 static void
613 gen8_emit_hs(struct intel_batchbuffer *batch) {
614         OUT_BATCH(GEN7_3DSTATE_CONSTANT_HS | (11 - 2));
615         OUT_BATCH(0);
616         OUT_BATCH(0);
617         OUT_BATCH(0);
618         OUT_BATCH(0);
619         OUT_BATCH(0);
620         OUT_BATCH(0);
621         OUT_BATCH(0);
622         OUT_BATCH(0);
623         OUT_BATCH(0);
624         OUT_BATCH(0);
625
626         OUT_BATCH(GEN7_3DSTATE_HS | (9-2));
627         OUT_BATCH(0);
628         OUT_BATCH(0);
629         OUT_BATCH(0);
630         OUT_BATCH(0);
631         OUT_BATCH(0);
632         OUT_BATCH(0);
633         OUT_BATCH(0);
634         OUT_BATCH(0);
635
636         OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_HS);
637         OUT_BATCH(0);
638
639         OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_HS);
640         OUT_BATCH(0);
641 }
642
643 static void
644 gen8_emit_gs(struct intel_batchbuffer *batch) {
645         OUT_BATCH(GEN7_3DSTATE_CONSTANT_GS | (11 - 2));
646         OUT_BATCH(0);
647         OUT_BATCH(0);
648         OUT_BATCH(0);
649         OUT_BATCH(0);
650         OUT_BATCH(0);
651         OUT_BATCH(0);
652         OUT_BATCH(0);
653         OUT_BATCH(0);
654         OUT_BATCH(0);
655         OUT_BATCH(0);
656
657         OUT_BATCH(GEN7_3DSTATE_GS | (10-2));
658         OUT_BATCH(0);
659         OUT_BATCH(0);
660         OUT_BATCH(0);
661         OUT_BATCH(0);
662         OUT_BATCH(0);
663         OUT_BATCH(0);
664         OUT_BATCH(0);
665         OUT_BATCH(0);
666         OUT_BATCH(0);
667
668         OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_GS);
669         OUT_BATCH(0);
670
671         OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_GS);
672         OUT_BATCH(0);
673 }
674
675 static void
676 gen8_emit_ds(struct intel_batchbuffer *batch) {
677         OUT_BATCH(GEN7_3DSTATE_CONSTANT_DS | (11 - 2));
678         OUT_BATCH(0);
679         OUT_BATCH(0);
680         OUT_BATCH(0);
681         OUT_BATCH(0);
682         OUT_BATCH(0);
683         OUT_BATCH(0);
684         OUT_BATCH(0);
685         OUT_BATCH(0);
686         OUT_BATCH(0);
687         OUT_BATCH(0);
688
689         OUT_BATCH(GEN7_3DSTATE_DS | (9-2));
690         OUT_BATCH(0);
691         OUT_BATCH(0);
692         OUT_BATCH(0);
693         OUT_BATCH(0);
694         OUT_BATCH(0);
695         OUT_BATCH(0);
696         OUT_BATCH(0);
697         OUT_BATCH(0);
698
699         OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_DS);
700         OUT_BATCH(0);
701
702         OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_DS);
703         OUT_BATCH(0);
704 }
705
706 static void
707 gen8_emit_wm_hz_op(struct intel_batchbuffer *batch) {
708         OUT_BATCH(GEN8_3DSTATE_WM_HZ_OP | (5-2));
709         OUT_BATCH(0);
710         OUT_BATCH(0);
711         OUT_BATCH(0);
712         OUT_BATCH(0);
713 }
714
715 static void
716 gen8_emit_null_state(struct intel_batchbuffer *batch) {
717         gen8_emit_wm_hz_op(batch);
718         gen8_emit_hs(batch);
719         OUT_BATCH(GEN7_3DSTATE_TE | (4-2));
720         OUT_BATCH(0);
721         OUT_BATCH(0);
722         OUT_BATCH(0);
723         gen8_emit_gs(batch);
724         gen8_emit_ds(batch);
725         gen8_emit_vs(batch);
726 }
727
728 static void
729 gen7_emit_clip(struct intel_batchbuffer *batch) {
730         OUT_BATCH(GEN6_3DSTATE_CLIP | (4 - 2));
731         OUT_BATCH(0); 
732         OUT_BATCH(0); /*  pass-through */
733         OUT_BATCH(0);
734 }
735
736 static void
737 gen8_emit_sf(struct intel_batchbuffer *batch)
738 {
739         int i;
740
741         OUT_BATCH(GEN7_3DSTATE_SBE | (4 - 2));
742         OUT_BATCH(1 << GEN7_SBE_NUM_OUTPUTS_SHIFT |
743                   GEN8_SBE_FORCE_URB_ENTRY_READ_LENGTH |
744                   GEN8_SBE_FORCE_URB_ENTRY_READ_OFFSET |
745                   1 << GEN7_SBE_URB_ENTRY_READ_LENGTH_SHIFT |
746                   1 << GEN8_SBE_URB_ENTRY_READ_OFFSET_SHIFT);
747         OUT_BATCH(0);
748         OUT_BATCH(0);
749
750         OUT_BATCH(GEN8_3DSTATE_SBE_SWIZ | (11 - 2));
751         for (i = 0; i < 8; i++)
752                 OUT_BATCH(0);
753         OUT_BATCH(0);
754         OUT_BATCH(0);
755
756         OUT_BATCH(GEN8_3DSTATE_RASTER | (5 - 2));
757         OUT_BATCH(GEN8_RASTER_FRONT_WINDING_CCW | GEN8_RASTER_CULL_NONE);
758         OUT_BATCH(0);
759         OUT_BATCH(0);
760         OUT_BATCH(0);
761
762         OUT_BATCH(GEN6_3DSTATE_SF | (4 - 2));
763         OUT_BATCH(0);
764         OUT_BATCH(0);
765         OUT_BATCH(0);
766 }
767
768 static void
769 gen8_emit_ps(struct intel_batchbuffer *batch, uint32_t kernel) {
770         const int max_threads = 63;
771
772         OUT_BATCH(GEN6_3DSTATE_WM | (2 - 2));
773         OUT_BATCH(/* XXX: I don't understand the BARYCENTRIC stuff, but it
774                    * appears we need it to put our setup data in the place we
775                    * expect (g6, see below) */
776                   GEN7_3DSTATE_PS_PERSPECTIVE_PIXEL_BARYCENTRIC);
777
778         OUT_BATCH(GEN6_3DSTATE_CONSTANT_PS | (11-2));
779         OUT_BATCH(0);
780         OUT_BATCH(0);
781         OUT_BATCH(0);
782         OUT_BATCH(0);
783         OUT_BATCH(0);
784         OUT_BATCH(0);
785         OUT_BATCH(0);
786         OUT_BATCH(0);
787         OUT_BATCH(0);
788         OUT_BATCH(0);
789
790         OUT_BATCH(GEN7_3DSTATE_PS | (12-2));
791         OUT_BATCH(kernel);
792         OUT_BATCH(0); /* kernel hi */
793         OUT_BATCH(1 << GEN6_3DSTATE_WM_SAMPLER_COUNT_SHIFT |
794                   2 << GEN6_3DSTATE_WM_BINDING_TABLE_ENTRY_COUNT_SHIFT);
795         OUT_BATCH(0); /* scratch space stuff */
796         OUT_BATCH(0); /* scratch hi */
797         OUT_BATCH((max_threads - 1) << GEN8_3DSTATE_PS_MAX_THREADS_SHIFT |
798                   GEN6_3DSTATE_WM_16_DISPATCH_ENABLE);
799         OUT_BATCH(6 << GEN6_3DSTATE_WM_DISPATCH_START_GRF_0_SHIFT);
800         OUT_BATCH(0); // kernel 1
801         OUT_BATCH(0); /* kernel 1 hi */
802         OUT_BATCH(0); // kernel 2
803         OUT_BATCH(0); /* kernel 2 hi */
804
805         OUT_BATCH(GEN8_3DSTATE_PS_BLEND | (2 - 2));
806         OUT_BATCH(GEN8_PS_BLEND_HAS_WRITEABLE_RT);
807
808         OUT_BATCH(GEN8_3DSTATE_PS_EXTRA | (2 - 2));
809         OUT_BATCH(GEN8_PSX_PIXEL_SHADER_VALID | GEN8_PSX_ATTRIBUTE_ENABLE);
810 }
811
812 static void
813 gen8_emit_depth(struct intel_batchbuffer *batch) {
814         OUT_BATCH(GEN8_3DSTATE_WM_DEPTH_STENCIL | (3 - 2));
815         OUT_BATCH(0);
816         OUT_BATCH(0);
817
818         OUT_BATCH(GEN7_3DSTATE_DEPTH_BUFFER | (8-2));
819         OUT_BATCH(0);
820         OUT_BATCH(0);
821         OUT_BATCH(0);
822         OUT_BATCH(0);
823         OUT_BATCH(0);
824         OUT_BATCH(0);
825         OUT_BATCH(0);
826
827         OUT_BATCH(GEN7_3DSTATE_HIER_DEPTH_BUFFER | (5 - 2));
828         OUT_BATCH(0);
829         OUT_BATCH(0);
830         OUT_BATCH(0);
831         OUT_BATCH(0);
832
833         OUT_BATCH(GEN7_3DSTATE_STENCIL_BUFFER | (5 - 2));
834         OUT_BATCH(0);
835         OUT_BATCH(0);
836         OUT_BATCH(0);
837         OUT_BATCH(0);
838 }
839
840 static void
841 gen7_emit_clear(struct intel_batchbuffer *batch) {
842         OUT_BATCH(GEN7_3DSTATE_CLEAR_PARAMS | (3-2));
843         OUT_BATCH(0);
844         OUT_BATCH(1); // clear valid
845 }
846
847 static void
848 gen6_emit_drawing_rectangle(struct intel_batchbuffer *batch, struct igt_buf *dst)
849 {
850         OUT_BATCH(GEN6_3DSTATE_DRAWING_RECTANGLE | (4 - 2));
851         OUT_BATCH(0);
852         OUT_BATCH((igt_buf_height(dst) - 1) << 16 | (igt_buf_width(dst) - 1));
853         OUT_BATCH(0);
854 }
855
856 static void gen8_emit_vf_topology(struct intel_batchbuffer *batch)
857 {
858         OUT_BATCH(GEN8_3DSTATE_VF_TOPOLOGY);
859         OUT_BATCH(_3DPRIM_RECTLIST);
860 }
861
862 /* Vertex elements MUST be defined before this according to spec */
863 static void gen8_emit_primitive(struct intel_batchbuffer *batch, uint32_t offset)
864 {
865         OUT_BATCH(GEN8_3DSTATE_VF_INSTANCING | (3 - 2));
866         OUT_BATCH(0);
867         OUT_BATCH(0);
868
869         OUT_BATCH(GEN6_3DPRIMITIVE | (7-2));
870         OUT_BATCH(0);   /* gen8+ ignore the topology type field */
871         OUT_BATCH(3);   /* vertex count */
872         OUT_BATCH(0);   /*  We're specifying this instead with offset in GEN6_3DSTATE_VERTEX_BUFFERS */
873         OUT_BATCH(1);   /* single instance */
874         OUT_BATCH(0);   /* start instance location */
875         OUT_BATCH(0);   /* index buffer offset, ignored */
876 }
877
878 /* The general rule is if it's named gen6 it is directly copied from
879  * gen6_render_copyfunc.
880  *
881  * This sets up most of the 3d pipeline, and most of that to NULL state. The
882  * docs aren't specific about exactly what must be set up NULL, but the general
883  * rule is we could be run at any time, and so the most state we set to NULL,
884  * the better our odds of success.
885  *
886  * +---------------+ <---- 4096
887  * |       ^       |
888  * |       |       |
889  * |    various    |
890  * |      state    |
891  * |       |       |
892  * |_______|_______| <---- 2048 + ?
893  * |       ^       |
894  * |       |       |
895  * |   batch       |
896  * |    commands   |
897  * |       |       |
898  * |       |       |
899  * +---------------+ <---- 0 + ?
900  *
901  * The batch commands point to state within tthe batch, so all state offsets should be
902  * 0 < offset < 4096. Both commands and state build upwards, and are constructed
903  * in that order. This means too many batch commands can delete state if not
904  * careful.
905  *
906  */
907
908 #define BATCH_STATE_SPLIT 2048
909
910 void gen8_render_copyfunc(struct intel_batchbuffer *batch,
911                           drm_intel_context *context,
912                           struct igt_buf *src, unsigned src_x, unsigned src_y,
913                           unsigned width, unsigned height,
914                           struct igt_buf *dst, unsigned dst_x, unsigned dst_y)
915 {
916         uint32_t ps_sampler_state, ps_kernel_off, ps_binding_table;
917         uint32_t scissor_state;
918         uint32_t vertex_buffer;
919         uint32_t batch_end;
920
921         intel_batchbuffer_flush_with_context(batch, context);
922
923         batch_align(batch, 8);
924
925         batch->ptr = &batch->buffer[BATCH_STATE_SPLIT];
926
927         annotation_init(&aub_annotations);
928
929         ps_binding_table  = gen8_bind_surfaces(batch, src, dst);
930         ps_sampler_state  = gen8_create_sampler(batch);
931         ps_kernel_off = gen8_fill_ps(batch, ps_kernel, sizeof(ps_kernel));
932         vertex_buffer = gen7_fill_vertex_buffer_data(batch, src,
933                                                      src_x, src_y,
934                                                      dst_x, dst_y,
935                                                      width, height);
936         cc.cc_state = gen6_create_cc_state(batch);
937         cc.blend_state = gen8_create_blend_state(batch);
938         viewport.cc_state = gen6_create_cc_viewport(batch);
939         viewport.sf_clip_state = gen7_create_sf_clip_viewport(batch);
940         scissor_state = gen6_create_scissor_rect(batch);
941         /* TODO: theree is other state which isn't setup */
942
943         igt_assert(batch->ptr < &batch->buffer[4095]);
944
945         batch->ptr = batch->buffer;
946
947         /* Start emitting the commands. The order roughly follows the mesa blorp
948          * order */
949         OUT_BATCH(GEN6_PIPELINE_SELECT | PIPELINE_SELECT_3D);
950
951         gen8_emit_sip(batch);
952
953         gen7_emit_push_constants(batch);
954
955         gen8_emit_state_base_address(batch);
956
957         OUT_BATCH(GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_CC);
958         OUT_BATCH(viewport.cc_state);
959         OUT_BATCH(GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP);
960         OUT_BATCH(viewport.sf_clip_state);
961
962         gen7_emit_urb(batch);
963
964         gen8_emit_cc(batch);
965
966         gen8_emit_multisample(batch);
967
968         gen8_emit_null_state(batch);
969
970         OUT_BATCH(GEN7_3DSTATE_STREAMOUT | (5-2));
971         OUT_BATCH(0);
972         OUT_BATCH(0);
973         OUT_BATCH(0);
974         OUT_BATCH(0);
975
976         gen7_emit_clip(batch);
977
978         gen8_emit_sf(batch);
979
980         OUT_BATCH(GEN7_3DSTATE_BINDING_TABLE_POINTERS_PS);
981         OUT_BATCH(ps_binding_table);
982
983         OUT_BATCH(GEN7_3DSTATE_SAMPLER_STATE_POINTERS_PS);
984         OUT_BATCH(ps_sampler_state);
985
986         gen8_emit_ps(batch, ps_kernel_off);
987
988         OUT_BATCH(GEN6_3DSTATE_SCISSOR_STATE_POINTERS);
989         OUT_BATCH(scissor_state);
990
991         gen8_emit_depth(batch);
992
993         gen7_emit_clear(batch);
994
995         gen6_emit_drawing_rectangle(batch, dst);
996
997         gen8_emit_vertex_buffer(batch, vertex_buffer);
998         gen6_emit_vertex_elements(batch);
999
1000         gen8_emit_vf_topology(batch);
1001         gen8_emit_primitive(batch, vertex_buffer);
1002
1003         OUT_BATCH(MI_BATCH_BUFFER_END);
1004
1005         batch_end = batch_align(batch, 8);
1006         igt_assert(batch_end < BATCH_STATE_SPLIT);
1007         annotation_add_batch(&aub_annotations, batch_end);
1008
1009         dump_batch(batch);
1010
1011         annotation_flush(&aub_annotations, batch);
1012
1013         gen6_render_flush(batch, context, batch_end);
1014         intel_batchbuffer_reset(batch);
1015 }