summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/ilo/core/ilo_builder_3d_bottom.h
blob: f7f95f493b761043db40544bc618274b15833d82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2014 LunarG, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *    Chia-I Wu <olv@lunarg.com>
 */

#ifndef ILO_BUILDER_3D_BOTTOM_H
#define ILO_BUILDER_3D_BOTTOM_H

#include "genhw/genhw.h"
#include "intel_winsys.h"

#include "ilo_core.h"
#include "ilo_dev.h"
#include "ilo_state_cc.h"
#include "ilo_state_raster.h"
#include "ilo_state_sbe.h"
#include "ilo_state_shader.h"
#include "ilo_state_viewport.h"
#include "ilo_builder.h"
#include "ilo_builder_3d_top.h"

static inline void
gen6_3DSTATE_CLIP(struct ilo_builder *builder,
                  const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 4;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_CLIP) | (cmd_len - 2);
   /* see raster_set_gen6_3DSTATE_CLIP() */
   dw[1] = rs->clip[0];
   dw[2] = rs->clip[1];
   dw[3] = rs->clip[2];
}

static inline void
gen6_3DSTATE_SF(struct ilo_builder *builder,
                const struct ilo_state_raster *rs,
                const struct ilo_state_sbe *sbe)
{
   const uint8_t cmd_len = 20;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_SF) | (cmd_len - 2);
   /* see sbe_set_gen8_3DSTATE_SBE() */
   dw[1] = sbe->sbe[0];

   /* see raster_set_gen7_3DSTATE_SF() */
   dw[2] = rs->sf[0];
   dw[3] = rs->sf[1];
   dw[4] = rs->sf[2];
   dw[5] = rs->raster[1];
   dw[6] = rs->raster[2];
   dw[7] = rs->raster[3];

   /* see sbe_set_gen8_3DSTATE_SBE_SWIZ() */
   memcpy(&dw[8], sbe->swiz, sizeof(*dw) * 8);

   dw[16] = sbe->sbe[1];
   dw[17] = sbe->sbe[2];
   /* WrapShortest enables */
   dw[18] = 0;
   dw[19] = 0;
}

static inline void
gen7_3DSTATE_SF(struct ilo_builder *builder,
                const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) ? 4 : 7;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 7, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_SF) | (cmd_len - 2);

   /* see raster_set_gen7_3DSTATE_SF() or raster_set_gen8_3DSTATE_SF() */
   dw[1] = rs->sf[0];
   dw[2] = rs->sf[1];
   dw[3] = rs->sf[2];
   if (ilo_dev_gen(builder->dev) < ILO_GEN(8)) {
      dw[4] = rs->raster[1];
      dw[5] = rs->raster[2];
      dw[6] = rs->raster[3];
   }
}

static inline void
gen7_3DSTATE_SBE(struct ilo_builder *builder,
                 const struct ilo_state_sbe *sbe)
{
   const uint8_t cmd_len = 14;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 7, 7.5);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN7_RENDER_CMD(3D, 3DSTATE_SBE) | (cmd_len - 2);
   /* see sbe_set_gen8_3DSTATE_SBE() and sbe_set_gen8_3DSTATE_SBE_SWIZ() */
   dw[1] = sbe->sbe[0];
   memcpy(&dw[2], sbe->swiz, sizeof(*dw) * 8);
   dw[10] = sbe->sbe[1];
   dw[11] = sbe->sbe[2];

   /* WrapShortest enables */
   dw[12] = 0;
   dw[13] = 0;
}

static inline void
gen8_3DSTATE_SBE(struct ilo_builder *builder,
                 const struct ilo_state_sbe *sbe)
{
   const uint8_t cmd_len = 4;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   /* see sbe_set_gen8_3DSTATE_SBE() */
   dw[0] = GEN7_RENDER_CMD(3D, 3DSTATE_SBE) | (cmd_len - 2);
   dw[1] = sbe->sbe[0];
   dw[2] = sbe->sbe[1];
   dw[3] = sbe->sbe[2];
}

