iris: fix GS dispatch mode
[mesa.git] / src / gallium / drivers / iris / iris_state.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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #include <stdio.h>
24 #include <errno.h>
25
26 #if HAVE_VALGRIND
27 #include <valgrind.h>
28 #include <memcheck.h>
29 #define VG(x) x
30 #ifndef NDEBUG
31 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
32 #endif
33 #else
34 #define VG(x)
35 #endif
36
37 #include "pipe/p_defines.h"
38 #include "pipe/p_state.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_screen.h"
41 #include "util/u_inlines.h"
42 #include "util/u_format.h"
43 #include "util/u_framebuffer.h"
44 #include "util/u_transfer.h"
45 #include "util/u_upload_mgr.h"
46 #include "i915_drm.h"
47 #include "nir.h"
48 #include "intel/compiler/brw_compiler.h"
49 #include "intel/common/gen_l3_config.h"
50 #include "intel/common/gen_sample_positions.h"
51 #include "iris_batch.h"
52 #include "iris_context.h"
53 #include "iris_pipe.h"
54 #include "iris_resource.h"
55
56 #define __gen_address_type struct iris_address
57 #define __gen_user_data struct iris_batch
58
59 #define ARRAY_BYTES(x) (sizeof(uint32_t) * ARRAY_SIZE(x))
60
61 static uint64_t
62 __gen_combine_address(struct iris_batch *batch, void *location,
63 struct iris_address addr, uint32_t delta)
64 {
65 uint64_t result = addr.offset + delta;
66
67 if (addr.bo) {
68 iris_use_pinned_bo(batch, addr.bo, addr.write);
69 /* Assume this is a general address, not relative to a base. */
70 result += addr.bo->gtt_offset;
71 }
72
73 return result;
74 }
75
76 #define __genxml_cmd_length(cmd) cmd ## _length
77 #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
78 #define __genxml_cmd_header(cmd) cmd ## _header
79 #define __genxml_cmd_pack(cmd) cmd ## _pack
80
81 #define _iris_pack_command(batch, cmd, dst, name) \
82 for (struct cmd name = { __genxml_cmd_header(cmd) }, \
83 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
84 ({ __genxml_cmd_pack(cmd)(batch, (void *)_dst, &name); \
85 _dst = NULL; \
86 }))
87
88 #define iris_pack_command(cmd, dst, name) \
89 _iris_pack_command(NULL, cmd, dst, name)
90
91 #define iris_pack_state(cmd, dst, name) \
92 for (struct cmd name = {}, \
93 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
94 __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
95 _dst = NULL)
96
97 #define iris_emit_cmd(batch, cmd, name) \
98 _iris_pack_command(batch, cmd, iris_get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
99
100 #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
101 do { \
102 uint32_t *dw = iris_get_command_space(batch, 4 * num_dwords); \
103 for (uint32_t i = 0; i < num_dwords; i++) \
104 dw[i] = (dwords0)[i] | (dwords1)[i]; \
105 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
106 } while (0)
107
108 #include "genxml/genX_pack.h"
109 #include "genxml/gen_macros.h"
110 #include "genxml/genX_bits.h"
111
112 #define MOCS_WB (2 << 1)
113
114 UNUSED static void pipe_asserts()
115 {
116 #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
117
118 /* pipe_logicop happens to match the hardware. */
119 PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
120 PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
121 PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
122 PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
123 PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
124 PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
125 PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
126 PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
127 PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
128 PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
129 PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
130 PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
131 PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
132 PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
133 PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
134 PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
135
136 /* pipe_blend_func happens to match the hardware. */
137 PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
138 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
139 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
140 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
141 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
142 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
143 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
144 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
145 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
146 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
147 PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
148 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
149 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
150 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
151 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
152 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
153 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
154 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
155 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
156
157 /* pipe_blend_func happens to match the hardware. */
158 PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
159 PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
160 PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
161 PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
162 PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
163
164 /* pipe_stencil_op happens to match the hardware. */
165 PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
166 PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
167 PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
168 PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
169 PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
170 PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
171 PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
172 PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
173
174 /* pipe_sprite_coord_mode happens to match 3DSTATE_SBE */
175 PIPE_ASSERT(PIPE_SPRITE_COORD_UPPER_LEFT == UPPERLEFT);
176 PIPE_ASSERT(PIPE_SPRITE_COORD_LOWER_LEFT == LOWERLEFT);
177 #undef PIPE_ASSERT
178 }
179
180 static unsigned
181 translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
182 {
183 static const unsigned map[] = {
184 [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
185 [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
186 [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
187 [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
188 [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
189 [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
190 [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
191 [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
192 [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
193 [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
194 [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
195 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
196 [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
197 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
198 [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
199 };
200
201 return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
202 }
203
204 static unsigned
205 translate_compare_func(enum pipe_compare_func pipe_func)
206 {
207 static const unsigned map[] = {
208 [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
209 [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
210 [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
211 [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
212 [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
213 [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
214 [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
215 [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
216 };
217 return map[pipe_func];
218 }
219
220 static unsigned
221 translate_shadow_func(enum pipe_compare_func pipe_func)
222 {
223 /* Gallium specifies the result of shadow comparisons as:
224 *
225 * 1 if ref <op> texel,
226 * 0 otherwise.
227 *
228 * The hardware does:
229 *
230 * 0 if texel <op> ref,
231 * 1 otherwise.
232 *
233 * So we need to flip the operator and also negate.
234 */
235 static const unsigned map[] = {
236 [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
237 [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
238 [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
239 [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
240 [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
241 [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
242 [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
243 [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
244 };
245 return map[pipe_func];
246 }
247
248 static unsigned
249 translate_cull_mode(unsigned pipe_face)
250 {
251 static const unsigned map[4] = {
252 [PIPE_FACE_NONE] = CULLMODE_NONE,
253 [PIPE_FACE_FRONT] = CULLMODE_FRONT,
254 [PIPE_FACE_BACK] = CULLMODE_BACK,
255 [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
256 };
257 return map[pipe_face];
258 }
259
260 static unsigned
261 translate_fill_mode(unsigned pipe_polymode)
262 {
263 static const unsigned map[4] = {
264 [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
265 [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
266 [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
267 [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
268 };
269 return map[pipe_polymode];
270 }
271
272 static struct iris_address
273 ro_bo(struct iris_bo *bo, uint64_t offset)
274 {
275 /* Not for CSOs! */
276 return (struct iris_address) { .bo = bo, .offset = offset };
277 }
278
279 static uint32_t *
280 stream_state(struct iris_batch *batch,
281 struct u_upload_mgr *uploader,
282 struct pipe_resource **out_res,
283 unsigned size,
284 unsigned alignment,
285 uint32_t *out_offset)
286 {
287 void *ptr = NULL;
288
289 u_upload_alloc(uploader, 0, size, alignment, out_offset, out_res, &ptr);
290
291 struct iris_bo *bo = iris_resource_bo(*out_res);
292 iris_use_pinned_bo(batch, bo, false);
293
294 *out_offset += iris_bo_offset_from_base_address(bo);
295
296 return ptr;
297 }
298
299 static uint32_t
300 emit_state(struct iris_batch *batch,
301 struct u_upload_mgr *uploader,
302 struct pipe_resource **out_res,
303 const void *data,
304 unsigned size,
305 unsigned alignment)
306 {
307 unsigned offset = 0;
308 uint32_t *map =
309 stream_state(batch, uploader, out_res, size, alignment, &offset);
310
311 if (map)
312 memcpy(map, data, size);
313
314 return offset;
315 }
316
317 #define cso_changed(x) (!old_cso || (old_cso->x != new_cso->x))
318 #define cso_changed_memcmp(x) \
319 (!old_cso || memcmp(old_cso->x, new_cso->x, sizeof(old_cso->x)) != 0)
320
321 static void
322 iris_init_render_context(struct iris_screen *screen,
323 struct iris_batch *batch,
324 struct iris_vtable *vtbl,
325 struct pipe_debug_callback *dbg)
326 {
327 iris_init_batch(batch, screen, vtbl, dbg, I915_EXEC_RENDER);
328
329 /* XXX: PIPE_CONTROLs */
330
331 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
332 #if 0
333 // XXX: MOCS is stupid for this.
334 sba.GeneralStateMemoryObjectControlState = MOCS_WB;
335 sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
336 sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
337 sba.DynamicStateMemoryObjectControlState = MOCS_WB;
338 sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
339 sba.InstructionMemoryObjectControlState = MOCS_WB;
340 sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
341 #endif
342
343 sba.GeneralStateBaseAddressModifyEnable = true;
344 sba.SurfaceStateBaseAddressModifyEnable = true;
345 sba.DynamicStateBaseAddressModifyEnable = true;
346 sba.IndirectObjectBaseAddressModifyEnable = true;
347 sba.InstructionBaseAddressModifyEnable = true;
348 sba.GeneralStateBufferSizeModifyEnable = true;
349 sba.DynamicStateBufferSizeModifyEnable = true;
350 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
351 sba.IndirectObjectBufferSizeModifyEnable = true;
352 sba.InstructionBuffersizeModifyEnable = true;
353
354 sba.InstructionBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SHADER_START);
355 sba.SurfaceStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_SURFACE_START);
356 sba.DynamicStateBaseAddress = ro_bo(NULL, IRIS_MEMZONE_DYNAMIC_START);
357
358 sba.GeneralStateBufferSize = 0xfffff;
359 sba.IndirectObjectBufferSize = 0xfffff;
360 sba.InstructionBufferSize = 0xfffff;
361 sba.DynamicStateBufferSize = 0xfffff;
362 }
363
364 iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
365 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
366 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
367 }
368 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
369 GEN_SAMPLE_POS_1X(pat._1xSample);
370 GEN_SAMPLE_POS_2X(pat._2xSample);
371 GEN_SAMPLE_POS_4X(pat._4xSample);
372 GEN_SAMPLE_POS_8X(pat._8xSample);
373 GEN_SAMPLE_POS_16X(pat._16xSample);
374 }
375 iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
376 iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
377 iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
378 /* XXX: may need to set an offset for origin-UL framebuffers */
379 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
380
381 /* Just assign a static partitioning. */
382 for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
383 iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
384 alloc._3DCommandSubOpcode = 18 + i;
385 alloc.ConstantBufferOffset = 6 * i;
386 alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
387 }
388 }
389 }
390
391 static void
392 iris_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)
393 {
394 }
395
396 static void
397 iris_set_blend_color(struct pipe_context *ctx,
398 const struct pipe_blend_color *state)
399 {
400 struct iris_context *ice = (struct iris_context *) ctx;
401
402 memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
403 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
404 }
405
406 struct iris_blend_state {
407 /** Partial 3DSTATE_PS_BLEND */
408 uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
409
410 /** Partial BLEND_STATE */
411 uint32_t blend_state[GENX(BLEND_STATE_length) +
412 BRW_MAX_DRAW_BUFFERS * GENX(BLEND_STATE_ENTRY_length)];
413
414 bool alpha_to_coverage; /* for shader key */
415 };
416
417 static void *
418 iris_create_blend_state(struct pipe_context *ctx,
419 const struct pipe_blend_state *state)
420 {
421 struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
422 uint32_t *blend_state = cso->blend_state;
423
424 cso->alpha_to_coverage = state->alpha_to_coverage;
425
426 iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
427 /* pb.HasWriteableRT is filled in at draw time. */
428 /* pb.AlphaTestEnable is filled in at draw time. */
429 pb.AlphaToCoverageEnable = state->alpha_to_coverage;
430 pb.IndependentAlphaBlendEnable = state->independent_blend_enable;
431
432 pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
433
434 pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
435 pb.SourceAlphaBlendFactor = state->rt[0].alpha_func;
436 pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
437 pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
438 }
439
440 iris_pack_state(GENX(BLEND_STATE), blend_state, bs) {
441 bs.AlphaToCoverageEnable = state->alpha_to_coverage;
442 bs.IndependentAlphaBlendEnable = state->independent_blend_enable;
443 bs.AlphaToOneEnable = state->alpha_to_one;
444 bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
445 bs.ColorDitherEnable = state->dither;
446 /* bl.AlphaTestEnable and bs.AlphaTestFunction are filled in later. */
447 }
448
449 blend_state += GENX(BLEND_STATE_length);
450
451 for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
452 iris_pack_state(GENX(BLEND_STATE_ENTRY), blend_state, be) {
453 be.LogicOpEnable = state->logicop_enable;
454 be.LogicOpFunction = state->logicop_func;
455
456 be.PreBlendSourceOnlyClampEnable = false;
457 be.ColorClampRange = COLORCLAMP_RTFORMAT;
458 be.PreBlendColorClampEnable = true;
459 be.PostBlendColorClampEnable = true;
460
461 be.ColorBufferBlendEnable = state->rt[i].blend_enable;
462
463 be.ColorBlendFunction = state->rt[i].rgb_func;
464 be.AlphaBlendFunction = state->rt[i].alpha_func;
465 be.SourceBlendFactor = state->rt[i].rgb_src_factor;
466 be.SourceAlphaBlendFactor = state->rt[i].alpha_func;
467 be.DestinationBlendFactor = state->rt[i].rgb_dst_factor;
468 be.DestinationAlphaBlendFactor = state->rt[i].alpha_dst_factor;
469
470 be.WriteDisableRed = !(state->rt[i].colormask & PIPE_MASK_R);
471 be.WriteDisableGreen = !(state->rt[i].colormask & PIPE_MASK_G);
472 be.WriteDisableBlue = !(state->rt[i].colormask & PIPE_MASK_B);
473 be.WriteDisableAlpha = !(state->rt[i].colormask & PIPE_MASK_A);
474 }
475 blend_state += GENX(BLEND_STATE_ENTRY_length);
476 }
477
478 return cso;
479 }
480
481 static void
482 iris_bind_blend_state(struct pipe_context *ctx, void *state)
483 {
484 struct iris_context *ice = (struct iris_context *) ctx;
485 ice->state.cso_blend = state;
486 ice->state.dirty |= IRIS_DIRTY_PS_BLEND;
487 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
488 }
489
490 struct iris_depth_stencil_alpha_state {
491 /** Partial 3DSTATE_WM_DEPTH_STENCIL */
492 uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
493
494 /** Complete CC_VIEWPORT */
495 uint32_t cc_vp[GENX(CC_VIEWPORT_length)];
496
497 /** Outbound to BLEND_STATE, 3DSTATE_PS_BLEND, COLOR_CALC_STATE */
498 struct pipe_alpha_state alpha;
499 };
500
501 static void *
502 iris_create_zsa_state(struct pipe_context *ctx,
503 const struct pipe_depth_stencil_alpha_state *state)
504 {
505 struct iris_depth_stencil_alpha_state *cso =
506 malloc(sizeof(struct iris_depth_stencil_alpha_state));
507
508 cso->alpha = state->alpha;
509
510 bool two_sided_stencil = state->stencil[1].enabled;
511
512 /* The state tracker needs to optimize away EQUAL writes for us. */
513 assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
514
515 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
516 wmds.StencilFailOp = state->stencil[0].fail_op;
517 wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
518 wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
519 wmds.StencilTestFunction =
520 translate_compare_func(state->stencil[0].func);
521 wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
522 wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
523 wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
524 wmds.BackfaceStencilTestFunction =
525 translate_compare_func(state->stencil[1].func);
526 wmds.DepthTestFunction = translate_compare_func(state->depth.func);
527 wmds.DoubleSidedStencilEnable = two_sided_stencil;
528 wmds.StencilTestEnable = state->stencil[0].enabled;
529 wmds.StencilBufferWriteEnable =
530 state->stencil[0].writemask != 0 ||
531 (two_sided_stencil && state->stencil[1].writemask != 0);
532 wmds.DepthTestEnable = state->depth.enabled;
533 wmds.DepthBufferWriteEnable = state->depth.writemask;
534 wmds.StencilTestMask = state->stencil[0].valuemask;
535 wmds.StencilWriteMask = state->stencil[0].writemask;
536 wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
537 wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
538 /* wmds.[Backface]StencilReferenceValue are merged later */
539 }
540
541 iris_pack_state(GENX(CC_VIEWPORT), cso->cc_vp, ccvp) {
542 ccvp.MinimumDepth = state->depth.bounds_min;
543 ccvp.MaximumDepth = state->depth.bounds_max;
544 }
545
546 return cso;
547 }
548
549 static void
550 iris_bind_zsa_state(struct pipe_context *ctx, void *state)
551 {
552 struct iris_context *ice = (struct iris_context *) ctx;
553 struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
554 struct iris_depth_stencil_alpha_state *new_cso = state;
555
556 if (new_cso) {
557 if (cso_changed(alpha.ref_value))
558 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
559
560 if (cso_changed(alpha.enabled))
561 ice->state.dirty |= IRIS_DIRTY_PS_BLEND | IRIS_DIRTY_BLEND_STATE;
562 }
563
564 ice->state.cso_zsa = new_cso;
565 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
566 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
567 }
568
569 struct iris_rasterizer_state {
570 uint32_t sf[GENX(3DSTATE_SF_length)];
571 uint32_t clip[GENX(3DSTATE_CLIP_length)];
572 uint32_t raster[GENX(3DSTATE_RASTER_length)];
573 uint32_t wm[GENX(3DSTATE_WM_length)];
574 uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
575
576 bool flatshade; /* for shader state */
577 bool clamp_fragment_color; /* for shader state */
578 bool light_twoside; /* for shader state */
579 bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
580 bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
581 bool line_stipple_enable;
582 bool poly_stipple_enable;
583 enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
584 uint16_t sprite_coord_enable;
585 };
586
587 static void *
588 iris_create_rasterizer_state(struct pipe_context *ctx,
589 const struct pipe_rasterizer_state *state)
590 {
591 struct iris_rasterizer_state *cso =
592 malloc(sizeof(struct iris_rasterizer_state));
593
594 #if 0
595 point_quad_rasterization -> SBE?
596
597 not necessary?
598 {
599 poly_smooth
600 force_persample_interp - ?
601 bottom_edge_rule
602
603 offset_units_unscaled - cap not exposed
604 }
605 #endif
606
607 cso->flatshade = state->flatshade;
608 cso->clamp_fragment_color = state->clamp_fragment_color;
609 cso->light_twoside = state->light_twoside;
610 cso->rasterizer_discard = state->rasterizer_discard;
611 cso->half_pixel_center = state->half_pixel_center;
612 cso->sprite_coord_mode = state->sprite_coord_mode;
613 cso->sprite_coord_enable = state->sprite_coord_enable;
614 cso->line_stipple_enable = state->line_stipple_enable;
615 cso->poly_stipple_enable = state->poly_stipple_enable;
616
617 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
618 sf.StatisticsEnable = true;
619 sf.ViewportTransformEnable = true;
620 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
621 sf.LineEndCapAntialiasingRegionWidth =
622 state->line_smooth ? _10pixels : _05pixels;
623 sf.LastPixelEnable = state->line_last_pixel;
624 sf.LineWidth = state->line_width;
625 sf.SmoothPointEnable = state->point_smooth;
626 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
627 sf.PointWidth = state->point_size;
628
629 if (state->flatshade_first) {
630 sf.TriangleFanProvokingVertexSelect = 1;
631 } else {
632 sf.TriangleStripListProvokingVertexSelect = 2;
633 sf.TriangleFanProvokingVertexSelect = 2;
634 sf.LineStripListProvokingVertexSelect = 1;
635 }
636 }
637
638 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
639 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
640 rr.CullMode = translate_cull_mode(state->cull_face);
641 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
642 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
643 rr.DXMultisampleRasterizationEnable = state->multisample;
644 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
645 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
646 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
647 rr.GlobalDepthOffsetConstant = state->offset_units * 2;
648 rr.GlobalDepthOffsetScale = state->offset_scale;
649 rr.GlobalDepthOffsetClamp = state->offset_clamp;
650 rr.SmoothPointEnable = state->point_smooth;
651 rr.AntialiasingEnable = state->line_smooth;
652 rr.ScissorRectangleEnable = state->scissor;
653 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
654 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
655 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
656 }
657
658 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
659 /* cl.NonPerspectiveBarycentricEnable is filled in at draw time from
660 * the FS program; cl.ForceZeroRTAIndexEnable is filled in from the FB.
661 */
662 cl.StatisticsEnable = true;
663 cl.EarlyCullEnable = true;
664 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
665 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
666 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
667 cl.GuardbandClipTestEnable = true;
668 cl.ClipMode = CLIPMODE_NORMAL;
669 cl.ClipEnable = true;
670 cl.ViewportXYClipTestEnable = state->point_tri_clip;
671 cl.MinimumPointWidth = 0.125;
672 cl.MaximumPointWidth = 255.875;
673
674 if (state->flatshade_first) {
675 cl.TriangleFanProvokingVertexSelect = 1;
676 } else {
677 cl.TriangleStripListProvokingVertexSelect = 2;
678 cl.TriangleFanProvokingVertexSelect = 2;
679 cl.LineStripListProvokingVertexSelect = 1;
680 }
681 }
682
683 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
684 /* wm.BarycentricInterpolationMode and wm.EarlyDepthStencilControl are
685 * filled in at draw time from the FS program.
686 */
687 wm.LineAntialiasingRegionWidth = _10pixels;
688 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
689 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
690 wm.StatisticsEnable = true;
691 wm.LineStippleEnable = state->line_stipple_enable;
692 wm.PolygonStippleEnable = state->poly_stipple_enable;
693 }
694
695 /* Remap from 0..255 back to 1..256 */
696 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
697
698 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
699 line.LineStipplePattern = state->line_stipple_pattern;
700 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
701 line.LineStippleRepeatCount = line_stipple_factor;
702 }
703
704 return cso;
705 }
706
707 static void
708 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
709 {
710 struct iris_context *ice = (struct iris_context *) ctx;
711 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
712 struct iris_rasterizer_state *new_cso = state;
713
714 if (new_cso) {
715 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
716 if (cso_changed_memcmp(line_stipple))
717 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
718
719 if (cso_changed(half_pixel_center))
720 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
721
722 if (cso_changed(line_stipple_enable) || cso_changed(poly_stipple_enable))
723 ice->state.dirty |= IRIS_DIRTY_WM;
724 }
725
726 ice->state.cso_rast = new_cso;
727 ice->state.dirty |= IRIS_DIRTY_RASTER;
728 ice->state.dirty |= IRIS_DIRTY_CLIP;
729 }
730
731 static uint32_t
732 translate_wrap(unsigned pipe_wrap)
733 {
734 static const unsigned map[] = {
735 [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
736 [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
737 [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
738 [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
739 [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
740 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
741
742 /* These are unsupported. */
743 [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1,
744 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1,
745 };
746 return map[pipe_wrap];
747 }
748
749 /**
750 * Return true if the given wrap mode requires the border color to exist.
751 */
752 static bool
753 wrap_mode_needs_border_color(unsigned wrap_mode)
754 {
755 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
756 }
757
758 static unsigned
759 translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
760 {
761 static const unsigned map[] = {
762 [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
763 [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
764 [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
765 };
766 return map[pipe_mip];
767 }
768
769 struct iris_sampler_state {
770 struct pipe_sampler_state base;
771
772 bool needs_border_color;
773
774 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
775 };
776
777 static void *
778 iris_create_sampler_state(struct pipe_context *pctx,
779 const struct pipe_sampler_state *state)
780 {
781 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
782
783 if (!cso)
784 return NULL;
785
786 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
787 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
788
789 unsigned wrap_s = translate_wrap(state->wrap_s);
790 unsigned wrap_t = translate_wrap(state->wrap_t);
791 unsigned wrap_r = translate_wrap(state->wrap_r);
792
793 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
794 wrap_mode_needs_border_color(wrap_t) ||
795 wrap_mode_needs_border_color(wrap_r);
796
797 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
798 samp.TCXAddressControlMode = wrap_s;
799 samp.TCYAddressControlMode = wrap_t;
800 samp.TCZAddressControlMode = wrap_r;
801 samp.CubeSurfaceControlMode = state->seamless_cube_map;
802 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
803 samp.MinModeFilter = state->min_img_filter;
804 samp.MagModeFilter = state->mag_img_filter;
805 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
806 samp.MaximumAnisotropy = RATIO21;
807
808 if (state->max_anisotropy >= 2) {
809 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
810 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
811 samp.AnisotropicAlgorithm = EWAApproximation;
812 }
813
814 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
815 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
816
817 samp.MaximumAnisotropy =
818 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
819 }
820
821 /* Set address rounding bits if not using nearest filtering. */
822 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
823 samp.UAddressMinFilterRoundingEnable = true;
824 samp.VAddressMinFilterRoundingEnable = true;
825 samp.RAddressMinFilterRoundingEnable = true;
826 }
827
828 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
829 samp.UAddressMagFilterRoundingEnable = true;
830 samp.VAddressMagFilterRoundingEnable = true;
831 samp.RAddressMagFilterRoundingEnable = true;
832 }
833
834 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
835 samp.ShadowFunction = translate_shadow_func(state->compare_func);
836
837 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
838
839 samp.LODPreClampMode = CLAMP_MODE_OGL;
840 samp.MinLOD = CLAMP(state->min_lod, 0, hw_max_lod);
841 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
842 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
843
844 //samp.BorderColorPointer = <<comes from elsewhere>>
845 }
846
847 return cso;
848 }
849
850 static void
851 iris_bind_sampler_states(struct pipe_context *ctx,
852 enum pipe_shader_type p_stage,
853 unsigned start, unsigned count,
854 void **states)
855 {
856 struct iris_context *ice = (struct iris_context *) ctx;
857 gl_shader_stage stage = stage_from_pipe(p_stage);
858
859 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
860
861 /* Assemble the SAMPLER_STATEs into a contiguous chunk of memory
862 * relative to Dynamic State Base Address.
863 */
864 void *map = NULL;
865 u_upload_alloc(ice->state.dynamic_uploader, 0,
866 count * 4 * GENX(SAMPLER_STATE_length), 32,
867 &ice->state.sampler_table_offset[stage],
868 &ice->state.sampler_table_resource[stage],
869 &map);
870 if (unlikely(!map))
871 return;
872
873 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
874 ice->state.sampler_table_offset[stage] +=
875 iris_bo_offset_from_base_address(iris_resource_bo(res));
876
877 for (int i = 0; i < count; i++) {
878 struct iris_sampler_state *state = states[i];
879
880 /* Save a pointer to the iris_sampler_state, a few fields need
881 * to inform draw-time decisions.
882 */
883 ice->state.samplers[stage][start + i] = state;
884
885 if (state)
886 memcpy(map, state->sampler_state, 4 * GENX(SAMPLER_STATE_length));
887
888 map += GENX(SAMPLER_STATE_length);
889 }
890
891 ice->state.num_samplers[stage] = count;
892
893 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
894 }
895
896 struct iris_sampler_view {
897 struct pipe_sampler_view pipe;
898 struct isl_view view;
899
900 /** The resource (BO) holding our SURFACE_STATE. */
901 struct pipe_resource *surface_state_resource;
902 unsigned surface_state_offset;
903 };
904
905 /**
906 * Convert an swizzle enumeration (i.e. PIPE_SWIZZLE_X) to one of the Gen7.5+
907 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
908 *
909 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
910 * 0 1 2 3 4 5
911 * 4 5 6 7 0 1
912 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
913 *
914 * which is simply adding 4 then modding by 8 (or anding with 7).
915 *
916 * We then may need to apply workarounds for textureGather hardware bugs.
917 */
918 static enum isl_channel_select
919 pipe_swizzle_to_isl_channel(enum pipe_swizzle swizzle)
920 {
921 return (swizzle + 4) & 7;
922 }
923
924 static struct pipe_sampler_view *
925 iris_create_sampler_view(struct pipe_context *ctx,
926 struct pipe_resource *tex,
927 const struct pipe_sampler_view *tmpl)
928 {
929 struct iris_context *ice = (struct iris_context *) ctx;
930 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
931 struct iris_resource *itex = (struct iris_resource *) tex;
932 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
933
934 if (!isv)
935 return NULL;
936
937 /* initialize base object */
938 isv->pipe = *tmpl;
939 isv->pipe.context = ctx;
940 isv->pipe.texture = NULL;
941 pipe_reference_init(&isv->pipe.reference, 1);
942 pipe_resource_reference(&isv->pipe.texture, tex);
943
944 /* XXX: do we need brw_get_texture_swizzle hacks here? */
945
946 isv->view = (struct isl_view) {
947 .format = iris_isl_format_for_pipe_format(tmpl->format),
948 .base_level = tmpl->u.tex.first_level,
949 .levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1,
950 .base_array_layer = tmpl->u.tex.first_layer,
951 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
952 .swizzle = (struct isl_swizzle) {
953 .r = pipe_swizzle_to_isl_channel(tmpl->swizzle_r),
954 .g = pipe_swizzle_to_isl_channel(tmpl->swizzle_g),
955 .b = pipe_swizzle_to_isl_channel(tmpl->swizzle_b),
956 .a = pipe_swizzle_to_isl_channel(tmpl->swizzle_a),
957 },
958 .usage = ISL_SURF_USAGE_TEXTURE_BIT,
959 };
960
961 void *map = NULL;
962 u_upload_alloc(ice->state.surface_uploader, 0,
963 4 * GENX(RENDER_SURFACE_STATE_length), 64,
964 &isv->surface_state_offset,
965 &isv->surface_state_resource,
966 &map);
967 if (!unlikely(map))
968 return NULL;
969
970 struct iris_bo *state_bo = iris_resource_bo(isv->surface_state_resource);
971 isv->surface_state_offset += iris_bo_offset_from_base_address(state_bo);
972
973 isl_surf_fill_state(&screen->isl_dev, map,
974 .surf = &itex->surf, .view = &isv->view,
975 .mocs = MOCS_WB,
976 .address = itex->bo->gtt_offset);
977 // .aux_surf =
978 // .clear_color = clear_color,
979
980 return &isv->pipe;
981 }
982
983 static struct pipe_surface *
984 iris_create_surface(struct pipe_context *ctx,
985 struct pipe_resource *tex,
986 const struct pipe_surface *tmpl)
987 {
988 struct iris_context *ice = (struct iris_context *) ctx;
989 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
990 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
991 struct pipe_surface *psurf = &surf->pipe;
992 struct iris_resource *res = (struct iris_resource *) tex;
993
994 if (!surf)
995 return NULL;
996
997 pipe_reference_init(&psurf->reference, 1);
998 pipe_resource_reference(&psurf->texture, tex);
999 psurf->context = ctx;
1000 psurf->format = tmpl->format;
1001 psurf->width = tex->width0;
1002 psurf->height = tex->height0;
1003 psurf->texture = tex;
1004 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
1005 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
1006 psurf->u.tex.level = tmpl->u.tex.level;
1007
1008 unsigned usage = 0;
1009 if (tmpl->writable)
1010 usage = ISL_SURF_USAGE_STORAGE_BIT;
1011 else if (util_format_is_depth_or_stencil(tmpl->format))
1012 usage = ISL_SURF_USAGE_DEPTH_BIT;
1013 else
1014 usage = ISL_SURF_USAGE_RENDER_TARGET_BIT;
1015
1016 surf->view = (struct isl_view) {
1017 .format = iris_isl_format_for_pipe_format(tmpl->format),
1018 .base_level = tmpl->u.tex.level,
1019 .levels = 1,
1020 .base_array_layer = tmpl->u.tex.first_layer,
1021 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
1022 .swizzle = ISL_SWIZZLE_IDENTITY,
1023 .usage = usage,
1024 };
1025
1026 /* Bail early for depth/stencil */
1027 if (res->surf.usage & (ISL_SURF_USAGE_DEPTH_BIT |
1028 ISL_SURF_USAGE_STENCIL_BIT))
1029 return psurf;
1030
1031 void *map = NULL;
1032 u_upload_alloc(ice->state.surface_uploader, 0,
1033 4 * GENX(RENDER_SURFACE_STATE_length), 64,
1034 &surf->surface_state_offset,
1035 &surf->surface_state_resource,
1036 &map);
1037 if (!unlikely(map))
1038 return NULL;
1039
1040 struct iris_bo *state_bo = iris_resource_bo(surf->surface_state_resource);
1041 surf->surface_state_offset += iris_bo_offset_from_base_address(state_bo);
1042
1043 isl_surf_fill_state(&screen->isl_dev, map,
1044 .surf = &res->surf, .view = &surf->view,
1045 .mocs = MOCS_WB,
1046 .address = res->bo->gtt_offset);
1047 // .aux_surf =
1048 // .clear_color = clear_color,
1049
1050 return psurf;
1051 }
1052
1053 static void
1054 iris_set_sampler_views(struct pipe_context *ctx,
1055 enum pipe_shader_type p_stage,
1056 unsigned start, unsigned count,
1057 struct pipe_sampler_view **views)
1058 {
1059 struct iris_context *ice = (struct iris_context *) ctx;
1060 gl_shader_stage stage = stage_from_pipe(p_stage);
1061
1062 unsigned i;
1063 for (i = 0; i < count; i++) {
1064 pipe_sampler_view_reference((struct pipe_sampler_view **)
1065 &ice->state.textures[stage][i], views[i]);
1066 }
1067 for (; i < ice->state.num_textures[stage]; i++) {
1068 pipe_sampler_view_reference((struct pipe_sampler_view **)
1069 &ice->state.textures[stage][i], NULL);
1070 }
1071
1072 ice->state.num_textures[stage] = count;
1073
1074 ice->state.dirty |= (IRIS_DIRTY_BINDINGS_VS << stage);
1075 }
1076
1077 static void
1078 iris_set_clip_state(struct pipe_context *ctx,
1079 const struct pipe_clip_state *state)
1080 {
1081 }
1082
1083 static void
1084 iris_set_polygon_stipple(struct pipe_context *ctx,
1085 const struct pipe_poly_stipple *state)
1086 {
1087 struct iris_context *ice = (struct iris_context *) ctx;
1088 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
1089 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
1090 }
1091
1092 static void
1093 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
1094 {
1095 struct iris_context *ice = (struct iris_context *) ctx;
1096
1097 ice->state.sample_mask = sample_mask;
1098 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
1099 }
1100
1101 static void
1102 iris_set_scissor_states(struct pipe_context *ctx,
1103 unsigned start_slot,
1104 unsigned num_scissors,
1105 const struct pipe_scissor_state *states)
1106 {
1107 struct iris_context *ice = (struct iris_context *) ctx;
1108
1109 for (unsigned i = 0; i < num_scissors; i++) {
1110 ice->state.scissors[start_slot + i] = states[i];
1111 }
1112
1113 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
1114 }
1115
1116 static void
1117 iris_set_stencil_ref(struct pipe_context *ctx,
1118 const struct pipe_stencil_ref *state)
1119 {
1120 struct iris_context *ice = (struct iris_context *) ctx;
1121 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
1122 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
1123 }
1124
1125
1126 struct iris_viewport_state {
1127 uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length) * IRIS_MAX_VIEWPORTS];
1128 };
1129
1130 static float
1131 viewport_extent(const struct pipe_viewport_state *state, int axis, float sign)
1132 {
1133 return copysignf(state->scale[axis], sign) + state->translate[axis];
1134 }
1135
1136 #if 0
1137 static void
1138 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
1139 float m00, float m11, float m30, float m31,
1140 float *xmin, float *xmax,
1141 float *ymin, float *ymax)
1142 {
1143 /* According to the "Vertex X,Y Clamping and Quantization" section of the
1144 * Strips and Fans documentation:
1145 *
1146 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
1147 * fixed-point "guardband" range supported by the rasterization hardware"
1148 *
1149 * and
1150 *
1151 * "In almost all circumstances, if an object’s vertices are actually
1152 * modified by this clamping (i.e., had X or Y coordinates outside of
1153 * the guardband extent the rendered object will not match the intended
1154 * result. Therefore software should take steps to ensure that this does
1155 * not happen - e.g., by clipping objects such that they do not exceed
1156 * these limits after the Drawing Rectangle is applied."
1157 *
1158 * I believe the fundamental restriction is that the rasterizer (in
1159 * the SF/WM stages) have a limit on the number of pixels that can be
1160 * rasterized. We need to ensure any coordinates beyond the rasterizer
1161 * limit are handled by the clipper. So effectively that limit becomes
1162 * the clipper's guardband size.
1163 *
1164 * It goes on to say:
1165 *
1166 * "In addition, in order to be correctly rendered, objects must have a
1167 * screenspace bounding box not exceeding 8K in the X or Y direction.
1168 * This additional restriction must also be comprehended by software,
1169 * i.e., enforced by use of clipping."
1170 *
1171 * This makes no sense. Gen7+ hardware supports 16K render targets,
1172 * and you definitely need to be able to draw polygons that fill the
1173 * surface. Our assumption is that the rasterizer was limited to 8K
1174 * on Sandybridge, which only supports 8K surfaces, and it was actually
1175 * increased to 16K on Ivybridge and later.
1176 *
1177 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1178 */
1179 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1180
1181 if (m00 != 0 && m11 != 0) {
1182 /* First, we compute the screen-space render area */
1183 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1184 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1185 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1186 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1187
1188 /* We want the guardband to be centered on that */
1189 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1190 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1191 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1192 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1193
1194 /* Now we need it in native device coordinates */
1195 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1196 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1197 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1198 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1199
1200 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1201 * flipped upside-down. X should be fine though.
1202 */
1203 assert(ndc_gb_xmin <= ndc_gb_xmax);
1204 *xmin = ndc_gb_xmin;
1205 *xmax = ndc_gb_xmax;
1206 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1207 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1208 } else {
1209 /* The viewport scales to 0, so nothing will be rendered. */
1210 *xmin = 0.0f;
1211 *xmax = 0.0f;
1212 *ymin = 0.0f;
1213 *ymax = 0.0f;
1214 }
1215 }
1216 #endif
1217
1218 static void
1219 iris_set_viewport_states(struct pipe_context *ctx,
1220 unsigned start_slot,
1221 unsigned count,
1222 const struct pipe_viewport_state *states)
1223 {
1224 struct iris_context *ice = (struct iris_context *) ctx;
1225 struct iris_viewport_state *cso = ice->state.cso_vp;
1226 uint32_t *vp_map = &cso->sf_cl_vp[start_slot];
1227
1228 // XXX: sf_cl_vp is only big enough for one slot, we don't iterate right
1229 for (unsigned i = 0; i < count; i++) {
1230 const struct pipe_viewport_state *state = &states[start_slot + i];
1231 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
1232 vp.ViewportMatrixElementm00 = state->scale[0];
1233 vp.ViewportMatrixElementm11 = state->scale[1];
1234 vp.ViewportMatrixElementm22 = state->scale[2];
1235 vp.ViewportMatrixElementm30 = state->translate[0];
1236 vp.ViewportMatrixElementm31 = state->translate[1];
1237 vp.ViewportMatrixElementm32 = state->translate[2];
1238 /* XXX: in i965 this is computed based on the drawbuffer size,
1239 * but we don't have that here...
1240 */
1241 vp.XMinClipGuardband = -1.0;
1242 vp.XMaxClipGuardband = 1.0;
1243 vp.YMinClipGuardband = -1.0;
1244 vp.YMaxClipGuardband = 1.0;
1245 vp.XMinViewPort = viewport_extent(state, 0, -1.0f);
1246 vp.XMaxViewPort = viewport_extent(state, 0, 1.0f) - 1;
1247 vp.YMinViewPort = viewport_extent(state, 1, -1.0f);
1248 vp.YMaxViewPort = viewport_extent(state, 1, 1.0f) - 1;
1249 }
1250
1251 vp_map += GENX(SF_CLIP_VIEWPORT_length);
1252 }
1253
1254 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
1255 }
1256
1257 struct iris_depth_buffer_state
1258 {
1259 uint32_t packets[GENX(3DSTATE_DEPTH_BUFFER_length) +
1260 GENX(3DSTATE_STENCIL_BUFFER_length) +
1261 GENX(3DSTATE_HIER_DEPTH_BUFFER_length) +
1262 GENX(3DSTATE_CLEAR_PARAMS_length)];
1263 };
1264
1265 static void
1266 iris_set_framebuffer_state(struct pipe_context *ctx,
1267 const struct pipe_framebuffer_state *state)
1268 {
1269 struct iris_context *ice = (struct iris_context *) ctx;
1270 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1271 struct isl_device *isl_dev = &screen->isl_dev;
1272 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
1273
1274 if (cso->samples != state->samples) {
1275 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1276 }
1277
1278 if (cso->nr_cbufs != state->nr_cbufs) {
1279 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
1280 }
1281
1282 if ((cso->layers == 0) == (state->layers == 0)) {
1283 ice->state.dirty |= IRIS_DIRTY_CLIP;
1284 }
1285
1286 util_copy_framebuffer_state(cso, state);
1287
1288 struct iris_depth_buffer_state *cso_z =
1289 malloc(sizeof(struct iris_depth_buffer_state));
1290
1291 struct isl_view view = {
1292 .base_level = 0,
1293 .levels = 1,
1294 .base_array_layer = 0,
1295 .array_len = 1,
1296 .swizzle = ISL_SWIZZLE_IDENTITY,
1297 };
1298
1299 struct isl_depth_stencil_hiz_emit_info info = {
1300 .view = &view,
1301 .mocs = MOCS_WB,
1302 };
1303
1304 struct iris_resource *zres =
1305 (void *) (cso->zsbuf ? cso->zsbuf->texture : NULL);
1306
1307 if (zres) {
1308 view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
1309
1310 info.depth_surf = &zres->surf;
1311 info.depth_address = zres->bo->gtt_offset;
1312
1313 view.format = zres->surf.format;
1314
1315 view.base_level = cso->zsbuf->u.tex.level;
1316 view.base_array_layer = cso->zsbuf->u.tex.first_layer;
1317 view.array_len =
1318 cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
1319
1320 info.hiz_usage = ISL_AUX_USAGE_NONE;
1321 }
1322
1323 #if 0
1324 if (stencil_mt) {
1325 view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
1326 info.stencil_surf = &stencil_mt->surf;
1327
1328 if (!depth_mt) {
1329 view.base_level = stencil_irb->mt_level - stencil_irb->mt->first_level;
1330 view.base_array_layer = stencil_irb->mt_layer;
1331 view.array_len = MAX2(stencil_irb->layer_count, 1);
1332 view.format = stencil_mt->surf.format;
1333 }
1334
1335 uint32_t stencil_offset = 0;
1336 info.stencil_address = stencil_mt->bo->gtt_offset + stencil_mt->offset;
1337 }
1338 #endif
1339
1340 isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
1341
1342 free(ice->state.cso_depthbuffer);
1343 ice->state.cso_depthbuffer = cso_z;
1344 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
1345
1346 /* Render target change */
1347 ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
1348 }
1349
1350 static void
1351 iris_set_constant_buffer(struct pipe_context *ctx,
1352 enum pipe_shader_type p_stage, unsigned index,
1353 const struct pipe_constant_buffer *input)
1354 {
1355 struct iris_context *ice = (struct iris_context *) ctx;
1356 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1357 gl_shader_stage stage = stage_from_pipe(p_stage);
1358 struct iris_shader_state *shs = &ice->shaders.state[stage];
1359 struct iris_const_buffer *cbuf = &shs->constbuf[index];
1360
1361 if (input && (input->buffer || input->user_buffer)) {
1362 if (input->user_buffer) {
1363 u_upload_data(ctx->const_uploader, 0, input->buffer_size, 32,
1364 input->user_buffer, &cbuf->offset, &cbuf->resource);
1365 } else {
1366 pipe_resource_reference(&cbuf->resource, input->buffer);
1367 }
1368
1369 void *map = NULL;
1370 // XXX: these are not retained forever, use a separate uploader?
1371 u_upload_alloc(ice->state.surface_uploader, 0,
1372 4 * GENX(RENDER_SURFACE_STATE_length), 64,
1373 &cbuf->surface_state_offset,
1374 &cbuf->surface_state_resource,
1375 &map);
1376 if (!unlikely(map)) {
1377 pipe_resource_reference(&cbuf->resource, NULL);
1378 return;
1379 }
1380
1381 struct iris_resource *res = (void *) cbuf->resource;
1382 struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state_resource);
1383 cbuf->surface_state_offset += iris_bo_offset_from_base_address(surf_bo);
1384
1385 isl_buffer_fill_state(&screen->isl_dev, map,
1386 .address = res->bo->gtt_offset + cbuf->offset,
1387 .size_B = input->buffer_size,
1388 .format = ISL_FORMAT_R32G32B32A32_FLOAT,
1389 .stride_B = 1,
1390 .mocs = MOCS_WB)
1391 } else {
1392 pipe_resource_reference(&cbuf->resource, NULL);
1393 pipe_resource_reference(&cbuf->surface_state_resource, NULL);
1394 }
1395
1396 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
1397 // XXX: maybe not necessary all the time...?
1398 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1399 }
1400
1401 static void
1402 iris_sampler_view_destroy(struct pipe_context *ctx,
1403 struct pipe_sampler_view *state)
1404 {
1405 struct iris_sampler_view *isv = (void *) state;
1406 pipe_resource_reference(&state->texture, NULL);
1407 pipe_resource_reference(&isv->surface_state_resource, NULL);
1408 free(isv);
1409 }
1410
1411
1412 static void
1413 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1414 {
1415 struct iris_surface *surf = (void *) p_surf;
1416 pipe_resource_reference(&p_surf->texture, NULL);
1417 pipe_resource_reference(&surf->surface_state_resource, NULL);
1418 free(surf);
1419 }
1420
1421 static void
1422 iris_delete_state(struct pipe_context *ctx, void *state)
1423 {
1424 free(state);
1425 }
1426
1427 struct iris_vertex_buffer_state {
1428 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
1429 struct pipe_resource *resources[33];
1430 unsigned num_buffers;
1431 };
1432
1433 static void
1434 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
1435 {
1436 for (unsigned i = 0; i < cso->num_buffers; i++)
1437 pipe_resource_reference(&cso->resources[i], NULL);
1438 }
1439
1440 static void
1441 iris_set_vertex_buffers(struct pipe_context *ctx,
1442 unsigned start_slot, unsigned count,
1443 const struct pipe_vertex_buffer *buffers)
1444 {
1445 struct iris_context *ice = (struct iris_context *) ctx;
1446 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
1447
1448 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
1449
1450 if (!buffers)
1451 count = 0;
1452
1453 cso->num_buffers = count;
1454
1455 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
1456 vb.DWordLength = 4 * MAX2(cso->num_buffers, 1) - 1;
1457 }
1458
1459 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
1460
1461 if (count == 0) {
1462 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1463 vb.VertexBufferIndex = start_slot;
1464 vb.NullVertexBuffer = true;
1465 vb.AddressModifyEnable = true;
1466 }
1467 }
1468
1469 for (unsigned i = 0; i < count; i++) {
1470 assert(!buffers[i].is_user_buffer);
1471
1472 pipe_resource_reference(&cso->resources[i], buffers[i].buffer.resource);
1473 struct iris_resource *res = (void *) cso->resources[i];
1474
1475 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1476 vb.VertexBufferIndex = start_slot + i;
1477 vb.MOCS = MOCS_WB;
1478 vb.AddressModifyEnable = true;
1479 vb.BufferPitch = buffers[i].stride;
1480 vb.BufferSize = res->bo->size;
1481 vb.BufferStartingAddress =
1482 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
1483 }
1484
1485 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
1486 }
1487
1488 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
1489 }
1490
1491 struct iris_vertex_element_state {
1492 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
1493 uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
1494 unsigned count;
1495 };
1496
1497 static void *
1498 iris_create_vertex_elements(struct pipe_context *ctx,
1499 unsigned count,
1500 const struct pipe_vertex_element *state)
1501 {
1502 struct iris_vertex_element_state *cso =
1503 malloc(sizeof(struct iris_vertex_element_state));
1504
1505 cso->count = MAX2(count, 1);
1506
1507 /* TODO:
1508 * - create edge flag one
1509 * - create SGV ones
1510 * - if those are necessary, use count + 1/2/3... OR in the length
1511 */
1512 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
1513 ve.DWordLength = 1 + GENX(VERTEX_ELEMENT_STATE_length) * cso->count - 2;
1514 }
1515
1516 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
1517 uint32_t *vfi_pack_dest = cso->vf_instancing;
1518
1519 if (count == 0) {
1520 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1521 ve.Valid = true;
1522 ve.SourceElementFormat = ISL_FORMAT_R32G32B32A32_FLOAT;
1523 ve.Component0Control = VFCOMP_STORE_0;
1524 ve.Component1Control = VFCOMP_STORE_0;
1525 ve.Component2Control = VFCOMP_STORE_0;
1526 ve.Component3Control = VFCOMP_STORE_1_FP;
1527 }
1528
1529 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
1530 }
1531 }
1532
1533 for (int i = 0; i < count; i++) {
1534 enum isl_format isl_format =
1535 iris_isl_format_for_pipe_format(state[i].src_format);
1536 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
1537 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
1538
1539 switch (isl_format_get_num_channels(isl_format)) {
1540 case 0: comp[0] = VFCOMP_STORE_0;
1541 case 1: comp[1] = VFCOMP_STORE_0;
1542 case 2: comp[2] = VFCOMP_STORE_0;
1543 case 3:
1544 comp[3] = isl_format_has_int_channel(isl_format) ? VFCOMP_STORE_1_INT
1545 : VFCOMP_STORE_1_FP;
1546 break;
1547 }
1548 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1549 ve.VertexBufferIndex = state[i].vertex_buffer_index;
1550 ve.Valid = true;
1551 ve.SourceElementOffset = state[i].src_offset;
1552 ve.SourceElementFormat = isl_format;
1553 ve.Component0Control = comp[0];
1554 ve.Component1Control = comp[1];
1555 ve.Component2Control = comp[2];
1556 ve.Component3Control = comp[3];
1557 }
1558
1559 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
1560 vi.VertexElementIndex = i;
1561 vi.InstancingEnable = state[i].instance_divisor > 0;
1562 vi.InstanceDataStepRate = state[i].instance_divisor;
1563 }
1564
1565 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
1566 vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
1567 }
1568
1569 return cso;
1570 }
1571
1572 static void
1573 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
1574 {
1575 struct iris_context *ice = (struct iris_context *) ctx;
1576
1577 ice->state.cso_vertex_elements = state;
1578 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
1579 }
1580
1581 static void *
1582 iris_create_compute_state(struct pipe_context *ctx,
1583 const struct pipe_compute_state *state)
1584 {
1585 return malloc(1);
1586 }
1587
1588 static struct pipe_stream_output_target *
1589 iris_create_stream_output_target(struct pipe_context *ctx,
1590 struct pipe_resource *res,
1591 unsigned buffer_offset,
1592 unsigned buffer_size)
1593 {
1594 struct pipe_stream_output_target *t =
1595 CALLOC_STRUCT(pipe_stream_output_target);
1596 if (!t)
1597 return NULL;
1598
1599 pipe_reference_init(&t->reference, 1);
1600 pipe_resource_reference(&t->buffer, res);
1601 t->buffer_offset = buffer_offset;
1602 t->buffer_size = buffer_size;
1603 return t;
1604 }
1605
1606 static void
1607 iris_stream_output_target_destroy(struct pipe_context *ctx,
1608 struct pipe_stream_output_target *t)
1609 {
1610 pipe_resource_reference(&t->buffer, NULL);
1611 free(t);
1612 }
1613
1614 static void
1615 iris_set_stream_output_targets(struct pipe_context *ctx,
1616 unsigned num_targets,
1617 struct pipe_stream_output_target **targets,
1618 const unsigned *offsets)
1619 {
1620 }
1621
1622 static void
1623 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
1624 const struct brw_vue_map *last_vue_map,
1625 bool two_sided_color,
1626 unsigned *out_offset,
1627 unsigned *out_length)
1628 {
1629 /* The compiler computes the first URB slot without considering COL/BFC
1630 * swizzling (because it doesn't know whether it's enabled), so we need
1631 * to do that here too. This may result in a smaller offset, which
1632 * should be safe.
1633 */
1634 const unsigned first_slot =
1635 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
1636
1637 /* This becomes the URB read offset (counted in pairs of slots). */
1638 assert(first_slot % 2 == 0);
1639 *out_offset = first_slot / 2;
1640
1641 /* We need to adjust the inputs read to account for front/back color
1642 * swizzling, as it can make the URB length longer.
1643 */
1644 for (int c = 0; c <= 1; c++) {
1645 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
1646 /* If two sided color is enabled, the fragment shader's gl_Color
1647 * (COL0) input comes from either the gl_FrontColor (COL0) or
1648 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
1649 */
1650 if (two_sided_color)
1651 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1652
1653 /* If front color isn't written, we opt to give them back color
1654 * instead of an undefined value. Switch from COL to BFC.
1655 */
1656 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
1657 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
1658 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1659 }
1660 }
1661 }
1662
1663 /* Compute the minimum URB Read Length necessary for the FS inputs.
1664 *
1665 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
1666 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
1667 *
1668 * "This field should be set to the minimum length required to read the
1669 * maximum source attribute. The maximum source attribute is indicated
1670 * by the maximum value of the enabled Attribute # Source Attribute if
1671 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
1672 * enable is not set.
1673 * read_length = ceiling((max_source_attr + 1) / 2)
1674 *
1675 * [errata] Corruption/Hang possible if length programmed larger than
1676 * recommended"
1677 *
1678 * Similar text exists for Ivy Bridge.
1679 *
1680 * We find the last URB slot that's actually read by the FS.
1681 */
1682 unsigned last_read_slot = last_vue_map->num_slots - 1;
1683 while (last_read_slot > first_slot && !(fs_input_slots &
1684 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
1685 --last_read_slot;
1686
1687 /* The URB read length is the difference of the two, counted in pairs. */
1688 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
1689 }
1690
1691 static void
1692 iris_emit_sbe_swiz(struct iris_batch *batch,
1693 const struct iris_context *ice,
1694 unsigned urb_read_offset)
1695 {
1696 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) attr_overrides[16] = {};
1697 const struct brw_wm_prog_data *wm_prog_data = (void *)
1698 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
1699 const struct brw_vue_map *vue_map = ice->shaders.last_vue_map;
1700 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
1701
1702 /* XXX: this should be generated when putting programs in place */
1703
1704 // XXX: raster->sprite_coord_enable
1705
1706 for (int fs_attr = 0; fs_attr < VARYING_SLOT_MAX; fs_attr++) {
1707 const int input_index = wm_prog_data->urb_setup[fs_attr];
1708 if (input_index < 0 || input_index >= 16)
1709 continue;
1710
1711 struct GENX(SF_OUTPUT_ATTRIBUTE_DETAIL) *attr =
1712 &attr_overrides[input_index];
1713
1714 /* Viewport and Layer are stored in the VUE header. We need to override
1715 * them to zero if earlier stages didn't write them, as GL requires that
1716 * they read back as zero when not explicitly set.
1717 */
1718 switch (fs_attr) {
1719 case VARYING_SLOT_VIEWPORT:
1720 case VARYING_SLOT_LAYER:
1721 attr->ComponentOverrideX = true;
1722 attr->ComponentOverrideW = true;
1723 attr->ConstantSource = CONST_0000;
1724
1725 if (!(vue_map->slots_valid & VARYING_BIT_LAYER))
1726 attr->ComponentOverrideY = true;
1727 if (!(vue_map->slots_valid & VARYING_BIT_VIEWPORT))
1728 attr->ComponentOverrideZ = true;
1729 continue;
1730
1731 case VARYING_SLOT_PRIMITIVE_ID:
1732 attr->ComponentOverrideX = true;
1733 attr->ComponentOverrideY = true;
1734 attr->ComponentOverrideZ = true;
1735 attr->ComponentOverrideW = true;
1736 attr->ConstantSource = PRIM_ID;
1737 continue;
1738
1739 default:
1740 break;
1741 }
1742
1743 int slot = vue_map->varying_to_slot[fs_attr];
1744
1745 /* If there was only a back color written but not front, use back
1746 * as the color instead of undefined.
1747 */
1748 if (slot == -1 && fs_attr == VARYING_SLOT_COL0)
1749 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC0];
1750 if (slot == -1 && fs_attr == VARYING_SLOT_COL1)
1751 slot = vue_map->varying_to_slot[VARYING_SLOT_BFC1];
1752
1753 /* Not written by the previous stage - undefined. */
1754 if (slot == -1) {
1755 attr->ComponentOverrideX = true;
1756 attr->ComponentOverrideY = true;
1757 attr->ComponentOverrideZ = true;
1758 attr->ComponentOverrideW = true;
1759 attr->ConstantSource = CONST_0001_FLOAT;
1760 continue;
1761 }
1762
1763 /* Compute the location of the attribute relative to the read offset,
1764 * which is counted in 256-bit increments (two 128-bit VUE slots).
1765 */
1766 const int source_attr = slot - 2 * urb_read_offset;
1767 assert(source_attr >= 0 && source_attr <= 32);
1768 attr->SourceAttribute = source_attr;
1769
1770 /* If we are doing two-sided color, and the VUE slot following this one
1771 * represents a back-facing color, then we need to instruct the SF unit
1772 * to do back-facing swizzling.
1773 */
1774 if (cso_rast->light_twoside &&
1775 ((vue_map->slot_to_varying[slot] == VARYING_SLOT_COL0 &&
1776 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC0) ||
1777 (vue_map->slot_to_varying[slot] == VARYING_SLOT_COL1 &&
1778 vue_map->slot_to_varying[slot+1] == VARYING_SLOT_BFC1)))
1779 attr->SwizzleSelect = INPUTATTR_FACING;
1780 }
1781
1782 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbes) {
1783 for (int i = 0; i < 16; i++)
1784 sbes.Attribute[i] = attr_overrides[i];
1785 }
1786 }
1787
1788 static void
1789 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
1790 {
1791 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
1792 const struct brw_wm_prog_data *wm_prog_data = (void *)
1793 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
1794 struct pipe_shader_state *p_fs =
1795 (void *) ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
1796 assert(p_fs->type == PIPE_SHADER_IR_NIR);
1797 nir_shader *fs_nir = p_fs->ir.nir;
1798
1799 unsigned urb_read_offset, urb_read_length;
1800 iris_compute_sbe_urb_read_interval(fs_nir->info.inputs_read,
1801 ice->shaders.last_vue_map,
1802 cso_rast->light_twoside,
1803 &urb_read_offset, &urb_read_length);
1804
1805 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
1806 sbe.AttributeSwizzleEnable = true;
1807 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1808 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
1809 sbe.VertexURBEntryReadOffset = urb_read_offset;
1810 sbe.VertexURBEntryReadLength = urb_read_length;
1811 sbe.ForceVertexURBEntryReadOffset = true;
1812 sbe.ForceVertexURBEntryReadLength = true;
1813 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
1814
1815 for (int i = 0; i < 32; i++) {
1816 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
1817 }
1818 }
1819
1820 iris_emit_sbe_swiz(batch, ice, urb_read_offset);
1821 }
1822
1823 static void
1824 iris_bind_compute_state(struct pipe_context *ctx, void *state)
1825 {
1826 }
1827
1828 static void
1829 iris_populate_sampler_key(const struct iris_context *ice,
1830 struct brw_sampler_prog_key_data *key)
1831 {
1832 for (int i = 0; i < MAX_SAMPLERS; i++) {
1833 key->swizzles[i] = 0x688; /* XYZW */
1834 }
1835 }
1836
1837 static void
1838 iris_populate_vs_key(const struct iris_context *ice,
1839 struct brw_vs_prog_key *key)
1840 {
1841 memset(key, 0, sizeof(*key));
1842 iris_populate_sampler_key(ice, &key->tex);
1843 }
1844
1845 static void
1846 iris_populate_tcs_key(const struct iris_context *ice,
1847 struct brw_tcs_prog_key *key)
1848 {
1849 memset(key, 0, sizeof(*key));
1850 iris_populate_sampler_key(ice, &key->tex);
1851 }
1852
1853 static void
1854 iris_populate_tes_key(const struct iris_context *ice,
1855 struct brw_tes_prog_key *key)
1856 {
1857 memset(key, 0, sizeof(*key));
1858 iris_populate_sampler_key(ice, &key->tex);
1859 }
1860
1861 static void
1862 iris_populate_gs_key(const struct iris_context *ice,
1863 struct brw_gs_prog_key *key)
1864 {
1865 memset(key, 0, sizeof(*key));
1866 iris_populate_sampler_key(ice, &key->tex);
1867 }
1868
1869 static void
1870 iris_populate_fs_key(const struct iris_context *ice,
1871 struct brw_wm_prog_key *key)
1872 {
1873 memset(key, 0, sizeof(*key));
1874 iris_populate_sampler_key(ice, &key->tex);
1875
1876 /* XXX: dirty flags? */
1877 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
1878 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
1879 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
1880 const struct iris_blend_state *blend = ice->state.cso_blend;
1881
1882 key->nr_color_regions = fb->nr_cbufs;
1883
1884 key->clamp_fragment_color = rast->clamp_fragment_color;
1885
1886 key->replicate_alpha = fb->nr_cbufs > 1 &&
1887 (zsa->alpha.enabled || blend->alpha_to_coverage);
1888
1889 /* XXX: only bother if COL0/1 are read */
1890 key->flat_shade = rast->flatshade;
1891
1892 // key->force_dual_color_blend for unigine
1893 #if 0
1894 if (cso_rast->multisample) {
1895 key->persample_interp =
1896 ctx->Multisample.SampleShading &&
1897 (ctx->Multisample.MinSampleShadingValue *
1898 _mesa_geometric_samples(ctx->DrawBuffer) > 1);
1899
1900 key->multisample_fbo = fb->samples > 1;
1901 }
1902 #endif
1903
1904 key->coherent_fb_fetch = true;
1905 }
1906
1907 #if 0
1908 // XXX: these need to go in INIT_THREAD_DISPATCH_FIELDS
1909 pkt.SamplerCount = \
1910 DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
1911 pkt.PerThreadScratchSpace = prog_data->total_scratch == 0 ? 0 : \
1912 ffs(stage_state->per_thread_scratch) - 11; \
1913
1914 #endif
1915
1916 static uint64_t
1917 KSP(const struct iris_compiled_shader *shader)
1918 {
1919 struct iris_resource *res = (void *) shader->buffer;
1920 return res->bo->gtt_offset + shader->offset;
1921 }
1922
1923 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
1924 pkt.KernelStartPointer = KSP(shader); \
1925 pkt.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4; \
1926 pkt.FloatingPointMode = prog_data->use_alt_mode; \
1927 \
1928 pkt.DispatchGRFStartRegisterForURBData = \
1929 prog_data->dispatch_grf_start_reg; \
1930 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
1931 pkt.prefix##URBEntryReadOffset = 0; \
1932 \
1933 pkt.StatisticsEnable = true; \
1934 pkt.Enable = true;
1935
1936 static void
1937 iris_store_vs_state(const struct gen_device_info *devinfo,
1938 struct iris_compiled_shader *shader)
1939 {
1940 struct brw_stage_prog_data *prog_data = shader->prog_data;
1941 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1942
1943 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
1944 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
1945 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1946 vs.SIMD8DispatchEnable = true;
1947 vs.UserClipDistanceCullTestEnableBitmask =
1948 vue_prog_data->cull_distance_mask;
1949 }
1950 }
1951
1952 static void
1953 iris_store_tcs_state(const struct gen_device_info *devinfo,
1954 struct iris_compiled_shader *shader)
1955 {
1956 struct brw_stage_prog_data *prog_data = shader->prog_data;
1957 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1958 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
1959
1960 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
1961 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
1962
1963 hs.InstanceCount = tcs_prog_data->instances - 1;
1964 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1965 hs.IncludeVertexHandles = true;
1966 }
1967 }
1968
1969 static void
1970 iris_store_tes_state(const struct gen_device_info *devinfo,
1971 struct iris_compiled_shader *shader)
1972 {
1973 struct brw_stage_prog_data *prog_data = shader->prog_data;
1974 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1975 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
1976
1977 uint32_t *te_state = (void *) shader->derived_data;
1978 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
1979
1980 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
1981 te.Partitioning = tes_prog_data->partitioning;
1982 te.OutputTopology = tes_prog_data->output_topology;
1983 te.TEDomain = tes_prog_data->domain;
1984 te.TEEnable = true;
1985 te.MaximumTessellationFactorOdd = 63.0;
1986 te.MaximumTessellationFactorNotOdd = 64.0;
1987 }
1988
1989 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
1990 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
1991
1992 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
1993 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1994 ds.ComputeWCoordinateEnable =
1995 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1996
1997 ds.UserClipDistanceCullTestEnableBitmask =
1998 vue_prog_data->cull_distance_mask;
1999 }
2000
2001 }
2002
2003 static void
2004 iris_store_gs_state(const struct gen_device_info *devinfo,
2005 struct iris_compiled_shader *shader)
2006 {
2007 struct brw_stage_prog_data *prog_data = shader->prog_data;
2008 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
2009 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
2010
2011 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
2012 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
2013
2014 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
2015 gs.OutputTopology = gs_prog_data->output_topology;
2016 gs.ControlDataHeaderSize =
2017 gs_prog_data->control_data_header_size_hwords;
2018 gs.InstanceControl = gs_prog_data->invocations - 1;
2019 gs.DispatchMode = DISPATCH_MODE_SIMD8;
2020 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
2021 gs.ControlDataFormat = gs_prog_data->control_data_format;
2022 gs.ReorderMode = TRAILING;
2023 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
2024 gs.MaximumNumberofThreads =
2025 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
2026 : (devinfo->max_gs_threads - 1);
2027
2028 if (gs_prog_data->static_vertex_count != -1) {
2029 gs.StaticOutput = true;
2030 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
2031 }
2032 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
2033
2034 gs.UserClipDistanceCullTestEnableBitmask =
2035 vue_prog_data->cull_distance_mask;
2036
2037 const int urb_entry_write_offset = 1;
2038 const uint32_t urb_entry_output_length =
2039 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
2040 urb_entry_write_offset;
2041
2042 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
2043 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
2044 }
2045 }
2046
2047 static void
2048 iris_store_fs_state(const struct gen_device_info *devinfo,
2049 struct iris_compiled_shader *shader)
2050 {
2051 struct brw_stage_prog_data *prog_data = shader->prog_data;
2052 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
2053
2054 uint32_t *ps_state = (void *) shader->derived_data;
2055 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
2056
2057 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
2058 ps.VectorMaskEnable = true;
2059 //ps.SamplerCount = ...
2060 ps.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4;
2061 ps.FloatingPointMode = prog_data->use_alt_mode;
2062 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
2063
2064 ps.PushConstantEnable = prog_data->nr_params > 0 ||
2065 prog_data->ubo_ranges[0].length > 0;
2066
2067 /* From the documentation for this packet:
2068 * "If the PS kernel does not need the Position XY Offsets to
2069 * compute a Position Value, then this field should be programmed
2070 * to POSOFFSET_NONE."
2071 *
2072 * "SW Recommendation: If the PS kernel needs the Position Offsets
2073 * to compute a Position XY value, this field should match Position
2074 * ZW Interpolation Mode to ensure a consistent position.xyzw
2075 * computation."
2076 *
2077 * We only require XY sample offsets. So, this recommendation doesn't
2078 * look useful at the moment. We might need this in future.
2079 */
2080 ps.PositionXYOffsetSelect =
2081 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
2082 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
2083 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
2084 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
2085
2086 // XXX: Disable SIMD32 with 16x MSAA
2087
2088 ps.DispatchGRFStartRegisterForConstantSetupData0 =
2089 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
2090 ps.DispatchGRFStartRegisterForConstantSetupData1 =
2091 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
2092 ps.DispatchGRFStartRegisterForConstantSetupData2 =
2093 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
2094
2095 ps.KernelStartPointer0 =
2096 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
2097 ps.KernelStartPointer1 =
2098 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
2099 ps.KernelStartPointer2 =
2100 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
2101 }
2102
2103 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
2104 psx.PixelShaderValid = true;
2105 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
2106 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
2107 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
2108 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
2109 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
2110 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
2111
2112 if (wm_prog_data->uses_sample_mask) {
2113 /* TODO: conservative rasterization */
2114 if (wm_prog_data->post_depth_coverage)
2115 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
2116 else
2117 psx.InputCoverageMaskState = ICMS_NORMAL;
2118 }
2119
2120 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
2121 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
2122 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
2123
2124 // XXX: UAV bit
2125 }
2126 }
2127
2128 static unsigned
2129 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
2130 {
2131 assert(cache_id <= IRIS_CACHE_BLORP);
2132
2133 static const unsigned dwords[] = {
2134 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
2135 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
2136 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
2137 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
2138 [IRIS_CACHE_FS] =
2139 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
2140 [IRIS_CACHE_CS] = 0,
2141 [IRIS_CACHE_BLORP] = 0,
2142 };
2143
2144 return sizeof(uint32_t) * dwords[cache_id];
2145 }
2146
2147 static void
2148 iris_store_derived_program_state(const struct gen_device_info *devinfo,
2149 enum iris_program_cache_id cache_id,
2150 struct iris_compiled_shader *shader)
2151 {
2152 switch (cache_id) {
2153 case IRIS_CACHE_VS:
2154 iris_store_vs_state(devinfo, shader);
2155 break;
2156 case IRIS_CACHE_TCS:
2157 iris_store_tcs_state(devinfo, shader);
2158 break;
2159 case IRIS_CACHE_TES:
2160 iris_store_tes_state(devinfo, shader);
2161 break;
2162 case IRIS_CACHE_GS:
2163 iris_store_gs_state(devinfo, shader);
2164 break;
2165 case IRIS_CACHE_FS:
2166 iris_store_fs_state(devinfo, shader);
2167 break;
2168 case IRIS_CACHE_CS:
2169 case IRIS_CACHE_BLORP:
2170 break;
2171 default:
2172 break;
2173 }
2174 }
2175
2176 static void
2177 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
2178 {
2179 const struct gen_device_info *devinfo = &batch->screen->devinfo;
2180 const unsigned push_size_kB = 32;
2181 unsigned entries[4];
2182 unsigned start[4];
2183 unsigned size[4];
2184
2185 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2186 if (!ice->shaders.prog[i]) {
2187 size[i] = 1;
2188 } else {
2189 struct brw_vue_prog_data *vue_prog_data =
2190 (void *) ice->shaders.prog[i]->prog_data;
2191 size[i] = vue_prog_data->urb_entry_size;
2192 }
2193 assert(size[i] != 0);
2194 }
2195
2196 gen_get_urb_config(devinfo, 1024 * push_size_kB,
2197 1024 * ice->shaders.urb_size,
2198 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
2199 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
2200 size, entries, start);
2201
2202 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2203 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
2204 urb._3DCommandSubOpcode += i;
2205 urb.VSURBStartingAddress = start[i];
2206 urb.VSURBEntryAllocationSize = size[i] - 1;
2207 urb.VSNumberofURBEntries = entries[i];
2208 }
2209 }
2210 }
2211
2212 static const uint32_t push_constant_opcodes[] = {
2213 [MESA_SHADER_VERTEX] = 21,
2214 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2215 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2216 [MESA_SHADER_GEOMETRY] = 22,
2217 [MESA_SHADER_FRAGMENT] = 23,
2218 [MESA_SHADER_COMPUTE] = 0,
2219 };
2220
2221 /**
2222 * Add a surface to the validation list, as well as the buffer containing
2223 * the corresponding SURFACE_STATE.
2224 *
2225 * Returns the binding table entry (offset to SURFACE_STATE).
2226 */
2227 static uint32_t
2228 use_surface(struct iris_batch *batch,
2229 struct pipe_surface *p_surf,
2230 bool writeable)
2231 {
2232 struct iris_surface *surf = (void *) p_surf;
2233 struct iris_resource *res = (void *) p_surf->texture;
2234 struct iris_resource *state_res = (void *) surf->surface_state_resource;
2235 iris_use_pinned_bo(batch, res->bo, writeable);
2236 iris_use_pinned_bo(batch, state_res->bo, false);
2237
2238 return surf->surface_state_offset;
2239 }
2240
2241 static uint32_t
2242 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
2243 {
2244 struct iris_resource *res = (void *) isv->pipe.texture;
2245 struct iris_resource *state_res = (void *) isv->surface_state_resource;
2246 iris_use_pinned_bo(batch, res->bo, false);
2247 iris_use_pinned_bo(batch, state_res->bo, false);
2248
2249 return isv->surface_state_offset;
2250 }
2251
2252 static uint32_t
2253 use_const_buffer(struct iris_batch *batch, struct iris_const_buffer *cbuf)
2254 {
2255 struct iris_resource *res = (void *) cbuf->resource;
2256 struct iris_resource *state_res = (void *) cbuf->surface_state_resource;
2257 iris_use_pinned_bo(batch, res->bo, false);
2258 iris_use_pinned_bo(batch, state_res->bo, false);
2259
2260 return cbuf->surface_state_offset;
2261 }
2262
2263 static void
2264 iris_populate_binding_table(struct iris_context *ice,
2265 struct iris_batch *batch,
2266 gl_shader_stage stage)
2267 {
2268 const struct iris_binder *binder = &batch->binder;
2269 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2270 if (!shader)
2271 return;
2272
2273 // Surfaces:
2274 // - pull constants
2275 // - ubos/ssbos/abos
2276 // - images
2277 // - textures
2278 // - render targets - write and read
2279
2280 //struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2281 uint32_t *bt_map = binder->map + binder->bt_offset[stage];
2282 int s = 0;
2283
2284 if (stage == MESA_SHADER_FRAGMENT) {
2285 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2286 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
2287 bt_map[s++] = use_surface(batch, cso_fb->cbufs[i], true);
2288 }
2289 }
2290
2291 //assert(prog_data->binding_table.texture_start ==
2292 //(ice->state.num_textures[stage] ? s : 0xd0d0d0d0));
2293
2294 for (int i = 0; i < ice->state.num_textures[stage]; i++) {
2295 struct iris_sampler_view *view = ice->state.textures[stage][i];
2296 bt_map[s++] = use_sampler_view(batch, view);
2297 }
2298
2299 // XXX: want the number of BTE's to shorten this loop
2300 struct iris_shader_state *shs = &ice->shaders.state[stage];
2301 for (int i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
2302 struct iris_const_buffer *cbuf = &shs->constbuf[i];
2303 if (!cbuf->surface_state_resource)
2304 break;
2305
2306 bt_map[s++] = use_const_buffer(batch, cbuf);
2307 }
2308 #if 0
2309 // XXX: not implemented yet
2310 assert(prog_data->binding_table.pull_constants_start == 0xd0d0d0d0);
2311 assert(prog_data->binding_table.ubo_start == 0xd0d0d0d0);
2312 assert(prog_data->binding_table.ssbo_start == 0xd0d0d0d0);
2313 assert(prog_data->binding_table.image_start == 0xd0d0d0d0);
2314 assert(prog_data->binding_table.shader_time_start == 0xd0d0d0d0);
2315 //assert(prog_data->binding_table.plane_start[1] == 0xd0d0d0d0);
2316 //assert(prog_data->binding_table.plane_start[2] == 0xd0d0d0d0);
2317 #endif
2318 }
2319
2320 static void
2321 iris_use_optional_res(struct iris_batch *batch,
2322 struct pipe_resource *res,
2323 bool writeable)
2324 {
2325 if (res) {
2326 struct iris_bo *bo = iris_resource_bo(res);
2327 iris_use_pinned_bo(batch, bo, writeable);
2328 }
2329 }
2330
2331
2332 /**
2333 * Pin any BOs which were installed by a previous batch, and restored
2334 * via the hardware logical context mechanism.
2335 *
2336 * We don't need to re-emit all state every batch - the hardware context
2337 * mechanism will save and restore it for us. This includes pointers to
2338 * various BOs...which won't exist unless we ask the kernel to pin them
2339 * by adding them to the validation list.
2340 *
2341 * We can skip buffers if we've re-emitted those packets, as we're
2342 * overwriting those stale pointers with new ones, and don't actually
2343 * refer to the old BOs.
2344 */
2345 static void
2346 iris_restore_context_saved_bos(struct iris_context *ice,
2347 struct iris_batch *batch,
2348 const struct pipe_draw_info *draw)
2349 {
2350 // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
2351
2352 const uint64_t clean = ~ice->state.dirty;
2353
2354 if (clean & IRIS_DIRTY_CC_VIEWPORT) {
2355 iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
2356 }
2357
2358 if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
2359 iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
2360 }
2361
2362 if (clean & IRIS_DIRTY_BLEND_STATE) {
2363 iris_use_optional_res(batch, ice->state.last_res.blend, false);
2364 }
2365
2366 if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
2367 iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
2368 }
2369
2370 if (clean & IRIS_DIRTY_SCISSOR_RECT) {
2371 iris_use_optional_res(batch, ice->state.last_res.scissor, false);
2372 }
2373
2374 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2375 if (clean & (IRIS_DIRTY_CONSTANTS_VS << stage))
2376 continue;
2377
2378 struct iris_shader_state *shs = &ice->shaders.state[stage];
2379 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2380
2381 if (!shader)
2382 continue;
2383
2384 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2385
2386 for (int i = 0; i < 4; i++) {
2387 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2388
2389 if (range->length == 0)
2390 continue;
2391
2392 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
2393 struct iris_resource *res = (void *) cbuf->resource;
2394
2395 if (res)
2396 iris_use_pinned_bo(batch, res->bo, false);
2397 else
2398 iris_use_pinned_bo(batch, batch->screen->workaround_bo, false);
2399 }
2400 }
2401
2402 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2403 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
2404 if (res)
2405 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
2406 }
2407
2408 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2409 if (clean & (IRIS_DIRTY_VS << stage)) {
2410 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2411 if (shader)
2412 iris_use_pinned_bo(batch, iris_resource_bo(shader->buffer), false);
2413
2414 // XXX: scratch buffer
2415 }
2416 }
2417
2418 // XXX: 3DSTATE_SO_BUFFER
2419
2420 if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
2421 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2422
2423 if (cso_fb->zsbuf) {
2424 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
2425 // XXX: depth might not be writable...
2426 iris_use_pinned_bo(batch, zres->bo, true);
2427 }
2428 }
2429
2430 if (draw->index_size > 0) {
2431 // XXX: index buffer
2432 }
2433
2434 if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
2435 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
2436 for (unsigned i = 0; i < cso->num_buffers; i++) {
2437 struct iris_resource *res = (void *) cso->resources[i];
2438 iris_use_pinned_bo(batch, res->bo, false);
2439 }
2440 }
2441 }
2442
2443 static void
2444 iris_upload_render_state(struct iris_context *ice,
2445 struct iris_batch *batch,
2446 const struct pipe_draw_info *draw)
2447 {
2448 const uint64_t dirty = ice->state.dirty;
2449
2450 struct brw_wm_prog_data *wm_prog_data = (void *)
2451 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2452
2453 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
2454 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2455 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
2456 ptr.CCViewportPointer =
2457 emit_state(batch, ice->state.dynamic_uploader,
2458 &ice->state.last_res.cc_vp,
2459 cso->cc_vp, sizeof(cso->cc_vp), 32);
2460 }
2461 }
2462
2463 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
2464 struct iris_viewport_state *cso = ice->state.cso_vp;
2465 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
2466 ptr.SFClipViewportPointer =
2467 emit_state(batch, ice->state.dynamic_uploader,
2468 &ice->state.last_res.sf_cl_vp,
2469 cso->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
2470 ice->state.num_viewports, 64);
2471 }
2472 }
2473
2474 /* XXX: L3 State */
2475
2476 // XXX: this is only flagged at setup, we assume a static configuration
2477 if (dirty & IRIS_DIRTY_URB) {
2478 iris_upload_urb_config(ice, batch);
2479 }
2480
2481 if (dirty & IRIS_DIRTY_BLEND_STATE) {
2482 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2483 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2484 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2485 const int num_dwords = 4 * (GENX(BLEND_STATE_length) +
2486 cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length));
2487 uint32_t blend_offset;
2488 uint32_t *blend_map =
2489 stream_state(batch, ice->state.dynamic_uploader,
2490 &ice->state.last_res.blend,
2491 4 * num_dwords, 64, &blend_offset);
2492
2493 uint32_t blend_state_header;
2494 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
2495 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
2496 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
2497 }
2498
2499 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
2500 memcpy(&blend_map[1], &cso_blend->blend_state[1],
2501 sizeof(cso_blend->blend_state) - sizeof(uint32_t));
2502
2503 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
2504 ptr.BlendStatePointer = blend_offset;
2505 ptr.BlendStatePointerValid = true;
2506 }
2507 }
2508
2509 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
2510 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2511 uint32_t cc_offset;
2512 void *cc_map =
2513 stream_state(batch, ice->state.dynamic_uploader,
2514 &ice->state.last_res.color_calc,
2515 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
2516 64, &cc_offset);
2517 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
2518 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
2519 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
2520 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
2521 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
2522 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
2523 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
2524 }
2525 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2526 ptr.ColorCalcStatePointer = cc_offset;
2527 ptr.ColorCalcStatePointerValid = true;
2528 }
2529 }
2530
2531 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2532 // XXX: wrong dirty tracking...
2533 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
2534 continue;
2535
2536 struct iris_shader_state *shs = &ice->shaders.state[stage];
2537 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2538
2539 if (!shader)
2540 continue;
2541
2542 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2543
2544 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
2545 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
2546 if (prog_data) {
2547 /* The Skylake PRM contains the following restriction:
2548 *
2549 * "The driver must ensure The following case does not occur
2550 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2551 * buffer 3 read length equal to zero committed followed by a
2552 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2553 * zero committed."
2554 *
2555 * To avoid this, we program the buffers in the highest slots.
2556 * This way, slot 0 is only used if slot 3 is also used.
2557 */
2558 int n = 3;
2559
2560 for (int i = 3; i >= 0; i--) {
2561 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2562
2563 if (range->length == 0)
2564 continue;
2565
2566 // XXX: is range->block a constbuf index? it would be nice
2567 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
2568 struct iris_resource *res = (void *) cbuf->resource;
2569
2570 assert(cbuf->offset % 32 == 0);
2571
2572 pkt.ConstantBody.ReadLength[n] = range->length;
2573 pkt.ConstantBody.Buffer[n] =
2574 res ? ro_bo(res->bo, range->start * 32 + cbuf->offset)
2575 : ro_bo(batch->screen->workaround_bo, 0);
2576 n--;
2577 }
2578 }
2579 }
2580 }
2581
2582 struct iris_binder *binder = &batch->binder;
2583
2584 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2585 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
2586 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
2587 ptr._3DCommandSubOpcode = 38 + stage;
2588 ptr.PointertoVSBindingTable = binder->bt_offset[stage];
2589 }
2590 }
2591 }
2592
2593 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2594 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
2595 iris_populate_binding_table(ice, batch, stage);
2596 }
2597 }
2598
2599 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2600 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
2601 !ice->shaders.prog[stage])
2602 continue;
2603
2604 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
2605 if (res)
2606 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
2607
2608 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
2609 ptr._3DCommandSubOpcode = 43 + stage;
2610 ptr.PointertoVSSamplerState = ice->state.sampler_table_offset[stage];
2611 }
2612 }
2613
2614 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
2615 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
2616 ms.PixelLocation =
2617 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
2618 if (ice->state.framebuffer.samples > 0)
2619 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
2620 }
2621 }
2622
2623 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
2624 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
2625 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
2626 }
2627 }
2628
2629 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2630 if (!(dirty & (IRIS_DIRTY_VS << stage)))
2631 continue;
2632
2633 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2634
2635 if (shader) {
2636 struct iris_resource *cache = (void *) shader->buffer;
2637 iris_use_pinned_bo(batch, cache->bo, false);
2638 iris_batch_emit(batch, shader->derived_data,
2639 iris_derived_program_state_size(stage));
2640 } else {
2641 if (stage == MESA_SHADER_TESS_EVAL) {
2642 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
2643 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
2644 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
2645 } else if (stage == MESA_SHADER_GEOMETRY) {
2646 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
2647 }
2648 }
2649 }
2650
2651 // XXX: SOL:
2652 // 3DSTATE_STREAMOUT
2653 // 3DSTATE_SO_BUFFER
2654 // 3DSTATE_SO_DECL_LIST
2655
2656 if (dirty & IRIS_DIRTY_CLIP) {
2657 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2658 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2659
2660 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
2661 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
2662 if (wm_prog_data->barycentric_interp_modes &
2663 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
2664 cl.NonPerspectiveBarycentricEnable = true;
2665
2666 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
2667 cl.MaximumVPIndex = ice->state.num_viewports - 1;
2668 }
2669 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
2670 ARRAY_SIZE(cso_rast->clip));
2671 }
2672
2673 if (dirty & IRIS_DIRTY_RASTER) {
2674 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2675 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
2676 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
2677
2678 }
2679
2680 /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
2681 if (dirty & IRIS_DIRTY_WM) {
2682 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2683 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
2684
2685 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
2686 wm.BarycentricInterpolationMode =
2687 wm_prog_data->barycentric_interp_modes;
2688
2689 if (wm_prog_data->early_fragment_tests)
2690 wm.EarlyDepthStencilControl = EDSC_PREPS;
2691 else if (wm_prog_data->has_side_effects)
2692 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
2693 }
2694 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
2695 }
2696
2697 if (1) {
2698 // XXX: 3DSTATE_SBE, 3DSTATE_SBE_SWIZ
2699 // -> iris_raster_state (point sprite texture coordinate origin)
2700 // -> bunch of shader state...
2701 iris_emit_sbe(batch, ice);
2702 }
2703
2704 if (dirty & IRIS_DIRTY_PS_BLEND) {
2705 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2706 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2707 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
2708 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
2709 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
2710 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
2711 }
2712
2713 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
2714 ARRAY_SIZE(cso_blend->ps_blend));
2715 }
2716
2717 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
2718 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2719 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
2720
2721 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
2722 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
2723 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
2724 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
2725 }
2726 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
2727 }
2728
2729 if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
2730 uint32_t scissor_offset =
2731 emit_state(batch, ice->state.dynamic_uploader,
2732 &ice->state.last_res.scissor,
2733 ice->state.scissors,
2734 sizeof(struct pipe_scissor_state) *
2735 ice->state.num_viewports, 32);
2736
2737 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
2738 ptr.ScissorRectPointer = scissor_offset;
2739 }
2740 }
2741
2742 if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
2743 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2744 struct iris_depth_buffer_state *cso_z = ice->state.cso_depthbuffer;
2745
2746 iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
2747
2748 if (cso_fb->zsbuf) {
2749 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
2750 // XXX: depth might not be writable...
2751 iris_use_pinned_bo(batch, zres->bo, true);
2752 }
2753 }
2754
2755 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
2756 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
2757 for (int i = 0; i < 32; i++) {
2758 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
2759 }
2760 }
2761 }
2762
2763 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
2764 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2765 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
2766 }
2767
2768 if (1) {
2769 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
2770 topo.PrimitiveTopologyType =
2771 translate_prim_type(draw->mode, draw->vertices_per_patch);
2772 }
2773 }
2774
2775 if (draw->index_size > 0) {
2776 struct iris_resource *res = NULL;
2777 unsigned offset;
2778
2779 if (draw->has_user_indices) {
2780 u_upload_data(ice->ctx.stream_uploader, 0,
2781 draw->count * draw->index_size, 4, draw->index.user,
2782 &offset, (struct pipe_resource **) &res);
2783 } else {
2784 res = (struct iris_resource *) draw->index.resource;
2785 offset = 0;
2786 }
2787
2788 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
2789 ib.IndexFormat = draw->index_size >> 1;
2790 ib.MOCS = MOCS_WB;
2791 ib.BufferSize = res->bo->size;
2792 ib.BufferStartingAddress = ro_bo(res->bo, offset);
2793 }
2794 }
2795
2796 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
2797 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
2798 const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
2799
2800 if (cso->num_buffers > 0) {
2801 iris_batch_emit(batch, cso->vertex_buffers, sizeof(uint32_t) *
2802 (1 + vb_dwords * cso->num_buffers));
2803
2804 for (unsigned i = 0; i < cso->num_buffers; i++) {
2805 struct iris_resource *res = (void *) cso->resources[i];
2806 iris_use_pinned_bo(batch, res->bo, false);
2807 }
2808 }
2809 }
2810
2811 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
2812 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
2813 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
2814 (1 + cso->count * GENX(VERTEX_ELEMENT_STATE_length)));
2815 iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
2816 cso->count * GENX(3DSTATE_VF_INSTANCING_length));
2817 for (int i = 0; i < cso->count; i++) {
2818 /* TODO: vertexid, instanceid support */
2819 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgvs);
2820 }
2821 }
2822
2823 if (1) {
2824 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
2825 if (draw->primitive_restart) {
2826 vf.IndexedDrawCutIndexEnable = true;
2827 vf.CutIndex = draw->restart_index;
2828 }
2829 }
2830 }
2831
2832 // XXX: Gen8 - PMA fix
2833
2834 assert(!draw->indirect); // XXX: indirect support
2835
2836 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
2837 prim.StartInstanceLocation = draw->start_instance;
2838 prim.InstanceCount = draw->instance_count;
2839 prim.VertexCountPerInstance = draw->count;
2840 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
2841
2842 // XXX: this is probably bonkers.
2843 prim.StartVertexLocation = draw->start;
2844
2845 if (draw->index_size) {
2846 prim.BaseVertexLocation += draw->index_bias;
2847 } else {
2848 prim.StartVertexLocation += draw->index_bias;
2849 }
2850
2851 //prim.BaseVertexLocation = ...;
2852 }
2853
2854 if (!batch->contains_draw) {
2855 iris_restore_context_saved_bos(ice, batch, draw);
2856 batch->contains_draw = true;
2857 }
2858 }
2859
2860 /**
2861 * State module teardown.
2862 */
2863 static void
2864 iris_destroy_state(struct iris_context *ice)
2865 {
2866 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
2867
2868 // XXX: unreference resources/surfaces.
2869 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
2870 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
2871 }
2872 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
2873
2874 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
2875 pipe_resource_reference(&ice->state.sampler_table_resource[stage], NULL);
2876 }
2877 free(ice->state.cso_vp);
2878 free(ice->state.cso_depthbuffer);
2879
2880 pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
2881 pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
2882 pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
2883 pipe_resource_reference(&ice->state.last_res.scissor, NULL);
2884 pipe_resource_reference(&ice->state.last_res.blend, NULL);
2885 }
2886
2887 static unsigned
2888 flags_to_post_sync_op(uint32_t flags)
2889 {
2890 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
2891 return WriteImmediateData;
2892
2893 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
2894 return WritePSDepthCount;
2895
2896 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
2897 return WriteTimestamp;
2898
2899 return 0;
2900 }
2901
2902 /**
2903 * Do the given flags have a Post Sync or LRI Post Sync operation?
2904 */
2905 static enum pipe_control_flags
2906 get_post_sync_flags(enum pipe_control_flags flags)
2907 {
2908 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
2909 PIPE_CONTROL_WRITE_DEPTH_COUNT |
2910 PIPE_CONTROL_WRITE_TIMESTAMP |
2911 PIPE_CONTROL_LRI_POST_SYNC_OP;
2912
2913 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
2914 * "LRI Post Sync Operation". So more than one bit set would be illegal.
2915 */
2916 assert(util_bitcount(flags) <= 1);
2917
2918 return flags;
2919 }
2920
2921 // XXX: compute support
2922 #define IS_COMPUTE_PIPELINE(batch) (batch->ring != I915_EXEC_RENDER)
2923
2924 /**
2925 * Emit a series of PIPE_CONTROL commands, taking into account any
2926 * workarounds necessary to actually accomplish the caller's request.
2927 *
2928 * Unless otherwise noted, spec quotations in this function come from:
2929 *
2930 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
2931 * Restrictions for PIPE_CONTROL.
2932 */
2933 static void
2934 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
2935 struct iris_bo *bo, uint32_t offset, uint64_t imm)
2936 {
2937 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
2938 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
2939 enum pipe_control_flags non_lri_post_sync_flags =
2940 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
2941
2942 /* Recursive PIPE_CONTROL workarounds --------------------------------
2943 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
2944 *
2945 * We do these first because we want to look at the original operation,
2946 * rather than any workarounds we set.
2947 */
2948 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
2949 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
2950 * lists several workarounds:
2951 *
2952 * "Project: SKL, KBL, BXT
2953 *
2954 * If the VF Cache Invalidation Enable is set to a 1 in a
2955 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
2956 * sets to 0, with the VF Cache Invalidation Enable set to 0
2957 * needs to be sent prior to the PIPE_CONTROL with VF Cache
2958 * Invalidation Enable set to a 1."
2959 */
2960 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
2961 }
2962
2963 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
2964 /* Project: SKL / Argument: LRI Post Sync Operation [23]
2965 *
2966 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
2967 * programmed prior to programming a PIPECONTROL command with "LRI
2968 * Post Sync Operation" in GPGPU mode of operation (i.e when
2969 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
2970 *
2971 * The same text exists a few rows below for Post Sync Op.
2972 */
2973 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
2974 }
2975
2976 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
2977 /* Cannonlake:
2978 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
2979 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
2980 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
2981 */
2982 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
2983 offset, imm);
2984 }
2985
2986 /* "Flush Types" workarounds ---------------------------------------------
2987 * We do these now because they may add post-sync operations or CS stalls.
2988 */
2989
2990 if (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
2991 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
2992 *
2993 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
2994 * 'Write PS Depth Count' or 'Write Timestamp'."
2995 */
2996 if (!bo) {
2997 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2998 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2999 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
3000 bo = batch->screen->workaround_bo;
3001 }
3002 }
3003
3004 /* #1130 from Gen10 workarounds page:
3005 *
3006 * "Enable Depth Stall on every Post Sync Op if Render target Cache
3007 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
3008 * board stall if Render target cache flush is enabled."
3009 *
3010 * Applicable to CNL B0 and C0 steppings only.
3011 *
3012 * The wording here is unclear, and this workaround doesn't look anything
3013 * like the internal bug report recommendations, but leave it be for now...
3014 */
3015 if (GEN_GEN == 10) {
3016 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
3017 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3018 } else if (flags & non_lri_post_sync_flags) {
3019 flags |= PIPE_CONTROL_DEPTH_STALL;
3020 }
3021 }
3022
3023 if (flags & PIPE_CONTROL_DEPTH_STALL) {
3024 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
3025 *
3026 * "This bit must be DISABLED for operations other than writing
3027 * PS_DEPTH_COUNT."
3028 *
3029 * This seems like nonsense. An Ivybridge workaround requires us to
3030 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
3031 * operation. Gen8+ requires us to emit depth stalls and depth cache
3032 * flushes together. So, it's hard to imagine this means anything other
3033 * than "we originally intended this to be used for PS_DEPTH_COUNT".
3034 *
3035 * We ignore the supposed restriction and do nothing.
3036 */
3037 }
3038
3039 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
3040 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
3041 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
3042 *
3043 * "This bit must be DISABLED for End-of-pipe (Read) fences,
3044 * PS_DEPTH_COUNT or TIMESTAMP queries."
3045 *
3046 * TODO: Implement end-of-pipe checking.
3047 */
3048 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
3049 PIPE_CONTROL_WRITE_TIMESTAMP)));
3050 }
3051
3052 if (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD) {
3053 /* From the PIPE_CONTROL instruction table, bit 1:
3054 *
3055 * "This bit is ignored if Depth Stall Enable is set.
3056 * Further, the render cache is not flushed even if Write Cache
3057 * Flush Enable bit is set."
3058 *
3059 * We assert that the caller doesn't do this combination, to try and
3060 * prevent mistakes. It shouldn't hurt the GPU, though.
3061 */
3062 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
3063 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
3064 }
3065
3066 /* PIPE_CONTROL page workarounds ------------------------------------- */
3067
3068 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
3069 /* From the PIPE_CONTROL page itself:
3070 *
3071 * "IVB, HSW, BDW
3072 * Restriction: Pipe_control with CS-stall bit set must be issued
3073 * before a pipe-control command that has the State Cache
3074 * Invalidate bit set."
3075 */
3076 flags |= PIPE_CONTROL_CS_STALL;
3077 }
3078
3079 if (flags & PIPE_CONTROL_FLUSH_LLC) {
3080 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
3081 *
3082 * "Project: ALL
3083 * SW must always program Post-Sync Operation to "Write Immediate
3084 * Data" when Flush LLC is set."
3085 *
3086 * For now, we just require the caller to do it.
3087 */
3088 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
3089 }
3090
3091 /* "Post-Sync Operation" workarounds -------------------------------- */
3092
3093 /* Project: All / Argument: Global Snapshot Count Reset [19]
3094 *
3095 * "This bit must not be exercised on any product.
3096 * Requires stall bit ([20] of DW1) set."
3097 *
3098 * We don't use this, so we just assert that it isn't used. The
3099 * PIPE_CONTROL instruction page indicates that they intended this
3100 * as a debug feature and don't think it is useful in production,
3101 * but it may actually be usable, should we ever want to.
3102 */
3103 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
3104
3105 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
3106 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
3107 /* Project: All / Arguments:
3108 *
3109 * - Generic Media State Clear [16]
3110 * - Indirect State Pointers Disable [16]
3111 *
3112 * "Requires stall bit ([20] of DW1) set."
3113 *
3114 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
3115 * State Clear) says:
3116 *
3117 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
3118 * programmed prior to programming a PIPECONTROL command with "Media
3119 * State Clear" set in GPGPU mode of operation"
3120 *
3121 * This is a subset of the earlier rule, so there's nothing to do.
3122 */
3123 flags |= PIPE_CONTROL_CS_STALL;
3124 }
3125
3126 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
3127 /* Project: All / Argument: Store Data Index
3128 *
3129 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3130 * than '0'."
3131 *
3132 * For now, we just assert that the caller does this. We might want to
3133 * automatically add a write to the workaround BO...
3134 */
3135 assert(non_lri_post_sync_flags != 0);
3136 }
3137
3138 if (flags & PIPE_CONTROL_SYNC_GFDT) {
3139 /* Project: All / Argument: Sync GFDT
3140 *
3141 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3142 * than '0' or 0x2520[13] must be set."
3143 *
3144 * For now, we just assert that the caller does this.
3145 */
3146 assert(non_lri_post_sync_flags != 0);
3147 }
3148
3149 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
3150 /* Project: IVB+ / Argument: TLB inv
3151 *
3152 * "Requires stall bit ([20] of DW1) set."
3153 *
3154 * Also, from the PIPE_CONTROL instruction table:
3155 *
3156 * "Project: SKL+
3157 * Post Sync Operation or CS stall must be set to ensure a TLB
3158 * invalidation occurs. Otherwise no cycle will occur to the TLB
3159 * cache to invalidate."
3160 *
3161 * This is not a subset of the earlier rule, so there's nothing to do.
3162 */
3163 flags |= PIPE_CONTROL_CS_STALL;
3164 }
3165
3166 if (GEN_GEN == 9 && devinfo->gt == 4) {
3167 /* TODO: The big Skylake GT4 post sync op workaround */
3168 }
3169
3170 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
3171
3172 if (IS_COMPUTE_PIPELINE(batch)) {
3173 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
3174 /* Project: SKL+ / Argument: Tex Invalidate
3175 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
3176 */
3177 flags |= PIPE_CONTROL_CS_STALL;
3178 }
3179
3180 if (GEN_GEN == 8 && (post_sync_flags ||
3181 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
3182 PIPE_CONTROL_DEPTH_STALL |
3183 PIPE_CONTROL_RENDER_TARGET_FLUSH |
3184 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3185 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
3186 /* Project: BDW / Arguments:
3187 *
3188 * - LRI Post Sync Operation [23]
3189 * - Post Sync Op [15:14]
3190 * - Notify En [8]
3191 * - Depth Stall [13]
3192 * - Render Target Cache Flush [12]
3193 * - Depth Cache Flush [0]
3194 * - DC Flush Enable [5]
3195 *
3196 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
3197 * Workloads."
3198 */
3199 flags |= PIPE_CONTROL_CS_STALL;
3200
3201 /* Also, from the PIPE_CONTROL instruction table, bit 20:
3202 *
3203 * "Project: BDW
3204 * This bit must be always set when PIPE_CONTROL command is
3205 * programmed by GPGPU and MEDIA workloads, except for the cases
3206 * when only Read Only Cache Invalidation bits are set (State
3207 * Cache Invalidation Enable, Instruction cache Invalidation
3208 * Enable, Texture Cache Invalidation Enable, Constant Cache
3209 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
3210 * need not implemented when FF_DOP_CG is disable via "Fixed
3211 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
3212 *
3213 * It sounds like we could avoid CS stalls in some cases, but we
3214 * don't currently bother. This list isn't exactly the list above,
3215 * either...
3216 */
3217 }
3218 }
3219
3220 /* "Stall" workarounds ----------------------------------------------
3221 * These have to come after the earlier ones because we may have added
3222 * some additional CS stalls above.
3223 */
3224
3225 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
3226 /* Project: PRE-SKL, VLV, CHV
3227 *
3228 * "[All Stepping][All SKUs]:
3229 *
3230 * One of the following must also be set:
3231 *
3232 * - Render Target Cache Flush Enable ([12] of DW1)
3233 * - Depth Cache Flush Enable ([0] of DW1)
3234 * - Stall at Pixel Scoreboard ([1] of DW1)
3235 * - Depth Stall ([13] of DW1)
3236 * - Post-Sync Operation ([13] of DW1)
3237 * - DC Flush Enable ([5] of DW1)"
3238 *
3239 * If we don't already have one of those bits set, we choose to add
3240 * "Stall at Pixel Scoreboard". Some of the other bits require a
3241 * CS stall as a workaround (see above), which would send us into
3242 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
3243 * appears to be safe, so we choose that.
3244 */
3245 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
3246 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3247 PIPE_CONTROL_WRITE_IMMEDIATE |
3248 PIPE_CONTROL_WRITE_DEPTH_COUNT |
3249 PIPE_CONTROL_WRITE_TIMESTAMP |
3250 PIPE_CONTROL_STALL_AT_SCOREBOARD |
3251 PIPE_CONTROL_DEPTH_STALL |
3252 PIPE_CONTROL_DATA_CACHE_FLUSH;
3253 if (!(flags & wa_bits))
3254 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3255 }
3256
3257 /* Emit --------------------------------------------------------------- */
3258
3259 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
3260 pc.LRIPostSyncOperation = NoLRIOperation;
3261 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
3262 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
3263 pc.StoreDataIndex = 0;
3264 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
3265 pc.GlobalSnapshotCountReset =
3266 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
3267 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
3268 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
3269 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
3270 pc.RenderTargetCacheFlushEnable =
3271 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
3272 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
3273 pc.StateCacheInvalidationEnable =
3274 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3275 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
3276 pc.ConstantCacheInvalidationEnable =
3277 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
3278 pc.PostSyncOperation = flags_to_post_sync_op(flags);
3279 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
3280 pc.InstructionCacheInvalidateEnable =
3281 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
3282 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
3283 pc.IndirectStatePointersDisable =
3284 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
3285 pc.TextureCacheInvalidationEnable =
3286 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
3287 pc.Address = ro_bo(bo, offset);
3288 pc.ImmediateData = imm;
3289 }
3290 }
3291
3292 void
3293 genX(init_state)(struct iris_context *ice)
3294 {
3295 struct pipe_context *ctx = &ice->ctx;
3296
3297 ctx->create_blend_state = iris_create_blend_state;
3298 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
3299 ctx->create_rasterizer_state = iris_create_rasterizer_state;
3300 ctx->create_sampler_state = iris_create_sampler_state;
3301 ctx->create_sampler_view = iris_create_sampler_view;
3302 ctx->create_surface = iris_create_surface;
3303 ctx->create_vertex_elements_state = iris_create_vertex_elements;
3304 ctx->create_compute_state = iris_create_compute_state;
3305 ctx->bind_blend_state = iris_bind_blend_state;
3306 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
3307 ctx->bind_sampler_states = iris_bind_sampler_states;
3308 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
3309 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
3310 ctx->bind_compute_state = iris_bind_compute_state;
3311 ctx->delete_blend_state = iris_delete_state;
3312 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
3313 ctx->delete_fs_state = iris_delete_state;
3314 ctx->delete_rasterizer_state = iris_delete_state;
3315 ctx->delete_sampler_state = iris_delete_state;
3316 ctx->delete_vertex_elements_state = iris_delete_state;
3317 ctx->delete_compute_state = iris_delete_state;
3318 ctx->delete_tcs_state = iris_delete_state;
3319 ctx->delete_tes_state = iris_delete_state;
3320 ctx->delete_gs_state = iris_delete_state;
3321 ctx->delete_vs_state = iris_delete_state;
3322 ctx->set_blend_color = iris_set_blend_color;
3323 ctx->set_clip_state = iris_set_clip_state;
3324 ctx->set_constant_buffer = iris_set_constant_buffer;
3325 ctx->set_sampler_views = iris_set_sampler_views;
3326 ctx->set_framebuffer_state = iris_set_framebuffer_state;
3327 ctx->set_polygon_stipple = iris_set_polygon_stipple;
3328 ctx->set_sample_mask = iris_set_sample_mask;
3329 ctx->set_scissor_states = iris_set_scissor_states;
3330 ctx->set_stencil_ref = iris_set_stencil_ref;
3331 ctx->set_vertex_buffers = iris_set_vertex_buffers;
3332 ctx->set_viewport_states = iris_set_viewport_states;
3333 ctx->sampler_view_destroy = iris_sampler_view_destroy;
3334 ctx->surface_destroy = iris_surface_destroy;
3335 ctx->draw_vbo = iris_draw_vbo;
3336 ctx->launch_grid = iris_launch_grid;
3337 ctx->create_stream_output_target = iris_create_stream_output_target;
3338 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
3339 ctx->set_stream_output_targets = iris_set_stream_output_targets;
3340
3341 ice->vtbl.destroy_state = iris_destroy_state;
3342 ice->vtbl.init_render_context = iris_init_render_context;
3343 ice->vtbl.upload_render_state = iris_upload_render_state;
3344 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
3345 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
3346 ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
3347 ice->vtbl.populate_vs_key = iris_populate_vs_key;
3348 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
3349 ice->vtbl.populate_tes_key = iris_populate_tes_key;
3350 ice->vtbl.populate_gs_key = iris_populate_gs_key;
3351 ice->vtbl.populate_fs_key = iris_populate_fs_key;
3352
3353 ice->state.dirty = ~0ull;
3354
3355 ice->state.num_viewports = 1;
3356 ice->state.cso_vp = calloc(1, sizeof(struct iris_viewport_state));
3357 ice->state.cso_vertex_buffers =
3358 calloc(1, sizeof(struct iris_vertex_buffer_state));
3359 }