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_math.h"
41 #include "util/u_memory.h"
42 #include "tgsi/tgsi_parse.h"
44 #include "cso_cache/cso_context.h"
45 #include "cso_cache/cso_cache.h"
46 #include "cso_cache/cso_hash.h"
47 #include "cso_context.h"
50 struct pipe_context
*pipe
;
51 struct cso_cache
*cache
;
54 void *samplers
[PIPE_MAX_SAMPLERS
];
57 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
58 unsigned nr_vertex_samplers
;
61 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
65 unsigned nr_vertex_samplers
;
67 unsigned nr_samplers_saved
;
68 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
70 unsigned nr_vertex_samplers_saved
;
71 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
73 uint nr_fragment_sampler_views
;
74 struct pipe_sampler_view
*fragment_sampler_views
[PIPE_MAX_SAMPLERS
];
76 uint nr_vertex_sampler_views
;
77 struct pipe_sampler_view
*vertex_sampler_views
[PIPE_MAX_VERTEX_SAMPLERS
];
79 uint nr_fragment_sampler_views_saved
;
80 struct pipe_sampler_view
*fragment_sampler_views_saved
[PIPE_MAX_SAMPLERS
];
82 uint nr_vertex_sampler_views_saved
;
83 struct pipe_sampler_view
*vertex_sampler_views_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
85 /** Current and saved state.
86 * The saved state is used as a 1-deep stack.
88 void *blend
, *blend_saved
;
89 void *depth_stencil
, *depth_stencil_saved
;
90 void *rasterizer
, *rasterizer_saved
;
91 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
92 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
93 void *velements
, *velements_saved
;
95 struct pipe_clip_state clip
;
96 struct pipe_clip_state clip_saved
;
98 struct pipe_framebuffer_state fb
, fb_saved
;
99 struct pipe_viewport_state vp
, vp_saved
;
100 struct pipe_blend_color blend_color
;
101 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
106 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
109 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
111 struct cso_blend
*cso
= (struct cso_blend
*)state
;
113 if (ctx
->blend
== cso
->data
)
116 if (cso
->delete_state
)
117 cso
->delete_state(cso
->context
, cso
->data
);
122 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
124 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
126 if (ctx
->depth_stencil
== cso
->data
)
129 if (cso
->delete_state
)
130 cso
->delete_state(cso
->context
, cso
->data
);
136 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
138 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
139 if (cso
->delete_state
)
140 cso
->delete_state(cso
->context
, cso
->data
);
145 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
147 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
149 if (ctx
->rasterizer
== cso
->data
)
151 if (cso
->delete_state
)
152 cso
->delete_state(cso
->context
, cso
->data
);
157 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
159 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
160 if (ctx
->fragment_shader
== cso
->data
)
162 if (cso
->delete_state
)
163 cso
->delete_state(cso
->context
, cso
->data
);
168 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
170 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
171 if (ctx
->vertex_shader
== cso
->data
)
173 if (cso
->delete_state
)
174 cso
->delete_state(cso
->context
, cso
->data
);
179 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
182 struct cso_velements
*cso
= (struct cso_velements
*)state
;
184 if (ctx
->velements
== cso
->data
)
187 if (cso
->delete_state
)
188 cso
->delete_state(cso
->context
, cso
->data
);
194 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
195 void *state
, enum cso_cache_type type
)
199 return delete_blend_state(ctx
, state
);
202 return delete_sampler_state(ctx
, state
);
204 case CSO_DEPTH_STENCIL_ALPHA
:
205 return delete_depth_stencil_state(ctx
, state
);
208 return delete_rasterizer_state(ctx
, state
);
210 case CSO_FRAGMENT_SHADER
:
211 return delete_fs_state(ctx
, state
);
213 case CSO_VERTEX_SHADER
:
214 return delete_vs_state(ctx
, state
);
217 return delete_vertex_elements(ctx
, state
);
226 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
227 int max_size
, void *user_data
)
229 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
230 /* if we're approach the maximum size, remove fourth of the entries
231 * otherwise every subsequent call will go through the same */
232 int hash_size
= cso_hash_size(hash
);
233 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
234 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
235 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
236 if (hash_size
> max_size
)
237 to_remove
+= hash_size
- max_size
;
239 /*remove elements until we're good */
240 /*fixme: currently we pick the nodes to remove at random*/
241 void *cso
= cso_hash_iter_data(iter
);
242 if (delete_cso(ctx
, cso
, type
)) {
243 iter
= cso_hash_erase(hash
, iter
);
246 iter
= cso_hash_iter_next(iter
);
251 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
253 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
257 ctx
->cache
= cso_cache_create();
258 if (ctx
->cache
== NULL
)
260 cso_cache_set_sanitize_callback(ctx
->cache
,
266 /* Enable for testing: */
267 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
272 cso_destroy_context( ctx
);
278 * Prior to context destruction, this function unbinds all state objects.
280 void cso_release_all( struct cso_context
*ctx
)
285 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
286 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
287 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
288 if (ctx
->pipe
->bind_vertex_sampler_states
)
289 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
290 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
291 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
293 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
296 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
297 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
298 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
], NULL
);
301 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
302 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
303 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
], NULL
);
306 free_framebuffer_state(&ctx
->fb
);
307 free_framebuffer_state(&ctx
->fb_saved
);
310 cso_cache_delete( ctx
->cache
);
316 void cso_destroy_context( struct cso_context
*ctx
)
319 /*cso_release_all( ctx );*/
325 /* Those function will either find the state of the given template
326 * in the cache or they will create a new state from the given
327 * template, insert it in the cache and return it.
331 * If the driver returns 0 from the create method then they will assign
332 * the data member of the cso to be the template itself.
335 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
336 const struct pipe_blend_state
*templ
)
338 unsigned key_size
, hash_key
;
339 struct cso_hash_iter iter
;
342 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
343 (char *)&(templ
->rt
[1]) - (char *)templ
;
344 hash_key
= cso_construct_key((void*)templ
, key_size
);
345 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
347 if (cso_hash_iter_is_null(iter
)) {
348 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
350 return PIPE_ERROR_OUT_OF_MEMORY
;
352 memcpy(&cso
->state
, templ
, key_size
);
353 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
354 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
355 cso
->context
= ctx
->pipe
;
357 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
358 if (cso_hash_iter_is_null(iter
)) {
360 return PIPE_ERROR_OUT_OF_MEMORY
;
366 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
369 if (ctx
->blend
!= handle
) {
371 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
376 void cso_save_blend(struct cso_context
*ctx
)
378 assert(!ctx
->blend_saved
);
379 ctx
->blend_saved
= ctx
->blend
;
382 void cso_restore_blend(struct cso_context
*ctx
)
384 if (ctx
->blend
!= ctx
->blend_saved
) {
385 ctx
->blend
= ctx
->blend_saved
;
386 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
388 ctx
->blend_saved
= NULL
;
393 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
395 const struct pipe_sampler_state
*templ
)
400 unsigned key_size
= sizeof(struct pipe_sampler_state
);
401 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
402 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
403 hash_key
, CSO_SAMPLER
,
404 (void*)templ
, key_size
);
406 if (cso_hash_iter_is_null(iter
)) {
407 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
409 return PIPE_ERROR_OUT_OF_MEMORY
;
411 memcpy(&cso
->state
, templ
, sizeof(*templ
));
412 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
413 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
414 cso
->context
= ctx
->pipe
;
416 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
417 if (cso_hash_iter_is_null(iter
)) {
419 return PIPE_ERROR_OUT_OF_MEMORY
;
425 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
429 ctx
->samplers
[idx
] = handle
;
434 cso_single_vertex_sampler(struct cso_context
*ctx
,
436 const struct pipe_sampler_state
*templ
)
441 unsigned key_size
= sizeof(struct pipe_sampler_state
);
442 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
443 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
444 hash_key
, CSO_SAMPLER
,
445 (void*)templ
, key_size
);
447 if (cso_hash_iter_is_null(iter
)) {
448 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
450 return PIPE_ERROR_OUT_OF_MEMORY
;
452 memcpy(&cso
->state
, templ
, sizeof(*templ
));
453 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
454 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
455 cso
->context
= ctx
->pipe
;
457 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
458 if (cso_hash_iter_is_null(iter
)) {
460 return PIPE_ERROR_OUT_OF_MEMORY
;
466 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
470 ctx
->vertex_samplers
[idx
] = handle
;
474 void cso_single_sampler_done( struct cso_context
*ctx
)
478 /* find highest non-null sampler */
479 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
480 if (ctx
->samplers
[i
- 1] != NULL
)
484 ctx
->nr_samplers
= i
;
486 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
487 memcmp(ctx
->hw
.samplers
,
489 ctx
->nr_samplers
* sizeof(void *)) != 0)
491 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
492 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
494 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
499 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
503 /* find highest non-null sampler */
504 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
505 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
509 ctx
->nr_vertex_samplers
= i
;
511 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
512 memcmp(ctx
->hw
.vertex_samplers
,
513 ctx
->vertex_samplers
,
514 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
516 memcpy(ctx
->hw
.vertex_samplers
,
517 ctx
->vertex_samplers
,
518 ctx
->nr_vertex_samplers
* sizeof(void *));
519 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
521 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
522 ctx
->nr_vertex_samplers
,
523 ctx
->vertex_samplers
);
528 * If the function encouters any errors it will return the
529 * last one. Done to always try to set as many samplers
532 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
534 const struct pipe_sampler_state
**templates
)
537 enum pipe_error temp
, error
= PIPE_OK
;
542 for (i
= 0; i
< nr
; i
++) {
543 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
548 for ( ; i
< ctx
->nr_samplers
; i
++) {
549 temp
= cso_single_sampler( ctx
, i
, NULL
);
554 cso_single_sampler_done( ctx
);
559 void cso_save_samplers(struct cso_context
*ctx
)
561 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
562 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
565 void cso_restore_samplers(struct cso_context
*ctx
)
567 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
568 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
569 cso_single_sampler_done( ctx
);
573 * If the function encouters any errors it will return the
574 * last one. Done to always try to set as many samplers
577 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
579 const struct pipe_sampler_state
**templates
)
582 enum pipe_error temp
, error
= PIPE_OK
;
587 for (i
= 0; i
< nr
; i
++) {
588 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
593 for ( ; i
< ctx
->nr_samplers
; i
++) {
594 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
599 cso_single_vertex_sampler_done( ctx
);
605 cso_save_vertex_samplers(struct cso_context
*ctx
)
607 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
608 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
612 cso_restore_vertex_samplers(struct cso_context
*ctx
)
614 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
615 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
616 cso_single_vertex_sampler_done(ctx
);
620 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
621 const struct pipe_depth_stencil_alpha_state
*templ
)
623 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
624 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
625 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
627 CSO_DEPTH_STENCIL_ALPHA
,
628 (void*)templ
, key_size
);
631 if (cso_hash_iter_is_null(iter
)) {
632 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
634 return PIPE_ERROR_OUT_OF_MEMORY
;
636 memcpy(&cso
->state
, templ
, sizeof(*templ
));
637 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
638 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
639 cso
->context
= ctx
->pipe
;
641 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
642 if (cso_hash_iter_is_null(iter
)) {
644 return PIPE_ERROR_OUT_OF_MEMORY
;
650 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
653 if (ctx
->depth_stencil
!= handle
) {
654 ctx
->depth_stencil
= handle
;
655 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
660 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
662 assert(!ctx
->depth_stencil_saved
);
663 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
666 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
668 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
669 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
670 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
672 ctx
->depth_stencil_saved
= NULL
;
677 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
678 const struct pipe_rasterizer_state
*templ
)
680 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
681 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
682 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
683 hash_key
, CSO_RASTERIZER
,
684 (void*)templ
, key_size
);
687 if (cso_hash_iter_is_null(iter
)) {
688 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
690 return PIPE_ERROR_OUT_OF_MEMORY
;
692 memcpy(&cso
->state
, templ
, sizeof(*templ
));
693 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
694 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
695 cso
->context
= ctx
->pipe
;
697 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
698 if (cso_hash_iter_is_null(iter
)) {
700 return PIPE_ERROR_OUT_OF_MEMORY
;
706 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
709 if (ctx
->rasterizer
!= handle
) {
710 ctx
->rasterizer
= handle
;
711 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
716 void cso_save_rasterizer(struct cso_context
*ctx
)
718 assert(!ctx
->rasterizer_saved
);
719 ctx
->rasterizer_saved
= ctx
->rasterizer
;
722 void cso_restore_rasterizer(struct cso_context
*ctx
)
724 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
725 ctx
->rasterizer
= ctx
->rasterizer_saved
;
726 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
728 ctx
->rasterizer_saved
= NULL
;
733 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
736 if (ctx
->fragment_shader
!= handle
) {
737 ctx
->fragment_shader
= handle
;
738 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
743 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
745 if (handle
== ctx
->fragment_shader
) {
746 /* unbind before deleting */
747 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
748 ctx
->fragment_shader
= NULL
;
750 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
753 /* Not really working:
756 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
757 const struct pipe_shader_state
*templ
)
759 const struct tgsi_token
*tokens
= templ
->tokens
;
760 unsigned num_tokens
= tgsi_num_tokens(tokens
);
761 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
762 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
763 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
767 sizeof(*templ
)); /* XXX correct? tokens_size? */
770 if (cso_hash_iter_is_null(iter
)) {
771 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
772 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
775 return PIPE_ERROR_OUT_OF_MEMORY
;
777 memcpy(cso_tokens
, tokens
, tokens_size
);
778 cso
->state
.tokens
= cso_tokens
;
779 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
780 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
781 cso
->context
= ctx
->pipe
;
783 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
784 if (cso_hash_iter_is_null(iter
)) {
786 return PIPE_ERROR_OUT_OF_MEMORY
;
792 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
795 return cso_set_fragment_shader_handle( ctx
, handle
);
799 void cso_save_fragment_shader(struct cso_context
*ctx
)
801 assert(!ctx
->fragment_shader_saved
);
802 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
805 void cso_restore_fragment_shader(struct cso_context
*ctx
)
807 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
808 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
809 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
811 ctx
->fragment_shader_saved
= NULL
;
815 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
818 if (ctx
->vertex_shader
!= handle
) {
819 ctx
->vertex_shader
= handle
;
820 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
825 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
827 if (handle
== ctx
->vertex_shader
) {
828 /* unbind before deleting */
829 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
830 ctx
->vertex_shader
= NULL
;
832 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
836 /* Not really working:
839 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
840 const struct pipe_shader_state
*templ
)
842 unsigned hash_key
= cso_construct_key((void*)templ
,
843 sizeof(struct pipe_shader_state
));
844 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
845 hash_key
, CSO_VERTEX_SHADER
,
850 if (cso_hash_iter_is_null(iter
)) {
851 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
854 return PIPE_ERROR_OUT_OF_MEMORY
;
856 memcpy(cso
->state
, templ
, sizeof(*templ
));
857 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
858 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
859 cso
->context
= ctx
->pipe
;
861 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
862 if (cso_hash_iter_is_null(iter
)) {
864 return PIPE_ERROR_OUT_OF_MEMORY
;
870 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
873 return cso_set_vertex_shader_handle( ctx
, handle
);
879 void cso_save_vertex_shader(struct cso_context
*ctx
)
881 assert(!ctx
->vertex_shader_saved
);
882 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
885 void cso_restore_vertex_shader(struct cso_context
*ctx
)
887 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
888 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
889 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
891 ctx
->vertex_shader_saved
= NULL
;
896 * Copy framebuffer state from src to dst with refcounting of surfaces.
899 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
900 const struct pipe_framebuffer_state
*src
)
904 dst
->width
= src
->width
;
905 dst
->height
= src
->height
;
906 dst
->nr_cbufs
= src
->nr_cbufs
;
907 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
908 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
910 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
915 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
919 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
920 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
922 pipe_surface_reference(&fb
->zsbuf
, NULL
);
926 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
927 const struct pipe_framebuffer_state
*fb
)
929 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
930 copy_framebuffer_state(&ctx
->fb
, fb
);
931 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
936 void cso_save_framebuffer(struct cso_context
*ctx
)
938 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
941 void cso_restore_framebuffer(struct cso_context
*ctx
)
943 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
944 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
945 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
946 free_framebuffer_state(&ctx
->fb_saved
);
951 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
952 const struct pipe_viewport_state
*vp
)
954 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
956 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
961 void cso_save_viewport(struct cso_context
*ctx
)
963 ctx
->vp_saved
= ctx
->vp
;
967 void cso_restore_viewport(struct cso_context
*ctx
)
969 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
970 ctx
->vp
= ctx
->vp_saved
;
971 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
976 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
977 const struct pipe_blend_color
*bc
)
979 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
980 ctx
->blend_color
= *bc
;
981 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
986 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
987 const struct pipe_stencil_ref
*sr
)
989 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
990 ctx
->stencil_ref
= *sr
;
991 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
996 void cso_save_stencil_ref(struct cso_context
*ctx
)
998 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1002 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1004 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1005 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1006 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1010 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1013 if (ctx
->geometry_shader
!= handle
) {
1014 ctx
->geometry_shader
= handle
;
1015 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1020 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1022 if (handle
== ctx
->geometry_shader
) {
1023 /* unbind before deleting */
1024 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1025 ctx
->geometry_shader
= NULL
;
1027 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1030 void cso_save_geometry_shader(struct cso_context
*ctx
)
1032 assert(!ctx
->geometry_shader_saved
);
1033 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1036 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1038 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1039 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1040 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1042 ctx
->geometry_shader_saved
= NULL
;
1048 clip_state_cpy(struct pipe_clip_state
*dst
,
1049 const struct pipe_clip_state
*src
)
1053 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1058 clip_state_cmp(const struct pipe_clip_state
*a
,
1059 const struct pipe_clip_state
*b
)
1061 if (a
->nr
!= b
->nr
) {
1065 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1071 cso_set_clip(struct cso_context
*ctx
,
1072 const struct pipe_clip_state
*clip
)
1074 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1075 clip_state_cpy(&ctx
->clip
, clip
);
1076 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1081 cso_save_clip(struct cso_context
*ctx
)
1083 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1087 cso_restore_clip(struct cso_context
*ctx
)
1089 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1090 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1091 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1095 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1097 const struct pipe_vertex_element
*states
)
1099 unsigned key_size
, hash_key
;
1100 struct cso_hash_iter iter
;
1102 struct cso_velems_state velems_state
;
1104 /* need to include the count into the stored state data too.
1105 Otherwise first few count pipe_vertex_elements could be identical even if count
1106 is different, and there's no guarantee the hash would be different in that
1108 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1109 velems_state
.count
= count
;
1110 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1111 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1112 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1114 if (cso_hash_iter_is_null(iter
)) {
1115 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1117 return PIPE_ERROR_OUT_OF_MEMORY
;
1119 memcpy(&cso
->state
, &velems_state
, key_size
);
1120 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1121 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1122 cso
->context
= ctx
->pipe
;
1124 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1125 if (cso_hash_iter_is_null(iter
)) {
1127 return PIPE_ERROR_OUT_OF_MEMORY
;
1133 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1136 if (ctx
->velements
!= handle
) {
1137 ctx
->velements
= handle
;
1138 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1143 void cso_save_vertex_elements(struct cso_context
*ctx
)
1145 assert(!ctx
->velements_saved
);
1146 ctx
->velements_saved
= ctx
->velements
;
1149 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1151 if (ctx
->velements
!= ctx
->velements_saved
) {
1152 ctx
->velements
= ctx
->velements_saved
;
1153 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1155 ctx
->velements_saved
= NULL
;
1158 /* fragment sampler view state */
1161 cso_set_fragment_sampler_views(struct cso_context
*cso
,
1163 struct pipe_sampler_view
**views
)
1167 for (i
= 0; i
< count
; i
++) {
1168 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], views
[i
]);
1170 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1171 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1174 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1175 MAX2(count
, cso
->nr_fragment_sampler_views
),
1176 cso
->fragment_sampler_views
);
1178 cso
->nr_fragment_sampler_views
= count
;
1182 cso_save_fragment_sampler_views(struct cso_context
*cso
)
1186 cso
->nr_fragment_sampler_views_saved
= cso
->nr_fragment_sampler_views
;
1188 for (i
= 0; i
< cso
->nr_fragment_sampler_views
; i
++) {
1189 assert(!cso
->fragment_sampler_views_saved
[i
]);
1191 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
],
1192 cso
->fragment_sampler_views
[i
]);
1197 cso_restore_fragment_sampler_views(struct cso_context
*cso
)
1201 for (i
= 0; i
< cso
->nr_fragment_sampler_views_saved
; i
++) {
1202 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], cso
->fragment_sampler_views_saved
[i
]);
1203 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
], NULL
);
1205 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1206 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1209 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1210 MAX2(cso
->nr_fragment_sampler_views
, cso
->nr_fragment_sampler_views_saved
),
1211 cso
->fragment_sampler_views
);
1213 cso
->nr_fragment_sampler_views
= cso
->nr_fragment_sampler_views_saved
;
1214 cso
->nr_fragment_sampler_views_saved
= 0;
1218 /* vertex sampler view state */
1221 cso_set_vertex_sampler_views(struct cso_context
*cso
,
1223 struct pipe_sampler_view
**views
)
1227 for (i
= 0; i
< count
; i
++) {
1228 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], views
[i
]);
1230 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1231 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1234 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1235 MAX2(count
, cso
->nr_vertex_sampler_views
),
1236 cso
->vertex_sampler_views
);
1238 cso
->nr_vertex_sampler_views
= count
;
1242 cso_save_vertex_sampler_views(struct cso_context
*cso
)
1246 cso
->nr_vertex_sampler_views_saved
= cso
->nr_vertex_sampler_views
;
1248 for (i
= 0; i
< cso
->nr_vertex_sampler_views
; i
++) {
1249 assert(!cso
->vertex_sampler_views_saved
[i
]);
1251 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
],
1252 cso
->vertex_sampler_views
[i
]);
1257 cso_restore_vertex_sampler_views(struct cso_context
*cso
)
1261 for (i
= 0; i
< cso
->nr_vertex_sampler_views_saved
; i
++) {
1262 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], cso
->vertex_sampler_views_saved
[i
]);
1263 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
], NULL
);
1265 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1266 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1269 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1270 MAX2(cso
->nr_vertex_sampler_views
, cso
->nr_vertex_sampler_views_saved
),
1271 cso
->vertex_sampler_views
);
1273 cso
->nr_vertex_sampler_views
= cso
->nr_vertex_sampler_views_saved
;
1274 cso
->nr_vertex_sampler_views_saved
= 0;