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 struct sampler_info fragment_samplers
;
82 struct sampler_info vertex_samplers
;
84 /** Current and saved state.
85 * The saved state is used as a 1-deep stack.
87 void *blend
, *blend_saved
;
88 void *depth_stencil
, *depth_stencil_saved
;
89 void *rasterizer
, *rasterizer_saved
;
90 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
91 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
92 void *velements
, *velements_saved
;
94 struct pipe_clip_state clip
;
95 struct pipe_clip_state clip_saved
;
97 struct pipe_framebuffer_state fb
, fb_saved
;
98 struct pipe_viewport_state vp
, vp_saved
;
99 struct pipe_blend_color blend_color
;
100 unsigned sample_mask
;
101 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
105 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
107 struct cso_blend
*cso
= (struct cso_blend
*)state
;
109 if (ctx
->blend
== cso
->data
)
112 if (cso
->delete_state
)
113 cso
->delete_state(cso
->context
, cso
->data
);
118 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
120 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
122 if (ctx
->depth_stencil
== cso
->data
)
125 if (cso
->delete_state
)
126 cso
->delete_state(cso
->context
, cso
->data
);
132 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
134 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
135 if (cso
->delete_state
)
136 cso
->delete_state(cso
->context
, cso
->data
);
141 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
143 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
145 if (ctx
->rasterizer
== cso
->data
)
147 if (cso
->delete_state
)
148 cso
->delete_state(cso
->context
, cso
->data
);
153 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
155 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
156 if (ctx
->fragment_shader
== cso
->data
)
158 if (cso
->delete_state
)
159 cso
->delete_state(cso
->context
, cso
->data
);
164 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
166 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
167 if (ctx
->vertex_shader
== cso
->data
)
169 if (cso
->delete_state
)
170 cso
->delete_state(cso
->context
, cso
->data
);
175 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
178 struct cso_velements
*cso
= (struct cso_velements
*)state
;
180 if (ctx
->velements
== cso
->data
)
183 if (cso
->delete_state
)
184 cso
->delete_state(cso
->context
, cso
->data
);
190 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
191 void *state
, enum cso_cache_type type
)
195 return delete_blend_state(ctx
, state
);
198 return delete_sampler_state(ctx
, state
);
200 case CSO_DEPTH_STENCIL_ALPHA
:
201 return delete_depth_stencil_state(ctx
, state
);
204 return delete_rasterizer_state(ctx
, state
);
206 case CSO_FRAGMENT_SHADER
:
207 return delete_fs_state(ctx
, state
);
209 case CSO_VERTEX_SHADER
:
210 return delete_vs_state(ctx
, state
);
213 return delete_vertex_elements(ctx
, state
);
222 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
223 int max_size
, void *user_data
)
225 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
226 /* if we're approach the maximum size, remove fourth of the entries
227 * otherwise every subsequent call will go through the same */
228 int hash_size
= cso_hash_size(hash
);
229 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
230 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
231 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
232 if (hash_size
> max_size
)
233 to_remove
+= hash_size
- max_size
;
235 /*remove elements until we're good */
236 /*fixme: currently we pick the nodes to remove at random*/
237 void *cso
= cso_hash_iter_data(iter
);
238 if (delete_cso(ctx
, cso
, type
)) {
239 iter
= cso_hash_erase(hash
, iter
);
242 iter
= cso_hash_iter_next(iter
);
247 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
249 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
253 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
255 ctx
->cache
= cso_cache_create();
256 if (ctx
->cache
== NULL
)
258 cso_cache_set_sanitize_callback(ctx
->cache
,
264 /* Enable for testing: */
265 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
270 cso_destroy_context( ctx
);
276 * Prior to context destruction, this function unbinds all state objects.
278 void cso_release_all( struct cso_context
*ctx
)
281 struct sampler_info
*info
;
284 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
285 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
286 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
287 if (ctx
->pipe
->bind_vertex_sampler_states
)
288 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
289 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
290 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
291 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
293 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
294 if (ctx
->pipe
->set_vertex_sampler_views
)
295 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
298 /* free fragment samplers, views */
299 info
= &ctx
->fragment_samplers
;
300 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
301 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
302 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
305 /* free vertex samplers, views */
306 info
= &ctx
->vertex_samplers
;
307 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
308 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
309 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
312 util_unreference_framebuffer_state(&ctx
->fb
);
313 util_unreference_framebuffer_state(&ctx
->fb_saved
);
316 cso_cache_delete( ctx
->cache
);
323 * Free the CSO context. NOTE: the state tracker should have previously called
326 void cso_destroy_context( struct cso_context
*ctx
)
334 /* Those function will either find the state of the given template
335 * in the cache or they will create a new state from the given
336 * template, insert it in the cache and return it.
340 * If the driver returns 0 from the create method then they will assign
341 * the data member of the cso to be the template itself.
344 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
345 const struct pipe_blend_state
*templ
)
347 unsigned key_size
, hash_key
;
348 struct cso_hash_iter iter
;
351 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
352 (char *)&(templ
->rt
[1]) - (char *)templ
;
353 hash_key
= cso_construct_key((void*)templ
, key_size
);
354 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
356 if (cso_hash_iter_is_null(iter
)) {
357 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
359 return PIPE_ERROR_OUT_OF_MEMORY
;
361 memset(&cso
->state
, 0, sizeof cso
->state
);
362 memcpy(&cso
->state
, templ
, key_size
);
363 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
364 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
365 cso
->context
= ctx
->pipe
;
367 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
368 if (cso_hash_iter_is_null(iter
)) {
370 return PIPE_ERROR_OUT_OF_MEMORY
;
376 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
379 if (ctx
->blend
!= handle
) {
381 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
386 void cso_save_blend(struct cso_context
*ctx
)
388 assert(!ctx
->blend_saved
);
389 ctx
->blend_saved
= ctx
->blend
;
392 void cso_restore_blend(struct cso_context
*ctx
)
394 if (ctx
->blend
!= ctx
->blend_saved
) {
395 ctx
->blend
= ctx
->blend_saved
;
396 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
398 ctx
->blend_saved
= NULL
;
403 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
404 const struct pipe_depth_stencil_alpha_state
*templ
)
406 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
407 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
408 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
410 CSO_DEPTH_STENCIL_ALPHA
,
411 (void*)templ
, key_size
);
414 if (cso_hash_iter_is_null(iter
)) {
415 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
417 return PIPE_ERROR_OUT_OF_MEMORY
;
419 memcpy(&cso
->state
, templ
, sizeof(*templ
));
420 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
421 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
422 cso
->context
= ctx
->pipe
;
424 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
425 if (cso_hash_iter_is_null(iter
)) {
427 return PIPE_ERROR_OUT_OF_MEMORY
;
433 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
436 if (ctx
->depth_stencil
!= handle
) {
437 ctx
->depth_stencil
= handle
;
438 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
443 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
445 assert(!ctx
->depth_stencil_saved
);
446 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
449 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
451 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
452 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
453 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
455 ctx
->depth_stencil_saved
= NULL
;
460 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
461 const struct pipe_rasterizer_state
*templ
)
463 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
464 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
465 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
466 hash_key
, CSO_RASTERIZER
,
467 (void*)templ
, key_size
);
470 if (cso_hash_iter_is_null(iter
)) {
471 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
473 return PIPE_ERROR_OUT_OF_MEMORY
;
475 memcpy(&cso
->state
, templ
, sizeof(*templ
));
476 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
477 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
478 cso
->context
= ctx
->pipe
;
480 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
481 if (cso_hash_iter_is_null(iter
)) {
483 return PIPE_ERROR_OUT_OF_MEMORY
;
489 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
492 if (ctx
->rasterizer
!= handle
) {
493 ctx
->rasterizer
= handle
;
494 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
499 void cso_save_rasterizer(struct cso_context
*ctx
)
501 assert(!ctx
->rasterizer_saved
);
502 ctx
->rasterizer_saved
= ctx
->rasterizer
;
505 void cso_restore_rasterizer(struct cso_context
*ctx
)
507 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
508 ctx
->rasterizer
= ctx
->rasterizer_saved
;
509 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
511 ctx
->rasterizer_saved
= NULL
;
516 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
519 if (ctx
->fragment_shader
!= handle
) {
520 ctx
->fragment_shader
= handle
;
521 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
526 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
528 if (handle
== ctx
->fragment_shader
) {
529 /* unbind before deleting */
530 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
531 ctx
->fragment_shader
= NULL
;
533 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
536 /* Not really working:
539 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
540 const struct pipe_shader_state
*templ
)
542 const struct tgsi_token
*tokens
= templ
->tokens
;
543 unsigned num_tokens
= tgsi_num_tokens(tokens
);
544 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
545 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
546 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
550 sizeof(*templ
)); /* XXX correct? tokens_size? */
553 if (cso_hash_iter_is_null(iter
)) {
554 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
555 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
558 return PIPE_ERROR_OUT_OF_MEMORY
;
560 memcpy(cso_tokens
, tokens
, tokens_size
);
561 cso
->state
.tokens
= cso_tokens
;
562 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
563 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
564 cso
->context
= ctx
->pipe
;
566 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
567 if (cso_hash_iter_is_null(iter
)) {
569 return PIPE_ERROR_OUT_OF_MEMORY
;
575 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
578 return cso_set_fragment_shader_handle( ctx
, handle
);
582 void cso_save_fragment_shader(struct cso_context
*ctx
)
584 assert(!ctx
->fragment_shader_saved
);
585 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
588 void cso_restore_fragment_shader(struct cso_context
*ctx
)
590 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
591 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
592 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
594 ctx
->fragment_shader_saved
= NULL
;
598 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
601 if (ctx
->vertex_shader
!= handle
) {
602 ctx
->vertex_shader
= handle
;
603 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
608 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
610 if (handle
== ctx
->vertex_shader
) {
611 /* unbind before deleting */
612 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
613 ctx
->vertex_shader
= NULL
;
615 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
619 /* Not really working:
622 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
623 const struct pipe_shader_state
*templ
)
625 unsigned hash_key
= cso_construct_key((void*)templ
,
626 sizeof(struct pipe_shader_state
));
627 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
628 hash_key
, CSO_VERTEX_SHADER
,
633 if (cso_hash_iter_is_null(iter
)) {
634 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
637 return PIPE_ERROR_OUT_OF_MEMORY
;
639 memcpy(cso
->state
, templ
, sizeof(*templ
));
640 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
641 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
642 cso
->context
= ctx
->pipe
;
644 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
645 if (cso_hash_iter_is_null(iter
)) {
647 return PIPE_ERROR_OUT_OF_MEMORY
;
653 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
656 return cso_set_vertex_shader_handle( ctx
, handle
);
662 void cso_save_vertex_shader(struct cso_context
*ctx
)
664 assert(!ctx
->vertex_shader_saved
);
665 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
668 void cso_restore_vertex_shader(struct cso_context
*ctx
)
670 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
671 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
672 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
674 ctx
->vertex_shader_saved
= NULL
;
678 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
679 const struct pipe_framebuffer_state
*fb
)
681 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
682 util_copy_framebuffer_state(&ctx
->fb
, fb
);
683 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
688 void cso_save_framebuffer(struct cso_context
*ctx
)
690 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
693 void cso_restore_framebuffer(struct cso_context
*ctx
)
695 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
696 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
697 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
698 util_unreference_framebuffer_state(&ctx
->fb_saved
);
703 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
704 const struct pipe_viewport_state
*vp
)
706 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
708 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
713 void cso_save_viewport(struct cso_context
*ctx
)
715 ctx
->vp_saved
= ctx
->vp
;
719 void cso_restore_viewport(struct cso_context
*ctx
)
721 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
722 ctx
->vp
= ctx
->vp_saved
;
723 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
728 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
729 const struct pipe_blend_color
*bc
)
731 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
732 ctx
->blend_color
= *bc
;
733 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
738 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
739 unsigned sample_mask
)
741 if (ctx
->sample_mask
!= sample_mask
) {
742 ctx
->sample_mask
= sample_mask
;
743 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
748 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
749 const struct pipe_stencil_ref
*sr
)
751 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
752 ctx
->stencil_ref
= *sr
;
753 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
758 void cso_save_stencil_ref(struct cso_context
*ctx
)
760 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
764 void cso_restore_stencil_ref(struct cso_context
*ctx
)
766 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
767 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
768 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
772 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
775 if (ctx
->geometry_shader
!= handle
) {
776 ctx
->geometry_shader
= handle
;
777 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
782 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
784 if (handle
== ctx
->geometry_shader
) {
785 /* unbind before deleting */
786 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
787 ctx
->geometry_shader
= NULL
;
789 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
792 void cso_save_geometry_shader(struct cso_context
*ctx
)
794 assert(!ctx
->geometry_shader_saved
);
795 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
798 void cso_restore_geometry_shader(struct cso_context
*ctx
)
800 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
801 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
802 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
804 ctx
->geometry_shader_saved
= NULL
;
810 clip_state_cpy(struct pipe_clip_state
*dst
,
811 const struct pipe_clip_state
*src
)
813 dst
->depth_clamp
= src
->depth_clamp
;
816 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
821 clip_state_cmp(const struct pipe_clip_state
*a
,
822 const struct pipe_clip_state
*b
)
824 if (a
->depth_clamp
!= b
->depth_clamp
) {
827 if (a
->nr
!= b
->nr
) {
831 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
837 cso_set_clip(struct cso_context
*ctx
,
838 const struct pipe_clip_state
*clip
)
840 if (clip_state_cmp(&ctx
->clip
, clip
)) {
841 clip_state_cpy(&ctx
->clip
, clip
);
842 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
847 cso_save_clip(struct cso_context
*ctx
)
849 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
853 cso_restore_clip(struct cso_context
*ctx
)
855 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
856 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
857 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
861 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
863 const struct pipe_vertex_element
*states
)
865 unsigned key_size
, hash_key
;
866 struct cso_hash_iter iter
;
868 struct cso_velems_state velems_state
;
870 /* need to include the count into the stored state data too.
871 Otherwise first few count pipe_vertex_elements could be identical even if count
872 is different, and there's no guarantee the hash would be different in that
874 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
875 velems_state
.count
= count
;
876 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
877 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
878 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
880 if (cso_hash_iter_is_null(iter
)) {
881 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
883 return PIPE_ERROR_OUT_OF_MEMORY
;
885 memcpy(&cso
->state
, &velems_state
, key_size
);
886 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
887 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
888 cso
->context
= ctx
->pipe
;
890 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
891 if (cso_hash_iter_is_null(iter
)) {
893 return PIPE_ERROR_OUT_OF_MEMORY
;
899 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
902 if (ctx
->velements
!= handle
) {
903 ctx
->velements
= handle
;
904 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
909 void cso_save_vertex_elements(struct cso_context
*ctx
)
911 assert(!ctx
->velements_saved
);
912 ctx
->velements_saved
= ctx
->velements
;
915 void cso_restore_vertex_elements(struct cso_context
*ctx
)
917 if (ctx
->velements
!= ctx
->velements_saved
) {
918 ctx
->velements
= ctx
->velements_saved
;
919 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
921 ctx
->velements_saved
= NULL
;
926 /**************** fragment/vertex sampler view state *************************/
928 static enum pipe_error
929 single_sampler(struct cso_context
*ctx
,
930 struct sampler_info
*info
,
932 const struct pipe_sampler_state
*templ
)
937 unsigned key_size
= sizeof(struct pipe_sampler_state
);
938 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
939 struct cso_hash_iter iter
=
940 cso_find_state_template(ctx
->cache
,
941 hash_key
, CSO_SAMPLER
,
942 (void *) templ
, key_size
);
944 if (cso_hash_iter_is_null(iter
)) {
945 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
947 return PIPE_ERROR_OUT_OF_MEMORY
;
949 memcpy(&cso
->state
, templ
, sizeof(*templ
));
950 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
951 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
952 cso
->context
= ctx
->pipe
;
954 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
955 if (cso_hash_iter_is_null(iter
)) {
957 return PIPE_ERROR_OUT_OF_MEMORY
;
963 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
967 info
->samplers
[idx
] = handle
;
973 cso_single_sampler(struct cso_context
*ctx
,
975 const struct pipe_sampler_state
*templ
)
977 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
981 cso_single_vertex_sampler(struct cso_context
*ctx
,
983 const struct pipe_sampler_state
*templ
)
985 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
991 single_sampler_done(struct cso_context
*ctx
,
992 struct sampler_info
*info
)
996 /* find highest non-null sampler */
997 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
998 if (info
->samplers
[i
- 1] != NULL
)
1002 info
->nr_samplers
= i
;
1004 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1005 memcmp(info
->hw
.samplers
,
1007 info
->nr_samplers
* sizeof(void *)) != 0)
1009 memcpy(info
->hw
.samplers
,
1011 info
->nr_samplers
* sizeof(void *));
1012 info
->hw
.nr_samplers
= info
->nr_samplers
;
1014 if (info
== &ctx
->fragment_samplers
) {
1015 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1019 else if (info
== &ctx
->vertex_samplers
) {
1020 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1031 cso_single_sampler_done( struct cso_context
*ctx
)
1033 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
1037 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
1039 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1044 * If the function encouters any errors it will return the
1045 * last one. Done to always try to set as many samplers
1048 static enum pipe_error
1049 set_samplers(struct cso_context
*ctx
,
1050 struct sampler_info
*info
,
1052 const struct pipe_sampler_state
**templates
)
1055 enum pipe_error temp
, error
= PIPE_OK
;
1060 for (i
= 0; i
< nr
; i
++) {
1061 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1062 if (temp
!= PIPE_OK
)
1066 for ( ; i
< info
->nr_samplers
; i
++) {
1067 temp
= single_sampler(ctx
, info
, i
, NULL
);
1068 if (temp
!= PIPE_OK
)
1072 single_sampler_done(ctx
, info
);
1078 cso_set_samplers(struct cso_context
*ctx
,
1080 const struct pipe_sampler_state
**templates
)
1082 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1086 cso_set_vertex_samplers(struct cso_context
*ctx
,
1088 const struct pipe_sampler_state
**templates
)
1090 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1096 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1098 info
->nr_samplers_saved
= info
->nr_samplers
;
1099 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1103 cso_save_samplers(struct cso_context
*ctx
)
1105 save_samplers(ctx
, &ctx
->fragment_samplers
);
1109 cso_save_vertex_samplers(struct cso_context
*ctx
)
1111 save_samplers(ctx
, &ctx
->vertex_samplers
);
1117 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1119 info
->nr_samplers
= info
->nr_samplers_saved
;
1120 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1121 single_sampler_done(ctx
, info
);
1125 cso_restore_samplers(struct cso_context
*ctx
)
1127 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1131 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1133 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1139 set_sampler_views(struct cso_context
*ctx
,
1140 struct sampler_info
*info
,
1141 void (*set_views
)(struct pipe_context
*,
1143 struct pipe_sampler_view
**),
1145 struct pipe_sampler_view
**views
)
1149 /* reference new views */
1150 for (i
= 0; i
< count
; i
++) {
1151 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1153 /* unref extra old views, if any */
1154 for (; i
< info
->nr_views
; i
++) {
1155 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1158 info
->nr_views
= count
;
1160 /* bind the new sampler views */
1161 set_views(ctx
->pipe
, count
, info
->views
);
1165 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1167 struct pipe_sampler_view
**views
)
1169 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1170 ctx
->pipe
->set_fragment_sampler_views
,
1175 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1177 struct pipe_sampler_view
**views
)
1179 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1180 ctx
->pipe
->set_vertex_sampler_views
,
1187 save_sampler_views(struct cso_context
*ctx
,
1188 struct sampler_info
*info
)
1192 info
->nr_views_saved
= info
->nr_views
;
1194 for (i
= 0; i
< info
->nr_views
; i
++) {
1195 assert(!info
->views_saved
[i
]);
1196 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1201 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1203 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1207 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1209 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1214 restore_sampler_views(struct cso_context
*ctx
,
1215 struct sampler_info
*info
,
1216 void (*set_views
)(struct pipe_context
*,
1218 struct pipe_sampler_view
**))
1222 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1223 pipe_sampler_view_reference(&info
->views
[i
], info
->views_saved
[i
]);
1224 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
1226 for (; i
< info
->nr_views
; i
++) {
1227 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1230 /* bind the old/saved sampler views */
1231 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1233 info
->nr_views
= info
->nr_views_saved
;
1234 info
->nr_views_saved
= 0;
1238 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1240 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1241 ctx
->pipe
->set_fragment_sampler_views
);
1245 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1247 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1248 ctx
->pipe
->set_vertex_sampler_views
);