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