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