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 uint nr_fragment_sampler_views
;
75 struct pipe_sampler_view
*fragment_sampler_views
[PIPE_MAX_SAMPLERS
];
77 uint nr_vertex_sampler_views
;
78 struct pipe_sampler_view
*vertex_sampler_views
[PIPE_MAX_VERTEX_SAMPLERS
];
80 uint nr_fragment_sampler_views_saved
;
81 struct pipe_sampler_view
*fragment_sampler_views_saved
[PIPE_MAX_SAMPLERS
];
83 uint nr_vertex_sampler_views_saved
;
84 struct pipe_sampler_view
*vertex_sampler_views_saved
[PIPE_MAX_VERTEX_SAMPLERS
];
86 /** Current and saved state.
87 * The saved state is used as a 1-deep stack.
89 void *blend
, *blend_saved
;
90 void *depth_stencil
, *depth_stencil_saved
;
91 void *rasterizer
, *rasterizer_saved
;
92 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
93 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
94 void *velements
, *velements_saved
;
96 struct pipe_clip_state clip
;
97 struct pipe_clip_state clip_saved
;
99 struct pipe_framebuffer_state fb
, fb_saved
;
100 struct pipe_viewport_state vp
, vp_saved
;
101 struct pipe_blend_color blend_color
;
102 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
107 free_framebuffer_state(struct pipe_framebuffer_state
*fb
);
110 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
112 struct cso_blend
*cso
= (struct cso_blend
*)state
;
114 if (ctx
->blend
== cso
->data
)
117 if (cso
->delete_state
)
118 cso
->delete_state(cso
->context
, cso
->data
);
123 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
125 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
127 if (ctx
->depth_stencil
== cso
->data
)
130 if (cso
->delete_state
)
131 cso
->delete_state(cso
->context
, cso
->data
);
137 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
139 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
140 if (cso
->delete_state
)
141 cso
->delete_state(cso
->context
, cso
->data
);
146 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
148 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
150 if (ctx
->rasterizer
== cso
->data
)
152 if (cso
->delete_state
)
153 cso
->delete_state(cso
->context
, cso
->data
);
158 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
160 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
161 if (ctx
->fragment_shader
== cso
->data
)
163 if (cso
->delete_state
)
164 cso
->delete_state(cso
->context
, cso
->data
);
169 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
171 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
172 if (ctx
->vertex_shader
== cso
->data
)
174 if (cso
->delete_state
)
175 cso
->delete_state(cso
->context
, cso
->data
);
180 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
183 struct cso_velements
*cso
= (struct cso_velements
*)state
;
185 if (ctx
->velements
== cso
->data
)
188 if (cso
->delete_state
)
189 cso
->delete_state(cso
->context
, cso
->data
);
195 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
196 void *state
, enum cso_cache_type type
)
200 return delete_blend_state(ctx
, state
);
203 return delete_sampler_state(ctx
, state
);
205 case CSO_DEPTH_STENCIL_ALPHA
:
206 return delete_depth_stencil_state(ctx
, state
);
209 return delete_rasterizer_state(ctx
, state
);
211 case CSO_FRAGMENT_SHADER
:
212 return delete_fs_state(ctx
, state
);
214 case CSO_VERTEX_SHADER
:
215 return delete_vs_state(ctx
, state
);
218 return delete_vertex_elements(ctx
, state
);
227 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
228 int max_size
, void *user_data
)
230 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
231 /* if we're approach the maximum size, remove fourth of the entries
232 * otherwise every subsequent call will go through the same */
233 int hash_size
= cso_hash_size(hash
);
234 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
235 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
236 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
237 if (hash_size
> max_size
)
238 to_remove
+= hash_size
- max_size
;
240 /*remove elements until we're good */
241 /*fixme: currently we pick the nodes to remove at random*/
242 void *cso
= cso_hash_iter_data(iter
);
243 if (delete_cso(ctx
, cso
, type
)) {
244 iter
= cso_hash_erase(hash
, iter
);
247 iter
= cso_hash_iter_next(iter
);
252 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
254 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
258 ctx
->cache
= cso_cache_create();
259 if (ctx
->cache
== NULL
)
261 cso_cache_set_sanitize_callback(ctx
->cache
,
267 /* Enable for testing: */
268 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
273 cso_destroy_context( ctx
);
279 * Prior to context destruction, this function unbinds all state objects.
281 void cso_release_all( struct cso_context
*ctx
)
286 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
287 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
288 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
289 if (ctx
->pipe
->bind_vertex_sampler_states
)
290 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
291 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
292 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
293 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
294 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
297 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
298 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
299 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
], NULL
);
302 for (i
= 0; i
< PIPE_MAX_VERTEX_SAMPLERS
; i
++) {
303 pipe_sampler_view_reference(&ctx
->vertex_sampler_views
[i
], NULL
);
304 pipe_sampler_view_reference(&ctx
->vertex_sampler_views_saved
[i
], NULL
);
307 free_framebuffer_state(&ctx
->fb
);
308 free_framebuffer_state(&ctx
->fb_saved
);
311 cso_cache_delete( ctx
->cache
);
317 void cso_destroy_context( struct cso_context
*ctx
)
320 /*cso_release_all( ctx );*/
326 /* Those function will either find the state of the given template
327 * in the cache or they will create a new state from the given
328 * template, insert it in the cache and return it.
332 * If the driver returns 0 from the create method then they will assign
333 * the data member of the cso to be the template itself.
336 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
337 const struct pipe_blend_state
*templ
)
339 unsigned key_size
, hash_key
;
340 struct cso_hash_iter iter
;
343 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
344 (char *)&(templ
->rt
[1]) - (char *)templ
;
345 hash_key
= cso_construct_key((void*)templ
, key_size
);
346 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
348 if (cso_hash_iter_is_null(iter
)) {
349 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
351 return PIPE_ERROR_OUT_OF_MEMORY
;
353 memcpy(&cso
->state
, templ
, key_size
);
354 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
355 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
356 cso
->context
= ctx
->pipe
;
358 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
359 if (cso_hash_iter_is_null(iter
)) {
361 return PIPE_ERROR_OUT_OF_MEMORY
;
367 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
370 if (ctx
->blend
!= handle
) {
372 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
377 void cso_save_blend(struct cso_context
*ctx
)
379 assert(!ctx
->blend_saved
);
380 ctx
->blend_saved
= ctx
->blend
;
383 void cso_restore_blend(struct cso_context
*ctx
)
385 if (ctx
->blend
!= ctx
->blend_saved
) {
386 ctx
->blend
= ctx
->blend_saved
;
387 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
389 ctx
->blend_saved
= NULL
;
394 enum pipe_error
cso_single_sampler(struct cso_context
*ctx
,
396 const struct pipe_sampler_state
*templ
)
401 unsigned key_size
= sizeof(struct pipe_sampler_state
);
402 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
403 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
404 hash_key
, CSO_SAMPLER
,
405 (void*)templ
, key_size
);
407 if (cso_hash_iter_is_null(iter
)) {
408 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
410 return PIPE_ERROR_OUT_OF_MEMORY
;
412 memcpy(&cso
->state
, templ
, sizeof(*templ
));
413 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
414 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
415 cso
->context
= ctx
->pipe
;
417 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
418 if (cso_hash_iter_is_null(iter
)) {
420 return PIPE_ERROR_OUT_OF_MEMORY
;
426 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
430 ctx
->samplers
[idx
] = handle
;
435 cso_single_vertex_sampler(struct cso_context
*ctx
,
437 const struct pipe_sampler_state
*templ
)
442 unsigned key_size
= sizeof(struct pipe_sampler_state
);
443 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
444 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
445 hash_key
, CSO_SAMPLER
,
446 (void*)templ
, key_size
);
448 if (cso_hash_iter_is_null(iter
)) {
449 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
451 return PIPE_ERROR_OUT_OF_MEMORY
;
453 memcpy(&cso
->state
, templ
, sizeof(*templ
));
454 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
455 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
456 cso
->context
= ctx
->pipe
;
458 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
459 if (cso_hash_iter_is_null(iter
)) {
461 return PIPE_ERROR_OUT_OF_MEMORY
;
467 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
471 ctx
->vertex_samplers
[idx
] = handle
;
475 void cso_single_sampler_done( struct cso_context
*ctx
)
479 /* find highest non-null sampler */
480 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
481 if (ctx
->samplers
[i
- 1] != NULL
)
485 ctx
->nr_samplers
= i
;
487 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
488 memcmp(ctx
->hw
.samplers
,
490 ctx
->nr_samplers
* sizeof(void *)) != 0)
492 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
493 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
495 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
500 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
504 /* find highest non-null sampler */
505 for (i
= PIPE_MAX_VERTEX_SAMPLERS
; i
> 0; i
--) {
506 if (ctx
->vertex_samplers
[i
- 1] != NULL
)
510 ctx
->nr_vertex_samplers
= i
;
512 if (ctx
->hw
.nr_vertex_samplers
!= ctx
->nr_vertex_samplers
||
513 memcmp(ctx
->hw
.vertex_samplers
,
514 ctx
->vertex_samplers
,
515 ctx
->nr_vertex_samplers
* sizeof(void *)) != 0)
517 memcpy(ctx
->hw
.vertex_samplers
,
518 ctx
->vertex_samplers
,
519 ctx
->nr_vertex_samplers
* sizeof(void *));
520 ctx
->hw
.nr_vertex_samplers
= ctx
->nr_vertex_samplers
;
522 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
523 ctx
->nr_vertex_samplers
,
524 ctx
->vertex_samplers
);
529 * If the function encouters any errors it will return the
530 * last one. Done to always try to set as many samplers
533 enum pipe_error
cso_set_samplers( struct cso_context
*ctx
,
535 const struct pipe_sampler_state
**templates
)
538 enum pipe_error temp
, error
= PIPE_OK
;
543 for (i
= 0; i
< nr
; i
++) {
544 temp
= cso_single_sampler( ctx
, i
, templates
[i
] );
549 for ( ; i
< ctx
->nr_samplers
; i
++) {
550 temp
= cso_single_sampler( ctx
, i
, NULL
);
555 cso_single_sampler_done( ctx
);
560 void cso_save_samplers(struct cso_context
*ctx
)
562 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
563 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
566 void cso_restore_samplers(struct cso_context
*ctx
)
568 ctx
->nr_samplers
= ctx
->nr_samplers_saved
;
569 memcpy(ctx
->samplers
, ctx
->samplers_saved
, sizeof(ctx
->samplers
));
570 cso_single_sampler_done( ctx
);
574 * If the function encouters any errors it will return the
575 * last one. Done to always try to set as many samplers
578 enum pipe_error
cso_set_vertex_samplers(struct cso_context
*ctx
,
580 const struct pipe_sampler_state
**templates
)
583 enum pipe_error temp
, error
= PIPE_OK
;
588 for (i
= 0; i
< nr
; i
++) {
589 temp
= cso_single_vertex_sampler( ctx
, i
, templates
[i
] );
594 for ( ; i
< ctx
->nr_samplers
; i
++) {
595 temp
= cso_single_vertex_sampler( ctx
, i
, NULL
);
600 cso_single_vertex_sampler_done( ctx
);
606 cso_save_vertex_samplers(struct cso_context
*ctx
)
608 ctx
->nr_vertex_samplers_saved
= ctx
->nr_vertex_samplers
;
609 memcpy(ctx
->vertex_samplers_saved
, ctx
->vertex_samplers
, sizeof(ctx
->vertex_samplers
));
613 cso_restore_vertex_samplers(struct cso_context
*ctx
)
615 ctx
->nr_vertex_samplers
= ctx
->nr_vertex_samplers_saved
;
616 memcpy(ctx
->vertex_samplers
, ctx
->vertex_samplers_saved
, sizeof(ctx
->vertex_samplers
));
617 cso_single_vertex_sampler_done(ctx
);
621 enum pipe_error
cso_set_sampler_textures( struct cso_context
*ctx
,
623 struct pipe_texture
**textures
)
627 ctx
->nr_fragment_sampler_views
= count
;
629 for (i
= 0; i
< count
; i
++) {
630 struct pipe_sampler_view templ
, *view
;
632 u_sampler_view_default_template(&templ
,
634 textures
[i
]->format
);
636 view
= ctx
->pipe
->create_sampler_view(ctx
->pipe
,
640 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], view
);
642 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
643 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
646 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
648 ctx
->fragment_sampler_views
);
653 void cso_save_sampler_textures( struct cso_context
*ctx
)
657 ctx
->nr_fragment_sampler_views_saved
= ctx
->nr_fragment_sampler_views
;
658 for (i
= 0; i
< ctx
->nr_fragment_sampler_views
; i
++) {
659 assert(!ctx
->fragment_sampler_views_saved
[i
]);
661 pipe_sampler_view_reference(&ctx
->fragment_sampler_views_saved
[i
],
662 ctx
->fragment_sampler_views
[i
]);
666 void cso_restore_sampler_textures( struct cso_context
*ctx
)
670 ctx
->nr_fragment_sampler_views
= ctx
->nr_fragment_sampler_views_saved
;
672 for (i
= 0; i
< ctx
->nr_fragment_sampler_views
; i
++) {
673 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
674 ctx
->fragment_sampler_views
[i
] = ctx
->fragment_sampler_views_saved
[i
];
675 ctx
->fragment_sampler_views_saved
[i
] = NULL
;
677 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++) {
678 pipe_sampler_view_reference(&ctx
->fragment_sampler_views
[i
], NULL
);
681 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
,
682 ctx
->nr_fragment_sampler_views
,
683 ctx
->fragment_sampler_views
);
685 ctx
->nr_fragment_sampler_views_saved
= 0;
689 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
690 const struct pipe_depth_stencil_alpha_state
*templ
)
692 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
693 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
694 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
696 CSO_DEPTH_STENCIL_ALPHA
,
697 (void*)templ
, key_size
);
700 if (cso_hash_iter_is_null(iter
)) {
701 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
703 return PIPE_ERROR_OUT_OF_MEMORY
;
705 memcpy(&cso
->state
, templ
, sizeof(*templ
));
706 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
707 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
708 cso
->context
= ctx
->pipe
;
710 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
711 if (cso_hash_iter_is_null(iter
)) {
713 return PIPE_ERROR_OUT_OF_MEMORY
;
719 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
722 if (ctx
->depth_stencil
!= handle
) {
723 ctx
->depth_stencil
= handle
;
724 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
729 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
731 assert(!ctx
->depth_stencil_saved
);
732 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
735 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
737 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
738 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
739 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
741 ctx
->depth_stencil_saved
= NULL
;
746 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
747 const struct pipe_rasterizer_state
*templ
)
749 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
750 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
751 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
752 hash_key
, CSO_RASTERIZER
,
753 (void*)templ
, key_size
);
756 if (cso_hash_iter_is_null(iter
)) {
757 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
759 return PIPE_ERROR_OUT_OF_MEMORY
;
761 memcpy(&cso
->state
, templ
, sizeof(*templ
));
762 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
763 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
764 cso
->context
= ctx
->pipe
;
766 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
767 if (cso_hash_iter_is_null(iter
)) {
769 return PIPE_ERROR_OUT_OF_MEMORY
;
775 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
778 if (ctx
->rasterizer
!= handle
) {
779 ctx
->rasterizer
= handle
;
780 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
785 void cso_save_rasterizer(struct cso_context
*ctx
)
787 assert(!ctx
->rasterizer_saved
);
788 ctx
->rasterizer_saved
= ctx
->rasterizer
;
791 void cso_restore_rasterizer(struct cso_context
*ctx
)
793 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
794 ctx
->rasterizer
= ctx
->rasterizer_saved
;
795 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
797 ctx
->rasterizer_saved
= NULL
;
802 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
805 if (ctx
->fragment_shader
!= handle
) {
806 ctx
->fragment_shader
= handle
;
807 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
812 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
814 if (handle
== ctx
->fragment_shader
) {
815 /* unbind before deleting */
816 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
817 ctx
->fragment_shader
= NULL
;
819 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
822 /* Not really working:
825 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
826 const struct pipe_shader_state
*templ
)
828 const struct tgsi_token
*tokens
= templ
->tokens
;
829 unsigned num_tokens
= tgsi_num_tokens(tokens
);
830 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
831 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
832 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
836 sizeof(*templ
)); /* XXX correct? tokens_size? */
839 if (cso_hash_iter_is_null(iter
)) {
840 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
841 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
844 return PIPE_ERROR_OUT_OF_MEMORY
;
846 memcpy(cso_tokens
, tokens
, tokens_size
);
847 cso
->state
.tokens
= cso_tokens
;
848 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
849 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
850 cso
->context
= ctx
->pipe
;
852 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
853 if (cso_hash_iter_is_null(iter
)) {
855 return PIPE_ERROR_OUT_OF_MEMORY
;
861 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
864 return cso_set_fragment_shader_handle( ctx
, handle
);
868 void cso_save_fragment_shader(struct cso_context
*ctx
)
870 assert(!ctx
->fragment_shader_saved
);
871 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
874 void cso_restore_fragment_shader(struct cso_context
*ctx
)
876 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
877 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
878 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
880 ctx
->fragment_shader_saved
= NULL
;
884 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
887 if (ctx
->vertex_shader
!= handle
) {
888 ctx
->vertex_shader
= handle
;
889 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
894 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
896 if (handle
== ctx
->vertex_shader
) {
897 /* unbind before deleting */
898 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
899 ctx
->vertex_shader
= NULL
;
901 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
905 /* Not really working:
908 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
909 const struct pipe_shader_state
*templ
)
911 unsigned hash_key
= cso_construct_key((void*)templ
,
912 sizeof(struct pipe_shader_state
));
913 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
914 hash_key
, CSO_VERTEX_SHADER
,
919 if (cso_hash_iter_is_null(iter
)) {
920 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
923 return PIPE_ERROR_OUT_OF_MEMORY
;
925 memcpy(cso
->state
, templ
, sizeof(*templ
));
926 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
927 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
928 cso
->context
= ctx
->pipe
;
930 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
931 if (cso_hash_iter_is_null(iter
)) {
933 return PIPE_ERROR_OUT_OF_MEMORY
;
939 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
942 return cso_set_vertex_shader_handle( ctx
, handle
);
948 void cso_save_vertex_shader(struct cso_context
*ctx
)
950 assert(!ctx
->vertex_shader_saved
);
951 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
954 void cso_restore_vertex_shader(struct cso_context
*ctx
)
956 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
957 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
958 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
960 ctx
->vertex_shader_saved
= NULL
;
965 * Copy framebuffer state from src to dst with refcounting of surfaces.
968 copy_framebuffer_state(struct pipe_framebuffer_state
*dst
,
969 const struct pipe_framebuffer_state
*src
)
973 dst
->width
= src
->width
;
974 dst
->height
= src
->height
;
975 dst
->nr_cbufs
= src
->nr_cbufs
;
976 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
977 pipe_surface_reference(&dst
->cbufs
[i
], src
->cbufs
[i
]);
979 pipe_surface_reference(&dst
->zsbuf
, src
->zsbuf
);
984 free_framebuffer_state(struct pipe_framebuffer_state
*fb
)
988 for (i
= 0; i
< PIPE_MAX_COLOR_BUFS
; i
++) {
989 pipe_surface_reference(&fb
->cbufs
[i
], NULL
);
991 pipe_surface_reference(&fb
->zsbuf
, NULL
);
995 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
996 const struct pipe_framebuffer_state
*fb
)
998 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
999 copy_framebuffer_state(&ctx
->fb
, fb
);
1000 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
1005 void cso_save_framebuffer(struct cso_context
*ctx
)
1007 copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
1010 void cso_restore_framebuffer(struct cso_context
*ctx
)
1012 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
1013 copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
1014 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
1015 free_framebuffer_state(&ctx
->fb_saved
);
1020 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
1021 const struct pipe_viewport_state
*vp
)
1023 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
1025 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
1030 void cso_save_viewport(struct cso_context
*ctx
)
1032 ctx
->vp_saved
= ctx
->vp
;
1036 void cso_restore_viewport(struct cso_context
*ctx
)
1038 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
1039 ctx
->vp
= ctx
->vp_saved
;
1040 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
1045 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
1046 const struct pipe_blend_color
*bc
)
1048 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
1049 ctx
->blend_color
= *bc
;
1050 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
1055 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
1056 const struct pipe_stencil_ref
*sr
)
1058 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
1059 ctx
->stencil_ref
= *sr
;
1060 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
1065 void cso_save_stencil_ref(struct cso_context
*ctx
)
1067 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
1071 void cso_restore_stencil_ref(struct cso_context
*ctx
)
1073 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
1074 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
1075 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
1079 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
1082 if (ctx
->geometry_shader
!= handle
) {
1083 ctx
->geometry_shader
= handle
;
1084 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
1089 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
1091 if (handle
== ctx
->geometry_shader
) {
1092 /* unbind before deleting */
1093 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
1094 ctx
->geometry_shader
= NULL
;
1096 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
1099 void cso_save_geometry_shader(struct cso_context
*ctx
)
1101 assert(!ctx
->geometry_shader_saved
);
1102 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
1105 void cso_restore_geometry_shader(struct cso_context
*ctx
)
1107 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
1108 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
1109 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
1111 ctx
->geometry_shader_saved
= NULL
;
1117 clip_state_cpy(struct pipe_clip_state
*dst
,
1118 const struct pipe_clip_state
*src
)
1122 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
1127 clip_state_cmp(const struct pipe_clip_state
*a
,
1128 const struct pipe_clip_state
*b
)
1130 if (a
->nr
!= b
->nr
) {
1134 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
1140 cso_set_clip(struct cso_context
*ctx
,
1141 const struct pipe_clip_state
*clip
)
1143 if (clip_state_cmp(&ctx
->clip
, clip
)) {
1144 clip_state_cpy(&ctx
->clip
, clip
);
1145 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
1150 cso_save_clip(struct cso_context
*ctx
)
1152 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
1156 cso_restore_clip(struct cso_context
*ctx
)
1158 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
1159 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
1160 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
1164 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
1166 const struct pipe_vertex_element
*states
)
1168 unsigned key_size
, hash_key
;
1169 struct cso_hash_iter iter
;
1171 struct cso_velems_state velems_state
;
1173 /* need to include the count into the stored state data too.
1174 Otherwise first few count pipe_vertex_elements could be identical even if count
1175 is different, and there's no guarantee the hash would be different in that
1177 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1178 velems_state
.count
= count
;
1179 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
1180 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1181 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
1183 if (cso_hash_iter_is_null(iter
)) {
1184 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1186 return PIPE_ERROR_OUT_OF_MEMORY
;
1188 memcpy(&cso
->state
, &velems_state
, key_size
);
1189 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
1190 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
1191 cso
->context
= ctx
->pipe
;
1193 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1194 if (cso_hash_iter_is_null(iter
)) {
1196 return PIPE_ERROR_OUT_OF_MEMORY
;
1202 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1205 if (ctx
->velements
!= handle
) {
1206 ctx
->velements
= handle
;
1207 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1212 void cso_save_vertex_elements(struct cso_context
*ctx
)
1214 assert(!ctx
->velements_saved
);
1215 ctx
->velements_saved
= ctx
->velements
;
1218 void cso_restore_vertex_elements(struct cso_context
*ctx
)
1220 if (ctx
->velements
!= ctx
->velements_saved
) {
1221 ctx
->velements
= ctx
->velements_saved
;
1222 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1224 ctx
->velements_saved
= NULL
;
1227 /* fragment sampler view state */
1230 cso_set_fragment_sampler_views(struct cso_context
*cso
,
1232 struct pipe_sampler_view
**views
)
1236 for (i
= 0; i
< count
; i
++) {
1237 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], views
[i
]);
1239 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1240 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1243 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1244 MAX2(count
, cso
->nr_fragment_sampler_views
),
1245 cso
->fragment_sampler_views
);
1247 cso
->nr_fragment_sampler_views
= count
;
1251 cso_save_fragment_sampler_views(struct cso_context
*cso
)
1255 cso
->nr_fragment_sampler_views_saved
= cso
->nr_fragment_sampler_views
;
1257 for (i
= 0; i
< cso
->nr_fragment_sampler_views
; i
++) {
1258 assert(!cso
->fragment_sampler_views_saved
[i
]);
1260 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
],
1261 cso
->fragment_sampler_views
[i
]);
1266 cso_restore_fragment_sampler_views(struct cso_context
*cso
)
1270 for (i
= 0; i
< cso
->nr_fragment_sampler_views_saved
; i
++) {
1271 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], cso
->fragment_sampler_views_saved
[i
]);
1272 pipe_sampler_view_reference(&cso
->fragment_sampler_views_saved
[i
], NULL
);
1274 for (; i
< cso
->nr_fragment_sampler_views
; i
++) {
1275 pipe_sampler_view_reference(&cso
->fragment_sampler_views
[i
], NULL
);
1278 cso
->pipe
->set_fragment_sampler_views(cso
->pipe
,
1279 MAX2(cso
->nr_fragment_sampler_views
, cso
->nr_fragment_sampler_views_saved
),
1280 cso
->fragment_sampler_views
);
1282 cso
->nr_fragment_sampler_views
= cso
->nr_fragment_sampler_views_saved
;
1283 cso
->nr_fragment_sampler_views_saved
= 0;
1287 /* vertex sampler view state */
1290 cso_set_vertex_sampler_views(struct cso_context
*cso
,
1292 struct pipe_sampler_view
**views
)
1296 for (i
= 0; i
< count
; i
++) {
1297 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], views
[i
]);
1299 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1300 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1303 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1304 MAX2(count
, cso
->nr_vertex_sampler_views
),
1305 cso
->vertex_sampler_views
);
1307 cso
->nr_vertex_sampler_views
= count
;
1311 cso_save_vertex_sampler_views(struct cso_context
*cso
)
1315 cso
->nr_vertex_sampler_views_saved
= cso
->nr_vertex_sampler_views
;
1317 for (i
= 0; i
< cso
->nr_vertex_sampler_views
; i
++) {
1318 assert(!cso
->vertex_sampler_views_saved
[i
]);
1320 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
],
1321 cso
->vertex_sampler_views
[i
]);
1326 cso_restore_vertex_sampler_views(struct cso_context
*cso
)
1330 for (i
= 0; i
< cso
->nr_vertex_sampler_views_saved
; i
++) {
1331 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], cso
->vertex_sampler_views_saved
[i
]);
1332 pipe_sampler_view_reference(&cso
->vertex_sampler_views_saved
[i
], NULL
);
1334 for (; i
< cso
->nr_vertex_sampler_views
; i
++) {
1335 pipe_sampler_view_reference(&cso
->vertex_sampler_views
[i
], NULL
);
1338 cso
->pipe
->set_vertex_sampler_views(cso
->pipe
,
1339 MAX2(cso
->nr_vertex_sampler_views
, cso
->nr_vertex_sampler_views_saved
),
1340 cso
->vertex_sampler_views
);
1342 cso
->nr_vertex_sampler_views
= cso
->nr_vertex_sampler_views_saved
;
1343 cso
->nr_vertex_sampler_views_saved
= 0;