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