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