1 /**************************************************************************
3 * Copyright 2007 VMware, Inc.
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 VMWARE 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 <zackr@vmware.com>
35 * @author Keith Whitwell <keithw@vmware.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 * Per-shader sampler information.
58 struct cso_sampler
*cso_samplers
[PIPE_MAX_SAMPLERS
];
59 void *samplers
[PIPE_MAX_SAMPLERS
];
65 struct pipe_context
*pipe
;
66 struct cso_cache
*cache
;
69 struct u_vbuf
*vbuf_current
;
72 boolean has_geometry_shader
;
73 boolean has_tessellation
;
74 boolean has_compute_shader
;
75 boolean has_streamout
;
77 unsigned saved_state
; /**< bitmask of CSO_BIT_x flags */
79 struct pipe_sampler_view
*fragment_views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
80 unsigned nr_fragment_views
;
82 struct pipe_sampler_view
*fragment_views_saved
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
83 unsigned nr_fragment_views_saved
;
85 struct sampler_info fragment_samplers_saved
;
86 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
88 /* Temporary number until cso_single_sampler_done is called.
89 * It tracks the highest sampler seen in cso_single_sampler.
93 struct pipe_vertex_buffer vertex_buffer0_current
;
94 struct pipe_vertex_buffer vertex_buffer0_saved
;
96 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
97 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
99 struct pipe_image_view fragment_image0_current
;
100 struct pipe_image_view fragment_image0_saved
;
102 unsigned nr_so_targets
;
103 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
105 unsigned nr_so_targets_saved
;
106 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
108 /** Current and saved state.
109 * The saved state is used as a 1-deep stack.
111 void *blend
, *blend_saved
;
112 void *depth_stencil
, *depth_stencil_saved
;
113 void *rasterizer
, *rasterizer_saved
;
114 void *fragment_shader
, *fragment_shader_saved
;
115 void *vertex_shader
, *vertex_shader_saved
;
116 void *geometry_shader
, *geometry_shader_saved
;
117 void *tessctrl_shader
, *tessctrl_shader_saved
;
118 void *tesseval_shader
, *tesseval_shader_saved
;
119 void *compute_shader
;
120 void *velements
, *velements_saved
;
121 struct pipe_query
*render_condition
, *render_condition_saved
;
122 uint render_condition_mode
, render_condition_mode_saved
;
123 boolean render_condition_cond
, render_condition_cond_saved
;
125 struct pipe_framebuffer_state fb
, fb_saved
;
126 struct pipe_viewport_state vp
, vp_saved
;
127 struct pipe_blend_color blend_color
;
128 unsigned sample_mask
, sample_mask_saved
;
129 unsigned min_samples
, min_samples_saved
;
130 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
133 struct pipe_context
*cso_get_pipe_context(struct cso_context
*cso
)
138 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
140 struct cso_blend
*cso
= (struct cso_blend
*)state
;
142 if (ctx
->blend
== cso
->data
)
145 if (cso
->delete_state
)
146 cso
->delete_state(cso
->context
, cso
->data
);
151 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
153 struct cso_depth_stencil_alpha
*cso
=
154 (struct cso_depth_stencil_alpha
*)state
;
156 if (ctx
->depth_stencil
== cso
->data
)
159 if (cso
->delete_state
)
160 cso
->delete_state(cso
->context
, cso
->data
);
166 static boolean
delete_sampler_state(UNUSED
struct cso_context
*ctx
, void *state
)
168 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
169 if (cso
->delete_state
)
170 cso
->delete_state(cso
->context
, cso
->data
);
175 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
177 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
179 if (ctx
->rasterizer
== cso
->data
)
181 if (cso
->delete_state
)
182 cso
->delete_state(cso
->context
, cso
->data
);
187 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
190 struct cso_velements
*cso
= (struct cso_velements
*)state
;
192 if (ctx
->velements
== cso
->data
)
195 if (cso
->delete_state
)
196 cso
->delete_state(cso
->context
, cso
->data
);
202 static inline boolean
delete_cso(struct cso_context
*ctx
,
203 void *state
, enum cso_cache_type type
)
207 return delete_blend_state(ctx
, state
);
209 return delete_sampler_state(ctx
, state
);
210 case CSO_DEPTH_STENCIL_ALPHA
:
211 return delete_depth_stencil_state(ctx
, state
);
213 return delete_rasterizer_state(ctx
, state
);
215 return delete_vertex_elements(ctx
, state
);
224 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
225 int max_size
, void *user_data
)
227 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
228 /* if we're approach the maximum size, remove fourth of the entries
229 * otherwise every subsequent call will go through the same */
230 int hash_size
= cso_hash_size(hash
);
231 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
232 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
233 struct cso_hash_iter iter
;
234 struct cso_sampler
**samplers_to_restore
= NULL
;
235 unsigned to_restore
= 0;
237 if (hash_size
> max_size
)
238 to_remove
+= hash_size
- max_size
;
243 if (type
== CSO_SAMPLER
) {
246 samplers_to_restore
= MALLOC(PIPE_SHADER_TYPES
* PIPE_MAX_SAMPLERS
*
247 sizeof(*samplers_to_restore
));
249 /* Temporarily remove currently bound sampler states from the hash
250 * table, to prevent them from being deleted
252 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
253 for (j
= 0; j
< PIPE_MAX_SAMPLERS
; j
++) {
254 struct cso_sampler
*sampler
= ctx
->samplers
[i
].cso_samplers
[j
];
256 if (sampler
&& cso_hash_take(hash
, sampler
->hash_key
))
257 samplers_to_restore
[to_restore
++] = sampler
;
262 iter
= cso_hash_first_node(hash
);
264 /*remove elements until we're good */
265 /*fixme: currently we pick the nodes to remove at random*/
266 void *cso
= cso_hash_iter_data(iter
);
271 if (delete_cso(ctx
, cso
, type
)) {
272 iter
= cso_hash_erase(hash
, iter
);
275 iter
= cso_hash_iter_next(iter
);
278 if (type
== CSO_SAMPLER
) {
279 /* Put currently bound sampler states back into the hash table */
280 while (to_restore
--) {
281 struct cso_sampler
*sampler
= samplers_to_restore
[to_restore
];
283 cso_hash_insert(hash
, sampler
->hash_key
, sampler
);
286 FREE(samplers_to_restore
);
290 static void cso_init_vbuf(struct cso_context
*cso
, unsigned flags
)
292 struct u_vbuf_caps caps
;
293 bool uses_user_vertex_buffers
= !(flags
& CSO_NO_USER_VERTEX_BUFFERS
);
295 u_vbuf_get_caps(cso
->pipe
->screen
, &caps
);
297 /* Enable u_vbuf if needed. */
298 if (caps
.fallback_always
||
299 (uses_user_vertex_buffers
&&
300 caps
.fallback_only_for_user_vbuffers
)) {
301 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
);
302 cso
->vbuf_current
= cso
->vbuf
;
303 cso
->always_use_vbuf
= caps
.fallback_always
;
308 cso_create_context(struct pipe_context
*pipe
, unsigned flags
)
310 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
314 ctx
->cache
= cso_cache_create();
315 if (ctx
->cache
== NULL
)
317 cso_cache_set_sanitize_callback(ctx
->cache
,
322 ctx
->sample_mask
= ~0;
324 cso_init_vbuf(ctx
, flags
);
326 /* Enable for testing: */
327 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
329 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
330 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
331 ctx
->has_geometry_shader
= TRUE
;
333 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
334 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
335 ctx
->has_tessellation
= TRUE
;
337 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
338 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
340 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
341 PIPE_SHADER_CAP_SUPPORTED_IRS
);
342 if (supported_irs
& ((1 << PIPE_SHADER_IR_TGSI
) |
343 (1 << PIPE_SHADER_IR_NIR
))) {
344 ctx
->has_compute_shader
= TRUE
;
347 if (pipe
->screen
->get_param(pipe
->screen
,
348 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
349 ctx
->has_streamout
= TRUE
;
352 ctx
->max_sampler_seen
= -1;
356 cso_destroy_context( ctx
);
361 * Free the CSO context.
363 void cso_destroy_context( struct cso_context
*ctx
)
368 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
369 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
372 static struct pipe_sampler_view
*views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
] = { NULL
};
373 static void *zeros
[PIPE_MAX_SAMPLERS
] = { NULL
};
374 struct pipe_screen
*scr
= ctx
->pipe
->screen
;
375 enum pipe_shader_type sh
;
376 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
377 int maxsam
= scr
->get_shader_param(scr
, sh
,
378 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
);
379 int maxview
= scr
->get_shader_param(scr
, sh
,
380 PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS
);
381 assert(maxsam
<= PIPE_MAX_SAMPLERS
);
382 assert(maxview
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
384 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, sh
, 0, maxsam
, zeros
);
387 ctx
->pipe
->set_sampler_views(ctx
->pipe
, sh
, 0, maxview
, views
);
392 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
393 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
394 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, NULL
);
395 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
396 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_VERTEX
, 0, NULL
);
397 if (ctx
->has_geometry_shader
) {
398 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
399 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_GEOMETRY
, 0, NULL
);
401 if (ctx
->has_tessellation
) {
402 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
403 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_CTRL
, 0, NULL
);
404 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
405 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_EVAL
, 0, NULL
);
407 if (ctx
->has_compute_shader
) {
408 ctx
->pipe
->bind_compute_state(ctx
->pipe
, NULL
);
409 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_COMPUTE
, 0, NULL
);
411 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
413 if (ctx
->has_streamout
)
414 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, NULL
);
417 for (i
= 0; i
< ctx
->nr_fragment_views
; i
++) {
418 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
420 for (i
= 0; i
< ctx
->nr_fragment_views_saved
; i
++) {
421 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
], NULL
);
424 util_unreference_framebuffer_state(&ctx
->fb
);
425 util_unreference_framebuffer_state(&ctx
->fb_saved
);
427 pipe_vertex_buffer_unreference(&ctx
->vertex_buffer0_current
);
428 pipe_vertex_buffer_unreference(&ctx
->vertex_buffer0_saved
);
430 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
431 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
432 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
435 pipe_resource_reference(&ctx
->fragment_image0_current
.resource
, NULL
);
436 pipe_resource_reference(&ctx
->fragment_image0_saved
.resource
, NULL
);
438 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
439 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
440 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
444 cso_cache_delete( ctx
->cache
);
449 u_vbuf_destroy(ctx
->vbuf
);
454 /* Those function will either find the state of the given template
455 * in the cache or they will create a new state from the given
456 * template, insert it in the cache and return it.
460 * If the driver returns 0 from the create method then they will assign
461 * the data member of the cso to be the template itself.
464 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
465 const struct pipe_blend_state
*templ
)
467 unsigned key_size
, hash_key
;
468 struct cso_hash_iter iter
;
471 key_size
= templ
->independent_blend_enable
?
472 sizeof(struct pipe_blend_state
) :
473 (char *)&(templ
->rt
[1]) - (char *)templ
;
474 hash_key
= cso_construct_key((void*)templ
, key_size
);
475 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
476 (void*)templ
, key_size
);
478 if (cso_hash_iter_is_null(iter
)) {
479 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
481 return PIPE_ERROR_OUT_OF_MEMORY
;
483 memset(&cso
->state
, 0, sizeof cso
->state
);
484 memcpy(&cso
->state
, templ
, key_size
);
485 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
486 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
487 cso
->context
= ctx
->pipe
;
489 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
490 if (cso_hash_iter_is_null(iter
)) {
492 return PIPE_ERROR_OUT_OF_MEMORY
;
498 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
501 if (ctx
->blend
!= handle
) {
503 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
509 cso_save_blend(struct cso_context
*ctx
)
511 assert(!ctx
->blend_saved
);
512 ctx
->blend_saved
= ctx
->blend
;
516 cso_restore_blend(struct cso_context
*ctx
)
518 if (ctx
->blend
!= ctx
->blend_saved
) {
519 ctx
->blend
= ctx
->blend_saved
;
520 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
522 ctx
->blend_saved
= NULL
;
528 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
529 const struct pipe_depth_stencil_alpha_state
*templ
)
531 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
532 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
533 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
535 CSO_DEPTH_STENCIL_ALPHA
,
536 (void*)templ
, key_size
);
539 if (cso_hash_iter_is_null(iter
)) {
540 struct cso_depth_stencil_alpha
*cso
=
541 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
543 return PIPE_ERROR_OUT_OF_MEMORY
;
545 memcpy(&cso
->state
, templ
, sizeof(*templ
));
546 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
549 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
550 cso
->context
= ctx
->pipe
;
552 iter
= cso_insert_state(ctx
->cache
, hash_key
,
553 CSO_DEPTH_STENCIL_ALPHA
, cso
);
554 if (cso_hash_iter_is_null(iter
)) {
556 return PIPE_ERROR_OUT_OF_MEMORY
;
562 handle
= ((struct cso_depth_stencil_alpha
*)
563 cso_hash_iter_data(iter
))->data
;
566 if (ctx
->depth_stencil
!= handle
) {
567 ctx
->depth_stencil
= handle
;
568 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
574 cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
576 assert(!ctx
->depth_stencil_saved
);
577 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
581 cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
583 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
584 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
585 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
586 ctx
->depth_stencil_saved
);
588 ctx
->depth_stencil_saved
= NULL
;
593 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
594 const struct pipe_rasterizer_state
*templ
)
596 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
597 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
598 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
601 (void*)templ
, key_size
);
604 /* We can't have both point_quad_rasterization (sprites) and point_smooth
605 * (round AA points) enabled at the same time.
607 assert(!(templ
->point_quad_rasterization
&& templ
->point_smooth
));
609 if (cso_hash_iter_is_null(iter
)) {
610 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
612 return PIPE_ERROR_OUT_OF_MEMORY
;
614 memcpy(&cso
->state
, templ
, sizeof(*templ
));
615 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
617 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
618 cso
->context
= ctx
->pipe
;
620 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
621 if (cso_hash_iter_is_null(iter
)) {
623 return PIPE_ERROR_OUT_OF_MEMORY
;
629 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
632 if (ctx
->rasterizer
!= handle
) {
633 ctx
->rasterizer
= handle
;
634 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
640 cso_save_rasterizer(struct cso_context
*ctx
)
642 assert(!ctx
->rasterizer_saved
);
643 ctx
->rasterizer_saved
= ctx
->rasterizer
;
647 cso_restore_rasterizer(struct cso_context
*ctx
)
649 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
650 ctx
->rasterizer
= ctx
->rasterizer_saved
;
651 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
653 ctx
->rasterizer_saved
= NULL
;
657 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
659 if (ctx
->fragment_shader
!= handle
) {
660 ctx
->fragment_shader
= handle
;
661 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
666 cso_save_fragment_shader(struct cso_context
*ctx
)
668 assert(!ctx
->fragment_shader_saved
);
669 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
673 cso_restore_fragment_shader(struct cso_context
*ctx
)
675 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
676 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
677 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
679 ctx
->fragment_shader_saved
= NULL
;
683 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
685 if (ctx
->vertex_shader
!= handle
) {
686 ctx
->vertex_shader
= handle
;
687 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
692 cso_save_vertex_shader(struct cso_context
*ctx
)
694 assert(!ctx
->vertex_shader_saved
);
695 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
699 cso_restore_vertex_shader(struct cso_context
*ctx
)
701 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
702 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
703 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
705 ctx
->vertex_shader_saved
= NULL
;
709 void cso_set_framebuffer(struct cso_context
*ctx
,
710 const struct pipe_framebuffer_state
*fb
)
712 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
713 util_copy_framebuffer_state(&ctx
->fb
, fb
);
714 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
719 cso_save_framebuffer(struct cso_context
*ctx
)
721 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
725 cso_restore_framebuffer(struct cso_context
*ctx
)
727 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
728 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
729 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
730 util_unreference_framebuffer_state(&ctx
->fb_saved
);
735 void cso_set_viewport(struct cso_context
*ctx
,
736 const struct pipe_viewport_state
*vp
)
738 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
740 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
745 * Setup viewport state for given width and height (position is always (0,0)).
746 * Invert the Y axis if 'invert' is true.
749 cso_set_viewport_dims(struct cso_context
*ctx
,
750 float width
, float height
, boolean invert
)
752 struct pipe_viewport_state vp
;
753 vp
.scale
[0] = width
* 0.5f
;
754 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
756 vp
.translate
[0] = 0.5f
* width
;
757 vp
.translate
[1] = 0.5f
* height
;
758 vp
.translate
[2] = 0.5f
;
759 vp
.swizzle_x
= PIPE_VIEWPORT_SWIZZLE_POSITIVE_X
;
760 vp
.swizzle_y
= PIPE_VIEWPORT_SWIZZLE_POSITIVE_Y
;
761 vp
.swizzle_z
= PIPE_VIEWPORT_SWIZZLE_POSITIVE_Z
;
762 vp
.swizzle_w
= PIPE_VIEWPORT_SWIZZLE_POSITIVE_W
;
763 cso_set_viewport(ctx
, &vp
);
767 cso_save_viewport(struct cso_context
*ctx
)
769 ctx
->vp_saved
= ctx
->vp
;
774 cso_restore_viewport(struct cso_context
*ctx
)
776 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
777 ctx
->vp
= ctx
->vp_saved
;
778 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
783 void cso_set_blend_color(struct cso_context
*ctx
,
784 const struct pipe_blend_color
*bc
)
786 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
787 ctx
->blend_color
= *bc
;
788 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
792 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
794 if (ctx
->sample_mask
!= sample_mask
) {
795 ctx
->sample_mask
= sample_mask
;
796 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
801 cso_save_sample_mask(struct cso_context
*ctx
)
803 ctx
->sample_mask_saved
= ctx
->sample_mask
;
807 cso_restore_sample_mask(struct cso_context
*ctx
)
809 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
812 void cso_set_min_samples(struct cso_context
*ctx
, unsigned min_samples
)
814 if (ctx
->min_samples
!= min_samples
&& ctx
->pipe
->set_min_samples
) {
815 ctx
->min_samples
= min_samples
;
816 ctx
->pipe
->set_min_samples(ctx
->pipe
, min_samples
);
821 cso_save_min_samples(struct cso_context
*ctx
)
823 ctx
->min_samples_saved
= ctx
->min_samples
;
827 cso_restore_min_samples(struct cso_context
*ctx
)
829 cso_set_min_samples(ctx
, ctx
->min_samples_saved
);
832 void cso_set_stencil_ref(struct cso_context
*ctx
,
833 const struct pipe_stencil_ref
*sr
)
835 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
836 ctx
->stencil_ref
= *sr
;
837 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
842 cso_save_stencil_ref(struct cso_context
*ctx
)
844 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
849 cso_restore_stencil_ref(struct cso_context
*ctx
)
851 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
852 sizeof(ctx
->stencil_ref
))) {
853 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
854 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
858 void cso_set_render_condition(struct cso_context
*ctx
,
859 struct pipe_query
*query
,
861 enum pipe_render_cond_flag mode
)
863 struct pipe_context
*pipe
= ctx
->pipe
;
865 if (ctx
->render_condition
!= query
||
866 ctx
->render_condition_mode
!= mode
||
867 ctx
->render_condition_cond
!= condition
) {
868 pipe
->render_condition(pipe
, query
, condition
, mode
);
869 ctx
->render_condition
= query
;
870 ctx
->render_condition_cond
= condition
;
871 ctx
->render_condition_mode
= mode
;
876 cso_save_render_condition(struct cso_context
*ctx
)
878 ctx
->render_condition_saved
= ctx
->render_condition
;
879 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
880 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
884 cso_restore_render_condition(struct cso_context
*ctx
)
886 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
887 ctx
->render_condition_cond_saved
,
888 ctx
->render_condition_mode_saved
);
891 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
893 assert(ctx
->has_geometry_shader
|| !handle
);
895 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
896 ctx
->geometry_shader
= handle
;
897 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
902 cso_save_geometry_shader(struct cso_context
*ctx
)
904 if (!ctx
->has_geometry_shader
) {
908 assert(!ctx
->geometry_shader_saved
);
909 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
913 cso_restore_geometry_shader(struct cso_context
*ctx
)
915 if (!ctx
->has_geometry_shader
) {
919 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
920 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
921 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
923 ctx
->geometry_shader_saved
= NULL
;
926 void cso_set_tessctrl_shader_handle(struct cso_context
*ctx
, void *handle
)
928 assert(ctx
->has_tessellation
|| !handle
);
930 if (ctx
->has_tessellation
&& ctx
->tessctrl_shader
!= handle
) {
931 ctx
->tessctrl_shader
= handle
;
932 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, handle
);
937 cso_save_tessctrl_shader(struct cso_context
*ctx
)
939 if (!ctx
->has_tessellation
) {
943 assert(!ctx
->tessctrl_shader_saved
);
944 ctx
->tessctrl_shader_saved
= ctx
->tessctrl_shader
;
948 cso_restore_tessctrl_shader(struct cso_context
*ctx
)
950 if (!ctx
->has_tessellation
) {
954 if (ctx
->tessctrl_shader_saved
!= ctx
->tessctrl_shader
) {
955 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, ctx
->tessctrl_shader_saved
);
956 ctx
->tessctrl_shader
= ctx
->tessctrl_shader_saved
;
958 ctx
->tessctrl_shader_saved
= NULL
;
961 void cso_set_tesseval_shader_handle(struct cso_context
*ctx
, void *handle
)
963 assert(ctx
->has_tessellation
|| !handle
);
965 if (ctx
->has_tessellation
&& ctx
->tesseval_shader
!= handle
) {
966 ctx
->tesseval_shader
= handle
;
967 ctx
->pipe
->bind_tes_state(ctx
->pipe
, handle
);
972 cso_save_tesseval_shader(struct cso_context
*ctx
)
974 if (!ctx
->has_tessellation
) {
978 assert(!ctx
->tesseval_shader_saved
);
979 ctx
->tesseval_shader_saved
= ctx
->tesseval_shader
;
983 cso_restore_tesseval_shader(struct cso_context
*ctx
)
985 if (!ctx
->has_tessellation
) {
989 if (ctx
->tesseval_shader_saved
!= ctx
->tesseval_shader
) {
990 ctx
->pipe
->bind_tes_state(ctx
->pipe
, ctx
->tesseval_shader_saved
);
991 ctx
->tesseval_shader
= ctx
->tesseval_shader_saved
;
993 ctx
->tesseval_shader_saved
= NULL
;
996 void cso_set_compute_shader_handle(struct cso_context
*ctx
, void *handle
)
998 assert(ctx
->has_compute_shader
|| !handle
);
1000 if (ctx
->has_compute_shader
&& ctx
->compute_shader
!= handle
) {
1001 ctx
->compute_shader
= handle
;
1002 ctx
->pipe
->bind_compute_state(ctx
->pipe
, handle
);
1007 cso_set_vertex_elements_direct(struct cso_context
*ctx
,
1008 const struct cso_velems_state
*velems
)
1010 unsigned key_size
, hash_key
;
1011 struct cso_hash_iter iter
;
1014 /* Need to include the count into the stored state data too.
1015 * Otherwise first few count pipe_vertex_elements could be identical
1016 * even if count is different, and there's no guarantee the hash would
1017 * be different in that case neither.
1019 key_size
= sizeof(struct pipe_vertex_element
) * velems
->count
+
1021 hash_key
= cso_construct_key((void*)velems
, key_size
);
1022 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
1023 (void*)velems
, key_size
);
1025 if (cso_hash_iter_is_null(iter
)) {
1026 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1030 memcpy(&cso
->state
, velems
, key_size
);
1031 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
,
1033 &cso
->state
.velems
[0]);
1035 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
1036 cso
->context
= ctx
->pipe
;
1038 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1039 if (cso_hash_iter_is_null(iter
)) {
1047 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1050 if (ctx
->velements
!= handle
) {
1051 ctx
->velements
= handle
;
1052 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1057 cso_set_vertex_elements(struct cso_context
*ctx
,
1058 const struct cso_velems_state
*velems
)
1060 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1063 u_vbuf_set_vertex_elements(vbuf
, velems
);
1067 cso_set_vertex_elements_direct(ctx
, velems
);
1072 cso_save_vertex_elements(struct cso_context
*ctx
)
1074 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1077 u_vbuf_save_vertex_elements(vbuf
);
1081 assert(!ctx
->velements_saved
);
1082 ctx
->velements_saved
= ctx
->velements
;
1086 cso_restore_vertex_elements(struct cso_context
*ctx
)
1088 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1091 u_vbuf_restore_vertex_elements(vbuf
);
1095 if (ctx
->velements
!= ctx
->velements_saved
) {
1096 ctx
->velements
= ctx
->velements_saved
;
1097 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1099 ctx
->velements_saved
= NULL
;
1102 /* vertex buffers */
1105 cso_set_vertex_buffers_direct(struct cso_context
*ctx
,
1106 unsigned start_slot
, unsigned count
,
1107 const struct pipe_vertex_buffer
*buffers
)
1109 /* Save what's in the auxiliary slot, so that we can save and restore it
1112 if (start_slot
== 0) {
1114 pipe_vertex_buffer_reference(&ctx
->vertex_buffer0_current
,
1117 pipe_vertex_buffer_unreference(&ctx
->vertex_buffer0_current
);
1121 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
1125 void cso_set_vertex_buffers(struct cso_context
*ctx
,
1126 unsigned start_slot
, unsigned count
,
1127 const struct pipe_vertex_buffer
*buffers
)
1129 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1135 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
1139 cso_set_vertex_buffers_direct(ctx
, start_slot
, count
, buffers
);
1143 cso_save_vertex_buffer0(struct cso_context
*ctx
)
1145 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1148 u_vbuf_save_vertex_buffer0(vbuf
);
1152 pipe_vertex_buffer_reference(&ctx
->vertex_buffer0_saved
,
1153 &ctx
->vertex_buffer0_current
);
1157 cso_restore_vertex_buffer0(struct cso_context
*ctx
)
1159 struct u_vbuf
*vbuf
= ctx
->vbuf_current
;
1162 u_vbuf_restore_vertex_buffer0(vbuf
);
1166 cso_set_vertex_buffers(ctx
, 0, 1, &ctx
->vertex_buffer0_saved
);
1167 pipe_vertex_buffer_unreference(&ctx
->vertex_buffer0_saved
);
1171 * Set vertex buffers and vertex elements. Skip u_vbuf if it's only needed
1172 * for user vertex buffers and user vertex buffers are not set by this call.
1173 * u_vbuf will be disabled. To re-enable u_vbuf, call this function again.
1175 * Skipping u_vbuf decreases CPU overhead for draw calls that don't need it,
1176 * such as VBOs, glBegin/End, and display lists.
1178 * Internal operations that do "save states, draw, restore states" shouldn't
1179 * use this, because the states are only saved in either cso_context or
1183 cso_set_vertex_buffers_and_elements(struct cso_context
*ctx
,
1184 const struct cso_velems_state
*velems
,
1186 unsigned unbind_trailing_vb_count
,
1187 const struct pipe_vertex_buffer
*vbuffers
,
1188 bool uses_user_vertex_buffers
)
1190 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1192 if (vbuf
&& (ctx
->always_use_vbuf
|| uses_user_vertex_buffers
)) {
1193 if (!ctx
->vbuf_current
) {
1194 /* Unbind all buffers in cso_context, because we'll use u_vbuf. */
1195 unsigned unbind_vb_count
= vb_count
+ unbind_trailing_vb_count
;
1196 if (unbind_vb_count
)
1197 cso_set_vertex_buffers_direct(ctx
, 0, unbind_vb_count
, NULL
);
1199 /* Unset this to make sure the CSO is re-bound on the next use. */
1200 ctx
->velements
= NULL
;
1201 ctx
->vbuf_current
= vbuf
;
1202 } else if (unbind_trailing_vb_count
) {
1203 u_vbuf_set_vertex_buffers(vbuf
, vb_count
, unbind_trailing_vb_count
,
1208 u_vbuf_set_vertex_buffers(vbuf
, 0, vb_count
, vbuffers
);
1209 u_vbuf_set_vertex_elements(vbuf
, velems
);
1213 if (ctx
->vbuf_current
) {
1214 /* Unbind all buffers in u_vbuf, because we'll use cso_context. */
1215 unsigned unbind_vb_count
= vb_count
+ unbind_trailing_vb_count
;
1216 if (unbind_vb_count
)
1217 u_vbuf_set_vertex_buffers(vbuf
, 0, unbind_vb_count
, NULL
);
1219 /* Unset this to make sure the CSO is re-bound on the next use. */
1220 u_vbuf_unset_vertex_elements(vbuf
);
1221 ctx
->vbuf_current
= NULL
;
1222 } else if (unbind_trailing_vb_count
) {
1223 cso_set_vertex_buffers_direct(ctx
, vb_count
, unbind_trailing_vb_count
,
1228 cso_set_vertex_buffers_direct(ctx
, 0, vb_count
, vbuffers
);
1229 cso_set_vertex_elements_direct(ctx
, velems
);
1233 cso_single_sampler(struct cso_context
*ctx
, enum pipe_shader_type shader_stage
,
1234 unsigned idx
, const struct pipe_sampler_state
*templ
)
1237 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1238 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1239 struct cso_sampler
*cso
;
1240 struct cso_hash_iter iter
=
1241 cso_find_state_template(ctx
->cache
,
1242 hash_key
, CSO_SAMPLER
,
1243 (void *) templ
, key_size
);
1245 if (cso_hash_iter_is_null(iter
)) {
1246 cso
= MALLOC(sizeof(struct cso_sampler
));
1250 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1251 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1253 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1254 cso
->context
= ctx
->pipe
;
1255 cso
->hash_key
= hash_key
;
1257 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1258 if (cso_hash_iter_is_null(iter
)) {
1264 cso
= cso_hash_iter_data(iter
);
1267 ctx
->samplers
[shader_stage
].cso_samplers
[idx
] = cso
;
1268 ctx
->samplers
[shader_stage
].samplers
[idx
] = cso
->data
;
1269 ctx
->max_sampler_seen
= MAX2(ctx
->max_sampler_seen
, (int)idx
);
1275 * Send staged sampler state to the driver.
1278 cso_single_sampler_done(struct cso_context
*ctx
,
1279 enum pipe_shader_type shader_stage
)
1281 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1283 if (ctx
->max_sampler_seen
== -1)
1286 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1287 ctx
->max_sampler_seen
+ 1,
1289 ctx
->max_sampler_seen
= -1;
1294 * If the function encouters any errors it will return the
1295 * last one. Done to always try to set as many samplers
1299 cso_set_samplers(struct cso_context
*ctx
,
1300 enum pipe_shader_type shader_stage
,
1302 const struct pipe_sampler_state
**templates
)
1304 for (unsigned i
= 0; i
< nr
; i
++)
1305 cso_single_sampler(ctx
, shader_stage
, i
, templates
[i
]);
1307 cso_single_sampler_done(ctx
, shader_stage
);
1311 cso_save_fragment_samplers(struct cso_context
*ctx
)
1313 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1314 struct sampler_info
*saved
= &ctx
->fragment_samplers_saved
;
1316 memcpy(saved
->cso_samplers
, info
->cso_samplers
,
1317 sizeof(info
->cso_samplers
));
1318 memcpy(saved
->samplers
, info
->samplers
, sizeof(info
->samplers
));
1323 cso_restore_fragment_samplers(struct cso_context
*ctx
)
1325 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1326 struct sampler_info
*saved
= &ctx
->fragment_samplers_saved
;
1328 memcpy(info
->cso_samplers
, saved
->cso_samplers
,
1329 sizeof(info
->cso_samplers
));
1330 memcpy(info
->samplers
, saved
->samplers
, sizeof(info
->samplers
));
1332 for (int i
= PIPE_MAX_SAMPLERS
- 1; i
>= 0; i
--) {
1333 if (info
->samplers
[i
]) {
1334 ctx
->max_sampler_seen
= i
;
1339 cso_single_sampler_done(ctx
, PIPE_SHADER_FRAGMENT
);
1344 cso_set_sampler_views(struct cso_context
*ctx
,
1345 enum pipe_shader_type shader_stage
,
1347 struct pipe_sampler_view
**views
)
1349 if (shader_stage
== PIPE_SHADER_FRAGMENT
) {
1351 boolean any_change
= FALSE
;
1353 /* reference new views */
1354 for (i
= 0; i
< count
; i
++) {
1355 any_change
|= ctx
->fragment_views
[i
] != views
[i
];
1356 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], views
[i
]);
1358 /* unref extra old views, if any */
1359 for (; i
< ctx
->nr_fragment_views
; i
++) {
1360 any_change
|= ctx
->fragment_views
[i
] != NULL
;
1361 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1364 /* bind the new sampler views */
1366 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0,
1367 MAX2(ctx
->nr_fragment_views
, count
),
1368 ctx
->fragment_views
);
1371 ctx
->nr_fragment_views
= count
;
1374 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, count
, views
);
1379 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1383 ctx
->nr_fragment_views_saved
= ctx
->nr_fragment_views
;
1385 for (i
= 0; i
< ctx
->nr_fragment_views
; i
++) {
1386 assert(!ctx
->fragment_views_saved
[i
]);
1387 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
],
1388 ctx
->fragment_views
[i
]);
1394 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1396 unsigned i
, nr_saved
= ctx
->nr_fragment_views_saved
;
1399 for (i
= 0; i
< nr_saved
; i
++) {
1400 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1401 /* move the reference from one pointer to another */
1402 ctx
->fragment_views
[i
] = ctx
->fragment_views_saved
[i
];
1403 ctx
->fragment_views_saved
[i
] = NULL
;
1405 for (; i
< ctx
->nr_fragment_views
; i
++) {
1406 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1409 num
= MAX2(ctx
->nr_fragment_views
, nr_saved
);
1411 /* bind the old/saved sampler views */
1412 ctx
->pipe
->set_sampler_views(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, num
,
1413 ctx
->fragment_views
);
1415 ctx
->nr_fragment_views
= nr_saved
;
1416 ctx
->nr_fragment_views_saved
= 0;
1421 cso_set_shader_images(struct cso_context
*ctx
,
1422 enum pipe_shader_type shader_stage
,
1423 unsigned start
, unsigned count
,
1424 struct pipe_image_view
*images
)
1426 if (shader_stage
== PIPE_SHADER_FRAGMENT
&& start
== 0 && count
>= 1) {
1427 util_copy_image_view(&ctx
->fragment_image0_current
, &images
[0]);
1430 ctx
->pipe
->set_shader_images(ctx
->pipe
, shader_stage
, start
, count
, images
);
1435 cso_save_fragment_image0(struct cso_context
*ctx
)
1437 util_copy_image_view(&ctx
->fragment_image0_saved
,
1438 &ctx
->fragment_image0_current
);
1443 cso_restore_fragment_image0(struct cso_context
*ctx
)
1445 cso_set_shader_images(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1,
1446 &ctx
->fragment_image0_saved
);
1451 cso_set_stream_outputs(struct cso_context
*ctx
,
1452 unsigned num_targets
,
1453 struct pipe_stream_output_target
**targets
,
1454 const unsigned *offsets
)
1456 struct pipe_context
*pipe
= ctx
->pipe
;
1459 if (!ctx
->has_streamout
) {
1460 assert(num_targets
== 0);
1464 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1465 /* Nothing to do. */
1469 /* reference new targets */
1470 for (i
= 0; i
< num_targets
; i
++) {
1471 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1473 /* unref extra old targets, if any */
1474 for (; i
< ctx
->nr_so_targets
; i
++) {
1475 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1478 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1480 ctx
->nr_so_targets
= num_targets
;
1484 cso_save_stream_outputs(struct cso_context
*ctx
)
1488 if (!ctx
->has_streamout
) {
1492 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1494 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1495 assert(!ctx
->so_targets_saved
[i
]);
1496 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1501 cso_restore_stream_outputs(struct cso_context
*ctx
)
1503 struct pipe_context
*pipe
= ctx
->pipe
;
1505 unsigned offset
[PIPE_MAX_SO_BUFFERS
];
1507 if (!ctx
->has_streamout
) {
1511 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1512 /* Nothing to do. */
1516 assert(ctx
->nr_so_targets_saved
<= PIPE_MAX_SO_BUFFERS
);
1517 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1518 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1519 /* move the reference from one pointer to another */
1520 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1521 ctx
->so_targets_saved
[i
] = NULL
;
1522 /* -1 means append */
1523 offset
[i
] = (unsigned)-1;
1525 for (; i
< ctx
->nr_so_targets
; i
++) {
1526 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1529 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1530 ctx
->so_targets
, offset
);
1532 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1533 ctx
->nr_so_targets_saved
= 0;
1536 /* constant buffers */
1539 cso_set_constant_buffer(struct cso_context
*cso
,
1540 enum pipe_shader_type shader_stage
,
1541 unsigned index
, struct pipe_constant_buffer
*cb
)
1543 struct pipe_context
*pipe
= cso
->pipe
;
1545 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1548 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1553 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1554 enum pipe_shader_type shader_stage
,
1556 struct pipe_resource
*buffer
)
1559 struct pipe_constant_buffer cb
;
1561 cb
.buffer_offset
= 0;
1562 cb
.buffer_size
= buffer
->width0
;
1563 cb
.user_buffer
= NULL
;
1564 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1566 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1571 cso_set_constant_user_buffer(struct cso_context
*cso
,
1572 enum pipe_shader_type shader_stage
,
1573 unsigned index
, void *ptr
, unsigned size
)
1576 struct pipe_constant_buffer cb
;
1578 cb
.buffer_offset
= 0;
1579 cb
.buffer_size
= size
;
1580 cb
.user_buffer
= ptr
;
1581 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1583 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1588 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1589 enum pipe_shader_type shader_stage
)
1591 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1592 &cso
->aux_constbuf_current
[shader_stage
]);
1596 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1597 enum pipe_shader_type shader_stage
)
1599 cso_set_constant_buffer(cso
, shader_stage
, 0,
1600 &cso
->aux_constbuf_saved
[shader_stage
]);
1601 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1607 * Save all the CSO state items specified by the state_mask bitmask
1608 * of CSO_BIT_x flags.
1611 cso_save_state(struct cso_context
*cso
, unsigned state_mask
)
1613 assert(cso
->saved_state
== 0);
1615 cso
->saved_state
= state_mask
;
1617 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1618 cso_save_vertex_buffer0(cso
);
1619 if (state_mask
& CSO_BIT_BLEND
)
1620 cso_save_blend(cso
);
1621 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1622 cso_save_depth_stencil_alpha(cso
);
1623 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1624 cso_save_fragment_samplers(cso
);
1625 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1626 cso_save_fragment_sampler_views(cso
);
1627 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1628 cso_save_fragment_shader(cso
);
1629 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1630 cso_save_framebuffer(cso
);
1631 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1632 cso_save_geometry_shader(cso
);
1633 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1634 cso_save_min_samples(cso
);
1635 if (state_mask
& CSO_BIT_RASTERIZER
)
1636 cso_save_rasterizer(cso
);
1637 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1638 cso_save_render_condition(cso
);
1639 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1640 cso_save_sample_mask(cso
);
1641 if (state_mask
& CSO_BIT_STENCIL_REF
)
1642 cso_save_stencil_ref(cso
);
1643 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1644 cso_save_stream_outputs(cso
);
1645 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1646 cso_save_tessctrl_shader(cso
);
1647 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1648 cso_save_tesseval_shader(cso
);
1649 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1650 cso_save_vertex_elements(cso
);
1651 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1652 cso_save_vertex_shader(cso
);
1653 if (state_mask
& CSO_BIT_VIEWPORT
)
1654 cso_save_viewport(cso
);
1655 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1656 cso
->pipe
->set_active_query_state(cso
->pipe
, false);
1657 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1658 cso_save_fragment_image0(cso
);
1663 * Restore the state which was saved by cso_save_state().
1666 cso_restore_state(struct cso_context
*cso
)
1668 unsigned state_mask
= cso
->saved_state
;
1672 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1673 cso_restore_vertex_buffer0(cso
);
1674 if (state_mask
& CSO_BIT_BLEND
)
1675 cso_restore_blend(cso
);
1676 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1677 cso_restore_depth_stencil_alpha(cso
);
1678 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1679 cso_restore_fragment_samplers(cso
);
1680 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1681 cso_restore_fragment_sampler_views(cso
);
1682 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1683 cso_restore_fragment_shader(cso
);
1684 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1685 cso_restore_framebuffer(cso
);
1686 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1687 cso_restore_geometry_shader(cso
);
1688 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1689 cso_restore_min_samples(cso
);
1690 if (state_mask
& CSO_BIT_RASTERIZER
)
1691 cso_restore_rasterizer(cso
);
1692 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1693 cso_restore_render_condition(cso
);
1694 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1695 cso_restore_sample_mask(cso
);
1696 if (state_mask
& CSO_BIT_STENCIL_REF
)
1697 cso_restore_stencil_ref(cso
);
1698 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1699 cso_restore_stream_outputs(cso
);
1700 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1701 cso_restore_tessctrl_shader(cso
);
1702 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1703 cso_restore_tesseval_shader(cso
);
1704 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1705 cso_restore_vertex_elements(cso
);
1706 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1707 cso_restore_vertex_shader(cso
);
1708 if (state_mask
& CSO_BIT_VIEWPORT
)
1709 cso_restore_viewport(cso
);
1710 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1711 cso
->pipe
->set_active_query_state(cso
->pipe
, true);
1712 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1713 cso_restore_fragment_image0(cso
);
1715 cso
->saved_state
= 0;
1723 cso_draw_vbo(struct cso_context
*cso
,
1724 const struct pipe_draw_info
*info
)
1726 struct u_vbuf
*vbuf
= cso
->vbuf_current
;
1728 /* We can't have both indirect drawing and SO-vertex-count drawing */
1729 assert(info
->indirect
== NULL
|| info
->count_from_stream_output
== NULL
);
1731 /* We can't have SO-vertex-count drawing with an index buffer */
1732 assert(info
->count_from_stream_output
== NULL
|| info
->index_size
== 0);
1735 u_vbuf_draw_vbo(vbuf
, info
);
1737 struct pipe_context
*pipe
= cso
->pipe
;
1738 pipe
->draw_vbo(pipe
, info
);
1743 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1745 struct pipe_draw_info info
;
1747 util_draw_init_info(&info
);
1752 info
.min_index
= start
;
1753 info
.max_index
= start
+ count
- 1;
1755 cso_draw_vbo(cso
, &info
);
1759 cso_draw_arrays_instanced(struct cso_context
*cso
, uint mode
,
1760 uint start
, uint count
,
1761 uint start_instance
, uint instance_count
)
1763 struct pipe_draw_info info
;
1765 util_draw_init_info(&info
);
1770 info
.min_index
= start
;
1771 info
.max_index
= start
+ count
- 1;
1772 info
.start_instance
= start_instance
;
1773 info
.instance_count
= instance_count
;
1775 cso_draw_vbo(cso
, &info
);