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_draw.h"
40 #include "util/u_framebuffer.h"
41 #include "util/u_inlines.h"
42 #include "util/u_math.h"
43 #include "util/u_memory.h"
44 #include "util/u_vbuf.h"
45 #include "tgsi/tgsi_parse.h"
47 #include "cso_cache/cso_context.h"
48 #include "cso_cache/cso_cache.h"
49 #include "cso_cache/cso_hash.h"
50 #include "cso_context.h"
54 * Info related to samplers and sampler views.
55 * We have one of these for fragment samplers and another for vertex samplers.
60 void *samplers
[PIPE_MAX_SAMPLERS
];
64 void *samplers
[PIPE_MAX_SAMPLERS
];
67 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
68 unsigned nr_samplers_saved
;
70 struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
];
73 struct pipe_sampler_view
*views_saved
[PIPE_MAX_SAMPLERS
];
74 unsigned nr_views_saved
;
80 struct pipe_context
*pipe
;
81 struct cso_cache
*cache
;
84 boolean has_geometry_shader
;
85 boolean has_streamout
;
87 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
89 uint nr_vertex_buffers
;
90 struct pipe_vertex_buffer vertex_buffers
[PIPE_MAX_ATTRIBS
];
92 uint nr_vertex_buffers_saved
;
93 struct pipe_vertex_buffer vertex_buffers_saved
[PIPE_MAX_ATTRIBS
];
95 unsigned nr_so_targets
;
96 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
98 unsigned nr_so_targets_saved
;
99 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
101 /** Current and saved state.
102 * The saved state is used as a 1-deep stack.
104 void *blend
, *blend_saved
;
105 void *depth_stencil
, *depth_stencil_saved
;
106 void *rasterizer
, *rasterizer_saved
;
107 void *fragment_shader
, *fragment_shader_saved
;
108 void *vertex_shader
, *vertex_shader_saved
;
109 void *geometry_shader
, *geometry_shader_saved
;
110 void *velements
, *velements_saved
;
112 struct pipe_clip_state clip
;
113 struct pipe_clip_state clip_saved
;
115 struct pipe_framebuffer_state fb
, fb_saved
;
116 struct pipe_viewport_state vp
, vp_saved
;
117 struct pipe_blend_color blend_color
;
118 unsigned sample_mask
, sample_mask_saved
;
119 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
123 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
125 struct cso_blend
*cso
= (struct cso_blend
*)state
;
127 if (ctx
->blend
== cso
->data
)
130 if (cso
->delete_state
)
131 cso
->delete_state(cso
->context
, cso
->data
);
136 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
138 struct cso_depth_stencil_alpha
*cso
=
139 (struct cso_depth_stencil_alpha
*)state
;
141 if (ctx
->depth_stencil
== cso
->data
)
144 if (cso
->delete_state
)
145 cso
->delete_state(cso
->context
, cso
->data
);
151 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
153 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
154 if (cso
->delete_state
)
155 cso
->delete_state(cso
->context
, cso
->data
);
160 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
162 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
164 if (ctx
->rasterizer
== cso
->data
)
166 if (cso
->delete_state
)
167 cso
->delete_state(cso
->context
, cso
->data
);
172 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
175 struct cso_velements
*cso
= (struct cso_velements
*)state
;
177 if (ctx
->velements
== cso
->data
)
180 if (cso
->delete_state
)
181 cso
->delete_state(cso
->context
, cso
->data
);
187 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
188 void *state
, enum cso_cache_type type
)
192 return delete_blend_state(ctx
, state
);
194 return delete_sampler_state(ctx
, state
);
195 case CSO_DEPTH_STENCIL_ALPHA
:
196 return delete_depth_stencil_state(ctx
, state
);
198 return delete_rasterizer_state(ctx
, state
);
200 return delete_vertex_elements(ctx
, state
);
209 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
);
233 static void cso_init_vbuf(struct cso_context
*cso
)
235 struct u_vbuf_caps caps
;
237 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
239 /* Install u_vbuf if there is anything unsupported. */
240 if (!caps
.buffer_offset_unaligned
||
241 !caps
.buffer_stride_unaligned
||
242 !caps
.velem_src_offset_unaligned
||
243 !caps
.format_fixed32
||
244 !caps
.format_float16
||
245 !caps
.format_float64
||
246 !caps
.format_norm32
||
247 !caps
.format_scaled32
||
248 !caps
.user_vertex_buffers
) {
249 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
);
253 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
255 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
259 assert(PIPE_MAX_SAMPLERS
== PIPE_MAX_VERTEX_SAMPLERS
);
261 ctx
->cache
= cso_cache_create();
262 if (ctx
->cache
== NULL
)
264 cso_cache_set_sanitize_callback(ctx
->cache
,
269 ctx
->sample_mask_saved
= ~0;
273 /* Enable for testing: */
274 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
276 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
277 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
278 ctx
->has_geometry_shader
= TRUE
;
280 if (pipe
->screen
->get_param(pipe
->screen
,
281 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
282 ctx
->has_streamout
= TRUE
;
288 cso_destroy_context( ctx
);
293 * Prior to context destruction, this function unbinds all state objects.
295 void cso_release_all( struct cso_context
*ctx
)
300 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
301 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
302 ctx
->pipe
->bind_fragment_sampler_states( ctx
->pipe
, 0, NULL
);
303 if (ctx
->pipe
->bind_vertex_sampler_states
)
304 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
, 0, NULL
);
305 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
306 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
307 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
308 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
309 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, 0, NULL
);
310 if (ctx
->pipe
->set_vertex_sampler_views
)
311 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, 0, NULL
);
312 if (ctx
->pipe
->set_stream_output_targets
)
313 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
316 /* free fragment samplers, views */
317 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
318 struct sampler_info
*info
= &ctx
->samplers
[shader
];
319 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
320 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
321 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
325 util_unreference_framebuffer_state(&ctx
->fb
);
326 util_unreference_framebuffer_state(&ctx
->fb_saved
);
328 util_copy_vertex_buffers(ctx
->vertex_buffers
,
329 &ctx
->nr_vertex_buffers
,
331 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
332 &ctx
->nr_vertex_buffers_saved
,
335 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
336 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
337 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
341 cso_cache_delete( ctx
->cache
);
348 * Free the CSO context. NOTE: the state tracker should have previously called
351 void cso_destroy_context( struct cso_context
*ctx
)
355 u_vbuf_destroy(ctx
->vbuf
);
361 /* Those function will either find the state of the given template
362 * in the cache or they will create a new state from the given
363 * template, insert it in the cache and return it.
367 * If the driver returns 0 from the create method then they will assign
368 * the data member of the cso to be the template itself.
371 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
372 const struct pipe_blend_state
*templ
)
374 unsigned key_size
, hash_key
;
375 struct cso_hash_iter iter
;
378 key_size
= templ
->independent_blend_enable
?
379 sizeof(struct pipe_blend_state
) :
380 (char *)&(templ
->rt
[1]) - (char *)templ
;
381 hash_key
= cso_construct_key((void*)templ
, key_size
);
382 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
383 (void*)templ
, key_size
);
385 if (cso_hash_iter_is_null(iter
)) {
386 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
388 return PIPE_ERROR_OUT_OF_MEMORY
;
390 memset(&cso
->state
, 0, sizeof cso
->state
);
391 memcpy(&cso
->state
, templ
, key_size
);
392 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
393 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
394 cso
->context
= ctx
->pipe
;
396 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
397 if (cso_hash_iter_is_null(iter
)) {
399 return PIPE_ERROR_OUT_OF_MEMORY
;
405 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
408 if (ctx
->blend
!= handle
) {
410 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
415 void cso_save_blend(struct cso_context
*ctx
)
417 assert(!ctx
->blend_saved
);
418 ctx
->blend_saved
= ctx
->blend
;
421 void cso_restore_blend(struct cso_context
*ctx
)
423 if (ctx
->blend
!= ctx
->blend_saved
) {
424 ctx
->blend
= ctx
->blend_saved
;
425 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
427 ctx
->blend_saved
= NULL
;
433 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
434 const struct pipe_depth_stencil_alpha_state
*templ
)
436 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
437 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
438 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
440 CSO_DEPTH_STENCIL_ALPHA
,
441 (void*)templ
, key_size
);
444 if (cso_hash_iter_is_null(iter
)) {
445 struct cso_depth_stencil_alpha
*cso
=
446 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
448 return PIPE_ERROR_OUT_OF_MEMORY
;
450 memcpy(&cso
->state
, templ
, sizeof(*templ
));
451 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
454 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
455 cso
->context
= ctx
->pipe
;
457 iter
= cso_insert_state(ctx
->cache
, hash_key
,
458 CSO_DEPTH_STENCIL_ALPHA
, cso
);
459 if (cso_hash_iter_is_null(iter
)) {
461 return PIPE_ERROR_OUT_OF_MEMORY
;
467 handle
= ((struct cso_depth_stencil_alpha
*)
468 cso_hash_iter_data(iter
))->data
;
471 if (ctx
->depth_stencil
!= handle
) {
472 ctx
->depth_stencil
= handle
;
473 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
478 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
480 assert(!ctx
->depth_stencil_saved
);
481 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
484 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
486 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
487 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
488 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
489 ctx
->depth_stencil_saved
);
491 ctx
->depth_stencil_saved
= NULL
;
496 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
497 const struct pipe_rasterizer_state
*templ
)
499 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
500 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
501 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
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
);
515 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
516 cso
->context
= ctx
->pipe
;
518 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
519 if (cso_hash_iter_is_null(iter
)) {
521 return PIPE_ERROR_OUT_OF_MEMORY
;
527 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
530 if (ctx
->rasterizer
!= handle
) {
531 ctx
->rasterizer
= handle
;
532 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
537 void cso_save_rasterizer(struct cso_context
*ctx
)
539 assert(!ctx
->rasterizer_saved
);
540 ctx
->rasterizer_saved
= ctx
->rasterizer
;
543 void cso_restore_rasterizer(struct cso_context
*ctx
)
545 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
546 ctx
->rasterizer
= ctx
->rasterizer_saved
;
547 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
549 ctx
->rasterizer_saved
= NULL
;
554 enum pipe_error
cso_set_fragment_shader_handle(struct cso_context
*ctx
,
557 if (ctx
->fragment_shader
!= handle
) {
558 ctx
->fragment_shader
= handle
;
559 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
564 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
566 if (handle
== ctx
->fragment_shader
) {
567 /* unbind before deleting */
568 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
569 ctx
->fragment_shader
= NULL
;
571 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
574 void cso_save_fragment_shader(struct cso_context
*ctx
)
576 assert(!ctx
->fragment_shader_saved
);
577 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
580 void cso_restore_fragment_shader(struct cso_context
*ctx
)
582 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
583 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
584 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
586 ctx
->fragment_shader_saved
= NULL
;
590 enum pipe_error
cso_set_vertex_shader_handle(struct cso_context
*ctx
,
593 if (ctx
->vertex_shader
!= handle
) {
594 ctx
->vertex_shader
= handle
;
595 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
600 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
602 if (handle
== ctx
->vertex_shader
) {
603 /* unbind before deleting */
604 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
605 ctx
->vertex_shader
= NULL
;
607 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
610 void cso_save_vertex_shader(struct cso_context
*ctx
)
612 assert(!ctx
->vertex_shader_saved
);
613 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
616 void cso_restore_vertex_shader(struct cso_context
*ctx
)
618 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
619 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
620 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
622 ctx
->vertex_shader_saved
= NULL
;
626 enum pipe_error
cso_set_framebuffer(struct cso_context
*ctx
,
627 const struct pipe_framebuffer_state
*fb
)
629 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
630 util_copy_framebuffer_state(&ctx
->fb
, fb
);
631 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
636 void cso_save_framebuffer(struct cso_context
*ctx
)
638 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
641 void cso_restore_framebuffer(struct cso_context
*ctx
)
643 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
644 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
645 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
646 util_unreference_framebuffer_state(&ctx
->fb_saved
);
651 enum pipe_error
cso_set_viewport(struct cso_context
*ctx
,
652 const struct pipe_viewport_state
*vp
)
654 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
656 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
661 void cso_save_viewport(struct cso_context
*ctx
)
663 ctx
->vp_saved
= ctx
->vp
;
667 void cso_restore_viewport(struct cso_context
*ctx
)
669 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
670 ctx
->vp
= ctx
->vp_saved
;
671 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
676 enum pipe_error
cso_set_blend_color(struct cso_context
*ctx
,
677 const struct pipe_blend_color
*bc
)
679 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
680 ctx
->blend_color
= *bc
;
681 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
686 enum pipe_error
cso_set_sample_mask(struct cso_context
*ctx
,
687 unsigned sample_mask
)
689 if (ctx
->sample_mask
!= sample_mask
) {
690 ctx
->sample_mask
= sample_mask
;
691 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
696 void cso_save_sample_mask(struct cso_context
*ctx
)
698 ctx
->sample_mask_saved
= ctx
->sample_mask
;
701 void cso_restore_sample_mask(struct cso_context
*ctx
)
703 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
706 enum pipe_error
cso_set_stencil_ref(struct cso_context
*ctx
,
707 const struct pipe_stencil_ref
*sr
)
709 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
710 ctx
->stencil_ref
= *sr
;
711 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
716 void cso_save_stencil_ref(struct cso_context
*ctx
)
718 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
722 void cso_restore_stencil_ref(struct cso_context
*ctx
)
724 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
725 sizeof(ctx
->stencil_ref
))) {
726 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
727 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
731 enum pipe_error
cso_set_geometry_shader_handle(struct cso_context
*ctx
,
734 assert(ctx
->has_geometry_shader
|| !handle
);
736 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
737 ctx
->geometry_shader
= handle
;
738 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
743 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
745 if (handle
== ctx
->geometry_shader
) {
746 /* unbind before deleting */
747 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
748 ctx
->geometry_shader
= NULL
;
750 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
753 void cso_save_geometry_shader(struct cso_context
*ctx
)
755 if (!ctx
->has_geometry_shader
) {
759 assert(!ctx
->geometry_shader_saved
);
760 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
763 void cso_restore_geometry_shader(struct cso_context
*ctx
)
765 if (!ctx
->has_geometry_shader
) {
769 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
770 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
771 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
773 ctx
->geometry_shader_saved
= NULL
;
779 clip_state_cpy(struct pipe_clip_state
*dst
,
780 const struct pipe_clip_state
*src
)
782 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
786 clip_state_cmp(const struct pipe_clip_state
*a
,
787 const struct pipe_clip_state
*b
)
789 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
793 cso_set_clip(struct cso_context
*ctx
,
794 const struct pipe_clip_state
*clip
)
796 if (clip_state_cmp(&ctx
->clip
, clip
)) {
797 clip_state_cpy(&ctx
->clip
, clip
);
798 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
803 cso_save_clip(struct cso_context
*ctx
)
805 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
809 cso_restore_clip(struct cso_context
*ctx
)
811 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
812 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
813 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
818 cso_set_vertex_elements(struct cso_context
*ctx
,
820 const struct pipe_vertex_element
*states
)
822 struct u_vbuf
*vbuf
= ctx
->vbuf
;
823 unsigned key_size
, hash_key
;
824 struct cso_hash_iter iter
;
826 struct cso_velems_state velems_state
;
829 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
833 /* Need to include the count into the stored state data too.
834 * Otherwise first few count pipe_vertex_elements could be identical
835 * even if count is different, and there's no guarantee the hash would
836 * be different in that case neither.
838 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
839 velems_state
.count
= count
;
840 memcpy(velems_state
.velems
, states
,
841 sizeof(struct pipe_vertex_element
) * count
);
842 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
843 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
844 (void*)&velems_state
, key_size
);
846 if (cso_hash_iter_is_null(iter
)) {
847 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
849 return PIPE_ERROR_OUT_OF_MEMORY
;
851 memcpy(&cso
->state
, &velems_state
, key_size
);
852 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
853 &cso
->state
.velems
[0]);
855 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
856 cso
->context
= ctx
->pipe
;
858 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
859 if (cso_hash_iter_is_null(iter
)) {
861 return PIPE_ERROR_OUT_OF_MEMORY
;
867 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
870 if (ctx
->velements
!= handle
) {
871 ctx
->velements
= handle
;
872 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
877 void cso_save_vertex_elements(struct cso_context
*ctx
)
879 struct u_vbuf
*vbuf
= ctx
->vbuf
;
882 u_vbuf_save_vertex_elements(vbuf
);
886 assert(!ctx
->velements_saved
);
887 ctx
->velements_saved
= ctx
->velements
;
890 void cso_restore_vertex_elements(struct cso_context
*ctx
)
892 struct u_vbuf
*vbuf
= ctx
->vbuf
;
895 u_vbuf_restore_vertex_elements(vbuf
);
899 if (ctx
->velements
!= ctx
->velements_saved
) {
900 ctx
->velements
= ctx
->velements_saved
;
901 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
903 ctx
->velements_saved
= NULL
;
908 void cso_set_vertex_buffers(struct cso_context
*ctx
,
910 const struct pipe_vertex_buffer
*buffers
)
912 struct u_vbuf
*vbuf
= ctx
->vbuf
;
915 u_vbuf_set_vertex_buffers(vbuf
, count
, buffers
);
919 if (count
!= ctx
->nr_vertex_buffers
||
920 memcmp(buffers
, ctx
->vertex_buffers
,
921 sizeof(struct pipe_vertex_buffer
) * count
) != 0) {
922 util_copy_vertex_buffers(ctx
->vertex_buffers
, &ctx
->nr_vertex_buffers
,
924 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, count
, buffers
);
928 void cso_save_vertex_buffers(struct cso_context
*ctx
)
930 struct u_vbuf
*vbuf
= ctx
->vbuf
;
933 u_vbuf_save_vertex_buffers(vbuf
);
937 util_copy_vertex_buffers(ctx
->vertex_buffers_saved
,
938 &ctx
->nr_vertex_buffers_saved
,
940 ctx
->nr_vertex_buffers
);
943 void cso_restore_vertex_buffers(struct cso_context
*ctx
)
946 struct u_vbuf
*vbuf
= ctx
->vbuf
;
949 u_vbuf_restore_vertex_buffers(vbuf
);
953 util_copy_vertex_buffers(ctx
->vertex_buffers
,
954 &ctx
->nr_vertex_buffers
,
955 ctx
->vertex_buffers_saved
,
956 ctx
->nr_vertex_buffers_saved
);
958 for (i
= 0; i
< ctx
->nr_vertex_buffers_saved
; i
++) {
959 pipe_resource_reference(&ctx
->vertex_buffers_saved
[i
].buffer
, NULL
);
961 ctx
->nr_vertex_buffers_saved
= 0;
963 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, ctx
->nr_vertex_buffers
,
964 ctx
->vertex_buffers
);
968 /**************** fragment/vertex sampler view state *************************/
970 static enum pipe_error
971 single_sampler(struct cso_context
*ctx
,
972 struct sampler_info
*info
,
974 const struct pipe_sampler_state
*templ
)
979 unsigned key_size
= sizeof(struct pipe_sampler_state
);
980 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
981 struct cso_hash_iter iter
=
982 cso_find_state_template(ctx
->cache
,
983 hash_key
, CSO_SAMPLER
,
984 (void *) templ
, key_size
);
986 if (cso_hash_iter_is_null(iter
)) {
987 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
989 return PIPE_ERROR_OUT_OF_MEMORY
;
991 memcpy(&cso
->state
, templ
, sizeof(*templ
));
992 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
994 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
995 cso
->context
= ctx
->pipe
;
997 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
998 if (cso_hash_iter_is_null(iter
)) {
1000 return PIPE_ERROR_OUT_OF_MEMORY
;
1006 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1010 info
->samplers
[idx
] = handle
;
1016 cso_single_sampler(struct cso_context
*ctx
,
1017 unsigned shader_stage
,
1019 const struct pipe_sampler_state
*templ
)
1021 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1027 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1029 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1032 /* find highest non-null sampler */
1033 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1034 if (info
->samplers
[i
- 1] != NULL
)
1038 info
->nr_samplers
= i
;
1040 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1041 memcmp(info
->hw
.samplers
,
1043 info
->nr_samplers
* sizeof(void *)) != 0)
1045 memcpy(info
->hw
.samplers
,
1047 info
->nr_samplers
* sizeof(void *));
1048 info
->hw
.nr_samplers
= info
->nr_samplers
;
1050 switch (shader_stage
) {
1051 case PIPE_SHADER_FRAGMENT
:
1052 ctx
->pipe
->bind_fragment_sampler_states(ctx
->pipe
,
1056 case PIPE_SHADER_VERTEX
:
1057 ctx
->pipe
->bind_vertex_sampler_states(ctx
->pipe
,
1061 case PIPE_SHADER_GEOMETRY
:
1062 ctx
->pipe
->bind_geometry_sampler_states(ctx
->pipe
,
1067 assert(!"bad shader type in single_sampler_done()");
1073 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1075 single_sampler_done(ctx
, shader_stage
);
1080 * If the function encouters any errors it will return the
1081 * last one. Done to always try to set as many samplers
1085 cso_set_samplers(struct cso_context
*ctx
,
1086 unsigned shader_stage
,
1088 const struct pipe_sampler_state
**templates
)
1090 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1092 enum pipe_error temp
, error
= PIPE_OK
;
1097 for (i
= 0; i
< nr
; i
++) {
1098 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1099 if (temp
!= PIPE_OK
)
1103 for ( ; i
< info
->nr_samplers
; i
++) {
1104 temp
= single_sampler(ctx
, info
, i
, NULL
);
1105 if (temp
!= PIPE_OK
)
1109 single_sampler_done(ctx
, shader_stage
);
1115 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1117 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1118 info
->nr_samplers_saved
= info
->nr_samplers
;
1119 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1124 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1126 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1127 info
->nr_samplers
= info
->nr_samplers_saved
;
1128 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1129 single_sampler_done(ctx
, shader_stage
);
1134 cso_set_sampler_views(struct cso_context
*ctx
,
1135 unsigned shader_stage
,
1137 struct pipe_sampler_view
**views
)
1139 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1142 /* reference new views */
1143 for (i
= 0; i
< count
; i
++) {
1144 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1146 /* unref extra old views, if any */
1147 for (; i
< info
->nr_views
; i
++) {
1148 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1151 info
->nr_views
= count
;
1153 /* bind the new sampler views */
1154 switch (shader_stage
) {
1155 case PIPE_SHADER_FRAGMENT
:
1156 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, count
, info
->views
);
1158 case PIPE_SHADER_VERTEX
:
1159 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, count
, info
->views
);
1161 case PIPE_SHADER_GEOMETRY
:
1162 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, count
, info
->views
);
1165 assert(!"bad shader type in cso_set_sampler_views()");
1171 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1173 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1176 info
->nr_views_saved
= info
->nr_views
;
1178 for (i
= 0; i
< info
->nr_views
; i
++) {
1179 assert(!info
->views_saved
[i
]);
1180 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1186 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1188 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1189 unsigned i
, nr_saved
= info
->nr_views_saved
;
1191 for (i
= 0; i
< nr_saved
; i
++) {
1192 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1193 /* move the reference from one pointer to another */
1194 info
->views
[i
] = info
->views_saved
[i
];
1195 info
->views_saved
[i
] = NULL
;
1197 for (; i
< info
->nr_views
; i
++) {
1198 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1201 /* bind the old/saved sampler views */
1202 switch (shader_stage
) {
1203 case PIPE_SHADER_FRAGMENT
:
1204 ctx
->pipe
->set_fragment_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1206 case PIPE_SHADER_VERTEX
:
1207 ctx
->pipe
->set_vertex_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1209 case PIPE_SHADER_GEOMETRY
:
1210 ctx
->pipe
->set_geometry_sampler_views(ctx
->pipe
, nr_saved
, info
->views
);
1213 assert(!"bad shader type in cso_restore_sampler_views()");
1216 info
->nr_views
= nr_saved
;
1217 info
->nr_views_saved
= 0;
1222 cso_set_stream_outputs(struct cso_context
*ctx
,
1223 unsigned num_targets
,
1224 struct pipe_stream_output_target
**targets
,
1225 unsigned append_bitmask
)
1227 struct pipe_context
*pipe
= ctx
->pipe
;
1230 if (!ctx
->has_streamout
) {
1231 assert(num_targets
== 0);
1235 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1236 /* Nothing to do. */
1240 /* reference new targets */
1241 for (i
= 0; i
< num_targets
; i
++) {
1242 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1244 /* unref extra old targets, if any */
1245 for (; i
< ctx
->nr_so_targets
; i
++) {
1246 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1249 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1251 ctx
->nr_so_targets
= num_targets
;
1255 cso_save_stream_outputs(struct cso_context
*ctx
)
1259 if (!ctx
->has_streamout
) {
1263 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1265 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1266 assert(!ctx
->so_targets_saved
[i
]);
1267 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1272 cso_restore_stream_outputs(struct cso_context
*ctx
)
1274 struct pipe_context
*pipe
= ctx
->pipe
;
1277 if (!ctx
->has_streamout
) {
1281 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1282 /* Nothing to do. */
1286 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1287 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1288 /* move the reference from one pointer to another */
1289 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1290 ctx
->so_targets_saved
[i
] = NULL
;
1292 for (; i
< ctx
->nr_so_targets
; i
++) {
1293 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1296 /* ~0 means append */
1297 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1298 ctx
->so_targets
, ~0);
1300 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1301 ctx
->nr_so_targets_saved
= 0;
1307 cso_set_index_buffer(struct cso_context
*cso
,
1308 const struct pipe_index_buffer
*ib
)
1310 struct u_vbuf
*vbuf
= cso
->vbuf
;
1313 u_vbuf_set_index_buffer(vbuf
, ib
);
1315 struct pipe_context
*pipe
= cso
->pipe
;
1316 pipe
->set_index_buffer(pipe
, ib
);
1321 cso_draw_vbo(struct cso_context
*cso
,
1322 const struct pipe_draw_info
*info
)
1324 struct u_vbuf
*vbuf
= cso
->vbuf
;
1327 u_vbuf_draw_vbo(vbuf
, info
);
1329 struct pipe_context
*pipe
= cso
->pipe
;
1330 pipe
->draw_vbo(pipe
, info
);
1335 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1337 struct pipe_draw_info info
;
1339 util_draw_init_info(&info
);
1344 info
.min_index
= start
;
1345 info
.max_index
= start
+ count
- 1;
1347 cso_draw_vbo(cso
, &info
);