i965: Only #if...#endif a single function or related section at a time.
[mesa.git] / src / mesa / drivers / dri / i965 / genX_state_upload.c
1 /*
2 * Copyright © 2017 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25
26 #include "common/gen_device_info.h"
27 #include "common/gen_sample_positions.h"
28 #include "genxml/gen_macros.h"
29
30 #include "main/bufferobj.h"
31 #include "main/context.h"
32 #include "main/enums.h"
33 #include "main/macros.h"
34
35 #include "brw_context.h"
36 #if GEN_GEN == 6
37 #include "brw_defines.h"
38 #endif
39 #include "brw_draw.h"
40 #include "brw_multisample_state.h"
41 #include "brw_state.h"
42 #include "brw_wm.h"
43 #include "brw_util.h"
44
45 #include "intel_batchbuffer.h"
46 #include "intel_buffer_objects.h"
47 #include "intel_fbo.h"
48
49 #include "main/enums.h"
50 #include "main/fbobject.h"
51 #include "main/framebuffer.h"
52 #include "main/glformats.h"
53 #include "main/shaderapi.h"
54 #include "main/stencil.h"
55 #include "main/transformfeedback.h"
56 #include "main/varray.h"
57 #include "main/viewport.h"
58
59 UNUSED static void *
60 emit_dwords(struct brw_context *brw, unsigned n)
61 {
62 intel_batchbuffer_begin(brw, n, RENDER_RING);
63 uint32_t *map = brw->batch.map_next;
64 brw->batch.map_next += n;
65 intel_batchbuffer_advance(brw);
66 return map;
67 }
68
69 struct brw_address {
70 struct brw_bo *bo;
71 uint32_t read_domains;
72 uint32_t write_domain;
73 uint32_t offset;
74 };
75
76 static uint64_t
77 emit_reloc(struct brw_context *brw,
78 void *location, struct brw_address address, uint32_t delta)
79 {
80 uint32_t offset = (char *) location - (char *) brw->batch.map;
81
82 return brw_emit_reloc(&brw->batch, offset, address.bo,
83 address.offset + delta,
84 address.read_domains,
85 address.write_domain);
86 }
87
88 #define __gen_address_type struct brw_address
89 #define __gen_user_data struct brw_context
90
91 static uint64_t
92 __gen_combine_address(struct brw_context *brw, void *location,
93 struct brw_address address, uint32_t delta)
94 {
95 if (address.bo == NULL) {
96 return address.offset + delta;
97 } else {
98 return emit_reloc(brw, location, address, delta);
99 }
100 }
101
102 static inline struct brw_address
103 render_bo(struct brw_bo *bo, uint32_t offset)
104 {
105 return (struct brw_address) {
106 .bo = bo,
107 .offset = offset,
108 .read_domains = I915_GEM_DOMAIN_RENDER,
109 .write_domain = I915_GEM_DOMAIN_RENDER,
110 };
111 }
112
113 static inline struct brw_address
114 render_ro_bo(struct brw_bo *bo, uint32_t offset)
115 {
116 return (struct brw_address) {
117 .bo = bo,
118 .offset = offset,
119 .read_domains = I915_GEM_DOMAIN_RENDER,
120 .write_domain = 0,
121 };
122 }
123
124 static inline struct brw_address
125 instruction_bo(struct brw_bo *bo, uint32_t offset)
126 {
127 return (struct brw_address) {
128 .bo = bo,
129 .offset = offset,
130 .read_domains = I915_GEM_DOMAIN_INSTRUCTION,
131 .write_domain = I915_GEM_DOMAIN_INSTRUCTION,
132 };
133 }
134
135 static inline struct brw_address
136 vertex_bo(struct brw_bo *bo, uint32_t offset)
137 {
138 return (struct brw_address) {
139 .bo = bo,
140 .offset = offset,
141 .read_domains = I915_GEM_DOMAIN_VERTEX,
142 .write_domain = 0,
143 };
144 }
145
146 #include "genxml/genX_pack.h"
147
148 #define _brw_cmd_length(cmd) cmd ## _length
149 #define _brw_cmd_length_bias(cmd) cmd ## _length_bias
150 #define _brw_cmd_header(cmd) cmd ## _header
151 #define _brw_cmd_pack(cmd) cmd ## _pack
152
153 #define brw_batch_emit(brw, cmd, name) \
154 for (struct cmd name = { _brw_cmd_header(cmd) }, \
155 *_dst = emit_dwords(brw, _brw_cmd_length(cmd)); \
156 __builtin_expect(_dst != NULL, 1); \
157 _brw_cmd_pack(cmd)(brw, (void *)_dst, &name), \
158 _dst = NULL)
159
160 #define brw_batch_emitn(brw, cmd, n, ...) ({ \
161 uint32_t *_dw = emit_dwords(brw, n); \
162 struct cmd template = { \
163 _brw_cmd_header(cmd), \
164 .DWordLength = n - _brw_cmd_length_bias(cmd), \
165 __VA_ARGS__ \
166 }; \
167 _brw_cmd_pack(cmd)(brw, _dw, &template); \
168 _dw + 1; /* Array starts at dw[1] */ \
169 })
170
171 #define brw_state_emit(brw, cmd, align, offset, name) \
172 for (struct cmd name = { 0, }, \
173 *_dst = brw_state_batch(brw, _brw_cmd_length(cmd) * 4, \
174 align, offset); \
175 __builtin_expect(_dst != NULL, 1); \
176 _brw_cmd_pack(cmd)(brw, (void *)_dst, &name), \
177 _dst = NULL)
178
179 /**
180 * Polygon stipple packet
181 */
182 static void
183 genX(upload_polygon_stipple)(struct brw_context *brw)
184 {
185 struct gl_context *ctx = &brw->ctx;
186
187 /* _NEW_POLYGON */
188 if (!ctx->Polygon.StippleFlag)
189 return;
190
191 brw_batch_emit(brw, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
192 /* Polygon stipple is provided in OpenGL order, i.e. bottom
193 * row first. If we're rendering to a window (i.e. the
194 * default frame buffer object, 0), then we need to invert
195 * it to match our pixel layout. But if we're rendering
196 * to a FBO (i.e. any named frame buffer object), we *don't*
197 * need to invert - we already match the layout.
198 */
199 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
200 for (unsigned i = 0; i < 32; i++)
201 poly.PatternRow[i] = ctx->PolygonStipple[31 - i]; /* invert */
202 } else {
203 for (unsigned i = 0; i < 32; i++)
204 poly.PatternRow[i] = ctx->PolygonStipple[i];
205 }
206 }
207 }
208
209 static const struct brw_tracked_state genX(polygon_stipple) = {
210 .dirty = {
211 .mesa = _NEW_POLYGON |
212 _NEW_POLYGONSTIPPLE,
213 .brw = BRW_NEW_CONTEXT,
214 },
215 .emit = genX(upload_polygon_stipple),
216 };
217
218 /**
219 * Polygon stipple offset packet
220 */
221 static void
222 genX(upload_polygon_stipple_offset)(struct brw_context *brw)
223 {
224 struct gl_context *ctx = &brw->ctx;
225
226 /* _NEW_POLYGON */
227 if (!ctx->Polygon.StippleFlag)
228 return;
229
230 brw_batch_emit(brw, GENX(3DSTATE_POLY_STIPPLE_OFFSET), poly) {
231 /* _NEW_BUFFERS
232 *
233 * If we're drawing to a system window we have to invert the Y axis
234 * in order to match the OpenGL pixel coordinate system, and our
235 * offset must be matched to the window position. If we're drawing
236 * to a user-created FBO then our native pixel coordinate system
237 * works just fine, and there's no window system to worry about.
238 */
239 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
240 poly.PolygonStippleYOffset =
241 (32 - (_mesa_geometric_height(ctx->DrawBuffer) & 31)) & 31;
242 }
243 }
244 }
245
246 static const struct brw_tracked_state genX(polygon_stipple_offset) = {
247 .dirty = {
248 .mesa = _NEW_BUFFERS |
249 _NEW_POLYGON,
250 .brw = BRW_NEW_CONTEXT,
251 },
252 .emit = genX(upload_polygon_stipple_offset),
253 };
254
255 /**
256 * Line stipple packet
257 */
258 static void
259 genX(upload_line_stipple)(struct brw_context *brw)
260 {
261 struct gl_context *ctx = &brw->ctx;
262
263 if (!ctx->Line.StippleFlag)
264 return;
265
266 brw_batch_emit(brw, GENX(3DSTATE_LINE_STIPPLE), line) {
267 line.LineStipplePattern = ctx->Line.StipplePattern;
268
269 line.LineStippleInverseRepeatCount = 1.0f / ctx->Line.StippleFactor;
270 line.LineStippleRepeatCount = ctx->Line.StippleFactor;
271 }
272 }
273
274 static const struct brw_tracked_state genX(line_stipple) = {
275 .dirty = {
276 .mesa = _NEW_LINE,
277 .brw = BRW_NEW_CONTEXT,
278 },
279 .emit = genX(upload_line_stipple),
280 };
281
282 /* Constant single cliprect for framebuffer object or DRI2 drawing */
283 static void
284 genX(upload_drawing_rect)(struct brw_context *brw)
285 {
286 struct gl_context *ctx = &brw->ctx;
287 const struct gl_framebuffer *fb = ctx->DrawBuffer;
288 const unsigned int fb_width = _mesa_geometric_width(fb);
289 const unsigned int fb_height = _mesa_geometric_height(fb);
290
291 brw_batch_emit(brw, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
292 rect.ClippedDrawingRectangleXMax = fb_width - 1;
293 rect.ClippedDrawingRectangleYMax = fb_height - 1;
294 }
295 }
296
297 static const struct brw_tracked_state genX(drawing_rect) = {
298 .dirty = {
299 .mesa = _NEW_BUFFERS,
300 .brw = BRW_NEW_BLORP |
301 BRW_NEW_CONTEXT,
302 },
303 .emit = genX(upload_drawing_rect),
304 };
305
306 static uint32_t *
307 genX(emit_vertex_buffer_state)(struct brw_context *brw,
308 uint32_t *dw,
309 unsigned buffer_nr,
310 struct brw_bo *bo,
311 unsigned start_offset,
312 unsigned end_offset,
313 unsigned stride,
314 unsigned step_rate)
315 {
316 struct GENX(VERTEX_BUFFER_STATE) buf_state = {
317 .VertexBufferIndex = buffer_nr,
318 .BufferPitch = stride,
319 .BufferStartingAddress = vertex_bo(bo, start_offset),
320 #if GEN_GEN >= 8
321 .BufferSize = end_offset - start_offset,
322 #endif
323
324 #if GEN_GEN >= 7
325 .AddressModifyEnable = true,
326 #endif
327
328 #if GEN_GEN < 8
329 .BufferAccessType = step_rate ? INSTANCEDATA : VERTEXDATA,
330 .InstanceDataStepRate = step_rate,
331 #if GEN_GEN >= 5
332 .EndAddress = vertex_bo(bo, end_offset - 1),
333 #endif
334 #endif
335
336 #if GEN_GEN == 9
337 .VertexBufferMOCS = SKL_MOCS_WB,
338 #elif GEN_GEN == 8
339 .VertexBufferMOCS = BDW_MOCS_WB,
340 #elif GEN_GEN == 7
341 .VertexBufferMOCS = GEN7_MOCS_L3,
342 #endif
343 };
344
345 GENX(VERTEX_BUFFER_STATE_pack)(brw, dw, &buf_state);
346 return dw + GENX(VERTEX_BUFFER_STATE_length);
347 }
348
349 UNUSED static bool
350 is_passthru_format(uint32_t format)
351 {
352 switch (format) {
353 case ISL_FORMAT_R64_PASSTHRU:
354 case ISL_FORMAT_R64G64_PASSTHRU:
355 case ISL_FORMAT_R64G64B64_PASSTHRU:
356 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
357 return true;
358 default:
359 return false;
360 }
361 }
362
363 UNUSED static int
364 genX(uploads_needed)(uint32_t format)
365 {
366 if (!is_passthru_format(format))
367 return 1;
368
369 switch (format) {
370 case ISL_FORMAT_R64_PASSTHRU:
371 case ISL_FORMAT_R64G64_PASSTHRU:
372 return 1;
373 case ISL_FORMAT_R64G64B64_PASSTHRU:
374 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
375 return 2;
376 default:
377 unreachable("not reached");
378 }
379 }
380
381 /*
382 * Returns the format that we are finally going to use when upload a vertex
383 * element. It will only change if we are using *64*PASSTHRU formats, as for
384 * gen < 8 they need to be splitted on two *32*FLOAT formats.
385 *
386 * @upload points in which upload we are. Valid values are [0,1]
387 */
388 static uint32_t
389 downsize_format_if_needed(uint32_t format,
390 int upload)
391 {
392 assert(upload == 0 || upload == 1);
393
394 if (!is_passthru_format(format))
395 return format;
396
397 switch (format) {
398 case ISL_FORMAT_R64_PASSTHRU:
399 return ISL_FORMAT_R32G32_FLOAT;
400 case ISL_FORMAT_R64G64_PASSTHRU:
401 return ISL_FORMAT_R32G32B32A32_FLOAT;
402 case ISL_FORMAT_R64G64B64_PASSTHRU:
403 return !upload ? ISL_FORMAT_R32G32B32A32_FLOAT
404 : ISL_FORMAT_R32G32_FLOAT;
405 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
406 return ISL_FORMAT_R32G32B32A32_FLOAT;
407 default:
408 unreachable("not reached");
409 }
410 }
411
412 /*
413 * Returns the number of componentes associated with a format that is used on
414 * a 64 to 32 format split. See downsize_format()
415 */
416 static int
417 upload_format_size(uint32_t upload_format)
418 {
419 switch (upload_format) {
420 case ISL_FORMAT_R32G32_FLOAT:
421 return 2;
422 case ISL_FORMAT_R32G32B32A32_FLOAT:
423 return 4;
424 default:
425 unreachable("not reached");
426 }
427 }
428
429 static void
430 genX(emit_vertices)(struct brw_context *brw)
431 {
432 uint32_t *dw;
433
434 brw_prepare_vertices(brw);
435 brw_prepare_shader_draw_parameters(brw);
436
437 #if GEN_GEN < 6
438 brw_emit_query_begin(brw);
439 #endif
440
441 const struct brw_vs_prog_data *vs_prog_data =
442 brw_vs_prog_data(brw->vs.base.prog_data);
443
444 #if GEN_GEN >= 8
445 struct gl_context *ctx = &brw->ctx;
446 bool uses_edge_flag = (ctx->Polygon.FrontMode != GL_FILL ||
447 ctx->Polygon.BackMode != GL_FILL);
448
449 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid) {
450 unsigned vue = brw->vb.nr_enabled;
451
452 /* The element for the edge flags must always be last, so we have to
453 * insert the SGVS before it in that case.
454 */
455 if (uses_edge_flag) {
456 assert(vue > 0);
457 vue--;
458 }
459
460 WARN_ONCE(vue >= 33,
461 "Trying to insert VID/IID past 33rd vertex element, "
462 "need to reorder the vertex attrbutes.");
463
464 brw_batch_emit(brw, GENX(3DSTATE_VF_SGVS), vfs) {
465 if (vs_prog_data->uses_vertexid) {
466 vfs.VertexIDEnable = true;
467 vfs.VertexIDComponentNumber = 2;
468 vfs.VertexIDElementOffset = vue;
469 }
470
471 if (vs_prog_data->uses_instanceid) {
472 vfs.InstanceIDEnable = true;
473 vfs.InstanceIDComponentNumber = 3;
474 vfs.InstanceIDElementOffset = vue;
475 }
476 }
477
478 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
479 vfi.InstancingEnable = true;
480 vfi.VertexElementIndex = vue;
481 }
482 } else {
483 brw_batch_emit(brw, GENX(3DSTATE_VF_SGVS), vfs);
484 }
485
486 /* Normally we don't need an element for the SGVS attribute because the
487 * 3DSTATE_VF_SGVS instruction lets you store the generated attribute in an
488 * element that is past the list in 3DSTATE_VERTEX_ELEMENTS. However if
489 * we're using draw parameters then we need an element for the those
490 * values. Additionally if there is an edge flag element then the SGVS
491 * can't be inserted past that so we need a dummy element to ensure that
492 * the edge flag is the last one.
493 */
494 const bool needs_sgvs_element = (vs_prog_data->uses_basevertex ||
495 vs_prog_data->uses_baseinstance ||
496 ((vs_prog_data->uses_instanceid ||
497 vs_prog_data->uses_vertexid)
498 && uses_edge_flag));
499 #else
500 const bool needs_sgvs_element = (vs_prog_data->uses_basevertex ||
501 vs_prog_data->uses_baseinstance ||
502 vs_prog_data->uses_instanceid ||
503 vs_prog_data->uses_vertexid);
504 #endif
505 unsigned nr_elements =
506 brw->vb.nr_enabled + needs_sgvs_element + vs_prog_data->uses_drawid;
507
508 #if GEN_GEN < 8
509 /* If any of the formats of vb.enabled needs more that one upload, we need
510 * to add it to nr_elements
511 */
512 for (unsigned i = 0; i < brw->vb.nr_enabled; i++) {
513 struct brw_vertex_element *input = brw->vb.enabled[i];
514 uint32_t format = brw_get_vertex_surface_type(brw, input->glarray);
515
516 if (genX(uploads_needed(format)) > 1)
517 nr_elements++;
518 }
519 #endif
520
521 /* If the VS doesn't read any inputs (calculating vertex position from
522 * a state variable for some reason, for example), emit a single pad
523 * VERTEX_ELEMENT struct and bail.
524 *
525 * The stale VB state stays in place, but they don't do anything unless
526 * a VE loads from them.
527 */
528 if (nr_elements == 0) {
529 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_ELEMENTS), 1 + GENX(VERTEX_ELEMENT_STATE_length));
530 struct GENX(VERTEX_ELEMENT_STATE) elem = {
531 .Valid = true,
532 .SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT,
533 .Component0Control = VFCOMP_STORE_0,
534 .Component1Control = VFCOMP_STORE_0,
535 .Component2Control = VFCOMP_STORE_0,
536 .Component3Control = VFCOMP_STORE_1_FP,
537 };
538 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem);
539 return;
540 }
541
542 /* Now emit 3DSTATE_VERTEX_BUFFERS and 3DSTATE_VERTEX_ELEMENTS packets. */
543 const bool uses_draw_params =
544 vs_prog_data->uses_basevertex ||
545 vs_prog_data->uses_baseinstance;
546 const unsigned nr_buffers = brw->vb.nr_buffers +
547 uses_draw_params + vs_prog_data->uses_drawid;
548
549 if (nr_buffers) {
550 #if GEN_GEN >= 6
551 assert(nr_buffers <= 33);
552 #else
553 assert(nr_buffers <= 17);
554 #endif
555 assert(nr_buffers <= (GEN_GEN >= 6 ? 33 : 17));
556
557 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_BUFFERS),
558 1 + GENX(VERTEX_BUFFER_STATE_length) * nr_buffers);
559
560 for (unsigned i = 0; i < brw->vb.nr_buffers; i++) {
561 const struct brw_vertex_buffer *buffer = &brw->vb.buffers[i];
562 /* Prior to Haswell and Bay Trail we have to use 4-component formats
563 * to fake 3-component ones. In particular, we do this for
564 * half-float and 8 and 16-bit integer formats. This means that the
565 * vertex element may poke over the end of the buffer by 2 bytes.
566 */
567 unsigned padding =
568 (GEN_GEN <= 7 && !brw->is_baytrail && !brw->is_haswell) * 2;
569 dw = genX(emit_vertex_buffer_state)(brw, dw, i, buffer->bo,
570 buffer->offset,
571 buffer->offset + buffer->size + padding,
572 buffer->stride,
573 buffer->step_rate);
574 }
575
576 if (uses_draw_params) {
577 dw = genX(emit_vertex_buffer_state)(brw, dw, brw->vb.nr_buffers,
578 brw->draw.draw_params_bo,
579 brw->draw.draw_params_offset,
580 brw->draw.draw_params_bo->size,
581 0 /* stride */,
582 0 /* step rate */);
583 }
584
585 if (vs_prog_data->uses_drawid) {
586 dw = genX(emit_vertex_buffer_state)(brw, dw, brw->vb.nr_buffers + 1,
587 brw->draw.draw_id_bo,
588 brw->draw.draw_id_offset,
589 brw->draw.draw_id_bo->size,
590 0 /* stride */,
591 0 /* step rate */);
592 }
593 }
594
595 /* The hardware allows one more VERTEX_ELEMENTS than VERTEX_BUFFERS,
596 * presumably for VertexID/InstanceID.
597 */
598 #if GEN_GEN >= 6
599 assert(nr_elements <= 34);
600 struct brw_vertex_element *gen6_edgeflag_input = NULL;
601 #else
602 assert(nr_elements <= 18);
603 #endif
604
605 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_ELEMENTS),
606 1 + GENX(VERTEX_ELEMENT_STATE_length) * nr_elements);
607 unsigned i;
608 for (i = 0; i < brw->vb.nr_enabled; i++) {
609 struct brw_vertex_element *input = brw->vb.enabled[i];
610 uint32_t format = brw_get_vertex_surface_type(brw, input->glarray);
611 uint32_t comp0 = VFCOMP_STORE_SRC;
612 uint32_t comp1 = VFCOMP_STORE_SRC;
613 uint32_t comp2 = VFCOMP_STORE_SRC;
614 uint32_t comp3 = VFCOMP_STORE_SRC;
615 unsigned num_uploads = 1;
616
617 #if GEN_GEN >= 8
618 /* From the BDW PRM, Volume 2d, page 588 (VERTEX_ELEMENT_STATE):
619 * "Any SourceElementFormat of *64*_PASSTHRU cannot be used with an
620 * element which has edge flag enabled."
621 */
622 assert(!(is_passthru_format(format) && uses_edge_flag));
623 #endif
624
625 /* The gen4 driver expects edgeflag to come in as a float, and passes
626 * that float on to the tests in the clipper. Mesa's current vertex
627 * attribute value for EdgeFlag is stored as a float, which works out.
628 * glEdgeFlagPointer, on the other hand, gives us an unnormalized
629 * integer ubyte. Just rewrite that to convert to a float.
630 *
631 * Gen6+ passes edgeflag as sideband along with the vertex, instead
632 * of in the VUE. We have to upload it sideband as the last vertex
633 * element according to the B-Spec.
634 */
635 #if GEN_GEN >= 6
636 if (input == &brw->vb.inputs[VERT_ATTRIB_EDGEFLAG]) {
637 gen6_edgeflag_input = input;
638 continue;
639 }
640 #endif
641
642 #if GEN_GEN < 8
643 num_uploads = genX(uploads_needed(format));
644 #endif
645
646 for (unsigned c = 0; c < num_uploads; c++) {
647 uint32_t upload_format = GEN_GEN >= 8 ? format :
648 downsize_format_if_needed(format, c);
649 /* If we need more that one upload, the offset stride would be 128
650 * bits (16 bytes), as for previous uploads we are using the full
651 * entry. */
652 unsigned int offset = input->offset + c * 16;
653 int size = input->glarray->Size;
654
655 if (GEN_GEN < 8 && is_passthru_format(format))
656 size = upload_format_size(upload_format);
657
658 switch (size) {
659 case 0: comp0 = VFCOMP_STORE_0;
660 case 1: comp1 = VFCOMP_STORE_0;
661 case 2: comp2 = VFCOMP_STORE_0;
662 case 3:
663 if (GEN_GEN >= 8 && input->glarray->Doubles) {
664 comp3 = VFCOMP_STORE_0;
665 } else if (input->glarray->Integer) {
666 comp3 = VFCOMP_STORE_1_INT;
667 } else {
668 comp3 = VFCOMP_STORE_1_FP;
669 }
670
671 break;
672 }
673
674 #if GEN_GEN >= 8
675 /* From the BDW PRM, Volume 2d, page 586 (VERTEX_ELEMENT_STATE):
676 *
677 * "When SourceElementFormat is set to one of the *64*_PASSTHRU
678 * formats, 64-bit components are stored in the URB without any
679 * conversion. In this case, vertex elements must be written as 128
680 * or 256 bits, with VFCOMP_STORE_0 being used to pad the output as
681 * required. E.g., if R64_PASSTHRU is used to copy a 64-bit Red
682 * component into the URB, Component 1 must be specified as
683 * VFCOMP_STORE_0 (with Components 2,3 set to VFCOMP_NOSTORE) in
684 * order to output a 128-bit vertex element, or Components 1-3 must
685 * be specified as VFCOMP_STORE_0 in order to output a 256-bit vertex
686 * element. Likewise, use of R64G64B64_PASSTHRU requires Component 3
687 * to be specified as VFCOMP_STORE_0 in order to output a 256-bit
688 * vertex element."
689 */
690 if (input->glarray->Doubles && !input->is_dual_slot) {
691 /* Store vertex elements which correspond to double and dvec2 vertex
692 * shader inputs as 128-bit vertex elements, instead of 256-bits.
693 */
694 comp2 = VFCOMP_NOSTORE;
695 comp3 = VFCOMP_NOSTORE;
696 }
697 #endif
698
699 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
700 .VertexBufferIndex = input->buffer,
701 .Valid = true,
702 .SourceElementFormat = upload_format,
703 .SourceElementOffset = offset,
704 .Component0Control = comp0,
705 .Component1Control = comp1,
706 .Component2Control = comp2,
707 .Component3Control = comp3,
708 #if GEN_GEN < 5
709 .DestinationElementOffset = i * 4,
710 #endif
711 };
712
713 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
714 dw += GENX(VERTEX_ELEMENT_STATE_length);
715 }
716 }
717
718 if (needs_sgvs_element) {
719 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
720 .Valid = true,
721 .Component0Control = VFCOMP_STORE_0,
722 .Component1Control = VFCOMP_STORE_0,
723 .Component2Control = VFCOMP_STORE_0,
724 .Component3Control = VFCOMP_STORE_0,
725 #if GEN_GEN < 5
726 .DestinationElementOffset = i * 4,
727 #endif
728 };
729
730 #if GEN_GEN >= 8
731 if (vs_prog_data->uses_basevertex ||
732 vs_prog_data->uses_baseinstance) {
733 elem_state.VertexBufferIndex = brw->vb.nr_buffers;
734 elem_state.SourceElementFormat = ISL_FORMAT_R32G32_UINT;
735 elem_state.Component0Control = VFCOMP_STORE_SRC;
736 elem_state.Component1Control = VFCOMP_STORE_SRC;
737 }
738 #else
739 elem_state.VertexBufferIndex = brw->vb.nr_buffers;
740 elem_state.SourceElementFormat = ISL_FORMAT_R32G32_UINT;
741 if (vs_prog_data->uses_basevertex)
742 elem_state.Component0Control = VFCOMP_STORE_SRC;
743
744 if (vs_prog_data->uses_baseinstance)
745 elem_state.Component1Control = VFCOMP_STORE_SRC;
746
747 if (vs_prog_data->uses_vertexid)
748 elem_state.Component2Control = VFCOMP_STORE_VID;
749
750 if (vs_prog_data->uses_instanceid)
751 elem_state.Component3Control = VFCOMP_STORE_IID;
752 #endif
753
754 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
755 dw += GENX(VERTEX_ELEMENT_STATE_length);
756 }
757
758 if (vs_prog_data->uses_drawid) {
759 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
760 .Valid = true,
761 .VertexBufferIndex = brw->vb.nr_buffers + 1,
762 .SourceElementFormat = ISL_FORMAT_R32_UINT,
763 .Component0Control = VFCOMP_STORE_SRC,
764 .Component1Control = VFCOMP_STORE_0,
765 .Component2Control = VFCOMP_STORE_0,
766 .Component3Control = VFCOMP_STORE_0,
767 #if GEN_GEN < 5
768 .DestinationElementOffset = i * 4,
769 #endif
770 };
771
772 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
773 dw += GENX(VERTEX_ELEMENT_STATE_length);
774 }
775
776 #if GEN_GEN >= 6
777 if (gen6_edgeflag_input) {
778 uint32_t format =
779 brw_get_vertex_surface_type(brw, gen6_edgeflag_input->glarray);
780
781 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
782 .Valid = true,
783 .VertexBufferIndex = gen6_edgeflag_input->buffer,
784 .EdgeFlagEnable = true,
785 .SourceElementFormat = format,
786 .SourceElementOffset = gen6_edgeflag_input->offset,
787 .Component0Control = VFCOMP_STORE_SRC,
788 .Component1Control = VFCOMP_STORE_0,
789 .Component2Control = VFCOMP_STORE_0,
790 .Component3Control = VFCOMP_STORE_0,
791 };
792
793 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
794 dw += GENX(VERTEX_ELEMENT_STATE_length);
795 }
796 #endif
797
798 #if GEN_GEN >= 8
799 for (unsigned i = 0, j = 0; i < brw->vb.nr_enabled; i++) {
800 const struct brw_vertex_element *input = brw->vb.enabled[i];
801 const struct brw_vertex_buffer *buffer = &brw->vb.buffers[input->buffer];
802 unsigned element_index;
803
804 /* The edge flag element is reordered to be the last one in the code
805 * above so we need to compensate for that in the element indices used
806 * below.
807 */
808 if (input == gen6_edgeflag_input)
809 element_index = nr_elements - 1;
810 else
811 element_index = j++;
812
813 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
814 vfi.VertexElementIndex = element_index;
815 vfi.InstancingEnable = buffer->step_rate != 0;
816 vfi.InstanceDataStepRate = buffer->step_rate;
817 }
818 }
819
820 if (vs_prog_data->uses_drawid) {
821 const unsigned element = brw->vb.nr_enabled + needs_sgvs_element;
822
823 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
824 vfi.VertexElementIndex = element;
825 }
826 }
827 #endif
828 }
829
830 static const struct brw_tracked_state genX(vertices) = {
831 .dirty = {
832 .mesa = _NEW_POLYGON,
833 .brw = BRW_NEW_BATCH |
834 BRW_NEW_BLORP |
835 BRW_NEW_VERTICES |
836 BRW_NEW_VS_PROG_DATA,
837 },
838 .emit = genX(emit_vertices),
839 };
840
841 #if GEN_IS_HASWELL || GEN_GEN >= 8
842 static void
843 genX(upload_cut_index)(struct brw_context *brw)
844 {
845 const struct gl_context *ctx = &brw->ctx;
846
847 brw_batch_emit(brw, GENX(3DSTATE_VF), vf) {
848 if (ctx->Array._PrimitiveRestart && brw->ib.ib) {
849 vf.IndexedDrawCutIndexEnable = true;
850 vf.CutIndex = _mesa_primitive_restart_index(ctx, brw->ib.index_size);
851 }
852 }
853 }
854
855 const struct brw_tracked_state genX(cut_index) = {
856 .dirty = {
857 .mesa = _NEW_TRANSFORM,
858 .brw = BRW_NEW_INDEX_BUFFER,
859 },
860 .emit = genX(upload_cut_index),
861 };
862 #endif
863
864 #if GEN_GEN >= 6
865 /**
866 * Determine the appropriate attribute override value to store into the
867 * 3DSTATE_SF structure for a given fragment shader attribute. The attribute
868 * override value contains two pieces of information: the location of the
869 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
870 * flag indicating whether to "swizzle" the attribute based on the direction
871 * the triangle is facing.
872 *
873 * If an attribute is "swizzled", then the given VUE location is used for
874 * front-facing triangles, and the VUE location that immediately follows is
875 * used for back-facing triangles. We use this to implement the mapping from
876 * gl_FrontColor/gl_BackColor to gl_Color.
877 *
878 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
879 * being instructed to begin reading attribute data. It can be set to a
880 * nonzero value to prevent the SF unit from wasting time reading elements of
881 * the VUE that are not needed by the fragment shader. It is measured in
882 * 256-bit increments.
883 */
884 static void
885 genX(get_attr_override)(struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr,
886 const struct brw_vue_map *vue_map,
887 int urb_entry_read_offset, int fs_attr,
888 bool two_side_color, uint32_t *max_source_attr)
889 {
890 /* Find the VUE slot for this attribute. */
891 int slot = vue_map->varying_to_slot[fs_attr];
892
893 /* Viewport and Layer are stored in the VUE header. We need to override
894 * them to zero if earlier stages didn't write them, as GL requires that
895 * they read back as zero when not explicitly set.
896 */
897 if (fs_attr == VARYING_SLOT_VIEWPORT || fs_attr == VARYING_SLOT_LAYER) {
898 attr->ComponentOverrideX = true;
899 attr->ComponentOverrideW = true;
900 attr->ConstantSource = CONST_0000;
901
902 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
903 attr->ComponentOverrideY = true;
904 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
905 attr->ComponentOverrideZ = true;
906
907 return;
908 }
909
910 /* If there was only a back color written but not front, use back
911 * as the color instead of undefined
912 */
913 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
914 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
915 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
916 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
917
918 if (slot == -1) {
919 /* This attribute does not exist in the VUE--that means that the vertex
920 * shader did not write to it. This means that either:
921 *
922 * (a) This attribute is a texture coordinate, and it is going to be
923 * replaced with point coordinates (as a consequence of a call to
924 * glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)), so the
925 * hardware will ignore whatever attribute override we supply.
926 *
927 * (b) This attribute is read by the fragment shader but not written by
928 * the vertex shader, so its value is undefined. Therefore the
929 * attribute override we supply doesn't matter.
930 *
931 * (c) This attribute is gl_PrimitiveID, and it wasn't written by the
932 * previous shader stage.
933 *
934 * Note that we don't have to worry about the cases where the attribute
935 * is gl_PointCoord or is undergoing point sprite coordinate
936 * replacement, because in those cases, this function isn't called.
937 *
938 * In case (c), we need to program the attribute overrides so that the
939 * primitive ID will be stored in this slot. In every other case, the
940 * attribute override we supply doesn't matter. So just go ahead and
941 * program primitive ID in every case.
942 */
943 attr->ComponentOverrideW = true;
944 attr->ComponentOverrideX = true;
945 attr->ComponentOverrideY = true;
946 attr->ComponentOverrideZ = true;
947 attr->ConstantSource = PRIM_ID;
948 return;
949 }
950
951 /* Compute the location of the attribute relative to urb_entry_read_offset.
952 * Each increment of urb_entry_read_offset represents a 256-bit value, so
953 * it counts for two 128-bit VUE slots.
954 */
955 int source_attr = slot - 2 * urb_entry_read_offset;
956 assert(source_attr >= 0 && source_attr < 32);
957
958 /* If we are doing two-sided color, and the VUE slot following this one
959 * represents a back-facing color, then we need to instruct the SF unit to
960 * do back-facing swizzling.
961 */
962 bool swizzling = two_side_color &&
963 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
964 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
965 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
966 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1));
967
968 /* Update max_source_attr. If swizzling, the SF will read this slot + 1. */
969 if (*max_source_attr < source_attr + swizzling)
970 *max_source_attr = source_attr + swizzling;
971
972 attr->SourceAttribute = source_attr;
973 if (swizzling)
974 attr->SwizzleSelect = INPUTATTR_FACING;
975 }
976
977
978 static void
979 genX(calculate_attr_overrides)(const struct brw_context *brw,
980 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr_overrides,
981 uint32_t *point_sprite_enables,
982 uint32_t *urb_entry_read_length,
983 uint32_t *urb_entry_read_offset)
984 {
985 const struct gl_context *ctx = &brw->ctx;
986
987 /* _NEW_POINT */
988 const struct gl_point_attrib *point = &ctx->Point;
989
990 /* BRW_NEW_FS_PROG_DATA */
991 const struct brw_wm_prog_data *wm_prog_data =
992 brw_wm_prog_data(brw->wm.base.prog_data);
993 uint32_t max_source_attr = 0;
994
995 *point_sprite_enables = 0;
996
997 /* BRW_NEW_FRAGMENT_PROGRAM
998 *
999 * If the fragment shader reads VARYING_SLOT_LAYER, then we need to pass in
1000 * the full vertex header. Otherwise, we can program the SF to start
1001 * reading at an offset of 1 (2 varying slots) to skip unnecessary data:
1002 * - VARYING_SLOT_PSIZ and BRW_VARYING_SLOT_NDC on gen4-5
1003 * - VARYING_SLOT_{PSIZ,LAYER} and VARYING_SLOT_POS on gen6+
1004 */
1005
1006 bool fs_needs_vue_header = brw->fragment_program->info.inputs_read &
1007 (VARYING_BIT_LAYER | VARYING_BIT_VIEWPORT);
1008
1009 *urb_entry_read_offset = fs_needs_vue_header ? 0 : 1;
1010
1011 /* From the Ivybridge PRM, Vol 2 Part 1, 3DSTATE_SBE,
1012 * description of dw10 Point Sprite Texture Coordinate Enable:
1013 *
1014 * "This field must be programmed to zero when non-point primitives
1015 * are rendered."
1016 *
1017 * The SandyBridge PRM doesn't explicitly say that point sprite enables
1018 * must be programmed to zero when rendering non-point primitives, but
1019 * the IvyBridge PRM does, and if we don't, we get garbage.
1020 *
1021 * This is not required on Haswell, as the hardware ignores this state
1022 * when drawing non-points -- although we do still need to be careful to
1023 * correctly set the attr overrides.
1024 *
1025 * _NEW_POLYGON
1026 * BRW_NEW_PRIMITIVE | BRW_NEW_GS_PROG_DATA | BRW_NEW_TES_PROG_DATA
1027 */
1028 bool drawing_points = brw_is_drawing_points(brw);
1029
1030 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1031 int input_index = wm_prog_data->urb_setup[attr];
1032
1033 if (input_index < 0)
1034 continue;
1035
1036 /* _NEW_POINT */
1037 bool point_sprite = false;
1038 if (drawing_points) {
1039 if (point->PointSprite &&
1040 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
1041 (point->CoordReplace & (1u << (attr - VARYING_SLOT_TEX0)))) {
1042 point_sprite = true;
1043 }
1044
1045 if (attr == VARYING_SLOT_PNTC)
1046 point_sprite = true;
1047
1048 if (point_sprite)
1049 *point_sprite_enables |= (1 << input_index);
1050 }
1051
1052 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
1053 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attribute = { 0 };
1054
1055 if (!point_sprite) {
1056 genX(get_attr_override)(&attribute,
1057 &brw->vue_map_geom_out,
1058 *urb_entry_read_offset, attr,
1059 brw->ctx.VertexProgram._TwoSideEnabled,
1060 &max_source_attr);
1061 }
1062
1063 /* The hardware can only do the overrides on 16 overrides at a
1064 * time, and the other up to 16 have to be lined up so that the
1065 * input index = the output index. We'll need to do some
1066 * tweaking to make sure that's the case.
1067 */
1068 if (input_index < 16)
1069 attr_overrides[input_index] = attribute;
1070 else
1071 assert(attribute.SourceAttribute == input_index);
1072 }
1073
1074 /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
1075 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
1076 *
1077 * "This field should be set to the minimum length required to read the
1078 * maximum source attribute. The maximum source attribute is indicated
1079 * by the maximum value of the enabled Attribute # Source Attribute if
1080 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
1081 * enable is not set.
1082 * read_length = ceiling((max_source_attr + 1) / 2)
1083 *
1084 * [errata] Corruption/Hang possible if length programmed larger than
1085 * recommended"
1086 *
1087 * Similar text exists for Ivy Bridge.
1088 */
1089 *urb_entry_read_length = DIV_ROUND_UP(max_source_attr + 1, 2);
1090 }
1091 #endif
1092
1093 /* ---------------------------------------------------------------------- */
1094
1095 #if GEN_GEN >= 6
1096 static void
1097 genX(upload_depth_stencil_state)(struct brw_context *brw)
1098 {
1099 struct gl_context *ctx = &brw->ctx;
1100
1101 /* _NEW_BUFFERS */
1102 struct intel_renderbuffer *depth_irb =
1103 intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
1104
1105 /* _NEW_DEPTH */
1106 struct gl_depthbuffer_attrib *depth = &ctx->Depth;
1107
1108 /* _NEW_STENCIL */
1109 struct gl_stencil_attrib *stencil = &ctx->Stencil;
1110 const int b = stencil->_BackFace;
1111
1112 #if GEN_GEN >= 8
1113 brw_batch_emit(brw, GENX(3DSTATE_WM_DEPTH_STENCIL), wmds) {
1114 #else
1115 uint32_t ds_offset;
1116 brw_state_emit(brw, GENX(DEPTH_STENCIL_STATE), 64, &ds_offset, wmds) {
1117 #endif
1118 if (depth->Test && depth_irb) {
1119 wmds.DepthTestEnable = true;
1120 wmds.DepthBufferWriteEnable = brw_depth_writes_enabled(brw);
1121 wmds.DepthTestFunction = intel_translate_compare_func(depth->Func);
1122 }
1123
1124 if (stencil->_Enabled) {
1125 wmds.StencilTestEnable = true;
1126 wmds.StencilWriteMask = stencil->WriteMask[0] & 0xff;
1127 wmds.StencilTestMask = stencil->ValueMask[0] & 0xff;
1128
1129 wmds.StencilTestFunction =
1130 intel_translate_compare_func(stencil->Function[0]);
1131 wmds.StencilFailOp =
1132 intel_translate_stencil_op(stencil->FailFunc[0]);
1133 wmds.StencilPassDepthPassOp =
1134 intel_translate_stencil_op(stencil->ZPassFunc[0]);
1135 wmds.StencilPassDepthFailOp =
1136 intel_translate_stencil_op(stencil->ZFailFunc[0]);
1137
1138 wmds.StencilBufferWriteEnable = stencil->_WriteEnabled;
1139
1140 if (stencil->_TestTwoSide) {
1141 wmds.DoubleSidedStencilEnable = true;
1142 wmds.BackfaceStencilWriteMask = stencil->WriteMask[b] & 0xff;
1143 wmds.BackfaceStencilTestMask = stencil->ValueMask[b] & 0xff;
1144
1145 wmds.BackfaceStencilTestFunction =
1146 intel_translate_compare_func(stencil->Function[b]);
1147 wmds.BackfaceStencilFailOp =
1148 intel_translate_stencil_op(stencil->FailFunc[b]);
1149 wmds.BackfaceStencilPassDepthPassOp =
1150 intel_translate_stencil_op(stencil->ZPassFunc[b]);
1151 wmds.BackfaceStencilPassDepthFailOp =
1152 intel_translate_stencil_op(stencil->ZFailFunc[b]);
1153 }
1154
1155 #if GEN_GEN >= 9
1156 wmds.StencilReferenceValue = _mesa_get_stencil_ref(ctx, 0);
1157 wmds.BackfaceStencilReferenceValue = _mesa_get_stencil_ref(ctx, b);
1158 #endif
1159 }
1160 }
1161
1162 #if GEN_GEN == 6
1163 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
1164 ptr.PointertoDEPTH_STENCIL_STATE = ds_offset;
1165 ptr.DEPTH_STENCIL_STATEChange = true;
1166 }
1167 #elif GEN_GEN == 7
1168 brw_batch_emit(brw, GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS), ptr) {
1169 ptr.PointertoDEPTH_STENCIL_STATE = ds_offset;
1170 }
1171 #endif
1172 }
1173
1174 static const struct brw_tracked_state genX(depth_stencil_state) = {
1175 .dirty = {
1176 .mesa = _NEW_BUFFERS |
1177 _NEW_DEPTH |
1178 _NEW_STENCIL,
1179 .brw = BRW_NEW_BLORP |
1180 (GEN_GEN >= 8 ? BRW_NEW_CONTEXT
1181 : BRW_NEW_BATCH |
1182 BRW_NEW_STATE_BASE_ADDRESS),
1183 },
1184 .emit = genX(upload_depth_stencil_state),
1185 };
1186 #endif
1187
1188 /* ---------------------------------------------------------------------- */
1189
1190 #if GEN_GEN >= 6
1191 static void
1192 genX(upload_clip_state)(struct brw_context *brw)
1193 {
1194 struct gl_context *ctx = &brw->ctx;
1195
1196 /* _NEW_BUFFERS */
1197 struct gl_framebuffer *fb = ctx->DrawBuffer;
1198
1199 /* BRW_NEW_FS_PROG_DATA */
1200 struct brw_wm_prog_data *wm_prog_data =
1201 brw_wm_prog_data(brw->wm.base.prog_data);
1202
1203 brw_batch_emit(brw, GENX(3DSTATE_CLIP), clip) {
1204 clip.StatisticsEnable = !brw->meta_in_progress;
1205
1206 if (wm_prog_data->barycentric_interp_modes &
1207 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
1208 clip.NonPerspectiveBarycentricEnable = true;
1209
1210 #if GEN_GEN >= 7
1211 clip.EarlyCullEnable = true;
1212 #endif
1213
1214 #if GEN_GEN == 7
1215 clip.FrontWinding = ctx->Polygon._FrontBit == _mesa_is_user_fbo(fb);
1216
1217 if (ctx->Polygon.CullFlag) {
1218 switch (ctx->Polygon.CullFaceMode) {
1219 case GL_FRONT:
1220 clip.CullMode = CULLMODE_FRONT;
1221 break;
1222 case GL_BACK:
1223 clip.CullMode = CULLMODE_BACK;
1224 break;
1225 case GL_FRONT_AND_BACK:
1226 clip.CullMode = CULLMODE_BOTH;
1227 break;
1228 default:
1229 unreachable("Should not get here: invalid CullFlag");
1230 }
1231 } else {
1232 clip.CullMode = CULLMODE_NONE;
1233 }
1234 #endif
1235
1236 #if GEN_GEN < 8
1237 clip.UserClipDistanceCullTestEnableBitmask =
1238 brw_vue_prog_data(brw->vs.base.prog_data)->cull_distance_mask;
1239
1240 clip.ViewportZClipTestEnable = !ctx->Transform.DepthClamp;
1241 #endif
1242
1243 /* _NEW_LIGHT */
1244 if (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION) {
1245 clip.TriangleStripListProvokingVertexSelect = 0;
1246 clip.TriangleFanProvokingVertexSelect = 1;
1247 clip.LineStripListProvokingVertexSelect = 0;
1248 } else {
1249 clip.TriangleStripListProvokingVertexSelect = 2;
1250 clip.TriangleFanProvokingVertexSelect = 2;
1251 clip.LineStripListProvokingVertexSelect = 1;
1252 }
1253
1254 /* _NEW_TRANSFORM */
1255 clip.UserClipDistanceClipTestEnableBitmask =
1256 ctx->Transform.ClipPlanesEnabled;
1257
1258 #if GEN_GEN >= 8
1259 clip.ForceUserClipDistanceClipTestEnableBitmask = true;
1260 #endif
1261
1262 if (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE)
1263 clip.APIMode = APIMODE_D3D;
1264 else
1265 clip.APIMode = APIMODE_OGL;
1266
1267 clip.GuardbandClipTestEnable = true;
1268
1269 /* BRW_NEW_VIEWPORT_COUNT */
1270 const unsigned viewport_count = brw->clip.viewport_count;
1271
1272 if (ctx->RasterDiscard) {
1273 clip.ClipMode = CLIPMODE_REJECT_ALL;
1274 #if GEN_GEN == 6
1275 perf_debug("Rasterizer discard is currently implemented via the "
1276 "clipper; having the GS not write primitives would "
1277 "likely be faster.\n");
1278 #endif
1279 } else {
1280 clip.ClipMode = CLIPMODE_NORMAL;
1281 }
1282
1283 clip.ClipEnable = brw->primitive != _3DPRIM_RECTLIST;
1284
1285 /* _NEW_POLYGON,
1286 * BRW_NEW_GEOMETRY_PROGRAM | BRW_NEW_TES_PROG_DATA | BRW_NEW_PRIMITIVE
1287 */
1288 if (!brw_is_drawing_points(brw) && !brw_is_drawing_lines(brw))
1289 clip.ViewportXYClipTestEnable = true;
1290
1291 clip.MinimumPointWidth = 0.125;
1292 clip.MaximumPointWidth = 255.875;
1293 clip.MaximumVPIndex = viewport_count - 1;
1294 if (_mesa_geometric_layers(fb) == 0)
1295 clip.ForceZeroRTAIndexEnable = true;
1296 }
1297 }
1298
1299 static const struct brw_tracked_state genX(clip_state) = {
1300 .dirty = {
1301 .mesa = _NEW_BUFFERS |
1302 _NEW_LIGHT |
1303 _NEW_POLYGON |
1304 _NEW_TRANSFORM,
1305 .brw = BRW_NEW_BLORP |
1306 BRW_NEW_CONTEXT |
1307 BRW_NEW_FS_PROG_DATA |
1308 BRW_NEW_GS_PROG_DATA |
1309 BRW_NEW_VS_PROG_DATA |
1310 BRW_NEW_META_IN_PROGRESS |
1311 BRW_NEW_PRIMITIVE |
1312 BRW_NEW_RASTERIZER_DISCARD |
1313 BRW_NEW_TES_PROG_DATA |
1314 BRW_NEW_VIEWPORT_COUNT,
1315 },
1316 .emit = genX(upload_clip_state),
1317 };
1318 #endif
1319
1320 /* ---------------------------------------------------------------------- */
1321
1322 #if GEN_GEN >= 6
1323 static void
1324 genX(upload_sf)(struct brw_context *brw)
1325 {
1326 struct gl_context *ctx = &brw->ctx;
1327 float point_size;
1328
1329 #if GEN_GEN <= 7
1330 /* _NEW_BUFFERS */
1331 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
1332 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
1333 #endif
1334
1335 brw_batch_emit(brw, GENX(3DSTATE_SF), sf) {
1336 sf.StatisticsEnable = true;
1337 sf.ViewportTransformEnable = brw->sf.viewport_transform_enable;
1338
1339 #if GEN_GEN == 7
1340 /* _NEW_BUFFERS */
1341 sf.DepthBufferSurfaceFormat = brw_depthbuffer_format(brw);
1342 #endif
1343
1344 #if GEN_GEN <= 7
1345 /* _NEW_POLYGON */
1346 sf.FrontWinding = ctx->Polygon._FrontBit == render_to_fbo;
1347 sf.GlobalDepthOffsetEnableSolid = ctx->Polygon.OffsetFill;
1348 sf.GlobalDepthOffsetEnableWireframe = ctx->Polygon.OffsetLine;
1349 sf.GlobalDepthOffsetEnablePoint = ctx->Polygon.OffsetPoint;
1350
1351 switch (ctx->Polygon.FrontMode) {
1352 case GL_FILL:
1353 sf.FrontFaceFillMode = FILL_MODE_SOLID;
1354 break;
1355 case GL_LINE:
1356 sf.FrontFaceFillMode = FILL_MODE_WIREFRAME;
1357 break;
1358 case GL_POINT:
1359 sf.FrontFaceFillMode = FILL_MODE_POINT;
1360 break;
1361 default:
1362 unreachable("not reached");
1363 }
1364
1365 switch (ctx->Polygon.BackMode) {
1366 case GL_FILL:
1367 sf.BackFaceFillMode = FILL_MODE_SOLID;
1368 break;
1369 case GL_LINE:
1370 sf.BackFaceFillMode = FILL_MODE_WIREFRAME;
1371 break;
1372 case GL_POINT:
1373 sf.BackFaceFillMode = FILL_MODE_POINT;
1374 break;
1375 default:
1376 unreachable("not reached");
1377 }
1378
1379 sf.ScissorRectangleEnable = true;
1380
1381 if (ctx->Polygon.CullFlag) {
1382 switch (ctx->Polygon.CullFaceMode) {
1383 case GL_FRONT:
1384 sf.CullMode = CULLMODE_FRONT;
1385 break;
1386 case GL_BACK:
1387 sf.CullMode = CULLMODE_BACK;
1388 break;
1389 case GL_FRONT_AND_BACK:
1390 sf.CullMode = CULLMODE_BOTH;
1391 break;
1392 default:
1393 unreachable("not reached");
1394 }
1395 } else {
1396 sf.CullMode = CULLMODE_NONE;
1397 }
1398
1399 #if GEN_IS_HASWELL
1400 sf.LineStippleEnable = ctx->Line.StippleFlag;
1401 #endif
1402
1403 if (multisampled_fbo && ctx->Multisample.Enabled)
1404 sf.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1405
1406 sf.GlobalDepthOffsetConstant = ctx->Polygon.OffsetUnits * 2;
1407 sf.GlobalDepthOffsetScale = ctx->Polygon.OffsetFactor;
1408 sf.GlobalDepthOffsetClamp = ctx->Polygon.OffsetClamp;
1409 #endif
1410
1411 /* _NEW_LINE */
1412 #if GEN_GEN == 8
1413 if (brw->is_cherryview)
1414 sf.CHVLineWidth = brw_get_line_width(brw);
1415 else
1416 sf.LineWidth = brw_get_line_width(brw);
1417 #else
1418 sf.LineWidth = brw_get_line_width(brw);
1419 #endif
1420
1421 if (ctx->Line.SmoothFlag) {
1422 sf.LineEndCapAntialiasingRegionWidth = _10pixels;
1423 #if GEN_GEN <= 7
1424 sf.AntiAliasingEnable = true;
1425 #endif
1426 }
1427
1428 /* _NEW_POINT - Clamp to ARB_point_parameters user limits */
1429 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
1430 /* Clamp to the hardware limits */
1431 sf.PointWidth = CLAMP(point_size, 0.125f, 255.875f);
1432
1433 /* _NEW_PROGRAM | _NEW_POINT, BRW_NEW_VUE_MAP_GEOM_OUT */
1434 if (use_state_point_size(brw))
1435 sf.PointWidthSource = State;
1436
1437 #if GEN_GEN >= 8
1438 /* _NEW_POINT | _NEW_MULTISAMPLE */
1439 if ((ctx->Point.SmoothFlag || _mesa_is_multisample_enabled(ctx)) &&
1440 !ctx->Point.PointSprite)
1441 sf.SmoothPointEnable = true;
1442 #endif
1443
1444 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
1445
1446 /* _NEW_LIGHT */
1447 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
1448 sf.TriangleStripListProvokingVertexSelect = 2;
1449 sf.TriangleFanProvokingVertexSelect = 2;
1450 sf.LineStripListProvokingVertexSelect = 1;
1451 } else {
1452 sf.TriangleFanProvokingVertexSelect = 1;
1453 }
1454
1455 #if GEN_GEN == 6
1456 /* BRW_NEW_FS_PROG_DATA */
1457 const struct brw_wm_prog_data *wm_prog_data =
1458 brw_wm_prog_data(brw->wm.base.prog_data);
1459
1460 sf.AttributeSwizzleEnable = true;
1461 sf.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1462
1463 /*
1464 * Window coordinates in an FBO are inverted, which means point
1465 * sprite origin must be inverted, too.
1466 */
1467 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
1468 sf.PointSpriteTextureCoordinateOrigin = LOWERLEFT;
1469 } else {
1470 sf.PointSpriteTextureCoordinateOrigin = UPPERLEFT;
1471 }
1472
1473 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
1474 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
1475 */
1476 uint32_t urb_entry_read_length;
1477 uint32_t urb_entry_read_offset;
1478 uint32_t point_sprite_enables;
1479 genX(calculate_attr_overrides)(brw, sf.Attribute, &point_sprite_enables,
1480 &urb_entry_read_length,
1481 &urb_entry_read_offset);
1482 sf.VertexURBEntryReadLength = urb_entry_read_length;
1483 sf.VertexURBEntryReadOffset = urb_entry_read_offset;
1484 sf.PointSpriteTextureCoordinateEnable = point_sprite_enables;
1485 sf.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
1486 #endif
1487 }
1488 }
1489
1490 static const struct brw_tracked_state genX(sf_state) = {
1491 .dirty = {
1492 .mesa = _NEW_LIGHT |
1493 _NEW_LINE |
1494 _NEW_MULTISAMPLE |
1495 _NEW_POINT |
1496 _NEW_PROGRAM |
1497 (GEN_GEN <= 7 ? _NEW_BUFFERS | _NEW_POLYGON : 0),
1498 .brw = BRW_NEW_BLORP |
1499 BRW_NEW_CONTEXT |
1500 BRW_NEW_VUE_MAP_GEOM_OUT |
1501 (GEN_GEN <= 7 ? BRW_NEW_GS_PROG_DATA |
1502 BRW_NEW_PRIMITIVE |
1503 BRW_NEW_TES_PROG_DATA
1504 : 0) |
1505 (GEN_GEN == 6 ? BRW_NEW_FS_PROG_DATA |
1506 BRW_NEW_FRAGMENT_PROGRAM
1507 : 0),
1508 },
1509 .emit = genX(upload_sf),
1510 };
1511 #endif
1512
1513 /* ---------------------------------------------------------------------- */
1514
1515 #if GEN_GEN >= 6
1516 static void
1517 genX(upload_wm)(struct brw_context *brw)
1518 {
1519 struct gl_context *ctx = &brw->ctx;
1520
1521 /* BRW_NEW_FS_PROG_DATA */
1522 const struct brw_wm_prog_data *wm_prog_data =
1523 brw_wm_prog_data(brw->wm.base.prog_data);
1524
1525 UNUSED bool writes_depth =
1526 wm_prog_data->computed_depth_mode != BRW_PSCDEPTH_OFF;
1527
1528 #if GEN_GEN < 7
1529 const struct brw_stage_state *stage_state = &brw->wm.base;
1530 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1531
1532 /* We can't fold this into gen6_upload_wm_push_constants(), because
1533 * according to the SNB PRM, vol 2 part 1 section 7.2.2
1534 * (3DSTATE_CONSTANT_PS [DevSNB]):
1535 *
1536 * "[DevSNB]: This packet must be followed by WM_STATE."
1537 */
1538 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_PS), wmcp) {
1539 if (wm_prog_data->base.nr_params != 0) {
1540 wmcp.Buffer0Valid = true;
1541 /* Pointer to the WM constant buffer. Covered by the set of
1542 * state flags from gen6_upload_wm_push_constants.
1543 */
1544 wmcp.PointertoPSConstantBuffer0 = stage_state->push_const_offset;
1545 wmcp.PSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
1546 }
1547 }
1548 #endif
1549
1550 brw_batch_emit(brw, GENX(3DSTATE_WM), wm) {
1551 wm.StatisticsEnable = true;
1552 wm.LineAntialiasingRegionWidth = _10pixels;
1553 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1554
1555 #if GEN_GEN < 7
1556 if (wm_prog_data->base.use_alt_mode)
1557 wm.FloatingPointMode = Alternate;
1558
1559 wm.SamplerCount = DIV_ROUND_UP(stage_state->sampler_count, 4);
1560 wm.BindingTableEntryCount = wm_prog_data->base.binding_table.size_bytes / 4;
1561 wm.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1562 wm._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1563 wm._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1564 wm.DispatchGRFStartRegisterForConstantSetupData0 =
1565 wm_prog_data->base.dispatch_grf_start_reg;
1566 wm.DispatchGRFStartRegisterForConstantSetupData2 =
1567 wm_prog_data->dispatch_grf_start_reg_2;
1568 wm.KernelStartPointer0 = stage_state->prog_offset;
1569 wm.KernelStartPointer2 = stage_state->prog_offset +
1570 wm_prog_data->prog_offset_2;
1571 wm.DualSourceBlendEnable =
1572 wm_prog_data->dual_src_blend && (ctx->Color.BlendEnabled & 1) &&
1573 ctx->Color.Blend[0]._UsesDualSrc;
1574 wm.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1575 wm.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1576
1577 /* From the SNB PRM, volume 2 part 1, page 281:
1578 * "If the PS kernel does not need the Position XY Offsets
1579 * to compute a Position XY value, then this field should be
1580 * programmed to POSOFFSET_NONE."
1581 *
1582 * "SW Recommendation: If the PS kernel needs the Position Offsets
1583 * to compute a Position XY value, this field should match Position
1584 * ZW Interpolation Mode to ensure a consistent position.xyzw
1585 * computation."
1586 * We only require XY sample offsets. So, this recommendation doesn't
1587 * look useful at the moment. We might need this in future.
1588 */
1589 if (wm_prog_data->uses_pos_offset)
1590 wm.PositionXYOffsetSelect = POSOFFSET_SAMPLE;
1591 else
1592 wm.PositionXYOffsetSelect = POSOFFSET_NONE;
1593
1594 if (wm_prog_data->base.total_scratch) {
1595 wm.ScratchSpaceBasePointer =
1596 render_bo(stage_state->scratch_bo,
1597 ffs(stage_state->per_thread_scratch) - 11);
1598 }
1599
1600 wm.PixelShaderComputedDepth = writes_depth;
1601 #endif
1602
1603 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1604
1605 /* _NEW_LINE */
1606 wm.LineStippleEnable = ctx->Line.StippleFlag;
1607
1608 /* _NEW_POLYGON */
1609 wm.PolygonStippleEnable = ctx->Polygon.StippleFlag;
1610 wm.BarycentricInterpolationMode = wm_prog_data->barycentric_interp_modes;
1611
1612 #if GEN_GEN < 8
1613 /* _NEW_BUFFERS */
1614 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
1615
1616 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
1617 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
1618 if (wm_prog_data->uses_kill ||
1619 _mesa_is_alpha_test_enabled(ctx) ||
1620 _mesa_is_alpha_to_coverage_enabled(ctx) ||
1621 wm_prog_data->uses_omask) {
1622 wm.PixelShaderKillsPixel = true;
1623 }
1624
1625 /* _NEW_BUFFERS | _NEW_COLOR */
1626 if (brw_color_buffer_write_enabled(brw) || writes_depth ||
1627 wm_prog_data->has_side_effects || wm.PixelShaderKillsPixel) {
1628 wm.ThreadDispatchEnable = true;
1629 }
1630 if (multisampled_fbo) {
1631 /* _NEW_MULTISAMPLE */
1632 if (ctx->Multisample.Enabled)
1633 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1634 else
1635 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1636
1637 if (wm_prog_data->persample_dispatch)
1638 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1639 else
1640 wm.MultisampleDispatchMode = MSDISPMODE_PERPIXEL;
1641 } else {
1642 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
1643 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
1644 }
1645
1646 #if GEN_GEN >= 7
1647 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
1648 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
1649 #endif
1650
1651 /* The "UAV access enable" bits are unnecessary on HSW because they only
1652 * seem to have an effect on the HW-assisted coherency mechanism which we
1653 * don't need, and the rasterization-related UAV_ONLY flag and the
1654 * DISPATCH_ENABLE bit can be set independently from it.
1655 * C.f. gen8_upload_ps_extra().
1656 *
1657 * BRW_NEW_FRAGMENT_PROGRAM | BRW_NEW_FS_PROG_DATA | _NEW_BUFFERS |
1658 * _NEW_COLOR
1659 */
1660 #if GEN_IS_HASWELL
1661 if (!(brw_color_buffer_write_enabled(brw) || writes_depth) &&
1662 wm_prog_data->has_side_effects)
1663 wm.PSUAVonly = ON;
1664 #endif
1665 #endif
1666
1667 #if GEN_GEN >= 7
1668 /* BRW_NEW_FS_PROG_DATA */
1669 if (wm_prog_data->early_fragment_tests)
1670 wm.EarlyDepthStencilControl = EDSC_PREPS;
1671 else if (wm_prog_data->has_side_effects)
1672 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
1673 #endif
1674 }
1675 }
1676
1677 static const struct brw_tracked_state genX(wm_state) = {
1678 .dirty = {
1679 .mesa = _NEW_LINE |
1680 _NEW_POLYGON |
1681 (GEN_GEN < 8 ? _NEW_BUFFERS |
1682 _NEW_COLOR |
1683 _NEW_MULTISAMPLE :
1684 0) |
1685 (GEN_GEN < 7 ? _NEW_PROGRAM_CONSTANTS : 0),
1686 .brw = BRW_NEW_BLORP |
1687 BRW_NEW_FS_PROG_DATA |
1688 (GEN_GEN < 7 ? BRW_NEW_PUSH_CONSTANT_ALLOCATION |
1689 BRW_NEW_BATCH
1690 : BRW_NEW_CONTEXT),
1691 },
1692 .emit = genX(upload_wm),
1693 };
1694 #endif
1695
1696 /* ---------------------------------------------------------------------- */
1697
1698 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
1699 pkt.KernelStartPointer = stage_state->prog_offset; \
1700 pkt.SamplerCount = \
1701 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
1702 pkt.BindingTableEntryCount = \
1703 stage_prog_data->binding_table.size_bytes / 4; \
1704 pkt.FloatingPointMode = stage_prog_data->use_alt_mode; \
1705 \
1706 if (stage_prog_data->total_scratch) { \
1707 pkt.ScratchSpaceBasePointer = \
1708 render_bo(stage_state->scratch_bo, 0); \
1709 pkt.PerThreadScratchSpace = \
1710 ffs(stage_state->per_thread_scratch) - 11; \
1711 } \
1712 \
1713 pkt.DispatchGRFStartRegisterForURBData = \
1714 stage_prog_data->dispatch_grf_start_reg; \
1715 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
1716 pkt.prefix##URBEntryReadOffset = 0; \
1717 \
1718 pkt.StatisticsEnable = true; \
1719 pkt.Enable = true;
1720
1721 #if GEN_GEN >= 6
1722 static void
1723 genX(upload_vs_state)(struct brw_context *brw)
1724 {
1725 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1726 const struct brw_stage_state *stage_state = &brw->vs.base;
1727
1728 /* BRW_NEW_VS_PROG_DATA */
1729 const struct brw_vue_prog_data *vue_prog_data =
1730 brw_vue_prog_data(brw->vs.base.prog_data);
1731 const struct brw_stage_prog_data *stage_prog_data = &vue_prog_data->base;
1732
1733 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8 ||
1734 vue_prog_data->dispatch_mode == DISPATCH_MODE_4X2_DUAL_OBJECT);
1735
1736 /* From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
1737 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
1738 *
1739 * [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
1740 * command that causes the VS Function Enable to toggle. Pipeline
1741 * flush can be executed by sending a PIPE_CONTROL command with CS
1742 * stall bit set and a post sync operation.
1743 *
1744 * We've already done such a flush at the start of state upload, so we
1745 * don't need to do another one here.
1746 */
1747
1748 #if GEN_GEN < 7
1749 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_VS), cvs) {
1750 if (stage_state->push_const_size != 0) {
1751 cvs.Buffer0Valid = true;
1752 cvs.PointertoVSConstantBuffer0 = stage_state->push_const_offset;
1753 cvs.VSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
1754 }
1755 }
1756 #endif
1757
1758 if (GEN_GEN == 7 && devinfo->is_ivybridge)
1759 gen7_emit_vs_workaround_flush(brw);
1760
1761 brw_batch_emit(brw, GENX(3DSTATE_VS), vs) {
1762 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
1763
1764 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1765
1766 #if GEN_GEN >= 8
1767 vs.SIMD8DispatchEnable =
1768 vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8;
1769
1770 vs.UserClipDistanceCullTestEnableBitmask =
1771 vue_prog_data->cull_distance_mask;
1772 #endif
1773 }
1774
1775 #if GEN_GEN < 7
1776 /* Based on my reading of the simulator, the VS constants don't get
1777 * pulled into the VS FF unit until an appropriate pipeline flush
1778 * happens, and instead the 3DSTATE_CONSTANT_VS packet just adds
1779 * references to them into a little FIFO. The flushes are common,
1780 * but don't reliably happen between this and a 3DPRIMITIVE, causing
1781 * the primitive to use the wrong constants. Then the FIFO
1782 * containing the constant setup gets added to again on the next
1783 * constants change, and eventually when a flush does happen the
1784 * unit is overwhelmed by constant changes and dies.
1785 *
1786 * To avoid this, send a PIPE_CONTROL down the line that will
1787 * update the unit immediately loading the constants. The flush
1788 * type bits here were those set by the STATE_BASE_ADDRESS whose
1789 * move in a82a43e8d99e1715dd11c9c091b5ab734079b6a6 triggered the
1790 * bug reports that led to this workaround, and may be more than
1791 * what is strictly required to avoid the issue.
1792 */
1793 brw_emit_pipe_control_flush(brw,
1794 PIPE_CONTROL_DEPTH_STALL |
1795 PIPE_CONTROL_INSTRUCTION_INVALIDATE |
1796 PIPE_CONTROL_STATE_CACHE_INVALIDATE);
1797 #endif
1798 }
1799
1800 static const struct brw_tracked_state genX(vs_state) = {
1801 .dirty = {
1802 .mesa = (GEN_GEN < 7 ? (_NEW_PROGRAM_CONSTANTS | _NEW_TRANSFORM) : 0),
1803 .brw = BRW_NEW_BATCH |
1804 BRW_NEW_BLORP |
1805 BRW_NEW_CONTEXT |
1806 BRW_NEW_VS_PROG_DATA |
1807 (GEN_GEN < 7 ? BRW_NEW_PUSH_CONSTANT_ALLOCATION |
1808 BRW_NEW_VERTEX_PROGRAM
1809 : 0),
1810 },
1811 .emit = genX(upload_vs_state),
1812 };
1813 #endif
1814
1815 /* ---------------------------------------------------------------------- */
1816
1817 #if GEN_GEN >= 6
1818 static void
1819 brw_calculate_guardband_size(const struct gen_device_info *devinfo,
1820 uint32_t fb_width, uint32_t fb_height,
1821 float m00, float m11, float m30, float m31,
1822 float *xmin, float *xmax,
1823 float *ymin, float *ymax)
1824 {
1825 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1826 * Strips and Fans documentation:
1827 *
1828 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1829 * fixed-point "guardband" range supported by the rasterization hardware"
1830 *
1831 * and
1832 *
1833 * "In almost all circumstances, if an object’s vertices are actually
1834 * modified by this clamping (i.e., had X or Y coordinates outside of
1835 * the guardband extent the rendered object will not match the intended
1836 * result. Therefore software should take steps to ensure that this does
1837 * not happen - e.g., by clipping objects such that they do not exceed
1838 * these limits after the Drawing Rectangle is applied."
1839 *
1840 * I believe the fundamental restriction is that the rasterizer (in
1841 * the SF/WM stages) have a limit on the number of pixels that can be
1842 * rasterized. We need to ensure any coordinates beyond the rasterizer
1843 * limit are handled by the clipper. So effectively that limit becomes
1844 * the clipper's guardband size.
1845 *
1846 * It goes on to say:
1847 *
1848 * "In addition, in order to be correctly rendered, objects must have a
1849 * screenspace bounding box not exceeding 8K in the X or Y direction.
1850 * This additional restriction must also be comprehended by software,
1851 * i.e., enforced by use of clipping."
1852 *
1853 * This makes no sense. Gen7+ hardware supports 16K render targets,
1854 * and you definitely need to be able to draw polygons that fill the
1855 * surface. Our assumption is that the rasterizer was limited to 8K
1856 * on Sandybridge, which only supports 8K surfaces, and it was actually
1857 * increased to 16K on Ivybridge and later.
1858 *
1859 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1860 */
1861 const float gb_size = devinfo->gen >= 7 ? 16384.0f : 8192.0f;
1862
1863 if (m00 != 0 && m11 != 0) {
1864 /* First, we compute the screen-space render area */
1865 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1866 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1867 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1868 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1869
1870 /* We want the guardband to be centered on that */
1871 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1872 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1873 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1874 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1875
1876 /* Now we need it in native device coordinates */
1877 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1878 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1879 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1880 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1881
1882 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1883 * flipped upside-down. X should be fine though.
1884 */
1885 assert(ndc_gb_xmin <= ndc_gb_xmax);
1886 *xmin = ndc_gb_xmin;
1887 *xmax = ndc_gb_xmax;
1888 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1889 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1890 } else {
1891 /* The viewport scales to 0, so nothing will be rendered. */
1892 *xmin = 0.0f;
1893 *xmax = 0.0f;
1894 *ymin = 0.0f;
1895 *ymax = 0.0f;
1896 }
1897 }
1898
1899 static void
1900 genX(upload_sf_clip_viewport)(struct brw_context *brw)
1901 {
1902 struct gl_context *ctx = &brw->ctx;
1903 float y_scale, y_bias;
1904 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1905
1906 /* BRW_NEW_VIEWPORT_COUNT */
1907 const unsigned viewport_count = brw->clip.viewport_count;
1908
1909 /* _NEW_BUFFERS */
1910 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
1911 const uint32_t fb_width = (float)_mesa_geometric_width(ctx->DrawBuffer);
1912 const uint32_t fb_height = (float)_mesa_geometric_height(ctx->DrawBuffer);
1913
1914 #if GEN_GEN >= 7
1915 #define clv sfv
1916 struct GENX(SF_CLIP_VIEWPORT) sfv;
1917 uint32_t sf_clip_vp_offset;
1918 uint32_t *sf_clip_map = brw_state_batch(brw, 16 * 4 * viewport_count,
1919 64, &sf_clip_vp_offset);
1920 #else
1921 struct GENX(SF_VIEWPORT) sfv;
1922 struct GENX(CLIP_VIEWPORT) clv;
1923 uint32_t *sf_map = brw_state_batch(brw, 8 * 4 * viewport_count,
1924 32, &brw->sf.vp_offset);
1925 uint32_t *clip_map = brw_state_batch(brw, 4 * 4 * viewport_count,
1926 32, &brw->clip.vp_offset);
1927 #endif
1928
1929 /* _NEW_BUFFERS */
1930 if (render_to_fbo) {
1931 y_scale = 1.0;
1932 y_bias = 0;
1933 } else {
1934 y_scale = -1.0;
1935 y_bias = (float)fb_height;
1936 }
1937
1938 for (unsigned i = 0; i < brw->clip.viewport_count; i++) {
1939 /* _NEW_VIEWPORT: Guardband Clipping */
1940 float scale[3], translate[3], gb_xmin, gb_xmax, gb_ymin, gb_ymax;
1941 _mesa_get_viewport_xform(ctx, i, scale, translate);
1942
1943 sfv.ViewportMatrixElementm00 = scale[0];
1944 sfv.ViewportMatrixElementm11 = scale[1] * y_scale,
1945 sfv.ViewportMatrixElementm22 = scale[2],
1946 sfv.ViewportMatrixElementm30 = translate[0],
1947 sfv.ViewportMatrixElementm31 = translate[1] * y_scale + y_bias,
1948 sfv.ViewportMatrixElementm32 = translate[2],
1949 brw_calculate_guardband_size(devinfo, fb_width, fb_height,
1950 sfv.ViewportMatrixElementm00,
1951 sfv.ViewportMatrixElementm11,
1952 sfv.ViewportMatrixElementm30,
1953 sfv.ViewportMatrixElementm31,
1954 &gb_xmin, &gb_xmax, &gb_ymin, &gb_ymax);
1955
1956
1957 clv.XMinClipGuardband = gb_xmin;
1958 clv.XMaxClipGuardband = gb_xmax;
1959 clv.YMinClipGuardband = gb_ymin;
1960 clv.YMaxClipGuardband = gb_ymax;
1961
1962 #if GEN_GEN >= 8
1963 /* _NEW_VIEWPORT | _NEW_BUFFERS: Screen Space Viewport
1964 * The hardware will take the intersection of the drawing rectangle,
1965 * scissor rectangle, and the viewport extents. We don't need to be
1966 * smart, and can therefore just program the viewport extents.
1967 */
1968 const float viewport_Xmax =
1969 ctx->ViewportArray[i].X + ctx->ViewportArray[i].Width;
1970 const float viewport_Ymax =
1971 ctx->ViewportArray[i].Y + ctx->ViewportArray[i].Height;
1972
1973 if (render_to_fbo) {
1974 sfv.XMinViewPort = ctx->ViewportArray[i].X;
1975 sfv.XMaxViewPort = viewport_Xmax - 1;
1976 sfv.YMinViewPort = ctx->ViewportArray[i].Y;
1977 sfv.YMaxViewPort = viewport_Ymax - 1;
1978 } else {
1979 sfv.XMinViewPort = ctx->ViewportArray[i].X;
1980 sfv.XMaxViewPort = viewport_Xmax - 1;
1981 sfv.YMinViewPort = fb_height - viewport_Ymax;
1982 sfv.YMaxViewPort = fb_height - ctx->ViewportArray[i].Y - 1;
1983 }
1984 #endif
1985
1986 #if GEN_GEN >= 7
1987 GENX(SF_CLIP_VIEWPORT_pack)(NULL, sf_clip_map, &sfv);
1988 sf_clip_map += 16;
1989 #else
1990 GENX(SF_VIEWPORT_pack)(NULL, sf_map, &sfv);
1991 GENX(CLIP_VIEWPORT_pack)(NULL, clip_map, &clv);
1992 sf_map += 8;
1993 clip_map += 4;
1994 #endif
1995 }
1996
1997 #if GEN_GEN >= 7
1998 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
1999 ptr.SFClipViewportPointer = sf_clip_vp_offset;
2000 }
2001 #else
2002 brw->ctx.NewDriverState |= BRW_NEW_SF_VP | BRW_NEW_CLIP_VP;
2003 #endif
2004 }
2005
2006 static const struct brw_tracked_state genX(sf_clip_viewport) = {
2007 .dirty = {
2008 .mesa = _NEW_BUFFERS |
2009 _NEW_VIEWPORT,
2010 .brw = BRW_NEW_BATCH |
2011 BRW_NEW_BLORP |
2012 BRW_NEW_VIEWPORT_COUNT,
2013 },
2014 .emit = genX(upload_sf_clip_viewport),
2015 };
2016 #endif
2017
2018 /* ---------------------------------------------------------------------- */
2019
2020 #if GEN_GEN >= 6
2021 static void
2022 genX(upload_gs_state)(struct brw_context *brw)
2023 {
2024 const struct gen_device_info *devinfo = &brw->screen->devinfo;
2025 const struct brw_stage_state *stage_state = &brw->gs.base;
2026 /* BRW_NEW_GEOMETRY_PROGRAM */
2027 bool active = brw->geometry_program;
2028
2029 /* BRW_NEW_GS_PROG_DATA */
2030 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
2031 const struct brw_vue_prog_data *vue_prog_data =
2032 brw_vue_prog_data(stage_prog_data);
2033 #if GEN_GEN >= 7
2034 const struct brw_gs_prog_data *gs_prog_data =
2035 brw_gs_prog_data(stage_prog_data);
2036 #endif
2037
2038 #if GEN_GEN < 7
2039 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_GS), cgs) {
2040 if (active && stage_state->push_const_size != 0) {
2041 cgs.Buffer0Valid = true;
2042 cgs.PointertoGSConstantBuffer0 = stage_state->push_const_offset;
2043 cgs.GSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
2044 }
2045 }
2046 #endif
2047
2048 #if GEN_GEN == 7 && !GEN_IS_HASWELL
2049 /**
2050 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
2051 * Geometry > Geometry Shader > State:
2052 *
2053 * "Note: Because of corruption in IVB:GT2, software needs to flush the
2054 * whole fixed function pipeline when the GS enable changes value in
2055 * the 3DSTATE_GS."
2056 *
2057 * The hardware architects have clarified that in this context "flush the
2058 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
2059 * Stall" bit set.
2060 */
2061 if (brw->gt == 2 && brw->gs.enabled != active)
2062 gen7_emit_cs_stall_flush(brw);
2063 #endif
2064
2065 if (active) {
2066 brw_batch_emit(brw, GENX(3DSTATE_GS), gs) {
2067 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
2068
2069 #if GEN_GEN >= 7
2070 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
2071 gs.OutputTopology = gs_prog_data->output_topology;
2072 gs.ControlDataHeaderSize =
2073 gs_prog_data->control_data_header_size_hwords;
2074
2075 gs.InstanceControl = gs_prog_data->invocations - 1;
2076 gs.DispatchMode = vue_prog_data->dispatch_mode;
2077
2078 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
2079
2080 gs.ControlDataFormat = gs_prog_data->control_data_format;
2081 #endif
2082
2083 /* Note: the meaning of the GEN7_GS_REORDER_TRAILING bit changes between
2084 * Ivy Bridge and Haswell.
2085 *
2086 * On Ivy Bridge, setting this bit causes the vertices of a triangle
2087 * strip to be delivered to the geometry shader in an order that does
2088 * not strictly follow the OpenGL spec, but preserves triangle
2089 * orientation. For example, if the vertices are (1, 2, 3, 4, 5), then
2090 * the geometry shader sees triangles:
2091 *
2092 * (1, 2, 3), (2, 4, 3), (3, 4, 5)
2093 *
2094 * (Clearing the bit is even worse, because it fails to preserve
2095 * orientation).
2096 *
2097 * Triangle strips with adjacency always ordered in a way that preserves
2098 * triangle orientation but does not strictly follow the OpenGL spec,
2099 * regardless of the setting of this bit.
2100 *
2101 * On Haswell, both triangle strips and triangle strips with adjacency
2102 * are always ordered in a way that preserves triangle orientation.
2103 * Setting this bit causes the ordering to strictly follow the OpenGL
2104 * spec.
2105 *
2106 * So in either case we want to set the bit. Unfortunately on Ivy
2107 * Bridge this will get the order close to correct but not perfect.
2108 */
2109 gs.ReorderMode = TRAILING;
2110 gs.MaximumNumberofThreads =
2111 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
2112 : (devinfo->max_gs_threads - 1);
2113
2114 #if GEN_GEN < 7
2115 gs.SOStatisticsEnable = true;
2116 gs.RenderingEnabled = 1;
2117 if (brw->geometry_program->info.has_transform_feedback_varyings)
2118 gs.SVBIPayloadEnable = true;
2119
2120 /* GEN6_GS_SPF_MODE and GEN6_GS_VECTOR_MASK_ENABLE are enabled as it
2121 * was previously done for gen6.
2122 *
2123 * TODO: test with both disabled to see if the HW is behaving
2124 * as expected, like in gen7.
2125 */
2126 gs.SingleProgramFlow = true;
2127 gs.VectorMaskEnable = true;
2128 #endif
2129
2130 #if GEN_GEN >= 8
2131 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
2132
2133 if (gs_prog_data->static_vertex_count != -1) {
2134 gs.StaticOutput = true;
2135 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
2136 }
2137 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
2138
2139 gs.UserClipDistanceCullTestEnableBitmask =
2140 vue_prog_data->cull_distance_mask;
2141
2142 const int urb_entry_write_offset = 1;
2143 const uint32_t urb_entry_output_length =
2144 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
2145 urb_entry_write_offset;
2146
2147 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
2148 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
2149 #endif
2150 }
2151 #if GEN_GEN < 7
2152 } else if (brw->ff_gs.prog_active) {
2153 /* In gen6, transform feedback for the VS stage is done with an ad-hoc GS
2154 * program. This function provides the needed 3DSTATE_GS for this.
2155 */
2156 upload_gs_state_for_tf(brw);
2157 #endif
2158 } else {
2159 brw_batch_emit(brw, GENX(3DSTATE_GS), gs) {
2160 gs.StatisticsEnable = true;
2161 #if GEN_GEN < 7
2162 gs.RenderingEnabled = true;
2163 #endif
2164
2165 #if GEN_GEN < 8
2166 gs.DispatchGRFStartRegisterForURBData = 1;
2167 #if GEN_GEN >= 7
2168 gs.IncludeVertexHandles = true;
2169 #endif
2170 #endif
2171 }
2172 }
2173 #if GEN_GEN < 7
2174 brw->gs.enabled = active;
2175 #endif
2176 }
2177
2178 static const struct brw_tracked_state genX(gs_state) = {
2179 .dirty = {
2180 .mesa = (GEN_GEN < 7 ? _NEW_PROGRAM_CONSTANTS : 0),
2181 .brw = BRW_NEW_BATCH |
2182 BRW_NEW_BLORP |
2183 BRW_NEW_CONTEXT |
2184 BRW_NEW_GEOMETRY_PROGRAM |
2185 BRW_NEW_GS_PROG_DATA |
2186 (GEN_GEN < 7 ? BRW_NEW_FF_GS_PROG_DATA |
2187 BRW_NEW_PUSH_CONSTANT_ALLOCATION
2188 : 0),
2189 },
2190 .emit = genX(upload_gs_state),
2191 };
2192 #endif
2193
2194 /* ---------------------------------------------------------------------- */
2195
2196 #define blend_factor(x) brw_translate_blend_factor(x)
2197 #define blend_eqn(x) brw_translate_blend_equation(x)
2198
2199 #if GEN_GEN >= 6
2200 static void
2201 genX(upload_blend_state)(struct brw_context *brw)
2202 {
2203 struct gl_context *ctx = &brw->ctx;
2204 int size;
2205
2206 /* We need at least one BLEND_STATE written, because we might do
2207 * thread dispatch even if _NumColorDrawBuffers is 0 (for example
2208 * for computed depth or alpha test), which will do an FB write
2209 * with render target 0, which will reference BLEND_STATE[0] for
2210 * alpha test enable.
2211 */
2212 int nr_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
2213 if (nr_draw_buffers == 0 && ctx->Color.AlphaEnabled)
2214 nr_draw_buffers = 1;
2215
2216 size = GENX(BLEND_STATE_ENTRY_length) * 4 * nr_draw_buffers;
2217 #if GEN_GEN >= 8
2218 size += GENX(BLEND_STATE_length) * 4;
2219 #endif
2220
2221 uint32_t *blend_map;
2222 blend_map = brw_state_batch(brw, size, 64, &brw->cc.blend_state_offset);
2223
2224 #if GEN_GEN >= 8
2225 struct GENX(BLEND_STATE) blend = { 0 };
2226 {
2227 #else
2228 for (int i = 0; i < nr_draw_buffers; i++) {
2229 struct GENX(BLEND_STATE_ENTRY) entry = { 0 };
2230 #define blend entry
2231 #endif
2232 /* OpenGL specification 3.3 (page 196), section 4.1.3 says:
2233 * "If drawbuffer zero is not NONE and the buffer it references has an
2234 * integer format, the SAMPLE_ALPHA_TO_COVERAGE and SAMPLE_ALPHA_TO_ONE
2235 * operations are skipped."
2236 */
2237 if (!(ctx->DrawBuffer->_IntegerBuffers & 0x1)) {
2238 /* _NEW_MULTISAMPLE */
2239 if (_mesa_is_multisample_enabled(ctx)) {
2240 if (ctx->Multisample.SampleAlphaToCoverage) {
2241 blend.AlphaToCoverageEnable = true;
2242 blend.AlphaToCoverageDitherEnable = GEN_GEN >= 7;
2243 }
2244 if (ctx->Multisample.SampleAlphaToOne)
2245 blend.AlphaToOneEnable = true;
2246 }
2247
2248 /* _NEW_COLOR */
2249 if (ctx->Color.AlphaEnabled) {
2250 blend.AlphaTestEnable = true;
2251 blend.AlphaTestFunction =
2252 intel_translate_compare_func(ctx->Color.AlphaFunc);
2253 }
2254
2255 if (ctx->Color.DitherFlag) {
2256 blend.ColorDitherEnable = true;
2257 }
2258 }
2259
2260 #if GEN_GEN >= 8
2261 for (int i = 0; i < nr_draw_buffers; i++) {
2262 struct GENX(BLEND_STATE_ENTRY) entry = { 0 };
2263 #else
2264 {
2265 #endif
2266
2267 /* _NEW_BUFFERS */
2268 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2269
2270 /* Used for implementing the following bit of GL_EXT_texture_integer:
2271 * "Per-fragment operations that require floating-point color
2272 * components, including multisample alpha operations, alpha test,
2273 * blending, and dithering, have no effect when the corresponding
2274 * colors are written to an integer color buffer."
2275 */
2276 bool integer = ctx->DrawBuffer->_IntegerBuffers & (0x1 << i);
2277
2278 /* _NEW_COLOR */
2279 if (ctx->Color.ColorLogicOpEnabled) {
2280 GLenum rb_type = rb ? _mesa_get_format_datatype(rb->Format)
2281 : GL_UNSIGNED_NORMALIZED;
2282 WARN_ONCE(ctx->Color.LogicOp != GL_COPY &&
2283 rb_type != GL_UNSIGNED_NORMALIZED &&
2284 rb_type != GL_FLOAT, "Ignoring %s logic op on %s "
2285 "renderbuffer\n",
2286 _mesa_enum_to_string(ctx->Color.LogicOp),
2287 _mesa_enum_to_string(rb_type));
2288 if (GEN_GEN >= 8 || rb_type == GL_UNSIGNED_NORMALIZED) {
2289 entry.LogicOpEnable = true;
2290 entry.LogicOpFunction =
2291 intel_translate_logic_op(ctx->Color.LogicOp);
2292 }
2293 } else if (ctx->Color.BlendEnabled & (1 << i) && !integer &&
2294 !ctx->Color._AdvancedBlendMode) {
2295 GLenum eqRGB = ctx->Color.Blend[i].EquationRGB;
2296 GLenum eqA = ctx->Color.Blend[i].EquationA;
2297 GLenum srcRGB = ctx->Color.Blend[i].SrcRGB;
2298 GLenum dstRGB = ctx->Color.Blend[i].DstRGB;
2299 GLenum srcA = ctx->Color.Blend[i].SrcA;
2300 GLenum dstA = ctx->Color.Blend[i].DstA;
2301
2302 if (eqRGB == GL_MIN || eqRGB == GL_MAX)
2303 srcRGB = dstRGB = GL_ONE;
2304
2305 if (eqA == GL_MIN || eqA == GL_MAX)
2306 srcA = dstA = GL_ONE;
2307
2308 /* Due to hardware limitations, the destination may have information
2309 * in an alpha channel even when the format specifies no alpha
2310 * channel. In order to avoid getting any incorrect blending due to
2311 * that alpha channel, coerce the blend factors to values that will
2312 * not read the alpha channel, but will instead use the correct
2313 * implicit value for alpha.
2314 */
2315 if (rb && !_mesa_base_format_has_channel(rb->_BaseFormat,
2316 GL_TEXTURE_ALPHA_TYPE)) {
2317 srcRGB = brw_fix_xRGB_alpha(srcRGB);
2318 srcA = brw_fix_xRGB_alpha(srcA);
2319 dstRGB = brw_fix_xRGB_alpha(dstRGB);
2320 dstA = brw_fix_xRGB_alpha(dstA);
2321 }
2322
2323 entry.ColorBufferBlendEnable = true;
2324 entry.DestinationBlendFactor = blend_factor(dstRGB);
2325 entry.SourceBlendFactor = blend_factor(srcRGB);
2326 entry.DestinationAlphaBlendFactor = blend_factor(dstA);
2327 entry.SourceAlphaBlendFactor = blend_factor(srcA);
2328 entry.ColorBlendFunction = blend_eqn(eqRGB);
2329 entry.AlphaBlendFunction = blend_eqn(eqA);
2330
2331 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB)
2332 blend.IndependentAlphaBlendEnable = true;
2333 }
2334
2335 /* See section 8.1.6 "Pre-Blend Color Clamping" of the
2336 * SandyBridge PRM Volume 2 Part 1 for HW requirements.
2337 *
2338 * We do our ARB_color_buffer_float CLAMP_FRAGMENT_COLOR
2339 * clamping in the fragment shader. For its clamping of
2340 * blending, the spec says:
2341 *
2342 * "RESOLVED: For fixed-point color buffers, the inputs and
2343 * the result of the blending equation are clamped. For
2344 * floating-point color buffers, no clamping occurs."
2345 *
2346 * So, generally, we want clamping to the render target's range.
2347 * And, good news, the hardware tables for both pre- and
2348 * post-blend color clamping are either ignored, or any are
2349 * allowed, or clamping is required but RT range clamping is a
2350 * valid option.
2351 */
2352 entry.PreBlendColorClampEnable = true;
2353 entry.PostBlendColorClampEnable = true;
2354 entry.ColorClampRange = COLORCLAMP_RTFORMAT;
2355
2356 entry.WriteDisableRed = !ctx->Color.ColorMask[i][0];
2357 entry.WriteDisableGreen = !ctx->Color.ColorMask[i][1];
2358 entry.WriteDisableBlue = !ctx->Color.ColorMask[i][2];
2359 entry.WriteDisableAlpha = !ctx->Color.ColorMask[i][3];
2360
2361 /* From the BLEND_STATE docs, DWord 0, Bit 29 (AlphaToOne Enable):
2362 * "If Dual Source Blending is enabled, this bit must be disabled."
2363 */
2364 WARN_ONCE(ctx->Color.Blend[i]._UsesDualSrc &&
2365 _mesa_is_multisample_enabled(ctx) &&
2366 ctx->Multisample.SampleAlphaToOne,
2367 "HW workaround: disabling alpha to one with dual src "
2368 "blending\n");
2369 if (ctx->Color.Blend[i]._UsesDualSrc)
2370 blend.AlphaToOneEnable = false;
2371 #if GEN_GEN >= 8
2372 GENX(BLEND_STATE_ENTRY_pack)(NULL, &blend_map[1 + i * 2], &entry);
2373 #else
2374 GENX(BLEND_STATE_ENTRY_pack)(NULL, &blend_map[i * 2], &entry);
2375 #endif
2376 }
2377 }
2378
2379 #if GEN_GEN >= 8
2380 GENX(BLEND_STATE_pack)(NULL, blend_map, &blend);
2381 #endif
2382
2383 #if GEN_GEN < 7
2384 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2385 ptr.PointertoBLEND_STATE = brw->cc.blend_state_offset;
2386 ptr.BLEND_STATEChange = true;
2387 }
2388 #else
2389 brw_batch_emit(brw, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
2390 ptr.BlendStatePointer = brw->cc.blend_state_offset;
2391 #if GEN_GEN >= 8
2392 ptr.BlendStatePointerValid = true;
2393 #endif
2394 }
2395 #endif
2396 }
2397
2398 static const struct brw_tracked_state genX(blend_state) = {
2399 .dirty = {
2400 .mesa = _NEW_BUFFERS |
2401 _NEW_COLOR |
2402 _NEW_MULTISAMPLE,
2403 .brw = BRW_NEW_BATCH |
2404 BRW_NEW_BLORP |
2405 BRW_NEW_STATE_BASE_ADDRESS,
2406 },
2407 .emit = genX(upload_blend_state),
2408 };
2409 #endif
2410
2411 /* ---------------------------------------------------------------------- */
2412
2413 #if GEN_GEN >= 6
2414 static void
2415 genX(upload_scissor_state)(struct brw_context *brw)
2416 {
2417 struct gl_context *ctx = &brw->ctx;
2418 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
2419 struct GENX(SCISSOR_RECT) scissor;
2420 uint32_t scissor_state_offset;
2421 const unsigned int fb_width = _mesa_geometric_width(ctx->DrawBuffer);
2422 const unsigned int fb_height = _mesa_geometric_height(ctx->DrawBuffer);
2423 uint32_t *scissor_map;
2424
2425 /* BRW_NEW_VIEWPORT_COUNT */
2426 const unsigned viewport_count = brw->clip.viewport_count;
2427
2428 scissor_map = brw_state_batch(
2429 brw, GENX(SCISSOR_RECT_length) * sizeof(uint32_t) * viewport_count,
2430 32, &scissor_state_offset);
2431
2432 /* _NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT */
2433
2434 /* The scissor only needs to handle the intersection of drawable and
2435 * scissor rect. Clipping to the boundaries of static shared buffers
2436 * for front/back/depth is covered by looping over cliprects in brw_draw.c.
2437 *
2438 * Note that the hardware's coordinates are inclusive, while Mesa's min is
2439 * inclusive but max is exclusive.
2440 */
2441 for (unsigned i = 0; i < viewport_count; i++) {
2442 int bbox[4];
2443
2444 bbox[0] = MAX2(ctx->ViewportArray[i].X, 0);
2445 bbox[1] = MIN2(bbox[0] + ctx->ViewportArray[i].Width, fb_width);
2446 bbox[2] = MAX2(ctx->ViewportArray[i].Y, 0);
2447 bbox[3] = MIN2(bbox[2] + ctx->ViewportArray[i].Height, fb_height);
2448 _mesa_intersect_scissor_bounding_box(ctx, i, bbox);
2449
2450 if (bbox[0] == bbox[1] || bbox[2] == bbox[3]) {
2451 /* If the scissor was out of bounds and got clamped to 0 width/height
2452 * at the bounds, the subtraction of 1 from maximums could produce a
2453 * negative number and thus not clip anything. Instead, just provide
2454 * a min > max scissor inside the bounds, which produces the expected
2455 * no rendering.
2456 */
2457 scissor.ScissorRectangleXMin = 1;
2458 scissor.ScissorRectangleXMax = 0;
2459 scissor.ScissorRectangleYMin = 1;
2460 scissor.ScissorRectangleYMax = 0;
2461 } else if (render_to_fbo) {
2462 /* texmemory: Y=0=bottom */
2463 scissor.ScissorRectangleXMin = bbox[0];
2464 scissor.ScissorRectangleXMax = bbox[1] - 1;
2465 scissor.ScissorRectangleYMin = bbox[2];
2466 scissor.ScissorRectangleYMax = bbox[3] - 1;
2467 } else {
2468 /* memory: Y=0=top */
2469 scissor.ScissorRectangleXMin = bbox[0];
2470 scissor.ScissorRectangleXMax = bbox[1] - 1;
2471 scissor.ScissorRectangleYMin = fb_height - bbox[3];
2472 scissor.ScissorRectangleYMax = fb_height - bbox[2] - 1;
2473 }
2474
2475 GENX(SCISSOR_RECT_pack)(
2476 NULL, scissor_map + i * GENX(SCISSOR_RECT_length), &scissor);
2477 }
2478
2479 brw_batch_emit(brw, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
2480 ptr.ScissorRectPointer = scissor_state_offset;
2481 }
2482 }
2483
2484 static const struct brw_tracked_state genX(scissor_state) = {
2485 .dirty = {
2486 .mesa = _NEW_BUFFERS |
2487 _NEW_SCISSOR |
2488 _NEW_VIEWPORT,
2489 .brw = BRW_NEW_BATCH |
2490 BRW_NEW_BLORP |
2491 BRW_NEW_VIEWPORT_COUNT,
2492 },
2493 .emit = genX(upload_scissor_state),
2494 };
2495 #endif
2496
2497 /* ---------------------------------------------------------------------- */
2498
2499 #if GEN_GEN >= 7
2500 UNUSED static const uint32_t push_constant_opcodes[] = {
2501 [MESA_SHADER_VERTEX] = 21,
2502 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2503 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2504 [MESA_SHADER_GEOMETRY] = 22,
2505 [MESA_SHADER_FRAGMENT] = 23,
2506 [MESA_SHADER_COMPUTE] = 0,
2507 };
2508
2509 static void
2510 upload_constant_state(struct brw_context *brw,
2511 struct brw_stage_state *stage_state,
2512 bool active, uint32_t stage)
2513 {
2514 UNUSED uint32_t mocs = GEN_GEN < 8 ? GEN7_MOCS_L3 : 0;
2515 active = active && stage_state->push_const_size != 0;
2516
2517 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_VS), pkt) {
2518 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
2519 if (active) {
2520 #if GEN_GEN >= 9
2521 pkt.ConstantBody.ConstantBuffer2ReadLength =
2522 stage_state->push_const_size;
2523 pkt.ConstantBody.PointerToConstantBuffer2 =
2524 render_ro_bo(brw->batch.bo, stage_state->push_const_offset);
2525 #else
2526 pkt.ConstantBody.ConstantBuffer0ReadLength =
2527 stage_state->push_const_size;
2528 pkt.ConstantBody.PointerToConstantBuffer0.offset =
2529 stage_state->push_const_offset | mocs;
2530 #endif
2531 }
2532 }
2533
2534 brw->ctx.NewDriverState |= GEN_GEN >= 9 ? BRW_NEW_SURFACES : 0;
2535 }
2536 #endif
2537
2538 #if GEN_GEN >= 6
2539 static void
2540 genX(upload_vs_push_constants)(struct brw_context *brw)
2541 {
2542 struct brw_stage_state *stage_state = &brw->vs.base;
2543
2544 /* _BRW_NEW_VERTEX_PROGRAM */
2545 const struct brw_program *vp = brw_program_const(brw->vertex_program);
2546 /* BRW_NEW_VS_PROG_DATA */
2547 const struct brw_stage_prog_data *prog_data = brw->vs.base.prog_data;
2548
2549 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_VERTEX);
2550 gen6_upload_push_constants(brw, &vp->program, prog_data, stage_state);
2551
2552 #if GEN_GEN >= 7
2553 if (GEN_GEN == 7 && !GEN_IS_HASWELL && !brw->is_baytrail)
2554 gen7_emit_vs_workaround_flush(brw);
2555
2556 upload_constant_state(brw, stage_state, true /* active */,
2557 MESA_SHADER_VERTEX);
2558 #endif
2559 }
2560
2561 static const struct brw_tracked_state genX(vs_push_constants) = {
2562 .dirty = {
2563 .mesa = _NEW_PROGRAM_CONSTANTS |
2564 _NEW_TRANSFORM,
2565 .brw = BRW_NEW_BATCH |
2566 BRW_NEW_BLORP |
2567 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
2568 BRW_NEW_VERTEX_PROGRAM |
2569 BRW_NEW_VS_PROG_DATA,
2570 },
2571 .emit = genX(upload_vs_push_constants),
2572 };
2573
2574 static void
2575 genX(upload_gs_push_constants)(struct brw_context *brw)
2576 {
2577 struct brw_stage_state *stage_state = &brw->gs.base;
2578
2579 /* BRW_NEW_GEOMETRY_PROGRAM */
2580 const struct brw_program *gp = brw_program_const(brw->geometry_program);
2581
2582 if (gp) {
2583 /* BRW_NEW_GS_PROG_DATA */
2584 struct brw_stage_prog_data *prog_data = brw->gs.base.prog_data;
2585
2586 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_GEOMETRY);
2587 gen6_upload_push_constants(brw, &gp->program, prog_data, stage_state);
2588 }
2589
2590 #if GEN_GEN >= 7
2591 upload_constant_state(brw, stage_state, gp, MESA_SHADER_GEOMETRY);
2592 #endif
2593 }
2594
2595 static const struct brw_tracked_state genX(gs_push_constants) = {
2596 .dirty = {
2597 .mesa = _NEW_PROGRAM_CONSTANTS |
2598 _NEW_TRANSFORM,
2599 .brw = BRW_NEW_BATCH |
2600 BRW_NEW_BLORP |
2601 BRW_NEW_GEOMETRY_PROGRAM |
2602 BRW_NEW_GS_PROG_DATA |
2603 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
2604 },
2605 .emit = genX(upload_gs_push_constants),
2606 };
2607
2608 static void
2609 genX(upload_wm_push_constants)(struct brw_context *brw)
2610 {
2611 struct brw_stage_state *stage_state = &brw->wm.base;
2612 /* BRW_NEW_FRAGMENT_PROGRAM */
2613 const struct brw_program *fp = brw_program_const(brw->fragment_program);
2614 /* BRW_NEW_FS_PROG_DATA */
2615 const struct brw_stage_prog_data *prog_data = brw->wm.base.prog_data;
2616
2617 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_FRAGMENT);
2618
2619 gen6_upload_push_constants(brw, &fp->program, prog_data, stage_state);
2620
2621 #if GEN_GEN >= 7
2622 upload_constant_state(brw, stage_state, true, MESA_SHADER_FRAGMENT);
2623 #endif
2624 }
2625
2626 static const struct brw_tracked_state genX(wm_push_constants) = {
2627 .dirty = {
2628 .mesa = _NEW_PROGRAM_CONSTANTS,
2629 .brw = BRW_NEW_BATCH |
2630 BRW_NEW_BLORP |
2631 BRW_NEW_FRAGMENT_PROGRAM |
2632 BRW_NEW_FS_PROG_DATA |
2633 BRW_NEW_PUSH_CONSTANT_ALLOCATION,
2634 },
2635 .emit = genX(upload_wm_push_constants),
2636 };
2637 #endif
2638
2639 /* ---------------------------------------------------------------------- */
2640
2641 #if GEN_GEN >= 6
2642 static unsigned
2643 genX(determine_sample_mask)(struct brw_context *brw)
2644 {
2645 struct gl_context *ctx = &brw->ctx;
2646 float coverage = 1.0f;
2647 float coverage_invert = false;
2648 unsigned sample_mask = ~0u;
2649
2650 /* BRW_NEW_NUM_SAMPLES */
2651 unsigned num_samples = brw->num_samples;
2652
2653 if (_mesa_is_multisample_enabled(ctx)) {
2654 if (ctx->Multisample.SampleCoverage) {
2655 coverage = ctx->Multisample.SampleCoverageValue;
2656 coverage_invert = ctx->Multisample.SampleCoverageInvert;
2657 }
2658 if (ctx->Multisample.SampleMask) {
2659 sample_mask = ctx->Multisample.SampleMaskValue;
2660 }
2661 }
2662
2663 if (num_samples > 1) {
2664 int coverage_int = (int) (num_samples * coverage + 0.5f);
2665 uint32_t coverage_bits = (1 << coverage_int) - 1;
2666 if (coverage_invert)
2667 coverage_bits ^= (1 << num_samples) - 1;
2668 return coverage_bits & sample_mask;
2669 } else {
2670 return 1;
2671 }
2672 }
2673
2674 static void
2675 genX(emit_3dstate_multisample2)(struct brw_context *brw,
2676 unsigned num_samples)
2677 {
2678 assert(brw->num_samples <= 16);
2679
2680 unsigned log2_samples = ffs(MAX2(num_samples, 1)) - 1;
2681
2682 brw_batch_emit(brw, GENX(3DSTATE_MULTISAMPLE), multi) {
2683 multi.PixelLocation = CENTER;
2684 multi.NumberofMultisamples = log2_samples;
2685 #if GEN_GEN == 6
2686 GEN_SAMPLE_POS_4X(multi.Sample);
2687 #elif GEN_GEN == 7
2688 switch (num_samples) {
2689 case 1:
2690 GEN_SAMPLE_POS_1X(multi.Sample);
2691 break;
2692 case 2:
2693 GEN_SAMPLE_POS_2X(multi.Sample);
2694 break;
2695 case 4:
2696 GEN_SAMPLE_POS_4X(multi.Sample);
2697 break;
2698 case 8:
2699 GEN_SAMPLE_POS_8X(multi.Sample);
2700 break;
2701 default:
2702 break;
2703 }
2704 #endif
2705 }
2706 }
2707
2708 static void
2709 genX(upload_multisample_state)(struct brw_context *brw)
2710 {
2711 genX(emit_3dstate_multisample2)(brw, brw->num_samples);
2712
2713 brw_batch_emit(brw, GENX(3DSTATE_SAMPLE_MASK), sm) {
2714 sm.SampleMask = genX(determine_sample_mask)(brw);
2715 }
2716 }
2717
2718 static const struct brw_tracked_state genX(multisample_state) = {
2719 .dirty = {
2720 .mesa = _NEW_MULTISAMPLE,
2721 .brw = BRW_NEW_BLORP |
2722 BRW_NEW_CONTEXT |
2723 BRW_NEW_NUM_SAMPLES,
2724 },
2725 .emit = genX(upload_multisample_state)
2726 };
2727 #endif
2728
2729 /* ---------------------------------------------------------------------- */
2730
2731 #if GEN_GEN >= 6
2732 static void
2733 genX(upload_color_calc_state)(struct brw_context *brw)
2734 {
2735 struct gl_context *ctx = &brw->ctx;
2736
2737 brw_state_emit(brw, GENX(COLOR_CALC_STATE), 64, &brw->cc.state_offset, cc) {
2738 /* _NEW_COLOR */
2739 cc.AlphaTestFormat = ALPHATEST_UNORM8;
2740 UNCLAMPED_FLOAT_TO_UBYTE(cc.AlphaReferenceValueAsUNORM8,
2741 ctx->Color.AlphaRef);
2742
2743 #if GEN_GEN < 9
2744 /* _NEW_STENCIL */
2745 cc.StencilReferenceValue = _mesa_get_stencil_ref(ctx, 0);
2746 cc.BackfaceStencilReferenceValue =
2747 _mesa_get_stencil_ref(ctx, ctx->Stencil._BackFace);
2748 #endif
2749
2750 /* _NEW_COLOR */
2751 cc.BlendConstantColorRed = ctx->Color.BlendColorUnclamped[0];
2752 cc.BlendConstantColorGreen = ctx->Color.BlendColorUnclamped[1];
2753 cc.BlendConstantColorBlue = ctx->Color.BlendColorUnclamped[2];
2754 cc.BlendConstantColorAlpha = ctx->Color.BlendColorUnclamped[3];
2755 }
2756
2757 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2758 ptr.ColorCalcStatePointer = brw->cc.state_offset;
2759 #if GEN_GEN != 7
2760 ptr.ColorCalcStatePointerValid = true;
2761 #endif
2762 }
2763 }
2764
2765 static const struct brw_tracked_state genX(color_calc_state) = {
2766 .dirty = {
2767 .mesa = _NEW_COLOR |
2768 _NEW_STENCIL,
2769 .brw = BRW_NEW_BATCH |
2770 BRW_NEW_BLORP |
2771 BRW_NEW_CC_STATE |
2772 BRW_NEW_STATE_BASE_ADDRESS,
2773 },
2774 .emit = genX(upload_color_calc_state),
2775 };
2776
2777 #endif
2778
2779 /* ---------------------------------------------------------------------- */
2780
2781 #if GEN_GEN >= 7
2782 static void
2783 genX(upload_sbe)(struct brw_context *brw)
2784 {
2785 struct gl_context *ctx = &brw->ctx;
2786 /* BRW_NEW_FS_PROG_DATA */
2787 const struct brw_wm_prog_data *wm_prog_data =
2788 brw_wm_prog_data(brw->wm.base.prog_data);
2789 #if GEN_GEN >= 8
2790 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = { { 0 } };
2791 #else
2792 #define attr_overrides sbe.Attribute
2793 #endif
2794 uint32_t urb_entry_read_length;
2795 uint32_t urb_entry_read_offset;
2796 uint32_t point_sprite_enables;
2797
2798 brw_batch_emit(brw, GENX(3DSTATE_SBE), sbe) {
2799 sbe.AttributeSwizzleEnable = true;
2800 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
2801
2802 /* _NEW_BUFFERS */
2803 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
2804
2805 /* _NEW_POINT
2806 *
2807 * Window coordinates in an FBO are inverted, which means point
2808 * sprite origin must be inverted.
2809 */
2810 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
2811 sbe.PointSpriteTextureCoordinateOrigin = LOWERLEFT;
2812 else
2813 sbe.PointSpriteTextureCoordinateOrigin = UPPERLEFT;
2814
2815 /* _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM,
2816 * BRW_NEW_FS_PROG_DATA | BRW_NEW_FRAGMENT_PROGRAM |
2817 * BRW_NEW_GS_PROG_DATA | BRW_NEW_PRIMITIVE | BRW_NEW_TES_PROG_DATA |
2818 * BRW_NEW_VUE_MAP_GEOM_OUT
2819 */
2820 genX(calculate_attr_overrides)(brw,
2821 attr_overrides,
2822 &point_sprite_enables,
2823 &urb_entry_read_length,
2824 &urb_entry_read_offset);
2825
2826 /* Typically, the URB entry read length and offset should be programmed
2827 * in 3DSTATE_VS and 3DSTATE_GS; SBE inherits it from the last active
2828 * stage which produces geometry. However, we don't know the proper
2829 * value until we call calculate_attr_overrides().
2830 *
2831 * To fit with our existing code, we override the inherited values and
2832 * specify it here directly, as we did on previous generations.
2833 */
2834 sbe.VertexURBEntryReadLength = urb_entry_read_length;
2835 sbe.VertexURBEntryReadOffset = urb_entry_read_offset;
2836 sbe.PointSpriteTextureCoordinateEnable = point_sprite_enables;
2837 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
2838
2839 #if GEN_GEN >= 8
2840 sbe.ForceVertexURBEntryReadLength = true;
2841 sbe.ForceVertexURBEntryReadOffset = true;
2842 #endif
2843
2844 #if GEN_GEN >= 9
2845 /* prepare the active component dwords */
2846 int input_index = 0;
2847 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
2848 if (!(brw->fragment_program->info.inputs_read &
2849 BITFIELD64_BIT(attr))) {
2850 continue;
2851 }
2852
2853 assert(input_index < 32);
2854
2855 sbe.AttributeActiveComponentFormat[input_index] = ACTIVE_COMPONENT_XYZW;
2856 ++input_index;
2857 }
2858 #endif
2859 }
2860
2861 #if GEN_GEN >= 8
2862 brw_batch_emit(brw, GENX(3DSTATE_SBE_SWIZ), sbes) {
2863 for (int i = 0; i < 16; i++)
2864 sbes.Attribute[i] = attr_overrides[i];
2865 }
2866 #endif
2867
2868 #undef attr_overrides
2869 }
2870
2871 static const struct brw_tracked_state genX(sbe_state) = {
2872 .dirty = {
2873 .mesa = _NEW_BUFFERS |
2874 _NEW_LIGHT |
2875 _NEW_POINT |
2876 _NEW_POLYGON |
2877 _NEW_PROGRAM,
2878 .brw = BRW_NEW_BLORP |
2879 BRW_NEW_CONTEXT |
2880 BRW_NEW_FRAGMENT_PROGRAM |
2881 BRW_NEW_FS_PROG_DATA |
2882 BRW_NEW_GS_PROG_DATA |
2883 BRW_NEW_TES_PROG_DATA |
2884 BRW_NEW_VUE_MAP_GEOM_OUT |
2885 (GEN_GEN == 7 ? BRW_NEW_PRIMITIVE
2886 : 0),
2887 },
2888 .emit = genX(upload_sbe),
2889 };
2890 #endif
2891
2892 /* ---------------------------------------------------------------------- */
2893
2894 #if GEN_GEN >= 7
2895 /**
2896 * Outputs the 3DSTATE_SO_DECL_LIST command.
2897 *
2898 * The data output is a series of 64-bit entries containing a SO_DECL per
2899 * stream. We only have one stream of rendering coming out of the GS unit, so
2900 * we only emit stream 0 (low 16 bits) SO_DECLs.
2901 */
2902 static void
2903 genX(upload_3dstate_so_decl_list)(struct brw_context *brw,
2904 const struct brw_vue_map *vue_map)
2905 {
2906 struct gl_context *ctx = &brw->ctx;
2907 /* BRW_NEW_TRANSFORM_FEEDBACK */
2908 struct gl_transform_feedback_object *xfb_obj =
2909 ctx->TransformFeedback.CurrentObject;
2910 const struct gl_transform_feedback_info *linked_xfb_info =
2911 xfb_obj->program->sh.LinkedTransformFeedback;
2912 struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
2913 int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2914 int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2915 int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
2916 int max_decls = 0;
2917 STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
2918
2919 memset(so_decl, 0, sizeof(so_decl));
2920
2921 /* Construct the list of SO_DECLs to be emitted. The formatting of the
2922 * command feels strange -- each dword pair contains a SO_DECL per stream.
2923 */
2924 for (unsigned i = 0; i < linked_xfb_info->NumOutputs; i++) {
2925 int buffer = linked_xfb_info->Outputs[i].OutputBuffer;
2926 struct GENX(SO_DECL) decl = {0};
2927 int varying = linked_xfb_info->Outputs[i].OutputRegister;
2928 const unsigned components = linked_xfb_info->Outputs[i].NumComponents;
2929 unsigned component_mask = (1 << components) - 1;
2930 unsigned stream_id = linked_xfb_info->Outputs[i].StreamId;
2931 unsigned decl_buffer_slot = buffer;
2932 assert(stream_id < MAX_VERTEX_STREAMS);
2933
2934 /* gl_PointSize is stored in VARYING_SLOT_PSIZ.w
2935 * gl_Layer is stored in VARYING_SLOT_PSIZ.y
2936 * gl_ViewportIndex is stored in VARYING_SLOT_PSIZ.z
2937 */
2938 if (varying == VARYING_SLOT_PSIZ) {
2939 assert(components == 1);
2940 component_mask <<= 3;
2941 } else if (varying == VARYING_SLOT_LAYER) {
2942 assert(components == 1);
2943 component_mask <<= 1;
2944 } else if (varying == VARYING_SLOT_VIEWPORT) {
2945 assert(components == 1);
2946 component_mask <<= 2;
2947 } else {
2948 component_mask <<= linked_xfb_info->Outputs[i].ComponentOffset;
2949 }
2950
2951 buffer_mask[stream_id] |= 1 << buffer;
2952
2953 decl.OutputBufferSlot = decl_buffer_slot;
2954 if (varying == VARYING_SLOT_LAYER || varying == VARYING_SLOT_VIEWPORT) {
2955 decl.RegisterIndex = vue_map->varying_to_slot[VARYING_SLOT_PSIZ];
2956 } else {
2957 assert(vue_map->varying_to_slot[varying] >= 0);
2958 decl.RegisterIndex = vue_map->varying_to_slot[varying];
2959 }
2960 decl.ComponentMask = component_mask;
2961
2962 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
2963 * array. Instead, it simply increments DstOffset for the following
2964 * input by the number of components that should be skipped.
2965 *
2966 * Our hardware is unusual in that it requires us to program SO_DECLs
2967 * for fake "hole" components, rather than simply taking the offset
2968 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
2969 * program as many size = 4 holes as we can, then a final hole to
2970 * accommodate the final 1, 2, or 3 remaining.
2971 */
2972 int skip_components =
2973 linked_xfb_info->Outputs[i].DstOffset - next_offset[buffer];
2974
2975 next_offset[buffer] += skip_components;
2976
2977 while (skip_components >= 4) {
2978 struct GENX(SO_DECL) *d = &so_decl[stream_id][decls[stream_id]++];
2979 d->HoleFlag = 1;
2980 d->OutputBufferSlot = decl_buffer_slot;
2981 d->ComponentMask = 0xf;
2982 skip_components -= 4;
2983 }
2984
2985 if (skip_components > 0) {
2986 struct GENX(SO_DECL) *d = &so_decl[stream_id][decls[stream_id]++];
2987 d->HoleFlag = 1;
2988 d->OutputBufferSlot = decl_buffer_slot;
2989 d->ComponentMask = (1 << skip_components) - 1;
2990 }
2991
2992 assert(linked_xfb_info->Outputs[i].DstOffset == next_offset[buffer]);
2993
2994 next_offset[buffer] += components;
2995
2996 so_decl[stream_id][decls[stream_id]++] = decl;
2997
2998 if (decls[stream_id] > max_decls)
2999 max_decls = decls[stream_id];
3000 }
3001
3002 uint32_t *dw;
3003 dw = brw_batch_emitn(brw, GENX(3DSTATE_SO_DECL_LIST), 3 + 2 * max_decls,
3004 .StreamtoBufferSelects0 = buffer_mask[0],
3005 .StreamtoBufferSelects1 = buffer_mask[1],
3006 .StreamtoBufferSelects2 = buffer_mask[2],
3007 .StreamtoBufferSelects3 = buffer_mask[3],
3008 .NumEntries0 = decls[0],
3009 .NumEntries1 = decls[1],
3010 .NumEntries2 = decls[2],
3011 .NumEntries3 = decls[3]);
3012
3013 for (int i = 0; i < max_decls; i++) {
3014 GENX(SO_DECL_ENTRY_pack)(
3015 brw, dw + 2 + i * 2,
3016 &(struct GENX(SO_DECL_ENTRY)) {
3017 .Stream0Decl = so_decl[0][i],
3018 .Stream1Decl = so_decl[1][i],
3019 .Stream2Decl = so_decl[2][i],
3020 .Stream3Decl = so_decl[3][i],
3021 });
3022 }
3023 }
3024
3025 static void
3026 genX(upload_3dstate_so_buffers)(struct brw_context *brw)
3027 {
3028 struct gl_context *ctx = &brw->ctx;
3029 /* BRW_NEW_TRANSFORM_FEEDBACK */
3030 struct gl_transform_feedback_object *xfb_obj =
3031 ctx->TransformFeedback.CurrentObject;
3032 #if GEN_GEN < 8
3033 const struct gl_transform_feedback_info *linked_xfb_info =
3034 xfb_obj->program->sh.LinkedTransformFeedback;
3035 #else
3036 struct brw_transform_feedback_object *brw_obj =
3037 (struct brw_transform_feedback_object *) xfb_obj;
3038 uint32_t mocs_wb = brw->gen >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
3039 #endif
3040
3041 /* Set up the up to 4 output buffers. These are the ranges defined in the
3042 * gl_transform_feedback_object.
3043 */
3044 for (int i = 0; i < 4; i++) {
3045 struct intel_buffer_object *bufferobj =
3046 intel_buffer_object(xfb_obj->Buffers[i]);
3047
3048 if (!bufferobj) {
3049 brw_batch_emit(brw, GENX(3DSTATE_SO_BUFFER), sob) {
3050 sob.SOBufferIndex = i;
3051 }
3052 continue;
3053 }
3054
3055 uint32_t start = xfb_obj->Offset[i];
3056 assert(start % 4 == 0);
3057 uint32_t end = ALIGN(start + xfb_obj->Size[i], 4);
3058 struct brw_bo *bo =
3059 intel_bufferobj_buffer(brw, bufferobj, start, end - start);
3060 assert(end <= bo->size);
3061
3062 brw_batch_emit(brw, GENX(3DSTATE_SO_BUFFER), sob) {
3063 sob.SOBufferIndex = i;
3064
3065 sob.SurfaceBaseAddress = render_bo(bo, start);
3066 #if GEN_GEN < 8
3067 sob.SurfacePitch = linked_xfb_info->Buffers[i].Stride * 4;
3068 sob.SurfaceEndAddress = render_bo(bo, end);
3069 #else
3070 sob.SOBufferEnable = true;
3071 sob.StreamOffsetWriteEnable = true;
3072 sob.StreamOutputBufferOffsetAddressEnable = true;
3073 sob.SOBufferMOCS = mocs_wb;
3074
3075 sob.SurfaceSize = MAX2(xfb_obj->Size[i] / 4, 1) - 1;
3076 sob.StreamOutputBufferOffsetAddress =
3077 instruction_bo(brw_obj->offset_bo, i * sizeof(uint32_t));
3078
3079 if (brw_obj->zero_offsets) {
3080 /* Zero out the offset and write that to offset_bo */
3081 sob.StreamOffset = 0;
3082 } else {
3083 /* Use offset_bo as the "Stream Offset." */
3084 sob.StreamOffset = 0xFFFFFFFF;
3085 }
3086 #endif
3087 }
3088 }
3089
3090 #if GEN_GEN >= 8
3091 brw_obj->zero_offsets = false;
3092 #endif
3093 }
3094
3095 static inline bool
3096 query_active(struct gl_query_object *q)
3097 {
3098 return q && q->Active;
3099 }
3100
3101 static void
3102 genX(upload_3dstate_streamout)(struct brw_context *brw, bool active,
3103 const struct brw_vue_map *vue_map)
3104 {
3105 struct gl_context *ctx = &brw->ctx;
3106 /* BRW_NEW_TRANSFORM_FEEDBACK */
3107 struct gl_transform_feedback_object *xfb_obj =
3108 ctx->TransformFeedback.CurrentObject;
3109
3110 brw_batch_emit(brw, GENX(3DSTATE_STREAMOUT), sos) {
3111 if (active) {
3112 int urb_entry_read_offset = 0;
3113 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
3114 urb_entry_read_offset;
3115
3116 sos.SOFunctionEnable = true;
3117 sos.SOStatisticsEnable = true;
3118
3119 /* BRW_NEW_RASTERIZER_DISCARD */
3120 if (ctx->RasterDiscard) {
3121 if (!query_active(ctx->Query.PrimitivesGenerated[0])) {
3122 sos.RenderingDisable = true;
3123 } else {
3124 perf_debug("Rasterizer discard with a GL_PRIMITIVES_GENERATED "
3125 "query active relies on the clipper.");
3126 }
3127 }
3128
3129 /* _NEW_LIGHT */
3130 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
3131 sos.ReorderMode = TRAILING;
3132
3133 #if GEN_GEN < 8
3134 sos.SOBufferEnable0 = xfb_obj->Buffers[0] != NULL;
3135 sos.SOBufferEnable1 = xfb_obj->Buffers[1] != NULL;
3136 sos.SOBufferEnable2 = xfb_obj->Buffers[2] != NULL;
3137 sos.SOBufferEnable3 = xfb_obj->Buffers[3] != NULL;
3138 #else
3139 const struct gl_transform_feedback_info *linked_xfb_info =
3140 xfb_obj->program->sh.LinkedTransformFeedback;
3141 /* Set buffer pitches; 0 means unbound. */
3142 if (xfb_obj->Buffers[0])
3143 sos.Buffer0SurfacePitch = linked_xfb_info->Buffers[0].Stride * 4;
3144 if (xfb_obj->Buffers[1])
3145 sos.Buffer1SurfacePitch = linked_xfb_info->Buffers[1].Stride * 4;
3146 if (xfb_obj->Buffers[2])
3147 sos.Buffer2SurfacePitch = linked_xfb_info->Buffers[2].Stride * 4;
3148 if (xfb_obj->Buffers[3])
3149 sos.Buffer3SurfacePitch = linked_xfb_info->Buffers[3].Stride * 4;
3150 #endif
3151
3152 /* We always read the whole vertex. This could be reduced at some
3153 * point by reading less and offsetting the register index in the
3154 * SO_DECLs.
3155 */
3156 sos.Stream0VertexReadOffset = urb_entry_read_offset;
3157 sos.Stream0VertexReadLength = urb_entry_read_length - 1;
3158 sos.Stream1VertexReadOffset = urb_entry_read_offset;
3159 sos.Stream1VertexReadLength = urb_entry_read_length - 1;
3160 sos.Stream2VertexReadOffset = urb_entry_read_offset;
3161 sos.Stream2VertexReadLength = urb_entry_read_length - 1;
3162 sos.Stream3VertexReadOffset = urb_entry_read_offset;
3163 sos.Stream3VertexReadLength = urb_entry_read_length - 1;
3164 }
3165 }
3166 }
3167
3168 static void
3169 genX(upload_sol)(struct brw_context *brw)
3170 {
3171 struct gl_context *ctx = &brw->ctx;
3172 /* BRW_NEW_TRANSFORM_FEEDBACK */
3173 bool active = _mesa_is_xfb_active_and_unpaused(ctx);
3174
3175 if (active) {
3176 genX(upload_3dstate_so_buffers)(brw);
3177
3178 /* BRW_NEW_VUE_MAP_GEOM_OUT */
3179 genX(upload_3dstate_so_decl_list)(brw, &brw->vue_map_geom_out);
3180 }
3181
3182 /* Finally, set up the SOL stage. This command must always follow updates to
3183 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
3184 * MMIO register updates (current performed by the kernel at each batch
3185 * emit).
3186 */
3187 genX(upload_3dstate_streamout)(brw, active, &brw->vue_map_geom_out);
3188 }
3189
3190 static const struct brw_tracked_state genX(sol_state) = {
3191 .dirty = {
3192 .mesa = _NEW_LIGHT,
3193 .brw = BRW_NEW_BATCH |
3194 BRW_NEW_BLORP |
3195 BRW_NEW_RASTERIZER_DISCARD |
3196 BRW_NEW_VUE_MAP_GEOM_OUT |
3197 BRW_NEW_TRANSFORM_FEEDBACK,
3198 },
3199 .emit = genX(upload_sol),
3200 };
3201 #endif
3202
3203 /* ---------------------------------------------------------------------- */
3204
3205 #if GEN_GEN >= 7
3206 static void
3207 genX(upload_ps)(struct brw_context *brw)
3208 {
3209 UNUSED const struct gl_context *ctx = &brw->ctx;
3210 UNUSED const struct gen_device_info *devinfo = &brw->screen->devinfo;
3211
3212 /* BRW_NEW_FS_PROG_DATA */
3213 const struct brw_wm_prog_data *prog_data =
3214 brw_wm_prog_data(brw->wm.base.prog_data);
3215 const struct brw_stage_state *stage_state = &brw->wm.base;
3216
3217 #if GEN_GEN < 8
3218 #endif
3219
3220 brw_batch_emit(brw, GENX(3DSTATE_PS), ps) {
3221 /* Initialize the execution mask with VMask. Otherwise, derivatives are
3222 * incorrect for subspans where some of the pixels are unlit. We believe
3223 * the bit just didn't take effect in previous generations.
3224 */
3225 ps.VectorMaskEnable = GEN_GEN >= 8;
3226
3227 ps.SamplerCount =
3228 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4);
3229
3230 /* BRW_NEW_FS_PROG_DATA */
3231 ps.BindingTableEntryCount = prog_data->base.binding_table.size_bytes / 4;
3232
3233 if (prog_data->base.use_alt_mode)
3234 ps.FloatingPointMode = Alternate;
3235
3236 /* Haswell requires the sample mask to be set in this packet as well as
3237 * in 3DSTATE_SAMPLE_MASK; the values should match.
3238 */
3239
3240 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
3241 #if GEN_IS_HASWELL
3242 ps.SampleMask = genX(determine_sample_mask(brw));
3243 #endif
3244
3245 /* 3DSTATE_PS expects the number of threads per PSD, which is always 64;
3246 * it implicitly scales for different GT levels (which have some # of
3247 * PSDs).
3248 *
3249 * In Gen8 the format is U8-2 whereas in Gen9 it is U8-1.
3250 */
3251 #if GEN_GEN >= 9
3252 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
3253 #elif GEN_GEN >= 8
3254 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
3255 #else
3256 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
3257 #endif
3258
3259 if (prog_data->base.nr_params > 0)
3260 ps.PushConstantEnable = true;
3261
3262 #if GEN_GEN < 8
3263 /* From the IVB PRM, volume 2 part 1, page 287:
3264 * "This bit is inserted in the PS payload header and made available to
3265 * the DataPort (either via the message header or via header bypass) to
3266 * indicate that oMask data (one or two phases) is included in Render
3267 * Target Write messages. If present, the oMask data is used to mask off
3268 * samples."
3269 */
3270 ps.oMaskPresenttoRenderTarget = prog_data->uses_omask;
3271
3272 /* The hardware wedges if you have this bit set but don't turn on any
3273 * dual source blend factors.
3274 *
3275 * BRW_NEW_FS_PROG_DATA | _NEW_COLOR
3276 */
3277 ps.DualSourceBlendEnable = prog_data->dual_src_blend &&
3278 (ctx->Color.BlendEnabled & 1) &&
3279 ctx->Color.Blend[0]._UsesDualSrc;
3280
3281 /* BRW_NEW_FS_PROG_DATA */
3282 ps.AttributeEnable = (prog_data->num_varying_inputs != 0);
3283 #endif
3284
3285 /* From the documentation for this packet:
3286 * "If the PS kernel does not need the Position XY Offsets to
3287 * compute a Position Value, then this field should be programmed
3288 * to POSOFFSET_NONE."
3289 *
3290 * "SW Recommendation: If the PS kernel needs the Position Offsets
3291 * to compute a Position XY value, this field should match Position
3292 * ZW Interpolation Mode to ensure a consistent position.xyzw
3293 * computation."
3294 *
3295 * We only require XY sample offsets. So, this recommendation doesn't
3296 * look useful at the moment. We might need this in future.
3297 */
3298 if (prog_data->uses_pos_offset)
3299 ps.PositionXYOffsetSelect = POSOFFSET_SAMPLE;
3300 else
3301 ps.PositionXYOffsetSelect = POSOFFSET_NONE;
3302
3303 ps.RenderTargetFastClearEnable = brw->wm.fast_clear_op;
3304 ps._8PixelDispatchEnable = prog_data->dispatch_8;
3305 ps._16PixelDispatchEnable = prog_data->dispatch_16;
3306 ps.DispatchGRFStartRegisterForConstantSetupData0 =
3307 prog_data->base.dispatch_grf_start_reg;
3308 ps.DispatchGRFStartRegisterForConstantSetupData2 =
3309 prog_data->dispatch_grf_start_reg_2;
3310
3311 ps.KernelStartPointer0 = stage_state->prog_offset;
3312 ps.KernelStartPointer2 = stage_state->prog_offset +
3313 prog_data->prog_offset_2;
3314
3315 if (prog_data->base.total_scratch) {
3316 ps.ScratchSpaceBasePointer =
3317 render_bo(stage_state->scratch_bo,
3318 ffs(stage_state->per_thread_scratch) - 11);
3319 }
3320 }
3321 }
3322
3323 static const struct brw_tracked_state genX(ps_state) = {
3324 .dirty = {
3325 .mesa = _NEW_MULTISAMPLE |
3326 (GEN_GEN < 8 ? _NEW_BUFFERS |
3327 _NEW_COLOR
3328 : 0),
3329 .brw = BRW_NEW_BATCH |
3330 BRW_NEW_BLORP |
3331 BRW_NEW_FS_PROG_DATA,
3332 },
3333 .emit = genX(upload_ps),
3334 };
3335 #endif
3336
3337 /* ---------------------------------------------------------------------- */
3338
3339 #if GEN_GEN >= 7
3340 static void
3341 genX(upload_hs_state)(struct brw_context *brw)
3342 {
3343 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3344 struct brw_stage_state *stage_state = &brw->tcs.base;
3345 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
3346 const struct brw_vue_prog_data *vue_prog_data =
3347 brw_vue_prog_data(stage_prog_data);
3348
3349 /* BRW_NEW_TES_PROG_DATA */
3350 struct brw_tcs_prog_data *tcs_prog_data =
3351 brw_tcs_prog_data(stage_prog_data);
3352
3353 if (!tcs_prog_data) {
3354 brw_batch_emit(brw, GENX(3DSTATE_HS), hs);
3355 } else {
3356 brw_batch_emit(brw, GENX(3DSTATE_HS), hs) {
3357 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
3358
3359 hs.InstanceCount = tcs_prog_data->instances - 1;
3360 hs.IncludeVertexHandles = true;
3361
3362 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
3363 }
3364 }
3365 }
3366
3367 static const struct brw_tracked_state genX(hs_state) = {
3368 .dirty = {
3369 .mesa = 0,
3370 .brw = BRW_NEW_BATCH |
3371 BRW_NEW_BLORP |
3372 BRW_NEW_TCS_PROG_DATA |
3373 BRW_NEW_TESS_PROGRAMS,
3374 },
3375 .emit = genX(upload_hs_state),
3376 };
3377
3378 static void
3379 genX(upload_ds_state)(struct brw_context *brw)
3380 {
3381 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3382 const struct brw_stage_state *stage_state = &brw->tes.base;
3383 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
3384
3385 /* BRW_NEW_TES_PROG_DATA */
3386 const struct brw_tes_prog_data *tes_prog_data =
3387 brw_tes_prog_data(stage_prog_data);
3388 const struct brw_vue_prog_data *vue_prog_data =
3389 brw_vue_prog_data(stage_prog_data);
3390
3391 if (!tes_prog_data) {
3392 brw_batch_emit(brw, GENX(3DSTATE_DS), ds);
3393 } else {
3394 brw_batch_emit(brw, GENX(3DSTATE_DS), ds) {
3395 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
3396
3397 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
3398 ds.ComputeWCoordinateEnable =
3399 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
3400
3401 #if GEN_GEN >= 8
3402 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8)
3403 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
3404 ds.UserClipDistanceCullTestEnableBitmask =
3405 vue_prog_data->cull_distance_mask;
3406 #endif
3407 }
3408 }
3409 }
3410
3411 static const struct brw_tracked_state genX(ds_state) = {
3412 .dirty = {
3413 .mesa = 0,
3414 .brw = BRW_NEW_BATCH |
3415 BRW_NEW_BLORP |
3416 BRW_NEW_TESS_PROGRAMS |
3417 BRW_NEW_TES_PROG_DATA,
3418 },
3419 .emit = genX(upload_ds_state),
3420 };
3421
3422 /* ---------------------------------------------------------------------- */
3423
3424 static void
3425 upload_te_state(struct brw_context *brw)
3426 {
3427 /* BRW_NEW_TESS_PROGRAMS */
3428 bool active = brw->tess_eval_program;
3429
3430 /* BRW_NEW_TES_PROG_DATA */
3431 const struct brw_tes_prog_data *tes_prog_data =
3432 brw_tes_prog_data(brw->tes.base.prog_data);
3433
3434 if (active) {
3435 brw_batch_emit(brw, GENX(3DSTATE_TE), te) {
3436 te.Partitioning = tes_prog_data->partitioning;
3437 te.OutputTopology = tes_prog_data->output_topology;
3438 te.TEDomain = tes_prog_data->domain;
3439 te.TEEnable = true;
3440 te.MaximumTessellationFactorOdd = 63.0;
3441 te.MaximumTessellationFactorNotOdd = 64.0;
3442 }
3443 } else {
3444 brw_batch_emit(brw, GENX(3DSTATE_TE), te);
3445 }
3446 }
3447
3448 static const struct brw_tracked_state genX(te_state) = {
3449 .dirty = {
3450 .mesa = 0,
3451 .brw = BRW_NEW_BLORP |
3452 BRW_NEW_CONTEXT |
3453 BRW_NEW_TES_PROG_DATA |
3454 BRW_NEW_TESS_PROGRAMS,
3455 },
3456 .emit = upload_te_state,
3457 };
3458
3459 /* ---------------------------------------------------------------------- */
3460
3461 static void
3462 genX(upload_tes_push_constants)(struct brw_context *brw)
3463 {
3464 struct brw_stage_state *stage_state = &brw->tes.base;
3465 /* BRW_NEW_TESS_PROGRAMS */
3466 const struct brw_program *tep = brw_program_const(brw->tess_eval_program);
3467
3468 if (tep) {
3469 /* BRW_NEW_TES_PROG_DATA */
3470 const struct brw_stage_prog_data *prog_data = brw->tes.base.prog_data;
3471 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_TESS_EVAL);
3472 gen6_upload_push_constants(brw, &tep->program, prog_data, stage_state);
3473 }
3474
3475 upload_constant_state(brw, stage_state, tep, MESA_SHADER_TESS_EVAL);
3476 }
3477
3478 static const struct brw_tracked_state genX(tes_push_constants) = {
3479 .dirty = {
3480 .mesa = _NEW_PROGRAM_CONSTANTS,
3481 .brw = BRW_NEW_BATCH |
3482 BRW_NEW_BLORP |
3483 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
3484 BRW_NEW_TESS_PROGRAMS |
3485 BRW_NEW_TES_PROG_DATA,
3486 },
3487 .emit = genX(upload_tes_push_constants),
3488 };
3489
3490 static void
3491 genX(upload_tcs_push_constants)(struct brw_context *brw)
3492 {
3493 struct brw_stage_state *stage_state = &brw->tcs.base;
3494 /* BRW_NEW_TESS_PROGRAMS */
3495 const struct brw_program *tcp = brw_program_const(brw->tess_ctrl_program);
3496 bool active = brw->tess_eval_program;
3497
3498 if (active) {
3499 /* BRW_NEW_TCS_PROG_DATA */
3500 const struct brw_stage_prog_data *prog_data = brw->tcs.base.prog_data;
3501
3502 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_TESS_CTRL);
3503 gen6_upload_push_constants(brw, &tcp->program, prog_data, stage_state);
3504 }
3505
3506 upload_constant_state(brw, stage_state, active, MESA_SHADER_TESS_CTRL);
3507 }
3508
3509 static const struct brw_tracked_state genX(tcs_push_constants) = {
3510 .dirty = {
3511 .mesa = _NEW_PROGRAM_CONSTANTS,
3512 .brw = BRW_NEW_BATCH |
3513 BRW_NEW_BLORP |
3514 BRW_NEW_DEFAULT_TESS_LEVELS |
3515 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
3516 BRW_NEW_TESS_PROGRAMS |
3517 BRW_NEW_TCS_PROG_DATA,
3518 },
3519 .emit = genX(upload_tcs_push_constants),
3520 };
3521 #endif
3522
3523 /* ---------------------------------------------------------------------- */
3524
3525 #if GEN_GEN >= 8
3526 static void
3527 genX(upload_raster)(struct brw_context *brw)
3528 {
3529 struct gl_context *ctx = &brw->ctx;
3530
3531 /* _NEW_BUFFERS */
3532 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
3533
3534 /* _NEW_POLYGON */
3535 struct gl_polygon_attrib *polygon = &ctx->Polygon;
3536
3537 /* _NEW_POINT */
3538 struct gl_point_attrib *point = &ctx->Point;
3539
3540 brw_batch_emit(brw, GENX(3DSTATE_RASTER), raster) {
3541 if (polygon->_FrontBit == render_to_fbo)
3542 raster.FrontWinding = CounterClockwise;
3543
3544 if (polygon->CullFlag) {
3545 switch (polygon->CullFaceMode) {
3546 case GL_FRONT:
3547 raster.CullMode = CULLMODE_FRONT;
3548 break;
3549 case GL_BACK:
3550 raster.CullMode = CULLMODE_BACK;
3551 break;
3552 case GL_FRONT_AND_BACK:
3553 raster.CullMode = CULLMODE_BOTH;
3554 break;
3555 default:
3556 unreachable("not reached");
3557 }
3558 } else {
3559 raster.CullMode = CULLMODE_NONE;
3560 }
3561
3562 point->SmoothFlag = raster.SmoothPointEnable;
3563
3564 raster.DXMultisampleRasterizationEnable =
3565 _mesa_is_multisample_enabled(ctx);
3566
3567 raster.GlobalDepthOffsetEnableSolid = polygon->OffsetFill;
3568 raster.GlobalDepthOffsetEnableWireframe = polygon->OffsetLine;
3569 raster.GlobalDepthOffsetEnablePoint = polygon->OffsetPoint;
3570
3571 switch (polygon->FrontMode) {
3572 case GL_FILL:
3573 raster.FrontFaceFillMode = FILL_MODE_SOLID;
3574 break;
3575 case GL_LINE:
3576 raster.FrontFaceFillMode = FILL_MODE_WIREFRAME;
3577 break;
3578 case GL_POINT:
3579 raster.FrontFaceFillMode = FILL_MODE_POINT;
3580 break;
3581 default:
3582 unreachable("not reached");
3583 }
3584
3585 switch (polygon->BackMode) {
3586 case GL_FILL:
3587 raster.BackFaceFillMode = FILL_MODE_SOLID;
3588 break;
3589 case GL_LINE:
3590 raster.BackFaceFillMode = FILL_MODE_WIREFRAME;
3591 break;
3592 case GL_POINT:
3593 raster.BackFaceFillMode = FILL_MODE_POINT;
3594 break;
3595 default:
3596 unreachable("not reached");
3597 }
3598
3599 /* _NEW_LINE */
3600 raster.AntialiasingEnable = ctx->Line.SmoothFlag;
3601
3602 /* _NEW_SCISSOR */
3603 raster.ScissorRectangleEnable = ctx->Scissor.EnableFlags;
3604
3605 /* _NEW_TRANSFORM */
3606 if (!ctx->Transform.DepthClamp) {
3607 #if GEN_GEN >= 9
3608 raster.ViewportZFarClipTestEnable = true;
3609 raster.ViewportZNearClipTestEnable = true;
3610 #else
3611 raster.ViewportZClipTestEnable = true;
3612 #endif
3613 }
3614
3615 /* BRW_NEW_CONSERVATIVE_RASTERIZATION */
3616 #if GEN_GEN >= 9
3617 raster.ConservativeRasterizationEnable =
3618 ctx->IntelConservativeRasterization;
3619 #endif
3620
3621 raster.GlobalDepthOffsetClamp = polygon->OffsetClamp;
3622 raster.GlobalDepthOffsetScale = polygon->OffsetFactor;
3623
3624 raster.GlobalDepthOffsetConstant = polygon->OffsetUnits * 2;
3625 }
3626 }
3627
3628 static const struct brw_tracked_state genX(raster_state) = {
3629 .dirty = {
3630 .mesa = _NEW_BUFFERS |
3631 _NEW_LINE |
3632 _NEW_MULTISAMPLE |
3633 _NEW_POINT |
3634 _NEW_POLYGON |
3635 _NEW_SCISSOR |
3636 _NEW_TRANSFORM,
3637 .brw = BRW_NEW_BLORP |
3638 BRW_NEW_CONTEXT |
3639 BRW_NEW_CONSERVATIVE_RASTERIZATION,
3640 },
3641 .emit = genX(upload_raster),
3642 };
3643 #endif
3644
3645 /* ---------------------------------------------------------------------- */
3646
3647 #if GEN_GEN >= 8
3648 static void
3649 genX(upload_ps_extra)(struct brw_context *brw)
3650 {
3651 UNUSED struct gl_context *ctx = &brw->ctx;
3652
3653 const struct brw_wm_prog_data *prog_data =
3654 brw_wm_prog_data(brw->wm.base.prog_data);
3655
3656 brw_batch_emit(brw, GENX(3DSTATE_PS_EXTRA), psx) {
3657 psx.PixelShaderValid = true;
3658 psx.PixelShaderComputedDepthMode = prog_data->computed_depth_mode;
3659 psx.PixelShaderKillsPixel = prog_data->uses_kill;
3660 psx.AttributeEnable = prog_data->num_varying_inputs != 0;
3661 psx.PixelShaderUsesSourceDepth = prog_data->uses_src_depth;
3662 psx.PixelShaderUsesSourceW = prog_data->uses_src_w;
3663 psx.PixelShaderIsPerSample = prog_data->persample_dispatch;
3664
3665 /* _NEW_MULTISAMPLE | BRW_NEW_CONSERVATIVE_RASTERIZATION */
3666 if (prog_data->uses_sample_mask) {
3667 #if GEN_GEN >= 9
3668 if (prog_data->post_depth_coverage)
3669 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
3670 else if (prog_data->inner_coverage && ctx->IntelConservativeRasterization)
3671 psx.InputCoverageMaskState = ICMS_INNER_CONSERVATIVE;
3672 else
3673 psx.InputCoverageMaskState = ICMS_NORMAL;
3674 #else
3675 psx.PixelShaderUsesInputCoverageMask = true;
3676 #endif
3677 }
3678
3679 psx.oMaskPresenttoRenderTarget = prog_data->uses_omask;
3680 #if GEN_GEN >= 9
3681 psx.PixelShaderPullsBary = prog_data->pulls_bary;
3682 psx.PixelShaderComputesStencil = prog_data->computed_stencil;
3683 #endif
3684
3685 /* The stricter cross-primitive coherency guarantees that the hardware
3686 * gives us with the "Accesses UAV" bit set for at least one shader stage
3687 * and the "UAV coherency required" bit set on the 3DPRIMITIVE command
3688 * are redundant within the current image, atomic counter and SSBO GL
3689 * APIs, which all have very loose ordering and coherency requirements
3690 * and generally rely on the application to insert explicit barriers when
3691 * a shader invocation is expected to see the memory writes performed by
3692 * the invocations of some previous primitive. Regardless of the value
3693 * of "UAV coherency required", the "Accesses UAV" bits will implicitly
3694 * cause an in most cases useless DC flush when the lowermost stage with
3695 * the bit set finishes execution.
3696 *
3697 * It would be nice to disable it, but in some cases we can't because on
3698 * Gen8+ it also has an influence on rasterization via the PS UAV-only
3699 * signal (which could be set independently from the coherency mechanism
3700 * in the 3DSTATE_WM command on Gen7), and because in some cases it will
3701 * determine whether the hardware skips execution of the fragment shader
3702 * or not via the ThreadDispatchEnable signal. However if we know that
3703 * GEN8_PS_BLEND_HAS_WRITEABLE_RT is going to be set and
3704 * GEN8_PSX_PIXEL_SHADER_NO_RT_WRITE is not set it shouldn't make any
3705 * difference so we may just disable it here.
3706 *
3707 * Gen8 hardware tries to compute ThreadDispatchEnable for us but doesn't
3708 * take into account KillPixels when no depth or stencil writes are
3709 * enabled. In order for occlusion queries to work correctly with no
3710 * attachments, we need to force-enable here.
3711 *
3712 * BRW_NEW_FS_PROG_DATA | BRW_NEW_FRAGMENT_PROGRAM | _NEW_BUFFERS |
3713 * _NEW_COLOR
3714 */
3715 if ((prog_data->has_side_effects || prog_data->uses_kill) &&
3716 !brw_color_buffer_write_enabled(brw))
3717 psx.PixelShaderHasUAV = true;
3718 }
3719 }
3720
3721 const struct brw_tracked_state genX(ps_extra) = {
3722 .dirty = {
3723 .mesa = _NEW_BUFFERS | _NEW_COLOR,
3724 .brw = BRW_NEW_BLORP |
3725 BRW_NEW_CONTEXT |
3726 BRW_NEW_FRAGMENT_PROGRAM |
3727 BRW_NEW_FS_PROG_DATA |
3728 BRW_NEW_CONSERVATIVE_RASTERIZATION,
3729 },
3730 .emit = genX(upload_ps_extra),
3731 };
3732 #endif
3733
3734 /* ---------------------------------------------------------------------- */
3735
3736 #if GEN_GEN >= 8
3737 static void
3738 genX(upload_ps_blend)(struct brw_context *brw)
3739 {
3740 struct gl_context *ctx = &brw->ctx;
3741
3742 /* _NEW_BUFFERS */
3743 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
3744 const bool buffer0_is_integer = ctx->DrawBuffer->_IntegerBuffers & 0x1;
3745
3746 /* _NEW_COLOR */
3747 struct gl_colorbuffer_attrib *color = &ctx->Color;
3748
3749 brw_batch_emit(brw, GENX(3DSTATE_PS_BLEND), pb) {
3750 /* BRW_NEW_FRAGMENT_PROGRAM | _NEW_BUFFERS | _NEW_COLOR */
3751 pb.HasWriteableRT = brw_color_buffer_write_enabled(brw);
3752
3753 if (!buffer0_is_integer) {
3754 /* _NEW_MULTISAMPLE */
3755 pb.AlphaToCoverageEnable =
3756 _mesa_is_multisample_enabled(ctx) &&
3757 ctx->Multisample.SampleAlphaToCoverage;
3758
3759 pb.AlphaTestEnable = color->AlphaEnabled;
3760 }
3761
3762 /* Used for implementing the following bit of GL_EXT_texture_integer:
3763 * "Per-fragment operations that require floating-point color
3764 * components, including multisample alpha operations, alpha test,
3765 * blending, and dithering, have no effect when the corresponding
3766 * colors are written to an integer color buffer."
3767 *
3768 * The OpenGL specification 3.3 (page 196), section 4.1.3 says:
3769 * "If drawbuffer zero is not NONE and the buffer it references has an
3770 * integer format, the SAMPLE_ALPHA_TO_COVERAGE and SAMPLE_ALPHA_TO_ONE
3771 * operations are skipped."
3772 */
3773 if (rb && !buffer0_is_integer && (color->BlendEnabled & 1)) {
3774 GLenum eqRGB = color->Blend[0].EquationRGB;
3775 GLenum eqA = color->Blend[0].EquationA;
3776 GLenum srcRGB = color->Blend[0].SrcRGB;
3777 GLenum dstRGB = color->Blend[0].DstRGB;
3778 GLenum srcA = color->Blend[0].SrcA;
3779 GLenum dstA = color->Blend[0].DstA;
3780
3781 if (eqRGB == GL_MIN || eqRGB == GL_MAX)
3782 srcRGB = dstRGB = GL_ONE;
3783
3784 if (eqA == GL_MIN || eqA == GL_MAX)
3785 srcA = dstA = GL_ONE;
3786
3787 /* Due to hardware limitations, the destination may have information
3788 * in an alpha channel even when the format specifies no alpha
3789 * channel. In order to avoid getting any incorrect blending due to
3790 * that alpha channel, coerce the blend factors to values that will
3791 * not read the alpha channel, but will instead use the correct
3792 * implicit value for alpha.
3793 */
3794 if (!_mesa_base_format_has_channel(rb->_BaseFormat,
3795 GL_TEXTURE_ALPHA_TYPE)) {
3796 srcRGB = brw_fix_xRGB_alpha(srcRGB);
3797 srcA = brw_fix_xRGB_alpha(srcA);
3798 dstRGB = brw_fix_xRGB_alpha(dstRGB);
3799 dstA = brw_fix_xRGB_alpha(dstA);
3800 }
3801
3802 pb.ColorBufferBlendEnable = true;
3803 pb.SourceAlphaBlendFactor = brw_translate_blend_factor(srcA);
3804 pb.DestinationAlphaBlendFactor = brw_translate_blend_factor(dstA);
3805 pb.SourceBlendFactor = brw_translate_blend_factor(srcRGB);
3806 pb.DestinationBlendFactor = brw_translate_blend_factor(dstRGB);
3807
3808 pb.IndependentAlphaBlendEnable =
3809 srcA != srcRGB || dstA != dstRGB || eqA != eqRGB;
3810 }
3811 }
3812 }
3813
3814 static const struct brw_tracked_state genX(ps_blend) = {
3815 .dirty = {
3816 .mesa = _NEW_BUFFERS |
3817 _NEW_COLOR |
3818 _NEW_MULTISAMPLE,
3819 .brw = BRW_NEW_BLORP |
3820 BRW_NEW_CONTEXT |
3821 BRW_NEW_FRAGMENT_PROGRAM,
3822 },
3823 .emit = genX(upload_ps_blend)
3824 };
3825
3826 #endif
3827
3828 /* ---------------------------------------------------------------------- */
3829
3830 #if GEN_GEN == 6
3831 static void
3832 genX(upload_viewport_state_pointers)(struct brw_context *brw)
3833 {
3834 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS), vp) {
3835 vp.CCViewportStateChange = 1;
3836 vp.SFViewportStateChange = 1;
3837 vp.CLIPViewportStateChange = 1;
3838 vp.PointertoCLIP_VIEWPORT = brw->clip.vp_offset;
3839 vp.PointertoSF_VIEWPORT = brw->sf.vp_offset;
3840 vp.PointertoCC_VIEWPORT = brw->cc.vp_offset;
3841 }
3842 }
3843
3844 static const struct brw_tracked_state genX(viewport_state) = {
3845 .dirty = {
3846 .mesa = 0,
3847 .brw = BRW_NEW_BATCH |
3848 BRW_NEW_BLORP |
3849 BRW_NEW_CC_VP |
3850 BRW_NEW_CLIP_VP |
3851 BRW_NEW_SF_VP |
3852 BRW_NEW_STATE_BASE_ADDRESS,
3853 },
3854 .emit = genX(upload_viewport_state_pointers),
3855 };
3856 #endif
3857
3858 /* ---------------------------------------------------------------------- */
3859
3860 void
3861 genX(init_atoms)(struct brw_context *brw)
3862 {
3863 #if GEN_GEN < 6
3864 static const struct brw_tracked_state *render_atoms[] =
3865 {
3866 /* Once all the programs are done, we know how large urb entry
3867 * sizes need to be and can decide if we need to change the urb
3868 * layout.
3869 */
3870 &brw_curbe_offsets,
3871 &brw_recalculate_urb_fence,
3872
3873 &brw_cc_vp,
3874 &brw_cc_unit,
3875
3876 /* Surface state setup. Must come before the VS/WM unit. The binding
3877 * table upload must be last.
3878 */
3879 &brw_vs_pull_constants,
3880 &brw_wm_pull_constants,
3881 &brw_renderbuffer_surfaces,
3882 &brw_renderbuffer_read_surfaces,
3883 &brw_texture_surfaces,
3884 &brw_vs_binding_table,
3885 &brw_wm_binding_table,
3886
3887 &brw_fs_samplers,
3888 &brw_vs_samplers,
3889
3890 /* These set up state for brw_psp_urb_cbs */
3891 &brw_wm_unit,
3892 &brw_sf_vp,
3893 &brw_sf_unit,
3894 &brw_vs_unit, /* always required, enabled or not */
3895 &brw_clip_unit,
3896 &brw_gs_unit,
3897
3898 /* Command packets:
3899 */
3900 &brw_invariant_state,
3901
3902 &brw_binding_table_pointers,
3903 &brw_blend_constant_color,
3904
3905 &brw_depthbuffer,
3906
3907 &genX(polygon_stipple),
3908 &genX(polygon_stipple_offset),
3909
3910 &genX(line_stipple),
3911
3912 &brw_psp_urb_cbs,
3913
3914 &genX(drawing_rect),
3915 &brw_indices, /* must come before brw_vertices */
3916 &brw_index_buffer,
3917 &genX(vertices),
3918
3919 &brw_constant_buffer
3920 };
3921 #elif GEN_GEN == 6
3922 static const struct brw_tracked_state *render_atoms[] =
3923 {
3924 &genX(sf_clip_viewport),
3925
3926 /* Command packets: */
3927
3928 &brw_cc_vp,
3929 &genX(viewport_state), /* must do after *_vp stages */
3930
3931 &gen6_urb,
3932 &genX(blend_state), /* must do before cc unit */
3933 &genX(color_calc_state), /* must do before cc unit */
3934 &genX(depth_stencil_state), /* must do before cc unit */
3935
3936 &genX(vs_push_constants), /* Before vs_state */
3937 &genX(gs_push_constants), /* Before gs_state */
3938 &genX(wm_push_constants), /* Before wm_state */
3939
3940 /* Surface state setup. Must come before the VS/WM unit. The binding
3941 * table upload must be last.
3942 */
3943 &brw_vs_pull_constants,
3944 &brw_vs_ubo_surfaces,
3945 &brw_gs_pull_constants,
3946 &brw_gs_ubo_surfaces,
3947 &brw_wm_pull_constants,
3948 &brw_wm_ubo_surfaces,
3949 &gen6_renderbuffer_surfaces,
3950 &brw_renderbuffer_read_surfaces,
3951 &brw_texture_surfaces,
3952 &gen6_sol_surface,
3953 &brw_vs_binding_table,
3954 &gen6_gs_binding_table,
3955 &brw_wm_binding_table,
3956
3957 &brw_fs_samplers,
3958 &brw_vs_samplers,
3959 &brw_gs_samplers,
3960 &gen6_sampler_state,
3961 &genX(multisample_state),
3962
3963 &genX(vs_state),
3964 &genX(gs_state),
3965 &genX(clip_state),
3966 &genX(sf_state),
3967 &genX(wm_state),
3968
3969 &genX(scissor_state),
3970
3971 &gen6_binding_table_pointers,
3972
3973 &brw_depthbuffer,
3974
3975 &genX(polygon_stipple),
3976 &genX(polygon_stipple_offset),
3977
3978 &genX(line_stipple),
3979
3980 &genX(drawing_rect),
3981
3982 &brw_indices, /* must come before brw_vertices */
3983 &brw_index_buffer,
3984 &genX(vertices),
3985 };
3986 #elif GEN_GEN == 7
3987 static const struct brw_tracked_state *render_atoms[] =
3988 {
3989 /* Command packets: */
3990
3991 &brw_cc_vp,
3992 &genX(sf_clip_viewport),
3993
3994 &gen7_l3_state,
3995 &gen7_push_constant_space,
3996 &gen7_urb,
3997 &genX(blend_state), /* must do before cc unit */
3998 &genX(color_calc_state), /* must do before cc unit */
3999 &genX(depth_stencil_state), /* must do before cc unit */
4000
4001 &brw_vs_image_surfaces, /* Before vs push/pull constants and binding table */
4002 &brw_tcs_image_surfaces, /* Before tcs push/pull constants and binding table */
4003 &brw_tes_image_surfaces, /* Before tes push/pull constants and binding table */
4004 &brw_gs_image_surfaces, /* Before gs push/pull constants and binding table */
4005 &brw_wm_image_surfaces, /* Before wm push/pull constants and binding table */
4006
4007 &genX(vs_push_constants), /* Before vs_state */
4008 &genX(tcs_push_constants),
4009 &genX(tes_push_constants),
4010 &genX(gs_push_constants), /* Before gs_state */
4011 &genX(wm_push_constants), /* Before wm_surfaces and constant_buffer */
4012
4013 /* Surface state setup. Must come before the VS/WM unit. The binding
4014 * table upload must be last.
4015 */
4016 &brw_vs_pull_constants,
4017 &brw_vs_ubo_surfaces,
4018 &brw_vs_abo_surfaces,
4019 &brw_tcs_pull_constants,
4020 &brw_tcs_ubo_surfaces,
4021 &brw_tcs_abo_surfaces,
4022 &brw_tes_pull_constants,
4023 &brw_tes_ubo_surfaces,
4024 &brw_tes_abo_surfaces,
4025 &brw_gs_pull_constants,
4026 &brw_gs_ubo_surfaces,
4027 &brw_gs_abo_surfaces,
4028 &brw_wm_pull_constants,
4029 &brw_wm_ubo_surfaces,
4030 &brw_wm_abo_surfaces,
4031 &gen6_renderbuffer_surfaces,
4032 &brw_renderbuffer_read_surfaces,
4033 &brw_texture_surfaces,
4034 &brw_vs_binding_table,
4035 &brw_tcs_binding_table,
4036 &brw_tes_binding_table,
4037 &brw_gs_binding_table,
4038 &brw_wm_binding_table,
4039
4040 &brw_fs_samplers,
4041 &brw_vs_samplers,
4042 &brw_tcs_samplers,
4043 &brw_tes_samplers,
4044 &brw_gs_samplers,
4045 &genX(multisample_state),
4046
4047 &genX(vs_state),
4048 &genX(hs_state),
4049 &genX(te_state),
4050 &genX(ds_state),
4051 &genX(gs_state),
4052 &genX(sol_state),
4053 &genX(clip_state),
4054 &genX(sbe_state),
4055 &genX(sf_state),
4056 &genX(wm_state),
4057 &genX(ps_state),
4058
4059 &genX(scissor_state),
4060
4061 &gen7_depthbuffer,
4062
4063 &genX(polygon_stipple),
4064 &genX(polygon_stipple_offset),
4065
4066 &genX(line_stipple),
4067
4068 &genX(drawing_rect),
4069
4070 &brw_indices, /* must come before brw_vertices */
4071 &brw_index_buffer,
4072 &genX(vertices),
4073
4074 #if GEN_IS_HASWELL
4075 &genX(cut_index),
4076 #endif
4077 };
4078 #elif GEN_GEN >= 8
4079 static const struct brw_tracked_state *render_atoms[] =
4080 {
4081 &brw_cc_vp,
4082 &genX(sf_clip_viewport),
4083
4084 &gen7_l3_state,
4085 &gen7_push_constant_space,
4086 &gen7_urb,
4087 &genX(blend_state),
4088 &genX(color_calc_state),
4089
4090 &brw_vs_image_surfaces, /* Before vs push/pull constants and binding table */
4091 &brw_tcs_image_surfaces, /* Before tcs push/pull constants and binding table */
4092 &brw_tes_image_surfaces, /* Before tes push/pull constants and binding table */
4093 &brw_gs_image_surfaces, /* Before gs push/pull constants and binding table */
4094 &brw_wm_image_surfaces, /* Before wm push/pull constants and binding table */
4095
4096 &genX(vs_push_constants), /* Before vs_state */
4097 &genX(tcs_push_constants),
4098 &genX(tes_push_constants),
4099 &genX(gs_push_constants), /* Before gs_state */
4100 &genX(wm_push_constants), /* Before wm_surfaces and constant_buffer */
4101
4102 /* Surface state setup. Must come before the VS/WM unit. The binding
4103 * table upload must be last.
4104 */
4105 &brw_vs_pull_constants,
4106 &brw_vs_ubo_surfaces,
4107 &brw_vs_abo_surfaces,
4108 &brw_tcs_pull_constants,
4109 &brw_tcs_ubo_surfaces,
4110 &brw_tcs_abo_surfaces,
4111 &brw_tes_pull_constants,
4112 &brw_tes_ubo_surfaces,
4113 &brw_tes_abo_surfaces,
4114 &brw_gs_pull_constants,
4115 &brw_gs_ubo_surfaces,
4116 &brw_gs_abo_surfaces,
4117 &brw_wm_pull_constants,
4118 &brw_wm_ubo_surfaces,
4119 &brw_wm_abo_surfaces,
4120 &gen6_renderbuffer_surfaces,
4121 &brw_renderbuffer_read_surfaces,
4122 &brw_texture_surfaces,
4123 &brw_vs_binding_table,
4124 &brw_tcs_binding_table,
4125 &brw_tes_binding_table,
4126 &brw_gs_binding_table,
4127 &brw_wm_binding_table,
4128
4129 &brw_fs_samplers,
4130 &brw_vs_samplers,
4131 &brw_tcs_samplers,
4132 &brw_tes_samplers,
4133 &brw_gs_samplers,
4134 &genX(multisample_state),
4135
4136 &genX(vs_state),
4137 &genX(hs_state),
4138 &genX(te_state),
4139 &genX(ds_state),
4140 &genX(gs_state),
4141 &genX(sol_state),
4142 &genX(clip_state),
4143 &genX(raster_state),
4144 &genX(sbe_state),
4145 &genX(sf_state),
4146 &genX(ps_blend),
4147 &genX(ps_extra),
4148 &genX(ps_state),
4149 &genX(depth_stencil_state),
4150 &genX(wm_state),
4151
4152 &genX(scissor_state),
4153
4154 &gen7_depthbuffer,
4155
4156 &genX(polygon_stipple),
4157 &genX(polygon_stipple_offset),
4158
4159 &genX(line_stipple),
4160
4161 &genX(drawing_rect),
4162
4163 &gen8_vf_topology,
4164
4165 &brw_indices,
4166 &gen8_index_buffer,
4167 &genX(vertices),
4168
4169 &genX(cut_index),
4170 &gen8_pma_fix,
4171 };
4172 #endif
4173
4174 STATIC_ASSERT(ARRAY_SIZE(render_atoms) <= ARRAY_SIZE(brw->render_atoms));
4175 brw_copy_pipeline_atoms(brw, BRW_RENDER_PIPELINE,
4176 render_atoms, ARRAY_SIZE(render_atoms));
4177
4178 #if GEN_GEN >= 7
4179 static const struct brw_tracked_state *compute_atoms[] =
4180 {
4181 &gen7_l3_state,
4182 &brw_cs_image_surfaces,
4183 &gen7_cs_push_constants,
4184 &brw_cs_pull_constants,
4185 &brw_cs_ubo_surfaces,
4186 &brw_cs_abo_surfaces,
4187 &brw_cs_texture_surfaces,
4188 &brw_cs_work_groups_surface,
4189 &brw_cs_samplers,
4190 &brw_cs_state,
4191 };
4192
4193 STATIC_ASSERT(ARRAY_SIZE(compute_atoms) <= ARRAY_SIZE(brw->compute_atoms));
4194 brw_copy_pipeline_atoms(brw, BRW_COMPUTE_PIPELINE,
4195 compute_atoms, ARRAY_SIZE(compute_atoms));
4196 #endif
4197 }