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_memory.h"
40 #include "tgsi/tgsi_parse.h"
42 #include "cso_cache/cso_context.h"
43 #include "cso_cache/cso_cache.h"
44 #include "cso_cache/cso_hash.h"
47 struct pipe_context
*pipe
;
48 struct cso_cache
*cache
;
51 void *samplers
[PIPE_MAX_SAMPLERS
];
55 void *samplers
[PIPE_MAX_SAMPLERS
];
58 unsigned nr_samplers_saved
;
59 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
61 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
64 uint nr_textures_saved
;
65 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
67 /** Current and saved state.
68 * The saved state is used as a 1-deep stack.
70 void *blend
, *blend_saved
;
71 void *depth_stencil
, *depth_stencil_saved
;
72 void *rasterizer
, *rasterizer_saved
;
73 void *fragment_shader
, *fragment_shader_saved
;
74 void *vertex_shader
, *vertex_shader_saved
;
76 struct pipe_framebuffer_state fb
, fb_saved
;
77 struct pipe_viewport_state vp
, vp_saved
;
78 struct pipe_blend_color blend_color
;
83 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
86 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
88 struct cso_blend
*cso
= (struct cso_blend
*)state
;
90 if (ctx
->blend
== cso
->data
)
93 if (cso
->delete_state
)
94 cso
->delete_state(cso
->context
, cso
->data
);
99 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
101 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
103 if (ctx
->depth_stencil
== cso
->data
)
106 if (cso
->delete_state
)
107 cso
->delete_state(cso
->context
, cso
->data
);
113 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
115 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
116 if (cso
->delete_state
)
117 cso
->delete_state(cso
->context
, cso
->data
);
122 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
124 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
126 if (ctx
->rasterizer
== cso
->data
)
128 if (cso
->delete_state
)
129 cso
->delete_state(cso
->context
, cso
->data
);
134 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
136 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
137 if (ctx
->fragment_shader
== cso
->data
)
139 if (cso
->delete_state
)
140 cso
->delete_state(cso
->context
, cso
->data
);
145 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
147 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
148 if (ctx
->vertex_shader
== cso
->data
)
150 if (cso
->delete_state
)
151 cso
->delete_state(cso
->context
, cso
->data
);
157 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
158 void *state
, enum cso_cache_type type
)
162 return delete_blend_state(ctx
, state
);
165 return delete_sampler_state(ctx
, state
);
167 case CSO_DEPTH_STENCIL_ALPHA
:
168 return delete_depth_stencil_state(ctx
, state
);
171 return delete_rasterizer_state(ctx
, state
);
173 case CSO_FRAGMENT_SHADER
:
174 return delete_fs_state(ctx
, state
);
176 case CSO_VERTEX_SHADER
:
177 return delete_vs_state(ctx
, state
);
186 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
187 int max_size
, void *user_data
)
189 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
190 /* if we're approach the maximum size, remove fourth of the entries
191 * otherwise every subsequent call will go through the same */
192 int hash_size
= cso_hash_size(hash
);
193 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
194 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
195 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
196 if (hash_size
> max_size
)
197 to_remove
+= hash_size
- max_size
;
199 /*remove elements until we're good */
200 /*fixme: currently we pick the nodes to remove at random*/
201 void *cso
= cso_hash_iter_data(iter
);
202 if (delete_cso(ctx
, cso
, type
)) {
203 iter
= cso_hash_erase(hash
, iter
);
206 iter
= cso_hash_iter_next(iter
);
211 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
213 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
217 ctx
->cache
= cso_cache_create();
218 if (ctx
->cache
== NULL
)
220 cso_cache_set_sanitize_callback(ctx
->cache
,
226 /* Enable for testing: */
227 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
232 cso_destroy_context( ctx
);
238 * Prior to context destruction, this function unbinds all state objects.
240 void cso_release_all( struct cso_context
*ctx
)
245 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
246 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
247 ctx
->pipe
->bind_sampler_states( ctx
->pipe
, 0, NULL
);
248 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
249 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
250 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
253 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
254 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
255 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
258 free_framebuffer_state(&ctx
->fb
);
259 free_framebuffer_state(&ctx
->fb_saved
);
262 cso_cache_delete( ctx
->cache
);
268 void cso_destroy_context( struct cso_context
*ctx
)
271 /*cso_release_all( ctx );*/
277 /* Those function will either find the state of the given template
278 * in the cache or they will create a new state from the given
279 * template, insert it in the cache and return it.
283 * If the driver returns 0 from the create method then they will assign
284 * the data member of the cso to be the template itself.
287 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
288 const struct pipe_blend_state
*templ
)
290 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_blend_state
));
291 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
296 if (cso_hash_iter_is_null(iter
)) {
297 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
299 return PIPE_ERROR_OUT_OF_MEMORY
;
301 memcpy(&cso
->state
, templ
, sizeof(*templ
));
302 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
303 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
304 cso
->context
= ctx
->pipe
;
306 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
307 if (cso_hash_iter_is_null(iter
)) {
309 return PIPE_ERROR_OUT_OF_MEMORY
;
315 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
318 if (ctx
->blend
!= handle
) {
320 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
325 void cso_save_blend(struct cso_context
*ctx
)
327 assert(!ctx
->blend_saved
);
328 ctx
->blend_saved
= ctx
->blend
;
331 void cso_restore_blend(struct cso_context
*ctx
)
333 if (ctx
->blend
!= ctx
->blend_saved
) {
334 ctx
->blend
= ctx
->blend_saved
;
335 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
337 ctx
->blend_saved
= NULL
;
342 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
344 const struct pipe_sampler_state
*templ
)
349 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
350 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
351 hash_key
, CSO_SAMPLER
,
354 if (cso_hash_iter_is_null(iter
)) {
355 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
357 return PIPE_ERROR_OUT_OF_MEMORY
;
359 memcpy(&cso
->state
, templ
, sizeof(*templ
));
360 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
361 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
362 cso
->context
= ctx
->pipe
;
364 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
365 if (cso_hash_iter_is_null(iter
)) {
367 return PIPE_ERROR_OUT_OF_MEMORY
;
373 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
377 ctx
->samplers
[idx
] = handle
;
381 void cso_single_sampler_done( struct cso_context
*ctx
)
385 /* find highest non-null sampler */
386 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
387 if (ctx
->samplers
[i
- 1] != NULL
)
391 ctx
->nr_samplers
= i
;
393 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
394 memcmp(ctx
->hw
.samplers
,
396 ctx
->nr_samplers
* sizeof(void *)) != 0)
398 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
399 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
401 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
406 * If the function encouters any errors it will return the
407 * last one. Done to always try to set as many samplers
410 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
412 const struct pipe_sampler_state
**templates
)
415 enum pipe_error temp
, error
= PIPE_OK
;
420 for (i
= 0; i
< nr
; i
++) {
421 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
426 for ( ; i
< ctx
->nr_samplers
; i
++) {
427 temp
= cso_single_sampler( ctx
, i
, NULL
);
432 cso_single_sampler_done( ctx
);
437 void cso_save_samplers(struct cso_context
*ctx
)
439 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
440 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
443 void cso_restore_samplers(struct cso_context
*ctx
)
445 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
446 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
447 cso_single_sampler_done( ctx
);
451 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
453 struct pipe_texture
**textures
)
457 ctx
->nr_textures
= count
;
459 for (i
= 0; i
< count
; i
++)
460 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
461 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
462 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
464 ctx
->pipe
->set_sampler_textures(ctx
->pipe
, count
, textures
);
469 void cso_save_sampler_textures( struct cso_context
*ctx
)
473 ctx
->nr_textures_saved
= ctx
->nr_textures
;
474 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
475 assert(!ctx
->textures_saved
[i
]);
476 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
480 void cso_restore_sampler_textures( struct cso_context
*ctx
)
484 ctx
->nr_textures
= ctx
->nr_textures_saved
;
486 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
487 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
488 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
489 ctx
->textures_saved
[i
] = NULL
;
491 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
492 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
494 ctx
->pipe
->set_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
496 ctx
->nr_textures_saved
= 0;
501 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
502 const struct pipe_depth_stencil_alpha_state
*templ
)
504 unsigned hash_key
= cso_construct_key((void*)templ
,
505 sizeof(struct pipe_depth_stencil_alpha_state
));
506 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
508 CSO_DEPTH_STENCIL_ALPHA
,
512 if (cso_hash_iter_is_null(iter
)) {
513 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
515 return PIPE_ERROR_OUT_OF_MEMORY
;
517 memcpy(&cso
->state
, templ
, sizeof(*templ
));
518 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
519 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
520 cso
->context
= ctx
->pipe
;
522 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
523 if (cso_hash_iter_is_null(iter
)) {
525 return PIPE_ERROR_OUT_OF_MEMORY
;
531 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
534 if (ctx
->depth_stencil
!= handle
) {
535 ctx
->depth_stencil
= handle
;
536 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
541 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
543 assert(!ctx
->depth_stencil_saved
);
544 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
547 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
549 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
550 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
551 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
553 ctx
->depth_stencil_saved
= NULL
;
558 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
559 const struct pipe_rasterizer_state
*templ
)
561 unsigned hash_key
= cso_construct_key((void*)templ
,
562 sizeof(struct pipe_rasterizer_state
));
563 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
564 hash_key
, CSO_RASTERIZER
,
568 if (cso_hash_iter_is_null(iter
)) {
569 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
571 return PIPE_ERROR_OUT_OF_MEMORY
;
573 memcpy(&cso
->state
, templ
, sizeof(*templ
));
574 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
575 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
576 cso
->context
= ctx
->pipe
;
578 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
579 if (cso_hash_iter_is_null(iter
)) {
581 return PIPE_ERROR_OUT_OF_MEMORY
;
587 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
590 if (ctx
->rasterizer
!= handle
) {
591 ctx
->rasterizer
= handle
;
592 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
597 void cso_save_rasterizer(struct cso_context
*ctx
)
599 assert(!ctx
->rasterizer_saved
);
600 ctx
->rasterizer_saved
= ctx
->rasterizer
;
603 void cso_restore_rasterizer(struct cso_context
*ctx
)
605 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
606 ctx
->rasterizer
= ctx
->rasterizer_saved
;
607 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
609 ctx
->rasterizer_saved
= NULL
;
614 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
617 if (ctx
->fragment_shader
!= handle
) {
618 ctx
->fragment_shader
= handle
;
619 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
624 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
626 if (handle
== ctx
->fragment_shader
) {
627 /* unbind before deleting */
628 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
629 ctx
->fragment_shader
= NULL
;
631 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
634 /* Not really working:
637 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
638 const struct pipe_shader_state
*templ
)
640 const struct tgsi_token
*tokens
= templ
->tokens
;
641 unsigned num_tokens
= tgsi_num_tokens(tokens
);
642 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
643 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
644 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
650 if (cso_hash_iter_is_null(iter
)) {
651 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
652 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
655 return PIPE_ERROR_OUT_OF_MEMORY
;
657 memcpy(cso_tokens
, tokens
, tokens_size
);
658 cso
->state
.tokens
= cso_tokens
;
659 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
660 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
661 cso
->context
= ctx
->pipe
;
663 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
664 if (cso_hash_iter_is_null(iter
)) {
666 return PIPE_ERROR_OUT_OF_MEMORY
;
672 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
675 return cso_set_fragment_shader_handle( ctx
, handle
);
679 void cso_save_fragment_shader(struct cso_context
*ctx
)
681 assert(!ctx
->fragment_shader_saved
);
682 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
685 void cso_restore_fragment_shader(struct cso_context
*ctx
)
687 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
688 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
689 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
691 ctx
->fragment_shader_saved
= NULL
;
695 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
698 if (ctx
->vertex_shader
!= handle
) {
699 ctx
->vertex_shader
= handle
;
700 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
705 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
707 if (handle
== ctx
->vertex_shader
) {
708 /* unbind before deleting */
709 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
710 ctx
->vertex_shader
= NULL
;
712 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
716 /* Not really working:
719 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
720 const struct pipe_shader_state
*templ
)
722 unsigned hash_key
= cso_construct_key((void*)templ
,
723 sizeof(struct pipe_shader_state
));
724 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
725 hash_key
, CSO_VERTEX_SHADER
,
729 if (cso_hash_iter_is_null(iter
)) {
730 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
733 return PIPE_ERROR_OUT_OF_MEMORY
;
735 memcpy(cso
->state
, templ
, sizeof(*templ
));
736 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
737 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
738 cso
->context
= ctx
->pipe
;
740 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
741 if (cso_hash_iter_is_null(iter
)) {
743 return PIPE_ERROR_OUT_OF_MEMORY
;
749 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
752 return cso_set_vertex_shader_handle( ctx
, handle
);
758 void cso_save_vertex_shader(struct cso_context
*ctx
)
760 assert(!ctx
->vertex_shader_saved
);
761 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
764 void cso_restore_vertex_shader(struct cso_context
*ctx
)
766 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
767 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
768 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
770 ctx
->vertex_shader_saved
= NULL
;
775 * Copy framebuffer state from src to dst with refcounting of surfaces.
778 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
779 const struct pipe_framebuffer_state
*src
)
783 dst
->width
= src
->width
;
784 dst
->height
= src
->height
;
785 dst
->nr_cbufs
= src
->nr_cbufs
;
786 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
787 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
789 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
794 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
798 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
799 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
801 pipe_surface_reference(&fb
->zsbuf
, NULL
);
805 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
806 const struct pipe_framebuffer_state
*fb
)
808 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
809 copy_framebuffer_state(&ctx
->fb
, fb
);
810 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
815 void cso_save_framebuffer(struct cso_context
*ctx
)
817 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
820 void cso_restore_framebuffer(struct cso_context
*ctx
)
822 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
823 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
824 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
825 free_framebuffer_state(&ctx
->fb_saved
);
830 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
831 const struct pipe_viewport_state
*vp
)
833 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
835 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
840 void cso_save_viewport(struct cso_context
*ctx
)
842 ctx
->vp_saved
= ctx
->vp
;
846 void cso_restore_viewport(struct cso_context
*ctx
)
848 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
849 ctx
->vp
= ctx
->vp_saved
;
850 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
857 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
858 const struct pipe_blend_color
*bc
)
860 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
861 ctx
->blend_color
= *bc
;
862 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);