static inline void
gen8_3DSTATE_SBE_SWIZ(struct ilo_builder *builder,
                      const struct ilo_state_sbe *sbe)
{
   const uint8_t cmd_len = 11;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_SBE_SWIZ) | (cmd_len - 2);
   /* see sbe_set_gen8_3DSTATE_SBE_SWIZ() */
   memcpy(&dw[1], sbe->swiz, sizeof(*dw) * 8);
   /* WrapShortest enables */
   dw[9] = 0;
   dw[10] = 0;
}

static inline void
gen8_3DSTATE_RASTER(struct ilo_builder *builder,
                    const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 5;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_RASTER) | (cmd_len - 2);
   /* see raster_set_gen8_3DSTATE_RASTER() */
   dw[1] = rs->raster[0];
   dw[2] = rs->raster[1];
   dw[3] = rs->raster[2];
   dw[4] = rs->raster[3];
}

static inline void
gen6_3DSTATE_WM(struct ilo_builder *builder,
                const struct ilo_state_raster *rs,
                const struct ilo_state_ps *ps,
                uint32_t kernel_offset)
{
   const uint8_t cmd_len = 9;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_WM) | (cmd_len - 2);
   dw[1] = kernel_offset;
   /* see raster_set_gen6_3dstate_wm() and ps_set_gen6_3dstate_wm() */
   dw[2] = ps->ps[0];
   dw[3] = ps->ps[1];
   dw[4] = rs->wm[0] | ps->ps[2];
   dw[5] = rs->wm[1] | ps->ps[3];
   dw[6] = rs->wm[2] | ps->ps[4];
   dw[7] = 0; /* kernel 1 */
   dw[8] = 0; /* kernel 2 */
}

static inline void
gen7_3DSTATE_WM(struct ilo_builder *builder,
                const struct ilo_state_raster *rs,
                const struct ilo_state_ps *ps)
{
   const uint8_t cmd_len = 3;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 7, 7.5);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_WM) | (cmd_len - 2);
   /* see raster_set_gen8_3DSTATE_WM() and ps_set_gen7_3dstate_wm() */
   dw[1] = rs->wm[0] | ps->ps[0];
   dw[2] = ps->ps[1];
}

static inline void
gen8_3DSTATE_WM(struct ilo_builder *builder,
                const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_WM) | (cmd_len - 2);
   /* see raster_set_gen8_3DSTATE_WM() */
   dw[1] = rs->wm[0];
}

static inline void
gen8_3DSTATE_WM_DEPTH_STENCIL(struct ilo_builder *builder,
                              const struct ilo_state_cc *cc)
{
   const uint8_t cmd_len = 3;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_WM_DEPTH_STENCIL) | (cmd_len - 2);
   /* see cc_set_gen8_3DSTATE_WM_DEPTH_STENCIL() */
   dw[1] = cc->ds[0];
   dw[2] = cc->ds[1];
}

static inline void
gen8_3DSTATE_WM_HZ_OP(struct ilo_builder *builder,
                      const struct ilo_state_raster *rs,
                      uint16_t width, uint16_t height)
{
   const uint8_t cmd_len = 5;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_WM_HZ_OP) | (cmd_len - 2);
   /* see raster_set_gen8_3dstate_wm_hz_op() */
   dw[1] = rs->wm[1];
   dw[2] = 0;
   /* exclusive */
   dw[3] = height << 16 | width;
   dw[4] = rs->wm[2];
}

static inline void
gen8_disable_3DSTATE_WM_HZ_OP(struct ilo_builder *builder)
{
   const uint8_t cmd_len = 5;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_WM_HZ_OP) | (cmd_len - 2);
   dw[1] = 0;
   dw[2] = 0;
   dw[3] = 0;
   dw[4] = 0;
}

