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
++) {
99 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
100 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
102 /* upload user buffer */
103 if (cbuf
->user_buffer
) {
104 const enum pipe_format elem_format
=
105 PIPE_FORMAT_R32G32B32A32_FLOAT
;
108 u_upload_data(ilo
->uploader
, 0, cbuf
->user_buffer_size
,
109 cbuf
->user_buffer
, &offset
, &cbuf
->resource
);
111 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
112 ilo_buffer(cbuf
->resource
),
113 offset
, cbuf
->user_buffer_size
,
114 util_format_get_blocksize(elem_format
), elem_format
,
115 false, false, &cbuf
->surface
);
117 cbuf
->user_buffer
= NULL
;
118 cbuf
->user_buffer_size
= 0;
125 ilo
->cbuf
[sh
].count
= last_cbuf
+ 1;
130 * Finalize states. Some states depend on other states and are
131 * incomplete/invalid until finalized.
134 ilo_finalize_states(struct ilo_context
*ilo
)
136 finalize_shader_states(ilo
);
137 finalize_constant_buffers(ilo
);
139 u_upload_unmap(ilo
->uploader
);
143 ilo_create_blend_state(struct pipe_context
*pipe
,
144 const struct pipe_blend_state
*state
)
146 struct ilo_context
*ilo
= ilo_context(pipe
);
147 struct ilo_blend_state
*blend
;
149 blend
= MALLOC_STRUCT(ilo_blend_state
);
152 ilo_gpe_init_blend(ilo
->dev
, state
, blend
);
158 ilo_bind_blend_state(struct pipe_context
*pipe
, void *state
)
160 struct ilo_context
*ilo
= ilo_context(pipe
);
164 ilo
->dirty
|= ILO_DIRTY_BLEND
;
168 ilo_delete_blend_state(struct pipe_context
*pipe
, void *state
)
174 ilo_create_sampler_state(struct pipe_context
*pipe
,
175 const struct pipe_sampler_state
*state
)
177 struct ilo_context
*ilo
= ilo_context(pipe
);
178 struct ilo_sampler_cso
*sampler
;
180 sampler
= MALLOC_STRUCT(ilo_sampler_cso
);
183 ilo_gpe_init_sampler_cso(ilo
->dev
, state
, sampler
);
189 bind_samplers(struct ilo_context
*ilo
,
190 unsigned shader
, unsigned start
, unsigned count
,
191 void **samplers
, bool unbind_old
)
193 const struct ilo_sampler_cso
**dst
= ilo
->sampler
[shader
].cso
;
196 assert(start
+ count
<= Elements(ilo
->sampler
[shader
].cso
));
204 for (i
= 0; i
< start
; i
++)
206 for (; i
< start
+ count
; i
++)
207 dst
[i
] = samplers
[i
- start
];
208 for (; i
< ilo
->sampler
[shader
].count
; i
++)
211 ilo
->sampler
[shader
].count
= start
+ count
;
218 for (i
= 0; i
< count
; i
++)
219 dst
[i
] = samplers
[i
];
222 for (i
= 0; i
< count
; i
++)
226 if (ilo
->sampler
[shader
].count
<= start
+ count
) {
229 while (count
> 0 && !ilo
->sampler
[shader
].cso
[count
- 1])
232 ilo
->sampler
[shader
].count
= count
;
237 ilo_bind_fragment_sampler_states(struct pipe_context
*pipe
,
238 unsigned num_samplers
,
241 struct ilo_context
*ilo
= ilo_context(pipe
);
243 bind_samplers(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_samplers
, samplers
, true);
244 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLERS
;
248 ilo_bind_vertex_sampler_states(struct pipe_context
*pipe
,
249 unsigned num_samplers
,
252 struct ilo_context
*ilo
= ilo_context(pipe
);
254 bind_samplers(ilo
, PIPE_SHADER_VERTEX
, 0, num_samplers
, samplers
, true);
255 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLERS
;
259 ilo_bind_geometry_sampler_states(struct pipe_context
*pipe
,
260 unsigned num_samplers
,
263 struct ilo_context
*ilo
= ilo_context(pipe
);
265 bind_samplers(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_samplers
, samplers
, true);
266 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLERS
;
270 ilo_bind_compute_sampler_states(struct pipe_context
*pipe
,
272 unsigned num_samplers
,
275 struct ilo_context
*ilo
= ilo_context(pipe
);
277 bind_samplers(ilo
, PIPE_SHADER_COMPUTE
,
278 start_slot
, num_samplers
, samplers
, false);
279 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLERS
;
283 ilo_delete_sampler_state(struct pipe_context
*pipe
, void *state
)
289 ilo_create_rasterizer_state(struct pipe_context
*pipe
,
290 const struct pipe_rasterizer_state
*state
)
292 struct ilo_context
*ilo
= ilo_context(pipe
);
293 struct ilo_rasterizer_state
*rast
;
295 rast
= MALLOC_STRUCT(ilo_rasterizer_state
);
298 rast
->state
= *state
;
299 ilo_gpe_init_rasterizer(ilo
->dev
, state
, rast
);
305 ilo_bind_rasterizer_state(struct pipe_context
*pipe
, void *state
)
307 struct ilo_context
*ilo
= ilo_context(pipe
);
309 ilo
->rasterizer
= state
;
311 ilo
->dirty
|= ILO_DIRTY_RASTERIZER
;
315 ilo_delete_rasterizer_state(struct pipe_context
*pipe
, void *state
)
321 ilo_create_depth_stencil_alpha_state(struct pipe_context
*pipe
,
322 const struct pipe_depth_stencil_alpha_state
*state
)
324 struct ilo_context
*ilo
= ilo_context(pipe
);
325 struct ilo_dsa_state
*dsa
;
327 dsa
= MALLOC_STRUCT(ilo_dsa_state
);
330 ilo_gpe_init_dsa(ilo
->dev
, state
, dsa
);
336 ilo_bind_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
338 struct ilo_context
*ilo
= ilo_context(pipe
);
342 ilo
->dirty
|= ILO_DIRTY_DEPTH_STENCIL_ALPHA
;
346 ilo_delete_depth_stencil_alpha_state(struct pipe_context
*pipe
, void *state
)
352 ilo_create_fs_state(struct pipe_context
*pipe
,
353 const struct pipe_shader_state
*state
)
355 struct ilo_context
*ilo
= ilo_context(pipe
);
356 struct ilo_shader_state
*shader
;
358 shader
= ilo_shader_create_fs(ilo
->dev
, state
, ilo
);
361 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
367 ilo_bind_fs_state(struct pipe_context
*pipe
, void *state
)
369 struct ilo_context
*ilo
= ilo_context(pipe
);
373 ilo
->dirty
|= ILO_DIRTY_FS
;
377 ilo_delete_fs_state(struct pipe_context
*pipe
, void *state
)
379 struct ilo_context
*ilo
= ilo_context(pipe
);
380 struct ilo_shader_state
*fs
= (struct ilo_shader_state
*) state
;
382 ilo_shader_cache_remove(ilo
->shader_cache
, fs
);
383 ilo_shader_destroy(fs
);
387 ilo_create_vs_state(struct pipe_context
*pipe
,
388 const struct pipe_shader_state
*state
)
390 struct ilo_context
*ilo
= ilo_context(pipe
);
391 struct ilo_shader_state
*shader
;
393 shader
= ilo_shader_create_vs(ilo
->dev
, state
, ilo
);
396 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
402 ilo_bind_vs_state(struct pipe_context
*pipe
, void *state
)
404 struct ilo_context
*ilo
= ilo_context(pipe
);
408 ilo
->dirty
|= ILO_DIRTY_VS
;
412 ilo_delete_vs_state(struct pipe_context
*pipe
, void *state
)
414 struct ilo_context
*ilo
= ilo_context(pipe
);
415 struct ilo_shader_state
*vs
= (struct ilo_shader_state
*) state
;
417 ilo_shader_cache_remove(ilo
->shader_cache
, vs
);
418 ilo_shader_destroy(vs
);
422 ilo_create_gs_state(struct pipe_context
*pipe
,
423 const struct pipe_shader_state
*state
)
425 struct ilo_context
*ilo
= ilo_context(pipe
);
426 struct ilo_shader_state
*shader
;
428 shader
= ilo_shader_create_gs(ilo
->dev
, state
, ilo
);
431 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
437 ilo_bind_gs_state(struct pipe_context
*pipe
, void *state
)
439 struct ilo_context
*ilo
= ilo_context(pipe
);
443 ilo
->dirty
|= ILO_DIRTY_GS
;
447 ilo_delete_gs_state(struct pipe_context
*pipe
, void *state
)
449 struct ilo_context
*ilo
= ilo_context(pipe
);
450 struct ilo_shader_state
*gs
= (struct ilo_shader_state
*) state
;
452 ilo_shader_cache_remove(ilo
->shader_cache
, gs
);
453 ilo_shader_destroy(gs
);
457 ilo_create_vertex_elements_state(struct pipe_context
*pipe
,
458 unsigned num_elements
,
459 const struct pipe_vertex_element
*elements
)
461 struct ilo_context
*ilo
= ilo_context(pipe
);
462 struct ilo_ve_state
*ve
;
464 ve
= MALLOC_STRUCT(ilo_ve_state
);
467 ilo_gpe_init_ve(ilo
->dev
, num_elements
, elements
, ve
);
473 ilo_bind_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
475 struct ilo_context
*ilo
= ilo_context(pipe
);
479 ilo
->dirty
|= ILO_DIRTY_VERTEX_ELEMENTS
;
483 ilo_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
485 struct ilo_ve_state
*ve
= state
;
491 ilo_set_blend_color(struct pipe_context
*pipe
,
492 const struct pipe_blend_color
*state
)
494 struct ilo_context
*ilo
= ilo_context(pipe
);
496 ilo
->blend_color
= *state
;
498 ilo
->dirty
|= ILO_DIRTY_BLEND_COLOR
;
502 ilo_set_stencil_ref(struct pipe_context
*pipe
,
503 const struct pipe_stencil_ref
*state
)
505 struct ilo_context
*ilo
= ilo_context(pipe
);
507 ilo
->stencil_ref
= *state
;
509 ilo
->dirty
|= ILO_DIRTY_STENCIL_REF
;
513 ilo_set_sample_mask(struct pipe_context
*pipe
,
514 unsigned sample_mask
)
516 struct ilo_context
*ilo
= ilo_context(pipe
);
518 ilo
->sample_mask
= sample_mask
;
520 ilo
->dirty
|= ILO_DIRTY_SAMPLE_MASK
;
524 ilo_set_clip_state(struct pipe_context
*pipe
,
525 const struct pipe_clip_state
*state
)
527 struct ilo_context
*ilo
= ilo_context(pipe
);
531 ilo
->dirty
|= ILO_DIRTY_CLIP
;
535 ilo_set_constant_buffer(struct pipe_context
*pipe
,
536 uint shader
, uint index
,
537 struct pipe_constant_buffer
*state
)
539 struct ilo_context
*ilo
= ilo_context(pipe
);
540 struct ilo_cbuf_cso
*cbuf
;
542 assert(shader
< Elements(ilo
->cbuf
));
543 assert(index
< Elements(ilo
->cbuf
[shader
].cso
));
545 cbuf
= &ilo
->cbuf
[shader
].cso
[index
];
548 pipe_resource_reference(&cbuf
->resource
, state
->buffer
);
551 const enum pipe_format elem_format
= PIPE_FORMAT_R32G32B32A32_FLOAT
;
553 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
,
554 ilo_buffer(cbuf
->resource
),
555 state
->buffer_offset
, state
->buffer_size
,
556 util_format_get_blocksize(elem_format
), elem_format
,
557 false, false, &cbuf
->surface
);
559 cbuf
->user_buffer
= NULL
;
560 cbuf
->user_buffer_size
= 0;
563 assert(state
->user_buffer
);
565 cbuf
->surface
.bo
= NULL
;
567 /* state->buffer_offset does not apply for user buffer */
568 cbuf
->user_buffer
= state
->user_buffer
;
569 cbuf
->user_buffer_size
= state
->buffer_size
;
573 pipe_resource_reference(&cbuf
->resource
, NULL
);
574 cbuf
->surface
.bo
= NULL
;
576 cbuf
->user_buffer
= NULL
;
577 cbuf
->user_buffer_size
= 0;
580 /* the correct value will be set in ilo_finalize_states() */
581 ilo
->cbuf
[shader
].count
= 0;
583 ilo
->dirty
|= ILO_DIRTY_CONSTANT_BUFFER
;
587 ilo_set_framebuffer_state(struct pipe_context
*pipe
,
588 const struct pipe_framebuffer_state
*state
)
590 struct ilo_context
*ilo
= ilo_context(pipe
);
592 util_copy_framebuffer_state(&ilo
->fb
.state
, state
);
595 ilo
->fb
.num_samples
= state
->cbufs
[0]->texture
->nr_samples
;
596 else if (state
->zsbuf
)
597 ilo
->fb
.num_samples
= state
->zsbuf
->texture
->nr_samples
;
599 ilo
->fb
.num_samples
= 1;
601 if (!ilo
->fb
.num_samples
)
602 ilo
->fb
.num_samples
= 1;
604 ilo
->dirty
|= ILO_DIRTY_FRAMEBUFFER
;
608 ilo_set_polygon_stipple(struct pipe_context
*pipe
,
609 const struct pipe_poly_stipple
*state
)
611 struct ilo_context
*ilo
= ilo_context(pipe
);
613 ilo
->poly_stipple
= *state
;
615 ilo
->dirty
|= ILO_DIRTY_POLY_STIPPLE
;
619 ilo_set_scissor_states(struct pipe_context
*pipe
,
621 unsigned num_scissors
,
622 const struct pipe_scissor_state
*scissors
)
624 struct ilo_context
*ilo
= ilo_context(pipe
);
626 ilo_gpe_set_scissor(ilo
->dev
, start_slot
, num_scissors
,
627 scissors
, &ilo
->scissor
);
629 ilo
->dirty
|= ILO_DIRTY_SCISSOR
;
633 ilo_set_viewport_states(struct pipe_context
*pipe
,
635 unsigned num_viewports
,
636 const struct pipe_viewport_state
*viewports
)
638 struct ilo_context
*ilo
= ilo_context(pipe
);
643 for (i
= 0; i
< num_viewports
; i
++) {
644 ilo_gpe_set_viewport_cso(ilo
->dev
, &viewports
[i
],
645 &ilo
->viewport
.cso
[start_slot
+ i
]);
648 if (ilo
->viewport
.count
< start_slot
+ num_viewports
)
649 ilo
->viewport
.count
= start_slot
+ num_viewports
;
651 /* need to save viewport 0 for util_blitter */
652 if (!start_slot
&& num_viewports
)
653 ilo
->viewport
.viewport0
= viewports
[0];
656 if (ilo
->viewport
.count
<= start_slot
+ num_viewports
&&
657 ilo
->viewport
.count
> start_slot
)
658 ilo
->viewport
.count
= start_slot
;
661 ilo
->dirty
|= ILO_DIRTY_VIEWPORT
;
665 set_sampler_views(struct ilo_context
*ilo
,
666 unsigned shader
, unsigned start
, unsigned count
,
667 struct pipe_sampler_view
**views
, bool unset_old
)
669 struct pipe_sampler_view
**dst
= ilo
->view
[shader
].states
;
672 assert(start
+ count
<= Elements(ilo
->view
[shader
].states
));
680 for (i
= 0; i
< start
; i
++)
681 pipe_sampler_view_reference(&dst
[i
], NULL
);
682 for (; i
< start
+ count
; i
++)
683 pipe_sampler_view_reference(&dst
[i
], views
[i
- start
]);
684 for (; i
< ilo
->view
[shader
].count
; i
++)
685 pipe_sampler_view_reference(&dst
[i
], NULL
);
687 ilo
->view
[shader
].count
= start
+ count
;
694 for (i
= 0; i
< count
; i
++)
695 pipe_sampler_view_reference(&dst
[i
], views
[i
]);
698 for (i
= 0; i
< count
; i
++)
699 pipe_sampler_view_reference(&dst
[i
], NULL
);
702 if (ilo
->view
[shader
].count
<= start
+ count
) {
705 while (count
> 0 && !ilo
->view
[shader
].states
[count
- 1])
708 ilo
->view
[shader
].count
= count
;
713 ilo_set_fragment_sampler_views(struct pipe_context
*pipe
,
715 struct pipe_sampler_view
**views
)
717 struct ilo_context
*ilo
= ilo_context(pipe
);
719 set_sampler_views(ilo
, PIPE_SHADER_FRAGMENT
, 0, num_views
, views
, true);
720 ilo
->dirty
|= ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
;
724 ilo_set_vertex_sampler_views(struct pipe_context
*pipe
,
726 struct pipe_sampler_view
**views
)
728 struct ilo_context
*ilo
= ilo_context(pipe
);
730 set_sampler_views(ilo
, PIPE_SHADER_VERTEX
, 0, num_views
, views
, true);
731 ilo
->dirty
|= ILO_DIRTY_VERTEX_SAMPLER_VIEWS
;
735 ilo_set_geometry_sampler_views(struct pipe_context
*pipe
,
737 struct pipe_sampler_view
**views
)
739 struct ilo_context
*ilo
= ilo_context(pipe
);
741 set_sampler_views(ilo
, PIPE_SHADER_GEOMETRY
, 0, num_views
, views
, true);
742 ilo
->dirty
|= ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
;
746 ilo_set_compute_sampler_views(struct pipe_context
*pipe
,
747 unsigned start_slot
, unsigned num_views
,
748 struct pipe_sampler_view
**views
)
750 struct ilo_context
*ilo
= ilo_context(pipe
);
752 set_sampler_views(ilo
, PIPE_SHADER_COMPUTE
,
753 start_slot
, num_views
, views
, false);
755 ilo
->dirty
|= ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
;
759 ilo_set_shader_resources(struct pipe_context
*pipe
,
760 unsigned start
, unsigned count
,
761 struct pipe_surface
**surfaces
)
763 struct ilo_context
*ilo
= ilo_context(pipe
);
764 struct pipe_surface
**dst
= ilo
->resource
.states
;
767 assert(start
+ count
<= Elements(ilo
->resource
.states
));
771 for (i
= 0; i
< count
; i
++)
772 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
775 for (i
= 0; i
< count
; i
++)
776 pipe_surface_reference(&dst
[i
], NULL
);
779 if (ilo
->resource
.count
<= start
+ count
) {
782 while (count
> 0 && !ilo
->resource
.states
[count
- 1])
785 ilo
->resource
.count
= count
;
788 ilo
->dirty
|= ILO_DIRTY_SHADER_RESOURCES
;
792 ilo_set_vertex_buffers(struct pipe_context
*pipe
,
793 unsigned start_slot
, unsigned num_buffers
,
794 const struct pipe_vertex_buffer
*buffers
)
796 struct ilo_context
*ilo
= ilo_context(pipe
);
799 /* no PIPE_CAP_USER_VERTEX_BUFFERS */
801 for (i
= 0; i
< num_buffers
; i
++)
802 assert(!buffers
[i
].user_buffer
);
805 util_set_vertex_buffers_mask(ilo
->vb
.states
,
806 &ilo
->vb
.enabled_mask
, buffers
, start_slot
, num_buffers
);
808 ilo
->dirty
|= ILO_DIRTY_VERTEX_BUFFERS
;
812 ilo_set_index_buffer(struct pipe_context
*pipe
,
813 const struct pipe_index_buffer
*state
)
815 struct ilo_context
*ilo
= ilo_context(pipe
);
818 /* no PIPE_CAP_USER_INDEX_BUFFERS */
819 assert(!state
->user_buffer
);
821 ilo
->ib
.state
.index_size
= state
->index_size
;
822 ilo
->ib
.state
.offset
= state
->offset
;
823 pipe_resource_reference(&ilo
->ib
.state
.buffer
, state
->buffer
);
824 ilo
->ib
.state
.user_buffer
= state
->user_buffer
;
827 ilo
->ib
.state
.index_size
= 0;
828 ilo
->ib
.state
.offset
= 0;
829 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
830 ilo
->ib
.state
.user_buffer
= NULL
;
833 ilo
->dirty
|= ILO_DIRTY_INDEX_BUFFER
;
836 static struct pipe_stream_output_target
*
837 ilo_create_stream_output_target(struct pipe_context
*pipe
,
838 struct pipe_resource
*res
,
839 unsigned buffer_offset
,
840 unsigned buffer_size
)
842 struct pipe_stream_output_target
*target
;
844 target
= MALLOC_STRUCT(pipe_stream_output_target
);
847 pipe_reference_init(&target
->reference
, 1);
848 target
->buffer
= NULL
;
849 pipe_resource_reference(&target
->buffer
, res
);
850 target
->context
= pipe
;
851 target
->buffer_offset
= buffer_offset
;
852 target
->buffer_size
= buffer_size
;
858 ilo_set_stream_output_targets(struct pipe_context
*pipe
,
859 unsigned num_targets
,
860 struct pipe_stream_output_target
**targets
,
861 unsigned append_bitmask
)
863 struct ilo_context
*ilo
= ilo_context(pipe
);
869 for (i
= 0; i
< num_targets
; i
++)
870 pipe_so_target_reference(&ilo
->so
.states
[i
], targets
[i
]);
872 for (; i
< ilo
->so
.count
; i
++)
873 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
875 ilo
->so
.count
= num_targets
;
876 ilo
->so
.append_bitmask
= append_bitmask
;
878 ilo
->so
.enabled
= (ilo
->so
.count
> 0);
880 ilo
->dirty
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
884 ilo_stream_output_target_destroy(struct pipe_context
*pipe
,
885 struct pipe_stream_output_target
*target
)
887 pipe_resource_reference(&target
->buffer
, NULL
);
891 static struct pipe_sampler_view
*
892 ilo_create_sampler_view(struct pipe_context
*pipe
,
893 struct pipe_resource
*res
,
894 const struct pipe_sampler_view
*templ
)
896 struct ilo_context
*ilo
= ilo_context(pipe
);
897 struct ilo_view_cso
*view
;
899 view
= MALLOC_STRUCT(ilo_view_cso
);
903 pipe_reference_init(&view
->base
.reference
, 1);
904 view
->base
.texture
= NULL
;
905 pipe_resource_reference(&view
->base
.texture
, res
);
906 view
->base
.context
= pipe
;
908 if (res
->target
== PIPE_BUFFER
) {
909 const unsigned elem_size
= util_format_get_blocksize(templ
->format
);
910 const unsigned first_elem
= templ
->u
.buf
.first_element
;
911 const unsigned num_elems
= templ
->u
.buf
.last_element
- first_elem
+ 1;
913 ilo_gpe_init_view_surface_for_buffer(ilo
->dev
, ilo_buffer(res
),
914 first_elem
* elem_size
, num_elems
* elem_size
,
915 elem_size
, templ
->format
, false, false, &view
->surface
);
918 struct ilo_texture
*tex
= ilo_texture(res
);
920 /* warn about degraded performance because of a missing binding flag */
921 if (tex
->tiling
== INTEL_TILING_NONE
&&
922 !(tex
->base
.bind
& PIPE_BIND_SAMPLER_VIEW
)) {
923 ilo_warn("creating sampler view for a resource "
924 "not created for sampling\n");
927 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, tex
,
929 templ
->u
.tex
.first_level
,
930 templ
->u
.tex
.last_level
- templ
->u
.tex
.first_level
+ 1,
931 templ
->u
.tex
.first_layer
,
932 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
933 false, false, &view
->surface
);
940 ilo_sampler_view_destroy(struct pipe_context
*pipe
,
941 struct pipe_sampler_view
*view
)
943 pipe_resource_reference(&view
->texture
, NULL
);
947 static struct pipe_surface
*
948 ilo_create_surface(struct pipe_context
*pipe
,
949 struct pipe_resource
*res
,
950 const struct pipe_surface
*templ
)
952 struct ilo_context
*ilo
= ilo_context(pipe
);
953 struct ilo_surface_cso
*surf
;
955 surf
= MALLOC_STRUCT(ilo_surface_cso
);
959 pipe_reference_init(&surf
->base
.reference
, 1);
960 surf
->base
.texture
= NULL
;
961 pipe_resource_reference(&surf
->base
.texture
, res
);
963 surf
->base
.context
= pipe
;
964 surf
->base
.width
= u_minify(res
->width0
, templ
->u
.tex
.level
);
965 surf
->base
.height
= u_minify(res
->height0
, templ
->u
.tex
.level
);
967 surf
->is_rt
= !util_format_is_depth_or_stencil(templ
->format
);
971 assert(res
->target
!= PIPE_BUFFER
);
974 * classic i965 sets render_cache_rw for constant buffers and sol
975 * surfaces but not render buffers. Why?
977 ilo_gpe_init_view_surface_for_texture(ilo
->dev
, ilo_texture(res
),
978 templ
->format
, templ
->u
.tex
.level
, 1,
979 templ
->u
.tex
.first_layer
,
980 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
981 true, true, &surf
->u
.rt
);
984 assert(res
->target
!= PIPE_BUFFER
);
986 ilo_gpe_init_zs_surface(ilo
->dev
, ilo_texture(res
),
987 templ
->format
, templ
->u
.tex
.level
,
988 templ
->u
.tex
.first_layer
,
989 templ
->u
.tex
.last_layer
- templ
->u
.tex
.first_layer
+ 1,
997 ilo_surface_destroy(struct pipe_context
*pipe
,
998 struct pipe_surface
*surface
)
1000 pipe_resource_reference(&surface
->texture
, NULL
);
1005 ilo_create_compute_state(struct pipe_context
*pipe
,
1006 const struct pipe_compute_state
*state
)
1008 struct ilo_context
*ilo
= ilo_context(pipe
);
1009 struct ilo_shader_state
*shader
;
1011 shader
= ilo_shader_create_cs(ilo
->dev
, state
, ilo
);
1014 ilo_shader_cache_add(ilo
->shader_cache
, shader
);
1020 ilo_bind_compute_state(struct pipe_context
*pipe
, void *state
)
1022 struct ilo_context
*ilo
= ilo_context(pipe
);
1026 ilo
->dirty
|= ILO_DIRTY_COMPUTE
;
1030 ilo_delete_compute_state(struct pipe_context
*pipe
, void *state
)
1032 struct ilo_context
*ilo
= ilo_context(pipe
);
1033 struct ilo_shader_state
*cs
= (struct ilo_shader_state
*) state
;
1035 ilo_shader_cache_remove(ilo
->shader_cache
, cs
);
1036 ilo_shader_destroy(cs
);
1040 ilo_set_compute_resources(struct pipe_context
*pipe
,
1041 unsigned start
, unsigned count
,
1042 struct pipe_surface
**surfaces
)
1044 struct ilo_context
*ilo
= ilo_context(pipe
);
1045 struct pipe_surface
**dst
= ilo
->cs_resource
.states
;
1048 assert(start
+ count
<= Elements(ilo
->cs_resource
.states
));
1052 for (i
= 0; i
< count
; i
++)
1053 pipe_surface_reference(&dst
[i
], surfaces
[i
]);
1056 for (i
= 0; i
< count
; i
++)
1057 pipe_surface_reference(&dst
[i
], NULL
);
1060 if (ilo
->cs_resource
.count
<= start
+ count
) {
1063 while (count
> 0 && !ilo
->cs_resource
.states
[count
- 1])
1066 ilo
->cs_resource
.count
= count
;
1069 ilo
->dirty
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1073 ilo_set_global_binding(struct pipe_context
*pipe
,
1074 unsigned start
, unsigned count
,
1075 struct pipe_resource
**resources
,
1078 struct ilo_context
*ilo
= ilo_context(pipe
);
1079 struct pipe_resource
**dst
= ilo
->global_binding
.resources
;
1082 assert(start
+ count
<= Elements(ilo
->global_binding
.resources
));
1086 for (i
= 0; i
< count
; i
++)
1087 pipe_resource_reference(&dst
[i
], resources
[i
]);
1090 for (i
= 0; i
< count
; i
++)
1091 pipe_resource_reference(&dst
[i
], NULL
);
1094 if (ilo
->global_binding
.count
<= start
+ count
) {
1097 while (count
> 0 && !ilo
->global_binding
.resources
[count
- 1])
1100 ilo
->global_binding
.count
= count
;
1103 ilo
->dirty
|= ILO_DIRTY_GLOBAL_BINDING
;
1107 * Initialize state-related functions.
1110 ilo_init_state_functions(struct ilo_context
*ilo
)
1112 STATIC_ASSERT(ILO_STATE_COUNT
<= 32);
1114 ilo
->base
.create_blend_state
= ilo_create_blend_state
;
1115 ilo
->base
.bind_blend_state
= ilo_bind_blend_state
;
1116 ilo
->base
.delete_blend_state
= ilo_delete_blend_state
;
1117 ilo
->base
.create_sampler_state
= ilo_create_sampler_state
;
1118 ilo
->base
.bind_fragment_sampler_states
= ilo_bind_fragment_sampler_states
;
1119 ilo
->base
.bind_vertex_sampler_states
= ilo_bind_vertex_sampler_states
;
1120 ilo
->base
.bind_geometry_sampler_states
= ilo_bind_geometry_sampler_states
;
1121 ilo
->base
.bind_compute_sampler_states
= ilo_bind_compute_sampler_states
;
1122 ilo
->base
.delete_sampler_state
= ilo_delete_sampler_state
;
1123 ilo
->base
.create_rasterizer_state
= ilo_create_rasterizer_state
;
1124 ilo
->base
.bind_rasterizer_state
= ilo_bind_rasterizer_state
;
1125 ilo
->base
.delete_rasterizer_state
= ilo_delete_rasterizer_state
;
1126 ilo
->base
.create_depth_stencil_alpha_state
= ilo_create_depth_stencil_alpha_state
;
1127 ilo
->base
.bind_depth_stencil_alpha_state
= ilo_bind_depth_stencil_alpha_state
;
1128 ilo
->base
.delete_depth_stencil_alpha_state
= ilo_delete_depth_stencil_alpha_state
;
1129 ilo
->base
.create_fs_state
= ilo_create_fs_state
;
1130 ilo
->base
.bind_fs_state
= ilo_bind_fs_state
;
1131 ilo
->base
.delete_fs_state
= ilo_delete_fs_state
;
1132 ilo
->base
.create_vs_state
= ilo_create_vs_state
;
1133 ilo
->base
.bind_vs_state
= ilo_bind_vs_state
;
1134 ilo
->base
.delete_vs_state
= ilo_delete_vs_state
;
1135 ilo
->base
.create_gs_state
= ilo_create_gs_state
;
1136 ilo
->base
.bind_gs_state
= ilo_bind_gs_state
;
1137 ilo
->base
.delete_gs_state
= ilo_delete_gs_state
;
1138 ilo
->base
.create_vertex_elements_state
= ilo_create_vertex_elements_state
;
1139 ilo
->base
.bind_vertex_elements_state
= ilo_bind_vertex_elements_state
;
1140 ilo
->base
.delete_vertex_elements_state
= ilo_delete_vertex_elements_state
;
1142 ilo
->base
.set_blend_color
= ilo_set_blend_color
;
1143 ilo
->base
.set_stencil_ref
= ilo_set_stencil_ref
;
1144 ilo
->base
.set_sample_mask
= ilo_set_sample_mask
;
1145 ilo
->base
.set_clip_state
= ilo_set_clip_state
;
1146 ilo
->base
.set_constant_buffer
= ilo_set_constant_buffer
;
1147 ilo
->base
.set_framebuffer_state
= ilo_set_framebuffer_state
;
1148 ilo
->base
.set_polygon_stipple
= ilo_set_polygon_stipple
;
1149 ilo
->base
.set_scissor_states
= ilo_set_scissor_states
;
1150 ilo
->base
.set_viewport_states
= ilo_set_viewport_states
;
1151 ilo
->base
.set_fragment_sampler_views
= ilo_set_fragment_sampler_views
;
1152 ilo
->base
.set_vertex_sampler_views
= ilo_set_vertex_sampler_views
;
1153 ilo
->base
.set_geometry_sampler_views
= ilo_set_geometry_sampler_views
;
1154 ilo
->base
.set_compute_sampler_views
= ilo_set_compute_sampler_views
;
1155 ilo
->base
.set_shader_resources
= ilo_set_shader_resources
;
1156 ilo
->base
.set_vertex_buffers
= ilo_set_vertex_buffers
;
1157 ilo
->base
.set_index_buffer
= ilo_set_index_buffer
;
1159 ilo
->base
.create_stream_output_target
= ilo_create_stream_output_target
;
1160 ilo
->base
.stream_output_target_destroy
= ilo_stream_output_target_destroy
;
1161 ilo
->base
.set_stream_output_targets
= ilo_set_stream_output_targets
;
1163 ilo
->base
.create_sampler_view
= ilo_create_sampler_view
;
1164 ilo
->base
.sampler_view_destroy
= ilo_sampler_view_destroy
;
1166 ilo
->base
.create_surface
= ilo_create_surface
;
1167 ilo
->base
.surface_destroy
= ilo_surface_destroy
;
1169 ilo
->base
.create_compute_state
= ilo_create_compute_state
;
1170 ilo
->base
.bind_compute_state
= ilo_bind_compute_state
;
1171 ilo
->base
.delete_compute_state
= ilo_delete_compute_state
;
1172 ilo
->base
.set_compute_resources
= ilo_set_compute_resources
;
1173 ilo
->base
.set_global_binding
= ilo_set_global_binding
;
1177 ilo_init_states(struct ilo_context
*ilo
)
1179 ilo_gpe_set_scissor_null(ilo
->dev
, &ilo
->scissor
);
1181 ilo_gpe_init_zs_surface(ilo
->dev
, NULL
,
1182 PIPE_FORMAT_NONE
, 0, 0, 1, &ilo
->fb
.null_zs
);
1184 ilo
->dirty
= ILO_DIRTY_ALL
;
1188 ilo_cleanup_states(struct ilo_context
*ilo
)
1192 for (i
= 0; i
< Elements(ilo
->vb
.states
); i
++) {
1193 if (ilo
->vb
.enabled_mask
& (1 << i
))
1194 pipe_resource_reference(&ilo
->vb
.states
[i
].buffer
, NULL
);
1197 pipe_resource_reference(&ilo
->ib
.state
.buffer
, NULL
);
1199 for (i
= 0; i
< ilo
->so
.count
; i
++)
1200 pipe_so_target_reference(&ilo
->so
.states
[i
], NULL
);
1202 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1203 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1204 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1205 pipe_sampler_view_reference(&view
, NULL
);
1208 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1209 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1210 pipe_resource_reference(&cbuf
->resource
, NULL
);
1214 for (i
= 0; i
< ilo
->resource
.count
; i
++)
1215 pipe_surface_reference(&ilo
->resource
.states
[i
], NULL
);
1217 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++)
1218 pipe_surface_reference(&ilo
->fb
.state
.cbufs
[i
], NULL
);
1220 if (ilo
->fb
.state
.zsbuf
)
1221 pipe_surface_reference(&ilo
->fb
.state
.zsbuf
, NULL
);
1223 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++)
1224 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1226 for (i
= 0; i
< ilo
->global_binding
.count
; i
++)
1227 pipe_resource_reference(&ilo
->global_binding
.resources
[i
], NULL
);
1231 * Mark all states that have the resource dirty.
1234 ilo_mark_states_with_resource_dirty(struct ilo_context
*ilo
,
1235 const struct pipe_resource
*res
)
1237 uint32_t states
= 0;
1240 if (res
->target
== PIPE_BUFFER
) {
1241 uint32_t vb_mask
= ilo
->vb
.enabled_mask
;
1244 const unsigned idx
= u_bit_scan(&vb_mask
);
1246 if (ilo
->vb
.states
[idx
].buffer
== res
) {
1247 states
|= ILO_DIRTY_VERTEX_BUFFERS
;
1252 if (ilo
->ib
.state
.buffer
== res
)
1253 states
|= ILO_DIRTY_INDEX_BUFFER
;
1255 for (i
= 0; i
< ilo
->so
.count
; i
++) {
1256 if (ilo
->so
.states
[i
]->buffer
== res
) {
1257 states
|= ILO_DIRTY_STREAM_OUTPUT_TARGETS
;
1263 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
1264 for (i
= 0; i
< ilo
->view
[sh
].count
; i
++) {
1265 struct pipe_sampler_view
*view
= ilo
->view
[sh
].states
[i
];
1267 if (view
->texture
== res
) {
1268 static const unsigned view_dirty_bits
[PIPE_SHADER_TYPES
] = {
1269 [PIPE_SHADER_VERTEX
] = ILO_DIRTY_VERTEX_SAMPLER_VIEWS
,
1270 [PIPE_SHADER_FRAGMENT
] = ILO_DIRTY_FRAGMENT_SAMPLER_VIEWS
,
1271 [PIPE_SHADER_GEOMETRY
] = ILO_DIRTY_GEOMETRY_SAMPLER_VIEWS
,
1272 [PIPE_SHADER_COMPUTE
] = ILO_DIRTY_COMPUTE_SAMPLER_VIEWS
,
1275 states
|= view_dirty_bits
[sh
];
1280 if (res
->target
== PIPE_BUFFER
) {
1281 for (i
= 0; i
< Elements(ilo
->cbuf
[sh
].cso
); i
++) {
1282 struct ilo_cbuf_cso
*cbuf
= &ilo
->cbuf
[sh
].cso
[i
];
1284 if (cbuf
->resource
== res
) {
1285 states
|= ILO_DIRTY_CONSTANT_BUFFER
;
1292 for (i
= 0; i
< ilo
->resource
.count
; i
++) {
1293 if (ilo
->resource
.states
[i
]->texture
== res
) {
1294 states
|= ILO_DIRTY_SHADER_RESOURCES
;
1300 if (res
->target
!= PIPE_BUFFER
) {
1301 for (i
= 0; i
< ilo
->fb
.state
.nr_cbufs
; i
++) {
1302 if (ilo
->fb
.state
.cbufs
[i
]->texture
== res
) {
1303 states
|= ILO_DIRTY_FRAMEBUFFER
;
1308 if (ilo
->fb
.state
.zsbuf
&& ilo
->fb
.state
.zsbuf
->texture
== res
)
1309 states
|= ILO_DIRTY_FRAMEBUFFER
;
1312 for (i
= 0; i
< ilo
->cs_resource
.count
; i
++) {
1313 pipe_surface_reference(&ilo
->cs_resource
.states
[i
], NULL
);
1314 if (ilo
->cs_resource
.states
[i
]->texture
== res
) {
1315 states
|= ILO_DIRTY_COMPUTE_RESOURCES
;
1320 for (i
= 0; i
< ilo
->global_binding
.count
; i
++) {
1321 if (ilo
->global_binding
.resources
[i
] == res
) {
1322 states
|= ILO_DIRTY_GLOBAL_BINDING
;
1327 ilo
->dirty
|= states
;