OSDN Git Service

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