static inline void
gen8_3DSTATE_WM_CHROMAKEY(struct ilo_builder *builder)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_WM_CHROMAKEY) | (cmd_len - 2);
   dw[1] = 0;
}

static inline void
gen7_3DSTATE_PS(struct ilo_builder *builder,
                const struct ilo_state_ps *ps,
                uint32_t kernel_offset)
{
   const uint8_t cmd_len = 8;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 7, 7.5);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN7_RENDER_CMD(3D, 3DSTATE_PS) | (cmd_len - 2);
   dw[1] = kernel_offset;
   /* see ps_set_gen7_3DSTATE_PS() */
   dw[2] = ps->ps[2];
   dw[3] = ps->ps[3];
   dw[4] = ps->ps[4];
   dw[5] = ps->ps[5];
   dw[6] = 0; /* kernel 1 */
   dw[7] = 0; /* kernel 2 */
}

static inline void
gen8_3DSTATE_PS(struct ilo_builder *builder,
                const struct ilo_state_ps *ps,
                uint32_t kernel_offset)
{
   const uint8_t cmd_len = 12;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN7_RENDER_CMD(3D, 3DSTATE_PS) | (cmd_len - 2);
   dw[1] = kernel_offset;
   dw[2] = 0;
   /* see ps_set_gen8_3DSTATE_PS() */
   dw[3] = ps->ps[0];
   dw[4] = ps->ps[1];
   dw[5] = 0;
   dw[6] = ps->ps[2];
   dw[7] = ps->ps[3];
   dw[8] = 0; /* kernel 1 */
   dw[9] = 0;
   dw[10] = 0; /* kernel 2 */
   dw[11] = 0;
}

static inline void
gen8_3DSTATE_PS_EXTRA(struct ilo_builder *builder,
                      const struct ilo_state_ps *ps)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_PS_EXTRA) | (cmd_len - 2);
   /* see ps_set_gen8_3DSTATE_PS_EXTRA() */
   dw[1] = ps->ps[4];
}

static inline void
gen8_3DSTATE_PS_BLEND(struct ilo_builder *builder,
                      const struct ilo_state_cc *cc)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_PS_BLEND) | (cmd_len - 2);
   /* see cc_set_gen8_3DSTATE_PS_BLEND() */
   dw[1] = cc->blend[0];
}

static inline void
gen6_3DSTATE_CONSTANT_PS(struct ilo_builder *builder,
                         const uint32_t *bufs, const int *sizes,
                         int num_bufs)
{
   gen6_3dstate_constant(builder, GEN6_RENDER_OPCODE_3DSTATE_CONSTANT_PS,
         bufs, sizes, num_bufs);
}

static inline void
gen7_3DSTATE_CONSTANT_PS(struct ilo_builder *builder,
                         const uint32_t *bufs, const int *sizes,
                         int num_bufs)
{
   gen7_3dstate_constant(builder, GEN6_RENDER_OPCODE_3DSTATE_CONSTANT_PS,
         bufs, sizes, num_bufs);
}

static inline void
gen7_3DSTATE_BINDING_TABLE_POINTERS_PS(struct ilo_builder *builder,
                                       uint32_t binding_table)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_BINDING_TABLE_POINTERS_PS,
         binding_table);
}

static inline void
gen7_3DSTATE_SAMPLER_STATE_POINTERS_PS(struct ilo_builder *builder,
                                       uint32_t sampler_state)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_SAMPLER_STATE_POINTERS_PS,
         sampler_state);
}

