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"
31 #include "ilo_context.h"
32 #include "ilo_resource.h"
33 #include "ilo_shader.h"
34 #include "ilo_state.h"
37 * We simply remember the pipe states here and derive HW commands/states from
38 * them later. We could do better by deriving (some of the) HW
39 * commands/states directly.
43 finalize_shader_states(struct ilo_context
*ilo
)
45 /* this table is ugly and is a burden to maintain.. */
47 struct ilo_shader_state
*state
;
48 struct ilo_shader
*prev_shader
;
49 uint32_t prev_cache_seqno
;
52 } sh
[PIPE_SHADER_TYPES
] = {
53 [PIPE_SHADER_VERTEX
] = {
55 .prev_shader
= (ilo
->vs
) ? ilo
->vs
->shader
: NULL
,
56 .prev_cache_seqno
= (ilo
->vs
) ? ilo
->vs
->shader
->cache_seqno
: 0,
57 .dirty
= ILO_DIRTY_VS
,
58 .deps
= ILO_DIRTY_VERTEX_SAMPLER_VIEWS
|
61 [PIPE_SHADER_FRAGMENT
] = {
63 .prev_shader
= (ilo
->fs
) ? ilo
->fs
->shader
: NULL
,
64 .prev_cache_seqno
= (ilo
->fs
) ? ilo
->fs
->shader
->cache_seqno
: 0,
65 .dirty
= ILO_DIRTY_FS
,
66 .deps
= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
|
67 ILO_DIRTY_RASTERIZER
|
68 ILO_DIRTY_FRAMEBUFFER
,
70 [PIPE_SHADER_GEOMETRY
] = {
72 .prev_shader
= (ilo
->gs
) ? ilo
->gs
->shader
: NULL
,
73 .prev_cache_seqno
= (ilo
->gs
) ? ilo
->gs
->shader
->cache_seqno
: 0,
74 .dirty
= ILO_DIRTY_GS
,
75 .deps
= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
|
79 [PIPE_SHADER_COMPUTE
] = {
82 .prev_cache_seqno
= 0,
87 struct ilo_shader
*shaders
[PIPE_SHADER_TYPES
];
88 int num_shaders
= 0, i
;
90 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
95 /* switch variant if the shader or the states it depends on changed */
96 if (ilo
->dirty
& (sh
[i
].dirty
| sh
[i
].deps
)) {
97 struct ilo_shader_variant variant
;
99 ilo_shader_variant_init(&variant
, &sh
[i
].state
->info
, ilo
);
100 ilo_shader_state_use_variant(sh
[i
].state
, &variant
);
103 shaders
[num_shaders
++] = sh
[i
].state
->shader
;
106 ilo_shader_cache_set(ilo
->shader_cache
, shaders
, num_shaders
);
108 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
114 * mark the shader state dirty if
116 * - a new variant is selected, or
117 * - the kernel is uploaded to a different bo
119 if (sh
[i
].state
->shader
!= sh
[i
].prev_shader
||
120 sh
[i
].state
->shader
->cache_seqno
!= sh
[i
].prev_cache_seqno
)
121 ilo
->dirty
|= sh
[i
].dirty
;
126 finalize_constant_buffers(struct ilo_context
*ilo
)
130 if (!(ilo
->dirty
& ILO_DIRTY_CONSTANT_BUFFER
))
133 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
134 int last_cbuf
= Elements(ilo
->cbuf
[sh
].cso
) - 1;
136 /* find the last cbuf */
137 while (last_cbuf
>= 0 &&
138 !ilo
->cbuf
[sh
].cso
[last_cbuf
].resource
)
141 ilo
->cbuf
[sh
].count
= last_cbuf
+ 1;
146 * Finalize states. Some states depend on other states and are
147 * incomplete/invalid until finalized.
150 ilo_finalize_states(struct ilo_context
*ilo
)
152 finalize_shader_states(ilo
);
153 finalize_constant_buffers(ilo
);
157 ilo_create_blend_state(struct pipe_context
*pipe
,
158 const struct pipe_blend_state
*state
)
160 struct ilo_context
*ilo
= ilo_context(pipe
);
161 struct ilo_blend_state
*blend
;
163 blend
= MALLOC_STRUCT(ilo_blend_state
);
166 ilo_gpe_init_blend(ilo
->dev
, state
, blend
);
172 ilo_bind_blend_state(struct pipe_context
*pipe
, void *state
)
174 struct ilo_context
*ilo
= ilo_context(pipe
);
178 ilo
->dirty
|= ILO_DIRTY_BLEND
;
182 ilo_delete_blend_state(struct pipe_context
*pipe
, void *state
)
188 ilo_create_sampler_state(struct pipe_context
*pipe
,
189 const struct pipe_sampler_state
*state
)
191 struct ilo_context
*ilo
= ilo_context(pipe
);
192 struct ilo_sampler_cso
*sampler
;
194 sampler
= MALLOC_STRUCT(ilo_sampler_cso
);
197 ilo_gpe_init_sampler_cso(ilo
->dev
, state
, sampler
);
203 bind_samplers(struct ilo_context
*ilo
,
204 unsigned shader
, unsigned start
, unsigned count
,
205 void **samplers
, bool unbind_old
)
207 const struct ilo_sampler_cso
**dst
= ilo
->sampler
[shader
].cso
;
210 assert(start
+ count
<= Elements(ilo
->sampler
[shader
].cso
));
218 for (i
= 0; i
< start
; i
++)
220 for (; i
< start
+ count
; i
++)
221 dst
[i
] = samplers
[i
- start
];
222 for (; i
< ilo
->sampler
[shader
].count
; i
++)
225 ilo
->sampler
[shader
].count
= start
+ count
;
232 for (i
= 0; i
< count
; i
++)
233 dst
[i
] = samplers
[i
];
236 for (i
= 0; i
< count
; i
++)
240 if (ilo
->sampler
[shader
].count
<= start
+ count
) {
243 while (count
> 0 && !ilo
->sampler
[shader
].cso
[count
- 1])
246 ilo
->sampler
[shader
].count
= count
;
251 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
252 unsigned num_samplers
,
255 struct ilo_context
*ilo
= ilo_context(pipe
);
257 bind_samplers(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_samplers
, samplers
, true);
258 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLERS
;
262 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
263 unsigned num_samplers
,
266 struct ilo_context
*ilo
= ilo_context(pipe
);
268 bind_samplers(ilo
, PIPE_SHADER_VERTEX
, 0, num_samplers
, samplers
, true);
269 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLERS
;
273 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
274 unsigned num_samplers
,
277 struct ilo_context
*ilo
= ilo_context(pipe
);
279 bind_samplers(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_samplers
, samplers
, true);
280 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLERS
;
284 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
286 unsigned num_samplers
,
289 struct ilo_context
*ilo
= ilo_context(pipe
);
291 bind_samplers(ilo
, PIPE_SHADER_COMPUTE
,
292 start_slot
, num_samplers
, samplers
, false);
293 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLERS
;
297 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
303 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
304 const struct pipe_rasterizer_state
*state
)
306 struct ilo_context
*ilo
= ilo_context(pipe
);
307 struct ilo_rasterizer_state
*rast
;
309 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
312 rast
->state
= *state
;
313 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
319 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
321 struct ilo_context
*ilo
= ilo_context(pipe
);
323 ilo
->rasterizer
= state
;
325 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
329 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
335 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
336 const struct pipe_depth_stencil_alpha_state
*state
)
338 struct ilo_context
*ilo
= ilo_context(pipe
);
339 struct ilo_dsa_state
*dsa
;
341 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
344 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
350 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
352 struct ilo_context
*ilo
= ilo_context(pipe
);
356 ilo
->dirty
|= ILO_DIRTY_DEPTH_STENCIL_ALPHA
;
360 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
366 ilo_create_fs_state(struct pipe_context
*pipe
,
367 const struct pipe_shader_state
*state
)
369 struct ilo_context
*ilo
= ilo_context(pipe
);
370 return ilo_shader_state_create(ilo
, PIPE_SHADER_FRAGMENT
, state
);
374 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
376 struct ilo_context
*ilo
= ilo_context(pipe
);
380 ilo
->dirty
|= ILO_DIRTY_FS
;
384 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
386 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
387 ilo_shader_state_destroy(fs
);
391 ilo_create_vs_state(struct pipe_context
*pipe
,
392 const struct pipe_shader_state
*state
)
394 struct ilo_context
*ilo
= ilo_context(pipe
);
395 return ilo_shader_state_create(ilo
, PIPE_SHADER_VERTEX
, state
);
399 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
401 struct ilo_context
*ilo
= ilo_context(pipe
);
405 ilo
->dirty
|= ILO_DIRTY_VS
;
409 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
411 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
412 ilo_shader_state_destroy(vs
);
416 ilo_create_gs_state(struct pipe_context
*pipe
,
417 const struct pipe_shader_state
*state
)
419 struct ilo_context
*ilo
= ilo_context(pipe
);
420 return ilo_shader_state_create(ilo
, PIPE_SHADER_GEOMETRY
, state
);
424 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
426 struct ilo_context
*ilo
= ilo_context(pipe
);
430 ilo
->dirty
|= ILO_DIRTY_GS
;
434 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
436 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
437 ilo_shader_state_destroy(gs
);
441 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
442 unsigned num_elements
,
443 const struct pipe_vertex_element
*elements
)
445 struct ilo_context
*ilo
= ilo_context(pipe
);
446 struct ilo_ve_state
*ve
;
448 ve
= MALLOC_STRUCT(ilo_ve_state
);
451 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
457 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
459 struct ilo_context
*ilo
= ilo_context(pipe
);
463 ilo
->dirty
|= ILO_DIRTY_VERTEX_ELEMENTS
;
467 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
469 struct ilo_ve_state
*ve
= state
;
475 ilo_set_blend_color(struct pipe_context
*pipe
,
476 const struct pipe_blend_color
*state
)
478 struct ilo_context
*ilo
= ilo_context(pipe
);
480 ilo
->blend_color
= *state
;
482 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
486 ilo_set_stencil_ref(struct pipe_context
*pipe
,
487 const struct pipe_stencil_ref
*state
)
489 struct ilo_context
*ilo
= ilo_context(pipe
);
491 ilo
->stencil_ref
= *state
;
493 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
497 ilo_set_sample_mask(struct pipe_context
*pipe
,
498 unsigned sample_mask
)
500 struct ilo_context
*ilo
= ilo_context(pipe
);
502 ilo
->sample_mask
= sample_mask
;
504 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
508 ilo_set_clip_state(struct pipe_context
*pipe
,
509 const struct pipe_clip_state
*state
)
511 struct ilo_context
*ilo
= ilo_context(pipe
);
515 ilo
->dirty
|= ILO_DIRTY_CLIP
;
519 ilo_set_constant_buffer(struct pipe_context
*pipe
,
520 uint shader
, uint index
,
521 struct pipe_constant_buffer
*buf
)
523 struct ilo_context
*ilo
= ilo_context(pipe
);
524 struct ilo_cbuf_cso
*cbuf
;
526 assert(shader
< Elements(ilo
->cbuf
));
527 assert(index
< Elements(ilo
->cbuf
[shader
].cso
));
529 cbuf
= &ilo
->cbuf
[shader
].cso
[index
];
532 const enum pipe_format elem_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
534 /* no PIPE_CAP_USER_CONSTANT_BUFFERS */
535 assert(!buf
->user_buffer
);
537 pipe_resource_reference(&cbuf
->resource
, buf
->buffer
);
539 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(buf
->buffer
),
540 buf
->buffer_offset
, buf
->buffer_size
,
541 util_format_get_blocksize(elem_format
), elem_format
,
542 false, false, &cbuf
->surface
);
545 pipe_resource_reference(&cbuf
->resource
, NULL
);
546 cbuf
->surface
.bo
= NULL
;
549 /* the correct value will be set in ilo_finalize_states() */
550 ilo
->cbuf
[shader
].count
= 0;
552 ilo
->dirty
|= ILO_DIRTY_CONSTANT_BUFFER
;
556 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
557 const struct pipe_framebuffer_state
*state
)
559 struct ilo_context
*ilo
= ilo_context(pipe
);
561 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
564 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
565 else if (state
->zsbuf
)
566 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
568 ilo
->fb
.num_samples
= 1;
570 if (!ilo
->fb
.num_samples
)
571 ilo
->fb
.num_samples
= 1;
573 ilo
->dirty
|= ILO_DIRTY_FRAMEBUFFER
;
577 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
578 const struct pipe_poly_stipple
*state
)
580 struct ilo_context
*ilo
= ilo_context(pipe
);
582 ilo
->poly_stipple
= *state
;
584 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
588 ilo_set_scissor_states(struct pipe_context
*pipe
,
590 unsigned num_scissors
,
591 const struct pipe_scissor_state
*scissors
)
593 struct ilo_context
*ilo
= ilo_context(pipe
);
595 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
596 scissors
, &ilo
->scissor
);
598 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
602 ilo_set_viewport_states(struct pipe_context
*pipe
,
604 unsigned num_viewports
,
605 const struct pipe_viewport_state
*viewports
)
607 struct ilo_context
*ilo
= ilo_context(pipe
);
612 for (i
= 0; i
< num_viewports
; i
++) {
613 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
614 &ilo
->viewport
.cso
[start_slot
+ i
]);
617 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
618 ilo
->viewport
.count
= start_slot
+ num_viewports
;
620 /* need to save viewport 0 for util_blitter */
621 if (!start_slot
&& num_viewports
)
622 ilo
->viewport
.viewport0
= viewports
[0];
625 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
626 ilo
->viewport
.count
> start_slot
)
627 ilo
->viewport
.count
= start_slot
;
630 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
634 set_sampler_views(struct ilo_context
*ilo
,
635 unsigned shader
, unsigned start
, unsigned count
,
636 struct pipe_sampler_view
**views
, bool unset_old
)
638 struct pipe_sampler_view
**dst
= ilo
->view
[shader
].states
;
641 assert(start
+ count
<= Elements(ilo
->view
[shader
].states
));
649 for (i
= 0; i
< start
; i
++)
650 pipe_sampler_view_reference(&dst
[i
], NULL
);
651 for (; i
< start
+ count
; i
++)
652 pipe_sampler_view_reference(&dst
[i
], views
[i
- start
]);
653 for (; i
< ilo
->view
[shader
].count
; i
++)
654 pipe_sampler_view_reference(&dst
[i
], NULL
);
656 ilo
->view
[shader
].count
= start
+ count
;
663 for (i
= 0; i
< count
; i
++)
664 pipe_sampler_view_reference(&dst
[i
], views
[i
]);
667 for (i
= 0; i
< count
; i
++)
668 pipe_sampler_view_reference(&dst
[i
], NULL
);
671 if (ilo
->view
[shader
].count
<= start
+ count
) {
674 while (count
> 0 && !ilo
->view
[shader
].states
[count
- 1])
677 ilo
->view
[shader
].count
= count
;
682 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
684 struct pipe_sampler_view
**views
)
686 struct ilo_context
*ilo
= ilo_context(pipe
);
688 set_sampler_views(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
, true);
689 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
;
693 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
695 struct pipe_sampler_view
**views
)
697 struct ilo_context
*ilo
= ilo_context(pipe
);
699 set_sampler_views(ilo
, PIPE_SHADER_VERTEX
, 0, num_views
, views
, true);
700 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLER_VIEWS
;
704 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
706 struct pipe_sampler_view
**views
)
708 struct ilo_context
*ilo
= ilo_context(pipe
);
710 set_sampler_views(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
, true);
711 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
;
715 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
716 unsigned start_slot
, unsigned num_views
,
717 struct pipe_sampler_view
**views
)
719 struct ilo_context
*ilo
= ilo_context(pipe
);
721 set_sampler_views(ilo
, PIPE_SHADER_COMPUTE
,
722 start_slot
, num_views
, views
, false);
724 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
;
728 ilo_set_shader_resources(struct pipe_context
*pipe
,
729 unsigned start
, unsigned count
,
730 struct pipe_surface
**surfaces
)
732 struct ilo_context
*ilo
= ilo_context(pipe
);
733 struct pipe_surface
**dst
= ilo
->resource
.states
;
736 assert(start
+ count
<= Elements(ilo
->resource
.states
));
740 for (i
= 0; i
< count
; i
++)
741 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
744 for (i
= 0; i
< count
; i
++)
745 pipe_surface_reference(&dst
[i
], NULL
);
748 if (ilo
->resource
.count
<= start
+ count
) {
751 while (count
> 0 && !ilo
->resource
.states
[count
- 1])
754 ilo
->resource
.count
= count
;
757 ilo
->dirty
|= ILO_DIRTY_SHADER_RESOURCES
;
761 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
762 unsigned start_slot
, unsigned num_buffers
,
763 const struct pipe_vertex_buffer
*buffers
)
765 struct ilo_context
*ilo
= ilo_context(pipe
);
768 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
770 for (i
= 0; i
< num_buffers
; i
++)
771 assert(!buffers
[i
].user_buffer
);
774 util_set_vertex_buffers_mask(ilo
->vb
.states
,
775 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
777 ilo
->dirty
|= ILO_DIRTY_VERTEX_BUFFERS
;
781 ilo_set_index_buffer(struct pipe_context
*pipe
,
782 const struct pipe_index_buffer
*state
)
784 struct ilo_context
*ilo
= ilo_context(pipe
);
787 /* no PIPE_CAP_USER_INDEX_BUFFERS */
788 assert(!state
->user_buffer
);
790 ilo
->ib
.state
.index_size
= state
->index_size
;
791 ilo
->ib
.state
.offset
= state
->offset
;
792 pipe_resource_reference(&ilo
->ib
.state
.buffer
, state
->buffer
);
793 ilo
->ib
.state
.user_buffer
= state
->user_buffer
;
796 ilo
->ib
.state
.index_size
= 0;
797 ilo
->ib
.state
.offset
= 0;
798 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
799 ilo
->ib
.state
.user_buffer
= NULL
;
802 ilo
->dirty
|= ILO_DIRTY_INDEX_BUFFER
;
805 static struct pipe_stream_output_target
*
806 ilo_create_stream_output_target(struct pipe_context
*pipe
,
807 struct pipe_resource
*res
,
808 unsigned buffer_offset
,
809 unsigned buffer_size
)
811 struct pipe_stream_output_target
*target
;
813 target
= MALLOC_STRUCT(pipe_stream_output_target
);
816 pipe_reference_init(&target
->reference
, 1);
817 target
->buffer
= NULL
;
818 pipe_resource_reference(&target
->buffer
, res
);
819 target
->context
= pipe
;
820 target
->buffer_offset
= buffer_offset
;
821 target
->buffer_size
= buffer_size
;
827 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
828 unsigned num_targets
,
829 struct pipe_stream_output_target
**targets
,
830 unsigned append_bitmask
)
832 struct ilo_context
*ilo
= ilo_context(pipe
);
838 for (i
= 0; i
< num_targets
; i
++)
839 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
841 for (; i
< ilo
->so
.count
; i
++)
842 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
844 ilo
->so
.count
= num_targets
;
845 ilo
->so
.append_bitmask
= append_bitmask
;
847 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
849 ilo
->dirty
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
853 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
854 struct pipe_stream_output_target
*target
)
856 pipe_resource_reference(&target
->buffer
, NULL
);
860 static struct pipe_sampler_view
*
861 ilo_create_sampler_view(struct pipe_context
*pipe
,
862 struct pipe_resource
*res
,
863 const struct pipe_sampler_view
*templ
)
865 struct ilo_context
*ilo
= ilo_context(pipe
);
866 struct ilo_view_cso
*view
;
868 view
= MALLOC_STRUCT(ilo_view_cso
);
872 pipe_reference_init(&view
->base
.reference
, 1);
873 view
->base
.texture
= NULL
;
874 pipe_resource_reference(&view
->base
.texture
, res
);
875 view
->base
.context
= pipe
;
877 if (res
->target
== PIPE_BUFFER
) {
878 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
879 const unsigned first_elem
= templ
->u
.buf
.first_element
;
880 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
882 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
883 first_elem
* elem_size
, num_elems
* elem_size
,
884 elem_size
, templ
->format
, false, false, &view
->surface
);
887 struct ilo_texture
*tex
= ilo_texture(res
);
889 /* warn about degraded performance because of a missing binding flag */
890 if (tex
->tiling
== INTEL_TILING_NONE
&&
891 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
892 ilo_warn("creating sampler view for a resource "
893 "not created for sampling\n");
896 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
898 templ
->u
.tex
.first_level
,
899 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
900 templ
->u
.tex
.first_layer
,
901 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
902 false, false, &view
->surface
);
909 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
910 struct pipe_sampler_view
*view
)
912 pipe_resource_reference(&view
->texture
, NULL
);
916 static struct pipe_surface
*
917 ilo_create_surface(struct pipe_context
*pipe
,
918 struct pipe_resource
*res
,
919 const struct pipe_surface
*templ
)
921 struct ilo_context
*ilo
= ilo_context(pipe
);
922 struct ilo_surface_cso
*surf
;
924 surf
= MALLOC_STRUCT(ilo_surface_cso
);
928 pipe_reference_init(&surf
->base
.reference
, 1);
929 surf
->base
.texture
= NULL
;
930 pipe_resource_reference(&surf
->base
.texture
, res
);
932 surf
->base
.context
= pipe
;
933 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
934 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
936 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
940 assert(res
->target
!= PIPE_BUFFER
);
943 * classic i965 sets render_cache_rw for constant buffers and sol
944 * surfaces but not render buffers. Why?
946 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
947 templ
->format
, templ
->u
.tex
.level
, 1,
948 templ
->u
.tex
.first_layer
,
949 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
950 true, true, &surf
->u
.rt
);
953 assert(res
->target
!= PIPE_BUFFER
);
955 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
956 templ
->format
, templ
->u
.tex
.level
,
957 templ
->u
.tex
.first_layer
,
958 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
966 ilo_surface_destroy(struct pipe_context
*pipe
,
967 struct pipe_surface
*surface
)
969 pipe_resource_reference(&surface
->texture
, NULL
);
974 ilo_create_compute_state(struct pipe_context
*pipe
,
975 const struct pipe_compute_state
*state
)
977 struct ilo_context
*ilo
= ilo_context(pipe
);
978 return ilo_shader_state_create(ilo
, PIPE_SHADER_COMPUTE
, state
);
982 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
984 struct ilo_context
*ilo
= ilo_context(pipe
);
988 ilo
->dirty
|= ILO_DIRTY_COMPUTE
;
992 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
994 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
995 ilo_shader_state_destroy(cs
);
999 ilo_set_compute_resources(struct pipe_context
*pipe
,
1000 unsigned start
, unsigned count
,
1001 struct pipe_surface
**surfaces
)
1003 struct ilo_context
*ilo
= ilo_context(pipe
);
1004 struct pipe_surface
**dst
= ilo
->cs_resource
.states
;
1007 assert(start
+ count
<= Elements(ilo
->cs_resource
.states
));
1011 for (i
= 0; i
< count
; i
++)
1012 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
1015 for (i
= 0; i
< count
; i
++)
1016 pipe_surface_reference(&dst
[i
], NULL
);
1019 if (ilo
->cs_resource
.count
<= start
+ count
) {
1022 while (count
> 0 && !ilo
->cs_resource
.states
[count
- 1])
1025 ilo
->cs_resource
.count
= count
;
1028 ilo
->dirty
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1032 ilo_set_global_binding(struct pipe_context
*pipe
,
1033 unsigned start
, unsigned count
,
1034 struct pipe_resource
**resources
,
1037 struct ilo_context
*ilo
= ilo_context(pipe
);
1038 struct pipe_resource
**dst
= ilo
->global_binding
.resources
;
1041 assert(start
+ count
<= Elements(ilo
->global_binding
.resources
));
1045 for (i
= 0; i
< count
; i
++)
1046 pipe_resource_reference(&dst
[i
], resources
[i
]);
1049 for (i
= 0; i
< count
; i
++)
1050 pipe_resource_reference(&dst
[i
], NULL
);
1053 if (ilo
->global_binding
.count
<= start
+ count
) {
1056 while (count
> 0 && !ilo
->global_binding
.resources
[count
- 1])
1059 ilo
->global_binding
.count
= count
;
1062 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1066 * Initialize state-related functions.
1069 ilo_init_state_functions(struct ilo_context
*ilo
)
1071 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1073 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1074 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1075 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1076 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1077 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1078 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1079 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1080 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1081 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1082 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1083 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1084 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1085 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1086 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1087 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1088 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1089 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1090 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1091 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1092 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1093 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1094 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1095 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1096 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1097 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1098 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1099 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1101 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1102 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1103 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1104 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1105 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1106 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1107 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1108 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1109 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1110 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1111 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1112 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1113 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1114 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1115 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1116 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1118 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1119 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1120 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1122 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1123 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1125 ilo
->base
.create_surface
= ilo_create_surface
;
1126 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1128 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1129 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1130 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1131 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1132 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1136 ilo_init_states(struct ilo_context
*ilo
)
1138 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1140 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1141 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1143 ilo
->dirty
= ILO_DIRTY_ALL
;
1147 ilo_cleanup_states(struct ilo_context
*ilo
)
1151 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1152 if (ilo
->vb
.enabled_mask
& (1 << i
))
1153 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1156 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
1158 for (i
= 0; i
< ilo
->so
.count
; i
++)
1159 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1161 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1162 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1163 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1164 pipe_sampler_view_reference(&view
, NULL
);
1167 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1168 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1169 pipe_resource_reference(&cbuf
->resource
, NULL
);
1173 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1174 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1176 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1177 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1179 if (ilo
->fb
.state
.zsbuf
)
1180 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1182 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1183 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1185 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1186 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1190 * Mark all states that have the resource dirty.
1193 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1194 const struct pipe_resource
*res
)
1196 uint32_t states
= 0;
1199 if (res
->target
== PIPE_BUFFER
) {
1200 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1203 const unsigned idx
= u_bit_scan(&vb_mask
);
1205 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1206 states
|= ILO_DIRTY_VERTEX_BUFFERS
;
1211 if (ilo
->ib
.state
.buffer
== res
)
1212 states
|= ILO_DIRTY_INDEX_BUFFER
;
1214 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1215 if (ilo
->so
.states
[i
]->buffer
== res
) {
1216 states
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
1222 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1223 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1224 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1226 if (view
->texture
== res
) {
1227 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1228 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VERTEX_SAMPLER_VIEWS
,
1229 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
,
1230 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
,
1231 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
,
1234 states
|= view_dirty_bits
[sh
];
1239 if (res
->target
== PIPE_BUFFER
) {
1240 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1241 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1243 if (cbuf
->resource
== res
) {
1244 states
|= ILO_DIRTY_CONSTANT_BUFFER
;
1251 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1252 if (ilo
->resource
.states
[i
]->texture
== res
) {
1253 states
|= ILO_DIRTY_SHADER_RESOURCES
;
1259 if (res
->target
!= PIPE_BUFFER
) {
1260 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1261 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1262 states
|= ILO_DIRTY_FRAMEBUFFER
;
1267 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1268 states
|= ILO_DIRTY_FRAMEBUFFER
;
1271 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1272 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1273 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1274 states
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1279 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1280 if (ilo
->global_binding
.resources
[i
] == res
) {
1281 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1286 ilo
->dirty
|= states
;