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