1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
31 * Wrap the cso cache & hash mechanisms in a simplified
32 * pipe-driver-specific interface.
34 * @author Zack Rusin <zack@tungstengraphics.com>
35 * @author Keith Whitwell <keith@tungstengraphics.com>
38 #include "pipe/p_state.h"
39 #include "util/u_draw.h"
40 #include "util/u_framebuffer.h"
41 #include "util/u_inlines.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "util/u_vbuf.h"
45 #include "tgsi/tgsi_parse.h"
47 #include "cso_cache/cso_context.h"
48 #include "cso_cache/cso_cache.h"
49 #include "cso_cache/cso_hash.h"
50 #include "cso_context.h"
54 * Info related to samplers and sampler views.
55 * We have one of these for fragment samplers and another for vertex samplers.
60 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *samplers
[PIPE_MAX_SAMPLERS
];
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
68 unsigned nr_samplers_saved
;
70 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
73 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
74 unsigned nr_views_saved
;
80 struct pipe_context
*pipe
;
81 struct cso_cache
*cache
;
84 boolean has_geometry_shader
;
85 boolean has_streamout
;
87 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
89 uint nr_vertex_buffers
;
90 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
92 uint nr_vertex_buffers_saved
;
93 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
95 unsigned nr_so_targets
;
96 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
98 unsigned nr_so_targets_saved
;
99 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
101 /** Current and saved state.
102 * The saved state is used as a 1-deep stack.
104 void *blend
, *blend_saved
;
105 void *depth_stencil
, *depth_stencil_saved
;
106 void *rasterizer
, *rasterizer_saved
;
107 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
108 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
109 void *velements
, *velements_saved
;
111 struct pipe_clip_state clip
;
112 struct pipe_clip_state clip_saved
;
114 struct pipe_framebuffer_state fb
, fb_saved
;
115 struct pipe_viewport_state vp
, vp_saved
;
116 struct pipe_blend_color blend_color
;
117 unsigned sample_mask
, sample_mask_saved
;
118 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
122 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
124 struct cso_blend
*cso
= (struct cso_blend
*)state
;
126 if (ctx
->blend
== cso
->data
)
129 if (cso
->delete_state
)
130 cso
->delete_state(cso
->context
, cso
->data
);
135 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
137 struct cso_depth_stencil_alpha
*cso
=
138 (struct cso_depth_stencil_alpha
*)state
;
140 if (ctx
->depth_stencil
== cso
->data
)
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
150 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
152 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
153 if (cso
->delete_state
)
154 cso
->delete_state(cso
->context
, cso
->data
);
159 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
161 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
163 if (ctx
->rasterizer
== cso
->data
)
165 if (cso
->delete_state
)
166 cso
->delete_state(cso
->context
, cso
->data
);
171 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
174 struct cso_velements
*cso
= (struct cso_velements
*)state
;
176 if (ctx
->velements
== cso
->data
)
179 if (cso
->delete_state
)
180 cso
->delete_state(cso
->context
, cso
->data
);
186 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
187 void *state
, enum cso_cache_type type
)
191 return delete_blend_state(ctx
, state
);
193 return delete_sampler_state(ctx
, state
);
194 case CSO_DEPTH_STENCIL_ALPHA
:
195 return delete_depth_stencil_state(ctx
, state
);
197 return delete_rasterizer_state(ctx
, state
);
199 return delete_vertex_elements(ctx
, state
);
208 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
209 int max_size
, void *user_data
)
211 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
212 /* if we're approach the maximum size, remove fourth of the entries
213 * otherwise every subsequent call will go through the same */
214 int hash_size
= cso_hash_size(hash
);
215 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
216 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
217 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
218 if (hash_size
> max_size
)
219 to_remove
+= hash_size
- max_size
;
221 /*remove elements until we're good */
222 /*fixme: currently we pick the nodes to remove at random*/
223 void *cso
= cso_hash_iter_data(iter
);
224 if (delete_cso(ctx
, cso
, type
)) {
225 iter
= cso_hash_erase(hash
, iter
);
228 iter
= cso_hash_iter_next(iter
);
232 static void cso_init_vbuf(struct cso_context
*cso
)
234 struct u_vbuf_caps caps
;
236 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
238 /* Install u_vbuf if there is anything unsupported. */
239 if (!caps
.buffer_offset_unaligned
||
240 !caps
.buffer_stride_unaligned
||
241 !caps
.velem_src_offset_unaligned
||
242 !caps
.format_fixed32
||
243 !caps
.format_float16
||
244 !caps
.format_float64
||
245 !caps
.format_norm32
||
246 !caps
.format_scaled32
||
247 !caps
.user_vertex_buffers
) {
248 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
);
252 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
254 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
258 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
260 ctx
->cache
= cso_cache_create();
261 if (ctx
->cache
== NULL
)
263 cso_cache_set_sanitize_callback(ctx
->cache
,
268 ctx
->sample_mask_saved
= ~0;
272 /* Enable for testing: */
273 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
275 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
276 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
277 ctx
->has_geometry_shader
= TRUE
;
279 if (pipe
->screen
->get_param(pipe
->screen
,
280 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
281 ctx
->has_streamout
= TRUE
;
287 cso_destroy_context( ctx
);
292 * Prior to context destruction, this function unbinds all state objects.
294 void cso_release_all( struct cso_context
*ctx
)
299 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
300 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
301 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
302 if (ctx
->pipe
->bind_vertex_sampler_states
)
303 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
304 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
305 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
306 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
307 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
308 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
309 if (ctx
->pipe
->set_vertex_sampler_views
)
310 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
311 if (ctx
->pipe
->set_stream_output_targets
)
312 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
315 /* free fragment samplers, views */
316 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
317 struct sampler_info
*info
= &ctx
->samplers
[shader
];
318 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
319 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
320 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
324 util_unreference_framebuffer_state(&ctx
->fb
);
325 util_unreference_framebuffer_state(&ctx
->fb_saved
);
327 util_copy_vertex_buffers(ctx
->vertex_buffers
,
328 &ctx
->nr_vertex_buffers
,
330 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
331 &ctx
->nr_vertex_buffers_saved
,
334 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
335 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
336 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
340 cso_cache_delete( ctx
->cache
);
347 * Free the CSO context. NOTE: the state tracker should have previously called
350 void cso_destroy_context( struct cso_context
*ctx
)
354 u_vbuf_destroy(ctx
->vbuf
);
360 /* Those function will either find the state of the given template
361 * in the cache or they will create a new state from the given
362 * template, insert it in the cache and return it.
366 * If the driver returns 0 from the create method then they will assign
367 * the data member of the cso to be the template itself.
370 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
371 const struct pipe_blend_state
*templ
)
373 unsigned key_size
, hash_key
;
374 struct cso_hash_iter iter
;
377 key_size
= templ
->independent_blend_enable
?
378 sizeof(struct pipe_blend_state
) :
379 (char *)&(templ
->rt
[1]) - (char *)templ
;
380 hash_key
= cso_construct_key((void*)templ
, key_size
);
381 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
382 (void*)templ
, key_size
);
384 if (cso_hash_iter_is_null(iter
)) {
385 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
387 return PIPE_ERROR_OUT_OF_MEMORY
;
389 memset(&cso
->state
, 0, sizeof cso
->state
);
390 memcpy(&cso
->state
, templ
, key_size
);
391 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
392 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
393 cso
->context
= ctx
->pipe
;
395 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
396 if (cso_hash_iter_is_null(iter
)) {
398 return PIPE_ERROR_OUT_OF_MEMORY
;
404 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
407 if (ctx
->blend
!= handle
) {
409 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
414 void cso_save_blend(struct cso_context
*ctx
)
416 assert(!ctx
->blend_saved
);
417 ctx
->blend_saved
= ctx
->blend
;
420 void cso_restore_blend(struct cso_context
*ctx
)
422 if (ctx
->blend
!= ctx
->blend_saved
) {
423 ctx
->blend
= ctx
->blend_saved
;
424 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
426 ctx
->blend_saved
= NULL
;
432 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
433 const struct pipe_depth_stencil_alpha_state
*templ
)
435 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
436 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
437 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
439 CSO_DEPTH_STENCIL_ALPHA
,
440 (void*)templ
, key_size
);
443 if (cso_hash_iter_is_null(iter
)) {
444 struct cso_depth_stencil_alpha
*cso
=
445 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
447 return PIPE_ERROR_OUT_OF_MEMORY
;
449 memcpy(&cso
->state
, templ
, sizeof(*templ
));
450 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
453 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
454 cso
->context
= ctx
->pipe
;
456 iter
= cso_insert_state(ctx
->cache
, hash_key
,
457 CSO_DEPTH_STENCIL_ALPHA
, cso
);
458 if (cso_hash_iter_is_null(iter
)) {
460 return PIPE_ERROR_OUT_OF_MEMORY
;
466 handle
= ((struct cso_depth_stencil_alpha
*)
467 cso_hash_iter_data(iter
))->data
;
470 if (ctx
->depth_stencil
!= handle
) {
471 ctx
->depth_stencil
= handle
;
472 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
477 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
479 assert(!ctx
->depth_stencil_saved
);
480 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
483 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
485 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
486 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
487 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
488 ctx
->depth_stencil_saved
);
490 ctx
->depth_stencil_saved
= NULL
;
495 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
496 const struct pipe_rasterizer_state
*templ
)
498 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
499 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
500 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
503 (void*)templ
, key_size
);
506 if (cso_hash_iter_is_null(iter
)) {
507 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
509 return PIPE_ERROR_OUT_OF_MEMORY
;
511 memcpy(&cso
->state
, templ
, sizeof(*templ
));
512 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
514 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
515 cso
->context
= ctx
->pipe
;
517 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
518 if (cso_hash_iter_is_null(iter
)) {
520 return PIPE_ERROR_OUT_OF_MEMORY
;
526 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
529 if (ctx
->rasterizer
!= handle
) {
530 ctx
->rasterizer
= handle
;
531 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
536 void cso_save_rasterizer(struct cso_context
*ctx
)
538 assert(!ctx
->rasterizer_saved
);
539 ctx
->rasterizer_saved
= ctx
->rasterizer
;
542 void cso_restore_rasterizer(struct cso_context
*ctx
)
544 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
545 ctx
->rasterizer
= ctx
->rasterizer_saved
;
546 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
548 ctx
->rasterizer_saved
= NULL
;
553 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
556 if (ctx
->fragment_shader
!= handle
) {
557 ctx
->fragment_shader
= handle
;
558 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
563 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
565 if (handle
== ctx
->fragment_shader
) {
566 /* unbind before deleting */
567 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
568 ctx
->fragment_shader
= NULL
;
570 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
573 void cso_save_fragment_shader(struct cso_context
*ctx
)
575 assert(!ctx
->fragment_shader_saved
);
576 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
579 void cso_restore_fragment_shader(struct cso_context
*ctx
)
581 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
582 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
583 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
585 ctx
->fragment_shader_saved
= NULL
;
589 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
592 if (ctx
->vertex_shader
!= handle
) {
593 ctx
->vertex_shader
= handle
;
594 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
599 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
601 if (handle
== ctx
->vertex_shader
) {
602 /* unbind before deleting */
603 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
604 ctx
->vertex_shader
= NULL
;
606 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
609 void cso_save_vertex_shader(struct cso_context
*ctx
)
611 assert(!ctx
->vertex_shader_saved
);
612 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
615 void cso_restore_vertex_shader(struct cso_context
*ctx
)
617 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
618 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
619 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
621 ctx
->vertex_shader_saved
= NULL
;
625 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
626 const struct pipe_framebuffer_state
*fb
)
628 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
629 util_copy_framebuffer_state(&ctx
->fb
, fb
);
630 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
635 void cso_save_framebuffer(struct cso_context
*ctx
)
637 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
640 void cso_restore_framebuffer(struct cso_context
*ctx
)
642 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
643 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
644 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
645 util_unreference_framebuffer_state(&ctx
->fb_saved
);
650 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
651 const struct pipe_viewport_state
*vp
)
653 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
655 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
660 void cso_save_viewport(struct cso_context
*ctx
)
662 ctx
->vp_saved
= ctx
->vp
;
666 void cso_restore_viewport(struct cso_context
*ctx
)
668 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
669 ctx
->vp
= ctx
->vp_saved
;
670 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
675 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
676 const struct pipe_blend_color
*bc
)
678 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
679 ctx
->blend_color
= *bc
;
680 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
685 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
686 unsigned sample_mask
)
688 if (ctx
->sample_mask
!= sample_mask
) {
689 ctx
->sample_mask
= sample_mask
;
690 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
695 void cso_save_sample_mask(struct cso_context
*ctx
)
697 ctx
->sample_mask_saved
= ctx
->sample_mask
;
700 void cso_restore_sample_mask(struct cso_context
*ctx
)
702 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
705 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
706 const struct pipe_stencil_ref
*sr
)
708 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
709 ctx
->stencil_ref
= *sr
;
710 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
715 void cso_save_stencil_ref(struct cso_context
*ctx
)
717 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
721 void cso_restore_stencil_ref(struct cso_context
*ctx
)
723 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
724 sizeof(ctx
->stencil_ref
))) {
725 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
726 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
730 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
733 assert(ctx
->has_geometry_shader
|| !handle
);
735 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
736 ctx
->geometry_shader
= handle
;
737 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
742 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
744 if (handle
== ctx
->geometry_shader
) {
745 /* unbind before deleting */
746 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
747 ctx
->geometry_shader
= NULL
;
749 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
752 void cso_save_geometry_shader(struct cso_context
*ctx
)
754 if (!ctx
->has_geometry_shader
) {
758 assert(!ctx
->geometry_shader_saved
);
759 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
762 void cso_restore_geometry_shader(struct cso_context
*ctx
)
764 if (!ctx
->has_geometry_shader
) {
768 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
769 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
770 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
772 ctx
->geometry_shader_saved
= NULL
;
778 clip_state_cpy(struct pipe_clip_state
*dst
,
779 const struct pipe_clip_state
*src
)
781 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
785 clip_state_cmp(const struct pipe_clip_state
*a
,
786 const struct pipe_clip_state
*b
)
788 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
792 cso_set_clip(struct cso_context
*ctx
,
793 const struct pipe_clip_state
*clip
)
795 if (clip_state_cmp(&ctx
->clip
, clip
)) {
796 clip_state_cpy(&ctx
->clip
, clip
);
797 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
802 cso_save_clip(struct cso_context
*ctx
)
804 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
808 cso_restore_clip(struct cso_context
*ctx
)
810 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
811 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
812 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
817 cso_set_vertex_elements(struct cso_context
*ctx
,
819 const struct pipe_vertex_element
*states
)
821 struct u_vbuf
*vbuf
= ctx
->vbuf
;
822 unsigned key_size
, hash_key
;
823 struct cso_hash_iter iter
;
825 struct cso_velems_state velems_state
;
828 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
832 /* Need to include the count into the stored state data too.
833 * Otherwise first few count pipe_vertex_elements could be identical
834 * even if count is different, and there's no guarantee the hash would
835 * be different in that case neither.
837 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
838 velems_state
.count
= count
;
839 memcpy(velems_state
.velems
, states
,
840 sizeof(struct pipe_vertex_element
) * count
);
841 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
842 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
843 (void*)&velems_state
, key_size
);
845 if (cso_hash_iter_is_null(iter
)) {
846 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
848 return PIPE_ERROR_OUT_OF_MEMORY
;
850 memcpy(&cso
->state
, &velems_state
, key_size
);
851 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
852 &cso
->state
.velems
[0]);
854 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
855 cso
->context
= ctx
->pipe
;
857 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
858 if (cso_hash_iter_is_null(iter
)) {
860 return PIPE_ERROR_OUT_OF_MEMORY
;
866 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
869 if (ctx
->velements
!= handle
) {
870 ctx
->velements
= handle
;
871 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
876 void cso_save_vertex_elements(struct cso_context
*ctx
)
878 struct u_vbuf
*vbuf
= ctx
->vbuf
;
881 u_vbuf_save_vertex_elements(vbuf
);
885 assert(!ctx
->velements_saved
);
886 ctx
->velements_saved
= ctx
->velements
;
889 void cso_restore_vertex_elements(struct cso_context
*ctx
)
891 struct u_vbuf
*vbuf
= ctx
->vbuf
;
894 u_vbuf_restore_vertex_elements(vbuf
);
898 if (ctx
->velements
!= ctx
->velements_saved
) {
899 ctx
->velements
= ctx
->velements_saved
;
900 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
902 ctx
->velements_saved
= NULL
;
907 void cso_set_vertex_buffers(struct cso_context
*ctx
,
909 const struct pipe_vertex_buffer
*buffers
)
911 struct u_vbuf
*vbuf
= ctx
->vbuf
;
914 u_vbuf_set_vertex_buffers(vbuf
, count
, buffers
);
918 if (count
!= ctx
->nr_vertex_buffers
||
919 memcmp(buffers
, ctx
->vertex_buffers
,
920 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
921 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
923 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
927 void cso_save_vertex_buffers(struct cso_context
*ctx
)
929 struct u_vbuf
*vbuf
= ctx
->vbuf
;
932 u_vbuf_save_vertex_buffers(vbuf
);
936 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
937 &ctx
->nr_vertex_buffers_saved
,
939 ctx
->nr_vertex_buffers
);
942 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
945 struct u_vbuf
*vbuf
= ctx
->vbuf
;
948 u_vbuf_restore_vertex_buffers(vbuf
);
952 util_copy_vertex_buffers(ctx
->vertex_buffers
,
953 &ctx
->nr_vertex_buffers
,
954 ctx
->vertex_buffers_saved
,
955 ctx
->nr_vertex_buffers_saved
);
957 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
958 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
960 ctx
->nr_vertex_buffers_saved
= 0;
962 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
963 ctx
->vertex_buffers
);
967 /**************** fragment/vertex sampler view state *************************/
969 static enum pipe_error
970 single_sampler(struct cso_context
*ctx
,
971 struct sampler_info
*info
,
973 const struct pipe_sampler_state
*templ
)
978 unsigned key_size
= sizeof(struct pipe_sampler_state
);
979 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
980 struct cso_hash_iter iter
=
981 cso_find_state_template(ctx
->cache
,
982 hash_key
, CSO_SAMPLER
,
983 (void *) templ
, key_size
);
985 if (cso_hash_iter_is_null(iter
)) {
986 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
988 return PIPE_ERROR_OUT_OF_MEMORY
;
990 memcpy(&cso
->state
, templ
, sizeof(*templ
));
991 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
993 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
994 cso
->context
= ctx
->pipe
;
996 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
997 if (cso_hash_iter_is_null(iter
)) {
999 return PIPE_ERROR_OUT_OF_MEMORY
;
1005 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1009 info
->samplers
[idx
] = handle
;
1015 cso_single_sampler(struct cso_context
*ctx
,
1016 unsigned shader_stage
,
1018 const struct pipe_sampler_state
*templ
)
1020 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1026 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1028 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1031 /* find highest non-null sampler */
1032 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1033 if (info
->samplers
[i
- 1] != NULL
)
1037 info
->nr_samplers
= i
;
1039 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1040 memcmp(info
->hw
.samplers
,
1042 info
->nr_samplers
* sizeof(void *)) != 0)
1044 memcpy(info
->hw
.samplers
,
1046 info
->nr_samplers
* sizeof(void *));
1047 info
->hw
.nr_samplers
= info
->nr_samplers
;
1049 switch (shader_stage
) {
1050 case PIPE_SHADER_FRAGMENT
:
1051 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1055 case PIPE_SHADER_VERTEX
:
1056 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1060 case PIPE_SHADER_GEOMETRY
:
1061 ctx
->pipe
->bind_geometry_sampler_states(ctx
->pipe
,
1066 assert(!"bad shader type in single_sampler_done()");
1072 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1074 single_sampler_done(ctx
, shader_stage
);
1079 * If the function encouters any errors it will return the
1080 * last one. Done to always try to set as many samplers
1084 cso_set_samplers(struct cso_context
*ctx
,
1085 unsigned shader_stage
,
1087 const struct pipe_sampler_state
**templates
)
1089 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1091 enum pipe_error temp
, error
= PIPE_OK
;
1096 for (i
= 0; i
< nr
; i
++) {
1097 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1098 if (temp
!= PIPE_OK
)
1102 for ( ; i
< info
->nr_samplers
; i
++) {
1103 temp
= single_sampler(ctx
, info
, i
, NULL
);
1104 if (temp
!= PIPE_OK
)
1108 single_sampler_done(ctx
, shader_stage
);
1114 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1116 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1117 info
->nr_samplers_saved
= info
->nr_samplers
;
1118 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1123 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1125 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1126 info
->nr_samplers
= info
->nr_samplers_saved
;
1127 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1128 single_sampler_done(ctx
, shader_stage
);
1133 cso_set_sampler_views(struct cso_context
*ctx
,
1134 unsigned shader_stage
,
1136 struct pipe_sampler_view
**views
)
1138 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1141 /* reference new views */
1142 for (i
= 0; i
< count
; i
++) {
1143 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1145 /* unref extra old views, if any */
1146 for (; i
< info
->nr_views
; i
++) {
1147 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1150 info
->nr_views
= count
;
1152 /* bind the new sampler views */
1153 switch (shader_stage
) {
1154 case PIPE_SHADER_FRAGMENT
:
1155 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, count
, info
->views
);
1157 case PIPE_SHADER_VERTEX
:
1158 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, count
, info
->views
);
1160 case PIPE_SHADER_GEOMETRY
:
1161 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, count
, info
->views
);
1164 assert(!"bad shader type in cso_set_sampler_views()");
1170 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1172 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1175 info
->nr_views_saved
= info
->nr_views
;
1177 for (i
= 0; i
< info
->nr_views
; i
++) {
1178 assert(!info
->views_saved
[i
]);
1179 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1185 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1187 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1188 unsigned i
, nr_saved
= info
->nr_views_saved
;
1190 for (i
= 0; i
< nr_saved
; i
++) {
1191 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1192 /* move the reference from one pointer to another */
1193 info
->views
[i
] = info
->views_saved
[i
];
1194 info
->views_saved
[i
] = NULL
;
1196 for (; i
< info
->nr_views
; i
++) {
1197 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1200 /* bind the old/saved sampler views */
1201 switch (shader_stage
) {
1202 case PIPE_SHADER_FRAGMENT
:
1203 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1205 case PIPE_SHADER_VERTEX
:
1206 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1208 case PIPE_SHADER_GEOMETRY
:
1209 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1212 assert(!"bad shader type in cso_restore_sampler_views()");
1215 info
->nr_views
= nr_saved
;
1216 info
->nr_views_saved
= 0;
1221 cso_set_stream_outputs(struct cso_context
*ctx
,
1222 unsigned num_targets
,
1223 struct pipe_stream_output_target
**targets
,
1224 unsigned append_bitmask
)
1226 struct pipe_context
*pipe
= ctx
->pipe
;
1229 if (!ctx
->has_streamout
) {
1230 assert(num_targets
== 0);
1234 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1235 /* Nothing to do. */
1239 /* reference new targets */
1240 for (i
= 0; i
< num_targets
; i
++) {
1241 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1243 /* unref extra old targets, if any */
1244 for (; i
< ctx
->nr_so_targets
; i
++) {
1245 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1248 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1250 ctx
->nr_so_targets
= num_targets
;
1254 cso_save_stream_outputs(struct cso_context
*ctx
)
1258 if (!ctx
->has_streamout
) {
1262 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1264 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1265 assert(!ctx
->so_targets_saved
[i
]);
1266 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1271 cso_restore_stream_outputs(struct cso_context
*ctx
)
1273 struct pipe_context
*pipe
= ctx
->pipe
;
1276 if (!ctx
->has_streamout
) {
1280 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1281 /* Nothing to do. */
1285 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1286 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1287 /* move the reference from one pointer to another */
1288 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1289 ctx
->so_targets_saved
[i
] = NULL
;
1291 for (; i
< ctx
->nr_so_targets
; i
++) {
1292 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1295 /* ~0 means append */
1296 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1297 ctx
->so_targets
, ~0);
1299 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1300 ctx
->nr_so_targets_saved
= 0;
1306 cso_set_index_buffer(struct cso_context
*cso
,
1307 const struct pipe_index_buffer
*ib
)
1309 struct u_vbuf
*vbuf
= cso
->vbuf
;
1312 u_vbuf_set_index_buffer(vbuf
, ib
);
1314 struct pipe_context
*pipe
= cso
->pipe
;
1315 pipe
->set_index_buffer(pipe
, ib
);
1320 cso_draw_vbo(struct cso_context
*cso
,
1321 const struct pipe_draw_info
*info
)
1323 struct u_vbuf
*vbuf
= cso
->vbuf
;
1326 u_vbuf_draw_vbo(vbuf
, info
);
1328 struct pipe_context
*pipe
= cso
->pipe
;
1329 pipe
->draw_vbo(pipe
, info
);
1334 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1336 struct pipe_draw_info info
;
1338 util_draw_init_info(&info
);
1343 info
.min_index
= start
;
1344 info
.max_index
= start
+ count
- 1;
1346 cso_draw_vbo(cso
, &info
);