freedreno/a3xx: add ARB_instanced_arrays support
[mesa.git] / src / gallium / drivers / ilo / ilo_render_gen7.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2013 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
26 */
27
28 #include "genhw/genhw.h"
29 #include "util/u_dual_blend.h"
30
31 #include "ilo_blitter.h"
32 #include "ilo_builder_3d.h"
33 #include "ilo_builder_render.h"
34 #include "ilo_shader.h"
35 #include "ilo_state.h"
36 #include "ilo_render_gen.h"
37
38 /**
39 * A wrapper for gen6_PIPE_CONTROL().
40 */
41 static void
42 gen7_pipe_control(struct ilo_render *r, uint32_t dw1)
43 {
44 struct intel_bo *bo = (dw1 & GEN6_PIPE_CONTROL_WRITE__MASK) ?
45 r->workaround_bo : NULL;
46
47 ILO_DEV_ASSERT(r->dev, 7, 7.5);
48
49 if (dw1 & GEN6_PIPE_CONTROL_CS_STALL) {
50 /* CS stall cannot be set alone */
51 const uint32_t mask = GEN6_PIPE_CONTROL_RENDER_CACHE_FLUSH |
52 GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH |
53 GEN6_PIPE_CONTROL_PIXEL_SCOREBOARD_STALL |
54 GEN6_PIPE_CONTROL_DEPTH_STALL |
55 GEN6_PIPE_CONTROL_WRITE__MASK;
56 if (!(dw1 & mask))
57 dw1 |= GEN6_PIPE_CONTROL_PIXEL_SCOREBOARD_STALL;
58 }
59
60 gen6_PIPE_CONTROL(r->builder, dw1, bo, 0, 0);
61
62
63 r->state.current_pipe_control_dw1 |= dw1;
64 r->state.deferred_pipe_control_dw1 &= ~dw1;
65 }
66
67 static void
68 gen7_wa_post_3dstate_push_constant_alloc_ps(struct ilo_render *r)
69 {
70 /*
71 * From the Ivy Bridge PRM, volume 2 part 1, page 292:
72 *
73 * "A PIPE_CONTOL command with the CS Stall bit set must be programmed
74 * in the ring after this instruction
75 * (3DSTATE_PUSH_CONSTANT_ALLOC_PS)."
76 */
77 const uint32_t dw1 = GEN6_PIPE_CONTROL_CS_STALL;
78
79 ILO_DEV_ASSERT(r->dev, 7, 7.5);
80
81 r->state.deferred_pipe_control_dw1 |= dw1;
82 }
83
84 static void
85 gen7_wa_pre_vs(struct ilo_render *r)
86 {
87 /*
88 * From the Ivy Bridge PRM, volume 2 part 1, page 106:
89 *
90 * "A PIPE_CONTROL with Post-Sync Operation set to 1h and a depth stall
91 * needs to be sent just prior to any 3DSTATE_VS, 3DSTATE_URB_VS,
92 * 3DSTATE_CONSTANT_VS, 3DSTATE_BINDING_TABLE_POINTER_VS,
93 * 3DSTATE_SAMPLER_STATE_POINTER_VS command. Only one PIPE_CONTROL
94 * needs to be sent before any combination of VS associated 3DSTATE."
95 */
96 const uint32_t dw1 = GEN6_PIPE_CONTROL_DEPTH_STALL |
97 GEN6_PIPE_CONTROL_WRITE_IMM;
98
99 ILO_DEV_ASSERT(r->dev, 7, 7.5);
100
101 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
102 gen7_pipe_control(r, dw1);
103 }
104
105 static void
106 gen7_wa_pre_3dstate_sf_depth_bias(struct ilo_render *r)
107 {
108 /*
109 * From the Ivy Bridge PRM, volume 2 part 1, page 258:
110 *
111 * "Due to an HW issue driver needs to send a pipe control with stall
112 * when ever there is state change in depth bias related state (in
113 * 3DSTATE_SF)"
114 */
115 const uint32_t dw1 = GEN6_PIPE_CONTROL_CS_STALL;
116
117 ILO_DEV_ASSERT(r->dev, 7, 7.5);
118
119 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
120 gen7_pipe_control(r, dw1);
121 }
122
123 static void
124 gen7_wa_pre_3dstate_multisample(struct ilo_render *r)
125 {
126 /*
127 * From the Ivy Bridge PRM, volume 2 part 1, page 304:
128 *
129 * "Driver must ierarchi that all the caches in the depth pipe are
130 * flushed before this command (3DSTATE_MULTISAMPLE) is parsed. This
131 * requires driver to send a PIPE_CONTROL with a CS stall along with a
132 * Depth Flush prior to this command.
133 */
134 const uint32_t dw1 = GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH |
135 GEN6_PIPE_CONTROL_CS_STALL;
136
137 ILO_DEV_ASSERT(r->dev, 7, 7.5);
138
139 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
140 gen7_pipe_control(r, dw1);
141 }
142
143 static void
144 gen7_wa_pre_depth(struct ilo_render *r)
145 {
146 /*
147 * From the Ivy Bridge PRM, volume 2 part 1, page 315:
148 *
149 * "Driver must send a least one PIPE_CONTROL command with CS Stall and
150 * a post sync operation prior to the group of depth
151 * commands(3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
152 * 3DSTATE_STENCIL_BUFFER, and 3DSTATE_HIER_DEPTH_BUFFER)."
153 */
154 const uint32_t dw1 = GEN6_PIPE_CONTROL_CS_STALL |
155 GEN6_PIPE_CONTROL_WRITE_IMM;
156
157 ILO_DEV_ASSERT(r->dev, 7, 7.5);
158
159 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
160 gen7_pipe_control(r, dw1);
161
162 /*
163 * From the Ivy Bridge PRM, volume 2 part 1, page 315:
164 *
165 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e.,
166 * any combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
167 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
168 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
169 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
170 * Depth Flush Bit set, followed by another pipelined depth stall
171 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
172 * guarantee that the pipeline from WM onwards is already flushed
173 * (e.g., via a preceding MI_FLUSH)."
174 */
175 gen7_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL);
176 gen7_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH);
177 gen7_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL);
178 }
179
180 static void
181 gen7_wa_pre_3dstate_ps_max_threads(struct ilo_render *r)
182 {
183 /*
184 * From the Ivy Bridge PRM, volume 2 part 1, page 286:
185 *
186 * "If this field (Maximum Number of Threads in 3DSTATE_PS) is changed
187 * between 3DPRIMITIVE commands, a PIPE_CONTROL command with Stall at
188 * Pixel Scoreboard set is required to be issued."
189 */
190 const uint32_t dw1 = GEN6_PIPE_CONTROL_PIXEL_SCOREBOARD_STALL;
191
192 ILO_DEV_ASSERT(r->dev, 7, 7.5);
193
194 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
195 gen7_pipe_control(r, dw1);
196 }
197
198 static void
199 gen7_wa_post_ps_and_later(struct ilo_render *r)
200 {
201 /*
202 * From the Ivy Bridge PRM, volume 2 part 1, page 276:
203 *
204 * "The driver must make sure a PIPE_CONTROL with the Depth Stall
205 * Enable bit set after all the following states are programmed:
206 *
207 * - 3DSTATE_PS
208 * - 3DSTATE_VIEWPORT_STATE_POINTERS_CC
209 * - 3DSTATE_CONSTANT_PS
210 * - 3DSTATE_BINDING_TABLE_POINTERS_PS
211 * - 3DSTATE_SAMPLER_STATE_POINTERS_PS
212 * - 3DSTATE_CC_STATE_POINTERS
213 * - 3DSTATE_BLEND_STATE_POINTERS
214 * - 3DSTATE_DEPTH_STENCIL_STATE_POINTERS"
215 */
216 const uint32_t dw1 = GEN6_PIPE_CONTROL_DEPTH_STALL;
217
218 ILO_DEV_ASSERT(r->dev, 7, 7.5);
219
220 r->state.deferred_pipe_control_dw1 |= dw1;
221 }
222
223 #define DIRTY(state) (session->pipe_dirty & ILO_DIRTY_ ## state)
224
225 void
226 gen7_draw_common_urb(struct ilo_render *r,
227 const struct ilo_state_vector *vec,
228 struct ilo_render_draw_session *session)
229 {
230 /* 3DSTATE_URB_{VS,GS,HS,DS} */
231 if (DIRTY(VE) || DIRTY(VS)) {
232 /* the first 16KB are reserved for VS and PS PCBs */
233 const int offset =
234 (ilo_dev_gen(r->dev) >= ILO_GEN(8)) ||
235 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ?
236 32768 : 16384;
237 int vs_entry_size, vs_total_size;
238
239 vs_entry_size = (vec->vs) ?
240 ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_OUTPUT_COUNT) : 0;
241
242 /*
243 * From the Ivy Bridge PRM, volume 2 part 1, page 35:
244 *
245 * "Programming Restriction: As the VS URB entry serves as both the
246 * per-vertex input and output of the VS shader, the VS URB
247 * Allocation Size must be sized to the maximum of the vertex input
248 * and output structures."
249 */
250 if (vs_entry_size < vec->ve->count + vec->ve->prepend_nosrc_cso)
251 vs_entry_size = vec->ve->count + vec->ve->prepend_nosrc_cso;
252
253 vs_entry_size *= sizeof(float) * 4;
254 vs_total_size = r->dev->urb_size - offset;
255
256 if (ilo_dev_gen(r->dev) < ILO_GEN(8))
257 gen7_wa_pre_vs(r);
258
259 gen7_3DSTATE_URB_VS(r->builder,
260 offset, vs_total_size, vs_entry_size);
261
262 gen7_3DSTATE_URB_GS(r->builder, offset, 0, 0);
263 gen7_3DSTATE_URB_HS(r->builder, offset, 0, 0);
264 gen7_3DSTATE_URB_DS(r->builder, offset, 0, 0);
265 }
266 }
267
268 void
269 gen7_draw_common_pcb_alloc(struct ilo_render *r,
270 const struct ilo_state_vector *vec,
271 struct ilo_render_draw_session *session)
272 {
273 /* 3DSTATE_PUSH_CONSTANT_ALLOC_{VS,PS} */
274 if (r->hw_ctx_changed) {
275 /*
276 * Push constant buffers are only allowed to take up at most the first
277 * 16KB of the URB. Split the space evenly for VS and FS.
278 */
279 const int max_size =
280 (ilo_dev_gen(r->dev) >= ILO_GEN(8)) ||
281 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ?
282 32768 : 16384;
283 const int size = max_size / 2;
284 int offset = 0;
285
286 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_VS(r->builder, offset, size);
287 offset += size;
288
289 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_PS(r->builder, offset, size);
290
291 if (ilo_dev_gen(r->dev) == ILO_GEN(7))
292 gen7_wa_post_3dstate_push_constant_alloc_ps(r);
293 }
294 }
295
296 void
297 gen7_draw_common_pointers_1(struct ilo_render *r,
298 const struct ilo_state_vector *vec,
299 struct ilo_render_draw_session *session)
300 {
301 /* 3DSTATE_VIEWPORT_STATE_POINTERS_{CC,SF_CLIP} */
302 if (session->viewport_changed) {
303 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(r->builder,
304 r->state.CC_VIEWPORT);
305
306 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(r->builder,
307 r->state.SF_CLIP_VIEWPORT);
308 }
309 }
310
311 void
312 gen7_draw_common_pointers_2(struct ilo_render *r,
313 const struct ilo_state_vector *vec,
314 struct ilo_render_draw_session *session)
315 {
316 /* 3DSTATE_BLEND_STATE_POINTERS */
317 if (session->blend_changed) {
318 gen7_3DSTATE_BLEND_STATE_POINTERS(r->builder,
319 r->state.BLEND_STATE);
320 }
321
322 /* 3DSTATE_CC_STATE_POINTERS */
323 if (session->cc_changed) {
324 gen7_3DSTATE_CC_STATE_POINTERS(r->builder,
325 r->state.COLOR_CALC_STATE);
326 }
327
328 /* 3DSTATE_DEPTH_STENCIL_STATE_POINTERS */
329 if (ilo_dev_gen(r->dev) < ILO_GEN(8) && session->dsa_changed) {
330 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(r->builder,
331 r->state.DEPTH_STENCIL_STATE);
332 }
333 }
334
335 void
336 gen7_draw_vs(struct ilo_render *r,
337 const struct ilo_state_vector *vec,
338 struct ilo_render_draw_session *session)
339 {
340 const bool emit_3dstate_binding_table = session->binding_table_vs_changed;
341 const bool emit_3dstate_sampler_state = session->sampler_vs_changed;
342 /* see gen6_draw_vs() */
343 const bool emit_3dstate_constant_vs = session->pcb_vs_changed;
344 const bool emit_3dstate_vs = (DIRTY(VS) || r->instruction_bo_changed);
345
346 /* emit depth stall before any of the VS commands */
347 if (ilo_dev_gen(r->dev) < ILO_GEN(8)) {
348 if (emit_3dstate_binding_table || emit_3dstate_sampler_state ||
349 emit_3dstate_constant_vs || emit_3dstate_vs)
350 gen7_wa_pre_vs(r);
351 }
352
353 /* 3DSTATE_BINDING_TABLE_POINTERS_VS */
354 if (emit_3dstate_binding_table) {
355 gen7_3DSTATE_BINDING_TABLE_POINTERS_VS(r->builder,
356 r->state.vs.BINDING_TABLE_STATE);
357 }
358
359 /* 3DSTATE_SAMPLER_STATE_POINTERS_VS */
360 if (emit_3dstate_sampler_state) {
361 gen7_3DSTATE_SAMPLER_STATE_POINTERS_VS(r->builder,
362 r->state.vs.SAMPLER_STATE);
363 }
364
365 /* 3DSTATE_CONSTANT_VS */
366 if (emit_3dstate_constant_vs) {
367 gen7_3DSTATE_CONSTANT_VS(r->builder,
368 &r->state.vs.PUSH_CONSTANT_BUFFER,
369 &r->state.vs.PUSH_CONSTANT_BUFFER_size,
370 1);
371 }
372
373 /* 3DSTATE_VS */
374 if (ilo_dev_gen(r->dev) >= ILO_GEN(8)) {
375 if (emit_3dstate_vs || DIRTY(RASTERIZER)) {
376 gen8_3DSTATE_VS(r->builder, vec->vs,
377 vec->rasterizer->state.clip_plane_enable);
378 }
379 } else {
380 if (emit_3dstate_vs)
381 gen6_3DSTATE_VS(r->builder, vec->vs);
382 }
383 }
384
385 void
386 gen7_draw_hs(struct ilo_render *r,
387 const struct ilo_state_vector *vec,
388 struct ilo_render_draw_session *session)
389 {
390 /* 3DSTATE_CONSTANT_HS and 3DSTATE_HS */
391 if (r->hw_ctx_changed) {
392 gen7_3DSTATE_CONSTANT_HS(r->builder, 0, 0, 0);
393 gen7_disable_3DSTATE_HS(r->builder);
394 }
395
396 /* 3DSTATE_BINDING_TABLE_POINTERS_HS */
397 if (r->hw_ctx_changed)
398 gen7_3DSTATE_BINDING_TABLE_POINTERS_HS(r->builder, 0);
399 }
400
401 void
402 gen7_draw_te(struct ilo_render *r,
403 const struct ilo_state_vector *vec,
404 struct ilo_render_draw_session *session)
405 {
406 /* 3DSTATE_TE */
407 if (r->hw_ctx_changed)
408 gen7_3DSTATE_TE(r->builder);
409 }
410
411 void
412 gen7_draw_ds(struct ilo_render *r,
413 const struct ilo_state_vector *vec,
414 struct ilo_render_draw_session *session)
415 {
416 /* 3DSTATE_CONSTANT_DS and 3DSTATE_DS */
417 if (r->hw_ctx_changed) {
418 gen7_3DSTATE_CONSTANT_DS(r->builder, 0, 0, 0);
419 gen7_disable_3DSTATE_DS(r->builder);
420 }
421
422 /* 3DSTATE_BINDING_TABLE_POINTERS_DS */
423 if (r->hw_ctx_changed)
424 gen7_3DSTATE_BINDING_TABLE_POINTERS_DS(r->builder, 0);
425
426 }
427
428 void
429 gen7_draw_gs(struct ilo_render *r,
430 const struct ilo_state_vector *vec,
431 struct ilo_render_draw_session *session)
432 {
433 /* 3DSTATE_CONSTANT_GS and 3DSTATE_GS */
434 if (r->hw_ctx_changed) {
435 gen7_3DSTATE_CONSTANT_GS(r->builder, 0, 0, 0);
436 gen7_disable_3DSTATE_GS(r->builder);
437 }
438
439 /* 3DSTATE_BINDING_TABLE_POINTERS_GS */
440 if (session->binding_table_gs_changed) {
441 gen7_3DSTATE_BINDING_TABLE_POINTERS_GS(r->builder,
442 r->state.gs.BINDING_TABLE_STATE);
443 }
444 }
445
446 void
447 gen7_draw_sol(struct ilo_render *r,
448 const struct ilo_state_vector *vec,
449 struct ilo_render_draw_session *session)
450 {
451 const struct pipe_stream_output_info *so_info;
452 const struct ilo_shader_state *shader;
453 bool dirty_sh = false;
454
455 if (vec->gs) {
456 shader = vec->gs;
457 dirty_sh = DIRTY(GS);
458 }
459 else {
460 shader = vec->vs;
461 dirty_sh = DIRTY(VS);
462 }
463
464 so_info = ilo_shader_get_kernel_so_info(shader);
465
466 /* 3DSTATE_SO_BUFFER */
467 if ((DIRTY(SO) || dirty_sh || r->batch_bo_changed) &&
468 vec->so.enabled) {
469 int i;
470
471 for (i = 0; i < vec->so.count; i++) {
472 const int stride = so_info->stride[i] * 4; /* in bytes */
473
474 gen7_3DSTATE_SO_BUFFER(r->builder, i, stride, vec->so.states[i]);
475 }
476
477 for (; i < 4; i++)
478 gen7_disable_3DSTATE_SO_BUFFER(r->builder, i);
479 }
480
481 /* 3DSTATE_SO_DECL_LIST */
482 if (dirty_sh && vec->so.enabled)
483 gen7_3DSTATE_SO_DECL_LIST(r->builder, so_info);
484
485 /* 3DSTATE_STREAMOUT */
486 if (DIRTY(SO) || DIRTY(RASTERIZER) || dirty_sh) {
487 const int output_count = ilo_shader_get_kernel_param(shader,
488 ILO_KERNEL_OUTPUT_COUNT);
489 int buf_strides[4] = { 0, 0, 0, 0 };
490 int i;
491
492 for (i = 0; i < vec->so.count; i++)
493 buf_strides[i] = so_info->stride[i] * 4;
494
495 gen7_3DSTATE_STREAMOUT(r->builder, 0,
496 vec->rasterizer->state.rasterizer_discard,
497 output_count, buf_strides);
498 }
499 }
500
501 static void
502 gen7_draw_sf(struct ilo_render *r,
503 const struct ilo_state_vector *vec,
504 struct ilo_render_draw_session *session)
505 {
506 /* 3DSTATE_SBE */
507 if (DIRTY(RASTERIZER) || DIRTY(FS)) {
508 gen7_3DSTATE_SBE(r->builder, vec->fs, (vec->rasterizer) ?
509 vec->rasterizer->state.sprite_coord_mode : 0);
510 }
511
512 /* 3DSTATE_SF */
513 if (DIRTY(RASTERIZER) || DIRTY(FB)) {
514 struct pipe_surface *zs = vec->fb.state.zsbuf;
515
516 gen7_wa_pre_3dstate_sf_depth_bias(r);
517 gen7_3DSTATE_SF(r->builder,
518 (vec->rasterizer) ? &vec->rasterizer->sf : NULL,
519 (zs) ? zs->format : PIPE_FORMAT_NONE,
520 vec->fb.num_samples);
521 }
522 }
523
524 static void
525 gen7_draw_wm(struct ilo_render *r,
526 const struct ilo_state_vector *vec,
527 struct ilo_render_draw_session *session)
528 {
529 /* 3DSTATE_WM */
530 if (DIRTY(FS) || DIRTY(BLEND) || DIRTY(DSA) || DIRTY(RASTERIZER)) {
531 const bool cc_may_kill = (vec->dsa->dw_blend_alpha ||
532 vec->blend->alpha_to_coverage);
533
534 gen7_3DSTATE_WM(r->builder, vec->fs, vec->rasterizer, cc_may_kill);
535 }
536
537 /* 3DSTATE_BINDING_TABLE_POINTERS_PS */
538 if (session->binding_table_fs_changed) {
539 gen7_3DSTATE_BINDING_TABLE_POINTERS_PS(r->builder,
540 r->state.wm.BINDING_TABLE_STATE);
541 }
542
543 /* 3DSTATE_SAMPLER_STATE_POINTERS_PS */
544 if (session->sampler_fs_changed) {
545 gen7_3DSTATE_SAMPLER_STATE_POINTERS_PS(r->builder,
546 r->state.wm.SAMPLER_STATE);
547 }
548
549 /* 3DSTATE_CONSTANT_PS */
550 if (session->pcb_fs_changed) {
551 gen7_3DSTATE_CONSTANT_PS(r->builder,
552 &r->state.wm.PUSH_CONSTANT_BUFFER,
553 &r->state.wm.PUSH_CONSTANT_BUFFER_size,
554 1);
555 }
556
557 /* 3DSTATE_PS */
558 if (DIRTY(FS) || DIRTY(BLEND) || r->instruction_bo_changed) {
559 const bool dual_blend = vec->blend->dual_blend;
560
561 if ((ilo_dev_gen(r->dev) == ILO_GEN(7) ||
562 ilo_dev_gen(r->dev) == ILO_GEN(7.5)) &&
563 r->hw_ctx_changed)
564 gen7_wa_pre_3dstate_ps_max_threads(r);
565
566 gen7_3DSTATE_PS(r->builder, vec->fs, dual_blend);
567 }
568
569 /* 3DSTATE_SCISSOR_STATE_POINTERS */
570 if (session->scissor_changed) {
571 gen6_3DSTATE_SCISSOR_STATE_POINTERS(r->builder,
572 r->state.SCISSOR_RECT);
573 }
574
575 /* XXX what is the best way to know if this workaround is needed? */
576 {
577 const bool emit_3dstate_ps = (DIRTY(FS) || DIRTY(BLEND));
578 const bool emit_3dstate_depth_buffer =
579 (DIRTY(FB) || DIRTY(DSA) || r->state_bo_changed);
580
581 if (emit_3dstate_ps ||
582 session->pcb_fs_changed ||
583 session->viewport_changed ||
584 session->binding_table_fs_changed ||
585 session->sampler_fs_changed ||
586 session->cc_changed ||
587 session->blend_changed ||
588 session->dsa_changed)
589 gen7_wa_post_ps_and_later(r);
590
591 if (emit_3dstate_depth_buffer)
592 gen7_wa_pre_depth(r);
593 }
594
595 /* 3DSTATE_DEPTH_BUFFER and 3DSTATE_CLEAR_PARAMS */
596 if (DIRTY(FB) || r->batch_bo_changed) {
597 const struct ilo_zs_surface *zs;
598 uint32_t clear_params;
599
600 if (vec->fb.state.zsbuf) {
601 const struct ilo_surface_cso *surface =
602 (const struct ilo_surface_cso *) vec->fb.state.zsbuf;
603 const struct ilo_texture_slice *slice =
604 ilo_texture_get_slice(ilo_texture(surface->base.texture),
605 surface->base.u.tex.level, surface->base.u.tex.first_layer);
606
607 assert(!surface->is_rt);
608 zs = &surface->u.zs;
609 clear_params = slice->clear_value;
610 }
611 else {
612 zs = &vec->fb.null_zs;
613 clear_params = 0;
614 }
615
616 gen6_3DSTATE_DEPTH_BUFFER(r->builder, zs, false);
617 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder, zs);
618 gen6_3DSTATE_STENCIL_BUFFER(r->builder, zs);
619 gen7_3DSTATE_CLEAR_PARAMS(r->builder, clear_params);
620 }
621 }
622
623 static void
624 gen7_draw_wm_multisample(struct ilo_render *r,
625 const struct ilo_state_vector *vec,
626 struct ilo_render_draw_session *session)
627 {
628 /* 3DSTATE_MULTISAMPLE and 3DSTATE_SAMPLE_MASK */
629 if (DIRTY(SAMPLE_MASK) || DIRTY(FB)) {
630 const uint32_t *pattern;
631
632 gen7_wa_pre_3dstate_multisample(r);
633
634 pattern = (vec->fb.num_samples > 4) ? r->sample_pattern_8x :
635 (vec->fb.num_samples > 1) ? &r->sample_pattern_4x :
636 &r->sample_pattern_1x;
637
638 gen6_3DSTATE_MULTISAMPLE(r->builder,
639 vec->fb.num_samples, pattern,
640 vec->rasterizer->state.half_pixel_center);
641
642 gen7_3DSTATE_SAMPLE_MASK(r->builder,
643 (vec->fb.num_samples > 1) ? vec->sample_mask : 0x1,
644 vec->fb.num_samples);
645 }
646 }
647
648 void
649 gen7_draw_vf_draw(struct ilo_render *r,
650 const struct ilo_state_vector *vec,
651 struct ilo_render_draw_session *session)
652 {
653 if (r->state.deferred_pipe_control_dw1)
654 gen7_pipe_control(r, r->state.deferred_pipe_control_dw1);
655
656 /* 3DPRIMITIVE */
657 gen7_3DPRIMITIVE(r->builder, vec->draw, &vec->ib);
658
659 r->state.current_pipe_control_dw1 = 0;
660 r->state.deferred_pipe_control_dw1 = 0;
661 }
662
663 void
664 ilo_render_emit_draw_commands_gen7(struct ilo_render *render,
665 const struct ilo_state_vector *vec,
666 struct ilo_render_draw_session *session)
667 {
668 ILO_DEV_ASSERT(render->dev, 7, 7.5);
669
670 /*
671 * We try to keep the order of the commands match, as closely as possible,
672 * that of the classic i965 driver. It allows us to compare the command
673 * streams easily.
674 */
675 gen6_draw_common_select(render, vec, session);
676 gen6_draw_common_sip(render, vec, session);
677 gen6_draw_vf_statistics(render, vec, session);
678 gen7_draw_common_pcb_alloc(render, vec, session);
679 gen6_draw_common_base_address(render, vec, session);
680 gen7_draw_common_pointers_1(render, vec, session);
681 gen7_draw_common_urb(render, vec, session);
682 gen7_draw_common_pointers_2(render, vec, session);
683 gen7_draw_wm_multisample(render, vec, session);
684 gen7_draw_gs(render, vec, session);
685 gen7_draw_hs(render, vec, session);
686 gen7_draw_te(render, vec, session);
687 gen7_draw_ds(render, vec, session);
688 gen7_draw_vs(render, vec, session);
689 gen7_draw_sol(render, vec, session);
690 gen6_draw_clip(render, vec, session);
691 gen7_draw_sf(render, vec, session);
692 gen7_draw_wm(render, vec, session);
693 gen6_draw_wm_raster(render, vec, session);
694 gen6_draw_sf_rect(render, vec, session);
695 gen6_draw_vf(render, vec, session);
696 gen7_draw_vf_draw(render, vec, session);
697 }
698
699 static void
700 gen7_rectlist_pcb_alloc(struct ilo_render *r,
701 const struct ilo_blitter *blitter)
702 {
703 /*
704 * Push constant buffers are only allowed to take up at most the first
705 * 16KB of the URB. Split the space evenly for VS and FS.
706 */
707 const int max_size =
708 (ilo_dev_gen(r->dev) >= ILO_GEN(8)) ||
709 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ?
710 32768 : 16384;
711 const int size = max_size / 2;
712 int offset = 0;
713
714 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_VS(r->builder, offset, size);
715 offset += size;
716
717 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_PS(r->builder, offset, size);
718
719 gen7_wa_post_3dstate_push_constant_alloc_ps(r);
720 }
721
722 static void
723 gen7_rectlist_urb(struct ilo_render *r,
724 const struct ilo_blitter *blitter)
725 {
726 /* the first 16KB are reserved for VS and PS PCBs */
727 const int offset =
728 (ilo_dev_gen(r->dev) >= ILO_GEN(8)) ||
729 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ?
730 32768 : 16384;
731
732 gen7_3DSTATE_URB_VS(r->builder, offset, r->dev->urb_size - offset,
733 (blitter->ve.count + blitter->ve.prepend_nosrc_cso) *
734 4 * sizeof(float));
735
736 gen7_3DSTATE_URB_GS(r->builder, offset, 0, 0);
737 gen7_3DSTATE_URB_HS(r->builder, offset, 0, 0);
738 gen7_3DSTATE_URB_DS(r->builder, offset, 0, 0);
739 }
740
741 static void
742 gen7_rectlist_vs_to_sf(struct ilo_render *r,
743 const struct ilo_blitter *blitter)
744 {
745 gen7_3DSTATE_CONSTANT_VS(r->builder, NULL, NULL, 0);
746 gen6_disable_3DSTATE_VS(r->builder);
747
748 gen7_3DSTATE_CONSTANT_HS(r->builder, NULL, NULL, 0);
749 gen7_disable_3DSTATE_HS(r->builder);
750
751 gen7_3DSTATE_TE(r->builder);
752
753 gen7_3DSTATE_CONSTANT_DS(r->builder, NULL, NULL, 0);
754 gen7_disable_3DSTATE_DS(r->builder);
755
756 gen7_3DSTATE_CONSTANT_GS(r->builder, NULL, NULL, 0);
757 gen7_disable_3DSTATE_GS(r->builder);
758
759 gen7_3DSTATE_STREAMOUT(r->builder, 0, false, 0x0, 0);
760
761 gen6_disable_3DSTATE_CLIP(r->builder);
762
763 gen7_wa_pre_3dstate_sf_depth_bias(r);
764
765 gen7_3DSTATE_SF(r->builder, NULL, blitter->fb.dst.base.format,
766 blitter->fb.num_samples);
767 gen7_3DSTATE_SBE(r->builder, NULL, 0);
768 }
769
770 static void
771 gen7_rectlist_wm(struct ilo_render *r,
772 const struct ilo_blitter *blitter)
773 {
774 uint32_t hiz_op;
775
776 switch (blitter->op) {
777 case ILO_BLITTER_RECTLIST_CLEAR_ZS:
778 hiz_op = GEN7_WM_DW1_DEPTH_CLEAR;
779 break;
780 case ILO_BLITTER_RECTLIST_RESOLVE_Z:
781 hiz_op = GEN7_WM_DW1_DEPTH_RESOLVE;
782 break;
783 case ILO_BLITTER_RECTLIST_RESOLVE_HIZ:
784 hiz_op = GEN7_WM_DW1_HIZ_RESOLVE;
785 break;
786 default:
787 hiz_op = 0;
788 break;
789 }
790
791 gen7_hiz_3DSTATE_WM(r->builder, hiz_op);
792
793 gen7_3DSTATE_CONSTANT_PS(r->builder, NULL, NULL, 0);
794
795 gen7_wa_pre_3dstate_ps_max_threads(r);
796 gen7_disable_3DSTATE_PS(r->builder);
797 }
798
799 static void
800 gen7_rectlist_wm_depth(struct ilo_render *r,
801 const struct ilo_blitter *blitter)
802 {
803 gen7_wa_pre_depth(r);
804
805 if (blitter->uses & (ILO_BLITTER_USE_FB_DEPTH |
806 ILO_BLITTER_USE_FB_STENCIL)) {
807 gen6_3DSTATE_DEPTH_BUFFER(r->builder,
808 &blitter->fb.dst.u.zs, true);
809 }
810
811 if (blitter->uses & ILO_BLITTER_USE_FB_DEPTH) {
812 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder,
813 &blitter->fb.dst.u.zs);
814 }
815
816 if (blitter->uses & ILO_BLITTER_USE_FB_STENCIL) {
817 gen6_3DSTATE_STENCIL_BUFFER(r->builder,
818 &blitter->fb.dst.u.zs);
819 }
820
821 gen7_3DSTATE_CLEAR_PARAMS(r->builder,
822 blitter->depth_clear_value);
823 }
824
825 static void
826 gen7_rectlist_wm_multisample(struct ilo_render *r,
827 const struct ilo_blitter *blitter)
828 {
829 const uint32_t *pattern =
830 (blitter->fb.num_samples > 4) ? r->sample_pattern_8x :
831 (blitter->fb.num_samples > 1) ? &r->sample_pattern_4x :
832 &r->sample_pattern_1x;
833
834 gen7_wa_pre_3dstate_multisample(r);
835
836 gen6_3DSTATE_MULTISAMPLE(r->builder, blitter->fb.num_samples,
837 pattern, true);
838
839 gen7_3DSTATE_SAMPLE_MASK(r->builder,
840 (1 << blitter->fb.num_samples) - 1, blitter->fb.num_samples);
841 }
842
843 void
844 ilo_render_emit_rectlist_commands_gen7(struct ilo_render *r,
845 const struct ilo_blitter *blitter,
846 const struct ilo_render_rectlist_session *session)
847 {
848 ILO_DEV_ASSERT(r->dev, 7, 7.5);
849
850 gen7_rectlist_wm_multisample(r, blitter);
851
852 gen6_state_base_address(r->builder, true);
853
854 gen6_user_3DSTATE_VERTEX_BUFFERS(r->builder,
855 session->vb_start, session->vb_end,
856 sizeof(blitter->vertices[0]));
857
858 gen6_3DSTATE_VERTEX_ELEMENTS(r->builder, &blitter->ve);
859
860 gen7_rectlist_pcb_alloc(r, blitter);
861
862 /* needed for any VS-related commands */
863 gen7_wa_pre_vs(r);
864
865 gen7_rectlist_urb(r, blitter);
866
867 if (blitter->uses & ILO_BLITTER_USE_DSA) {
868 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(r->builder,
869 r->state.DEPTH_STENCIL_STATE);
870 }
871
872 if (blitter->uses & ILO_BLITTER_USE_CC) {
873 gen7_3DSTATE_CC_STATE_POINTERS(r->builder,
874 r->state.COLOR_CALC_STATE);
875 }
876
877 gen7_rectlist_vs_to_sf(r, blitter);
878 gen7_rectlist_wm(r, blitter);
879
880 if (blitter->uses & ILO_BLITTER_USE_VIEWPORT) {
881 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(r->builder,
882 r->state.CC_VIEWPORT);
883 }
884
885 gen7_rectlist_wm_depth(r, blitter);
886
887 gen6_3DSTATE_DRAWING_RECTANGLE(r->builder, 0, 0,
888 blitter->fb.width, blitter->fb.height);
889
890 gen7_3DPRIMITIVE(r->builder, &blitter->draw, NULL);
891 }
892
893 int
894 ilo_render_get_draw_commands_len_gen7(const struct ilo_render *render,
895 const struct ilo_state_vector *vec)
896 {
897 static int len;
898
899 ILO_DEV_ASSERT(render->dev, 7, 7.5);
900
901 if (!len) {
902 len += GEN7_3DSTATE_URB_ANY__SIZE * 4;
903 len += GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_ANY__SIZE * 5;
904 len += GEN6_3DSTATE_CONSTANT_ANY__SIZE * 5;
905 len += GEN7_3DSTATE_POINTERS_ANY__SIZE * (5 + 5 + 4);
906 len += GEN7_3DSTATE_SO_BUFFER__SIZE * 4;
907 len += GEN6_PIPE_CONTROL__SIZE * 5;
908
909 len +=
910 GEN6_STATE_BASE_ADDRESS__SIZE +
911 GEN6_STATE_SIP__SIZE +
912 GEN6_3DSTATE_VF_STATISTICS__SIZE +
913 GEN6_PIPELINE_SELECT__SIZE +
914 GEN6_3DSTATE_CLEAR_PARAMS__SIZE +
915 GEN6_3DSTATE_DEPTH_BUFFER__SIZE +
916 GEN6_3DSTATE_STENCIL_BUFFER__SIZE +
917 GEN6_3DSTATE_HIER_DEPTH_BUFFER__SIZE +
918 GEN6_3DSTATE_VERTEX_BUFFERS__SIZE +
919 GEN6_3DSTATE_VERTEX_ELEMENTS__SIZE +
920 GEN6_3DSTATE_INDEX_BUFFER__SIZE +
921 GEN75_3DSTATE_VF__SIZE +
922 GEN6_3DSTATE_VS__SIZE +
923 GEN6_3DSTATE_GS__SIZE +
924 GEN6_3DSTATE_CLIP__SIZE +
925 GEN6_3DSTATE_SF__SIZE +
926 GEN6_3DSTATE_WM__SIZE +
927 GEN6_3DSTATE_SAMPLE_MASK__SIZE +
928 GEN7_3DSTATE_HS__SIZE +
929 GEN7_3DSTATE_TE__SIZE +
930 GEN7_3DSTATE_DS__SIZE +
931 GEN7_3DSTATE_STREAMOUT__SIZE +
932 GEN7_3DSTATE_SBE__SIZE +
933 GEN7_3DSTATE_PS__SIZE +
934 GEN6_3DSTATE_DRAWING_RECTANGLE__SIZE +
935 GEN6_3DSTATE_POLY_STIPPLE_OFFSET__SIZE +
936 GEN6_3DSTATE_POLY_STIPPLE_PATTERN__SIZE +
937 GEN6_3DSTATE_LINE_STIPPLE__SIZE +
938 GEN6_3DSTATE_AA_LINE_PARAMETERS__SIZE +
939 GEN6_3DSTATE_MULTISAMPLE__SIZE +
940 GEN7_3DSTATE_SO_DECL_LIST__SIZE +
941 GEN6_3DPRIMITIVE__SIZE;
942 }
943
944 return len;
945 }