c72d93a49df14011c39eb321b664edc8ae7bf180
[mesa.git] / src / gallium / drivers / ilo / ilo_state.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 2012-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 "util/u_framebuffer.h"
29 #include "util/u_helpers.h"
30 #include "util/u_upload_mgr.h"
31
32 #include "ilo_context.h"
33 #include "ilo_resource.h"
34 #include "ilo_shader.h"
35 #include "ilo_state.h"
36
37 static void
38 finalize_shader_states(struct ilo_context *ilo)
39 {
40 unsigned type;
41
42 for (type = 0; type < PIPE_SHADER_TYPES; type++) {
43 struct ilo_shader_state *shader;
44 uint32_t state;
45
46 switch (type) {
47 case PIPE_SHADER_VERTEX:
48 shader = ilo->vs;
49 state = ILO_DIRTY_VS;
50 break;
51 case PIPE_SHADER_GEOMETRY:
52 shader = ilo->gs;
53 state = ILO_DIRTY_GS;
54 break;
55 case PIPE_SHADER_FRAGMENT:
56 shader = ilo->fs;
57 state = ILO_DIRTY_FS;
58 break;
59 default:
60 shader = NULL;
61 state = 0;
62 break;
63 }
64
65 if (!shader)
66 continue;
67
68 /* compile if the shader or the states it depends on changed */
69 if (ilo->dirty & state) {
70 ilo_shader_select_kernel(shader, ilo, ILO_DIRTY_ALL);
71 }
72 else if (ilo_shader_select_kernel(shader, ilo, ilo->dirty)) {
73 /* mark the state dirty if a new kernel is selected */
74 ilo->dirty |= state;
75 }
76
77 /* need to setup SBE for FS */
78 if (type == PIPE_SHADER_FRAGMENT && ilo->dirty &
79 (state | ILO_DIRTY_GS | ILO_DIRTY_VS | ILO_DIRTY_RASTERIZER)) {
80 if (ilo_shader_select_kernel_routing(shader,
81 (ilo->gs) ? ilo->gs : ilo->vs, ilo->rasterizer))
82 ilo->dirty |= state;
83 }
84 }
85 }
86
87 static void
88 finalize_cbuf_state(struct ilo_context *ilo,
89 struct ilo_cbuf_state *cbuf,
90 const struct ilo_shader_state *sh)
91 {
92 uint32_t upload_mask = cbuf->enabled_mask;
93
94 /* skip CBUF0 if the kernel does not need it */
95 upload_mask &=
96 ~ilo_shader_get_kernel_param(sh, ILO_KERNEL_SKIP_CBUF0_UPLOAD);
97
98 while (upload_mask) {
99 const enum pipe_format elem_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
100 unsigned offset, i;
101
102 i = u_bit_scan(&upload_mask);
103 /* no need to upload */
104 if (cbuf->cso[i].resource)
105 continue;
106
107 u_upload_data(ilo->uploader, 0, cbuf->cso[i].user_buffer_size,
108 cbuf->cso[i].user_buffer, &offset, &cbuf->cso[i].resource);
109
110 ilo_gpe_init_view_surface_for_buffer(ilo->dev,
111 ilo_buffer(cbuf->cso[i].resource),
112 offset, cbuf->cso[i].user_buffer_size,
113 util_format_get_blocksize(elem_format), elem_format,
114 false, false, &cbuf->cso[i].surface);
115
116 ilo->dirty |= ILO_DIRTY_CBUF;
117 }
118 }
119
120 static void
121 finalize_constant_buffers(struct ilo_context *ilo)
122 {
123 if (ilo->dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_VS))
124 finalize_cbuf_state(ilo, &ilo->cbuf[PIPE_SHADER_VERTEX], ilo->vs);
125
126 if (ilo->dirty & (ILO_DIRTY_CBUF | ILO_DIRTY_FS))
127 finalize_cbuf_state(ilo, &ilo->cbuf[PIPE_SHADER_FRAGMENT], ilo->fs);
128 }
129
130 static void
131 finalize_index_buffer(struct ilo_context *ilo)
132 {
133 const bool need_upload = (ilo->draw->indexed &&
134 (ilo->ib.user_buffer || ilo->ib.offset % ilo->ib.index_size));
135 struct pipe_resource *current_hw_res = NULL;
136
137 if (!(ilo->dirty & ILO_DIRTY_IB) && !need_upload)
138 return;
139
140 pipe_resource_reference(&current_hw_res, ilo->ib.hw_resource);
141
142 if (need_upload) {
143 const unsigned offset = ilo->ib.index_size * ilo->draw->start;
144 const unsigned size = ilo->ib.index_size * ilo->draw->count;
145 unsigned hw_offset;
146
147 if (ilo->ib.user_buffer) {
148 u_upload_data(ilo->uploader, 0, size,
149 ilo->ib.user_buffer + offset, &hw_offset, &ilo->ib.hw_resource);
150 }
151 else {
152 u_upload_buffer(ilo->uploader, 0, ilo->ib.offset + offset, size,
153 ilo->ib.buffer, &hw_offset, &ilo->ib.hw_resource);
154 }
155
156 /* the HW offset should be aligned */
157 assert(hw_offset % ilo->ib.index_size == 0);
158 ilo->ib.draw_start_offset = hw_offset / ilo->ib.index_size;
159
160 /*
161 * INDEX[ilo->draw->start] in the original buffer is INDEX[0] in the HW
162 * resource
163 */
164 ilo->ib.draw_start_offset -= ilo->draw->start;
165 }
166 else {
167 pipe_resource_reference(&ilo->ib.hw_resource, ilo->ib.buffer);
168
169 /* note that index size may be zero when the draw is not indexed */
170 if (ilo->draw->indexed)
171 ilo->ib.draw_start_offset = ilo->ib.offset / ilo->ib.index_size;
172 else
173 ilo->ib.draw_start_offset = 0;
174 }
175
176 /* treat the IB as clean if the HW states do not change */
177 if (ilo->ib.hw_resource == current_hw_res &&
178 ilo->ib.hw_index_size == ilo->ib.index_size)
179 ilo->dirty &= ~ILO_DIRTY_IB;
180 else
181 ilo->ib.hw_index_size = ilo->ib.index_size;
182
183 pipe_resource_reference(&current_hw_res, NULL);
184 }
185
186 /**
187 * Finalize states. Some states depend on other states and are
188 * incomplete/invalid until finalized.
189 */
190 void
191 ilo_finalize_3d_states(struct ilo_context *ilo,
192 const struct pipe_draw_info *draw)
193 {
194 ilo->draw = draw;
195
196 finalize_shader_states(ilo);
197 finalize_constant_buffers(ilo);
198 finalize_index_buffer(ilo);
199
200 u_upload_unmap(ilo->uploader);
201 }
202
203 static void *
204 ilo_create_blend_state(struct pipe_context *pipe,
205 const struct pipe_blend_state *state)
206 {
207 struct ilo_context *ilo = ilo_context(pipe);
208 struct ilo_blend_state *blend;
209
210 blend = MALLOC_STRUCT(ilo_blend_state);
211 assert(blend);
212
213 ilo_gpe_init_blend(ilo->dev, state, blend);
214
215 return blend;
216 }
217
218 static void
219 ilo_bind_blend_state(struct pipe_context *pipe, void *state)
220 {
221 struct ilo_context *ilo = ilo_context(pipe);
222
223 ilo->blend = state;
224
225 ilo->dirty |= ILO_DIRTY_BLEND;
226 }
227
228 static void
229 ilo_delete_blend_state(struct pipe_context *pipe, void *state)
230 {
231 FREE(state);
232 }
233
234 static void *
235 ilo_create_sampler_state(struct pipe_context *pipe,
236 const struct pipe_sampler_state *state)
237 {
238 struct ilo_context *ilo = ilo_context(pipe);
239 struct ilo_sampler_cso *sampler;
240
241 sampler = MALLOC_STRUCT(ilo_sampler_cso);
242 assert(sampler);
243
244 ilo_gpe_init_sampler_cso(ilo->dev, state, sampler);
245
246 return sampler;
247 }
248
249 static bool
250 ilo_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
251 unsigned start, unsigned count, void **samplers)
252 {
253 struct ilo_context *ilo = ilo_context(pipe);
254 struct ilo_sampler_state *dst = &ilo->sampler[shader];
255 bool changed = false;
256 unsigned i;
257
258 assert(start + count <= Elements(dst->cso));
259
260 if (samplers) {
261 for (i = 0; i < count; i++) {
262 if (dst->cso[start + i] != samplers[i]) {
263 dst->cso[start + i] = samplers[i];
264
265 /*
266 * This function is sometimes called to reduce the number of bound
267 * samplers. Do not consider that as a state change (and create a
268 * new array of SAMPLER_STATE).
269 */
270 if (samplers[i])
271 changed = true;
272 }
273 }
274 }
275 else {
276 for (i = 0; i < count; i++)
277 dst->cso[start + i] = NULL;
278 }
279
280 if (dst->count <= start + count) {
281 if (samplers)
282 count += start;
283 else
284 count = start;
285
286 while (count > 0 && !dst->cso[count - 1])
287 count--;
288
289 dst->count = count;
290 }
291
292 return changed;
293 }
294
295 static void
296 ilo_bind_fragment_sampler_states(struct pipe_context *pipe,
297 unsigned num_samplers,
298 void **samplers)
299 {
300 struct ilo_context *ilo = ilo_context(pipe);
301
302 if (ilo_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT,
303 0, num_samplers, samplers))
304 ilo->dirty |= ILO_DIRTY_SAMPLER_FS;
305
306 if (ilo->sampler[PIPE_SHADER_FRAGMENT].count > num_samplers) {
307 ilo_bind_sampler_states(pipe, PIPE_SHADER_FRAGMENT, num_samplers,
308 ilo->sampler[PIPE_SHADER_FRAGMENT].count - num_samplers, NULL);
309 }
310 }
311
312 static void
313 ilo_bind_vertex_sampler_states(struct pipe_context *pipe,
314 unsigned num_samplers,
315 void **samplers)
316 {
317 struct ilo_context *ilo = ilo_context(pipe);
318
319 if (ilo_bind_sampler_states(pipe, PIPE_SHADER_VERTEX,
320 0, num_samplers, samplers))
321 ilo->dirty |= ILO_DIRTY_SAMPLER_VS;
322
323 if (ilo->sampler[PIPE_SHADER_VERTEX].count > num_samplers) {
324 ilo_bind_sampler_states(pipe, PIPE_SHADER_VERTEX, num_samplers,
325 ilo->sampler[PIPE_SHADER_VERTEX].count - num_samplers, NULL);
326 }
327 }
328
329 static void
330 ilo_bind_geometry_sampler_states(struct pipe_context *pipe,
331 unsigned num_samplers,
332 void **samplers)
333 {
334 struct ilo_context *ilo = ilo_context(pipe);
335
336 if (ilo_bind_sampler_states(pipe, PIPE_SHADER_GEOMETRY,
337 0, num_samplers, samplers))
338 ilo->dirty |= ILO_DIRTY_SAMPLER_GS;
339
340 if (ilo->sampler[PIPE_SHADER_GEOMETRY].count > num_samplers) {
341 ilo_bind_sampler_states(pipe, PIPE_SHADER_GEOMETRY, num_samplers,
342 ilo->sampler[PIPE_SHADER_GEOMETRY].count - num_samplers, NULL);
343 }
344 }
345
346 static void
347 ilo_bind_compute_sampler_states(struct pipe_context *pipe,
348 unsigned start_slot,
349 unsigned num_samplers,
350 void **samplers)
351 {
352 struct ilo_context *ilo = ilo_context(pipe);
353
354 if (ilo_bind_sampler_states(pipe, PIPE_SHADER_COMPUTE,
355 start_slot, num_samplers, samplers))
356 ilo->dirty |= ILO_DIRTY_SAMPLER_CS;
357 }
358
359 static void
360 ilo_delete_sampler_state(struct pipe_context *pipe, void *state)
361 {
362 FREE(state);
363 }
364
365 static void *
366 ilo_create_rasterizer_state(struct pipe_context *pipe,
367 const struct pipe_rasterizer_state *state)
368 {
369 struct ilo_context *ilo = ilo_context(pipe);
370 struct ilo_rasterizer_state *rast;
371
372 rast = MALLOC_STRUCT(ilo_rasterizer_state);
373 assert(rast);
374
375 rast->state = *state;
376 ilo_gpe_init_rasterizer(ilo->dev, state, rast);
377
378 return rast;
379 }
380
381 static void
382 ilo_bind_rasterizer_state(struct pipe_context *pipe, void *state)
383 {
384 struct ilo_context *ilo = ilo_context(pipe);
385
386 ilo->rasterizer = state;
387
388 ilo->dirty |= ILO_DIRTY_RASTERIZER;
389 }
390
391 static void
392 ilo_delete_rasterizer_state(struct pipe_context *pipe, void *state)
393 {
394 FREE(state);
395 }
396
397 static void *
398 ilo_create_depth_stencil_alpha_state(struct pipe_context *pipe,
399 const struct pipe_depth_stencil_alpha_state *state)
400 {
401 struct ilo_context *ilo = ilo_context(pipe);
402 struct ilo_dsa_state *dsa;
403
404 dsa = MALLOC_STRUCT(ilo_dsa_state);
405 assert(dsa);
406
407 ilo_gpe_init_dsa(ilo->dev, state, dsa);
408
409 return dsa;
410 }
411
412 static void
413 ilo_bind_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
414 {
415 struct ilo_context *ilo = ilo_context(pipe);
416
417 ilo->dsa = state;
418
419 ilo->dirty |= ILO_DIRTY_DSA;
420 }
421
422 static void
423 ilo_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
424 {
425 FREE(state);
426 }
427
428 static void *
429 ilo_create_fs_state(struct pipe_context *pipe,
430 const struct pipe_shader_state *state)
431 {
432 struct ilo_context *ilo = ilo_context(pipe);
433 struct ilo_shader_state *shader;
434
435 shader = ilo_shader_create_fs(ilo->dev, state, ilo);
436 assert(shader);
437
438 ilo_shader_cache_add(ilo->shader_cache, shader);
439
440 return shader;
441 }
442
443 static void
444 ilo_bind_fs_state(struct pipe_context *pipe, void *state)
445 {
446 struct ilo_context *ilo = ilo_context(pipe);
447
448 ilo->fs = state;
449
450 ilo->dirty |= ILO_DIRTY_FS;
451 }
452
453 static void
454 ilo_delete_fs_state(struct pipe_context *pipe, void *state)
455 {
456 struct ilo_context *ilo = ilo_context(pipe);
457 struct ilo_shader_state *fs = (struct ilo_shader_state *) state;
458
459 ilo_shader_cache_remove(ilo->shader_cache, fs);
460 ilo_shader_destroy(fs);
461 }
462
463 static void *
464 ilo_create_vs_state(struct pipe_context *pipe,
465 const struct pipe_shader_state *state)
466 {
467 struct ilo_context *ilo = ilo_context(pipe);
468 struct ilo_shader_state *shader;
469
470 shader = ilo_shader_create_vs(ilo->dev, state, ilo);
471 assert(shader);
472
473 ilo_shader_cache_add(ilo->shader_cache, shader);
474
475 return shader;
476 }
477
478 static void
479 ilo_bind_vs_state(struct pipe_context *pipe, void *state)
480 {
481 struct ilo_context *ilo = ilo_context(pipe);
482
483 ilo->vs = state;
484
485 ilo->dirty |= ILO_DIRTY_VS;
486 }
487
488 static void
489 ilo_delete_vs_state(struct pipe_context *pipe, void *state)
490 {
491 struct ilo_context *ilo = ilo_context(pipe);
492 struct ilo_shader_state *vs = (struct ilo_shader_state *) state;
493
494 ilo_shader_cache_remove(ilo->shader_cache, vs);
495 ilo_shader_destroy(vs);
496 }
497
498 static void *
499 ilo_create_gs_state(struct pipe_context *pipe,
500 const struct pipe_shader_state *state)
501 {
502 struct ilo_context *ilo = ilo_context(pipe);
503 struct ilo_shader_state *shader;
504
505 shader = ilo_shader_create_gs(ilo->dev, state, ilo);
506 assert(shader);
507
508 ilo_shader_cache_add(ilo->shader_cache, shader);
509
510 return shader;
511 }
512
513 static void
514 ilo_bind_gs_state(struct pipe_context *pipe, void *state)
515 {
516 struct ilo_context *ilo = ilo_context(pipe);
517
518 /* util_blitter may set this unnecessarily */
519 if (ilo->gs == state)
520 return;
521
522 ilo->gs = state;
523
524 ilo->dirty |= ILO_DIRTY_GS;
525 }
526
527 static void
528 ilo_delete_gs_state(struct pipe_context *pipe, void *state)
529 {
530 struct ilo_context *ilo = ilo_context(pipe);
531 struct ilo_shader_state *gs = (struct ilo_shader_state *) state;
532
533 ilo_shader_cache_remove(ilo->shader_cache, gs);
534 ilo_shader_destroy(gs);
535 }
536
537 static void *
538 ilo_create_vertex_elements_state(struct pipe_context *pipe,
539 unsigned num_elements,
540 const struct pipe_vertex_element *elements)
541 {
542 struct ilo_context *ilo = ilo_context(pipe);
543 struct ilo_ve_state *ve;
544
545 ve = MALLOC_STRUCT(ilo_ve_state);
546 assert(ve);
547
548 ilo_gpe_init_ve(ilo->dev, num_elements, elements, ve);
549
550 return ve;
551 }
552
553 static void
554 ilo_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
555 {
556 struct ilo_context *ilo = ilo_context(pipe);
557
558 ilo->ve = state;
559
560 ilo->dirty |= ILO_DIRTY_VE;
561 }
562
563 static void
564 ilo_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
565 {
566 struct ilo_ve_state *ve = state;
567
568 FREE(ve);
569 }
570
571 static void
572 ilo_set_blend_color(struct pipe_context *pipe,
573 const struct pipe_blend_color *state)
574 {
575 struct ilo_context *ilo = ilo_context(pipe);
576
577 ilo->blend_color = *state;
578
579 ilo->dirty |= ILO_DIRTY_BLEND_COLOR;
580 }
581
582 static void
583 ilo_set_stencil_ref(struct pipe_context *pipe,
584 const struct pipe_stencil_ref *state)
585 {
586 struct ilo_context *ilo = ilo_context(pipe);
587
588 /* util_blitter may set this unnecessarily */
589 if (!memcmp(&ilo->stencil_ref, state, sizeof(*state)))
590 return;
591
592 ilo->stencil_ref = *state;
593
594 ilo->dirty |= ILO_DIRTY_STENCIL_REF;
595 }
596
597 static void
598 ilo_set_sample_mask(struct pipe_context *pipe,
599 unsigned sample_mask)
600 {
601 struct ilo_context *ilo = ilo_context(pipe);
602
603 /* util_blitter may set this unnecessarily */
604 if (ilo->sample_mask == sample_mask)
605 return;
606
607 ilo->sample_mask = sample_mask;
608
609 ilo->dirty |= ILO_DIRTY_SAMPLE_MASK;
610 }
611
612 static void
613 ilo_set_clip_state(struct pipe_context *pipe,
614 const struct pipe_clip_state *state)
615 {
616 struct ilo_context *ilo = ilo_context(pipe);
617
618 ilo->clip = *state;
619
620 ilo->dirty |= ILO_DIRTY_CLIP;
621 }
622
623 static void
624 ilo_set_constant_buffer(struct pipe_context *pipe,
625 uint shader, uint index,
626 struct pipe_constant_buffer *buf)
627 {
628 struct ilo_context *ilo = ilo_context(pipe);
629 struct ilo_cbuf_state *cbuf = &ilo->cbuf[shader];
630 const unsigned count = 1;
631 unsigned i;
632
633 assert(shader < Elements(ilo->cbuf));
634 assert(index + count <= Elements(ilo->cbuf[shader].cso));
635
636 if (buf) {
637 for (i = 0; i < count; i++) {
638 struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
639
640 pipe_resource_reference(&cso->resource, buf[i].buffer);
641
642 if (buf[i].buffer) {
643 const enum pipe_format elem_format =
644 PIPE_FORMAT_R32G32B32A32_FLOAT;
645
646 ilo_gpe_init_view_surface_for_buffer(ilo->dev,
647 ilo_buffer(buf[i].buffer),
648 buf[i].buffer_offset, buf[i].buffer_size,
649 util_format_get_blocksize(elem_format), elem_format,
650 false, false, &cso->surface);
651
652 cso->user_buffer = NULL;
653 cso->user_buffer_size = 0;
654
655 cbuf->enabled_mask |= 1 << (index + i);
656 }
657 else if (buf[i].user_buffer) {
658 cso->surface.bo = NULL;
659
660 /* buffer_offset does not apply for user buffer */
661 cso->user_buffer = buf[i].user_buffer;
662 cso->user_buffer_size = buf[i].buffer_size;
663
664 cbuf->enabled_mask |= 1 << (index + i);
665 }
666 else {
667 cso->surface.bo = NULL;
668 cso->user_buffer = NULL;
669 cso->user_buffer_size = 0;
670
671 cbuf->enabled_mask &= ~(1 << (index + i));
672 }
673 }
674 }
675 else {
676 for (i = 0; i < count; i++) {
677 struct ilo_cbuf_cso *cso = &cbuf->cso[index + i];
678
679 pipe_resource_reference(&cso->resource, NULL);
680 cso->surface.bo = NULL;
681 cso->user_buffer = NULL;
682 cso->user_buffer_size = 0;
683
684 cbuf->enabled_mask &= ~(1 << (index + i));
685 }
686 }
687
688 ilo->dirty |= ILO_DIRTY_CBUF;
689 }
690
691 static void
692 ilo_set_framebuffer_state(struct pipe_context *pipe,
693 const struct pipe_framebuffer_state *state)
694 {
695 struct ilo_context *ilo = ilo_context(pipe);
696
697 util_copy_framebuffer_state(&ilo->fb.state, state);
698
699 if (state->nr_cbufs)
700 ilo->fb.num_samples = state->cbufs[0]->texture->nr_samples;
701 else if (state->zsbuf)
702 ilo->fb.num_samples = state->zsbuf->texture->nr_samples;
703 else
704 ilo->fb.num_samples = 1;
705
706 if (!ilo->fb.num_samples)
707 ilo->fb.num_samples = 1;
708
709 ilo->dirty |= ILO_DIRTY_FB;
710 }
711
712 static void
713 ilo_set_polygon_stipple(struct pipe_context *pipe,
714 const struct pipe_poly_stipple *state)
715 {
716 struct ilo_context *ilo = ilo_context(pipe);
717
718 ilo->poly_stipple = *state;
719
720 ilo->dirty |= ILO_DIRTY_POLY_STIPPLE;
721 }
722
723 static void
724 ilo_set_scissor_states(struct pipe_context *pipe,
725 unsigned start_slot,
726 unsigned num_scissors,
727 const struct pipe_scissor_state *scissors)
728 {
729 struct ilo_context *ilo = ilo_context(pipe);
730
731 ilo_gpe_set_scissor(ilo->dev, start_slot, num_scissors,
732 scissors, &ilo->scissor);
733
734 ilo->dirty |= ILO_DIRTY_SCISSOR;
735 }
736
737 static void
738 ilo_set_viewport_states(struct pipe_context *pipe,
739 unsigned start_slot,
740 unsigned num_viewports,
741 const struct pipe_viewport_state *viewports)
742 {
743 struct ilo_context *ilo = ilo_context(pipe);
744
745 if (viewports) {
746 unsigned i;
747
748 for (i = 0; i < num_viewports; i++) {
749 ilo_gpe_set_viewport_cso(ilo->dev, &viewports[i],
750 &ilo->viewport.cso[start_slot + i]);
751 }
752
753 if (ilo->viewport.count < start_slot + num_viewports)
754 ilo->viewport.count = start_slot + num_viewports;
755
756 /* need to save viewport 0 for util_blitter */
757 if (!start_slot && num_viewports)
758 ilo->viewport.viewport0 = viewports[0];
759 }
760 else {
761 if (ilo->viewport.count <= start_slot + num_viewports &&
762 ilo->viewport.count > start_slot)
763 ilo->viewport.count = start_slot;
764 }
765
766 ilo->dirty |= ILO_DIRTY_VIEWPORT;
767 }
768
769 static void
770 ilo_set_sampler_views(struct pipe_context *pipe, unsigned shader,
771 unsigned start, unsigned count,
772 struct pipe_sampler_view **views)
773 {
774 struct ilo_context *ilo = ilo_context(pipe);
775 struct ilo_view_state *dst = &ilo->view[shader];
776 unsigned i;
777
778 assert(start + count <= Elements(dst->states));
779
780 if (views) {
781 for (i = 0; i < count; i++)
782 pipe_sampler_view_reference(&dst->states[start + i], views[i]);
783 }
784 else {
785 for (i = 0; i < count; i++)
786 pipe_sampler_view_reference(&dst->states[start + i], NULL);
787 }
788
789 if (dst->count <= start + count) {
790 if (views)
791 count += start;
792 else
793 count = start;
794
795 while (count > 0 && !dst->states[count - 1])
796 count--;
797
798 dst->count = count;
799 }
800 }
801
802 static void
803 ilo_set_fragment_sampler_views(struct pipe_context *pipe,
804 unsigned num_views,
805 struct pipe_sampler_view **views)
806 {
807 struct ilo_context *ilo = ilo_context(pipe);
808
809 ilo_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, 0, num_views, views);
810
811 if (ilo->view[PIPE_SHADER_FRAGMENT].count > num_views) {
812 ilo_set_sampler_views(pipe, PIPE_SHADER_FRAGMENT, num_views,
813 ilo->view[PIPE_SHADER_FRAGMENT].count - num_views, NULL);
814 }
815
816 ilo->dirty |= ILO_DIRTY_VIEW_FS;
817 }
818
819 static void
820 ilo_set_vertex_sampler_views(struct pipe_context *pipe,
821 unsigned num_views,
822 struct pipe_sampler_view **views)
823 {
824 struct ilo_context *ilo = ilo_context(pipe);
825
826 ilo_set_sampler_views(pipe, PIPE_SHADER_VERTEX, 0, num_views, views);
827
828 if (ilo->view[PIPE_SHADER_VERTEX].count > num_views) {
829 ilo_set_sampler_views(pipe, PIPE_SHADER_VERTEX, num_views,
830 ilo->view[PIPE_SHADER_VERTEX].count - num_views, NULL);
831 }
832
833 ilo->dirty |= ILO_DIRTY_VIEW_VS;
834 }
835
836 static void
837 ilo_set_geometry_sampler_views(struct pipe_context *pipe,
838 unsigned num_views,
839 struct pipe_sampler_view **views)
840 {
841 struct ilo_context *ilo = ilo_context(pipe);
842
843 ilo_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, 0, num_views, views);
844
845 if (ilo->view[PIPE_SHADER_GEOMETRY].count > num_views) {
846 ilo_set_sampler_views(pipe, PIPE_SHADER_GEOMETRY, num_views,
847 ilo->view[PIPE_SHADER_GEOMETRY].count - num_views, NULL);
848 }
849
850 ilo->dirty |= ILO_DIRTY_VIEW_GS;
851 }
852
853 static void
854 ilo_set_compute_sampler_views(struct pipe_context *pipe,
855 unsigned start_slot, unsigned num_views,
856 struct pipe_sampler_view **views)
857 {
858 struct ilo_context *ilo = ilo_context(pipe);
859
860 ilo_set_sampler_views(pipe, PIPE_SHADER_COMPUTE,
861 start_slot, num_views, views);
862
863 ilo->dirty |= ILO_DIRTY_VIEW_CS;
864 }
865
866 static void
867 ilo_set_shader_resources(struct pipe_context *pipe,
868 unsigned start, unsigned count,
869 struct pipe_surface **surfaces)
870 {
871 struct ilo_context *ilo = ilo_context(pipe);
872 struct ilo_resource_state *dst = &ilo->resource;
873 unsigned i;
874
875 assert(start + count <= Elements(dst->states));
876
877 if (surfaces) {
878 for (i = 0; i < count; i++)
879 pipe_surface_reference(&dst->states[start + i], surfaces[i]);
880 }
881 else {
882 for (i = 0; i < count; i++)
883 pipe_surface_reference(&dst->states[start + i], NULL);
884 }
885
886 if (dst->count <= start + count) {
887 if (surfaces)
888 count += start;
889 else
890 count = start;
891
892 while (count > 0 && !dst->states[count - 1])
893 count--;
894
895 dst->count = count;
896 }
897
898 ilo->dirty |= ILO_DIRTY_RESOURCE;
899 }
900
901 static void
902 ilo_set_vertex_buffers(struct pipe_context *pipe,
903 unsigned start_slot, unsigned num_buffers,
904 const struct pipe_vertex_buffer *buffers)
905 {
906 struct ilo_context *ilo = ilo_context(pipe);
907 unsigned i;
908
909 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
910 if (buffers) {
911 for (i = 0; i < num_buffers; i++)
912 assert(!buffers[i].user_buffer);
913 }
914
915 util_set_vertex_buffers_mask(ilo->vb.states,
916 &ilo->vb.enabled_mask, buffers, start_slot, num_buffers);
917
918 ilo->dirty |= ILO_DIRTY_VB;
919 }
920
921 static void
922 ilo_set_index_buffer(struct pipe_context *pipe,
923 const struct pipe_index_buffer *state)
924 {
925 struct ilo_context *ilo = ilo_context(pipe);
926
927 if (state) {
928 pipe_resource_reference(&ilo->ib.buffer, state->buffer);
929 ilo->ib.user_buffer = state->user_buffer;
930 ilo->ib.offset = state->offset;
931 ilo->ib.index_size = state->index_size;
932 }
933 else {
934 pipe_resource_reference(&ilo->ib.buffer, NULL);
935 ilo->ib.user_buffer = NULL;
936 ilo->ib.offset = 0;
937 ilo->ib.index_size = 0;
938 }
939
940 ilo->dirty |= ILO_DIRTY_IB;
941 }
942
943 static struct pipe_stream_output_target *
944 ilo_create_stream_output_target(struct pipe_context *pipe,
945 struct pipe_resource *res,
946 unsigned buffer_offset,
947 unsigned buffer_size)
948 {
949 struct pipe_stream_output_target *target;
950
951 target = MALLOC_STRUCT(pipe_stream_output_target);
952 assert(target);
953
954 pipe_reference_init(&target->reference, 1);
955 target->buffer = NULL;
956 pipe_resource_reference(&target->buffer, res);
957 target->context = pipe;
958 target->buffer_offset = buffer_offset;
959 target->buffer_size = buffer_size;
960
961 return target;
962 }
963
964 static void
965 ilo_set_stream_output_targets(struct pipe_context *pipe,
966 unsigned num_targets,
967 struct pipe_stream_output_target **targets,
968 unsigned append_bitmask)
969 {
970 struct ilo_context *ilo = ilo_context(pipe);
971 unsigned i;
972
973 if (!targets)
974 num_targets = 0;
975
976 /* util_blitter may set this unnecessarily */
977 if (!ilo->so.count && !num_targets)
978 return;
979
980 for (i = 0; i < num_targets; i++)
981 pipe_so_target_reference(&ilo->so.states[i], targets[i]);
982
983 for (; i < ilo->so.count; i++)
984 pipe_so_target_reference(&ilo->so.states[i], NULL);
985
986 ilo->so.count = num_targets;
987 ilo->so.append_bitmask = append_bitmask;
988
989 ilo->so.enabled = (ilo->so.count > 0);
990
991 ilo->dirty |= ILO_DIRTY_SO;
992 }
993
994 static void
995 ilo_stream_output_target_destroy(struct pipe_context *pipe,
996 struct pipe_stream_output_target *target)
997 {
998 pipe_resource_reference(&target->buffer, NULL);
999 FREE(target);
1000 }
1001
1002 static struct pipe_sampler_view *
1003 ilo_create_sampler_view(struct pipe_context *pipe,
1004 struct pipe_resource *res,
1005 const struct pipe_sampler_view *templ)
1006 {
1007 struct ilo_context *ilo = ilo_context(pipe);
1008 struct ilo_view_cso *view;
1009
1010 view = MALLOC_STRUCT(ilo_view_cso);
1011 assert(view);
1012
1013 view->base = *templ;
1014 pipe_reference_init(&view->base.reference, 1);
1015 view->base.texture = NULL;
1016 pipe_resource_reference(&view->base.texture, res);
1017 view->base.context = pipe;
1018
1019 if (res->target == PIPE_BUFFER) {
1020 const unsigned elem_size = util_format_get_blocksize(templ->format);
1021 const unsigned first_elem = templ->u.buf.first_element;
1022 const unsigned num_elems = templ->u.buf.last_element - first_elem + 1;
1023
1024 ilo_gpe_init_view_surface_for_buffer(ilo->dev, ilo_buffer(res),
1025 first_elem * elem_size, num_elems * elem_size,
1026 elem_size, templ->format, false, false, &view->surface);
1027 }
1028 else {
1029 struct ilo_texture *tex = ilo_texture(res);
1030
1031 /* warn about degraded performance because of a missing binding flag */
1032 if (tex->tiling == INTEL_TILING_NONE &&
1033 !(tex->base.bind & PIPE_BIND_SAMPLER_VIEW)) {
1034 ilo_warn("creating sampler view for a resource "
1035 "not created for sampling\n");
1036 }
1037
1038 ilo_gpe_init_view_surface_for_texture(ilo->dev, tex,
1039 templ->format,
1040 templ->u.tex.first_level,
1041 templ->u.tex.last_level - templ->u.tex.first_level + 1,
1042 templ->u.tex.first_layer,
1043 templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
1044 false, false, &view->surface);
1045 }
1046
1047 return &view->base;
1048 }
1049
1050 static void
1051 ilo_sampler_view_destroy(struct pipe_context *pipe,
1052 struct pipe_sampler_view *view)
1053 {
1054 pipe_resource_reference(&view->texture, NULL);
1055 FREE(view);
1056 }
1057
1058 static struct pipe_surface *
1059 ilo_create_surface(struct pipe_context *pipe,
1060 struct pipe_resource *res,
1061 const struct pipe_surface *templ)
1062 {
1063 struct ilo_context *ilo = ilo_context(pipe);
1064 struct ilo_surface_cso *surf;
1065
1066 surf = MALLOC_STRUCT(ilo_surface_cso);
1067 assert(surf);
1068
1069 surf->base = *templ;
1070 pipe_reference_init(&surf->base.reference, 1);
1071 surf->base.texture = NULL;
1072 pipe_resource_reference(&surf->base.texture, res);
1073
1074 surf->base.context = pipe;
1075 surf->base.width = u_minify(res->width0, templ->u.tex.level);
1076 surf->base.height = u_minify(res->height0, templ->u.tex.level);
1077
1078 surf->is_rt = !util_format_is_depth_or_stencil(templ->format);
1079
1080 if (surf->is_rt) {
1081 /* relax this? */
1082 assert(res->target != PIPE_BUFFER);
1083
1084 /*
1085 * classic i965 sets render_cache_rw for constant buffers and sol
1086 * surfaces but not render buffers. Why?
1087 */
1088 ilo_gpe_init_view_surface_for_texture(ilo->dev, ilo_texture(res),
1089 templ->format, templ->u.tex.level, 1,
1090 templ->u.tex.first_layer,
1091 templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
1092 true, true, &surf->u.rt);
1093 }
1094 else {
1095 assert(res->target != PIPE_BUFFER);
1096
1097 ilo_gpe_init_zs_surface(ilo->dev, ilo_texture(res),
1098 templ->format, templ->u.tex.level,
1099 templ->u.tex.first_layer,
1100 templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
1101 &surf->u.zs);
1102 }
1103
1104 return &surf->base;
1105 }
1106
1107 static void
1108 ilo_surface_destroy(struct pipe_context *pipe,
1109 struct pipe_surface *surface)
1110 {
1111 pipe_resource_reference(&surface->texture, NULL);
1112 FREE(surface);
1113 }
1114
1115 static void *
1116 ilo_create_compute_state(struct pipe_context *pipe,
1117 const struct pipe_compute_state *state)
1118 {
1119 struct ilo_context *ilo = ilo_context(pipe);
1120 struct ilo_shader_state *shader;
1121
1122 shader = ilo_shader_create_cs(ilo->dev, state, ilo);
1123 assert(shader);
1124
1125 ilo_shader_cache_add(ilo->shader_cache, shader);
1126
1127 return shader;
1128 }
1129
1130 static void
1131 ilo_bind_compute_state(struct pipe_context *pipe, void *state)
1132 {
1133 struct ilo_context *ilo = ilo_context(pipe);
1134
1135 ilo->cs = state;
1136
1137 ilo->dirty |= ILO_DIRTY_CS;
1138 }
1139
1140 static void
1141 ilo_delete_compute_state(struct pipe_context *pipe, void *state)
1142 {
1143 struct ilo_context *ilo = ilo_context(pipe);
1144 struct ilo_shader_state *cs = (struct ilo_shader_state *) state;
1145
1146 ilo_shader_cache_remove(ilo->shader_cache, cs);
1147 ilo_shader_destroy(cs);
1148 }
1149
1150 static void
1151 ilo_set_compute_resources(struct pipe_context *pipe,
1152 unsigned start, unsigned count,
1153 struct pipe_surface **surfaces)
1154 {
1155 struct ilo_context *ilo = ilo_context(pipe);
1156 struct ilo_resource_state *dst = &ilo->cs_resource;
1157 unsigned i;
1158
1159 assert(start + count <= Elements(dst->states));
1160
1161 if (surfaces) {
1162 for (i = 0; i < count; i++)
1163 pipe_surface_reference(&dst->states[start + i], surfaces[i]);
1164 }
1165 else {
1166 for (i = 0; i < count; i++)
1167 pipe_surface_reference(&dst->states[start + i], NULL);
1168 }
1169
1170 if (dst->count <= start + count) {
1171 if (surfaces)
1172 count += start;
1173 else
1174 count = start;
1175
1176 while (count > 0 && !dst->states[count - 1])
1177 count--;
1178
1179 dst->count = count;
1180 }
1181
1182 ilo->dirty |= ILO_DIRTY_CS_RESOURCE;
1183 }
1184
1185 static void
1186 ilo_set_global_binding(struct pipe_context *pipe,
1187 unsigned start, unsigned count,
1188 struct pipe_resource **resources,
1189 uint32_t **handles)
1190 {
1191 struct ilo_context *ilo = ilo_context(pipe);
1192 struct ilo_global_binding *dst = &ilo->global_binding;
1193 unsigned i;
1194
1195 assert(start + count <= Elements(dst->resources));
1196
1197 if (resources) {
1198 for (i = 0; i < count; i++)
1199 pipe_resource_reference(&dst->resources[start + i], resources[i]);
1200 }
1201 else {
1202 for (i = 0; i < count; i++)
1203 pipe_resource_reference(&dst->resources[start + i], NULL);
1204 }
1205
1206 if (dst->count <= start + count) {
1207 if (resources)
1208 count += start;
1209 else
1210 count = start;
1211
1212 while (count > 0 && !dst->resources[count - 1])
1213 count--;
1214
1215 dst->count = count;
1216 }
1217
1218 ilo->dirty |= ILO_DIRTY_GLOBAL_BINDING;
1219 }
1220
1221 /**
1222 * Initialize state-related functions.
1223 */
1224 void
1225 ilo_init_state_functions(struct ilo_context *ilo)
1226 {
1227 STATIC_ASSERT(ILO_STATE_COUNT <= 32);
1228
1229 ilo->base.create_blend_state = ilo_create_blend_state;
1230 ilo->base.bind_blend_state = ilo_bind_blend_state;
1231 ilo->base.delete_blend_state = ilo_delete_blend_state;
1232 ilo->base.create_sampler_state = ilo_create_sampler_state;
1233 ilo->base.bind_fragment_sampler_states = ilo_bind_fragment_sampler_states;
1234 ilo->base.bind_vertex_sampler_states = ilo_bind_vertex_sampler_states;
1235 ilo->base.bind_geometry_sampler_states = ilo_bind_geometry_sampler_states;
1236 ilo->base.bind_compute_sampler_states = ilo_bind_compute_sampler_states;
1237 ilo->base.delete_sampler_state = ilo_delete_sampler_state;
1238 ilo->base.create_rasterizer_state = ilo_create_rasterizer_state;
1239 ilo->base.bind_rasterizer_state = ilo_bind_rasterizer_state;
1240 ilo->base.delete_rasterizer_state = ilo_delete_rasterizer_state;
1241 ilo->base.create_depth_stencil_alpha_state = ilo_create_depth_stencil_alpha_state;
1242 ilo->base.bind_depth_stencil_alpha_state = ilo_bind_depth_stencil_alpha_state;
1243 ilo->base.delete_depth_stencil_alpha_state = ilo_delete_depth_stencil_alpha_state;
1244 ilo->base.create_fs_state = ilo_create_fs_state;
1245 ilo->base.bind_fs_state = ilo_bind_fs_state;
1246 ilo->base.delete_fs_state = ilo_delete_fs_state;
1247 ilo->base.create_vs_state = ilo_create_vs_state;
1248 ilo->base.bind_vs_state = ilo_bind_vs_state;
1249 ilo->base.delete_vs_state = ilo_delete_vs_state;
1250 ilo->base.create_gs_state = ilo_create_gs_state;
1251 ilo->base.bind_gs_state = ilo_bind_gs_state;
1252 ilo->base.delete_gs_state = ilo_delete_gs_state;
1253 ilo->base.create_vertex_elements_state = ilo_create_vertex_elements_state;
1254 ilo->base.bind_vertex_elements_state = ilo_bind_vertex_elements_state;
1255 ilo->base.delete_vertex_elements_state = ilo_delete_vertex_elements_state;
1256
1257 ilo->base.set_blend_color = ilo_set_blend_color;
1258 ilo->base.set_stencil_ref = ilo_set_stencil_ref;
1259 ilo->base.set_sample_mask = ilo_set_sample_mask;
1260 ilo->base.set_clip_state = ilo_set_clip_state;
1261 ilo->base.set_constant_buffer = ilo_set_constant_buffer;
1262 ilo->base.set_framebuffer_state = ilo_set_framebuffer_state;
1263 ilo->base.set_polygon_stipple = ilo_set_polygon_stipple;
1264 ilo->base.set_scissor_states = ilo_set_scissor_states;
1265 ilo->base.set_viewport_states = ilo_set_viewport_states;
1266 ilo->base.set_fragment_sampler_views = ilo_set_fragment_sampler_views;
1267 ilo->base.set_vertex_sampler_views = ilo_set_vertex_sampler_views;
1268 ilo->base.set_geometry_sampler_views = ilo_set_geometry_sampler_views;
1269 ilo->base.set_compute_sampler_views = ilo_set_compute_sampler_views;
1270 ilo->base.set_shader_resources = ilo_set_shader_resources;
1271 ilo->base.set_vertex_buffers = ilo_set_vertex_buffers;
1272 ilo->base.set_index_buffer = ilo_set_index_buffer;
1273
1274 ilo->base.create_stream_output_target = ilo_create_stream_output_target;
1275 ilo->base.stream_output_target_destroy = ilo_stream_output_target_destroy;
1276 ilo->base.set_stream_output_targets = ilo_set_stream_output_targets;
1277
1278 ilo->base.create_sampler_view = ilo_create_sampler_view;
1279 ilo->base.sampler_view_destroy = ilo_sampler_view_destroy;
1280
1281 ilo->base.create_surface = ilo_create_surface;
1282 ilo->base.surface_destroy = ilo_surface_destroy;
1283
1284 ilo->base.create_compute_state = ilo_create_compute_state;
1285 ilo->base.bind_compute_state = ilo_bind_compute_state;
1286 ilo->base.delete_compute_state = ilo_delete_compute_state;
1287 ilo->base.set_compute_resources = ilo_set_compute_resources;
1288 ilo->base.set_global_binding = ilo_set_global_binding;
1289 }
1290
1291 void
1292 ilo_init_states(struct ilo_context *ilo)
1293 {
1294 ilo_gpe_set_scissor_null(ilo->dev, &ilo->scissor);
1295
1296 ilo_gpe_init_zs_surface(ilo->dev, NULL,
1297 PIPE_FORMAT_NONE, 0, 0, 1, &ilo->fb.null_zs);
1298
1299 ilo->dirty = ILO_DIRTY_ALL;
1300 }
1301
1302 void
1303 ilo_cleanup_states(struct ilo_context *ilo)
1304 {
1305 unsigned i, sh;
1306
1307 for (i = 0; i < Elements(ilo->vb.states); i++) {
1308 if (ilo->vb.enabled_mask & (1 << i))
1309 pipe_resource_reference(&ilo->vb.states[i].buffer, NULL);
1310 }
1311
1312 pipe_resource_reference(&ilo->ib.buffer, NULL);
1313 pipe_resource_reference(&ilo->ib.hw_resource, NULL);
1314
1315 for (i = 0; i < ilo->so.count; i++)
1316 pipe_so_target_reference(&ilo->so.states[i], NULL);
1317
1318 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
1319 for (i = 0; i < ilo->view[sh].count; i++) {
1320 struct pipe_sampler_view *view = ilo->view[sh].states[i];
1321 pipe_sampler_view_reference(&view, NULL);
1322 }
1323
1324 for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
1325 struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
1326 pipe_resource_reference(&cbuf->resource, NULL);
1327 }
1328 }
1329
1330 for (i = 0; i < ilo->resource.count; i++)
1331 pipe_surface_reference(&ilo->resource.states[i], NULL);
1332
1333 for (i = 0; i < ilo->fb.state.nr_cbufs; i++)
1334 pipe_surface_reference(&ilo->fb.state.cbufs[i], NULL);
1335
1336 if (ilo->fb.state.zsbuf)
1337 pipe_surface_reference(&ilo->fb.state.zsbuf, NULL);
1338
1339 for (i = 0; i < ilo->cs_resource.count; i++)
1340 pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
1341
1342 for (i = 0; i < ilo->global_binding.count; i++)
1343 pipe_resource_reference(&ilo->global_binding.resources[i], NULL);
1344 }
1345
1346 /**
1347 * Mark all states that have the resource dirty.
1348 */
1349 void
1350 ilo_mark_states_with_resource_dirty(struct ilo_context *ilo,
1351 const struct pipe_resource *res)
1352 {
1353 uint32_t states = 0;
1354 unsigned sh, i;
1355
1356 if (res->target == PIPE_BUFFER) {
1357 uint32_t vb_mask = ilo->vb.enabled_mask;
1358
1359 while (vb_mask) {
1360 const unsigned idx = u_bit_scan(&vb_mask);
1361
1362 if (ilo->vb.states[idx].buffer == res) {
1363 states |= ILO_DIRTY_VB;
1364 break;
1365 }
1366 }
1367
1368 if (ilo->ib.buffer == res) {
1369 states |= ILO_DIRTY_IB;
1370
1371 /*
1372 * finalize_index_buffer() has an optimization that clears
1373 * ILO_DIRTY_IB when the HW states do not change. However, it fails
1374 * to flush the VF cache when the HW states do not change, but the
1375 * contents of the IB has changed. Here, we set the index size to an
1376 * invalid value to avoid the optimization.
1377 */
1378 ilo->ib.hw_index_size = 0;
1379 }
1380
1381 for (i = 0; i < ilo->so.count; i++) {
1382 if (ilo->so.states[i]->buffer == res) {
1383 states |= ILO_DIRTY_SO;
1384 break;
1385 }
1386 }
1387 }
1388
1389 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
1390 for (i = 0; i < ilo->view[sh].count; i++) {
1391 struct pipe_sampler_view *view = ilo->view[sh].states[i];
1392
1393 if (view->texture == res) {
1394 static const unsigned view_dirty_bits[PIPE_SHADER_TYPES] = {
1395 [PIPE_SHADER_VERTEX] = ILO_DIRTY_VIEW_VS,
1396 [PIPE_SHADER_FRAGMENT] = ILO_DIRTY_VIEW_FS,
1397 [PIPE_SHADER_GEOMETRY] = ILO_DIRTY_VIEW_GS,
1398 [PIPE_SHADER_COMPUTE] = ILO_DIRTY_VIEW_CS,
1399 };
1400
1401 states |= view_dirty_bits[sh];
1402 break;
1403 }
1404 }
1405
1406 if (res->target == PIPE_BUFFER) {
1407 for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
1408 struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
1409
1410 if (cbuf->resource == res) {
1411 states |= ILO_DIRTY_CBUF;
1412 break;
1413 }
1414 }
1415 }
1416 }
1417
1418 for (i = 0; i < ilo->resource.count; i++) {
1419 if (ilo->resource.states[i]->texture == res) {
1420 states |= ILO_DIRTY_RESOURCE;
1421 break;
1422 }
1423 }
1424
1425 /* for now? */
1426 if (res->target != PIPE_BUFFER) {
1427 for (i = 0; i < ilo->fb.state.nr_cbufs; i++) {
1428 if (ilo->fb.state.cbufs[i]->texture == res) {
1429 states |= ILO_DIRTY_FB;
1430 break;
1431 }
1432 }
1433
1434 if (ilo->fb.state.zsbuf && ilo->fb.state.zsbuf->texture == res)
1435 states |= ILO_DIRTY_FB;
1436 }
1437
1438 for (i = 0; i < ilo->cs_resource.count; i++) {
1439 pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
1440 if (ilo->cs_resource.states[i]->texture == res) {
1441 states |= ILO_DIRTY_CS_RESOURCE;
1442 break;
1443 }
1444 }
1445
1446 for (i = 0; i < ilo->global_binding.count; i++) {
1447 if (ilo->global_binding.resources[i] == res) {
1448 states |= ILO_DIRTY_GLOBAL_BINDING;
1449 break;
1450 }
1451 }
1452
1453 ilo->dirty |= states;
1454 }