static inline void
gen6_3DSTATE_MULTISAMPLE(struct ilo_builder *builder,
                         const struct ilo_state_raster *rs,
                         const struct ilo_state_sample_pattern *pattern,
                         uint8_t sample_count)
{
   const uint8_t cmd_len = (ilo_dev_gen(builder->dev) >= ILO_GEN(7)) ? 4 : 3;
   const uint32_t *packed = (const uint32_t *)
      ilo_state_sample_pattern_get_packed_offsets(pattern,
            builder->dev, sample_count);
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 7.5);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_MULTISAMPLE) | (cmd_len - 2);
   /* see raster_set_gen8_3DSTATE_MULTISAMPLE() */
   dw[1] = rs->sample[0];

   /* see sample_pattern_set_gen8_3DSTATE_SAMPLE_PATTERN() */
   dw[2] = (sample_count >= 4) ? packed[0] : 0;
   if (ilo_dev_gen(builder->dev) >= ILO_GEN(7))
      dw[3] = (sample_count >= 8) ? packed[1] : 0;
}

static inline void
gen8_3DSTATE_MULTISAMPLE(struct ilo_builder *builder,
                         const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_MULTISAMPLE) | (cmd_len - 2);
   /* see raster_set_gen8_3DSTATE_MULTISAMPLE() */
   dw[1] = rs->sample[0];
}

static inline void
gen8_3DSTATE_SAMPLE_PATTERN(struct ilo_builder *builder,
                            const struct ilo_state_sample_pattern *pattern)
{
   const uint8_t cmd_len = 9;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN8_RENDER_CMD(3D, 3DSTATE_SAMPLE_PATTERN) | (cmd_len - 2);
   dw[1] = 0;
   dw[2] = 0;
   dw[3] = 0;
   dw[4] = 0;
   /* see sample_pattern_set_gen8_3DSTATE_SAMPLE_PATTERN() */
   dw[5] = ((const uint32_t *) pattern->pattern_8x)[1];
   dw[6] = ((const uint32_t *) pattern->pattern_8x)[0];
   dw[7] = ((const uint32_t *) pattern->pattern_4x)[0];
   dw[8] = pattern->pattern_1x[0] << 16 |
           ((const uint16_t *) pattern->pattern_2x)[0];
}

static inline void
gen6_3DSTATE_SAMPLE_MASK(struct ilo_builder *builder,
                         const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_SAMPLE_MASK) | (cmd_len - 2);
   /* see raster_set_gen6_3DSTATE_SAMPLE_MASK() */
   dw[1] = rs->sample[1];
}

static inline void
gen6_3DSTATE_DRAWING_RECTANGLE(struct ilo_builder *builder,
                               unsigned x, unsigned y,
                               unsigned width, unsigned height)
{
   const uint8_t cmd_len = 4;
   unsigned xmax = x + width - 1;
   unsigned ymax = y + height - 1;
   unsigned rect_limit;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   if (ilo_dev_gen(builder->dev) >= ILO_GEN(7)) {
      rect_limit = 16383;
   }
   else {
      /*
       * From the Sandy Bridge PRM, volume 2 part 1, page 230:
       *
       *     "[DevSNB] Errata: This field (Clipped Drawing Rectangle Y Min)
       *      must be an even number"
       */
      assert(y % 2 == 0);

      rect_limit = 8191;
   }

   if (x > rect_limit) x = rect_limit;
   if (y > rect_limit) y = rect_limit;
   if (xmax > rect_limit) xmax = rect_limit;
   if (ymax > rect_limit) ymax = rect_limit;

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_DRAWING_RECTANGLE) | (cmd_len - 2);
   dw[1] = y << 16 | x;
   dw[2] = ymax << 16 | xmax;
   /*
    * There is no need to set the origin.  It is intended to support front
    * buffer rendering.
    */
   dw[3] = 0;
}

static inline void
gen6_3DSTATE_POLY_STIPPLE_OFFSET(struct ilo_builder *builder,
                                 const struct ilo_state_poly_stipple *stipple)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_POLY_STIPPLE_OFFSET) | (cmd_len - 2);
   /* constant */
   dw[1] = 0;
}

