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
;
100 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
103 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
105 struct cso_blend
*cso
= (struct cso_blend
*)state
;
107 if (ctx
->blend
== cso
->data
)
110 if (cso
->delete_state
)
111 cso
->delete_state(cso
->context
, cso
->data
);
116 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
118 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
120 if (ctx
->depth_stencil
== cso
->data
)
123 if (cso
->delete_state
)
124 cso
->delete_state(cso
->context
, cso
->data
);
130 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
132 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
133 if (cso
->delete_state
)
134 cso
->delete_state(cso
->context
, cso
->data
);
139 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
141 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
143 if (ctx
->rasterizer
== cso
->data
)
145 if (cso
->delete_state
)
146 cso
->delete_state(cso
->context
, cso
->data
);
151 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
153 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
154 if (ctx
->fragment_shader
== cso
->data
)
156 if (cso
->delete_state
)
157 cso
->delete_state(cso
->context
, cso
->data
);
162 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
164 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
165 if (ctx
->vertex_shader
== cso
->data
)
167 if (cso
->delete_state
)
168 cso
->delete_state(cso
->context
, cso
->data
);
174 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
175 void *state
, enum cso_cache_type type
)
179 return delete_blend_state(ctx
, state
);
182 return delete_sampler_state(ctx
, state
);
184 case CSO_DEPTH_STENCIL_ALPHA
:
185 return delete_depth_stencil_state(ctx
, state
);
188 return delete_rasterizer_state(ctx
, state
);
190 case CSO_FRAGMENT_SHADER
:
191 return delete_fs_state(ctx
, state
);
193 case CSO_VERTEX_SHADER
:
194 return delete_vs_state(ctx
, state
);
203 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
204 int max_size
, void *user_data
)
206 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
207 /* if we're approach the maximum size, remove fourth of the entries
208 * otherwise every subsequent call will go through the same */
209 int hash_size
= cso_hash_size(hash
);
210 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
211 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
212 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
213 if (hash_size
> max_size
)
214 to_remove
+= hash_size
- max_size
;
216 /*remove elements until we're good */
217 /*fixme: currently we pick the nodes to remove at random*/
218 void *cso
= cso_hash_iter_data(iter
);
219 if (delete_cso(ctx
, cso
, type
)) {
220 iter
= cso_hash_erase(hash
, iter
);
223 iter
= cso_hash_iter_next(iter
);
228 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
230 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
234 ctx
->cache
= cso_cache_create();
235 if (ctx
->cache
== NULL
)
237 cso_cache_set_sanitize_callback(ctx
->cache
,
243 /* Enable for testing: */
244 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
249 cso_destroy_context( ctx
);
255 * Prior to context destruction, this function unbinds all state objects.
257 void cso_release_all( struct cso_context
*ctx
)
262 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
263 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
264 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
265 if (ctx
->pipe
->bind_vertex_sampler_states
)
266 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
267 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
268 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
269 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
272 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
273 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
274 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
277 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
278 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
279 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], NULL
);
282 free_framebuffer_state(&ctx
->fb
);
283 free_framebuffer_state(&ctx
->fb_saved
);
286 cso_cache_delete( ctx
->cache
);
292 void cso_destroy_context( struct cso_context
*ctx
)
295 /*cso_release_all( ctx );*/
301 /* Those function will either find the state of the given template
302 * in the cache or they will create a new state from the given
303 * template, insert it in the cache and return it.
307 * If the driver returns 0 from the create method then they will assign
308 * the data member of the cso to be the template itself.
311 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
312 const struct pipe_blend_state
*templ
)
314 unsigned key_size
, hash_key
;
315 struct cso_hash_iter iter
;
318 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
319 (char *)&(templ
->rt
[1]) - (char *)templ
;
320 hash_key
= cso_construct_key((void*)templ
, key_size
);
321 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
323 if (cso_hash_iter_is_null(iter
)) {
324 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
326 return PIPE_ERROR_OUT_OF_MEMORY
;
328 memcpy(&cso
->state
, templ
, key_size
);
329 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
330 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
331 cso
->context
= ctx
->pipe
;
333 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
334 if (cso_hash_iter_is_null(iter
)) {
336 return PIPE_ERROR_OUT_OF_MEMORY
;
342 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
345 if (ctx
->blend
!= handle
) {
347 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
352 void cso_save_blend(struct cso_context
*ctx
)
354 assert(!ctx
->blend_saved
);
355 ctx
->blend_saved
= ctx
->blend
;
358 void cso_restore_blend(struct cso_context
*ctx
)
360 if (ctx
->blend
!= ctx
->blend_saved
) {
361 ctx
->blend
= ctx
->blend_saved
;
362 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
364 ctx
->blend_saved
= NULL
;
369 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
371 const struct pipe_sampler_state
*templ
)
376 unsigned key_size
= sizeof(struct pipe_sampler_state
);
377 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
378 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
379 hash_key
, CSO_SAMPLER
,
380 (void*)templ
, key_size
);
382 if (cso_hash_iter_is_null(iter
)) {
383 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
385 return PIPE_ERROR_OUT_OF_MEMORY
;
387 memcpy(&cso
->state
, templ
, sizeof(*templ
));
388 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
389 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
390 cso
->context
= ctx
->pipe
;
392 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
393 if (cso_hash_iter_is_null(iter
)) {
395 return PIPE_ERROR_OUT_OF_MEMORY
;
401 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
405 ctx
->samplers
[idx
] = handle
;
410 cso_single_vertex_sampler(struct cso_context
*ctx
,
412 const struct pipe_sampler_state
*templ
)
417 unsigned key_size
= sizeof(struct pipe_sampler_state
);
418 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
419 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
420 hash_key
, CSO_SAMPLER
,
421 (void*)templ
, key_size
);
423 if (cso_hash_iter_is_null(iter
)) {
424 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
426 return PIPE_ERROR_OUT_OF_MEMORY
;
428 memcpy(&cso
->state
, templ
, sizeof(*templ
));
429 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
430 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
431 cso
->context
= ctx
->pipe
;
433 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
434 if (cso_hash_iter_is_null(iter
)) {
436 return PIPE_ERROR_OUT_OF_MEMORY
;
442 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
446 ctx
->vertex_samplers
[idx
] = handle
;
450 void cso_single_sampler_done( struct cso_context
*ctx
)
454 /* find highest non-null sampler */
455 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
456 if (ctx
->samplers
[i
- 1] != NULL
)
460 ctx
->nr_samplers
= i
;
462 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
463 memcmp(ctx
->hw
.samplers
,
465 ctx
->nr_samplers
* sizeof(void *)) != 0)
467 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
468 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
470 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
475 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
479 /* find highest non-null sampler */
480 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
481 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
485 ctx
->nr_vertex_samplers
= i
;
487 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
488 memcmp(ctx
->hw
.vertex_samplers
,
489 ctx
->vertex_samplers
,
490 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
492 memcpy(ctx
->hw
.vertex_samplers
,
493 ctx
->vertex_samplers
,
494 ctx
->nr_vertex_samplers
* sizeof(void *));
495 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
497 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
498 ctx
->nr_vertex_samplers
,
499 ctx
->vertex_samplers
);
504 * If the function encouters any errors it will return the
505 * last one. Done to always try to set as many samplers
508 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
510 const struct pipe_sampler_state
**templates
)
513 enum pipe_error temp
, error
= PIPE_OK
;
518 for (i
= 0; i
< nr
; i
++) {
519 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
524 for ( ; i
< ctx
->nr_samplers
; i
++) {
525 temp
= cso_single_sampler( ctx
, i
, NULL
);
530 cso_single_sampler_done( ctx
);
535 void cso_save_samplers(struct cso_context
*ctx
)
537 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
538 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
541 void cso_restore_samplers(struct cso_context
*ctx
)
543 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
544 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
545 cso_single_sampler_done( ctx
);
549 * If the function encouters any errors it will return the
550 * last one. Done to always try to set as many samplers
553 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
555 const struct pipe_sampler_state
**templates
)
558 enum pipe_error temp
, error
= PIPE_OK
;
563 for (i
= 0; i
< nr
; i
++) {
564 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
569 for ( ; i
< ctx
->nr_samplers
; i
++) {
570 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
575 cso_single_vertex_sampler_done( ctx
);
581 cso_save_vertex_samplers(struct cso_context
*ctx
)
583 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
584 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
588 cso_restore_vertex_samplers(struct cso_context
*ctx
)
590 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
591 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
592 cso_single_vertex_sampler_done(ctx
);
596 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
598 struct pipe_texture
**textures
)
602 ctx
->nr_textures
= count
;
604 for (i
= 0; i
< count
; i
++)
605 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
606 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
607 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
609 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, count
, textures
);
614 void cso_save_sampler_textures( struct cso_context
*ctx
)
618 ctx
->nr_textures_saved
= ctx
->nr_textures
;
619 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
620 assert(!ctx
->textures_saved
[i
]);
621 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
625 void cso_restore_sampler_textures( struct cso_context
*ctx
)
629 ctx
->nr_textures
= ctx
->nr_textures_saved
;
631 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
632 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
633 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
634 ctx
->textures_saved
[i
] = NULL
;
636 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
637 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
639 ctx
->pipe
->set_fragment_sampler_textures(ctx
->pipe
, ctx
->nr_textures
, ctx
->textures
);
641 ctx
->nr_textures_saved
= 0;
647 cso_set_vertex_sampler_textures(struct cso_context
*ctx
,
649 struct pipe_texture
**textures
)
653 ctx
->nr_vertex_textures
= count
;
655 for (i
= 0; i
< count
; i
++) {
656 pipe_texture_reference(&ctx
->vertex_textures
[i
], textures
[i
]);
658 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
659 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
662 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
, count
, textures
);
668 cso_save_vertex_sampler_textures(struct cso_context
*ctx
)
672 ctx
->nr_vertex_textures_saved
= ctx
->nr_vertex_textures
;
673 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
674 assert(!ctx
->vertex_textures_saved
[i
]);
675 pipe_texture_reference(&ctx
->vertex_textures_saved
[i
], ctx
->vertex_textures
[i
]);
680 cso_restore_vertex_sampler_textures(struct cso_context
*ctx
)
684 ctx
->nr_vertex_textures
= ctx
->nr_vertex_textures_saved
;
686 for (i
= 0; i
< ctx
->nr_vertex_textures
; i
++) {
687 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
688 ctx
->vertex_textures
[i
] = ctx
->vertex_textures_saved
[i
];
689 ctx
->vertex_textures_saved
[i
] = NULL
;
691 for ( ; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
692 pipe_texture_reference(&ctx
->vertex_textures
[i
], NULL
);
695 ctx
->pipe
->set_vertex_sampler_textures(ctx
->pipe
,
696 ctx
->nr_vertex_textures
,
697 ctx
->vertex_textures
);
699 ctx
->nr_vertex_textures_saved
= 0;
704 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
705 const struct pipe_depth_stencil_alpha_state
*templ
)
707 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
708 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
709 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
711 CSO_DEPTH_STENCIL_ALPHA
,
712 (void*)templ
, key_size
);
715 if (cso_hash_iter_is_null(iter
)) {
716 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
718 return PIPE_ERROR_OUT_OF_MEMORY
;
720 memcpy(&cso
->state
, templ
, sizeof(*templ
));
721 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
722 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
723 cso
->context
= ctx
->pipe
;
725 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
726 if (cso_hash_iter_is_null(iter
)) {
728 return PIPE_ERROR_OUT_OF_MEMORY
;
734 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
737 if (ctx
->depth_stencil
!= handle
) {
738 ctx
->depth_stencil
= handle
;
739 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
744 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
746 assert(!ctx
->depth_stencil_saved
);
747 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
750 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
752 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
753 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
754 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
756 ctx
->depth_stencil_saved
= NULL
;
761 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
762 const struct pipe_rasterizer_state
*templ
)
764 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
765 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
766 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
767 hash_key
, CSO_RASTERIZER
,
768 (void*)templ
, key_size
);
771 if (cso_hash_iter_is_null(iter
)) {
772 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
774 return PIPE_ERROR_OUT_OF_MEMORY
;
776 memcpy(&cso
->state
, templ
, sizeof(*templ
));
777 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
778 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
779 cso
->context
= ctx
->pipe
;
781 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
782 if (cso_hash_iter_is_null(iter
)) {
784 return PIPE_ERROR_OUT_OF_MEMORY
;
790 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
793 if (ctx
->rasterizer
!= handle
) {
794 ctx
->rasterizer
= handle
;
795 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
800 void cso_save_rasterizer(struct cso_context
*ctx
)
802 assert(!ctx
->rasterizer_saved
);
803 ctx
->rasterizer_saved
= ctx
->rasterizer
;
806 void cso_restore_rasterizer(struct cso_context
*ctx
)
808 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
809 ctx
->rasterizer
= ctx
->rasterizer_saved
;
810 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
812 ctx
->rasterizer_saved
= NULL
;
817 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
820 if (ctx
->fragment_shader
!= handle
) {
821 ctx
->fragment_shader
= handle
;
822 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
827 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
829 if (handle
== ctx
->fragment_shader
) {
830 /* unbind before deleting */
831 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
832 ctx
->fragment_shader
= NULL
;
834 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
837 /* Not really working:
840 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
841 const struct pipe_shader_state
*templ
)
843 const struct tgsi_token
*tokens
= templ
->tokens
;
844 unsigned num_tokens
= tgsi_num_tokens(tokens
);
845 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
846 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
847 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
851 sizeof(*templ
)); /* XXX correct? tokens_size? */
854 if (cso_hash_iter_is_null(iter
)) {
855 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
856 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
859 return PIPE_ERROR_OUT_OF_MEMORY
;
861 memcpy(cso_tokens
, tokens
, tokens_size
);
862 cso
->state
.tokens
= cso_tokens
;
863 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
864 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
865 cso
->context
= ctx
->pipe
;
867 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
868 if (cso_hash_iter_is_null(iter
)) {
870 return PIPE_ERROR_OUT_OF_MEMORY
;
876 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
879 return cso_set_fragment_shader_handle( ctx
, handle
);
883 void cso_save_fragment_shader(struct cso_context
*ctx
)
885 assert(!ctx
->fragment_shader_saved
);
886 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
889 void cso_restore_fragment_shader(struct cso_context
*ctx
)
891 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
892 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
893 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
895 ctx
->fragment_shader_saved
= NULL
;
899 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
902 if (ctx
->vertex_shader
!= handle
) {
903 ctx
->vertex_shader
= handle
;
904 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
909 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
911 if (handle
== ctx
->vertex_shader
) {
912 /* unbind before deleting */
913 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
914 ctx
->vertex_shader
= NULL
;
916 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
920 /* Not really working:
923 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
924 const struct pipe_shader_state
*templ
)
926 unsigned hash_key
= cso_construct_key((void*)templ
,
927 sizeof(struct pipe_shader_state
));
928 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
929 hash_key
, CSO_VERTEX_SHADER
,
934 if (cso_hash_iter_is_null(iter
)) {
935 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
938 return PIPE_ERROR_OUT_OF_MEMORY
;
940 memcpy(cso
->state
, templ
, sizeof(*templ
));
941 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
942 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
943 cso
->context
= ctx
->pipe
;
945 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
946 if (cso_hash_iter_is_null(iter
)) {
948 return PIPE_ERROR_OUT_OF_MEMORY
;
954 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
957 return cso_set_vertex_shader_handle( ctx
, handle
);
963 void cso_save_vertex_shader(struct cso_context
*ctx
)
965 assert(!ctx
->vertex_shader_saved
);
966 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
969 void cso_restore_vertex_shader(struct cso_context
*ctx
)
971 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
972 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
973 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
975 ctx
->vertex_shader_saved
= NULL
;
980 * Copy framebuffer state from src to dst with refcounting of surfaces.
983 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
984 const struct pipe_framebuffer_state
*src
)
988 dst
->width
= src
->width
;
989 dst
->height
= src
->height
;
990 dst
->nr_cbufs
= src
->nr_cbufs
;
991 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
992 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
994 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
999 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1003 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1004 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1006 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1010 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1011 const struct pipe_framebuffer_state
*fb
)
1013 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1014 copy_framebuffer_state(&ctx
->fb
, fb
);
1015 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1020 void cso_save_framebuffer(struct cso_context
*ctx
)
1022 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1025 void cso_restore_framebuffer(struct cso_context
*ctx
)
1027 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1028 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1029 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1030 free_framebuffer_state(&ctx
->fb_saved
);
1035 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1036 const struct pipe_viewport_state
*vp
)
1038 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1040 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1045 void cso_save_viewport(struct cso_context
*ctx
)
1047 ctx
->vp_saved
= ctx
->vp
;
1051 void cso_restore_viewport(struct cso_context
*ctx
)
1053 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1054 ctx
->vp
= ctx
->vp_saved
;
1055 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1062 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1063 const struct pipe_blend_color
*bc
)
1065 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1066 ctx
->blend_color
= *bc
;
1067 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1072 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1075 if (ctx
->geometry_shader
!= handle
) {
1076 ctx
->geometry_shader
= handle
;
1077 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1082 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1084 if (handle
== ctx
->geometry_shader
) {
1085 /* unbind before deleting */
1086 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1087 ctx
->geometry_shader
= NULL
;
1089 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1092 void cso_save_geometry_shader(struct cso_context
*ctx
)
1094 assert(!ctx
->geometry_shader_saved
);
1095 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1098 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1100 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1101 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1102 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1104 ctx
->geometry_shader_saved
= NULL
;