iris: fix indentation
[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 #ifdef HAVE_VALGRIND
27 #include <valgrind.h>
28 #include <memcheck.h>
29 #define VG(x) x
30 #define __gen_validate_value(x) VALGRIND_CHECK_MEM_IS_DEFINED(&(x), sizeof(x))
31 #else
32 #define VG(x)
33 #endif
34
35 #include "pipe/p_defines.h"
36 #include "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_screen.h"
39 #include "util/u_inlines.h"
40 #include "util/u_transfer.h"
41 #include "intel/compiler/brw_compiler.h"
42 #include "intel/common/gen_sample_positions.h"
43 #include "iris_batch.h"
44 #include "iris_context.h"
45 #include "iris_pipe.h"
46 #include "iris_resource.h"
47
48 #define __gen_address_type struct iris_address
49 #define __gen_user_data struct iris_batch
50
51 static uint64_t
52 __gen_combine_address(struct iris_batch *batch, void *location,
53 struct iris_address addr, uint32_t delta)
54 {
55 if (addr.bo == NULL)
56 return addr.offset + delta;
57
58 return iris_batch_reloc(batch, location - batch->cmdbuf.map, addr.bo,
59 addr.offset + delta, addr.reloc_flags);
60 }
61
62 #define __genxml_cmd_length(cmd) cmd ## _length
63 #define __genxml_cmd_length_bias(cmd) cmd ## _length_bias
64 #define __genxml_cmd_header(cmd) cmd ## _header
65 #define __genxml_cmd_pack(cmd) cmd ## _pack
66
67 static void *
68 get_command_space(struct iris_batch *batch, unsigned bytes)
69 {
70 iris_require_command_space(batch, bytes);
71 void *map = batch->cmdbuf.map_next;
72 batch->cmdbuf.map_next += bytes;
73 return map;
74 }
75
76 #define iris_pack_command(cmd, dst, name) \
77 for (struct cmd name = { __genxml_cmd_header(cmd) }, \
78 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
79 ({ __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name); \
80 VG(VALGRIND_CHECK_MEM_IS_DEFINED(_dst, __genxml_cmd_length(cmd) * 4)); \
81 _dst = NULL; \
82 }))
83
84 #define iris_pack_state(cmd, dst, name) \
85 for (struct cmd name = {}, \
86 *_dst = (void *)(dst); __builtin_expect(_dst != NULL, 1); \
87 __genxml_cmd_pack(cmd)(NULL, (void *)_dst, &name), \
88 _dst = NULL)
89
90 #define iris_emit_cmd(batch, cmd, name) \
91 iris_pack_command(cmd, get_command_space(batch, 4 * __genxml_cmd_length(cmd)), name)
92
93 #define iris_emit_merge(batch, dwords0, dwords1, num_dwords) \
94 do { \
95 uint32_t *dw = get_command_space(batch, 4 * num_dwords); \
96 for (uint32_t i = 0; i < num_dwords; i++) \
97 dw[i] = (dwords0)[i] | (dwords1)[i]; \
98 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dwords)); \
99 } while (0)
100
101 #define iris_emit_with_addr(batch, dwords, num_dw, addr_field, addr) \
102 do { \
103 STATIC_ASSERT((GENX(addr_field) % 64) == 0); \
104 assert(num_dw <= ARRAY_SIZE(dwords)); \
105 int addr_idx = GENX(addr_field) / 32; \
106 uint32_t *dw = get_command_space(batch, 4 * num_dw); \
107 for (uint32_t i = 0; i < addr_idx; i++) { \
108 dw[i] = (dwords)[i]; \
109 } \
110 uint64_t *qw = (uint64_t *) &dw[addr_idx]; \
111 qw = iris_batch_reloc(batch, qw - batch->cmdbuf.map, addr.bo, \
112 addr.offset + (dwords)[addr_idx + 1], \
113 addr.reloc_flags); \
114 for (uint32_t i = addr_idx + 1; i < num_dw; i++) { \
115 dw[i] = (dwords)[i]; \
116 } \
117 VG(VALGRIND_CHECK_MEM_IS_DEFINED(dw, num_dw * 4)); \
118 } while (0)
119
120 #include "genxml/genX_pack.h"
121 #include "genxml/gen_macros.h"
122 #include "genxml/genX_bits.h"
123
124 #define MOCS_WB (2 << 1)
125
126 UNUSED static void pipe_asserts()
127 {
128 #define PIPE_ASSERT(x) STATIC_ASSERT((int)x)
129
130 /* pipe_logicop happens to match the hardware. */
131 PIPE_ASSERT(PIPE_LOGICOP_CLEAR == LOGICOP_CLEAR);
132 PIPE_ASSERT(PIPE_LOGICOP_NOR == LOGICOP_NOR);
133 PIPE_ASSERT(PIPE_LOGICOP_AND_INVERTED == LOGICOP_AND_INVERTED);
134 PIPE_ASSERT(PIPE_LOGICOP_COPY_INVERTED == LOGICOP_COPY_INVERTED);
135 PIPE_ASSERT(PIPE_LOGICOP_AND_REVERSE == LOGICOP_AND_REVERSE);
136 PIPE_ASSERT(PIPE_LOGICOP_INVERT == LOGICOP_INVERT);
137 PIPE_ASSERT(PIPE_LOGICOP_XOR == LOGICOP_XOR);
138 PIPE_ASSERT(PIPE_LOGICOP_NAND == LOGICOP_NAND);
139 PIPE_ASSERT(PIPE_LOGICOP_AND == LOGICOP_AND);
140 PIPE_ASSERT(PIPE_LOGICOP_EQUIV == LOGICOP_EQUIV);
141 PIPE_ASSERT(PIPE_LOGICOP_NOOP == LOGICOP_NOOP);
142 PIPE_ASSERT(PIPE_LOGICOP_OR_INVERTED == LOGICOP_OR_INVERTED);
143 PIPE_ASSERT(PIPE_LOGICOP_COPY == LOGICOP_COPY);
144 PIPE_ASSERT(PIPE_LOGICOP_OR_REVERSE == LOGICOP_OR_REVERSE);
145 PIPE_ASSERT(PIPE_LOGICOP_OR == LOGICOP_OR);
146 PIPE_ASSERT(PIPE_LOGICOP_SET == LOGICOP_SET);
147
148 /* pipe_blend_func happens to match the hardware. */
149 PIPE_ASSERT(PIPE_BLENDFACTOR_ONE == BLENDFACTOR_ONE);
150 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_COLOR == BLENDFACTOR_SRC_COLOR);
151 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA == BLENDFACTOR_SRC_ALPHA);
152 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_ALPHA == BLENDFACTOR_DST_ALPHA);
153 PIPE_ASSERT(PIPE_BLENDFACTOR_DST_COLOR == BLENDFACTOR_DST_COLOR);
154 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE == BLENDFACTOR_SRC_ALPHA_SATURATE);
155 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_COLOR == BLENDFACTOR_CONST_COLOR);
156 PIPE_ASSERT(PIPE_BLENDFACTOR_CONST_ALPHA == BLENDFACTOR_CONST_ALPHA);
157 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_COLOR == BLENDFACTOR_SRC1_COLOR);
158 PIPE_ASSERT(PIPE_BLENDFACTOR_SRC1_ALPHA == BLENDFACTOR_SRC1_ALPHA);
159 PIPE_ASSERT(PIPE_BLENDFACTOR_ZERO == BLENDFACTOR_ZERO);
160 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_COLOR == BLENDFACTOR_INV_SRC_COLOR);
161 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC_ALPHA == BLENDFACTOR_INV_SRC_ALPHA);
162 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_ALPHA == BLENDFACTOR_INV_DST_ALPHA);
163 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_DST_COLOR == BLENDFACTOR_INV_DST_COLOR);
164 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_COLOR == BLENDFACTOR_INV_CONST_COLOR);
165 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_CONST_ALPHA == BLENDFACTOR_INV_CONST_ALPHA);
166 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_COLOR == BLENDFACTOR_INV_SRC1_COLOR);
167 PIPE_ASSERT(PIPE_BLENDFACTOR_INV_SRC1_ALPHA == BLENDFACTOR_INV_SRC1_ALPHA);
168
169 /* pipe_blend_func happens to match the hardware. */
170 PIPE_ASSERT(PIPE_BLEND_ADD == BLENDFUNCTION_ADD);
171 PIPE_ASSERT(PIPE_BLEND_SUBTRACT == BLENDFUNCTION_SUBTRACT);
172 PIPE_ASSERT(PIPE_BLEND_REVERSE_SUBTRACT == BLENDFUNCTION_REVERSE_SUBTRACT);
173 PIPE_ASSERT(PIPE_BLEND_MIN == BLENDFUNCTION_MIN);
174 PIPE_ASSERT(PIPE_BLEND_MAX == BLENDFUNCTION_MAX);
175
176 /* pipe_stencil_op happens to match the hardware. */
177 PIPE_ASSERT(PIPE_STENCIL_OP_KEEP == STENCILOP_KEEP);
178 PIPE_ASSERT(PIPE_STENCIL_OP_ZERO == STENCILOP_ZERO);
179 PIPE_ASSERT(PIPE_STENCIL_OP_REPLACE == STENCILOP_REPLACE);
180 PIPE_ASSERT(PIPE_STENCIL_OP_INCR == STENCILOP_INCRSAT);
181 PIPE_ASSERT(PIPE_STENCIL_OP_DECR == STENCILOP_DECRSAT);
182 PIPE_ASSERT(PIPE_STENCIL_OP_INCR_WRAP == STENCILOP_INCR);
183 PIPE_ASSERT(PIPE_STENCIL_OP_DECR_WRAP == STENCILOP_DECR);
184 PIPE_ASSERT(PIPE_STENCIL_OP_INVERT == STENCILOP_INVERT);
185 #undef PIPE_ASSERT
186 }
187
188 static unsigned
189 translate_prim_type(enum pipe_prim_type prim, uint8_t verts_per_patch)
190 {
191 static const unsigned map[] = {
192 [PIPE_PRIM_POINTS] = _3DPRIM_POINTLIST,
193 [PIPE_PRIM_LINES] = _3DPRIM_LINELIST,
194 [PIPE_PRIM_LINE_LOOP] = _3DPRIM_LINELOOP,
195 [PIPE_PRIM_LINE_STRIP] = _3DPRIM_LINESTRIP,
196 [PIPE_PRIM_TRIANGLES] = _3DPRIM_TRILIST,
197 [PIPE_PRIM_TRIANGLE_STRIP] = _3DPRIM_TRISTRIP,
198 [PIPE_PRIM_TRIANGLE_FAN] = _3DPRIM_TRIFAN,
199 [PIPE_PRIM_QUADS] = _3DPRIM_QUADLIST,
200 [PIPE_PRIM_QUAD_STRIP] = _3DPRIM_QUADSTRIP,
201 [PIPE_PRIM_POLYGON] = _3DPRIM_POLYGON,
202 [PIPE_PRIM_LINES_ADJACENCY] = _3DPRIM_LINELIST_ADJ,
203 [PIPE_PRIM_LINE_STRIP_ADJACENCY] = _3DPRIM_LINESTRIP_ADJ,
204 [PIPE_PRIM_TRIANGLES_ADJACENCY] = _3DPRIM_TRILIST_ADJ,
205 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY] = _3DPRIM_TRISTRIP_ADJ,
206 [PIPE_PRIM_PATCHES] = _3DPRIM_PATCHLIST_1 - 1,
207 };
208
209 return map[prim] + (prim == PIPE_PRIM_PATCHES ? verts_per_patch : 0);
210 }
211
212 static unsigned
213 translate_compare_func(enum pipe_compare_func pipe_func)
214 {
215 static const unsigned map[] = {
216 [PIPE_FUNC_NEVER] = COMPAREFUNCTION_NEVER,
217 [PIPE_FUNC_LESS] = COMPAREFUNCTION_LESS,
218 [PIPE_FUNC_EQUAL] = COMPAREFUNCTION_EQUAL,
219 [PIPE_FUNC_LEQUAL] = COMPAREFUNCTION_LEQUAL,
220 [PIPE_FUNC_GREATER] = COMPAREFUNCTION_GREATER,
221 [PIPE_FUNC_NOTEQUAL] = COMPAREFUNCTION_NOTEQUAL,
222 [PIPE_FUNC_GEQUAL] = COMPAREFUNCTION_GEQUAL,
223 [PIPE_FUNC_ALWAYS] = COMPAREFUNCTION_ALWAYS,
224 };
225 return map[pipe_func];
226 }
227
228 static unsigned
229 translate_shadow_func(enum pipe_compare_func pipe_func)
230 {
231 /* Gallium specifies the result of shadow comparisons as:
232 *
233 * 1 if ref <op> texel,
234 * 0 otherwise.
235 *
236 * The hardware does:
237 *
238 * 0 if texel <op> ref,
239 * 1 otherwise.
240 *
241 * So we need to flip the operator and also negate.
242 */
243 static const unsigned map[] = {
244 [PIPE_FUNC_NEVER] = PREFILTEROPALWAYS,
245 [PIPE_FUNC_LESS] = PREFILTEROPLEQUAL,
246 [PIPE_FUNC_EQUAL] = PREFILTEROPNOTEQUAL,
247 [PIPE_FUNC_LEQUAL] = PREFILTEROPLESS,
248 [PIPE_FUNC_GREATER] = PREFILTEROPGEQUAL,
249 [PIPE_FUNC_NOTEQUAL] = PREFILTEROPEQUAL,
250 [PIPE_FUNC_GEQUAL] = PREFILTEROPGREATER,
251 [PIPE_FUNC_ALWAYS] = PREFILTEROPNEVER,
252 };
253 return map[pipe_func];
254 }
255
256 static unsigned
257 translate_cull_mode(unsigned pipe_face)
258 {
259 static const unsigned map[4] = {
260 [PIPE_FACE_NONE] = CULLMODE_NONE,
261 [PIPE_FACE_FRONT] = CULLMODE_FRONT,
262 [PIPE_FACE_BACK] = CULLMODE_BACK,
263 [PIPE_FACE_FRONT_AND_BACK] = CULLMODE_BOTH,
264 };
265 return map[pipe_face];
266 }
267
268 static unsigned
269 translate_fill_mode(unsigned pipe_polymode)
270 {
271 static const unsigned map[4] = {
272 [PIPE_POLYGON_MODE_FILL] = FILL_MODE_SOLID,
273 [PIPE_POLYGON_MODE_LINE] = FILL_MODE_WIREFRAME,
274 [PIPE_POLYGON_MODE_POINT] = FILL_MODE_POINT,
275 [PIPE_POLYGON_MODE_FILL_RECTANGLE] = FILL_MODE_SOLID,
276 };
277 return map[pipe_polymode];
278 }
279
280 static struct iris_address
281 ro_bo(struct iris_bo *bo, uint32_t offset)
282 {
283 return (struct iris_address) { .bo = bo, .offset = offset };
284 }
285
286 static void
287 iris_upload_initial_gpu_state(struct iris_context *ice,
288 struct iris_batch *batch)
289 {
290 iris_emit_cmd(batch, GENX(3DSTATE_DRAWING_RECTANGLE), rect) {
291 rect.ClippedDrawingRectangleXMax = UINT16_MAX;
292 rect.ClippedDrawingRectangleYMax = UINT16_MAX;
293 }
294 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_PATTERN), pat) {
295 GEN_SAMPLE_POS_1X(pat._1xSample);
296 GEN_SAMPLE_POS_2X(pat._2xSample);
297 GEN_SAMPLE_POS_4X(pat._4xSample);
298 GEN_SAMPLE_POS_8X(pat._8xSample);
299 GEN_SAMPLE_POS_16X(pat._16xSample);
300 }
301 iris_emit_cmd(batch, GENX(3DSTATE_AA_LINE_PARAMETERS), foo);
302 iris_emit_cmd(batch, GENX(3DSTATE_WM_CHROMAKEY), foo);
303 iris_emit_cmd(batch, GENX(3DSTATE_WM_HZ_OP), foo);
304 /* XXX: may need to set an offset for origin-UL framebuffers */
305 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_OFFSET), foo);
306
307 /* Just assign a static partitioning. */
308 for (int i = 0; i <= MESA_SHADER_FRAGMENT; i++) {
309 iris_emit_cmd(batch, GENX(3DSTATE_PUSH_CONSTANT_ALLOC_VS), alloc) {
310 alloc._3DCommandSubOpcode = 18 + i;
311 alloc.ConstantBufferOffset = 6 * i;
312 alloc.ConstantBufferSize = i == MESA_SHADER_FRAGMENT ? 8 : 6;
313 }
314 }
315 }
316
317 static void
318 iris_launch_grid(struct pipe_context *ctx, const struct pipe_grid_info *info)
319 {
320 }
321
322 static void
323 iris_set_blend_color(struct pipe_context *ctx,
324 const struct pipe_blend_color *state)
325 {
326 struct iris_context *ice = (struct iris_context *) ctx;
327
328 memcpy(&ice->state.blend_color, state, sizeof(struct pipe_blend_color));
329 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
330 }
331
332 struct iris_blend_state {
333 uint32_t ps_blend[GENX(3DSTATE_PS_BLEND_length)];
334 uint32_t blend_state[GENX(BLEND_STATE_length)];
335 uint32_t blend_entries[BRW_MAX_DRAW_BUFFERS *
336 GENX(BLEND_STATE_ENTRY_length)];
337 };
338
339 static void *
340 iris_create_blend_state(struct pipe_context *ctx,
341 const struct pipe_blend_state *state)
342 {
343 struct iris_blend_state *cso = malloc(sizeof(struct iris_blend_state));
344
345 iris_pack_state(GENX(BLEND_STATE), cso->blend_state, bs) {
346 bs.AlphaToCoverageEnable = state->alpha_to_coverage;
347 bs.IndependentAlphaBlendEnable = state->independent_blend_enable;
348 bs.AlphaToOneEnable = state->alpha_to_one;
349 bs.AlphaToCoverageDitherEnable = state->alpha_to_coverage;
350 bs.ColorDitherEnable = state->dither;
351 //bs.AlphaTestEnable = <comes from alpha state> :(
352 //bs.AlphaTestFunction = <comes from alpha state> :(
353 }
354
355 iris_pack_command(GENX(3DSTATE_PS_BLEND), cso->ps_blend, pb) {
356 //pb.HasWriteableRT = <comes from somewhere> :(
357 //pb.AlphaTestEnable = <comes from alpha state> :(
358 pb.AlphaToCoverageEnable = state->alpha_to_coverage;
359 pb.IndependentAlphaBlendEnable = state->independent_blend_enable;
360
361 pb.ColorBufferBlendEnable = state->rt[0].blend_enable;
362
363 pb.SourceBlendFactor = state->rt[0].rgb_src_factor;
364 pb.SourceAlphaBlendFactor = state->rt[0].alpha_func;
365 pb.DestinationBlendFactor = state->rt[0].rgb_dst_factor;
366 pb.DestinationAlphaBlendFactor = state->rt[0].alpha_dst_factor;
367 }
368
369 for (int i = 0; i < BRW_MAX_DRAW_BUFFERS; i++) {
370 iris_pack_state(GENX(BLEND_STATE_ENTRY), &cso->blend_entries[i], be) {
371 be.LogicOpEnable = state->logicop_enable;
372 be.LogicOpFunction = state->logicop_func;
373
374 be.PreBlendSourceOnlyClampEnable = false;
375 be.ColorClampRange = COLORCLAMP_RTFORMAT;
376 be.PreBlendColorClampEnable = true;
377 be.PostBlendColorClampEnable = true;
378
379 be.ColorBufferBlendEnable = state->rt[i].blend_enable;
380
381 be.ColorBlendFunction = state->rt[i].rgb_func;
382 be.AlphaBlendFunction = state->rt[i].alpha_func;
383 be.SourceBlendFactor = state->rt[i].rgb_src_factor;
384 be.SourceAlphaBlendFactor = state->rt[i].alpha_func;
385 be.DestinationBlendFactor = state->rt[i].rgb_dst_factor;
386 be.DestinationAlphaBlendFactor = state->rt[i].alpha_dst_factor;
387
388 be.WriteDisableRed = state->rt[i].colormask & PIPE_MASK_R;
389 be.WriteDisableGreen = state->rt[i].colormask & PIPE_MASK_G;
390 be.WriteDisableBlue = state->rt[i].colormask & PIPE_MASK_B;
391 be.WriteDisableAlpha = state->rt[i].colormask & PIPE_MASK_A;
392 }
393 }
394
395 return cso;
396 }
397
398 static void
399 iris_bind_blend_state(struct pipe_context *ctx, void *state)
400 {
401 struct iris_context *ice = (struct iris_context *) ctx;
402 ice->state.cso_blend = state;
403 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
404 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
405 }
406
407 struct iris_depth_stencil_alpha_state {
408 uint32_t wmds[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
409 uint32_t cc_vp[GENX(CC_VIEWPORT_length)];
410
411 struct pipe_alpha_state alpha; /* to BLEND_STATE, 3DSTATE_PS_BLEND */
412 };
413
414 static void *
415 iris_create_zsa_state(struct pipe_context *ctx,
416 const struct pipe_depth_stencil_alpha_state *state)
417 {
418 struct iris_depth_stencil_alpha_state *cso =
419 malloc(sizeof(struct iris_depth_stencil_alpha_state));
420
421 cso->alpha = state->alpha;
422
423 bool two_sided_stencil = state->stencil[1].enabled;
424
425 /* The state tracker needs to optimize away EQUAL writes for us. */
426 assert(!(state->depth.func == PIPE_FUNC_EQUAL && state->depth.writemask));
427
428 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), cso->wmds, wmds) {
429 wmds.StencilFailOp = state->stencil[0].fail_op;
430 wmds.StencilPassDepthFailOp = state->stencil[0].zfail_op;
431 wmds.StencilPassDepthPassOp = state->stencil[0].zpass_op;
432 wmds.StencilTestFunction =
433 translate_compare_func(state->stencil[0].func);
434 wmds.BackfaceStencilFailOp = state->stencil[1].fail_op;
435 wmds.BackfaceStencilPassDepthFailOp = state->stencil[1].zfail_op;
436 wmds.BackfaceStencilPassDepthPassOp = state->stencil[1].zpass_op;
437 wmds.BackfaceStencilTestFunction =
438 translate_compare_func(state->stencil[1].func);
439 wmds.DepthTestFunction = translate_compare_func(state->depth.func);
440 wmds.DoubleSidedStencilEnable = two_sided_stencil;
441 wmds.StencilTestEnable = state->stencil[0].enabled;
442 wmds.StencilBufferWriteEnable =
443 state->stencil[0].writemask != 0 ||
444 (two_sided_stencil && state->stencil[1].writemask != 0);
445 wmds.DepthTestEnable = state->depth.enabled;
446 wmds.DepthBufferWriteEnable = state->depth.writemask;
447 wmds.StencilTestMask = state->stencil[0].valuemask;
448 wmds.StencilWriteMask = state->stencil[0].writemask;
449 wmds.BackfaceStencilTestMask = state->stencil[1].valuemask;
450 wmds.BackfaceStencilWriteMask = state->stencil[1].writemask;
451 /* wmds.[Backface]StencilReferenceValue are merged later */
452 }
453
454 iris_pack_state(GENX(CC_VIEWPORT), cso->cc_vp, ccvp) {
455 ccvp.MinimumDepth = state->depth.bounds_min;
456 ccvp.MaximumDepth = state->depth.bounds_max;
457 }
458
459 return cso;
460 }
461
462 static void
463 iris_bind_zsa_state(struct pipe_context *ctx, void *state)
464 {
465 struct iris_context *ice = (struct iris_context *) ctx;
466 struct iris_depth_stencil_alpha_state *old_cso = ice->state.cso_zsa;
467 struct iris_depth_stencil_alpha_state *new_cso = state;
468
469 if (new_cso) {
470 if (!old_cso || old_cso->alpha.ref_value != new_cso->alpha.ref_value) {
471 ice->state.dirty |= IRIS_DIRTY_COLOR_CALC_STATE;
472 }
473 }
474
475 ice->state.cso_zsa = new_cso;
476 ice->state.dirty |= IRIS_DIRTY_CC_VIEWPORT;
477 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
478 }
479
480 struct iris_rasterizer_state {
481 uint32_t sf[GENX(3DSTATE_SF_length)];
482 uint32_t clip[GENX(3DSTATE_CLIP_length)];
483 uint32_t raster[GENX(3DSTATE_RASTER_length)];
484 uint32_t wm[GENX(3DSTATE_WM_length)];
485 uint32_t line_stipple[GENX(3DSTATE_LINE_STIPPLE_length)];
486
487 bool flatshade; /* for shader state */
488 bool light_twoside; /* for shader state */
489 bool rasterizer_discard; /* for 3DSTATE_STREAMOUT */
490 bool half_pixel_center; /* for 3DSTATE_MULTISAMPLE */
491 enum pipe_sprite_coord_mode sprite_coord_mode; /* PIPE_SPRITE_* */
492 };
493
494 static void *
495 iris_create_rasterizer_state(struct pipe_context *ctx,
496 const struct pipe_rasterizer_state *state)
497 {
498 struct iris_rasterizer_state *cso =
499 malloc(sizeof(struct iris_rasterizer_state));
500
501 #if 0
502 sprite_coord_mode -> SBE PointSpriteTextureCoordinateOrigin
503 sprite_coord_enable -> SBE PointSpriteTextureCoordinateEnable
504 point_quad_rasterization -> SBE?
505
506 not necessary?
507 {
508 poly_smooth
509 force_persample_interp - ?
510 bottom_edge_rule
511
512 offset_units_unscaled - cap not exposed
513 }
514 #endif
515
516 cso->flatshade = state->flatshade;
517 cso->light_twoside = state->light_twoside;
518 cso->rasterizer_discard = state->rasterizer_discard;
519 cso->half_pixel_center = state->half_pixel_center;
520
521 iris_pack_command(GENX(3DSTATE_SF), cso->sf, sf) {
522 sf.StatisticsEnable = true;
523 sf.ViewportTransformEnable = true;
524 sf.AALineDistanceMode = AALINEDISTANCE_TRUE;
525 sf.LineEndCapAntialiasingRegionWidth =
526 state->line_smooth ? _10pixels : _05pixels;
527 sf.LastPixelEnable = state->line_last_pixel;
528 sf.LineWidth = state->line_width;
529 sf.SmoothPointEnable = state->point_smooth;
530 sf.PointWidthSource = state->point_size_per_vertex ? Vertex : State;
531 sf.PointWidth = state->point_size;
532
533 if (state->flatshade_first) {
534 sf.TriangleStripListProvokingVertexSelect = 2;
535 sf.TriangleFanProvokingVertexSelect = 2;
536 sf.LineStripListProvokingVertexSelect = 1;
537 } else {
538 sf.TriangleFanProvokingVertexSelect = 1;
539 }
540 }
541
542 /* COMPLETE! */
543 iris_pack_command(GENX(3DSTATE_RASTER), cso->raster, rr) {
544 rr.FrontWinding = state->front_ccw ? CounterClockwise : Clockwise;
545 rr.CullMode = translate_cull_mode(state->cull_face);
546 rr.FrontFaceFillMode = translate_fill_mode(state->fill_front);
547 rr.BackFaceFillMode = translate_fill_mode(state->fill_back);
548 rr.DXMultisampleRasterizationEnable = state->multisample;
549 rr.GlobalDepthOffsetEnableSolid = state->offset_tri;
550 rr.GlobalDepthOffsetEnableWireframe = state->offset_line;
551 rr.GlobalDepthOffsetEnablePoint = state->offset_point;
552 rr.GlobalDepthOffsetConstant = state->offset_units;
553 rr.GlobalDepthOffsetScale = state->offset_scale;
554 rr.GlobalDepthOffsetClamp = state->offset_clamp;
555 rr.SmoothPointEnable = state->point_smooth;
556 rr.AntialiasingEnable = state->line_smooth;
557 rr.ScissorRectangleEnable = state->scissor;
558 rr.ViewportZNearClipTestEnable = state->depth_clip_near;
559 rr.ViewportZFarClipTestEnable = state->depth_clip_far;
560 //rr.ConservativeRasterizationEnable = not yet supported by Gallium...
561 }
562
563 iris_pack_command(GENX(3DSTATE_CLIP), cso->clip, cl) {
564 cl.StatisticsEnable = true;
565 cl.EarlyCullEnable = true;
566 cl.UserClipDistanceClipTestEnableBitmask = state->clip_plane_enable;
567 cl.ForceUserClipDistanceClipTestEnableBitmask = true;
568 cl.APIMode = state->clip_halfz ? APIMODE_D3D : APIMODE_OGL;
569 cl.GuardbandClipTestEnable = true;
570 cl.ClipMode = CLIPMODE_NORMAL;
571 cl.ClipEnable = true;
572 cl.ViewportXYClipTestEnable = state->point_tri_clip;
573 cl.MinimumPointWidth = 0.125;
574 cl.MaximumPointWidth = 255.875;
575 //.NonPerspectiveBarycentricEnable = <comes from FS prog> :(
576 //.ForceZeroRTAIndexEnable = <comes from FB layers being 0>
577
578 if (state->flatshade_first) {
579 cl.TriangleStripListProvokingVertexSelect = 2;
580 cl.TriangleFanProvokingVertexSelect = 2;
581 cl.LineStripListProvokingVertexSelect = 1;
582 } else {
583 cl.TriangleFanProvokingVertexSelect = 1;
584 }
585 }
586
587 iris_pack_command(GENX(3DSTATE_WM), cso->wm, wm) {
588 wm.LineAntialiasingRegionWidth = _10pixels;
589 wm.LineEndCapAntialiasingRegionWidth = _05pixels;
590 wm.PointRasterizationRule = RASTRULE_UPPER_RIGHT;
591 wm.StatisticsEnable = true;
592 wm.LineStippleEnable = state->line_stipple_enable;
593 wm.PolygonStippleEnable = state->poly_stipple_enable;
594 // wm.BarycentricInterpolationMode = <comes from FS program> :(
595 // wm.EarlyDepthStencilControl = <comes from FS program> :(
596 }
597
598 /* Remap from 0..255 back to 1..256 */
599 const unsigned line_stipple_factor = state->line_stipple_factor + 1;
600
601 iris_pack_command(GENX(3DSTATE_LINE_STIPPLE), cso->line_stipple, line) {
602 line.LineStipplePattern = state->line_stipple_pattern;
603 line.LineStippleInverseRepeatCount = 1.0f / line_stipple_factor;
604 line.LineStippleRepeatCount = line_stipple_factor;
605 }
606
607 return cso;
608 }
609
610 static void
611 iris_bind_rasterizer_state(struct pipe_context *ctx, void *state)
612 {
613 struct iris_context *ice = (struct iris_context *) ctx;
614 struct iris_rasterizer_state *old_cso = ice->state.cso_rast;
615 struct iris_rasterizer_state *new_cso = state;
616
617 if (new_cso) {
618 /* Try to avoid re-emitting 3DSTATE_LINE_STIPPLE, it's non-pipelined */
619 if (!old_cso || memcmp(old_cso->line_stipple, new_cso->line_stipple,
620 sizeof(old_cso->line_stipple)) != 0) {
621 ice->state.dirty |= IRIS_DIRTY_LINE_STIPPLE;
622 }
623
624 if (!old_cso ||
625 old_cso->half_pixel_center != new_cso->half_pixel_center) {
626 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
627 }
628 }
629
630 ice->state.cso_rast = new_cso;
631 ice->state.dirty |= IRIS_DIRTY_RASTER;
632 }
633
634 static uint32_t
635 translate_wrap(unsigned pipe_wrap)
636 {
637 static const unsigned map[] = {
638 [PIPE_TEX_WRAP_REPEAT] = TCM_WRAP,
639 [PIPE_TEX_WRAP_CLAMP] = TCM_HALF_BORDER,
640 [PIPE_TEX_WRAP_CLAMP_TO_EDGE] = TCM_CLAMP,
641 [PIPE_TEX_WRAP_CLAMP_TO_BORDER] = TCM_CLAMP_BORDER,
642 [PIPE_TEX_WRAP_MIRROR_REPEAT] = TCM_MIRROR,
643 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE] = TCM_MIRROR_ONCE,
644 [PIPE_TEX_WRAP_MIRROR_CLAMP] = -1, // XXX: ???
645 [PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER] = -1, // XXX: ???
646 };
647 return map[pipe_wrap];
648 }
649
650 /**
651 * Return true if the given wrap mode requires the border color to exist.
652 */
653 static bool
654 wrap_mode_needs_border_color(unsigned wrap_mode)
655 {
656 return wrap_mode == TCM_CLAMP_BORDER || wrap_mode == TCM_HALF_BORDER;
657 }
658
659 static unsigned
660 translate_mip_filter(enum pipe_tex_mipfilter pipe_mip)
661 {
662 static const unsigned map[] = {
663 [PIPE_TEX_MIPFILTER_NEAREST] = MIPFILTER_NEAREST,
664 [PIPE_TEX_MIPFILTER_LINEAR] = MIPFILTER_LINEAR,
665 [PIPE_TEX_MIPFILTER_NONE] = MIPFILTER_NONE,
666 };
667 return map[pipe_mip];
668 }
669
670 struct iris_sampler_state {
671 struct pipe_sampler_state base;
672
673 bool needs_border_color;
674
675 uint32_t sampler_state[GENX(SAMPLER_STATE_length)];
676 };
677
678 static void *
679 iris_create_sampler_state(struct pipe_context *pctx,
680 const struct pipe_sampler_state *state)
681 {
682 struct iris_sampler_state *cso = CALLOC_STRUCT(iris_sampler_state);
683
684 if (!cso)
685 return NULL;
686
687 STATIC_ASSERT(PIPE_TEX_FILTER_NEAREST == MAPFILTER_NEAREST);
688 STATIC_ASSERT(PIPE_TEX_FILTER_LINEAR == MAPFILTER_LINEAR);
689
690 unsigned wrap_s = translate_wrap(state->wrap_s);
691 unsigned wrap_t = translate_wrap(state->wrap_t);
692 unsigned wrap_r = translate_wrap(state->wrap_r);
693
694 cso->needs_border_color = wrap_mode_needs_border_color(wrap_s) ||
695 wrap_mode_needs_border_color(wrap_t) ||
696 wrap_mode_needs_border_color(wrap_r);
697
698 iris_pack_state(GENX(SAMPLER_STATE), cso->sampler_state, samp) {
699 samp.TCXAddressControlMode = wrap_s;
700 samp.TCYAddressControlMode = wrap_t;
701 samp.TCZAddressControlMode = wrap_r;
702 samp.CubeSurfaceControlMode = state->seamless_cube_map;
703 samp.NonnormalizedCoordinateEnable = !state->normalized_coords;
704 samp.MinModeFilter = state->min_img_filter;
705 samp.MagModeFilter = state->mag_img_filter;
706 samp.MipModeFilter = translate_mip_filter(state->min_mip_filter);
707 samp.MaximumAnisotropy = RATIO21;
708
709 if (state->max_anisotropy >= 2) {
710 if (state->min_img_filter == PIPE_TEX_FILTER_LINEAR) {
711 samp.MinModeFilter = MAPFILTER_ANISOTROPIC;
712 samp.AnisotropicAlgorithm = EWAApproximation;
713 }
714
715 if (state->mag_img_filter == PIPE_TEX_FILTER_LINEAR)
716 samp.MagModeFilter = MAPFILTER_ANISOTROPIC;
717
718 samp.MaximumAnisotropy =
719 MIN2((state->max_anisotropy - 2) / 2, RATIO161);
720 }
721
722 /* Set address rounding bits if not using nearest filtering. */
723 if (state->min_img_filter != PIPE_TEX_FILTER_NEAREST) {
724 samp.UAddressMinFilterRoundingEnable = true;
725 samp.VAddressMinFilterRoundingEnable = true;
726 samp.RAddressMinFilterRoundingEnable = true;
727 }
728
729 if (state->mag_img_filter != PIPE_TEX_FILTER_NEAREST) {
730 samp.UAddressMagFilterRoundingEnable = true;
731 samp.VAddressMagFilterRoundingEnable = true;
732 samp.RAddressMagFilterRoundingEnable = true;
733 }
734
735 if (state->compare_mode == PIPE_TEX_COMPARE_R_TO_TEXTURE)
736 samp.ShadowFunction = translate_shadow_func(state->compare_func);
737
738 const float hw_max_lod = GEN_GEN >= 7 ? 14 : 13;
739
740 samp.LODPreClampMode = CLAMP_MODE_OGL;
741 samp.MinLOD = CLAMP(state->min_lod, 0, hw_max_lod);
742 samp.MaxLOD = CLAMP(state->max_lod, 0, hw_max_lod);
743 samp.TextureLODBias = CLAMP(state->lod_bias, -16, 15);
744
745 //samp.BorderColorPointer = <<comes from elsewhere>>
746 }
747
748 return cso;
749 }
750
751 static void
752 iris_bind_sampler_states(struct pipe_context *ctx,
753 enum pipe_shader_type p_stage,
754 unsigned start, unsigned count,
755 void **states)
756 {
757 struct iris_context *ice = (struct iris_context *) ctx;
758 gl_shader_stage stage = stage_from_pipe(p_stage);
759
760 assert(start + count <= IRIS_MAX_TEXTURE_SAMPLERS);
761
762 for (int i = 0; i < count; i++) {
763 ice->state.samplers[stage][start + i] = states[i];
764 }
765
766 ice->state.dirty |= IRIS_DIRTY_SAMPLER_STATES_VS << stage;
767 }
768
769 struct iris_sampler_view {
770 struct pipe_sampler_view pipe;
771 struct isl_view view;
772 uint32_t surface_state[GENX(RENDER_SURFACE_STATE_length)];
773 };
774
775 /**
776 * Convert an swizzle enumeration (i.e. SWIZZLE_X) to one of the Gen7.5+
777 * "Shader Channel Select" enumerations (i.e. HSW_SCS_RED). The mappings are
778 *
779 * SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_W, SWIZZLE_ZERO, SWIZZLE_ONE
780 * 0 1 2 3 4 5
781 * 4 5 6 7 0 1
782 * SCS_RED, SCS_GREEN, SCS_BLUE, SCS_ALPHA, SCS_ZERO, SCS_ONE
783 *
784 * which is simply adding 4 then modding by 8 (or anding with 7).
785 *
786 * We then may need to apply workarounds for textureGather hardware bugs.
787 */
788 static enum isl_channel_select
789 pipe_swizzle_to_isl_channel(enum pipe_swizzle swizzle)
790 {
791 return (swizzle + 4) & 7;
792 }
793
794 static struct pipe_sampler_view *
795 iris_create_sampler_view(struct pipe_context *ctx,
796 struct pipe_resource *tex,
797 const struct pipe_sampler_view *tmpl)
798 {
799 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
800 struct iris_resource *itex = (struct iris_resource *) tex;
801 struct iris_sampler_view *isv = calloc(1, sizeof(struct iris_sampler_view));
802
803 if (!isv)
804 return NULL;
805
806 /* initialize base object */
807 isv->pipe = *tmpl;
808 isv->pipe.context = ctx;
809 isv->pipe.texture = NULL;
810 pipe_reference_init(&isv->pipe.reference, 1);
811 pipe_resource_reference(&isv->pipe.texture, tex);
812
813 /* XXX: do we need brw_get_texture_swizzle hacks here? */
814
815 isv->view = (struct isl_view) {
816 .format = iris_isl_format_for_pipe_format(tmpl->format),
817 .base_level = tmpl->u.tex.first_level,
818 .levels = tmpl->u.tex.last_level - tmpl->u.tex.first_level + 1,
819 .base_array_layer = tmpl->u.tex.first_layer,
820 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
821 .swizzle = (struct isl_swizzle) {
822 .r = pipe_swizzle_to_isl_channel(tmpl->swizzle_r),
823 .g = pipe_swizzle_to_isl_channel(tmpl->swizzle_g),
824 .b = pipe_swizzle_to_isl_channel(tmpl->swizzle_b),
825 .a = pipe_swizzle_to_isl_channel(tmpl->swizzle_a),
826 },
827 .usage = ISL_SURF_USAGE_TEXTURE_BIT,
828 };
829
830 isl_surf_fill_state(&screen->isl_dev, isv->surface_state,
831 .surf = &itex->surf, .view = &isv->view,
832 .mocs = MOCS_WB);
833 // .address = ...
834 // .aux_surf =
835 // .clear_color = clear_color,
836
837 return &isv->pipe;
838 }
839
840 struct iris_surface {
841 struct pipe_surface pipe;
842 struct isl_view view;
843 uint32_t surface_state[GENX(RENDER_SURFACE_STATE_length)];
844 };
845
846 static struct pipe_surface *
847 iris_create_surface(struct pipe_context *ctx,
848 struct pipe_resource *tex,
849 const struct pipe_surface *tmpl)
850 {
851 struct iris_screen *screen = (struct iris_screen *)ctx->screen;
852 struct iris_surface *surf = calloc(1, sizeof(struct iris_surface));
853 struct pipe_surface *psurf = &surf->pipe;
854 struct iris_resource *itex = (struct iris_resource *) tex;
855
856 if (!surf)
857 return NULL;
858
859 pipe_reference_init(&psurf->reference, 1);
860 pipe_resource_reference(&psurf->texture, tex);
861 psurf->context = ctx;
862 psurf->format = tmpl->format;
863 psurf->width = tex->width0;
864 psurf->height = tex->height0;
865 psurf->texture = tex;
866 psurf->u.tex.first_layer = tmpl->u.tex.first_layer;
867 psurf->u.tex.last_layer = tmpl->u.tex.last_layer;
868 psurf->u.tex.level = tmpl->u.tex.level;
869
870 surf->view = (struct isl_view) {
871 .format = iris_isl_format_for_pipe_format(tmpl->format),
872 .base_level = tmpl->u.tex.level,
873 .levels = 1,
874 .base_array_layer = tmpl->u.tex.first_layer,
875 .array_len = tmpl->u.tex.last_layer - tmpl->u.tex.first_layer + 1,
876 .swizzle = ISL_SWIZZLE_IDENTITY,
877 // XXX: DEPTH_BIt, STENCIL_BIT...CUBE_BIT? Other bits?!
878 .usage = ISL_SURF_USAGE_RENDER_TARGET_BIT,
879 };
880
881 isl_surf_fill_state(&screen->isl_dev, surf->surface_state,
882 .surf = &itex->surf, .view = &surf->view,
883 .mocs = MOCS_WB);
884 // .address = ...
885 // .aux_surf =
886 // .clear_color = clear_color,
887
888 return psurf;
889 }
890
891 static void
892 iris_set_sampler_views(struct pipe_context *ctx,
893 enum pipe_shader_type shader,
894 unsigned start, unsigned count,
895 struct pipe_sampler_view **views)
896 {
897 }
898
899 static void
900 iris_set_clip_state(struct pipe_context *ctx,
901 const struct pipe_clip_state *state)
902 {
903 }
904
905 static void
906 iris_set_polygon_stipple(struct pipe_context *ctx,
907 const struct pipe_poly_stipple *state)
908 {
909 struct iris_context *ice = (struct iris_context *) ctx;
910 memcpy(&ice->state.poly_stipple, state, sizeof(*state));
911 ice->state.dirty |= IRIS_DIRTY_POLYGON_STIPPLE;
912 }
913
914 static void
915 iris_set_sample_mask(struct pipe_context *ctx, unsigned sample_mask)
916 {
917 struct iris_context *ice = (struct iris_context *) ctx;
918
919 ice->state.sample_mask = sample_mask;
920 ice->state.dirty |= IRIS_DIRTY_SAMPLE_MASK;
921 }
922
923 static void
924 iris_set_scissor_states(struct pipe_context *ctx,
925 unsigned start_slot,
926 unsigned num_scissors,
927 const struct pipe_scissor_state *state)
928 {
929 struct iris_context *ice = (struct iris_context *) ctx;
930
931 // XXX: start_slot
932 ice->state.num_scissors = num_scissors;
933
934 for (unsigned i = start_slot; i < start_slot + num_scissors; i++) {
935 ice->state.scissors[i] = *state;
936 }
937
938 ice->state.dirty |= IRIS_DIRTY_SCISSOR_RECT;
939 }
940
941 static void
942 iris_set_stencil_ref(struct pipe_context *ctx,
943 const struct pipe_stencil_ref *state)
944 {
945 struct iris_context *ice = (struct iris_context *) ctx;
946 memcpy(&ice->state.stencil_ref, state, sizeof(*state));
947 ice->state.dirty |= IRIS_DIRTY_WM_DEPTH_STENCIL;
948 }
949
950
951 struct iris_viewport_state {
952 uint32_t sf_cl_vp[GENX(SF_CLIP_VIEWPORT_length)];
953 };
954
955 static float
956 extent_from_matrix(const struct pipe_viewport_state *state, int axis)
957 {
958 return fabsf(state->scale[axis]) * state->translate[axis];
959 }
960
961 #if 0
962 static void
963 calculate_guardband_size(uint32_t fb_width, uint32_t fb_height,
964 float m00, float m11, float m30, float m31,
965 float *xmin, float *xmax,
966 float *ymin, float *ymax)
967 {
968 /* According to the "Vertex X,Y Clamping and Quantization" section of the
969 * Strips and Fans documentation:
970 *
971 * "The vertex X and Y screen-space coordinates are also /clamped/ to the
972 * fixed-point "guardband" range supported by the rasterization hardware"
973 *
974 * and
975 *
976 * "In almost all circumstances, if an object’s vertices are actually
977 * modified by this clamping (i.e., had X or Y coordinates outside of
978 * the guardband extent the rendered object will not match the intended
979 * result. Therefore software should take steps to ensure that this does
980 * not happen - e.g., by clipping objects such that they do not exceed
981 * these limits after the Drawing Rectangle is applied."
982 *
983 * I believe the fundamental restriction is that the rasterizer (in
984 * the SF/WM stages) have a limit on the number of pixels that can be
985 * rasterized. We need to ensure any coordinates beyond the rasterizer
986 * limit are handled by the clipper. So effectively that limit becomes
987 * the clipper's guardband size.
988 *
989 * It goes on to say:
990 *
991 * "In addition, in order to be correctly rendered, objects must have a
992 * screenspace bounding box not exceeding 8K in the X or Y direction.
993 * This additional restriction must also be comprehended by software,
994 * i.e., enforced by use of clipping."
995 *
996 * This makes no sense. Gen7+ hardware supports 16K render targets,
997 * and you definitely need to be able to draw polygons that fill the
998 * surface. Our assumption is that the rasterizer was limited to 8K
999 * on Sandybridge, which only supports 8K surfaces, and it was actually
1000 * increased to 16K on Ivybridge and later.
1001 *
1002 * So, limit the guardband to 16K on Gen7+ and 8K on Sandybridge.
1003 */
1004 const float gb_size = GEN_GEN >= 7 ? 16384.0f : 8192.0f;
1005
1006 if (m00 != 0 && m11 != 0) {
1007 /* First, we compute the screen-space render area */
1008 const float ss_ra_xmin = MIN3( 0, m30 + m00, m30 - m00);
1009 const float ss_ra_xmax = MAX3( fb_width, m30 + m00, m30 - m00);
1010 const float ss_ra_ymin = MIN3( 0, m31 + m11, m31 - m11);
1011 const float ss_ra_ymax = MAX3(fb_height, m31 + m11, m31 - m11);
1012
1013 /* We want the guardband to be centered on that */
1014 const float ss_gb_xmin = (ss_ra_xmin + ss_ra_xmax) / 2 - gb_size;
1015 const float ss_gb_xmax = (ss_ra_xmin + ss_ra_xmax) / 2 + gb_size;
1016 const float ss_gb_ymin = (ss_ra_ymin + ss_ra_ymax) / 2 - gb_size;
1017 const float ss_gb_ymax = (ss_ra_ymin + ss_ra_ymax) / 2 + gb_size;
1018
1019 /* Now we need it in native device coordinates */
1020 const float ndc_gb_xmin = (ss_gb_xmin - m30) / m00;
1021 const float ndc_gb_xmax = (ss_gb_xmax - m30) / m00;
1022 const float ndc_gb_ymin = (ss_gb_ymin - m31) / m11;
1023 const float ndc_gb_ymax = (ss_gb_ymax - m31) / m11;
1024
1025 /* Thanks to Y-flipping and ORIGIN_UPPER_LEFT, the Y coordinates may be
1026 * flipped upside-down. X should be fine though.
1027 */
1028 assert(ndc_gb_xmin <= ndc_gb_xmax);
1029 *xmin = ndc_gb_xmin;
1030 *xmax = ndc_gb_xmax;
1031 *ymin = MIN2(ndc_gb_ymin, ndc_gb_ymax);
1032 *ymax = MAX2(ndc_gb_ymin, ndc_gb_ymax);
1033 } else {
1034 /* The viewport scales to 0, so nothing will be rendered. */
1035 *xmin = 0.0f;
1036 *xmax = 0.0f;
1037 *ymin = 0.0f;
1038 *ymax = 0.0f;
1039 }
1040 }
1041 #endif
1042
1043 static void
1044 iris_set_viewport_states(struct pipe_context *ctx,
1045 unsigned start_slot,
1046 unsigned num_viewports,
1047 const struct pipe_viewport_state *state)
1048 {
1049 struct iris_context *ice = (struct iris_context *) ctx;
1050 struct iris_viewport_state *cso =
1051 malloc(sizeof(struct iris_viewport_state));
1052
1053 for (unsigned i = start_slot; i < start_slot + num_viewports; i++) {
1054 float x_extent = extent_from_matrix(&state[i], 0);
1055 float y_extent = extent_from_matrix(&state[i], 1);
1056
1057 iris_pack_state(GENX(SF_CLIP_VIEWPORT), cso->sf_cl_vp, vp) {
1058 vp.ViewportMatrixElementm00 = state[i].scale[0];
1059 vp.ViewportMatrixElementm11 = state[i].scale[1];
1060 vp.ViewportMatrixElementm22 = state[i].scale[2];
1061 vp.ViewportMatrixElementm30 = state[i].translate[0];
1062 vp.ViewportMatrixElementm31 = state[i].translate[1];
1063 vp.ViewportMatrixElementm32 = state[i].translate[2];
1064 /* XXX: in i965 this is computed based on the drawbuffer size,
1065 * but we don't have that here...
1066 */
1067 vp.XMinClipGuardband = -1.0;
1068 vp.XMaxClipGuardband = 1.0;
1069 vp.YMinClipGuardband = -1.0;
1070 vp.YMaxClipGuardband = 1.0;
1071 vp.XMinViewPort = -x_extent;
1072 vp.XMaxViewPort = x_extent;
1073 vp.YMinViewPort = -y_extent;
1074 vp.YMaxViewPort = y_extent;
1075 }
1076 }
1077
1078 ice->state.cso_vp = cso;
1079 // XXX: start_slot
1080 ice->state.num_viewports = num_viewports;
1081 ice->state.dirty |= IRIS_DIRTY_SF_CL_VIEWPORT;
1082 }
1083
1084 struct iris_depth_state
1085 {
1086 uint32_t depth_buffer[GENX(3DSTATE_DEPTH_BUFFER_length)];
1087 uint32_t hier_depth_buffer[GENX(3DSTATE_HIER_DEPTH_BUFFER_length)];
1088 uint32_t stencil_buffer[GENX(3DSTATE_STENCIL_BUFFER_length)];
1089 };
1090
1091 static void
1092 iris_set_framebuffer_state(struct pipe_context *ctx,
1093 const struct pipe_framebuffer_state *state)
1094 {
1095 struct iris_context *ice = (struct iris_context *) ctx;
1096 struct pipe_framebuffer_state *cso = &ice->state.framebuffer;
1097
1098 if (cso->samples != state->samples) {
1099 ice->state.dirty |= IRIS_DIRTY_MULTISAMPLE;
1100 }
1101
1102 cso->width = state->width;
1103 cso->height = state->height;
1104 cso->layers = state->layers;
1105 cso->samples = state->samples;
1106
1107 unsigned i;
1108 for (i = 0; i < state->nr_cbufs; i++)
1109 pipe_surface_reference(&cso->cbufs[i], state->cbufs[i]);
1110 for (; i < cso->nr_cbufs; i++)
1111 pipe_surface_reference(&cso->cbufs[i], NULL);
1112
1113 cso->nr_cbufs = state->nr_cbufs;
1114
1115 pipe_surface_reference(&cso->zsbuf, state->zsbuf);
1116
1117 struct isl_depth_stencil_hiz_emit_info info = {
1118 .mocs = MOCS_WB,
1119 };
1120
1121 // XXX: depth buffers
1122 }
1123
1124 static void
1125 iris_set_constant_buffer(struct pipe_context *ctx,
1126 enum pipe_shader_type shader, uint index,
1127 const struct pipe_constant_buffer *cb)
1128 {
1129 }
1130
1131
1132 static void
1133 iris_sampler_view_destroy(struct pipe_context *ctx,
1134 struct pipe_sampler_view *state)
1135 {
1136 pipe_resource_reference(&state->texture, NULL);
1137 free(state);
1138 }
1139
1140
1141 static void
1142 iris_surface_destroy(struct pipe_context *ctx, struct pipe_surface *surface)
1143 {
1144 pipe_resource_reference(&surface->texture, NULL);
1145 free(surface);
1146 }
1147
1148 static void
1149 iris_delete_state(struct pipe_context *ctx, void *state)
1150 {
1151 free(state);
1152 }
1153
1154 struct iris_vertex_buffer_state {
1155 uint32_t vertex_buffers[1 + 33 * GENX(VERTEX_BUFFER_STATE_length)];
1156 struct iris_address bos[33];
1157 unsigned num_buffers;
1158 };
1159
1160 static void
1161 iris_free_vertex_buffers(struct iris_vertex_buffer_state *cso)
1162 {
1163 if (cso) {
1164 for (unsigned i = 0; i < cso->num_buffers; i++)
1165 iris_bo_unreference(cso->bos[i].bo);
1166 free(cso);
1167 }
1168 }
1169
1170 static void
1171 iris_set_vertex_buffers(struct pipe_context *ctx,
1172 unsigned start_slot, unsigned count,
1173 const struct pipe_vertex_buffer *buffers)
1174 {
1175 struct iris_context *ice = (struct iris_context *) ctx;
1176 struct iris_vertex_buffer_state *cso =
1177 malloc(sizeof(struct iris_vertex_buffer_state));
1178
1179 /* If there are no buffers, do nothing. We can leave the stale
1180 * 3DSTATE_VERTEX_BUFFERS in place - as long as there are no vertex
1181 * elements that point to them, it should be fine.
1182 */
1183 if (!buffers)
1184 return;
1185
1186 iris_free_vertex_buffers(ice->state.cso_vertex_buffers);
1187
1188 cso->num_buffers = count;
1189
1190 iris_pack_state(GENX(3DSTATE_VERTEX_BUFFERS), cso->vertex_buffers, vb) {
1191 vb.DWordLength = 4 * cso->num_buffers - 1;
1192 }
1193
1194 uint32_t *vb_pack_dest = &cso->vertex_buffers[1];
1195
1196 for (unsigned i = 0; i < count; i++) {
1197 assert(!buffers[i].is_user_buffer);
1198
1199 struct iris_resource *res = (void *) buffers[i].buffer.resource;
1200 iris_bo_reference(res->bo);
1201 cso->bos[i] = ro_bo(res->bo, buffers[i].buffer_offset);
1202
1203 iris_pack_state(GENX(VERTEX_BUFFER_STATE), vb_pack_dest, vb) {
1204 vb.VertexBufferIndex = start_slot + i;
1205 vb.MOCS = MOCS_WB;
1206 vb.AddressModifyEnable = true;
1207 vb.BufferPitch = buffers[i].stride;
1208 //vb.BufferStartingAddress = ro_bo(bo, buffers[i].buffer_offset);
1209 //vb.BufferSize = bo->size;
1210 }
1211
1212 vb_pack_dest += GENX(VERTEX_BUFFER_STATE_length);
1213 }
1214
1215 ice->state.cso_vertex_buffers = cso;
1216 ice->state.dirty |= IRIS_DIRTY_VERTEX_BUFFERS;
1217 }
1218
1219 struct iris_vertex_element_state {
1220 uint32_t vertex_elements[1 + 33 * GENX(VERTEX_ELEMENT_STATE_length)];
1221 uint32_t vf_instancing[GENX(3DSTATE_VF_INSTANCING_length)][33];
1222 unsigned count;
1223 };
1224
1225 static void *
1226 iris_create_vertex_elements(struct pipe_context *ctx,
1227 unsigned count,
1228 const struct pipe_vertex_element *state)
1229 {
1230 struct iris_vertex_element_state *cso =
1231 malloc(sizeof(struct iris_vertex_element_state));
1232
1233 cso->count = count;
1234
1235 /* TODO:
1236 * - create edge flag one
1237 * - create SGV ones
1238 * - if those are necessary, use count + 1/2/3... OR in the length
1239 */
1240 iris_pack_state(GENX(3DSTATE_VERTEX_ELEMENTS), cso->vertex_elements, ve);
1241
1242 uint32_t *ve_pack_dest = &cso->vertex_elements[1];
1243
1244 for (int i = 0; i < count; i++) {
1245 iris_pack_state(GENX(VERTEX_ELEMENT_STATE), ve_pack_dest, ve) {
1246 ve.VertexBufferIndex = state[i].vertex_buffer_index;
1247 ve.Valid = true;
1248 ve.SourceElementOffset = state[i].src_offset;
1249 ve.SourceElementFormat =
1250 iris_isl_format_for_pipe_format(state[i].src_format);
1251 }
1252
1253 iris_pack_state(GENX(3DSTATE_VF_INSTANCING), cso->vf_instancing[i], vi) {
1254 vi.VertexElementIndex = i;
1255 vi.InstancingEnable = state[i].instance_divisor > 0;
1256 vi.InstanceDataStepRate = state[i].instance_divisor;
1257 }
1258
1259 ve_pack_dest += GENX(VERTEX_ELEMENT_STATE_length);
1260 }
1261
1262 return cso;
1263 }
1264
1265 static void
1266 iris_bind_vertex_elements_state(struct pipe_context *ctx, void *state)
1267 {
1268 struct iris_context *ice = (struct iris_context *) ctx;
1269
1270 ice->state.cso_vertex_elements = state;
1271 ice->state.dirty |= IRIS_DIRTY_VERTEX_ELEMENTS;
1272 }
1273
1274 static void *
1275 iris_create_compute_state(struct pipe_context *ctx,
1276 const struct pipe_compute_state *state)
1277 {
1278 return malloc(1);
1279 }
1280
1281 static struct pipe_stream_output_target *
1282 iris_create_stream_output_target(struct pipe_context *ctx,
1283 struct pipe_resource *res,
1284 unsigned buffer_offset,
1285 unsigned buffer_size)
1286 {
1287 struct pipe_stream_output_target *t =
1288 CALLOC_STRUCT(pipe_stream_output_target);
1289 if (!t)
1290 return NULL;
1291
1292 pipe_reference_init(&t->reference, 1);
1293 pipe_resource_reference(&t->buffer, res);
1294 t->buffer_offset = buffer_offset;
1295 t->buffer_size = buffer_size;
1296 return t;
1297 }
1298
1299 static void
1300 iris_stream_output_target_destroy(struct pipe_context *ctx,
1301 struct pipe_stream_output_target *t)
1302 {
1303 pipe_resource_reference(&t->buffer, NULL);
1304 free(t);
1305 }
1306
1307 static void
1308 iris_set_stream_output_targets(struct pipe_context *ctx,
1309 unsigned num_targets,
1310 struct pipe_stream_output_target **targets,
1311 const unsigned *offsets)
1312 {
1313 }
1314
1315 void
1316 iris_setup_state_base_address(struct iris_context *ice,
1317 struct iris_batch *batch,
1318 struct iris_bo *instruction_bo)
1319 {
1320 if (!(ice->state.dirty & IRIS_DIRTY_STATE_BASE_ADDRESS))
1321 return;
1322
1323 //iris_batchbuffer_flush(...)
1324
1325 ice->state.dirty &= ~IRIS_DIRTY_STATE_BASE_ADDRESS;
1326
1327 /* XXX: PIPE_CONTROLs */
1328
1329 iris_emit_cmd(batch, GENX(STATE_BASE_ADDRESS), sba) {
1330 #if 0
1331 // XXX: MOCS is stupid for this.
1332 sba.GeneralStateMemoryObjectControlState = MOCS_WB;
1333 sba.StatelessDataPortAccessMemoryObjectControlState = MOCS_WB;
1334 sba.SurfaceStateMemoryObjectControlState = MOCS_WB;
1335 sba.DynamicStateMemoryObjectControlState = MOCS_WB;
1336 sba.IndirectObjectMemoryObjectControlState = MOCS_WB;
1337 sba.InstructionMemoryObjectControlState = MOCS_WB;
1338 sba.BindlessSurfaceStateMemoryObjectControlState = MOCS_WB;
1339 #endif
1340
1341 sba.GeneralStateBaseAddressModifyEnable = true;
1342 sba.SurfaceStateBaseAddressModifyEnable = true;
1343 sba.DynamicStateBaseAddressModifyEnable = true;
1344 sba.IndirectObjectBaseAddressModifyEnable = true;
1345 sba.InstructionBaseAddressModifyEnable = true;
1346 sba.GeneralStateBufferSizeModifyEnable = true;
1347 sba.DynamicStateBufferSizeModifyEnable = true;
1348 sba.BindlessSurfaceStateBaseAddressModifyEnable = true;
1349 sba.IndirectObjectBufferSizeModifyEnable = true;
1350 sba.InstructionBuffersizeModifyEnable = true;
1351
1352 sba.SurfaceStateBaseAddress = ro_bo(batch->statebuf.bo, 0);
1353 sba.DynamicStateBaseAddress = ro_bo(batch->statebuf.bo, 0);
1354 sba.InstructionBaseAddress = ro_bo(instruction_bo, 0);
1355
1356 sba.GeneralStateBufferSize = 0xfffff000;
1357 sba.DynamicStateBufferSize = ALIGN(MAX_STATE_SIZE, 4096);
1358 sba.IndirectObjectBufferSize = 0xfffff000;
1359 sba.InstructionBufferSize = ALIGN(ice->shaders.cache.bo->size, 4096);
1360 sba.BindlessSurfaceStateSize = 0;
1361 }
1362 }
1363
1364 void
1365 iris_upload_render_state(struct iris_context *ice,
1366 struct iris_batch *batch,
1367 const struct pipe_draw_info *draw)
1368 {
1369 const uint64_t dirty = ice->state.dirty;
1370
1371 if (dirty & IRIS_DIRTY_WM_DEPTH_STENCIL) {
1372 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
1373 struct pipe_stencil_ref *p_stencil_refs = &ice->state.stencil_ref;
1374
1375 uint32_t stencil_refs[GENX(3DSTATE_WM_DEPTH_STENCIL_length)];
1376 iris_pack_command(GENX(3DSTATE_WM_DEPTH_STENCIL), &stencil_refs, wmds) {
1377 wmds.StencilReferenceValue = p_stencil_refs->ref_value[0];
1378 wmds.BackfaceStencilReferenceValue = p_stencil_refs->ref_value[1];
1379 }
1380 iris_emit_merge(batch, cso->wmds, stencil_refs, ARRAY_SIZE(cso->wmds));
1381 }
1382
1383 if (dirty & IRIS_DIRTY_CC_VIEWPORT) {
1384 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
1385 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_CC), ptr) {
1386 ptr.CCViewportPointer =
1387 iris_emit_state(batch, cso->cc_vp, sizeof(cso->cc_vp), 32);
1388 }
1389 }
1390
1391 if (dirty & IRIS_DIRTY_PS_BLEND) {
1392 struct iris_blend_state *cso = ice->state.cso_blend;
1393 iris_batch_emit(batch, cso->ps_blend, sizeof(cso->ps_blend));
1394 }
1395
1396 if (dirty & IRIS_DIRTY_BLEND_STATE) {
1397 //struct iris_blend_state *cso = ice->state.cso_blend;
1398 // XXX: 3DSTATE_BLEND_STATE_POINTERS - BLEND_STATE
1399 // -> from iris_blend_state (most) + iris_depth_stencil_alpha_state
1400 // (alpha test function/enable) + has writeable RT from ???????
1401 }
1402
1403 if (dirty & IRIS_DIRTY_SF_CL_VIEWPORT) {
1404 struct iris_viewport_state *cso = ice->state.cso_vp;
1405 iris_emit_cmd(batch, GENX(3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP), ptr) {
1406 ptr.SFClipViewportPointer =
1407 iris_emit_state(batch, cso->sf_cl_vp, sizeof(cso->sf_cl_vp), 64);
1408 }
1409 }
1410
1411 if (dirty & IRIS_DIRTY_CLIP) {
1412 struct iris_rasterizer_state *cso = ice->state.cso_rast;
1413
1414 uint32_t dynamic_clip[GENX(3DSTATE_CLIP_length)];
1415 iris_pack_command(GENX(3DSTATE_CLIP), &dynamic_clip, cl) {
1416 //.NonPerspectiveBarycentricEnable = <comes from FS prog> :(
1417 //.ForceZeroRTAIndexEnable = <comes from FB layers being 0>
1418 // also userclip stuffs...
1419 }
1420 iris_emit_merge(batch, cso->clip, dynamic_clip, ARRAY_SIZE(cso->clip));
1421 }
1422
1423 if (dirty & IRIS_DIRTY_RASTER) {
1424 struct iris_rasterizer_state *cso = ice->state.cso_rast;
1425 iris_batch_emit(batch, cso->raster, sizeof(cso->raster));
1426 iris_batch_emit(batch, cso->sf, sizeof(cso->sf));
1427 }
1428
1429 if (dirty & IRIS_DIRTY_LINE_STIPPLE) {
1430 struct iris_rasterizer_state *cso = ice->state.cso_rast;
1431 iris_batch_emit(batch, cso->line_stipple, sizeof(cso->line_stipple));
1432 }
1433
1434 if (dirty & IRIS_DIRTY_SCISSOR) {
1435 uint32_t scissor_offset =
1436 iris_emit_state(batch, ice->state.scissors,
1437 sizeof(struct pipe_scissor_state) *
1438 ice->state.num_scissors, 32);
1439
1440 iris_emit_cmd(batch, GENX(3DSTATE_SCISSOR_STATE_POINTERS), ptr) {
1441 ptr.ScissorRectPointer = scissor_offset;
1442 }
1443 }
1444
1445 if (dirty & IRIS_DIRTY_POLYGON_STIPPLE) {
1446 iris_emit_cmd(batch, GENX(3DSTATE_POLY_STIPPLE_PATTERN), poly) {
1447 for (int i = 0; i < 32; i++) {
1448 poly.PatternRow[i] = ice->state.poly_stipple.stipple[i];
1449 }
1450 }
1451 }
1452
1453 if (dirty & IRIS_DIRTY_COLOR_CALC_STATE) {
1454 struct iris_depth_stencil_alpha_state *cso = ice->state.cso_zsa;
1455 uint32_t cc_offset;
1456 void *cc_map =
1457 iris_alloc_state(batch,
1458 sizeof(uint32_t) * GENX(COLOR_CALC_STATE_length),
1459 64, &cc_offset);
1460 iris_pack_state(GENX(COLOR_CALC_STATE), cc_map, cc) {
1461 cc.AlphaTestFormat = ALPHATEST_FLOAT32;
1462 cc.AlphaReferenceValueAsFLOAT32 = cso->alpha.ref_value;
1463 cc.BlendConstantColorRed = ice->state.blend_color.color[0];
1464 cc.BlendConstantColorGreen = ice->state.blend_color.color[1];
1465 cc.BlendConstantColorBlue = ice->state.blend_color.color[2];
1466 cc.BlendConstantColorAlpha = ice->state.blend_color.color[3];
1467 }
1468 iris_emit_cmd(batch, GENX(3DSTATE_CC_STATE_POINTERS), ptr) {
1469 ptr.ColorCalcStatePointer = cc_offset;
1470 ptr.ColorCalcStatePointerValid = true;
1471 }
1472 }
1473
1474 if (dirty & IRIS_DIRTY_VERTEX_BUFFERS) {
1475 struct iris_vertex_buffer_state *cso = ice->state.cso_vertex_buffers;
1476
1477 STATIC_ASSERT(GENX(VERTEX_BUFFER_STATE_length) == 4);
1478
1479 uint64_t *addr = batch->cmdbuf.map_next + sizeof(uint32_t) *
1480 (1 + GENX(VERTEX_BUFFER_STATE_BufferStartingAddress_bits) % 32);
1481 uint32_t *delta = cso->vertex_buffers +
1482 (2 + GENX(VERTEX_BUFFER_STATE_BufferStartingAddress_bits) % 32);
1483
1484 iris_batch_emit(batch, cso->vertex_buffers,
1485 sizeof(uint32_t) * (1 + 4 * cso->num_buffers));
1486
1487 for (unsigned i = 0; i < cso->num_buffers; i++) {
1488 *addr = iris_batch_reloc(batch, (void *) addr - batch->cmdbuf.map,
1489 cso->bos[i].bo, cso->bos[i].offset +
1490 *delta, cso->bos[i].reloc_flags);
1491 addr = (void *) addr + 16;
1492 delta = (void *) delta + 16;
1493 }
1494 }
1495
1496 if (dirty & IRIS_DIRTY_VERTEX_ELEMENTS) {
1497 struct iris_vertex_element_state *cso = ice->state.cso_vertex_elements;
1498 iris_batch_emit(batch, cso->vertex_elements, sizeof(uint32_t) *
1499 (1 + cso->count * GENX(VERTEX_ELEMENT_STATE_length)));
1500 for (int i = 0; i < cso->count; i++) {
1501 iris_batch_emit(batch, cso->vf_instancing[i],
1502 sizeof(cso->vf_instancing[0]));
1503 }
1504 for (int i = 0; i < cso->count; i++) {
1505 /* TODO: vertexid, instanceid support */
1506 iris_emit_cmd(batch, GENX(3DSTATE_VF_SGVS), sgvs);
1507 }
1508 }
1509
1510 if (dirty & IRIS_DIRTY_MULTISAMPLE) {
1511 iris_emit_cmd(batch, GENX(3DSTATE_MULTISAMPLE), ms) {
1512 ms.PixelLocation =
1513 ice->state.cso_rast->half_pixel_center ? CENTER : UL_CORNER;
1514 if (ice->state.framebuffer.samples > 0)
1515 ms.NumberofMultisamples = ffs(ice->state.framebuffer.samples) - 1;
1516 }
1517 }
1518
1519 if (dirty & IRIS_DIRTY_SAMPLE_MASK) {
1520 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLE_MASK), ms) {
1521 ms.SampleMask = ice->state.sample_mask;
1522 }
1523 }
1524
1525 for (int stage = 0; stage <= MESA_SHADER_FRAGMENT; stage++) {
1526 if (!(dirty & (IRIS_DIRTY_SAMPLER_STATES_VS << stage)))
1527 continue;
1528
1529 // XXX: get sampler count from shader; don't emit them all...
1530 const int count = IRIS_MAX_TEXTURE_SAMPLERS;
1531
1532 uint32_t offset;
1533 uint32_t *map = iris_alloc_state(batch,
1534 count * 4 * GENX(SAMPLER_STATE_length),
1535 32, &offset);
1536
1537 for (int i = 0; i < count; i++) {
1538 // XXX: when we have a correct count, these better be bound
1539 if (!ice->state.samplers[stage][i])
1540 continue;
1541 memcpy(map, ice->state.samplers[stage][i]->sampler_state,
1542 4 * GENX(SAMPLER_STATE_length));
1543 map += GENX(SAMPLER_STATE_length);
1544 }
1545
1546 iris_emit_cmd(batch, GENX(3DSTATE_SAMPLER_STATE_POINTERS_VS), ptr) {
1547 ptr._3DCommandSubOpcode = 43 + stage;
1548 ptr.PointertoVSSamplerState = offset;
1549 }
1550 }
1551
1552 if (1) {
1553 iris_emit_cmd(batch, GENX(3DSTATE_VF_TOPOLOGY), topo) {
1554 topo.PrimitiveTopologyType =
1555 translate_prim_type(draw->mode, draw->vertices_per_patch);
1556 }
1557 }
1558
1559 if (1) {
1560 iris_emit_cmd(batch, GENX(3DSTATE_VF), vf) {
1561 if (draw->primitive_restart) {
1562 vf.IndexedDrawCutIndexEnable = true;
1563 vf.CutIndex = draw->restart_index;
1564 }
1565 }
1566 }
1567
1568 // draw->index_size > 0
1569 if (draw->index_size > 0) {
1570 struct iris_resource *res = (struct iris_resource *)draw->index.resource;
1571
1572 assert(!draw->has_user_indices);
1573
1574 iris_emit_cmd(batch, GENX(3DSTATE_INDEX_BUFFER), ib) {
1575 ib.IndexFormat = draw->index_size;
1576 ib.MOCS = MOCS_WB;
1577 ib.BufferSize = res->bo->size;
1578 // XXX: gah, addresses :( need two different combine address funcs
1579 // ib.BufferStartingAddress = res->bo;
1580 }
1581 }
1582
1583 assert(!draw->indirect); // XXX: indirect support
1584
1585 iris_emit_cmd(batch, GENX(3DPRIMITIVE), prim) {
1586 prim.StartInstanceLocation = draw->start_instance;
1587 prim.InstanceCount = draw->instance_count;
1588
1589 // XXX: this is probably bonkers.
1590 prim.StartVertexLocation = draw->start;
1591
1592 if (draw->index_size) {
1593 prim.BaseVertexLocation += draw->index_bias;
1594 } else {
1595 prim.StartVertexLocation += draw->index_bias;
1596 }
1597
1598 //prim.BaseVertexLocation = ...;
1599 }
1600 #if 0
1601 l3 configuration
1602
1603 3DSTATE_URB_*
1604 -> TODO
1605
1606 3DSTATE_CONSTANT_* - push constants
1607 -> TODO
1608
1609 Surfaces:
1610 - pull constants
1611 - ubos/ssbos/abos
1612 - images
1613 - textures
1614 - render targets - write and read
1615 3DSTATE_BINDING_TABLE_POINTERS_*
1616 -> TODO
1617
1618 3DSTATE_VS
1619 3DSTATE_HS
1620 3DSTATE_TE
1621 3DSTATE_DS
1622 3DSTATE_GS
1623 3DSTATE_PS_EXTRA
1624 3DSTATE_PS
1625 3DSTATE_STREAMOUT
1626 3DSTATE_SO_BUFFER
1627 3DSTATE_SO_DECL_LIST
1628
1629 3DSTATE_WM
1630 -> iris_raster_state + FS state (barycentric, EDSC)
1631 3DSTATE_SBE
1632 -> iris_raster_state (point sprite texture coordinate origin)
1633 -> bunch of shader state...
1634 3DSTATE_SBE_SWIZ
1635 -> FS state
1636
1637 3DSTATE_DEPTH_BUFFER
1638 3DSTATE_HIER_DEPTH_BUFFER
1639 3DSTATE_STENCIL_BUFFER
1640 3DSTATE_CLEAR_PARAMS
1641 -> iris_framebuffer_state?
1642 #endif
1643 }
1644
1645 static void
1646 iris_bind_state(struct pipe_context *ctx, void *state)
1647 {
1648 }
1649
1650 void
1651 iris_destroy_state(struct iris_context *ice)
1652 {
1653 // XXX: unreference resources/surfaces.
1654 for (unsigned i = 0; i < ice->state.framebuffer.nr_cbufs; i++) {
1655 pipe_surface_reference(&ice->state.framebuffer.cbufs[i], NULL);
1656 }
1657 pipe_surface_reference(&ice->state.framebuffer.zsbuf, NULL);
1658 }
1659
1660 void
1661 iris_init_state(struct iris_context *ice)
1662 {
1663 struct pipe_context *ctx = &ice->ctx;
1664
1665 ice->state.dirty = ~0ull;
1666
1667 ctx->create_blend_state = iris_create_blend_state;
1668 ctx->create_depth_stencil_alpha_state = iris_create_zsa_state;
1669 ctx->create_rasterizer_state = iris_create_rasterizer_state;
1670 ctx->create_sampler_state = iris_create_sampler_state;
1671 ctx->create_sampler_view = iris_create_sampler_view;
1672 ctx->create_surface = iris_create_surface;
1673 ctx->create_vertex_elements_state = iris_create_vertex_elements;
1674 ctx->create_compute_state = iris_create_compute_state;
1675 ctx->bind_blend_state = iris_bind_blend_state;
1676 ctx->bind_depth_stencil_alpha_state = iris_bind_zsa_state;
1677 ctx->bind_sampler_states = iris_bind_sampler_states;
1678 ctx->bind_rasterizer_state = iris_bind_rasterizer_state;
1679 ctx->bind_vertex_elements_state = iris_bind_vertex_elements_state;
1680 ctx->bind_compute_state = iris_bind_state;
1681 ctx->delete_blend_state = iris_delete_state;
1682 ctx->delete_depth_stencil_alpha_state = iris_delete_state;
1683 ctx->delete_fs_state = iris_delete_state;
1684 ctx->delete_rasterizer_state = iris_delete_state;
1685 ctx->delete_sampler_state = iris_delete_state;
1686 ctx->delete_vertex_elements_state = iris_delete_state;
1687 ctx->delete_compute_state = iris_delete_state;
1688 ctx->delete_tcs_state = iris_delete_state;
1689 ctx->delete_tes_state = iris_delete_state;
1690 ctx->delete_gs_state = iris_delete_state;
1691 ctx->delete_vs_state = iris_delete_state;
1692 ctx->set_blend_color = iris_set_blend_color;
1693 ctx->set_clip_state = iris_set_clip_state;
1694 ctx->set_constant_buffer = iris_set_constant_buffer;
1695 ctx->set_sampler_views = iris_set_sampler_views;
1696 ctx->set_framebuffer_state = iris_set_framebuffer_state;
1697 ctx->set_polygon_stipple = iris_set_polygon_stipple;
1698 ctx->set_sample_mask = iris_set_sample_mask;
1699 ctx->set_scissor_states = iris_set_scissor_states;
1700 ctx->set_stencil_ref = iris_set_stencil_ref;
1701 ctx->set_vertex_buffers = iris_set_vertex_buffers;
1702 ctx->set_viewport_states = iris_set_viewport_states;
1703 ctx->sampler_view_destroy = iris_sampler_view_destroy;
1704 ctx->surface_destroy = iris_surface_destroy;
1705 ctx->draw_vbo = iris_draw_vbo;
1706 ctx->launch_grid = iris_launch_grid;
1707 ctx->create_stream_output_target = iris_create_stream_output_target;
1708 ctx->stream_output_target_destroy = iris_stream_output_target_destroy;
1709 ctx->set_stream_output_targets = iris_set_stream_output_targets;
1710 }