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