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
));
254 for (i
= 0; i
< count
; i
++)
255 dst
->cso
[start
+ i
] = samplers
[i
];
258 for (i
= 0; i
< count
; i
++)
259 dst
->cso
[start
+ i
] = NULL
;
262 if (dst
->count
<= start
+ count
) {
268 while (count
> 0 && !dst
->cso
[count
- 1])
276 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
277 unsigned num_samplers
,
280 struct ilo_context
*ilo
= ilo_context(pipe
);
282 ilo_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
283 0, num_samplers
, samplers
);
285 if (ilo
->sampler
[PIPE_SHADER_FRAGMENT
].count
> num_samplers
) {
286 ilo_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, num_samplers
,
287 ilo
->sampler
[PIPE_SHADER_FRAGMENT
].count
- num_samplers
, NULL
);
290 ilo
->dirty
|= ILO_DIRTY_SAMPLER_FS
;
294 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
295 unsigned num_samplers
,
298 struct ilo_context
*ilo
= ilo_context(pipe
);
300 ilo_bind_sampler_states(pipe
, PIPE_SHADER_VERTEX
,
301 0, num_samplers
, samplers
);
303 if (ilo
->sampler
[PIPE_SHADER_VERTEX
].count
> num_samplers
) {
304 ilo_bind_sampler_states(pipe
, PIPE_SHADER_VERTEX
, num_samplers
,
305 ilo
->sampler
[PIPE_SHADER_VERTEX
].count
- num_samplers
, NULL
);
308 ilo
->dirty
|= ILO_DIRTY_SAMPLER_VS
;
312 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
313 unsigned num_samplers
,
316 struct ilo_context
*ilo
= ilo_context(pipe
);
318 ilo_bind_sampler_states(pipe
, PIPE_SHADER_GEOMETRY
,
319 0, num_samplers
, samplers
);
321 if (ilo
->sampler
[PIPE_SHADER_GEOMETRY
].count
> num_samplers
) {
322 ilo_bind_sampler_states(pipe
, PIPE_SHADER_GEOMETRY
, num_samplers
,
323 ilo
->sampler
[PIPE_SHADER_GEOMETRY
].count
- num_samplers
, NULL
);
326 ilo
->dirty
|= ILO_DIRTY_SAMPLER_GS
;
330 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
332 unsigned num_samplers
,
335 struct ilo_context
*ilo
= ilo_context(pipe
);
337 ilo_bind_sampler_states(pipe
, PIPE_SHADER_COMPUTE
,
338 start_slot
, num_samplers
, samplers
);
340 ilo
->dirty
|= ILO_DIRTY_SAMPLER_CS
;
344 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
350 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
351 const struct pipe_rasterizer_state
*state
)
353 struct ilo_context
*ilo
= ilo_context(pipe
);
354 struct ilo_rasterizer_state
*rast
;
356 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
359 rast
->state
= *state
;
360 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
366 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
368 struct ilo_context
*ilo
= ilo_context(pipe
);
370 ilo
->rasterizer
= state
;
372 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
376 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
382 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
383 const struct pipe_depth_stencil_alpha_state
*state
)
385 struct ilo_context
*ilo
= ilo_context(pipe
);
386 struct ilo_dsa_state
*dsa
;
388 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
391 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
397 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
399 struct ilo_context
*ilo
= ilo_context(pipe
);
403 ilo
->dirty
|= ILO_DIRTY_DSA
;
407 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
413 ilo_create_fs_state(struct pipe_context
*pipe
,
414 const struct pipe_shader_state
*state
)
416 struct ilo_context
*ilo
= ilo_context(pipe
);
417 struct ilo_shader_state
*shader
;
419 shader
= ilo_shader_create_fs(ilo
->dev
, state
, ilo
);
422 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
428 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
430 struct ilo_context
*ilo
= ilo_context(pipe
);
434 ilo
->dirty
|= ILO_DIRTY_FS
;
438 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
440 struct ilo_context
*ilo
= ilo_context(pipe
);
441 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
443 ilo_shader_cache_remove(ilo
->shader_cache
, fs
);
444 ilo_shader_destroy(fs
);
448 ilo_create_vs_state(struct pipe_context
*pipe
,
449 const struct pipe_shader_state
*state
)
451 struct ilo_context
*ilo
= ilo_context(pipe
);
452 struct ilo_shader_state
*shader
;
454 shader
= ilo_shader_create_vs(ilo
->dev
, state
, ilo
);
457 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
463 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
465 struct ilo_context
*ilo
= ilo_context(pipe
);
469 ilo
->dirty
|= ILO_DIRTY_VS
;
473 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
475 struct ilo_context
*ilo
= ilo_context(pipe
);
476 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
478 ilo_shader_cache_remove(ilo
->shader_cache
, vs
);
479 ilo_shader_destroy(vs
);
483 ilo_create_gs_state(struct pipe_context
*pipe
,
484 const struct pipe_shader_state
*state
)
486 struct ilo_context
*ilo
= ilo_context(pipe
);
487 struct ilo_shader_state
*shader
;
489 shader
= ilo_shader_create_gs(ilo
->dev
, state
, ilo
);
492 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
498 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
500 struct ilo_context
*ilo
= ilo_context(pipe
);
502 /* util_blitter may set this unnecessarily */
503 if (ilo
->gs
== state
)
508 ilo
->dirty
|= ILO_DIRTY_GS
;
512 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
514 struct ilo_context
*ilo
= ilo_context(pipe
);
515 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
517 ilo_shader_cache_remove(ilo
->shader_cache
, gs
);
518 ilo_shader_destroy(gs
);
522 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
523 unsigned num_elements
,
524 const struct pipe_vertex_element
*elements
)
526 struct ilo_context
*ilo
= ilo_context(pipe
);
527 struct ilo_ve_state
*ve
;
529 ve
= MALLOC_STRUCT(ilo_ve_state
);
532 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
538 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
540 struct ilo_context
*ilo
= ilo_context(pipe
);
544 ilo
->dirty
|= ILO_DIRTY_VE
;
548 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
550 struct ilo_ve_state
*ve
= state
;
556 ilo_set_blend_color(struct pipe_context
*pipe
,
557 const struct pipe_blend_color
*state
)
559 struct ilo_context
*ilo
= ilo_context(pipe
);
561 ilo
->blend_color
= *state
;
563 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
567 ilo_set_stencil_ref(struct pipe_context
*pipe
,
568 const struct pipe_stencil_ref
*state
)
570 struct ilo_context
*ilo
= ilo_context(pipe
);
572 /* util_blitter may set this unnecessarily */
573 if (!memcmp(&ilo
->stencil_ref
, state
, sizeof(*state
)))
576 ilo
->stencil_ref
= *state
;
578 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
582 ilo_set_sample_mask(struct pipe_context
*pipe
,
583 unsigned sample_mask
)
585 struct ilo_context
*ilo
= ilo_context(pipe
);
587 /* util_blitter may set this unnecessarily */
588 if (ilo
->sample_mask
== sample_mask
)
591 ilo
->sample_mask
= sample_mask
;
593 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
597 ilo_set_clip_state(struct pipe_context
*pipe
,
598 const struct pipe_clip_state
*state
)
600 struct ilo_context
*ilo
= ilo_context(pipe
);
604 ilo
->dirty
|= ILO_DIRTY_CLIP
;
608 ilo_set_constant_buffer(struct pipe_context
*pipe
,
609 uint shader
, uint index
,
610 struct pipe_constant_buffer
*buf
)
612 struct ilo_context
*ilo
= ilo_context(pipe
);
613 struct ilo_cbuf_state
*cbuf
= &ilo
->cbuf
[shader
];
614 const unsigned count
= 1;
617 assert(shader
< Elements(ilo
->cbuf
));
618 assert(index
+ count
<= Elements(ilo
->cbuf
[shader
].cso
));
621 for (i
= 0; i
< count
; i
++) {
622 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
624 pipe_resource_reference(&cso
->resource
, buf
[i
].buffer
);
627 const enum pipe_format elem_format
=
628 PIPE_FORMAT_R32G32B32A32_FLOAT
;
630 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
631 ilo_buffer(buf
[i
].buffer
),
632 buf
[i
].buffer_offset
, buf
[i
].buffer_size
,
633 util_format_get_blocksize(elem_format
), elem_format
,
634 false, false, &cso
->surface
);
636 cso
->user_buffer
= NULL
;
637 cso
->user_buffer_size
= 0;
639 cbuf
->enabled_mask
|= 1 << (index
+ i
);
641 else if (buf
[i
].user_buffer
) {
642 cso
->surface
.bo
= NULL
;
644 /* buffer_offset does not apply for user buffer */
645 cso
->user_buffer
= buf
[i
].user_buffer
;
646 cso
->user_buffer_size
= buf
[i
].buffer_size
;
648 cbuf
->enabled_mask
|= 1 << (index
+ i
);
651 cso
->surface
.bo
= NULL
;
652 cso
->user_buffer
= NULL
;
653 cso
->user_buffer_size
= 0;
655 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
660 for (i
= 0; i
< count
; i
++) {
661 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
663 pipe_resource_reference(&cso
->resource
, NULL
);
664 cso
->surface
.bo
= NULL
;
665 cso
->user_buffer
= NULL
;
666 cso
->user_buffer_size
= 0;
668 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
672 ilo
->dirty
|= ILO_DIRTY_CBUF
;
676 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
677 const struct pipe_framebuffer_state
*state
)
679 struct ilo_context
*ilo
= ilo_context(pipe
);
681 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
684 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
685 else if (state
->zsbuf
)
686 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
688 ilo
->fb
.num_samples
= 1;
690 if (!ilo
->fb
.num_samples
)
691 ilo
->fb
.num_samples
= 1;
693 ilo
->dirty
|= ILO_DIRTY_FB
;
697 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
698 const struct pipe_poly_stipple
*state
)
700 struct ilo_context
*ilo
= ilo_context(pipe
);
702 ilo
->poly_stipple
= *state
;
704 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
708 ilo_set_scissor_states(struct pipe_context
*pipe
,
710 unsigned num_scissors
,
711 const struct pipe_scissor_state
*scissors
)
713 struct ilo_context
*ilo
= ilo_context(pipe
);
715 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
716 scissors
, &ilo
->scissor
);
718 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
722 ilo_set_viewport_states(struct pipe_context
*pipe
,
724 unsigned num_viewports
,
725 const struct pipe_viewport_state
*viewports
)
727 struct ilo_context
*ilo
= ilo_context(pipe
);
732 for (i
= 0; i
< num_viewports
; i
++) {
733 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
734 &ilo
->viewport
.cso
[start_slot
+ i
]);
737 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
738 ilo
->viewport
.count
= start_slot
+ num_viewports
;
740 /* need to save viewport 0 for util_blitter */
741 if (!start_slot
&& num_viewports
)
742 ilo
->viewport
.viewport0
= viewports
[0];
745 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
746 ilo
->viewport
.count
> start_slot
)
747 ilo
->viewport
.count
= start_slot
;
750 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
754 ilo_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
755 unsigned start
, unsigned count
,
756 struct pipe_sampler_view
**views
)
758 struct ilo_context
*ilo
= ilo_context(pipe
);
759 struct ilo_view_state
*dst
= &ilo
->view
[shader
];
762 assert(start
+ count
<= Elements(dst
->states
));
765 for (i
= 0; i
< count
; i
++)
766 pipe_sampler_view_reference(&dst
->states
[start
+ i
], views
[i
]);
769 for (i
= 0; i
< count
; i
++)
770 pipe_sampler_view_reference(&dst
->states
[start
+ i
], NULL
);
773 if (dst
->count
<= start
+ count
) {
779 while (count
> 0 && !dst
->states
[count
- 1])
787 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
789 struct pipe_sampler_view
**views
)
791 struct ilo_context
*ilo
= ilo_context(pipe
);
793 ilo_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
);
795 if (ilo
->view
[PIPE_SHADER_FRAGMENT
].count
> num_views
) {
796 ilo_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, num_views
,
797 ilo
->view
[PIPE_SHADER_FRAGMENT
].count
- num_views
, NULL
);
800 ilo
->dirty
|= ILO_DIRTY_VIEW_FS
;
804 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
806 struct pipe_sampler_view
**views
)
808 struct ilo_context
*ilo
= ilo_context(pipe
);
810 ilo_set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, 0, num_views
, views
);
812 if (ilo
->view
[PIPE_SHADER_VERTEX
].count
> num_views
) {
813 ilo_set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, num_views
,
814 ilo
->view
[PIPE_SHADER_VERTEX
].count
- num_views
, NULL
);
817 ilo
->dirty
|= ILO_DIRTY_VIEW_VS
;
821 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
823 struct pipe_sampler_view
**views
)
825 struct ilo_context
*ilo
= ilo_context(pipe
);
827 ilo_set_sampler_views(pipe
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
);
829 if (ilo
->view
[PIPE_SHADER_GEOMETRY
].count
> num_views
) {
830 ilo_set_sampler_views(pipe
, PIPE_SHADER_GEOMETRY
, num_views
,
831 ilo
->view
[PIPE_SHADER_GEOMETRY
].count
- num_views
, NULL
);
834 ilo
->dirty
|= ILO_DIRTY_VIEW_GS
;
838 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
839 unsigned start_slot
, unsigned num_views
,
840 struct pipe_sampler_view
**views
)
842 struct ilo_context
*ilo
= ilo_context(pipe
);
844 ilo_set_sampler_views(pipe
, PIPE_SHADER_COMPUTE
,
845 start_slot
, num_views
, views
);
847 ilo
->dirty
|= ILO_DIRTY_VIEW_CS
;
851 ilo_set_shader_resources(struct pipe_context
*pipe
,
852 unsigned start
, unsigned count
,
853 struct pipe_surface
**surfaces
)
855 struct ilo_context
*ilo
= ilo_context(pipe
);
856 struct ilo_resource_state
*dst
= &ilo
->resource
;
859 assert(start
+ count
<= Elements(dst
->states
));
862 for (i
= 0; i
< count
; i
++)
863 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
866 for (i
= 0; i
< count
; i
++)
867 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
870 if (dst
->count
<= start
+ count
) {
876 while (count
> 0 && !dst
->states
[count
- 1])
882 ilo
->dirty
|= ILO_DIRTY_RESOURCE
;
886 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
887 unsigned start_slot
, unsigned num_buffers
,
888 const struct pipe_vertex_buffer
*buffers
)
890 struct ilo_context
*ilo
= ilo_context(pipe
);
893 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
895 for (i
= 0; i
< num_buffers
; i
++)
896 assert(!buffers
[i
].user_buffer
);
899 util_set_vertex_buffers_mask(ilo
->vb
.states
,
900 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
902 ilo
->dirty
|= ILO_DIRTY_VB
;
906 ilo_set_index_buffer(struct pipe_context
*pipe
,
907 const struct pipe_index_buffer
*state
)
909 struct ilo_context
*ilo
= ilo_context(pipe
);
912 pipe_resource_reference(&ilo
->ib
.buffer
, state
->buffer
);
913 ilo
->ib
.user_buffer
= state
->user_buffer
;
914 ilo
->ib
.offset
= state
->offset
;
915 ilo
->ib
.index_size
= state
->index_size
;
918 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
919 ilo
->ib
.user_buffer
= NULL
;
921 ilo
->ib
.index_size
= 0;
924 ilo
->dirty
|= ILO_DIRTY_IB
;
927 static struct pipe_stream_output_target
*
928 ilo_create_stream_output_target(struct pipe_context
*pipe
,
929 struct pipe_resource
*res
,
930 unsigned buffer_offset
,
931 unsigned buffer_size
)
933 struct pipe_stream_output_target
*target
;
935 target
= MALLOC_STRUCT(pipe_stream_output_target
);
938 pipe_reference_init(&target
->reference
, 1);
939 target
->buffer
= NULL
;
940 pipe_resource_reference(&target
->buffer
, res
);
941 target
->context
= pipe
;
942 target
->buffer_offset
= buffer_offset
;
943 target
->buffer_size
= buffer_size
;
949 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
950 unsigned num_targets
,
951 struct pipe_stream_output_target
**targets
,
952 unsigned append_bitmask
)
954 struct ilo_context
*ilo
= ilo_context(pipe
);
960 /* util_blitter may set this unnecessarily */
961 if (!ilo
->so
.count
&& !num_targets
)
964 for (i
= 0; i
< num_targets
; i
++)
965 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
967 for (; i
< ilo
->so
.count
; i
++)
968 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
970 ilo
->so
.count
= num_targets
;
971 ilo
->so
.append_bitmask
= append_bitmask
;
973 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
975 ilo
->dirty
|= ILO_DIRTY_SO
;
979 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
980 struct pipe_stream_output_target
*target
)
982 pipe_resource_reference(&target
->buffer
, NULL
);
986 static struct pipe_sampler_view
*
987 ilo_create_sampler_view(struct pipe_context
*pipe
,
988 struct pipe_resource
*res
,
989 const struct pipe_sampler_view
*templ
)
991 struct ilo_context
*ilo
= ilo_context(pipe
);
992 struct ilo_view_cso
*view
;
994 view
= MALLOC_STRUCT(ilo_view_cso
);
998 pipe_reference_init(&view
->base
.reference
, 1);
999 view
->base
.texture
= NULL
;
1000 pipe_resource_reference(&view
->base
.texture
, res
);
1001 view
->base
.context
= pipe
;
1003 if (res
->target
== PIPE_BUFFER
) {
1004 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
1005 const unsigned first_elem
= templ
->u
.buf
.first_element
;
1006 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
1008 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
1009 first_elem
* elem_size
, num_elems
* elem_size
,
1010 elem_size
, templ
->format
, false, false, &view
->surface
);
1013 struct ilo_texture
*tex
= ilo_texture(res
);
1015 /* warn about degraded performance because of a missing binding flag */
1016 if (tex
->tiling
== INTEL_TILING_NONE
&&
1017 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
1018 ilo_warn("creating sampler view for a resource "
1019 "not created for sampling\n");
1022 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
1024 templ
->u
.tex
.first_level
,
1025 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
1026 templ
->u
.tex
.first_layer
,
1027 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1028 false, false, &view
->surface
);
1035 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
1036 struct pipe_sampler_view
*view
)
1038 pipe_resource_reference(&view
->texture
, NULL
);
1042 static struct pipe_surface
*
1043 ilo_create_surface(struct pipe_context
*pipe
,
1044 struct pipe_resource
*res
,
1045 const struct pipe_surface
*templ
)
1047 struct ilo_context
*ilo
= ilo_context(pipe
);
1048 struct ilo_surface_cso
*surf
;
1050 surf
= MALLOC_STRUCT(ilo_surface_cso
);
1053 surf
->base
= *templ
;
1054 pipe_reference_init(&surf
->base
.reference
, 1);
1055 surf
->base
.texture
= NULL
;
1056 pipe_resource_reference(&surf
->base
.texture
, res
);
1058 surf
->base
.context
= pipe
;
1059 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
1060 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
1062 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
1066 assert(res
->target
!= PIPE_BUFFER
);
1069 * classic i965 sets render_cache_rw for constant buffers and sol
1070 * surfaces but not render buffers. Why?
1072 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
1073 templ
->format
, templ
->u
.tex
.level
, 1,
1074 templ
->u
.tex
.first_layer
,
1075 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1076 true, true, &surf
->u
.rt
);
1079 assert(res
->target
!= PIPE_BUFFER
);
1081 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
1082 templ
->format
, templ
->u
.tex
.level
,
1083 templ
->u
.tex
.first_layer
,
1084 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1092 ilo_surface_destroy(struct pipe_context
*pipe
,
1093 struct pipe_surface
*surface
)
1095 pipe_resource_reference(&surface
->texture
, NULL
);
1100 ilo_create_compute_state(struct pipe_context
*pipe
,
1101 const struct pipe_compute_state
*state
)
1103 struct ilo_context
*ilo
= ilo_context(pipe
);
1104 struct ilo_shader_state
*shader
;
1106 shader
= ilo_shader_create_cs(ilo
->dev
, state
, ilo
);
1109 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
1115 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
1117 struct ilo_context
*ilo
= ilo_context(pipe
);
1121 ilo
->dirty
|= ILO_DIRTY_CS
;
1125 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
1127 struct ilo_context
*ilo
= ilo_context(pipe
);
1128 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
1130 ilo_shader_cache_remove(ilo
->shader_cache
, cs
);
1131 ilo_shader_destroy(cs
);
1135 ilo_set_compute_resources(struct pipe_context
*pipe
,
1136 unsigned start
, unsigned count
,
1137 struct pipe_surface
**surfaces
)
1139 struct ilo_context
*ilo
= ilo_context(pipe
);
1140 struct ilo_resource_state
*dst
= &ilo
->cs_resource
;
1143 assert(start
+ count
<= Elements(dst
->states
));
1146 for (i
= 0; i
< count
; i
++)
1147 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
1150 for (i
= 0; i
< count
; i
++)
1151 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
1154 if (dst
->count
<= start
+ count
) {
1160 while (count
> 0 && !dst
->states
[count
- 1])
1166 ilo
->dirty
|= ILO_DIRTY_CS_RESOURCE
;
1170 ilo_set_global_binding(struct pipe_context
*pipe
,
1171 unsigned start
, unsigned count
,
1172 struct pipe_resource
**resources
,
1175 struct ilo_context
*ilo
= ilo_context(pipe
);
1176 struct ilo_global_binding
*dst
= &ilo
->global_binding
;
1179 assert(start
+ count
<= Elements(dst
->resources
));
1182 for (i
= 0; i
< count
; i
++)
1183 pipe_resource_reference(&dst
->resources
[start
+ i
], resources
[i
]);
1186 for (i
= 0; i
< count
; i
++)
1187 pipe_resource_reference(&dst
->resources
[start
+ i
], NULL
);
1190 if (dst
->count
<= start
+ count
) {
1196 while (count
> 0 && !dst
->resources
[count
- 1])
1202 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1206 * Initialize state-related functions.
1209 ilo_init_state_functions(struct ilo_context
*ilo
)
1211 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1213 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1214 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1215 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1216 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1217 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1218 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1219 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1220 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1221 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1222 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1223 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1224 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1225 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1226 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1227 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1228 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1229 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1230 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1231 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1232 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1233 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1234 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1235 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1236 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1237 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1238 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1239 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1241 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1242 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1243 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1244 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1245 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1246 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1247 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1248 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1249 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1250 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1251 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1252 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1253 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1254 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1255 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1256 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1258 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1259 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1260 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1262 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1263 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1265 ilo
->base
.create_surface
= ilo_create_surface
;
1266 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1268 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1269 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1270 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1271 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1272 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1276 ilo_init_states(struct ilo_context
*ilo
)
1278 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1280 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1281 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1283 ilo
->dirty
= ILO_DIRTY_ALL
;
1287 ilo_cleanup_states(struct ilo_context
*ilo
)
1291 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1292 if (ilo
->vb
.enabled_mask
& (1 << i
))
1293 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1296 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
1297 pipe_resource_reference(&ilo
->ib
.hw_resource
, NULL
);
1299 for (i
= 0; i
< ilo
->so
.count
; i
++)
1300 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1302 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1303 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1304 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1305 pipe_sampler_view_reference(&view
, NULL
);
1308 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1309 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1310 pipe_resource_reference(&cbuf
->resource
, NULL
);
1314 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1315 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1317 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1318 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1320 if (ilo
->fb
.state
.zsbuf
)
1321 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1323 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1324 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1326 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1327 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1331 * Mark all states that have the resource dirty.
1334 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1335 const struct pipe_resource
*res
)
1337 uint32_t states
= 0;
1340 if (res
->target
== PIPE_BUFFER
) {
1341 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1344 const unsigned idx
= u_bit_scan(&vb_mask
);
1346 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1347 states
|= ILO_DIRTY_VB
;
1352 if (ilo
->ib
.buffer
== res
) {
1353 states
|= ILO_DIRTY_IB
;
1356 * finalize_index_buffer() has an optimization that clears
1357 * ILO_DIRTY_IB when the HW states do not change. However, it fails
1358 * to flush the VF cache when the HW states do not change, but the
1359 * contents of the IB has changed. Here, we set the index size to an
1360 * invalid value to avoid the optimization.
1362 ilo
->ib
.hw_index_size
= 0;
1365 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1366 if (ilo
->so
.states
[i
]->buffer
== res
) {
1367 states
|= ILO_DIRTY_SO
;
1373 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1374 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1375 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1377 if (view
->texture
== res
) {
1378 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1379 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VIEW_VS
,
1380 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_VIEW_FS
,
1381 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_VIEW_GS
,
1382 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_VIEW_CS
,
1385 states
|= view_dirty_bits
[sh
];
1390 if (res
->target
== PIPE_BUFFER
) {
1391 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1392 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1394 if (cbuf
->resource
== res
) {
1395 states
|= ILO_DIRTY_CBUF
;
1402 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1403 if (ilo
->resource
.states
[i
]->texture
== res
) {
1404 states
|= ILO_DIRTY_RESOURCE
;
1410 if (res
->target
!= PIPE_BUFFER
) {
1411 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1412 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1413 states
|= ILO_DIRTY_FB
;
1418 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1419 states
|= ILO_DIRTY_FB
;
1422 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1423 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1424 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1425 states
|= ILO_DIRTY_CS_RESOURCE
;
1430 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1431 if (ilo
->global_binding
.resources
[i
] == res
) {
1432 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1437 ilo
->dirty
|= states
;