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_vertex_elements(struct cso_context
*ctx
,
171 struct cso_velements
*cso
= (struct cso_velements
*)state
;
173 if (ctx
->velements
== cso
->data
)
176 if (cso
->delete_state
)
177 cso
->delete_state(cso
->context
, cso
->data
);
183 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
184 void *state
, enum cso_cache_type type
)
188 return delete_blend_state(ctx
, state
);
191 return delete_sampler_state(ctx
, state
);
193 case CSO_DEPTH_STENCIL_ALPHA
:
194 return delete_depth_stencil_state(ctx
, state
);
197 return delete_rasterizer_state(ctx
, state
);
200 return delete_vertex_elements(ctx
, state
);
209 static INLINE
void sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
210 int max_size
, void *user_data
)
212 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
213 /* if we're approach the maximum size, remove fourth of the entries
214 * otherwise every subsequent call will go through the same */
215 int hash_size
= cso_hash_size(hash
);
216 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
217 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
218 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
219 if (hash_size
> max_size
)
220 to_remove
+= hash_size
- max_size
;
222 /*remove elements until we're good */
223 /*fixme: currently we pick the nodes to remove at random*/
224 void *cso
= cso_hash_iter_data(iter
);
225 if (delete_cso(ctx
, cso
, type
)) {
226 iter
= cso_hash_erase(hash
, iter
);
229 iter
= cso_hash_iter_next(iter
);
234 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
236 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
240 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
242 ctx
->cache
= cso_cache_create();
243 if (ctx
->cache
== NULL
)
245 cso_cache_set_sanitize_callback(ctx
->cache
,
251 /* Enable for testing: */
252 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
254 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
255 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
256 ctx
->has_geometry_shader
= TRUE
;
258 if (pipe
->screen
->get_param(pipe
->screen
,
259 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
260 ctx
->has_streamout
= TRUE
;
266 cso_destroy_context( ctx
);
272 * Prior to context destruction, this function unbinds all state objects.
274 void cso_release_all( struct cso_context
*ctx
)
277 struct sampler_info
*info
;
280 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
281 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
282 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
283 if (ctx
->pipe
->bind_vertex_sampler_states
)
284 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
285 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
286 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
287 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
288 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
289 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
290 if (ctx
->pipe
->set_vertex_sampler_views
)
291 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
292 if (ctx
->pipe
->set_stream_output_targets
)
293 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
296 /* free fragment samplers, views */
297 info
= &ctx
->fragment_samplers
;
298 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
299 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
300 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
303 /* free vertex samplers, views */
304 info
= &ctx
->vertex_samplers
;
305 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
306 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
307 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
310 util_unreference_framebuffer_state(&ctx
->fb
);
311 util_unreference_framebuffer_state(&ctx
->fb_saved
);
313 util_copy_vertex_buffers(ctx
->vertex_buffers
,
314 &ctx
->nr_vertex_buffers
,
316 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
317 &ctx
->nr_vertex_buffers_saved
,
320 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
321 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
322 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
326 cso_cache_delete( ctx
->cache
);
333 * Free the CSO context. NOTE: the state tracker should have previously called
336 void cso_destroy_context( struct cso_context
*ctx
)
344 /* Those function will either find the state of the given template
345 * in the cache or they will create a new state from the given
346 * template, insert it in the cache and return it.
350 * If the driver returns 0 from the create method then they will assign
351 * the data member of the cso to be the template itself.
354 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
355 const struct pipe_blend_state
*templ
)
357 unsigned key_size
, hash_key
;
358 struct cso_hash_iter iter
;
361 key_size
= templ
->independent_blend_enable
? sizeof(struct pipe_blend_state
) :
362 (char *)&(templ
->rt
[1]) - (char *)templ
;
363 hash_key
= cso_construct_key((void*)templ
, key_size
);
364 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
, (void*)templ
, key_size
);
366 if (cso_hash_iter_is_null(iter
)) {
367 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
369 return PIPE_ERROR_OUT_OF_MEMORY
;
371 memset(&cso
->state
, 0, sizeof cso
->state
);
372 memcpy(&cso
->state
, templ
, key_size
);
373 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
374 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
375 cso
->context
= ctx
->pipe
;
377 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
378 if (cso_hash_iter_is_null(iter
)) {
380 return PIPE_ERROR_OUT_OF_MEMORY
;
386 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
389 if (ctx
->blend
!= handle
) {
391 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
396 void cso_save_blend(struct cso_context
*ctx
)
398 assert(!ctx
->blend_saved
);
399 ctx
->blend_saved
= ctx
->blend
;
402 void cso_restore_blend(struct cso_context
*ctx
)
404 if (ctx
->blend
!= ctx
->blend_saved
) {
405 ctx
->blend
= ctx
->blend_saved
;
406 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
408 ctx
->blend_saved
= NULL
;
413 enum pipe_error
cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
414 const struct pipe_depth_stencil_alpha_state
*templ
)
416 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
417 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
418 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
420 CSO_DEPTH_STENCIL_ALPHA
,
421 (void*)templ
, key_size
);
424 if (cso_hash_iter_is_null(iter
)) {
425 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
427 return PIPE_ERROR_OUT_OF_MEMORY
;
429 memcpy(&cso
->state
, templ
, sizeof(*templ
));
430 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
431 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
432 cso
->context
= ctx
->pipe
;
434 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
435 if (cso_hash_iter_is_null(iter
)) {
437 return PIPE_ERROR_OUT_OF_MEMORY
;
443 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
446 if (ctx
->depth_stencil
!= handle
) {
447 ctx
->depth_stencil
= handle
;
448 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
453 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
455 assert(!ctx
->depth_stencil_saved
);
456 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
459 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
461 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
462 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
463 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
465 ctx
->depth_stencil_saved
= NULL
;
470 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
471 const struct pipe_rasterizer_state
*templ
)
473 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
474 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
475 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
476 hash_key
, CSO_RASTERIZER
,
477 (void*)templ
, key_size
);
480 if (cso_hash_iter_is_null(iter
)) {
481 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
483 return PIPE_ERROR_OUT_OF_MEMORY
;
485 memcpy(&cso
->state
, templ
, sizeof(*templ
));
486 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
487 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
488 cso
->context
= ctx
->pipe
;
490 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
491 if (cso_hash_iter_is_null(iter
)) {
493 return PIPE_ERROR_OUT_OF_MEMORY
;
499 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
502 if (ctx
->rasterizer
!= handle
) {
503 ctx
->rasterizer
= handle
;
504 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
509 void cso_save_rasterizer(struct cso_context
*ctx
)
511 assert(!ctx
->rasterizer_saved
);
512 ctx
->rasterizer_saved
= ctx
->rasterizer
;
515 void cso_restore_rasterizer(struct cso_context
*ctx
)
517 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
518 ctx
->rasterizer
= ctx
->rasterizer_saved
;
519 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
521 ctx
->rasterizer_saved
= NULL
;
526 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
529 if (ctx
->fragment_shader
!= handle
) {
530 ctx
->fragment_shader
= handle
;
531 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
536 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
538 if (handle
== ctx
->fragment_shader
) {
539 /* unbind before deleting */
540 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
541 ctx
->fragment_shader
= NULL
;
543 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
546 void cso_save_fragment_shader(struct cso_context
*ctx
)
548 assert(!ctx
->fragment_shader_saved
);
549 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
552 void cso_restore_fragment_shader(struct cso_context
*ctx
)
554 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
555 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
556 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
558 ctx
->fragment_shader_saved
= NULL
;
562 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
565 if (ctx
->vertex_shader
!= handle
) {
566 ctx
->vertex_shader
= handle
;
567 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
572 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
574 if (handle
== ctx
->vertex_shader
) {
575 /* unbind before deleting */
576 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
577 ctx
->vertex_shader
= NULL
;
579 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
582 void cso_save_vertex_shader(struct cso_context
*ctx
)
584 assert(!ctx
->vertex_shader_saved
);
585 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
588 void cso_restore_vertex_shader(struct cso_context
*ctx
)
590 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
591 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
592 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
594 ctx
->vertex_shader_saved
= NULL
;
598 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
599 const struct pipe_framebuffer_state
*fb
)
601 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
602 util_copy_framebuffer_state(&ctx
->fb
, fb
);
603 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
608 void cso_save_framebuffer(struct cso_context
*ctx
)
610 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
613 void cso_restore_framebuffer(struct cso_context
*ctx
)
615 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
616 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
617 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
618 util_unreference_framebuffer_state(&ctx
->fb_saved
);
623 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
624 const struct pipe_viewport_state
*vp
)
626 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
628 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
633 void cso_save_viewport(struct cso_context
*ctx
)
635 ctx
->vp_saved
= ctx
->vp
;
639 void cso_restore_viewport(struct cso_context
*ctx
)
641 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
642 ctx
->vp
= ctx
->vp_saved
;
643 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
648 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
649 const struct pipe_blend_color
*bc
)
651 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
652 ctx
->blend_color
= *bc
;
653 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
658 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
659 unsigned sample_mask
)
661 if (ctx
->sample_mask
!= sample_mask
) {
662 ctx
->sample_mask
= sample_mask
;
663 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
668 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
669 const struct pipe_stencil_ref
*sr
)
671 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
672 ctx
->stencil_ref
= *sr
;
673 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
678 void cso_save_stencil_ref(struct cso_context
*ctx
)
680 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
684 void cso_restore_stencil_ref(struct cso_context
*ctx
)
686 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
, sizeof(ctx
->stencil_ref
))) {
687 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
688 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
692 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
695 assert(ctx
->has_geometry_shader
|| !handle
);
697 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
698 ctx
->geometry_shader
= handle
;
699 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
704 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
706 if (handle
== ctx
->geometry_shader
) {
707 /* unbind before deleting */
708 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
709 ctx
->geometry_shader
= NULL
;
711 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
714 void cso_save_geometry_shader(struct cso_context
*ctx
)
716 if (!ctx
->has_geometry_shader
) {
720 assert(!ctx
->geometry_shader_saved
);
721 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
724 void cso_restore_geometry_shader(struct cso_context
*ctx
)
726 if (!ctx
->has_geometry_shader
) {
730 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
731 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
732 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
734 ctx
->geometry_shader_saved
= NULL
;
740 clip_state_cpy(struct pipe_clip_state
*dst
,
741 const struct pipe_clip_state
*src
)
743 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
747 clip_state_cmp(const struct pipe_clip_state
*a
,
748 const struct pipe_clip_state
*b
)
750 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
754 cso_set_clip(struct cso_context
*ctx
,
755 const struct pipe_clip_state
*clip
)
757 if (clip_state_cmp(&ctx
->clip
, clip
)) {
758 clip_state_cpy(&ctx
->clip
, clip
);
759 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
764 cso_save_clip(struct cso_context
*ctx
)
766 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
770 cso_restore_clip(struct cso_context
*ctx
)
772 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
773 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
774 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
778 enum pipe_error
cso_set_vertex_elements(struct cso_context
*ctx
,
780 const struct pipe_vertex_element
*states
)
782 unsigned key_size
, hash_key
;
783 struct cso_hash_iter iter
;
785 struct cso_velems_state velems_state
;
787 /* need to include the count into the stored state data too.
788 Otherwise first few count pipe_vertex_elements could be identical even if count
789 is different, and there's no guarantee the hash would be different in that
791 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
792 velems_state
.count
= count
;
793 memcpy(velems_state
.velems
, states
, sizeof(struct pipe_vertex_element
) * count
);
794 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
795 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
, (void*)&velems_state
, key_size
);
797 if (cso_hash_iter_is_null(iter
)) {
798 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
800 return PIPE_ERROR_OUT_OF_MEMORY
;
802 memcpy(&cso
->state
, &velems_state
, key_size
);
803 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
, &cso
->state
.velems
[0]);
804 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vertex_elements_state
;
805 cso
->context
= ctx
->pipe
;
807 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
808 if (cso_hash_iter_is_null(iter
)) {
810 return PIPE_ERROR_OUT_OF_MEMORY
;
816 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
819 if (ctx
->velements
!= handle
) {
820 ctx
->velements
= handle
;
821 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
826 void cso_save_vertex_elements(struct cso_context
*ctx
)
828 assert(!ctx
->velements_saved
);
829 ctx
->velements_saved
= ctx
->velements
;
832 void cso_restore_vertex_elements(struct cso_context
*ctx
)
834 if (ctx
->velements
!= ctx
->velements_saved
) {
835 ctx
->velements
= ctx
->velements_saved
;
836 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
838 ctx
->velements_saved
= NULL
;
843 void cso_set_vertex_buffers(struct cso_context
*ctx
,
845 const struct pipe_vertex_buffer
*buffers
)
847 if (count
!= ctx
->nr_vertex_buffers
||
848 memcmp(buffers
, ctx
->vertex_buffers
,
849 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
850 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
852 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
856 void cso_save_vertex_buffers(struct cso_context
*ctx
)
858 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
859 &ctx
->nr_vertex_buffers_saved
,
861 ctx
->nr_vertex_buffers
);
864 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
868 util_copy_vertex_buffers(ctx
->vertex_buffers
,
869 &ctx
->nr_vertex_buffers
,
870 ctx
->vertex_buffers_saved
,
871 ctx
->nr_vertex_buffers_saved
);
873 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
874 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
876 ctx
->nr_vertex_buffers_saved
= 0;
878 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
879 ctx
->vertex_buffers
);
883 /**************** fragment/vertex sampler view state *************************/
885 static enum pipe_error
886 single_sampler(struct cso_context
*ctx
,
887 struct sampler_info
*info
,
889 const struct pipe_sampler_state
*templ
)
894 unsigned key_size
= sizeof(struct pipe_sampler_state
);
895 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
896 struct cso_hash_iter iter
=
897 cso_find_state_template(ctx
->cache
,
898 hash_key
, CSO_SAMPLER
,
899 (void *) templ
, key_size
);
901 if (cso_hash_iter_is_null(iter
)) {
902 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
904 return PIPE_ERROR_OUT_OF_MEMORY
;
906 memcpy(&cso
->state
, templ
, sizeof(*templ
));
907 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
908 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
909 cso
->context
= ctx
->pipe
;
911 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
912 if (cso_hash_iter_is_null(iter
)) {
914 return PIPE_ERROR_OUT_OF_MEMORY
;
920 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
924 info
->samplers
[idx
] = handle
;
930 cso_single_sampler(struct cso_context
*ctx
,
932 const struct pipe_sampler_state
*templ
)
934 return single_sampler(ctx
, &ctx
->fragment_samplers
, idx
, templ
);
938 cso_single_vertex_sampler(struct cso_context
*ctx
,
940 const struct pipe_sampler_state
*templ
)
942 return single_sampler(ctx
, &ctx
->vertex_samplers
, idx
, templ
);
948 single_sampler_done(struct cso_context
*ctx
,
949 struct sampler_info
*info
)
953 /* find highest non-null sampler */
954 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
955 if (info
->samplers
[i
- 1] != NULL
)
959 info
->nr_samplers
= i
;
961 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
962 memcmp(info
->hw
.samplers
,
964 info
->nr_samplers
* sizeof(void *)) != 0)
966 memcpy(info
->hw
.samplers
,
968 info
->nr_samplers
* sizeof(void *));
969 info
->hw
.nr_samplers
= info
->nr_samplers
;
971 if (info
== &ctx
->fragment_samplers
) {
972 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
976 else if (info
== &ctx
->vertex_samplers
) {
977 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
988 cso_single_sampler_done( struct cso_context
*ctx
)
990 single_sampler_done(ctx
, &ctx
->fragment_samplers
);
994 cso_single_vertex_sampler_done(struct cso_context
*ctx
)
996 single_sampler_done(ctx
, &ctx
->vertex_samplers
);
1001 * If the function encouters any errors it will return the
1002 * last one. Done to always try to set as many samplers
1005 static enum pipe_error
1006 set_samplers(struct cso_context
*ctx
,
1007 struct sampler_info
*info
,
1009 const struct pipe_sampler_state
**templates
)
1012 enum pipe_error temp
, error
= PIPE_OK
;
1017 for (i
= 0; i
< nr
; i
++) {
1018 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1019 if (temp
!= PIPE_OK
)
1023 for ( ; i
< info
->nr_samplers
; i
++) {
1024 temp
= single_sampler(ctx
, info
, i
, NULL
);
1025 if (temp
!= PIPE_OK
)
1029 single_sampler_done(ctx
, info
);
1035 cso_set_samplers(struct cso_context
*ctx
,
1037 const struct pipe_sampler_state
**templates
)
1039 return set_samplers(ctx
, &ctx
->fragment_samplers
, nr
, templates
);
1043 cso_set_vertex_samplers(struct cso_context
*ctx
,
1045 const struct pipe_sampler_state
**templates
)
1047 return set_samplers(ctx
, &ctx
->vertex_samplers
, nr
, templates
);
1053 save_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1055 info
->nr_samplers_saved
= info
->nr_samplers
;
1056 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1060 cso_save_samplers(struct cso_context
*ctx
)
1062 save_samplers(ctx
, &ctx
->fragment_samplers
);
1066 cso_save_vertex_samplers(struct cso_context
*ctx
)
1068 save_samplers(ctx
, &ctx
->vertex_samplers
);
1074 restore_samplers(struct cso_context
*ctx
, struct sampler_info
*info
)
1076 info
->nr_samplers
= info
->nr_samplers_saved
;
1077 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1078 single_sampler_done(ctx
, info
);
1082 cso_restore_samplers(struct cso_context
*ctx
)
1084 restore_samplers(ctx
, &ctx
->fragment_samplers
);
1088 cso_restore_vertex_samplers(struct cso_context
*ctx
)
1090 restore_samplers(ctx
, &ctx
->vertex_samplers
);
1096 set_sampler_views(struct cso_context
*ctx
,
1097 struct sampler_info
*info
,
1098 void (*set_views
)(struct pipe_context
*,
1100 struct pipe_sampler_view
**),
1102 struct pipe_sampler_view
**views
)
1106 /* reference new views */
1107 for (i
= 0; i
< count
; i
++) {
1108 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1110 /* unref extra old views, if any */
1111 for (; i
< info
->nr_views
; i
++) {
1112 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1115 info
->nr_views
= count
;
1117 /* bind the new sampler views */
1118 set_views(ctx
->pipe
, count
, info
->views
);
1122 cso_set_fragment_sampler_views(struct cso_context
*ctx
,
1124 struct pipe_sampler_view
**views
)
1126 set_sampler_views(ctx
, &ctx
->fragment_samplers
,
1127 ctx
->pipe
->set_fragment_sampler_views
,
1132 cso_set_vertex_sampler_views(struct cso_context
*ctx
,
1134 struct pipe_sampler_view
**views
)
1136 set_sampler_views(ctx
, &ctx
->vertex_samplers
,
1137 ctx
->pipe
->set_vertex_sampler_views
,
1144 save_sampler_views(struct cso_context
*ctx
,
1145 struct sampler_info
*info
)
1149 info
->nr_views_saved
= info
->nr_views
;
1151 for (i
= 0; i
< info
->nr_views
; i
++) {
1152 assert(!info
->views_saved
[i
]);
1153 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1158 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1160 save_sampler_views(ctx
, &ctx
->fragment_samplers
);
1164 cso_save_vertex_sampler_views(struct cso_context
*ctx
)
1166 save_sampler_views(ctx
, &ctx
->vertex_samplers
);
1171 restore_sampler_views(struct cso_context
*ctx
,
1172 struct sampler_info
*info
,
1173 void (*set_views
)(struct pipe_context
*,
1175 struct pipe_sampler_view
**))
1179 for (i
= 0; i
< info
->nr_views_saved
; i
++) {
1180 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1181 /* move the reference from one pointer to another */
1182 info
->views
[i
] = info
->views_saved
[i
];
1183 info
->views_saved
[i
] = NULL
;
1185 for (; i
< info
->nr_views
; i
++) {
1186 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1189 /* bind the old/saved sampler views */
1190 set_views(ctx
->pipe
, info
->nr_views_saved
, info
->views
);
1192 info
->nr_views
= info
->nr_views_saved
;
1193 info
->nr_views_saved
= 0;
1197 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1199 restore_sampler_views(ctx
, &ctx
->fragment_samplers
,
1200 ctx
->pipe
->set_fragment_sampler_views
);
1204 cso_restore_vertex_sampler_views(struct cso_context
*ctx
)
1206 restore_sampler_views(ctx
, &ctx
->vertex_samplers
,
1207 ctx
->pipe
->set_vertex_sampler_views
);
1212 cso_set_stream_outputs(struct cso_context
*ctx
,
1213 unsigned num_targets
,
1214 struct pipe_stream_output_target
**targets
,
1215 unsigned append_bitmask
)
1217 struct pipe_context
*pipe
= ctx
->pipe
;
1220 if (!ctx
->has_streamout
) {
1221 assert(num_targets
== 0);
1225 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1226 /* Nothing to do. */
1230 /* reference new targets */
1231 for (i
= 0; i
< num_targets
; i
++) {
1232 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1234 /* unref extra old targets, if any */
1235 for (; i
< ctx
->nr_so_targets
; i
++) {
1236 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1239 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1241 ctx
->nr_so_targets
= num_targets
;
1245 cso_save_stream_outputs(struct cso_context
*ctx
)
1249 if (!ctx
->has_streamout
) {
1253 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1255 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1256 assert(!ctx
->so_targets_saved
[i
]);
1257 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1262 cso_restore_stream_outputs(struct cso_context
*ctx
)
1264 struct pipe_context
*pipe
= ctx
->pipe
;
1267 if (!ctx
->has_streamout
) {
1271 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1272 /* Nothing to do. */
1276 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1277 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1278 /* move the reference from one pointer to another */
1279 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1280 ctx
->so_targets_saved
[i
] = NULL
;
1282 for (; i
< ctx
->nr_so_targets
; i
++) {
1283 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1286 /* ~0 means append */
1287 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1288 ctx
->so_targets
, ~0);
1290 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1291 ctx
->nr_so_targets_saved
= 0;