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_framebuffer.h"
40 #include "util/u_inlines.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.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"
52 * Info related to samplers and sampler views.
53 * We have one of these for fragment samplers and another for vertex samplers.
58 void *samplers
[PIPE_MAX_SAMPLERS
];
62 void *samplers
[PIPE_MAX_SAMPLERS
];
65 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
66 unsigned nr_samplers_saved
;
68 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
71 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
72 unsigned nr_views_saved
;
78 struct pipe_context
*pipe
;
79 struct cso_cache
*cache
;
81 boolean has_geometry_shader
;
82 boolean has_streamout
;
84 struct sampler_info fragment_samplers
;
85 struct sampler_info vertex_samplers
;
87 uint nr_vertex_buffers
;
88 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
90 uint nr_vertex_buffers_saved
;
91 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
93 unsigned nr_so_targets
;
94 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
96 unsigned nr_so_targets_saved
;
97 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
99 /** Current and saved state.
100 * The saved state is used as a 1-deep stack.
102 void *blend
, *blend_saved
;
103 void *depth_stencil
, *depth_stencil_saved
;
104 void *rasterizer
, *rasterizer_saved
;
105 void *fragment_shader
, *fragment_shader_saved
, *geometry_shader
;
106 void *vertex_shader
, *vertex_shader_saved
, *geometry_shader_saved
;
107 void *velements
, *velements_saved
;
109 struct pipe_clip_state clip
;
110 struct pipe_clip_state clip_saved
;
112 struct pipe_framebuffer_state fb
, fb_saved
;
113 struct pipe_viewport_state vp
, vp_saved
;
114 struct pipe_blend_color blend_color
;
115 unsigned sample_mask
;
116 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
120 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
122 struct cso_blend
*cso
= (struct cso_blend
*)state
;
124 if (ctx
->blend
== cso
->data
)
127 if (cso
->delete_state
)
128 cso
->delete_state(cso
->context
, cso
->data
);
133 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
135 struct cso_depth_stencil_alpha
*cso
= (struct cso_depth_stencil_alpha
*)state
;
137 if (ctx
->depth_stencil
== cso
->data
)
140 if (cso
->delete_state
)
141 cso
->delete_state(cso
->context
, cso
->data
);
147 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
149 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
150 if (cso
->delete_state
)
151 cso
->delete_state(cso
->context
, cso
->data
);
156 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
158 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
160 if (ctx
->rasterizer
== cso
->data
)
162 if (cso
->delete_state
)
163 cso
->delete_state(cso
->context
, cso
->data
);
168 static boolean
delete_fs_state(struct cso_context
*ctx
, void *state
)
170 struct cso_fragment_shader
*cso
= (struct cso_fragment_shader
*)state
;
171 if (ctx
->fragment_shader
== cso
->data
)
173 if (cso
->delete_state
)
174 cso
->delete_state(cso
->context
, cso
->data
);
179 static boolean
delete_vs_state(struct cso_context
*ctx
, void *state
)
181 struct cso_vertex_shader
*cso
= (struct cso_vertex_shader
*)state
;
182 if (ctx
->vertex_shader
== cso
->data
)
184 if (cso
->delete_state
)
185 cso
->delete_state(cso
->context
, cso
->data
);
190 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
193 struct cso_velements
*cso
= (struct cso_velements
*)state
;
195 if (ctx
->velements
== cso
->data
)
198 if (cso
->delete_state
)
199 cso
->delete_state(cso
->context
, cso
->data
);
205 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
206 void *state
, enum cso_cache_type type
)
210 return delete_blend_state(ctx
, state
);
213 return delete_sampler_state(ctx
, state
);
215 case CSO_DEPTH_STENCIL_ALPHA
:
216 return delete_depth_stencil_state(ctx
, state
);
219 return delete_rasterizer_state(ctx
, state
);
221 case CSO_FRAGMENT_SHADER
:
222 return delete_fs_state(ctx
, state
);
224 case CSO_VERTEX_SHADER
:
225 return delete_vs_state(ctx
, state
);
228 return delete_vertex_elements(ctx
, state
);
237 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
238 int max_size
, void *user_data
)
240 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
241 /* if we're approach the maximum size, remove fourth of the entries
242 * otherwise every subsequent call will go through the same */
243 int hash_size
= cso_hash_size(hash
);
244 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
245 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
246 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
247 if (hash_size
> max_size
)
248 to_remove
+= hash_size
- max_size
;
250 /*remove elements until we're good */
251 /*fixme: currently we pick the nodes to remove at random*/
252 void *cso
= cso_hash_iter_data(iter
);
253 if (delete_cso(ctx
, cso
, type
)) {
254 iter
= cso_hash_erase(hash
, iter
);
257 iter
= cso_hash_iter_next(iter
);
262 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
264 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
268 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
270 ctx
->cache
= cso_cache_create();
271 if (ctx
->cache
== NULL
)
273 cso_cache_set_sanitize_callback(ctx
->cache
,
279 /* Enable for testing: */
280 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
282 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
283 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
284 ctx
->has_geometry_shader
= TRUE
;
286 if (pipe
->screen
->get_param(pipe
->screen
,
287 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
288 ctx
->has_streamout
= TRUE
;
294 cso_destroy_context( ctx
);
300 * Prior to context destruction, this function unbinds all state objects.
302 void cso_release_all( struct cso_context
*ctx
)
305 struct sampler_info
*info
;
308 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
309 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
310 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
311 if (ctx
->pipe
->bind_vertex_sampler_states
)
312 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
313 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
314 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
315 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
316 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
317 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
318 if (ctx
->pipe
->set_vertex_sampler_views
)
319 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
320 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
323 /* free fragment samplers, views */
324 info
= &ctx
->fragment_samplers
;
325 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
326 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
327 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
330 /* free vertex samplers, views */
331 info
= &ctx
->vertex_samplers
;
332 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
333 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
334 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
337 util_unreference_framebuffer_state(&ctx
->fb
);
338 util_unreference_framebuffer_state(&ctx
->fb_saved
);
340 util_copy_vertex_buffers(ctx
->vertex_buffers
,
341 &ctx
->nr_vertex_buffers
,
343 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
344 &ctx
->nr_vertex_buffers_saved
,
347 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
348 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
349 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
353 cso_cache_delete( ctx
->cache
);
360 * Free the CSO context. NOTE: the state tracker should have previously called
363 void cso_destroy_context( struct cso_context
*ctx
)
371 /* Those function will either find the state of the given template
372 * in the cache or they will create a new state from the given
373 * template, insert it in the cache and return it.
377 * If the driver returns 0 from the create method then they will assign
378 * the data member of the cso to be the template itself.
381 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
382 const struct pipe_blend_state
*templ
)
384 unsigned key_size
, hash_key
;
385 struct cso_hash_iter iter
;
388 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
389 (char *)&(templ
->rt
[1]) - (char *)templ
;
390 hash_key
= cso_construct_key((void*)templ
, key_size
);
391 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
393 if (cso_hash_iter_is_null(iter
)) {
394 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
396 return PIPE_ERROR_OUT_OF_MEMORY
;
398 memset(&cso
->state
, 0, sizeof cso
->state
);
399 memcpy(&cso
->state
, templ
, key_size
);
400 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
401 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
402 cso
->context
= ctx
->pipe
;
404 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
405 if (cso_hash_iter_is_null(iter
)) {
407 return PIPE_ERROR_OUT_OF_MEMORY
;
413 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
416 if (ctx
->blend
!= handle
) {
418 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
423 void cso_save_blend(struct cso_context
*ctx
)
425 assert(!ctx
->blend_saved
);
426 ctx
->blend_saved
= ctx
->blend
;
429 void cso_restore_blend(struct cso_context
*ctx
)
431 if (ctx
->blend
!= ctx
->blend_saved
) {
432 ctx
->blend
= ctx
->blend_saved
;
433 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
435 ctx
->blend_saved
= NULL
;
440 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
441 const struct pipe_depth_stencil_alpha_state
*templ
)
443 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
444 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
445 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
447 CSO_DEPTH_STENCIL_ALPHA
,
448 (void*)templ
, key_size
);
451 if (cso_hash_iter_is_null(iter
)) {
452 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
454 return PIPE_ERROR_OUT_OF_MEMORY
;
456 memcpy(&cso
->state
, templ
, sizeof(*templ
));
457 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
458 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
459 cso
->context
= ctx
->pipe
;
461 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
462 if (cso_hash_iter_is_null(iter
)) {
464 return PIPE_ERROR_OUT_OF_MEMORY
;
470 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
473 if (ctx
->depth_stencil
!= handle
) {
474 ctx
->depth_stencil
= handle
;
475 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
480 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
482 assert(!ctx
->depth_stencil_saved
);
483 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
486 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
488 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
489 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
490 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
492 ctx
->depth_stencil_saved
= NULL
;
497 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
498 const struct pipe_rasterizer_state
*templ
)
500 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
501 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
502 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
503 hash_key
, CSO_RASTERIZER
,
504 (void*)templ
, key_size
);
507 if (cso_hash_iter_is_null(iter
)) {
508 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
510 return PIPE_ERROR_OUT_OF_MEMORY
;
512 memcpy(&cso
->state
, templ
, sizeof(*templ
));
513 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
514 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
515 cso
->context
= ctx
->pipe
;
517 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
518 if (cso_hash_iter_is_null(iter
)) {
520 return PIPE_ERROR_OUT_OF_MEMORY
;
526 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
529 if (ctx
->rasterizer
!= handle
) {
530 ctx
->rasterizer
= handle
;
531 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
536 void cso_save_rasterizer(struct cso_context
*ctx
)
538 assert(!ctx
->rasterizer_saved
);
539 ctx
->rasterizer_saved
= ctx
->rasterizer
;
542 void cso_restore_rasterizer(struct cso_context
*ctx
)
544 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
545 ctx
->rasterizer
= ctx
->rasterizer_saved
;
546 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
548 ctx
->rasterizer_saved
= NULL
;
553 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
556 if (ctx
->fragment_shader
!= handle
) {
557 ctx
->fragment_shader
= handle
;
558 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
563 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
565 if (handle
== ctx
->fragment_shader
) {
566 /* unbind before deleting */
567 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
568 ctx
->fragment_shader
= NULL
;
570 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
573 /* Not really working:
576 enum pipe_error
cso_set_fragment_shader(struct cso_context
*ctx
,
577 const struct pipe_shader_state
*templ
)
579 const struct tgsi_token
*tokens
= templ
->tokens
;
580 unsigned num_tokens
= tgsi_num_tokens(tokens
);
581 size_t tokens_size
= num_tokens
*sizeof(struct tgsi_token
);
582 unsigned hash_key
= cso_construct_key((void*)tokens
, tokens_size
);
583 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
587 sizeof(*templ
)); /* XXX correct? tokens_size? */
590 if (cso_hash_iter_is_null(iter
)) {
591 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
) + tokens_size
);
592 struct tgsi_token
*cso_tokens
= (struct tgsi_token
*)((char *)cso
+ sizeof(*cso
));
595 return PIPE_ERROR_OUT_OF_MEMORY
;
597 memcpy(cso_tokens
, tokens
, tokens_size
);
598 cso
->state
.tokens
= cso_tokens
;
599 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
600 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
601 cso
->context
= ctx
->pipe
;
603 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
604 if (cso_hash_iter_is_null(iter
)) {
606 return PIPE_ERROR_OUT_OF_MEMORY
;
612 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
615 return cso_set_fragment_shader_handle( ctx
, handle
);
619 void cso_save_fragment_shader(struct cso_context
*ctx
)
621 assert(!ctx
->fragment_shader_saved
);
622 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
625 void cso_restore_fragment_shader(struct cso_context
*ctx
)
627 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
628 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
629 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
631 ctx
->fragment_shader_saved
= NULL
;
635 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
638 if (ctx
->vertex_shader
!= handle
) {
639 ctx
->vertex_shader
= handle
;
640 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
645 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
647 if (handle
== ctx
->vertex_shader
) {
648 /* unbind before deleting */
649 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
650 ctx
->vertex_shader
= NULL
;
652 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
656 /* Not really working:
659 enum pipe_error
cso_set_vertex_shader(struct cso_context
*ctx
,
660 const struct pipe_shader_state
*templ
)
662 unsigned hash_key
= cso_construct_key((void*)templ
,
663 sizeof(struct pipe_shader_state
));
664 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
665 hash_key
, CSO_VERTEX_SHADER
,
670 if (cso_hash_iter_is_null(iter
)) {
671 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
674 return PIPE_ERROR_OUT_OF_MEMORY
;
676 memcpy(cso
->state
, templ
, sizeof(*templ
));
677 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
678 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
679 cso
->context
= ctx
->pipe
;
681 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
682 if (cso_hash_iter_is_null(iter
)) {
684 return PIPE_ERROR_OUT_OF_MEMORY
;
690 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
693 return cso_set_vertex_shader_handle( ctx
, handle
);
699 void cso_save_vertex_shader(struct cso_context
*ctx
)
701 assert(!ctx
->vertex_shader_saved
);
702 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
705 void cso_restore_vertex_shader(struct cso_context
*ctx
)
707 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
708 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
709 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
711 ctx
->vertex_shader_saved
= NULL
;
715 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
716 const struct pipe_framebuffer_state
*fb
)
718 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
719 util_copy_framebuffer_state(&ctx
->fb
, fb
);
720 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
725 void cso_save_framebuffer(struct cso_context
*ctx
)
727 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
730 void cso_restore_framebuffer(struct cso_context
*ctx
)
732 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
733 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
734 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
735 util_unreference_framebuffer_state(&ctx
->fb_saved
);
740 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
741 const struct pipe_viewport_state
*vp
)
743 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
745 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
750 void cso_save_viewport(struct cso_context
*ctx
)
752 ctx
->vp_saved
= ctx
->vp
;
756 void cso_restore_viewport(struct cso_context
*ctx
)
758 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
759 ctx
->vp
= ctx
->vp_saved
;
760 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
765 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
766 const struct pipe_blend_color
*bc
)
768 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
769 ctx
->blend_color
= *bc
;
770 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
775 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
776 unsigned sample_mask
)
778 if (ctx
->sample_mask
!= sample_mask
) {
779 ctx
->sample_mask
= sample_mask
;
780 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
785 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
786 const struct pipe_stencil_ref
*sr
)
788 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
789 ctx
->stencil_ref
= *sr
;
790 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
795 void cso_save_stencil_ref(struct cso_context
*ctx
)
797 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
801 void cso_restore_stencil_ref(struct cso_context
*ctx
)
803 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
804 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
805 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
809 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
812 assert(ctx
->has_geometry_shader
|| !handle
);
814 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
815 ctx
->geometry_shader
= handle
;
816 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
821 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
823 if (handle
== ctx
->geometry_shader
) {
824 /* unbind before deleting */
825 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
826 ctx
->geometry_shader
= NULL
;
828 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
831 void cso_save_geometry_shader(struct cso_context
*ctx
)
833 if (!ctx
->has_geometry_shader
) {
837 assert(!ctx
->geometry_shader_saved
);
838 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
841 void cso_restore_geometry_shader(struct cso_context
*ctx
)
843 if (!ctx
->has_geometry_shader
) {
847 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
848 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
849 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
851 ctx
->geometry_shader_saved
= NULL
;
857 clip_state_cpy(struct pipe_clip_state
*dst
,
858 const struct pipe_clip_state
*src
)
860 dst
->depth_clamp
= src
->depth_clamp
;
863 memcpy(dst
->ucp
, src
->ucp
, src
->nr
* sizeof(src
->ucp
[0]));
868 clip_state_cmp(const struct pipe_clip_state
*a
,
869 const struct pipe_clip_state
*b
)
871 if (a
->depth_clamp
!= b
->depth_clamp
) {
874 if (a
->nr
!= b
->nr
) {
878 return memcmp(a
->ucp
, b
->ucp
, a
->nr
* sizeof(a
->ucp
[0]));
884 cso_set_clip(struct cso_context
*ctx
,
885 const struct pipe_clip_state
*clip
)
887 if (clip_state_cmp(&ctx
->clip
, clip
)) {
888 clip_state_cpy(&ctx
->clip
, clip
);
889 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
894 cso_save_clip(struct cso_context
*ctx
)
896 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
900 cso_restore_clip(struct cso_context
*ctx
)
902 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
903 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
904 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
908 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
910 const struct pipe_vertex_element
*states
)
912 unsigned key_size
, hash_key
;
913 struct cso_hash_iter iter
;
915 struct cso_velems_state velems_state
;
917 /* need to include the count into the stored state data too.
918 Otherwise first few count pipe_vertex_elements could be identical even if count
919 is different, and there's no guarantee the hash would be different in that
921 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
922 velems_state
.count
= count
;
923 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
924 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
925 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
927 if (cso_hash_iter_is_null(iter
)) {
928 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
930 return PIPE_ERROR_OUT_OF_MEMORY
;
932 memcpy(&cso
->state
, &velems_state
, key_size
);
933 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
934 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
935 cso
->context
= ctx
->pipe
;
937 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
938 if (cso_hash_iter_is_null(iter
)) {
940 return PIPE_ERROR_OUT_OF_MEMORY
;
946 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
949 if (ctx
->velements
!= handle
) {
950 ctx
->velements
= handle
;
951 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
956 void cso_save_vertex_elements(struct cso_context
*ctx
)
958 assert(!ctx
->velements_saved
);
959 ctx
->velements_saved
= ctx
->velements
;
962 void cso_restore_vertex_elements(struct cso_context
*ctx
)
964 if (ctx
->velements
!= ctx
->velements_saved
) {
965 ctx
->velements
= ctx
->velements_saved
;
966 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
968 ctx
->velements_saved
= NULL
;
973 void cso_set_vertex_buffers(struct cso_context
*ctx
,
975 const struct pipe_vertex_buffer
*buffers
)
977 if (count
!= ctx
->nr_vertex_buffers
||
978 memcmp(buffers
, ctx
->vertex_buffers
,
979 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
980 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
982 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
986 void cso_save_vertex_buffers(struct cso_context
*ctx
)
988 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
989 &ctx
->nr_vertex_buffers_saved
,
991 ctx
->nr_vertex_buffers
);
994 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
996 util_copy_vertex_buffers(ctx
->vertex_buffers
,
997 &ctx
->nr_vertex_buffers
,
998 ctx
->vertex_buffers_saved
,
999 ctx
->nr_vertex_buffers_saved
);
1000 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
1001 ctx
->vertex_buffers
);
1005 /**************** fragment/vertex sampler view state *************************/
1007 static enum pipe_error
1008 single_sampler(struct cso_context
*ctx
,
1009 struct sampler_info
*info
,
1011 const struct pipe_sampler_state
*templ
)
1013 void *handle
= NULL
;
1015 if (templ
!= NULL
) {
1016 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1017 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1018 struct cso_hash_iter iter
=
1019 cso_find_state_template(ctx
->cache
,
1020 hash_key
, CSO_SAMPLER
,
1021 (void *) templ
, key_size
);
1023 if (cso_hash_iter_is_null(iter
)) {
1024 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1026 return PIPE_ERROR_OUT_OF_MEMORY
;
1028 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1029 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1030 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
1031 cso
->context
= ctx
->pipe
;
1033 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1034 if (cso_hash_iter_is_null(iter
)) {
1036 return PIPE_ERROR_OUT_OF_MEMORY
;
1042 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1046 info
->samplers
[idx
] = handle
;
1052 cso_single_sampler(struct cso_context
*ctx
,
1054 const struct pipe_sampler_state
*templ
)
1056 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
1060 cso_single_vertex_sampler(struct cso_context
*ctx
,
1062 const struct pipe_sampler_state
*templ
)
1064 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
1070 single_sampler_done(struct cso_context
*ctx
,
1071 struct sampler_info
*info
)
1075 /* find highest non-null sampler */
1076 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1077 if (info
->samplers
[i
- 1] != NULL
)
1081 info
->nr_samplers
= i
;
1083 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1084 memcmp(info
->hw
.samplers
,
1086 info
->nr_samplers
* sizeof(void *)) != 0)
1088 memcpy(info
->hw
.samplers
,
1090 info
->nr_samplers
* sizeof(void *));
1091 info
->hw
.nr_samplers
= info
->nr_samplers
;
1093 if (info
== &ctx
->fragment_samplers
) {
1094 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1098 else if (info
== &ctx
->vertex_samplers
) {
1099 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1110 cso_single_sampler_done( struct cso_context
*ctx
)
1112 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
1116 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
1118 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1123 * If the function encouters any errors it will return the
1124 * last one. Done to always try to set as many samplers
1127 static enum pipe_error
1128 set_samplers(struct cso_context
*ctx
,
1129 struct sampler_info
*info
,
1131 const struct pipe_sampler_state
**templates
)
1134 enum pipe_error temp
, error
= PIPE_OK
;
1139 for (i
= 0; i
< nr
; i
++) {
1140 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1141 if (temp
!= PIPE_OK
)
1145 for ( ; i
< info
->nr_samplers
; i
++) {
1146 temp
= single_sampler(ctx
, info
, i
, NULL
);
1147 if (temp
!= PIPE_OK
)
1151 single_sampler_done(ctx
, info
);
1157 cso_set_samplers(struct cso_context
*ctx
,
1159 const struct pipe_sampler_state
**templates
)
1161 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1165 cso_set_vertex_samplers(struct cso_context
*ctx
,
1167 const struct pipe_sampler_state
**templates
)
1169 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1175 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1177 info
->nr_samplers_saved
= info
->nr_samplers
;
1178 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1182 cso_save_samplers(struct cso_context
*ctx
)
1184 save_samplers(ctx
, &ctx
->fragment_samplers
);
1188 cso_save_vertex_samplers(struct cso_context
*ctx
)
1190 save_samplers(ctx
, &ctx
->vertex_samplers
);
1196 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1198 info
->nr_samplers
= info
->nr_samplers_saved
;
1199 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1200 single_sampler_done(ctx
, info
);
1204 cso_restore_samplers(struct cso_context
*ctx
)
1206 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1210 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1212 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1218 set_sampler_views(struct cso_context
*ctx
,
1219 struct sampler_info
*info
,
1220 void (*set_views
)(struct pipe_context
*,
1222 struct pipe_sampler_view
**),
1224 struct pipe_sampler_view
**views
)
1228 /* reference new views */
1229 for (i
= 0; i
< count
; i
++) {
1230 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1232 /* unref extra old views, if any */
1233 for (; i
< info
->nr_views
; i
++) {
1234 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1237 info
->nr_views
= count
;
1239 /* bind the new sampler views */
1240 set_views(ctx
->pipe
, count
, info
->views
);
1244 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1246 struct pipe_sampler_view
**views
)
1248 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1249 ctx
->pipe
->set_fragment_sampler_views
,
1254 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1256 struct pipe_sampler_view
**views
)
1258 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1259 ctx
->pipe
->set_vertex_sampler_views
,
1266 save_sampler_views(struct cso_context
*ctx
,
1267 struct sampler_info
*info
)
1271 info
->nr_views_saved
= info
->nr_views
;
1273 for (i
= 0; i
< info
->nr_views
; i
++) {
1274 assert(!info
->views_saved
[i
]);
1275 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1280 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1282 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1286 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1288 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1293 restore_sampler_views(struct cso_context
*ctx
,
1294 struct sampler_info
*info
,
1295 void (*set_views
)(struct pipe_context
*,
1297 struct pipe_sampler_view
**))
1301 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1302 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1303 /* move the reference from one pointer to another */
1304 info
->views
[i
] = info
->views_saved
[i
];
1305 info
->views_saved
[i
] = NULL
;
1307 for (; i
< info
->nr_views
; i
++) {
1308 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1311 /* bind the old/saved sampler views */
1312 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1314 info
->nr_views
= info
->nr_views_saved
;
1315 info
->nr_views_saved
= 0;
1319 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1321 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1322 ctx
->pipe
->set_fragment_sampler_views
);
1326 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1328 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1329 ctx
->pipe
->set_vertex_sampler_views
);
1334 cso_set_stream_outputs(struct cso_context
*ctx
,
1335 unsigned num_targets
,
1336 struct pipe_stream_output_target
**targets
,
1337 unsigned append_bitmask
)
1339 struct pipe_context
*pipe
= ctx
->pipe
;
1342 if (!ctx
->has_streamout
) {
1343 assert(num_targets
== 0);
1347 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1348 /* Nothing to do. */
1352 /* reference new targets */
1353 for (i
= 0; i
< num_targets
; i
++) {
1354 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1356 /* unref extra old targets, if any */
1357 for (; i
< ctx
->nr_so_targets
; i
++) {
1358 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1361 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1363 ctx
->nr_so_targets
= num_targets
;
1367 cso_save_stream_outputs(struct cso_context
*ctx
)
1371 if (!ctx
->has_streamout
) {
1375 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1377 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1378 assert(!ctx
->so_targets_saved
[i
]);
1379 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1384 cso_restore_stream_outputs(struct cso_context
*ctx
)
1386 struct pipe_context
*pipe
= ctx
->pipe
;
1389 if (!ctx
->has_streamout
) {
1393 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1394 /* Nothing to do. */
1398 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1399 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1400 /* move the reference from one pointer to another */
1401 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1402 ctx
->so_targets_saved
[i
] = NULL
;
1404 for (; i
< ctx
->nr_so_targets
; i
++) {
1405 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1408 /* ~0 means append */
1409 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1410 ctx
->so_targets
, ~0);
1412 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1413 ctx
->nr_so_targets_saved
= 0;