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 pipe_resource_reference(&cbuf
->resource
, buf
->buffer
);
536 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(buf
->buffer
),
537 buf
->buffer_offset
, buf
->buffer_size
,
538 util_format_get_blocksize(elem_format
), elem_format
,
539 false, false, &cbuf
->surface
);
542 pipe_resource_reference(&cbuf
->resource
, NULL
);
543 cbuf
->surface
.bo
= NULL
;
546 /* the correct value will be set in ilo_finalize_states() */
547 ilo
->cbuf
[shader
].count
= 0;
549 ilo
->dirty
|= ILO_DIRTY_CONSTANT_BUFFER
;
553 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
554 const struct pipe_framebuffer_state
*state
)
556 struct ilo_context
*ilo
= ilo_context(pipe
);
558 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
561 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
562 else if (state
->zsbuf
)
563 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
565 ilo
->fb
.num_samples
= 1;
567 if (!ilo
->fb
.num_samples
)
568 ilo
->fb
.num_samples
= 1;
570 ilo
->dirty
|= ILO_DIRTY_FRAMEBUFFER
;
574 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
575 const struct pipe_poly_stipple
*state
)
577 struct ilo_context
*ilo
= ilo_context(pipe
);
579 ilo
->poly_stipple
= *state
;
581 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
585 ilo_set_scissor_states(struct pipe_context
*pipe
,
587 unsigned num_scissors
,
588 const struct pipe_scissor_state
*scissors
)
590 struct ilo_context
*ilo
= ilo_context(pipe
);
592 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
593 scissors
, &ilo
->scissor
);
595 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
599 ilo_set_viewport_states(struct pipe_context
*pipe
,
601 unsigned num_viewports
,
602 const struct pipe_viewport_state
*viewports
)
604 struct ilo_context
*ilo
= ilo_context(pipe
);
609 for (i
= 0; i
< num_viewports
; i
++) {
610 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
611 &ilo
->viewport
.cso
[start_slot
+ i
]);
614 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
615 ilo
->viewport
.count
= start_slot
+ num_viewports
;
617 /* need to save viewport 0 for util_blitter */
618 if (!start_slot
&& num_viewports
)
619 ilo
->viewport
.viewport0
= viewports
[0];
622 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
623 ilo
->viewport
.count
> start_slot
)
624 ilo
->viewport
.count
= start_slot
;
627 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
631 set_sampler_views(struct ilo_context
*ilo
,
632 unsigned shader
, unsigned start
, unsigned count
,
633 struct pipe_sampler_view
**views
, bool unset_old
)
635 struct pipe_sampler_view
**dst
= ilo
->view
[shader
].states
;
638 assert(start
+ count
<= Elements(ilo
->view
[shader
].states
));
646 for (i
= 0; i
< start
; i
++)
647 pipe_sampler_view_reference(&dst
[i
], NULL
);
648 for (; i
< start
+ count
; i
++)
649 pipe_sampler_view_reference(&dst
[i
], views
[i
- start
]);
650 for (; i
< ilo
->view
[shader
].count
; i
++)
651 pipe_sampler_view_reference(&dst
[i
], NULL
);
653 ilo
->view
[shader
].count
= start
+ count
;
660 for (i
= 0; i
< count
; i
++)
661 pipe_sampler_view_reference(&dst
[i
], views
[i
]);
664 for (i
= 0; i
< count
; i
++)
665 pipe_sampler_view_reference(&dst
[i
], NULL
);
668 if (ilo
->view
[shader
].count
<= start
+ count
) {
671 while (count
> 0 && !ilo
->view
[shader
].states
[count
- 1])
674 ilo
->view
[shader
].count
= count
;
679 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
681 struct pipe_sampler_view
**views
)
683 struct ilo_context
*ilo
= ilo_context(pipe
);
685 set_sampler_views(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
, true);
686 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
;
690 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
692 struct pipe_sampler_view
**views
)
694 struct ilo_context
*ilo
= ilo_context(pipe
);
696 set_sampler_views(ilo
, PIPE_SHADER_VERTEX
, 0, num_views
, views
, true);
697 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLER_VIEWS
;
701 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
703 struct pipe_sampler_view
**views
)
705 struct ilo_context
*ilo
= ilo_context(pipe
);
707 set_sampler_views(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
, true);
708 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
;
712 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
713 unsigned start_slot
, unsigned num_views
,
714 struct pipe_sampler_view
**views
)
716 struct ilo_context
*ilo
= ilo_context(pipe
);
718 set_sampler_views(ilo
, PIPE_SHADER_COMPUTE
,
719 start_slot
, num_views
, views
, false);
721 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
;
725 ilo_set_shader_resources(struct pipe_context
*pipe
,
726 unsigned start
, unsigned count
,
727 struct pipe_surface
**surfaces
)
729 struct ilo_context
*ilo
= ilo_context(pipe
);
730 struct pipe_surface
**dst
= ilo
->resource
.states
;
733 assert(start
+ count
<= Elements(ilo
->resource
.states
));
737 for (i
= 0; i
< count
; i
++)
738 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
741 for (i
= 0; i
< count
; i
++)
742 pipe_surface_reference(&dst
[i
], NULL
);
745 if (ilo
->resource
.count
<= start
+ count
) {
748 while (count
> 0 && !ilo
->resource
.states
[count
- 1])
751 ilo
->resource
.count
= count
;
754 ilo
->dirty
|= ILO_DIRTY_SHADER_RESOURCES
;
758 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
759 unsigned start_slot
, unsigned num_buffers
,
760 const struct pipe_vertex_buffer
*buffers
)
762 struct ilo_context
*ilo
= ilo_context(pipe
);
764 util_set_vertex_buffers_mask(ilo
->vb
.states
,
765 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
767 ilo
->dirty
|= ILO_DIRTY_VERTEX_BUFFERS
;
771 ilo_set_index_buffer(struct pipe_context
*pipe
,
772 const struct pipe_index_buffer
*state
)
774 struct ilo_context
*ilo
= ilo_context(pipe
);
777 ilo
->ib
.state
.index_size
= state
->index_size
;
778 ilo
->ib
.state
.offset
= state
->offset
;
779 pipe_resource_reference(&ilo
->ib
.state
.buffer
, state
->buffer
);
780 ilo
->ib
.state
.user_buffer
= state
->user_buffer
;
783 ilo
->ib
.state
.index_size
= 0;
784 ilo
->ib
.state
.offset
= 0;
785 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
786 ilo
->ib
.state
.user_buffer
= NULL
;
789 ilo
->dirty
|= ILO_DIRTY_INDEX_BUFFER
;
792 static struct pipe_stream_output_target
*
793 ilo_create_stream_output_target(struct pipe_context
*pipe
,
794 struct pipe_resource
*res
,
795 unsigned buffer_offset
,
796 unsigned buffer_size
)
798 struct pipe_stream_output_target
*target
;
800 target
= MALLOC_STRUCT(pipe_stream_output_target
);
803 pipe_reference_init(&target
->reference
, 1);
804 target
->buffer
= NULL
;
805 pipe_resource_reference(&target
->buffer
, res
);
806 target
->context
= pipe
;
807 target
->buffer_offset
= buffer_offset
;
808 target
->buffer_size
= buffer_size
;
814 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
815 unsigned num_targets
,
816 struct pipe_stream_output_target
**targets
,
817 unsigned append_bitmask
)
819 struct ilo_context
*ilo
= ilo_context(pipe
);
825 for (i
= 0; i
< num_targets
; i
++)
826 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
828 for (; i
< ilo
->so
.count
; i
++)
829 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
831 ilo
->so
.count
= num_targets
;
832 ilo
->so
.append_bitmask
= append_bitmask
;
834 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
836 ilo
->dirty
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
840 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
841 struct pipe_stream_output_target
*target
)
843 pipe_resource_reference(&target
->buffer
, NULL
);
847 static struct pipe_sampler_view
*
848 ilo_create_sampler_view(struct pipe_context
*pipe
,
849 struct pipe_resource
*res
,
850 const struct pipe_sampler_view
*templ
)
852 struct ilo_context
*ilo
= ilo_context(pipe
);
853 struct ilo_view_cso
*view
;
855 view
= MALLOC_STRUCT(ilo_view_cso
);
859 pipe_reference_init(&view
->base
.reference
, 1);
860 view
->base
.texture
= NULL
;
861 pipe_resource_reference(&view
->base
.texture
, res
);
862 view
->base
.context
= pipe
;
864 if (res
->target
== PIPE_BUFFER
) {
865 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
866 const unsigned first_elem
= templ
->u
.buf
.first_element
;
867 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
869 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
870 first_elem
* elem_size
, num_elems
* elem_size
,
871 elem_size
, templ
->format
, false, false, &view
->surface
);
874 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
876 templ
->u
.tex
.first_level
,
877 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
878 templ
->u
.tex
.first_layer
,
879 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
880 false, false, &view
->surface
);
887 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
888 struct pipe_sampler_view
*view
)
890 pipe_resource_reference(&view
->texture
, NULL
);
894 static struct pipe_surface
*
895 ilo_create_surface(struct pipe_context
*pipe
,
896 struct pipe_resource
*res
,
897 const struct pipe_surface
*templ
)
899 struct ilo_context
*ilo
= ilo_context(pipe
);
900 struct ilo_surface_cso
*surf
;
902 surf
= MALLOC_STRUCT(ilo_surface_cso
);
906 pipe_reference_init(&surf
->base
.reference
, 1);
907 surf
->base
.texture
= NULL
;
908 pipe_resource_reference(&surf
->base
.texture
, res
);
910 surf
->base
.context
= pipe
;
911 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
912 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
914 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
918 assert(res
->target
!= PIPE_BUFFER
);
921 * classic i965 sets render_cache_rw for constant buffers and sol
922 * surfaces but not render buffers. Why?
924 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
925 templ
->format
, templ
->u
.tex
.level
, 1,
926 templ
->u
.tex
.first_layer
,
927 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
928 true, true, &surf
->u
.rt
);
931 assert(res
->target
!= PIPE_BUFFER
);
933 /* will construct dynamically */
940 ilo_surface_destroy(struct pipe_context
*pipe
,
941 struct pipe_surface
*surface
)
943 pipe_resource_reference(&surface
->texture
, NULL
);
948 ilo_create_compute_state(struct pipe_context
*pipe
,
949 const struct pipe_compute_state
*state
)
951 struct ilo_context
*ilo
= ilo_context(pipe
);
952 return ilo_shader_state_create(ilo
, PIPE_SHADER_COMPUTE
, state
);
956 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
958 struct ilo_context
*ilo
= ilo_context(pipe
);
962 ilo
->dirty
|= ILO_DIRTY_COMPUTE
;
966 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
968 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
969 ilo_shader_state_destroy(cs
);
973 ilo_set_compute_resources(struct pipe_context
*pipe
,
974 unsigned start
, unsigned count
,
975 struct pipe_surface
**surfaces
)
977 struct ilo_context
*ilo
= ilo_context(pipe
);
978 struct pipe_surface
**dst
= ilo
->cs_resource
.states
;
981 assert(start
+ count
<= Elements(ilo
->cs_resource
.states
));
985 for (i
= 0; i
< count
; i
++)
986 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
989 for (i
= 0; i
< count
; i
++)
990 pipe_surface_reference(&dst
[i
], NULL
);
993 if (ilo
->cs_resource
.count
<= start
+ count
) {
996 while (count
> 0 && !ilo
->cs_resource
.states
[count
- 1])
999 ilo
->cs_resource
.count
= count
;
1002 ilo
->dirty
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1006 ilo_set_global_binding(struct pipe_context
*pipe
,
1007 unsigned start
, unsigned count
,
1008 struct pipe_resource
**resources
,
1011 struct ilo_context
*ilo
= ilo_context(pipe
);
1012 struct pipe_resource
**dst
= ilo
->global_binding
.resources
;
1015 assert(start
+ count
<= Elements(ilo
->global_binding
.resources
));
1019 for (i
= 0; i
< count
; i
++)
1020 pipe_resource_reference(&dst
[i
], resources
[i
]);
1023 for (i
= 0; i
< count
; i
++)
1024 pipe_resource_reference(&dst
[i
], NULL
);
1027 if (ilo
->global_binding
.count
<= start
+ count
) {
1030 while (count
> 0 && !ilo
->global_binding
.resources
[count
- 1])
1033 ilo
->global_binding
.count
= count
;
1036 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1040 * Initialize state-related functions.
1043 ilo_init_state_functions(struct ilo_context
*ilo
)
1045 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1047 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1048 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1049 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1050 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1051 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1052 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1053 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1054 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1055 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1056 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1057 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1058 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1059 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1060 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1061 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1062 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1063 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1064 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1065 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1066 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1067 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1068 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1069 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1070 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1071 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1072 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1073 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1075 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1076 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1077 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1078 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1079 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1080 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1081 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1082 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1083 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1084 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1085 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1086 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1087 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1088 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1089 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1090 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1092 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1093 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1094 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1096 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1097 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1099 ilo
->base
.create_surface
= ilo_create_surface
;
1100 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1102 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1103 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1104 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1105 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1106 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1110 ilo_init_states(struct ilo_context
*ilo
)
1112 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1114 ilo
->dirty
= ILO_DIRTY_ALL
;
1118 ilo_cleanup_states(struct ilo_context
*ilo
)
1122 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1123 if (ilo
->vb
.enabled_mask
& (1 << i
))
1124 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1127 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
1129 for (i
= 0; i
< ilo
->so
.count
; i
++)
1130 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1132 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1133 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1134 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1135 pipe_sampler_view_reference(&view
, NULL
);
1138 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1139 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1140 pipe_resource_reference(&cbuf
->resource
, NULL
);
1144 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1145 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1147 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1148 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1150 if (ilo
->fb
.state
.zsbuf
)
1151 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1153 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1154 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1156 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1157 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1161 * Mark all states that have the resource dirty.
1164 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1165 const struct pipe_resource
*res
)
1167 uint32_t states
= 0;
1170 if (res
->target
== PIPE_BUFFER
) {
1171 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1174 const unsigned idx
= u_bit_scan(&vb_mask
);
1176 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1177 states
|= ILO_DIRTY_VERTEX_BUFFERS
;
1182 if (ilo
->ib
.state
.buffer
== res
)
1183 states
|= ILO_DIRTY_INDEX_BUFFER
;
1185 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1186 if (ilo
->so
.states
[i
]->buffer
== res
) {
1187 states
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
1193 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1194 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1195 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1197 if (view
->texture
== res
) {
1198 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1199 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VERTEX_SAMPLER_VIEWS
,
1200 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
,
1201 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
,
1202 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
,
1205 states
|= view_dirty_bits
[sh
];
1210 if (res
->target
== PIPE_BUFFER
) {
1211 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1212 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1214 if (cbuf
->resource
== res
) {
1215 states
|= ILO_DIRTY_CONSTANT_BUFFER
;
1222 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1223 if (ilo
->resource
.states
[i
]->texture
== res
) {
1224 states
|= ILO_DIRTY_SHADER_RESOURCES
;
1230 if (res
->target
!= PIPE_BUFFER
) {
1231 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1232 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1233 states
|= ILO_DIRTY_FRAMEBUFFER
;
1238 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1239 states
|= ILO_DIRTY_FRAMEBUFFER
;
1242 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1243 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1244 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1245 states
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1250 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1251 if (ilo
->global_binding
.resources
[i
] == res
) {
1252 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1257 ilo
->dirty
|= states
;