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_inlines.h"
40 #include "util/u_memory.h"
41 #include "tgsi/tgsi_parse.h"
43 #include "cso_cache/cso_context.h"
44 #include "cso_cache/cso_cache.h"
45 #include "cso_cache/cso_hash.h"
46 #include "cso_context.h"
49 struct pipe_context
*pipe
;
50 struct cso_cache
*cache
;
53 void *samplers
[PIPE_MAX_SAMPLERS
];
56 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
57 unsigned nr_vertex_samplers
;
60 void *samplers
[PIPE_MAX_SAMPLERS
];
63 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
64 unsigned nr_vertex_samplers
;
66 unsigned nr_samplers_saved
;
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
69 unsigned nr_vertex_samplers_saved
;
70 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
72 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
75 struct pipe_texture
*vertex_textures
[PIPE_MAX_VERTEX_SAMPLERS
];
76 uint nr_vertex_textures
;
78 uint nr_textures_saved
;
79 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
81 uint nr_vertex_textures_saved
;
82 struct pipe_texture
*vertex_textures_saved
[PIPE_MAX_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
;
93 struct pipe_framebuffer_state fb
, fb_saved
;
94 struct pipe_viewport_state vp
, vp_saved
;
95 struct pipe_blend_color blend_color
;
96 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
101 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
104 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
106 struct cso_blend
*cso
= (struct cso_blend
*)state
;
108 if (ctx
->blend
== cso
->data
)
111 if (cso
->delete_state
)
112 cso
->delete_state(cso
->context
, cso
->data
);
117 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
119 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
121 if (ctx
->depth_stencil
== cso
->data
)
124 if (cso
->delete_state
)
125 cso
->delete_state(cso
->context
, cso
->data
);
131 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
133 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
134 if (cso
->delete_state
)
135 cso
->delete_state(cso
->context
, cso
->data
);
140 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
142 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
144 if (ctx
->rasterizer
== cso
->data
)
146 if (cso
->delete_state
)
147 cso
->delete_state(cso
->context
, cso
->data
);
152 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
154 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
155 if (ctx
->fragment_shader
== cso
->data
)
157 if (cso
->delete_state
)
158 cso
->delete_state(cso
->context
, cso
->data
);
163 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
165 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
166 if (ctx
->vertex_shader
== cso
->data
)
168 if (cso
->delete_state
)
169 cso
->delete_state(cso
->context
, cso
->data
);
175 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
176 void *state
, enum cso_cache_type type
)
180 return delete_blend_state(ctx
, state
);
183 return delete_sampler_state(ctx
, state
);
185 case CSO_DEPTH_STENCIL_ALPHA
:
186 return delete_depth_stencil_state(ctx
, state
);
189 return delete_rasterizer_state(ctx
, state
);
191 case CSO_FRAGMENT_SHADER
:
192 return delete_fs_state(ctx
, state
);
194 case CSO_VERTEX_SHADER
:
195 return delete_vs_state(ctx
, state
);
204 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
205 int max_size
, void *user_data
)
207 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
208 /* if we're approach the maximum size, remove fourth of the entries
209 * otherwise every subsequent call will go through the same */
210 int hash_size
= cso_hash_size(hash
);
211 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
212 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
213 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
214 if (hash_size
> max_size
)
215 to_remove
+= hash_size
- max_size
;
217 /*remove elements until we're good */
218 /*fixme: currently we pick the nodes to remove at random*/
219 void *cso
= cso_hash_iter_data(iter
);
220 if (delete_cso(ctx
, cso
, type
)) {
221 iter
= cso_hash_erase(hash
, iter
);
224 iter
= cso_hash_iter_next(iter
);
229 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
231 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
235 ctx
->cache
= cso_cache_create();
236 if (ctx
->cache
== NULL
)
238 cso_cache_set_sanitize_callback(ctx
->cache
,
244 /* Enable for testing: */
245 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
250 cso_destroy_context( ctx
);
256 * Prior to context destruction, this function unbinds all state objects.
258 void cso_release_all( struct cso_context
*ctx
)
263 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
264 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
265 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
266 if (ctx
->pipe
->bind_vertex_sampler_states
)
267 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
268 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
269 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
270 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
273 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
274 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
275 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
278 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
279 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
280 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
283 free_framebuffer_state(&ctx
->fb
);
284 free_framebuffer_state(&ctx
->fb_saved
);
287 cso_cache_delete( ctx
->cache
);
293 void cso_destroy_context( struct cso_context
*ctx
)
296 /*cso_release_all( ctx );*/
302 /* Those function will either find the state of the given template
303 * in the cache or they will create a new state from the given
304 * template, insert it in the cache and return it.
308 * If the driver returns 0 from the create method then they will assign
309 * the data member of the cso to be the template itself.
312 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
313 const struct pipe_blend_state
*templ
)
315 unsigned key_size
, hash_key
;
316 struct cso_hash_iter iter
;
319 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
320 (char *)&(templ
->rt
[1]) - (char *)templ
;
321 hash_key
= cso_construct_key((void*)templ
, key_size
);
322 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
324 if (cso_hash_iter_is_null(iter
)) {
325 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
327 return PIPE_ERROR_OUT_OF_MEMORY
;
329 memcpy(&cso
->state
, templ
, key_size
);
330 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
331 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
332 cso
->context
= ctx
->pipe
;
334 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
335 if (cso_hash_iter_is_null(iter
)) {
337 return PIPE_ERROR_OUT_OF_MEMORY
;
343 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
346 if (ctx
->blend
!= handle
) {
348 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
353 void cso_save_blend(struct cso_context
*ctx
)
355 assert(!ctx
->blend_saved
);
356 ctx
->blend_saved
= ctx
->blend
;
359 void cso_restore_blend(struct cso_context
*ctx
)
361 if (ctx
->blend
!= ctx
->blend_saved
) {
362 ctx
->blend
= ctx
->blend_saved
;
363 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
365 ctx
->blend_saved
= NULL
;
370 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
372 const struct pipe_sampler_state
*templ
)
377 unsigned key_size
= sizeof(struct pipe_sampler_state
);
378 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
379 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
380 hash_key
, CSO_SAMPLER
,
381 (void*)templ
, key_size
);
383 if (cso_hash_iter_is_null(iter
)) {
384 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
386 return PIPE_ERROR_OUT_OF_MEMORY
;
388 memcpy(&cso
->state
, templ
, sizeof(*templ
));
389 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
390 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
391 cso
->context
= ctx
->pipe
;
393 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
394 if (cso_hash_iter_is_null(iter
)) {
396 return PIPE_ERROR_OUT_OF_MEMORY
;
402 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
406 ctx
->samplers
[idx
] = handle
;
411 cso_single_vertex_sampler(struct cso_context
*ctx
,
413 const struct pipe_sampler_state
*templ
)
418 unsigned key_size
= sizeof(struct pipe_sampler_state
);
419 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
420 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
421 hash_key
, CSO_SAMPLER
,
422 (void*)templ
, key_size
);
424 if (cso_hash_iter_is_null(iter
)) {
425 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
427 return PIPE_ERROR_OUT_OF_MEMORY
;
429 memcpy(&cso
->state
, templ
, sizeof(*templ
));
430 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
431 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
432 cso
->context
= ctx
->pipe
;
434 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
435 if (cso_hash_iter_is_null(iter
)) {
437 return PIPE_ERROR_OUT_OF_MEMORY
;
443 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
447 ctx
->vertex_samplers
[idx
] = handle
;
451 void cso_single_sampler_done( struct cso_context
*ctx
)
455 /* find highest non-null sampler */
456 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
457 if (ctx
->samplers
[i
- 1] != NULL
)
461 ctx
->nr_samplers
= i
;
463 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
464 memcmp(ctx
->hw
.samplers
,
466 ctx
->nr_samplers
* sizeof(void *)) != 0)
468 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
469 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
471 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
476 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
480 /* find highest non-null sampler */
481 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
482 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
486 ctx
->nr_vertex_samplers
= i
;
488 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
489 memcmp(ctx
->hw
.vertex_samplers
,
490 ctx
->vertex_samplers
,
491 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
493 memcpy(ctx
->hw
.vertex_samplers
,
494 ctx
->vertex_samplers
,
495 ctx
->nr_vertex_samplers
* sizeof(void *));
496 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
498 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
499 ctx
->nr_vertex_samplers
,
500 ctx
->vertex_samplers
);
505 * If the function encouters any errors it will return the
506 * last one. Done to always try to set as many samplers
509 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
511 const struct pipe_sampler_state
**templates
)
514 enum pipe_error temp
, error
= PIPE_OK
;
519 for (i
= 0; i
< nr
; i
++) {
520 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
525 for ( ; i
< ctx
->nr_samplers
; i
++) {
526 temp
= cso_single_sampler( ctx
, i
, NULL
);
531 cso_single_sampler_done( ctx
);
536 void cso_save_samplers(struct cso_context
*ctx
)
538 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
539 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
542 void cso_restore_samplers(struct cso_context
*ctx
)
544 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
545 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
546 cso_single_sampler_done( ctx
);
550 * If the function encouters any errors it will return the
551 * last one. Done to always try to set as many samplers
554 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
556 const struct pipe_sampler_state
**templates
)
559 enum pipe_error temp
, error
= PIPE_OK
;
564 for (i
= 0; i
< nr
; i
++) {
565 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
570 for ( ; i
< ctx
->nr_samplers
; i
++) {
571 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
576 cso_single_vertex_sampler_done( ctx
);
582 cso_save_vertex_samplers(struct cso_context
*ctx
)
584 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
585 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
589 cso_restore_vertex_samplers(struct cso_context
*ctx
)
591 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
592 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
593 cso_single_vertex_sampler_done(ctx
);
597 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
599 struct pipe_texture
**textures
)
603 ctx
->nr_textures
= count
;
605 for (i
= 0; i
< count
; i
++)
606 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
607 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
608 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
610 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
615 void cso_save_sampler_textures( struct cso_context
*ctx
)
619 ctx
->nr_textures_saved
= ctx
->nr_textures
;
620 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
621 assert(!ctx
->textures_saved
[i
]);
622 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
626 void cso_restore_sampler_textures( struct cso_context
*ctx
)
630 ctx
->nr_textures
= ctx
->nr_textures_saved
;
632 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
633 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
634 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
635 ctx
->textures_saved
[i
] = NULL
;
637 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
638 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
640 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
642 ctx
->nr_textures_saved
= 0;
648 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
650 struct pipe_texture
**textures
)
654 ctx
->nr_vertex_textures
= count
;
656 for (i
= 0; i
< count
; i
++) {
657 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
659 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
660 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
663 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
669 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
673 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
674 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
675 assert(!ctx
->vertex_textures_saved
[i
]);
676 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
681 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
685 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
687 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
688 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
689 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
690 ctx
->vertex_textures_saved
[i
] = NULL
;
692 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
693 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
696 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
697 ctx
->nr_vertex_textures
,
698 ctx
->vertex_textures
);
700 ctx
->nr_vertex_textures_saved
= 0;
705 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
706 const struct pipe_depth_stencil_alpha_state
*templ
)
708 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
709 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
710 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
712 CSO_DEPTH_STENCIL_ALPHA
,
713 (void*)templ
, key_size
);
716 if (cso_hash_iter_is_null(iter
)) {
717 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
719 return PIPE_ERROR_OUT_OF_MEMORY
;
721 memcpy(&cso
->state
, templ
, sizeof(*templ
));
722 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
723 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
724 cso
->context
= ctx
->pipe
;
726 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
727 if (cso_hash_iter_is_null(iter
)) {
729 return PIPE_ERROR_OUT_OF_MEMORY
;
735 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
738 if (ctx
->depth_stencil
!= handle
) {
739 ctx
->depth_stencil
= handle
;
740 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
745 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
747 assert(!ctx
->depth_stencil_saved
);
748 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
751 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
753 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
754 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
755 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
757 ctx
->depth_stencil_saved
= NULL
;
762 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
763 const struct pipe_rasterizer_state
*templ
)
765 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
766 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
767 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
768 hash_key
, CSO_RASTERIZER
,
769 (void*)templ
, key_size
);
772 if (cso_hash_iter_is_null(iter
)) {
773 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
775 return PIPE_ERROR_OUT_OF_MEMORY
;
777 memcpy(&cso
->state
, templ
, sizeof(*templ
));
778 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
779 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
780 cso
->context
= ctx
->pipe
;
782 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
783 if (cso_hash_iter_is_null(iter
)) {
785 return PIPE_ERROR_OUT_OF_MEMORY
;
791 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
794 if (ctx
->rasterizer
!= handle
) {
795 ctx
->rasterizer
= handle
;
796 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
801 void cso_save_rasterizer(struct cso_context
*ctx
)
803 assert(!ctx
->rasterizer_saved
);
804 ctx
->rasterizer_saved
= ctx
->rasterizer
;
807 void cso_restore_rasterizer(struct cso_context
*ctx
)
809 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
810 ctx
->rasterizer
= ctx
->rasterizer_saved
;
811 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
813 ctx
->rasterizer_saved
= NULL
;
818 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
821 if (ctx
->fragment_shader
!= handle
) {
822 ctx
->fragment_shader
= handle
;
823 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
828 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
830 if (handle
== ctx
->fragment_shader
) {
831 /* unbind before deleting */
832 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
833 ctx
->fragment_shader
= NULL
;
835 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
838 /* Not really working:
841 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
842 const struct pipe_shader_state
*templ
)
844 const struct tgsi_token
*tokens
= templ
->tokens
;
845 unsigned num_tokens
= tgsi_num_tokens(tokens
);
846 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
847 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
848 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
852 sizeof(*templ
)); /* XXX correct? tokens_size? */
855 if (cso_hash_iter_is_null(iter
)) {
856 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
857 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
860 return PIPE_ERROR_OUT_OF_MEMORY
;
862 memcpy(cso_tokens
, tokens
, tokens_size
);
863 cso
->state
.tokens
= cso_tokens
;
864 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
865 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
866 cso
->context
= ctx
->pipe
;
868 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
869 if (cso_hash_iter_is_null(iter
)) {
871 return PIPE_ERROR_OUT_OF_MEMORY
;
877 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
880 return cso_set_fragment_shader_handle( ctx
, handle
);
884 void cso_save_fragment_shader(struct cso_context
*ctx
)
886 assert(!ctx
->fragment_shader_saved
);
887 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
890 void cso_restore_fragment_shader(struct cso_context
*ctx
)
892 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
893 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
894 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
896 ctx
->fragment_shader_saved
= NULL
;
900 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
903 if (ctx
->vertex_shader
!= handle
) {
904 ctx
->vertex_shader
= handle
;
905 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
910 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
912 if (handle
== ctx
->vertex_shader
) {
913 /* unbind before deleting */
914 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
915 ctx
->vertex_shader
= NULL
;
917 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
921 /* Not really working:
924 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
925 const struct pipe_shader_state
*templ
)
927 unsigned hash_key
= cso_construct_key((void*)templ
,
928 sizeof(struct pipe_shader_state
));
929 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
930 hash_key
, CSO_VERTEX_SHADER
,
935 if (cso_hash_iter_is_null(iter
)) {
936 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
939 return PIPE_ERROR_OUT_OF_MEMORY
;
941 memcpy(cso
->state
, templ
, sizeof(*templ
));
942 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
943 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
944 cso
->context
= ctx
->pipe
;
946 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
947 if (cso_hash_iter_is_null(iter
)) {
949 return PIPE_ERROR_OUT_OF_MEMORY
;
955 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
958 return cso_set_vertex_shader_handle( ctx
, handle
);
964 void cso_save_vertex_shader(struct cso_context
*ctx
)
966 assert(!ctx
->vertex_shader_saved
);
967 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
970 void cso_restore_vertex_shader(struct cso_context
*ctx
)
972 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
973 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
974 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
976 ctx
->vertex_shader_saved
= NULL
;
981 * Copy framebuffer state from src to dst with refcounting of surfaces.
984 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
985 const struct pipe_framebuffer_state
*src
)
989 dst
->width
= src
->width
;
990 dst
->height
= src
->height
;
991 dst
->nr_cbufs
= src
->nr_cbufs
;
992 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
993 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
995 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1000 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1004 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1005 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1007 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1011 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1012 const struct pipe_framebuffer_state
*fb
)
1014 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1015 copy_framebuffer_state(&ctx
->fb
, fb
);
1016 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1021 void cso_save_framebuffer(struct cso_context
*ctx
)
1023 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1026 void cso_restore_framebuffer(struct cso_context
*ctx
)
1028 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1029 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1030 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1031 free_framebuffer_state(&ctx
->fb_saved
);
1036 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1037 const struct pipe_viewport_state
*vp
)
1039 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1041 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1046 void cso_save_viewport(struct cso_context
*ctx
)
1048 ctx
->vp_saved
= ctx
->vp
;
1052 void cso_restore_viewport(struct cso_context
*ctx
)
1054 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1055 ctx
->vp
= ctx
->vp_saved
;
1056 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1061 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1062 const struct pipe_blend_color
*bc
)
1064 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1065 ctx
->blend_color
= *bc
;
1066 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1071 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1072 const struct pipe_stencil_ref
*sr
)
1074 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1075 ctx
->stencil_ref
= *sr
;
1076 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1081 void cso_save_stencil_ref(struct cso_context
*ctx
)
1083 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1087 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1089 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1090 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1091 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1095 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1098 if (ctx
->geometry_shader
!= handle
) {
1099 ctx
->geometry_shader
= handle
;
1100 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1105 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1107 if (handle
== ctx
->geometry_shader
) {
1108 /* unbind before deleting */
1109 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1110 ctx
->geometry_shader
= NULL
;
1112 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1115 void cso_save_geometry_shader(struct cso_context
*ctx
)
1117 assert(!ctx
->geometry_shader_saved
);
1118 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1121 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1123 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1124 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1125 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1127 ctx
->geometry_shader_saved
= NULL
;