2 * Mesa 3-D graphics library
4 * Copyright (C) 2012-2013 LunarG, Inc.
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:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
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.
25 * Chia-I Wu <olv@lunarg.com>
28 #include "util/u_framebuffer.h"
29 #include "util/u_helpers.h"
30 #include "util/u_upload_mgr.h"
32 #include "ilo_context.h"
33 #include "ilo_resource.h"
34 #include "ilo_shader.h"
35 #include "ilo_state.h"
38 finalize_shader_states(struct ilo_context
*ilo
)
42 for (type
= 0; type
< PIPE_SHADER_TYPES
; type
++) {
43 struct ilo_shader_state
*shader
;
47 case PIPE_SHADER_VERTEX
:
51 case PIPE_SHADER_GEOMETRY
:
55 case PIPE_SHADER_FRAGMENT
:
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
);
72 else if (ilo_shader_select_kernel(shader
, ilo
, ilo
->dirty
)) {
73 /* mark the state dirty if a new kernel is selected */
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
))
88 finalize_constant_buffers(struct ilo_context
*ilo
)
92 if (!(ilo
->dirty
& ILO_DIRTY_CBUF
))
95 /* TODO push constants? */
96 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
97 unsigned enabled_mask
= ilo
->cbuf
[sh
].enabled_mask
;
99 while (enabled_mask
) {
100 struct ilo_cbuf_cso
*cbuf
;
103 i
= u_bit_scan(&enabled_mask
);
104 cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
106 /* upload user buffer */
107 if (cbuf
->user_buffer
) {
108 const enum pipe_format elem_format
=
109 PIPE_FORMAT_R32G32B32A32_FLOAT
;
112 u_upload_data(ilo
->uploader
, 0, cbuf
->user_buffer_size
,
113 cbuf
->user_buffer
, &offset
, &cbuf
->resource
);
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
);
121 cbuf
->user_buffer
= NULL
;
122 cbuf
->user_buffer_size
= 0;
129 finalize_index_buffer(struct ilo_context
*ilo
)
131 const struct pipe_resource
*current_hw_res
= ilo
->ib
.hw_resource
;
132 const bool need_upload
= (ilo
->draw
->indexed
&&
133 (ilo
->ib
.user_buffer
|| ilo
->ib
.offset
% ilo
->ib
.index_size
));
135 if (!(ilo
->dirty
& ILO_DIRTY_IB
) && !need_upload
)
139 const unsigned offset
= ilo
->ib
.index_size
* ilo
->draw
->start
;
140 const unsigned size
= ilo
->ib
.index_size
* ilo
->draw
->count
;
143 if (ilo
->ib
.user_buffer
) {
144 u_upload_data(ilo
->uploader
, 0, size
,
145 ilo
->ib
.user_buffer
+ offset
, &hw_offset
, &ilo
->ib
.hw_resource
);
148 u_upload_buffer(ilo
->uploader
, 0, ilo
->ib
.offset
+ offset
, size
,
149 ilo
->ib
.buffer
, &hw_offset
, &ilo
->ib
.hw_resource
);
152 /* the HW offset should be aligned */
153 assert(hw_offset
% ilo
->ib
.index_size
== 0);
154 ilo
->ib
.draw_start_offset
= hw_offset
/ ilo
->ib
.index_size
;
157 * INDEX[ilo->draw->start] in the original buffer is INDEX[0] in the HW
160 ilo
->ib
.draw_start_offset
-= ilo
->draw
->start
;
163 pipe_resource_reference(&ilo
->ib
.hw_resource
, ilo
->ib
.buffer
);
165 /* note that index size may be zero when the draw is not indexed */
166 if (ilo
->draw
->indexed
)
167 ilo
->ib
.draw_start_offset
= ilo
->ib
.offset
/ ilo
->ib
.index_size
;
169 ilo
->ib
.draw_start_offset
= 0;
172 /* treat the IB as clean if the HW states do not change */
173 if (ilo
->ib
.hw_resource
== current_hw_res
&&
174 ilo
->ib
.hw_index_size
== ilo
->ib
.index_size
)
175 ilo
->dirty
&= ~ILO_DIRTY_IB
;
177 ilo
->ib
.hw_index_size
= ilo
->ib
.index_size
;
181 * Finalize states. Some states depend on other states and are
182 * incomplete/invalid until finalized.
185 ilo_finalize_3d_states(struct ilo_context
*ilo
,
186 const struct pipe_draw_info
*draw
)
190 finalize_shader_states(ilo
);
191 finalize_constant_buffers(ilo
);
192 finalize_index_buffer(ilo
);
194 u_upload_unmap(ilo
->uploader
);
198 ilo_create_blend_state(struct pipe_context
*pipe
,
199 const struct pipe_blend_state
*state
)
201 struct ilo_context
*ilo
= ilo_context(pipe
);
202 struct ilo_blend_state
*blend
;
204 blend
= MALLOC_STRUCT(ilo_blend_state
);
207 ilo_gpe_init_blend(ilo
->dev
, state
, blend
);
213 ilo_bind_blend_state(struct pipe_context
*pipe
, void *state
)
215 struct ilo_context
*ilo
= ilo_context(pipe
);
219 ilo
->dirty
|= ILO_DIRTY_BLEND
;
223 ilo_delete_blend_state(struct pipe_context
*pipe
, void *state
)
229 ilo_create_sampler_state(struct pipe_context
*pipe
,
230 const struct pipe_sampler_state
*state
)
232 struct ilo_context
*ilo
= ilo_context(pipe
);
233 struct ilo_sampler_cso
*sampler
;
235 sampler
= MALLOC_STRUCT(ilo_sampler_cso
);
238 ilo_gpe_init_sampler_cso(ilo
->dev
, state
, sampler
);
244 ilo_bind_sampler_states(struct pipe_context
*pipe
, unsigned shader
,
245 unsigned start
, unsigned count
, void **samplers
)
247 struct ilo_context
*ilo
= ilo_context(pipe
);
248 struct ilo_sampler_state
*dst
= &ilo
->sampler
[shader
];
251 assert(start
+ count
<= Elements(dst
->cso
));
253 if (likely(shader
!= PIPE_SHADER_COMPUTE
)) {
259 /* samplers not in range are also unbound */
260 for (i
= 0; i
< start
; i
++)
262 for (; i
< start
+ count
; i
++)
263 dst
->cso
[i
] = samplers
[i
- start
];
264 for (; i
< dst
->count
; i
++)
267 dst
->count
= start
+ count
;
273 for (i
= 0; i
< count
; i
++)
274 dst
->cso
[start
+ i
] = samplers
[i
];
277 for (i
= 0; i
< count
; i
++)
278 dst
->cso
[start
+ i
] = NULL
;
281 if (dst
->count
<= start
+ count
) {
287 while (count
> 0 && !dst
->cso
[count
- 1])
295 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
296 unsigned num_samplers
,
299 struct ilo_context
*ilo
= ilo_context(pipe
);
301 ilo_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
302 0, num_samplers
, samplers
);
304 ilo
->dirty
|= ILO_DIRTY_SAMPLER_FS
;
308 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
309 unsigned num_samplers
,
312 struct ilo_context
*ilo
= ilo_context(pipe
);
314 ilo_bind_sampler_states(pipe
, PIPE_SHADER_VERTEX
,
315 0, num_samplers
, samplers
);
317 ilo
->dirty
|= ILO_DIRTY_SAMPLER_VS
;
321 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
322 unsigned num_samplers
,
325 struct ilo_context
*ilo
= ilo_context(pipe
);
327 ilo_bind_sampler_states(pipe
, PIPE_SHADER_GEOMETRY
,
328 0, num_samplers
, samplers
);
330 ilo
->dirty
|= ILO_DIRTY_SAMPLER_GS
;
334 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
336 unsigned num_samplers
,
339 struct ilo_context
*ilo
= ilo_context(pipe
);
341 ilo_bind_sampler_states(pipe
, PIPE_SHADER_COMPUTE
,
342 start_slot
, num_samplers
, samplers
);
344 ilo
->dirty
|= ILO_DIRTY_SAMPLER_CS
;
348 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
354 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
355 const struct pipe_rasterizer_state
*state
)
357 struct ilo_context
*ilo
= ilo_context(pipe
);
358 struct ilo_rasterizer_state
*rast
;
360 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
363 rast
->state
= *state
;
364 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
370 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
372 struct ilo_context
*ilo
= ilo_context(pipe
);
374 ilo
->rasterizer
= state
;
376 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
380 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
386 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
387 const struct pipe_depth_stencil_alpha_state
*state
)
389 struct ilo_context
*ilo
= ilo_context(pipe
);
390 struct ilo_dsa_state
*dsa
;
392 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
395 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
401 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
403 struct ilo_context
*ilo
= ilo_context(pipe
);
407 ilo
->dirty
|= ILO_DIRTY_DSA
;
411 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
417 ilo_create_fs_state(struct pipe_context
*pipe
,
418 const struct pipe_shader_state
*state
)
420 struct ilo_context
*ilo
= ilo_context(pipe
);
421 struct ilo_shader_state
*shader
;
423 shader
= ilo_shader_create_fs(ilo
->dev
, state
, ilo
);
426 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
432 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
434 struct ilo_context
*ilo
= ilo_context(pipe
);
438 ilo
->dirty
|= ILO_DIRTY_FS
;
442 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
444 struct ilo_context
*ilo
= ilo_context(pipe
);
445 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
447 ilo_shader_cache_remove(ilo
->shader_cache
, fs
);
448 ilo_shader_destroy(fs
);
452 ilo_create_vs_state(struct pipe_context
*pipe
,
453 const struct pipe_shader_state
*state
)
455 struct ilo_context
*ilo
= ilo_context(pipe
);
456 struct ilo_shader_state
*shader
;
458 shader
= ilo_shader_create_vs(ilo
->dev
, state
, ilo
);
461 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
467 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
469 struct ilo_context
*ilo
= ilo_context(pipe
);
473 ilo
->dirty
|= ILO_DIRTY_VS
;
477 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
479 struct ilo_context
*ilo
= ilo_context(pipe
);
480 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
482 ilo_shader_cache_remove(ilo
->shader_cache
, vs
);
483 ilo_shader_destroy(vs
);
487 ilo_create_gs_state(struct pipe_context
*pipe
,
488 const struct pipe_shader_state
*state
)
490 struct ilo_context
*ilo
= ilo_context(pipe
);
491 struct ilo_shader_state
*shader
;
493 shader
= ilo_shader_create_gs(ilo
->dev
, state
, ilo
);
496 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
502 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
504 struct ilo_context
*ilo
= ilo_context(pipe
);
506 /* util_blitter may set this unnecessarily */
507 if (ilo
->gs
== state
)
512 ilo
->dirty
|= ILO_DIRTY_GS
;
516 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
518 struct ilo_context
*ilo
= ilo_context(pipe
);
519 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
521 ilo_shader_cache_remove(ilo
->shader_cache
, gs
);
522 ilo_shader_destroy(gs
);
526 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
527 unsigned num_elements
,
528 const struct pipe_vertex_element
*elements
)
530 struct ilo_context
*ilo
= ilo_context(pipe
);
531 struct ilo_ve_state
*ve
;
533 ve
= MALLOC_STRUCT(ilo_ve_state
);
536 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
542 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
544 struct ilo_context
*ilo
= ilo_context(pipe
);
548 ilo
->dirty
|= ILO_DIRTY_VE
;
552 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
554 struct ilo_ve_state
*ve
= state
;
560 ilo_set_blend_color(struct pipe_context
*pipe
,
561 const struct pipe_blend_color
*state
)
563 struct ilo_context
*ilo
= ilo_context(pipe
);
565 ilo
->blend_color
= *state
;
567 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
571 ilo_set_stencil_ref(struct pipe_context
*pipe
,
572 const struct pipe_stencil_ref
*state
)
574 struct ilo_context
*ilo
= ilo_context(pipe
);
576 /* util_blitter may set this unnecessarily */
577 if (!memcpy(&ilo
->stencil_ref
, state
, sizeof(*state
)))
580 ilo
->stencil_ref
= *state
;
582 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
586 ilo_set_sample_mask(struct pipe_context
*pipe
,
587 unsigned sample_mask
)
589 struct ilo_context
*ilo
= ilo_context(pipe
);
591 /* util_blitter may set this unnecessarily */
592 if (ilo
->sample_mask
== sample_mask
)
595 ilo
->sample_mask
= sample_mask
;
597 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
601 ilo_set_clip_state(struct pipe_context
*pipe
,
602 const struct pipe_clip_state
*state
)
604 struct ilo_context
*ilo
= ilo_context(pipe
);
608 ilo
->dirty
|= ILO_DIRTY_CLIP
;
612 ilo_set_constant_buffer(struct pipe_context
*pipe
,
613 uint shader
, uint index
,
614 struct pipe_constant_buffer
*buf
)
616 struct ilo_context
*ilo
= ilo_context(pipe
);
617 struct ilo_cbuf_state
*cbuf
= &ilo
->cbuf
[shader
];
618 const unsigned count
= 1;
621 assert(shader
< Elements(ilo
->cbuf
));
622 assert(index
+ count
<= Elements(ilo
->cbuf
[shader
].cso
));
625 for (i
= 0; i
< count
; i
++) {
626 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
628 pipe_resource_reference(&cso
->resource
, buf
[i
].buffer
);
631 const enum pipe_format elem_format
=
632 PIPE_FORMAT_R32G32B32A32_FLOAT
;
634 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
635 ilo_buffer(buf
[i
].buffer
),
636 buf
[i
].buffer_offset
, buf
[i
].buffer_size
,
637 util_format_get_blocksize(elem_format
), elem_format
,
638 false, false, &cso
->surface
);
640 cso
->user_buffer
= NULL
;
641 cso
->user_buffer_size
= 0;
643 cbuf
->enabled_mask
|= 1 << (index
+ i
);
645 else if (buf
[i
].user_buffer
) {
646 cso
->surface
.bo
= NULL
;
648 /* buffer_offset does not apply for user buffer */
649 cso
->user_buffer
= buf
[i
].user_buffer
;
650 cso
->user_buffer_size
= buf
[i
].buffer_size
;
652 cbuf
->enabled_mask
|= 1 << (index
+ i
);
655 cso
->surface
.bo
= NULL
;
656 cso
->user_buffer
= NULL
;
657 cso
->user_buffer_size
= 0;
659 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
664 for (i
= 0; i
< count
; i
++) {
665 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
667 pipe_resource_reference(&cso
->resource
, NULL
);
668 cso
->surface
.bo
= NULL
;
669 cso
->user_buffer
= NULL
;
670 cso
->user_buffer_size
= 0;
672 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
676 ilo
->dirty
|= ILO_DIRTY_CBUF
;
680 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
681 const struct pipe_framebuffer_state
*state
)
683 struct ilo_context
*ilo
= ilo_context(pipe
);
685 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
688 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
689 else if (state
->zsbuf
)
690 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
692 ilo
->fb
.num_samples
= 1;
694 if (!ilo
->fb
.num_samples
)
695 ilo
->fb
.num_samples
= 1;
697 ilo
->dirty
|= ILO_DIRTY_FB
;
701 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
702 const struct pipe_poly_stipple
*state
)
704 struct ilo_context
*ilo
= ilo_context(pipe
);
706 ilo
->poly_stipple
= *state
;
708 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
712 ilo_set_scissor_states(struct pipe_context
*pipe
,
714 unsigned num_scissors
,
715 const struct pipe_scissor_state
*scissors
)
717 struct ilo_context
*ilo
= ilo_context(pipe
);
719 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
720 scissors
, &ilo
->scissor
);
722 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
726 ilo_set_viewport_states(struct pipe_context
*pipe
,
728 unsigned num_viewports
,
729 const struct pipe_viewport_state
*viewports
)
731 struct ilo_context
*ilo
= ilo_context(pipe
);
736 for (i
= 0; i
< num_viewports
; i
++) {
737 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
738 &ilo
->viewport
.cso
[start_slot
+ i
]);
741 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
742 ilo
->viewport
.count
= start_slot
+ num_viewports
;
744 /* need to save viewport 0 for util_blitter */
745 if (!start_slot
&& num_viewports
)
746 ilo
->viewport
.viewport0
= viewports
[0];
749 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
750 ilo
->viewport
.count
> start_slot
)
751 ilo
->viewport
.count
= start_slot
;
754 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
758 ilo_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
759 unsigned start
, unsigned count
,
760 struct pipe_sampler_view
**views
)
762 struct ilo_context
*ilo
= ilo_context(pipe
);
763 struct ilo_view_state
*dst
= &ilo
->view
[shader
];
766 assert(start
+ count
<= Elements(dst
->states
));
768 if (likely(shader
!= PIPE_SHADER_COMPUTE
)) {
774 /* views not in range are also unbound */
775 for (i
= 0; i
< start
; i
++)
776 pipe_sampler_view_reference(&dst
->states
[i
], NULL
);
777 for (; i
< start
+ count
; i
++)
778 pipe_sampler_view_reference(&dst
->states
[i
], views
[i
- start
]);
779 for (; i
< dst
->count
; i
++)
780 pipe_sampler_view_reference(&dst
->states
[i
], NULL
);
782 dst
->count
= start
+ count
;
788 for (i
= 0; i
< count
; i
++)
789 pipe_sampler_view_reference(&dst
->states
[start
+ i
], views
[i
]);
792 for (i
= 0; i
< count
; i
++)
793 pipe_sampler_view_reference(&dst
->states
[start
+ i
], NULL
);
796 if (dst
->count
<= start
+ count
) {
802 while (count
> 0 && !dst
->states
[count
- 1])
810 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
812 struct pipe_sampler_view
**views
)
814 struct ilo_context
*ilo
= ilo_context(pipe
);
816 ilo_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
,
817 0, num_views
, views
);
819 ilo
->dirty
|= ILO_DIRTY_VIEW_FS
;
823 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
825 struct pipe_sampler_view
**views
)
827 struct ilo_context
*ilo
= ilo_context(pipe
);
829 ilo_set_sampler_views(pipe
, PIPE_SHADER_VERTEX
,
830 0, num_views
, views
);
832 ilo
->dirty
|= ILO_DIRTY_VIEW_VS
;
836 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
838 struct pipe_sampler_view
**views
)
840 struct ilo_context
*ilo
= ilo_context(pipe
);
842 ilo_set_sampler_views(pipe
, PIPE_SHADER_GEOMETRY
,
843 0, num_views
, views
);
845 ilo
->dirty
|= ILO_DIRTY_VIEW_GS
;
849 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
850 unsigned start_slot
, unsigned num_views
,
851 struct pipe_sampler_view
**views
)
853 struct ilo_context
*ilo
= ilo_context(pipe
);
855 ilo_set_sampler_views(pipe
, PIPE_SHADER_COMPUTE
,
856 start_slot
, num_views
, views
);
858 ilo
->dirty
|= ILO_DIRTY_VIEW_CS
;
862 ilo_set_shader_resources(struct pipe_context
*pipe
,
863 unsigned start
, unsigned count
,
864 struct pipe_surface
**surfaces
)
866 struct ilo_context
*ilo
= ilo_context(pipe
);
867 struct ilo_resource_state
*dst
= &ilo
->resource
;
870 assert(start
+ count
<= Elements(dst
->states
));
873 for (i
= 0; i
< count
; i
++)
874 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
877 for (i
= 0; i
< count
; i
++)
878 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
881 if (dst
->count
<= start
+ count
) {
887 while (count
> 0 && !dst
->states
[count
- 1])
893 ilo
->dirty
|= ILO_DIRTY_RESOURCE
;
897 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
898 unsigned start_slot
, unsigned num_buffers
,
899 const struct pipe_vertex_buffer
*buffers
)
901 struct ilo_context
*ilo
= ilo_context(pipe
);
904 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
906 for (i
= 0; i
< num_buffers
; i
++)
907 assert(!buffers
[i
].user_buffer
);
910 util_set_vertex_buffers_mask(ilo
->vb
.states
,
911 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
913 ilo
->dirty
|= ILO_DIRTY_VB
;
917 ilo_set_index_buffer(struct pipe_context
*pipe
,
918 const struct pipe_index_buffer
*state
)
920 struct ilo_context
*ilo
= ilo_context(pipe
);
923 pipe_resource_reference(&ilo
->ib
.buffer
, state
->buffer
);
924 ilo
->ib
.user_buffer
= state
->user_buffer
;
925 ilo
->ib
.offset
= state
->offset
;
926 ilo
->ib
.index_size
= state
->index_size
;
929 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
930 ilo
->ib
.user_buffer
= NULL
;
932 ilo
->ib
.index_size
= 0;
935 ilo
->dirty
|= ILO_DIRTY_IB
;
938 static struct pipe_stream_output_target
*
939 ilo_create_stream_output_target(struct pipe_context
*pipe
,
940 struct pipe_resource
*res
,
941 unsigned buffer_offset
,
942 unsigned buffer_size
)
944 struct pipe_stream_output_target
*target
;
946 target
= MALLOC_STRUCT(pipe_stream_output_target
);
949 pipe_reference_init(&target
->reference
, 1);
950 target
->buffer
= NULL
;
951 pipe_resource_reference(&target
->buffer
, res
);
952 target
->context
= pipe
;
953 target
->buffer_offset
= buffer_offset
;
954 target
->buffer_size
= buffer_size
;
960 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
961 unsigned num_targets
,
962 struct pipe_stream_output_target
**targets
,
963 unsigned append_bitmask
)
965 struct ilo_context
*ilo
= ilo_context(pipe
);
971 /* util_blitter may set this unnecessarily */
972 if (!ilo
->so
.count
&& !num_targets
)
975 for (i
= 0; i
< num_targets
; i
++)
976 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
978 for (; i
< ilo
->so
.count
; i
++)
979 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
981 ilo
->so
.count
= num_targets
;
982 ilo
->so
.append_bitmask
= append_bitmask
;
984 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
986 ilo
->dirty
|= ILO_DIRTY_SO
;
990 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
991 struct pipe_stream_output_target
*target
)
993 pipe_resource_reference(&target
->buffer
, NULL
);
997 static struct pipe_sampler_view
*
998 ilo_create_sampler_view(struct pipe_context
*pipe
,
999 struct pipe_resource
*res
,
1000 const struct pipe_sampler_view
*templ
)
1002 struct ilo_context
*ilo
= ilo_context(pipe
);
1003 struct ilo_view_cso
*view
;
1005 view
= MALLOC_STRUCT(ilo_view_cso
);
1008 view
->base
= *templ
;
1009 pipe_reference_init(&view
->base
.reference
, 1);
1010 view
->base
.texture
= NULL
;
1011 pipe_resource_reference(&view
->base
.texture
, res
);
1012 view
->base
.context
= pipe
;
1014 if (res
->target
== PIPE_BUFFER
) {
1015 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
1016 const unsigned first_elem
= templ
->u
.buf
.first_element
;
1017 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
1019 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
1020 first_elem
* elem_size
, num_elems
* elem_size
,
1021 elem_size
, templ
->format
, false, false, &view
->surface
);
1024 struct ilo_texture
*tex
= ilo_texture(res
);
1026 /* warn about degraded performance because of a missing binding flag */
1027 if (tex
->tiling
== INTEL_TILING_NONE
&&
1028 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
1029 ilo_warn("creating sampler view for a resource "
1030 "not created for sampling\n");
1033 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
1035 templ
->u
.tex
.first_level
,
1036 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
1037 templ
->u
.tex
.first_layer
,
1038 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1039 false, false, &view
->surface
);
1046 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
1047 struct pipe_sampler_view
*view
)
1049 pipe_resource_reference(&view
->texture
, NULL
);
1053 static struct pipe_surface
*
1054 ilo_create_surface(struct pipe_context
*pipe
,
1055 struct pipe_resource
*res
,
1056 const struct pipe_surface
*templ
)
1058 struct ilo_context
*ilo
= ilo_context(pipe
);
1059 struct ilo_surface_cso
*surf
;
1061 surf
= MALLOC_STRUCT(ilo_surface_cso
);
1064 surf
->base
= *templ
;
1065 pipe_reference_init(&surf
->base
.reference
, 1);
1066 surf
->base
.texture
= NULL
;
1067 pipe_resource_reference(&surf
->base
.texture
, res
);
1069 surf
->base
.context
= pipe
;
1070 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
1071 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
1073 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
1077 assert(res
->target
!= PIPE_BUFFER
);
1080 * classic i965 sets render_cache_rw for constant buffers and sol
1081 * surfaces but not render buffers. Why?
1083 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
1084 templ
->format
, templ
->u
.tex
.level
, 1,
1085 templ
->u
.tex
.first_layer
,
1086 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1087 true, true, &surf
->u
.rt
);
1090 assert(res
->target
!= PIPE_BUFFER
);
1092 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
1093 templ
->format
, templ
->u
.tex
.level
,
1094 templ
->u
.tex
.first_layer
,
1095 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1103 ilo_surface_destroy(struct pipe_context
*pipe
,
1104 struct pipe_surface
*surface
)
1106 pipe_resource_reference(&surface
->texture
, NULL
);
1111 ilo_create_compute_state(struct pipe_context
*pipe
,
1112 const struct pipe_compute_state
*state
)
1114 struct ilo_context
*ilo
= ilo_context(pipe
);
1115 struct ilo_shader_state
*shader
;
1117 shader
= ilo_shader_create_cs(ilo
->dev
, state
, ilo
);
1120 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
1126 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
1128 struct ilo_context
*ilo
= ilo_context(pipe
);
1132 ilo
->dirty
|= ILO_DIRTY_CS
;
1136 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
1138 struct ilo_context
*ilo
= ilo_context(pipe
);
1139 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
1141 ilo_shader_cache_remove(ilo
->shader_cache
, cs
);
1142 ilo_shader_destroy(cs
);
1146 ilo_set_compute_resources(struct pipe_context
*pipe
,
1147 unsigned start
, unsigned count
,
1148 struct pipe_surface
**surfaces
)
1150 struct ilo_context
*ilo
= ilo_context(pipe
);
1151 struct ilo_resource_state
*dst
= &ilo
->cs_resource
;
1154 assert(start
+ count
<= Elements(dst
->states
));
1157 for (i
= 0; i
< count
; i
++)
1158 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
1161 for (i
= 0; i
< count
; i
++)
1162 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
1165 if (dst
->count
<= start
+ count
) {
1171 while (count
> 0 && !dst
->states
[count
- 1])
1177 ilo
->dirty
|= ILO_DIRTY_CS_RESOURCE
;
1181 ilo_set_global_binding(struct pipe_context
*pipe
,
1182 unsigned start
, unsigned count
,
1183 struct pipe_resource
**resources
,
1186 struct ilo_context
*ilo
= ilo_context(pipe
);
1187 struct ilo_global_binding
*dst
= &ilo
->global_binding
;
1190 assert(start
+ count
<= Elements(dst
->resources
));
1193 for (i
= 0; i
< count
; i
++)
1194 pipe_resource_reference(&dst
->resources
[start
+ i
], resources
[i
]);
1197 for (i
= 0; i
< count
; i
++)
1198 pipe_resource_reference(&dst
->resources
[start
+ i
], NULL
);
1201 if (dst
->count
<= start
+ count
) {
1207 while (count
> 0 && !dst
->resources
[count
- 1])
1213 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1217 * Initialize state-related functions.
1220 ilo_init_state_functions(struct ilo_context
*ilo
)
1222 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1224 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1225 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1226 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1227 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1228 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1229 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1230 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1231 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1232 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1233 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1234 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1235 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1236 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1237 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1238 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1239 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1240 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1241 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1242 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1243 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1244 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1245 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1246 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1247 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1248 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1249 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1250 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1252 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1253 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1254 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1255 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1256 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1257 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1258 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1259 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1260 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1261 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1262 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1263 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1264 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1265 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1266 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1267 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1269 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1270 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1271 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1273 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1274 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1276 ilo
->base
.create_surface
= ilo_create_surface
;
1277 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1279 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1280 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1281 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1282 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1283 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1287 ilo_init_states(struct ilo_context
*ilo
)
1289 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1291 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1292 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1294 ilo
->dirty
= ILO_DIRTY_ALL
;
1298 ilo_cleanup_states(struct ilo_context
*ilo
)
1302 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1303 if (ilo
->vb
.enabled_mask
& (1 << i
))
1304 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1307 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
1308 pipe_resource_reference(&ilo
->ib
.hw_resource
, NULL
);
1310 for (i
= 0; i
< ilo
->so
.count
; i
++)
1311 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1313 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1314 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1315 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1316 pipe_sampler_view_reference(&view
, NULL
);
1319 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1320 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1321 pipe_resource_reference(&cbuf
->resource
, NULL
);
1325 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1326 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1328 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1329 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1331 if (ilo
->fb
.state
.zsbuf
)
1332 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1334 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1335 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1337 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1338 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1342 * Mark all states that have the resource dirty.
1345 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1346 const struct pipe_resource
*res
)
1348 uint32_t states
= 0;
1351 if (res
->target
== PIPE_BUFFER
) {
1352 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1355 const unsigned idx
= u_bit_scan(&vb_mask
);
1357 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1358 states
|= ILO_DIRTY_VB
;
1363 if (ilo
->ib
.buffer
== res
) {
1364 states
|= ILO_DIRTY_IB
;
1367 * finalize_index_buffer() has an optimization that clears
1368 * ILO_DIRTY_IB when the HW states do not change. However, it fails
1369 * to flush the VF cache when the HW states do not change, but the
1370 * contents of the IB has changed. Here, we set the index size to an
1371 * invalid value to avoid the optimization.
1373 ilo
->ib
.hw_index_size
= 0;
1376 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1377 if (ilo
->so
.states
[i
]->buffer
== res
) {
1378 states
|= ILO_DIRTY_SO
;
1384 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1385 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1386 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1388 if (view
->texture
== res
) {
1389 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1390 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VIEW_VS
,
1391 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_VIEW_FS
,
1392 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_VIEW_GS
,
1393 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_VIEW_CS
,
1396 states
|= view_dirty_bits
[sh
];
1401 if (res
->target
== PIPE_BUFFER
) {
1402 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1403 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1405 if (cbuf
->resource
== res
) {
1406 states
|= ILO_DIRTY_CBUF
;
1413 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1414 if (ilo
->resource
.states
[i
]->texture
== res
) {
1415 states
|= ILO_DIRTY_RESOURCE
;
1421 if (res
->target
!= PIPE_BUFFER
) {
1422 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1423 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1424 states
|= ILO_DIRTY_FB
;
1429 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1430 states
|= ILO_DIRTY_FB
;
1433 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1434 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1435 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1436 states
|= ILO_DIRTY_CS_RESOURCE
;
1441 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1442 if (ilo
->global_binding
.resources
[i
] == res
) {
1443 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1448 ilo
->dirty
|= states
;