ilo: add variants of 3DSTATE_VS
[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 inline 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, false);
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 static 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 = (ilo_dev_gen(r->dev) == ILO_GEN(7.5) &&
234 r->dev->gt == 3) ? 32768 : 16384;
235 int vs_entry_size, vs_total_size;
236
237 vs_entry_size = (vec->vs) ?
238 ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_OUTPUT_COUNT) : 0;
239
240 /*
241 * From the Ivy Bridge PRM, volume 2 part 1, page 35:
242 *
243 * "Programming Restriction: As the VS URB entry serves as both the
244 * per-vertex input and output of the VS shader, the VS URB
245 * Allocation Size must be sized to the maximum of the vertex input
246 * and output structures."
247 */
248 if (vs_entry_size < vec->ve->count + vec->ve->prepend_nosrc_cso)
249 vs_entry_size = vec->ve->count + vec->ve->prepend_nosrc_cso;
250
251 vs_entry_size *= sizeof(float) * 4;
252 vs_total_size = r->dev->urb_size - offset;
253
254 gen7_wa_pre_vs(r);
255
256 gen7_3DSTATE_URB_VS(r->builder,
257 offset, vs_total_size, vs_entry_size);
258
259 gen7_3DSTATE_URB_GS(r->builder, offset, 0, 0);
260 gen7_3DSTATE_URB_HS(r->builder, offset, 0, 0);
261 gen7_3DSTATE_URB_DS(r->builder, offset, 0, 0);
262 }
263 }
264
265 static void
266 gen7_draw_common_pcb_alloc(struct ilo_render *r,
267 const struct ilo_state_vector *vec,
268 struct ilo_render_draw_session *session)
269 {
270 /* 3DSTATE_PUSH_CONSTANT_ALLOC_{VS,PS} */
271 if (r->hw_ctx_changed) {
272 /*
273 * Push constant buffers are only allowed to take up at most the first
274 * 16KB of the URB. Split the space evenly for VS and FS.
275 */
276 const int max_size = (ilo_dev_gen(r->dev) == ILO_GEN(7.5) &&
277 r->dev->gt == 3) ? 32768 : 16384;
278 const int size = max_size / 2;
279 int offset = 0;
280
281 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_VS(r->builder, offset, size);
282 offset += size;
283
284 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_PS(r->builder, offset, size);
285
286 if (ilo_dev_gen(r->dev) == ILO_GEN(7))
287 gen7_wa_post_3dstate_push_constant_alloc_ps(r);
288 }
289 }
290
291 static void
292 gen7_draw_common_pointers_1(struct ilo_render *r,
293 const struct ilo_state_vector *vec,
294 struct ilo_render_draw_session *session)
295 {
296 /* 3DSTATE_VIEWPORT_STATE_POINTERS_{CC,SF_CLIP} */
297 if (session->viewport_changed) {
298 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(r->builder,
299 r->state.CC_VIEWPORT);
300
301 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_SF_CLIP(r->builder,
302 r->state.SF_CLIP_VIEWPORT);
303 }
304 }
305
306 static void
307 gen7_draw_common_pointers_2(struct ilo_render *r,
308 const struct ilo_state_vector *vec,
309 struct ilo_render_draw_session *session)
310 {
311 /* 3DSTATE_BLEND_STATE_POINTERS */
312 if (session->blend_changed) {
313 gen7_3DSTATE_BLEND_STATE_POINTERS(r->builder,
314 r->state.BLEND_STATE);
315 }
316
317 /* 3DSTATE_CC_STATE_POINTERS */
318 if (session->cc_changed) {
319 gen7_3DSTATE_CC_STATE_POINTERS(r->builder,
320 r->state.COLOR_CALC_STATE);
321 }
322
323 /* 3DSTATE_DEPTH_STENCIL_STATE_POINTERS */
324 if (session->dsa_changed) {
325 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(r->builder,
326 r->state.DEPTH_STENCIL_STATE);
327 }
328 }
329
330 static void
331 gen7_draw_vs(struct ilo_render *r,
332 const struct ilo_state_vector *vec,
333 struct ilo_render_draw_session *session)
334 {
335 const bool emit_3dstate_binding_table =
336 session->binding_table_vs_changed;
337 const bool emit_3dstate_sampler_state =
338 session->sampler_vs_changed;
339 /* see gen6_draw_vs() */
340 const bool emit_3dstate_constant_vs = session->pcb_vs_changed;
341 const bool emit_3dstate_vs = (DIRTY(VS) || r->instruction_bo_changed);
342
343 /* emit depth stall before any of the VS commands */
344 if (emit_3dstate_binding_table || emit_3dstate_sampler_state ||
345 emit_3dstate_constant_vs || emit_3dstate_vs)
346 gen7_wa_pre_vs(r);
347
348 /* 3DSTATE_BINDING_TABLE_POINTERS_VS */
349 if (emit_3dstate_binding_table) {
350 gen7_3DSTATE_BINDING_TABLE_POINTERS_VS(r->builder,
351 r->state.vs.BINDING_TABLE_STATE);
352 }
353
354 /* 3DSTATE_SAMPLER_STATE_POINTERS_VS */
355 if (emit_3dstate_sampler_state) {
356 gen7_3DSTATE_SAMPLER_STATE_POINTERS_VS(r->builder,
357 r->state.vs.SAMPLER_STATE);
358 }
359
360 /* 3DSTATE_CONSTANT_VS */
361 if (emit_3dstate_constant_vs) {
362 gen7_3DSTATE_CONSTANT_VS(r->builder,
363 &r->state.vs.PUSH_CONSTANT_BUFFER,
364 &r->state.vs.PUSH_CONSTANT_BUFFER_size,
365 1);
366 }
367
368 /* 3DSTATE_VS */
369 if (emit_3dstate_vs)
370 gen6_3DSTATE_VS(r->builder, vec->vs);
371 }
372
373 static void
374 gen7_draw_hs(struct ilo_render *r,
375 const struct ilo_state_vector *vec,
376 struct ilo_render_draw_session *session)
377 {
378 /* 3DSTATE_CONSTANT_HS and 3DSTATE_HS */
379 if (r->hw_ctx_changed) {
380 gen7_3DSTATE_CONSTANT_HS(r->builder, 0, 0, 0);
381 gen7_3DSTATE_HS(r->builder, NULL);
382 }
383
384 /* 3DSTATE_BINDING_TABLE_POINTERS_HS */
385 if (r->hw_ctx_changed)
386 gen7_3DSTATE_BINDING_TABLE_POINTERS_HS(r->builder, 0);
387 }
388
389 static void
390 gen7_draw_te(struct ilo_render *r,
391 const struct ilo_state_vector *vec,
392 struct ilo_render_draw_session *session)
393 {
394 /* 3DSTATE_TE */
395 if (r->hw_ctx_changed)
396 gen7_3DSTATE_TE(r->builder);
397 }
398
399 static void
400 gen7_draw_ds(struct ilo_render *r,
401 const struct ilo_state_vector *vec,
402 struct ilo_render_draw_session *session)
403 {
404 /* 3DSTATE_CONSTANT_DS and 3DSTATE_DS */
405 if (r->hw_ctx_changed) {
406 gen7_3DSTATE_CONSTANT_DS(r->builder, 0, 0, 0);
407 gen7_3DSTATE_DS(r->builder, NULL);
408 }
409
410 /* 3DSTATE_BINDING_TABLE_POINTERS_DS */
411 if (r->hw_ctx_changed)
412 gen7_3DSTATE_BINDING_TABLE_POINTERS_DS(r->builder, 0);
413
414 }
415
416 static void
417 gen7_draw_gs(struct ilo_render *r,
418 const struct ilo_state_vector *vec,
419 struct ilo_render_draw_session *session)
420 {
421 /* 3DSTATE_CONSTANT_GS and 3DSTATE_GS */
422 if (r->hw_ctx_changed) {
423 gen7_3DSTATE_CONSTANT_GS(r->builder, 0, 0, 0);
424 gen7_3DSTATE_GS(r->builder, NULL);
425 }
426
427 /* 3DSTATE_BINDING_TABLE_POINTERS_GS */
428 if (session->binding_table_gs_changed) {
429 gen7_3DSTATE_BINDING_TABLE_POINTERS_GS(r->builder,
430 r->state.gs.BINDING_TABLE_STATE);
431 }
432 }
433
434 static void
435 gen7_draw_sol(struct ilo_render *r,
436 const struct ilo_state_vector *vec,
437 struct ilo_render_draw_session *session)
438 {
439 const struct pipe_stream_output_info *so_info;
440 const struct ilo_shader_state *shader;
441 bool dirty_sh = false;
442
443 if (vec->gs) {
444 shader = vec->gs;
445 dirty_sh = DIRTY(GS);
446 }
447 else {
448 shader = vec->vs;
449 dirty_sh = DIRTY(VS);
450 }
451
452 so_info = ilo_shader_get_kernel_so_info(shader);
453
454 /* 3DSTATE_SO_BUFFER */
455 if ((DIRTY(SO) || dirty_sh || r->batch_bo_changed) &&
456 vec->so.enabled) {
457 int i;
458
459 for (i = 0; i < vec->so.count; i++) {
460 const int stride = so_info->stride[i] * 4; /* in bytes */
461 int base = 0;
462
463 gen7_3DSTATE_SO_BUFFER(r->builder, i, base, stride,
464 vec->so.states[i]);
465 }
466
467 for (; i < 4; i++)
468 gen7_3DSTATE_SO_BUFFER(r->builder, i, 0, 0, NULL);
469 }
470
471 /* 3DSTATE_SO_DECL_LIST */
472 if (dirty_sh && vec->so.enabled)
473 gen7_3DSTATE_SO_DECL_LIST(r->builder, so_info);
474
475 /* 3DSTATE_STREAMOUT */
476 if (DIRTY(SO) || DIRTY(RASTERIZER) || dirty_sh) {
477 const unsigned buffer_mask = (1 << vec->so.count) - 1;
478 const int output_count = ilo_shader_get_kernel_param(shader,
479 ILO_KERNEL_OUTPUT_COUNT);
480
481 gen7_3DSTATE_STREAMOUT(r->builder, buffer_mask, output_count,
482 vec->rasterizer->state.rasterizer_discard);
483 }
484 }
485
486 static void
487 gen7_draw_sf(struct ilo_render *r,
488 const struct ilo_state_vector *vec,
489 struct ilo_render_draw_session *session)
490 {
491 /* 3DSTATE_SBE */
492 if (DIRTY(RASTERIZER) || DIRTY(FS))
493 gen7_3DSTATE_SBE(r->builder, vec->rasterizer, vec->fs);
494
495 /* 3DSTATE_SF */
496 if (DIRTY(RASTERIZER) || DIRTY(FB)) {
497 struct pipe_surface *zs = vec->fb.state.zsbuf;
498
499 gen7_wa_pre_3dstate_sf_depth_bias(r);
500 gen7_3DSTATE_SF(r->builder, vec->rasterizer,
501 (zs) ? zs->format : PIPE_FORMAT_NONE);
502 }
503 }
504
505 static void
506 gen7_draw_wm(struct ilo_render *r,
507 const struct ilo_state_vector *vec,
508 struct ilo_render_draw_session *session)
509 {
510 /* 3DSTATE_WM */
511 if (DIRTY(FS) || DIRTY(BLEND) || DIRTY(DSA) || DIRTY(RASTERIZER)) {
512 const bool cc_may_kill = (vec->dsa->dw_alpha ||
513 vec->blend->alpha_to_coverage);
514
515 gen7_3DSTATE_WM(r->builder, vec->fs, vec->rasterizer, cc_may_kill);
516 }
517
518 /* 3DSTATE_BINDING_TABLE_POINTERS_PS */
519 if (session->binding_table_fs_changed) {
520 gen7_3DSTATE_BINDING_TABLE_POINTERS_PS(r->builder,
521 r->state.wm.BINDING_TABLE_STATE);
522 }
523
524 /* 3DSTATE_SAMPLER_STATE_POINTERS_PS */
525 if (session->sampler_fs_changed) {
526 gen7_3DSTATE_SAMPLER_STATE_POINTERS_PS(r->builder,
527 r->state.wm.SAMPLER_STATE);
528 }
529
530 /* 3DSTATE_CONSTANT_PS */
531 if (session->pcb_fs_changed) {
532 gen7_3DSTATE_CONSTANT_PS(r->builder,
533 &r->state.wm.PUSH_CONSTANT_BUFFER,
534 &r->state.wm.PUSH_CONSTANT_BUFFER_size,
535 1);
536 }
537
538 /* 3DSTATE_PS */
539 if (DIRTY(FS) || DIRTY(BLEND) || r->instruction_bo_changed) {
540 const bool dual_blend = vec->blend->dual_blend;
541
542 if ((ilo_dev_gen(r->dev) == ILO_GEN(7) ||
543 ilo_dev_gen(r->dev) == ILO_GEN(7.5)) &&
544 r->hw_ctx_changed)
545 gen7_wa_pre_3dstate_ps_max_threads(r);
546
547 gen7_3DSTATE_PS(r->builder, vec->fs, dual_blend);
548 }
549
550 /* 3DSTATE_SCISSOR_STATE_POINTERS */
551 if (session->scissor_changed) {
552 gen6_3DSTATE_SCISSOR_STATE_POINTERS(r->builder,
553 r->state.SCISSOR_RECT);
554 }
555
556 /* XXX what is the best way to know if this workaround is needed? */
557 {
558 const bool emit_3dstate_ps = (DIRTY(FS) || DIRTY(BLEND));
559 const bool emit_3dstate_depth_buffer =
560 (DIRTY(FB) || DIRTY(DSA) || r->state_bo_changed);
561
562 if (emit_3dstate_ps ||
563 session->pcb_fs_changed ||
564 session->viewport_changed ||
565 session->binding_table_fs_changed ||
566 session->sampler_fs_changed ||
567 session->cc_changed ||
568 session->blend_changed ||
569 session->dsa_changed)
570 gen7_wa_post_ps_and_later(r);
571
572 if (emit_3dstate_depth_buffer)
573 gen7_wa_pre_depth(r);
574 }
575
576 /* 3DSTATE_DEPTH_BUFFER and 3DSTATE_CLEAR_PARAMS */
577 if (DIRTY(FB) || r->batch_bo_changed) {
578 const struct ilo_zs_surface *zs;
579 uint32_t clear_params;
580
581 if (vec->fb.state.zsbuf) {
582 const struct ilo_surface_cso *surface =
583 (const struct ilo_surface_cso *) vec->fb.state.zsbuf;
584 const struct ilo_texture_slice *slice =
585 ilo_texture_get_slice(ilo_texture(surface->base.texture),
586 surface->base.u.tex.level, surface->base.u.tex.first_layer);
587
588 assert(!surface->is_rt);
589 zs = &surface->u.zs;
590 clear_params = slice->clear_value;
591 }
592 else {
593 zs = &vec->fb.null_zs;
594 clear_params = 0;
595 }
596
597 gen6_3DSTATE_DEPTH_BUFFER(r->builder, zs, false);
598 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder, zs);
599 gen6_3DSTATE_STENCIL_BUFFER(r->builder, zs);
600 gen7_3DSTATE_CLEAR_PARAMS(r->builder, clear_params);
601 }
602 }
603
604 static void
605 gen7_draw_wm_multisample(struct ilo_render *r,
606 const struct ilo_state_vector *vec,
607 struct ilo_render_draw_session *session)
608 {
609 /* 3DSTATE_MULTISAMPLE and 3DSTATE_SAMPLE_MASK */
610 if (DIRTY(SAMPLE_MASK) || DIRTY(FB)) {
611 const uint32_t *packed_sample_pos;
612
613 gen7_wa_pre_3dstate_multisample(r);
614
615 packed_sample_pos =
616 (vec->fb.num_samples > 4) ? r->packed_sample_position_8x :
617 (vec->fb.num_samples > 1) ? &r->packed_sample_position_4x :
618 &r->packed_sample_position_1x;
619
620 gen6_3DSTATE_MULTISAMPLE(r->builder,
621 vec->fb.num_samples, packed_sample_pos,
622 vec->rasterizer->state.half_pixel_center);
623
624 gen7_3DSTATE_SAMPLE_MASK(r->builder,
625 (vec->fb.num_samples > 1) ? vec->sample_mask : 0x1,
626 vec->fb.num_samples);
627 }
628 }
629
630 static void
631 gen7_draw_vf_draw(struct ilo_render *r,
632 const struct ilo_state_vector *vec,
633 struct ilo_render_draw_session *session)
634 {
635 if (r->state.deferred_pipe_control_dw1)
636 gen7_pipe_control(r, r->state.deferred_pipe_control_dw1);
637
638 /* 3DPRIMITIVE */
639 gen7_3DPRIMITIVE(r->builder, vec->draw, &vec->ib);
640
641 r->state.current_pipe_control_dw1 = 0;
642 r->state.deferred_pipe_control_dw1 = 0;
643 }
644
645 void
646 ilo_render_emit_draw_commands_gen7(struct ilo_render *render,
647 const struct ilo_state_vector *vec,
648 struct ilo_render_draw_session *session)
649 {
650 ILO_DEV_ASSERT(render->dev, 7, 7.5);
651
652 /*
653 * We try to keep the order of the commands match, as closely as possible,
654 * that of the classic i965 driver. It allows us to compare the command
655 * streams easily.
656 */
657 gen6_draw_common_select(render, vec, session);
658 gen6_draw_common_sip(render, vec, session);
659 gen6_draw_vf_statistics(render, vec, session);
660 gen7_draw_common_pcb_alloc(render, vec, session);
661 gen6_draw_common_base_address(render, vec, session);
662 gen7_draw_common_pointers_1(render, vec, session);
663 gen7_draw_common_urb(render, vec, session);
664 gen7_draw_common_pointers_2(render, vec, session);
665 gen7_draw_wm_multisample(render, vec, session);
666 gen7_draw_gs(render, vec, session);
667 gen7_draw_hs(render, vec, session);
668 gen7_draw_te(render, vec, session);
669 gen7_draw_ds(render, vec, session);
670 gen7_draw_vs(render, vec, session);
671 gen7_draw_sol(render, vec, session);
672 gen6_draw_clip(render, vec, session);
673 gen7_draw_sf(render, vec, session);
674 gen7_draw_wm(render, vec, session);
675 gen6_draw_wm_raster(render, vec, session);
676 gen6_draw_sf_rect(render, vec, session);
677 gen6_draw_vf(render, vec, session);
678 gen7_draw_vf_draw(render, vec, session);
679 }
680
681 static void
682 gen7_rectlist_pcb_alloc(struct ilo_render *r,
683 const struct ilo_blitter *blitter)
684 {
685 /*
686 * Push constant buffers are only allowed to take up at most the first
687 * 16KB of the URB. Split the space evenly for VS and FS.
688 */
689 const int max_size =
690 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ? 32768 : 16384;
691 const int size = max_size / 2;
692 int offset = 0;
693
694 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_VS(r->builder, offset, size);
695 offset += size;
696
697 gen7_3DSTATE_PUSH_CONSTANT_ALLOC_PS(r->builder, offset, size);
698
699 gen7_wa_post_3dstate_push_constant_alloc_ps(r);
700 }
701
702 static void
703 gen7_rectlist_urb(struct ilo_render *r,
704 const struct ilo_blitter *blitter)
705 {
706 /* the first 16KB are reserved for VS and PS PCBs */
707 const int offset =
708 (ilo_dev_gen(r->dev) == ILO_GEN(7.5) && r->dev->gt == 3) ? 32768 : 16384;
709
710 gen7_3DSTATE_URB_VS(r->builder, offset, r->dev->urb_size - offset,
711 (blitter->ve.count + blitter->ve.prepend_nosrc_cso) *
712 4 * sizeof(float));
713
714 gen7_3DSTATE_URB_GS(r->builder, offset, 0, 0);
715 gen7_3DSTATE_URB_HS(r->builder, offset, 0, 0);
716 gen7_3DSTATE_URB_DS(r->builder, offset, 0, 0);
717 }
718
719 static void
720 gen7_rectlist_vs_to_sf(struct ilo_render *r,
721 const struct ilo_blitter *blitter)
722 {
723 gen7_3DSTATE_CONSTANT_VS(r->builder, NULL, NULL, 0);
724 gen6_disable_3DSTATE_VS(r->builder);
725
726 gen7_3DSTATE_CONSTANT_HS(r->builder, NULL, NULL, 0);
727 gen7_3DSTATE_HS(r->builder, NULL);
728
729 gen7_3DSTATE_TE(r->builder);
730
731 gen7_3DSTATE_CONSTANT_DS(r->builder, NULL, NULL, 0);
732 gen7_3DSTATE_DS(r->builder, NULL);
733
734 gen7_3DSTATE_CONSTANT_GS(r->builder, NULL, NULL, 0);
735 gen7_3DSTATE_GS(r->builder, NULL);
736
737 gen7_3DSTATE_STREAMOUT(r->builder, 0x0, 0, false);
738
739 gen6_disable_3DSTATE_CLIP(r->builder);
740
741 gen7_wa_pre_3dstate_sf_depth_bias(r);
742
743 gen7_3DSTATE_SF(r->builder, NULL, blitter->fb.dst.base.format);
744 gen7_3DSTATE_SBE(r->builder, NULL, NULL);
745 }
746
747 static void
748 gen7_rectlist_wm(struct ilo_render *r,
749 const struct ilo_blitter *blitter)
750 {
751 uint32_t hiz_op;
752
753 switch (blitter->op) {
754 case ILO_BLITTER_RECTLIST_CLEAR_ZS:
755 hiz_op = GEN7_WM_DW1_DEPTH_CLEAR;
756 break;
757 case ILO_BLITTER_RECTLIST_RESOLVE_Z:
758 hiz_op = GEN7_WM_DW1_DEPTH_RESOLVE;
759 break;
760 case ILO_BLITTER_RECTLIST_RESOLVE_HIZ:
761 hiz_op = GEN7_WM_DW1_HIZ_RESOLVE;
762 break;
763 default:
764 hiz_op = 0;
765 break;
766 }
767
768 gen7_hiz_3DSTATE_WM(r->builder, hiz_op);
769
770 gen7_3DSTATE_CONSTANT_PS(r->builder, NULL, NULL, 0);
771
772 gen7_wa_pre_3dstate_ps_max_threads(r);
773 gen7_disable_3DSTATE_PS(r->builder);
774 }
775
776 static void
777 gen7_rectlist_wm_depth(struct ilo_render *r,
778 const struct ilo_blitter *blitter)
779 {
780 gen7_wa_pre_depth(r);
781
782 if (blitter->uses & (ILO_BLITTER_USE_FB_DEPTH |
783 ILO_BLITTER_USE_FB_STENCIL)) {
784 gen6_3DSTATE_DEPTH_BUFFER(r->builder,
785 &blitter->fb.dst.u.zs, true);
786 }
787
788 if (blitter->uses & ILO_BLITTER_USE_FB_DEPTH) {
789 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder,
790 &blitter->fb.dst.u.zs);
791 }
792
793 if (blitter->uses & ILO_BLITTER_USE_FB_STENCIL) {
794 gen6_3DSTATE_STENCIL_BUFFER(r->builder,
795 &blitter->fb.dst.u.zs);
796 }
797
798 gen7_3DSTATE_CLEAR_PARAMS(r->builder,
799 blitter->depth_clear_value);
800 }
801
802 static void
803 gen7_rectlist_wm_multisample(struct ilo_render *r,
804 const struct ilo_blitter *blitter)
805 {
806 const uint32_t *packed_sample_pos =
807 (blitter->fb.num_samples > 4) ? r->packed_sample_position_8x :
808 (blitter->fb.num_samples > 1) ? &r->packed_sample_position_4x :
809 &r->packed_sample_position_1x;
810
811 gen7_wa_pre_3dstate_multisample(r);
812
813 gen6_3DSTATE_MULTISAMPLE(r->builder, blitter->fb.num_samples,
814 packed_sample_pos, true);
815
816 gen7_3DSTATE_SAMPLE_MASK(r->builder,
817 (1 << blitter->fb.num_samples) - 1, blitter->fb.num_samples);
818 }
819
820 void
821 ilo_render_emit_rectlist_commands_gen7(struct ilo_render *r,
822 const struct ilo_blitter *blitter,
823 const struct ilo_render_rectlist_session *session)
824 {
825 ILO_DEV_ASSERT(r->dev, 7, 7.5);
826
827 gen7_rectlist_wm_multisample(r, blitter);
828
829 gen6_state_base_address(r->builder, true);
830
831 gen6_user_3DSTATE_VERTEX_BUFFERS(r->builder,
832 session->vb_start, session->vb_end,
833 sizeof(blitter->vertices[0]));
834
835 gen6_3DSTATE_VERTEX_ELEMENTS(r->builder, &blitter->ve);
836
837 gen7_rectlist_pcb_alloc(r, blitter);
838
839 /* needed for any VS-related commands */
840 gen7_wa_pre_vs(r);
841
842 gen7_rectlist_urb(r, blitter);
843
844 if (blitter->uses & ILO_BLITTER_USE_DSA) {
845 gen7_3DSTATE_DEPTH_STENCIL_STATE_POINTERS(r->builder,
846 r->state.DEPTH_STENCIL_STATE);
847 }
848
849 if (blitter->uses & ILO_BLITTER_USE_CC) {
850 gen7_3DSTATE_CC_STATE_POINTERS(r->builder,
851 r->state.COLOR_CALC_STATE);
852 }
853
854 gen7_rectlist_vs_to_sf(r, blitter);
855 gen7_rectlist_wm(r, blitter);
856
857 if (blitter->uses & ILO_BLITTER_USE_VIEWPORT) {
858 gen7_3DSTATE_VIEWPORT_STATE_POINTERS_CC(r->builder,
859 r->state.CC_VIEWPORT);
860 }
861
862 gen7_rectlist_wm_depth(r, blitter);
863
864 gen6_3DSTATE_DRAWING_RECTANGLE(r->builder, 0, 0,
865 blitter->fb.width, blitter->fb.height);
866
867 gen7_3DPRIMITIVE(r->builder, &blitter->draw, NULL);
868 }
869
870 int
871 ilo_render_get_draw_commands_len_gen7(const struct ilo_render *render,
872 const struct ilo_state_vector *vec)
873 {
874 static int len;
875
876 ILO_DEV_ASSERT(render->dev, 7, 7.5);
877
878 if (!len) {
879 len += GEN7_3DSTATE_URB_ANY__SIZE * 4;
880 len += GEN7_3DSTATE_PUSH_CONSTANT_ALLOC_ANY__SIZE * 5;
881 len += GEN6_3DSTATE_CONSTANT_ANY__SIZE * 5;
882 len += GEN7_3DSTATE_POINTERS_ANY__SIZE * (5 + 5 + 4);
883 len += GEN7_3DSTATE_SO_BUFFER__SIZE * 4;
884 len += GEN6_PIPE_CONTROL__SIZE * 5;
885
886 len +=
887 GEN6_STATE_BASE_ADDRESS__SIZE +
888 GEN6_STATE_SIP__SIZE +
889 GEN6_3DSTATE_VF_STATISTICS__SIZE +
890 GEN6_PIPELINE_SELECT__SIZE +
891 GEN6_3DSTATE_CLEAR_PARAMS__SIZE +
892 GEN6_3DSTATE_DEPTH_BUFFER__SIZE +
893 GEN6_3DSTATE_STENCIL_BUFFER__SIZE +
894 GEN6_3DSTATE_HIER_DEPTH_BUFFER__SIZE +
895 GEN6_3DSTATE_VERTEX_BUFFERS__SIZE +
896 GEN6_3DSTATE_VERTEX_ELEMENTS__SIZE +
897 GEN6_3DSTATE_INDEX_BUFFER__SIZE +
898 GEN75_3DSTATE_VF__SIZE +
899 GEN6_3DSTATE_VS__SIZE +
900 GEN6_3DSTATE_GS__SIZE +
901 GEN6_3DSTATE_CLIP__SIZE +
902 GEN6_3DSTATE_SF__SIZE +
903 GEN6_3DSTATE_WM__SIZE +
904 GEN6_3DSTATE_SAMPLE_MASK__SIZE +
905 GEN7_3DSTATE_HS__SIZE +
906 GEN7_3DSTATE_TE__SIZE +
907 GEN7_3DSTATE_DS__SIZE +
908 GEN7_3DSTATE_STREAMOUT__SIZE +
909 GEN7_3DSTATE_SBE__SIZE +
910 GEN7_3DSTATE_PS__SIZE +
911 GEN6_3DSTATE_DRAWING_RECTANGLE__SIZE +
912 GEN6_3DSTATE_POLY_STIPPLE_OFFSET__SIZE +
913 GEN6_3DSTATE_POLY_STIPPLE_PATTERN__SIZE +
914 GEN6_3DSTATE_LINE_STIPPLE__SIZE +
915 GEN6_3DSTATE_AA_LINE_PARAMETERS__SIZE +
916 GEN6_3DSTATE_MULTISAMPLE__SIZE +
917 GEN7_3DSTATE_SO_DECL_LIST__SIZE +
918 GEN6_3DPRIMITIVE__SIZE;
919 }
920
921 return len;
922 }