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