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 "util/u_sampler.h"
43 #include "tgsi/tgsi_parse.h"
45 #include "cso_cache/cso_context.h"
46 #include "cso_cache/cso_cache.h"
47 #include "cso_cache/cso_hash.h"
48 #include "cso_context.h"
51 struct pipe_context
*pipe
;
52 struct cso_cache
*cache
;
55 void *samplers
[PIPE_MAX_SAMPLERS
];
58 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
59 unsigned nr_vertex_samplers
;
62 void *samplers
[PIPE_MAX_SAMPLERS
];
65 void *vertex_samplers
[PIPE_MAX_VERTEX_SAMPLERS
];
66 unsigned nr_vertex_samplers
;
68 unsigned nr_samplers_saved
;
69 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
71 unsigned nr_vertex_samplers_saved
;
72 void *vertex_samplers_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
74 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
75 uint nr_fragment_sampler_views
;
76 struct pipe_sampler_view
*fragment_sampler_views
[PIPE_MAX_SAMPLERS
];
79 uint nr_vertex_sampler_views
;
80 struct pipe_sampler_view
*vertex_sampler_views
[PIPE_MAX_VERTEX_SAMPLERS
];
82 uint nr_textures_saved
;
83 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
84 uint nr_fragment_sampler_views_saved
;
85 struct pipe_sampler_view
*fragment_sampler_views_saved
[PIPE_MAX_SAMPLERS
];
87 uint nr_vertex_sampler_views_saved
;
88 struct pipe_sampler_view
*vertex_sampler_views_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
90 /** Current and saved state.
91 * The saved state is used as a 1-deep stack.
93 void *blend
, *blend_saved
;
94 void *depth_stencil
, *depth_stencil_saved
;
95 void *rasterizer
, *rasterizer_saved
;
96 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
97 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
98 void *velements
, *velements_saved
;
100 struct pipe_clip_state clip
;
101 struct pipe_clip_state clip_saved
;
103 struct pipe_framebuffer_state fb
, fb_saved
;
104 struct pipe_viewport_state vp
, vp_saved
;
105 struct pipe_blend_color blend_color
;
106 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
111 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
114 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
116 struct cso_blend
*cso
= (struct cso_blend
*)state
;
118 if (ctx
->blend
== cso
->data
)
121 if (cso
->delete_state
)
122 cso
->delete_state(cso
->context
, cso
->data
);
127 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
129 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
131 if (ctx
->depth_stencil
== cso
->data
)
134 if (cso
->delete_state
)
135 cso
->delete_state(cso
->context
, cso
->data
);
141 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
143 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
144 if (cso
->delete_state
)
145 cso
->delete_state(cso
->context
, cso
->data
);
150 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
152 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
154 if (ctx
->rasterizer
== cso
->data
)
156 if (cso
->delete_state
)
157 cso
->delete_state(cso
->context
, cso
->data
);
162 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
164 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
165 if (ctx
->fragment_shader
== cso
->data
)
167 if (cso
->delete_state
)
168 cso
->delete_state(cso
->context
, cso
->data
);
173 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
175 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
176 if (ctx
->vertex_shader
== cso
->data
)
178 if (cso
->delete_state
)
179 cso
->delete_state(cso
->context
, cso
->data
);
184 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
187 struct cso_velements
*cso
= (struct cso_velements
*)state
;
189 if (ctx
->velements
== cso
->data
)
192 if (cso
->delete_state
)
193 cso
->delete_state(cso
->context
, cso
->data
);
199 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
200 void *state
, enum cso_cache_type type
)
204 return delete_blend_state(ctx
, state
);
207 return delete_sampler_state(ctx
, state
);
209 case CSO_DEPTH_STENCIL_ALPHA
:
210 return delete_depth_stencil_state(ctx
, state
);
213 return delete_rasterizer_state(ctx
, state
);
215 case CSO_FRAGMENT_SHADER
:
216 return delete_fs_state(ctx
, state
);
218 case CSO_VERTEX_SHADER
:
219 return delete_vs_state(ctx
, state
);
222 return delete_vertex_elements(ctx
, state
);
231 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
232 int max_size
, void *user_data
)
234 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
235 /* if we're approach the maximum size, remove fourth of the entries
236 * otherwise every subsequent call will go through the same */
237 int hash_size
= cso_hash_size(hash
);
238 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
239 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
240 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
241 if (hash_size
> max_size
)
242 to_remove
+= hash_size
- max_size
;
244 /*remove elements until we're good */
245 /*fixme: currently we pick the nodes to remove at random*/
246 void *cso
= cso_hash_iter_data(iter
);
247 if (delete_cso(ctx
, cso
, type
)) {
248 iter
= cso_hash_erase(hash
, iter
);
251 iter
= cso_hash_iter_next(iter
);
256 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
258 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
262 ctx
->cache
= cso_cache_create();
263 if (ctx
->cache
== NULL
)
265 cso_cache_set_sanitize_callback(ctx
->cache
,
271 /* Enable for testing: */
272 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
277 cso_destroy_context( ctx
);
283 * Prior to context destruction, this function unbinds all state objects.
285 void cso_release_all( struct cso_context
*ctx
)
290 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
291 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
293 if (ctx
->pipe
->bind_vertex_sampler_states
)
294 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
295 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
296 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
297 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
298 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
301 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
302 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
303 pipe_texture_reference(&ctx
->textures_saved
[i
], NULL
);
304 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
305 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
], NULL
);
308 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
309 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
310 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
], NULL
);
313 free_framebuffer_state(&ctx
->fb
);
314 free_framebuffer_state(&ctx
->fb_saved
);
317 cso_cache_delete( ctx
->cache
);
323 void cso_destroy_context( struct cso_context
*ctx
)
326 /*cso_release_all( ctx );*/
332 /* Those function will either find the state of the given template
333 * in the cache or they will create a new state from the given
334 * template, insert it in the cache and return it.
338 * If the driver returns 0 from the create method then they will assign
339 * the data member of the cso to be the template itself.
342 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
343 const struct pipe_blend_state
*templ
)
345 unsigned key_size
, hash_key
;
346 struct cso_hash_iter iter
;
349 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
350 (char *)&(templ
->rt
[1]) - (char *)templ
;
351 hash_key
= cso_construct_key((void*)templ
, key_size
);
352 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
354 if (cso_hash_iter_is_null(iter
)) {
355 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
357 return PIPE_ERROR_OUT_OF_MEMORY
;
359 memcpy(&cso
->state
, templ
, key_size
);
360 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
361 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
362 cso
->context
= ctx
->pipe
;
364 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
365 if (cso_hash_iter_is_null(iter
)) {
367 return PIPE_ERROR_OUT_OF_MEMORY
;
373 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
376 if (ctx
->blend
!= handle
) {
378 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
383 void cso_save_blend(struct cso_context
*ctx
)
385 assert(!ctx
->blend_saved
);
386 ctx
->blend_saved
= ctx
->blend
;
389 void cso_restore_blend(struct cso_context
*ctx
)
391 if (ctx
->blend
!= ctx
->blend_saved
) {
392 ctx
->blend
= ctx
->blend_saved
;
393 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
395 ctx
->blend_saved
= NULL
;
400 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
402 const struct pipe_sampler_state
*templ
)
407 unsigned key_size
= sizeof(struct pipe_sampler_state
);
408 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
409 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
410 hash_key
, CSO_SAMPLER
,
411 (void*)templ
, key_size
);
413 if (cso_hash_iter_is_null(iter
)) {
414 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
416 return PIPE_ERROR_OUT_OF_MEMORY
;
418 memcpy(&cso
->state
, templ
, sizeof(*templ
));
419 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
420 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
421 cso
->context
= ctx
->pipe
;
423 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
424 if (cso_hash_iter_is_null(iter
)) {
426 return PIPE_ERROR_OUT_OF_MEMORY
;
432 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
436 ctx
->samplers
[idx
] = handle
;
441 cso_single_vertex_sampler(struct cso_context
*ctx
,
443 const struct pipe_sampler_state
*templ
)
448 unsigned key_size
= sizeof(struct pipe_sampler_state
);
449 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
450 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
451 hash_key
, CSO_SAMPLER
,
452 (void*)templ
, key_size
);
454 if (cso_hash_iter_is_null(iter
)) {
455 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
457 return PIPE_ERROR_OUT_OF_MEMORY
;
459 memcpy(&cso
->state
, templ
, sizeof(*templ
));
460 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
461 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
462 cso
->context
= ctx
->pipe
;
464 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
465 if (cso_hash_iter_is_null(iter
)) {
467 return PIPE_ERROR_OUT_OF_MEMORY
;
473 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
477 ctx
->vertex_samplers
[idx
] = handle
;
481 void cso_single_sampler_done( struct cso_context
*ctx
)
485 /* find highest non-null sampler */
486 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
487 if (ctx
->samplers
[i
- 1] != NULL
)
491 ctx
->nr_samplers
= i
;
493 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
494 memcmp(ctx
->hw
.samplers
,
496 ctx
->nr_samplers
* sizeof(void *)) != 0)
498 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
499 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
501 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
506 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
510 /* find highest non-null sampler */
511 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
512 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
516 ctx
->nr_vertex_samplers
= i
;
518 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
519 memcmp(ctx
->hw
.vertex_samplers
,
520 ctx
->vertex_samplers
,
521 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
523 memcpy(ctx
->hw
.vertex_samplers
,
524 ctx
->vertex_samplers
,
525 ctx
->nr_vertex_samplers
* sizeof(void *));
526 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
528 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
529 ctx
->nr_vertex_samplers
,
530 ctx
->vertex_samplers
);
535 * If the function encouters any errors it will return the
536 * last one. Done to always try to set as many samplers
539 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
541 const struct pipe_sampler_state
**templates
)
544 enum pipe_error temp
, error
= PIPE_OK
;
549 for (i
= 0; i
< nr
; i
++) {
550 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
555 for ( ; i
< ctx
->nr_samplers
; i
++) {
556 temp
= cso_single_sampler( ctx
, i
, NULL
);
561 cso_single_sampler_done( ctx
);
566 void cso_save_samplers(struct cso_context
*ctx
)
568 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
569 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
572 void cso_restore_samplers(struct cso_context
*ctx
)
574 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
575 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
576 cso_single_sampler_done( ctx
);
580 * If the function encouters any errors it will return the
581 * last one. Done to always try to set as many samplers
584 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
586 const struct pipe_sampler_state
**templates
)
589 enum pipe_error temp
, error
= PIPE_OK
;
594 for (i
= 0; i
< nr
; i
++) {
595 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
600 for ( ; i
< ctx
->nr_samplers
; i
++) {
601 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
606 cso_single_vertex_sampler_done( ctx
);
612 cso_save_vertex_samplers(struct cso_context
*ctx
)
614 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
615 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
619 cso_restore_vertex_samplers(struct cso_context
*ctx
)
621 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
622 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
623 cso_single_vertex_sampler_done(ctx
);
627 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
629 struct pipe_texture
**textures
)
633 ctx
->nr_textures
= count
;
635 for (i
= 0; i
< count
; i
++) {
636 struct pipe_sampler_view templ
, *view
;
638 u_sampler_view_default_template(&templ
,
640 textures
[i
]->format
);
641 view
= ctx
->pipe
->create_sampler_view(ctx
->pipe
,
645 pipe_texture_reference(&ctx
->textures
[i
], textures
[i
]);
646 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], view
);
648 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
649 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
650 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
653 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
655 ctx
->fragment_sampler_views
);
660 void cso_save_sampler_textures( struct cso_context
*ctx
)
664 ctx
->nr_textures_saved
= ctx
->nr_textures
;
665 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
666 assert(!ctx
->textures_saved
[i
]);
667 assert(!ctx
->fragment_sampler_views_saved
[i
]);
669 pipe_texture_reference(&ctx
->textures_saved
[i
], ctx
->textures
[i
]);
670 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
],
671 ctx
->fragment_sampler_views
[i
]);
675 void cso_restore_sampler_textures( struct cso_context
*ctx
)
679 ctx
->nr_textures
= ctx
->nr_textures_saved
;
681 for (i
= 0; i
< ctx
->nr_textures
; i
++) {
682 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
683 ctx
->textures
[i
] = ctx
->textures_saved
[i
];
684 ctx
->textures_saved
[i
] = NULL
;
686 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
687 ctx
->fragment_sampler_views
[i
] = ctx
->fragment_sampler_views_saved
[i
];
688 ctx
->fragment_sampler_views_saved
[i
] = NULL
;
690 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
691 pipe_texture_reference(&ctx
->textures
[i
], NULL
);
692 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
695 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
697 ctx
->fragment_sampler_views
);
699 ctx
->nr_textures_saved
= 0;
703 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
704 const struct pipe_depth_stencil_alpha_state
*templ
)
706 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
707 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
708 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
710 CSO_DEPTH_STENCIL_ALPHA
,
711 (void*)templ
, key_size
);
714 if (cso_hash_iter_is_null(iter
)) {
715 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
717 return PIPE_ERROR_OUT_OF_MEMORY
;
719 memcpy(&cso
->state
, templ
, sizeof(*templ
));
720 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
721 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
722 cso
->context
= ctx
->pipe
;
724 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
725 if (cso_hash_iter_is_null(iter
)) {
727 return PIPE_ERROR_OUT_OF_MEMORY
;
733 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
736 if (ctx
->depth_stencil
!= handle
) {
737 ctx
->depth_stencil
= handle
;
738 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
743 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
745 assert(!ctx
->depth_stencil_saved
);
746 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
749 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
751 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
752 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
753 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
755 ctx
->depth_stencil_saved
= NULL
;
760 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
761 const struct pipe_rasterizer_state
*templ
)
763 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
764 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
765 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
766 hash_key
, CSO_RASTERIZER
,
767 (void*)templ
, key_size
);
770 if (cso_hash_iter_is_null(iter
)) {
771 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
773 return PIPE_ERROR_OUT_OF_MEMORY
;
775 memcpy(&cso
->state
, templ
, sizeof(*templ
));
776 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
777 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
778 cso
->context
= ctx
->pipe
;
780 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
781 if (cso_hash_iter_is_null(iter
)) {
783 return PIPE_ERROR_OUT_OF_MEMORY
;
789 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
792 if (ctx
->rasterizer
!= handle
) {
793 ctx
->rasterizer
= handle
;
794 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
799 void cso_save_rasterizer(struct cso_context
*ctx
)
801 assert(!ctx
->rasterizer_saved
);
802 ctx
->rasterizer_saved
= ctx
->rasterizer
;
805 void cso_restore_rasterizer(struct cso_context
*ctx
)
807 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
808 ctx
->rasterizer
= ctx
->rasterizer_saved
;
809 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
811 ctx
->rasterizer_saved
= NULL
;
816 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
819 if (ctx
->fragment_shader
!= handle
) {
820 ctx
->fragment_shader
= handle
;
821 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
826 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
828 if (handle
== ctx
->fragment_shader
) {
829 /* unbind before deleting */
830 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
831 ctx
->fragment_shader
= NULL
;
833 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
836 /* Not really working:
839 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
840 const struct pipe_shader_state
*templ
)
842 const struct tgsi_token
*tokens
= templ
->tokens
;
843 unsigned num_tokens
= tgsi_num_tokens(tokens
);
844 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
845 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
846 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
850 sizeof(*templ
)); /* XXX correct? tokens_size? */
853 if (cso_hash_iter_is_null(iter
)) {
854 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
855 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
858 return PIPE_ERROR_OUT_OF_MEMORY
;
860 memcpy(cso_tokens
, tokens
, tokens_size
);
861 cso
->state
.tokens
= cso_tokens
;
862 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
863 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
864 cso
->context
= ctx
->pipe
;
866 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
867 if (cso_hash_iter_is_null(iter
)) {
869 return PIPE_ERROR_OUT_OF_MEMORY
;
875 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
878 return cso_set_fragment_shader_handle( ctx
, handle
);
882 void cso_save_fragment_shader(struct cso_context
*ctx
)
884 assert(!ctx
->fragment_shader_saved
);
885 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
888 void cso_restore_fragment_shader(struct cso_context
*ctx
)
890 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
891 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
892 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
894 ctx
->fragment_shader_saved
= NULL
;
898 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
901 if (ctx
->vertex_shader
!= handle
) {
902 ctx
->vertex_shader
= handle
;
903 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
908 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
910 if (handle
== ctx
->vertex_shader
) {
911 /* unbind before deleting */
912 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
913 ctx
->vertex_shader
= NULL
;
915 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
919 /* Not really working:
922 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
923 const struct pipe_shader_state
*templ
)
925 unsigned hash_key
= cso_construct_key((void*)templ
,
926 sizeof(struct pipe_shader_state
));
927 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
928 hash_key
, CSO_VERTEX_SHADER
,
933 if (cso_hash_iter_is_null(iter
)) {
934 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
937 return PIPE_ERROR_OUT_OF_MEMORY
;
939 memcpy(cso
->state
, templ
, sizeof(*templ
));
940 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
941 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
942 cso
->context
= ctx
->pipe
;
944 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
945 if (cso_hash_iter_is_null(iter
)) {
947 return PIPE_ERROR_OUT_OF_MEMORY
;
953 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
956 return cso_set_vertex_shader_handle( ctx
, handle
);
962 void cso_save_vertex_shader(struct cso_context
*ctx
)
964 assert(!ctx
->vertex_shader_saved
);
965 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
968 void cso_restore_vertex_shader(struct cso_context
*ctx
)
970 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
971 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
972 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
974 ctx
->vertex_shader_saved
= NULL
;
979 * Copy framebuffer state from src to dst with refcounting of surfaces.
982 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
983 const struct pipe_framebuffer_state
*src
)
987 dst
->width
= src
->width
;
988 dst
->height
= src
->height
;
989 dst
->nr_cbufs
= src
->nr_cbufs
;
990 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
991 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
993 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
998 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
1002 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
1003 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
1005 pipe_surface_reference(&fb
->zsbuf
, NULL
);
1009 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
1010 const struct pipe_framebuffer_state
*fb
)
1012 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
1013 copy_framebuffer_state(&ctx
->fb
, fb
);
1014 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1019 void cso_save_framebuffer(struct cso_context
*ctx
)
1021 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1024 void cso_restore_framebuffer(struct cso_context
*ctx
)
1026 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1027 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1028 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1029 free_framebuffer_state(&ctx
->fb_saved
);
1034 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1035 const struct pipe_viewport_state
*vp
)
1037 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1039 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1044 void cso_save_viewport(struct cso_context
*ctx
)
1046 ctx
->vp_saved
= ctx
->vp
;
1050 void cso_restore_viewport(struct cso_context
*ctx
)
1052 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1053 ctx
->vp
= ctx
->vp_saved
;
1054 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1059 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1060 const struct pipe_blend_color
*bc
)
1062 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1063 ctx
->blend_color
= *bc
;
1064 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1069 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1070 const struct pipe_stencil_ref
*sr
)
1072 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1073 ctx
->stencil_ref
= *sr
;
1074 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1079 void cso_save_stencil_ref(struct cso_context
*ctx
)
1081 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1085 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1087 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1088 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1089 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1093 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1096 if (ctx
->geometry_shader
!= handle
) {
1097 ctx
->geometry_shader
= handle
;
1098 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1103 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1105 if (handle
== ctx
->geometry_shader
) {
1106 /* unbind before deleting */
1107 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1108 ctx
->geometry_shader
= NULL
;
1110 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1113 void cso_save_geometry_shader(struct cso_context
*ctx
)
1115 assert(!ctx
->geometry_shader_saved
);
1116 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1119 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1121 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1122 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1123 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1125 ctx
->geometry_shader_saved
= NULL
;
1131 clip_state_cpy(struct pipe_clip_state
*dst
,
1132 const struct pipe_clip_state
*src
)
1136 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1141 clip_state_cmp(const struct pipe_clip_state
*a
,
1142 const struct pipe_clip_state
*b
)
1144 if (a
->nr
!= b
->nr
) {
1148 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1154 cso_set_clip(struct cso_context
*ctx
,
1155 const struct pipe_clip_state
*clip
)
1157 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1158 clip_state_cpy(&ctx
->clip
, clip
);
1159 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1164 cso_save_clip(struct cso_context
*ctx
)
1166 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1170 cso_restore_clip(struct cso_context
*ctx
)
1172 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1173 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1174 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1178 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1180 const struct pipe_vertex_element
*states
)
1182 unsigned key_size
, hash_key
;
1183 struct cso_hash_iter iter
;
1185 struct cso_velems_state velems_state
;
1187 /* need to include the count into the stored state data too.
1188 Otherwise first few count pipe_vertex_elements could be identical even if count
1189 is different, and there's no guarantee the hash would be different in that
1191 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1192 velems_state
.count
= count
;
1193 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1194 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1195 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1197 if (cso_hash_iter_is_null(iter
)) {
1198 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1200 return PIPE_ERROR_OUT_OF_MEMORY
;
1202 memcpy(&cso
->state
, &velems_state
, key_size
);
1203 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1204 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1205 cso
->context
= ctx
->pipe
;
1207 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1208 if (cso_hash_iter_is_null(iter
)) {
1210 return PIPE_ERROR_OUT_OF_MEMORY
;
1216 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1219 if (ctx
->velements
!= handle
) {
1220 ctx
->velements
= handle
;
1221 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1226 void cso_save_vertex_elements(struct cso_context
*ctx
)
1228 assert(!ctx
->velements_saved
);
1229 ctx
->velements_saved
= ctx
->velements
;
1232 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1234 if (ctx
->velements
!= ctx
->velements_saved
) {
1235 ctx
->velements
= ctx
->velements_saved
;
1236 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1238 ctx
->velements_saved
= NULL
;
1241 /* fragment sampler view state */
1244 cso_set_fragment_sampler_views(struct cso_context
*cso
,
1246 struct pipe_sampler_view
**views
)
1250 for (i
= 0; i
< count
; i
++) {
1251 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], views
[i
]);
1253 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1254 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1257 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1258 MAX2(count
, cso
->nr_fragment_sampler_views
),
1259 cso
->fragment_sampler_views
);
1261 cso
->nr_fragment_sampler_views
= count
;
1265 cso_save_fragment_sampler_views(struct cso_context
*cso
)
1269 cso
->nr_fragment_sampler_views_saved
= cso
->nr_fragment_sampler_views
;
1271 for (i
= 0; i
< cso
->nr_fragment_sampler_views
; i
++) {
1272 assert(!cso
->fragment_sampler_views_saved
[i
]);
1274 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
],
1275 cso
->fragment_sampler_views
[i
]);
1280 cso_restore_fragment_sampler_views(struct cso_context
*cso
)
1284 for (i
= 0; i
< cso
->nr_fragment_sampler_views_saved
; i
++) {
1285 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], cso
->fragment_sampler_views_saved
[i
]);
1286 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
], NULL
);
1288 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1289 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1292 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1293 MAX2(cso
->nr_fragment_sampler_views
, cso
->nr_fragment_sampler_views_saved
),
1294 cso
->fragment_sampler_views
);
1296 cso
->nr_fragment_sampler_views
= cso
->nr_fragment_sampler_views_saved
;
1297 cso
->nr_fragment_sampler_views_saved
= 0;
1301 /* vertex sampler view state */
1304 cso_set_vertex_sampler_views(struct cso_context
*cso
,
1306 struct pipe_sampler_view
**views
)
1310 for (i
= 0; i
< count
; i
++) {
1311 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], views
[i
]);
1313 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1314 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1317 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1318 MAX2(count
, cso
->nr_vertex_sampler_views
),
1319 cso
->vertex_sampler_views
);
1321 cso
->nr_vertex_sampler_views
= count
;
1325 cso_save_vertex_sampler_views(struct cso_context
*cso
)
1329 cso
->nr_vertex_sampler_views_saved
= cso
->nr_vertex_sampler_views
;
1331 for (i
= 0; i
< cso
->nr_vertex_sampler_views
; i
++) {
1332 assert(!cso
->vertex_sampler_views_saved
[i
]);
1334 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
],
1335 cso
->vertex_sampler_views
[i
]);
1340 cso_restore_vertex_sampler_views(struct cso_context
*cso
)
1344 for (i
= 0; i
< cso
->nr_vertex_sampler_views_saved
; i
++) {
1345 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], cso
->vertex_sampler_views_saved
[i
]);
1346 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
], NULL
);
1348 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1349 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1352 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1353 MAX2(cso
->nr_vertex_sampler_views
, cso
->nr_vertex_sampler_views_saved
),
1354 cso
->vertex_sampler_views
);
1356 cso
->nr_vertex_sampler_views
= cso
->nr_vertex_sampler_views_saved
;
1357 cso
->nr_vertex_sampler_views_saved
= 0;