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