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_framebuffer.h"
40 #include "util/u_inlines.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "tgsi/tgsi_parse.h"
45 #include "cso_cache/cso_context.h"
46 #include "cso_cache/cso_cache.h"
47 #include "cso_cache/cso_hash.h"
48 #include "cso_context.h"
52 * Info related to samplers and sampler views.
53 * We have one of these for fragment samplers and another for vertex samplers.
58 void *samplers
[PIPE_MAX_SAMPLERS
];
62 void *samplers
[PIPE_MAX_SAMPLERS
];
65 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
66 unsigned nr_samplers_saved
;
68 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
71 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
72 unsigned nr_views_saved
;
78 struct pipe_context
*pipe
;
79 struct cso_cache
*cache
;
81 boolean has_geometry_shader
;
83 struct sampler_info fragment_samplers
;
84 struct sampler_info vertex_samplers
;
86 uint nr_vertex_buffers
;
87 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
89 uint nr_vertex_buffers_saved
;
90 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
92 /** Current and saved state.
93 * The saved state is used as a 1-deep stack.
95 void *blend
, *blend_saved
;
96 void *depth_stencil
, *depth_stencil_saved
;
97 void *rasterizer
, *rasterizer_saved
;
98 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
99 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
100 void *velements
, *velements_saved
;
102 struct pipe_clip_state clip
;
103 struct pipe_clip_state clip_saved
;
105 struct pipe_framebuffer_state fb
, fb_saved
;
106 struct pipe_viewport_state vp
, vp_saved
;
107 struct pipe_blend_color blend_color
;
108 unsigned sample_mask
;
109 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
113 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
115 struct cso_blend
*cso
= (struct cso_blend
*)state
;
117 if (ctx
->blend
== cso
->data
)
120 if (cso
->delete_state
)
121 cso
->delete_state(cso
->context
, cso
->data
);
126 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
128 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
130 if (ctx
->depth_stencil
== cso
->data
)
133 if (cso
->delete_state
)
134 cso
->delete_state(cso
->context
, cso
->data
);
140 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
142 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
149 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
151 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
153 if (ctx
->rasterizer
== cso
->data
)
155 if (cso
->delete_state
)
156 cso
->delete_state(cso
->context
, cso
->data
);
161 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
163 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
164 if (ctx
->fragment_shader
== cso
->data
)
166 if (cso
->delete_state
)
167 cso
->delete_state(cso
->context
, cso
->data
);
172 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
174 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
175 if (ctx
->vertex_shader
== cso
->data
)
177 if (cso
->delete_state
)
178 cso
->delete_state(cso
->context
, cso
->data
);
183 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
186 struct cso_velements
*cso
= (struct cso_velements
*)state
;
188 if (ctx
->velements
== cso
->data
)
191 if (cso
->delete_state
)
192 cso
->delete_state(cso
->context
, cso
->data
);
198 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
199 void *state
, enum cso_cache_type type
)
203 return delete_blend_state(ctx
, state
);
206 return delete_sampler_state(ctx
, state
);
208 case CSO_DEPTH_STENCIL_ALPHA
:
209 return delete_depth_stencil_state(ctx
, state
);
212 return delete_rasterizer_state(ctx
, state
);
214 case CSO_FRAGMENT_SHADER
:
215 return delete_fs_state(ctx
, state
);
217 case CSO_VERTEX_SHADER
:
218 return delete_vs_state(ctx
, state
);
221 return delete_vertex_elements(ctx
, state
);
230 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
231 int max_size
, void *user_data
)
233 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
234 /* if we're approach the maximum size, remove fourth of the entries
235 * otherwise every subsequent call will go through the same */
236 int hash_size
= cso_hash_size(hash
);
237 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
238 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
239 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
240 if (hash_size
> max_size
)
241 to_remove
+= hash_size
- max_size
;
243 /*remove elements until we're good */
244 /*fixme: currently we pick the nodes to remove at random*/
245 void *cso
= cso_hash_iter_data(iter
);
246 if (delete_cso(ctx
, cso
, type
)) {
247 iter
= cso_hash_erase(hash
, iter
);
250 iter
= cso_hash_iter_next(iter
);
255 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
257 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
261 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
263 ctx
->cache
= cso_cache_create();
264 if (ctx
->cache
== NULL
)
266 cso_cache_set_sanitize_callback(ctx
->cache
,
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
;
283 cso_destroy_context( ctx
);
289 * Prior to context destruction, this function unbinds all state objects.
291 void cso_release_all( struct cso_context
*ctx
)
294 struct sampler_info
*info
;
297 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
298 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
299 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
300 if (ctx
->pipe
->bind_vertex_sampler_states
)
301 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
302 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
303 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
304 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
305 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
306 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
307 if (ctx
->pipe
->set_vertex_sampler_views
)
308 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
311 /* free fragment samplers, views */
312 info
= &ctx
->fragment_samplers
;
313 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
314 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
315 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
318 /* free vertex samplers, views */
319 info
= &ctx
->vertex_samplers
;
320 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
321 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
322 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
325 util_unreference_framebuffer_state(&ctx
->fb
);
326 util_unreference_framebuffer_state(&ctx
->fb_saved
);
328 util_copy_vertex_buffers(ctx
->vertex_buffers
,
329 &ctx
->nr_vertex_buffers
,
331 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
332 &ctx
->nr_vertex_buffers_saved
,
336 cso_cache_delete( ctx
->cache
);
343 * Free the CSO context. NOTE: the state tracker should have previously called
346 void cso_destroy_context( struct cso_context
*ctx
)
354 /* Those function will either find the state of the given template
355 * in the cache or they will create a new state from the given
356 * template, insert it in the cache and return it.
360 * If the driver returns 0 from the create method then they will assign
361 * the data member of the cso to be the template itself.
364 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
365 const struct pipe_blend_state
*templ
)
367 unsigned key_size
, hash_key
;
368 struct cso_hash_iter iter
;
371 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
372 (char *)&(templ
->rt
[1]) - (char *)templ
;
373 hash_key
= cso_construct_key((void*)templ
, key_size
);
374 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
376 if (cso_hash_iter_is_null(iter
)) {
377 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
379 return PIPE_ERROR_OUT_OF_MEMORY
;
381 memset(&cso
->state
, 0, sizeof cso
->state
);
382 memcpy(&cso
->state
, templ
, key_size
);
383 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
384 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
385 cso
->context
= ctx
->pipe
;
387 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
388 if (cso_hash_iter_is_null(iter
)) {
390 return PIPE_ERROR_OUT_OF_MEMORY
;
396 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
399 if (ctx
->blend
!= handle
) {
401 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
406 void cso_save_blend(struct cso_context
*ctx
)
408 assert(!ctx
->blend_saved
);
409 ctx
->blend_saved
= ctx
->blend
;
412 void cso_restore_blend(struct cso_context
*ctx
)
414 if (ctx
->blend
!= ctx
->blend_saved
) {
415 ctx
->blend
= ctx
->blend_saved
;
416 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
418 ctx
->blend_saved
= NULL
;
423 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
424 const struct pipe_depth_stencil_alpha_state
*templ
)
426 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
427 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
428 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
430 CSO_DEPTH_STENCIL_ALPHA
,
431 (void*)templ
, key_size
);
434 if (cso_hash_iter_is_null(iter
)) {
435 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
437 return PIPE_ERROR_OUT_OF_MEMORY
;
439 memcpy(&cso
->state
, templ
, sizeof(*templ
));
440 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
441 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
442 cso
->context
= ctx
->pipe
;
444 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
445 if (cso_hash_iter_is_null(iter
)) {
447 return PIPE_ERROR_OUT_OF_MEMORY
;
453 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
456 if (ctx
->depth_stencil
!= handle
) {
457 ctx
->depth_stencil
= handle
;
458 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
463 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
465 assert(!ctx
->depth_stencil_saved
);
466 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
469 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
471 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
472 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
473 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
475 ctx
->depth_stencil_saved
= NULL
;
480 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
481 const struct pipe_rasterizer_state
*templ
)
483 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
484 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
485 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
486 hash_key
, CSO_RASTERIZER
,
487 (void*)templ
, key_size
);
490 if (cso_hash_iter_is_null(iter
)) {
491 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
493 return PIPE_ERROR_OUT_OF_MEMORY
;
495 memcpy(&cso
->state
, templ
, sizeof(*templ
));
496 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
497 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
498 cso
->context
= ctx
->pipe
;
500 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
501 if (cso_hash_iter_is_null(iter
)) {
503 return PIPE_ERROR_OUT_OF_MEMORY
;
509 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
512 if (ctx
->rasterizer
!= handle
) {
513 ctx
->rasterizer
= handle
;
514 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
519 void cso_save_rasterizer(struct cso_context
*ctx
)
521 assert(!ctx
->rasterizer_saved
);
522 ctx
->rasterizer_saved
= ctx
->rasterizer
;
525 void cso_restore_rasterizer(struct cso_context
*ctx
)
527 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
528 ctx
->rasterizer
= ctx
->rasterizer_saved
;
529 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
531 ctx
->rasterizer_saved
= NULL
;
536 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
539 if (ctx
->fragment_shader
!= handle
) {
540 ctx
->fragment_shader
= handle
;
541 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
546 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
548 if (handle
== ctx
->fragment_shader
) {
549 /* unbind before deleting */
550 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
551 ctx
->fragment_shader
= NULL
;
553 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
556 /* Not really working:
559 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
560 const struct pipe_shader_state
*templ
)
562 const struct tgsi_token
*tokens
= templ
->tokens
;
563 unsigned num_tokens
= tgsi_num_tokens(tokens
);
564 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
565 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
566 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
570 sizeof(*templ
)); /* XXX correct? tokens_size? */
573 if (cso_hash_iter_is_null(iter
)) {
574 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
575 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
578 return PIPE_ERROR_OUT_OF_MEMORY
;
580 memcpy(cso_tokens
, tokens
, tokens_size
);
581 cso
->state
.tokens
= cso_tokens
;
582 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
583 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
584 cso
->context
= ctx
->pipe
;
586 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
587 if (cso_hash_iter_is_null(iter
)) {
589 return PIPE_ERROR_OUT_OF_MEMORY
;
595 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
598 return cso_set_fragment_shader_handle( ctx
, handle
);
602 void cso_save_fragment_shader(struct cso_context
*ctx
)
604 assert(!ctx
->fragment_shader_saved
);
605 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
608 void cso_restore_fragment_shader(struct cso_context
*ctx
)
610 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
611 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
612 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
614 ctx
->fragment_shader_saved
= NULL
;
618 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
621 if (ctx
->vertex_shader
!= handle
) {
622 ctx
->vertex_shader
= handle
;
623 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
628 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
630 if (handle
== ctx
->vertex_shader
) {
631 /* unbind before deleting */
632 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
633 ctx
->vertex_shader
= NULL
;
635 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
639 /* Not really working:
642 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
643 const struct pipe_shader_state
*templ
)
645 unsigned hash_key
= cso_construct_key((void*)templ
,
646 sizeof(struct pipe_shader_state
));
647 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
648 hash_key
, CSO_VERTEX_SHADER
,
653 if (cso_hash_iter_is_null(iter
)) {
654 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
657 return PIPE_ERROR_OUT_OF_MEMORY
;
659 memcpy(cso
->state
, templ
, sizeof(*templ
));
660 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
661 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
662 cso
->context
= ctx
->pipe
;
664 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
665 if (cso_hash_iter_is_null(iter
)) {
667 return PIPE_ERROR_OUT_OF_MEMORY
;
673 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
676 return cso_set_vertex_shader_handle( ctx
, handle
);
682 void cso_save_vertex_shader(struct cso_context
*ctx
)
684 assert(!ctx
->vertex_shader_saved
);
685 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
688 void cso_restore_vertex_shader(struct cso_context
*ctx
)
690 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
691 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
692 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
694 ctx
->vertex_shader_saved
= NULL
;
698 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
699 const struct pipe_framebuffer_state
*fb
)
701 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
702 util_copy_framebuffer_state(&ctx
->fb
, fb
);
703 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
708 void cso_save_framebuffer(struct cso_context
*ctx
)
710 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
713 void cso_restore_framebuffer(struct cso_context
*ctx
)
715 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
716 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
717 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
718 util_unreference_framebuffer_state(&ctx
->fb_saved
);
723 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
724 const struct pipe_viewport_state
*vp
)
726 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
728 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
733 void cso_save_viewport(struct cso_context
*ctx
)
735 ctx
->vp_saved
= ctx
->vp
;
739 void cso_restore_viewport(struct cso_context
*ctx
)
741 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
742 ctx
->vp
= ctx
->vp_saved
;
743 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
748 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
749 const struct pipe_blend_color
*bc
)
751 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
752 ctx
->blend_color
= *bc
;
753 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
758 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
759 unsigned sample_mask
)
761 if (ctx
->sample_mask
!= sample_mask
) {
762 ctx
->sample_mask
= sample_mask
;
763 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
768 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
769 const struct pipe_stencil_ref
*sr
)
771 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
772 ctx
->stencil_ref
= *sr
;
773 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
778 void cso_save_stencil_ref(struct cso_context
*ctx
)
780 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
784 void cso_restore_stencil_ref(struct cso_context
*ctx
)
786 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
787 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
788 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
792 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
795 assert(ctx
->has_geometry_shader
|| !handle
);
797 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
798 ctx
->geometry_shader
= handle
;
799 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
804 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
806 if (handle
== ctx
->geometry_shader
) {
807 /* unbind before deleting */
808 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
809 ctx
->geometry_shader
= NULL
;
811 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
814 void cso_save_geometry_shader(struct cso_context
*ctx
)
816 if (!ctx
->has_geometry_shader
) {
820 assert(!ctx
->geometry_shader_saved
);
821 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
824 void cso_restore_geometry_shader(struct cso_context
*ctx
)
826 if (!ctx
->has_geometry_shader
) {
830 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
831 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
832 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
834 ctx
->geometry_shader_saved
= NULL
;
840 clip_state_cpy(struct pipe_clip_state
*dst
,
841 const struct pipe_clip_state
*src
)
843 dst
->depth_clamp
= src
->depth_clamp
;
846 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
851 clip_state_cmp(const struct pipe_clip_state
*a
,
852 const struct pipe_clip_state
*b
)
854 if (a
->depth_clamp
!= b
->depth_clamp
) {
857 if (a
->nr
!= b
->nr
) {
861 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
867 cso_set_clip(struct cso_context
*ctx
,
868 const struct pipe_clip_state
*clip
)
870 if (clip_state_cmp(&ctx
->clip
, clip
)) {
871 clip_state_cpy(&ctx
->clip
, clip
);
872 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
877 cso_save_clip(struct cso_context
*ctx
)
879 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
883 cso_restore_clip(struct cso_context
*ctx
)
885 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
886 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
887 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
891 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
893 const struct pipe_vertex_element
*states
)
895 unsigned key_size
, hash_key
;
896 struct cso_hash_iter iter
;
898 struct cso_velems_state velems_state
;
900 /* need to include the count into the stored state data too.
901 Otherwise first few count pipe_vertex_elements could be identical even if count
902 is different, and there's no guarantee the hash would be different in that
904 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
905 velems_state
.count
= count
;
906 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
907 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
908 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
910 if (cso_hash_iter_is_null(iter
)) {
911 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
913 return PIPE_ERROR_OUT_OF_MEMORY
;
915 memcpy(&cso
->state
, &velems_state
, key_size
);
916 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
917 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
918 cso
->context
= ctx
->pipe
;
920 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
921 if (cso_hash_iter_is_null(iter
)) {
923 return PIPE_ERROR_OUT_OF_MEMORY
;
929 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
932 if (ctx
->velements
!= handle
) {
933 ctx
->velements
= handle
;
934 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
939 void cso_save_vertex_elements(struct cso_context
*ctx
)
941 assert(!ctx
->velements_saved
);
942 ctx
->velements_saved
= ctx
->velements
;
945 void cso_restore_vertex_elements(struct cso_context
*ctx
)
947 if (ctx
->velements
!= ctx
->velements_saved
) {
948 ctx
->velements
= ctx
->velements_saved
;
949 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
951 ctx
->velements_saved
= NULL
;
956 void cso_set_vertex_buffers(struct cso_context
*ctx
,
958 const struct pipe_vertex_buffer
*buffers
)
960 if (count
!= ctx
->nr_vertex_buffers
||
961 memcmp(buffers
, ctx
->vertex_buffers
,
962 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
963 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
965 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
969 void cso_save_vertex_buffers(struct cso_context
*ctx
)
971 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
972 &ctx
->nr_vertex_buffers_saved
,
974 ctx
->nr_vertex_buffers
);
977 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
979 util_copy_vertex_buffers(ctx
->vertex_buffers
,
980 &ctx
->nr_vertex_buffers
,
981 ctx
->vertex_buffers_saved
,
982 ctx
->nr_vertex_buffers_saved
);
983 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
984 ctx
->vertex_buffers
);
988 /**************** fragment/vertex sampler view state *************************/
990 static enum pipe_error
991 single_sampler(struct cso_context
*ctx
,
992 struct sampler_info
*info
,
994 const struct pipe_sampler_state
*templ
)
999 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1000 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1001 struct cso_hash_iter iter
=
1002 cso_find_state_template(ctx
->cache
,
1003 hash_key
, CSO_SAMPLER
,
1004 (void *) templ
, key_size
);
1006 if (cso_hash_iter_is_null(iter
)) {
1007 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1009 return PIPE_ERROR_OUT_OF_MEMORY
;
1011 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1012 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1013 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
1014 cso
->context
= ctx
->pipe
;
1016 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1017 if (cso_hash_iter_is_null(iter
)) {
1019 return PIPE_ERROR_OUT_OF_MEMORY
;
1025 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1029 info
->samplers
[idx
] = handle
;
1035 cso_single_sampler(struct cso_context
*ctx
,
1037 const struct pipe_sampler_state
*templ
)
1039 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
1043 cso_single_vertex_sampler(struct cso_context
*ctx
,
1045 const struct pipe_sampler_state
*templ
)
1047 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
1053 single_sampler_done(struct cso_context
*ctx
,
1054 struct sampler_info
*info
)
1058 /* find highest non-null sampler */
1059 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1060 if (info
->samplers
[i
- 1] != NULL
)
1064 info
->nr_samplers
= i
;
1066 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1067 memcmp(info
->hw
.samplers
,
1069 info
->nr_samplers
* sizeof(void *)) != 0)
1071 memcpy(info
->hw
.samplers
,
1073 info
->nr_samplers
* sizeof(void *));
1074 info
->hw
.nr_samplers
= info
->nr_samplers
;
1076 if (info
== &ctx
->fragment_samplers
) {
1077 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1081 else if (info
== &ctx
->vertex_samplers
) {
1082 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1093 cso_single_sampler_done( struct cso_context
*ctx
)
1095 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
1099 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
1101 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1106 * If the function encouters any errors it will return the
1107 * last one. Done to always try to set as many samplers
1110 static enum pipe_error
1111 set_samplers(struct cso_context
*ctx
,
1112 struct sampler_info
*info
,
1114 const struct pipe_sampler_state
**templates
)
1117 enum pipe_error temp
, error
= PIPE_OK
;
1122 for (i
= 0; i
< nr
; i
++) {
1123 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1124 if (temp
!= PIPE_OK
)
1128 for ( ; i
< info
->nr_samplers
; i
++) {
1129 temp
= single_sampler(ctx
, info
, i
, NULL
);
1130 if (temp
!= PIPE_OK
)
1134 single_sampler_done(ctx
, info
);
1140 cso_set_samplers(struct cso_context
*ctx
,
1142 const struct pipe_sampler_state
**templates
)
1144 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1148 cso_set_vertex_samplers(struct cso_context
*ctx
,
1150 const struct pipe_sampler_state
**templates
)
1152 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1158 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1160 info
->nr_samplers_saved
= info
->nr_samplers
;
1161 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1165 cso_save_samplers(struct cso_context
*ctx
)
1167 save_samplers(ctx
, &ctx
->fragment_samplers
);
1171 cso_save_vertex_samplers(struct cso_context
*ctx
)
1173 save_samplers(ctx
, &ctx
->vertex_samplers
);
1179 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1181 info
->nr_samplers
= info
->nr_samplers_saved
;
1182 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1183 single_sampler_done(ctx
, info
);
1187 cso_restore_samplers(struct cso_context
*ctx
)
1189 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1193 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1195 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1201 set_sampler_views(struct cso_context
*ctx
,
1202 struct sampler_info
*info
,
1203 void (*set_views
)(struct pipe_context
*,
1205 struct pipe_sampler_view
**),
1207 struct pipe_sampler_view
**views
)
1211 /* reference new views */
1212 for (i
= 0; i
< count
; i
++) {
1213 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1215 /* unref extra old views, if any */
1216 for (; i
< info
->nr_views
; i
++) {
1217 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1220 info
->nr_views
= count
;
1222 /* bind the new sampler views */
1223 set_views(ctx
->pipe
, count
, info
->views
);
1227 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1229 struct pipe_sampler_view
**views
)
1231 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1232 ctx
->pipe
->set_fragment_sampler_views
,
1237 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1239 struct pipe_sampler_view
**views
)
1241 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1242 ctx
->pipe
->set_vertex_sampler_views
,
1249 save_sampler_views(struct cso_context
*ctx
,
1250 struct sampler_info
*info
)
1254 info
->nr_views_saved
= info
->nr_views
;
1256 for (i
= 0; i
< info
->nr_views
; i
++) {
1257 assert(!info
->views_saved
[i
]);
1258 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1263 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1265 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1269 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1271 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1276 restore_sampler_views(struct cso_context
*ctx
,
1277 struct sampler_info
*info
,
1278 void (*set_views
)(struct pipe_context
*,
1280 struct pipe_sampler_view
**))
1284 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1285 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1286 /* move the reference from one pointer to another */
1287 info
->views
[i
] = info
->views_saved
[i
];
1288 info
->views_saved
[i
] = NULL
;
1290 for (; i
< info
->nr_views
; i
++) {
1291 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1294 /* bind the old/saved sampler views */
1295 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1297 info
->nr_views
= info
->nr_views_saved
;
1298 info
->nr_views_saved
= 0;
1302 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1304 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1305 ctx
->pipe
->set_fragment_sampler_views
);
1309 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1311 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1312 ctx
->pipe
->set_vertex_sampler_views
);