i965: Emit VF cache invalidates for 48-bit addressing bugs with softpin.
[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 "dev/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 #include "main/state.h"
35
36 #include "brw_context.h"
37 #include "brw_draw.h"
38 #include "brw_multisample_state.h"
39 #include "brw_state.h"
40 #include "brw_wm.h"
41 #include "brw_util.h"
42
43 #include "intel_batchbuffer.h"
44 #include "intel_buffer_objects.h"
45 #include "intel_fbo.h"
46
47 #include "main/enums.h"
48 #include "main/fbobject.h"
49 #include "main/framebuffer.h"
50 #include "main/glformats.h"
51 #include "main/samplerobj.h"
52 #include "main/shaderapi.h"
53 #include "main/stencil.h"
54 #include "main/transformfeedback.h"
55 #include "main/varray.h"
56 #include "main/viewport.h"
57 #include "util/half_float.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 unsigned reloc_flags;
72 uint32_t offset;
73 };
74
75 #define __gen_address_type struct brw_address
76 #define __gen_user_data struct brw_context
77
78 static uint64_t
79 __gen_combine_address(struct brw_context *brw, void *location,
80 struct brw_address address, uint32_t delta)
81 {
82 struct intel_batchbuffer *batch = &brw->batch;
83 uint32_t offset;
84
85 if (address.bo == NULL) {
86 return address.offset + delta;
87 } else {
88 if (GEN_GEN < 6 && brw_ptr_in_state_buffer(batch, location)) {
89 offset = (char *) location - (char *) brw->batch.state.map;
90 return brw_state_reloc(batch, offset, address.bo,
91 address.offset + delta,
92 address.reloc_flags);
93 }
94
95 assert(!brw_ptr_in_state_buffer(batch, location));
96
97 offset = (char *) location - (char *) brw->batch.batch.map;
98 return brw_batch_reloc(batch, offset, address.bo,
99 address.offset + delta,
100 address.reloc_flags);
101 }
102 }
103
104 UNUSED static struct brw_address
105 rw_bo(struct brw_bo *bo, uint32_t offset)
106 {
107 return (struct brw_address) {
108 .bo = bo,
109 .offset = offset,
110 .reloc_flags = RELOC_WRITE,
111 };
112 }
113
114 static struct brw_address
115 ro_bo(struct brw_bo *bo, uint32_t offset)
116 {
117 return (struct brw_address) {
118 .bo = bo,
119 .offset = offset,
120 };
121 }
122
123 static struct brw_address
124 rw_32_bo(struct brw_bo *bo, uint32_t offset)
125 {
126 return (struct brw_address) {
127 .bo = bo,
128 .offset = offset,
129 .reloc_flags = RELOC_WRITE | RELOC_32BIT,
130 };
131 }
132
133 static struct brw_address
134 ro_32_bo(struct brw_bo *bo, uint32_t offset)
135 {
136 return (struct brw_address) {
137 .bo = bo,
138 .offset = offset,
139 .reloc_flags = RELOC_32BIT,
140 };
141 }
142
143 UNUSED static struct brw_address
144 ggtt_bo(struct brw_bo *bo, uint32_t offset)
145 {
146 return (struct brw_address) {
147 .bo = bo,
148 .offset = offset,
149 .reloc_flags = RELOC_WRITE | RELOC_NEEDS_GGTT,
150 };
151 }
152
153 #if GEN_GEN == 4
154 static struct brw_address
155 KSP(struct brw_context *brw, uint32_t offset)
156 {
157 return ro_bo(brw->cache.bo, offset);
158 }
159 #else
160 static uint32_t
161 KSP(UNUSED struct brw_context *brw, uint32_t offset)
162 {
163 return offset;
164 }
165 #endif
166
167 #include "genxml/genX_pack.h"
168
169 #define _brw_cmd_length(cmd) cmd ## _length
170 #define _brw_cmd_length_bias(cmd) cmd ## _length_bias
171 #define _brw_cmd_header(cmd) cmd ## _header
172 #define _brw_cmd_pack(cmd) cmd ## _pack
173
174 #define brw_batch_emit(brw, cmd, name) \
175 for (struct cmd name = { _brw_cmd_header(cmd) }, \
176 *_dst = emit_dwords(brw, _brw_cmd_length(cmd)); \
177 __builtin_expect(_dst != NULL, 1); \
178 _brw_cmd_pack(cmd)(brw, (void *)_dst, &name), \
179 _dst = NULL)
180
181 #define brw_batch_emitn(brw, cmd, n, ...) ({ \
182 uint32_t *_dw = emit_dwords(brw, n); \
183 struct cmd template = { \
184 _brw_cmd_header(cmd), \
185 .DWordLength = n - _brw_cmd_length_bias(cmd), \
186 __VA_ARGS__ \
187 }; \
188 _brw_cmd_pack(cmd)(brw, _dw, &template); \
189 _dw + 1; /* Array starts at dw[1] */ \
190 })
191
192 #define brw_state_emit(brw, cmd, align, offset, name) \
193 for (struct cmd name = {}, \
194 *_dst = brw_state_batch(brw, _brw_cmd_length(cmd) * 4, \
195 align, offset); \
196 __builtin_expect(_dst != NULL, 1); \
197 _brw_cmd_pack(cmd)(brw, (void *)_dst, &name), \
198 _dst = NULL)
199
200 /**
201 * Polygon stipple packet
202 */
203 static void
204 genX(upload_polygon_stipple)(struct brw_context *brw)
205 {
206 struct gl_context *ctx = &brw->ctx;
207
208 /* _NEW_POLYGON */
209 if (!ctx->Polygon.StippleFlag)
210 return;
211
212 brw_batch_emit(brw, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
213 /* Polygon stipple is provided in OpenGL order, i.e. bottom
214 * row first. If we're rendering to a window (i.e. the
215 * default frame buffer object, 0), then we need to invert
216 * it to match our pixel layout. But if we're rendering
217 * to a FBO (i.e. any named frame buffer object), we *don't*
218 * need to invert - we already match the layout.
219 */
220 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
221 for (unsigned i = 0; i < 32; i++)
222 poly.PatternRow[i] = ctx->PolygonStipple[31 - i]; /* invert */
223 } else {
224 for (unsigned i = 0; i < 32; i++)
225 poly.PatternRow[i] = ctx->PolygonStipple[i];
226 }
227 }
228 }
229
230 static const struct brw_tracked_state genX(polygon_stipple) = {
231 .dirty = {
232 .mesa = _NEW_POLYGON |
233 _NEW_POLYGONSTIPPLE,
234 .brw = BRW_NEW_CONTEXT,
235 },
236 .emit = genX(upload_polygon_stipple),
237 };
238
239 /**
240 * Polygon stipple offset packet
241 */
242 static void
243 genX(upload_polygon_stipple_offset)(struct brw_context *brw)
244 {
245 struct gl_context *ctx = &brw->ctx;
246
247 /* _NEW_POLYGON */
248 if (!ctx->Polygon.StippleFlag)
249 return;
250
251 brw_batch_emit(brw, GENX(3DSTATE_POLY_STIPPLE_OFFSET), poly) {
252 /* _NEW_BUFFERS
253 *
254 * If we're drawing to a system window we have to invert the Y axis
255 * in order to match the OpenGL pixel coordinate system, and our
256 * offset must be matched to the window position. If we're drawing
257 * to a user-created FBO then our native pixel coordinate system
258 * works just fine, and there's no window system to worry about.
259 */
260 if (_mesa_is_winsys_fbo(ctx->DrawBuffer)) {
261 poly.PolygonStippleYOffset =
262 (32 - (_mesa_geometric_height(ctx->DrawBuffer) & 31)) & 31;
263 }
264 }
265 }
266
267 static const struct brw_tracked_state genX(polygon_stipple_offset) = {
268 .dirty = {
269 .mesa = _NEW_BUFFERS |
270 _NEW_POLYGON,
271 .brw = BRW_NEW_CONTEXT,
272 },
273 .emit = genX(upload_polygon_stipple_offset),
274 };
275
276 /**
277 * Line stipple packet
278 */
279 static void
280 genX(upload_line_stipple)(struct brw_context *brw)
281 {
282 struct gl_context *ctx = &brw->ctx;
283
284 if (!ctx->Line.StippleFlag)
285 return;
286
287 brw_batch_emit(brw, GENX(3DSTATE_LINE_STIPPLE), line) {
288 line.LineStipplePattern = ctx->Line.StipplePattern;
289
290 line.LineStippleInverseRepeatCount = 1.0f / ctx->Line.StippleFactor;
291 line.LineStippleRepeatCount = ctx->Line.StippleFactor;
292 }
293 }
294
295 static const struct brw_tracked_state genX(line_stipple) = {
296 .dirty = {
297 .mesa = _NEW_LINE,
298 .brw = BRW_NEW_CONTEXT,
299 },
300 .emit = genX(upload_line_stipple),
301 };
302
303 /* Constant single cliprect for framebuffer object or DRI2 drawing */
304 static void
305 genX(upload_drawing_rect)(struct brw_context *brw)
306 {
307 struct gl_context *ctx = &brw->ctx;
308 const struct gl_framebuffer *fb = ctx->DrawBuffer;
309 const unsigned int fb_width = _mesa_geometric_width(fb);
310 const unsigned int fb_height = _mesa_geometric_height(fb);
311
312 brw_batch_emit(brw, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
313 rect.ClippedDrawingRectangleXMax = fb_width - 1;
314 rect.ClippedDrawingRectangleYMax = fb_height - 1;
315 }
316 }
317
318 static const struct brw_tracked_state genX(drawing_rect) = {
319 .dirty = {
320 .mesa = _NEW_BUFFERS,
321 .brw = BRW_NEW_BLORP |
322 BRW_NEW_CONTEXT,
323 },
324 .emit = genX(upload_drawing_rect),
325 };
326
327 static uint32_t *
328 genX(emit_vertex_buffer_state)(struct brw_context *brw,
329 uint32_t *dw,
330 unsigned buffer_nr,
331 struct brw_bo *bo,
332 unsigned start_offset,
333 MAYBE_UNUSED unsigned end_offset,
334 unsigned stride,
335 MAYBE_UNUSED unsigned step_rate)
336 {
337 struct GENX(VERTEX_BUFFER_STATE) buf_state = {
338 .VertexBufferIndex = buffer_nr,
339 .BufferPitch = stride,
340
341 /* The VF cache designers apparently cut corners, and made the cache
342 * only consider the bottom 32 bits of memory addresses. If you happen
343 * to have two vertex buffers which get placed exactly 4 GiB apart and
344 * use them in back-to-back draw calls, you can get collisions. To work
345 * around this problem, we restrict vertex buffers to the low 32 bits of
346 * the address space.
347 */
348 .BufferStartingAddress = ro_32_bo(bo, start_offset),
349 #if GEN_GEN >= 8
350 .BufferSize = end_offset - start_offset,
351 #endif
352
353 #if GEN_GEN >= 7
354 .AddressModifyEnable = true,
355 #endif
356
357 #if GEN_GEN < 8
358 .BufferAccessType = step_rate ? INSTANCEDATA : VERTEXDATA,
359 .InstanceDataStepRate = step_rate,
360 #if GEN_GEN >= 5
361 .EndAddress = ro_bo(bo, end_offset - 1),
362 #endif
363 #endif
364
365 #if GEN_GEN == 11
366 .VertexBufferMOCS = ICL_MOCS_WB,
367 #elif GEN_GEN == 10
368 .VertexBufferMOCS = CNL_MOCS_WB,
369 #elif GEN_GEN == 9
370 .VertexBufferMOCS = SKL_MOCS_WB,
371 #elif GEN_GEN == 8
372 .VertexBufferMOCS = BDW_MOCS_WB,
373 #elif GEN_GEN == 7
374 .VertexBufferMOCS = GEN7_MOCS_L3,
375 #endif
376 };
377
378 GENX(VERTEX_BUFFER_STATE_pack)(brw, dw, &buf_state);
379 return dw + GENX(VERTEX_BUFFER_STATE_length);
380 }
381
382 UNUSED static bool
383 is_passthru_format(uint32_t format)
384 {
385 switch (format) {
386 case ISL_FORMAT_R64_PASSTHRU:
387 case ISL_FORMAT_R64G64_PASSTHRU:
388 case ISL_FORMAT_R64G64B64_PASSTHRU:
389 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
390 return true;
391 default:
392 return false;
393 }
394 }
395
396 UNUSED static int
397 uploads_needed(uint32_t format,
398 bool is_dual_slot)
399 {
400 if (!is_passthru_format(format))
401 return 1;
402
403 if (is_dual_slot)
404 return 2;
405
406 switch (format) {
407 case ISL_FORMAT_R64_PASSTHRU:
408 case ISL_FORMAT_R64G64_PASSTHRU:
409 return 1;
410 case ISL_FORMAT_R64G64B64_PASSTHRU:
411 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
412 return 2;
413 default:
414 unreachable("not reached");
415 }
416 }
417
418 /*
419 * Returns the format that we are finally going to use when upload a vertex
420 * element. It will only change if we are using *64*PASSTHRU formats, as for
421 * gen < 8 they need to be splitted on two *32*FLOAT formats.
422 *
423 * @upload points in which upload we are. Valid values are [0,1]
424 */
425 static uint32_t
426 downsize_format_if_needed(uint32_t format,
427 int upload)
428 {
429 assert(upload == 0 || upload == 1);
430
431 if (!is_passthru_format(format))
432 return format;
433
434 /* ISL_FORMAT_R64_PASSTHRU and ISL_FORMAT_R64G64_PASSTHRU with an upload ==
435 * 1 means that we have been forced to do 2 uploads for a size <= 2. This
436 * happens with gen < 8 and dvec3 or dvec4 vertex shader input
437 * variables. In those cases, we return ISL_FORMAT_R32_FLOAT as a way of
438 * flagging that we want to fill with zeroes this second forced upload.
439 */
440 switch (format) {
441 case ISL_FORMAT_R64_PASSTHRU:
442 return upload == 0 ? ISL_FORMAT_R32G32_FLOAT
443 : ISL_FORMAT_R32_FLOAT;
444 case ISL_FORMAT_R64G64_PASSTHRU:
445 return upload == 0 ? ISL_FORMAT_R32G32B32A32_FLOAT
446 : ISL_FORMAT_R32_FLOAT;
447 case ISL_FORMAT_R64G64B64_PASSTHRU:
448 return upload == 0 ? ISL_FORMAT_R32G32B32A32_FLOAT
449 : ISL_FORMAT_R32G32_FLOAT;
450 case ISL_FORMAT_R64G64B64A64_PASSTHRU:
451 return ISL_FORMAT_R32G32B32A32_FLOAT;
452 default:
453 unreachable("not reached");
454 }
455 }
456
457 /*
458 * Returns the number of componentes associated with a format that is used on
459 * a 64 to 32 format split. See downsize_format()
460 */
461 static int
462 upload_format_size(uint32_t upload_format)
463 {
464 switch (upload_format) {
465 case ISL_FORMAT_R32_FLOAT:
466
467 /* downsized_format has returned this one in order to flag that we are
468 * performing a second upload which we want to have filled with
469 * zeroes. This happens with gen < 8, a size <= 2, and dvec3 or dvec4
470 * vertex shader input variables.
471 */
472
473 return 0;
474 case ISL_FORMAT_R32G32_FLOAT:
475 return 2;
476 case ISL_FORMAT_R32G32B32A32_FLOAT:
477 return 4;
478 default:
479 unreachable("not reached");
480 }
481 }
482
483 static UNUSED uint16_t
484 pinned_bo_high_bits(struct brw_bo *bo)
485 {
486 return (bo->kflags & EXEC_OBJECT_PINNED) ? bo->gtt_offset >> 32ull : 0;
487 }
488
489 /* The VF cache designers apparently cut corners, and made the cache key's
490 * <VertexBufferIndex, Memory Address> tuple only consider the bottom 32 bits
491 * of the address. If you happen to have two vertex buffers which get placed
492 * exactly 4 GiB apart and use them in back-to-back draw calls, you can get
493 * collisions. (These collisions can happen within a single batch.)
494 *
495 * In the soft-pin world, we'd like to assign addresses up front, and never
496 * move buffers. So, we need to do a VF cache invalidate if the buffer for
497 * a particular VB slot has different [48:32] address bits than the last one.
498 *
499 * In the relocation world, we have no idea what the addresses will be, so
500 * we can't apply this workaround. Instead, we tell the kernel to move it
501 * to the low 4GB regardless.
502 */
503 static void
504 vf_invalidate_for_vb_48bit_transitions(struct brw_context *brw)
505 {
506 #if GEN_GEN >= 8
507 bool need_invalidate = true;
508 unsigned i;
509
510 for (i = 0; i < brw->vb.nr_buffers; i++) {
511 uint16_t high_bits = pinned_bo_high_bits(brw->vb.buffers[i].bo);
512
513 if (high_bits != brw->vb.last_bo_high_bits[i]) {
514 need_invalidate = true;
515 brw->vb.last_bo_high_bits[i] = high_bits;
516 }
517 }
518
519 /* Don't bother with draw parameter buffers - those are generated by
520 * the driver so we can select a consistent memory zone.
521 */
522
523 if (need_invalidate) {
524 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_VF_CACHE_INVALIDATE);
525 }
526 #endif
527 }
528
529 static void
530 vf_invalidate_for_ib_48bit_transition(struct brw_context *brw)
531 {
532 #if GEN_GEN >= 8
533 uint16_t high_bits = pinned_bo_high_bits(brw->ib.bo);
534
535 if (high_bits != brw->ib.last_bo_high_bits) {
536 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_VF_CACHE_INVALIDATE);
537 brw->ib.last_bo_high_bits = high_bits;
538 }
539 #endif
540 }
541
542 static void
543 genX(emit_vertices)(struct brw_context *brw)
544 {
545 const struct gen_device_info *devinfo = &brw->screen->devinfo;
546 uint32_t *dw;
547
548 brw_prepare_vertices(brw);
549 brw_prepare_shader_draw_parameters(brw);
550
551 #if GEN_GEN < 6
552 brw_emit_query_begin(brw);
553 #endif
554
555 const struct brw_vs_prog_data *vs_prog_data =
556 brw_vs_prog_data(brw->vs.base.prog_data);
557
558 #if GEN_GEN >= 8
559 struct gl_context *ctx = &brw->ctx;
560 const bool uses_edge_flag = (ctx->Polygon.FrontMode != GL_FILL ||
561 ctx->Polygon.BackMode != GL_FILL);
562
563 if (vs_prog_data->uses_vertexid || vs_prog_data->uses_instanceid) {
564 unsigned vue = brw->vb.nr_enabled;
565
566 /* The element for the edge flags must always be last, so we have to
567 * insert the SGVS before it in that case.
568 */
569 if (uses_edge_flag) {
570 assert(vue > 0);
571 vue--;
572 }
573
574 WARN_ONCE(vue >= 33,
575 "Trying to insert VID/IID past 33rd vertex element, "
576 "need to reorder the vertex attrbutes.");
577
578 brw_batch_emit(brw, GENX(3DSTATE_VF_SGVS), vfs) {
579 if (vs_prog_data->uses_vertexid) {
580 vfs.VertexIDEnable = true;
581 vfs.VertexIDComponentNumber = 2;
582 vfs.VertexIDElementOffset = vue;
583 }
584
585 if (vs_prog_data->uses_instanceid) {
586 vfs.InstanceIDEnable = true;
587 vfs.InstanceIDComponentNumber = 3;
588 vfs.InstanceIDElementOffset = vue;
589 }
590 }
591
592 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
593 vfi.InstancingEnable = true;
594 vfi.VertexElementIndex = vue;
595 }
596 } else {
597 brw_batch_emit(brw, GENX(3DSTATE_VF_SGVS), vfs);
598 }
599 #endif
600
601 const bool uses_draw_params =
602 vs_prog_data->uses_firstvertex ||
603 vs_prog_data->uses_baseinstance;
604
605 const bool uses_derived_draw_params =
606 vs_prog_data->uses_drawid ||
607 vs_prog_data->uses_is_indexed_draw;
608
609 const bool needs_sgvs_element = (uses_draw_params ||
610 vs_prog_data->uses_instanceid ||
611 vs_prog_data->uses_vertexid);
612
613 unsigned nr_elements =
614 brw->vb.nr_enabled + needs_sgvs_element + uses_derived_draw_params;
615
616 #if GEN_GEN < 8
617 /* If any of the formats of vb.enabled needs more that one upload, we need
618 * to add it to nr_elements
619 */
620 for (unsigned i = 0; i < brw->vb.nr_enabled; i++) {
621 struct brw_vertex_element *input = brw->vb.enabled[i];
622 const struct gl_array_attributes *glattrib = input->glattrib;
623 uint32_t format = brw_get_vertex_surface_type(brw, glattrib);
624
625 if (uploads_needed(format, input->is_dual_slot) > 1)
626 nr_elements++;
627 }
628 #endif
629
630 /* If the VS doesn't read any inputs (calculating vertex position from
631 * a state variable for some reason, for example), emit a single pad
632 * VERTEX_ELEMENT struct and bail.
633 *
634 * The stale VB state stays in place, but they don't do anything unless
635 * a VE loads from them.
636 */
637 if (nr_elements == 0) {
638 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_ELEMENTS),
639 1 + GENX(VERTEX_ELEMENT_STATE_length));
640 struct GENX(VERTEX_ELEMENT_STATE) elem = {
641 .Valid = true,
642 .SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT,
643 .Component0Control = VFCOMP_STORE_0,
644 .Component1Control = VFCOMP_STORE_0,
645 .Component2Control = VFCOMP_STORE_0,
646 .Component3Control = VFCOMP_STORE_1_FP,
647 };
648 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem);
649 return;
650 }
651
652 /* Now emit 3DSTATE_VERTEX_BUFFERS and 3DSTATE_VERTEX_ELEMENTS packets. */
653 const unsigned nr_buffers = brw->vb.nr_buffers +
654 uses_draw_params + uses_derived_draw_params;
655
656 vf_invalidate_for_vb_48bit_transitions(brw);
657
658 if (nr_buffers) {
659 assert(nr_buffers <= (GEN_GEN >= 6 ? 33 : 17));
660
661 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_BUFFERS),
662 1 + GENX(VERTEX_BUFFER_STATE_length) * nr_buffers);
663
664 for (unsigned i = 0; i < brw->vb.nr_buffers; i++) {
665 const struct brw_vertex_buffer *buffer = &brw->vb.buffers[i];
666 /* Prior to Haswell and Bay Trail we have to use 4-component formats
667 * to fake 3-component ones. In particular, we do this for
668 * half-float and 8 and 16-bit integer formats. This means that the
669 * vertex element may poke over the end of the buffer by 2 bytes.
670 */
671 const unsigned padding =
672 (GEN_GEN <= 7 && !GEN_IS_HASWELL && !devinfo->is_baytrail) * 2;
673 const unsigned end = buffer->offset + buffer->size + padding;
674 dw = genX(emit_vertex_buffer_state)(brw, dw, i, buffer->bo,
675 buffer->offset,
676 end,
677 buffer->stride,
678 buffer->step_rate);
679 }
680
681 if (uses_draw_params) {
682 dw = genX(emit_vertex_buffer_state)(brw, dw, brw->vb.nr_buffers,
683 brw->draw.draw_params_bo,
684 brw->draw.draw_params_offset,
685 brw->draw.draw_params_bo->size,
686 0 /* stride */,
687 0 /* step rate */);
688 }
689
690 if (uses_derived_draw_params) {
691 dw = genX(emit_vertex_buffer_state)(brw, dw, brw->vb.nr_buffers + 1,
692 brw->draw.derived_draw_params_bo,
693 brw->draw.derived_draw_params_offset,
694 brw->draw.derived_draw_params_bo->size,
695 0 /* stride */,
696 0 /* step rate */);
697 }
698 }
699
700 /* The hardware allows one more VERTEX_ELEMENTS than VERTEX_BUFFERS,
701 * presumably for VertexID/InstanceID.
702 */
703 #if GEN_GEN >= 6
704 assert(nr_elements <= 34);
705 const struct brw_vertex_element *gen6_edgeflag_input = NULL;
706 #else
707 assert(nr_elements <= 18);
708 #endif
709
710 dw = brw_batch_emitn(brw, GENX(3DSTATE_VERTEX_ELEMENTS),
711 1 + GENX(VERTEX_ELEMENT_STATE_length) * nr_elements);
712 unsigned i;
713 for (i = 0; i < brw->vb.nr_enabled; i++) {
714 const struct brw_vertex_element *input = brw->vb.enabled[i];
715 const struct gl_array_attributes *glattrib = input->glattrib;
716 uint32_t format = brw_get_vertex_surface_type(brw, glattrib);
717 uint32_t comp0 = VFCOMP_STORE_SRC;
718 uint32_t comp1 = VFCOMP_STORE_SRC;
719 uint32_t comp2 = VFCOMP_STORE_SRC;
720 uint32_t comp3 = VFCOMP_STORE_SRC;
721 const unsigned num_uploads = GEN_GEN < 8 ?
722 uploads_needed(format, input->is_dual_slot) : 1;
723
724 #if GEN_GEN >= 8
725 /* From the BDW PRM, Volume 2d, page 588 (VERTEX_ELEMENT_STATE):
726 * "Any SourceElementFormat of *64*_PASSTHRU cannot be used with an
727 * element which has edge flag enabled."
728 */
729 assert(!(is_passthru_format(format) && uses_edge_flag));
730 #endif
731
732 /* The gen4 driver expects edgeflag to come in as a float, and passes
733 * that float on to the tests in the clipper. Mesa's current vertex
734 * attribute value for EdgeFlag is stored as a float, which works out.
735 * glEdgeFlagPointer, on the other hand, gives us an unnormalized
736 * integer ubyte. Just rewrite that to convert to a float.
737 *
738 * Gen6+ passes edgeflag as sideband along with the vertex, instead
739 * of in the VUE. We have to upload it sideband as the last vertex
740 * element according to the B-Spec.
741 */
742 #if GEN_GEN >= 6
743 if (input == &brw->vb.inputs[VERT_ATTRIB_EDGEFLAG]) {
744 gen6_edgeflag_input = input;
745 continue;
746 }
747 #endif
748
749 for (unsigned c = 0; c < num_uploads; c++) {
750 const uint32_t upload_format = GEN_GEN >= 8 ? format :
751 downsize_format_if_needed(format, c);
752 /* If we need more that one upload, the offset stride would be 128
753 * bits (16 bytes), as for previous uploads we are using the full
754 * entry. */
755 const unsigned offset = input->offset + c * 16;
756
757 const struct gl_array_attributes *glattrib = input->glattrib;
758 const int size = (GEN_GEN < 8 && is_passthru_format(format)) ?
759 upload_format_size(upload_format) : glattrib->Size;
760
761 switch (size) {
762 case 0: comp0 = VFCOMP_STORE_0;
763 case 1: comp1 = VFCOMP_STORE_0;
764 case 2: comp2 = VFCOMP_STORE_0;
765 case 3:
766 if (GEN_GEN >= 8 && glattrib->Doubles) {
767 comp3 = VFCOMP_STORE_0;
768 } else if (glattrib->Integer) {
769 comp3 = VFCOMP_STORE_1_INT;
770 } else {
771 comp3 = VFCOMP_STORE_1_FP;
772 }
773
774 break;
775 }
776
777 #if GEN_GEN >= 8
778 /* From the BDW PRM, Volume 2d, page 586 (VERTEX_ELEMENT_STATE):
779 *
780 * "When SourceElementFormat is set to one of the *64*_PASSTHRU
781 * formats, 64-bit components are stored in the URB without any
782 * conversion. In this case, vertex elements must be written as 128
783 * or 256 bits, with VFCOMP_STORE_0 being used to pad the output as
784 * required. E.g., if R64_PASSTHRU is used to copy a 64-bit Red
785 * component into the URB, Component 1 must be specified as
786 * VFCOMP_STORE_0 (with Components 2,3 set to VFCOMP_NOSTORE) in
787 * order to output a 128-bit vertex element, or Components 1-3 must
788 * be specified as VFCOMP_STORE_0 in order to output a 256-bit vertex
789 * element. Likewise, use of R64G64B64_PASSTHRU requires Component 3
790 * to be specified as VFCOMP_STORE_0 in order to output a 256-bit
791 * vertex element."
792 */
793 if (glattrib->Doubles && !input->is_dual_slot) {
794 /* Store vertex elements which correspond to double and dvec2 vertex
795 * shader inputs as 128-bit vertex elements, instead of 256-bits.
796 */
797 comp2 = VFCOMP_NOSTORE;
798 comp3 = VFCOMP_NOSTORE;
799 }
800 #endif
801
802 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
803 .VertexBufferIndex = input->buffer,
804 .Valid = true,
805 .SourceElementFormat = upload_format,
806 .SourceElementOffset = offset,
807 .Component0Control = comp0,
808 .Component1Control = comp1,
809 .Component2Control = comp2,
810 .Component3Control = comp3,
811 #if GEN_GEN < 5
812 .DestinationElementOffset = i * 4,
813 #endif
814 };
815
816 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
817 dw += GENX(VERTEX_ELEMENT_STATE_length);
818 }
819 }
820
821 if (needs_sgvs_element) {
822 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
823 .Valid = true,
824 .Component0Control = VFCOMP_STORE_0,
825 .Component1Control = VFCOMP_STORE_0,
826 .Component2Control = VFCOMP_STORE_0,
827 .Component3Control = VFCOMP_STORE_0,
828 #if GEN_GEN < 5
829 .DestinationElementOffset = i * 4,
830 #endif
831 };
832
833 #if GEN_GEN >= 8
834 if (uses_draw_params) {
835 elem_state.VertexBufferIndex = brw->vb.nr_buffers;
836 elem_state.SourceElementFormat = ISL_FORMAT_R32G32_UINT;
837 elem_state.Component0Control = VFCOMP_STORE_SRC;
838 elem_state.Component1Control = VFCOMP_STORE_SRC;
839 }
840 #else
841 elem_state.VertexBufferIndex = brw->vb.nr_buffers;
842 elem_state.SourceElementFormat = ISL_FORMAT_R32G32_UINT;
843 if (uses_draw_params) {
844 elem_state.Component0Control = VFCOMP_STORE_SRC;
845 elem_state.Component1Control = VFCOMP_STORE_SRC;
846 }
847
848 if (vs_prog_data->uses_vertexid)
849 elem_state.Component2Control = VFCOMP_STORE_VID;
850
851 if (vs_prog_data->uses_instanceid)
852 elem_state.Component3Control = VFCOMP_STORE_IID;
853 #endif
854
855 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
856 dw += GENX(VERTEX_ELEMENT_STATE_length);
857 }
858
859 if (uses_derived_draw_params) {
860 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
861 .Valid = true,
862 .VertexBufferIndex = brw->vb.nr_buffers + 1,
863 .SourceElementFormat = ISL_FORMAT_R32G32_UINT,
864 .Component0Control = VFCOMP_STORE_SRC,
865 .Component1Control = VFCOMP_STORE_SRC,
866 .Component2Control = VFCOMP_STORE_0,
867 .Component3Control = VFCOMP_STORE_0,
868 #if GEN_GEN < 5
869 .DestinationElementOffset = i * 4,
870 #endif
871 };
872
873 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
874 dw += GENX(VERTEX_ELEMENT_STATE_length);
875 }
876
877 #if GEN_GEN >= 6
878 if (gen6_edgeflag_input) {
879 const struct gl_array_attributes *glattrib = gen6_edgeflag_input->glattrib;
880 const uint32_t format = brw_get_vertex_surface_type(brw, glattrib);
881
882 struct GENX(VERTEX_ELEMENT_STATE) elem_state = {
883 .Valid = true,
884 .VertexBufferIndex = gen6_edgeflag_input->buffer,
885 .EdgeFlagEnable = true,
886 .SourceElementFormat = format,
887 .SourceElementOffset = gen6_edgeflag_input->offset,
888 .Component0Control = VFCOMP_STORE_SRC,
889 .Component1Control = VFCOMP_STORE_0,
890 .Component2Control = VFCOMP_STORE_0,
891 .Component3Control = VFCOMP_STORE_0,
892 };
893
894 GENX(VERTEX_ELEMENT_STATE_pack)(brw, dw, &elem_state);
895 dw += GENX(VERTEX_ELEMENT_STATE_length);
896 }
897 #endif
898
899 #if GEN_GEN >= 8
900 for (unsigned i = 0, j = 0; i < brw->vb.nr_enabled; i++) {
901 const struct brw_vertex_element *input = brw->vb.enabled[i];
902 const struct brw_vertex_buffer *buffer = &brw->vb.buffers[input->buffer];
903 unsigned element_index;
904
905 /* The edge flag element is reordered to be the last one in the code
906 * above so we need to compensate for that in the element indices used
907 * below.
908 */
909 if (input == gen6_edgeflag_input)
910 element_index = nr_elements - 1;
911 else
912 element_index = j++;
913
914 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
915 vfi.VertexElementIndex = element_index;
916 vfi.InstancingEnable = buffer->step_rate != 0;
917 vfi.InstanceDataStepRate = buffer->step_rate;
918 }
919 }
920
921 if (vs_prog_data->uses_drawid) {
922 const unsigned element = brw->vb.nr_enabled + needs_sgvs_element;
923
924 brw_batch_emit(brw, GENX(3DSTATE_VF_INSTANCING), vfi) {
925 vfi.VertexElementIndex = element;
926 }
927 }
928 #endif
929 }
930
931 static const struct brw_tracked_state genX(vertices) = {
932 .dirty = {
933 .mesa = _NEW_POLYGON,
934 .brw = BRW_NEW_BATCH |
935 BRW_NEW_BLORP |
936 BRW_NEW_VERTICES |
937 BRW_NEW_VS_PROG_DATA,
938 },
939 .emit = genX(emit_vertices),
940 };
941
942 static void
943 genX(emit_index_buffer)(struct brw_context *brw)
944 {
945 const struct _mesa_index_buffer *index_buffer = brw->ib.ib;
946
947 if (index_buffer == NULL)
948 return;
949
950 vf_invalidate_for_ib_48bit_transition(brw);
951
952 brw_batch_emit(brw, GENX(3DSTATE_INDEX_BUFFER), ib) {
953 #if GEN_GEN < 8 && !GEN_IS_HASWELL
954 ib.CutIndexEnable = brw->prim_restart.enable_cut_index;
955 #endif
956 ib.IndexFormat = brw_get_index_type(index_buffer->index_size);
957
958 /* The VF cache designers apparently cut corners, and made the cache
959 * only consider the bottom 32 bits of memory addresses. If you happen
960 * to have two index buffers which get placed exactly 4 GiB apart and
961 * use them in back-to-back draw calls, you can get collisions. To work
962 * around this problem, we restrict index buffers to the low 32 bits of
963 * the address space.
964 */
965 ib.BufferStartingAddress = ro_32_bo(brw->ib.bo, 0);
966 #if GEN_GEN >= 8
967 ib.IndexBufferMOCS = GEN_GEN >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
968 ib.BufferSize = brw->ib.size;
969 #else
970 ib.BufferEndingAddress = ro_bo(brw->ib.bo, brw->ib.size - 1);
971 #endif
972 }
973 }
974
975 static const struct brw_tracked_state genX(index_buffer) = {
976 .dirty = {
977 .mesa = 0,
978 .brw = BRW_NEW_BATCH |
979 BRW_NEW_BLORP |
980 BRW_NEW_INDEX_BUFFER,
981 },
982 .emit = genX(emit_index_buffer),
983 };
984
985 #if GEN_IS_HASWELL || GEN_GEN >= 8
986 static void
987 genX(upload_cut_index)(struct brw_context *brw)
988 {
989 const struct gl_context *ctx = &brw->ctx;
990
991 brw_batch_emit(brw, GENX(3DSTATE_VF), vf) {
992 if (ctx->Array._PrimitiveRestart && brw->ib.ib) {
993 vf.IndexedDrawCutIndexEnable = true;
994 vf.CutIndex = _mesa_primitive_restart_index(ctx, brw->ib.index_size);
995 }
996 }
997 }
998
999 const struct brw_tracked_state genX(cut_index) = {
1000 .dirty = {
1001 .mesa = _NEW_TRANSFORM,
1002 .brw = BRW_NEW_INDEX_BUFFER,
1003 },
1004 .emit = genX(upload_cut_index),
1005 };
1006 #endif
1007
1008 #if GEN_GEN >= 6
1009 /**
1010 * Determine the appropriate attribute override value to store into the
1011 * 3DSTATE_SF structure for a given fragment shader attribute. The attribute
1012 * override value contains two pieces of information: the location of the
1013 * attribute in the VUE (relative to urb_entry_read_offset, see below), and a
1014 * flag indicating whether to "swizzle" the attribute based on the direction
1015 * the triangle is facing.
1016 *
1017 * If an attribute is "swizzled", then the given VUE location is used for
1018 * front-facing triangles, and the VUE location that immediately follows is
1019 * used for back-facing triangles. We use this to implement the mapping from
1020 * gl_FrontColor/gl_BackColor to gl_Color.
1021 *
1022 * urb_entry_read_offset is the offset into the VUE at which the SF unit is
1023 * being instructed to begin reading attribute data. It can be set to a
1024 * nonzero value to prevent the SF unit from wasting time reading elements of
1025 * the VUE that are not needed by the fragment shader. It is measured in
1026 * 256-bit increments.
1027 */
1028 static void
1029 genX(get_attr_override)(struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr,
1030 const struct brw_vue_map *vue_map,
1031 int urb_entry_read_offset, int fs_attr,
1032 bool two_side_color, uint32_t *max_source_attr)
1033 {
1034 /* Find the VUE slot for this attribute. */
1035 int slot = vue_map->varying_to_slot[fs_attr];
1036
1037 /* Viewport and Layer are stored in the VUE header. We need to override
1038 * them to zero if earlier stages didn't write them, as GL requires that
1039 * they read back as zero when not explicitly set.
1040 */
1041 if (fs_attr == VARYING_SLOT_VIEWPORT || fs_attr == VARYING_SLOT_LAYER) {
1042 attr->ComponentOverrideX = true;
1043 attr->ComponentOverrideW = true;
1044 attr->ConstantSource = CONST_0000;
1045
1046 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
1047 attr->ComponentOverrideY = true;
1048 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
1049 attr->ComponentOverrideZ = true;
1050
1051 return;
1052 }
1053
1054 /* If there was only a back color written but not front, use back
1055 * as the color instead of undefined
1056 */
1057 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
1058 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
1059 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
1060 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
1061
1062 if (slot == -1) {
1063 /* This attribute does not exist in the VUE--that means that the vertex
1064 * shader did not write to it. This means that either:
1065 *
1066 * (a) This attribute is a texture coordinate, and it is going to be
1067 * replaced with point coordinates (as a consequence of a call to
1068 * glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE)), so the
1069 * hardware will ignore whatever attribute override we supply.
1070 *
1071 * (b) This attribute is read by the fragment shader but not written by
1072 * the vertex shader, so its value is undefined. Therefore the
1073 * attribute override we supply doesn't matter.
1074 *
1075 * (c) This attribute is gl_PrimitiveID, and it wasn't written by the
1076 * previous shader stage.
1077 *
1078 * Note that we don't have to worry about the cases where the attribute
1079 * is gl_PointCoord or is undergoing point sprite coordinate
1080 * replacement, because in those cases, this function isn't called.
1081 *
1082 * In case (c), we need to program the attribute overrides so that the
1083 * primitive ID will be stored in this slot. In every other case, the
1084 * attribute override we supply doesn't matter. So just go ahead and
1085 * program primitive ID in every case.
1086 */
1087 attr->ComponentOverrideW = true;
1088 attr->ComponentOverrideX = true;
1089 attr->ComponentOverrideY = true;
1090 attr->ComponentOverrideZ = true;
1091 attr->ConstantSource = PRIM_ID;
1092 return;
1093 }
1094
1095 /* Compute the location of the attribute relative to urb_entry_read_offset.
1096 * Each increment of urb_entry_read_offset represents a 256-bit value, so
1097 * it counts for two 128-bit VUE slots.
1098 */
1099 int source_attr = slot - 2 * urb_entry_read_offset;
1100 assert(source_attr >= 0 && source_attr < 32);
1101
1102 /* If we are doing two-sided color, and the VUE slot following this one
1103 * represents a back-facing color, then we need to instruct the SF unit to
1104 * do back-facing swizzling.
1105 */
1106 bool swizzling = two_side_color &&
1107 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
1108 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
1109 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
1110 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1));
1111
1112 /* Update max_source_attr. If swizzling, the SF will read this slot + 1. */
1113 if (*max_source_attr < source_attr + swizzling)
1114 *max_source_attr = source_attr + swizzling;
1115
1116 attr->SourceAttribute = source_attr;
1117 if (swizzling)
1118 attr->SwizzleSelect = INPUTATTR_FACING;
1119 }
1120
1121
1122 static void
1123 genX(calculate_attr_overrides)(const struct brw_context *brw,
1124 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr_overrides,
1125 uint32_t *point_sprite_enables,
1126 uint32_t *urb_entry_read_length,
1127 uint32_t *urb_entry_read_offset)
1128 {
1129 const struct gl_context *ctx = &brw->ctx;
1130
1131 /* _NEW_POINT */
1132 const struct gl_point_attrib *point = &ctx->Point;
1133
1134 /* BRW_NEW_FRAGMENT_PROGRAM */
1135 const struct gl_program *fp = brw->programs[MESA_SHADER_FRAGMENT];
1136
1137 /* BRW_NEW_FS_PROG_DATA */
1138 const struct brw_wm_prog_data *wm_prog_data =
1139 brw_wm_prog_data(brw->wm.base.prog_data);
1140 uint32_t max_source_attr = 0;
1141
1142 *point_sprite_enables = 0;
1143
1144 int first_slot =
1145 brw_compute_first_urb_slot_required(fp->info.inputs_read,
1146 &brw->vue_map_geom_out);
1147
1148 /* Each URB offset packs two varying slots */
1149 assert(first_slot % 2 == 0);
1150 *urb_entry_read_offset = first_slot / 2;
1151
1152 /* From the Ivybridge PRM, Vol 2 Part 1, 3DSTATE_SBE,
1153 * description of dw10 Point Sprite Texture Coordinate Enable:
1154 *
1155 * "This field must be programmed to zero when non-point primitives
1156 * are rendered."
1157 *
1158 * The SandyBridge PRM doesn't explicitly say that point sprite enables
1159 * must be programmed to zero when rendering non-point primitives, but
1160 * the IvyBridge PRM does, and if we don't, we get garbage.
1161 *
1162 * This is not required on Haswell, as the hardware ignores this state
1163 * when drawing non-points -- although we do still need to be careful to
1164 * correctly set the attr overrides.
1165 *
1166 * _NEW_POLYGON
1167 * BRW_NEW_PRIMITIVE | BRW_NEW_GS_PROG_DATA | BRW_NEW_TES_PROG_DATA
1168 */
1169 bool drawing_points = brw_is_drawing_points(brw);
1170
1171 for (int attr = 0; attr < VARYING_SLOT_MAX; attr++) {
1172 int input_index = wm_prog_data->urb_setup[attr];
1173
1174 if (input_index < 0)
1175 continue;
1176
1177 /* _NEW_POINT */
1178 bool point_sprite = false;
1179 if (drawing_points) {
1180 if (point->PointSprite &&
1181 (attr >= VARYING_SLOT_TEX0 && attr <= VARYING_SLOT_TEX7) &&
1182 (point->CoordReplace & (1u << (attr - VARYING_SLOT_TEX0)))) {
1183 point_sprite = true;
1184 }
1185
1186 if (attr == VARYING_SLOT_PNTC)
1187 point_sprite = true;
1188
1189 if (point_sprite)
1190 *point_sprite_enables |= (1 << input_index);
1191 }
1192
1193 /* BRW_NEW_VUE_MAP_GEOM_OUT | _NEW_LIGHT | _NEW_PROGRAM */
1194 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attribute = { 0 };
1195
1196 if (!point_sprite) {
1197 genX(get_attr_override)(&attribute,
1198 &brw->vue_map_geom_out,
1199 *urb_entry_read_offset, attr,
1200 _mesa_vertex_program_two_side_enabled(ctx),
1201 &max_source_attr);
1202 }
1203
1204 /* The hardware can only do the overrides on 16 overrides at a
1205 * time, and the other up to 16 have to be lined up so that the
1206 * input index = the output index. We'll need to do some
1207 * tweaking to make sure that's the case.
1208 */
1209 if (input_index < 16)
1210 attr_overrides[input_index] = attribute;
1211 else
1212 assert(attribute.SourceAttribute == input_index);
1213 }
1214
1215 /* From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
1216 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
1217 *
1218 * "This field should be set to the minimum length required to read the
1219 * maximum source attribute. The maximum source attribute is indicated
1220 * by the maximum value of the enabled Attribute # Source Attribute if
1221 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
1222 * enable is not set.
1223 * read_length = ceiling((max_source_attr + 1) / 2)
1224 *
1225 * [errata] Corruption/Hang possible if length programmed larger than
1226 * recommended"
1227 *
1228 * Similar text exists for Ivy Bridge.
1229 */
1230 *urb_entry_read_length = DIV_ROUND_UP(max_source_attr + 1, 2);
1231 }
1232 #endif
1233
1234 /* ---------------------------------------------------------------------- */
1235
1236 #if GEN_GEN >= 8
1237 typedef struct GENX(3DSTATE_WM_DEPTH_STENCIL) DEPTH_STENCIL_GENXML;
1238 #elif GEN_GEN >= 6
1239 typedef struct GENX(DEPTH_STENCIL_STATE) DEPTH_STENCIL_GENXML;
1240 #else
1241 typedef struct GENX(COLOR_CALC_STATE) DEPTH_STENCIL_GENXML;
1242 #endif
1243
1244 static inline void
1245 set_depth_stencil_bits(struct brw_context *brw, DEPTH_STENCIL_GENXML *ds)
1246 {
1247 struct gl_context *ctx = &brw->ctx;
1248
1249 /* _NEW_BUFFERS */
1250 struct intel_renderbuffer *depth_irb =
1251 intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
1252
1253 /* _NEW_DEPTH */
1254 struct gl_depthbuffer_attrib *depth = &ctx->Depth;
1255
1256 /* _NEW_STENCIL */
1257 struct gl_stencil_attrib *stencil = &ctx->Stencil;
1258 const int b = stencil->_BackFace;
1259
1260 if (depth->Test && depth_irb) {
1261 ds->DepthTestEnable = true;
1262 ds->DepthBufferWriteEnable = brw_depth_writes_enabled(brw);
1263 ds->DepthTestFunction = intel_translate_compare_func(depth->Func);
1264 }
1265
1266 if (brw->stencil_enabled) {
1267 ds->StencilTestEnable = true;
1268 ds->StencilWriteMask = stencil->WriteMask[0] & 0xff;
1269 ds->StencilTestMask = stencil->ValueMask[0] & 0xff;
1270
1271 ds->StencilTestFunction =
1272 intel_translate_compare_func(stencil->Function[0]);
1273 ds->StencilFailOp =
1274 intel_translate_stencil_op(stencil->FailFunc[0]);
1275 ds->StencilPassDepthPassOp =
1276 intel_translate_stencil_op(stencil->ZPassFunc[0]);
1277 ds->StencilPassDepthFailOp =
1278 intel_translate_stencil_op(stencil->ZFailFunc[0]);
1279
1280 ds->StencilBufferWriteEnable = brw->stencil_write_enabled;
1281
1282 if (brw->stencil_two_sided) {
1283 ds->DoubleSidedStencilEnable = true;
1284 ds->BackfaceStencilWriteMask = stencil->WriteMask[b] & 0xff;
1285 ds->BackfaceStencilTestMask = stencil->ValueMask[b] & 0xff;
1286
1287 ds->BackfaceStencilTestFunction =
1288 intel_translate_compare_func(stencil->Function[b]);
1289 ds->BackfaceStencilFailOp =
1290 intel_translate_stencil_op(stencil->FailFunc[b]);
1291 ds->BackfaceStencilPassDepthPassOp =
1292 intel_translate_stencil_op(stencil->ZPassFunc[b]);
1293 ds->BackfaceStencilPassDepthFailOp =
1294 intel_translate_stencil_op(stencil->ZFailFunc[b]);
1295 }
1296
1297 #if GEN_GEN <= 5 || GEN_GEN >= 9
1298 ds->StencilReferenceValue = _mesa_get_stencil_ref(ctx, 0);
1299 ds->BackfaceStencilReferenceValue = _mesa_get_stencil_ref(ctx, b);
1300 #endif
1301 }
1302 }
1303
1304 #if GEN_GEN >= 6
1305 static void
1306 genX(upload_depth_stencil_state)(struct brw_context *brw)
1307 {
1308 #if GEN_GEN >= 8
1309 brw_batch_emit(brw, GENX(3DSTATE_WM_DEPTH_STENCIL), wmds) {
1310 set_depth_stencil_bits(brw, &wmds);
1311 }
1312 #else
1313 uint32_t ds_offset;
1314 brw_state_emit(brw, GENX(DEPTH_STENCIL_STATE), 64, &ds_offset, ds) {
1315 set_depth_stencil_bits(brw, &ds);
1316 }
1317
1318 /* Now upload a pointer to the indirect state */
1319 #if GEN_GEN == 6
1320 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
1321 ptr.PointertoDEPTH_STENCIL_STATE = ds_offset;
1322 ptr.DEPTH_STENCIL_STATEChange = true;
1323 }
1324 #else
1325 brw_batch_emit(brw, GENX(3DSTATE_DEPTH_STENCIL_STATE_POINTERS), ptr) {
1326 ptr.PointertoDEPTH_STENCIL_STATE = ds_offset;
1327 }
1328 #endif
1329 #endif
1330 }
1331
1332 static const struct brw_tracked_state genX(depth_stencil_state) = {
1333 .dirty = {
1334 .mesa = _NEW_BUFFERS |
1335 _NEW_DEPTH |
1336 _NEW_STENCIL,
1337 .brw = BRW_NEW_BLORP |
1338 (GEN_GEN >= 8 ? BRW_NEW_CONTEXT
1339 : BRW_NEW_BATCH |
1340 BRW_NEW_STATE_BASE_ADDRESS),
1341 },
1342 .emit = genX(upload_depth_stencil_state),
1343 };
1344 #endif
1345
1346 /* ---------------------------------------------------------------------- */
1347
1348 #if GEN_GEN <= 5
1349
1350 static void
1351 genX(upload_clip_state)(struct brw_context *brw)
1352 {
1353 struct gl_context *ctx = &brw->ctx;
1354
1355 ctx->NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
1356 brw_state_emit(brw, GENX(CLIP_STATE), 32, &brw->clip.state_offset, clip) {
1357 clip.KernelStartPointer = KSP(brw, brw->clip.prog_offset);
1358 clip.GRFRegisterCount =
1359 DIV_ROUND_UP(brw->clip.prog_data->total_grf, 16) - 1;
1360 clip.FloatingPointMode = FLOATING_POINT_MODE_Alternate;
1361 clip.SingleProgramFlow = true;
1362 clip.VertexURBEntryReadLength = brw->clip.prog_data->urb_read_length;
1363 clip.ConstantURBEntryReadLength = brw->clip.prog_data->curb_read_length;
1364
1365 /* BRW_NEW_PUSH_CONSTANT_ALLOCATION */
1366 clip.ConstantURBEntryReadOffset = brw->curbe.clip_start * 2;
1367 clip.DispatchGRFStartRegisterForURBData = 1;
1368 clip.VertexURBEntryReadOffset = 0;
1369
1370 /* BRW_NEW_URB_FENCE */
1371 clip.NumberofURBEntries = brw->urb.nr_clip_entries;
1372 clip.URBEntryAllocationSize = brw->urb.vsize - 1;
1373
1374 if (brw->urb.nr_clip_entries >= 10) {
1375 /* Half of the URB entries go to each thread, and it has to be an
1376 * even number.
1377 */
1378 assert(brw->urb.nr_clip_entries % 2 == 0);
1379
1380 /* Although up to 16 concurrent Clip threads are allowed on Ironlake,
1381 * only 2 threads can output VUEs at a time.
1382 */
1383 clip.MaximumNumberofThreads = (GEN_GEN == 5 ? 16 : 2) - 1;
1384 } else {
1385 assert(brw->urb.nr_clip_entries >= 5);
1386 clip.MaximumNumberofThreads = 1 - 1;
1387 }
1388
1389 clip.VertexPositionSpace = VPOS_NDCSPACE;
1390 clip.UserClipFlagsMustClipEnable = true;
1391 clip.GuardbandClipTestEnable = true;
1392
1393 clip.ClipperViewportStatePointer =
1394 ro_bo(brw->batch.state.bo, brw->clip.vp_offset);
1395
1396 clip.ScreenSpaceViewportXMin = -1;
1397 clip.ScreenSpaceViewportXMax = 1;
1398 clip.ScreenSpaceViewportYMin = -1;
1399 clip.ScreenSpaceViewportYMax = 1;
1400
1401 clip.ViewportXYClipTestEnable = true;
1402 clip.ViewportZClipTestEnable = !ctx->Transform.DepthClamp;
1403
1404 /* _NEW_TRANSFORM */
1405 if (GEN_GEN == 5 || GEN_IS_G4X) {
1406 clip.UserClipDistanceClipTestEnableBitmask =
1407 ctx->Transform.ClipPlanesEnabled;
1408 } else {
1409 /* Up to 6 actual clip flags, plus the 7th for the negative RHW
1410 * workaround.
1411 */
1412 clip.UserClipDistanceClipTestEnableBitmask =
1413 (ctx->Transform.ClipPlanesEnabled & 0x3f) | 0x40;
1414 }
1415
1416 if (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE)
1417 clip.APIMode = APIMODE_D3D;
1418 else
1419 clip.APIMode = APIMODE_OGL;
1420
1421 clip.GuardbandClipTestEnable = true;
1422
1423 clip.ClipMode = brw->clip.prog_data->clip_mode;
1424
1425 #if GEN_IS_G4X
1426 clip.NegativeWClipTestEnable = true;
1427 #endif
1428 }
1429 }
1430
1431 const struct brw_tracked_state genX(clip_state) = {
1432 .dirty = {
1433 .mesa = _NEW_TRANSFORM |
1434 _NEW_VIEWPORT,
1435 .brw = BRW_NEW_BATCH |
1436 BRW_NEW_BLORP |
1437 BRW_NEW_CLIP_PROG_DATA |
1438 BRW_NEW_PUSH_CONSTANT_ALLOCATION |
1439 BRW_NEW_PROGRAM_CACHE |
1440 BRW_NEW_URB_FENCE,
1441 },
1442 .emit = genX(upload_clip_state),
1443 };
1444
1445 #else
1446
1447 static void
1448 genX(upload_clip_state)(struct brw_context *brw)
1449 {
1450 struct gl_context *ctx = &brw->ctx;
1451
1452 /* _NEW_BUFFERS */
1453 struct gl_framebuffer *fb = ctx->DrawBuffer;
1454
1455 /* BRW_NEW_FS_PROG_DATA */
1456 struct brw_wm_prog_data *wm_prog_data =
1457 brw_wm_prog_data(brw->wm.base.prog_data);
1458
1459 brw_batch_emit(brw, GENX(3DSTATE_CLIP), clip) {
1460 clip.StatisticsEnable = !brw->meta_in_progress;
1461
1462 if (wm_prog_data->barycentric_interp_modes &
1463 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
1464 clip.NonPerspectiveBarycentricEnable = true;
1465
1466 #if GEN_GEN >= 7
1467 clip.EarlyCullEnable = true;
1468 #endif
1469
1470 #if GEN_GEN == 7
1471 clip.FrontWinding = brw->polygon_front_bit == _mesa_is_user_fbo(fb);
1472
1473 if (ctx->Polygon.CullFlag) {
1474 switch (ctx->Polygon.CullFaceMode) {
1475 case GL_FRONT:
1476 clip.CullMode = CULLMODE_FRONT;
1477 break;
1478 case GL_BACK:
1479 clip.CullMode = CULLMODE_BACK;
1480 break;
1481 case GL_FRONT_AND_BACK:
1482 clip.CullMode = CULLMODE_BOTH;
1483 break;
1484 default:
1485 unreachable("Should not get here: invalid CullFlag");
1486 }
1487 } else {
1488 clip.CullMode = CULLMODE_NONE;
1489 }
1490 #endif
1491
1492 #if GEN_GEN < 8
1493 clip.UserClipDistanceCullTestEnableBitmask =
1494 brw_vue_prog_data(brw->vs.base.prog_data)->cull_distance_mask;
1495
1496 clip.ViewportZClipTestEnable = !ctx->Transform.DepthClamp;
1497 #endif
1498
1499 /* _NEW_LIGHT */
1500 if (ctx->Light.ProvokingVertex == GL_FIRST_VERTEX_CONVENTION) {
1501 clip.TriangleStripListProvokingVertexSelect = 0;
1502 clip.TriangleFanProvokingVertexSelect = 1;
1503 clip.LineStripListProvokingVertexSelect = 0;
1504 } else {
1505 clip.TriangleStripListProvokingVertexSelect = 2;
1506 clip.TriangleFanProvokingVertexSelect = 2;
1507 clip.LineStripListProvokingVertexSelect = 1;
1508 }
1509
1510 /* _NEW_TRANSFORM */
1511 clip.UserClipDistanceClipTestEnableBitmask =
1512 ctx->Transform.ClipPlanesEnabled;
1513
1514 #if GEN_GEN >= 8
1515 clip.ForceUserClipDistanceClipTestEnableBitmask = true;
1516 #endif
1517
1518 if (ctx->Transform.ClipDepthMode == GL_ZERO_TO_ONE)
1519 clip.APIMode = APIMODE_D3D;
1520 else
1521 clip.APIMode = APIMODE_OGL;
1522
1523 clip.GuardbandClipTestEnable = true;
1524
1525 /* BRW_NEW_VIEWPORT_COUNT */
1526 const unsigned viewport_count = brw->clip.viewport_count;
1527
1528 if (ctx->RasterDiscard) {
1529 clip.ClipMode = CLIPMODE_REJECT_ALL;
1530 #if GEN_GEN == 6
1531 perf_debug("Rasterizer discard is currently implemented via the "
1532 "clipper; having the GS not write primitives would "
1533 "likely be faster.\n");
1534 #endif
1535 } else {
1536 clip.ClipMode = CLIPMODE_NORMAL;
1537 }
1538
1539 clip.ClipEnable = true;
1540
1541 /* _NEW_POLYGON,
1542 * BRW_NEW_GEOMETRY_PROGRAM | BRW_NEW_TES_PROG_DATA | BRW_NEW_PRIMITIVE
1543 */
1544 if (!brw_is_drawing_points(brw) && !brw_is_drawing_lines(brw))
1545 clip.ViewportXYClipTestEnable = true;
1546
1547 clip.MinimumPointWidth = 0.125;
1548 clip.MaximumPointWidth = 255.875;
1549 clip.MaximumVPIndex = viewport_count - 1;
1550 if (_mesa_geometric_layers(fb) == 0)
1551 clip.ForceZeroRTAIndexEnable = true;
1552 }
1553 }
1554
1555 static const struct brw_tracked_state genX(clip_state) = {
1556 .dirty = {
1557 .mesa = _NEW_BUFFERS |
1558 _NEW_LIGHT |
1559 _NEW_POLYGON |
1560 _NEW_TRANSFORM,
1561 .brw = BRW_NEW_BLORP |
1562 BRW_NEW_CONTEXT |
1563 BRW_NEW_FS_PROG_DATA |
1564 BRW_NEW_GS_PROG_DATA |
1565 BRW_NEW_VS_PROG_DATA |
1566 BRW_NEW_META_IN_PROGRESS |
1567 BRW_NEW_PRIMITIVE |
1568 BRW_NEW_RASTERIZER_DISCARD |
1569 BRW_NEW_TES_PROG_DATA |
1570 BRW_NEW_VIEWPORT_COUNT,
1571 },
1572 .emit = genX(upload_clip_state),
1573 };
1574 #endif
1575
1576 /* ---------------------------------------------------------------------- */
1577
1578 static void
1579 genX(upload_sf)(struct brw_context *brw)
1580 {
1581 struct gl_context *ctx = &brw->ctx;
1582 float point_size;
1583
1584 #if GEN_GEN <= 7
1585 /* _NEW_BUFFERS */
1586 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
1587 UNUSED const bool multisampled_fbo =
1588 _mesa_geometric_samples(ctx->DrawBuffer) > 1;
1589 #endif
1590
1591 #if GEN_GEN < 6
1592 const struct brw_sf_prog_data *sf_prog_data = brw->sf.prog_data;
1593
1594 ctx->NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
1595
1596 brw_state_emit(brw, GENX(SF_STATE), 64, &brw->sf.state_offset, sf) {
1597 sf.KernelStartPointer = KSP(brw, brw->sf.prog_offset);
1598 sf.FloatingPointMode = FLOATING_POINT_MODE_Alternate;
1599 sf.GRFRegisterCount = DIV_ROUND_UP(sf_prog_data->total_grf, 16) - 1;
1600 sf.DispatchGRFStartRegisterForURBData = 3;
1601 sf.VertexURBEntryReadOffset = BRW_SF_URB_ENTRY_READ_OFFSET;
1602 sf.VertexURBEntryReadLength = sf_prog_data->urb_read_length;
1603 sf.NumberofURBEntries = brw->urb.nr_sf_entries;
1604 sf.URBEntryAllocationSize = brw->urb.sfsize - 1;
1605
1606 /* STATE_PREFETCH command description describes this state as being
1607 * something loaded through the GPE (L2 ISC), so it's INSTRUCTION
1608 * domain.
1609 */
1610 sf.SetupViewportStateOffset =
1611 ro_bo(brw->batch.state.bo, brw->sf.vp_offset);
1612
1613 sf.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1614
1615 /* sf.ConstantURBEntryReadLength = stage_prog_data->curb_read_length; */
1616 /* sf.ConstantURBEntryReadOffset = brw->curbe.vs_start * 2; */
1617
1618 sf.MaximumNumberofThreads =
1619 MIN2(GEN_GEN == 5 ? 48 : 24, brw->urb.nr_sf_entries) - 1;
1620
1621 sf.SpritePointEnable = ctx->Point.PointSprite;
1622
1623 sf.DestinationOriginHorizontalBias = 0.5;
1624 sf.DestinationOriginVerticalBias = 0.5;
1625 #else
1626 brw_batch_emit(brw, GENX(3DSTATE_SF), sf) {
1627 sf.StatisticsEnable = true;
1628 #endif
1629 sf.ViewportTransformEnable = true;
1630
1631 #if GEN_GEN == 7
1632 /* _NEW_BUFFERS */
1633 sf.DepthBufferSurfaceFormat = brw_depthbuffer_format(brw);
1634 #endif
1635
1636 #if GEN_GEN <= 7
1637 /* _NEW_POLYGON */
1638 sf.FrontWinding = brw->polygon_front_bit == render_to_fbo;
1639 #if GEN_GEN >= 6
1640 sf.GlobalDepthOffsetEnableSolid = ctx->Polygon.OffsetFill;
1641 sf.GlobalDepthOffsetEnableWireframe = ctx->Polygon.OffsetLine;
1642 sf.GlobalDepthOffsetEnablePoint = ctx->Polygon.OffsetPoint;
1643
1644 switch (ctx->Polygon.FrontMode) {
1645 case GL_FILL:
1646 sf.FrontFaceFillMode = FILL_MODE_SOLID;
1647 break;
1648 case GL_LINE:
1649 sf.FrontFaceFillMode = FILL_MODE_WIREFRAME;
1650 break;
1651 case GL_POINT:
1652 sf.FrontFaceFillMode = FILL_MODE_POINT;
1653 break;
1654 default:
1655 unreachable("not reached");
1656 }
1657
1658 switch (ctx->Polygon.BackMode) {
1659 case GL_FILL:
1660 sf.BackFaceFillMode = FILL_MODE_SOLID;
1661 break;
1662 case GL_LINE:
1663 sf.BackFaceFillMode = FILL_MODE_WIREFRAME;
1664 break;
1665 case GL_POINT:
1666 sf.BackFaceFillMode = FILL_MODE_POINT;
1667 break;
1668 default:
1669 unreachable("not reached");
1670 }
1671
1672 if (multisampled_fbo && ctx->Multisample.Enabled)
1673 sf.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
1674
1675 sf.GlobalDepthOffsetConstant = ctx->Polygon.OffsetUnits * 2;
1676 sf.GlobalDepthOffsetScale = ctx->Polygon.OffsetFactor;
1677 sf.GlobalDepthOffsetClamp = ctx->Polygon.OffsetClamp;
1678 #endif
1679
1680 sf.ScissorRectangleEnable = true;
1681
1682 if (ctx->Polygon.CullFlag) {
1683 switch (ctx->Polygon.CullFaceMode) {
1684 case GL_FRONT:
1685 sf.CullMode = CULLMODE_FRONT;
1686 break;
1687 case GL_BACK:
1688 sf.CullMode = CULLMODE_BACK;
1689 break;
1690 case GL_FRONT_AND_BACK:
1691 sf.CullMode = CULLMODE_BOTH;
1692 break;
1693 default:
1694 unreachable("not reached");
1695 }
1696 } else {
1697 sf.CullMode = CULLMODE_NONE;
1698 }
1699
1700 #if GEN_IS_HASWELL
1701 sf.LineStippleEnable = ctx->Line.StippleFlag;
1702 #endif
1703
1704 #endif
1705
1706 /* _NEW_LINE */
1707 #if GEN_GEN == 8
1708 const struct gen_device_info *devinfo = &brw->screen->devinfo;
1709
1710 if (devinfo->is_cherryview)
1711 sf.CHVLineWidth = brw_get_line_width(brw);
1712 else
1713 sf.LineWidth = brw_get_line_width(brw);
1714 #else
1715 sf.LineWidth = brw_get_line_width(brw);
1716 #endif
1717
1718 if (ctx->Line.SmoothFlag) {
1719 sf.LineEndCapAntialiasingRegionWidth = _10pixels;
1720 #if GEN_GEN <= 7
1721 sf.AntiAliasingEnable = true;
1722 #endif
1723 }
1724
1725 /* _NEW_POINT - Clamp to ARB_point_parameters user limits */
1726 point_size = CLAMP(ctx->Point.Size, ctx->Point.MinSize, ctx->Point.MaxSize);
1727 /* Clamp to the hardware limits */
1728 sf.PointWidth = CLAMP(point_size, 0.125f, 255.875f);
1729
1730 /* _NEW_PROGRAM | _NEW_POINT, BRW_NEW_VUE_MAP_GEOM_OUT */
1731 if (use_state_point_size(brw))
1732 sf.PointWidthSource = State;
1733
1734 #if GEN_GEN >= 8
1735 /* _NEW_POINT | _NEW_MULTISAMPLE */
1736 if ((ctx->Point.SmoothFlag || _mesa_is_multisample_enabled(ctx)) &&
1737 !ctx->Point.PointSprite)
1738 sf.SmoothPointEnable = true;
1739 #endif
1740
1741 #if GEN_GEN == 10
1742 /* _NEW_BUFFERS
1743 * Smooth Point Enable bit MUST not be set when NUM_MULTISAMPLES > 1.
1744 */
1745 const bool multisampled_fbo =
1746 _mesa_geometric_samples(ctx->DrawBuffer) > 1;
1747 if (multisampled_fbo)
1748 sf.SmoothPointEnable = false;
1749 #endif
1750
1751 #if GEN_IS_G4X || GEN_GEN >= 5
1752 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
1753 #endif
1754
1755 /* _NEW_LIGHT */
1756 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION) {
1757 sf.TriangleStripListProvokingVertexSelect = 2;
1758 sf.TriangleFanProvokingVertexSelect = 2;
1759 sf.LineStripListProvokingVertexSelect = 1;
1760 } else {
1761 sf.TriangleFanProvokingVertexSelect = 1;
1762 }
1763
1764 #if GEN_GEN == 6
1765 /* BRW_NEW_FS_PROG_DATA */
1766 const struct brw_wm_prog_data *wm_prog_data =
1767 brw_wm_prog_data(brw->wm.base.prog_data);
1768
1769 sf.AttributeSwizzleEnable = true;
1770 sf.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1771
1772 /*
1773 * Window coordinates in an FBO are inverted, which means point
1774 * sprite origin must be inverted, too.
1775 */
1776 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo) {
1777 sf.PointSpriteTextureCoordinateOrigin = LOWERLEFT;
1778 } else {
1779 sf.PointSpriteTextureCoordinateOrigin = UPPERLEFT;
1780 }
1781
1782 /* BRW_NEW_VUE_MAP_GEOM_OUT | BRW_NEW_FRAGMENT_PROGRAM |
1783 * _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM | BRW_NEW_FS_PROG_DATA
1784 */
1785 uint32_t urb_entry_read_length;
1786 uint32_t urb_entry_read_offset;
1787 uint32_t point_sprite_enables;
1788 genX(calculate_attr_overrides)(brw, sf.Attribute, &point_sprite_enables,
1789 &urb_entry_read_length,
1790 &urb_entry_read_offset);
1791 sf.VertexURBEntryReadLength = urb_entry_read_length;
1792 sf.VertexURBEntryReadOffset = urb_entry_read_offset;
1793 sf.PointSpriteTextureCoordinateEnable = point_sprite_enables;
1794 sf.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
1795 #endif
1796 }
1797 }
1798
1799 static const struct brw_tracked_state genX(sf_state) = {
1800 .dirty = {
1801 .mesa = _NEW_LIGHT |
1802 _NEW_LINE |
1803 _NEW_POINT |
1804 _NEW_PROGRAM |
1805 (GEN_GEN >= 6 ? _NEW_MULTISAMPLE : 0) |
1806 (GEN_GEN <= 7 ? _NEW_BUFFERS | _NEW_POLYGON : 0) |
1807 (GEN_GEN == 10 ? _NEW_BUFFERS : 0),
1808 .brw = BRW_NEW_BLORP |
1809 BRW_NEW_VUE_MAP_GEOM_OUT |
1810 (GEN_GEN <= 5 ? BRW_NEW_BATCH |
1811 BRW_NEW_PROGRAM_CACHE |
1812 BRW_NEW_SF_PROG_DATA |
1813 BRW_NEW_SF_VP |
1814 BRW_NEW_URB_FENCE
1815 : 0) |
1816 (GEN_GEN >= 6 ? BRW_NEW_CONTEXT : 0) |
1817 (GEN_GEN >= 6 && GEN_GEN <= 7 ?
1818 BRW_NEW_GS_PROG_DATA |
1819 BRW_NEW_PRIMITIVE |
1820 BRW_NEW_TES_PROG_DATA
1821 : 0) |
1822 (GEN_GEN == 6 ? BRW_NEW_FS_PROG_DATA |
1823 BRW_NEW_FRAGMENT_PROGRAM
1824 : 0),
1825 },
1826 .emit = genX(upload_sf),
1827 };
1828
1829 /* ---------------------------------------------------------------------- */
1830
1831 static bool
1832 brw_color_buffer_write_enabled(struct brw_context *brw)
1833 {
1834 struct gl_context *ctx = &brw->ctx;
1835 /* BRW_NEW_FRAGMENT_PROGRAM */
1836 const struct gl_program *fp = brw->programs[MESA_SHADER_FRAGMENT];
1837 unsigned i;
1838
1839 /* _NEW_BUFFERS */
1840 for (i = 0; i < ctx->DrawBuffer->_NumColorDrawBuffers; i++) {
1841 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
1842 uint64_t outputs_written = fp->info.outputs_written;
1843
1844 /* _NEW_COLOR */
1845 if (rb && (outputs_written & BITFIELD64_BIT(FRAG_RESULT_COLOR) ||
1846 outputs_written & BITFIELD64_BIT(FRAG_RESULT_DATA0 + i)) &&
1847 GET_COLORMASK(ctx->Color.ColorMask, i)) {
1848 return true;
1849 }
1850 }
1851
1852 return false;
1853 }
1854
1855 static void
1856 genX(upload_wm)(struct brw_context *brw)
1857 {
1858 struct gl_context *ctx = &brw->ctx;
1859
1860 /* BRW_NEW_FS_PROG_DATA */
1861 const struct brw_wm_prog_data *wm_prog_data =
1862 brw_wm_prog_data(brw->wm.base.prog_data);
1863
1864 UNUSED bool writes_depth =
1865 wm_prog_data->computed_depth_mode != BRW_PSCDEPTH_OFF;
1866 UNUSED struct brw_stage_state *stage_state = &brw->wm.base;
1867 UNUSED const struct gen_device_info *devinfo = &brw->screen->devinfo;
1868
1869 #if GEN_GEN == 6
1870 /* We can't fold this into gen6_upload_wm_push_constants(), because
1871 * according to the SNB PRM, vol 2 part 1 section 7.2.2
1872 * (3DSTATE_CONSTANT_PS [DevSNB]):
1873 *
1874 * "[DevSNB]: This packet must be followed by WM_STATE."
1875 */
1876 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_PS), wmcp) {
1877 if (wm_prog_data->base.nr_params != 0) {
1878 wmcp.Buffer0Valid = true;
1879 /* Pointer to the WM constant buffer. Covered by the set of
1880 * state flags from gen6_upload_wm_push_constants.
1881 */
1882 wmcp.PointertoPSConstantBuffer0 = stage_state->push_const_offset;
1883 wmcp.PSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
1884 }
1885 }
1886 #endif
1887
1888 #if GEN_GEN >= 6
1889 brw_batch_emit(brw, GENX(3DSTATE_WM), wm) {
1890 wm.LineAntialiasingRegionWidth = _10pixels;
1891 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
1892
1893 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
1894 wm.BarycentricInterpolationMode = wm_prog_data->barycentric_interp_modes;
1895 #else
1896 ctx->NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
1897 brw_state_emit(brw, GENX(WM_STATE), 64, &stage_state->state_offset, wm) {
1898 if (wm_prog_data->dispatch_8 && wm_prog_data->dispatch_16) {
1899 /* These two fields should be the same pre-gen6, which is why we
1900 * only have one hardware field to program for both dispatch
1901 * widths.
1902 */
1903 assert(wm_prog_data->base.dispatch_grf_start_reg ==
1904 wm_prog_data->dispatch_grf_start_reg_2);
1905 }
1906
1907 if (wm_prog_data->dispatch_8 || wm_prog_data->dispatch_16)
1908 wm.GRFRegisterCount0 = wm_prog_data->reg_blocks_0;
1909
1910 if (stage_state->sampler_count)
1911 wm.SamplerStatePointer =
1912 ro_bo(brw->batch.state.bo, stage_state->sampler_offset);
1913 #if GEN_GEN == 5
1914 if (wm_prog_data->prog_offset_2)
1915 wm.GRFRegisterCount2 = wm_prog_data->reg_blocks_2;
1916 #endif
1917
1918 wm.SetupURBEntryReadLength = wm_prog_data->num_varying_inputs * 2;
1919 wm.ConstantURBEntryReadLength = wm_prog_data->base.curb_read_length;
1920 /* BRW_NEW_PUSH_CONSTANT_ALLOCATION */
1921 wm.ConstantURBEntryReadOffset = brw->curbe.wm_start * 2;
1922 wm.EarlyDepthTestEnable = true;
1923 wm.LineAntialiasingRegionWidth = _05pixels;
1924 wm.LineEndCapAntialiasingRegionWidth = _10pixels;
1925
1926 /* _NEW_POLYGON */
1927 if (ctx->Polygon.OffsetFill) {
1928 wm.GlobalDepthOffsetEnable = true;
1929 /* Something weird going on with legacy_global_depth_bias,
1930 * offset_constant, scaling and MRD. This value passes glean
1931 * but gives some odd results elsewere (eg. the
1932 * quad-offset-units test).
1933 */
1934 wm.GlobalDepthOffsetConstant = ctx->Polygon.OffsetUnits * 2;
1935
1936 /* This is the only value that passes glean:
1937 */
1938 wm.GlobalDepthOffsetScale = ctx->Polygon.OffsetFactor;
1939 }
1940
1941 wm.DepthCoefficientURBReadOffset = 1;
1942 #endif
1943
1944 /* BRW_NEW_STATS_WM */
1945 wm.StatisticsEnable = GEN_GEN >= 6 || brw->stats_wm;
1946
1947 #if GEN_GEN < 7
1948 if (wm_prog_data->base.use_alt_mode)
1949 wm.FloatingPointMode = FLOATING_POINT_MODE_Alternate;
1950
1951 wm.SamplerCount = GEN_GEN == 5 ?
1952 0 : DIV_ROUND_UP(stage_state->sampler_count, 4);
1953
1954 wm.BindingTableEntryCount =
1955 wm_prog_data->base.binding_table.size_bytes / 4;
1956 wm.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
1957 wm._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1958 wm._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1959 wm.DispatchGRFStartRegisterForConstantSetupData0 =
1960 wm_prog_data->base.dispatch_grf_start_reg;
1961 if (GEN_GEN == 6 ||
1962 wm_prog_data->dispatch_8 || wm_prog_data->dispatch_16) {
1963 wm.KernelStartPointer0 = KSP(brw, stage_state->prog_offset);
1964 }
1965
1966 #if GEN_GEN >= 5
1967 if (GEN_GEN == 6 || wm_prog_data->prog_offset_2) {
1968 wm.KernelStartPointer2 =
1969 KSP(brw, stage_state->prog_offset + wm_prog_data->prog_offset_2);
1970 }
1971 #endif
1972
1973 #if GEN_GEN == 6
1974 wm.DualSourceBlendEnable =
1975 wm_prog_data->dual_src_blend && (ctx->Color.BlendEnabled & 1) &&
1976 ctx->Color.Blend[0]._UsesDualSrc;
1977 wm.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
1978 wm.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1979
1980 /* From the SNB PRM, volume 2 part 1, page 281:
1981 * "If the PS kernel does not need the Position XY Offsets
1982 * to compute a Position XY value, then this field should be
1983 * programmed to POSOFFSET_NONE."
1984 *
1985 * "SW Recommendation: If the PS kernel needs the Position Offsets
1986 * to compute a Position XY value, this field should match Position
1987 * ZW Interpolation Mode to ensure a consistent position.xyzw
1988 * computation."
1989 * We only require XY sample offsets. So, this recommendation doesn't
1990 * look useful at the moment. We might need this in future.
1991 */
1992 if (wm_prog_data->uses_pos_offset)
1993 wm.PositionXYOffsetSelect = POSOFFSET_SAMPLE;
1994 else
1995 wm.PositionXYOffsetSelect = POSOFFSET_NONE;
1996
1997 wm.DispatchGRFStartRegisterForConstantSetupData2 =
1998 wm_prog_data->dispatch_grf_start_reg_2;
1999 #endif
2000
2001 if (wm_prog_data->base.total_scratch) {
2002 wm.ScratchSpaceBasePointer = rw_32_bo(stage_state->scratch_bo, 0);
2003 wm.PerThreadScratchSpace =
2004 ffs(stage_state->per_thread_scratch) - 11;
2005 }
2006
2007 wm.PixelShaderComputedDepth = writes_depth;
2008 #endif
2009
2010 /* _NEW_LINE */
2011 wm.LineStippleEnable = ctx->Line.StippleFlag;
2012
2013 /* _NEW_POLYGON */
2014 wm.PolygonStippleEnable = ctx->Polygon.StippleFlag;
2015
2016 #if GEN_GEN < 8
2017
2018 #if GEN_GEN >= 6
2019 wm.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
2020
2021 /* _NEW_BUFFERS */
2022 const bool multisampled_fbo = _mesa_geometric_samples(ctx->DrawBuffer) > 1;
2023
2024 if (multisampled_fbo) {
2025 /* _NEW_MULTISAMPLE */
2026 if (ctx->Multisample.Enabled)
2027 wm.MultisampleRasterizationMode = MSRASTMODE_ON_PATTERN;
2028 else
2029 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
2030
2031 if (wm_prog_data->persample_dispatch)
2032 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
2033 else
2034 wm.MultisampleDispatchMode = MSDISPMODE_PERPIXEL;
2035 } else {
2036 wm.MultisampleRasterizationMode = MSRASTMODE_OFF_PIXEL;
2037 wm.MultisampleDispatchMode = MSDISPMODE_PERSAMPLE;
2038 }
2039 #endif
2040 wm.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
2041 if (wm_prog_data->uses_kill ||
2042 _mesa_is_alpha_test_enabled(ctx) ||
2043 _mesa_is_alpha_to_coverage_enabled(ctx) ||
2044 (GEN_GEN >= 6 && wm_prog_data->uses_omask)) {
2045 wm.PixelShaderKillsPixel = true;
2046 }
2047
2048 /* _NEW_BUFFERS | _NEW_COLOR */
2049 if (brw_color_buffer_write_enabled(brw) || writes_depth ||
2050 wm.PixelShaderKillsPixel ||
2051 (GEN_GEN >= 6 && wm_prog_data->has_side_effects)) {
2052 wm.ThreadDispatchEnable = true;
2053 }
2054
2055 #if GEN_GEN >= 7
2056 wm.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
2057 wm.PixelShaderUsesInputCoverageMask = wm_prog_data->uses_sample_mask;
2058 #endif
2059
2060 /* The "UAV access enable" bits are unnecessary on HSW because they only
2061 * seem to have an effect on the HW-assisted coherency mechanism which we
2062 * don't need, and the rasterization-related UAV_ONLY flag and the
2063 * DISPATCH_ENABLE bit can be set independently from it.
2064 * C.f. gen8_upload_ps_extra().
2065 *
2066 * BRW_NEW_FRAGMENT_PROGRAM | BRW_NEW_FS_PROG_DATA | _NEW_BUFFERS |
2067 * _NEW_COLOR
2068 */
2069 #if GEN_IS_HASWELL
2070 if (!(brw_color_buffer_write_enabled(brw) || writes_depth) &&
2071 wm_prog_data->has_side_effects)
2072 wm.PSUAVonly = ON;
2073 #endif
2074 #endif
2075
2076 #if GEN_GEN >= 7
2077 /* BRW_NEW_FS_PROG_DATA */
2078 if (wm_prog_data->early_fragment_tests)
2079 wm.EarlyDepthStencilControl = EDSC_PREPS;
2080 else if (wm_prog_data->has_side_effects)
2081 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
2082 #endif
2083 }
2084
2085 #if GEN_GEN <= 5
2086 if (brw->wm.offset_clamp != ctx->Polygon.OffsetClamp) {
2087 brw_batch_emit(brw, GENX(3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP), clamp) {
2088 clamp.GlobalDepthOffsetClamp = ctx->Polygon.OffsetClamp;
2089 }
2090
2091 brw->wm.offset_clamp = ctx->Polygon.OffsetClamp;
2092 }
2093 #endif
2094 }
2095
2096 static const struct brw_tracked_state genX(wm_state) = {
2097 .dirty = {
2098 .mesa = _NEW_LINE |
2099 _NEW_POLYGON |
2100 (GEN_GEN < 8 ? _NEW_BUFFERS |
2101 _NEW_COLOR :
2102 0) |
2103 (GEN_GEN == 6 ? _NEW_PROGRAM_CONSTANTS : 0) |
2104 (GEN_GEN < 6 ? _NEW_POLYGONSTIPPLE : 0) |
2105 (GEN_GEN < 8 && GEN_GEN >= 6 ? _NEW_MULTISAMPLE : 0),
2106 .brw = BRW_NEW_BLORP |
2107 BRW_NEW_FS_PROG_DATA |
2108 (GEN_GEN < 6 ? BRW_NEW_PUSH_CONSTANT_ALLOCATION |
2109 BRW_NEW_FRAGMENT_PROGRAM |
2110 BRW_NEW_PROGRAM_CACHE |
2111 BRW_NEW_SAMPLER_STATE_TABLE |
2112 BRW_NEW_STATS_WM
2113 : 0) |
2114 (GEN_GEN < 7 ? BRW_NEW_BATCH : BRW_NEW_CONTEXT),
2115 },
2116 .emit = genX(upload_wm),
2117 };
2118
2119 /* ---------------------------------------------------------------------- */
2120
2121 /* We restrict scratch buffers to the bottom 32 bits of the address space
2122 * by using rw_32_bo().
2123 *
2124 * General State Base Address is a bit broken. If the address + size as
2125 * seen by STATE_BASE_ADDRESS overflows 48 bits, the GPU appears to treat
2126 * all accesses to the buffer as being out of bounds and returns zero.
2127 */
2128
2129 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
2130 pkt.KernelStartPointer = KSP(brw, stage_state->prog_offset); \
2131 pkt.SamplerCount = \
2132 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
2133 pkt.BindingTableEntryCount = \
2134 stage_prog_data->binding_table.size_bytes / 4; \
2135 pkt.FloatingPointMode = stage_prog_data->use_alt_mode; \
2136 \
2137 if (stage_prog_data->total_scratch) { \
2138 pkt.ScratchSpaceBasePointer = rw_32_bo(stage_state->scratch_bo, 0); \
2139 pkt.PerThreadScratchSpace = \
2140 ffs(stage_state->per_thread_scratch) - 11; \
2141 } \
2142 \
2143 pkt.DispatchGRFStartRegisterForURBData = \
2144 stage_prog_data->dispatch_grf_start_reg; \
2145 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
2146 pkt.prefix##URBEntryReadOffset = 0; \
2147 \
2148 pkt.StatisticsEnable = true; \
2149 pkt.Enable = true;
2150
2151 static void
2152 genX(upload_vs_state)(struct brw_context *brw)
2153 {
2154 UNUSED struct gl_context *ctx = &brw->ctx;
2155 const struct gen_device_info *devinfo = &brw->screen->devinfo;
2156 struct brw_stage_state *stage_state = &brw->vs.base;
2157
2158 /* BRW_NEW_VS_PROG_DATA */
2159 const struct brw_vue_prog_data *vue_prog_data =
2160 brw_vue_prog_data(brw->vs.base.prog_data);
2161 const struct brw_stage_prog_data *stage_prog_data = &vue_prog_data->base;
2162
2163 assert(vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8 ||
2164 vue_prog_data->dispatch_mode == DISPATCH_MODE_4X2_DUAL_OBJECT);
2165 assert(GEN_GEN < 11 ||
2166 vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8);
2167
2168 #if GEN_GEN == 6
2169 /* From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
2170 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
2171 *
2172 * [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
2173 * command that causes the VS Function Enable to toggle. Pipeline
2174 * flush can be executed by sending a PIPE_CONTROL command with CS
2175 * stall bit set and a post sync operation.
2176 *
2177 * We've already done such a flush at the start of state upload, so we
2178 * don't need to do another one here.
2179 */
2180 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_VS), cvs) {
2181 if (stage_state->push_const_size != 0) {
2182 cvs.Buffer0Valid = true;
2183 cvs.PointertoVSConstantBuffer0 = stage_state->push_const_offset;
2184 cvs.VSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
2185 }
2186 }
2187 #endif
2188
2189 if (GEN_GEN == 7 && devinfo->is_ivybridge)
2190 gen7_emit_vs_workaround_flush(brw);
2191
2192 #if GEN_GEN >= 6
2193 brw_batch_emit(brw, GENX(3DSTATE_VS), vs) {
2194 #else
2195 ctx->NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
2196 brw_state_emit(brw, GENX(VS_STATE), 32, &stage_state->state_offset, vs) {
2197 #endif
2198 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
2199
2200 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
2201
2202 #if GEN_GEN < 6
2203 vs.GRFRegisterCount = DIV_ROUND_UP(vue_prog_data->total_grf, 16) - 1;
2204 vs.ConstantURBEntryReadLength = stage_prog_data->curb_read_length;
2205 vs.ConstantURBEntryReadOffset = brw->curbe.vs_start * 2;
2206
2207 vs.NumberofURBEntries = brw->urb.nr_vs_entries >> (GEN_GEN == 5 ? 2 : 0);
2208 vs.URBEntryAllocationSize = brw->urb.vsize - 1;
2209
2210 vs.MaximumNumberofThreads =
2211 CLAMP(brw->urb.nr_vs_entries / 2, 1, devinfo->max_vs_threads) - 1;
2212
2213 vs.StatisticsEnable = false;
2214 vs.SamplerStatePointer =
2215 ro_bo(brw->batch.state.bo, stage_state->sampler_offset);
2216 #endif
2217
2218 #if GEN_GEN == 5
2219 /* Force single program flow on Ironlake. We cannot reliably get
2220 * all applications working without it. See:
2221 * https://bugs.freedesktop.org/show_bug.cgi?id=29172
2222 *
2223 * The most notable and reliably failing application is the Humus
2224 * demo "CelShading"
2225 */
2226 vs.SingleProgramFlow = true;
2227 vs.SamplerCount = 0; /* hardware requirement */
2228 #endif
2229
2230 #if GEN_GEN >= 8
2231 vs.SIMD8DispatchEnable =
2232 vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8;
2233
2234 vs.UserClipDistanceCullTestEnableBitmask =
2235 vue_prog_data->cull_distance_mask;
2236 #endif
2237 }
2238
2239 #if GEN_GEN == 6
2240 /* Based on my reading of the simulator, the VS constants don't get
2241 * pulled into the VS FF unit until an appropriate pipeline flush
2242 * happens, and instead the 3DSTATE_CONSTANT_VS packet just adds
2243 * references to them into a little FIFO. The flushes are common,
2244 * but don't reliably happen between this and a 3DPRIMITIVE, causing
2245 * the primitive to use the wrong constants. Then the FIFO
2246 * containing the constant setup gets added to again on the next
2247 * constants change, and eventually when a flush does happen the
2248 * unit is overwhelmed by constant changes and dies.
2249 *
2250 * To avoid this, send a PIPE_CONTROL down the line that will
2251 * update the unit immediately loading the constants. The flush
2252 * type bits here were those set by the STATE_BASE_ADDRESS whose
2253 * move in a82a43e8d99e1715dd11c9c091b5ab734079b6a6 triggered the
2254 * bug reports that led to this workaround, and may be more than
2255 * what is strictly required to avoid the issue.
2256 */
2257 brw_emit_pipe_control_flush(brw,
2258 PIPE_CONTROL_DEPTH_STALL |
2259 PIPE_CONTROL_INSTRUCTION_INVALIDATE |
2260 PIPE_CONTROL_STATE_CACHE_INVALIDATE);
2261 #endif
2262 }
2263
2264 static const struct brw_tracked_state genX(vs_state) = {
2265 .dirty = {
2266 .mesa = (GEN_GEN == 6 ? (_NEW_PROGRAM_CONSTANTS | _NEW_TRANSFORM) : 0),
2267 .brw = BRW_NEW_BATCH |
2268 BRW_NEW_BLORP |
2269 BRW_NEW_CONTEXT |
2270 BRW_NEW_VS_PROG_DATA |
2271 (GEN_GEN == 6 ? BRW_NEW_VERTEX_PROGRAM : 0) |
2272 (GEN_GEN <= 5 ? BRW_NEW_PUSH_CONSTANT_ALLOCATION |
2273 BRW_NEW_PROGRAM_CACHE |
2274 BRW_NEW_SAMPLER_STATE_TABLE |
2275 BRW_NEW_URB_FENCE
2276 : 0),
2277 },
2278 .emit = genX(upload_vs_state),
2279 };
2280
2281 /* ---------------------------------------------------------------------- */
2282
2283 static void
2284 genX(upload_cc_viewport)(struct brw_context *brw)
2285 {
2286 struct gl_context *ctx = &brw->ctx;
2287
2288 /* BRW_NEW_VIEWPORT_COUNT */
2289 const unsigned viewport_count = brw->clip.viewport_count;
2290
2291 struct GENX(CC_VIEWPORT) ccv;
2292 uint32_t cc_vp_offset;
2293 uint32_t *cc_map =
2294 brw_state_batch(brw, 4 * GENX(CC_VIEWPORT_length) * viewport_count,
2295 32, &cc_vp_offset);
2296
2297 for (unsigned i = 0; i < viewport_count; i++) {
2298 /* _NEW_VIEWPORT | _NEW_TRANSFORM */
2299 const struct gl_viewport_attrib *vp = &ctx->ViewportArray[i];
2300 if (ctx->Transform.DepthClamp) {
2301 ccv.MinimumDepth = MIN2(vp->Near, vp->Far);
2302 ccv.MaximumDepth = MAX2(vp->Near, vp->Far);
2303 } else {
2304 ccv.MinimumDepth = 0.0;
2305 ccv.MaximumDepth = 1.0;
2306 }
2307 GENX(CC_VIEWPORT_pack)(NULL, cc_map, &ccv);
2308 cc_map += GENX(CC_VIEWPORT_length);
2309 }
2310
2311 #if GEN_GEN >= 7
2312 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
2313 ptr.CCViewportPointer = cc_vp_offset;
2314 }
2315 #elif GEN_GEN == 6
2316 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS), vp) {
2317 vp.CCViewportStateChange = 1;
2318 vp.PointertoCC_VIEWPORT = cc_vp_offset;
2319 }
2320 #else
2321 brw->cc.vp_offset = cc_vp_offset;
2322 ctx->NewDriverState |= BRW_NEW_CC_VP;
2323 #endif
2324 }
2325
2326 const struct brw_tracked_state genX(cc_vp) = {
2327 .dirty = {
2328 .mesa = _NEW_TRANSFORM |
2329 _NEW_VIEWPORT,
2330 .brw = BRW_NEW_BATCH |
2331 BRW_NEW_BLORP |
2332 BRW_NEW_VIEWPORT_COUNT,
2333 },
2334 .emit = genX(upload_cc_viewport)
2335 };
2336
2337 /* ---------------------------------------------------------------------- */
2338
2339 static void
2340 set_scissor_bits(const struct gl_context *ctx, int i,
2341 bool render_to_fbo, unsigned fb_width, unsigned fb_height,
2342 struct GENX(SCISSOR_RECT) *sc)
2343 {
2344 int bbox[4];
2345
2346 bbox[0] = MAX2(ctx->ViewportArray[i].X, 0);
2347 bbox[1] = MIN2(bbox[0] + ctx->ViewportArray[i].Width, fb_width);
2348 bbox[2] = MAX2(ctx->ViewportArray[i].Y, 0);
2349 bbox[3] = MIN2(bbox[2] + ctx->ViewportArray[i].Height, fb_height);
2350 _mesa_intersect_scissor_bounding_box(ctx, i, bbox);
2351
2352 if (bbox[0] == bbox[1] || bbox[2] == bbox[3]) {
2353 /* If the scissor was out of bounds and got clamped to 0 width/height
2354 * at the bounds, the subtraction of 1 from maximums could produce a
2355 * negative number and thus not clip anything. Instead, just provide
2356 * a min > max scissor inside the bounds, which produces the expected
2357 * no rendering.
2358 */
2359 sc->ScissorRectangleXMin = 1;
2360 sc->ScissorRectangleXMax = 0;
2361 sc->ScissorRectangleYMin = 1;
2362 sc->ScissorRectangleYMax = 0;
2363 } else if (render_to_fbo) {
2364 /* texmemory: Y=0=bottom */
2365 sc->ScissorRectangleXMin = bbox[0];
2366 sc->ScissorRectangleXMax = bbox[1] - 1;
2367 sc->ScissorRectangleYMin = bbox[2];
2368 sc->ScissorRectangleYMax = bbox[3] - 1;
2369 } else {
2370 /* memory: Y=0=top */
2371 sc->ScissorRectangleXMin = bbox[0];
2372 sc->ScissorRectangleXMax = bbox[1] - 1;
2373 sc->ScissorRectangleYMin = fb_height - bbox[3];
2374 sc->ScissorRectangleYMax = fb_height - bbox[2] - 1;
2375 }
2376 }
2377
2378 #if GEN_GEN >= 6
2379 static void
2380 genX(upload_scissor_state)(struct brw_context *brw)
2381 {
2382 struct gl_context *ctx = &brw->ctx;
2383 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
2384 struct GENX(SCISSOR_RECT) scissor;
2385 uint32_t scissor_state_offset;
2386 const unsigned int fb_width = _mesa_geometric_width(ctx->DrawBuffer);
2387 const unsigned int fb_height = _mesa_geometric_height(ctx->DrawBuffer);
2388 uint32_t *scissor_map;
2389
2390 /* BRW_NEW_VIEWPORT_COUNT */
2391 const unsigned viewport_count = brw->clip.viewport_count;
2392
2393 scissor_map = brw_state_batch(
2394 brw, GENX(SCISSOR_RECT_length) * sizeof(uint32_t) * viewport_count,
2395 32, &scissor_state_offset);
2396
2397 /* _NEW_SCISSOR | _NEW_BUFFERS | _NEW_VIEWPORT */
2398
2399 /* The scissor only needs to handle the intersection of drawable and
2400 * scissor rect. Clipping to the boundaries of static shared buffers
2401 * for front/back/depth is covered by looping over cliprects in brw_draw.c.
2402 *
2403 * Note that the hardware's coordinates are inclusive, while Mesa's min is
2404 * inclusive but max is exclusive.
2405 */
2406 for (unsigned i = 0; i < viewport_count; i++) {
2407 set_scissor_bits(ctx, i, render_to_fbo, fb_width, fb_height, &scissor);
2408 GENX(SCISSOR_RECT_pack)(
2409 NULL, scissor_map + i * GENX(SCISSOR_RECT_length), &scissor);
2410 }
2411
2412 brw_batch_emit(brw, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
2413 ptr.ScissorRectPointer = scissor_state_offset;
2414 }
2415 }
2416
2417 static const struct brw_tracked_state genX(scissor_state) = {
2418 .dirty = {
2419 .mesa = _NEW_BUFFERS |
2420 _NEW_SCISSOR |
2421 _NEW_VIEWPORT,
2422 .brw = BRW_NEW_BATCH |
2423 BRW_NEW_BLORP |
2424 BRW_NEW_VIEWPORT_COUNT,
2425 },
2426 .emit = genX(upload_scissor_state),
2427 };
2428 #endif
2429
2430 /* ---------------------------------------------------------------------- */
2431
2432 static void
2433 brw_calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
2434 float m00, float m11, float m30, float m31,
2435 float *xmin, float *xmax,
2436 float *ymin, float *ymax)
2437 {
2438 /* According to the "Vertex X,Y Clamping and Quantization" section of the
2439 * Strips and Fans documentation:
2440 *
2441 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
2442 * fixed-point "guardband" range supported by the rasterization hardware"
2443 *
2444 * and
2445 *
2446 * "In almost all circumstances, if an object’s vertices are actually
2447 * modified by this clamping (i.e., had X or Y coordinates outside of
2448 * the guardband extent the rendered object will not match the intended
2449 * result. Therefore software should take steps to ensure that this does
2450 * not happen - e.g., by clipping objects such that they do not exceed
2451 * these limits after the Drawing Rectangle is applied."
2452 *
2453 * I believe the fundamental restriction is that the rasterizer (in
2454 * the SF/WM stages) have a limit on the number of pixels that can be
2455 * rasterized. We need to ensure any coordinates beyond the rasterizer
2456 * limit are handled by the clipper. So effectively that limit becomes
2457 * the clipper's guardband size.
2458 *
2459 * It goes on to say:
2460 *
2461 * "In addition, in order to be correctly rendered, objects must have a
2462 * screenspace bounding box not exceeding 8K in the X or Y direction.
2463 * This additional restriction must also be comprehended by software,
2464 * i.e., enforced by use of clipping."
2465 *
2466 * This makes no sense. Gen7+ hardware supports 16K render targets,
2467 * and you definitely need to be able to draw polygons that fill the
2468 * surface. Our assumption is that the rasterizer was limited to 8K
2469 * on Sandybridge, which only supports 8K surfaces, and it was actually
2470 * increased to 16K on Ivybridge and later.
2471 *
2472 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
2473 */
2474 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
2475
2476 if (m00 != 0 && m11 != 0) {
2477 /* First, we compute the screen-space render area */
2478 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
2479 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
2480 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
2481 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
2482
2483 /* We want the guardband to be centered on that */
2484 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
2485 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
2486 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
2487 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
2488
2489 /* Now we need it in native device coordinates */
2490 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
2491 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
2492 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
2493 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
2494
2495 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
2496 * flipped upside-down. X should be fine though.
2497 */
2498 assert(ndc_gb_xmin <= ndc_gb_xmax);
2499 *xmin = ndc_gb_xmin;
2500 *xmax = ndc_gb_xmax;
2501 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
2502 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
2503 } else {
2504 /* The viewport scales to 0, so nothing will be rendered. */
2505 *xmin = 0.0f;
2506 *xmax = 0.0f;
2507 *ymin = 0.0f;
2508 *ymax = 0.0f;
2509 }
2510 }
2511
2512 static void
2513 genX(upload_sf_clip_viewport)(struct brw_context *brw)
2514 {
2515 struct gl_context *ctx = &brw->ctx;
2516 float y_scale, y_bias;
2517
2518 /* BRW_NEW_VIEWPORT_COUNT */
2519 const unsigned viewport_count = brw->clip.viewport_count;
2520
2521 /* _NEW_BUFFERS */
2522 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
2523 const uint32_t fb_width = (float)_mesa_geometric_width(ctx->DrawBuffer);
2524 const uint32_t fb_height = (float)_mesa_geometric_height(ctx->DrawBuffer);
2525
2526 #if GEN_GEN >= 7
2527 #define clv sfv
2528 struct GENX(SF_CLIP_VIEWPORT) sfv;
2529 uint32_t sf_clip_vp_offset;
2530 uint32_t *sf_clip_map =
2531 brw_state_batch(brw, GENX(SF_CLIP_VIEWPORT_length) * 4 * viewport_count,
2532 64, &sf_clip_vp_offset);
2533 #else
2534 struct GENX(SF_VIEWPORT) sfv;
2535 struct GENX(CLIP_VIEWPORT) clv;
2536 uint32_t sf_vp_offset, clip_vp_offset;
2537 uint32_t *sf_map =
2538 brw_state_batch(brw, GENX(SF_VIEWPORT_length) * 4 * viewport_count,
2539 32, &sf_vp_offset);
2540 uint32_t *clip_map =
2541 brw_state_batch(brw, GENX(CLIP_VIEWPORT_length) * 4 * viewport_count,
2542 32, &clip_vp_offset);
2543 #endif
2544
2545 /* _NEW_BUFFERS */
2546 if (render_to_fbo) {
2547 y_scale = 1.0;
2548 y_bias = 0;
2549 } else {
2550 y_scale = -1.0;
2551 y_bias = (float)fb_height;
2552 }
2553
2554 for (unsigned i = 0; i < brw->clip.viewport_count; i++) {
2555 /* _NEW_VIEWPORT: Guardband Clipping */
2556 float scale[3], translate[3], gb_xmin, gb_xmax, gb_ymin, gb_ymax;
2557 _mesa_get_viewport_xform(ctx, i, scale, translate);
2558
2559 sfv.ViewportMatrixElementm00 = scale[0];
2560 sfv.ViewportMatrixElementm11 = scale[1] * y_scale,
2561 sfv.ViewportMatrixElementm22 = scale[2],
2562 sfv.ViewportMatrixElementm30 = translate[0],
2563 sfv.ViewportMatrixElementm31 = translate[1] * y_scale + y_bias,
2564 sfv.ViewportMatrixElementm32 = translate[2],
2565 brw_calculate_guardband_size(fb_width, fb_height,
2566 sfv.ViewportMatrixElementm00,
2567 sfv.ViewportMatrixElementm11,
2568 sfv.ViewportMatrixElementm30,
2569 sfv.ViewportMatrixElementm31,
2570 &gb_xmin, &gb_xmax, &gb_ymin, &gb_ymax);
2571
2572
2573 clv.XMinClipGuardband = gb_xmin;
2574 clv.XMaxClipGuardband = gb_xmax;
2575 clv.YMinClipGuardband = gb_ymin;
2576 clv.YMaxClipGuardband = gb_ymax;
2577
2578 #if GEN_GEN < 6
2579 set_scissor_bits(ctx, i, render_to_fbo, fb_width, fb_height,
2580 &sfv.ScissorRectangle);
2581 #elif GEN_GEN >= 8
2582 /* _NEW_VIEWPORT | _NEW_BUFFERS: Screen Space Viewport
2583 * The hardware will take the intersection of the drawing rectangle,
2584 * scissor rectangle, and the viewport extents. However, emitting
2585 * 3DSTATE_DRAWING_RECTANGLE is expensive since it requires a full
2586 * pipeline stall so we're better off just being a little more clever
2587 * with our viewport so we can emit it once at context creation time.
2588 */
2589 const float viewport_Xmin = MAX2(ctx->ViewportArray[i].X, 0);
2590 const float viewport_Ymin = MAX2(ctx->ViewportArray[i].Y, 0);
2591 const float viewport_Xmax =
2592 MIN2(ctx->ViewportArray[i].X + ctx->ViewportArray[i].Width, fb_width);
2593 const float viewport_Ymax =
2594 MIN2(ctx->ViewportArray[i].Y + ctx->ViewportArray[i].Height, fb_height);
2595
2596 if (render_to_fbo) {
2597 sfv.XMinViewPort = viewport_Xmin;
2598 sfv.XMaxViewPort = viewport_Xmax - 1;
2599 sfv.YMinViewPort = viewport_Ymin;
2600 sfv.YMaxViewPort = viewport_Ymax - 1;
2601 } else {
2602 sfv.XMinViewPort = viewport_Xmin;
2603 sfv.XMaxViewPort = viewport_Xmax - 1;
2604 sfv.YMinViewPort = fb_height - viewport_Ymax;
2605 sfv.YMaxViewPort = fb_height - viewport_Ymin - 1;
2606 }
2607 #endif
2608
2609 #if GEN_GEN >= 7
2610 GENX(SF_CLIP_VIEWPORT_pack)(NULL, sf_clip_map, &sfv);
2611 sf_clip_map += GENX(SF_CLIP_VIEWPORT_length);
2612 #else
2613 GENX(SF_VIEWPORT_pack)(NULL, sf_map, &sfv);
2614 GENX(CLIP_VIEWPORT_pack)(NULL, clip_map, &clv);
2615 sf_map += GENX(SF_VIEWPORT_length);
2616 clip_map += GENX(CLIP_VIEWPORT_length);
2617 #endif
2618 }
2619
2620 #if GEN_GEN >= 7
2621 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
2622 ptr.SFClipViewportPointer = sf_clip_vp_offset;
2623 }
2624 #elif GEN_GEN == 6
2625 brw_batch_emit(brw, GENX(3DSTATE_VIEWPORT_STATE_POINTERS), vp) {
2626 vp.SFViewportStateChange = 1;
2627 vp.CLIPViewportStateChange = 1;
2628 vp.PointertoCLIP_VIEWPORT = clip_vp_offset;
2629 vp.PointertoSF_VIEWPORT = sf_vp_offset;
2630 }
2631 #else
2632 brw->sf.vp_offset = sf_vp_offset;
2633 brw->clip.vp_offset = clip_vp_offset;
2634 brw->ctx.NewDriverState |= BRW_NEW_SF_VP | BRW_NEW_CLIP_VP;
2635 #endif
2636 }
2637
2638 static const struct brw_tracked_state genX(sf_clip_viewport) = {
2639 .dirty = {
2640 .mesa = _NEW_BUFFERS |
2641 _NEW_VIEWPORT |
2642 (GEN_GEN <= 5 ? _NEW_SCISSOR : 0),
2643 .brw = BRW_NEW_BATCH |
2644 BRW_NEW_BLORP |
2645 BRW_NEW_VIEWPORT_COUNT,
2646 },
2647 .emit = genX(upload_sf_clip_viewport),
2648 };
2649
2650 /* ---------------------------------------------------------------------- */
2651
2652 static void
2653 genX(upload_gs_state)(struct brw_context *brw)
2654 {
2655 UNUSED struct gl_context *ctx = &brw->ctx;
2656 UNUSED const struct gen_device_info *devinfo = &brw->screen->devinfo;
2657 const struct brw_stage_state *stage_state = &brw->gs.base;
2658 const struct gl_program *gs_prog = brw->programs[MESA_SHADER_GEOMETRY];
2659 /* BRW_NEW_GEOMETRY_PROGRAM */
2660 bool active = GEN_GEN >= 6 && gs_prog;
2661
2662 /* BRW_NEW_GS_PROG_DATA */
2663 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
2664 UNUSED const struct brw_vue_prog_data *vue_prog_data =
2665 brw_vue_prog_data(stage_prog_data);
2666 #if GEN_GEN >= 7
2667 const struct brw_gs_prog_data *gs_prog_data =
2668 brw_gs_prog_data(stage_prog_data);
2669 #endif
2670
2671 #if GEN_GEN == 6
2672 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_GS), cgs) {
2673 if (active && stage_state->push_const_size != 0) {
2674 cgs.Buffer0Valid = true;
2675 cgs.PointertoGSConstantBuffer0 = stage_state->push_const_offset;
2676 cgs.GSConstantBuffer0ReadLength = stage_state->push_const_size - 1;
2677 }
2678 }
2679 #endif
2680
2681 #if GEN_GEN == 7 && !GEN_IS_HASWELL
2682 /**
2683 * From Graphics BSpec: 3D-Media-GPGPU Engine > 3D Pipeline Stages >
2684 * Geometry > Geometry Shader > State:
2685 *
2686 * "Note: Because of corruption in IVB:GT2, software needs to flush the
2687 * whole fixed function pipeline when the GS enable changes value in
2688 * the 3DSTATE_GS."
2689 *
2690 * The hardware architects have clarified that in this context "flush the
2691 * whole fixed function pipeline" means to emit a PIPE_CONTROL with the "CS
2692 * Stall" bit set.
2693 */
2694 if (devinfo->gt == 2 && brw->gs.enabled != active)
2695 gen7_emit_cs_stall_flush(brw);
2696 #endif
2697
2698 #if GEN_GEN >= 6
2699 brw_batch_emit(brw, GENX(3DSTATE_GS), gs) {
2700 #else
2701 ctx->NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
2702 brw_state_emit(brw, GENX(GS_STATE), 32, &brw->ff_gs.state_offset, gs) {
2703 #endif
2704
2705 #if GEN_GEN >= 6
2706 if (active) {
2707 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
2708
2709 #if GEN_GEN >= 7
2710 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
2711 gs.OutputTopology = gs_prog_data->output_topology;
2712 gs.ControlDataHeaderSize =
2713 gs_prog_data->control_data_header_size_hwords;
2714
2715 gs.InstanceControl = gs_prog_data->invocations - 1;
2716 gs.DispatchMode = vue_prog_data->dispatch_mode;
2717
2718 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
2719
2720 gs.ControlDataFormat = gs_prog_data->control_data_format;
2721 #endif
2722
2723 /* Note: the meaning of the GEN7_GS_REORDER_TRAILING bit changes between
2724 * Ivy Bridge and Haswell.
2725 *
2726 * On Ivy Bridge, setting this bit causes the vertices of a triangle
2727 * strip to be delivered to the geometry shader in an order that does
2728 * not strictly follow the OpenGL spec, but preserves triangle
2729 * orientation. For example, if the vertices are (1, 2, 3, 4, 5), then
2730 * the geometry shader sees triangles:
2731 *
2732 * (1, 2, 3), (2, 4, 3), (3, 4, 5)
2733 *
2734 * (Clearing the bit is even worse, because it fails to preserve
2735 * orientation).
2736 *
2737 * Triangle strips with adjacency always ordered in a way that preserves
2738 * triangle orientation but does not strictly follow the OpenGL spec,
2739 * regardless of the setting of this bit.
2740 *
2741 * On Haswell, both triangle strips and triangle strips with adjacency
2742 * are always ordered in a way that preserves triangle orientation.
2743 * Setting this bit causes the ordering to strictly follow the OpenGL
2744 * spec.
2745 *
2746 * So in either case we want to set the bit. Unfortunately on Ivy
2747 * Bridge this will get the order close to correct but not perfect.
2748 */
2749 gs.ReorderMode = TRAILING;
2750 gs.MaximumNumberofThreads =
2751 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
2752 : (devinfo->max_gs_threads - 1);
2753
2754 #if GEN_GEN < 7
2755 gs.SOStatisticsEnable = true;
2756 if (gs_prog->info.has_transform_feedback_varyings)
2757 gs.SVBIPayloadEnable = true;
2758
2759 /* GEN6_GS_SPF_MODE and GEN6_GS_VECTOR_MASK_ENABLE are enabled as it
2760 * was previously done for gen6.
2761 *
2762 * TODO: test with both disabled to see if the HW is behaving
2763 * as expected, like in gen7.
2764 */
2765 gs.SingleProgramFlow = true;
2766 gs.VectorMaskEnable = true;
2767 #endif
2768
2769 #if GEN_GEN >= 8
2770 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
2771
2772 if (gs_prog_data->static_vertex_count != -1) {
2773 gs.StaticOutput = true;
2774 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
2775 }
2776 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
2777
2778 gs.UserClipDistanceCullTestEnableBitmask =
2779 vue_prog_data->cull_distance_mask;
2780
2781 const int urb_entry_write_offset = 1;
2782 const uint32_t urb_entry_output_length =
2783 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
2784 urb_entry_write_offset;
2785
2786 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
2787 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
2788 #endif
2789 }
2790 #endif
2791
2792 #if GEN_GEN <= 6
2793 if (!active && brw->ff_gs.prog_active) {
2794 /* In gen6, transform feedback for the VS stage is done with an
2795 * ad-hoc GS program. This function provides the needed 3DSTATE_GS
2796 * for this.
2797 */
2798 gs.KernelStartPointer = KSP(brw, brw->ff_gs.prog_offset);
2799 gs.SingleProgramFlow = true;
2800 gs.DispatchGRFStartRegisterForURBData = GEN_GEN == 6 ? 2 : 1;
2801 gs.VertexURBEntryReadLength = brw->ff_gs.prog_data->urb_read_length;
2802
2803 #if GEN_GEN <= 5
2804 gs.GRFRegisterCount =
2805 DIV_ROUND_UP(brw->ff_gs.prog_data->total_grf, 16) - 1;
2806 /* BRW_NEW_URB_FENCE */
2807 gs.NumberofURBEntries = brw->urb.nr_gs_entries;
2808 gs.URBEntryAllocationSize = brw->urb.vsize - 1;
2809 gs.MaximumNumberofThreads = brw->urb.nr_gs_entries >= 8 ? 1 : 0;
2810 gs.FloatingPointMode = FLOATING_POINT_MODE_Alternate;
2811 #else
2812 gs.Enable = true;
2813 gs.VectorMaskEnable = true;
2814 gs.SVBIPayloadEnable = true;
2815 gs.SVBIPostIncrementEnable = true;
2816 gs.SVBIPostIncrementValue =
2817 brw->ff_gs.prog_data->svbi_postincrement_value;
2818 gs.SOStatisticsEnable = true;
2819 gs.MaximumNumberofThreads = devinfo->max_gs_threads - 1;
2820 #endif
2821 }
2822 #endif
2823 if (!active && !brw->ff_gs.prog_active) {
2824 #if GEN_GEN < 8
2825 gs.DispatchGRFStartRegisterForURBData = 1;
2826 #if GEN_GEN >= 7
2827 gs.IncludeVertexHandles = true;
2828 #endif
2829 #endif
2830 }
2831
2832 #if GEN_GEN >= 6
2833 gs.StatisticsEnable = true;
2834 #endif
2835 #if GEN_GEN == 5 || GEN_GEN == 6
2836 gs.RenderingEnabled = true;
2837 #endif
2838 #if GEN_GEN <= 5
2839 gs.MaximumVPIndex = brw->clip.viewport_count - 1;
2840 #endif
2841 }
2842
2843 #if GEN_GEN == 6
2844 brw->gs.enabled = active;
2845 #endif
2846 }
2847
2848 static const struct brw_tracked_state genX(gs_state) = {
2849 .dirty = {
2850 .mesa = (GEN_GEN == 6 ? _NEW_PROGRAM_CONSTANTS : 0),
2851 .brw = BRW_NEW_BATCH |
2852 BRW_NEW_BLORP |
2853 (GEN_GEN <= 5 ? BRW_NEW_PUSH_CONSTANT_ALLOCATION |
2854 BRW_NEW_PROGRAM_CACHE |
2855 BRW_NEW_URB_FENCE |
2856 BRW_NEW_VIEWPORT_COUNT
2857 : 0) |
2858 (GEN_GEN >= 6 ? BRW_NEW_CONTEXT |
2859 BRW_NEW_GEOMETRY_PROGRAM |
2860 BRW_NEW_GS_PROG_DATA
2861 : 0) |
2862 (GEN_GEN < 7 ? BRW_NEW_FF_GS_PROG_DATA : 0),
2863 },
2864 .emit = genX(upload_gs_state),
2865 };
2866
2867 /* ---------------------------------------------------------------------- */
2868
2869 UNUSED static GLenum
2870 fix_dual_blend_alpha_to_one(GLenum function)
2871 {
2872 switch (function) {
2873 case GL_SRC1_ALPHA:
2874 return GL_ONE;
2875
2876 case GL_ONE_MINUS_SRC1_ALPHA:
2877 return GL_ZERO;
2878 }
2879
2880 return function;
2881 }
2882
2883 #define blend_factor(x) brw_translate_blend_factor(x)
2884 #define blend_eqn(x) brw_translate_blend_equation(x)
2885
2886 /**
2887 * Modify blend function to force destination alpha to 1.0
2888 *
2889 * If \c function specifies a blend function that uses destination alpha,
2890 * replace it with a function that hard-wires destination alpha to 1.0. This
2891 * is used when rendering to xRGB targets.
2892 */
2893 static GLenum
2894 brw_fix_xRGB_alpha(GLenum function)
2895 {
2896 switch (function) {
2897 case GL_DST_ALPHA:
2898 return GL_ONE;
2899
2900 case GL_ONE_MINUS_DST_ALPHA:
2901 case GL_SRC_ALPHA_SATURATE:
2902 return GL_ZERO;
2903 }
2904
2905 return function;
2906 }
2907
2908 #if GEN_GEN >= 6
2909 typedef struct GENX(BLEND_STATE_ENTRY) BLEND_ENTRY_GENXML;
2910 #else
2911 typedef struct GENX(COLOR_CALC_STATE) BLEND_ENTRY_GENXML;
2912 #endif
2913
2914 UNUSED static bool
2915 set_blend_entry_bits(struct brw_context *brw, BLEND_ENTRY_GENXML *entry, int i,
2916 bool alpha_to_one)
2917 {
2918 struct gl_context *ctx = &brw->ctx;
2919
2920 /* _NEW_BUFFERS */
2921 const struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[i];
2922
2923 bool independent_alpha_blend = false;
2924
2925 /* Used for implementing the following bit of GL_EXT_texture_integer:
2926 * "Per-fragment operations that require floating-point color
2927 * components, including multisample alpha operations, alpha test,
2928 * blending, and dithering, have no effect when the corresponding
2929 * colors are written to an integer color buffer."
2930 */
2931 const bool integer = ctx->DrawBuffer->_IntegerBuffers & (0x1 << i);
2932
2933 const unsigned blend_enabled = GEN_GEN >= 6 ?
2934 ctx->Color.BlendEnabled & (1 << i) : ctx->Color.BlendEnabled;
2935
2936 /* _NEW_COLOR */
2937 if (ctx->Color.ColorLogicOpEnabled) {
2938 GLenum rb_type = rb ? _mesa_get_format_datatype(rb->Format)
2939 : GL_UNSIGNED_NORMALIZED;
2940 WARN_ONCE(ctx->Color.LogicOp != GL_COPY &&
2941 rb_type != GL_UNSIGNED_NORMALIZED &&
2942 rb_type != GL_FLOAT, "Ignoring %s logic op on %s "
2943 "renderbuffer\n",
2944 _mesa_enum_to_string(ctx->Color.LogicOp),
2945 _mesa_enum_to_string(rb_type));
2946 if (GEN_GEN >= 8 || rb_type == GL_UNSIGNED_NORMALIZED) {
2947 entry->LogicOpEnable = true;
2948 entry->LogicOpFunction = ctx->Color._LogicOp;
2949 }
2950 } else if (blend_enabled && !ctx->Color._AdvancedBlendMode
2951 && (GEN_GEN <= 5 || !integer)) {
2952 GLenum eqRGB = ctx->Color.Blend[i].EquationRGB;
2953 GLenum eqA = ctx->Color.Blend[i].EquationA;
2954 GLenum srcRGB = ctx->Color.Blend[i].SrcRGB;
2955 GLenum dstRGB = ctx->Color.Blend[i].DstRGB;
2956 GLenum srcA = ctx->Color.Blend[i].SrcA;
2957 GLenum dstA = ctx->Color.Blend[i].DstA;
2958
2959 if (eqRGB == GL_MIN || eqRGB == GL_MAX)
2960 srcRGB = dstRGB = GL_ONE;
2961
2962 if (eqA == GL_MIN || eqA == GL_MAX)
2963 srcA = dstA = GL_ONE;
2964
2965 /* Due to hardware limitations, the destination may have information
2966 * in an alpha channel even when the format specifies no alpha
2967 * channel. In order to avoid getting any incorrect blending due to
2968 * that alpha channel, coerce the blend factors to values that will
2969 * not read the alpha channel, but will instead use the correct
2970 * implicit value for alpha.
2971 */
2972 if (rb && !_mesa_base_format_has_channel(rb->_BaseFormat,
2973 GL_TEXTURE_ALPHA_TYPE)) {
2974 srcRGB = brw_fix_xRGB_alpha(srcRGB);
2975 srcA = brw_fix_xRGB_alpha(srcA);
2976 dstRGB = brw_fix_xRGB_alpha(dstRGB);
2977 dstA = brw_fix_xRGB_alpha(dstA);
2978 }
2979
2980 /* From the BLEND_STATE docs, DWord 0, Bit 29 (AlphaToOne Enable):
2981 * "If Dual Source Blending is enabled, this bit must be disabled."
2982 *
2983 * We override SRC1_ALPHA to ONE and ONE_MINUS_SRC1_ALPHA to ZERO,
2984 * and leave it enabled anyway.
2985 */
2986 if (GEN_GEN >= 6 && ctx->Color.Blend[i]._UsesDualSrc && alpha_to_one) {
2987 srcRGB = fix_dual_blend_alpha_to_one(srcRGB);
2988 srcA = fix_dual_blend_alpha_to_one(srcA);
2989 dstRGB = fix_dual_blend_alpha_to_one(dstRGB);
2990 dstA = fix_dual_blend_alpha_to_one(dstA);
2991 }
2992
2993 entry->ColorBufferBlendEnable = true;
2994 entry->DestinationBlendFactor = blend_factor(dstRGB);
2995 entry->SourceBlendFactor = blend_factor(srcRGB);
2996 entry->DestinationAlphaBlendFactor = blend_factor(dstA);
2997 entry->SourceAlphaBlendFactor = blend_factor(srcA);
2998 entry->ColorBlendFunction = blend_eqn(eqRGB);
2999 entry->AlphaBlendFunction = blend_eqn(eqA);
3000
3001 if (srcA != srcRGB || dstA != dstRGB || eqA != eqRGB)
3002 independent_alpha_blend = true;
3003 }
3004
3005 return independent_alpha_blend;
3006 }
3007
3008 #if GEN_GEN >= 6
3009 static void
3010 genX(upload_blend_state)(struct brw_context *brw)
3011 {
3012 struct gl_context *ctx = &brw->ctx;
3013 int size;
3014
3015 /* We need at least one BLEND_STATE written, because we might do
3016 * thread dispatch even if _NumColorDrawBuffers is 0 (for example
3017 * for computed depth or alpha test), which will do an FB write
3018 * with render target 0, which will reference BLEND_STATE[0] for
3019 * alpha test enable.
3020 */
3021 int nr_draw_buffers = ctx->DrawBuffer->_NumColorDrawBuffers;
3022 if (nr_draw_buffers == 0 && ctx->Color.AlphaEnabled)
3023 nr_draw_buffers = 1;
3024
3025 size = GENX(BLEND_STATE_ENTRY_length) * 4 * nr_draw_buffers;
3026 #if GEN_GEN >= 8
3027 size += GENX(BLEND_STATE_length) * 4;
3028 #endif
3029
3030 uint32_t *blend_map;
3031 blend_map = brw_state_batch(brw, size, 64, &brw->cc.blend_state_offset);
3032
3033 #if GEN_GEN >= 8
3034 struct GENX(BLEND_STATE) blend = { 0 };
3035 {
3036 #else
3037 for (int i = 0; i < nr_draw_buffers; i++) {
3038 struct GENX(BLEND_STATE_ENTRY) entry = { 0 };
3039 #define blend entry
3040 #endif
3041 /* OpenGL specification 3.3 (page 196), section 4.1.3 says:
3042 * "If drawbuffer zero is not NONE and the buffer it references has an
3043 * integer format, the SAMPLE_ALPHA_TO_COVERAGE and SAMPLE_ALPHA_TO_ONE
3044 * operations are skipped."
3045 */
3046 if (!(ctx->DrawBuffer->_IntegerBuffers & 0x1)) {
3047 /* _NEW_MULTISAMPLE */
3048 if (_mesa_is_multisample_enabled(ctx)) {
3049 if (ctx->Multisample.SampleAlphaToCoverage) {
3050 blend.AlphaToCoverageEnable = true;
3051 blend.AlphaToCoverageDitherEnable = GEN_GEN >= 7;
3052 }
3053 if (ctx->Multisample.SampleAlphaToOne)
3054 blend.AlphaToOneEnable = true;
3055 }
3056
3057 /* _NEW_COLOR */
3058 if (ctx->Color.AlphaEnabled) {
3059 blend.AlphaTestEnable = true;
3060 blend.AlphaTestFunction =
3061 intel_translate_compare_func(ctx->Color.AlphaFunc);
3062 }
3063
3064 if (ctx->Color.DitherFlag) {
3065 blend.ColorDitherEnable = true;
3066 }
3067 }
3068
3069 #if GEN_GEN >= 8
3070 for (int i = 0; i < nr_draw_buffers; i++) {
3071 struct GENX(BLEND_STATE_ENTRY) entry = { 0 };
3072 #else
3073 {
3074 #endif
3075 blend.IndependentAlphaBlendEnable =
3076 set_blend_entry_bits(brw, &entry, i, blend.AlphaToOneEnable) ||
3077 blend.IndependentAlphaBlendEnable;
3078
3079 /* See section 8.1.6 "Pre-Blend Color Clamping" of the
3080 * SandyBridge PRM Volume 2 Part 1 for HW requirements.
3081 *
3082 * We do our ARB_color_buffer_float CLAMP_FRAGMENT_COLOR
3083 * clamping in the fragment shader. For its clamping of
3084 * blending, the spec says:
3085 *
3086 * "RESOLVED: For fixed-point color buffers, the inputs and
3087 * the result of the blending equation are clamped. For
3088 * floating-point color buffers, no clamping occurs."
3089 *
3090 * So, generally, we want clamping to the render target's range.
3091 * And, good news, the hardware tables for both pre- and
3092 * post-blend color clamping are either ignored, or any are
3093 * allowed, or clamping is required but RT range clamping is a
3094 * valid option.
3095 */
3096 entry.PreBlendColorClampEnable = true;
3097 entry.PostBlendColorClampEnable = true;
3098 entry.ColorClampRange = COLORCLAMP_RTFORMAT;
3099
3100 entry.WriteDisableRed = !GET_COLORMASK_BIT(ctx->Color.ColorMask, i, 0);
3101 entry.WriteDisableGreen = !GET_COLORMASK_BIT(ctx->Color.ColorMask, i, 1);
3102 entry.WriteDisableBlue = !GET_COLORMASK_BIT(ctx->Color.ColorMask, i, 2);
3103 entry.WriteDisableAlpha = !GET_COLORMASK_BIT(ctx->Color.ColorMask, i, 3);
3104
3105 #if GEN_GEN >= 8
3106 GENX(BLEND_STATE_ENTRY_pack)(NULL, &blend_map[1 + i * 2], &entry);
3107 #else
3108 GENX(BLEND_STATE_ENTRY_pack)(NULL, &blend_map[i * 2], &entry);
3109 #endif
3110 }
3111 }
3112
3113 #if GEN_GEN >= 8
3114 GENX(BLEND_STATE_pack)(NULL, blend_map, &blend);
3115 #endif
3116
3117 #if GEN_GEN < 7
3118 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
3119 ptr.PointertoBLEND_STATE = brw->cc.blend_state_offset;
3120 ptr.BLEND_STATEChange = true;
3121 }
3122 #else
3123 brw_batch_emit(brw, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
3124 ptr.BlendStatePointer = brw->cc.blend_state_offset;
3125 #if GEN_GEN >= 8
3126 ptr.BlendStatePointerValid = true;
3127 #endif
3128 }
3129 #endif
3130 }
3131
3132 static const struct brw_tracked_state genX(blend_state) = {
3133 .dirty = {
3134 .mesa = _NEW_BUFFERS |
3135 _NEW_COLOR |
3136 _NEW_MULTISAMPLE,
3137 .brw = BRW_NEW_BATCH |
3138 BRW_NEW_BLORP |
3139 BRW_NEW_STATE_BASE_ADDRESS,
3140 },
3141 .emit = genX(upload_blend_state),
3142 };
3143 #endif
3144
3145 /* ---------------------------------------------------------------------- */
3146
3147 #if GEN_GEN >= 7
3148 UNUSED static const uint32_t push_constant_opcodes[] = {
3149 [MESA_SHADER_VERTEX] = 21,
3150 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
3151 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
3152 [MESA_SHADER_GEOMETRY] = 22,
3153 [MESA_SHADER_FRAGMENT] = 23,
3154 [MESA_SHADER_COMPUTE] = 0,
3155 };
3156
3157 static void
3158 genX(upload_push_constant_packets)(struct brw_context *brw)
3159 {
3160 const struct gen_device_info *devinfo = &brw->screen->devinfo;
3161 struct gl_context *ctx = &brw->ctx;
3162
3163 UNUSED uint32_t mocs = GEN_GEN < 8 ? GEN7_MOCS_L3 : 0;
3164
3165 struct brw_stage_state *stage_states[] = {
3166 &brw->vs.base,
3167 &brw->tcs.base,
3168 &brw->tes.base,
3169 &brw->gs.base,
3170 &brw->wm.base,
3171 };
3172
3173 if (GEN_GEN == 7 && !GEN_IS_HASWELL && !devinfo->is_baytrail &&
3174 stage_states[MESA_SHADER_VERTEX]->push_constants_dirty)
3175 gen7_emit_vs_workaround_flush(brw);
3176
3177 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
3178 struct brw_stage_state *stage_state = stage_states[stage];
3179 UNUSED struct gl_program *prog = ctx->_Shader->CurrentProgram[stage];
3180
3181 if (!stage_state->push_constants_dirty)
3182 continue;
3183
3184 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_VS), pkt) {
3185 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
3186 if (stage_state->prog_data) {
3187 #if GEN_GEN >= 8 || GEN_IS_HASWELL
3188 /* The Skylake PRM contains the following restriction:
3189 *
3190 * "The driver must ensure The following case does not occur
3191 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
3192 * buffer 3 read length equal to zero committed followed by a
3193 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
3194 * zero committed."
3195 *
3196 * To avoid this, we program the buffers in the highest slots.
3197 * This way, slot 0 is only used if slot 3 is also used.
3198 */
3199 int n = 3;
3200
3201 for (int i = 3; i >= 0; i--) {
3202 const struct brw_ubo_range *range =
3203 &stage_state->prog_data->ubo_ranges[i];
3204
3205 if (range->length == 0)
3206 continue;
3207
3208 const struct gl_uniform_block *block =
3209 prog->sh.UniformBlocks[range->block];
3210 const struct gl_buffer_binding *binding =
3211 &ctx->UniformBufferBindings[block->Binding];
3212
3213 if (binding->BufferObject == ctx->Shared->NullBufferObj) {
3214 static unsigned msg_id = 0;
3215 _mesa_gl_debug(ctx, &msg_id, MESA_DEBUG_SOURCE_API,
3216 MESA_DEBUG_TYPE_UNDEFINED,
3217 MESA_DEBUG_SEVERITY_HIGH,
3218 "UBO %d unbound, %s shader uniform data "
3219 "will be undefined.",
3220 range->block,
3221 _mesa_shader_stage_to_string(stage));
3222 continue;
3223 }
3224
3225 assert(binding->Offset % 32 == 0);
3226
3227 struct brw_bo *bo = intel_bufferobj_buffer(brw,
3228 intel_buffer_object(binding->BufferObject),
3229 binding->Offset, range->length * 32, false);
3230
3231 pkt.ConstantBody.ReadLength[n] = range->length;
3232 pkt.ConstantBody.Buffer[n] =
3233 ro_bo(bo, range->start * 32 + binding->Offset);
3234 n--;
3235 }
3236
3237 if (stage_state->push_const_size > 0) {
3238 assert(n >= 0);
3239 pkt.ConstantBody.ReadLength[n] = stage_state->push_const_size;
3240 pkt.ConstantBody.Buffer[n] =
3241 ro_bo(stage_state->push_const_bo,
3242 stage_state->push_const_offset);
3243 }
3244 #else
3245 pkt.ConstantBody.ReadLength[0] = stage_state->push_const_size;
3246 pkt.ConstantBody.Buffer[0].offset =
3247 stage_state->push_const_offset | mocs;
3248 #endif
3249 }
3250 }
3251
3252 stage_state->push_constants_dirty = false;
3253 brw->ctx.NewDriverState |= GEN_GEN >= 9 ? BRW_NEW_SURFACES : 0;
3254 }
3255 }
3256
3257 const struct brw_tracked_state genX(push_constant_packets) = {
3258 .dirty = {
3259 .mesa = 0,
3260 .brw = BRW_NEW_DRAW_CALL,
3261 },
3262 .emit = genX(upload_push_constant_packets),
3263 };
3264 #endif
3265
3266 #if GEN_GEN >= 6
3267 static void
3268 genX(upload_vs_push_constants)(struct brw_context *brw)
3269 {
3270 struct brw_stage_state *stage_state = &brw->vs.base;
3271
3272 /* BRW_NEW_VERTEX_PROGRAM */
3273 const struct gl_program *vp = brw->programs[MESA_SHADER_VERTEX];
3274 /* BRW_NEW_VS_PROG_DATA */
3275 const struct brw_stage_prog_data *prog_data = brw->vs.base.prog_data;
3276
3277 gen6_upload_push_constants(brw, vp, prog_data, stage_state);
3278 }
3279
3280 static const struct brw_tracked_state genX(vs_push_constants) = {
3281 .dirty = {
3282 .mesa = _NEW_PROGRAM_CONSTANTS |
3283 _NEW_TRANSFORM,
3284 .brw = BRW_NEW_BATCH |
3285 BRW_NEW_BLORP |
3286 BRW_NEW_VERTEX_PROGRAM |
3287 BRW_NEW_VS_PROG_DATA,
3288 },
3289 .emit = genX(upload_vs_push_constants),
3290 };
3291
3292 static void
3293 genX(upload_gs_push_constants)(struct brw_context *brw)
3294 {
3295 struct brw_stage_state *stage_state = &brw->gs.base;
3296
3297 /* BRW_NEW_GEOMETRY_PROGRAM */
3298 const struct gl_program *gp = brw->programs[MESA_SHADER_GEOMETRY];
3299
3300 /* BRW_NEW_GS_PROG_DATA */
3301 struct brw_stage_prog_data *prog_data = brw->gs.base.prog_data;
3302
3303 gen6_upload_push_constants(brw, gp, prog_data, stage_state);
3304 }
3305
3306 static const struct brw_tracked_state genX(gs_push_constants) = {
3307 .dirty = {
3308 .mesa = _NEW_PROGRAM_CONSTANTS |
3309 _NEW_TRANSFORM,
3310 .brw = BRW_NEW_BATCH |
3311 BRW_NEW_BLORP |
3312 BRW_NEW_GEOMETRY_PROGRAM |
3313 BRW_NEW_GS_PROG_DATA,
3314 },
3315 .emit = genX(upload_gs_push_constants),
3316 };
3317
3318 static void
3319 genX(upload_wm_push_constants)(struct brw_context *brw)
3320 {
3321 struct brw_stage_state *stage_state = &brw->wm.base;
3322 /* BRW_NEW_FRAGMENT_PROGRAM */
3323 const struct gl_program *fp = brw->programs[MESA_SHADER_FRAGMENT];
3324 /* BRW_NEW_FS_PROG_DATA */
3325 const struct brw_stage_prog_data *prog_data = brw->wm.base.prog_data;
3326
3327 gen6_upload_push_constants(brw, fp, prog_data, stage_state);
3328 }
3329
3330 static const struct brw_tracked_state genX(wm_push_constants) = {
3331 .dirty = {
3332 .mesa = _NEW_PROGRAM_CONSTANTS,
3333 .brw = BRW_NEW_BATCH |
3334 BRW_NEW_BLORP |
3335 BRW_NEW_FRAGMENT_PROGRAM |
3336 BRW_NEW_FS_PROG_DATA,
3337 },
3338 .emit = genX(upload_wm_push_constants),
3339 };
3340 #endif
3341
3342 /* ---------------------------------------------------------------------- */
3343
3344 #if GEN_GEN >= 6
3345 static unsigned
3346 genX(determine_sample_mask)(struct brw_context *brw)
3347 {
3348 struct gl_context *ctx = &brw->ctx;
3349 float coverage = 1.0f;
3350 float coverage_invert = false;
3351 unsigned sample_mask = ~0u;
3352
3353 /* BRW_NEW_NUM_SAMPLES */
3354 unsigned num_samples = brw->num_samples;
3355
3356 if (_mesa_is_multisample_enabled(ctx)) {
3357 if (ctx->Multisample.SampleCoverage) {
3358 coverage = ctx->Multisample.SampleCoverageValue;
3359 coverage_invert = ctx->Multisample.SampleCoverageInvert;
3360 }
3361 if (ctx->Multisample.SampleMask) {
3362 sample_mask = ctx->Multisample.SampleMaskValue;
3363 }
3364 }
3365
3366 if (num_samples > 1) {
3367 int coverage_int = (int) (num_samples * coverage + 0.5f);
3368 uint32_t coverage_bits = (1 << coverage_int) - 1;
3369 if (coverage_invert)
3370 coverage_bits ^= (1 << num_samples) - 1;
3371 return coverage_bits & sample_mask;
3372 } else {
3373 return 1;
3374 }
3375 }
3376
3377 static void
3378 genX(emit_3dstate_multisample2)(struct brw_context *brw,
3379 unsigned num_samples)
3380 {
3381 unsigned log2_samples = ffs(num_samples) - 1;
3382
3383 brw_batch_emit(brw, GENX(3DSTATE_MULTISAMPLE), multi) {
3384 multi.PixelLocation = CENTER;
3385 multi.NumberofMultisamples = log2_samples;
3386 #if GEN_GEN == 6
3387 GEN_SAMPLE_POS_4X(multi.Sample);
3388 #elif GEN_GEN == 7
3389 switch (num_samples) {
3390 case 1:
3391 GEN_SAMPLE_POS_1X(multi.Sample);
3392 break;
3393 case 2:
3394 GEN_SAMPLE_POS_2X(multi.Sample);
3395 break;
3396 case 4:
3397 GEN_SAMPLE_POS_4X(multi.Sample);
3398 break;
3399 case 8:
3400 GEN_SAMPLE_POS_8X(multi.Sample);
3401 break;
3402 default:
3403 break;
3404 }
3405 #endif
3406 }
3407 }
3408
3409 static void
3410 genX(upload_multisample_state)(struct brw_context *brw)
3411 {
3412 assert(brw->num_samples > 0 && brw->num_samples <= 16);
3413
3414 genX(emit_3dstate_multisample2)(brw, brw->num_samples);
3415
3416 brw_batch_emit(brw, GENX(3DSTATE_SAMPLE_MASK), sm) {
3417 sm.SampleMask = genX(determine_sample_mask)(brw);
3418 }
3419 }
3420
3421 static const struct brw_tracked_state genX(multisample_state) = {
3422 .dirty = {
3423 .mesa = _NEW_MULTISAMPLE |
3424 (GEN_GEN == 10 ? _NEW_BUFFERS : 0),
3425 .brw = BRW_NEW_BLORP |
3426 BRW_NEW_CONTEXT |
3427 BRW_NEW_NUM_SAMPLES,
3428 },
3429 .emit = genX(upload_multisample_state)
3430 };
3431 #endif
3432
3433 /* ---------------------------------------------------------------------- */
3434
3435 static void
3436 genX(upload_color_calc_state)(struct brw_context *brw)
3437 {
3438 struct gl_context *ctx = &brw->ctx;
3439
3440 brw_state_emit(brw, GENX(COLOR_CALC_STATE), 64, &brw->cc.state_offset, cc) {
3441 #if GEN_GEN <= 5
3442 cc.IndependentAlphaBlendEnable =
3443 set_blend_entry_bits(brw, &cc, 0, false);
3444 set_depth_stencil_bits(brw, &cc);
3445
3446 if (ctx->Color.AlphaEnabled &&
3447 ctx->DrawBuffer->_NumColorDrawBuffers <= 1) {
3448 cc.AlphaTestEnable = true;
3449 cc.AlphaTestFunction =
3450 intel_translate_compare_func(ctx->Color.AlphaFunc);
3451 }
3452
3453 cc.ColorDitherEnable = ctx->Color.DitherFlag;
3454
3455 cc.StatisticsEnable = brw->stats_wm;
3456
3457 cc.CCViewportStatePointer =
3458 ro_bo(brw->batch.state.bo, brw->cc.vp_offset);
3459 #else
3460 /* _NEW_COLOR */
3461 cc.BlendConstantColorRed = ctx->Color.BlendColorUnclamped[0];
3462 cc.BlendConstantColorGreen = ctx->Color.BlendColorUnclamped[1];
3463 cc.BlendConstantColorBlue = ctx->Color.BlendColorUnclamped[2];
3464 cc.BlendConstantColorAlpha = ctx->Color.BlendColorUnclamped[3];
3465
3466 #if GEN_GEN < 9
3467 /* _NEW_STENCIL */
3468 cc.StencilReferenceValue = _mesa_get_stencil_ref(ctx, 0);
3469 cc.BackfaceStencilReferenceValue =
3470 _mesa_get_stencil_ref(ctx, ctx->Stencil._BackFace);
3471 #endif
3472
3473 #endif
3474
3475 /* _NEW_COLOR */
3476 UNCLAMPED_FLOAT_TO_UBYTE(cc.AlphaReferenceValueAsUNORM8,
3477 ctx->Color.AlphaRef);
3478 }
3479
3480 #if GEN_GEN >= 6
3481 brw_batch_emit(brw, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
3482 ptr.ColorCalcStatePointer = brw->cc.state_offset;
3483 #if GEN_GEN != 7
3484 ptr.ColorCalcStatePointerValid = true;
3485 #endif
3486 }
3487 #else
3488 brw->ctx.NewDriverState |= BRW_NEW_GEN4_UNIT_STATE;
3489 #endif
3490 }
3491
3492 static const struct brw_tracked_state genX(color_calc_state) = {
3493 .dirty = {
3494 .mesa = _NEW_COLOR |
3495 _NEW_STENCIL |
3496 (GEN_GEN <= 5 ? _NEW_BUFFERS |
3497 _NEW_DEPTH
3498 : 0),
3499 .brw = BRW_NEW_BATCH |
3500 BRW_NEW_BLORP |
3501 (GEN_GEN <= 5 ? BRW_NEW_CC_VP |
3502 BRW_NEW_STATS_WM
3503 : BRW_NEW_CC_STATE |
3504 BRW_NEW_STATE_BASE_ADDRESS),
3505 },
3506 .emit = genX(upload_color_calc_state),
3507 };
3508
3509
3510 /* ---------------------------------------------------------------------- */
3511
3512 #if GEN_GEN >= 7
3513 static void
3514 genX(upload_sbe)(struct brw_context *brw)
3515 {
3516 struct gl_context *ctx = &brw->ctx;
3517 /* BRW_NEW_FRAGMENT_PROGRAM */
3518 UNUSED const struct gl_program *fp = brw->programs[MESA_SHADER_FRAGMENT];
3519 /* BRW_NEW_FS_PROG_DATA */
3520 const struct brw_wm_prog_data *wm_prog_data =
3521 brw_wm_prog_data(brw->wm.base.prog_data);
3522 #if GEN_GEN >= 8
3523 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = { { 0 } };
3524 #else
3525 #define attr_overrides sbe.Attribute
3526 #endif
3527 uint32_t urb_entry_read_length;
3528 uint32_t urb_entry_read_offset;
3529 uint32_t point_sprite_enables;
3530
3531 brw_batch_emit(brw, GENX(3DSTATE_SBE), sbe) {
3532 sbe.AttributeSwizzleEnable = true;
3533 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
3534
3535 /* _NEW_BUFFERS */
3536 bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
3537
3538 /* _NEW_POINT
3539 *
3540 * Window coordinates in an FBO are inverted, which means point
3541 * sprite origin must be inverted.
3542 */
3543 if ((ctx->Point.SpriteOrigin == GL_LOWER_LEFT) != render_to_fbo)
3544 sbe.PointSpriteTextureCoordinateOrigin = LOWERLEFT;
3545 else
3546 sbe.PointSpriteTextureCoordinateOrigin = UPPERLEFT;
3547
3548 /* _NEW_POINT | _NEW_LIGHT | _NEW_PROGRAM,
3549 * BRW_NEW_FS_PROG_DATA | BRW_NEW_FRAGMENT_PROGRAM |
3550 * BRW_NEW_GS_PROG_DATA | BRW_NEW_PRIMITIVE | BRW_NEW_TES_PROG_DATA |
3551 * BRW_NEW_VUE_MAP_GEOM_OUT
3552 */
3553 genX(calculate_attr_overrides)(brw,
3554 attr_overrides,
3555 &point_sprite_enables,
3556 &urb_entry_read_length,
3557 &urb_entry_read_offset);
3558
3559 /* Typically, the URB entry read length and offset should be programmed
3560 * in 3DSTATE_VS and 3DSTATE_GS; SBE inherits it from the last active
3561 * stage which produces geometry. However, we don't know the proper
3562 * value until we call calculate_attr_overrides().
3563 *
3564 * To fit with our existing code, we override the inherited values and
3565 * specify it here directly, as we did on previous generations.
3566 */
3567 sbe.VertexURBEntryReadLength = urb_entry_read_length;
3568 sbe.VertexURBEntryReadOffset = urb_entry_read_offset;
3569 sbe.PointSpriteTextureCoordinateEnable = point_sprite_enables;
3570 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
3571
3572 #if GEN_GEN >= 8
3573 sbe.ForceVertexURBEntryReadLength = true;
3574 sbe.ForceVertexURBEntryReadOffset = true;
3575 #endif
3576
3577 #if GEN_GEN >= 9
3578 /* prepare the active component dwords */
3579 for (int i = 0; i < 32; i++)
3580 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
3581 #endif
3582 }
3583
3584 #if GEN_GEN >= 8
3585 brw_batch_emit(brw, GENX(3DSTATE_SBE_SWIZ), sbes) {
3586 for (int i = 0; i < 16; i++)
3587 sbes.Attribute[i] = attr_overrides[i];
3588 }
3589 #endif
3590
3591 #undef attr_overrides
3592 }
3593
3594 static const struct brw_tracked_state genX(sbe_state) = {
3595 .dirty = {
3596 .mesa = _NEW_BUFFERS |
3597 _NEW_LIGHT |
3598 _NEW_POINT |
3599 _NEW_POLYGON |
3600 _NEW_PROGRAM,
3601 .brw = BRW_NEW_BLORP |
3602 BRW_NEW_CONTEXT |
3603 BRW_NEW_FRAGMENT_PROGRAM |
3604 BRW_NEW_FS_PROG_DATA |
3605 BRW_NEW_GS_PROG_DATA |
3606 BRW_NEW_TES_PROG_DATA |
3607 BRW_NEW_VUE_MAP_GEOM_OUT |
3608 (GEN_GEN == 7 ? BRW_NEW_PRIMITIVE
3609 : 0),
3610 },
3611 .emit = genX(upload_sbe),
3612 };
3613 #endif
3614
3615 /* ---------------------------------------------------------------------- */
3616
3617 #if GEN_GEN >= 7
3618 /**
3619 * Outputs the 3DSTATE_SO_DECL_LIST command.
3620 *
3621 * The data output is a series of 64-bit entries containing a SO_DECL per
3622 * stream. We only have one stream of rendering coming out of the GS unit, so
3623 * we only emit stream 0 (low 16 bits) SO_DECLs.
3624 */
3625 static void
3626 genX(upload_3dstate_so_decl_list)(struct brw_context *brw,
3627 const struct brw_vue_map *vue_map)
3628 {
3629 struct gl_context *ctx = &brw->ctx;
3630 /* BRW_NEW_TRANSFORM_FEEDBACK */
3631 struct gl_transform_feedback_object *xfb_obj =
3632 ctx->TransformFeedback.CurrentObject;
3633 const struct gl_transform_feedback_info *linked_xfb_info =
3634 xfb_obj->program->sh.LinkedTransformFeedback;
3635 struct GENX(SO_DECL) so_decl[MAX_VERTEX_STREAMS][128];
3636 int buffer_mask[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
3637 int next_offset[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
3638 int decls[MAX_VERTEX_STREAMS] = {0, 0, 0, 0};
3639 int max_decls = 0;
3640 STATIC_ASSERT(ARRAY_SIZE(so_decl[0]) >= MAX_PROGRAM_OUTPUTS);
3641
3642 memset(so_decl, 0, sizeof(so_decl));
3643
3644 /* Construct the list of SO_DECLs to be emitted. The formatting of the
3645 * command feels strange -- each dword pair contains a SO_DECL per stream.
3646 */
3647 for (unsigned i = 0; i < linked_xfb_info->NumOutputs; i++) {
3648 const struct gl_transform_feedback_output *output =
3649 &linked_xfb_info->Outputs[i];
3650 const int buffer = output->OutputBuffer;
3651 const int varying = output->OutputRegister;
3652 const unsigned stream_id = output->StreamId;
3653 assert(stream_id < MAX_VERTEX_STREAMS);
3654
3655 buffer_mask[stream_id] |= 1 << buffer;
3656
3657 assert(vue_map->varying_to_slot[varying] >= 0);
3658
3659 /* Mesa doesn't store entries for gl_SkipComponents in the Outputs[]
3660 * array. Instead, it simply increments DstOffset for the following
3661 * input by the number of components that should be skipped.
3662 *
3663 * Our hardware is unusual in that it requires us to program SO_DECLs
3664 * for fake "hole" components, rather than simply taking the offset
3665 * for each real varying. Each hole can have size 1, 2, 3, or 4; we
3666 * program as many size = 4 holes as we can, then a final hole to
3667 * accommodate the final 1, 2, or 3 remaining.
3668 */
3669 int skip_components = output->DstOffset - next_offset[buffer];
3670
3671 while (skip_components > 0) {
3672 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
3673 .HoleFlag = 1,
3674 .OutputBufferSlot = output->OutputBuffer,
3675 .ComponentMask = (1 << MIN2(skip_components, 4)) - 1,
3676 };
3677 skip_components -= 4;
3678 }
3679
3680 next_offset[buffer] = output->DstOffset + output->NumComponents;
3681
3682 so_decl[stream_id][decls[stream_id]++] = (struct GENX(SO_DECL)) {
3683 .OutputBufferSlot = output->OutputBuffer,
3684 .RegisterIndex = vue_map->varying_to_slot[varying],
3685 .ComponentMask =
3686 ((1 << output->NumComponents) - 1) << output->ComponentOffset,
3687 };
3688
3689 if (decls[stream_id] > max_decls)
3690 max_decls = decls[stream_id];
3691 }
3692
3693 uint32_t *dw;
3694 dw = brw_batch_emitn(brw, GENX(3DSTATE_SO_DECL_LIST), 3 + 2 * max_decls,
3695 .StreamtoBufferSelects0 = buffer_mask[0],
3696 .StreamtoBufferSelects1 = buffer_mask[1],
3697 .StreamtoBufferSelects2 = buffer_mask[2],
3698 .StreamtoBufferSelects3 = buffer_mask[3],
3699 .NumEntries0 = decls[0],
3700 .NumEntries1 = decls[1],
3701 .NumEntries2 = decls[2],
3702 .NumEntries3 = decls[3]);
3703
3704 for (int i = 0; i < max_decls; i++) {
3705 GENX(SO_DECL_ENTRY_pack)(
3706 brw, dw + 2 + i * 2,
3707 &(struct GENX(SO_DECL_ENTRY)) {
3708 .Stream0Decl = so_decl[0][i],
3709 .Stream1Decl = so_decl[1][i],
3710 .Stream2Decl = so_decl[2][i],
3711 .Stream3Decl = so_decl[3][i],
3712 });
3713 }
3714 }
3715
3716 static void
3717 genX(upload_3dstate_so_buffers)(struct brw_context *brw)
3718 {
3719 struct gl_context *ctx = &brw->ctx;
3720 /* BRW_NEW_TRANSFORM_FEEDBACK */
3721 struct gl_transform_feedback_object *xfb_obj =
3722 ctx->TransformFeedback.CurrentObject;
3723 #if GEN_GEN < 8
3724 const struct gl_transform_feedback_info *linked_xfb_info =
3725 xfb_obj->program->sh.LinkedTransformFeedback;
3726 #else
3727 struct brw_transform_feedback_object *brw_obj =
3728 (struct brw_transform_feedback_object *) xfb_obj;
3729 uint32_t mocs_wb = GEN_GEN >= 9 ? SKL_MOCS_WB : BDW_MOCS_WB;
3730 #endif
3731
3732 /* Set up the up to 4 output buffers. These are the ranges defined in the
3733 * gl_transform_feedback_object.
3734 */
3735 for (int i = 0; i < 4; i++) {
3736 struct intel_buffer_object *bufferobj =
3737 intel_buffer_object(xfb_obj->Buffers[i]);
3738
3739 if (!bufferobj) {
3740 brw_batch_emit(brw, GENX(3DSTATE_SO_BUFFER), sob) {
3741 sob.SOBufferIndex = i;
3742 }
3743 continue;
3744 }
3745
3746 uint32_t start = xfb_obj->Offset[i];
3747 assert(start % 4 == 0);
3748 uint32_t end = ALIGN(start + xfb_obj->Size[i], 4);
3749 struct brw_bo *bo =
3750 intel_bufferobj_buffer(brw, bufferobj, start, end - start, true);
3751 assert(end <= bo->size);
3752
3753 brw_batch_emit(brw, GENX(3DSTATE_SO_BUFFER), sob) {
3754 sob.SOBufferIndex = i;
3755
3756 sob.SurfaceBaseAddress = rw_bo(bo, start);
3757 #if GEN_GEN < 8
3758 sob.SurfacePitch = linked_xfb_info->Buffers[i].Stride * 4;
3759 sob.SurfaceEndAddress = rw_bo(bo, end);
3760 #else
3761 sob.SOBufferEnable = true;
3762 sob.StreamOffsetWriteEnable = true;
3763 sob.StreamOutputBufferOffsetAddressEnable = true;
3764 sob.SOBufferMOCS = mocs_wb;
3765
3766 sob.SurfaceSize = MAX2(xfb_obj->Size[i] / 4, 1) - 1;
3767 sob.StreamOutputBufferOffsetAddress =
3768 rw_bo(brw_obj->offset_bo, i * sizeof(uint32_t));
3769
3770 if (brw_obj->zero_offsets) {
3771 /* Zero out the offset and write that to offset_bo */
3772 sob.StreamOffset = 0;
3773 } else {
3774 /* Use offset_bo as the "Stream Offset." */
3775 sob.StreamOffset = 0xFFFFFFFF;
3776 }
3777 #endif
3778 }
3779 }
3780
3781 #if GEN_GEN >= 8
3782 brw_obj->zero_offsets = false;
3783 #endif
3784 }
3785
3786 static bool
3787 query_active(struct gl_query_object *q)
3788 {
3789 return q && q->Active;
3790 }
3791
3792 static void
3793 genX(upload_3dstate_streamout)(struct brw_context *brw, bool active,
3794 const struct brw_vue_map *vue_map)
3795 {
3796 struct gl_context *ctx = &brw->ctx;
3797 /* BRW_NEW_TRANSFORM_FEEDBACK */
3798 struct gl_transform_feedback_object *xfb_obj =
3799 ctx->TransformFeedback.CurrentObject;
3800
3801 brw_batch_emit(brw, GENX(3DSTATE_STREAMOUT), sos) {
3802 if (active) {
3803 int urb_entry_read_offset = 0;
3804 int urb_entry_read_length = (vue_map->num_slots + 1) / 2 -
3805 urb_entry_read_offset;
3806
3807 sos.SOFunctionEnable = true;
3808 sos.SOStatisticsEnable = true;
3809
3810 /* BRW_NEW_RASTERIZER_DISCARD */
3811 if (ctx->RasterDiscard) {
3812 if (!query_active(ctx->Query.PrimitivesGenerated[0])) {
3813 sos.RenderingDisable = true;
3814 } else {
3815 perf_debug("Rasterizer discard with a GL_PRIMITIVES_GENERATED "
3816 "query active relies on the clipper.\n");
3817 }
3818 }
3819
3820 /* _NEW_LIGHT */
3821 if (ctx->Light.ProvokingVertex != GL_FIRST_VERTEX_CONVENTION)
3822 sos.ReorderMode = TRAILING;
3823
3824 #if GEN_GEN < 8
3825 sos.SOBufferEnable0 = xfb_obj->Buffers[0] != NULL;
3826 sos.SOBufferEnable1 = xfb_obj->Buffers[1] != NULL;
3827 sos.SOBufferEnable2 = xfb_obj->Buffers[2] != NULL;
3828 sos.SOBufferEnable3 = xfb_obj->Buffers[3] != NULL;
3829 #else
3830 const struct gl_transform_feedback_info *linked_xfb_info =
3831 xfb_obj->program->sh.LinkedTransformFeedback;
3832 /* Set buffer pitches; 0 means unbound. */
3833 if (xfb_obj->Buffers[0])
3834 sos.Buffer0SurfacePitch = linked_xfb_info->Buffers[0].Stride * 4;
3835 if (xfb_obj->Buffers[1])
3836 sos.Buffer1SurfacePitch = linked_xfb_info->Buffers[1].Stride * 4;
3837 if (xfb_obj->Buffers[2])
3838 sos.Buffer2SurfacePitch = linked_xfb_info->Buffers[2].Stride * 4;
3839 if (xfb_obj->Buffers[3])
3840 sos.Buffer3SurfacePitch = linked_xfb_info->Buffers[3].Stride * 4;
3841 #endif
3842
3843 /* We always read the whole vertex. This could be reduced at some
3844 * point by reading less and offsetting the register index in the
3845 * SO_DECLs.
3846 */
3847 sos.Stream0VertexReadOffset = urb_entry_read_offset;
3848 sos.Stream0VertexReadLength = urb_entry_read_length - 1;
3849 sos.Stream1VertexReadOffset = urb_entry_read_offset;
3850 sos.Stream1VertexReadLength = urb_entry_read_length - 1;
3851 sos.Stream2VertexReadOffset = urb_entry_read_offset;
3852 sos.Stream2VertexReadLength = urb_entry_read_length - 1;
3853 sos.Stream3VertexReadOffset = urb_entry_read_offset;
3854 sos.Stream3VertexReadLength = urb_entry_read_length - 1;
3855 }
3856 }
3857 }
3858
3859 static void
3860 genX(upload_sol)(struct brw_context *brw)
3861 {
3862 struct gl_context *ctx = &brw->ctx;
3863 /* BRW_NEW_TRANSFORM_FEEDBACK */
3864 bool active = _mesa_is_xfb_active_and_unpaused(ctx);
3865
3866 if (active) {
3867 genX(upload_3dstate_so_buffers)(brw);
3868
3869 /* BRW_NEW_VUE_MAP_GEOM_OUT */
3870 genX(upload_3dstate_so_decl_list)(brw, &brw->vue_map_geom_out);
3871 }
3872
3873 /* Finally, set up the SOL stage. This command must always follow updates to
3874 * the nonpipelined SOL state (3DSTATE_SO_BUFFER, 3DSTATE_SO_DECL_LIST) or
3875 * MMIO register updates (current performed by the kernel at each batch
3876 * emit).
3877 */
3878 genX(upload_3dstate_streamout)(brw, active, &brw->vue_map_geom_out);
3879 }
3880
3881 static const struct brw_tracked_state genX(sol_state) = {
3882 .dirty = {
3883 .mesa = _NEW_LIGHT,
3884 .brw = BRW_NEW_BATCH |
3885 BRW_NEW_BLORP |
3886 BRW_NEW_RASTERIZER_DISCARD |
3887 BRW_NEW_VUE_MAP_GEOM_OUT |
3888 BRW_NEW_TRANSFORM_FEEDBACK,
3889 },
3890 .emit = genX(upload_sol),
3891 };
3892 #endif
3893
3894 /* ---------------------------------------------------------------------- */
3895
3896 #if GEN_GEN >= 7
3897 static void
3898 genX(upload_ps)(struct brw_context *brw)
3899 {
3900 UNUSED const struct gl_context *ctx = &brw->ctx;
3901 UNUSED const struct gen_device_info *devinfo = &brw->screen->devinfo;
3902
3903 /* BRW_NEW_FS_PROG_DATA */
3904 const struct brw_wm_prog_data *prog_data =
3905 brw_wm_prog_data(brw->wm.base.prog_data);
3906 const struct brw_stage_state *stage_state = &brw->wm.base;
3907
3908 #if GEN_GEN < 8
3909 #endif
3910
3911 brw_batch_emit(brw, GENX(3DSTATE_PS), ps) {
3912 /* Initialize the execution mask with VMask. Otherwise, derivatives are
3913 * incorrect for subspans where some of the pixels are unlit. We believe
3914 * the bit just didn't take effect in previous generations.
3915 */
3916 ps.VectorMaskEnable = GEN_GEN >= 8;
3917
3918 ps.SamplerCount =
3919 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4);
3920
3921 /* BRW_NEW_FS_PROG_DATA */
3922 ps.BindingTableEntryCount = prog_data->base.binding_table.size_bytes / 4;
3923
3924 if (prog_data->base.use_alt_mode)
3925 ps.FloatingPointMode = Alternate;
3926
3927 /* Haswell requires the sample mask to be set in this packet as well as
3928 * in 3DSTATE_SAMPLE_MASK; the values should match.
3929 */
3930
3931 /* _NEW_BUFFERS, _NEW_MULTISAMPLE */
3932 #if GEN_IS_HASWELL
3933 ps.SampleMask = genX(determine_sample_mask(brw));
3934 #endif
3935
3936 /* 3DSTATE_PS expects the number of threads per PSD, which is always 64
3937 * for pre Gen11 and 128 for gen11+; On gen11+ If a programmed value is
3938 * k, it implies 2(k+1) threads. It implicitly scales for different GT
3939 * levels (which have some # of PSDs).
3940 *
3941 * In Gen8 the format is U8-2 whereas in Gen9+ it is U9-1.
3942 */
3943 #if GEN_GEN >= 9
3944 ps.MaximumNumberofThreadsPerPSD = 64 - 1;
3945 #elif GEN_GEN >= 8
3946 ps.MaximumNumberofThreadsPerPSD = 64 - 2;
3947 #else
3948 ps.MaximumNumberofThreads = devinfo->max_wm_threads - 1;
3949 #endif
3950
3951 if (prog_data->base.nr_params > 0 ||
3952 prog_data->base.ubo_ranges[0].length > 0)
3953 ps.PushConstantEnable = true;
3954
3955 #if GEN_GEN < 8
3956 /* From the IVB PRM, volume 2 part 1, page 287:
3957 * "This bit is inserted in the PS payload header and made available to
3958 * the DataPort (either via the message header or via header bypass) to
3959 * indicate that oMask data (one or two phases) is included in Render
3960 * Target Write messages. If present, the oMask data is used to mask off
3961 * samples."
3962 */
3963 ps.oMaskPresenttoRenderTarget = prog_data->uses_omask;
3964
3965 /* The hardware wedges if you have this bit set but don't turn on any
3966 * dual source blend factors.
3967 *
3968 * BRW_NEW_FS_PROG_DATA | _NEW_COLOR
3969 */
3970 ps.DualSourceBlendEnable = prog_data->dual_src_blend &&
3971 (ctx->Color.BlendEnabled & 1) &&
3972 ctx->Color.Blend[0]._UsesDualSrc;
3973
3974 /* BRW_NEW_FS_PROG_DATA */
3975 ps.AttributeEnable = (prog_data->num_varying_inputs != 0);
3976 #endif
3977
3978 /* From the documentation for this packet:
3979 * "If the PS kernel does not need the Position XY Offsets to
3980 * compute a Position Value, then this field should be programmed
3981 * to POSOFFSET_NONE."
3982 *
3983 * "SW Recommendation: If the PS kernel needs the Position Offsets
3984 * to compute a Position XY value, this field should match Position
3985 * ZW Interpolation Mode to ensure a consistent position.xyzw
3986 * computation."
3987 *
3988 * We only require XY sample offsets. So, this recommendation doesn't
3989 * look useful at the moment. We might need this in future.
3990 */
3991 if (prog_data->uses_pos_offset)
3992 ps.PositionXYOffsetSelect = POSOFFSET_SAMPLE;
3993 else
3994 ps.PositionXYOffsetSelect = POSOFFSET_NONE;
3995
3996 ps._8PixelDispatchEnable = prog_data->dispatch_8;
3997 ps._16PixelDispatchEnable = prog_data->dispatch_16;
3998 ps.DispatchGRFStartRegisterForConstantSetupData0 =
3999 prog_data->base.dispatch_grf_start_reg;
4000 ps.DispatchGRFStartRegisterForConstantSetupData2 =
4001 prog_data->dispatch_grf_start_reg_2;
4002
4003 ps.KernelStartPointer0 = stage_state->prog_offset;
4004 ps.KernelStartPointer2 = stage_state->prog_offset +
4005 prog_data->prog_offset_2;
4006
4007 if (prog_data->base.total_scratch) {
4008 ps.ScratchSpaceBasePointer =
4009 rw_32_bo(stage_state->scratch_bo,
4010 ffs(stage_state->per_thread_scratch) - 11);
4011 }
4012 }
4013 }
4014
4015 static const struct brw_tracked_state genX(ps_state) = {
4016 .dirty = {
4017 .mesa = _NEW_MULTISAMPLE |
4018 (GEN_GEN < 8 ? _NEW_BUFFERS |
4019 _NEW_COLOR
4020 : 0),
4021 .brw = BRW_NEW_BATCH |
4022 BRW_NEW_BLORP |
4023 BRW_NEW_FS_PROG_DATA,
4024 },
4025 .emit = genX(upload_ps),
4026 };
4027 #endif
4028
4029 /* ---------------------------------------------------------------------- */
4030
4031 #if GEN_GEN >= 7
4032 static void
4033 genX(upload_hs_state)(struct brw_context *brw)
4034 {
4035 const struct gen_device_info *devinfo = &brw->screen->devinfo;
4036 struct brw_stage_state *stage_state = &brw->tcs.base;
4037 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
4038 const struct brw_vue_prog_data *vue_prog_data =
4039 brw_vue_prog_data(stage_prog_data);
4040
4041 /* BRW_NEW_TES_PROG_DATA */
4042 struct brw_tcs_prog_data *tcs_prog_data =
4043 brw_tcs_prog_data(stage_prog_data);
4044
4045 if (!tcs_prog_data) {
4046 brw_batch_emit(brw, GENX(3DSTATE_HS), hs);
4047 } else {
4048 brw_batch_emit(brw, GENX(3DSTATE_HS), hs) {
4049 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
4050
4051 hs.InstanceCount = tcs_prog_data->instances - 1;
4052 hs.IncludeVertexHandles = true;
4053
4054 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
4055 }
4056 }
4057 }
4058
4059 static const struct brw_tracked_state genX(hs_state) = {
4060 .dirty = {
4061 .mesa = 0,
4062 .brw = BRW_NEW_BATCH |
4063 BRW_NEW_BLORP |
4064 BRW_NEW_TCS_PROG_DATA |
4065 BRW_NEW_TESS_PROGRAMS,
4066 },
4067 .emit = genX(upload_hs_state),
4068 };
4069
4070 static void
4071 genX(upload_ds_state)(struct brw_context *brw)
4072 {
4073 const struct gen_device_info *devinfo = &brw->screen->devinfo;
4074 const struct brw_stage_state *stage_state = &brw->tes.base;
4075 struct brw_stage_prog_data *stage_prog_data = stage_state->prog_data;
4076
4077 /* BRW_NEW_TES_PROG_DATA */
4078 const struct brw_tes_prog_data *tes_prog_data =
4079 brw_tes_prog_data(stage_prog_data);
4080 const struct brw_vue_prog_data *vue_prog_data =
4081 brw_vue_prog_data(stage_prog_data);
4082
4083 if (!tes_prog_data) {
4084 brw_batch_emit(brw, GENX(3DSTATE_DS), ds);
4085 } else {
4086 assert(GEN_GEN < 11 ||
4087 vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8);
4088
4089 brw_batch_emit(brw, GENX(3DSTATE_DS), ds) {
4090 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
4091
4092 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
4093 ds.ComputeWCoordinateEnable =
4094 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
4095
4096 #if GEN_GEN >= 8
4097 if (vue_prog_data->dispatch_mode == DISPATCH_MODE_SIMD8)
4098 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
4099 ds.UserClipDistanceCullTestEnableBitmask =
4100 vue_prog_data->cull_distance_mask;
4101 #endif
4102 }
4103 }
4104 }
4105
4106 static const struct brw_tracked_state genX(ds_state) = {
4107 .dirty = {
4108 .mesa = 0,
4109 .brw = BRW_NEW_BATCH |
4110 BRW_NEW_BLORP |
4111 BRW_NEW_TESS_PROGRAMS |
4112 BRW_NEW_TES_PROG_DATA,
4113 },
4114 .emit = genX(upload_ds_state),
4115 };
4116
4117 /* ---------------------------------------------------------------------- */
4118
4119 static void
4120 upload_te_state(struct brw_context *brw)
4121 {
4122 /* BRW_NEW_TESS_PROGRAMS */
4123 bool active = brw->programs[MESA_SHADER_TESS_EVAL];
4124
4125 /* BRW_NEW_TES_PROG_DATA */
4126 const struct brw_tes_prog_data *tes_prog_data =
4127 brw_tes_prog_data(brw->tes.base.prog_data);
4128
4129 if (active) {
4130 brw_batch_emit(brw, GENX(3DSTATE_TE), te) {
4131 te.Partitioning = tes_prog_data->partitioning;
4132 te.OutputTopology = tes_prog_data->output_topology;
4133 te.TEDomain = tes_prog_data->domain;
4134 te.TEEnable = true;
4135 te.MaximumTessellationFactorOdd = 63.0;
4136 te.MaximumTessellationFactorNotOdd = 64.0;
4137 }
4138 } else {
4139 brw_batch_emit(brw, GENX(3DSTATE_TE), te);
4140 }
4141 }
4142
4143 static const struct brw_tracked_state genX(te_state) = {
4144 .dirty = {
4145 .mesa = 0,
4146 .brw = BRW_NEW_BLORP |
4147 BRW_NEW_CONTEXT |
4148 BRW_NEW_TES_PROG_DATA |
4149 BRW_NEW_TESS_PROGRAMS,
4150 },
4151 .emit = upload_te_state,
4152 };
4153
4154 /* ---------------------------------------------------------------------- */
4155
4156 static void
4157 genX(upload_tes_push_constants)(struct brw_context *brw)
4158 {
4159 struct brw_stage_state *stage_state = &brw->tes.base;
4160 /* BRW_NEW_TESS_PROGRAMS */
4161 const struct gl_program *tep = brw->programs[MESA_SHADER_TESS_EVAL];
4162
4163 /* BRW_NEW_TES_PROG_DATA */
4164 const struct brw_stage_prog_data *prog_data = brw->tes.base.prog_data;
4165 gen6_upload_push_constants(brw, tep, prog_data, stage_state);
4166 }
4167
4168 static const struct brw_tracked_state genX(tes_push_constants) = {
4169 .dirty = {
4170 .mesa = _NEW_PROGRAM_CONSTANTS,
4171 .brw = BRW_NEW_BATCH |
4172 BRW_NEW_BLORP |
4173 BRW_NEW_TESS_PROGRAMS |
4174 BRW_NEW_TES_PROG_DATA,
4175 },
4176 .emit = genX(upload_tes_push_constants),
4177 };
4178
4179 static void
4180 genX(upload_tcs_push_constants)(struct brw_context *brw)
4181 {
4182 struct brw_stage_state *stage_state = &brw->tcs.base;
4183 /* BRW_NEW_TESS_PROGRAMS */
4184 const struct gl_program *tcp = brw->programs[MESA_SHADER_TESS_CTRL];
4185
4186 /* BRW_NEW_TCS_PROG_DATA */
4187 const struct brw_stage_prog_data *prog_data = brw->tcs.base.prog_data;
4188
4189 gen6_upload_push_constants(brw, tcp, prog_data, stage_state);
4190 }
4191
4192 static const struct brw_tracked_state genX(tcs_push_constants) = {
4193 .dirty = {
4194 .mesa = _NEW_PROGRAM_CONSTANTS,
4195 .brw = BRW_NEW_BATCH |
4196 BRW_NEW_BLORP |
4197 BRW_NEW_DEFAULT_TESS_LEVELS |
4198 BRW_NEW_TESS_PROGRAMS |
4199 BRW_NEW_TCS_PROG_DATA,
4200 },
4201 .emit = genX(upload_tcs_push_constants),
4202 };
4203
4204 #endif
4205
4206 /* ---------------------------------------------------------------------- */
4207
4208 #if GEN_GEN >= 7
4209 static void
4210 genX(upload_cs_push_constants)(struct brw_context *brw)
4211 {
4212 struct brw_stage_state *stage_state = &brw->cs.base;
4213
4214 /* BRW_NEW_COMPUTE_PROGRAM */
4215 const struct gl_program *cp = brw->programs[MESA_SHADER_COMPUTE];
4216
4217 if (cp) {
4218 /* BRW_NEW_CS_PROG_DATA */
4219 struct brw_cs_prog_data *cs_prog_data =
4220 brw_cs_prog_data(brw->cs.base.prog_data);
4221
4222 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_COMPUTE);
4223 brw_upload_cs_push_constants(brw, cp, cs_prog_data, stage_state);
4224 }
4225 }
4226
4227 const struct brw_tracked_state genX(cs_push_constants) = {
4228 .dirty = {
4229 .mesa = _NEW_PROGRAM_CONSTANTS,
4230 .brw = BRW_NEW_BATCH |
4231 BRW_NEW_BLORP |
4232 BRW_NEW_COMPUTE_PROGRAM |
4233 BRW_NEW_CS_PROG_DATA,
4234 },
4235 .emit = genX(upload_cs_push_constants),
4236 };
4237
4238 /**
4239 * Creates a new CS constant buffer reflecting the current CS program's
4240 * constants, if needed by the CS program.
4241 */
4242 static void
4243 genX(upload_cs_pull_constants)(struct brw_context *brw)
4244 {
4245 struct brw_stage_state *stage_state = &brw->cs.base;
4246
4247 /* BRW_NEW_COMPUTE_PROGRAM */
4248 struct brw_program *cp =
4249 (struct brw_program *) brw->programs[MESA_SHADER_COMPUTE];
4250
4251 /* BRW_NEW_CS_PROG_DATA */
4252 const struct brw_stage_prog_data *prog_data = brw->cs.base.prog_data;
4253
4254 _mesa_shader_write_subroutine_indices(&brw->ctx, MESA_SHADER_COMPUTE);
4255 /* _NEW_PROGRAM_CONSTANTS */
4256 brw_upload_pull_constants(brw, BRW_NEW_SURFACES, &cp->program,
4257 stage_state, prog_data);
4258 }
4259
4260 const struct brw_tracked_state genX(cs_pull_constants) = {
4261 .dirty = {
4262 .mesa = _NEW_PROGRAM_CONSTANTS,
4263 .brw = BRW_NEW_BATCH |
4264 BRW_NEW_BLORP |
4265 BRW_NEW_COMPUTE_PROGRAM |
4266 BRW_NEW_CS_PROG_DATA,
4267 },
4268 .emit = genX(upload_cs_pull_constants),
4269 };
4270
4271 static void
4272 genX(upload_cs_state)(struct brw_context *brw)
4273 {
4274 if (!brw->cs.base.prog_data)
4275 return;
4276
4277 uint32_t offset;
4278 uint32_t *desc = (uint32_t*) brw_state_batch(
4279 brw, GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t), 64,
4280 &offset);
4281
4282 struct brw_stage_state *stage_state = &brw->cs.base;
4283 struct brw_stage_prog_data *prog_data = stage_state->prog_data;
4284 struct brw_cs_prog_data *cs_prog_data = brw_cs_prog_data(prog_data);
4285 const struct gen_device_info *devinfo = &brw->screen->devinfo;
4286
4287 if (INTEL_DEBUG & DEBUG_SHADER_TIME) {
4288 brw_emit_buffer_surface_state(
4289 brw, &stage_state->surf_offset[
4290 prog_data->binding_table.shader_time_start],
4291 brw->shader_time.bo, 0, ISL_FORMAT_RAW,
4292 brw->shader_time.bo->size, 1,
4293 RELOC_WRITE);
4294 }
4295
4296 uint32_t *bind = brw_state_batch(brw, prog_data->binding_table.size_bytes,
4297 32, &stage_state->bind_bo_offset);
4298
4299 /* The MEDIA_VFE_STATE documentation for Gen8+ says:
4300 *
4301 * "A stalling PIPE_CONTROL is required before MEDIA_VFE_STATE unless
4302 * the only bits that are changed are scoreboard related: Scoreboard
4303 * Enable, Scoreboard Type, Scoreboard Mask, Scoreboard * Delta. For
4304 * these scoreboard related states, a MEDIA_STATE_FLUSH is sufficient."
4305 *
4306 * Earlier generations say "MI_FLUSH" instead of "stalling PIPE_CONTROL",
4307 * but MI_FLUSH isn't really a thing, so we assume they meant PIPE_CONTROL.
4308 */
4309 brw_emit_pipe_control_flush(brw, PIPE_CONTROL_CS_STALL);
4310
4311 brw_batch_emit(brw, GENX(MEDIA_VFE_STATE), vfe) {
4312 if (prog_data->total_scratch) {
4313 uint32_t per_thread_scratch_value;
4314
4315 if (GEN_GEN >= 8) {
4316 /* Broadwell's Per Thread Scratch Space is in the range [0, 11]
4317 * where 0 = 1k, 1 = 2k, 2 = 4k, ..., 11 = 2M.
4318 */
4319 per_thread_scratch_value = ffs(stage_state->per_thread_scratch) - 11;
4320 } else if (GEN_IS_HASWELL) {
4321 /* Haswell's Per Thread Scratch Space is in the range [0, 10]
4322 * where 0 = 2k, 1 = 4k, 2 = 8k, ..., 10 = 2M.
4323 */
4324 per_thread_scratch_value = ffs(stage_state->per_thread_scratch) - 12;
4325 } else {
4326 /* Earlier platforms use the range [0, 11] to mean [1kB, 12kB]
4327 * where 0 = 1kB, 1 = 2kB, 2 = 3kB, ..., 11 = 12kB.
4328 */
4329 per_thread_scratch_value = stage_state->per_thread_scratch / 1024 - 1;
4330 }
4331 vfe.ScratchSpaceBasePointer = rw_32_bo(stage_state->scratch_bo, 0);
4332 vfe.PerThreadScratchSpace = per_thread_scratch_value;
4333 }
4334
4335 /* If brw->screen->subslice_total is greater than one, then
4336 * devinfo->max_cs_threads stores number of threads per sub-slice;
4337 * thus we need to multiply by that number by subslices to get
4338 * the actual maximum number of threads; the -1 is because the HW
4339 * has a bias of 1 (would not make sense to say the maximum number
4340 * of threads is 0).
4341 */
4342 const uint32_t subslices = MAX2(brw->screen->subslice_total, 1);
4343 vfe.MaximumNumberofThreads = devinfo->max_cs_threads * subslices - 1;
4344 vfe.NumberofURBEntries = GEN_GEN >= 8 ? 2 : 0;
4345 #if GEN_GEN < 11
4346 vfe.ResetGatewayTimer =
4347 Resettingrelativetimerandlatchingtheglobaltimestamp;
4348 #endif
4349 #if GEN_GEN < 9
4350 vfe.BypassGatewayControl = BypassingOpenGatewayCloseGatewayprotocol;
4351 #endif
4352 #if GEN_GEN == 7
4353 vfe.GPGPUMode = 1;
4354 #endif
4355
4356 /* We are uploading duplicated copies of push constant uniforms for each
4357 * thread. Although the local id data needs to vary per thread, it won't
4358 * change for other uniform data. Unfortunately this duplication is
4359 * required for gen7. As of Haswell, this duplication can be avoided,
4360 * but this older mechanism with duplicated data continues to work.
4361 *
4362 * FINISHME: As of Haswell, we could make use of the
4363 * INTERFACE_DESCRIPTOR_DATA "Cross-Thread Constant Data Read Length"
4364 * field to only store one copy of uniform data.
4365 *
4366 * FINISHME: Broadwell adds a new alternative "Indirect Payload Storage"
4367 * which is described in the GPGPU_WALKER command and in the Broadwell
4368 * PRM Volume 7: 3D Media GPGPU, under Media GPGPU Pipeline => Mode of
4369 * Operations => GPGPU Mode => Indirect Payload Storage.
4370 *
4371 * Note: The constant data is built in brw_upload_cs_push_constants
4372 * below.
4373 */
4374 vfe.URBEntryAllocationSize = GEN_GEN >= 8 ? 2 : 0;
4375
4376 const uint32_t vfe_curbe_allocation =
4377 ALIGN(cs_prog_data->push.per_thread.regs * cs_prog_data->threads +
4378 cs_prog_data->push.cross_thread.regs, 2);
4379 vfe.CURBEAllocationSize = vfe_curbe_allocation;
4380 }
4381
4382 if (cs_prog_data->push.total.size > 0) {
4383 brw_batch_emit(brw, GENX(MEDIA_CURBE_LOAD), curbe) {
4384 curbe.CURBETotalDataLength =
4385 ALIGN(cs_prog_data->push.total.size, 64);
4386 curbe.CURBEDataStartAddress = stage_state->push_const_offset;
4387 }
4388 }
4389
4390 /* BRW_NEW_SURFACES and BRW_NEW_*_CONSTBUF */
4391 memcpy(bind, stage_state->surf_offset,
4392 prog_data->binding_table.size_bytes);
4393 const struct GENX(INTERFACE_DESCRIPTOR_DATA) idd = {
4394 .KernelStartPointer = brw->cs.base.prog_offset,
4395 .SamplerStatePointer = stage_state->sampler_offset,
4396 .SamplerCount = DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4),
4397 .BindingTablePointer = stage_state->bind_bo_offset,
4398 .ConstantURBEntryReadLength = cs_prog_data->push.per_thread.regs,
4399 .NumberofThreadsinGPGPUThreadGroup = cs_prog_data->threads,
4400 .SharedLocalMemorySize = encode_slm_size(GEN_GEN,
4401 prog_data->total_shared),
4402 .BarrierEnable = cs_prog_data->uses_barrier,
4403 #if GEN_GEN >= 8 || GEN_IS_HASWELL
4404 .CrossThreadConstantDataReadLength =
4405 cs_prog_data->push.cross_thread.regs,
4406 #endif
4407 };
4408
4409 GENX(INTERFACE_DESCRIPTOR_DATA_pack)(brw, desc, &idd);
4410
4411 brw_batch_emit(brw, GENX(MEDIA_INTERFACE_DESCRIPTOR_LOAD), load) {
4412 load.InterfaceDescriptorTotalLength =
4413 GENX(INTERFACE_DESCRIPTOR_DATA_length) * sizeof(uint32_t);
4414 load.InterfaceDescriptorDataStartAddress = offset;
4415 }
4416 }
4417
4418 static const struct brw_tracked_state genX(cs_state) = {
4419 .dirty = {
4420 .mesa = _NEW_PROGRAM_CONSTANTS,
4421 .brw = BRW_NEW_BATCH |
4422 BRW_NEW_BLORP |
4423 BRW_NEW_CS_PROG_DATA |
4424 BRW_NEW_SAMPLER_STATE_TABLE |
4425 BRW_NEW_SURFACES,
4426 },
4427 .emit = genX(upload_cs_state)
4428 };
4429
4430 #endif
4431
4432 /* ---------------------------------------------------------------------- */
4433
4434 #if GEN_GEN >= 8
4435 static void
4436 genX(upload_raster)(struct brw_context *brw)
4437 {
4438 const struct gl_context *ctx = &brw->ctx;
4439
4440 /* _NEW_BUFFERS */
4441 const bool render_to_fbo = _mesa_is_user_fbo(ctx->DrawBuffer);
4442
4443 /* _NEW_POLYGON */
4444 const struct gl_polygon_attrib *polygon = &ctx->Polygon;
4445
4446 /* _NEW_POINT */
4447 const struct gl_point_attrib *point = &ctx->Point;
4448
4449 brw_batch_emit(brw, GENX(3DSTATE_RASTER), raster) {
4450 if (brw->polygon_front_bit == render_to_fbo)
4451 raster.FrontWinding = CounterClockwise;
4452
4453 if (polygon->CullFlag) {
4454 switch (polygon->CullFaceMode) {
4455 case GL_FRONT:
4456 raster.CullMode = CULLMODE_FRONT;
4457 break;
4458 case GL_BACK:
4459 raster.CullMode = CULLMODE_BACK;
4460 break;
4461 case GL_FRONT_AND_BACK:
4462 raster.CullMode = CULLMODE_BOTH;
4463 break;
4464 default:
4465 unreachable("not reached");
4466 }
4467 } else {
4468 raster.CullMode = CULLMODE_NONE;
4469 }
4470
4471 raster.SmoothPointEnable = point->SmoothFlag;
4472
4473 raster.DXMultisampleRasterizationEnable =
4474 _mesa_is_multisample_enabled(ctx);
4475
4476 raster.GlobalDepthOffsetEnableSolid = polygon->OffsetFill;
4477 raster.GlobalDepthOffsetEnableWireframe = polygon->OffsetLine;
4478 raster.GlobalDepthOffsetEnablePoint = polygon->OffsetPoint;
4479
4480 switch (polygon->FrontMode) {
4481 case GL_FILL:
4482 raster.FrontFaceFillMode = FILL_MODE_SOLID;
4483 break;
4484 case GL_LINE:
4485 raster.FrontFaceFillMode = FILL_MODE_WIREFRAME;
4486 break;
4487 case GL_POINT:
4488 raster.FrontFaceFillMode = FILL_MODE_POINT;
4489 break;
4490 default:
4491 unreachable("not reached");
4492 }
4493
4494 switch (polygon->BackMode) {
4495 case GL_FILL:
4496 raster.BackFaceFillMode = FILL_MODE_SOLID;
4497 break;
4498 case GL_LINE:
4499 raster.BackFaceFillMode = FILL_MODE_WIREFRAME;
4500 break;
4501 case GL_POINT:
4502 raster.BackFaceFillMode = FILL_MODE_POINT;
4503 break;
4504 default:
4505 unreachable("not reached");
4506 }
4507
4508 /* _NEW_LINE */
4509 raster.AntialiasingEnable = ctx->Line.SmoothFlag;
4510
4511 #if GEN_GEN == 10
4512 /* _NEW_BUFFERS
4513 * Antialiasing Enable bit MUST not be set when NUM_MULTISAMPLES > 1.
4514 */
4515 const bool multisampled_fbo =
4516 _mesa_geometric_samples(ctx->DrawBuffer) > 1;
4517 if (multisampled_fbo)
4518 raster.AntialiasingEnable = false;
4519 #endif
4520
4521 /* _NEW_SCISSOR */
4522 raster.ScissorRectangleEnable = ctx->Scissor.EnableFlags;
4523
4524 /* _NEW_TRANSFORM */
4525 if (!ctx->Transform.DepthClamp) {
4526 #if GEN_GEN >= 9
4527 raster.ViewportZFarClipTestEnable = true;
4528 raster.ViewportZNearClipTestEnable = true;
4529 #else
4530 raster.ViewportZClipTestEnable = true;
4531 #endif
4532 }
4533
4534 /* BRW_NEW_CONSERVATIVE_RASTERIZATION */
4535 #if GEN_GEN >= 9
4536 raster.ConservativeRasterizationEnable =
4537 ctx->IntelConservativeRasterization;
4538 #endif
4539
4540 raster.GlobalDepthOffsetClamp = polygon->OffsetClamp;
4541 raster.GlobalDepthOffsetScale = polygon->OffsetFactor;
4542
4543 raster.GlobalDepthOffsetConstant = polygon->OffsetUnits * 2;
4544 }
4545 }
4546
4547 static const struct brw_tracked_state genX(raster_state) = {
4548 .dirty = {
4549 .mesa = _NEW_BUFFERS |
4550 _NEW_LINE |
4551 _NEW_MULTISAMPLE |
4552 _NEW_POINT |
4553 _NEW_POLYGON |
4554 _NEW_SCISSOR |
4555 _NEW_TRANSFORM,
4556 .brw = BRW_NEW_BLORP |
4557 BRW_NEW_CONTEXT |
4558 BRW_NEW_CONSERVATIVE_RASTERIZATION,
4559 },
4560 .emit = genX(upload_raster),
4561 };
4562 #endif
4563
4564 /* ---------------------------------------------------------------------- */
4565
4566 #if GEN_GEN >= 8
4567 static void
4568 genX(upload_ps_extra)(struct brw_context *brw)
4569 {
4570 UNUSED struct gl_context *ctx = &brw->ctx;
4571
4572 const struct brw_wm_prog_data *prog_data =
4573 brw_wm_prog_data(brw->wm.base.prog_data);
4574
4575 brw_batch_emit(brw, GENX(3DSTATE_PS_EXTRA), psx) {
4576 psx.PixelShaderValid = true;
4577 psx.PixelShaderComputedDepthMode = prog_data->computed_depth_mode;
4578 psx.PixelShaderKillsPixel = prog_data->uses_kill;
4579 psx.AttributeEnable = prog_data->num_varying_inputs != 0;
4580 psx.PixelShaderUsesSourceDepth = prog_data->uses_src_depth;
4581 psx.PixelShaderUsesSourceW = prog_data->uses_src_w;
4582 psx.PixelShaderIsPerSample = prog_data->persample_dispatch;
4583
4584 /* _NEW_MULTISAMPLE | BRW_NEW_CONSERVATIVE_RASTERIZATION */
4585 if (prog_data->uses_sample_mask) {
4586 #if GEN_GEN >= 9
4587 if (prog_data->post_depth_coverage)
4588 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
4589 else if (prog_data->inner_coverage && ctx->IntelConservativeRasterization)
4590 psx.InputCoverageMaskState = ICMS_INNER_CONSERVATIVE;
4591 else
4592 psx.InputCoverageMaskState = ICMS_NORMAL;
4593 #else
4594 psx.PixelShaderUsesInputCoverageMask = true;
4595 #endif
4596 }
4597
4598 psx.oMaskPresenttoRenderTarget = prog_data->uses_omask;
4599 #if GEN_GEN >= 9
4600 psx.PixelShaderPullsBary = prog_data->pulls_bary;
4601 psx.PixelShaderComputesStencil = prog_data->computed_stencil;
4602 #endif
4603
4604 /* The stricter cross-primitive coherency guarantees that the hardware
4605 * gives us with the "Accesses UAV" bit set for at least one shader stage
4606 * and the "UAV coherency required" bit set on the 3DPRIMITIVE command
4607 * are redundant within the current image, atomic counter and SSBO GL
4608 * APIs, which all have very loose ordering and coherency requirements
4609 * and generally rely on the application to insert explicit barriers when
4610 * a shader invocation is expected to see the memory writes performed by
4611 * the invocations of some previous primitive. Regardless of the value
4612 * of "UAV coherency required", the "Accesses UAV" bits will implicitly
4613 * cause an in most cases useless DC flush when the lowermost stage with
4614 * the bit set finishes execution.
4615 *
4616 * It would be nice to disable it, but in some cases we can't because on
4617 * Gen8+ it also has an influence on rasterization via the PS UAV-only
4618 * signal (which could be set independently from the coherency mechanism
4619 * in the 3DSTATE_WM command on Gen7), and because in some cases it will
4620 * determine whether the hardware skips execution of the fragment shader
4621 * or not via the ThreadDispatchEnable signal. However if we know that
4622 * GEN8_PS_BLEND_HAS_WRITEABLE_RT is going to be set and
4623 * GEN8_PSX_PIXEL_SHADER_NO_RT_WRITE is not set it shouldn't make any
4624 * difference so we may just disable it here.
4625 *
4626 * Gen8 hardware tries to compute ThreadDispatchEnable for us but doesn't
4627 * take into account KillPixels when no depth or stencil writes are
4628 * enabled. In order for occlusion queries to work correctly with no
4629 * attachments, we need to force-enable here.
4630 *
4631 * BRW_NEW_FS_PROG_DATA | BRW_NEW_FRAGMENT_PROGRAM | _NEW_BUFFERS |
4632 * _NEW_COLOR
4633 */
4634 if ((prog_data->has_side_effects || prog_data->uses_kill) &&
4635 !brw_color_buffer_write_enabled(brw))
4636 psx.PixelShaderHasUAV = true;
4637 }
4638 }
4639
4640 const struct brw_tracked_state genX(ps_extra) = {
4641 .dirty = {
4642 .mesa = _NEW_BUFFERS | _NEW_COLOR,
4643 .brw = BRW_NEW_BLORP |
4644 BRW_NEW_CONTEXT |
4645 BRW_NEW_FRAGMENT_PROGRAM |
4646 BRW_NEW_FS_PROG_DATA |
4647 BRW_NEW_CONSERVATIVE_RASTERIZATION,
4648 },
4649 .emit = genX(upload_ps_extra),
4650 };
4651 #endif
4652
4653 /* ---------------------------------------------------------------------- */
4654
4655 #if GEN_GEN >= 8
4656 static void
4657 genX(upload_ps_blend)(struct brw_context *brw)
4658 {
4659 struct gl_context *ctx = &brw->ctx;
4660
4661 /* _NEW_BUFFERS */
4662 struct gl_renderbuffer *rb = ctx->DrawBuffer->_ColorDrawBuffers[0];
4663 const bool buffer0_is_integer = ctx->DrawBuffer->_IntegerBuffers & 0x1;
4664
4665 /* _NEW_COLOR */
4666 struct gl_colorbuffer_attrib *color = &ctx->Color;
4667
4668 brw_batch_emit(brw, GENX(3DSTATE_PS_BLEND), pb) {
4669 /* BRW_NEW_FRAGMENT_PROGRAM | _NEW_BUFFERS | _NEW_COLOR */
4670 pb.HasWriteableRT = brw_color_buffer_write_enabled(brw);
4671
4672 bool alpha_to_one = false;
4673
4674 if (!buffer0_is_integer) {
4675 /* _NEW_MULTISAMPLE */
4676
4677 if (_mesa_is_multisample_enabled(ctx)) {
4678 pb.AlphaToCoverageEnable = ctx->Multisample.SampleAlphaToCoverage;
4679 alpha_to_one = ctx->Multisample.SampleAlphaToOne;
4680 }
4681
4682 pb.AlphaTestEnable = color->AlphaEnabled;
4683 }
4684
4685 /* Used for implementing the following bit of GL_EXT_texture_integer:
4686 * "Per-fragment operations that require floating-point color
4687 * components, including multisample alpha operations, alpha test,
4688 * blending, and dithering, have no effect when the corresponding
4689 * colors are written to an integer color buffer."
4690 *
4691 * The OpenGL specification 3.3 (page 196), section 4.1.3 says:
4692 * "If drawbuffer zero is not NONE and the buffer it references has an
4693 * integer format, the SAMPLE_ALPHA_TO_COVERAGE and SAMPLE_ALPHA_TO_ONE
4694 * operations are skipped."
4695 */
4696 if (rb && !buffer0_is_integer && (color->BlendEnabled & 1)) {
4697 GLenum eqRGB = color->Blend[0].EquationRGB;
4698 GLenum eqA = color->Blend[0].EquationA;
4699 GLenum srcRGB = color->Blend[0].SrcRGB;
4700 GLenum dstRGB = color->Blend[0].DstRGB;
4701 GLenum srcA = color->Blend[0].SrcA;
4702 GLenum dstA = color->Blend[0].DstA;
4703
4704 if (eqRGB == GL_MIN || eqRGB == GL_MAX)
4705 srcRGB = dstRGB = GL_ONE;
4706
4707 if (eqA == GL_MIN || eqA == GL_MAX)
4708 srcA = dstA = GL_ONE;
4709
4710 /* Due to hardware limitations, the destination may have information
4711 * in an alpha channel even when the format specifies no alpha
4712 * channel. In order to avoid getting any incorrect blending due to
4713 * that alpha channel, coerce the blend factors to values that will
4714 * not read the alpha channel, but will instead use the correct
4715 * implicit value for alpha.
4716 */
4717 if (!_mesa_base_format_has_channel(rb->_BaseFormat,
4718 GL_TEXTURE_ALPHA_TYPE)) {
4719 srcRGB = brw_fix_xRGB_alpha(srcRGB);
4720 srcA = brw_fix_xRGB_alpha(srcA);
4721 dstRGB = brw_fix_xRGB_alpha(dstRGB);
4722 dstA = brw_fix_xRGB_alpha(dstA);
4723 }
4724
4725 /* Alpha to One doesn't work with Dual Color Blending. Override
4726 * SRC1_ALPHA to ONE and ONE_MINUS_SRC1_ALPHA to ZERO.
4727 */
4728 if (alpha_to_one && color->Blend[0]._UsesDualSrc) {
4729 srcRGB = fix_dual_blend_alpha_to_one(srcRGB);
4730 srcA = fix_dual_blend_alpha_to_one(srcA);
4731 dstRGB = fix_dual_blend_alpha_to_one(dstRGB);
4732 dstA = fix_dual_blend_alpha_to_one(dstA);
4733 }
4734
4735 pb.ColorBufferBlendEnable = true;
4736 pb.SourceAlphaBlendFactor = brw_translate_blend_factor(srcA);
4737 pb.DestinationAlphaBlendFactor = brw_translate_blend_factor(dstA);
4738 pb.SourceBlendFactor = brw_translate_blend_factor(srcRGB);
4739 pb.DestinationBlendFactor = brw_translate_blend_factor(dstRGB);
4740
4741 pb.IndependentAlphaBlendEnable =
4742 srcA != srcRGB || dstA != dstRGB || eqA != eqRGB;
4743 }
4744 }
4745 }
4746
4747 static const struct brw_tracked_state genX(ps_blend) = {
4748 .dirty = {
4749 .mesa = _NEW_BUFFERS |
4750 _NEW_COLOR |
4751 _NEW_MULTISAMPLE,
4752 .brw = BRW_NEW_BLORP |
4753 BRW_NEW_CONTEXT |
4754 BRW_NEW_FRAGMENT_PROGRAM,
4755 },
4756 .emit = genX(upload_ps_blend)
4757 };
4758 #endif
4759
4760 /* ---------------------------------------------------------------------- */
4761
4762 #if GEN_GEN >= 8
4763 static void
4764 genX(emit_vf_topology)(struct brw_context *brw)
4765 {
4766 brw_batch_emit(brw, GENX(3DSTATE_VF_TOPOLOGY), vftopo) {
4767 vftopo.PrimitiveTopologyType = brw->primitive;
4768 }
4769 }
4770
4771 static const struct brw_tracked_state genX(vf_topology) = {
4772 .dirty = {
4773 .mesa = 0,
4774 .brw = BRW_NEW_BLORP |
4775 BRW_NEW_PRIMITIVE,
4776 },
4777 .emit = genX(emit_vf_topology),
4778 };
4779 #endif
4780
4781 /* ---------------------------------------------------------------------- */
4782
4783 #if GEN_GEN >= 7
4784 static void
4785 genX(emit_mi_report_perf_count)(struct brw_context *brw,
4786 struct brw_bo *bo,
4787 uint32_t offset_in_bytes,
4788 uint32_t report_id)
4789 {
4790 brw_batch_emit(brw, GENX(MI_REPORT_PERF_COUNT), mi_rpc) {
4791 mi_rpc.MemoryAddress = ggtt_bo(bo, offset_in_bytes);
4792 mi_rpc.ReportID = report_id;
4793 }
4794 }
4795 #endif
4796
4797 /* ---------------------------------------------------------------------- */
4798
4799 /**
4800 * Emit a 3DSTATE_SAMPLER_STATE_POINTERS_{VS,HS,GS,DS,PS} packet.
4801 */
4802 static void
4803 genX(emit_sampler_state_pointers_xs)(MAYBE_UNUSED struct brw_context *brw,
4804 MAYBE_UNUSED struct brw_stage_state *stage_state)
4805 {
4806 #if GEN_GEN >= 7
4807 static const uint16_t packet_headers[] = {
4808 [MESA_SHADER_VERTEX] = 43,
4809 [MESA_SHADER_TESS_CTRL] = 44,
4810 [MESA_SHADER_TESS_EVAL] = 45,
4811 [MESA_SHADER_GEOMETRY] = 46,
4812 [MESA_SHADER_FRAGMENT] = 47,
4813 };
4814
4815 /* Ivybridge requires a workaround flush before VS packets. */
4816 if (GEN_GEN == 7 && !GEN_IS_HASWELL &&
4817 stage_state->stage == MESA_SHADER_VERTEX) {
4818 gen7_emit_vs_workaround_flush(brw);
4819 }
4820
4821 brw_batch_emit(brw, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
4822 ptr._3DCommandSubOpcode = packet_headers[stage_state->stage];
4823 ptr.PointertoVSSamplerState = stage_state->sampler_offset;
4824 }
4825 #endif
4826 }
4827
4828 UNUSED static bool
4829 has_component(mesa_format format, int i)
4830 {
4831 if (_mesa_is_format_color_format(format))
4832 return _mesa_format_has_color_component(format, i);
4833
4834 /* depth and stencil have only one component */
4835 return i == 0;
4836 }
4837
4838 /**
4839 * Upload SAMPLER_BORDER_COLOR_STATE.
4840 */
4841 static void
4842 genX(upload_default_color)(struct brw_context *brw,
4843 const struct gl_sampler_object *sampler,
4844 MAYBE_UNUSED mesa_format format, GLenum base_format,
4845 bool is_integer_format, bool is_stencil_sampling,
4846 uint32_t *sdc_offset)
4847 {
4848 union gl_color_union color;
4849
4850 switch (base_format) {
4851 case GL_DEPTH_COMPONENT:
4852 /* GL specs that border color for depth textures is taken from the
4853 * R channel, while the hardware uses A. Spam R into all the
4854 * channels for safety.
4855 */
4856 color.ui[0] = sampler->BorderColor.ui[0];
4857 color.ui[1] = sampler->BorderColor.ui[0];
4858 color.ui[2] = sampler->BorderColor.ui[0];
4859 color.ui[3] = sampler->BorderColor.ui[0];
4860 break;
4861 case GL_ALPHA:
4862 color.ui[0] = 0u;
4863 color.ui[1] = 0u;
4864 color.ui[2] = 0u;
4865 color.ui[3] = sampler->BorderColor.ui[3];
4866 break;
4867 case GL_INTENSITY:
4868 color.ui[0] = sampler->BorderColor.ui[0];
4869 color.ui[1] = sampler->BorderColor.ui[0];
4870 color.ui[2] = sampler->BorderColor.ui[0];
4871 color.ui[3] = sampler->BorderColor.ui[0];
4872 break;
4873 case GL_LUMINANCE:
4874 color.ui[0] = sampler->BorderColor.ui[0];
4875 color.ui[1] = sampler->BorderColor.ui[0];
4876 color.ui[2] = sampler->BorderColor.ui[0];
4877 color.ui[3] = float_as_int(1.0);
4878 break;
4879 case GL_LUMINANCE_ALPHA:
4880 color.ui[0] = sampler->BorderColor.ui[0];
4881 color.ui[1] = sampler->BorderColor.ui[0];
4882 color.ui[2] = sampler->BorderColor.ui[0];
4883 color.ui[3] = sampler->BorderColor.ui[3];
4884 break;
4885 default:
4886 color.ui[0] = sampler->BorderColor.ui[0];
4887 color.ui[1] = sampler->BorderColor.ui[1];
4888 color.ui[2] = sampler->BorderColor.ui[2];
4889 color.ui[3] = sampler->BorderColor.ui[3];
4890 break;
4891 }
4892
4893 /* In some cases we use an RGBA surface format for GL RGB textures,
4894 * where we've initialized the A channel to 1.0. We also have to set
4895 * the border color alpha to 1.0 in that case.
4896 */
4897 if (base_format == GL_RGB)
4898 color.ui[3] = float_as_int(1.0);
4899
4900 int alignment = 32;
4901 if (GEN_GEN >= 8) {
4902 alignment = 64;
4903 } else if (GEN_IS_HASWELL && (is_integer_format || is_stencil_sampling)) {
4904 alignment = 512;
4905 }
4906
4907 uint32_t *sdc = brw_state_batch(
4908 brw, GENX(SAMPLER_BORDER_COLOR_STATE_length) * sizeof(uint32_t),
4909 alignment, sdc_offset);
4910
4911 struct GENX(SAMPLER_BORDER_COLOR_STATE) state = { 0 };
4912
4913 #define ASSIGN(dst, src) \
4914 do { \
4915 dst = src; \
4916 } while (0)
4917
4918 #define ASSIGNu16(dst, src) \
4919 do { \
4920 dst = (uint16_t)src; \
4921 } while (0)
4922
4923 #define ASSIGNu8(dst, src) \
4924 do { \
4925 dst = (uint8_t)src; \
4926 } while (0)
4927
4928 #define BORDER_COLOR_ATTR(macro, _color_type, src) \
4929 macro(state.BorderColor ## _color_type ## Red, src[0]); \
4930 macro(state.BorderColor ## _color_type ## Green, src[1]); \
4931 macro(state.BorderColor ## _color_type ## Blue, src[2]); \
4932 macro(state.BorderColor ## _color_type ## Alpha, src[3]);
4933
4934 #if GEN_GEN >= 8
4935 /* On Broadwell, the border color is represented as four 32-bit floats,
4936 * integers, or unsigned values, interpreted according to the surface
4937 * format. This matches the sampler->BorderColor union exactly; just
4938 * memcpy the values.
4939 */
4940 BORDER_COLOR_ATTR(ASSIGN, 32bit, color.ui);
4941 #elif GEN_IS_HASWELL
4942 if (is_integer_format || is_stencil_sampling) {
4943 bool stencil = format == MESA_FORMAT_S_UINT8 || is_stencil_sampling;
4944 const int bits_per_channel =
4945 _mesa_get_format_bits(format, stencil ? GL_STENCIL_BITS : GL_RED_BITS);
4946
4947 /* From the Haswell PRM, "Command Reference: Structures", Page 36:
4948 * "If any color channel is missing from the surface format,
4949 * corresponding border color should be programmed as zero and if
4950 * alpha channel is missing, corresponding Alpha border color should
4951 * be programmed as 1."
4952 */
4953 unsigned c[4] = { 0, 0, 0, 1 };
4954 for (int i = 0; i < 4; i++) {
4955 if (has_component(format, i))
4956 c[i] = color.ui[i];
4957 }
4958
4959 switch (bits_per_channel) {
4960 case 8:
4961 /* Copy RGBA in order. */
4962 BORDER_COLOR_ATTR(ASSIGNu8, 8bit, c);
4963 break;
4964 case 10:
4965 /* R10G10B10A2_UINT is treated like a 16-bit format. */
4966 case 16:
4967 BORDER_COLOR_ATTR(ASSIGNu16, 16bit, c);
4968 break;
4969 case 32:
4970 if (base_format == GL_RG) {
4971 /* Careful inspection of the tables reveals that for RG32 formats,
4972 * the green channel needs to go where blue normally belongs.
4973 */
4974 state.BorderColor32bitRed = c[0];
4975 state.BorderColor32bitBlue = c[1];
4976 state.BorderColor32bitAlpha = 1;
4977 } else {
4978 /* Copy RGBA in order. */
4979 BORDER_COLOR_ATTR(ASSIGN, 32bit, c);
4980 }
4981 break;
4982 default:
4983 assert(!"Invalid number of bits per channel in integer format.");
4984 break;
4985 }
4986 } else {
4987 BORDER_COLOR_ATTR(ASSIGN, Float, color.f);
4988 }
4989 #elif GEN_GEN == 5 || GEN_GEN == 6
4990 BORDER_COLOR_ATTR(UNCLAMPED_FLOAT_TO_UBYTE, Unorm, color.f);
4991 BORDER_COLOR_ATTR(UNCLAMPED_FLOAT_TO_USHORT, Unorm16, color.f);
4992 BORDER_COLOR_ATTR(UNCLAMPED_FLOAT_TO_SHORT, Snorm16, color.f);
4993
4994 #define MESA_FLOAT_TO_HALF(dst, src) \
4995 dst = _mesa_float_to_half(src);
4996
4997 BORDER_COLOR_ATTR(MESA_FLOAT_TO_HALF, Float16, color.f);
4998
4999 #undef MESA_FLOAT_TO_HALF
5000
5001 state.BorderColorSnorm8Red = state.BorderColorSnorm16Red >> 8;
5002 state.BorderColorSnorm8Green = state.BorderColorSnorm16Green >> 8;
5003 state.BorderColorSnorm8Blue = state.BorderColorSnorm16Blue >> 8;
5004 state.BorderColorSnorm8Alpha = state.BorderColorSnorm16Alpha >> 8;
5005
5006 BORDER_COLOR_ATTR(ASSIGN, Float, color.f);
5007 #elif GEN_GEN == 4
5008 BORDER_COLOR_ATTR(ASSIGN, , color.f);
5009 #else
5010 BORDER_COLOR_ATTR(ASSIGN, Float, color.f);
5011 #endif
5012
5013 #undef ASSIGN
5014 #undef BORDER_COLOR_ATTR
5015
5016 GENX(SAMPLER_BORDER_COLOR_STATE_pack)(brw, sdc, &state);
5017 }
5018
5019 static uint32_t
5020 translate_wrap_mode(GLenum wrap, MAYBE_UNUSED bool using_nearest)
5021 {
5022 switch (wrap) {
5023 case GL_REPEAT:
5024 return TCM_WRAP;
5025 case GL_CLAMP:
5026 #if GEN_GEN >= 8
5027 /* GL_CLAMP is the weird mode where coordinates are clamped to
5028 * [0.0, 1.0], so linear filtering of coordinates outside of
5029 * [0.0, 1.0] give you half edge texel value and half border
5030 * color.
5031 *
5032 * Gen8+ supports this natively.
5033 */
5034 return TCM_HALF_BORDER;
5035 #else
5036 /* On Gen4-7.5, we clamp the coordinates in the fragment shader
5037 * and set clamp_border here, which gets the result desired.
5038 * We just use clamp(_to_edge) for nearest, because for nearest
5039 * clamping to 1.0 gives border color instead of the desired
5040 * edge texels.
5041 */
5042 if (using_nearest)
5043 return TCM_CLAMP;
5044 else
5045 return TCM_CLAMP_BORDER;
5046 #endif
5047 case GL_CLAMP_TO_EDGE:
5048 return TCM_CLAMP;
5049 case GL_CLAMP_TO_BORDER:
5050 return TCM_CLAMP_BORDER;
5051 case GL_MIRRORED_REPEAT:
5052 return TCM_MIRROR;
5053 case GL_MIRROR_CLAMP_TO_EDGE:
5054 return TCM_MIRROR_ONCE;
5055 default:
5056 return TCM_WRAP;
5057 }
5058 }
5059
5060 /**
5061 * Return true if the given wrap mode requires the border color to exist.
5062 */
5063 static bool
5064 wrap_mode_needs_border_color(unsigned wrap_mode)
5065 {
5066 #if GEN_GEN >= 8
5067 return wrap_mode == TCM_CLAMP_BORDER ||
5068 wrap_mode == TCM_HALF_BORDER;
5069 #else
5070 return wrap_mode == TCM_CLAMP_BORDER;
5071 #endif
5072 }
5073
5074 /**
5075 * Sets the sampler state for a single unit based off of the sampler key
5076 * entry.
5077 */
5078 static void
5079 genX(update_sampler_state)(struct brw_context *brw,
5080 GLenum target, bool tex_cube_map_seamless,
5081 GLfloat tex_unit_lod_bias,
5082 mesa_format format, GLenum base_format,
5083 const struct gl_texture_object *texObj,
5084 const struct gl_sampler_object *sampler,
5085 uint32_t *sampler_state)
5086 {
5087 struct GENX(SAMPLER_STATE) samp_st = { 0 };
5088
5089 /* Select min and mip filters. */
5090 switch (sampler->MinFilter) {
5091 case GL_NEAREST:
5092 samp_st.MinModeFilter = MAPFILTER_NEAREST;
5093 samp_st.MipModeFilter = MIPFILTER_NONE;
5094 break;
5095 case GL_LINEAR:
5096 samp_st.MinModeFilter = MAPFILTER_LINEAR;
5097 samp_st.MipModeFilter = MIPFILTER_NONE;
5098 break;
5099 case GL_NEAREST_MIPMAP_NEAREST:
5100 samp_st.MinModeFilter = MAPFILTER_NEAREST;
5101 samp_st.MipModeFilter = MIPFILTER_NEAREST;
5102 break;
5103 case GL_LINEAR_MIPMAP_NEAREST:
5104 samp_st.MinModeFilter = MAPFILTER_LINEAR;
5105 samp_st.MipModeFilter = MIPFILTER_NEAREST;
5106 break;
5107 case GL_NEAREST_MIPMAP_LINEAR:
5108 samp_st.MinModeFilter = MAPFILTER_NEAREST;
5109 samp_st.MipModeFilter = MIPFILTER_LINEAR;
5110 break;
5111 case GL_LINEAR_MIPMAP_LINEAR:
5112 samp_st.MinModeFilter = MAPFILTER_LINEAR;
5113 samp_st.MipModeFilter = MIPFILTER_LINEAR;
5114 break;
5115 default:
5116 unreachable("not reached");
5117 }
5118
5119 /* Select mag filter. */
5120 samp_st.MagModeFilter = sampler->MagFilter == GL_LINEAR ?
5121 MAPFILTER_LINEAR : MAPFILTER_NEAREST;
5122
5123 /* Enable anisotropic filtering if desired. */
5124 samp_st.MaximumAnisotropy = RATIO21;
5125
5126 if (sampler->MaxAnisotropy > 1.0f) {
5127 if (samp_st.MinModeFilter == MAPFILTER_LINEAR)
5128 samp_st.MinModeFilter = MAPFILTER_ANISOTROPIC;
5129 if (samp_st.MagModeFilter == MAPFILTER_LINEAR)
5130 samp_st.MagModeFilter = MAPFILTER_ANISOTROPIC;
5131
5132 if (sampler->MaxAnisotropy > 2.0f) {
5133 samp_st.MaximumAnisotropy =
5134 MIN2((sampler->MaxAnisotropy - 2) / 2, RATIO161);
5135 }
5136 }
5137
5138 /* Set address rounding bits if not using nearest filtering. */
5139 if (samp_st.MinModeFilter != MAPFILTER_NEAREST) {
5140 samp_st.UAddressMinFilterRoundingEnable = true;
5141 samp_st.VAddressMinFilterRoundingEnable = true;
5142 samp_st.RAddressMinFilterRoundingEnable = true;
5143 }
5144
5145 if (samp_st.MagModeFilter != MAPFILTER_NEAREST) {
5146 samp_st.UAddressMagFilterRoundingEnable = true;
5147 samp_st.VAddressMagFilterRoundingEnable = true;
5148 samp_st.RAddressMagFilterRoundingEnable = true;
5149 }
5150
5151 bool either_nearest =
5152 sampler->MinFilter == GL_NEAREST || sampler->MagFilter == GL_NEAREST;
5153 unsigned wrap_s = translate_wrap_mode(sampler->WrapS, either_nearest);
5154 unsigned wrap_t = translate_wrap_mode(sampler->WrapT, either_nearest);
5155 unsigned wrap_r = translate_wrap_mode(sampler->WrapR, either_nearest);
5156
5157 if (target == GL_TEXTURE_CUBE_MAP ||
5158 target == GL_TEXTURE_CUBE_MAP_ARRAY) {
5159 /* Cube maps must use the same wrap mode for all three coordinate
5160 * dimensions. Prior to Haswell, only CUBE and CLAMP are valid.
5161 *
5162 * Ivybridge and Baytrail seem to have problems with CUBE mode and
5163 * integer formats. Fall back to CLAMP for now.
5164 */
5165 if ((tex_cube_map_seamless || sampler->CubeMapSeamless) &&
5166 !(GEN_GEN == 7 && !GEN_IS_HASWELL && texObj->_IsIntegerFormat)) {
5167 wrap_s = TCM_CUBE;
5168 wrap_t = TCM_CUBE;
5169 wrap_r = TCM_CUBE;
5170 } else {
5171 wrap_s = TCM_CLAMP;
5172 wrap_t = TCM_CLAMP;
5173 wrap_r = TCM_CLAMP;
5174 }
5175 } else if (target == GL_TEXTURE_1D) {
5176 /* There's a bug in 1D texture sampling - it actually pays
5177 * attention to the wrap_t value, though it should not.
5178 * Override the wrap_t value here to GL_REPEAT to keep
5179 * any nonexistent border pixels from floating in.
5180 */
5181 wrap_t = TCM_WRAP;
5182 }
5183
5184 samp_st.TCXAddressControlMode = wrap_s;
5185 samp_st.TCYAddressControlMode = wrap_t;
5186 samp_st.TCZAddressControlMode = wrap_r;
5187
5188 samp_st.ShadowFunction =
5189 sampler->CompareMode == GL_COMPARE_R_TO_TEXTURE_ARB ?
5190 intel_translate_shadow_compare_func(sampler->CompareFunc) : 0;
5191
5192 #if GEN_GEN >= 7
5193 /* Set shadow function. */
5194 samp_st.AnisotropicAlgorithm =
5195 samp_st.MinModeFilter == MAPFILTER_ANISOTROPIC ?
5196 EWAApproximation : LEGACY;
5197 #endif
5198
5199 #if GEN_GEN >= 6
5200 samp_st.NonnormalizedCoordinateEnable = target == GL_TEXTURE_RECTANGLE;
5201 #endif
5202
5203 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
5204 samp_st.MinLOD = CLAMP(sampler->MinLod, 0, hw_max_lod);
5205 samp_st.MaxLOD = CLAMP(sampler->MaxLod, 0, hw_max_lod);
5206 samp_st.TextureLODBias =
5207 CLAMP(tex_unit_lod_bias + sampler->LodBias, -16, 15);
5208
5209 #if GEN_GEN == 6
5210 samp_st.BaseMipLevel =
5211 CLAMP(texObj->MinLevel + texObj->BaseLevel, 0, hw_max_lod);
5212 samp_st.MinandMagStateNotEqual =
5213 samp_st.MinModeFilter != samp_st.MagModeFilter;
5214 #endif
5215
5216 /* Upload the border color if necessary. If not, just point it at
5217 * offset 0 (the start of the batch) - the color should be ignored,
5218 * but that address won't fault in case something reads it anyway.
5219 */
5220 uint32_t border_color_offset = 0;
5221 if (wrap_mode_needs_border_color(wrap_s) ||
5222 wrap_mode_needs_border_color(wrap_t) ||
5223 wrap_mode_needs_border_color(wrap_r)) {
5224 genX(upload_default_color)(brw, sampler, format, base_format,
5225 texObj->_IsIntegerFormat,
5226 texObj->StencilSampling,
5227 &border_color_offset);
5228 }
5229 #if GEN_GEN < 6
5230 samp_st.BorderColorPointer =
5231 ro_bo(brw->batch.state.bo, border_color_offset);
5232 #else
5233 samp_st.BorderColorPointer = border_color_offset;
5234 #endif
5235
5236 #if GEN_GEN >= 8
5237 samp_st.LODPreClampMode = CLAMP_MODE_OGL;
5238 #else
5239 samp_st.LODPreClampEnable = true;
5240 #endif
5241
5242 GENX(SAMPLER_STATE_pack)(brw, sampler_state, &samp_st);
5243 }
5244
5245 static void
5246 update_sampler_state(struct brw_context *brw,
5247 int unit,
5248 uint32_t *sampler_state)
5249 {
5250 struct gl_context *ctx = &brw->ctx;
5251 const struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit];
5252 const struct gl_texture_object *texObj = texUnit->_Current;
5253 const struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
5254
5255 /* These don't use samplers at all. */
5256 if (texObj->Target == GL_TEXTURE_BUFFER)
5257 return;
5258
5259 struct gl_texture_image *firstImage = texObj->Image[0][texObj->BaseLevel];
5260 genX(update_sampler_state)(brw, texObj->Target,
5261 ctx->Texture.CubeMapSeamless,
5262 texUnit->LodBias,
5263 firstImage->TexFormat, firstImage->_BaseFormat,
5264 texObj, sampler,
5265 sampler_state);
5266 }
5267
5268 static void
5269 genX(upload_sampler_state_table)(struct brw_context *brw,
5270 struct gl_program *prog,
5271 struct brw_stage_state *stage_state)
5272 {
5273 struct gl_context *ctx = &brw->ctx;
5274 uint32_t sampler_count = stage_state->sampler_count;
5275
5276 GLbitfield SamplersUsed = prog->SamplersUsed;
5277
5278 if (sampler_count == 0)
5279 return;
5280
5281 /* SAMPLER_STATE is 4 DWords on all platforms. */
5282 const int dwords = GENX(SAMPLER_STATE_length);
5283 const int size_in_bytes = dwords * sizeof(uint32_t);
5284
5285 uint32_t *sampler_state = brw_state_batch(brw,
5286 sampler_count * size_in_bytes,
5287 32, &stage_state->sampler_offset);
5288 /* memset(sampler_state, 0, sampler_count * size_in_bytes); */
5289
5290 for (unsigned s = 0; s < sampler_count; s++) {
5291 if (SamplersUsed & (1 << s)) {
5292 const unsigned unit = prog->SamplerUnits[s];
5293 if (ctx->Texture.Unit[unit]._Current) {
5294 update_sampler_state(brw, unit, sampler_state);
5295 }
5296 }
5297
5298 sampler_state += dwords;
5299 }
5300
5301 if (GEN_GEN >= 7 && stage_state->stage != MESA_SHADER_COMPUTE) {
5302 /* Emit a 3DSTATE_SAMPLER_STATE_POINTERS_XS packet. */
5303 genX(emit_sampler_state_pointers_xs)(brw, stage_state);
5304 } else {
5305 /* Flag that the sampler state table pointer has changed; later atoms
5306 * will handle it.
5307 */
5308 brw->ctx.NewDriverState |= BRW_NEW_SAMPLER_STATE_TABLE;
5309 }
5310 }
5311
5312 static void
5313 genX(upload_fs_samplers)(struct brw_context *brw)
5314 {
5315 /* BRW_NEW_FRAGMENT_PROGRAM */
5316 struct gl_program *fs = brw->programs[MESA_SHADER_FRAGMENT];
5317 genX(upload_sampler_state_table)(brw, fs, &brw->wm.base);
5318 }
5319
5320 static const struct brw_tracked_state genX(fs_samplers) = {
5321 .dirty = {
5322 .mesa = _NEW_TEXTURE,
5323 .brw = BRW_NEW_BATCH |
5324 BRW_NEW_BLORP |
5325 BRW_NEW_FRAGMENT_PROGRAM,
5326 },
5327 .emit = genX(upload_fs_samplers),
5328 };
5329
5330 static void
5331 genX(upload_vs_samplers)(struct brw_context *brw)
5332 {
5333 /* BRW_NEW_VERTEX_PROGRAM */
5334 struct gl_program *vs = brw->programs[MESA_SHADER_VERTEX];
5335 genX(upload_sampler_state_table)(brw, vs, &brw->vs.base);
5336 }
5337
5338 static const struct brw_tracked_state genX(vs_samplers) = {
5339 .dirty = {
5340 .mesa = _NEW_TEXTURE,
5341 .brw = BRW_NEW_BATCH |
5342 BRW_NEW_BLORP |
5343 BRW_NEW_VERTEX_PROGRAM,
5344 },
5345 .emit = genX(upload_vs_samplers),
5346 };
5347
5348 #if GEN_GEN >= 6
5349 static void
5350 genX(upload_gs_samplers)(struct brw_context *brw)
5351 {
5352 /* BRW_NEW_GEOMETRY_PROGRAM */
5353 struct gl_program *gs = brw->programs[MESA_SHADER_GEOMETRY];
5354 if (!gs)
5355 return;
5356
5357 genX(upload_sampler_state_table)(brw, gs, &brw->gs.base);
5358 }
5359
5360
5361 static const struct brw_tracked_state genX(gs_samplers) = {
5362 .dirty = {
5363 .mesa = _NEW_TEXTURE,
5364 .brw = BRW_NEW_BATCH |
5365 BRW_NEW_BLORP |
5366 BRW_NEW_GEOMETRY_PROGRAM,
5367 },
5368 .emit = genX(upload_gs_samplers),
5369 };
5370 #endif
5371
5372 #if GEN_GEN >= 7
5373 static void
5374 genX(upload_tcs_samplers)(struct brw_context *brw)
5375 {
5376 /* BRW_NEW_TESS_PROGRAMS */
5377 struct gl_program *tcs = brw->programs[MESA_SHADER_TESS_CTRL];
5378 if (!tcs)
5379 return;
5380
5381 genX(upload_sampler_state_table)(brw, tcs, &brw->tcs.base);
5382 }
5383
5384 static const struct brw_tracked_state genX(tcs_samplers) = {
5385 .dirty = {
5386 .mesa = _NEW_TEXTURE,
5387 .brw = BRW_NEW_BATCH |
5388 BRW_NEW_BLORP |
5389 BRW_NEW_TESS_PROGRAMS,
5390 },
5391 .emit = genX(upload_tcs_samplers),
5392 };
5393 #endif
5394
5395 #if GEN_GEN >= 7
5396 static void
5397 genX(upload_tes_samplers)(struct brw_context *brw)
5398 {
5399 /* BRW_NEW_TESS_PROGRAMS */
5400 struct gl_program *tes = brw->programs[MESA_SHADER_TESS_EVAL];
5401 if (!tes)
5402 return;
5403
5404 genX(upload_sampler_state_table)(brw, tes, &brw->tes.base);
5405 }
5406
5407 static const struct brw_tracked_state genX(tes_samplers) = {
5408 .dirty = {
5409 .mesa = _NEW_TEXTURE,
5410 .brw = BRW_NEW_BATCH |
5411 BRW_NEW_BLORP |
5412 BRW_NEW_TESS_PROGRAMS,
5413 },
5414 .emit = genX(upload_tes_samplers),
5415 };
5416 #endif
5417
5418 #if GEN_GEN >= 7
5419 static void
5420 genX(upload_cs_samplers)(struct brw_context *brw)
5421 {
5422 /* BRW_NEW_COMPUTE_PROGRAM */
5423 struct gl_program *cs = brw->programs[MESA_SHADER_COMPUTE];
5424 if (!cs)
5425 return;
5426
5427 genX(upload_sampler_state_table)(brw, cs, &brw->cs.base);
5428 }
5429
5430 const struct brw_tracked_state genX(cs_samplers) = {
5431 .dirty = {
5432 .mesa = _NEW_TEXTURE,
5433 .brw = BRW_NEW_BATCH |
5434 BRW_NEW_BLORP |
5435 BRW_NEW_COMPUTE_PROGRAM,
5436 },
5437 .emit = genX(upload_cs_samplers),
5438 };
5439 #endif
5440
5441 /* ---------------------------------------------------------------------- */
5442
5443 #if GEN_GEN <= 5
5444
5445 static void genX(upload_blend_constant_color)(struct brw_context *brw)
5446 {
5447 struct gl_context *ctx = &brw->ctx;
5448
5449 brw_batch_emit(brw, GENX(3DSTATE_CONSTANT_COLOR), blend_cc) {
5450 blend_cc.BlendConstantColorRed = ctx->Color.BlendColorUnclamped[0];
5451 blend_cc.BlendConstantColorGreen = ctx->Color.BlendColorUnclamped[1];
5452 blend_cc.BlendConstantColorBlue = ctx->Color.BlendColorUnclamped[2];
5453 blend_cc.BlendConstantColorAlpha = ctx->Color.BlendColorUnclamped[3];
5454 }
5455 }
5456
5457 static const struct brw_tracked_state genX(blend_constant_color) = {
5458 .dirty = {
5459 .mesa = _NEW_COLOR,
5460 .brw = BRW_NEW_CONTEXT |
5461 BRW_NEW_BLORP,
5462 },
5463 .emit = genX(upload_blend_constant_color)
5464 };
5465 #endif
5466
5467 /* ---------------------------------------------------------------------- */
5468
5469 void
5470 genX(init_atoms)(struct brw_context *brw)
5471 {
5472 #if GEN_GEN < 6
5473 static const struct brw_tracked_state *render_atoms[] =
5474 {
5475 /* Once all the programs are done, we know how large urb entry
5476 * sizes need to be and can decide if we need to change the urb
5477 * layout.
5478 */
5479 &brw_curbe_offsets,
5480 &brw_recalculate_urb_fence,
5481
5482 &genX(cc_vp),
5483 &genX(color_calc_state),
5484
5485 /* Surface state setup. Must come before the VS/WM unit. The binding
5486 * table upload must be last.
5487 */
5488 &brw_vs_pull_constants,
5489 &brw_wm_pull_constants,
5490 &brw_renderbuffer_surfaces,
5491 &brw_renderbuffer_read_surfaces,
5492 &brw_texture_surfaces,
5493 &brw_vs_binding_table,
5494 &brw_wm_binding_table,
5495
5496 &genX(fs_samplers),
5497 &genX(vs_samplers),
5498
5499 /* These set up state for brw_psp_urb_cbs */
5500 &genX(wm_state),
5501 &genX(sf_clip_viewport),
5502 &genX(sf_state),
5503 &genX(vs_state), /* always required, enabled or not */
5504 &genX(clip_state),
5505 &genX(gs_state),
5506
5507 /* Command packets:
5508 */
5509 &brw_binding_table_pointers,
5510 &genX(blend_constant_color),
5511
5512 &brw_depthbuffer,
5513
5514 &genX(polygon_stipple),
5515 &genX(polygon_stipple_offset),
5516
5517 &genX(line_stipple),
5518
5519 &brw_psp_urb_cbs,
5520
5521 &genX(drawing_rect),
5522 &brw_indices, /* must come before brw_vertices */
5523 &genX(index_buffer),
5524 &genX(vertices),
5525
5526 &brw_constant_buffer
5527 };
5528 #elif GEN_GEN == 6
5529 static const struct brw_tracked_state *render_atoms[] =
5530 {
5531 &genX(sf_clip_viewport),
5532
5533 /* Command packets: */
5534
5535 &genX(cc_vp),
5536
5537 &gen6_urb,
5538 &genX(blend_state), /* must do before cc unit */
5539 &genX(color_calc_state), /* must do before cc unit */
5540 &genX(depth_stencil_state), /* must do before cc unit */
5541
5542 &genX(vs_push_constants), /* Before vs_state */
5543 &genX(gs_push_constants), /* Before gs_state */
5544 &genX(wm_push_constants), /* Before wm_state */
5545
5546 /* Surface state setup. Must come before the VS/WM unit. The binding
5547 * table upload must be last.
5548 */
5549 &brw_vs_pull_constants,
5550 &brw_vs_ubo_surfaces,
5551 &brw_gs_pull_constants,
5552 &brw_gs_ubo_surfaces,
5553 &brw_wm_pull_constants,
5554 &brw_wm_ubo_surfaces,
5555 &gen6_renderbuffer_surfaces,
5556 &brw_renderbuffer_read_surfaces,
5557 &brw_texture_surfaces,
5558 &gen6_sol_surface,
5559 &brw_vs_binding_table,
5560 &gen6_gs_binding_table,
5561 &brw_wm_binding_table,
5562
5563 &genX(fs_samplers),
5564 &genX(vs_samplers),
5565 &genX(gs_samplers),
5566 &gen6_sampler_state,
5567 &genX(multisample_state),
5568
5569 &genX(vs_state),
5570 &genX(gs_state),
5571 &genX(clip_state),
5572 &genX(sf_state),
5573 &genX(wm_state),
5574
5575 &genX(scissor_state),
5576
5577 &gen6_binding_table_pointers,
5578
5579 &brw_depthbuffer,
5580
5581 &genX(polygon_stipple),
5582 &genX(polygon_stipple_offset),
5583
5584 &genX(line_stipple),
5585
5586 &genX(drawing_rect),
5587
5588 &brw_indices, /* must come before brw_vertices */
5589 &genX(index_buffer),
5590 &genX(vertices),
5591 };
5592 #elif GEN_GEN == 7
5593 static const struct brw_tracked_state *render_atoms[] =
5594 {
5595 /* Command packets: */
5596
5597 &genX(cc_vp),
5598 &genX(sf_clip_viewport),
5599
5600 &gen7_l3_state,
5601 &gen7_push_constant_space,
5602 &gen7_urb,
5603 &genX(blend_state), /* must do before cc unit */
5604 &genX(color_calc_state), /* must do before cc unit */
5605 &genX(depth_stencil_state), /* must do before cc unit */
5606
5607 &brw_vs_image_surfaces, /* Before vs push/pull constants and binding table */
5608 &brw_tcs_image_surfaces, /* Before tcs push/pull constants and binding table */
5609 &brw_tes_image_surfaces, /* Before tes push/pull constants and binding table */
5610 &brw_gs_image_surfaces, /* Before gs push/pull constants and binding table */
5611 &brw_wm_image_surfaces, /* Before wm push/pull constants and binding table */
5612
5613 &genX(vs_push_constants), /* Before vs_state */
5614 &genX(tcs_push_constants),
5615 &genX(tes_push_constants),
5616 &genX(gs_push_constants), /* Before gs_state */
5617 &genX(wm_push_constants), /* Before wm_surfaces and constant_buffer */
5618
5619 /* Surface state setup. Must come before the VS/WM unit. The binding
5620 * table upload must be last.
5621 */
5622 &brw_vs_pull_constants,
5623 &brw_vs_ubo_surfaces,
5624 &brw_tcs_pull_constants,
5625 &brw_tcs_ubo_surfaces,
5626 &brw_tes_pull_constants,
5627 &brw_tes_ubo_surfaces,
5628 &brw_gs_pull_constants,
5629 &brw_gs_ubo_surfaces,
5630 &brw_wm_pull_constants,
5631 &brw_wm_ubo_surfaces,
5632 &gen6_renderbuffer_surfaces,
5633 &brw_renderbuffer_read_surfaces,
5634 &brw_texture_surfaces,
5635
5636 &genX(push_constant_packets),
5637
5638 &brw_vs_binding_table,
5639 &brw_tcs_binding_table,
5640 &brw_tes_binding_table,
5641 &brw_gs_binding_table,
5642 &brw_wm_binding_table,
5643
5644 &genX(fs_samplers),
5645 &genX(vs_samplers),
5646 &genX(tcs_samplers),
5647 &genX(tes_samplers),
5648 &genX(gs_samplers),
5649 &genX(multisample_state),
5650
5651 &genX(vs_state),
5652 &genX(hs_state),
5653 &genX(te_state),
5654 &genX(ds_state),
5655 &genX(gs_state),
5656 &genX(sol_state),
5657 &genX(clip_state),
5658 &genX(sbe_state),
5659 &genX(sf_state),
5660 &genX(wm_state),
5661 &genX(ps_state),
5662
5663 &genX(scissor_state),
5664
5665 &brw_depthbuffer,
5666
5667 &genX(polygon_stipple),
5668 &genX(polygon_stipple_offset),
5669
5670 &genX(line_stipple),
5671
5672 &genX(drawing_rect),
5673
5674 &brw_indices, /* must come before brw_vertices */
5675 &genX(index_buffer),
5676 &genX(vertices),
5677
5678 #if GEN_IS_HASWELL
5679 &genX(cut_index),
5680 #endif
5681 };
5682 #elif GEN_GEN >= 8
5683 static const struct brw_tracked_state *render_atoms[] =
5684 {
5685 &genX(cc_vp),
5686 &genX(sf_clip_viewport),
5687
5688 &gen7_l3_state,
5689 &gen7_push_constant_space,
5690 &gen7_urb,
5691 &genX(blend_state),
5692 &genX(color_calc_state),
5693
5694 &brw_vs_image_surfaces, /* Before vs push/pull constants and binding table */
5695 &brw_tcs_image_surfaces, /* Before tcs push/pull constants and binding table */
5696 &brw_tes_image_surfaces, /* Before tes push/pull constants and binding table */
5697 &brw_gs_image_surfaces, /* Before gs push/pull constants and binding table */
5698 &brw_wm_image_surfaces, /* Before wm push/pull constants and binding table */
5699
5700 &genX(vs_push_constants), /* Before vs_state */
5701 &genX(tcs_push_constants),
5702 &genX(tes_push_constants),
5703 &genX(gs_push_constants), /* Before gs_state */
5704 &genX(wm_push_constants), /* Before wm_surfaces and constant_buffer */
5705
5706 /* Surface state setup. Must come before the VS/WM unit. The binding
5707 * table upload must be last.
5708 */
5709 &brw_vs_pull_constants,
5710 &brw_vs_ubo_surfaces,
5711 &brw_tcs_pull_constants,
5712 &brw_tcs_ubo_surfaces,
5713 &brw_tes_pull_constants,
5714 &brw_tes_ubo_surfaces,
5715 &brw_gs_pull_constants,
5716 &brw_gs_ubo_surfaces,
5717 &brw_wm_pull_constants,
5718 &brw_wm_ubo_surfaces,
5719 &gen6_renderbuffer_surfaces,
5720 &brw_renderbuffer_read_surfaces,
5721 &brw_texture_surfaces,
5722
5723 &genX(push_constant_packets),
5724
5725 &brw_vs_binding_table,
5726 &brw_tcs_binding_table,
5727 &brw_tes_binding_table,
5728 &brw_gs_binding_table,
5729 &brw_wm_binding_table,
5730
5731 &genX(fs_samplers),
5732 &genX(vs_samplers),
5733 &genX(tcs_samplers),
5734 &genX(tes_samplers),
5735 &genX(gs_samplers),
5736 &genX(multisample_state),
5737
5738 &genX(vs_state),
5739 &genX(hs_state),
5740 &genX(te_state),
5741 &genX(ds_state),
5742 &genX(gs_state),
5743 &genX(sol_state),
5744 &genX(clip_state),
5745 &genX(raster_state),
5746 &genX(sbe_state),
5747 &genX(sf_state),
5748 &genX(ps_blend),
5749 &genX(ps_extra),
5750 &genX(ps_state),
5751 &genX(depth_stencil_state),
5752 &genX(wm_state),
5753
5754 &genX(scissor_state),
5755
5756 &brw_depthbuffer,
5757
5758 &genX(polygon_stipple),
5759 &genX(polygon_stipple_offset),
5760
5761 &genX(line_stipple),
5762
5763 &genX(drawing_rect),
5764
5765 &genX(vf_topology),
5766
5767 &brw_indices,
5768 &genX(index_buffer),
5769 &genX(vertices),
5770
5771 &genX(cut_index),
5772 &gen8_pma_fix,
5773 };
5774 #endif
5775
5776 STATIC_ASSERT(ARRAY_SIZE(render_atoms) <= ARRAY_SIZE(brw->render_atoms));
5777 brw_copy_pipeline_atoms(brw, BRW_RENDER_PIPELINE,
5778 render_atoms, ARRAY_SIZE(render_atoms));
5779
5780 #if GEN_GEN >= 7
5781 static const struct brw_tracked_state *compute_atoms[] =
5782 {
5783 &gen7_l3_state,
5784 &brw_cs_image_surfaces,
5785 &genX(cs_push_constants),
5786 &genX(cs_pull_constants),
5787 &brw_cs_ubo_surfaces,
5788 &brw_cs_texture_surfaces,
5789 &brw_cs_work_groups_surface,
5790 &genX(cs_samplers),
5791 &genX(cs_state),
5792 };
5793
5794 STATIC_ASSERT(ARRAY_SIZE(compute_atoms) <= ARRAY_SIZE(brw->compute_atoms));
5795 brw_copy_pipeline_atoms(brw, BRW_COMPUTE_PIPELINE,
5796 compute_atoms, ARRAY_SIZE(compute_atoms));
5797
5798 brw->vtbl.emit_mi_report_perf_count = genX(emit_mi_report_perf_count);
5799 #endif
5800 }