static inline void
gen6_3DSTATE_POLY_STIPPLE_PATTERN(struct ilo_builder *builder,
                                  const struct ilo_state_poly_stipple *stipple)
{
   const uint8_t cmd_len = 33;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_POLY_STIPPLE_PATTERN) | (cmd_len - 2);
   /* see poly_stipple_set_gen6_3DSTATE_POLY_STIPPLE_PATTERN() */
   memcpy(&dw[1], stipple->stipple, sizeof(stipple->stipple));
}

static inline void
gen6_3DSTATE_LINE_STIPPLE(struct ilo_builder *builder,
                          const struct ilo_state_line_stipple *stipple)
{
   const uint8_t cmd_len = 3;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_LINE_STIPPLE) | (cmd_len - 2);
   /* see line_stipple_set_gen6_3DSTATE_LINE_STIPPLE() */
   dw[1] = stipple->stipple[0];
   dw[2] = stipple->stipple[1];
}

static inline void
gen6_3DSTATE_AA_LINE_PARAMETERS(struct ilo_builder *builder,
                                const struct ilo_state_raster *rs)
{
   const uint8_t cmd_len = 3;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_AA_LINE_PARAMETERS) | (cmd_len - 2);
   /* constant */
   dw[1] = 0 << GEN6_AA_LINE_DW1_BIAS__SHIFT |
           0 << GEN6_AA_LINE_DW1_SLOPE__SHIFT;
   dw[2] = 0 << GEN6_AA_LINE_DW2_CAP_BIAS__SHIFT |
           0 << GEN6_AA_LINE_DW2_CAP_SLOPE__SHIFT;
}

