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
;
749 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
752 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
754 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
755 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
756 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
758 ctx
->depth_stencil_saved
= NULL
;
759 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
760 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
761 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
767 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
768 const struct pipe_rasterizer_state
*templ
)
770 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
771 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
772 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
773 hash_key
, CSO_RASTERIZER
,
774 (void*)templ
, key_size
);
777 if (cso_hash_iter_is_null(iter
)) {
778 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
780 return PIPE_ERROR_OUT_OF_MEMORY
;
782 memcpy(&cso
->state
, templ
, sizeof(*templ
));
783 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
784 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
785 cso
->context
= ctx
->pipe
;
787 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
788 if (cso_hash_iter_is_null(iter
)) {
790 return PIPE_ERROR_OUT_OF_MEMORY
;
796 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
799 if (ctx
->rasterizer
!= handle
) {
800 ctx
->rasterizer
= handle
;
801 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
806 void cso_save_rasterizer(struct cso_context
*ctx
)
808 assert(!ctx
->rasterizer_saved
);
809 ctx
->rasterizer_saved
= ctx
->rasterizer
;
812 void cso_restore_rasterizer(struct cso_context
*ctx
)
814 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
815 ctx
->rasterizer
= ctx
->rasterizer_saved
;
816 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
818 ctx
->rasterizer_saved
= NULL
;
823 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
826 if (ctx
->fragment_shader
!= handle
) {
827 ctx
->fragment_shader
= handle
;
828 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
833 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
835 if (handle
== ctx
->fragment_shader
) {
836 /* unbind before deleting */
837 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
838 ctx
->fragment_shader
= NULL
;
840 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
843 /* Not really working:
846 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
847 const struct pipe_shader_state
*templ
)
849 const struct tgsi_token
*tokens
= templ
->tokens
;
850 unsigned num_tokens
= tgsi_num_tokens(tokens
);
851 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
852 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
853 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
857 sizeof(*templ
)); /* XXX correct? tokens_size? */
860 if (cso_hash_iter_is_null(iter
)) {
861 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
862 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
865 return PIPE_ERROR_OUT_OF_MEMORY
;
867 memcpy(cso_tokens
, tokens
, tokens_size
);
868 cso
->state
.tokens
= cso_tokens
;
869 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
870 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
871 cso
->context
= ctx
->pipe
;
873 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
874 if (cso_hash_iter_is_null(iter
)) {
876 return PIPE_ERROR_OUT_OF_MEMORY
;
882 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
885 return cso_set_fragment_shader_handle( ctx
, handle
);
889 void cso_save_fragment_shader(struct cso_context
*ctx
)
891 assert(!ctx
->fragment_shader_saved
);
892 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
895 void cso_restore_fragment_shader(struct cso_context
*ctx
)
897 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
898 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
899 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
901 ctx
->fragment_shader_saved
= NULL
;
905 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
908 if (ctx
->vertex_shader
!= handle
) {
909 ctx
->vertex_shader
= handle
;
910 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
915 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
917 if (handle
== ctx
->vertex_shader
) {
918 /* unbind before deleting */
919 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
920 ctx
->vertex_shader
= NULL
;
922 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
926 /* Not really working:
929 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
930 const struct pipe_shader_state
*templ
)
932 unsigned hash_key
= cso_construct_key((void*)templ
,
933 sizeof(struct pipe_shader_state
));
934 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
935 hash_key
, CSO_VERTEX_SHADER
,
940 if (cso_hash_iter_is_null(iter
)) {
941 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
944 return PIPE_ERROR_OUT_OF_MEMORY
;
946 memcpy(cso
->state
, templ
, sizeof(*templ
));
947 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
948 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
949 cso
->context
= ctx
->pipe
;
951 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
952 if (cso_hash_iter_is_null(iter
)) {
954 return PIPE_ERROR_OUT_OF_MEMORY
;
960 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
963 return cso_set_vertex_shader_handle( ctx
, handle
);
969 void cso_save_vertex_shader(struct cso_context
*ctx
)
971 assert(!ctx
->vertex_shader_saved
);
972 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
975 void cso_restore_vertex_shader(struct cso_context
*ctx
)
977 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
978 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
979 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
981 ctx
->vertex_shader_saved
= NULL
;
986 * Copy framebuffer state from src to dst with refcounting of surfaces.
989 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
990 const struct pipe_framebuffer_state
*src
)
994 dst
->width
= src
->width
;
995 dst
->height
= src
->height
;
996 dst
->nr_cbufs
= src
->nr_cbufs
;
997 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
998 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
1000 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
1005 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1009 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1010 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1012 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1016 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1017 const struct pipe_framebuffer_state
*fb
)
1019 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1020 copy_framebuffer_state(&ctx
->fb
, fb
);
1021 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1026 void cso_save_framebuffer(struct cso_context
*ctx
)
1028 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1031 void cso_restore_framebuffer(struct cso_context
*ctx
)
1033 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1034 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1035 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1036 free_framebuffer_state(&ctx
->fb_saved
);
1041 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1042 const struct pipe_viewport_state
*vp
)
1044 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1046 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1051 void cso_save_viewport(struct cso_context
*ctx
)
1053 ctx
->vp_saved
= ctx
->vp
;
1057 void cso_restore_viewport(struct cso_context
*ctx
)
1059 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1060 ctx
->vp
= ctx
->vp_saved
;
1061 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1066 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1067 const struct pipe_blend_color
*bc
)
1069 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1070 ctx
->blend_color
= *bc
;
1071 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1076 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1077 const struct pipe_stencil_ref
*sr
)
1079 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1080 ctx
->stencil_ref
= *sr
;
1081 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1086 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1089 if (ctx
->geometry_shader
!= handle
) {
1090 ctx
->geometry_shader
= handle
;
1091 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1096 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1098 if (handle
== ctx
->geometry_shader
) {
1099 /* unbind before deleting */
1100 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1101 ctx
->geometry_shader
= NULL
;
1103 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1106 void cso_save_geometry_shader(struct cso_context
*ctx
)
1108 assert(!ctx
->geometry_shader_saved
);
1109 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1112 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1114 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1115 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1116 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1118 ctx
->geometry_shader_saved
= NULL
;