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 bool need_upload
= (ilo
->draw
->indexed
&&
132 (ilo
->ib
.user_buffer
|| ilo
->ib
.offset
% ilo
->ib
.index_size
));
133 struct pipe_resource
*current_hw_res
= NULL
;
135 if (!(ilo
->dirty
& ILO_DIRTY_IB
) && !need_upload
)
138 pipe_resource_reference(¤t_hw_res
, ilo
->ib
.hw_resource
);
141 const unsigned offset
= ilo
->ib
.index_size
* ilo
->draw
->start
;
142 const unsigned size
= ilo
->ib
.index_size
* ilo
->draw
->count
;
145 if (ilo
->ib
.user_buffer
) {
146 u_upload_data(ilo
->uploader
, 0, size
,
147 ilo
->ib
.user_buffer
+ offset
, &hw_offset
, &ilo
->ib
.hw_resource
);
150 u_upload_buffer(ilo
->uploader
, 0, ilo
->ib
.offset
+ offset
, size
,
151 ilo
->ib
.buffer
, &hw_offset
, &ilo
->ib
.hw_resource
);
154 /* the HW offset should be aligned */
155 assert(hw_offset
% ilo
->ib
.index_size
== 0);
156 ilo
->ib
.draw_start_offset
= hw_offset
/ ilo
->ib
.index_size
;
159 * INDEX[ilo->draw->start] in the original buffer is INDEX[0] in the HW
162 ilo
->ib
.draw_start_offset
-= ilo
->draw
->start
;
165 pipe_resource_reference(&ilo
->ib
.hw_resource
, ilo
->ib
.buffer
);
167 /* note that index size may be zero when the draw is not indexed */
168 if (ilo
->draw
->indexed
)
169 ilo
->ib
.draw_start_offset
= ilo
->ib
.offset
/ ilo
->ib
.index_size
;
171 ilo
->ib
.draw_start_offset
= 0;
174 /* treat the IB as clean if the HW states do not change */
175 if (ilo
->ib
.hw_resource
== current_hw_res
&&
176 ilo
->ib
.hw_index_size
== ilo
->ib
.index_size
)
177 ilo
->dirty
&= ~ILO_DIRTY_IB
;
179 ilo
->ib
.hw_index_size
= ilo
->ib
.index_size
;
181 pipe_resource_reference(¤t_hw_res
, NULL
);
185 * Finalize states. Some states depend on other states and are
186 * incomplete/invalid until finalized.
189 ilo_finalize_3d_states(struct ilo_context
*ilo
,
190 const struct pipe_draw_info
*draw
)
194 finalize_shader_states(ilo
);
195 finalize_constant_buffers(ilo
);
196 finalize_index_buffer(ilo
);
198 u_upload_unmap(ilo
->uploader
);
202 ilo_create_blend_state(struct pipe_context
*pipe
,
203 const struct pipe_blend_state
*state
)
205 struct ilo_context
*ilo
= ilo_context(pipe
);
206 struct ilo_blend_state
*blend
;
208 blend
= MALLOC_STRUCT(ilo_blend_state
);
211 ilo_gpe_init_blend(ilo
->dev
, state
, blend
);
217 ilo_bind_blend_state(struct pipe_context
*pipe
, void *state
)
219 struct ilo_context
*ilo
= ilo_context(pipe
);
223 ilo
->dirty
|= ILO_DIRTY_BLEND
;
227 ilo_delete_blend_state(struct pipe_context
*pipe
, void *state
)
233 ilo_create_sampler_state(struct pipe_context
*pipe
,
234 const struct pipe_sampler_state
*state
)
236 struct ilo_context
*ilo
= ilo_context(pipe
);
237 struct ilo_sampler_cso
*sampler
;
239 sampler
= MALLOC_STRUCT(ilo_sampler_cso
);
242 ilo_gpe_init_sampler_cso(ilo
->dev
, state
, sampler
);
248 ilo_bind_sampler_states(struct pipe_context
*pipe
, unsigned shader
,
249 unsigned start
, unsigned count
, void **samplers
)
251 struct ilo_context
*ilo
= ilo_context(pipe
);
252 struct ilo_sampler_state
*dst
= &ilo
->sampler
[shader
];
253 bool changed
= false;
256 assert(start
+ count
<= Elements(dst
->cso
));
259 for (i
= 0; i
< count
; i
++) {
260 if (dst
->cso
[start
+ i
] != samplers
[i
]) {
261 dst
->cso
[start
+ i
] = samplers
[i
];
264 * This function is sometimes called to reduce the number of bound
265 * samplers. Do not consider that as a state change (and create a
266 * new array of SAMPLER_STATE).
274 for (i
= 0; i
< count
; i
++)
275 dst
->cso
[start
+ i
] = NULL
;
278 if (dst
->count
<= start
+ count
) {
284 while (count
> 0 && !dst
->cso
[count
- 1])
294 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
295 unsigned num_samplers
,
298 struct ilo_context
*ilo
= ilo_context(pipe
);
300 if (ilo_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
,
301 0, num_samplers
, samplers
))
302 ilo
->dirty
|= ILO_DIRTY_SAMPLER_FS
;
304 if (ilo
->sampler
[PIPE_SHADER_FRAGMENT
].count
> num_samplers
) {
305 ilo_bind_sampler_states(pipe
, PIPE_SHADER_FRAGMENT
, num_samplers
,
306 ilo
->sampler
[PIPE_SHADER_FRAGMENT
].count
- num_samplers
, NULL
);
311 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
312 unsigned num_samplers
,
315 struct ilo_context
*ilo
= ilo_context(pipe
);
317 if (ilo_bind_sampler_states(pipe
, PIPE_SHADER_VERTEX
,
318 0, num_samplers
, samplers
))
319 ilo
->dirty
|= ILO_DIRTY_SAMPLER_VS
;
321 if (ilo
->sampler
[PIPE_SHADER_VERTEX
].count
> num_samplers
) {
322 ilo_bind_sampler_states(pipe
, PIPE_SHADER_VERTEX
, num_samplers
,
323 ilo
->sampler
[PIPE_SHADER_VERTEX
].count
- num_samplers
, NULL
);
328 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
329 unsigned num_samplers
,
332 struct ilo_context
*ilo
= ilo_context(pipe
);
334 if (ilo_bind_sampler_states(pipe
, PIPE_SHADER_GEOMETRY
,
335 0, num_samplers
, samplers
))
336 ilo
->dirty
|= ILO_DIRTY_SAMPLER_GS
;
338 if (ilo
->sampler
[PIPE_SHADER_GEOMETRY
].count
> num_samplers
) {
339 ilo_bind_sampler_states(pipe
, PIPE_SHADER_GEOMETRY
, num_samplers
,
340 ilo
->sampler
[PIPE_SHADER_GEOMETRY
].count
- num_samplers
, NULL
);
345 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
347 unsigned num_samplers
,
350 struct ilo_context
*ilo
= ilo_context(pipe
);
352 if (ilo_bind_sampler_states(pipe
, PIPE_SHADER_COMPUTE
,
353 start_slot
, num_samplers
, samplers
))
354 ilo
->dirty
|= ILO_DIRTY_SAMPLER_CS
;
358 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
364 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
365 const struct pipe_rasterizer_state
*state
)
367 struct ilo_context
*ilo
= ilo_context(pipe
);
368 struct ilo_rasterizer_state
*rast
;
370 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
373 rast
->state
= *state
;
374 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
380 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
382 struct ilo_context
*ilo
= ilo_context(pipe
);
384 ilo
->rasterizer
= state
;
386 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
390 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
396 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
397 const struct pipe_depth_stencil_alpha_state
*state
)
399 struct ilo_context
*ilo
= ilo_context(pipe
);
400 struct ilo_dsa_state
*dsa
;
402 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
405 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
411 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
413 struct ilo_context
*ilo
= ilo_context(pipe
);
417 ilo
->dirty
|= ILO_DIRTY_DSA
;
421 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
427 ilo_create_fs_state(struct pipe_context
*pipe
,
428 const struct pipe_shader_state
*state
)
430 struct ilo_context
*ilo
= ilo_context(pipe
);
431 struct ilo_shader_state
*shader
;
433 shader
= ilo_shader_create_fs(ilo
->dev
, state
, ilo
);
436 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
442 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
444 struct ilo_context
*ilo
= ilo_context(pipe
);
448 ilo
->dirty
|= ILO_DIRTY_FS
;
452 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
454 struct ilo_context
*ilo
= ilo_context(pipe
);
455 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
457 ilo_shader_cache_remove(ilo
->shader_cache
, fs
);
458 ilo_shader_destroy(fs
);
462 ilo_create_vs_state(struct pipe_context
*pipe
,
463 const struct pipe_shader_state
*state
)
465 struct ilo_context
*ilo
= ilo_context(pipe
);
466 struct ilo_shader_state
*shader
;
468 shader
= ilo_shader_create_vs(ilo
->dev
, state
, ilo
);
471 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
477 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
479 struct ilo_context
*ilo
= ilo_context(pipe
);
483 ilo
->dirty
|= ILO_DIRTY_VS
;
487 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
489 struct ilo_context
*ilo
= ilo_context(pipe
);
490 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
492 ilo_shader_cache_remove(ilo
->shader_cache
, vs
);
493 ilo_shader_destroy(vs
);
497 ilo_create_gs_state(struct pipe_context
*pipe
,
498 const struct pipe_shader_state
*state
)
500 struct ilo_context
*ilo
= ilo_context(pipe
);
501 struct ilo_shader_state
*shader
;
503 shader
= ilo_shader_create_gs(ilo
->dev
, state
, ilo
);
506 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
512 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
514 struct ilo_context
*ilo
= ilo_context(pipe
);
516 /* util_blitter may set this unnecessarily */
517 if (ilo
->gs
== state
)
522 ilo
->dirty
|= ILO_DIRTY_GS
;
526 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
528 struct ilo_context
*ilo
= ilo_context(pipe
);
529 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
531 ilo_shader_cache_remove(ilo
->shader_cache
, gs
);
532 ilo_shader_destroy(gs
);
536 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
537 unsigned num_elements
,
538 const struct pipe_vertex_element
*elements
)
540 struct ilo_context
*ilo
= ilo_context(pipe
);
541 struct ilo_ve_state
*ve
;
543 ve
= MALLOC_STRUCT(ilo_ve_state
);
546 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
552 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
554 struct ilo_context
*ilo
= ilo_context(pipe
);
558 ilo
->dirty
|= ILO_DIRTY_VE
;
562 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
564 struct ilo_ve_state
*ve
= state
;
570 ilo_set_blend_color(struct pipe_context
*pipe
,
571 const struct pipe_blend_color
*state
)
573 struct ilo_context
*ilo
= ilo_context(pipe
);
575 ilo
->blend_color
= *state
;
577 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
581 ilo_set_stencil_ref(struct pipe_context
*pipe
,
582 const struct pipe_stencil_ref
*state
)
584 struct ilo_context
*ilo
= ilo_context(pipe
);
586 /* util_blitter may set this unnecessarily */
587 if (!memcmp(&ilo
->stencil_ref
, state
, sizeof(*state
)))
590 ilo
->stencil_ref
= *state
;
592 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
596 ilo_set_sample_mask(struct pipe_context
*pipe
,
597 unsigned sample_mask
)
599 struct ilo_context
*ilo
= ilo_context(pipe
);
601 /* util_blitter may set this unnecessarily */
602 if (ilo
->sample_mask
== sample_mask
)
605 ilo
->sample_mask
= sample_mask
;
607 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
611 ilo_set_clip_state(struct pipe_context
*pipe
,
612 const struct pipe_clip_state
*state
)
614 struct ilo_context
*ilo
= ilo_context(pipe
);
618 ilo
->dirty
|= ILO_DIRTY_CLIP
;
622 ilo_set_constant_buffer(struct pipe_context
*pipe
,
623 uint shader
, uint index
,
624 struct pipe_constant_buffer
*buf
)
626 struct ilo_context
*ilo
= ilo_context(pipe
);
627 struct ilo_cbuf_state
*cbuf
= &ilo
->cbuf
[shader
];
628 const unsigned count
= 1;
631 assert(shader
< Elements(ilo
->cbuf
));
632 assert(index
+ count
<= Elements(ilo
->cbuf
[shader
].cso
));
635 for (i
= 0; i
< count
; i
++) {
636 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
638 pipe_resource_reference(&cso
->resource
, buf
[i
].buffer
);
641 const enum pipe_format elem_format
=
642 PIPE_FORMAT_R32G32B32A32_FLOAT
;
644 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
645 ilo_buffer(buf
[i
].buffer
),
646 buf
[i
].buffer_offset
, buf
[i
].buffer_size
,
647 util_format_get_blocksize(elem_format
), elem_format
,
648 false, false, &cso
->surface
);
650 cso
->user_buffer
= NULL
;
651 cso
->user_buffer_size
= 0;
653 cbuf
->enabled_mask
|= 1 << (index
+ i
);
655 else if (buf
[i
].user_buffer
) {
656 cso
->surface
.bo
= NULL
;
658 /* buffer_offset does not apply for user buffer */
659 cso
->user_buffer
= buf
[i
].user_buffer
;
660 cso
->user_buffer_size
= buf
[i
].buffer_size
;
662 cbuf
->enabled_mask
|= 1 << (index
+ i
);
665 cso
->surface
.bo
= NULL
;
666 cso
->user_buffer
= NULL
;
667 cso
->user_buffer_size
= 0;
669 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
674 for (i
= 0; i
< count
; i
++) {
675 struct ilo_cbuf_cso
*cso
= &cbuf
->cso
[index
+ i
];
677 pipe_resource_reference(&cso
->resource
, NULL
);
678 cso
->surface
.bo
= NULL
;
679 cso
->user_buffer
= NULL
;
680 cso
->user_buffer_size
= 0;
682 cbuf
->enabled_mask
&= ~(1 << (index
+ i
));
686 ilo
->dirty
|= ILO_DIRTY_CBUF
;
690 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
691 const struct pipe_framebuffer_state
*state
)
693 struct ilo_context
*ilo
= ilo_context(pipe
);
695 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
698 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
699 else if (state
->zsbuf
)
700 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
702 ilo
->fb
.num_samples
= 1;
704 if (!ilo
->fb
.num_samples
)
705 ilo
->fb
.num_samples
= 1;
707 ilo
->dirty
|= ILO_DIRTY_FB
;
711 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
712 const struct pipe_poly_stipple
*state
)
714 struct ilo_context
*ilo
= ilo_context(pipe
);
716 ilo
->poly_stipple
= *state
;
718 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
722 ilo_set_scissor_states(struct pipe_context
*pipe
,
724 unsigned num_scissors
,
725 const struct pipe_scissor_state
*scissors
)
727 struct ilo_context
*ilo
= ilo_context(pipe
);
729 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
730 scissors
, &ilo
->scissor
);
732 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
736 ilo_set_viewport_states(struct pipe_context
*pipe
,
738 unsigned num_viewports
,
739 const struct pipe_viewport_state
*viewports
)
741 struct ilo_context
*ilo
= ilo_context(pipe
);
746 for (i
= 0; i
< num_viewports
; i
++) {
747 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
748 &ilo
->viewport
.cso
[start_slot
+ i
]);
751 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
752 ilo
->viewport
.count
= start_slot
+ num_viewports
;
754 /* need to save viewport 0 for util_blitter */
755 if (!start_slot
&& num_viewports
)
756 ilo
->viewport
.viewport0
= viewports
[0];
759 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
760 ilo
->viewport
.count
> start_slot
)
761 ilo
->viewport
.count
= start_slot
;
764 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
768 ilo_set_sampler_views(struct pipe_context
*pipe
, unsigned shader
,
769 unsigned start
, unsigned count
,
770 struct pipe_sampler_view
**views
)
772 struct ilo_context
*ilo
= ilo_context(pipe
);
773 struct ilo_view_state
*dst
= &ilo
->view
[shader
];
776 assert(start
+ count
<= Elements(dst
->states
));
779 for (i
= 0; i
< count
; i
++)
780 pipe_sampler_view_reference(&dst
->states
[start
+ i
], views
[i
]);
783 for (i
= 0; i
< count
; i
++)
784 pipe_sampler_view_reference(&dst
->states
[start
+ i
], NULL
);
787 if (dst
->count
<= start
+ count
) {
793 while (count
> 0 && !dst
->states
[count
- 1])
801 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
803 struct pipe_sampler_view
**views
)
805 struct ilo_context
*ilo
= ilo_context(pipe
);
807 ilo_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
);
809 if (ilo
->view
[PIPE_SHADER_FRAGMENT
].count
> num_views
) {
810 ilo_set_sampler_views(pipe
, PIPE_SHADER_FRAGMENT
, num_views
,
811 ilo
->view
[PIPE_SHADER_FRAGMENT
].count
- num_views
, NULL
);
814 ilo
->dirty
|= ILO_DIRTY_VIEW_FS
;
818 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
820 struct pipe_sampler_view
**views
)
822 struct ilo_context
*ilo
= ilo_context(pipe
);
824 ilo_set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, 0, num_views
, views
);
826 if (ilo
->view
[PIPE_SHADER_VERTEX
].count
> num_views
) {
827 ilo_set_sampler_views(pipe
, PIPE_SHADER_VERTEX
, num_views
,
828 ilo
->view
[PIPE_SHADER_VERTEX
].count
- num_views
, NULL
);
831 ilo
->dirty
|= ILO_DIRTY_VIEW_VS
;
835 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
837 struct pipe_sampler_view
**views
)
839 struct ilo_context
*ilo
= ilo_context(pipe
);
841 ilo_set_sampler_views(pipe
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
);
843 if (ilo
->view
[PIPE_SHADER_GEOMETRY
].count
> num_views
) {
844 ilo_set_sampler_views(pipe
, PIPE_SHADER_GEOMETRY
, num_views
,
845 ilo
->view
[PIPE_SHADER_GEOMETRY
].count
- num_views
, NULL
);
848 ilo
->dirty
|= ILO_DIRTY_VIEW_GS
;
852 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
853 unsigned start_slot
, unsigned num_views
,
854 struct pipe_sampler_view
**views
)
856 struct ilo_context
*ilo
= ilo_context(pipe
);
858 ilo_set_sampler_views(pipe
, PIPE_SHADER_COMPUTE
,
859 start_slot
, num_views
, views
);
861 ilo
->dirty
|= ILO_DIRTY_VIEW_CS
;
865 ilo_set_shader_resources(struct pipe_context
*pipe
,
866 unsigned start
, unsigned count
,
867 struct pipe_surface
**surfaces
)
869 struct ilo_context
*ilo
= ilo_context(pipe
);
870 struct ilo_resource_state
*dst
= &ilo
->resource
;
873 assert(start
+ count
<= Elements(dst
->states
));
876 for (i
= 0; i
< count
; i
++)
877 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
880 for (i
= 0; i
< count
; i
++)
881 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
884 if (dst
->count
<= start
+ count
) {
890 while (count
> 0 && !dst
->states
[count
- 1])
896 ilo
->dirty
|= ILO_DIRTY_RESOURCE
;
900 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
901 unsigned start_slot
, unsigned num_buffers
,
902 const struct pipe_vertex_buffer
*buffers
)
904 struct ilo_context
*ilo
= ilo_context(pipe
);
907 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
909 for (i
= 0; i
< num_buffers
; i
++)
910 assert(!buffers
[i
].user_buffer
);
913 util_set_vertex_buffers_mask(ilo
->vb
.states
,
914 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
916 ilo
->dirty
|= ILO_DIRTY_VB
;
920 ilo_set_index_buffer(struct pipe_context
*pipe
,
921 const struct pipe_index_buffer
*state
)
923 struct ilo_context
*ilo
= ilo_context(pipe
);
926 pipe_resource_reference(&ilo
->ib
.buffer
, state
->buffer
);
927 ilo
->ib
.user_buffer
= state
->user_buffer
;
928 ilo
->ib
.offset
= state
->offset
;
929 ilo
->ib
.index_size
= state
->index_size
;
932 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
933 ilo
->ib
.user_buffer
= NULL
;
935 ilo
->ib
.index_size
= 0;
938 ilo
->dirty
|= ILO_DIRTY_IB
;
941 static struct pipe_stream_output_target
*
942 ilo_create_stream_output_target(struct pipe_context
*pipe
,
943 struct pipe_resource
*res
,
944 unsigned buffer_offset
,
945 unsigned buffer_size
)
947 struct pipe_stream_output_target
*target
;
949 target
= MALLOC_STRUCT(pipe_stream_output_target
);
952 pipe_reference_init(&target
->reference
, 1);
953 target
->buffer
= NULL
;
954 pipe_resource_reference(&target
->buffer
, res
);
955 target
->context
= pipe
;
956 target
->buffer_offset
= buffer_offset
;
957 target
->buffer_size
= buffer_size
;
963 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
964 unsigned num_targets
,
965 struct pipe_stream_output_target
**targets
,
966 unsigned append_bitmask
)
968 struct ilo_context
*ilo
= ilo_context(pipe
);
974 /* util_blitter may set this unnecessarily */
975 if (!ilo
->so
.count
&& !num_targets
)
978 for (i
= 0; i
< num_targets
; i
++)
979 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
981 for (; i
< ilo
->so
.count
; i
++)
982 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
984 ilo
->so
.count
= num_targets
;
985 ilo
->so
.append_bitmask
= append_bitmask
;
987 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
989 ilo
->dirty
|= ILO_DIRTY_SO
;
993 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
994 struct pipe_stream_output_target
*target
)
996 pipe_resource_reference(&target
->buffer
, NULL
);
1000 static struct pipe_sampler_view
*
1001 ilo_create_sampler_view(struct pipe_context
*pipe
,
1002 struct pipe_resource
*res
,
1003 const struct pipe_sampler_view
*templ
)
1005 struct ilo_context
*ilo
= ilo_context(pipe
);
1006 struct ilo_view_cso
*view
;
1008 view
= MALLOC_STRUCT(ilo_view_cso
);
1011 view
->base
= *templ
;
1012 pipe_reference_init(&view
->base
.reference
, 1);
1013 view
->base
.texture
= NULL
;
1014 pipe_resource_reference(&view
->base
.texture
, res
);
1015 view
->base
.context
= pipe
;
1017 if (res
->target
== PIPE_BUFFER
) {
1018 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
1019 const unsigned first_elem
= templ
->u
.buf
.first_element
;
1020 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
1022 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
1023 first_elem
* elem_size
, num_elems
* elem_size
,
1024 elem_size
, templ
->format
, false, false, &view
->surface
);
1027 struct ilo_texture
*tex
= ilo_texture(res
);
1029 /* warn about degraded performance because of a missing binding flag */
1030 if (tex
->tiling
== INTEL_TILING_NONE
&&
1031 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
1032 ilo_warn("creating sampler view for a resource "
1033 "not created for sampling\n");
1036 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
1038 templ
->u
.tex
.first_level
,
1039 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
1040 templ
->u
.tex
.first_layer
,
1041 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1042 false, false, &view
->surface
);
1049 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
1050 struct pipe_sampler_view
*view
)
1052 pipe_resource_reference(&view
->texture
, NULL
);
1056 static struct pipe_surface
*
1057 ilo_create_surface(struct pipe_context
*pipe
,
1058 struct pipe_resource
*res
,
1059 const struct pipe_surface
*templ
)
1061 struct ilo_context
*ilo
= ilo_context(pipe
);
1062 struct ilo_surface_cso
*surf
;
1064 surf
= MALLOC_STRUCT(ilo_surface_cso
);
1067 surf
->base
= *templ
;
1068 pipe_reference_init(&surf
->base
.reference
, 1);
1069 surf
->base
.texture
= NULL
;
1070 pipe_resource_reference(&surf
->base
.texture
, res
);
1072 surf
->base
.context
= pipe
;
1073 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
1074 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
1076 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
1080 assert(res
->target
!= PIPE_BUFFER
);
1083 * classic i965 sets render_cache_rw for constant buffers and sol
1084 * surfaces but not render buffers. Why?
1086 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
1087 templ
->format
, templ
->u
.tex
.level
, 1,
1088 templ
->u
.tex
.first_layer
,
1089 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1090 true, true, &surf
->u
.rt
);
1093 assert(res
->target
!= PIPE_BUFFER
);
1095 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
1096 templ
->format
, templ
->u
.tex
.level
,
1097 templ
->u
.tex
.first_layer
,
1098 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1106 ilo_surface_destroy(struct pipe_context
*pipe
,
1107 struct pipe_surface
*surface
)
1109 pipe_resource_reference(&surface
->texture
, NULL
);
1114 ilo_create_compute_state(struct pipe_context
*pipe
,
1115 const struct pipe_compute_state
*state
)
1117 struct ilo_context
*ilo
= ilo_context(pipe
);
1118 struct ilo_shader_state
*shader
;
1120 shader
= ilo_shader_create_cs(ilo
->dev
, state
, ilo
);
1123 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
1129 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
1131 struct ilo_context
*ilo
= ilo_context(pipe
);
1135 ilo
->dirty
|= ILO_DIRTY_CS
;
1139 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
1141 struct ilo_context
*ilo
= ilo_context(pipe
);
1142 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
1144 ilo_shader_cache_remove(ilo
->shader_cache
, cs
);
1145 ilo_shader_destroy(cs
);
1149 ilo_set_compute_resources(struct pipe_context
*pipe
,
1150 unsigned start
, unsigned count
,
1151 struct pipe_surface
**surfaces
)
1153 struct ilo_context
*ilo
= ilo_context(pipe
);
1154 struct ilo_resource_state
*dst
= &ilo
->cs_resource
;
1157 assert(start
+ count
<= Elements(dst
->states
));
1160 for (i
= 0; i
< count
; i
++)
1161 pipe_surface_reference(&dst
->states
[start
+ i
], surfaces
[i
]);
1164 for (i
= 0; i
< count
; i
++)
1165 pipe_surface_reference(&dst
->states
[start
+ i
], NULL
);
1168 if (dst
->count
<= start
+ count
) {
1174 while (count
> 0 && !dst
->states
[count
- 1])
1180 ilo
->dirty
|= ILO_DIRTY_CS_RESOURCE
;
1184 ilo_set_global_binding(struct pipe_context
*pipe
,
1185 unsigned start
, unsigned count
,
1186 struct pipe_resource
**resources
,
1189 struct ilo_context
*ilo
= ilo_context(pipe
);
1190 struct ilo_global_binding
*dst
= &ilo
->global_binding
;
1193 assert(start
+ count
<= Elements(dst
->resources
));
1196 for (i
= 0; i
< count
; i
++)
1197 pipe_resource_reference(&dst
->resources
[start
+ i
], resources
[i
]);
1200 for (i
= 0; i
< count
; i
++)
1201 pipe_resource_reference(&dst
->resources
[start
+ i
], NULL
);
1204 if (dst
->count
<= start
+ count
) {
1210 while (count
> 0 && !dst
->resources
[count
- 1])
1216 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1220 * Initialize state-related functions.
1223 ilo_init_state_functions(struct ilo_context
*ilo
)
1225 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1227 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1228 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1229 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1230 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1231 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1232 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1233 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1234 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1235 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1236 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1237 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1238 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1239 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1240 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1241 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1242 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1243 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1244 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1245 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1246 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1247 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1248 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1249 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1250 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1251 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1252 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1253 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1255 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1256 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1257 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1258 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1259 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1260 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1261 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1262 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1263 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1264 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1265 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1266 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1267 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1268 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1269 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1270 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1272 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1273 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1274 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1276 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1277 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1279 ilo
->base
.create_surface
= ilo_create_surface
;
1280 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1282 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1283 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1284 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1285 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1286 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1290 ilo_init_states(struct ilo_context
*ilo
)
1292 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1294 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1295 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1297 ilo
->dirty
= ILO_DIRTY_ALL
;
1301 ilo_cleanup_states(struct ilo_context
*ilo
)
1305 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1306 if (ilo
->vb
.enabled_mask
& (1 << i
))
1307 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1310 pipe_resource_reference(&ilo
->ib
.buffer
, NULL
);
1311 pipe_resource_reference(&ilo
->ib
.hw_resource
, NULL
);
1313 for (i
= 0; i
< ilo
->so
.count
; i
++)
1314 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1316 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1317 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1318 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1319 pipe_sampler_view_reference(&view
, NULL
);
1322 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1323 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1324 pipe_resource_reference(&cbuf
->resource
, NULL
);
1328 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1329 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1331 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1332 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1334 if (ilo
->fb
.state
.zsbuf
)
1335 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1337 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1338 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1340 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1341 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1345 * Mark all states that have the resource dirty.
1348 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1349 const struct pipe_resource
*res
)
1351 uint32_t states
= 0;
1354 if (res
->target
== PIPE_BUFFER
) {
1355 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1358 const unsigned idx
= u_bit_scan(&vb_mask
);
1360 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1361 states
|= ILO_DIRTY_VB
;
1366 if (ilo
->ib
.buffer
== res
) {
1367 states
|= ILO_DIRTY_IB
;
1370 * finalize_index_buffer() has an optimization that clears
1371 * ILO_DIRTY_IB when the HW states do not change. However, it fails
1372 * to flush the VF cache when the HW states do not change, but the
1373 * contents of the IB has changed. Here, we set the index size to an
1374 * invalid value to avoid the optimization.
1376 ilo
->ib
.hw_index_size
= 0;
1379 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1380 if (ilo
->so
.states
[i
]->buffer
== res
) {
1381 states
|= ILO_DIRTY_SO
;
1387 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1388 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1389 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1391 if (view
->texture
== res
) {
1392 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1393 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VIEW_VS
,
1394 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_VIEW_FS
,
1395 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_VIEW_GS
,
1396 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_VIEW_CS
,
1399 states
|= view_dirty_bits
[sh
];
1404 if (res
->target
== PIPE_BUFFER
) {
1405 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1406 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1408 if (cbuf
->resource
== res
) {
1409 states
|= ILO_DIRTY_CBUF
;
1416 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1417 if (ilo
->resource
.states
[i
]->texture
== res
) {
1418 states
|= ILO_DIRTY_RESOURCE
;
1424 if (res
->target
!= PIPE_BUFFER
) {
1425 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1426 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1427 states
|= ILO_DIRTY_FB
;
1432 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1433 states
|= ILO_DIRTY_FB
;
1436 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1437 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1438 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1439 states
|= ILO_DIRTY_CS_RESOURCE
;
1444 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1445 if (ilo
->global_binding
.resources
[i
] == res
) {
1446 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1451 ilo
->dirty
|= states
;