ilo: mapping a resource may make some states dirty
[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
31 #include "ilo_context.h"
32 #include "ilo_resource.h"
33 #include "ilo_shader.h"
34 #include "ilo_state.h"
35
36 /*
37 * We simply remember the pipe states here and derive HW commands/states from
38 * them later. We could do better by deriving (some of the) HW
39 * commands/states directly.
40 */
41
42 static void
43 finalize_shader_states(struct ilo_context *ilo)
44 {
45 /* this table is ugly and is a burden to maintain.. */
46 const struct {
47 struct ilo_shader_state *state;
48 struct ilo_shader *prev_shader;
49 uint32_t prev_cache_seqno;
50 uint32_t dirty;
51 uint32_t deps;
52 } sh[PIPE_SHADER_TYPES] = {
53 [PIPE_SHADER_VERTEX] = {
54 .state = ilo->vs,
55 .prev_shader = (ilo->vs) ? ilo->vs->shader : NULL,
56 .prev_cache_seqno = (ilo->vs) ? ilo->vs->shader->cache_seqno : 0,
57 .dirty = ILO_DIRTY_VS,
58 .deps = ILO_DIRTY_VERTEX_SAMPLER_VIEWS |
59 ILO_DIRTY_RASTERIZER,
60 },
61 [PIPE_SHADER_FRAGMENT] = {
62 .state = ilo->fs,
63 .prev_shader = (ilo->fs) ? ilo->fs->shader : NULL,
64 .prev_cache_seqno = (ilo->fs) ? ilo->fs->shader->cache_seqno : 0,
65 .dirty = ILO_DIRTY_FS,
66 .deps = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS |
67 ILO_DIRTY_RASTERIZER |
68 ILO_DIRTY_FRAMEBUFFER,
69 },
70 [PIPE_SHADER_GEOMETRY] = {
71 .state = ilo->gs,
72 .prev_shader = (ilo->gs) ? ilo->gs->shader : NULL,
73 .prev_cache_seqno = (ilo->gs) ? ilo->gs->shader->cache_seqno : 0,
74 .dirty = ILO_DIRTY_GS,
75 .deps = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS |
76 ILO_DIRTY_VS |
77 ILO_DIRTY_RASTERIZER,
78 },
79 [PIPE_SHADER_COMPUTE] = {
80 .state = NULL,
81 .prev_shader = NULL,
82 .prev_cache_seqno = 0,
83 .dirty = 0,
84 .deps = 0,
85 },
86 };
87 struct ilo_shader *shaders[PIPE_SHADER_TYPES];
88 int num_shaders = 0, i;
89
90 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
91 /* no state bound */
92 if (!sh[i].state)
93 continue;
94
95 /* switch variant if the shader or the states it depends on changed */
96 if (ilo->dirty & (sh[i].dirty | sh[i].deps)) {
97 struct ilo_shader_variant variant;
98
99 ilo_shader_variant_init(&variant, &sh[i].state->info, ilo);
100 ilo_shader_state_use_variant(sh[i].state, &variant);
101 }
102
103 shaders[num_shaders++] = sh[i].state->shader;
104 }
105
106 ilo_shader_cache_set(ilo->shader_cache, shaders, num_shaders);
107
108 for (i = 0; i < PIPE_SHADER_TYPES; i++) {
109 /* no state bound */
110 if (!sh[i].state)
111 continue;
112
113 /*
114 * mark the shader state dirty if
115 *
116 * - a new variant is selected, or
117 * - the kernel is uploaded to a different bo
118 */
119 if (sh[i].state->shader != sh[i].prev_shader ||
120 sh[i].state->shader->cache_seqno != sh[i].prev_cache_seqno)
121 ilo->dirty |= sh[i].dirty;
122 }
123 }
124
125 static void
126 finalize_constant_buffers(struct ilo_context *ilo)
127 {
128 int sh;
129
130 if (!(ilo->dirty & ILO_DIRTY_CONSTANT_BUFFER))
131 return;
132
133 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
134 int last_cbuf = Elements(ilo->cbuf[sh].cso) - 1;
135
136 /* find the last cbuf */
137 while (last_cbuf >= 0 &&
138 !ilo->cbuf[sh].cso[last_cbuf].resource)
139 last_cbuf--;
140
141 ilo->cbuf[sh].count = last_cbuf + 1;
142 }
143 }
144
145 /**
146 * Finalize states. Some states depend on other states and are
147 * incomplete/invalid until finalized.
148 */
149 void
150 ilo_finalize_states(struct ilo_context *ilo)
151 {
152 finalize_shader_states(ilo);
153 finalize_constant_buffers(ilo);
154 }
155
156 static void *
157 ilo_create_blend_state(struct pipe_context *pipe,
158 const struct pipe_blend_state *state)
159 {
160 struct ilo_context *ilo = ilo_context(pipe);
161 struct ilo_blend_state *blend;
162
163 blend = MALLOC_STRUCT(ilo_blend_state);
164 assert(blend);
165
166 ilo_gpe_init_blend(ilo->dev, state, blend);
167
168 return blend;
169 }
170
171 static void
172 ilo_bind_blend_state(struct pipe_context *pipe, void *state)
173 {
174 struct ilo_context *ilo = ilo_context(pipe);
175
176 ilo->blend = state;
177
178 ilo->dirty |= ILO_DIRTY_BLEND;
179 }
180
181 static void
182 ilo_delete_blend_state(struct pipe_context *pipe, void *state)
183 {
184 FREE(state);
185 }
186
187 static void *
188 ilo_create_sampler_state(struct pipe_context *pipe,
189 const struct pipe_sampler_state *state)
190 {
191 struct ilo_context *ilo = ilo_context(pipe);
192 struct ilo_sampler_cso *sampler;
193
194 sampler = MALLOC_STRUCT(ilo_sampler_cso);
195 assert(sampler);
196
197 ilo_gpe_init_sampler_cso(ilo->dev, state, sampler);
198
199 return sampler;
200 }
201
202 static void
203 bind_samplers(struct ilo_context *ilo,
204 unsigned shader, unsigned start, unsigned count,
205 void **samplers, bool unbind_old)
206 {
207 const struct ilo_sampler_cso **dst = ilo->sampler[shader].cso;
208 unsigned i;
209
210 assert(start + count <= Elements(ilo->sampler[shader].cso));
211
212 if (unbind_old) {
213 if (!samplers) {
214 start = 0;
215 count = 0;
216 }
217
218 for (i = 0; i < start; i++)
219 dst[i] = NULL;
220 for (; i < start + count; i++)
221 dst[i] = samplers[i - start];
222 for (; i < ilo->sampler[shader].count; i++)
223 dst[i] = NULL;
224
225 ilo->sampler[shader].count = start + count;
226
227 return;
228 }
229
230 dst += start;
231 if (samplers) {
232 for (i = 0; i < count; i++)
233 dst[i] = samplers[i];
234 }
235 else {
236 for (i = 0; i < count; i++)
237 dst[i] = NULL;
238 }
239
240 if (ilo->sampler[shader].count <= start + count) {
241 count += start;
242
243 while (count > 0 && !ilo->sampler[shader].cso[count - 1])
244 count--;
245
246 ilo->sampler[shader].count = count;
247 }
248 }
249
250 static void
251 ilo_bind_fragment_sampler_states(struct pipe_context *pipe,
252 unsigned num_samplers,
253 void **samplers)
254 {
255 struct ilo_context *ilo = ilo_context(pipe);
256
257 bind_samplers(ilo, PIPE_SHADER_FRAGMENT, 0, num_samplers, samplers, true);
258 ilo->dirty |= ILO_DIRTY_FRAGMENT_SAMPLERS;
259 }
260
261 static void
262 ilo_bind_vertex_sampler_states(struct pipe_context *pipe,
263 unsigned num_samplers,
264 void **samplers)
265 {
266 struct ilo_context *ilo = ilo_context(pipe);
267
268 bind_samplers(ilo, PIPE_SHADER_VERTEX, 0, num_samplers, samplers, true);
269 ilo->dirty |= ILO_DIRTY_VERTEX_SAMPLERS;
270 }
271
272 static void
273 ilo_bind_geometry_sampler_states(struct pipe_context *pipe,
274 unsigned num_samplers,
275 void **samplers)
276 {
277 struct ilo_context *ilo = ilo_context(pipe);
278
279 bind_samplers(ilo, PIPE_SHADER_GEOMETRY, 0, num_samplers, samplers, true);
280 ilo->dirty |= ILO_DIRTY_GEOMETRY_SAMPLERS;
281 }
282
283 static void
284 ilo_bind_compute_sampler_states(struct pipe_context *pipe,
285 unsigned start_slot,
286 unsigned num_samplers,
287 void **samplers)
288 {
289 struct ilo_context *ilo = ilo_context(pipe);
290
291 bind_samplers(ilo, PIPE_SHADER_COMPUTE,
292 start_slot, num_samplers, samplers, false);
293 ilo->dirty |= ILO_DIRTY_COMPUTE_SAMPLERS;
294 }
295
296 static void
297 ilo_delete_sampler_state(struct pipe_context *pipe, void *state)
298 {
299 FREE(state);
300 }
301
302 static void *
303 ilo_create_rasterizer_state(struct pipe_context *pipe,
304 const struct pipe_rasterizer_state *state)
305 {
306 struct ilo_context *ilo = ilo_context(pipe);
307 struct ilo_rasterizer_state *rast;
308
309 rast = MALLOC_STRUCT(ilo_rasterizer_state);
310 assert(rast);
311
312 rast->state = *state;
313 ilo_gpe_init_rasterizer(ilo->dev, state, rast);
314
315 return rast;
316 }
317
318 static void
319 ilo_bind_rasterizer_state(struct pipe_context *pipe, void *state)
320 {
321 struct ilo_context *ilo = ilo_context(pipe);
322
323 ilo->rasterizer = state;
324
325 ilo->dirty |= ILO_DIRTY_RASTERIZER;
326 }
327
328 static void
329 ilo_delete_rasterizer_state(struct pipe_context *pipe, void *state)
330 {
331 FREE(state);
332 }
333
334 static void *
335 ilo_create_depth_stencil_alpha_state(struct pipe_context *pipe,
336 const struct pipe_depth_stencil_alpha_state *state)
337 {
338 struct ilo_context *ilo = ilo_context(pipe);
339 struct ilo_dsa_state *dsa;
340
341 dsa = MALLOC_STRUCT(ilo_dsa_state);
342 assert(dsa);
343
344 ilo_gpe_init_dsa(ilo->dev, state, dsa);
345
346 return dsa;
347 }
348
349 static void
350 ilo_bind_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
351 {
352 struct ilo_context *ilo = ilo_context(pipe);
353
354 ilo->dsa = state;
355
356 ilo->dirty |= ILO_DIRTY_DEPTH_STENCIL_ALPHA;
357 }
358
359 static void
360 ilo_delete_depth_stencil_alpha_state(struct pipe_context *pipe, void *state)
361 {
362 FREE(state);
363 }
364
365 static void *
366 ilo_create_fs_state(struct pipe_context *pipe,
367 const struct pipe_shader_state *state)
368 {
369 struct ilo_context *ilo = ilo_context(pipe);
370 return ilo_shader_state_create(ilo, PIPE_SHADER_FRAGMENT, state);
371 }
372
373 static void
374 ilo_bind_fs_state(struct pipe_context *pipe, void *state)
375 {
376 struct ilo_context *ilo = ilo_context(pipe);
377
378 ilo->fs = state;
379
380 ilo->dirty |= ILO_DIRTY_FS;
381 }
382
383 static void
384 ilo_delete_fs_state(struct pipe_context *pipe, void *state)
385 {
386 struct ilo_shader_state *fs = (struct ilo_shader_state *) state;
387 ilo_shader_state_destroy(fs);
388 }
389
390 static void *
391 ilo_create_vs_state(struct pipe_context *pipe,
392 const struct pipe_shader_state *state)
393 {
394 struct ilo_context *ilo = ilo_context(pipe);
395 return ilo_shader_state_create(ilo, PIPE_SHADER_VERTEX, state);
396 }
397
398 static void
399 ilo_bind_vs_state(struct pipe_context *pipe, void *state)
400 {
401 struct ilo_context *ilo = ilo_context(pipe);
402
403 ilo->vs = state;
404
405 ilo->dirty |= ILO_DIRTY_VS;
406 }
407
408 static void
409 ilo_delete_vs_state(struct pipe_context *pipe, void *state)
410 {
411 struct ilo_shader_state *vs = (struct ilo_shader_state *) state;
412 ilo_shader_state_destroy(vs);
413 }
414
415 static void *
416 ilo_create_gs_state(struct pipe_context *pipe,
417 const struct pipe_shader_state *state)
418 {
419 struct ilo_context *ilo = ilo_context(pipe);
420 return ilo_shader_state_create(ilo, PIPE_SHADER_GEOMETRY, state);
421 }
422
423 static void
424 ilo_bind_gs_state(struct pipe_context *pipe, void *state)
425 {
426 struct ilo_context *ilo = ilo_context(pipe);
427
428 ilo->gs = state;
429
430 ilo->dirty |= ILO_DIRTY_GS;
431 }
432
433 static void
434 ilo_delete_gs_state(struct pipe_context *pipe, void *state)
435 {
436 struct ilo_shader_state *gs = (struct ilo_shader_state *) state;
437 ilo_shader_state_destroy(gs);
438 }
439
440 static void *
441 ilo_create_vertex_elements_state(struct pipe_context *pipe,
442 unsigned num_elements,
443 const struct pipe_vertex_element *elements)
444 {
445 struct ilo_context *ilo = ilo_context(pipe);
446 struct ilo_ve_state *ve;
447
448 ve = MALLOC_STRUCT(ilo_ve_state);
449 assert(ve);
450
451 ilo_gpe_init_ve(ilo->dev, num_elements, elements, ve);
452
453 return ve;
454 }
455
456 static void
457 ilo_bind_vertex_elements_state(struct pipe_context *pipe, void *state)
458 {
459 struct ilo_context *ilo = ilo_context(pipe);
460
461 ilo->ve = state;
462
463 ilo->dirty |= ILO_DIRTY_VERTEX_ELEMENTS;
464 }
465
466 static void
467 ilo_delete_vertex_elements_state(struct pipe_context *pipe, void *state)
468 {
469 struct ilo_ve_state *ve = state;
470
471 FREE(ve);
472 }
473
474 static void
475 ilo_set_blend_color(struct pipe_context *pipe,
476 const struct pipe_blend_color *state)
477 {
478 struct ilo_context *ilo = ilo_context(pipe);
479
480 ilo->blend_color = *state;
481
482 ilo->dirty |= ILO_DIRTY_BLEND_COLOR;
483 }
484
485 static void
486 ilo_set_stencil_ref(struct pipe_context *pipe,
487 const struct pipe_stencil_ref *state)
488 {
489 struct ilo_context *ilo = ilo_context(pipe);
490
491 ilo->stencil_ref = *state;
492
493 ilo->dirty |= ILO_DIRTY_STENCIL_REF;
494 }
495
496 static void
497 ilo_set_sample_mask(struct pipe_context *pipe,
498 unsigned sample_mask)
499 {
500 struct ilo_context *ilo = ilo_context(pipe);
501
502 ilo->sample_mask = sample_mask;
503
504 ilo->dirty |= ILO_DIRTY_SAMPLE_MASK;
505 }
506
507 static void
508 ilo_set_clip_state(struct pipe_context *pipe,
509 const struct pipe_clip_state *state)
510 {
511 struct ilo_context *ilo = ilo_context(pipe);
512
513 ilo->clip = *state;
514
515 ilo->dirty |= ILO_DIRTY_CLIP;
516 }
517
518 static void
519 ilo_set_constant_buffer(struct pipe_context *pipe,
520 uint shader, uint index,
521 struct pipe_constant_buffer *buf)
522 {
523 struct ilo_context *ilo = ilo_context(pipe);
524 struct ilo_cbuf_cso *cbuf;
525
526 assert(shader < Elements(ilo->cbuf));
527 assert(index < Elements(ilo->cbuf[shader].cso));
528
529 cbuf = &ilo->cbuf[shader].cso[index];
530
531 if (buf) {
532 const enum pipe_format elem_format = PIPE_FORMAT_R32G32B32A32_FLOAT;
533
534 pipe_resource_reference(&cbuf->resource, buf->buffer);
535
536 ilo_gpe_init_view_surface_for_buffer(ilo->dev, ilo_buffer(buf->buffer),
537 buf->buffer_offset, buf->buffer_size,
538 util_format_get_blocksize(elem_format), elem_format,
539 false, false, &cbuf->surface);
540 }
541 else {
542 pipe_resource_reference(&cbuf->resource, NULL);
543 cbuf->surface.bo = NULL;
544 }
545
546 /* the correct value will be set in ilo_finalize_states() */
547 ilo->cbuf[shader].count = 0;
548
549 ilo->dirty |= ILO_DIRTY_CONSTANT_BUFFER;
550 }
551
552 static void
553 ilo_set_framebuffer_state(struct pipe_context *pipe,
554 const struct pipe_framebuffer_state *state)
555 {
556 struct ilo_context *ilo = ilo_context(pipe);
557
558 util_copy_framebuffer_state(&ilo->fb.state, state);
559
560 if (state->nr_cbufs)
561 ilo->fb.num_samples = state->cbufs[0]->texture->nr_samples;
562 else if (state->zsbuf)
563 ilo->fb.num_samples = state->zsbuf->texture->nr_samples;
564 else
565 ilo->fb.num_samples = 1;
566
567 if (!ilo->fb.num_samples)
568 ilo->fb.num_samples = 1;
569
570 ilo->dirty |= ILO_DIRTY_FRAMEBUFFER;
571 }
572
573 static void
574 ilo_set_polygon_stipple(struct pipe_context *pipe,
575 const struct pipe_poly_stipple *state)
576 {
577 struct ilo_context *ilo = ilo_context(pipe);
578
579 ilo->poly_stipple = *state;
580
581 ilo->dirty |= ILO_DIRTY_POLY_STIPPLE;
582 }
583
584 static void
585 ilo_set_scissor_states(struct pipe_context *pipe,
586 unsigned start_slot,
587 unsigned num_scissors,
588 const struct pipe_scissor_state *scissors)
589 {
590 struct ilo_context *ilo = ilo_context(pipe);
591
592 ilo_gpe_set_scissor(ilo->dev, start_slot, num_scissors,
593 scissors, &ilo->scissor);
594
595 ilo->dirty |= ILO_DIRTY_SCISSOR;
596 }
597
598 static void
599 ilo_set_viewport_states(struct pipe_context *pipe,
600 unsigned start_slot,
601 unsigned num_viewports,
602 const struct pipe_viewport_state *viewports)
603 {
604 struct ilo_context *ilo = ilo_context(pipe);
605
606 if (viewports) {
607 unsigned i;
608
609 for (i = 0; i < num_viewports; i++) {
610 ilo_gpe_set_viewport_cso(ilo->dev, &viewports[i],
611 &ilo->viewport.cso[start_slot + i]);
612 }
613
614 if (ilo->viewport.count < start_slot + num_viewports)
615 ilo->viewport.count = start_slot + num_viewports;
616
617 /* need to save viewport 0 for util_blitter */
618 if (!start_slot && num_viewports)
619 ilo->viewport.viewport0 = viewports[0];
620 }
621 else {
622 if (ilo->viewport.count <= start_slot + num_viewports &&
623 ilo->viewport.count > start_slot)
624 ilo->viewport.count = start_slot;
625 }
626
627 ilo->dirty |= ILO_DIRTY_VIEWPORT;
628 }
629
630 static void
631 set_sampler_views(struct ilo_context *ilo,
632 unsigned shader, unsigned start, unsigned count,
633 struct pipe_sampler_view **views, bool unset_old)
634 {
635 struct pipe_sampler_view **dst = ilo->view[shader].states;
636 unsigned i;
637
638 assert(start + count <= Elements(ilo->view[shader].states));
639
640 if (unset_old) {
641 if (!views) {
642 start = 0;
643 count = 0;
644 }
645
646 for (i = 0; i < start; i++)
647 pipe_sampler_view_reference(&dst[i], NULL);
648 for (; i < start + count; i++)
649 pipe_sampler_view_reference(&dst[i], views[i - start]);
650 for (; i < ilo->view[shader].count; i++)
651 pipe_sampler_view_reference(&dst[i], NULL);
652
653 ilo->view[shader].count = start + count;
654
655 return;
656 }
657
658 dst += start;
659 if (views) {
660 for (i = 0; i < count; i++)
661 pipe_sampler_view_reference(&dst[i], views[i]);
662 }
663 else {
664 for (i = 0; i < count; i++)
665 pipe_sampler_view_reference(&dst[i], NULL);
666 }
667
668 if (ilo->view[shader].count <= start + count) {
669 count += start;
670
671 while (count > 0 && !ilo->view[shader].states[count - 1])
672 count--;
673
674 ilo->view[shader].count = count;
675 }
676 }
677
678 static void
679 ilo_set_fragment_sampler_views(struct pipe_context *pipe,
680 unsigned num_views,
681 struct pipe_sampler_view **views)
682 {
683 struct ilo_context *ilo = ilo_context(pipe);
684
685 set_sampler_views(ilo, PIPE_SHADER_FRAGMENT, 0, num_views, views, true);
686 ilo->dirty |= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS;
687 }
688
689 static void
690 ilo_set_vertex_sampler_views(struct pipe_context *pipe,
691 unsigned num_views,
692 struct pipe_sampler_view **views)
693 {
694 struct ilo_context *ilo = ilo_context(pipe);
695
696 set_sampler_views(ilo, PIPE_SHADER_VERTEX, 0, num_views, views, true);
697 ilo->dirty |= ILO_DIRTY_VERTEX_SAMPLER_VIEWS;
698 }
699
700 static void
701 ilo_set_geometry_sampler_views(struct pipe_context *pipe,
702 unsigned num_views,
703 struct pipe_sampler_view **views)
704 {
705 struct ilo_context *ilo = ilo_context(pipe);
706
707 set_sampler_views(ilo, PIPE_SHADER_GEOMETRY, 0, num_views, views, true);
708 ilo->dirty |= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS;
709 }
710
711 static void
712 ilo_set_compute_sampler_views(struct pipe_context *pipe,
713 unsigned start_slot, unsigned num_views,
714 struct pipe_sampler_view **views)
715 {
716 struct ilo_context *ilo = ilo_context(pipe);
717
718 set_sampler_views(ilo, PIPE_SHADER_COMPUTE,
719 start_slot, num_views, views, false);
720
721 ilo->dirty |= ILO_DIRTY_COMPUTE_SAMPLER_VIEWS;
722 }
723
724 static void
725 ilo_set_shader_resources(struct pipe_context *pipe,
726 unsigned start, unsigned count,
727 struct pipe_surface **surfaces)
728 {
729 struct ilo_context *ilo = ilo_context(pipe);
730 struct pipe_surface **dst = ilo->resource.states;
731 unsigned i;
732
733 assert(start + count <= Elements(ilo->resource.states));
734
735 dst += start;
736 if (surfaces) {
737 for (i = 0; i < count; i++)
738 pipe_surface_reference(&dst[i], surfaces[i]);
739 }
740 else {
741 for (i = 0; i < count; i++)
742 pipe_surface_reference(&dst[i], NULL);
743 }
744
745 if (ilo->resource.count <= start + count) {
746 count += start;
747
748 while (count > 0 && !ilo->resource.states[count - 1])
749 count--;
750
751 ilo->resource.count = count;
752 }
753
754 ilo->dirty |= ILO_DIRTY_SHADER_RESOURCES;
755 }
756
757 static void
758 ilo_set_vertex_buffers(struct pipe_context *pipe,
759 unsigned start_slot, unsigned num_buffers,
760 const struct pipe_vertex_buffer *buffers)
761 {
762 struct ilo_context *ilo = ilo_context(pipe);
763
764 util_set_vertex_buffers_mask(ilo->vb.states,
765 &ilo->vb.enabled_mask, buffers, start_slot, num_buffers);
766
767 ilo->dirty |= ILO_DIRTY_VERTEX_BUFFERS;
768 }
769
770 static void
771 ilo_set_index_buffer(struct pipe_context *pipe,
772 const struct pipe_index_buffer *state)
773 {
774 struct ilo_context *ilo = ilo_context(pipe);
775
776 if (state) {
777 ilo->ib.state.index_size = state->index_size;
778 ilo->ib.state.offset = state->offset;
779 pipe_resource_reference(&ilo->ib.state.buffer, state->buffer);
780 ilo->ib.state.user_buffer = state->user_buffer;
781 }
782 else {
783 ilo->ib.state.index_size = 0;
784 ilo->ib.state.offset = 0;
785 pipe_resource_reference(&ilo->ib.state.buffer, NULL);
786 ilo->ib.state.user_buffer = NULL;
787 }
788
789 ilo->dirty |= ILO_DIRTY_INDEX_BUFFER;
790 }
791
792 static struct pipe_stream_output_target *
793 ilo_create_stream_output_target(struct pipe_context *pipe,
794 struct pipe_resource *res,
795 unsigned buffer_offset,
796 unsigned buffer_size)
797 {
798 struct pipe_stream_output_target *target;
799
800 target = MALLOC_STRUCT(pipe_stream_output_target);
801 assert(target);
802
803 pipe_reference_init(&target->reference, 1);
804 target->buffer = NULL;
805 pipe_resource_reference(&target->buffer, res);
806 target->context = pipe;
807 target->buffer_offset = buffer_offset;
808 target->buffer_size = buffer_size;
809
810 return target;
811 }
812
813 static void
814 ilo_set_stream_output_targets(struct pipe_context *pipe,
815 unsigned num_targets,
816 struct pipe_stream_output_target **targets,
817 unsigned append_bitmask)
818 {
819 struct ilo_context *ilo = ilo_context(pipe);
820 unsigned i;
821
822 if (!targets)
823 num_targets = 0;
824
825 for (i = 0; i < num_targets; i++)
826 pipe_so_target_reference(&ilo->so.states[i], targets[i]);
827
828 for (; i < ilo->so.count; i++)
829 pipe_so_target_reference(&ilo->so.states[i], NULL);
830
831 ilo->so.count = num_targets;
832 ilo->so.append_bitmask = append_bitmask;
833
834 ilo->so.enabled = (ilo->so.count > 0);
835
836 ilo->dirty |= ILO_DIRTY_STREAM_OUTPUT_TARGETS;
837 }
838
839 static void
840 ilo_stream_output_target_destroy(struct pipe_context *pipe,
841 struct pipe_stream_output_target *target)
842 {
843 pipe_resource_reference(&target->buffer, NULL);
844 FREE(target);
845 }
846
847 static struct pipe_sampler_view *
848 ilo_create_sampler_view(struct pipe_context *pipe,
849 struct pipe_resource *res,
850 const struct pipe_sampler_view *templ)
851 {
852 struct ilo_context *ilo = ilo_context(pipe);
853 struct ilo_view_cso *view;
854
855 view = MALLOC_STRUCT(ilo_view_cso);
856 assert(view);
857
858 view->base = *templ;
859 pipe_reference_init(&view->base.reference, 1);
860 view->base.texture = NULL;
861 pipe_resource_reference(&view->base.texture, res);
862 view->base.context = pipe;
863
864 if (res->target == PIPE_BUFFER) {
865 const unsigned elem_size = util_format_get_blocksize(templ->format);
866 const unsigned first_elem = templ->u.buf.first_element;
867 const unsigned num_elems = templ->u.buf.last_element - first_elem + 1;
868
869 ilo_gpe_init_view_surface_for_buffer(ilo->dev, ilo_buffer(res),
870 first_elem * elem_size, num_elems * elem_size,
871 elem_size, templ->format, false, false, &view->surface);
872 }
873 else {
874 ilo_gpe_init_view_surface_for_texture(ilo->dev, ilo_texture(res),
875 templ->format,
876 templ->u.tex.first_level,
877 templ->u.tex.last_level - templ->u.tex.first_level + 1,
878 templ->u.tex.first_layer,
879 templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
880 false, false, &view->surface);
881 }
882
883 return &view->base;
884 }
885
886 static void
887 ilo_sampler_view_destroy(struct pipe_context *pipe,
888 struct pipe_sampler_view *view)
889 {
890 pipe_resource_reference(&view->texture, NULL);
891 FREE(view);
892 }
893
894 static struct pipe_surface *
895 ilo_create_surface(struct pipe_context *pipe,
896 struct pipe_resource *res,
897 const struct pipe_surface *templ)
898 {
899 struct ilo_context *ilo = ilo_context(pipe);
900 struct ilo_surface_cso *surf;
901
902 surf = MALLOC_STRUCT(ilo_surface_cso);
903 assert(surf);
904
905 surf->base = *templ;
906 pipe_reference_init(&surf->base.reference, 1);
907 surf->base.texture = NULL;
908 pipe_resource_reference(&surf->base.texture, res);
909
910 surf->base.context = pipe;
911 surf->base.width = u_minify(res->width0, templ->u.tex.level);
912 surf->base.height = u_minify(res->height0, templ->u.tex.level);
913
914 surf->is_rt = !util_format_is_depth_or_stencil(templ->format);
915
916 if (surf->is_rt) {
917 /* relax this? */
918 assert(res->target != PIPE_BUFFER);
919
920 /*
921 * classic i965 sets render_cache_rw for constant buffers and sol
922 * surfaces but not render buffers. Why?
923 */
924 ilo_gpe_init_view_surface_for_texture(ilo->dev, ilo_texture(res),
925 templ->format, templ->u.tex.level, 1,
926 templ->u.tex.first_layer,
927 templ->u.tex.last_layer - templ->u.tex.first_layer + 1,
928 true, true, &surf->u.rt);
929 }
930 else {
931 assert(res->target != PIPE_BUFFER);
932
933 /* will construct dynamically */
934 }
935
936 return &surf->base;
937 }
938
939 static void
940 ilo_surface_destroy(struct pipe_context *pipe,
941 struct pipe_surface *surface)
942 {
943 pipe_resource_reference(&surface->texture, NULL);
944 FREE(surface);
945 }
946
947 static void *
948 ilo_create_compute_state(struct pipe_context *pipe,
949 const struct pipe_compute_state *state)
950 {
951 struct ilo_context *ilo = ilo_context(pipe);
952 return ilo_shader_state_create(ilo, PIPE_SHADER_COMPUTE, state);
953 }
954
955 static void
956 ilo_bind_compute_state(struct pipe_context *pipe, void *state)
957 {
958 struct ilo_context *ilo = ilo_context(pipe);
959
960 ilo->cs = state;
961
962 ilo->dirty |= ILO_DIRTY_COMPUTE;
963 }
964
965 static void
966 ilo_delete_compute_state(struct pipe_context *pipe, void *state)
967 {
968 struct ilo_shader_state *cs = (struct ilo_shader_state *) state;
969 ilo_shader_state_destroy(cs);
970 }
971
972 static void
973 ilo_set_compute_resources(struct pipe_context *pipe,
974 unsigned start, unsigned count,
975 struct pipe_surface **surfaces)
976 {
977 struct ilo_context *ilo = ilo_context(pipe);
978 struct pipe_surface **dst = ilo->cs_resource.states;
979 unsigned i;
980
981 assert(start + count <= Elements(ilo->cs_resource.states));
982
983 dst += start;
984 if (surfaces) {
985 for (i = 0; i < count; i++)
986 pipe_surface_reference(&dst[i], surfaces[i]);
987 }
988 else {
989 for (i = 0; i < count; i++)
990 pipe_surface_reference(&dst[i], NULL);
991 }
992
993 if (ilo->cs_resource.count <= start + count) {
994 count += start;
995
996 while (count > 0 && !ilo->cs_resource.states[count - 1])
997 count--;
998
999 ilo->cs_resource.count = count;
1000 }
1001
1002 ilo->dirty |= ILO_DIRTY_COMPUTE_RESOURCES;
1003 }
1004
1005 static void
1006 ilo_set_global_binding(struct pipe_context *pipe,
1007 unsigned start, unsigned count,
1008 struct pipe_resource **resources,
1009 uint32_t **handles)
1010 {
1011 struct ilo_context *ilo = ilo_context(pipe);
1012 struct pipe_resource **dst = ilo->global_binding.resources;
1013 unsigned i;
1014
1015 assert(start + count <= Elements(ilo->global_binding.resources));
1016
1017 dst += start;
1018 if (resources) {
1019 for (i = 0; i < count; i++)
1020 pipe_resource_reference(&dst[i], resources[i]);
1021 }
1022 else {
1023 for (i = 0; i < count; i++)
1024 pipe_resource_reference(&dst[i], NULL);
1025 }
1026
1027 if (ilo->global_binding.count <= start + count) {
1028 count += start;
1029
1030 while (count > 0 && !ilo->global_binding.resources[count - 1])
1031 count--;
1032
1033 ilo->global_binding.count = count;
1034 }
1035
1036 ilo->dirty |= ILO_DIRTY_GLOBAL_BINDING;
1037 }
1038
1039 /**
1040 * Initialize state-related functions.
1041 */
1042 void
1043 ilo_init_state_functions(struct ilo_context *ilo)
1044 {
1045 STATIC_ASSERT(ILO_STATE_COUNT <= 32);
1046
1047 ilo->base.create_blend_state = ilo_create_blend_state;
1048 ilo->base.bind_blend_state = ilo_bind_blend_state;
1049 ilo->base.delete_blend_state = ilo_delete_blend_state;
1050 ilo->base.create_sampler_state = ilo_create_sampler_state;
1051 ilo->base.bind_fragment_sampler_states = ilo_bind_fragment_sampler_states;
1052 ilo->base.bind_vertex_sampler_states = ilo_bind_vertex_sampler_states;
1053 ilo->base.bind_geometry_sampler_states = ilo_bind_geometry_sampler_states;
1054 ilo->base.bind_compute_sampler_states = ilo_bind_compute_sampler_states;
1055 ilo->base.delete_sampler_state = ilo_delete_sampler_state;
1056 ilo->base.create_rasterizer_state = ilo_create_rasterizer_state;
1057 ilo->base.bind_rasterizer_state = ilo_bind_rasterizer_state;
1058 ilo->base.delete_rasterizer_state = ilo_delete_rasterizer_state;
1059 ilo->base.create_depth_stencil_alpha_state = ilo_create_depth_stencil_alpha_state;
1060 ilo->base.bind_depth_stencil_alpha_state = ilo_bind_depth_stencil_alpha_state;
1061 ilo->base.delete_depth_stencil_alpha_state = ilo_delete_depth_stencil_alpha_state;
1062 ilo->base.create_fs_state = ilo_create_fs_state;
1063 ilo->base.bind_fs_state = ilo_bind_fs_state;
1064 ilo->base.delete_fs_state = ilo_delete_fs_state;
1065 ilo->base.create_vs_state = ilo_create_vs_state;
1066 ilo->base.bind_vs_state = ilo_bind_vs_state;
1067 ilo->base.delete_vs_state = ilo_delete_vs_state;
1068 ilo->base.create_gs_state = ilo_create_gs_state;
1069 ilo->base.bind_gs_state = ilo_bind_gs_state;
1070 ilo->base.delete_gs_state = ilo_delete_gs_state;
1071 ilo->base.create_vertex_elements_state = ilo_create_vertex_elements_state;
1072 ilo->base.bind_vertex_elements_state = ilo_bind_vertex_elements_state;
1073 ilo->base.delete_vertex_elements_state = ilo_delete_vertex_elements_state;
1074
1075 ilo->base.set_blend_color = ilo_set_blend_color;
1076 ilo->base.set_stencil_ref = ilo_set_stencil_ref;
1077 ilo->base.set_sample_mask = ilo_set_sample_mask;
1078 ilo->base.set_clip_state = ilo_set_clip_state;
1079 ilo->base.set_constant_buffer = ilo_set_constant_buffer;
1080 ilo->base.set_framebuffer_state = ilo_set_framebuffer_state;
1081 ilo->base.set_polygon_stipple = ilo_set_polygon_stipple;
1082 ilo->base.set_scissor_states = ilo_set_scissor_states;
1083 ilo->base.set_viewport_states = ilo_set_viewport_states;
1084 ilo->base.set_fragment_sampler_views = ilo_set_fragment_sampler_views;
1085 ilo->base.set_vertex_sampler_views = ilo_set_vertex_sampler_views;
1086 ilo->base.set_geometry_sampler_views = ilo_set_geometry_sampler_views;
1087 ilo->base.set_compute_sampler_views = ilo_set_compute_sampler_views;
1088 ilo->base.set_shader_resources = ilo_set_shader_resources;
1089 ilo->base.set_vertex_buffers = ilo_set_vertex_buffers;
1090 ilo->base.set_index_buffer = ilo_set_index_buffer;
1091
1092 ilo->base.create_stream_output_target = ilo_create_stream_output_target;
1093 ilo->base.stream_output_target_destroy = ilo_stream_output_target_destroy;
1094 ilo->base.set_stream_output_targets = ilo_set_stream_output_targets;
1095
1096 ilo->base.create_sampler_view = ilo_create_sampler_view;
1097 ilo->base.sampler_view_destroy = ilo_sampler_view_destroy;
1098
1099 ilo->base.create_surface = ilo_create_surface;
1100 ilo->base.surface_destroy = ilo_surface_destroy;
1101
1102 ilo->base.create_compute_state = ilo_create_compute_state;
1103 ilo->base.bind_compute_state = ilo_bind_compute_state;
1104 ilo->base.delete_compute_state = ilo_delete_compute_state;
1105 ilo->base.set_compute_resources = ilo_set_compute_resources;
1106 ilo->base.set_global_binding = ilo_set_global_binding;
1107 }
1108
1109 void
1110 ilo_init_states(struct ilo_context *ilo)
1111 {
1112 ilo_gpe_set_scissor_null(ilo->dev, &ilo->scissor);
1113
1114 ilo->dirty = ILO_DIRTY_ALL;
1115 }
1116
1117 void
1118 ilo_cleanup_states(struct ilo_context *ilo)
1119 {
1120 unsigned i, sh;
1121
1122 for (i = 0; i < Elements(ilo->vb.states); i++) {
1123 if (ilo->vb.enabled_mask & (1 << i))
1124 pipe_resource_reference(&ilo->vb.states[i].buffer, NULL);
1125 }
1126
1127 pipe_resource_reference(&ilo->ib.state.buffer, NULL);
1128
1129 for (i = 0; i < ilo->so.count; i++)
1130 pipe_so_target_reference(&ilo->so.states[i], NULL);
1131
1132 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
1133 for (i = 0; i < ilo->view[sh].count; i++) {
1134 struct pipe_sampler_view *view = ilo->view[sh].states[i];
1135 pipe_sampler_view_reference(&view, NULL);
1136 }
1137
1138 for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
1139 struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
1140 pipe_resource_reference(&cbuf->resource, NULL);
1141 }
1142 }
1143
1144 for (i = 0; i < ilo->resource.count; i++)
1145 pipe_surface_reference(&ilo->resource.states[i], NULL);
1146
1147 for (i = 0; i < ilo->fb.state.nr_cbufs; i++)
1148 pipe_surface_reference(&ilo->fb.state.cbufs[i], NULL);
1149
1150 if (ilo->fb.state.zsbuf)
1151 pipe_surface_reference(&ilo->fb.state.zsbuf, NULL);
1152
1153 for (i = 0; i < ilo->cs_resource.count; i++)
1154 pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
1155
1156 for (i = 0; i < ilo->global_binding.count; i++)
1157 pipe_resource_reference(&ilo->global_binding.resources[i], NULL);
1158 }
1159
1160 /**
1161 * Mark all states that have the resource dirty.
1162 */
1163 void
1164 ilo_mark_states_with_resource_dirty(struct ilo_context *ilo,
1165 const struct pipe_resource *res)
1166 {
1167 uint32_t states = 0;
1168 unsigned sh, i;
1169
1170 if (res->target == PIPE_BUFFER) {
1171 uint32_t vb_mask = ilo->vb.enabled_mask;
1172
1173 while (vb_mask) {
1174 const unsigned idx = u_bit_scan(&vb_mask);
1175
1176 if (ilo->vb.states[idx].buffer == res) {
1177 states |= ILO_DIRTY_VERTEX_BUFFERS;
1178 break;
1179 }
1180 }
1181
1182 if (ilo->ib.state.buffer == res)
1183 states |= ILO_DIRTY_INDEX_BUFFER;
1184
1185 for (i = 0; i < ilo->so.count; i++) {
1186 if (ilo->so.states[i]->buffer == res) {
1187 states |= ILO_DIRTY_STREAM_OUTPUT_TARGETS;
1188 break;
1189 }
1190 }
1191 }
1192
1193 for (sh = 0; sh < PIPE_SHADER_TYPES; sh++) {
1194 for (i = 0; i < ilo->view[sh].count; i++) {
1195 struct pipe_sampler_view *view = ilo->view[sh].states[i];
1196
1197 if (view->texture == res) {
1198 static const unsigned view_dirty_bits[PIPE_SHADER_TYPES] = {
1199 [PIPE_SHADER_VERTEX] = ILO_DIRTY_VERTEX_SAMPLER_VIEWS,
1200 [PIPE_SHADER_FRAGMENT] = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS,
1201 [PIPE_SHADER_GEOMETRY] = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS,
1202 [PIPE_SHADER_COMPUTE] = ILO_DIRTY_COMPUTE_SAMPLER_VIEWS,
1203 };
1204
1205 states |= view_dirty_bits[sh];
1206 break;
1207 }
1208 }
1209
1210 if (res->target == PIPE_BUFFER) {
1211 for (i = 0; i < Elements(ilo->cbuf[sh].cso); i++) {
1212 struct ilo_cbuf_cso *cbuf = &ilo->cbuf[sh].cso[i];
1213
1214 if (cbuf->resource == res) {
1215 states |= ILO_DIRTY_CONSTANT_BUFFER;
1216 break;
1217 }
1218 }
1219 }
1220 }
1221
1222 for (i = 0; i < ilo->resource.count; i++) {
1223 if (ilo->resource.states[i]->texture == res) {
1224 states |= ILO_DIRTY_SHADER_RESOURCES;
1225 break;
1226 }
1227 }
1228
1229 /* for now? */
1230 if (res->target != PIPE_BUFFER) {
1231 for (i = 0; i < ilo->fb.state.nr_cbufs; i++) {
1232 if (ilo->fb.state.cbufs[i]->texture == res) {
1233 states |= ILO_DIRTY_FRAMEBUFFER;
1234 break;
1235 }
1236 }
1237
1238 if (ilo->fb.state.zsbuf && ilo->fb.state.zsbuf->texture == res)
1239 states |= ILO_DIRTY_FRAMEBUFFER;
1240 }
1241
1242 for (i = 0; i < ilo->cs_resource.count; i++) {
1243 pipe_surface_reference(&ilo->cs_resource.states[i], NULL);
1244 if (ilo->cs_resource.states[i]->texture == res) {
1245 states |= ILO_DIRTY_COMPUTE_RESOURCES;
1246 break;
1247 }
1248 }
1249
1250 for (i = 0; i < ilo->global_binding.count; i++) {
1251 if (ilo->global_binding.resources[i] == res) {
1252 states |= ILO_DIRTY_GLOBAL_BINDING;
1253 break;
1254 }
1255 }
1256
1257 ilo->dirty |= states;
1258 }