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 struct pipe_vertex_buffer aux_vertex_buffer_current
;
90 struct pipe_vertex_buffer aux_vertex_buffer_saved
;
91 unsigned aux_vertex_buffer_index
;
93 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
94 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
96 unsigned nr_so_targets
;
97 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
99 unsigned nr_so_targets_saved
;
100 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
102 /** Current and saved state.
103 * The saved state is used as a 1-deep stack.
105 void *blend
, *blend_saved
;
106 void *depth_stencil
, *depth_stencil_saved
;
107 void *rasterizer
, *rasterizer_saved
;
108 void *fragment_shader
, *fragment_shader_saved
;
109 void *vertex_shader
, *vertex_shader_saved
;
110 void *geometry_shader
, *geometry_shader_saved
;
111 void *velements
, *velements_saved
;
112 struct pipe_query
*render_condition
, *render_condition_saved
;
113 uint render_condition_mode
, render_condition_mode_saved
;
114 boolean render_condition_cond
, render_condition_cond_saved
;
116 struct pipe_clip_state clip
;
117 struct pipe_clip_state clip_saved
;
119 struct pipe_framebuffer_state fb
, fb_saved
;
120 struct pipe_viewport_state vp
, vp_saved
;
121 struct pipe_blend_color blend_color
;
122 unsigned sample_mask
, sample_mask_saved
;
123 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
127 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
129 struct cso_blend
*cso
= (struct cso_blend
*)state
;
131 if (ctx
->blend
== cso
->data
)
134 if (cso
->delete_state
)
135 cso
->delete_state(cso
->context
, cso
->data
);
140 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
142 struct cso_depth_stencil_alpha
*cso
=
143 (struct cso_depth_stencil_alpha
*)state
;
145 if (ctx
->depth_stencil
== cso
->data
)
148 if (cso
->delete_state
)
149 cso
->delete_state(cso
->context
, cso
->data
);
155 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
157 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
158 if (cso
->delete_state
)
159 cso
->delete_state(cso
->context
, cso
->data
);
164 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
166 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
168 if (ctx
->rasterizer
== cso
->data
)
170 if (cso
->delete_state
)
171 cso
->delete_state(cso
->context
, cso
->data
);
176 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
179 struct cso_velements
*cso
= (struct cso_velements
*)state
;
181 if (ctx
->velements
== cso
->data
)
184 if (cso
->delete_state
)
185 cso
->delete_state(cso
->context
, cso
->data
);
191 static INLINE boolean
delete_cso(struct cso_context
*ctx
,
192 void *state
, enum cso_cache_type type
)
196 return delete_blend_state(ctx
, state
);
198 return delete_sampler_state(ctx
, state
);
199 case CSO_DEPTH_STENCIL_ALPHA
:
200 return delete_depth_stencil_state(ctx
, state
);
202 return delete_rasterizer_state(ctx
, state
);
204 return delete_vertex_elements(ctx
, state
);
213 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
214 int max_size
, void *user_data
)
216 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
217 /* if we're approach the maximum size, remove fourth of the entries
218 * otherwise every subsequent call will go through the same */
219 int hash_size
= cso_hash_size(hash
);
220 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
221 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
222 struct cso_hash_iter iter
= cso_hash_first_node(hash
);
223 if (hash_size
> max_size
)
224 to_remove
+= hash_size
- max_size
;
226 /*remove elements until we're good */
227 /*fixme: currently we pick the nodes to remove at random*/
228 void *cso
= cso_hash_iter_data(iter
);
229 if (delete_cso(ctx
, cso
, type
)) {
230 iter
= cso_hash_erase(hash
, iter
);
233 iter
= cso_hash_iter_next(iter
);
237 static void cso_init_vbuf(struct cso_context
*cso
)
239 struct u_vbuf_caps caps
;
241 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
243 /* Install u_vbuf if there is anything unsupported. */
244 if (!caps
.buffer_offset_unaligned
||
245 !caps
.buffer_stride_unaligned
||
246 !caps
.velem_src_offset_unaligned
||
247 !caps
.format_fixed32
||
248 !caps
.format_float16
||
249 !caps
.format_float64
||
250 !caps
.format_norm32
||
251 !caps
.format_scaled32
||
252 !caps
.user_vertex_buffers
) {
253 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
,
254 cso
->aux_vertex_buffer_index
);
258 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
260 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
264 ctx
->cache
= cso_cache_create();
265 if (ctx
->cache
== NULL
)
267 cso_cache_set_sanitize_callback(ctx
->cache
,
272 ctx
->sample_mask_saved
= ~0;
274 ctx
->aux_vertex_buffer_index
= 0; /* 0 for now */
278 /* Enable for testing: */
279 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
281 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
282 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
283 ctx
->has_geometry_shader
= TRUE
;
285 if (pipe
->screen
->get_param(pipe
->screen
,
286 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
287 ctx
->has_streamout
= TRUE
;
293 cso_destroy_context( ctx
);
298 * Prior to context destruction, this function unbinds all state objects.
300 void cso_release_all( struct cso_context
*ctx
)
305 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
306 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
309 static struct pipe_sampler_view
*views
[PIPE_MAX_SAMPLERS
] = { NULL
};
310 static void *zeros
[PIPE_MAX_SAMPLERS
] = { NULL
};
311 struct pipe_screen
*scr
= ctx
->pipe
->screen
;
313 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
314 int max
= scr
->get_shader_param(scr
, sh
,
315 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
);
316 assert(max
<= PIPE_MAX_SAMPLERS
);
318 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, sh
, 0, max
, zeros
);
319 ctx
->pipe
->set_sampler_views(ctx
->pipe
, sh
, 0, max
, views
);
324 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
325 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
326 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
327 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
329 if (ctx
->pipe
->set_stream_output_targets
)
330 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, 0);
333 /* free fragment samplers, views */
334 for (shader
= 0; shader
< Elements(ctx
->samplers
); shader
++) {
335 struct sampler_info
*info
= &ctx
->samplers
[shader
];
336 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
337 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
338 pipe_sampler_view_reference(&info
->views_saved
[i
], NULL
);
342 util_unreference_framebuffer_state(&ctx
->fb
);
343 util_unreference_framebuffer_state(&ctx
->fb_saved
);
345 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
, NULL
);
346 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
348 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
349 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
350 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
353 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
354 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
355 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
359 cso_cache_delete( ctx
->cache
);
366 * Free the CSO context. NOTE: the state tracker should have previously called
369 void cso_destroy_context( struct cso_context
*ctx
)
373 u_vbuf_destroy(ctx
->vbuf
);
379 /* Those function will either find the state of the given template
380 * in the cache or they will create a new state from the given
381 * template, insert it in the cache and return it.
385 * If the driver returns 0 from the create method then they will assign
386 * the data member of the cso to be the template itself.
389 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
390 const struct pipe_blend_state
*templ
)
392 unsigned key_size
, hash_key
;
393 struct cso_hash_iter iter
;
396 key_size
= templ
->independent_blend_enable
?
397 sizeof(struct pipe_blend_state
) :
398 (char *)&(templ
->rt
[1]) - (char *)templ
;
399 hash_key
= cso_construct_key((void*)templ
, key_size
);
400 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
401 (void*)templ
, key_size
);
403 if (cso_hash_iter_is_null(iter
)) {
404 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
406 return PIPE_ERROR_OUT_OF_MEMORY
;
408 memset(&cso
->state
, 0, sizeof cso
->state
);
409 memcpy(&cso
->state
, templ
, key_size
);
410 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
411 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
412 cso
->context
= ctx
->pipe
;
414 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
415 if (cso_hash_iter_is_null(iter
)) {
417 return PIPE_ERROR_OUT_OF_MEMORY
;
423 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
426 if (ctx
->blend
!= handle
) {
428 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
433 void cso_save_blend(struct cso_context
*ctx
)
435 assert(!ctx
->blend_saved
);
436 ctx
->blend_saved
= ctx
->blend
;
439 void cso_restore_blend(struct cso_context
*ctx
)
441 if (ctx
->blend
!= ctx
->blend_saved
) {
442 ctx
->blend
= ctx
->blend_saved
;
443 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
445 ctx
->blend_saved
= NULL
;
451 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
452 const struct pipe_depth_stencil_alpha_state
*templ
)
454 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
455 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
456 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
458 CSO_DEPTH_STENCIL_ALPHA
,
459 (void*)templ
, key_size
);
462 if (cso_hash_iter_is_null(iter
)) {
463 struct cso_depth_stencil_alpha
*cso
=
464 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
466 return PIPE_ERROR_OUT_OF_MEMORY
;
468 memcpy(&cso
->state
, templ
, sizeof(*templ
));
469 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
472 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
473 cso
->context
= ctx
->pipe
;
475 iter
= cso_insert_state(ctx
->cache
, hash_key
,
476 CSO_DEPTH_STENCIL_ALPHA
, cso
);
477 if (cso_hash_iter_is_null(iter
)) {
479 return PIPE_ERROR_OUT_OF_MEMORY
;
485 handle
= ((struct cso_depth_stencil_alpha
*)
486 cso_hash_iter_data(iter
))->data
;
489 if (ctx
->depth_stencil
!= handle
) {
490 ctx
->depth_stencil
= handle
;
491 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
496 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
498 assert(!ctx
->depth_stencil_saved
);
499 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
502 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
504 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
505 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
506 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
507 ctx
->depth_stencil_saved
);
509 ctx
->depth_stencil_saved
= NULL
;
514 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
515 const struct pipe_rasterizer_state
*templ
)
517 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
518 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
519 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
522 (void*)templ
, key_size
);
525 if (cso_hash_iter_is_null(iter
)) {
526 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
528 return PIPE_ERROR_OUT_OF_MEMORY
;
530 memcpy(&cso
->state
, templ
, sizeof(*templ
));
531 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
533 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
534 cso
->context
= ctx
->pipe
;
536 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
537 if (cso_hash_iter_is_null(iter
)) {
539 return PIPE_ERROR_OUT_OF_MEMORY
;
545 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
548 if (ctx
->rasterizer
!= handle
) {
549 ctx
->rasterizer
= handle
;
550 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
555 void cso_save_rasterizer(struct cso_context
*ctx
)
557 assert(!ctx
->rasterizer_saved
);
558 ctx
->rasterizer_saved
= ctx
->rasterizer
;
561 void cso_restore_rasterizer(struct cso_context
*ctx
)
563 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
564 ctx
->rasterizer
= ctx
->rasterizer_saved
;
565 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
567 ctx
->rasterizer_saved
= NULL
;
571 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
573 if (ctx
->fragment_shader
!= handle
) {
574 ctx
->fragment_shader
= handle
;
575 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
579 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
581 if (handle
== ctx
->fragment_shader
) {
582 /* unbind before deleting */
583 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
584 ctx
->fragment_shader
= NULL
;
586 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
589 void cso_save_fragment_shader(struct cso_context
*ctx
)
591 assert(!ctx
->fragment_shader_saved
);
592 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
595 void cso_restore_fragment_shader(struct cso_context
*ctx
)
597 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
598 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
599 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
601 ctx
->fragment_shader_saved
= NULL
;
605 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
607 if (ctx
->vertex_shader
!= handle
) {
608 ctx
->vertex_shader
= handle
;
609 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
613 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
615 if (handle
== ctx
->vertex_shader
) {
616 /* unbind before deleting */
617 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
618 ctx
->vertex_shader
= NULL
;
620 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
623 void cso_save_vertex_shader(struct cso_context
*ctx
)
625 assert(!ctx
->vertex_shader_saved
);
626 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
629 void cso_restore_vertex_shader(struct cso_context
*ctx
)
631 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
632 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
633 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
635 ctx
->vertex_shader_saved
= NULL
;
639 void cso_set_framebuffer(struct cso_context
*ctx
,
640 const struct pipe_framebuffer_state
*fb
)
642 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
643 util_copy_framebuffer_state(&ctx
->fb
, fb
);
644 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
648 void cso_save_framebuffer(struct cso_context
*ctx
)
650 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
653 void cso_restore_framebuffer(struct cso_context
*ctx
)
655 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
656 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
657 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
658 util_unreference_framebuffer_state(&ctx
->fb_saved
);
663 void cso_set_viewport(struct cso_context
*ctx
,
664 const struct pipe_viewport_state
*vp
)
666 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
668 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
672 void cso_save_viewport(struct cso_context
*ctx
)
674 ctx
->vp_saved
= ctx
->vp
;
678 void cso_restore_viewport(struct cso_context
*ctx
)
680 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
681 ctx
->vp
= ctx
->vp_saved
;
682 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
687 void cso_set_blend_color(struct cso_context
*ctx
,
688 const struct pipe_blend_color
*bc
)
690 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
691 ctx
->blend_color
= *bc
;
692 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
696 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
698 if (ctx
->sample_mask
!= sample_mask
) {
699 ctx
->sample_mask
= sample_mask
;
700 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
704 void cso_save_sample_mask(struct cso_context
*ctx
)
706 ctx
->sample_mask_saved
= ctx
->sample_mask
;
709 void cso_restore_sample_mask(struct cso_context
*ctx
)
711 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
714 void cso_set_stencil_ref(struct cso_context
*ctx
,
715 const struct pipe_stencil_ref
*sr
)
717 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
718 ctx
->stencil_ref
= *sr
;
719 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
723 void cso_save_stencil_ref(struct cso_context
*ctx
)
725 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
729 void cso_restore_stencil_ref(struct cso_context
*ctx
)
731 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
732 sizeof(ctx
->stencil_ref
))) {
733 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
734 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
738 void cso_set_render_condition(struct cso_context
*ctx
,
739 struct pipe_query
*query
,
740 boolean condition
, uint mode
)
742 struct pipe_context
*pipe
= ctx
->pipe
;
744 if (ctx
->render_condition
!= query
||
745 ctx
->render_condition_mode
!= mode
||
746 ctx
->render_condition_cond
!= condition
) {
747 pipe
->render_condition(pipe
, query
, condition
, mode
);
748 ctx
->render_condition
= query
;
749 ctx
->render_condition_cond
= condition
;
750 ctx
->render_condition_mode
= mode
;
754 void cso_save_render_condition(struct cso_context
*ctx
)
756 ctx
->render_condition_saved
= ctx
->render_condition
;
757 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
758 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
761 void cso_restore_render_condition(struct cso_context
*ctx
)
763 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
764 ctx
->render_condition_cond_saved
,
765 ctx
->render_condition_mode_saved
);
768 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
770 assert(ctx
->has_geometry_shader
|| !handle
);
772 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
773 ctx
->geometry_shader
= handle
;
774 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
778 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
780 if (handle
== ctx
->geometry_shader
) {
781 /* unbind before deleting */
782 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
783 ctx
->geometry_shader
= NULL
;
785 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
788 void cso_save_geometry_shader(struct cso_context
*ctx
)
790 if (!ctx
->has_geometry_shader
) {
794 assert(!ctx
->geometry_shader_saved
);
795 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
798 void cso_restore_geometry_shader(struct cso_context
*ctx
)
800 if (!ctx
->has_geometry_shader
) {
804 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
805 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
806 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
808 ctx
->geometry_shader_saved
= NULL
;
814 clip_state_cpy(struct pipe_clip_state
*dst
,
815 const struct pipe_clip_state
*src
)
817 memcpy(dst
->ucp
, src
->ucp
, sizeof(dst
->ucp
));
821 clip_state_cmp(const struct pipe_clip_state
*a
,
822 const struct pipe_clip_state
*b
)
824 return memcmp(a
->ucp
, b
->ucp
, sizeof(a
->ucp
));
828 cso_set_clip(struct cso_context
*ctx
,
829 const struct pipe_clip_state
*clip
)
831 if (clip_state_cmp(&ctx
->clip
, clip
)) {
832 clip_state_cpy(&ctx
->clip
, clip
);
833 ctx
->pipe
->set_clip_state(ctx
->pipe
, clip
);
838 cso_save_clip(struct cso_context
*ctx
)
840 clip_state_cpy(&ctx
->clip_saved
, &ctx
->clip
);
844 cso_restore_clip(struct cso_context
*ctx
)
846 if (clip_state_cmp(&ctx
->clip
, &ctx
->clip_saved
)) {
847 clip_state_cpy(&ctx
->clip
, &ctx
->clip_saved
);
848 ctx
->pipe
->set_clip_state(ctx
->pipe
, &ctx
->clip_saved
);
853 cso_set_vertex_elements(struct cso_context
*ctx
,
855 const struct pipe_vertex_element
*states
)
857 struct u_vbuf
*vbuf
= ctx
->vbuf
;
858 unsigned key_size
, hash_key
;
859 struct cso_hash_iter iter
;
861 struct cso_velems_state velems_state
;
864 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
868 /* Need to include the count into the stored state data too.
869 * Otherwise first few count pipe_vertex_elements could be identical
870 * even if count is different, and there's no guarantee the hash would
871 * be different in that case neither.
873 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
874 velems_state
.count
= count
;
875 memcpy(velems_state
.velems
, states
,
876 sizeof(struct pipe_vertex_element
) * count
);
877 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
878 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
879 (void*)&velems_state
, key_size
);
881 if (cso_hash_iter_is_null(iter
)) {
882 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
884 return PIPE_ERROR_OUT_OF_MEMORY
;
886 memcpy(&cso
->state
, &velems_state
, key_size
);
887 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
888 &cso
->state
.velems
[0]);
890 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
891 cso
->context
= ctx
->pipe
;
893 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
894 if (cso_hash_iter_is_null(iter
)) {
896 return PIPE_ERROR_OUT_OF_MEMORY
;
902 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
905 if (ctx
->velements
!= handle
) {
906 ctx
->velements
= handle
;
907 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
912 void cso_save_vertex_elements(struct cso_context
*ctx
)
914 struct u_vbuf
*vbuf
= ctx
->vbuf
;
917 u_vbuf_save_vertex_elements(vbuf
);
921 assert(!ctx
->velements_saved
);
922 ctx
->velements_saved
= ctx
->velements
;
925 void cso_restore_vertex_elements(struct cso_context
*ctx
)
927 struct u_vbuf
*vbuf
= ctx
->vbuf
;
930 u_vbuf_restore_vertex_elements(vbuf
);
934 if (ctx
->velements
!= ctx
->velements_saved
) {
935 ctx
->velements
= ctx
->velements_saved
;
936 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
938 ctx
->velements_saved
= NULL
;
943 void cso_set_vertex_buffers(struct cso_context
*ctx
,
944 unsigned start_slot
, unsigned count
,
945 const struct pipe_vertex_buffer
*buffers
)
947 struct u_vbuf
*vbuf
= ctx
->vbuf
;
950 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
954 /* Save what's in the auxiliary slot, so that we can save and restore it
956 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
957 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
959 const struct pipe_vertex_buffer
*vb
=
960 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
962 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
964 memcpy(&ctx
->aux_vertex_buffer_current
, vb
,
965 sizeof(struct pipe_vertex_buffer
));
968 pipe_resource_reference(&ctx
->aux_vertex_buffer_current
.buffer
,
970 ctx
->aux_vertex_buffer_current
.user_buffer
= NULL
;
974 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
977 void cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
979 struct u_vbuf
*vbuf
= ctx
->vbuf
;
982 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
986 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
,
987 ctx
->aux_vertex_buffer_current
.buffer
);
988 memcpy(&ctx
->aux_vertex_buffer_saved
, &ctx
->aux_vertex_buffer_current
,
989 sizeof(struct pipe_vertex_buffer
));
992 void cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
994 struct u_vbuf
*vbuf
= ctx
->vbuf
;
997 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
1001 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
1002 &ctx
->aux_vertex_buffer_saved
);
1003 pipe_resource_reference(&ctx
->aux_vertex_buffer_saved
.buffer
, NULL
);
1006 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1008 return ctx
->aux_vertex_buffer_index
;
1012 /**************** fragment/vertex sampler view state *************************/
1014 static enum pipe_error
1015 single_sampler(struct cso_context
*ctx
,
1016 struct sampler_info
*info
,
1018 const struct pipe_sampler_state
*templ
)
1020 void *handle
= NULL
;
1022 if (templ
!= NULL
) {
1023 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1024 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1025 struct cso_hash_iter iter
=
1026 cso_find_state_template(ctx
->cache
,
1027 hash_key
, CSO_SAMPLER
,
1028 (void *) templ
, key_size
);
1030 if (cso_hash_iter_is_null(iter
)) {
1031 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
1033 return PIPE_ERROR_OUT_OF_MEMORY
;
1035 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1036 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1038 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1039 cso
->context
= ctx
->pipe
;
1041 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1042 if (cso_hash_iter_is_null(iter
)) {
1044 return PIPE_ERROR_OUT_OF_MEMORY
;
1050 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
1054 info
->samplers
[idx
] = handle
;
1060 cso_single_sampler(struct cso_context
*ctx
,
1061 unsigned shader_stage
,
1063 const struct pipe_sampler_state
*templ
)
1065 return single_sampler(ctx
, &ctx
->samplers
[shader_stage
], idx
, templ
);
1071 single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1073 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1076 /* find highest non-null sampler */
1077 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
1078 if (info
->samplers
[i
- 1] != NULL
)
1082 info
->nr_samplers
= i
;
1084 if (info
->hw
.nr_samplers
!= info
->nr_samplers
||
1085 memcmp(info
->hw
.samplers
,
1087 info
->nr_samplers
* sizeof(void *)) != 0)
1089 memcpy(info
->hw
.samplers
,
1091 info
->nr_samplers
* sizeof(void *));
1093 /* set remaining slots/pointers to null */
1094 for (i
= info
->nr_samplers
; i
< info
->hw
.nr_samplers
; i
++)
1095 info
->samplers
[i
] = NULL
;
1097 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1098 MAX2(info
->nr_samplers
,
1099 info
->hw
.nr_samplers
),
1102 info
->hw
.nr_samplers
= info
->nr_samplers
;
1107 cso_single_sampler_done(struct cso_context
*ctx
, unsigned shader_stage
)
1109 single_sampler_done(ctx
, shader_stage
);
1114 * If the function encouters any errors it will return the
1115 * last one. Done to always try to set as many samplers
1119 cso_set_samplers(struct cso_context
*ctx
,
1120 unsigned shader_stage
,
1122 const struct pipe_sampler_state
**templates
)
1124 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1126 enum pipe_error temp
, error
= PIPE_OK
;
1131 for (i
= 0; i
< nr
; i
++) {
1132 temp
= single_sampler(ctx
, info
, i
, templates
[i
]);
1133 if (temp
!= PIPE_OK
)
1137 for ( ; i
< info
->nr_samplers
; i
++) {
1138 temp
= single_sampler(ctx
, info
, i
, NULL
);
1139 if (temp
!= PIPE_OK
)
1143 single_sampler_done(ctx
, shader_stage
);
1149 cso_save_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1151 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1152 info
->nr_samplers_saved
= info
->nr_samplers
;
1153 memcpy(info
->samplers_saved
, info
->samplers
, sizeof(info
->samplers
));
1158 cso_restore_samplers(struct cso_context
*ctx
, unsigned shader_stage
)
1160 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1161 info
->nr_samplers
= info
->nr_samplers_saved
;
1162 memcpy(info
->samplers
, info
->samplers_saved
, sizeof(info
->samplers
));
1163 single_sampler_done(ctx
, shader_stage
);
1168 cso_set_sampler_views(struct cso_context
*ctx
,
1169 unsigned shader_stage
,
1171 struct pipe_sampler_view
**views
)
1173 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1176 /* reference new views */
1177 for (i
= 0; i
< count
; i
++) {
1178 pipe_sampler_view_reference(&info
->views
[i
], views
[i
]);
1180 /* unref extra old views, if any */
1181 for (; i
< info
->nr_views
; i
++) {
1182 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1185 info
->nr_views
= count
;
1187 /* bind the new sampler views */
1188 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, count
,
1194 cso_save_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1196 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1199 info
->nr_views_saved
= info
->nr_views
;
1201 for (i
= 0; i
< info
->nr_views
; i
++) {
1202 assert(!info
->views_saved
[i
]);
1203 pipe_sampler_view_reference(&info
->views_saved
[i
], info
->views
[i
]);
1209 cso_restore_sampler_views(struct cso_context
*ctx
, unsigned shader_stage
)
1211 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1212 unsigned i
, nr_saved
= info
->nr_views_saved
;
1215 for (i
= 0; i
< nr_saved
; i
++) {
1216 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1217 /* move the reference from one pointer to another */
1218 info
->views
[i
] = info
->views_saved
[i
];
1219 info
->views_saved
[i
] = NULL
;
1221 for (; i
< info
->nr_views
; i
++) {
1222 pipe_sampler_view_reference(&info
->views
[i
], NULL
);
1225 num
= MAX2(info
->nr_views
, nr_saved
);
1227 /* bind the old/saved sampler views */
1228 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, num
, info
->views
);
1230 info
->nr_views
= nr_saved
;
1231 info
->nr_views_saved
= 0;
1236 cso_set_stream_outputs(struct cso_context
*ctx
,
1237 unsigned num_targets
,
1238 struct pipe_stream_output_target
**targets
,
1239 unsigned append_bitmask
)
1241 struct pipe_context
*pipe
= ctx
->pipe
;
1244 if (!ctx
->has_streamout
) {
1245 assert(num_targets
== 0);
1249 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1250 /* Nothing to do. */
1254 /* reference new targets */
1255 for (i
= 0; i
< num_targets
; i
++) {
1256 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1258 /* unref extra old targets, if any */
1259 for (; i
< ctx
->nr_so_targets
; i
++) {
1260 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1263 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1265 ctx
->nr_so_targets
= num_targets
;
1269 cso_save_stream_outputs(struct cso_context
*ctx
)
1273 if (!ctx
->has_streamout
) {
1277 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1279 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1280 assert(!ctx
->so_targets_saved
[i
]);
1281 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1286 cso_restore_stream_outputs(struct cso_context
*ctx
)
1288 struct pipe_context
*pipe
= ctx
->pipe
;
1291 if (!ctx
->has_streamout
) {
1295 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1296 /* Nothing to do. */
1300 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1301 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1302 /* move the reference from one pointer to another */
1303 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1304 ctx
->so_targets_saved
[i
] = NULL
;
1306 for (; i
< ctx
->nr_so_targets
; i
++) {
1307 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1310 /* ~0 means append */
1311 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1312 ctx
->so_targets
, ~0);
1314 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1315 ctx
->nr_so_targets_saved
= 0;
1318 /* constant buffers */
1321 cso_set_constant_buffer(struct cso_context
*cso
, unsigned shader_stage
,
1322 unsigned index
, struct pipe_constant_buffer
*cb
)
1324 struct pipe_context
*pipe
= cso
->pipe
;
1326 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1329 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1334 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1335 unsigned shader_stage
,
1337 struct pipe_resource
*buffer
)
1340 struct pipe_constant_buffer cb
;
1342 cb
.buffer_offset
= 0;
1343 cb
.buffer_size
= buffer
->width0
;
1344 cb
.user_buffer
= NULL
;
1345 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1347 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1352 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1353 unsigned shader_stage
)
1355 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1356 &cso
->aux_constbuf_current
[shader_stage
]);
1360 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1361 unsigned shader_stage
)
1363 cso_set_constant_buffer(cso
, shader_stage
, 0,
1364 &cso
->aux_constbuf_saved
[shader_stage
]);
1365 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1372 cso_set_index_buffer(struct cso_context
*cso
,
1373 const struct pipe_index_buffer
*ib
)
1375 struct u_vbuf
*vbuf
= cso
->vbuf
;
1378 u_vbuf_set_index_buffer(vbuf
, ib
);
1380 struct pipe_context
*pipe
= cso
->pipe
;
1381 pipe
->set_index_buffer(pipe
, ib
);
1386 cso_draw_vbo(struct cso_context
*cso
,
1387 const struct pipe_draw_info
*info
)
1389 struct u_vbuf
*vbuf
= cso
->vbuf
;
1392 u_vbuf_draw_vbo(vbuf
, info
);
1394 struct pipe_context
*pipe
= cso
->pipe
;
1395 pipe
->draw_vbo(pipe
, info
);
1400 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1402 struct pipe_draw_info info
;
1404 util_draw_init_info(&info
);
1409 info
.min_index
= start
;
1410 info
.max_index
= start
+ count
- 1;
1412 cso_draw_vbo(cso
, &info
);