iris: don't leak sampler state table resources
[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 num_viewports,
1231 const struct pipe_viewport_state *state)
1232 {
1233 struct iris_context *ice = (struct iris_context *) ctx;
1234 struct iris_viewport_state *cso =
1235 malloc(sizeof(struct iris_viewport_state));
1236 uint32_t *vp_map = &cso->sf_cl_vp[start_slot];
1237
1238 // XXX: sf_cl_vp is only big enough for one slot, we don't iterate right
1239 for (unsigned i = 0; i < num_viewports; i++) {
1240 iris_pack_state(GENX(SF_CLIP_VIEWPORT), vp_map, vp) {
1241 vp.ViewportMatrixElementm00 = state[i].scale[0];
1242 vp.ViewportMatrixElementm11 = state[i].scale[1];
1243 vp.ViewportMatrixElementm22 = state[i].scale[2];
1244 vp.ViewportMatrixElementm30 = state[i].translate[0];
1245 vp.ViewportMatrixElementm31 = state[i].translate[1];
1246 vp.ViewportMatrixElementm32 = state[i].translate[2];
1247 /* XXX: in i965 this is computed based on the drawbuffer size,
1248 * but we don't have that here...
1249 */
1250 vp.XMinClipGuardband = -1.0;
1251 vp.XMaxClipGuardband = 1.0;
1252 vp.YMinClipGuardband = -1.0;
1253 vp.YMaxClipGuardband = 1.0;
1254 vp.XMinViewPort = viewport_extent(&state[i], 0, -1.0f);
1255 vp.XMaxViewPort = viewport_extent(&state[i], 0, 1.0f) - 1;
1256 vp.YMinViewPort = viewport_extent(&state[i], 1, -1.0f);
1257 vp.YMaxViewPort = viewport_extent(&state[i], 1, 1.0f) - 1;
1258 }
1259
1260 vp_map += GENX(SF_CLIP_VIEWPORT_length);
1261 }
1262
1263 free(ice->state.cso_vp);
1264 ice->state.cso_vp = cso;
1265 ice->state.num_viewports = num_viewports;
1266 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
1267 }
1268
1269 struct iris_depth_buffer_state
1270 {
1271 uint32_t packets[GENX(3DSTATE_DEPTH_BUFFER_length) +
1272 GENX(3DSTATE_STENCIL_BUFFER_length) +
1273 GENX(3DSTATE_HIER_DEPTH_BUFFER_length) +
1274 GENX(3DSTATE_CLEAR_PARAMS_length)];
1275 };
1276
1277 static void
1278 iris_set_framebuffer_state(struct pipe_context *ctx,
1279 const struct pipe_framebuffer_state *state)
1280 {
1281 struct iris_context *ice = (struct iris_context *) ctx;
1282 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1283 struct isl_device *isl_dev = &screen->isl_dev;
1284 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
1285
1286 if (cso->samples != state->samples) {
1287 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1288 }
1289
1290 if (cso->nr_cbufs != state->nr_cbufs) {
1291 ice->state.dirty |= IRIS_DIRTY_BLEND_STATE;
1292 }
1293
1294 if ((cso->layers == 0) == (state->layers == 0)) {
1295 ice->state.dirty |= IRIS_DIRTY_CLIP;
1296 }
1297
1298 util_copy_framebuffer_state(cso, state);
1299
1300 struct iris_depth_buffer_state *cso_z =
1301 malloc(sizeof(struct iris_depth_buffer_state));
1302
1303 struct isl_view view = {
1304 .base_level = 0,
1305 .levels = 1,
1306 .base_array_layer = 0,
1307 .array_len = 1,
1308 .swizzle = ISL_SWIZZLE_IDENTITY,
1309 };
1310
1311 struct isl_depth_stencil_hiz_emit_info info = {
1312 .view = &view,
1313 .mocs = MOCS_WB,
1314 };
1315
1316 struct iris_resource *zres =
1317 (void *) (cso->zsbuf ? cso->zsbuf->texture : NULL);
1318
1319 if (zres) {
1320 view.usage |= ISL_SURF_USAGE_DEPTH_BIT;
1321
1322 info.depth_surf = &zres->surf;
1323 info.depth_address = zres->bo->gtt_offset;
1324
1325 view.format = zres->surf.format;
1326
1327 view.base_level = cso->zsbuf->u.tex.level;
1328 view.base_array_layer = cso->zsbuf->u.tex.first_layer;
1329 view.array_len =
1330 cso->zsbuf->u.tex.last_layer - cso->zsbuf->u.tex.first_layer + 1;
1331
1332 info.hiz_usage = ISL_AUX_USAGE_NONE;
1333 }
1334
1335 #if 0
1336 if (stencil_mt) {
1337 view.usage |= ISL_SURF_USAGE_STENCIL_BIT;
1338 info.stencil_surf = &stencil_mt->surf;
1339
1340 if (!depth_mt) {
1341 view.base_level = stencil_irb->mt_level - stencil_irb->mt->first_level;
1342 view.base_array_layer = stencil_irb->mt_layer;
1343 view.array_len = MAX2(stencil_irb->layer_count, 1);
1344 view.format = stencil_mt->surf.format;
1345 }
1346
1347 uint32_t stencil_offset = 0;
1348 info.stencil_address = stencil_mt->bo->gtt_offset + stencil_mt->offset;
1349 }
1350 #endif
1351
1352 isl_emit_depth_stencil_hiz_s(isl_dev, cso_z->packets, &info);
1353
1354 free(ice->state.cso_depthbuffer);
1355 ice->state.cso_depthbuffer = cso_z;
1356 ice->state.dirty |= IRIS_DIRTY_DEPTH_BUFFER;
1357
1358 /* Render target change */
1359 ice->state.dirty |= IRIS_DIRTY_BINDINGS_FS;
1360 }
1361
1362 static void
1363 iris_set_constant_buffer(struct pipe_context *ctx,
1364 enum pipe_shader_type p_stage, unsigned index,
1365 const struct pipe_constant_buffer *input)
1366 {
1367 struct iris_context *ice = (struct iris_context *) ctx;
1368 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
1369 gl_shader_stage stage = stage_from_pipe(p_stage);
1370 struct iris_shader_state *shs = &ice->shaders.state[stage];
1371 struct iris_const_buffer *cbuf = &shs->constbuf[index];
1372
1373 if (input && (input->buffer || input->user_buffer)) {
1374 if (input->user_buffer) {
1375 u_upload_data(ctx->const_uploader, 0, input->buffer_size, 32,
1376 input->user_buffer, &cbuf->offset, &cbuf->resource);
1377 } else {
1378 pipe_resource_reference(&cbuf->resource, input->buffer);
1379 }
1380
1381 void *map = NULL;
1382 // XXX: these are not retained forever, use a separate uploader?
1383 u_upload_alloc(ice->state.surface_uploader, 0,
1384 4 * GENX(RENDER_SURFACE_STATE_length), 64,
1385 &cbuf->surface_state_offset,
1386 &cbuf->surface_state_resource,
1387 &map);
1388 if (!unlikely(map)) {
1389 pipe_resource_reference(&cbuf->resource, NULL);
1390 return;
1391 }
1392
1393 struct iris_resource *res = (void *) cbuf->resource;
1394 struct iris_bo *surf_bo = iris_resource_bo(cbuf->surface_state_resource);
1395 cbuf->surface_state_offset += iris_bo_offset_from_base_address(surf_bo);
1396
1397 isl_buffer_fill_state(&screen->isl_dev, map,
1398 .address = res->bo->gtt_offset + cbuf->offset,
1399 .size_B = input->buffer_size,
1400 .format = ISL_FORMAT_R32G32B32A32_FLOAT,
1401 .stride_B = 1,
1402 .mocs = MOCS_WB)
1403 } else {
1404 pipe_resource_reference(&cbuf->resource, NULL);
1405 pipe_resource_reference(&cbuf->surface_state_resource, NULL);
1406 }
1407
1408 ice->state.dirty |= IRIS_DIRTY_CONSTANTS_VS << stage;
1409 // XXX: maybe not necessary all the time...?
1410 ice->state.dirty |= IRIS_DIRTY_BINDINGS_VS << stage;
1411 }
1412
1413 static void
1414 iris_sampler_view_destroy(struct pipe_context *ctx,
1415 struct pipe_sampler_view *state)
1416 {
1417 struct iris_sampler_view *isv = (void *) state;
1418 pipe_resource_reference(&state->texture, NULL);
1419 pipe_resource_reference(&isv->surface_state_resource, NULL);
1420 free(isv);
1421 }
1422
1423
1424 static void
1425 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *p_surf)
1426 {
1427 struct iris_surface *surf = (void *) p_surf;
1428 pipe_resource_reference(&p_surf->texture, NULL);
1429 pipe_resource_reference(&surf->surface_state_resource, NULL);
1430 free(surf);
1431 }
1432
1433 static void
1434 iris_delete_state(struct pipe_context *ctx, void *state)
1435 {
1436 free(state);
1437 }
1438
1439 struct iris_vertex_buffer_state {
1440 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
1441 struct iris_bo *bos[33];
1442 unsigned num_buffers;
1443 };
1444
1445 static void
1446 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
1447 {
1448 if (cso) {
1449 for (unsigned i = 0; i < cso->num_buffers; i++)
1450 iris_bo_unreference(cso->bos[i]);
1451 free(cso);
1452 }
1453 }
1454
1455 static void
1456 iris_set_vertex_buffers(struct pipe_context *ctx,
1457 unsigned start_slot, unsigned count,
1458 const struct pipe_vertex_buffer *buffers)
1459 {
1460 struct iris_context *ice = (struct iris_context *) ctx;
1461
1462 /* If there are no buffers, do nothing. We can leave the stale
1463 * 3DSTATE_VERTEX_BUFFERS in place - as long as there are no vertex
1464 * elements that point to them, it should be fine.
1465 */
1466 if (!buffers)
1467 return;
1468
1469 struct iris_vertex_buffer_state *cso =
1470 malloc(sizeof(struct iris_vertex_buffer_state));
1471
1472 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
1473
1474 cso->num_buffers = count;
1475
1476 iris_pack_command(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
1477 vb.DWordLength = 4 * cso->num_buffers - 1;
1478 }
1479
1480 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
1481
1482 for (unsigned i = 0; i < count; i++) {
1483 assert(!buffers[i].is_user_buffer);
1484
1485 struct iris_resource *res = (void *) buffers[i].buffer.resource;
1486 iris_bo_reference(res->bo);
1487 cso->bos[i] = res->bo;
1488
1489 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1490 vb.VertexBufferIndex = start_slot + i;
1491 vb.MOCS = MOCS_WB;
1492 vb.AddressModifyEnable = true;
1493 vb.BufferPitch = buffers[i].stride;
1494 vb.BufferSize = res->bo->size;
1495 vb.BufferStartingAddress =
1496 ro_bo(NULL, res->bo->gtt_offset + buffers[i].buffer_offset);
1497 }
1498
1499 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
1500 }
1501
1502 ice->state.cso_vertex_buffers = cso;
1503 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
1504 }
1505
1506 struct iris_vertex_element_state {
1507 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
1508 uint32_t vf_instancing[33 * GENX(3DSTATE_VF_INSTANCING_length)];
1509 unsigned count;
1510 };
1511
1512 static void *
1513 iris_create_vertex_elements(struct pipe_context *ctx,
1514 unsigned count,
1515 const struct pipe_vertex_element *state)
1516 {
1517 struct iris_vertex_element_state *cso =
1518 malloc(sizeof(struct iris_vertex_element_state));
1519
1520 cso->count = count;
1521
1522 /* TODO:
1523 * - create edge flag one
1524 * - create SGV ones
1525 * - if those are necessary, use count + 1/2/3... OR in the length
1526 */
1527 iris_pack_command(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve) {
1528 ve.DWordLength =
1529 1 + GENX(VERTEX_ELEMENT_STATE_length) * MAX2(count, 1) - 2;
1530 }
1531
1532 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
1533 uint32_t *vfi_pack_dest = cso->vf_instancing;
1534
1535 for (int i = 0; i < count; i++) {
1536 enum isl_format isl_format =
1537 iris_isl_format_for_pipe_format(state[i].src_format);
1538 unsigned comp[4] = { VFCOMP_STORE_SRC, VFCOMP_STORE_SRC,
1539 VFCOMP_STORE_SRC, VFCOMP_STORE_SRC };
1540
1541 switch (isl_format_get_num_channels(isl_format)) {
1542 case 0: comp[0] = VFCOMP_STORE_0;
1543 case 1: comp[1] = VFCOMP_STORE_0;
1544 case 2: comp[2] = VFCOMP_STORE_0;
1545 case 3:
1546 comp[3] = isl_format_has_int_channel(isl_format) ? VFCOMP_STORE_1_INT
1547 : VFCOMP_STORE_1_FP;
1548 break;
1549 }
1550 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1551 ve.VertexBufferIndex = state[i].vertex_buffer_index;
1552 ve.Valid = true;
1553 ve.SourceElementOffset = state[i].src_offset;
1554 ve.SourceElementFormat = isl_format;
1555 ve.Component0Control = comp[0];
1556 ve.Component1Control = comp[1];
1557 ve.Component2Control = comp[2];
1558 ve.Component3Control = comp[3];
1559 }
1560
1561 iris_pack_command(GENX(3DSTATE_VF_INSTANCING), vfi_pack_dest, vi) {
1562 vi.VertexElementIndex = i;
1563 vi.InstancingEnable = state[i].instance_divisor > 0;
1564 vi.InstanceDataStepRate = state[i].instance_divisor;
1565 }
1566
1567 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
1568 vfi_pack_dest += GENX(3DSTATE_VF_INSTANCING_length);
1569 }
1570
1571 return cso;
1572 }
1573
1574 static void
1575 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
1576 {
1577 struct iris_context *ice = (struct iris_context *) ctx;
1578
1579 ice->state.cso_vertex_elements = state;
1580 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
1581 }
1582
1583 static void *
1584 iris_create_compute_state(struct pipe_context *ctx,
1585 const struct pipe_compute_state *state)
1586 {
1587 return malloc(1);
1588 }
1589
1590 static struct pipe_stream_output_target *
1591 iris_create_stream_output_target(struct pipe_context *ctx,
1592 struct pipe_resource *res,
1593 unsigned buffer_offset,
1594 unsigned buffer_size)
1595 {
1596 struct pipe_stream_output_target *t =
1597 CALLOC_STRUCT(pipe_stream_output_target);
1598 if (!t)
1599 return NULL;
1600
1601 pipe_reference_init(&t->reference, 1);
1602 pipe_resource_reference(&t->buffer, res);
1603 t->buffer_offset = buffer_offset;
1604 t->buffer_size = buffer_size;
1605 return t;
1606 }
1607
1608 static void
1609 iris_stream_output_target_destroy(struct pipe_context *ctx,
1610 struct pipe_stream_output_target *t)
1611 {
1612 pipe_resource_reference(&t->buffer, NULL);
1613 free(t);
1614 }
1615
1616 static void
1617 iris_set_stream_output_targets(struct pipe_context *ctx,
1618 unsigned num_targets,
1619 struct pipe_stream_output_target **targets,
1620 const unsigned *offsets)
1621 {
1622 }
1623
1624 static void
1625 iris_compute_sbe_urb_read_interval(uint64_t fs_input_slots,
1626 const struct brw_vue_map *last_vue_map,
1627 bool two_sided_color,
1628 unsigned *out_offset,
1629 unsigned *out_length)
1630 {
1631 /* The compiler computes the first URB slot without considering COL/BFC
1632 * swizzling (because it doesn't know whether it's enabled), so we need
1633 * to do that here too. This may result in a smaller offset, which
1634 * should be safe.
1635 */
1636 const unsigned first_slot =
1637 brw_compute_first_urb_slot_required(fs_input_slots, last_vue_map);
1638
1639 /* This becomes the URB read offset (counted in pairs of slots). */
1640 assert(first_slot % 2 == 0);
1641 *out_offset = first_slot / 2;
1642
1643 /* We need to adjust the inputs read to account for front/back color
1644 * swizzling, as it can make the URB length longer.
1645 */
1646 for (int c = 0; c <= 1; c++) {
1647 if (fs_input_slots & (VARYING_BIT_COL0 << c)) {
1648 /* If two sided color is enabled, the fragment shader's gl_Color
1649 * (COL0) input comes from either the gl_FrontColor (COL0) or
1650 * gl_BackColor (BFC0) input varyings. Mark BFC as used, too.
1651 */
1652 if (two_sided_color)
1653 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1654
1655 /* If front color isn't written, we opt to give them back color
1656 * instead of an undefined value. Switch from COL to BFC.
1657 */
1658 if (last_vue_map->varying_to_slot[VARYING_SLOT_COL0 + c] == -1) {
1659 fs_input_slots &= ~(VARYING_BIT_COL0 << c);
1660 fs_input_slots |= (VARYING_BIT_BFC0 << c);
1661 }
1662 }
1663 }
1664
1665 /* Compute the minimum URB Read Length necessary for the FS inputs.
1666 *
1667 * From the Sandy Bridge PRM, Volume 2, Part 1, documentation for
1668 * 3DSTATE_SF DWord 1 bits 15:11, "Vertex URB Entry Read Length":
1669 *
1670 * "This field should be set to the minimum length required to read the
1671 * maximum source attribute. The maximum source attribute is indicated
1672 * by the maximum value of the enabled Attribute # Source Attribute if
1673 * Attribute Swizzle Enable is set, Number of Output Attributes-1 if
1674 * enable is not set.
1675 * read_length = ceiling((max_source_attr + 1) / 2)
1676 *
1677 * [errata] Corruption/Hang possible if length programmed larger than
1678 * recommended"
1679 *
1680 * Similar text exists for Ivy Bridge.
1681 *
1682 * We find the last URB slot that's actually read by the FS.
1683 */
1684 unsigned last_read_slot = last_vue_map->num_slots - 1;
1685 while (last_read_slot > first_slot && !(fs_input_slots &
1686 (1ull << last_vue_map->slot_to_varying[last_read_slot])))
1687 --last_read_slot;
1688
1689 /* The URB read length is the difference of the two, counted in pairs. */
1690 *out_length = DIV_ROUND_UP(last_read_slot - first_slot + 1, 2);
1691 }
1692
1693 static void
1694 iris_emit_sbe(struct iris_batch *batch, const struct iris_context *ice)
1695 {
1696 const struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
1697 const struct brw_wm_prog_data *wm_prog_data = (void *)
1698 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
1699 struct pipe_shader_state *p_fs =
1700 (void *) ice->shaders.uncompiled[MESA_SHADER_FRAGMENT];
1701 assert(p_fs->type == PIPE_SHADER_IR_NIR);
1702 nir_shader *fs_nir = p_fs->ir.nir;
1703
1704 unsigned urb_read_offset, urb_read_length;
1705 iris_compute_sbe_urb_read_interval(fs_nir->info.inputs_read,
1706 ice->shaders.last_vue_map,
1707 cso_rast->light_twoside,
1708 &urb_read_offset, &urb_read_length);
1709
1710 iris_emit_cmd(batch, GENX(3DSTATE_SBE), sbe) {
1711 sbe.AttributeSwizzleEnable = true;
1712 sbe.NumberofSFOutputAttributes = wm_prog_data->num_varying_inputs;
1713 sbe.PointSpriteTextureCoordinateOrigin = cso_rast->sprite_coord_mode;
1714 sbe.VertexURBEntryReadOffset = urb_read_offset;
1715 sbe.VertexURBEntryReadLength = urb_read_length;
1716 sbe.ForceVertexURBEntryReadOffset = true;
1717 sbe.ForceVertexURBEntryReadLength = true;
1718 sbe.ConstantInterpolationEnable = wm_prog_data->flat_inputs;
1719
1720 for (int i = 0; i < 32; i++) {
1721 sbe.AttributeActiveComponentFormat[i] = ACTIVE_COMPONENT_XYZW;
1722 }
1723 }
1724 }
1725
1726 static void
1727 iris_bind_compute_state(struct pipe_context *ctx, void *state)
1728 {
1729 }
1730
1731 static void
1732 iris_populate_sampler_key(const struct iris_context *ice,
1733 struct brw_sampler_prog_key_data *key)
1734 {
1735 for (int i = 0; i < MAX_SAMPLERS; i++) {
1736 key->swizzles[i] = 0x688; /* XYZW */
1737 }
1738 }
1739
1740 static void
1741 iris_populate_vs_key(const struct iris_context *ice,
1742 struct brw_vs_prog_key *key)
1743 {
1744 memset(key, 0, sizeof(*key));
1745 iris_populate_sampler_key(ice, &key->tex);
1746 }
1747
1748 static void
1749 iris_populate_tcs_key(const struct iris_context *ice,
1750 struct brw_tcs_prog_key *key)
1751 {
1752 memset(key, 0, sizeof(*key));
1753 iris_populate_sampler_key(ice, &key->tex);
1754 }
1755
1756 static void
1757 iris_populate_tes_key(const struct iris_context *ice,
1758 struct brw_tes_prog_key *key)
1759 {
1760 memset(key, 0, sizeof(*key));
1761 iris_populate_sampler_key(ice, &key->tex);
1762 }
1763
1764 static void
1765 iris_populate_gs_key(const struct iris_context *ice,
1766 struct brw_gs_prog_key *key)
1767 {
1768 memset(key, 0, sizeof(*key));
1769 iris_populate_sampler_key(ice, &key->tex);
1770 }
1771
1772 static void
1773 iris_populate_fs_key(const struct iris_context *ice,
1774 struct brw_wm_prog_key *key)
1775 {
1776 memset(key, 0, sizeof(*key));
1777 iris_populate_sampler_key(ice, &key->tex);
1778
1779 /* XXX: dirty flags? */
1780 const struct pipe_framebuffer_state *fb = &ice->state.framebuffer;
1781 const struct iris_depth_stencil_alpha_state *zsa = ice->state.cso_zsa;
1782 const struct iris_rasterizer_state *rast = ice->state.cso_rast;
1783 const struct iris_blend_state *blend = ice->state.cso_blend;
1784
1785 key->nr_color_regions = fb->nr_cbufs;
1786
1787 key->clamp_fragment_color = rast->clamp_fragment_color;
1788
1789 key->replicate_alpha = fb->nr_cbufs > 1 &&
1790 (zsa->alpha.enabled || blend->alpha_to_coverage);
1791
1792 // key->force_dual_color_blend for unigine
1793 #if 0
1794 if (cso_rast->multisample) {
1795 key->persample_interp =
1796 ctx->Multisample.SampleShading &&
1797 (ctx->Multisample.MinSampleShadingValue *
1798 _mesa_geometric_samples(ctx->DrawBuffer) > 1);
1799
1800 key->multisample_fbo = fb->samples > 1;
1801 }
1802 #endif
1803
1804 key->coherent_fb_fetch = true;
1805 }
1806
1807 //pkt.SamplerCount = \
1808 //DIV_ROUND_UP(CLAMP(stage_state->sampler_count, 0, 16), 4); \
1809 //pkt.PerThreadScratchSpace = prog_data->total_scratch == 0 ? 0 : \
1810 //ffs(stage_state->per_thread_scratch) - 11; \
1811
1812 static uint64_t
1813 KSP(const struct iris_compiled_shader *shader)
1814 {
1815 struct iris_resource *res = (void *) shader->buffer;
1816 return res->bo->gtt_offset + shader->offset;
1817 }
1818
1819 #define INIT_THREAD_DISPATCH_FIELDS(pkt, prefix) \
1820 pkt.KernelStartPointer = KSP(shader); \
1821 pkt.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4; \
1822 pkt.FloatingPointMode = prog_data->use_alt_mode; \
1823 \
1824 pkt.DispatchGRFStartRegisterForURBData = \
1825 prog_data->dispatch_grf_start_reg; \
1826 pkt.prefix##URBEntryReadLength = vue_prog_data->urb_read_length; \
1827 pkt.prefix##URBEntryReadOffset = 0; \
1828 \
1829 pkt.StatisticsEnable = true; \
1830 pkt.Enable = true;
1831
1832 static void
1833 iris_store_vs_state(const struct gen_device_info *devinfo,
1834 struct iris_compiled_shader *shader)
1835 {
1836 struct brw_stage_prog_data *prog_data = shader->prog_data;
1837 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1838
1839 iris_pack_command(GENX(3DSTATE_VS), shader->derived_data, vs) {
1840 INIT_THREAD_DISPATCH_FIELDS(vs, Vertex);
1841 vs.MaximumNumberofThreads = devinfo->max_vs_threads - 1;
1842 vs.SIMD8DispatchEnable = true;
1843 vs.UserClipDistanceCullTestEnableBitmask =
1844 vue_prog_data->cull_distance_mask;
1845 }
1846 }
1847
1848 static void
1849 iris_store_tcs_state(const struct gen_device_info *devinfo,
1850 struct iris_compiled_shader *shader)
1851 {
1852 struct brw_stage_prog_data *prog_data = shader->prog_data;
1853 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1854 struct brw_tcs_prog_data *tcs_prog_data = (void *) prog_data;
1855
1856 iris_pack_command(GENX(3DSTATE_HS), shader->derived_data, hs) {
1857 INIT_THREAD_DISPATCH_FIELDS(hs, Vertex);
1858
1859 hs.InstanceCount = tcs_prog_data->instances - 1;
1860 hs.MaximumNumberofThreads = devinfo->max_tcs_threads - 1;
1861 hs.IncludeVertexHandles = true;
1862 }
1863 }
1864
1865 static void
1866 iris_store_tes_state(const struct gen_device_info *devinfo,
1867 struct iris_compiled_shader *shader)
1868 {
1869 struct brw_stage_prog_data *prog_data = shader->prog_data;
1870 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1871 struct brw_tes_prog_data *tes_prog_data = (void *) prog_data;
1872
1873 uint32_t *te_state = (void *) shader->derived_data;
1874 uint32_t *ds_state = te_state + GENX(3DSTATE_TE_length);
1875
1876 iris_pack_command(GENX(3DSTATE_TE), te_state, te) {
1877 te.Partitioning = tes_prog_data->partitioning;
1878 te.OutputTopology = tes_prog_data->output_topology;
1879 te.TEDomain = tes_prog_data->domain;
1880 te.TEEnable = true;
1881 te.MaximumTessellationFactorOdd = 63.0;
1882 te.MaximumTessellationFactorNotOdd = 64.0;
1883 }
1884
1885 iris_pack_command(GENX(3DSTATE_DS), ds_state, ds) {
1886 INIT_THREAD_DISPATCH_FIELDS(ds, Patch);
1887
1888 ds.DispatchMode = DISPATCH_MODE_SIMD8_SINGLE_PATCH;
1889 ds.MaximumNumberofThreads = devinfo->max_tes_threads - 1;
1890 ds.ComputeWCoordinateEnable =
1891 tes_prog_data->domain == BRW_TESS_DOMAIN_TRI;
1892
1893 ds.UserClipDistanceCullTestEnableBitmask =
1894 vue_prog_data->cull_distance_mask;
1895 }
1896
1897 }
1898
1899 static void
1900 iris_store_gs_state(const struct gen_device_info *devinfo,
1901 struct iris_compiled_shader *shader)
1902 {
1903 struct brw_stage_prog_data *prog_data = shader->prog_data;
1904 struct brw_vue_prog_data *vue_prog_data = (void *) prog_data;
1905 struct brw_gs_prog_data *gs_prog_data = (void *) prog_data;
1906
1907 iris_pack_command(GENX(3DSTATE_GS), shader->derived_data, gs) {
1908 INIT_THREAD_DISPATCH_FIELDS(gs, Vertex);
1909
1910 gs.OutputVertexSize = gs_prog_data->output_vertex_size_hwords * 2 - 1;
1911 gs.OutputTopology = gs_prog_data->output_topology;
1912 gs.ControlDataHeaderSize =
1913 gs_prog_data->control_data_header_size_hwords;
1914 gs.InstanceControl = gs_prog_data->invocations - 1;
1915 gs.DispatchMode = SIMD8;
1916 gs.IncludePrimitiveID = gs_prog_data->include_primitive_id;
1917 gs.ControlDataFormat = gs_prog_data->control_data_format;
1918 gs.ReorderMode = TRAILING;
1919 gs.ExpectedVertexCount = gs_prog_data->vertices_in;
1920 gs.MaximumNumberofThreads =
1921 GEN_GEN == 8 ? (devinfo->max_gs_threads / 2 - 1)
1922 : (devinfo->max_gs_threads - 1);
1923
1924 if (gs_prog_data->static_vertex_count != -1) {
1925 gs.StaticOutput = true;
1926 gs.StaticOutputVertexCount = gs_prog_data->static_vertex_count;
1927 }
1928 gs.IncludeVertexHandles = vue_prog_data->include_vue_handles;
1929
1930 gs.UserClipDistanceCullTestEnableBitmask =
1931 vue_prog_data->cull_distance_mask;
1932
1933 const int urb_entry_write_offset = 1;
1934 const uint32_t urb_entry_output_length =
1935 DIV_ROUND_UP(vue_prog_data->vue_map.num_slots, 2) -
1936 urb_entry_write_offset;
1937
1938 gs.VertexURBEntryOutputReadOffset = urb_entry_write_offset;
1939 gs.VertexURBEntryOutputLength = MAX2(urb_entry_output_length, 1);
1940 }
1941 }
1942
1943 static void
1944 iris_store_fs_state(const struct gen_device_info *devinfo,
1945 struct iris_compiled_shader *shader)
1946 {
1947 struct brw_stage_prog_data *prog_data = shader->prog_data;
1948 struct brw_wm_prog_data *wm_prog_data = (void *) shader->prog_data;
1949
1950 uint32_t *ps_state = (void *) shader->derived_data;
1951 uint32_t *psx_state = ps_state + GENX(3DSTATE_PS_length);
1952
1953 iris_pack_command(GENX(3DSTATE_PS), ps_state, ps) {
1954 ps.VectorMaskEnable = true;
1955 //ps.SamplerCount = ...
1956 ps.BindingTableEntryCount = prog_data->binding_table.size_bytes / 4;
1957 ps.FloatingPointMode = prog_data->use_alt_mode;
1958 ps.MaximumNumberofThreadsPerPSD = 64 - (GEN_GEN == 8 ? 2 : 1);
1959
1960 ps.PushConstantEnable = prog_data->nr_params > 0 ||
1961 prog_data->ubo_ranges[0].length > 0;
1962
1963 /* From the documentation for this packet:
1964 * "If the PS kernel does not need the Position XY Offsets to
1965 * compute a Position Value, then this field should be programmed
1966 * to POSOFFSET_NONE."
1967 *
1968 * "SW Recommendation: If the PS kernel needs the Position Offsets
1969 * to compute a Position XY value, this field should match Position
1970 * ZW Interpolation Mode to ensure a consistent position.xyzw
1971 * computation."
1972 *
1973 * We only require XY sample offsets. So, this recommendation doesn't
1974 * look useful at the moment. We might need this in future.
1975 */
1976 ps.PositionXYOffsetSelect =
1977 wm_prog_data->uses_pos_offset ? POSOFFSET_SAMPLE : POSOFFSET_NONE;
1978 ps._8PixelDispatchEnable = wm_prog_data->dispatch_8;
1979 ps._16PixelDispatchEnable = wm_prog_data->dispatch_16;
1980 ps._32PixelDispatchEnable = wm_prog_data->dispatch_32;
1981
1982 // XXX: Disable SIMD32 with 16x MSAA
1983
1984 ps.DispatchGRFStartRegisterForConstantSetupData0 =
1985 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 0);
1986 ps.DispatchGRFStartRegisterForConstantSetupData1 =
1987 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 1);
1988 ps.DispatchGRFStartRegisterForConstantSetupData2 =
1989 brw_wm_prog_data_dispatch_grf_start_reg(wm_prog_data, ps, 2);
1990
1991 ps.KernelStartPointer0 =
1992 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 0);
1993 ps.KernelStartPointer1 =
1994 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 1);
1995 ps.KernelStartPointer2 =
1996 KSP(shader) + brw_wm_prog_data_prog_offset(wm_prog_data, ps, 2);
1997 }
1998
1999 iris_pack_command(GENX(3DSTATE_PS_EXTRA), psx_state, psx) {
2000 psx.PixelShaderValid = true;
2001 psx.PixelShaderComputedDepthMode = wm_prog_data->computed_depth_mode;
2002 psx.PixelShaderKillsPixel = wm_prog_data->uses_kill;
2003 psx.AttributeEnable = wm_prog_data->num_varying_inputs != 0;
2004 psx.PixelShaderUsesSourceDepth = wm_prog_data->uses_src_depth;
2005 psx.PixelShaderUsesSourceW = wm_prog_data->uses_src_w;
2006 psx.PixelShaderIsPerSample = wm_prog_data->persample_dispatch;
2007
2008 if (wm_prog_data->uses_sample_mask) {
2009 /* TODO: conservative rasterization */
2010 if (wm_prog_data->post_depth_coverage)
2011 psx.InputCoverageMaskState = ICMS_DEPTH_COVERAGE;
2012 else
2013 psx.InputCoverageMaskState = ICMS_NORMAL;
2014 }
2015
2016 psx.oMaskPresenttoRenderTarget = wm_prog_data->uses_omask;
2017 psx.PixelShaderPullsBary = wm_prog_data->pulls_bary;
2018 psx.PixelShaderComputesStencil = wm_prog_data->computed_stencil;
2019
2020 // XXX: UAV bit
2021 }
2022 }
2023
2024 static unsigned
2025 iris_derived_program_state_size(enum iris_program_cache_id cache_id)
2026 {
2027 assert(cache_id <= IRIS_CACHE_BLORP);
2028
2029 static const unsigned dwords[] = {
2030 [IRIS_CACHE_VS] = GENX(3DSTATE_VS_length),
2031 [IRIS_CACHE_TCS] = GENX(3DSTATE_HS_length),
2032 [IRIS_CACHE_TES] = GENX(3DSTATE_TE_length) + GENX(3DSTATE_DS_length),
2033 [IRIS_CACHE_GS] = GENX(3DSTATE_GS_length),
2034 [IRIS_CACHE_FS] =
2035 GENX(3DSTATE_PS_length) + GENX(3DSTATE_PS_EXTRA_length),
2036 [IRIS_CACHE_CS] = 0,
2037 [IRIS_CACHE_BLORP] = 0,
2038 };
2039
2040 return sizeof(uint32_t) * dwords[cache_id];
2041 }
2042
2043 static void
2044 iris_store_derived_program_state(const struct gen_device_info *devinfo,
2045 enum iris_program_cache_id cache_id,
2046 struct iris_compiled_shader *shader)
2047 {
2048 switch (cache_id) {
2049 case IRIS_CACHE_VS:
2050 iris_store_vs_state(devinfo, shader);
2051 break;
2052 case IRIS_CACHE_TCS:
2053 iris_store_tcs_state(devinfo, shader);
2054 break;
2055 case IRIS_CACHE_TES:
2056 iris_store_tes_state(devinfo, shader);
2057 break;
2058 case IRIS_CACHE_GS:
2059 iris_store_gs_state(devinfo, shader);
2060 break;
2061 case IRIS_CACHE_FS:
2062 iris_store_fs_state(devinfo, shader);
2063 break;
2064 case IRIS_CACHE_CS:
2065 case IRIS_CACHE_BLORP:
2066 break;
2067 default:
2068 break;
2069 }
2070 }
2071
2072 static void
2073 iris_upload_urb_config(struct iris_context *ice, struct iris_batch *batch)
2074 {
2075 const struct gen_device_info *devinfo = &batch->screen->devinfo;
2076 const unsigned push_size_kB = 32;
2077 unsigned entries[4];
2078 unsigned start[4];
2079 unsigned size[4];
2080
2081 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2082 if (!ice->shaders.prog[i]) {
2083 size[i] = 1;
2084 } else {
2085 struct brw_vue_prog_data *vue_prog_data =
2086 (void *) ice->shaders.prog[i]->prog_data;
2087 size[i] = vue_prog_data->urb_entry_size;
2088 }
2089 assert(size[i] != 0);
2090 }
2091
2092 gen_get_urb_config(devinfo, 1024 * push_size_kB,
2093 1024 * ice->shaders.urb_size,
2094 ice->shaders.prog[MESA_SHADER_TESS_EVAL] != NULL,
2095 ice->shaders.prog[MESA_SHADER_GEOMETRY] != NULL,
2096 size, entries, start);
2097
2098 for (int i = MESA_SHADER_VERTEX; i <= MESA_SHADER_GEOMETRY; i++) {
2099 iris_emit_cmd(batch, GENX(3DSTATE_URB_VS), urb) {
2100 urb._3DCommandSubOpcode += i;
2101 urb.VSURBStartingAddress = start[i];
2102 urb.VSURBEntryAllocationSize = size[i] - 1;
2103 urb.VSNumberofURBEntries = entries[i];
2104 }
2105 }
2106 }
2107
2108 static const uint32_t push_constant_opcodes[] = {
2109 [MESA_SHADER_VERTEX] = 21,
2110 [MESA_SHADER_TESS_CTRL] = 25, /* HS */
2111 [MESA_SHADER_TESS_EVAL] = 26, /* DS */
2112 [MESA_SHADER_GEOMETRY] = 22,
2113 [MESA_SHADER_FRAGMENT] = 23,
2114 [MESA_SHADER_COMPUTE] = 0,
2115 };
2116
2117 /**
2118 * Add a surface to the validation list, as well as the buffer containing
2119 * the corresponding SURFACE_STATE.
2120 *
2121 * Returns the binding table entry (offset to SURFACE_STATE).
2122 */
2123 static uint32_t
2124 use_surface(struct iris_batch *batch,
2125 struct pipe_surface *p_surf,
2126 bool writeable)
2127 {
2128 struct iris_surface *surf = (void *) p_surf;
2129 struct iris_resource *res = (void *) p_surf->texture;
2130 struct iris_resource *state_res = (void *) surf->surface_state_resource;
2131 iris_use_pinned_bo(batch, res->bo, writeable);
2132 iris_use_pinned_bo(batch, state_res->bo, false);
2133
2134 return surf->surface_state_offset;
2135 }
2136
2137 static uint32_t
2138 use_sampler_view(struct iris_batch *batch, struct iris_sampler_view *isv)
2139 {
2140 struct iris_resource *res = (void *) isv->pipe.texture;
2141 struct iris_resource *state_res = (void *) isv->surface_state_resource;
2142 iris_use_pinned_bo(batch, res->bo, false);
2143 iris_use_pinned_bo(batch, state_res->bo, false);
2144
2145 return isv->surface_state_offset;
2146 }
2147
2148 static uint32_t
2149 use_const_buffer(struct iris_batch *batch, struct iris_const_buffer *cbuf)
2150 {
2151 struct iris_resource *res = (void *) cbuf->resource;
2152 struct iris_resource *state_res = (void *) cbuf->surface_state_resource;
2153 iris_use_pinned_bo(batch, res->bo, false);
2154 iris_use_pinned_bo(batch, state_res->bo, false);
2155
2156 return cbuf->surface_state_offset;
2157 }
2158
2159 static void
2160 iris_populate_binding_table(struct iris_context *ice,
2161 struct iris_batch *batch,
2162 gl_shader_stage stage)
2163 {
2164 const struct iris_binder *binder = &batch->binder;
2165 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2166 if (!shader)
2167 return;
2168
2169 // Surfaces:
2170 // - pull constants
2171 // - ubos/ssbos/abos
2172 // - images
2173 // - textures
2174 // - render targets - write and read
2175
2176 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2177 uint32_t *bt_map = binder->map + binder->bt_offset[stage];
2178 int s = 0;
2179
2180 if (stage == MESA_SHADER_FRAGMENT) {
2181 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2182 for (unsigned i = 0; i < cso_fb->nr_cbufs; i++) {
2183 bt_map[s++] = use_surface(batch, cso_fb->cbufs[i], true);
2184 }
2185 }
2186
2187 //assert(prog_data->binding_table.texture_start ==
2188 //(ice->state.num_textures[stage] ? s : 0xd0d0d0d0));
2189
2190 for (int i = 0; i < ice->state.num_textures[stage]; i++) {
2191 struct iris_sampler_view *view = ice->state.textures[stage][i];
2192 bt_map[s++] = use_sampler_view(batch, view);
2193 }
2194
2195 // XXX: want the number of BTE's to shorten this loop
2196 struct iris_shader_state *shs = &ice->shaders.state[stage];
2197 for (int i = 0; i < PIPE_MAX_CONSTANT_BUFFERS; i++) {
2198 struct iris_const_buffer *cbuf = &shs->constbuf[i];
2199 if (!cbuf->surface_state_resource)
2200 break;
2201
2202 bt_map[s++] = use_const_buffer(batch, cbuf);
2203 }
2204 #if 0
2205 // XXX: not implemented yet
2206 assert(prog_data->binding_table.pull_constants_start == 0xd0d0d0d0);
2207 assert(prog_data->binding_table.ubo_start == 0xd0d0d0d0);
2208 assert(prog_data->binding_table.ssbo_start == 0xd0d0d0d0);
2209 assert(prog_data->binding_table.image_start == 0xd0d0d0d0);
2210 assert(prog_data->binding_table.shader_time_start == 0xd0d0d0d0);
2211 //assert(prog_data->binding_table.plane_start[1] == 0xd0d0d0d0);
2212 //assert(prog_data->binding_table.plane_start[2] == 0xd0d0d0d0);
2213 #endif
2214 }
2215
2216 static void
2217 iris_use_optional_res(struct iris_batch *batch,
2218 struct pipe_resource *res,
2219 bool writeable)
2220 {
2221 if (res) {
2222 struct iris_bo *bo = iris_resource_bo(res);
2223 iris_use_pinned_bo(batch, bo, writeable);
2224 }
2225 }
2226
2227
2228 /**
2229 * Pin any BOs which were installed by a previous batch, and restored
2230 * via the hardware logical context mechanism.
2231 *
2232 * We don't need to re-emit all state every batch - the hardware context
2233 * mechanism will save and restore it for us. This includes pointers to
2234 * various BOs...which won't exist unless we ask the kernel to pin them
2235 * by adding them to the validation list.
2236 *
2237 * We can skip buffers if we've re-emitted those packets, as we're
2238 * overwriting those stale pointers with new ones, and don't actually
2239 * refer to the old BOs.
2240 */
2241 static void
2242 iris_restore_context_saved_bos(struct iris_context *ice,
2243 struct iris_batch *batch,
2244 const struct pipe_draw_info *draw)
2245 {
2246 // XXX: whack IRIS_SHADER_DIRTY_BINDING_TABLE on new batch
2247
2248 const uint64_t clean =
2249 unlikely(INTEL_DEBUG & DEBUG_REEMIT) ? 0ull : ~ice->state.dirty;
2250
2251 if (clean & IRIS_DIRTY_CC_VIEWPORT) {
2252 iris_use_optional_res(batch, ice->state.last_res.cc_vp, false);
2253 }
2254
2255 if (clean & IRIS_DIRTY_SF_CL_VIEWPORT) {
2256 iris_use_optional_res(batch, ice->state.last_res.sf_cl_vp, false);
2257 }
2258
2259 if (clean & IRIS_DIRTY_BLEND_STATE) {
2260 iris_use_optional_res(batch, ice->state.last_res.blend, false);
2261 }
2262
2263 if (clean & IRIS_DIRTY_COLOR_CALC_STATE) {
2264 iris_use_optional_res(batch, ice->state.last_res.color_calc, false);
2265 }
2266
2267 if (clean & IRIS_DIRTY_SCISSOR_RECT) {
2268 iris_use_optional_res(batch, ice->state.last_res.scissor, false);
2269 }
2270
2271 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2272 if (clean & (IRIS_DIRTY_CONSTANTS_VS << stage))
2273 continue;
2274
2275 struct iris_shader_state *shs = &ice->shaders.state[stage];
2276 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2277
2278 if (!shader)
2279 continue;
2280
2281 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2282
2283 for (int i = 0; i < 4; i++) {
2284 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2285
2286 if (range->length == 0)
2287 continue;
2288
2289 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
2290 struct iris_resource *res = (void *) cbuf->resource;
2291
2292 iris_use_pinned_bo(batch, res->bo, false);
2293 }
2294 }
2295
2296 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2297 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
2298 if (res)
2299 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
2300 }
2301
2302 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2303 if (clean & (IRIS_DIRTY_VS << stage)) {
2304 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2305 if (shader)
2306 iris_use_pinned_bo(batch, iris_resource_bo(shader->buffer), false);
2307
2308 // XXX: scratch buffer
2309 }
2310 }
2311
2312 // XXX: 3DSTATE_SO_BUFFER
2313
2314 if (clean & IRIS_DIRTY_DEPTH_BUFFER) {
2315 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2316
2317 if (cso_fb->zsbuf) {
2318 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
2319 // XXX: depth might not be writable...
2320 iris_use_pinned_bo(batch, zres->bo, true);
2321 }
2322 }
2323
2324 if (draw->index_size > 0) {
2325 // XXX: index buffer
2326 }
2327
2328 if (clean & IRIS_DIRTY_VERTEX_BUFFERS) {
2329 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
2330 for (unsigned i = 0; i < cso->num_buffers; i++) {
2331 iris_use_pinned_bo(batch, cso->bos[i], false);
2332 }
2333 }
2334 }
2335
2336 static void
2337 iris_upload_render_state(struct iris_context *ice,
2338 struct iris_batch *batch,
2339 const struct pipe_draw_info *draw)
2340 {
2341 const uint64_t dirty =
2342 unlikely(INTEL_DEBUG & DEBUG_REEMIT) ? ~0ull : ice->state.dirty;
2343
2344 struct brw_wm_prog_data *wm_prog_data = (void *)
2345 ice->shaders.prog[MESA_SHADER_FRAGMENT]->prog_data;
2346
2347 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
2348 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2349 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
2350 ptr.CCViewportPointer =
2351 emit_state(batch, ice->state.dynamic_uploader,
2352 &ice->state.last_res.cc_vp,
2353 cso->cc_vp, sizeof(cso->cc_vp), 32);
2354 }
2355 }
2356
2357 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
2358 struct iris_viewport_state *cso = ice->state.cso_vp;
2359 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
2360 ptr.SFClipViewportPointer =
2361 emit_state(batch, ice->state.dynamic_uploader,
2362 &ice->state.last_res.sf_cl_vp,
2363 cso->sf_cl_vp, 4 * GENX(SF_CLIP_VIEWPORT_length) *
2364 ice->state.num_viewports, 64);
2365 }
2366 }
2367
2368 /* XXX: L3 State */
2369
2370 // XXX: this is only flagged at setup, we assume a static configuration
2371 if (dirty & IRIS_DIRTY_URB) {
2372 iris_upload_urb_config(ice, batch);
2373 }
2374
2375 if (dirty & IRIS_DIRTY_BLEND_STATE) {
2376 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2377 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2378 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2379 const int num_dwords = 4 * (GENX(BLEND_STATE_length) +
2380 cso_fb->nr_cbufs * GENX(BLEND_STATE_ENTRY_length));
2381 uint32_t blend_offset;
2382 uint32_t *blend_map =
2383 stream_state(batch, ice->state.dynamic_uploader,
2384 &ice->state.last_res.blend,
2385 4 * num_dwords, 64, &blend_offset);
2386
2387 uint32_t blend_state_header;
2388 iris_pack_state(GENX(BLEND_STATE), &blend_state_header, bs) {
2389 bs.AlphaTestEnable = cso_zsa->alpha.enabled;
2390 bs.AlphaTestFunction = translate_compare_func(cso_zsa->alpha.func);
2391 }
2392
2393 blend_map[0] = blend_state_header | cso_blend->blend_state[0];
2394 memcpy(&blend_map[1], &cso_blend->blend_state[1],
2395 sizeof(cso_blend->blend_state) - sizeof(uint32_t));
2396
2397 iris_emit_cmd(batch, GENX(3DSTATE_BLEND_STATE_POINTERS), ptr) {
2398 ptr.BlendStatePointer = blend_offset;
2399 ptr.BlendStatePointerValid = true;
2400 }
2401 }
2402
2403 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
2404 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2405 uint32_t cc_offset;
2406 void *cc_map =
2407 stream_state(batch, ice->state.dynamic_uploader,
2408 &ice->state.last_res.color_calc,
2409 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
2410 64, &cc_offset);
2411 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
2412 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
2413 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
2414 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
2415 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
2416 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
2417 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
2418 }
2419 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
2420 ptr.ColorCalcStatePointer = cc_offset;
2421 ptr.ColorCalcStatePointerValid = true;
2422 }
2423 }
2424
2425 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2426 // XXX: wrong dirty tracking...
2427 if (!(dirty & (IRIS_DIRTY_CONSTANTS_VS << stage)))
2428 continue;
2429
2430 struct iris_shader_state *shs = &ice->shaders.state[stage];
2431 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2432
2433 if (!shader)
2434 continue;
2435
2436 struct brw_stage_prog_data *prog_data = (void *) shader->prog_data;
2437
2438 iris_emit_cmd(batch, GENX(3DSTATE_CONSTANT_VS), pkt) {
2439 pkt._3DCommandSubOpcode = push_constant_opcodes[stage];
2440 if (prog_data) {
2441 /* The Skylake PRM contains the following restriction:
2442 *
2443 * "The driver must ensure The following case does not occur
2444 * without a flush to the 3D engine: 3DSTATE_CONSTANT_* with
2445 * buffer 3 read length equal to zero committed followed by a
2446 * 3DSTATE_CONSTANT_* with buffer 0 read length not equal to
2447 * zero committed."
2448 *
2449 * To avoid this, we program the buffers in the highest slots.
2450 * This way, slot 0 is only used if slot 3 is also used.
2451 */
2452 int n = 3;
2453
2454 for (int i = 3; i >= 0; i--) {
2455 const struct brw_ubo_range *range = &prog_data->ubo_ranges[i];
2456
2457 if (range->length == 0)
2458 continue;
2459
2460 // XXX: is range->block a constbuf index? it would be nice
2461 struct iris_const_buffer *cbuf = &shs->constbuf[range->block];
2462 struct iris_resource *res = (void *) cbuf->resource;
2463
2464 assert(cbuf->offset % 32 == 0);
2465
2466 pkt.ConstantBody.ReadLength[n] = range->length;
2467 pkt.ConstantBody.Buffer[n] =
2468 ro_bo(res->bo, range->start * 32 + cbuf->offset);
2469 n--;
2470 }
2471 }
2472 }
2473 }
2474
2475 struct iris_binder *binder = &batch->binder;
2476
2477 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2478 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
2479 iris_emit_cmd(batch, GENX(3DSTATE_BINDING_TABLE_POINTERS_VS), ptr) {
2480 ptr._3DCommandSubOpcode = 38 + stage;
2481 ptr.PointertoVSBindingTable = binder->bt_offset[stage];
2482 }
2483 }
2484 }
2485
2486 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2487 if (dirty & (IRIS_DIRTY_BINDINGS_VS << stage)) {
2488 iris_populate_binding_table(ice, batch, stage);
2489 }
2490 }
2491
2492 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2493 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)) ||
2494 !ice->shaders.prog[stage])
2495 continue;
2496
2497 struct pipe_resource *res = ice->state.sampler_table_resource[stage];
2498 if (res)
2499 iris_use_pinned_bo(batch, iris_resource_bo(res), false);
2500
2501 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
2502 ptr._3DCommandSubOpcode = 43 + stage;
2503 ptr.PointertoVSSamplerState = ice->state.sampler_table_offset[stage];
2504 }
2505 }
2506
2507 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
2508 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
2509 ms.PixelLocation =
2510 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
2511 if (ice->state.framebuffer.samples > 0)
2512 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
2513 }
2514 }
2515
2516 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
2517 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
2518 ms.SampleMask = MAX2(ice->state.sample_mask, 1);
2519 }
2520 }
2521
2522 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
2523 if (!(dirty & (IRIS_DIRTY_VS << stage)))
2524 continue;
2525
2526 struct iris_compiled_shader *shader = ice->shaders.prog[stage];
2527
2528 if (shader) {
2529 struct iris_resource *cache = (void *) shader->buffer;
2530 iris_use_pinned_bo(batch, cache->bo, false);
2531 iris_batch_emit(batch, shader->derived_data,
2532 iris_derived_program_state_size(stage));
2533 } else {
2534 if (stage == MESA_SHADER_TESS_EVAL) {
2535 iris_emit_cmd(batch, GENX(3DSTATE_HS), hs);
2536 iris_emit_cmd(batch, GENX(3DSTATE_TE), te);
2537 iris_emit_cmd(batch, GENX(3DSTATE_DS), ds);
2538 } else if (stage == MESA_SHADER_GEOMETRY) {
2539 iris_emit_cmd(batch, GENX(3DSTATE_GS), gs);
2540 }
2541 }
2542 }
2543
2544 // XXX: SOL:
2545 // 3DSTATE_STREAMOUT
2546 // 3DSTATE_SO_BUFFER
2547 // 3DSTATE_SO_DECL_LIST
2548
2549 if (dirty & IRIS_DIRTY_CLIP) {
2550 struct iris_rasterizer_state *cso_rast = ice->state.cso_rast;
2551 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2552
2553 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
2554 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
2555 if (wm_prog_data->barycentric_interp_modes &
2556 BRW_BARYCENTRIC_NONPERSPECTIVE_BITS)
2557 cl.NonPerspectiveBarycentricEnable = true;
2558
2559 cl.ForceZeroRTAIndexEnable = cso_fb->layers == 0;
2560 }
2561 iris_emit_merge(batch, cso_rast->clip, dynamic_clip,
2562 ARRAY_SIZE(cso_rast->clip));
2563 }
2564
2565 if (dirty & IRIS_DIRTY_RASTER) {
2566 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2567 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
2568 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
2569
2570 }
2571
2572 /* XXX: FS program updates needs to flag IRIS_DIRTY_WM */
2573 if (dirty & IRIS_DIRTY_WM) {
2574 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2575 uint32_t dynamic_wm[GENX(3DSTATE_WM_length)];
2576
2577 iris_pack_command(GENX(3DSTATE_WM), &dynamic_wm, wm) {
2578 wm.BarycentricInterpolationMode =
2579 wm_prog_data->barycentric_interp_modes;
2580
2581 if (wm_prog_data->early_fragment_tests)
2582 wm.EarlyDepthStencilControl = EDSC_PREPS;
2583 else if (wm_prog_data->has_side_effects)
2584 wm.EarlyDepthStencilControl = EDSC_PSEXEC;
2585 }
2586 iris_emit_merge(batch, cso->wm, dynamic_wm, ARRAY_SIZE(cso->wm));
2587 }
2588
2589 if (1) {
2590 // XXX: 3DSTATE_SBE, 3DSTATE_SBE_SWIZ
2591 // -> iris_raster_state (point sprite texture coordinate origin)
2592 // -> bunch of shader state...
2593 iris_emit_sbe(batch, ice);
2594 iris_emit_cmd(batch, GENX(3DSTATE_SBE_SWIZ), sbe) {
2595 }
2596 }
2597
2598 if (dirty & IRIS_DIRTY_PS_BLEND) {
2599 struct iris_blend_state *cso_blend = ice->state.cso_blend;
2600 struct iris_depth_stencil_alpha_state *cso_zsa = ice->state.cso_zsa;
2601 uint32_t dynamic_pb[GENX(3DSTATE_PS_BLEND_length)];
2602 iris_pack_command(GENX(3DSTATE_PS_BLEND), &dynamic_pb, pb) {
2603 pb.HasWriteableRT = true; // XXX: comes from somewhere :(
2604 pb.AlphaTestEnable = cso_zsa->alpha.enabled;
2605 }
2606
2607 iris_emit_merge(batch, cso_blend->ps_blend, dynamic_pb,
2608 ARRAY_SIZE(cso_blend->ps_blend));
2609 }
2610
2611 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
2612 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
2613 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
2614
2615 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
2616 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
2617 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
2618 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
2619 }
2620 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
2621 }
2622
2623 if (dirty & IRIS_DIRTY_SCISSOR_RECT) {
2624 // XXX: allocate at set_scissor time?
2625 uint32_t scissor_offset = ice->state.num_scissors == 0 ? 0 :
2626 emit_state(batch, ice->state.dynamic_uploader,
2627 &ice->state.last_res.scissor,
2628 ice->state.scissors,
2629 sizeof(struct pipe_scissor_state) *
2630 ice->state.num_scissors, 32);
2631
2632 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
2633 ptr.ScissorRectPointer = scissor_offset;
2634 }
2635 }
2636
2637 if (dirty & IRIS_DIRTY_DEPTH_BUFFER) {
2638 struct pipe_framebuffer_state *cso_fb = &ice->state.framebuffer;
2639 struct iris_depth_buffer_state *cso_z = ice->state.cso_depthbuffer;
2640
2641 iris_batch_emit(batch, cso_z->packets, sizeof(cso_z->packets));
2642
2643 if (cso_fb->zsbuf) {
2644 struct iris_resource *zres = (void *) cso_fb->zsbuf->texture;
2645 // XXX: depth might not be writable...
2646 iris_use_pinned_bo(batch, zres->bo, true);
2647 }
2648 }
2649
2650 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
2651 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
2652 for (int i = 0; i < 32; i++) {
2653 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
2654 }
2655 }
2656 }
2657
2658 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
2659 struct iris_rasterizer_state *cso = ice->state.cso_rast;
2660 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
2661 }
2662
2663 if (1) {
2664 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
2665 topo.PrimitiveTopologyType =
2666 translate_prim_type(draw->mode, draw->vertices_per_patch);
2667 }
2668 }
2669
2670 if (draw->index_size > 0) {
2671 struct iris_resource *res = NULL;
2672 unsigned offset;
2673
2674 if (draw->has_user_indices) {
2675 u_upload_data(ice->ctx.stream_uploader, 0,
2676 draw->count * draw->index_size, 4, draw->index.user,
2677 &offset, (struct pipe_resource **) &res);
2678 } else {
2679 res = (struct iris_resource *) draw->index.resource;
2680 offset = 0;
2681 }
2682
2683 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
2684 ib.IndexFormat = draw->index_size >> 1;
2685 ib.MOCS = MOCS_WB;
2686 ib.BufferSize = res->bo->size;
2687 ib.BufferStartingAddress = ro_bo(res->bo, offset);
2688 }
2689 }
2690
2691 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
2692 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
2693 const unsigned vb_dwords = GENX(VERTEX_BUFFER_STATE_length);
2694
2695 iris_batch_emit(batch, cso->vertex_buffers,
2696 sizeof(uint32_t) * (1 + vb_dwords * cso->num_buffers));
2697
2698 for (unsigned i = 0; i < cso->num_buffers; i++) {
2699 iris_use_pinned_bo(batch, cso->bos[i], false);
2700 }
2701 }
2702
2703 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
2704 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
2705 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
2706 (1 + cso->count * GENX(VERTEX_ELEMENT_STATE_length)));
2707 iris_batch_emit(batch, cso->vf_instancing, sizeof(uint32_t) *
2708 cso->count * GENX(3DSTATE_VF_INSTANCING_length));
2709 for (int i = 0; i < cso->count; i++) {
2710 /* TODO: vertexid, instanceid support */
2711 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgvs);
2712 }
2713 }
2714
2715 if (1) {
2716 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
2717 if (draw->primitive_restart) {
2718 vf.IndexedDrawCutIndexEnable = true;
2719 vf.CutIndex = draw->restart_index;
2720 }
2721 }
2722 }
2723
2724 // XXX: Gen8 - PMA fix
2725
2726 assert(!draw->indirect); // XXX: indirect support
2727
2728 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
2729 prim.StartInstanceLocation = draw->start_instance;
2730 prim.InstanceCount = draw->instance_count;
2731 prim.VertexCountPerInstance = draw->count;
2732 prim.VertexAccessType = draw->index_size > 0 ? RANDOM : SEQUENTIAL;
2733
2734 // XXX: this is probably bonkers.
2735 prim.StartVertexLocation = draw->start;
2736
2737 if (draw->index_size) {
2738 prim.BaseVertexLocation += draw->index_bias;
2739 } else {
2740 prim.StartVertexLocation += draw->index_bias;
2741 }
2742
2743 //prim.BaseVertexLocation = ...;
2744 }
2745
2746 if (!batch->contains_draw) {
2747 iris_restore_context_saved_bos(ice, batch, draw);
2748 batch->contains_draw = true;
2749 }
2750 }
2751
2752 /**
2753 * State module teardown.
2754 */
2755 static void
2756 iris_destroy_state(struct iris_context *ice)
2757 {
2758 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
2759
2760 // XXX: unreference resources/surfaces.
2761 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
2762 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
2763 }
2764 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
2765
2766 for (int stage = 0; stage < MESA_SHADER_STAGES; stage++) {
2767 pipe_resource_reference(&ice->state.sampler_table_resource[stage], NULL);
2768 }
2769 free(ice->state.cso_vp);
2770 free(ice->state.cso_depthbuffer);
2771
2772 pipe_resource_reference(&ice->state.last_res.cc_vp, NULL);
2773 pipe_resource_reference(&ice->state.last_res.sf_cl_vp, NULL);
2774 pipe_resource_reference(&ice->state.last_res.color_calc, NULL);
2775 pipe_resource_reference(&ice->state.last_res.scissor, NULL);
2776 pipe_resource_reference(&ice->state.last_res.blend, NULL);
2777 }
2778
2779 static unsigned
2780 flags_to_post_sync_op(uint32_t flags)
2781 {
2782 if (flags & PIPE_CONTROL_WRITE_IMMEDIATE)
2783 return WriteImmediateData;
2784
2785 if (flags & PIPE_CONTROL_WRITE_DEPTH_COUNT)
2786 return WritePSDepthCount;
2787
2788 if (flags & PIPE_CONTROL_WRITE_TIMESTAMP)
2789 return WriteTimestamp;
2790
2791 return 0;
2792 }
2793
2794 /**
2795 * Do the given flags have a Post Sync or LRI Post Sync operation?
2796 */
2797 static enum pipe_control_flags
2798 get_post_sync_flags(enum pipe_control_flags flags)
2799 {
2800 flags &= PIPE_CONTROL_WRITE_IMMEDIATE |
2801 PIPE_CONTROL_WRITE_DEPTH_COUNT |
2802 PIPE_CONTROL_WRITE_TIMESTAMP |
2803 PIPE_CONTROL_LRI_POST_SYNC_OP;
2804
2805 /* Only one "Post Sync Op" is allowed, and it's mutually exclusive with
2806 * "LRI Post Sync Operation". So more than one bit set would be illegal.
2807 */
2808 assert(util_bitcount(flags) <= 1);
2809
2810 return flags;
2811 }
2812
2813 // XXX: compute support
2814 #define IS_COMPUTE_PIPELINE(batch) (batch->ring != I915_EXEC_RENDER)
2815
2816 /**
2817 * Emit a series of PIPE_CONTROL commands, taking into account any
2818 * workarounds necessary to actually accomplish the caller's request.
2819 *
2820 * Unless otherwise noted, spec quotations in this function come from:
2821 *
2822 * Synchronization of the 3D Pipeline > PIPE_CONTROL Command > Programming
2823 * Restrictions for PIPE_CONTROL.
2824 */
2825 static void
2826 iris_emit_raw_pipe_control(struct iris_batch *batch, uint32_t flags,
2827 struct iris_bo *bo, uint32_t offset, uint64_t imm)
2828 {
2829 UNUSED const struct gen_device_info *devinfo = &batch->screen->devinfo;
2830 enum pipe_control_flags post_sync_flags = get_post_sync_flags(flags);
2831 enum pipe_control_flags non_lri_post_sync_flags =
2832 post_sync_flags & ~PIPE_CONTROL_LRI_POST_SYNC_OP;
2833
2834 /* Recursive PIPE_CONTROL workarounds --------------------------------
2835 * (http://knowyourmeme.com/memes/xzibit-yo-dawg)
2836 *
2837 * We do these first because we want to look at the original operation,
2838 * rather than any workarounds we set.
2839 */
2840 if (GEN_GEN == 9 && (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE)) {
2841 /* The PIPE_CONTROL "VF Cache Invalidation Enable" bit description
2842 * lists several workarounds:
2843 *
2844 * "Project: SKL, KBL, BXT
2845 *
2846 * If the VF Cache Invalidation Enable is set to a 1 in a
2847 * PIPE_CONTROL, a separate Null PIPE_CONTROL, all bitfields
2848 * sets to 0, with the VF Cache Invalidation Enable set to 0
2849 * needs to be sent prior to the PIPE_CONTROL with VF Cache
2850 * Invalidation Enable set to a 1."
2851 */
2852 iris_emit_raw_pipe_control(batch, 0, NULL, 0, 0);
2853 }
2854
2855 if (GEN_GEN == 9 && IS_COMPUTE_PIPELINE(batch) && post_sync_flags) {
2856 /* Project: SKL / Argument: LRI Post Sync Operation [23]
2857 *
2858 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
2859 * programmed prior to programming a PIPECONTROL command with "LRI
2860 * Post Sync Operation" in GPGPU mode of operation (i.e when
2861 * PIPELINE_SELECT command is set to GPGPU mode of operation)."
2862 *
2863 * The same text exists a few rows below for Post Sync Op.
2864 */
2865 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_CS_STALL, bo, offset, imm);
2866 }
2867
2868 if (GEN_GEN == 10 && (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH)) {
2869 /* Cannonlake:
2870 * "Before sending a PIPE_CONTROL command with bit 12 set, SW must issue
2871 * another PIPE_CONTROL with Render Target Cache Flush Enable (bit 12)
2872 * = 0 and Pipe Control Flush Enable (bit 7) = 1"
2873 */
2874 iris_emit_raw_pipe_control(batch, PIPE_CONTROL_FLUSH_ENABLE, bo,
2875 offset, imm);
2876 }
2877
2878 /* "Flush Types" workarounds ---------------------------------------------
2879 * We do these now because they may add post-sync operations or CS stalls.
2880 */
2881
2882 if (flags & PIPE_CONTROL_VF_CACHE_INVALIDATE) {
2883 /* Project: BDW, SKL+ (stopping at CNL) / Argument: VF Invalidate
2884 *
2885 * "'Post Sync Operation' must be enabled to 'Write Immediate Data' or
2886 * 'Write PS Depth Count' or 'Write Timestamp'."
2887 */
2888 if (!bo) {
2889 flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2890 post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2891 non_lri_post_sync_flags |= PIPE_CONTROL_WRITE_IMMEDIATE;
2892 bo = batch->screen->workaround_bo;
2893 }
2894 }
2895
2896 /* #1130 from Gen10 workarounds page:
2897 *
2898 * "Enable Depth Stall on every Post Sync Op if Render target Cache
2899 * Flush is not enabled in same PIPE CONTROL and Enable Pixel score
2900 * board stall if Render target cache flush is enabled."
2901 *
2902 * Applicable to CNL B0 and C0 steppings only.
2903 *
2904 * The wording here is unclear, and this workaround doesn't look anything
2905 * like the internal bug report recommendations, but leave it be for now...
2906 */
2907 if (GEN_GEN == 10) {
2908 if (flags & PIPE_CONTROL_RENDER_TARGET_FLUSH) {
2909 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
2910 } else if (flags & non_lri_post_sync_flags) {
2911 flags |= PIPE_CONTROL_DEPTH_STALL;
2912 }
2913 }
2914
2915 if (flags & PIPE_CONTROL_DEPTH_STALL) {
2916 /* From the PIPE_CONTROL instruction table, bit 13 (Depth Stall Enable):
2917 *
2918 * "This bit must be DISABLED for operations other than writing
2919 * PS_DEPTH_COUNT."
2920 *
2921 * This seems like nonsense. An Ivybridge workaround requires us to
2922 * emit a PIPE_CONTROL with a depth stall and write immediate post-sync
2923 * operation. Gen8+ requires us to emit depth stalls and depth cache
2924 * flushes together. So, it's hard to imagine this means anything other
2925 * than "we originally intended this to be used for PS_DEPTH_COUNT".
2926 *
2927 * We ignore the supposed restriction and do nothing.
2928 */
2929 }
2930
2931 if (flags & (PIPE_CONTROL_RENDER_TARGET_FLUSH |
2932 PIPE_CONTROL_STALL_AT_SCOREBOARD)) {
2933 /* From the PIPE_CONTROL instruction table, bit 12 and bit 1:
2934 *
2935 * "This bit must be DISABLED for End-of-pipe (Read) fences,
2936 * PS_DEPTH_COUNT or TIMESTAMP queries."
2937 *
2938 * TODO: Implement end-of-pipe checking.
2939 */
2940 assert(!(post_sync_flags & (PIPE_CONTROL_WRITE_DEPTH_COUNT |
2941 PIPE_CONTROL_WRITE_TIMESTAMP)));
2942 }
2943
2944 if (flags & PIPE_CONTROL_STALL_AT_SCOREBOARD) {
2945 /* From the PIPE_CONTROL instruction table, bit 1:
2946 *
2947 * "This bit is ignored if Depth Stall Enable is set.
2948 * Further, the render cache is not flushed even if Write Cache
2949 * Flush Enable bit is set."
2950 *
2951 * We assert that the caller doesn't do this combination, to try and
2952 * prevent mistakes. It shouldn't hurt the GPU, though.
2953 */
2954 assert(!(flags & (PIPE_CONTROL_DEPTH_STALL |
2955 PIPE_CONTROL_RENDER_TARGET_FLUSH)));
2956 }
2957
2958 /* PIPE_CONTROL page workarounds ------------------------------------- */
2959
2960 if (GEN_GEN <= 8 && (flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE)) {
2961 /* From the PIPE_CONTROL page itself:
2962 *
2963 * "IVB, HSW, BDW
2964 * Restriction: Pipe_control with CS-stall bit set must be issued
2965 * before a pipe-control command that has the State Cache
2966 * Invalidate bit set."
2967 */
2968 flags |= PIPE_CONTROL_CS_STALL;
2969 }
2970
2971 if (flags & PIPE_CONTROL_FLUSH_LLC) {
2972 /* From the PIPE_CONTROL instruction table, bit 26 (Flush LLC):
2973 *
2974 * "Project: ALL
2975 * SW must always program Post-Sync Operation to "Write Immediate
2976 * Data" when Flush LLC is set."
2977 *
2978 * For now, we just require the caller to do it.
2979 */
2980 assert(flags & PIPE_CONTROL_WRITE_IMMEDIATE);
2981 }
2982
2983 /* "Post-Sync Operation" workarounds -------------------------------- */
2984
2985 /* Project: All / Argument: Global Snapshot Count Reset [19]
2986 *
2987 * "This bit must not be exercised on any product.
2988 * Requires stall bit ([20] of DW1) set."
2989 *
2990 * We don't use this, so we just assert that it isn't used. The
2991 * PIPE_CONTROL instruction page indicates that they intended this
2992 * as a debug feature and don't think it is useful in production,
2993 * but it may actually be usable, should we ever want to.
2994 */
2995 assert((flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET) == 0);
2996
2997 if (flags & (PIPE_CONTROL_MEDIA_STATE_CLEAR |
2998 PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE)) {
2999 /* Project: All / Arguments:
3000 *
3001 * - Generic Media State Clear [16]
3002 * - Indirect State Pointers Disable [16]
3003 *
3004 * "Requires stall bit ([20] of DW1) set."
3005 *
3006 * Also, the PIPE_CONTROL instruction table, bit 16 (Generic Media
3007 * State Clear) says:
3008 *
3009 * "PIPECONTROL command with “Command Streamer Stall Enable” must be
3010 * programmed prior to programming a PIPECONTROL command with "Media
3011 * State Clear" set in GPGPU mode of operation"
3012 *
3013 * This is a subset of the earlier rule, so there's nothing to do.
3014 */
3015 flags |= PIPE_CONTROL_CS_STALL;
3016 }
3017
3018 if (flags & PIPE_CONTROL_STORE_DATA_INDEX) {
3019 /* Project: All / Argument: Store Data Index
3020 *
3021 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3022 * than '0'."
3023 *
3024 * For now, we just assert that the caller does this. We might want to
3025 * automatically add a write to the workaround BO...
3026 */
3027 assert(non_lri_post_sync_flags != 0);
3028 }
3029
3030 if (flags & PIPE_CONTROL_SYNC_GFDT) {
3031 /* Project: All / Argument: Sync GFDT
3032 *
3033 * "Post-Sync Operation ([15:14] of DW1) must be set to something other
3034 * than '0' or 0x2520[13] must be set."
3035 *
3036 * For now, we just assert that the caller does this.
3037 */
3038 assert(non_lri_post_sync_flags != 0);
3039 }
3040
3041 if (flags & PIPE_CONTROL_TLB_INVALIDATE) {
3042 /* Project: IVB+ / Argument: TLB inv
3043 *
3044 * "Requires stall bit ([20] of DW1) set."
3045 *
3046 * Also, from the PIPE_CONTROL instruction table:
3047 *
3048 * "Project: SKL+
3049 * Post Sync Operation or CS stall must be set to ensure a TLB
3050 * invalidation occurs. Otherwise no cycle will occur to the TLB
3051 * cache to invalidate."
3052 *
3053 * This is not a subset of the earlier rule, so there's nothing to do.
3054 */
3055 flags |= PIPE_CONTROL_CS_STALL;
3056 }
3057
3058 if (GEN_GEN == 9 && devinfo->gt == 4) {
3059 /* TODO: The big Skylake GT4 post sync op workaround */
3060 }
3061
3062 /* "GPGPU specific workarounds" (both post-sync and flush) ------------ */
3063
3064 if (IS_COMPUTE_PIPELINE(batch)) {
3065 if (GEN_GEN >= 9 && (flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE)) {
3066 /* Project: SKL+ / Argument: Tex Invalidate
3067 * "Requires stall bit ([20] of DW) set for all GPGPU Workloads."
3068 */
3069 flags |= PIPE_CONTROL_CS_STALL;
3070 }
3071
3072 if (GEN_GEN == 8 && (post_sync_flags ||
3073 (flags & (PIPE_CONTROL_NOTIFY_ENABLE |
3074 PIPE_CONTROL_DEPTH_STALL |
3075 PIPE_CONTROL_RENDER_TARGET_FLUSH |
3076 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3077 PIPE_CONTROL_DATA_CACHE_FLUSH)))) {
3078 /* Project: BDW / Arguments:
3079 *
3080 * - LRI Post Sync Operation [23]
3081 * - Post Sync Op [15:14]
3082 * - Notify En [8]
3083 * - Depth Stall [13]
3084 * - Render Target Cache Flush [12]
3085 * - Depth Cache Flush [0]
3086 * - DC Flush Enable [5]
3087 *
3088 * "Requires stall bit ([20] of DW) set for all GPGPU and Media
3089 * Workloads."
3090 */
3091 flags |= PIPE_CONTROL_CS_STALL;
3092
3093 /* Also, from the PIPE_CONTROL instruction table, bit 20:
3094 *
3095 * "Project: BDW
3096 * This bit must be always set when PIPE_CONTROL command is
3097 * programmed by GPGPU and MEDIA workloads, except for the cases
3098 * when only Read Only Cache Invalidation bits are set (State
3099 * Cache Invalidation Enable, Instruction cache Invalidation
3100 * Enable, Texture Cache Invalidation Enable, Constant Cache
3101 * Invalidation Enable). This is to WA FFDOP CG issue, this WA
3102 * need not implemented when FF_DOP_CG is disable via "Fixed
3103 * Function DOP Clock Gate Disable" bit in RC_PSMI_CTRL register."
3104 *
3105 * It sounds like we could avoid CS stalls in some cases, but we
3106 * don't currently bother. This list isn't exactly the list above,
3107 * either...
3108 */
3109 }
3110 }
3111
3112 /* "Stall" workarounds ----------------------------------------------
3113 * These have to come after the earlier ones because we may have added
3114 * some additional CS stalls above.
3115 */
3116
3117 if (GEN_GEN < 9 && (flags & PIPE_CONTROL_CS_STALL)) {
3118 /* Project: PRE-SKL, VLV, CHV
3119 *
3120 * "[All Stepping][All SKUs]:
3121 *
3122 * One of the following must also be set:
3123 *
3124 * - Render Target Cache Flush Enable ([12] of DW1)
3125 * - Depth Cache Flush Enable ([0] of DW1)
3126 * - Stall at Pixel Scoreboard ([1] of DW1)
3127 * - Depth Stall ([13] of DW1)
3128 * - Post-Sync Operation ([13] of DW1)
3129 * - DC Flush Enable ([5] of DW1)"
3130 *
3131 * If we don't already have one of those bits set, we choose to add
3132 * "Stall at Pixel Scoreboard". Some of the other bits require a
3133 * CS stall as a workaround (see above), which would send us into
3134 * an infinite recursion of PIPE_CONTROLs. "Stall at Pixel Scoreboard"
3135 * appears to be safe, so we choose that.
3136 */
3137 const uint32_t wa_bits = PIPE_CONTROL_RENDER_TARGET_FLUSH |
3138 PIPE_CONTROL_DEPTH_CACHE_FLUSH |
3139 PIPE_CONTROL_WRITE_IMMEDIATE |
3140 PIPE_CONTROL_WRITE_DEPTH_COUNT |
3141 PIPE_CONTROL_WRITE_TIMESTAMP |
3142 PIPE_CONTROL_STALL_AT_SCOREBOARD |
3143 PIPE_CONTROL_DEPTH_STALL |
3144 PIPE_CONTROL_DATA_CACHE_FLUSH;
3145 if (!(flags & wa_bits))
3146 flags |= PIPE_CONTROL_STALL_AT_SCOREBOARD;
3147 }
3148
3149 /* Emit --------------------------------------------------------------- */
3150
3151 iris_emit_cmd(batch, GENX(PIPE_CONTROL), pc) {
3152 pc.LRIPostSyncOperation = NoLRIOperation;
3153 pc.PipeControlFlushEnable = flags & PIPE_CONTROL_FLUSH_ENABLE;
3154 pc.DCFlushEnable = flags & PIPE_CONTROL_DATA_CACHE_FLUSH;
3155 pc.StoreDataIndex = 0;
3156 pc.CommandStreamerStallEnable = flags & PIPE_CONTROL_CS_STALL;
3157 pc.GlobalSnapshotCountReset =
3158 flags & PIPE_CONTROL_GLOBAL_SNAPSHOT_COUNT_RESET;
3159 pc.TLBInvalidate = flags & PIPE_CONTROL_TLB_INVALIDATE;
3160 pc.GenericMediaStateClear = flags & PIPE_CONTROL_MEDIA_STATE_CLEAR;
3161 pc.StallAtPixelScoreboard = flags & PIPE_CONTROL_STALL_AT_SCOREBOARD;
3162 pc.RenderTargetCacheFlushEnable =
3163 flags & PIPE_CONTROL_RENDER_TARGET_FLUSH;
3164 pc.DepthCacheFlushEnable = flags & PIPE_CONTROL_DEPTH_CACHE_FLUSH;
3165 pc.StateCacheInvalidationEnable =
3166 flags & PIPE_CONTROL_STATE_CACHE_INVALIDATE;
3167 pc.VFCacheInvalidationEnable = flags & PIPE_CONTROL_VF_CACHE_INVALIDATE;
3168 pc.ConstantCacheInvalidationEnable =
3169 flags & PIPE_CONTROL_CONST_CACHE_INVALIDATE;
3170 pc.PostSyncOperation = flags_to_post_sync_op(flags);
3171 pc.DepthStallEnable = flags & PIPE_CONTROL_DEPTH_STALL;
3172 pc.InstructionCacheInvalidateEnable =
3173 flags & PIPE_CONTROL_INSTRUCTION_INVALIDATE;
3174 pc.NotifyEnable = flags & PIPE_CONTROL_NOTIFY_ENABLE;
3175 pc.IndirectStatePointersDisable =
3176 flags & PIPE_CONTROL_INDIRECT_STATE_POINTERS_DISABLE;
3177 pc.TextureCacheInvalidationEnable =
3178 flags & PIPE_CONTROL_TEXTURE_CACHE_INVALIDATE;
3179 pc.Address = ro_bo(bo, offset);
3180 pc.ImmediateData = imm;
3181 }
3182 }
3183
3184 void
3185 genX(init_state)(struct iris_context *ice)
3186 {
3187 struct pipe_context *ctx = &ice->ctx;
3188
3189 ctx->create_blend_state = iris_create_blend_state;
3190 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
3191 ctx->create_rasterizer_state = iris_create_rasterizer_state;
3192 ctx->create_sampler_state = iris_create_sampler_state;
3193 ctx->create_sampler_view = iris_create_sampler_view;
3194 ctx->create_surface = iris_create_surface;
3195 ctx->create_vertex_elements_state = iris_create_vertex_elements;
3196 ctx->create_compute_state = iris_create_compute_state;
3197 ctx->bind_blend_state = iris_bind_blend_state;
3198 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
3199 ctx->bind_sampler_states = iris_bind_sampler_states;
3200 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
3201 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
3202 ctx->bind_compute_state = iris_bind_compute_state;
3203 ctx->delete_blend_state = iris_delete_state;
3204 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
3205 ctx->delete_fs_state = iris_delete_state;
3206 ctx->delete_rasterizer_state = iris_delete_state;
3207 ctx->delete_sampler_state = iris_delete_state;
3208 ctx->delete_vertex_elements_state = iris_delete_state;
3209 ctx->delete_compute_state = iris_delete_state;
3210 ctx->delete_tcs_state = iris_delete_state;
3211 ctx->delete_tes_state = iris_delete_state;
3212 ctx->delete_gs_state = iris_delete_state;
3213 ctx->delete_vs_state = iris_delete_state;
3214 ctx->set_blend_color = iris_set_blend_color;
3215 ctx->set_clip_state = iris_set_clip_state;
3216 ctx->set_constant_buffer = iris_set_constant_buffer;
3217 ctx->set_sampler_views = iris_set_sampler_views;
3218 ctx->set_framebuffer_state = iris_set_framebuffer_state;
3219 ctx->set_polygon_stipple = iris_set_polygon_stipple;
3220 ctx->set_sample_mask = iris_set_sample_mask;
3221 ctx->set_scissor_states = iris_set_scissor_states;
3222 ctx->set_stencil_ref = iris_set_stencil_ref;
3223 ctx->set_vertex_buffers = iris_set_vertex_buffers;
3224 ctx->set_viewport_states = iris_set_viewport_states;
3225 ctx->sampler_view_destroy = iris_sampler_view_destroy;
3226 ctx->surface_destroy = iris_surface_destroy;
3227 ctx->draw_vbo = iris_draw_vbo;
3228 ctx->launch_grid = iris_launch_grid;
3229 ctx->create_stream_output_target = iris_create_stream_output_target;
3230 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
3231 ctx->set_stream_output_targets = iris_set_stream_output_targets;
3232
3233 ice->vtbl.destroy_state = iris_destroy_state;
3234 ice->vtbl.init_render_context = iris_init_render_context;
3235 ice->vtbl.upload_render_state = iris_upload_render_state;
3236 ice->vtbl.emit_raw_pipe_control = iris_emit_raw_pipe_control;
3237 ice->vtbl.derived_program_state_size = iris_derived_program_state_size;
3238 ice->vtbl.store_derived_program_state = iris_store_derived_program_state;
3239 ice->vtbl.populate_vs_key = iris_populate_vs_key;
3240 ice->vtbl.populate_tcs_key = iris_populate_tcs_key;
3241 ice->vtbl.populate_tes_key = iris_populate_tes_key;
3242 ice->vtbl.populate_gs_key = iris_populate_gs_key;
3243 ice->vtbl.populate_fs_key = iris_populate_fs_key;
3244
3245 ice->state.dirty = ~0ull;
3246 }