static inline void
gen6_3DSTATE_DEPTH_BUFFER(struct ilo_builder *builder,
                          const struct ilo_state_zs *zs)
{
   const uint32_t cmd = (ilo_dev_gen(builder->dev) >= ILO_GEN(7)) ?
      GEN7_RENDER_CMD(3D, 3DSTATE_DEPTH_BUFFER) :
      GEN6_RENDER_CMD(3D, 3DSTATE_DEPTH_BUFFER);
   const uint8_t cmd_len = (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) ? 8 : 7;
   uint32_t *dw;
   unsigned pos;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   pos = ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = cmd | (cmd_len - 2);

   /*
    * see zs_set_gen6_3DSTATE_DEPTH_BUFFER() and
    * zs_set_gen7_3DSTATE_DEPTH_BUFFER()
    */
   if (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) {
      dw[1] = zs->depth[0];
      dw[2] = 0;
      dw[3] = 0;
      dw[4] = zs->depth[2];
      dw[5] = zs->depth[3];
      dw[6] = 0;
      dw[7] = zs->depth[4];

      dw[5] |= builder->mocs << GEN8_DEPTH_DW5_MOCS__SHIFT;

      if (zs->depth_bo) {
         ilo_builder_batch_reloc64(builder, pos + 2, zs->depth_bo,
               zs->depth[1], (zs->z_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   } else {
      dw[1] = zs->depth[0];
      dw[2] = 0;
      dw[3] = zs->depth[2];
      dw[4] = zs->depth[3];
      dw[5] = 0;
      dw[6] = zs->depth[4];

      if (ilo_dev_gen(builder->dev) >= ILO_GEN(7))
         dw[4] |= builder->mocs << GEN7_DEPTH_DW4_MOCS__SHIFT;
      else
         dw[6] |= builder->mocs << GEN6_DEPTH_DW6_MOCS__SHIFT;

      if (zs->depth_bo) {
         ilo_builder_batch_reloc(builder, pos + 2, zs->depth_bo,
               zs->depth[1], (zs->z_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   }
}

static inline void
gen6_3DSTATE_STENCIL_BUFFER(struct ilo_builder *builder,
                            const struct ilo_state_zs *zs)
{
   const uint32_t cmd = (ilo_dev_gen(builder->dev) >= ILO_GEN(7)) ?
      GEN7_RENDER_CMD(3D, 3DSTATE_STENCIL_BUFFER) :
      GEN6_RENDER_CMD(3D, 3DSTATE_STENCIL_BUFFER);
   const uint8_t cmd_len = (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) ? 5 : 3;
   uint32_t *dw;
   unsigned pos;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   pos = ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = cmd | (cmd_len - 2);

   /* see zs_set_gen6_3DSTATE_STENCIL_BUFFER() */
   if (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) {
      dw[1] = zs->stencil[0];
      dw[2] = 0;
      dw[3] = 0;
      dw[4] = zs->stencil[2];

      dw[1] |= builder->mocs << GEN8_STENCIL_DW1_MOCS__SHIFT;

      if (zs->stencil_bo) {
         ilo_builder_batch_reloc64(builder, pos + 2, zs->stencil_bo,
               zs->stencil[1], (zs->s_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   } else {
      dw[1] = zs->stencil[0];
      dw[2] = 0;

      dw[1] |= builder->mocs << GEN6_STENCIL_DW1_MOCS__SHIFT;

      if (zs->stencil_bo) {
         ilo_builder_batch_reloc(builder, pos + 2, zs->stencil_bo,
               zs->stencil[1], (zs->s_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   }
}

static inline void
gen6_3DSTATE_HIER_DEPTH_BUFFER(struct ilo_builder *builder,
                               const struct ilo_state_zs *zs)
{
   const uint32_t cmd = (ilo_dev_gen(builder->dev) >= ILO_GEN(7)) ?
      GEN7_RENDER_CMD(3D, 3DSTATE_HIER_DEPTH_BUFFER) :
      GEN6_RENDER_CMD(3D, 3DSTATE_HIER_DEPTH_BUFFER);
   const uint8_t cmd_len = (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) ? 5 : 3;
   uint32_t *dw;
   unsigned pos;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   pos = ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = cmd | (cmd_len - 2);

   /* see zs_set_gen6_3DSTATE_HIER_DEPTH_BUFFER() */
   if (ilo_dev_gen(builder->dev) >= ILO_GEN(8)) {
      dw[1] = zs->hiz[0];
      dw[2] = 0;
      dw[3] = 0;
      dw[4] = zs->hiz[2];

      dw[1] |= builder->mocs << GEN8_HIZ_DW1_MOCS__SHIFT;

      if (zs->hiz_bo) {
         ilo_builder_batch_reloc64(builder, pos + 2, zs->hiz_bo,
               zs->hiz[1], (zs->z_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   } else {
      dw[1] = zs->hiz[0];
      dw[2] = 0;

      dw[1] |= builder->mocs << GEN6_HIZ_DW1_MOCS__SHIFT;

      if (zs->hiz_bo) {
         ilo_builder_batch_reloc(builder, pos + 2, zs->hiz_bo,
               zs->hiz[1], (zs->z_readonly) ? 0 : INTEL_RELOC_WRITE);
      }
   }
}

static inline void
gen6_3DSTATE_CLEAR_PARAMS(struct ilo_builder *builder,
                          uint32_t clear_val)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_CLEAR_PARAMS) |
           GEN6_CLEAR_PARAMS_DW0_VALID |
           (cmd_len - 2);
   dw[1] = clear_val;
}

static inline void
gen7_3DSTATE_CLEAR_PARAMS(struct ilo_builder *builder,
                          uint32_t clear_val)
{
   const uint8_t cmd_len = 3;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 7, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN7_RENDER_CMD(3D, 3DSTATE_CLEAR_PARAMS) | (cmd_len - 2);
   dw[1] = clear_val;
   dw[2] = GEN7_CLEAR_PARAMS_DW2_VALID;
}

static inline void
gen6_3DSTATE_VIEWPORT_STATE_POINTERS(struct ilo_builder *builder,
                                     uint32_t clip_viewport,
                                     uint32_t sf_viewport,
                                     uint32_t cc_viewport)
{
   const uint8_t cmd_len = 4;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_VIEWPORT_STATE_POINTERS) |
           GEN6_VP_PTR_DW0_CLIP_CHANGED |
           GEN6_VP_PTR_DW0_SF_CHANGED |
           GEN6_VP_PTR_DW0_CC_CHANGED |
           (cmd_len - 2);
   dw[1] = clip_viewport;
   dw[2] = sf_viewport;
   dw[3] = cc_viewport;
}

static inline void
gen6_3DSTATE_SCISSOR_STATE_POINTERS(struct ilo_builder *builder,
                                    uint32_t scissor_rect)
{
   const uint8_t cmd_len = 2;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_SCISSOR_STATE_POINTERS) |
           (cmd_len - 2);
   dw[1] = scissor_rect;
}

static inline void
gen6_3DSTATE_CC_STATE_POINTERS(struct ilo_builder *builder,
                               uint32_t blend_state,
                               uint32_t depth_stencil_state,
                               uint32_t color_calc_state)
{
   const uint8_t cmd_len = 4;
   uint32_t *dw;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   ilo_builder_batch_pointer(builder, cmd_len, &dw);

   dw[0] = GEN6_RENDER_CMD(3D, 3DSTATE_CC_STATE_POINTERS) | (cmd_len - 2);
   dw[1] = blend_state | GEN6_CC_PTR_DW1_BLEND_CHANGED;
   dw[2] = depth_stencil_state | GEN6_CC_PTR_DW2_ZS_CHANGED;
   dw[3] = color_calc_state | GEN6_CC_PTR_DW3_CC_CHANGED;
}

static inline void
gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(struct ilo_builder *builder,
                                             uint32_t sf_clip_viewport)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP,
         sf_clip_viewport);
}

static inline void
gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(struct ilo_builder *builder,
                                        uint32_t cc_viewport)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_VIEWPORT_STATE_POINTERS_CC,
         cc_viewport);
}

static inline void
gen7_3DSTATE_CC_STATE_POINTERS(struct ilo_builder *builder,
                               uint32_t color_calc_state)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   if (ilo_dev_gen(builder->dev) >= ILO_GEN(8))
      color_calc_state |= 1;

   gen7_3dstate_pointer(builder,
         GEN6_RENDER_OPCODE_3DSTATE_CC_STATE_POINTERS, color_calc_state);
}

static inline void
gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(struct ilo_builder *builder,
                                          uint32_t depth_stencil_state)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_DEPTH_STENCIL_STATE_POINTERS,
         depth_stencil_state);
}

static inline void
gen7_3DSTATE_BLEND_STATE_POINTERS(struct ilo_builder *builder,
                                  uint32_t blend_state)
{
   ILO_DEV_ASSERT(builder->dev, 7, 8);

   if (ilo_dev_gen(builder->dev) >= ILO_GEN(8))
      blend_state |= 1;

   gen7_3dstate_pointer(builder,
         GEN7_RENDER_OPCODE_3DSTATE_BLEND_STATE_POINTERS,
         blend_state);
}

static inline uint32_t
gen6_CLIP_VIEWPORT(struct ilo_builder *builder,
                   const struct ilo_state_viewport *vp)
{
   const int state_align = 32;
   const int state_len = 4 * vp->count;
   uint32_t state_offset, *dw;
   int i;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   state_offset = ilo_builder_dynamic_pointer(builder,
         ILO_BUILDER_ITEM_CLIP_VIEWPORT, state_align, state_len, &dw);

   for (i = 0; i < vp->count; i++) {
      /* see viewport_matrix_set_gen7_SF_CLIP_VIEWPORT() */
      dw[0] = vp->sf_clip[i][8];
      dw[1] = vp->sf_clip[i][9];
      dw[2] = vp->sf_clip[i][10];
      dw[3] = vp->sf_clip[i][11];

      dw += 4;
   }

   return state_offset;
}

static inline uint32_t
gen6_SF_VIEWPORT(struct ilo_builder *builder,
                 const struct ilo_state_viewport *vp)
{
   const int state_align = 32;
   const int state_len = 8 * vp->count;
   uint32_t state_offset, *dw;
   int i;

   ILO_DEV_ASSERT(builder->dev, 6, 6);

   state_offset = ilo_builder_dynamic_pointer(builder,
         ILO_BUILDER_ITEM_SF_VIEWPORT, state_align, state_len, &dw);

   for (i = 0; i < vp->count; i++) {
      /* see viewport_matrix_set_gen7_SF_CLIP_VIEWPORT() */
      memcpy(dw, vp->sf_clip[i], sizeof(*dw) * 8);

      dw += 8;
   }

   return state_offset;
}

static inline uint32_t
gen7_SF_CLIP_VIEWPORT(struct ilo_builder *builder,
                      const struct ilo_state_viewport *vp)
{
   const int state_align = 64;
   const int state_len = 16 * vp->count;

   ILO_DEV_ASSERT(builder->dev, 7, 8);

   /* see viewport_matrix_set_gen7_SF_CLIP_VIEWPORT() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_SF_VIEWPORT,
         state_align, state_len, (const uint32_t *) vp->sf_clip);
}

static inline uint32_t
gen6_CC_VIEWPORT(struct ilo_builder *builder,
                 const struct ilo_state_viewport *vp)
{
   const int state_align = 32;
   const int state_len = 2 * vp->count;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   /* see viewport_matrix_set_gen6_CC_VIEWPORT() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_CC_VIEWPORT,
         state_align, state_len, (const uint32_t *) vp->cc);
}

static inline uint32_t
gen6_SCISSOR_RECT(struct ilo_builder *builder,
                  const struct ilo_state_viewport *vp)
{
   const int state_align = 32;
   const int state_len = 2 * vp->count;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   /* see viewport_scissor_set_gen6_SCISSOR_RECT() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_SCISSOR_RECT,
         state_align, state_len, (const uint32_t *) vp->scissor);
}

static inline uint32_t
gen6_COLOR_CALC_STATE(struct ilo_builder *builder,
                      const struct ilo_state_cc *cc)
{
   const int state_align = 64;
   const int state_len = 6;

   ILO_DEV_ASSERT(builder->dev, 6, 8);

   /* see cc_params_set_gen6_COLOR_CALC_STATE() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_COLOR_CALC,
         state_align, state_len, cc->cc);
}

static inline uint32_t
gen6_DEPTH_STENCIL_STATE(struct ilo_builder *builder,
                         const struct ilo_state_cc *cc)
{
   const int state_align = 64;
   const int state_len = 3;

   ILO_DEV_ASSERT(builder->dev, 6, 7.5);

   /* see cc_set_gen6_DEPTH_STENCIL_STATE() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_DEPTH_STENCIL,
         state_align, state_len, cc->ds);
}

static inline uint32_t
gen6_BLEND_STATE(struct ilo_builder *builder,
                 const struct ilo_state_cc *cc)
{
   const int state_align = 64;
   const int state_len = 2 * cc->blend_state_count;

   ILO_DEV_ASSERT(builder->dev, 6, 7.5);

   if (!state_len)
      return 0;

   /* see cc_set_gen6_BLEND_STATE() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_BLEND,
         state_align, state_len, cc->blend);
}

static inline uint32_t
gen8_BLEND_STATE(struct ilo_builder *builder,
                 const struct ilo_state_cc *cc)
{
   const int state_align = 64;
   const int state_len = 1 + 2 * cc->blend_state_count;

   ILO_DEV_ASSERT(builder->dev, 8, 8);

   /* see cc_set_gen8_BLEND_STATE() */
   return ilo_builder_dynamic_write(builder, ILO_BUILDER_ITEM_BLEND,
         state_align, state_len, &cc->blend[1]);
}

#endif /* ILO_BUILDER_3D_BOTTOM_H */