ilo: add 3DSTATE_VF_INSTANCING to ilo_state_vf
[mesa.git] / src / gallium / drivers / ilo / ilo_render_gen6.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_mi.h"
31 #include "core/ilo_builder_render.h"
32 #include "util/u_prim.h"
33
34 #include "ilo_blitter.h"
35 #include "ilo_query.h"
36 #include "ilo_shader.h"
37 #include "ilo_state.h"
38 #include "ilo_render_gen.h"
39
40 /**
41 * This should be called before PIPE_CONTROL.
42 */
43 void
44 gen6_wa_pre_pipe_control(struct ilo_render *r, uint32_t dw1)
45 {
46 /*
47 * From the Sandy Bridge PRM, volume 2 part 1, page 60:
48 *
49 * "Pipe-control with CS-stall bit set must be sent BEFORE the
50 * pipe-control with a post-sync op and no write-cache flushes."
51 *
52 * This WA may also be triggered indirectly by the other two WAs on the
53 * same page:
54 *
55 * "Before any depth stall flush (including those produced by
56 * non-pipelined state commands), software needs to first send a
57 * PIPE_CONTROL with no bits set except Post-Sync Operation != 0."
58 *
59 * "Before a PIPE_CONTROL with Write Cache Flush Enable =1, a
60 * PIPE_CONTROL with any non-zero post-sync-op is required."
61 */
62 const bool direct_wa_cond = (dw1 & GEN6_PIPE_CONTROL_WRITE__MASK) &&
63 !(dw1 & GEN6_PIPE_CONTROL_RENDER_CACHE_FLUSH);
64 const bool indirect_wa_cond = (dw1 & GEN6_PIPE_CONTROL_DEPTH_STALL) |
65 (dw1 & GEN6_PIPE_CONTROL_RENDER_CACHE_FLUSH);
66
67 ILO_DEV_ASSERT(r->dev, 6, 6);
68
69 if (!direct_wa_cond && !indirect_wa_cond)
70 return;
71
72 if (!(r->state.current_pipe_control_dw1 & GEN6_PIPE_CONTROL_CS_STALL)) {
73 /*
74 * From the Sandy Bridge PRM, volume 2 part 1, page 73:
75 *
76 * "1 of the following must also be set (when CS stall is set):
77 *
78 * - Depth Cache Flush Enable ([0] of DW1)
79 * - Stall at Pixel Scoreboard ([1] of DW1)
80 * - Depth Stall ([13] of DW1)
81 * - Post-Sync Operation ([13] of DW1)
82 * - Render Target Cache Flush Enable ([12] of DW1)
83 * - Notify Enable ([8] of DW1)"
84 *
85 * Because of the WAs above, we have to pick Stall at Pixel Scoreboard.
86 */
87 const uint32_t direct_wa = GEN6_PIPE_CONTROL_CS_STALL |
88 GEN6_PIPE_CONTROL_PIXEL_SCOREBOARD_STALL;
89
90 ilo_render_pipe_control(r, direct_wa);
91 }
92
93 if (indirect_wa_cond &&
94 !(r->state.current_pipe_control_dw1 & GEN6_PIPE_CONTROL_WRITE__MASK)) {
95 const uint32_t indirect_wa = GEN6_PIPE_CONTROL_WRITE_IMM;
96
97 ilo_render_pipe_control(r, indirect_wa);
98 }
99 }
100
101 /**
102 * This should be called before any non-pipelined state command.
103 */
104 static void
105 gen6_wa_pre_non_pipelined(struct ilo_render *r)
106 {
107 ILO_DEV_ASSERT(r->dev, 6, 6);
108
109 /* non-pipelined state commands produce depth stall */
110 gen6_wa_pre_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL);
111 }
112
113 static void
114 gen6_wa_post_3dstate_urb_no_gs(struct ilo_render *r)
115 {
116 /*
117 * From the Sandy Bridge PRM, volume 2 part 1, page 27:
118 *
119 * "Because of a urb corruption caused by allocating a previous
120 * gsunit's urb entry to vsunit software is required to send a
121 * "GS NULL Fence" (Send URB fence with VS URB size == 1 and GS URB
122 * size == 0) plus a dummy DRAW call before any case where VS will
123 * be taking over GS URB space."
124 */
125 const uint32_t dw1 = GEN6_PIPE_CONTROL_CS_STALL;
126
127 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
128 gen6_wa_pre_pipe_control(r, dw1);
129 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
130 ilo_render_pipe_control(r, dw1);
131 }
132
133 static void
134 gen6_wa_post_3dstate_constant_vs(struct ilo_render *r)
135 {
136 /*
137 * According to upload_vs_state() of the classic driver, we need to emit a
138 * PIPE_CONTROL after 3DSTATE_CONSTANT_VS, otherwise the command is kept
139 * being buffered by VS FF, to the point that the FF dies.
140 */
141 const uint32_t dw1 = GEN6_PIPE_CONTROL_DEPTH_STALL |
142 GEN6_PIPE_CONTROL_INSTRUCTION_CACHE_INVALIDATE |
143 GEN6_PIPE_CONTROL_STATE_CACHE_INVALIDATE;
144
145 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
146 gen6_wa_pre_pipe_control(r, dw1);
147 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
148 ilo_render_pipe_control(r, dw1);
149 }
150
151 static void
152 gen6_wa_pre_3dstate_vs_toggle(struct ilo_render *r)
153 {
154 /*
155 * The classic driver has this undocumented WA:
156 *
157 * From the BSpec, 3D Pipeline > Geometry > Vertex Shader > State,
158 * 3DSTATE_VS, Dword 5.0 "VS Function Enable":
159 *
160 * [DevSNB] A pipeline flush must be programmed prior to a 3DSTATE_VS
161 * command that causes the VS Function Enable to toggle. Pipeline
162 * flush can be executed by sending a PIPE_CONTROL command with CS
163 * stall bit set and a post sync operation.
164 */
165 const uint32_t dw1 = GEN6_PIPE_CONTROL_WRITE_IMM |
166 GEN6_PIPE_CONTROL_CS_STALL;
167
168 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
169 gen6_wa_pre_pipe_control(r, dw1);
170 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
171 ilo_render_pipe_control(r, dw1);
172 }
173
174 static void
175 gen6_wa_pre_3dstate_wm_max_threads(struct ilo_render *r)
176 {
177 /*
178 * From the Sandy Bridge PRM, volume 2 part 1, page 274:
179 *
180 * "A PIPE_CONTROL command, with only the Stall At Pixel Scoreboard
181 * field set (DW1 Bit 1), must be issued prior to any change to the
182 * value in this field (Maximum Number of Threads in 3DSTATE_WM)"
183 */
184 const uint32_t dw1 = GEN6_PIPE_CONTROL_PIXEL_SCOREBOARD_STALL;
185
186 ILO_DEV_ASSERT(r->dev, 6, 6);
187
188 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
189 gen6_wa_pre_pipe_control(r, dw1);
190 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
191 ilo_render_pipe_control(r, dw1);
192 }
193
194 static void
195 gen6_wa_pre_3dstate_multisample(struct ilo_render *r)
196 {
197 /*
198 * From the Sandy Bridge PRM, volume 2 part 1, page 305:
199 *
200 * "Driver must guarentee that all the caches in the depth pipe are
201 * flushed before this command (3DSTATE_MULTISAMPLE) is parsed. This
202 * requires driver to send a PIPE_CONTROL with a CS stall along with a
203 * Depth Flush prior to this command."
204 */
205 const uint32_t dw1 = GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH |
206 GEN6_PIPE_CONTROL_CS_STALL;
207
208 ILO_DEV_ASSERT(r->dev, 6, 6);
209
210 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
211 gen6_wa_pre_pipe_control(r, dw1);
212 if ((r->state.current_pipe_control_dw1 & dw1) != dw1)
213 ilo_render_pipe_control(r, dw1);
214 }
215
216 static void
217 gen6_wa_pre_depth(struct ilo_render *r)
218 {
219 ILO_DEV_ASSERT(r->dev, 6, 6);
220
221 /*
222 * From the Ivy Bridge PRM, volume 2 part 1, page 315:
223 *
224 * "Restriction: Prior to changing Depth/Stencil Buffer state (i.e.,
225 * any combination of 3DSTATE_DEPTH_BUFFER, 3DSTATE_CLEAR_PARAMS,
226 * 3DSTATE_STENCIL_BUFFER, 3DSTATE_HIER_DEPTH_BUFFER) SW must first
227 * issue a pipelined depth stall (PIPE_CONTROL with Depth Stall bit
228 * set), followed by a pipelined depth cache flush (PIPE_CONTROL with
229 * Depth Flush Bit set, followed by another pipelined depth stall
230 * (PIPE_CONTROL with Depth Stall Bit set), unless SW can otherwise
231 * guarantee that the pipeline from WM onwards is already flushed
232 * (e.g., via a preceding MI_FLUSH)."
233 *
234 * According to the classic driver, it also applies for GEN6.
235 */
236 gen6_wa_pre_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL |
237 GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH);
238
239 ilo_render_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL);
240 ilo_render_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_CACHE_FLUSH);
241 ilo_render_pipe_control(r, GEN6_PIPE_CONTROL_DEPTH_STALL);
242 }
243
244 #define DIRTY(state) (session->pipe_dirty & ILO_DIRTY_ ## state)
245
246 void
247 gen6_draw_common_select(struct ilo_render *r,
248 const struct ilo_state_vector *vec,
249 struct ilo_render_draw_session *session)
250 {
251 /* PIPELINE_SELECT */
252 if (r->hw_ctx_changed) {
253 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
254 gen6_wa_pre_non_pipelined(r);
255
256 gen6_PIPELINE_SELECT(r->builder, 0x0);
257 }
258 }
259
260 void
261 gen6_draw_common_sip(struct ilo_render *r,
262 const struct ilo_state_vector *vec,
263 struct ilo_render_draw_session *session)
264 {
265 /* STATE_SIP */
266 if (r->hw_ctx_changed) {
267 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
268 gen6_wa_pre_non_pipelined(r);
269
270 gen6_STATE_SIP(r->builder, 0);
271 }
272 }
273
274 void
275 gen6_draw_common_base_address(struct ilo_render *r,
276 const struct ilo_state_vector *vec,
277 struct ilo_render_draw_session *session)
278 {
279 /* STATE_BASE_ADDRESS */
280 if (r->state_bo_changed || r->instruction_bo_changed ||
281 r->batch_bo_changed) {
282 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
283 gen6_wa_pre_non_pipelined(r);
284
285 if (ilo_dev_gen(r->dev) >= ILO_GEN(8))
286 gen8_state_base_address(r->builder, r->hw_ctx_changed);
287 else
288 gen6_state_base_address(r->builder, r->hw_ctx_changed);
289
290 /*
291 * From the Sandy Bridge PRM, volume 1 part 1, page 28:
292 *
293 * "The following commands must be reissued following any change to
294 * the base addresses:
295 *
296 * * 3DSTATE_BINDING_TABLE_POINTERS
297 * * 3DSTATE_SAMPLER_STATE_POINTERS
298 * * 3DSTATE_VIEWPORT_STATE_POINTERS
299 * * 3DSTATE_CC_POINTERS
300 * * MEDIA_STATE_POINTERS"
301 *
302 * 3DSTATE_SCISSOR_STATE_POINTERS is not on the list, but it is
303 * reasonable to also reissue the command. Same to PCB.
304 */
305 session->viewport_changed = true;
306
307 session->scissor_changed = true;
308
309 session->blend_changed = true;
310 session->dsa_changed = true;
311 session->cc_changed = true;
312
313 session->sampler_vs_changed = true;
314 session->sampler_gs_changed = true;
315 session->sampler_fs_changed = true;
316
317 session->pcb_vs_changed = true;
318 session->pcb_gs_changed = true;
319 session->pcb_fs_changed = true;
320
321 session->binding_table_vs_changed = true;
322 session->binding_table_gs_changed = true;
323 session->binding_table_fs_changed = true;
324 }
325 }
326
327 static void
328 gen6_draw_common_urb(struct ilo_render *r,
329 const struct ilo_state_vector *vec,
330 struct ilo_render_draw_session *session)
331 {
332 const bool gs_active = (vec->gs || (vec->vs &&
333 ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_GEN6_SO)));
334
335 /* 3DSTATE_URB */
336 if (session->urb_delta.dirty & (ILO_STATE_URB_3DSTATE_URB_VS |
337 ILO_STATE_URB_3DSTATE_URB_GS)) {
338 gen6_3DSTATE_URB(r->builder, &vec->urb);
339
340 if (r->state.gs.active && !gs_active)
341 gen6_wa_post_3dstate_urb_no_gs(r);
342 }
343
344 r->state.gs.active = gs_active;
345 }
346
347 static void
348 gen6_draw_common_pointers_1(struct ilo_render *r,
349 const struct ilo_state_vector *vec,
350 struct ilo_render_draw_session *session)
351 {
352 /* 3DSTATE_VIEWPORT_STATE_POINTERS */
353 if (session->viewport_changed) {
354 gen6_3DSTATE_VIEWPORT_STATE_POINTERS(r->builder,
355 r->state.CLIP_VIEWPORT,
356 r->state.SF_VIEWPORT,
357 r->state.CC_VIEWPORT);
358 }
359 }
360
361 static void
362 gen6_draw_common_pointers_2(struct ilo_render *r,
363 const struct ilo_state_vector *vec,
364 struct ilo_render_draw_session *session)
365 {
366 /* 3DSTATE_CC_STATE_POINTERS */
367 if (session->blend_changed ||
368 session->dsa_changed ||
369 session->cc_changed) {
370 gen6_3DSTATE_CC_STATE_POINTERS(r->builder,
371 r->state.BLEND_STATE,
372 r->state.DEPTH_STENCIL_STATE,
373 r->state.COLOR_CALC_STATE);
374 }
375
376 /* 3DSTATE_SAMPLER_STATE_POINTERS */
377 if (session->sampler_vs_changed ||
378 session->sampler_gs_changed ||
379 session->sampler_fs_changed) {
380 gen6_3DSTATE_SAMPLER_STATE_POINTERS(r->builder,
381 r->state.vs.SAMPLER_STATE,
382 0,
383 r->state.wm.SAMPLER_STATE);
384 }
385 }
386
387 static void
388 gen6_draw_common_pointers_3(struct ilo_render *r,
389 const struct ilo_state_vector *vec,
390 struct ilo_render_draw_session *session)
391 {
392 /* 3DSTATE_SCISSOR_STATE_POINTERS */
393 if (session->scissor_changed) {
394 gen6_3DSTATE_SCISSOR_STATE_POINTERS(r->builder,
395 r->state.SCISSOR_RECT);
396 }
397
398 /* 3DSTATE_BINDING_TABLE_POINTERS */
399 if (session->binding_table_vs_changed ||
400 session->binding_table_gs_changed ||
401 session->binding_table_fs_changed) {
402 gen6_3DSTATE_BINDING_TABLE_POINTERS(r->builder,
403 r->state.vs.BINDING_TABLE_STATE,
404 r->state.gs.BINDING_TABLE_STATE,
405 r->state.wm.BINDING_TABLE_STATE);
406 }
407 }
408
409 void
410 gen6_draw_vf(struct ilo_render *r,
411 const struct ilo_state_vector *vec,
412 struct ilo_render_draw_session *session)
413 {
414 if (ilo_dev_gen(r->dev) >= ILO_GEN(7.5)) {
415 /* 3DSTATE_INDEX_BUFFER */
416 if ((session->vf_delta.dirty & ILO_STATE_VF_3DSTATE_INDEX_BUFFER) ||
417 DIRTY(IB) || r->batch_bo_changed)
418 gen6_3DSTATE_INDEX_BUFFER(r->builder, &vec->ve->vf, &vec->ib);
419
420 /* 3DSTATE_VF */
421 if (session->vf_delta.dirty & ILO_STATE_VF_3DSTATE_VF)
422 gen75_3DSTATE_VF(r->builder, &vec->ve->vf);
423 } else {
424 /* 3DSTATE_INDEX_BUFFER */
425 if ((session->vf_delta.dirty & ILO_STATE_VF_3DSTATE_INDEX_BUFFER) ||
426 DIRTY(IB) || r->batch_bo_changed)
427 gen6_3DSTATE_INDEX_BUFFER(r->builder, &vec->ve->vf, &vec->ib);
428 }
429
430 /* 3DSTATE_VERTEX_BUFFERS */
431 if ((session->vf_delta.dirty & ILO_STATE_VF_3DSTATE_VERTEX_BUFFERS) ||
432 DIRTY(VB) || DIRTY(VE) || r->batch_bo_changed) {
433 gen6_3DSTATE_VERTEX_BUFFERS(r->builder, &vec->ve->vf, &vec->vb,
434 vec->ve->vb_mapping, vec->ve->vb_count);
435 }
436
437 /* 3DSTATE_VERTEX_ELEMENTS */
438 if (session->vf_delta.dirty & ILO_STATE_VF_3DSTATE_VERTEX_ELEMENTS)
439 gen6_3DSTATE_VERTEX_ELEMENTS(r->builder, &vec->ve->vf);
440 }
441
442 void
443 gen6_draw_vf_statistics(struct ilo_render *r,
444 const struct ilo_state_vector *vec,
445 struct ilo_render_draw_session *session)
446 {
447 /* 3DSTATE_VF_STATISTICS */
448 if (r->hw_ctx_changed)
449 gen6_3DSTATE_VF_STATISTICS(r->builder, false);
450 }
451
452 void
453 gen6_draw_vs(struct ilo_render *r,
454 const struct ilo_state_vector *vec,
455 struct ilo_render_draw_session *session)
456 {
457 /* 3DSTATE_CONSTANT_VS */
458 if (session->pcb_vs_changed) {
459 gen6_3DSTATE_CONSTANT_VS(r->builder,
460 &r->state.vs.PUSH_CONSTANT_BUFFER,
461 &r->state.vs.PUSH_CONSTANT_BUFFER_size,
462 1);
463
464 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
465 gen6_wa_post_3dstate_constant_vs(r);
466 }
467
468 /* 3DSTATE_VS */
469 if (DIRTY(VS) || r->instruction_bo_changed) {
470 const union ilo_shader_cso *cso = ilo_shader_get_kernel_cso(vec->vs);
471 const uint32_t kernel_offset = ilo_shader_get_kernel_offset(vec->vs);
472
473 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
474 gen6_wa_pre_3dstate_vs_toggle(r);
475
476 if (ilo_dev_gen(r->dev) == ILO_GEN(6) &&
477 ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_GEN6_SO))
478 gen6_3DSTATE_VS(r->builder, &cso->vs_sol.vs, kernel_offset);
479 else
480 gen6_3DSTATE_VS(r->builder, &cso->vs, kernel_offset);
481 }
482 }
483
484 static void
485 gen6_draw_gs(struct ilo_render *r,
486 const struct ilo_state_vector *vec,
487 struct ilo_render_draw_session *session)
488 {
489 /* 3DSTATE_CONSTANT_GS */
490 if (session->pcb_gs_changed)
491 gen6_3DSTATE_CONSTANT_GS(r->builder, NULL, NULL, 0);
492
493 /* 3DSTATE_GS */
494 if (DIRTY(GS) || DIRTY(VS) ||
495 session->prim_changed || r->instruction_bo_changed) {
496 const union ilo_shader_cso *cso;
497 uint32_t kernel_offset;
498
499 if (vec->gs) {
500 cso = ilo_shader_get_kernel_cso(vec->gs);
501 kernel_offset = ilo_shader_get_kernel_offset(vec->gs);
502
503 gen6_3DSTATE_GS(r->builder, &cso->gs, kernel_offset);
504 } else if (ilo_dev_gen(r->dev) == ILO_GEN(6) &&
505 ilo_shader_get_kernel_param(vec->vs, ILO_KERNEL_VS_GEN6_SO)) {
506 const int verts_per_prim =
507 u_vertices_per_prim(session->reduced_prim);
508 enum ilo_kernel_param param;
509
510 switch (verts_per_prim) {
511 case 1:
512 param = ILO_KERNEL_VS_GEN6_SO_POINT_OFFSET;
513 break;
514 case 2:
515 param = ILO_KERNEL_VS_GEN6_SO_LINE_OFFSET;
516 break;
517 default:
518 param = ILO_KERNEL_VS_GEN6_SO_TRI_OFFSET;
519 break;
520 }
521
522 cso = ilo_shader_get_kernel_cso(vec->vs);
523 kernel_offset = ilo_shader_get_kernel_offset(vec->vs) +
524 ilo_shader_get_kernel_param(vec->vs, param);
525
526 gen6_3DSTATE_GS(r->builder, &cso->vs_sol.sol, kernel_offset);
527 } else {
528 gen6_3DSTATE_GS(r->builder, &vec->disabled_gs, 0);
529 }
530 }
531 }
532
533 static bool
534 gen6_draw_update_max_svbi(struct ilo_render *r,
535 const struct ilo_state_vector *vec,
536 struct ilo_render_draw_session *session)
537 {
538 if (DIRTY(VS) || DIRTY(GS) || DIRTY(SO)) {
539 const struct pipe_stream_output_info *so_info =
540 (vec->gs) ? ilo_shader_get_kernel_so_info(vec->gs) :
541 (vec->vs) ? ilo_shader_get_kernel_so_info(vec->vs) : NULL;
542 unsigned max_svbi = 0xffffffff;
543 int i;
544
545 for (i = 0; i < so_info->num_outputs; i++) {
546 const int output_buffer = so_info->output[i].output_buffer;
547 const struct pipe_stream_output_target *so =
548 vec->so.states[output_buffer];
549 const int struct_size = so_info->stride[output_buffer] * 4;
550 const int elem_size = so_info->output[i].num_components * 4;
551 int buf_size, count;
552
553 if (!so) {
554 max_svbi = 0;
555 break;
556 }
557
558 buf_size = so->buffer_size - so_info->output[i].dst_offset * 4;
559
560 count = buf_size / struct_size;
561 if (buf_size % struct_size >= elem_size)
562 count++;
563
564 if (count < max_svbi)
565 max_svbi = count;
566 }
567
568 if (r->state.so_max_vertices != max_svbi) {
569 r->state.so_max_vertices = max_svbi;
570 return true;
571 }
572 }
573
574 return false;
575 }
576
577 static void
578 gen6_draw_gs_svbi(struct ilo_render *r,
579 const struct ilo_state_vector *vec,
580 struct ilo_render_draw_session *session)
581 {
582 const bool emit = gen6_draw_update_max_svbi(r, vec, session);
583
584 /* 3DSTATE_GS_SVB_INDEX */
585 if (emit) {
586 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
587 gen6_wa_pre_non_pipelined(r);
588
589 gen6_3DSTATE_GS_SVB_INDEX(r->builder,
590 0, 0, r->state.so_max_vertices,
591 false);
592
593 if (r->hw_ctx_changed) {
594 int i;
595
596 /*
597 * From the Sandy Bridge PRM, volume 2 part 1, page 148:
598 *
599 * "If a buffer is not enabled then the SVBI must be set to 0x0
600 * in order to not cause overflow in that SVBI."
601 *
602 * "If a buffer is not enabled then the MaxSVBI must be set to
603 * 0xFFFFFFFF in order to not cause overflow in that SVBI."
604 */
605 for (i = 1; i < 4; i++) {
606 gen6_3DSTATE_GS_SVB_INDEX(r->builder,
607 i, 0, 0xffffffff, false);
608 }
609 }
610 }
611 }
612
613 void
614 gen6_draw_clip(struct ilo_render *r,
615 const struct ilo_state_vector *vec,
616 struct ilo_render_draw_session *session)
617 {
618 /* 3DSTATE_CLIP */
619 if (session->rs_delta.dirty & ILO_STATE_RASTER_3DSTATE_CLIP)
620 gen6_3DSTATE_CLIP(r->builder, &vec->rasterizer->rs);
621 }
622
623 static void
624 gen6_draw_sf(struct ilo_render *r,
625 const struct ilo_state_vector *vec,
626 struct ilo_render_draw_session *session)
627 {
628 /* 3DSTATE_SF */
629 if ((session->rs_delta.dirty & ILO_STATE_RASTER_3DSTATE_SF) || DIRTY(FS)) {
630 const struct ilo_state_sbe *sbe = ilo_shader_get_kernel_sbe(vec->fs);
631 gen6_3DSTATE_SF(r->builder, &vec->rasterizer->rs, sbe);
632 }
633 }
634
635 void
636 gen6_draw_sf_rect(struct ilo_render *r,
637 const struct ilo_state_vector *vec,
638 struct ilo_render_draw_session *session)
639 {
640 /* 3DSTATE_DRAWING_RECTANGLE */
641 if (DIRTY(FB)) {
642 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
643 gen6_wa_pre_non_pipelined(r);
644
645 gen6_3DSTATE_DRAWING_RECTANGLE(r->builder, 0, 0,
646 vec->fb.state.width, vec->fb.state.height);
647 }
648 }
649
650 static void
651 gen6_draw_wm(struct ilo_render *r,
652 const struct ilo_state_vector *vec,
653 struct ilo_render_draw_session *session)
654 {
655 /* 3DSTATE_CONSTANT_PS */
656 if (session->pcb_fs_changed) {
657 gen6_3DSTATE_CONSTANT_PS(r->builder,
658 &r->state.wm.PUSH_CONSTANT_BUFFER,
659 &r->state.wm.PUSH_CONSTANT_BUFFER_size,
660 1);
661 }
662
663 /* 3DSTATE_WM */
664 if (DIRTY(FS) ||
665 (session->rs_delta.dirty & ILO_STATE_RASTER_3DSTATE_WM) ||
666 r->instruction_bo_changed) {
667 const union ilo_shader_cso *cso = ilo_shader_get_kernel_cso(vec->fs);
668 const uint32_t kernel_offset = ilo_shader_get_kernel_offset(vec->fs);
669
670 if (ilo_dev_gen(r->dev) == ILO_GEN(6) && r->hw_ctx_changed)
671 gen6_wa_pre_3dstate_wm_max_threads(r);
672
673 gen6_3DSTATE_WM(r->builder, &vec->rasterizer->rs,
674 &cso->ps, kernel_offset);
675 }
676 }
677
678 static void
679 gen6_draw_wm_multisample(struct ilo_render *r,
680 const struct ilo_state_vector *vec,
681 struct ilo_render_draw_session *session)
682 {
683 /* 3DSTATE_MULTISAMPLE */
684 if (DIRTY(FB) || (session->rs_delta.dirty &
685 ILO_STATE_RASTER_3DSTATE_MULTISAMPLE)) {
686 const uint8_t sample_count = (vec->fb.num_samples > 1) ? 4 : 1;
687
688 if (ilo_dev_gen(r->dev) == ILO_GEN(6)) {
689 gen6_wa_pre_non_pipelined(r);
690 gen6_wa_pre_3dstate_multisample(r);
691 }
692
693 gen6_3DSTATE_MULTISAMPLE(r->builder, &vec->rasterizer->rs,
694 &r->sample_pattern, sample_count);
695 }
696
697 /* 3DSTATE_SAMPLE_MASK */
698 if (session->rs_delta.dirty & ILO_STATE_RASTER_3DSTATE_SAMPLE_MASK)
699 gen6_3DSTATE_SAMPLE_MASK(r->builder, &vec->rasterizer->rs);
700 }
701
702 static void
703 gen6_draw_wm_depth(struct ilo_render *r,
704 const struct ilo_state_vector *vec,
705 struct ilo_render_draw_session *session)
706 {
707 /* 3DSTATE_DEPTH_BUFFER and 3DSTATE_CLEAR_PARAMS */
708 if (DIRTY(FB) || r->batch_bo_changed) {
709 const struct ilo_state_zs *zs;
710 uint32_t clear_params;
711
712 if (vec->fb.state.zsbuf) {
713 const struct ilo_surface_cso *surface =
714 (const struct ilo_surface_cso *) vec->fb.state.zsbuf;
715 const struct ilo_texture_slice *slice =
716 ilo_texture_get_slice(ilo_texture(surface->base.texture),
717 surface->base.u.tex.level, surface->base.u.tex.first_layer);
718
719 assert(!surface->is_rt);
720
721 zs = &surface->u.zs;
722 clear_params = slice->clear_value;
723 }
724 else {
725 zs = &vec->fb.null_zs;
726 clear_params = 0;
727 }
728
729 if (ilo_dev_gen(r->dev) == ILO_GEN(6)) {
730 gen6_wa_pre_non_pipelined(r);
731 gen6_wa_pre_depth(r);
732 }
733
734 gen6_3DSTATE_DEPTH_BUFFER(r->builder, zs);
735 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder, zs);
736 gen6_3DSTATE_STENCIL_BUFFER(r->builder, zs);
737 gen6_3DSTATE_CLEAR_PARAMS(r->builder, clear_params);
738 }
739 }
740
741 void
742 gen6_draw_wm_raster(struct ilo_render *r,
743 const struct ilo_state_vector *vec,
744 struct ilo_render_draw_session *session)
745 {
746 /* 3DSTATE_POLY_STIPPLE_PATTERN and 3DSTATE_POLY_STIPPLE_OFFSET */
747 if ((DIRTY(RASTERIZER) || DIRTY(POLY_STIPPLE)) &&
748 vec->rasterizer->state.poly_stipple_enable) {
749 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
750 gen6_wa_pre_non_pipelined(r);
751
752 gen6_3DSTATE_POLY_STIPPLE_PATTERN(r->builder, &vec->poly_stipple);
753 gen6_3DSTATE_POLY_STIPPLE_OFFSET(r->builder, &vec->poly_stipple);
754 }
755
756 /* 3DSTATE_LINE_STIPPLE */
757 if (DIRTY(RASTERIZER) && vec->rasterizer->state.line_stipple_enable) {
758 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
759 gen6_wa_pre_non_pipelined(r);
760
761 gen6_3DSTATE_LINE_STIPPLE(r->builder, &vec->line_stipple);
762 }
763
764 /* 3DSTATE_AA_LINE_PARAMETERS */
765 if (session->rs_delta.dirty &
766 ILO_STATE_RASTER_3DSTATE_AA_LINE_PARAMETERS) {
767 if (ilo_dev_gen(r->dev) == ILO_GEN(6))
768 gen6_wa_pre_non_pipelined(r);
769
770 gen6_3DSTATE_AA_LINE_PARAMETERS(r->builder, &vec->rasterizer->rs);
771 }
772 }
773
774 #undef DIRTY
775
776 void
777 ilo_render_emit_draw_commands_gen6(struct ilo_render *render,
778 const struct ilo_state_vector *vec,
779 struct ilo_render_draw_session *session)
780 {
781 ILO_DEV_ASSERT(render->dev, 6, 6);
782
783 /*
784 * We try to keep the order of the commands match, as closely as possible,
785 * that of the classic i965 driver. It allows us to compare the command
786 * streams easily.
787 */
788 gen6_draw_common_select(render, vec, session);
789 gen6_draw_gs_svbi(render, vec, session);
790 gen6_draw_common_sip(render, vec, session);
791 gen6_draw_vf_statistics(render, vec, session);
792 gen6_draw_common_base_address(render, vec, session);
793 gen6_draw_common_pointers_1(render, vec, session);
794 gen6_draw_common_urb(render, vec, session);
795 gen6_draw_common_pointers_2(render, vec, session);
796 gen6_draw_wm_multisample(render, vec, session);
797 gen6_draw_vs(render, vec, session);
798 gen6_draw_gs(render, vec, session);
799 gen6_draw_clip(render, vec, session);
800 gen6_draw_sf(render, vec, session);
801 gen6_draw_wm(render, vec, session);
802 gen6_draw_common_pointers_3(render, vec, session);
803 gen6_draw_wm_depth(render, vec, session);
804 gen6_draw_wm_raster(render, vec, session);
805 gen6_draw_sf_rect(render, vec, session);
806 gen6_draw_vf(render, vec, session);
807
808 ilo_render_3dprimitive(render, vec->draw, &vec->ib);
809 }
810
811 static void
812 gen6_rectlist_vs_to_sf(struct ilo_render *r,
813 const struct ilo_blitter *blitter)
814 {
815 gen6_3DSTATE_CONSTANT_VS(r->builder, NULL, NULL, 0);
816 gen6_wa_post_3dstate_constant_vs(r);
817
818 gen6_wa_pre_3dstate_vs_toggle(r);
819 gen6_3DSTATE_VS(r->builder, &blitter->vs, 0);
820
821 gen6_3DSTATE_CONSTANT_GS(r->builder, NULL, NULL, 0);
822 gen6_3DSTATE_GS(r->builder, &blitter->gs, 0);
823
824 gen6_3DSTATE_CLIP(r->builder, &blitter->fb.rs);
825 gen6_3DSTATE_SF(r->builder, &blitter->fb.rs, &blitter->sbe);
826 }
827
828 static void
829 gen6_rectlist_wm(struct ilo_render *r,
830 const struct ilo_blitter *blitter)
831 {
832 gen6_3DSTATE_CONSTANT_PS(r->builder, NULL, NULL, 0);
833
834 gen6_wa_pre_3dstate_wm_max_threads(r);
835 gen6_3DSTATE_WM(r->builder, &blitter->fb.rs, &blitter->ps, 0);
836 }
837
838 static void
839 gen6_rectlist_wm_depth(struct ilo_render *r,
840 const struct ilo_blitter *blitter)
841 {
842 gen6_wa_pre_depth(r);
843
844 if (blitter->uses & (ILO_BLITTER_USE_FB_DEPTH |
845 ILO_BLITTER_USE_FB_STENCIL))
846 gen6_3DSTATE_DEPTH_BUFFER(r->builder, &blitter->fb.dst.u.zs);
847
848 if (blitter->uses & ILO_BLITTER_USE_FB_DEPTH) {
849 gen6_3DSTATE_HIER_DEPTH_BUFFER(r->builder,
850 &blitter->fb.dst.u.zs);
851 }
852
853 if (blitter->uses & ILO_BLITTER_USE_FB_STENCIL) {
854 gen6_3DSTATE_STENCIL_BUFFER(r->builder,
855 &blitter->fb.dst.u.zs);
856 }
857
858 gen6_3DSTATE_CLEAR_PARAMS(r->builder,
859 blitter->depth_clear_value);
860 }
861
862 static void
863 gen6_rectlist_wm_multisample(struct ilo_render *r,
864 const struct ilo_blitter *blitter)
865 {
866 const uint8_t sample_count = (blitter->fb.num_samples > 1) ? 4 : 1;
867
868 gen6_wa_pre_3dstate_multisample(r);
869
870 gen6_3DSTATE_MULTISAMPLE(r->builder, &blitter->fb.rs, &r->sample_pattern, sample_count);
871 gen6_3DSTATE_SAMPLE_MASK(r->builder, &blitter->fb.rs);
872 }
873
874 int
875 ilo_render_get_rectlist_commands_len_gen6(const struct ilo_render *render,
876 const struct ilo_blitter *blitter)
877 {
878 ILO_DEV_ASSERT(render->dev, 6, 7.5);
879
880 return 256;
881 }
882
883 void
884 ilo_render_emit_rectlist_commands_gen6(struct ilo_render *r,
885 const struct ilo_blitter *blitter,
886 const struct ilo_render_rectlist_session *session)
887 {
888 ILO_DEV_ASSERT(r->dev, 6, 6);
889
890 gen6_wa_pre_non_pipelined(r);
891
892 gen6_rectlist_wm_multisample(r, blitter);
893
894 gen6_state_base_address(r->builder, true);
895
896 gen6_user_3DSTATE_VERTEX_BUFFERS(r->builder,
897 session->vb_start, session->vb_end,
898 sizeof(blitter->vertices[0]));
899
900 gen6_3DSTATE_VERTEX_ELEMENTS(r->builder, &blitter->vf);
901
902 gen6_3DSTATE_URB(r->builder, &blitter->urb);
903
904 if (r->state.gs.active) {
905 gen6_wa_post_3dstate_urb_no_gs(r);
906 r->state.gs.active = false;
907 }
908
909 if (blitter->uses &
910 (ILO_BLITTER_USE_DSA | ILO_BLITTER_USE_CC)) {
911 gen6_3DSTATE_CC_STATE_POINTERS(r->builder, 0,
912 r->state.DEPTH_STENCIL_STATE, r->state.COLOR_CALC_STATE);
913 }
914
915 gen6_rectlist_vs_to_sf(r, blitter);
916 gen6_rectlist_wm(r, blitter);
917
918 if (blitter->uses & ILO_BLITTER_USE_VIEWPORT) {
919 gen6_3DSTATE_VIEWPORT_STATE_POINTERS(r->builder,
920 0, 0, r->state.CC_VIEWPORT);
921 }
922
923 gen6_rectlist_wm_depth(r, blitter);
924
925 gen6_3DSTATE_DRAWING_RECTANGLE(r->builder, 0, 0,
926 blitter->fb.width, blitter->fb.height);
927
928 ilo_render_3dprimitive(r, &blitter->draw, NULL);
929 }
930
931 int
932 ilo_render_get_draw_commands_len_gen6(const struct ilo_render *render,
933 const struct ilo_state_vector *vec)
934 {
935 static int len;
936
937 ILO_DEV_ASSERT(render->dev, 6, 6);
938
939 if (!len) {
940 len += GEN6_3DSTATE_CONSTANT_ANY__SIZE * 3;
941 len += GEN6_3DSTATE_GS_SVB_INDEX__SIZE * 4;
942 len += GEN6_PIPE_CONTROL__SIZE * 5;
943
944 len +=
945 GEN6_STATE_BASE_ADDRESS__SIZE +
946 GEN6_STATE_SIP__SIZE +
947 GEN6_3DSTATE_VF_STATISTICS__SIZE +
948 GEN6_PIPELINE_SELECT__SIZE +
949 GEN6_3DSTATE_BINDING_TABLE_POINTERS__SIZE +
950 GEN6_3DSTATE_SAMPLER_STATE_POINTERS__SIZE +
951 GEN6_3DSTATE_URB__SIZE +
952 GEN6_3DSTATE_VERTEX_BUFFERS__SIZE +
953 GEN6_3DSTATE_VERTEX_ELEMENTS__SIZE +
954 GEN6_3DSTATE_INDEX_BUFFER__SIZE +
955 GEN6_3DSTATE_VIEWPORT_STATE_POINTERS__SIZE +
956 GEN6_3DSTATE_CC_STATE_POINTERS__SIZE +
957 GEN6_3DSTATE_SCISSOR_STATE_POINTERS__SIZE +
958 GEN6_3DSTATE_VS__SIZE +
959 GEN6_3DSTATE_GS__SIZE +
960 GEN6_3DSTATE_CLIP__SIZE +
961 GEN6_3DSTATE_SF__SIZE +
962 GEN6_3DSTATE_WM__SIZE +
963 GEN6_3DSTATE_SAMPLE_MASK__SIZE +
964 GEN6_3DSTATE_DRAWING_RECTANGLE__SIZE +
965 GEN6_3DSTATE_DEPTH_BUFFER__SIZE +
966 GEN6_3DSTATE_POLY_STIPPLE_OFFSET__SIZE +
967 GEN6_3DSTATE_POLY_STIPPLE_PATTERN__SIZE +
968 GEN6_3DSTATE_LINE_STIPPLE__SIZE +
969 GEN6_3DSTATE_AA_LINE_PARAMETERS__SIZE +
970 GEN6_3DSTATE_MULTISAMPLE__SIZE +
971 GEN6_3DSTATE_STENCIL_BUFFER__SIZE +
972 GEN6_3DSTATE_HIER_DEPTH_BUFFER__SIZE +
973 GEN6_3DSTATE_CLEAR_PARAMS__SIZE +
974 GEN6_3DPRIMITIVE__SIZE;
975 }
976
977 return len;
978 }