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