OSDN Git Service

ilo: fix textureSize() for single-layered array textures
[android-x86/external-mesa.git] / src / gallium / drivers / ilo / ilo_gpe_gen7.c
1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (C) 2013 LunarG, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Chia-I Wu <olv@lunarg.com>
26  */
27
28 #include "util/u_resource.h"
29 #include "brw_defines.h"
30 #include "intel_reg.h"
31
32 #include "ilo_cp.h"
33 #include "ilo_format.h"
34 #include "ilo_resource.h"
35 #include "ilo_shader.h"
36 #include "ilo_gpe_gen7.h"
37
38 static void
39 gen7_emit_GPGPU_WALKER(const struct ilo_dev_info *dev,
40                        struct ilo_cp *cp)
41 {
42    assert(!"GPGPU_WALKER unsupported");
43 }
44
45 static void
46 gen7_emit_3DSTATE_CLEAR_PARAMS(const struct ilo_dev_info *dev,
47                                uint32_t clear_val,
48                                struct ilo_cp *cp)
49 {
50    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x04);
51    const uint8_t cmd_len = 3;
52
53    ILO_GPE_VALID_GEN(dev, 7, 7);
54
55    ilo_cp_begin(cp, cmd_len);
56    ilo_cp_write(cp, cmd | (cmd_len - 2));
57    ilo_cp_write(cp, clear_val);
58    ilo_cp_write(cp, 1);
59    ilo_cp_end(cp);
60 }
61
62 static void
63 gen7_emit_3dstate_pointer(const struct ilo_dev_info *dev,
64                           int subop, uint32_t pointer,
65                           struct ilo_cp *cp)
66 {
67    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, subop);
68    const uint8_t cmd_len = 2;
69
70    ILO_GPE_VALID_GEN(dev, 7, 7);
71
72    ilo_cp_begin(cp, cmd_len);
73    ilo_cp_write(cp, cmd | (cmd_len - 2));
74    ilo_cp_write(cp, pointer);
75    ilo_cp_end(cp);
76 }
77
78 static void
79 gen7_emit_3DSTATE_CC_STATE_POINTERS(const struct ilo_dev_info *dev,
80                                     uint32_t color_calc_state,
81                                     struct ilo_cp *cp)
82 {
83    gen7_emit_3dstate_pointer(dev, 0x0e, color_calc_state, cp);
84 }
85
86 static void
87 gen7_emit_3DSTATE_GS(const struct ilo_dev_info *dev,
88                      const struct ilo_shader *gs,
89                      int num_samplers,
90                      struct ilo_cp *cp)
91 {
92    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x11);
93    const uint8_t cmd_len = 7;
94    uint32_t dw2, dw4, dw5;
95    int max_threads;
96
97    ILO_GPE_VALID_GEN(dev, 7, 7);
98
99    switch (dev->gen) {
100    case ILO_GEN(7):
101       max_threads = (dev->gt == 2) ? 128 : 36;
102       break;
103    default:
104       max_threads = 1;
105       break;
106    }
107
108    if (!gs) {
109       ilo_cp_begin(cp, cmd_len);
110       ilo_cp_write(cp, cmd | (cmd_len - 2));
111       ilo_cp_write(cp, 0);
112       ilo_cp_write(cp, 0);
113       ilo_cp_write(cp, 0);
114       ilo_cp_write(cp, 0);
115       ilo_cp_write(cp, GEN6_GS_STATISTICS_ENABLE);
116       ilo_cp_write(cp, 0);
117       ilo_cp_end(cp);
118       return;
119    }
120
121    dw2 = ((num_samplers + 3) / 4) << GEN6_GS_SAMPLER_COUNT_SHIFT;
122
123    dw4 = ((gs->in.count + 1) / 2) << GEN6_GS_URB_READ_LENGTH_SHIFT |
124          GEN7_GS_INCLUDE_VERTEX_HANDLES |
125          0 << GEN6_GS_URB_ENTRY_READ_OFFSET_SHIFT |
126          gs->in.start_grf << GEN6_GS_DISPATCH_START_GRF_SHIFT;
127
128    dw5 = (max_threads - 1) << GEN6_GS_MAX_THREADS_SHIFT |
129          GEN6_GS_STATISTICS_ENABLE |
130          GEN6_GS_ENABLE;
131
132    ilo_cp_begin(cp, cmd_len);
133    ilo_cp_write(cp, cmd | (cmd_len - 2));
134    ilo_cp_write(cp, gs->cache_offset);
135    ilo_cp_write(cp, dw2);
136    ilo_cp_write(cp, 0); /* scratch */
137    ilo_cp_write(cp, dw4);
138    ilo_cp_write(cp, dw5);
139    ilo_cp_write(cp, 0);
140    ilo_cp_end(cp);
141 }
142
143 static void
144 gen7_emit_3DSTATE_SF(const struct ilo_dev_info *dev,
145                      const struct ilo_rasterizer_state *rasterizer,
146                      const struct pipe_surface *zs_surf,
147                      struct ilo_cp *cp)
148 {
149    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x13);
150    const uint8_t cmd_len = 7;
151    const int num_samples = 1;
152    uint32_t payload[6];
153
154    ILO_GPE_VALID_GEN(dev, 7, 7);
155
156    ilo_gpe_gen6_fill_3dstate_sf_raster(dev,
157          &rasterizer->sf, num_samples,
158          (zs_surf) ? zs_surf->format : PIPE_FORMAT_NONE,
159          payload, Elements(payload));
160
161    ilo_cp_begin(cp, cmd_len);
162    ilo_cp_write(cp, cmd | (cmd_len - 2));
163    ilo_cp_write_multi(cp, payload, 6);
164    ilo_cp_end(cp);
165 }
166
167 static void
168 gen7_emit_3DSTATE_WM(const struct ilo_dev_info *dev,
169                      const struct ilo_shader *fs,
170                      const struct pipe_rasterizer_state *rasterizer,
171                      bool cc_may_kill,
172                      struct ilo_cp *cp)
173 {
174    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x14);
175    const uint8_t cmd_len = 3;
176    const int num_samples = 1;
177    uint32_t dw1, dw2;
178
179    ILO_GPE_VALID_GEN(dev, 7, 7);
180
181    dw1 = GEN7_WM_STATISTICS_ENABLE |
182          GEN7_WM_LINE_AA_WIDTH_2_0;
183
184    if (false) {
185       dw1 |= GEN7_WM_DEPTH_CLEAR;
186       dw1 |= GEN7_WM_DEPTH_RESOLVE;
187       dw1 |= GEN7_WM_HIERARCHICAL_DEPTH_RESOLVE;
188    }
189
190    if (fs) {
191       /*
192        * Set this bit if
193        *
194        *  a) fs writes colors and color is not masked, or
195        *  b) fs writes depth, or
196        *  c) fs or cc kills
197        */
198       dw1 |= GEN7_WM_DISPATCH_ENABLE;
199
200       /*
201        * From the Ivy Bridge PRM, volume 2 part 1, page 278:
202        *
203        *     "This bit (Pixel Shader Kill Pixel), if ENABLED, indicates that
204        *      the PS kernel or color calculator has the ability to kill
205        *      (discard) pixels or samples, other than due to depth or stencil
206        *      testing. This bit is required to be ENABLED in the following
207        *      situations:
208        *
209        *      - The API pixel shader program contains "killpix" or "discard"
210        *        instructions, or other code in the pixel shader kernel that
211        *        can cause the final pixel mask to differ from the pixel mask
212        *        received on dispatch.
213        *
214        *      - A sampler with chroma key enabled with kill pixel mode is used
215        *        by the pixel shader.
216        *
217        *      - Any render target has Alpha Test Enable or AlphaToCoverage
218        *        Enable enabled.
219        *
220        *      - The pixel shader kernel generates and outputs oMask.
221        *
222        *      Note: As ClipDistance clipping is fully supported in hardware
223        *      and therefore not via PS instructions, there should be no need
224        *      to ENABLE this bit due to ClipDistance clipping."
225        */
226       if (fs->has_kill || cc_may_kill)
227          dw1 |= GEN7_WM_KILL_ENABLE;
228
229       if (fs->out.has_pos)
230          dw1 |= GEN7_WM_PSCDEPTH_ON;
231       if (fs->in.has_pos)
232          dw1 |= GEN7_WM_USES_SOURCE_DEPTH | GEN7_WM_USES_SOURCE_W;
233
234       dw1 |= fs->in.barycentric_interpolation_mode <<
235          GEN7_WM_BARYCENTRIC_INTERPOLATION_MODE_SHIFT;
236    }
237    else if (cc_may_kill) {
238          dw1 |= GEN7_WM_DISPATCH_ENABLE |
239                 GEN7_WM_KILL_ENABLE;
240    }
241
242    dw1 |= GEN7_WM_POSITION_ZW_PIXEL;
243
244    /* same value as in 3DSTATE_SF */
245    if (rasterizer->line_smooth)
246       dw1 |= GEN7_WM_LINE_END_CAP_AA_WIDTH_1_0;
247
248    if (rasterizer->poly_stipple_enable)
249       dw1 |= GEN7_WM_POLYGON_STIPPLE_ENABLE;
250    if (rasterizer->line_stipple_enable)
251       dw1 |= GEN7_WM_LINE_STIPPLE_ENABLE;
252
253    if (rasterizer->bottom_edge_rule)
254       dw1 |= GEN7_WM_POINT_RASTRULE_UPPER_RIGHT;
255
256    if (num_samples > 1) {
257       if (rasterizer->multisample)
258          dw1 |= GEN7_WM_MSRAST_ON_PATTERN;
259       else
260          dw1 |= GEN7_WM_MSRAST_OFF_PIXEL;
261
262       dw2 = GEN7_WM_MSDISPMODE_PERPIXEL;
263    }
264    else {
265       dw1 |= GEN7_WM_MSRAST_OFF_PIXEL;
266
267       dw2 = GEN7_WM_MSDISPMODE_PERSAMPLE;
268    }
269
270    ilo_cp_begin(cp, cmd_len);
271    ilo_cp_write(cp, cmd | (cmd_len - 2));
272    ilo_cp_write(cp, dw1);
273    ilo_cp_write(cp, dw2);
274    ilo_cp_end(cp);
275 }
276
277 static void
278 gen7_emit_3dstate_constant(const struct ilo_dev_info *dev,
279                            int subop,
280                            const uint32_t *bufs, const int *sizes,
281                            int num_bufs,
282                            struct ilo_cp *cp)
283 {
284    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, subop);
285    const uint8_t cmd_len = 7;
286    uint32_t dw[6];
287    int total_read_length, i;
288
289    ILO_GPE_VALID_GEN(dev, 7, 7);
290
291    /* VS, HS, DS, GS, and PS variants */
292    assert(subop >= 0x15 && subop <= 0x1a && subop != 0x18);
293
294    assert(num_bufs <= 4);
295
296    dw[0] = 0;
297    dw[1] = 0;
298
299    total_read_length = 0;
300    for (i = 0; i < 4; i++) {
301       int read_len;
302
303       /*
304        * From the Ivy Bridge PRM, volume 2 part 1, page 112:
305        *
306        *     "Constant buffers must be enabled in order from Constant Buffer 0
307        *      to Constant Buffer 3 within this command.  For example, it is
308        *      not allowed to enable Constant Buffer 1 by programming a
309        *      non-zero value in the VS Constant Buffer 1 Read Length without a
310        *      non-zero value in VS Constant Buffer 0 Read Length."
311        */
312       if (i >= num_bufs || !sizes[i]) {
313          for (; i < 4; i++) {
314             assert(i >= num_bufs || !sizes[i]);
315             dw[2 + i] = 0;
316          }
317          break;
318       }
319
320       /* read lengths are in 256-bit units */
321       read_len = (sizes[i] + 31) / 32;
322       /* the lower 5 bits are used for memory object control state */
323       assert(bufs[i] % 32 == 0);
324
325       dw[i / 2] |= read_len << ((i % 2) ? 16 : 0);
326       dw[2 + i] = bufs[i];
327
328       total_read_length += read_len;
329    }
330
331    /*
332     * From the Ivy Bridge PRM, volume 2 part 1, page 113:
333     *
334     *     "The sum of all four read length fields must be less than or equal
335     *      to the size of 64"
336     */
337    assert(total_read_length <= 64);
338
339    ilo_cp_begin(cp, cmd_len);
340    ilo_cp_write(cp, cmd | (cmd_len - 2));
341    ilo_cp_write_multi(cp, dw, 6);
342    ilo_cp_end(cp);
343 }
344
345 static void
346 gen7_emit_3DSTATE_CONSTANT_VS(const struct ilo_dev_info *dev,
347                               const uint32_t *bufs, const int *sizes,
348                               int num_bufs,
349                               struct ilo_cp *cp)
350 {
351    gen7_emit_3dstate_constant(dev, 0x15, bufs, sizes, num_bufs, cp);
352 }
353
354 static void
355 gen7_emit_3DSTATE_CONSTANT_GS(const struct ilo_dev_info *dev,
356                               const uint32_t *bufs, const int *sizes,
357                               int num_bufs,
358                               struct ilo_cp *cp)
359 {
360    gen7_emit_3dstate_constant(dev, 0x16, bufs, sizes, num_bufs, cp);
361 }
362
363 static void
364 gen7_emit_3DSTATE_CONSTANT_PS(const struct ilo_dev_info *dev,
365                               const uint32_t *bufs, const int *sizes,
366                               int num_bufs,
367                               struct ilo_cp *cp)
368 {
369    gen7_emit_3dstate_constant(dev, 0x17, bufs, sizes, num_bufs, cp);
370 }
371
372 static void
373 gen7_emit_3DSTATE_SAMPLE_MASK(const struct ilo_dev_info *dev,
374                               unsigned sample_mask,
375                               int num_samples,
376                               struct ilo_cp *cp)
377 {
378    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x18);
379    const uint8_t cmd_len = 2;
380    const unsigned valid_mask = ((1 << num_samples) - 1) | 0x1;
381
382    ILO_GPE_VALID_GEN(dev, 7, 7);
383
384    /*
385     * From the Ivy Bridge PRM, volume 2 part 1, page 294:
386     *
387     *     "If Number of Multisamples is NUMSAMPLES_1, bits 7:1 of this field
388     *      (Sample Mask) must be zero.
389     *
390     *      If Number of Multisamples is NUMSAMPLES_4, bits 7:4 of this field
391     *      must be zero."
392     */
393    sample_mask &= valid_mask;
394
395    ilo_cp_begin(cp, cmd_len);
396    ilo_cp_write(cp, cmd | (cmd_len - 2));
397    ilo_cp_write(cp, sample_mask);
398    ilo_cp_end(cp);
399 }
400
401 static void
402 gen7_emit_3DSTATE_CONSTANT_HS(const struct ilo_dev_info *dev,
403                               const uint32_t *bufs, const int *sizes,
404                               int num_bufs,
405                               struct ilo_cp *cp)
406 {
407    gen7_emit_3dstate_constant(dev, 0x19, bufs, sizes, num_bufs, cp);
408 }
409
410 static void
411 gen7_emit_3DSTATE_CONSTANT_DS(const struct ilo_dev_info *dev,
412                               const uint32_t *bufs, const int *sizes,
413                               int num_bufs,
414                               struct ilo_cp *cp)
415 {
416    gen7_emit_3dstate_constant(dev, 0x1a, bufs, sizes, num_bufs, cp);
417 }
418
419 static void
420 gen7_emit_3DSTATE_HS(const struct ilo_dev_info *dev,
421                      const struct ilo_shader *hs,
422                      int max_threads, int num_samplers,
423                      struct ilo_cp *cp)
424 {
425    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x1b);
426    const uint8_t cmd_len = 7;
427    uint32_t dw1, dw2, dw5;
428
429    ILO_GPE_VALID_GEN(dev, 7, 7);
430
431    if (!hs) {
432       ilo_cp_begin(cp, cmd_len);
433       ilo_cp_write(cp, cmd | (cmd_len - 2));
434       ilo_cp_write(cp, 0);
435       ilo_cp_write(cp, 0);
436       ilo_cp_write(cp, 0);
437       ilo_cp_write(cp, 0);
438       ilo_cp_write(cp, 0);
439       ilo_cp_write(cp, 0);
440       ilo_cp_end(cp);
441
442       return;
443    }
444
445    dw1 = (num_samplers + 3) / 4 << 27 |
446          0 << 18 |
447          (max_threads - 1);
448    if (false)
449       dw1 |= 1 << 16;
450
451    dw2 = 1 << 31 | /* HS Enable */
452          1 << 29 | /* HS Statistics Enable */
453          0; /* Instance Count */
454
455    dw5 = hs->in.start_grf << 19 |
456          0 << 11 |
457          0 << 4;
458
459    ilo_cp_begin(cp, cmd_len);
460    ilo_cp_write(cp, cmd | (cmd_len - 2));
461    ilo_cp_write(cp, dw1);
462    ilo_cp_write(cp, dw2);
463    ilo_cp_write(cp, hs->cache_offset);
464    ilo_cp_write(cp, 0);
465    ilo_cp_write(cp, dw5);
466    ilo_cp_write(cp, 0);
467    ilo_cp_end(cp);
468 }
469
470 static void
471 gen7_emit_3DSTATE_TE(const struct ilo_dev_info *dev,
472                      struct ilo_cp *cp)
473 {
474    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x1c);
475    const uint8_t cmd_len = 4;
476
477    ILO_GPE_VALID_GEN(dev, 7, 7);
478
479    ilo_cp_begin(cp, cmd_len);
480    ilo_cp_write(cp, cmd | (cmd_len - 2));
481    ilo_cp_write(cp, 0);
482    ilo_cp_write(cp, 0);
483    ilo_cp_write(cp, 0);
484    ilo_cp_end(cp);
485 }
486
487 static void
488 gen7_emit_3DSTATE_DS(const struct ilo_dev_info *dev,
489                      const struct ilo_shader *ds,
490                      int max_threads, int num_samplers,
491                      struct ilo_cp *cp)
492 {
493    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x1d);
494    const uint8_t cmd_len = 6;
495    uint32_t dw2, dw4, dw5;
496
497    ILO_GPE_VALID_GEN(dev, 7, 7);
498
499    if (!ds) {
500       ilo_cp_begin(cp, cmd_len);
501       ilo_cp_write(cp, cmd | (cmd_len - 2));
502       ilo_cp_write(cp, 0);
503       ilo_cp_write(cp, 0);
504       ilo_cp_write(cp, 0);
505       ilo_cp_write(cp, 0);
506       ilo_cp_write(cp, 0);
507       ilo_cp_end(cp);
508
509       return;
510    }
511
512    dw2 = (num_samplers + 3) / 4 << 27 |
513          0 << 18 |
514          (max_threads - 1);
515    if (false)
516       dw2 |= 1 << 16;
517
518    dw4 = ds->in.start_grf << 20 |
519          0 << 11 |
520          0 << 4;
521
522    dw5 = (max_threads - 1) << 25 |
523          1 << 10 |
524          1;
525
526    ilo_cp_begin(cp, cmd_len);
527    ilo_cp_write(cp, cmd | (cmd_len - 2));
528    ilo_cp_write(cp, ds->cache_offset);
529    ilo_cp_write(cp, dw2);
530    ilo_cp_write(cp, 0);
531    ilo_cp_write(cp, dw4);
532    ilo_cp_write(cp, dw5);
533    ilo_cp_end(cp);
534 }
535
536 static void
537 gen7_emit_3DSTATE_STREAMOUT(const struct ilo_dev_info *dev,
538                             unsigned buffer_mask,
539                             int vertex_attrib_count,
540                             bool rasterizer_discard,
541                             struct ilo_cp *cp)
542 {
543    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x1e);
544    const uint8_t cmd_len = 3;
545    const bool enable = (buffer_mask != 0);
546    uint32_t dw1, dw2;
547    int read_len;
548
549    ILO_GPE_VALID_GEN(dev, 7, 7);
550
551    if (!enable) {
552       dw1 = 0 << SO_RENDER_STREAM_SELECT_SHIFT;
553       if (rasterizer_discard)
554          dw1 |= SO_RENDERING_DISABLE;
555
556       dw2 = 0;
557
558       ilo_cp_begin(cp, cmd_len);
559       ilo_cp_write(cp, cmd | (cmd_len - 2));
560       ilo_cp_write(cp, dw1);
561       ilo_cp_write(cp, dw2);
562       ilo_cp_end(cp);
563       return;
564    }
565
566    read_len = (vertex_attrib_count + 1) / 2;
567    if (!read_len)
568       read_len = 1;
569
570    dw1 = SO_FUNCTION_ENABLE |
571          0 << SO_RENDER_STREAM_SELECT_SHIFT |
572          SO_STATISTICS_ENABLE |
573          buffer_mask << 8;
574
575    if (rasterizer_discard)
576       dw1 |= SO_RENDERING_DISABLE;
577
578    /* API_OPENGL */
579    if (true)
580       dw1 |= SO_REORDER_TRAILING;
581
582    dw2 = 0 << SO_STREAM_3_VERTEX_READ_OFFSET_SHIFT |
583          0 << SO_STREAM_3_VERTEX_READ_LENGTH_SHIFT |
584          0 << SO_STREAM_2_VERTEX_READ_OFFSET_SHIFT |
585          0 << SO_STREAM_2_VERTEX_READ_LENGTH_SHIFT |
586          0 << SO_STREAM_1_VERTEX_READ_OFFSET_SHIFT |
587          0 << SO_STREAM_1_VERTEX_READ_LENGTH_SHIFT |
588          0 << SO_STREAM_0_VERTEX_READ_OFFSET_SHIFT |
589          (read_len - 1) << SO_STREAM_0_VERTEX_READ_LENGTH_SHIFT;
590
591    ilo_cp_begin(cp, cmd_len);
592    ilo_cp_write(cp, cmd | (cmd_len - 2));
593    ilo_cp_write(cp, dw1);
594    ilo_cp_write(cp, dw2);
595    ilo_cp_end(cp);
596 }
597
598 static void
599 gen7_emit_3DSTATE_SBE(const struct ilo_dev_info *dev,
600                       const struct pipe_rasterizer_state *rasterizer,
601                       const struct ilo_shader *fs,
602                       const struct ilo_shader *last_sh,
603                       struct ilo_cp *cp)
604 {
605    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x1f);
606    const uint8_t cmd_len = 14;
607    uint32_t dw[13];
608
609    ILO_GPE_VALID_GEN(dev, 7, 7);
610
611    ilo_gpe_gen6_fill_3dstate_sf_sbe(dev, rasterizer,
612          fs, last_sh, dw, Elements(dw));
613
614    ilo_cp_begin(cp, cmd_len);
615    ilo_cp_write(cp, cmd | (cmd_len - 2));
616    ilo_cp_write_multi(cp, dw, 13);
617    ilo_cp_end(cp);
618 }
619
620 static void
621 gen7_emit_3DSTATE_PS(const struct ilo_dev_info *dev,
622                      const struct ilo_shader *fs,
623                      int num_samplers, bool dual_blend,
624                      struct ilo_cp *cp)
625 {
626    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, 0x20);
627    const uint8_t cmd_len = 8;
628    uint32_t dw2, dw4, dw5;
629    int max_threads;
630
631    ILO_GPE_VALID_GEN(dev, 7, 7);
632
633    /* see brwCreateContext() */
634    max_threads = (dev->gt == 2) ? 172 : 48;
635
636    if (!fs) {
637       ilo_cp_begin(cp, cmd_len);
638       ilo_cp_write(cp, cmd | (cmd_len - 2));
639       ilo_cp_write(cp, 0);
640       ilo_cp_write(cp, 0);
641       ilo_cp_write(cp, 0);
642       /* GPU hangs if none of the dispatch enable bits is set */
643       ilo_cp_write(cp, (max_threads - 1) << IVB_PS_MAX_THREADS_SHIFT |
644                        GEN7_PS_8_DISPATCH_ENABLE);
645       ilo_cp_write(cp, 0);
646       ilo_cp_write(cp, 0);
647       ilo_cp_write(cp, 0);
648       ilo_cp_end(cp);
649
650       return;
651    }
652
653    dw2 = (num_samplers + 3) / 4 << GEN7_PS_SAMPLER_COUNT_SHIFT |
654          0 << GEN7_PS_BINDING_TABLE_ENTRY_COUNT_SHIFT;
655    if (false)
656       dw2 |= GEN7_PS_FLOATING_POINT_MODE_ALT;
657
658    dw4 = (max_threads - 1) << IVB_PS_MAX_THREADS_SHIFT |
659          GEN7_PS_POSOFFSET_NONE;
660
661    if (false)
662       dw4 |= GEN7_PS_PUSH_CONSTANT_ENABLE;
663    if (fs->in.count)
664       dw4 |= GEN7_PS_ATTRIBUTE_ENABLE;
665    if (dual_blend)
666       dw4 |= GEN7_PS_DUAL_SOURCE_BLEND_ENABLE;
667
668    if (fs->dispatch_16)
669       dw4 |= GEN7_PS_16_DISPATCH_ENABLE;
670    else
671       dw4 |= GEN7_PS_8_DISPATCH_ENABLE;
672
673    dw5 = fs->in.start_grf << GEN7_PS_DISPATCH_START_GRF_SHIFT_0 |
674          0 << GEN7_PS_DISPATCH_START_GRF_SHIFT_1 |
675          0 << GEN7_PS_DISPATCH_START_GRF_SHIFT_2;
676
677    ilo_cp_begin(cp, cmd_len);
678    ilo_cp_write(cp, cmd | (cmd_len - 2));
679    ilo_cp_write(cp, fs->cache_offset);
680    ilo_cp_write(cp, dw2);
681    ilo_cp_write(cp, 0); /* scratch */
682    ilo_cp_write(cp, dw4);
683    ilo_cp_write(cp, dw5);
684    ilo_cp_write(cp, 0); /* kernel 1 */
685    ilo_cp_write(cp, 0); /* kernel 2 */
686    ilo_cp_end(cp);
687 }
688
689 static void
690 gen7_emit_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(const struct ilo_dev_info *dev,
691                                                   uint32_t sf_clip_viewport,
692                                                   struct ilo_cp *cp)
693 {
694    gen7_emit_3dstate_pointer(dev, 0x21, sf_clip_viewport, cp);
695 }
696
697 static void
698 gen7_emit_3DSTATE_VIEWPORT_STATE_POINTERS_CC(const struct ilo_dev_info *dev,
699                                              uint32_t cc_viewport,
700                                              struct ilo_cp *cp)
701 {
702    gen7_emit_3dstate_pointer(dev, 0x23, cc_viewport, cp);
703 }
704
705 static void
706 gen7_emit_3DSTATE_BLEND_STATE_POINTERS(const struct ilo_dev_info *dev,
707                                        uint32_t blend_state,
708                                        struct ilo_cp *cp)
709 {
710    gen7_emit_3dstate_pointer(dev, 0x24, blend_state, cp);
711 }
712
713 static void
714 gen7_emit_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(const struct ilo_dev_info *dev,
715                                                uint32_t depth_stencil_state,
716                                                struct ilo_cp *cp)
717 {
718    gen7_emit_3dstate_pointer(dev, 0x25, depth_stencil_state, cp);
719 }
720
721 static void
722 gen7_emit_3DSTATE_BINDING_TABLE_POINTERS_VS(const struct ilo_dev_info *dev,
723                                             uint32_t binding_table,
724                                             struct ilo_cp *cp)
725 {
726    gen7_emit_3dstate_pointer(dev, 0x26, binding_table, cp);
727 }
728
729 static void
730 gen7_emit_3DSTATE_BINDING_TABLE_POINTERS_HS(const struct ilo_dev_info *dev,
731                                             uint32_t binding_table,
732                                             struct ilo_cp *cp)
733 {
734    gen7_emit_3dstate_pointer(dev, 0x27, binding_table, cp);
735 }
736
737 static void
738 gen7_emit_3DSTATE_BINDING_TABLE_POINTERS_DS(const struct ilo_dev_info *dev,
739                                             uint32_t binding_table,
740                                             struct ilo_cp *cp)
741 {
742    gen7_emit_3dstate_pointer(dev, 0x28, binding_table, cp);
743 }
744
745 static void
746 gen7_emit_3DSTATE_BINDING_TABLE_POINTERS_GS(const struct ilo_dev_info *dev,
747                                             uint32_t binding_table,
748                                             struct ilo_cp *cp)
749 {
750    gen7_emit_3dstate_pointer(dev, 0x29, binding_table, cp);
751 }
752
753 static void
754 gen7_emit_3DSTATE_BINDING_TABLE_POINTERS_PS(const struct ilo_dev_info *dev,
755                                             uint32_t binding_table,
756                                             struct ilo_cp *cp)
757 {
758    gen7_emit_3dstate_pointer(dev, 0x2a, binding_table, cp);
759 }
760
761 static void
762 gen7_emit_3DSTATE_SAMPLER_STATE_POINTERS_VS(const struct ilo_dev_info *dev,
763                                             uint32_t sampler_state,
764                                             struct ilo_cp *cp)
765 {
766    gen7_emit_3dstate_pointer(dev, 0x2b, sampler_state, cp);
767 }
768
769 static void
770 gen7_emit_3DSTATE_SAMPLER_STATE_POINTERS_HS(const struct ilo_dev_info *dev,
771                                             uint32_t sampler_state,
772                                             struct ilo_cp *cp)
773 {
774    gen7_emit_3dstate_pointer(dev, 0x2c, sampler_state, cp);
775 }
776
777 static void
778 gen7_emit_3DSTATE_SAMPLER_STATE_POINTERS_DS(const struct ilo_dev_info *dev,
779                                             uint32_t sampler_state,
780                                             struct ilo_cp *cp)
781 {
782    gen7_emit_3dstate_pointer(dev, 0x2d, sampler_state, cp);
783 }
784
785 static void
786 gen7_emit_3DSTATE_SAMPLER_STATE_POINTERS_GS(const struct ilo_dev_info *dev,
787                                             uint32_t sampler_state,
788                                             struct ilo_cp *cp)
789 {
790    gen7_emit_3dstate_pointer(dev, 0x2e, sampler_state, cp);
791 }
792
793 static void
794 gen7_emit_3DSTATE_SAMPLER_STATE_POINTERS_PS(const struct ilo_dev_info *dev,
795                                             uint32_t sampler_state,
796                                             struct ilo_cp *cp)
797 {
798    gen7_emit_3dstate_pointer(dev, 0x2f, sampler_state, cp);
799 }
800
801 static void
802 gen7_emit_3dstate_urb(const struct ilo_dev_info *dev,
803                       int subop, int offset, int size,
804                       int entry_size,
805                       struct ilo_cp *cp)
806 {
807    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x0, subop);
808    const uint8_t cmd_len = 2;
809    const int row_size = 64; /* 512 bits */
810    int alloc_size, num_entries, min_entries, max_entries;
811
812    ILO_GPE_VALID_GEN(dev, 7, 7);
813
814    /* VS, HS, DS, and GS variants */
815    assert(subop >= 0x30 && subop <= 0x33);
816
817    /* in multiples of 8KB */
818    assert(offset % 8192 == 0);
819    offset /= 8192;
820
821    /* in multiple of 512-bit rows */
822    alloc_size = (entry_size + row_size - 1) / row_size;
823    if (!alloc_size)
824       alloc_size = 1;
825
826    /*
827     * From the Ivy Bridge PRM, volume 2 part 1, page 34:
828     *
829     *     "VS URB Entry Allocation Size equal to 4(5 512-bit URB rows) may
830     *      cause performance to decrease due to banking in the URB. Element
831     *      sizes of 16 to 20 should be programmed with six 512-bit URB rows."
832     */
833    if (subop == 0x30 && alloc_size == 5)
834       alloc_size = 6;
835
836    /* in multiples of 8 */
837    num_entries = (size / row_size / alloc_size) & ~7;
838
839    switch (subop) {
840    case 0x30: /* 3DSTATE_URB_VS */
841       min_entries = 32;
842       max_entries = (dev->gt == 2) ? 704 : 512;
843
844       assert(num_entries >= min_entries);
845       if (num_entries > max_entries)
846          num_entries = max_entries;
847       break;
848    case 0x31: /* 3DSTATE_URB_HS */
849       max_entries = (dev->gt == 2) ? 64 : 32;
850       if (num_entries > max_entries)
851          num_entries = max_entries;
852       break;
853    case 0x32: /* 3DSTATE_URB_DS */
854       if (num_entries)
855          assert(num_entries >= 138);
856       break;
857    case 0x33: /* 3DSTATE_URB_GS */
858       max_entries = (dev->gt == 2) ? 320 : 192;
859       if (num_entries > max_entries)
860          num_entries = max_entries;
861       break;
862    default:
863       break;
864    }
865
866    ilo_cp_begin(cp, cmd_len);
867    ilo_cp_write(cp, cmd | (cmd_len - 2));
868    ilo_cp_write(cp, offset << GEN7_URB_STARTING_ADDRESS_SHIFT |
869                     (alloc_size - 1) << GEN7_URB_ENTRY_SIZE_SHIFT |
870                     num_entries);
871    ilo_cp_end(cp);
872 }
873
874 static void
875 gen7_emit_3DSTATE_URB_VS(const struct ilo_dev_info *dev,
876                          int offset, int size, int entry_size,
877                          struct ilo_cp *cp)
878 {
879    gen7_emit_3dstate_urb(dev, 0x30, offset, size, entry_size, cp);
880 }
881
882 static void
883 gen7_emit_3DSTATE_URB_HS(const struct ilo_dev_info *dev,
884                          int offset, int size, int entry_size,
885                          struct ilo_cp *cp)
886 {
887    gen7_emit_3dstate_urb(dev, 0x31, offset, size, entry_size, cp);
888 }
889
890 static void
891 gen7_emit_3DSTATE_URB_DS(const struct ilo_dev_info *dev,
892                          int offset, int size, int entry_size,
893                          struct ilo_cp *cp)
894 {
895    gen7_emit_3dstate_urb(dev, 0x32, offset, size, entry_size, cp);
896 }
897
898 static void
899 gen7_emit_3DSTATE_URB_GS(const struct ilo_dev_info *dev,
900                          int offset, int size, int entry_size,
901                          struct ilo_cp *cp)
902 {
903    gen7_emit_3dstate_urb(dev, 0x33, offset, size, entry_size, cp);
904 }
905
906 static void
907 gen7_emit_3dstate_push_constant_alloc(const struct ilo_dev_info *dev,
908                                       int subop, int offset, int size,
909                                       struct ilo_cp *cp)
910 {
911    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x1, subop);
912    const uint8_t cmd_len = 2;
913    int end;
914
915    ILO_GPE_VALID_GEN(dev, 7, 7);
916
917    /* VS, HS, DS, GS, and PS variants */
918    assert(subop >= 0x12 && subop <= 0x16);
919
920    /*
921     * From the Ivy Bridge PRM, volume 2 part 1, page 68:
922     *
923     *     "(A table that says the maximum size of each constant buffer is
924     *      16KB")
925     *
926     * From the Ivy Bridge PRM, volume 2 part 1, page 115:
927     *
928     *     "The sum of the Constant Buffer Offset and the Constant Buffer Size
929     *      may not exceed the maximum value of the Constant Buffer Size."
930     *
931     * Thus, the valid range of buffer end is [0KB, 16KB].
932     */
933    end = (offset + size) / 1024;
934    if (end > 16) {
935       assert(!"invalid constant buffer end");
936       end = 16;
937    }
938
939    /* the valid range of buffer offset is [0KB, 15KB] */
940    offset = (offset + 1023) / 1024;
941    if (offset > 15) {
942       assert(!"invalid constant buffer offset");
943       offset = 15;
944    }
945
946    if (offset > end) {
947       assert(!size);
948       offset = end;
949    }
950
951    /* the valid range of buffer size is [0KB, 15KB] */
952    size = end - offset;
953    if (size > 15) {
954       assert(!"invalid constant buffer size");
955       size = 15;
956    }
957
958    ilo_cp_begin(cp, cmd_len);
959    ilo_cp_write(cp, cmd | (cmd_len - 2));
960    ilo_cp_write(cp, offset << GEN7_PUSH_CONSTANT_BUFFER_OFFSET_SHIFT |
961                     size);
962    ilo_cp_end(cp);
963 }
964
965 static void
966 gen7_emit_3DSTATE_PUSH_CONSTANT_ALLOC_VS(const struct ilo_dev_info *dev,
967                                          int offset, int size,
968                                          struct ilo_cp *cp)
969 {
970    gen7_emit_3dstate_push_constant_alloc(dev, 0x12, offset, size, cp);
971 }
972
973 static void
974 gen7_emit_3DSTATE_PUSH_CONSTANT_ALLOC_HS(const struct ilo_dev_info *dev,
975                                          int offset, int size,
976                                          struct ilo_cp *cp)
977 {
978    gen7_emit_3dstate_push_constant_alloc(dev, 0x13, offset, size, cp);
979 }
980
981 static void
982 gen7_emit_3DSTATE_PUSH_CONSTANT_ALLOC_DS(const struct ilo_dev_info *dev,
983                                          int offset, int size,
984                                          struct ilo_cp *cp)
985 {
986    gen7_emit_3dstate_push_constant_alloc(dev, 0x14, offset, size, cp);
987 }
988
989 static void
990 gen7_emit_3DSTATE_PUSH_CONSTANT_ALLOC_GS(const struct ilo_dev_info *dev,
991                                          int offset, int size,
992                                          struct ilo_cp *cp)
993 {
994    gen7_emit_3dstate_push_constant_alloc(dev, 0x15, offset, size, cp);
995 }
996
997 static void
998 gen7_emit_3DSTATE_PUSH_CONSTANT_ALLOC_PS(const struct ilo_dev_info *dev,
999                                          int offset, int size,
1000                                          struct ilo_cp *cp)
1001 {
1002    gen7_emit_3dstate_push_constant_alloc(dev, 0x16, offset, size, cp);
1003 }
1004
1005 static void
1006 gen7_emit_3DSTATE_SO_DECL_LIST(const struct ilo_dev_info *dev,
1007                                const struct pipe_stream_output_info *so_info,
1008                                const struct ilo_shader *sh,
1009                                struct ilo_cp *cp)
1010 {
1011    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x1, 0x17);
1012    uint16_t cmd_len;
1013    int buffer_selects, num_entries, i;
1014    uint16_t so_decls[128];
1015
1016    ILO_GPE_VALID_GEN(dev, 7, 7);
1017
1018    buffer_selects = 0;
1019    num_entries = 0;
1020
1021    if (so_info) {
1022       int buffer_offsets[PIPE_MAX_SO_BUFFERS];
1023
1024       memset(buffer_offsets, 0, sizeof(buffer_offsets));
1025
1026       for (i = 0; i < so_info->num_outputs; i++) {
1027          unsigned decl, buf, attr, mask;
1028
1029          buf = so_info->output[i].output_buffer;
1030
1031          /* pad with holes */
1032          assert(buffer_offsets[buf] <= so_info->output[i].dst_offset);
1033          while (buffer_offsets[buf] < so_info->output[i].dst_offset) {
1034             int num_dwords;
1035
1036             num_dwords = so_info->output[i].dst_offset - buffer_offsets[buf];
1037             if (num_dwords > 4)
1038                num_dwords = 4;
1039
1040             decl = buf << SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT |
1041                    SO_DECL_HOLE_FLAG |
1042                    ((1 << num_dwords) - 1) << SO_DECL_COMPONENT_MASK_SHIFT;
1043
1044             so_decls[num_entries++] = decl;
1045             buffer_offsets[buf] += num_dwords;
1046          }
1047
1048          /* figure out which attribute is sourced */
1049          for (attr = 0; attr < sh->out.count; attr++) {
1050             const int idx = sh->out.register_indices[attr];
1051             if (idx == so_info->output[i].register_index)
1052                break;
1053          }
1054
1055          decl = buf << SO_DECL_OUTPUT_BUFFER_SLOT_SHIFT;
1056
1057          if (attr < sh->out.count) {
1058             mask = ((1 << so_info->output[i].num_components) - 1) <<
1059                so_info->output[i].start_component;
1060
1061             /* PSIZE is at W channel */
1062             if (sh->out.semantic_names[attr] == TGSI_SEMANTIC_PSIZE) {
1063                assert(mask == 0x1);
1064                mask = (mask << 3) & 0xf;
1065             }
1066
1067             decl |= attr << SO_DECL_REGISTER_INDEX_SHIFT |
1068                     mask << SO_DECL_COMPONENT_MASK_SHIFT;
1069          }
1070          else {
1071             assert(!"stream output an undefined register");
1072             mask = (1 << so_info->output[i].num_components) - 1;
1073             decl |= SO_DECL_HOLE_FLAG |
1074                     mask << SO_DECL_COMPONENT_MASK_SHIFT;
1075          }
1076
1077          so_decls[num_entries++] = decl;
1078          buffer_selects |= 1 << buf;
1079          buffer_offsets[buf] += so_info->output[i].num_components;
1080       }
1081    }
1082
1083    /*
1084     * From the Ivy Bridge PRM, volume 2 part 1, page 201:
1085     *
1086     *     "Errata: All 128 decls for all four streams must be included
1087     *      whenever this command is issued. The "Num Entries [n]" fields still
1088     *      contain the actual numbers of valid decls."
1089     *
1090     * Also note that "DWord Length" has 9 bits for this command, and the type
1091     * of cmd_len is thus uint16_t.
1092     */
1093    cmd_len = 2 * 128 + 3;
1094
1095    ilo_cp_begin(cp, cmd_len);
1096    ilo_cp_write(cp, cmd | (cmd_len - 2));
1097    ilo_cp_write(cp, 0 << SO_STREAM_TO_BUFFER_SELECTS_3_SHIFT |
1098                     0 << SO_STREAM_TO_BUFFER_SELECTS_2_SHIFT |
1099                     0 << SO_STREAM_TO_BUFFER_SELECTS_1_SHIFT |
1100                     buffer_selects << SO_STREAM_TO_BUFFER_SELECTS_0_SHIFT);
1101    ilo_cp_write(cp, 0 << SO_NUM_ENTRIES_3_SHIFT |
1102                     0 << SO_NUM_ENTRIES_2_SHIFT |
1103                     0 << SO_NUM_ENTRIES_1_SHIFT |
1104                     num_entries << SO_NUM_ENTRIES_0_SHIFT);
1105
1106    for (i = 0; i < num_entries; i++) {
1107       ilo_cp_write(cp, so_decls[i]);
1108       ilo_cp_write(cp, 0);
1109    }
1110    for (; i < 128; i++) {
1111       ilo_cp_write(cp, 0);
1112       ilo_cp_write(cp, 0);
1113    }
1114
1115    ilo_cp_end(cp);
1116 }
1117
1118 static void
1119 gen7_emit_3DSTATE_SO_BUFFER(const struct ilo_dev_info *dev,
1120                             int index, int base, int stride,
1121                             const struct pipe_stream_output_target *so_target,
1122                             struct ilo_cp *cp)
1123 {
1124    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x1, 0x18);
1125    const uint8_t cmd_len = 4;
1126    struct ilo_buffer *buf;
1127    int end;
1128
1129    ILO_GPE_VALID_GEN(dev, 7, 7);
1130
1131    if (!so_target || !so_target->buffer) {
1132       ilo_cp_begin(cp, cmd_len);
1133       ilo_cp_write(cp, cmd | (cmd_len - 2));
1134       ilo_cp_write(cp, index << SO_BUFFER_INDEX_SHIFT);
1135       ilo_cp_write(cp, 0);
1136       ilo_cp_write(cp, 0);
1137       ilo_cp_end(cp);
1138       return;
1139    }
1140
1141    buf = ilo_buffer(so_target->buffer);
1142
1143    /* DWord-aligned */
1144    assert(stride % 4 == 0 && base % 4 == 0);
1145    assert(so_target->buffer_offset % 4 == 0);
1146
1147    stride &= ~3;
1148    base = (base + so_target->buffer_offset) & ~3;
1149    end = (base + so_target->buffer_size) & ~3;
1150
1151    ilo_cp_begin(cp, cmd_len);
1152    ilo_cp_write(cp, cmd | (cmd_len - 2));
1153    ilo_cp_write(cp, index << SO_BUFFER_INDEX_SHIFT |
1154                     stride);
1155    ilo_cp_write_bo(cp, base, buf->bo, INTEL_DOMAIN_RENDER, INTEL_DOMAIN_RENDER);
1156    ilo_cp_write_bo(cp, end, buf->bo, INTEL_DOMAIN_RENDER, INTEL_DOMAIN_RENDER);
1157    ilo_cp_end(cp);
1158 }
1159
1160 static void
1161 gen7_emit_3DPRIMITIVE(const struct ilo_dev_info *dev,
1162                       const struct pipe_draw_info *info,
1163                       bool rectlist,
1164                       struct ilo_cp *cp)
1165 {
1166    const uint32_t cmd = ILO_GPE_CMD(0x3, 0x3, 0x00);
1167    const uint8_t cmd_len = 7;
1168    const int prim = (rectlist) ?
1169       _3DPRIM_RECTLIST : ilo_gpe_gen6_translate_pipe_prim(info->mode);
1170    const int vb_access = (info->indexed) ?
1171       GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM :
1172       GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
1173
1174    ILO_GPE_VALID_GEN(dev, 7, 7);
1175
1176    ilo_cp_begin(cp, cmd_len);
1177    ilo_cp_write(cp, cmd | (cmd_len - 2));
1178    ilo_cp_write(cp, vb_access | prim);
1179    ilo_cp_write(cp, info->count);
1180    ilo_cp_write(cp, info->start);
1181    ilo_cp_write(cp, info->instance_count);
1182    ilo_cp_write(cp, info->start_instance);
1183    ilo_cp_write(cp, info->index_bias);
1184    ilo_cp_end(cp);
1185 }
1186
1187 static uint32_t
1188 gen7_emit_SF_CLIP_VIEWPORT(const struct ilo_dev_info *dev,
1189                            const struct ilo_viewport_cso *viewports,
1190                            unsigned num_viewports,
1191                            struct ilo_cp *cp)
1192 {
1193    const int state_align = 64 / 4;
1194    const int state_len = 16 * num_viewports;
1195    uint32_t state_offset, *dw;
1196    unsigned i;
1197
1198    ILO_GPE_VALID_GEN(dev, 7, 7);
1199
1200    /*
1201     * From the Ivy Bridge PRM, volume 2 part 1, page 270:
1202     *
1203     *     "The viewport-specific state used by both the SF and CL units
1204     *      (SF_CLIP_VIEWPORT) is stored as an array of up to 16 elements, each
1205     *      of which contains the DWords described below. The start of each
1206     *      element is spaced 16 DWords apart. The location of first element of
1207     *      the array, as specified by both Pointer to SF_VIEWPORT and Pointer
1208     *      to CLIP_VIEWPORT, is aligned to a 64-byte boundary."
1209     */
1210    assert(num_viewports && num_viewports <= 16);
1211
1212    dw = ilo_cp_steal_ptr(cp, "SF_CLIP_VIEWPORT",
1213          state_len, state_align, &state_offset);
1214
1215    for (i = 0; i < num_viewports; i++) {
1216       const struct ilo_viewport_cso *vp = &viewports[i];
1217
1218       dw[0] = fui(vp->m00);
1219       dw[1] = fui(vp->m11);
1220       dw[2] = fui(vp->m22);
1221       dw[3] = fui(vp->m30);
1222       dw[4] = fui(vp->m31);
1223       dw[5] = fui(vp->m32);
1224       dw[6] = 0;
1225       dw[7] = 0;
1226       dw[8] = fui(vp->min_gbx);
1227       dw[9] = fui(vp->max_gbx);
1228       dw[10] = fui(vp->min_gby);
1229       dw[11] = fui(vp->max_gby);
1230       dw[12] = 0;
1231       dw[13] = 0;
1232       dw[14] = 0;
1233       dw[15] = 0;
1234
1235       dw += 16;
1236    }
1237
1238    return state_offset;
1239 }
1240
1241 void
1242 ilo_gpe_init_view_surface_null_gen7(const struct ilo_dev_info *dev,
1243                                     unsigned width, unsigned height,
1244                                     unsigned depth, unsigned level,
1245                                     struct ilo_view_surface *surf)
1246 {
1247    uint32_t *dw;
1248
1249    ILO_GPE_VALID_GEN(dev, 7, 7);
1250
1251    /*
1252     * From the Ivy Bridge PRM, volume 4 part 1, page 62:
1253     *
1254     *     "A null surface is used in instances where an actual surface is not
1255     *      bound. When a write message is generated to a null surface, no
1256     *      actual surface is written to. When a read message (including any
1257     *      sampling engine message) is generated to a null surface, the result
1258     *      is all zeros.  Note that a null surface type is allowed to be used
1259     *      with all messages, even if it is not specificially indicated as
1260     *      supported. All of the remaining fields in surface state are ignored
1261     *      for null surfaces, with the following exceptions:
1262     *
1263     *      * Width, Height, Depth, LOD, and Render Target View Extent fields
1264     *        must match the depth buffer's corresponding state for all render
1265     *        target surfaces, including null.
1266     *      * All sampling engine and data port messages support null surfaces
1267     *        with the above behavior, even if not mentioned as specifically
1268     *        supported, except for the following:
1269     *        * Data Port Media Block Read/Write messages.
1270     *      * The Surface Type of a surface used as a render target (accessed
1271     *        via the Data Port's Render Target Write message) must be the same
1272     *        as the Surface Type of all other render targets and of the depth
1273     *        buffer (defined in 3DSTATE_DEPTH_BUFFER), unless either the depth
1274     *        buffer or render targets are SURFTYPE_NULL."
1275     *
1276     * From the Ivy Bridge PRM, volume 4 part 1, page 65:
1277     *
1278     *     "If Surface Type is SURFTYPE_NULL, this field (Tiled Surface) must be
1279     *      true"
1280     */
1281
1282    STATIC_ASSERT(Elements(surf->payload) >= 8);
1283    dw = surf->payload;
1284
1285    dw[0] = BRW_SURFACE_NULL << BRW_SURFACE_TYPE_SHIFT |
1286            BRW_SURFACEFORMAT_B8G8R8A8_UNORM << BRW_SURFACE_FORMAT_SHIFT |
1287            BRW_SURFACE_TILED << 13;
1288
1289    dw[1] = 0;
1290
1291    dw[2] = SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT) |
1292            SET_FIELD(width  - 1, GEN7_SURFACE_WIDTH);
1293
1294    dw[3] = SET_FIELD(depth - 1, BRW_SURFACE_DEPTH);
1295
1296    dw[4] = 0;
1297    dw[5] = level;
1298
1299    dw[6] = 0;
1300    dw[7] = 0;
1301
1302    surf->bo = NULL;
1303 }
1304
1305 void
1306 ilo_gpe_init_view_surface_for_buffer_gen7(const struct ilo_dev_info *dev,
1307                                           const struct ilo_buffer *buf,
1308                                           unsigned offset, unsigned size,
1309                                           unsigned struct_size,
1310                                           enum pipe_format elem_format,
1311                                           bool is_rt, bool render_cache_rw,
1312                                           struct ilo_view_surface *surf)
1313 {
1314    const bool typed = (elem_format != PIPE_FORMAT_NONE);
1315    const bool structured = (!typed && struct_size > 1);
1316    const int elem_size = (typed) ?
1317       util_format_get_blocksize(elem_format) : 1;
1318    int width, height, depth, pitch;
1319    int surface_type, surface_format, num_entries;
1320    uint32_t *dw;
1321
1322    ILO_GPE_VALID_GEN(dev, 7, 7);
1323
1324    surface_type = (structured) ? 5 : BRW_SURFACE_BUFFER;
1325
1326    surface_format = (typed) ?
1327       ilo_translate_color_format(elem_format) : BRW_SURFACEFORMAT_RAW;
1328
1329    num_entries = size / struct_size;
1330    /* see if there is enough space to fit another element */
1331    if (size % struct_size >= elem_size && !structured)
1332       num_entries++;
1333
1334    /*
1335     * From the Ivy Bridge PRM, volume 4 part 1, page 67:
1336     *
1337     *     "For SURFTYPE_BUFFER render targets, this field (Surface Base
1338     *      Address) specifies the base address of first element of the
1339     *      surface. The surface is interpreted as a simple array of that
1340     *      single element type. The address must be naturally-aligned to the
1341     *      element size (e.g., a buffer containing R32G32B32A32_FLOAT elements
1342     *      must be 16-byte aligned)
1343     *
1344     *      For SURFTYPE_BUFFER non-rendertarget surfaces, this field specifies
1345     *      the base address of the first element of the surface, computed in
1346     *      software by adding the surface base address to the byte offset of
1347     *      the element in the buffer."
1348     */
1349    if (is_rt)
1350       assert(offset % elem_size == 0);
1351
1352    /*
1353     * From the Ivy Bridge PRM, volume 4 part 1, page 68:
1354     *
1355     *     "For typed buffer and structured buffer surfaces, the number of
1356     *      entries in the buffer ranges from 1 to 2^27.  For raw buffer
1357     *      surfaces, the number of entries in the buffer is the number of
1358     *      bytes which can range from 1 to 2^30."
1359     */
1360    assert(num_entries >= 1 &&
1361           num_entries <= 1 << ((typed || structured) ? 27 : 30));
1362
1363    /*
1364     * From the Ivy Bridge PRM, volume 4 part 1, page 69:
1365     *
1366     *     "For SURFTYPE_BUFFER: The low two bits of this field (Width) must be
1367     *      11 if the Surface Format is RAW (the size of the buffer must be a
1368     *      multiple of 4 bytes)."
1369     *
1370     * From the Ivy Bridge PRM, volume 4 part 1, page 70:
1371     *
1372     *     "For surfaces of type SURFTYPE_BUFFER and SURFTYPE_STRBUF, this
1373     *      field (Surface Pitch) indicates the size of the structure."
1374     *
1375     *     "For linear surfaces with Surface Type of SURFTYPE_STRBUF, the pitch
1376     *      must be a multiple of 4 bytes."
1377     */
1378    if (structured)
1379       assert(struct_size % 4 == 0);
1380    else if (!typed)
1381       assert(num_entries % 4 == 0);
1382
1383    pitch = struct_size;
1384
1385    pitch--;
1386    num_entries--;
1387    /* bits [6:0] */
1388    width  = (num_entries & 0x0000007f);
1389    /* bits [20:7] */
1390    height = (num_entries & 0x001fff80) >> 7;
1391    /* bits [30:21] */
1392    depth  = (num_entries & 0x7fe00000) >> 21;
1393    /* limit to [26:21] */
1394    if (typed || structured)
1395       depth &= 0x3f;
1396
1397    STATIC_ASSERT(Elements(surf->payload) >= 8);
1398    dw = surf->payload;
1399
1400    dw[0] = surface_type << BRW_SURFACE_TYPE_SHIFT |
1401            surface_format << BRW_SURFACE_FORMAT_SHIFT;
1402    if (render_cache_rw)
1403       dw[0] |= BRW_SURFACE_RC_READ_WRITE;
1404
1405    dw[1] = offset;
1406
1407    dw[2] = SET_FIELD(height, GEN7_SURFACE_HEIGHT) |
1408            SET_FIELD(width, GEN7_SURFACE_WIDTH);
1409
1410    dw[3] = SET_FIELD(depth, BRW_SURFACE_DEPTH) |
1411            pitch;
1412
1413    dw[4] = 0;
1414    dw[5] = 0;
1415
1416    dw[6] = 0;
1417    dw[7] = 0;
1418
1419    /* do not increment reference count */
1420    surf->bo = buf->bo;
1421 }
1422
1423 void
1424 ilo_gpe_init_view_surface_for_texture_gen7(const struct ilo_dev_info *dev,
1425                                            const struct ilo_texture *tex,
1426                                            enum pipe_format format,
1427                                            unsigned first_level,
1428                                            unsigned num_levels,
1429                                            unsigned first_layer,
1430                                            unsigned num_layers,
1431                                            bool is_rt, bool render_cache_rw,
1432                                            struct ilo_view_surface *surf)
1433 {
1434    int surface_type, surface_format;
1435    int width, height, depth, pitch, lod;
1436    unsigned layer_offset, x_offset, y_offset;
1437    uint32_t *dw;
1438
1439    ILO_GPE_VALID_GEN(dev, 7, 7);
1440
1441    surface_type = ilo_gpe_gen6_translate_texture(tex->base.target);
1442    assert(surface_type != BRW_SURFACE_BUFFER);
1443
1444    if (format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT && tex->separate_s8)
1445       format = PIPE_FORMAT_Z32_FLOAT;
1446
1447    if (is_rt)
1448       surface_format = ilo_translate_render_format(format);
1449    else
1450       surface_format = ilo_translate_texture_format(format);
1451    assert(surface_format >= 0);
1452
1453    width = tex->base.width0;
1454    height = tex->base.height0;
1455    depth = (tex->base.target == PIPE_TEXTURE_3D) ?
1456       tex->base.depth0 : num_layers;
1457    pitch = tex->bo_stride;
1458
1459    if (surface_type == BRW_SURFACE_CUBE) {
1460       /*
1461        * From the Ivy Bridge PRM, volume 4 part 1, page 70:
1462        *
1463        *     "For SURFTYPE_CUBE:For Sampling Engine Surfaces, the range of
1464        *      this field is [0,340], indicating the number of cube array
1465        *      elements (equal to the number of underlying 2D array elements
1466        *      divided by 6). For other surfaces, this field must be zero."
1467        *
1468        * When is_rt is true, we treat the texture as a 2D one to avoid the
1469        * restriction.
1470        */
1471       if (is_rt) {
1472          surface_type = BRW_SURFACE_2D;
1473       }
1474       else {
1475          assert(num_layers % 6 == 0);
1476          depth = num_layers / 6;
1477       }
1478    }
1479
1480    /* sanity check the size */
1481    assert(width >= 1 && height >= 1 && depth >= 1 && pitch >= 1);
1482    assert(first_layer < 2048 && num_layers <= 2048);
1483    switch (surface_type) {
1484    case BRW_SURFACE_1D:
1485       assert(width <= 16384 && height == 1 && depth <= 2048);
1486       break;
1487    case BRW_SURFACE_2D:
1488       assert(width <= 16384 && height <= 16384 && depth <= 2048);
1489       break;
1490    case BRW_SURFACE_3D:
1491       assert(width <= 2048 && height <= 2048 && depth <= 2048);
1492       if (!is_rt)
1493          assert(first_layer == 0);
1494       break;
1495    case BRW_SURFACE_CUBE:
1496       assert(width <= 16384 && height <= 16384 && depth <= 86);
1497       assert(width == height);
1498       if (is_rt)
1499          assert(first_layer == 0);
1500       break;
1501    default:
1502       assert(!"unexpected surface type");
1503       break;
1504    }
1505
1506    if (is_rt) {
1507       /*
1508        * Compute the offset to the layer manually.
1509        *
1510        * For rendering, the hardware requires LOD to be the same for all
1511        * render targets and the depth buffer.  We need to compute the offset
1512        * to the layer manually and always set LOD to 0.
1513        */
1514       if (true) {
1515          /* we lose the capability for layered rendering */
1516          assert(num_layers == 1);
1517
1518          layer_offset = ilo_texture_get_slice_offset(tex,
1519                first_level, first_layer, &x_offset, &y_offset);
1520
1521          assert(x_offset % 4 == 0);
1522          assert(y_offset % 2 == 0);
1523          x_offset /= 4;
1524          y_offset /= 2;
1525
1526          /* derive the size for the LOD */
1527          width = u_minify(width, first_level);
1528          height = u_minify(height, first_level);
1529          if (surface_type == BRW_SURFACE_3D)
1530             depth = u_minify(depth, first_level);
1531          else
1532             depth = 1;
1533
1534          first_level = 0;
1535          first_layer = 0;
1536          lod = 0;
1537       }
1538       else {
1539          layer_offset = 0;
1540          x_offset = 0;
1541          y_offset = 0;
1542       }
1543
1544       assert(num_levels == 1);
1545       lod = first_level;
1546    }
1547    else {
1548       layer_offset = 0;
1549       x_offset = 0;
1550       y_offset = 0;
1551
1552       lod = num_levels - 1;
1553    }
1554
1555    /*
1556     * From the Ivy Bridge PRM, volume 4 part 1, page 68:
1557     *
1558     *     "The Base Address for linear render target surfaces and surfaces
1559     *      accessed with the typed surface read/write data port messages must
1560     *      be element-size aligned, for non-YUV surface formats, or a multiple
1561     *      of 2 element-sizes for YUV surface formats.  Other linear surfaces
1562     *      have no alignment requirements (byte alignment is sufficient)."
1563     *
1564     * From the Ivy Bridge PRM, volume 4 part 1, page 70:
1565     *
1566     *     "For linear render target surfaces and surfaces accessed with the
1567     *      typed data port messages, the pitch must be a multiple of the
1568     *      element size for non-YUV surface formats. Pitch must be a multiple
1569     *      of 2 * element size for YUV surface formats. For linear surfaces
1570     *      with Surface Type of SURFTYPE_STRBUF, the pitch must be a multiple
1571     *      of 4 bytes.For other linear surfaces, the pitch can be any multiple
1572     *      of bytes."
1573     *
1574     * From the Ivy Bridge PRM, volume 4 part 1, page 74:
1575     *
1576     *     "For linear surfaces, this field (X Offset) must be zero."
1577     */
1578    if (tex->tiling == INTEL_TILING_NONE) {
1579       if (is_rt) {
1580          const int elem_size = util_format_get_blocksize(format);
1581          assert(layer_offset % elem_size == 0);
1582          assert(pitch % elem_size == 0);
1583       }
1584
1585       assert(!x_offset);
1586    }
1587
1588    STATIC_ASSERT(Elements(surf->payload) >= 8);
1589    dw = surf->payload;
1590
1591    dw[0] = surface_type << BRW_SURFACE_TYPE_SHIFT |
1592            surface_format << BRW_SURFACE_FORMAT_SHIFT |
1593            ilo_gpe_gen6_translate_winsys_tiling(tex->tiling) << 13;
1594
1595    /*
1596     * From the Ivy Bridge PRM, volume 4 part 1, page 63:
1597     *
1598     *     "If this field (Surface Array) is enabled, the Surface Type must be
1599     *      SURFTYPE_1D, SURFTYPE_2D, or SURFTYPE_CUBE. If this field is
1600     *      disabled and Surface Type is SURFTYPE_1D, SURFTYPE_2D, or
1601     *      SURFTYPE_CUBE, the Depth field must be set to zero."
1602     *
1603     * For non-3D sampler surfaces, resinfo (the sampler message) always
1604     * returns zero for the number of layers when this field is not set.
1605     */
1606    if (surface_type != BRW_SURFACE_3D) {
1607       if (util_resource_is_array_texture(&tex->base))
1608          dw[0] |= GEN7_SURFACE_IS_ARRAY;
1609       else
1610          assert(depth == 1);
1611    }
1612
1613    if (tex->valign_4)
1614       dw[0] |= GEN7_SURFACE_VALIGN_4;
1615
1616    if (tex->halign_8)
1617       dw[0] |= GEN7_SURFACE_HALIGN_8;
1618
1619    if (tex->array_spacing_full)
1620       dw[0] |= GEN7_SURFACE_ARYSPC_FULL;
1621    else
1622       dw[0] |= GEN7_SURFACE_ARYSPC_LOD0;
1623
1624    if (render_cache_rw)
1625       dw[0] |= BRW_SURFACE_RC_READ_WRITE;
1626
1627    if (surface_type == BRW_SURFACE_CUBE && !is_rt)
1628       dw[0] |= BRW_SURFACE_CUBEFACE_ENABLES;
1629
1630    dw[1] = layer_offset;
1631
1632    dw[2] = SET_FIELD(height - 1, GEN7_SURFACE_HEIGHT) |
1633            SET_FIELD(width - 1, GEN7_SURFACE_WIDTH);
1634
1635    dw[3] = SET_FIELD(depth - 1, BRW_SURFACE_DEPTH) |
1636            (pitch - 1);
1637
1638    dw[4] = first_layer << 18 |
1639            (num_layers - 1) << 7;
1640
1641    /*
1642     * MSFMT_MSS means the samples are not interleaved and MSFMT_DEPTH_STENCIL
1643     * means the samples are interleaved.  The layouts are the same when the
1644     * number of samples is 1.
1645     */
1646    if (tex->interleaved && tex->base.nr_samples > 1) {
1647       assert(!is_rt);
1648       dw[4] |= GEN7_SURFACE_MSFMT_DEPTH_STENCIL;
1649    }
1650    else {
1651       dw[4] |= GEN7_SURFACE_MSFMT_MSS;
1652    }
1653
1654    if (tex->base.nr_samples > 4)
1655       dw[4] |= GEN7_SURFACE_MULTISAMPLECOUNT_8;
1656    else if (tex->base.nr_samples > 2)
1657       dw[4] |= GEN7_SURFACE_MULTISAMPLECOUNT_4;
1658    else
1659       dw[4] |= GEN7_SURFACE_MULTISAMPLECOUNT_1;
1660
1661    dw[5] = x_offset << BRW_SURFACE_X_OFFSET_SHIFT |
1662            y_offset << BRW_SURFACE_Y_OFFSET_SHIFT |
1663            SET_FIELD(first_level, GEN7_SURFACE_MIN_LOD) |
1664            lod;
1665
1666    dw[6] = 0;
1667    dw[7] = 0;
1668
1669    /* do not increment reference count */
1670    surf->bo = tex->bo;
1671 }
1672
1673 static int
1674 gen7_estimate_command_size(const struct ilo_dev_info *dev,
1675                            enum ilo_gpe_gen7_command cmd,
1676                            int arg)
1677 {
1678    static const struct {
1679       int header;
1680       int body;
1681    } gen7_command_size_table[ILO_GPE_GEN7_COMMAND_COUNT] = {
1682       [ILO_GPE_GEN7_STATE_BASE_ADDRESS]                       = { 0,  10 },
1683       [ILO_GPE_GEN7_STATE_SIP]                                = { 0,  2  },
1684       [ILO_GPE_GEN7_3DSTATE_VF_STATISTICS]                    = { 0,  1  },
1685       [ILO_GPE_GEN7_PIPELINE_SELECT]                          = { 0,  1  },
1686       [ILO_GPE_GEN7_MEDIA_VFE_STATE]                          = { 0,  8  },
1687       [ILO_GPE_GEN7_MEDIA_CURBE_LOAD]                         = { 0,  4  },
1688       [ILO_GPE_GEN7_MEDIA_INTERFACE_DESCRIPTOR_LOAD]          = { 0,  4  },
1689       [ILO_GPE_GEN7_MEDIA_STATE_FLUSH]                        = { 0,  2  },
1690       [ILO_GPE_GEN7_GPGPU_WALKER]                             = { 0,  11 },
1691       [ILO_GPE_GEN7_3DSTATE_CLEAR_PARAMS]                     = { 0,  3  },
1692       [ILO_GPE_GEN7_3DSTATE_DEPTH_BUFFER]                     = { 0,  7  },
1693       [ILO_GPE_GEN7_3DSTATE_STENCIL_BUFFER]                   = { 0,  3  },
1694       [ILO_GPE_GEN7_3DSTATE_HIER_DEPTH_BUFFER]                = { 0,  3  },
1695       [ILO_GPE_GEN7_3DSTATE_VERTEX_BUFFERS]                   = { 1,  4  },
1696       [ILO_GPE_GEN7_3DSTATE_VERTEX_ELEMENTS]                  = { 1,  2  },
1697       [ILO_GPE_GEN7_3DSTATE_INDEX_BUFFER]                     = { 0,  3  },
1698       [ILO_GPE_GEN7_3DSTATE_CC_STATE_POINTERS]                = { 0,  2  },
1699       [ILO_GPE_GEN7_3DSTATE_SCISSOR_STATE_POINTERS]           = { 0,  2  },
1700       [ILO_GPE_GEN7_3DSTATE_VS]                               = { 0,  6  },
1701       [ILO_GPE_GEN7_3DSTATE_GS]                               = { 0,  7  },
1702       [ILO_GPE_GEN7_3DSTATE_CLIP]                             = { 0,  4  },
1703       [ILO_GPE_GEN7_3DSTATE_SF]                               = { 0,  7  },
1704       [ILO_GPE_GEN7_3DSTATE_WM]                               = { 0,  3  },
1705       [ILO_GPE_GEN7_3DSTATE_CONSTANT_VS]                      = { 0,  7  },
1706       [ILO_GPE_GEN7_3DSTATE_CONSTANT_GS]                      = { 0,  7  },
1707       [ILO_GPE_GEN7_3DSTATE_CONSTANT_PS]                      = { 0,  7  },
1708       [ILO_GPE_GEN7_3DSTATE_SAMPLE_MASK]                      = { 0,  2  },
1709       [ILO_GPE_GEN7_3DSTATE_CONSTANT_HS]                      = { 0,  7  },
1710       [ILO_GPE_GEN7_3DSTATE_CONSTANT_DS]                      = { 0,  7  },
1711       [ILO_GPE_GEN7_3DSTATE_HS]                               = { 0,  7  },
1712       [ILO_GPE_GEN7_3DSTATE_TE]                               = { 0,  4  },
1713       [ILO_GPE_GEN7_3DSTATE_DS]                               = { 0,  6  },
1714       [ILO_GPE_GEN7_3DSTATE_STREAMOUT]                        = { 0,  3  },
1715       [ILO_GPE_GEN7_3DSTATE_SBE]                              = { 0,  14 },
1716       [ILO_GPE_GEN7_3DSTATE_PS]                               = { 0,  8  },
1717       [ILO_GPE_GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP]  = { 0,  2  },
1718       [ILO_GPE_GEN7_3DSTATE_VIEWPORT_STATE_POINTERS_CC]       = { 0,  2  },
1719       [ILO_GPE_GEN7_3DSTATE_BLEND_STATE_POINTERS]             = { 0,  2  },
1720       [ILO_GPE_GEN7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS]     = { 0,  2  },
1721       [ILO_GPE_GEN7_3DSTATE_BINDING_TABLE_POINTERS_VS]        = { 0,  2  },
1722       [ILO_GPE_GEN7_3DSTATE_BINDING_TABLE_POINTERS_HS]        = { 0,  2  },
1723       [ILO_GPE_GEN7_3DSTATE_BINDING_TABLE_POINTERS_DS]        = { 0,  2  },
1724       [ILO_GPE_GEN7_3DSTATE_BINDING_TABLE_POINTERS_GS]        = { 0,  2  },
1725       [ILO_GPE_GEN7_3DSTATE_BINDING_TABLE_POINTERS_PS]        = { 0,  2  },
1726       [ILO_GPE_GEN7_3DSTATE_SAMPLER_STATE_POINTERS_VS]        = { 0,  2  },
1727       [ILO_GPE_GEN7_3DSTATE_SAMPLER_STATE_POINTERS_HS]        = { 0,  2  },
1728       [ILO_GPE_GEN7_3DSTATE_SAMPLER_STATE_POINTERS_DS]        = { 0,  2  },
1729       [ILO_GPE_GEN7_3DSTATE_SAMPLER_STATE_POINTERS_GS]        = { 0,  2  },
1730       [ILO_GPE_GEN7_3DSTATE_SAMPLER_STATE_POINTERS_PS]        = { 0,  2  },
1731       [ILO_GPE_GEN7_3DSTATE_URB_VS]                           = { 0,  2  },
1732       [ILO_GPE_GEN7_3DSTATE_URB_HS]                           = { 0,  2  },
1733       [ILO_GPE_GEN7_3DSTATE_URB_DS]                           = { 0,  2  },
1734       [ILO_GPE_GEN7_3DSTATE_URB_GS]                           = { 0,  2  },
1735       [ILO_GPE_GEN7_3DSTATE_DRAWING_RECTANGLE]                = { 0,  4  },
1736       [ILO_GPE_GEN7_3DSTATE_POLY_STIPPLE_OFFSET]              = { 0,  2  },
1737       [ILO_GPE_GEN7_3DSTATE_POLY_STIPPLE_PATTERN]             = { 0,  33, },
1738       [ILO_GPE_GEN7_3DSTATE_LINE_STIPPLE]                     = { 0,  3  },
1739       [ILO_GPE_GEN7_3DSTATE_AA_LINE_PARAMETERS]               = { 0,  3  },
1740       [ILO_GPE_GEN7_3DSTATE_MULTISAMPLE]                      = { 0,  4  },
1741       [ILO_GPE_GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_VS]           = { 0,  2  },
1742       [ILO_GPE_GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_HS]           = { 0,  2  },
1743       [ILO_GPE_GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_DS]           = { 0,  2  },
1744       [ILO_GPE_GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_GS]           = { 0,  2  },
1745       [ILO_GPE_GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_PS]           = { 0,  2  },
1746       [ILO_GPE_GEN7_3DSTATE_SO_DECL_LIST]                     = { 3,  2  },
1747       [ILO_GPE_GEN7_3DSTATE_SO_BUFFER]                        = { 0,  4  },
1748       [ILO_GPE_GEN7_PIPE_CONTROL]                             = { 0,  5  },
1749       [ILO_GPE_GEN7_3DPRIMITIVE]                              = { 0,  7  },
1750    };
1751    const int header = gen7_command_size_table[cmd].header;
1752    const int body = gen7_command_size_table[cmd].body;
1753    const int count = arg;
1754
1755    ILO_GPE_VALID_GEN(dev, 7, 7);
1756    assert(cmd < ILO_GPE_GEN7_COMMAND_COUNT);
1757
1758    return (likely(count)) ? header + body * count : 0;
1759 }
1760
1761 static int
1762 gen7_estimate_state_size(const struct ilo_dev_info *dev,
1763                          enum ilo_gpe_gen7_state state,
1764                          int arg)
1765 {
1766    static const struct {
1767       int alignment;
1768       int body;
1769       bool is_array;
1770    } gen7_state_size_table[ILO_GPE_GEN7_STATE_COUNT] = {
1771       [ILO_GPE_GEN7_INTERFACE_DESCRIPTOR_DATA]          = { 8,  8,  true },
1772       [ILO_GPE_GEN7_SF_CLIP_VIEWPORT]                   = { 16, 16, true },
1773       [ILO_GPE_GEN7_CC_VIEWPORT]                        = { 8,  2,  true },
1774       [ILO_GPE_GEN7_COLOR_CALC_STATE]                   = { 16, 6,  false },
1775       [ILO_GPE_GEN7_BLEND_STATE]                        = { 16, 2,  true },
1776       [ILO_GPE_GEN7_DEPTH_STENCIL_STATE]                = { 16, 3,  false },
1777       [ILO_GPE_GEN7_SCISSOR_RECT]                       = { 8,  2,  true },
1778       [ILO_GPE_GEN7_BINDING_TABLE_STATE]                = { 8,  1,  true },
1779       [ILO_GPE_GEN7_SURFACE_STATE]                      = { 8,  8,  false },
1780       [ILO_GPE_GEN7_SAMPLER_STATE]                      = { 8,  4,  true },
1781       [ILO_GPE_GEN7_SAMPLER_BORDER_COLOR_STATE]         = { 8,  4,  false },
1782       [ILO_GPE_GEN7_PUSH_CONSTANT_BUFFER]               = { 8,  1,  true },
1783    };
1784    const int alignment = gen7_state_size_table[state].alignment;
1785    const int body = gen7_state_size_table[state].body;
1786    const bool is_array = gen7_state_size_table[state].is_array;
1787    const int count = arg;
1788    int estimate;
1789
1790    ILO_GPE_VALID_GEN(dev, 7, 7);
1791    assert(state < ILO_GPE_GEN7_STATE_COUNT);
1792
1793    if (likely(count)) {
1794       if (is_array) {
1795          estimate = (alignment - 1) + body * count;
1796       }
1797       else {
1798          estimate = (alignment - 1) + body;
1799          /* all states are aligned */
1800          if (count > 1)
1801             estimate += util_align_npot(body, alignment) * (count - 1);
1802       }
1803    }
1804    else {
1805       estimate = 0;
1806    }
1807
1808    return estimate;
1809 }
1810
1811 static void
1812 gen7_init(struct ilo_gpe_gen7 *gen7)
1813 {
1814    const struct ilo_gpe_gen6 *gen6 = ilo_gpe_gen6_get();
1815
1816    gen7->estimate_command_size = gen7_estimate_command_size;
1817    gen7->estimate_state_size = gen7_estimate_state_size;
1818
1819 #define GEN7_USE(gen7, name, from) gen7->emit_ ## name = from->emit_ ## name
1820 #define GEN7_SET(gen7, name)       gen7->emit_ ## name = gen7_emit_ ## name
1821    GEN7_USE(gen7, STATE_BASE_ADDRESS, gen6);
1822    GEN7_USE(gen7, STATE_SIP, gen6);
1823    GEN7_USE(gen7, 3DSTATE_VF_STATISTICS, gen6);
1824    GEN7_USE(gen7, PIPELINE_SELECT, gen6);
1825    GEN7_USE(gen7, MEDIA_VFE_STATE, gen6);
1826    GEN7_USE(gen7, MEDIA_CURBE_LOAD, gen6);
1827    GEN7_USE(gen7, MEDIA_INTERFACE_DESCRIPTOR_LOAD, gen6);
1828    GEN7_USE(gen7, MEDIA_STATE_FLUSH, gen6);
1829    GEN7_SET(gen7, GPGPU_WALKER);
1830    GEN7_SET(gen7, 3DSTATE_CLEAR_PARAMS);
1831    GEN7_USE(gen7, 3DSTATE_DEPTH_BUFFER, gen6);
1832    GEN7_USE(gen7, 3DSTATE_STENCIL_BUFFER, gen6);
1833    GEN7_USE(gen7, 3DSTATE_HIER_DEPTH_BUFFER, gen6);
1834    GEN7_USE(gen7, 3DSTATE_VERTEX_BUFFERS, gen6);
1835    GEN7_USE(gen7, 3DSTATE_VERTEX_ELEMENTS, gen6);
1836    GEN7_USE(gen7, 3DSTATE_INDEX_BUFFER, gen6);
1837    GEN7_SET(gen7, 3DSTATE_CC_STATE_POINTERS);
1838    GEN7_USE(gen7, 3DSTATE_SCISSOR_STATE_POINTERS, gen6);
1839    GEN7_USE(gen7, 3DSTATE_VS, gen6);
1840    GEN7_SET(gen7, 3DSTATE_GS);
1841    GEN7_USE(gen7, 3DSTATE_CLIP, gen6);
1842    GEN7_SET(gen7, 3DSTATE_SF);
1843    GEN7_SET(gen7, 3DSTATE_WM);
1844    GEN7_SET(gen7, 3DSTATE_CONSTANT_VS);
1845    GEN7_SET(gen7, 3DSTATE_CONSTANT_GS);
1846    GEN7_SET(gen7, 3DSTATE_CONSTANT_PS);
1847    GEN7_SET(gen7, 3DSTATE_SAMPLE_MASK);
1848    GEN7_SET(gen7, 3DSTATE_CONSTANT_HS);
1849    GEN7_SET(gen7, 3DSTATE_CONSTANT_DS);
1850    GEN7_SET(gen7, 3DSTATE_HS);
1851    GEN7_SET(gen7, 3DSTATE_TE);
1852    GEN7_SET(gen7, 3DSTATE_DS);
1853    GEN7_SET(gen7, 3DSTATE_STREAMOUT);
1854    GEN7_SET(gen7, 3DSTATE_SBE);
1855    GEN7_SET(gen7, 3DSTATE_PS);
1856    GEN7_SET(gen7, 3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP);
1857    GEN7_SET(gen7, 3DSTATE_VIEWPORT_STATE_POINTERS_CC);
1858    GEN7_SET(gen7, 3DSTATE_BLEND_STATE_POINTERS);
1859    GEN7_SET(gen7, 3DSTATE_DEPTH_STENCIL_STATE_POINTERS);
1860    GEN7_SET(gen7, 3DSTATE_BINDING_TABLE_POINTERS_VS);
1861    GEN7_SET(gen7, 3DSTATE_BINDING_TABLE_POINTERS_HS);
1862    GEN7_SET(gen7, 3DSTATE_BINDING_TABLE_POINTERS_DS);
1863    GEN7_SET(gen7, 3DSTATE_BINDING_TABLE_POINTERS_GS);
1864    GEN7_SET(gen7, 3DSTATE_BINDING_TABLE_POINTERS_PS);
1865    GEN7_SET(gen7, 3DSTATE_SAMPLER_STATE_POINTERS_VS);
1866    GEN7_SET(gen7, 3DSTATE_SAMPLER_STATE_POINTERS_HS);
1867    GEN7_SET(gen7, 3DSTATE_SAMPLER_STATE_POINTERS_DS);
1868    GEN7_SET(gen7, 3DSTATE_SAMPLER_STATE_POINTERS_GS);
1869    GEN7_SET(gen7, 3DSTATE_SAMPLER_STATE_POINTERS_PS);
1870    GEN7_SET(gen7, 3DSTATE_URB_VS);
1871    GEN7_SET(gen7, 3DSTATE_URB_HS);
1872    GEN7_SET(gen7, 3DSTATE_URB_DS);
1873    GEN7_SET(gen7, 3DSTATE_URB_GS);
1874    GEN7_USE(gen7, 3DSTATE_DRAWING_RECTANGLE, gen6);
1875    GEN7_USE(gen7, 3DSTATE_POLY_STIPPLE_OFFSET, gen6);
1876    GEN7_USE(gen7, 3DSTATE_POLY_STIPPLE_PATTERN, gen6);
1877    GEN7_USE(gen7, 3DSTATE_LINE_STIPPLE, gen6);
1878    GEN7_USE(gen7, 3DSTATE_AA_LINE_PARAMETERS, gen6);
1879    GEN7_USE(gen7, 3DSTATE_MULTISAMPLE, gen6);
1880    GEN7_SET(gen7, 3DSTATE_PUSH_CONSTANT_ALLOC_VS);
1881    GEN7_SET(gen7, 3DSTATE_PUSH_CONSTANT_ALLOC_HS);
1882    GEN7_SET(gen7, 3DSTATE_PUSH_CONSTANT_ALLOC_DS);
1883    GEN7_SET(gen7, 3DSTATE_PUSH_CONSTANT_ALLOC_GS);
1884    GEN7_SET(gen7, 3DSTATE_PUSH_CONSTANT_ALLOC_PS);
1885    GEN7_SET(gen7, 3DSTATE_SO_DECL_LIST);
1886    GEN7_SET(gen7, 3DSTATE_SO_BUFFER);
1887    GEN7_USE(gen7, PIPE_CONTROL, gen6);
1888    GEN7_SET(gen7, 3DPRIMITIVE);
1889    GEN7_USE(gen7, INTERFACE_DESCRIPTOR_DATA, gen6);
1890    GEN7_SET(gen7, SF_CLIP_VIEWPORT);
1891    GEN7_USE(gen7, CC_VIEWPORT, gen6);
1892    GEN7_USE(gen7, COLOR_CALC_STATE, gen6);
1893    GEN7_USE(gen7, BLEND_STATE, gen6);
1894    GEN7_USE(gen7, DEPTH_STENCIL_STATE, gen6);
1895    GEN7_USE(gen7, SCISSOR_RECT, gen6);
1896    GEN7_USE(gen7, BINDING_TABLE_STATE, gen6);
1897    GEN7_USE(gen7, SURFACE_STATE, gen6);
1898    GEN7_USE(gen7, SAMPLER_STATE, gen6);
1899    GEN7_USE(gen7, SAMPLER_BORDER_COLOR_STATE, gen6);
1900    GEN7_USE(gen7, push_constant_buffer, gen6);
1901 #undef GEN7_USE
1902 #undef GEN7_SET
1903 }
1904
1905 static struct ilo_gpe_gen7 gen7_gpe;
1906
1907 const struct ilo_gpe_gen7 *
1908 ilo_gpe_gen7_get(void)
1909 {
1910    if (!gen7_gpe.estimate_command_size)
1911       gen7_init(&gen7_gpe);
1912
1913    return &gen7_gpe;
1914 }