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