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_CONSTANT_BUFFER
))
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;
126 ilo
->cbuf
[sh
].count
= util_last_bit(ilo
->cbuf
[sh
].enabled_mask
);
131 finalize_index_buffer(struct ilo_context
*ilo
)
133 struct pipe_resource
*res
;
134 unsigned offset
, size
;
135 bool uploaded
= false;
137 if (!ilo
->draw
->indexed
)
140 res
= ilo
->ib
.resource
;
141 offset
= ilo
->ib
.state
.index_size
* ilo
->draw
->start
;
142 size
= ilo
->ib
.state
.index_size
* ilo
->draw
->count
;
144 if (ilo
->ib
.state
.user_buffer
) {
145 u_upload_data(ilo
->uploader
, 0, size
,
146 ilo
->ib
.state
.user_buffer
+ offset
, &offset
, &res
);
149 else if (unlikely(ilo
->ib
.state
.offset
% ilo
->ib
.state
.index_size
)) {
150 u_upload_buffer(ilo
->uploader
, 0, ilo
->ib
.state
.offset
+ offset
, size
,
151 ilo
->ib
.state
.buffer
, &offset
, &res
);
156 ilo
->ib
.resource
= res
;
158 assert(offset
% ilo
->ib
.state
.index_size
== 0);
159 ilo
->ib
.draw_start_offset
= offset
/ ilo
->ib
.state
.index_size
;
161 /* could be negative */
162 ilo
->ib
.draw_start_offset
-= ilo
->draw
->start
;
164 ilo
->dirty
|= ILO_DIRTY_INDEX_BUFFER
;
169 * Finalize states. Some states depend on other states and are
170 * incomplete/invalid until finalized.
173 ilo_finalize_3d_states(struct ilo_context
*ilo
,
174 const struct pipe_draw_info
*draw
)
178 finalize_shader_states(ilo
);
179 finalize_constant_buffers(ilo
);
180 finalize_index_buffer(ilo
);
182 u_upload_unmap(ilo
->uploader
);
186 ilo_create_blend_state(struct pipe_context
*pipe
,
187 const struct pipe_blend_state
*state
)
189 struct ilo_context
*ilo
= ilo_context(pipe
);
190 struct ilo_blend_state
*blend
;
192 blend
= MALLOC_STRUCT(ilo_blend_state
);
195 ilo_gpe_init_blend(ilo
->dev
, state
, blend
);
201 ilo_bind_blend_state(struct pipe_context
*pipe
, void *state
)
203 struct ilo_context
*ilo
= ilo_context(pipe
);
207 ilo
->dirty
|= ILO_DIRTY_BLEND
;
211 ilo_delete_blend_state(struct pipe_context
*pipe
, void *state
)
217 ilo_create_sampler_state(struct pipe_context
*pipe
,
218 const struct pipe_sampler_state
*state
)
220 struct ilo_context
*ilo
= ilo_context(pipe
);
221 struct ilo_sampler_cso
*sampler
;
223 sampler
= MALLOC_STRUCT(ilo_sampler_cso
);
226 ilo_gpe_init_sampler_cso(ilo
->dev
, state
, sampler
);
232 bind_samplers(struct ilo_context
*ilo
,
233 unsigned shader
, unsigned start
, unsigned count
,
234 void **samplers
, bool unbind_old
)
236 const struct ilo_sampler_cso
**dst
= ilo
->sampler
[shader
].cso
;
239 assert(start
+ count
<= Elements(ilo
->sampler
[shader
].cso
));
247 for (i
= 0; i
< start
; i
++)
249 for (; i
< start
+ count
; i
++)
250 dst
[i
] = samplers
[i
- start
];
251 for (; i
< ilo
->sampler
[shader
].count
; i
++)
254 ilo
->sampler
[shader
].count
= start
+ count
;
261 for (i
= 0; i
< count
; i
++)
262 dst
[i
] = samplers
[i
];
265 for (i
= 0; i
< count
; i
++)
269 if (ilo
->sampler
[shader
].count
<= start
+ count
) {
272 while (count
> 0 && !ilo
->sampler
[shader
].cso
[count
- 1])
275 ilo
->sampler
[shader
].count
= count
;
280 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
281 unsigned num_samplers
,
284 struct ilo_context
*ilo
= ilo_context(pipe
);
286 bind_samplers(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_samplers
, samplers
, true);
287 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLERS
;
291 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
292 unsigned num_samplers
,
295 struct ilo_context
*ilo
= ilo_context(pipe
);
297 bind_samplers(ilo
, PIPE_SHADER_VERTEX
, 0, num_samplers
, samplers
, true);
298 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLERS
;
302 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
303 unsigned num_samplers
,
306 struct ilo_context
*ilo
= ilo_context(pipe
);
308 bind_samplers(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_samplers
, samplers
, true);
309 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLERS
;
313 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
315 unsigned num_samplers
,
318 struct ilo_context
*ilo
= ilo_context(pipe
);
320 bind_samplers(ilo
, PIPE_SHADER_COMPUTE
,
321 start_slot
, num_samplers
, samplers
, false);
322 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLERS
;
326 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
332 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
333 const struct pipe_rasterizer_state
*state
)
335 struct ilo_context
*ilo
= ilo_context(pipe
);
336 struct ilo_rasterizer_state
*rast
;
338 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
341 rast
->state
= *state
;
342 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
348 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
350 struct ilo_context
*ilo
= ilo_context(pipe
);
352 ilo
->rasterizer
= state
;
354 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
358 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
364 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
365 const struct pipe_depth_stencil_alpha_state
*state
)
367 struct ilo_context
*ilo
= ilo_context(pipe
);
368 struct ilo_dsa_state
*dsa
;
370 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
373 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
379 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
381 struct ilo_context
*ilo
= ilo_context(pipe
);
385 ilo
->dirty
|= ILO_DIRTY_DEPTH_STENCIL_ALPHA
;
389 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
395 ilo_create_fs_state(struct pipe_context
*pipe
,
396 const struct pipe_shader_state
*state
)
398 struct ilo_context
*ilo
= ilo_context(pipe
);
399 struct ilo_shader_state
*shader
;
401 shader
= ilo_shader_create_fs(ilo
->dev
, state
, ilo
);
404 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
410 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
412 struct ilo_context
*ilo
= ilo_context(pipe
);
416 ilo
->dirty
|= ILO_DIRTY_FS
;
420 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
422 struct ilo_context
*ilo
= ilo_context(pipe
);
423 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
425 ilo_shader_cache_remove(ilo
->shader_cache
, fs
);
426 ilo_shader_destroy(fs
);
430 ilo_create_vs_state(struct pipe_context
*pipe
,
431 const struct pipe_shader_state
*state
)
433 struct ilo_context
*ilo
= ilo_context(pipe
);
434 struct ilo_shader_state
*shader
;
436 shader
= ilo_shader_create_vs(ilo
->dev
, state
, ilo
);
439 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
445 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
447 struct ilo_context
*ilo
= ilo_context(pipe
);
451 ilo
->dirty
|= ILO_DIRTY_VS
;
455 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
457 struct ilo_context
*ilo
= ilo_context(pipe
);
458 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
460 ilo_shader_cache_remove(ilo
->shader_cache
, vs
);
461 ilo_shader_destroy(vs
);
465 ilo_create_gs_state(struct pipe_context
*pipe
,
466 const struct pipe_shader_state
*state
)
468 struct ilo_context
*ilo
= ilo_context(pipe
);
469 struct ilo_shader_state
*shader
;
471 shader
= ilo_shader_create_gs(ilo
->dev
, state
, ilo
);
474 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
480 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
482 struct ilo_context
*ilo
= ilo_context(pipe
);
486 ilo
->dirty
|= ILO_DIRTY_GS
;
490 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
492 struct ilo_context
*ilo
= ilo_context(pipe
);
493 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
495 ilo_shader_cache_remove(ilo
->shader_cache
, gs
);
496 ilo_shader_destroy(gs
);
500 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
501 unsigned num_elements
,
502 const struct pipe_vertex_element
*elements
)
504 struct ilo_context
*ilo
= ilo_context(pipe
);
505 struct ilo_ve_state
*ve
;
507 ve
= MALLOC_STRUCT(ilo_ve_state
);
510 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
516 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
518 struct ilo_context
*ilo
= ilo_context(pipe
);
522 ilo
->dirty
|= ILO_DIRTY_VERTEX_ELEMENTS
;
526 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
528 struct ilo_ve_state
*ve
= state
;
534 ilo_set_blend_color(struct pipe_context
*pipe
,
535 const struct pipe_blend_color
*state
)
537 struct ilo_context
*ilo
= ilo_context(pipe
);
539 ilo
->blend_color
= *state
;
541 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
545 ilo_set_stencil_ref(struct pipe_context
*pipe
,
546 const struct pipe_stencil_ref
*state
)
548 struct ilo_context
*ilo
= ilo_context(pipe
);
550 ilo
->stencil_ref
= *state
;
552 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
556 ilo_set_sample_mask(struct pipe_context
*pipe
,
557 unsigned sample_mask
)
559 struct ilo_context
*ilo
= ilo_context(pipe
);
561 ilo
->sample_mask
= sample_mask
;
563 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
567 ilo_set_clip_state(struct pipe_context
*pipe
,
568 const struct pipe_clip_state
*state
)
570 struct ilo_context
*ilo
= ilo_context(pipe
);
574 ilo
->dirty
|= ILO_DIRTY_CLIP
;
578 ilo_set_constant_buffer(struct pipe_context
*pipe
,
579 uint shader
, uint index
,
580 struct pipe_constant_buffer
*state
)
582 struct ilo_context
*ilo
= ilo_context(pipe
);
583 struct ilo_cbuf_cso
*cbuf
;
585 assert(shader
< Elements(ilo
->cbuf
));
586 assert(index
< Elements(ilo
->cbuf
[shader
].cso
));
588 cbuf
= &ilo
->cbuf
[shader
].cso
[index
];
591 pipe_resource_reference(&cbuf
->resource
, state
->buffer
);
594 const enum pipe_format elem_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
596 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
597 ilo_buffer(cbuf
->resource
),
598 state
->buffer_offset
, state
->buffer_size
,
599 util_format_get_blocksize(elem_format
), elem_format
,
600 false, false, &cbuf
->surface
);
602 cbuf
->user_buffer
= NULL
;
603 cbuf
->user_buffer_size
= 0;
606 assert(state
->user_buffer
);
608 cbuf
->surface
.bo
= NULL
;
610 /* state->buffer_offset does not apply for user buffer */
611 cbuf
->user_buffer
= state
->user_buffer
;
612 cbuf
->user_buffer_size
= state
->buffer_size
;
615 ilo
->cbuf
[shader
].enabled_mask
|= 1 << index
;
618 pipe_resource_reference(&cbuf
->resource
, NULL
);
619 cbuf
->surface
.bo
= NULL
;
621 cbuf
->user_buffer
= NULL
;
622 cbuf
->user_buffer_size
= 0;
624 ilo
->cbuf
[shader
].enabled_mask
&= ~(1 << index
);
627 ilo
->dirty
|= ILO_DIRTY_CONSTANT_BUFFER
;
631 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
632 const struct pipe_framebuffer_state
*state
)
634 struct ilo_context
*ilo
= ilo_context(pipe
);
636 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
639 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
640 else if (state
->zsbuf
)
641 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
643 ilo
->fb
.num_samples
= 1;
645 if (!ilo
->fb
.num_samples
)
646 ilo
->fb
.num_samples
= 1;
648 ilo
->dirty
|= ILO_DIRTY_FRAMEBUFFER
;
652 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
653 const struct pipe_poly_stipple
*state
)
655 struct ilo_context
*ilo
= ilo_context(pipe
);
657 ilo
->poly_stipple
= *state
;
659 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
663 ilo_set_scissor_states(struct pipe_context
*pipe
,
665 unsigned num_scissors
,
666 const struct pipe_scissor_state
*scissors
)
668 struct ilo_context
*ilo
= ilo_context(pipe
);
670 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
671 scissors
, &ilo
->scissor
);
673 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
677 ilo_set_viewport_states(struct pipe_context
*pipe
,
679 unsigned num_viewports
,
680 const struct pipe_viewport_state
*viewports
)
682 struct ilo_context
*ilo
= ilo_context(pipe
);
687 for (i
= 0; i
< num_viewports
; i
++) {
688 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
689 &ilo
->viewport
.cso
[start_slot
+ i
]);
692 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
693 ilo
->viewport
.count
= start_slot
+ num_viewports
;
695 /* need to save viewport 0 for util_blitter */
696 if (!start_slot
&& num_viewports
)
697 ilo
->viewport
.viewport0
= viewports
[0];
700 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
701 ilo
->viewport
.count
> start_slot
)
702 ilo
->viewport
.count
= start_slot
;
705 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
709 set_sampler_views(struct ilo_context
*ilo
,
710 unsigned shader
, unsigned start
, unsigned count
,
711 struct pipe_sampler_view
**views
, bool unset_old
)
713 struct pipe_sampler_view
**dst
= ilo
->view
[shader
].states
;
716 assert(start
+ count
<= Elements(ilo
->view
[shader
].states
));
724 for (i
= 0; i
< start
; i
++)
725 pipe_sampler_view_reference(&dst
[i
], NULL
);
726 for (; i
< start
+ count
; i
++)
727 pipe_sampler_view_reference(&dst
[i
], views
[i
- start
]);
728 for (; i
< ilo
->view
[shader
].count
; i
++)
729 pipe_sampler_view_reference(&dst
[i
], NULL
);
731 ilo
->view
[shader
].count
= start
+ count
;
738 for (i
= 0; i
< count
; i
++)
739 pipe_sampler_view_reference(&dst
[i
], views
[i
]);
742 for (i
= 0; i
< count
; i
++)
743 pipe_sampler_view_reference(&dst
[i
], NULL
);
746 if (ilo
->view
[shader
].count
<= start
+ count
) {
749 while (count
> 0 && !ilo
->view
[shader
].states
[count
- 1])
752 ilo
->view
[shader
].count
= count
;
757 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
759 struct pipe_sampler_view
**views
)
761 struct ilo_context
*ilo
= ilo_context(pipe
);
763 set_sampler_views(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
, true);
764 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
;
768 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
770 struct pipe_sampler_view
**views
)
772 struct ilo_context
*ilo
= ilo_context(pipe
);
774 set_sampler_views(ilo
, PIPE_SHADER_VERTEX
, 0, num_views
, views
, true);
775 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLER_VIEWS
;
779 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
781 struct pipe_sampler_view
**views
)
783 struct ilo_context
*ilo
= ilo_context(pipe
);
785 set_sampler_views(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
, true);
786 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
;
790 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
791 unsigned start_slot
, unsigned num_views
,
792 struct pipe_sampler_view
**views
)
794 struct ilo_context
*ilo
= ilo_context(pipe
);
796 set_sampler_views(ilo
, PIPE_SHADER_COMPUTE
,
797 start_slot
, num_views
, views
, false);
799 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
;
803 ilo_set_shader_resources(struct pipe_context
*pipe
,
804 unsigned start
, unsigned count
,
805 struct pipe_surface
**surfaces
)
807 struct ilo_context
*ilo
= ilo_context(pipe
);
808 struct pipe_surface
**dst
= ilo
->resource
.states
;
811 assert(start
+ count
<= Elements(ilo
->resource
.states
));
815 for (i
= 0; i
< count
; i
++)
816 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
819 for (i
= 0; i
< count
; i
++)
820 pipe_surface_reference(&dst
[i
], NULL
);
823 if (ilo
->resource
.count
<= start
+ count
) {
826 while (count
> 0 && !ilo
->resource
.states
[count
- 1])
829 ilo
->resource
.count
= count
;
832 ilo
->dirty
|= ILO_DIRTY_SHADER_RESOURCES
;
836 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
837 unsigned start_slot
, unsigned num_buffers
,
838 const struct pipe_vertex_buffer
*buffers
)
840 struct ilo_context
*ilo
= ilo_context(pipe
);
843 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
845 for (i
= 0; i
< num_buffers
; i
++)
846 assert(!buffers
[i
].user_buffer
);
849 util_set_vertex_buffers_mask(ilo
->vb
.states
,
850 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
852 ilo
->dirty
|= ILO_DIRTY_VERTEX_BUFFERS
;
856 ilo_set_index_buffer(struct pipe_context
*pipe
,
857 const struct pipe_index_buffer
*state
)
859 struct ilo_context
*ilo
= ilo_context(pipe
);
862 pipe_resource_reference(&ilo
->ib
.state
.buffer
, state
->buffer
);
863 ilo
->ib
.state
.offset
= state
->offset
;
864 ilo
->ib
.state
.index_size
= state
->index_size
;
866 /* state->offset does not apply for user buffer */
867 ilo
->ib
.state
.user_buffer
= state
->user_buffer
;
870 * when there is no state->buffer or state->offset is misaligned,
871 * ilo_finalize_3d_states() will set these to the valid values
873 pipe_resource_reference(&ilo
->ib
.resource
, state
->buffer
);
874 ilo
->ib
.draw_start_offset
= state
->offset
/ state
->index_size
;
877 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
878 ilo
->ib
.state
.offset
= 0;
879 ilo
->ib
.state
.index_size
= 0;
880 ilo
->ib
.state
.user_buffer
= NULL
;
882 pipe_resource_reference(&ilo
->ib
.resource
, NULL
);
883 ilo
->ib
.draw_start_offset
= 0;
886 ilo
->dirty
|= ILO_DIRTY_INDEX_BUFFER
;
889 static struct pipe_stream_output_target
*
890 ilo_create_stream_output_target(struct pipe_context
*pipe
,
891 struct pipe_resource
*res
,
892 unsigned buffer_offset
,
893 unsigned buffer_size
)
895 struct pipe_stream_output_target
*target
;
897 target
= MALLOC_STRUCT(pipe_stream_output_target
);
900 pipe_reference_init(&target
->reference
, 1);
901 target
->buffer
= NULL
;
902 pipe_resource_reference(&target
->buffer
, res
);
903 target
->context
= pipe
;
904 target
->buffer_offset
= buffer_offset
;
905 target
->buffer_size
= buffer_size
;
911 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
912 unsigned num_targets
,
913 struct pipe_stream_output_target
**targets
,
914 unsigned append_bitmask
)
916 struct ilo_context
*ilo
= ilo_context(pipe
);
922 for (i
= 0; i
< num_targets
; i
++)
923 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
925 for (; i
< ilo
->so
.count
; i
++)
926 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
928 ilo
->so
.count
= num_targets
;
929 ilo
->so
.append_bitmask
= append_bitmask
;
931 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
933 ilo
->dirty
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
937 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
938 struct pipe_stream_output_target
*target
)
940 pipe_resource_reference(&target
->buffer
, NULL
);
944 static struct pipe_sampler_view
*
945 ilo_create_sampler_view(struct pipe_context
*pipe
,
946 struct pipe_resource
*res
,
947 const struct pipe_sampler_view
*templ
)
949 struct ilo_context
*ilo
= ilo_context(pipe
);
950 struct ilo_view_cso
*view
;
952 view
= MALLOC_STRUCT(ilo_view_cso
);
956 pipe_reference_init(&view
->base
.reference
, 1);
957 view
->base
.texture
= NULL
;
958 pipe_resource_reference(&view
->base
.texture
, res
);
959 view
->base
.context
= pipe
;
961 if (res
->target
== PIPE_BUFFER
) {
962 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
963 const unsigned first_elem
= templ
->u
.buf
.first_element
;
964 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
966 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
967 first_elem
* elem_size
, num_elems
* elem_size
,
968 elem_size
, templ
->format
, false, false, &view
->surface
);
971 struct ilo_texture
*tex
= ilo_texture(res
);
973 /* warn about degraded performance because of a missing binding flag */
974 if (tex
->tiling
== INTEL_TILING_NONE
&&
975 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
976 ilo_warn("creating sampler view for a resource "
977 "not created for sampling\n");
980 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
982 templ
->u
.tex
.first_level
,
983 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
984 templ
->u
.tex
.first_layer
,
985 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
986 false, false, &view
->surface
);
993 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
994 struct pipe_sampler_view
*view
)
996 pipe_resource_reference(&view
->texture
, NULL
);
1000 static struct pipe_surface
*
1001 ilo_create_surface(struct pipe_context
*pipe
,
1002 struct pipe_resource
*res
,
1003 const struct pipe_surface
*templ
)
1005 struct ilo_context
*ilo
= ilo_context(pipe
);
1006 struct ilo_surface_cso
*surf
;
1008 surf
= MALLOC_STRUCT(ilo_surface_cso
);
1011 surf
->base
= *templ
;
1012 pipe_reference_init(&surf
->base
.reference
, 1);
1013 surf
->base
.texture
= NULL
;
1014 pipe_resource_reference(&surf
->base
.texture
, res
);
1016 surf
->base
.context
= pipe
;
1017 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
1018 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
1020 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
1024 assert(res
->target
!= PIPE_BUFFER
);
1027 * classic i965 sets render_cache_rw for constant buffers and sol
1028 * surfaces but not render buffers. Why?
1030 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
1031 templ
->format
, templ
->u
.tex
.level
, 1,
1032 templ
->u
.tex
.first_layer
,
1033 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1034 true, true, &surf
->u
.rt
);
1037 assert(res
->target
!= PIPE_BUFFER
);
1039 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
1040 templ
->format
, templ
->u
.tex
.level
,
1041 templ
->u
.tex
.first_layer
,
1042 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
1050 ilo_surface_destroy(struct pipe_context
*pipe
,
1051 struct pipe_surface
*surface
)
1053 pipe_resource_reference(&surface
->texture
, NULL
);
1058 ilo_create_compute_state(struct pipe_context
*pipe
,
1059 const struct pipe_compute_state
*state
)
1061 struct ilo_context
*ilo
= ilo_context(pipe
);
1062 struct ilo_shader_state
*shader
;
1064 shader
= ilo_shader_create_cs(ilo
->dev
, state
, ilo
);
1067 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
1073 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
1075 struct ilo_context
*ilo
= ilo_context(pipe
);
1079 ilo
->dirty
|= ILO_DIRTY_COMPUTE
;
1083 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
1085 struct ilo_context
*ilo
= ilo_context(pipe
);
1086 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
1088 ilo_shader_cache_remove(ilo
->shader_cache
, cs
);
1089 ilo_shader_destroy(cs
);
1093 ilo_set_compute_resources(struct pipe_context
*pipe
,
1094 unsigned start
, unsigned count
,
1095 struct pipe_surface
**surfaces
)
1097 struct ilo_context
*ilo
= ilo_context(pipe
);
1098 struct pipe_surface
**dst
= ilo
->cs_resource
.states
;
1101 assert(start
+ count
<= Elements(ilo
->cs_resource
.states
));
1105 for (i
= 0; i
< count
; i
++)
1106 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
1109 for (i
= 0; i
< count
; i
++)
1110 pipe_surface_reference(&dst
[i
], NULL
);
1113 if (ilo
->cs_resource
.count
<= start
+ count
) {
1116 while (count
> 0 && !ilo
->cs_resource
.states
[count
- 1])
1119 ilo
->cs_resource
.count
= count
;
1122 ilo
->dirty
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1126 ilo_set_global_binding(struct pipe_context
*pipe
,
1127 unsigned start
, unsigned count
,
1128 struct pipe_resource
**resources
,
1131 struct ilo_context
*ilo
= ilo_context(pipe
);
1132 struct pipe_resource
**dst
= ilo
->global_binding
.resources
;
1135 assert(start
+ count
<= Elements(ilo
->global_binding
.resources
));
1139 for (i
= 0; i
< count
; i
++)
1140 pipe_resource_reference(&dst
[i
], resources
[i
]);
1143 for (i
= 0; i
< count
; i
++)
1144 pipe_resource_reference(&dst
[i
], NULL
);
1147 if (ilo
->global_binding
.count
<= start
+ count
) {
1150 while (count
> 0 && !ilo
->global_binding
.resources
[count
- 1])
1153 ilo
->global_binding
.count
= count
;
1156 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1160 * Initialize state-related functions.
1163 ilo_init_state_functions(struct ilo_context
*ilo
)
1165 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1167 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1168 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1169 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1170 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1171 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1172 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1173 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1174 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1175 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1176 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1177 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1178 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1179 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1180 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1181 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1182 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1183 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1184 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1185 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1186 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1187 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1188 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1189 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1190 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1191 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1192 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1193 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1195 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1196 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1197 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1198 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1199 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1200 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1201 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1202 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1203 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1204 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1205 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1206 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1207 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1208 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1209 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1210 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1212 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1213 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1214 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1216 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1217 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1219 ilo
->base
.create_surface
= ilo_create_surface
;
1220 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1222 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1223 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1224 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1225 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1226 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1230 ilo_init_states(struct ilo_context
*ilo
)
1232 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1234 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1235 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1237 ilo
->dirty
= ILO_DIRTY_ALL
;
1241 ilo_cleanup_states(struct ilo_context
*ilo
)
1245 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1246 if (ilo
->vb
.enabled_mask
& (1 << i
))
1247 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1250 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
1251 pipe_resource_reference(&ilo
->ib
.resource
, NULL
);
1253 for (i
= 0; i
< ilo
->so
.count
; i
++)
1254 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1256 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1257 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1258 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1259 pipe_sampler_view_reference(&view
, NULL
);
1262 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1263 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1264 pipe_resource_reference(&cbuf
->resource
, NULL
);
1268 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1269 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1271 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1272 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1274 if (ilo
->fb
.state
.zsbuf
)
1275 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1277 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1278 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1280 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1281 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1285 * Mark all states that have the resource dirty.
1288 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1289 const struct pipe_resource
*res
)
1291 uint32_t states
= 0;
1294 if (res
->target
== PIPE_BUFFER
) {
1295 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1298 const unsigned idx
= u_bit_scan(&vb_mask
);
1300 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1301 states
|= ILO_DIRTY_VERTEX_BUFFERS
;
1306 if (ilo
->ib
.state
.buffer
== res
)
1307 states
|= ILO_DIRTY_INDEX_BUFFER
;
1309 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1310 if (ilo
->so
.states
[i
]->buffer
== res
) {
1311 states
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
1317 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1318 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1319 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1321 if (view
->texture
== res
) {
1322 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1323 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VERTEX_SAMPLER_VIEWS
,
1324 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
,
1325 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
,
1326 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
,
1329 states
|= view_dirty_bits
[sh
];
1334 if (res
->target
== PIPE_BUFFER
) {
1335 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1336 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1338 if (cbuf
->resource
== res
) {
1339 states
|= ILO_DIRTY_CONSTANT_BUFFER
;
1346 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1347 if (ilo
->resource
.states
[i
]->texture
== res
) {
1348 states
|= ILO_DIRTY_SHADER_RESOURCES
;
1354 if (res
->target
!= PIPE_BUFFER
) {
1355 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1356 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1357 states
|= ILO_DIRTY_FRAMEBUFFER
;
1362 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1363 states
|= ILO_DIRTY_FRAMEBUFFER
;
1366 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1367 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1368 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1369 states
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1374 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1375 if (ilo
->global_binding
.resources
[i
] == res
) {
1376 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1381 ilo
->dirty
|= states
;