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 boolean has_geometry_shader
;
70 boolean has_tessellation
;
71 boolean has_compute_shader
;
72 boolean has_streamout
;
74 unsigned saved_state
; /**< bitmask of CSO_BIT_x flags */
76 struct pipe_sampler_view
*fragment_views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
77 unsigned nr_fragment_views
;
79 struct pipe_sampler_view
*fragment_views_saved
[PIPE_MAX_SHADER_SAMPLER_VIEWS
];
80 unsigned nr_fragment_views_saved
;
82 struct sampler_info fragment_samplers_saved
;
83 struct sampler_info samplers
[PIPE_SHADER_TYPES
];
85 /* Temporary number until cso_single_sampler_done is called.
86 * It tracks the highest sampler seen in cso_single_sampler.
90 struct pipe_vertex_buffer aux_vertex_buffer_current
;
91 struct pipe_vertex_buffer aux_vertex_buffer_saved
;
92 unsigned aux_vertex_buffer_index
;
94 struct pipe_constant_buffer aux_constbuf_current
[PIPE_SHADER_TYPES
];
95 struct pipe_constant_buffer aux_constbuf_saved
[PIPE_SHADER_TYPES
];
97 struct pipe_image_view fragment_image0_current
;
98 struct pipe_image_view fragment_image0_saved
;
100 unsigned nr_so_targets
;
101 struct pipe_stream_output_target
*so_targets
[PIPE_MAX_SO_BUFFERS
];
103 unsigned nr_so_targets_saved
;
104 struct pipe_stream_output_target
*so_targets_saved
[PIPE_MAX_SO_BUFFERS
];
106 /** Current and saved state.
107 * The saved state is used as a 1-deep stack.
109 void *blend
, *blend_saved
;
110 void *depth_stencil
, *depth_stencil_saved
;
111 void *rasterizer
, *rasterizer_saved
;
112 void *fragment_shader
, *fragment_shader_saved
;
113 void *vertex_shader
, *vertex_shader_saved
;
114 void *geometry_shader
, *geometry_shader_saved
;
115 void *tessctrl_shader
, *tessctrl_shader_saved
;
116 void *tesseval_shader
, *tesseval_shader_saved
;
117 void *compute_shader
;
118 void *velements
, *velements_saved
;
119 struct pipe_query
*render_condition
, *render_condition_saved
;
120 uint render_condition_mode
, render_condition_mode_saved
;
121 boolean render_condition_cond
, render_condition_cond_saved
;
123 struct pipe_framebuffer_state fb
, fb_saved
;
124 struct pipe_viewport_state vp
, vp_saved
;
125 struct pipe_blend_color blend_color
;
126 unsigned sample_mask
, sample_mask_saved
;
127 unsigned min_samples
, min_samples_saved
;
128 struct pipe_stencil_ref stencil_ref
, stencil_ref_saved
;
131 struct pipe_context
*cso_get_pipe_context(struct cso_context
*cso
)
136 static boolean
delete_blend_state(struct cso_context
*ctx
, void *state
)
138 struct cso_blend
*cso
= (struct cso_blend
*)state
;
140 if (ctx
->blend
== cso
->data
)
143 if (cso
->delete_state
)
144 cso
->delete_state(cso
->context
, cso
->data
);
149 static boolean
delete_depth_stencil_state(struct cso_context
*ctx
, void *state
)
151 struct cso_depth_stencil_alpha
*cso
=
152 (struct cso_depth_stencil_alpha
*)state
;
154 if (ctx
->depth_stencil
== cso
->data
)
157 if (cso
->delete_state
)
158 cso
->delete_state(cso
->context
, cso
->data
);
164 static boolean
delete_sampler_state(struct cso_context
*ctx
, void *state
)
166 struct cso_sampler
*cso
= (struct cso_sampler
*)state
;
167 if (cso
->delete_state
)
168 cso
->delete_state(cso
->context
, cso
->data
);
173 static boolean
delete_rasterizer_state(struct cso_context
*ctx
, void *state
)
175 struct cso_rasterizer
*cso
= (struct cso_rasterizer
*)state
;
177 if (ctx
->rasterizer
== cso
->data
)
179 if (cso
->delete_state
)
180 cso
->delete_state(cso
->context
, cso
->data
);
185 static boolean
delete_vertex_elements(struct cso_context
*ctx
,
188 struct cso_velements
*cso
= (struct cso_velements
*)state
;
190 if (ctx
->velements
== cso
->data
)
193 if (cso
->delete_state
)
194 cso
->delete_state(cso
->context
, cso
->data
);
200 static inline boolean
delete_cso(struct cso_context
*ctx
,
201 void *state
, enum cso_cache_type type
)
205 return delete_blend_state(ctx
, state
);
207 return delete_sampler_state(ctx
, state
);
208 case CSO_DEPTH_STENCIL_ALPHA
:
209 return delete_depth_stencil_state(ctx
, state
);
211 return delete_rasterizer_state(ctx
, state
);
213 return delete_vertex_elements(ctx
, state
);
222 sanitize_hash(struct cso_hash
*hash
, enum cso_cache_type type
,
223 int max_size
, void *user_data
)
225 struct cso_context
*ctx
= (struct cso_context
*)user_data
;
226 /* if we're approach the maximum size, remove fourth of the entries
227 * otherwise every subsequent call will go through the same */
228 int hash_size
= cso_hash_size(hash
);
229 int max_entries
= (max_size
> hash_size
) ? max_size
: hash_size
;
230 int to_remove
= (max_size
< max_entries
) * max_entries
/4;
231 struct cso_hash_iter iter
;
232 struct cso_sampler
**samplers_to_restore
= NULL
;
233 unsigned to_restore
= 0;
235 if (hash_size
> max_size
)
236 to_remove
+= hash_size
- max_size
;
241 if (type
== CSO_SAMPLER
) {
244 samplers_to_restore
= MALLOC(PIPE_SHADER_TYPES
* PIPE_MAX_SAMPLERS
*
245 sizeof(*samplers_to_restore
));
247 /* Temporarily remove currently bound sampler states from the hash
248 * table, to prevent them from being deleted
250 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
251 for (j
= 0; j
< PIPE_MAX_SAMPLERS
; j
++) {
252 struct cso_sampler
*sampler
= ctx
->samplers
[i
].cso_samplers
[j
];
254 if (sampler
&& cso_hash_take(hash
, sampler
->hash_key
))
255 samplers_to_restore
[to_restore
++] = sampler
;
260 iter
= cso_hash_first_node(hash
);
262 /*remove elements until we're good */
263 /*fixme: currently we pick the nodes to remove at random*/
264 void *cso
= cso_hash_iter_data(iter
);
269 if (delete_cso(ctx
, cso
, type
)) {
270 iter
= cso_hash_erase(hash
, iter
);
273 iter
= cso_hash_iter_next(iter
);
276 if (type
== CSO_SAMPLER
) {
277 /* Put currently bound sampler states back into the hash table */
278 while (to_restore
--) {
279 struct cso_sampler
*sampler
= samplers_to_restore
[to_restore
];
281 cso_hash_insert(hash
, sampler
->hash_key
, sampler
);
284 FREE(samplers_to_restore
);
288 static void cso_init_vbuf(struct cso_context
*cso
, unsigned flags
)
290 struct u_vbuf_caps caps
;
292 /* Install u_vbuf if there is anything unsupported. */
293 if (u_vbuf_get_caps(cso
->pipe
->screen
, &caps
, flags
)) {
294 cso
->vbuf
= u_vbuf_create(cso
->pipe
, &caps
,
295 cso
->aux_vertex_buffer_index
);
300 cso_create_context(struct pipe_context
*pipe
, unsigned u_vbuf_flags
)
302 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
306 ctx
->cache
= cso_cache_create();
307 if (ctx
->cache
== NULL
)
309 cso_cache_set_sanitize_callback(ctx
->cache
,
314 ctx
->sample_mask
= ~0;
316 ctx
->aux_vertex_buffer_index
= 0; /* 0 for now */
318 cso_init_vbuf(ctx
, u_vbuf_flags
);
320 /* Enable for testing: */
321 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
323 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_GEOMETRY
,
324 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
325 ctx
->has_geometry_shader
= TRUE
;
327 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_TESS_CTRL
,
328 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
329 ctx
->has_tessellation
= TRUE
;
331 if (pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
332 PIPE_SHADER_CAP_MAX_INSTRUCTIONS
) > 0) {
334 pipe
->screen
->get_shader_param(pipe
->screen
, PIPE_SHADER_COMPUTE
,
335 PIPE_SHADER_CAP_SUPPORTED_IRS
);
336 if (supported_irs
& (1 << PIPE_SHADER_IR_TGSI
)) {
337 ctx
->has_compute_shader
= TRUE
;
340 if (pipe
->screen
->get_param(pipe
->screen
,
341 PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS
) != 0) {
342 ctx
->has_streamout
= TRUE
;
345 ctx
->max_sampler_seen
= -1;
349 cso_destroy_context( ctx
);
354 * Free the CSO context.
356 void cso_destroy_context( struct cso_context
*ctx
)
361 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
362 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
365 static struct pipe_sampler_view
*views
[PIPE_MAX_SHADER_SAMPLER_VIEWS
] = { NULL
};
366 static void *zeros
[PIPE_MAX_SAMPLERS
] = { NULL
};
367 struct pipe_screen
*scr
= ctx
->pipe
->screen
;
368 enum pipe_shader_type sh
;
369 for (sh
= 0; sh
< PIPE_SHADER_TYPES
; sh
++) {
370 int maxsam
= scr
->get_shader_param(scr
, sh
,
371 PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS
);
372 int maxview
= scr
->get_shader_param(scr
, sh
,
373 PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS
);
374 assert(maxsam
<= PIPE_MAX_SAMPLERS
);
375 assert(maxview
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
377 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, sh
, 0, maxsam
, zeros
);
380 ctx
->pipe
->set_sampler_views(ctx
->pipe
, sh
, 0, maxview
, views
);
385 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
386 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
387 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, NULL
);
388 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
389 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_VERTEX
, 0, NULL
);
390 if (ctx
->has_geometry_shader
) {
391 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
392 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_GEOMETRY
, 0, NULL
);
394 if (ctx
->has_tessellation
) {
395 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
396 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_CTRL
, 0, NULL
);
397 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
398 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_TESS_EVAL
, 0, NULL
);
400 if (ctx
->has_compute_shader
) {
401 ctx
->pipe
->bind_compute_state(ctx
->pipe
, NULL
);
402 ctx
->pipe
->set_constant_buffer(ctx
->pipe
, PIPE_SHADER_COMPUTE
, 0, NULL
);
404 ctx
->pipe
->bind_vertex_elements_state( ctx
->pipe
, NULL
);
406 if (ctx
->has_streamout
)
407 ctx
->pipe
->set_stream_output_targets(ctx
->pipe
, 0, NULL
, NULL
);
410 for (i
= 0; i
< PIPE_MAX_SHADER_SAMPLER_VIEWS
; i
++) {
411 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
412 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
], NULL
);
415 util_unreference_framebuffer_state(&ctx
->fb
);
416 util_unreference_framebuffer_state(&ctx
->fb_saved
);
418 pipe_vertex_buffer_unreference(&ctx
->aux_vertex_buffer_current
);
419 pipe_vertex_buffer_unreference(&ctx
->aux_vertex_buffer_saved
);
421 for (i
= 0; i
< PIPE_SHADER_TYPES
; i
++) {
422 pipe_resource_reference(&ctx
->aux_constbuf_current
[i
].buffer
, NULL
);
423 pipe_resource_reference(&ctx
->aux_constbuf_saved
[i
].buffer
, NULL
);
426 pipe_resource_reference(&ctx
->fragment_image0_current
.resource
, NULL
);
427 pipe_resource_reference(&ctx
->fragment_image0_saved
.resource
, NULL
);
429 for (i
= 0; i
< PIPE_MAX_SO_BUFFERS
; i
++) {
430 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
431 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], NULL
);
435 cso_cache_delete( ctx
->cache
);
440 u_vbuf_destroy(ctx
->vbuf
);
445 /* Those function will either find the state of the given template
446 * in the cache or they will create a new state from the given
447 * template, insert it in the cache and return it.
451 * If the driver returns 0 from the create method then they will assign
452 * the data member of the cso to be the template itself.
455 enum pipe_error
cso_set_blend(struct cso_context
*ctx
,
456 const struct pipe_blend_state
*templ
)
458 unsigned key_size
, hash_key
;
459 struct cso_hash_iter iter
;
462 key_size
= templ
->independent_blend_enable
?
463 sizeof(struct pipe_blend_state
) :
464 (char *)&(templ
->rt
[1]) - (char *)templ
;
465 hash_key
= cso_construct_key((void*)templ
, key_size
);
466 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_BLEND
,
467 (void*)templ
, key_size
);
469 if (cso_hash_iter_is_null(iter
)) {
470 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
472 return PIPE_ERROR_OUT_OF_MEMORY
;
474 memset(&cso
->state
, 0, sizeof cso
->state
);
475 memcpy(&cso
->state
, templ
, key_size
);
476 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
477 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
478 cso
->context
= ctx
->pipe
;
480 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
481 if (cso_hash_iter_is_null(iter
)) {
483 return PIPE_ERROR_OUT_OF_MEMORY
;
489 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
492 if (ctx
->blend
!= handle
) {
494 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
500 cso_save_blend(struct cso_context
*ctx
)
502 assert(!ctx
->blend_saved
);
503 ctx
->blend_saved
= ctx
->blend
;
507 cso_restore_blend(struct cso_context
*ctx
)
509 if (ctx
->blend
!= ctx
->blend_saved
) {
510 ctx
->blend
= ctx
->blend_saved
;
511 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
513 ctx
->blend_saved
= NULL
;
519 cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
520 const struct pipe_depth_stencil_alpha_state
*templ
)
522 unsigned key_size
= sizeof(struct pipe_depth_stencil_alpha_state
);
523 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
524 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
526 CSO_DEPTH_STENCIL_ALPHA
,
527 (void*)templ
, key_size
);
530 if (cso_hash_iter_is_null(iter
)) {
531 struct cso_depth_stencil_alpha
*cso
=
532 MALLOC(sizeof(struct cso_depth_stencil_alpha
));
534 return PIPE_ERROR_OUT_OF_MEMORY
;
536 memcpy(&cso
->state
, templ
, sizeof(*templ
));
537 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
,
540 (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
541 cso
->context
= ctx
->pipe
;
543 iter
= cso_insert_state(ctx
->cache
, hash_key
,
544 CSO_DEPTH_STENCIL_ALPHA
, cso
);
545 if (cso_hash_iter_is_null(iter
)) {
547 return PIPE_ERROR_OUT_OF_MEMORY
;
553 handle
= ((struct cso_depth_stencil_alpha
*)
554 cso_hash_iter_data(iter
))->data
;
557 if (ctx
->depth_stencil
!= handle
) {
558 ctx
->depth_stencil
= handle
;
559 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
565 cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
567 assert(!ctx
->depth_stencil_saved
);
568 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
572 cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
574 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
575 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
576 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
,
577 ctx
->depth_stencil_saved
);
579 ctx
->depth_stencil_saved
= NULL
;
584 enum pipe_error
cso_set_rasterizer(struct cso_context
*ctx
,
585 const struct pipe_rasterizer_state
*templ
)
587 unsigned key_size
= sizeof(struct pipe_rasterizer_state
);
588 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
589 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
592 (void*)templ
, key_size
);
595 if (cso_hash_iter_is_null(iter
)) {
596 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
598 return PIPE_ERROR_OUT_OF_MEMORY
;
600 memcpy(&cso
->state
, templ
, sizeof(*templ
));
601 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
603 (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
604 cso
->context
= ctx
->pipe
;
606 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
607 if (cso_hash_iter_is_null(iter
)) {
609 return PIPE_ERROR_OUT_OF_MEMORY
;
615 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
618 if (ctx
->rasterizer
!= handle
) {
619 ctx
->rasterizer
= handle
;
620 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
626 cso_save_rasterizer(struct cso_context
*ctx
)
628 assert(!ctx
->rasterizer_saved
);
629 ctx
->rasterizer_saved
= ctx
->rasterizer
;
633 cso_restore_rasterizer(struct cso_context
*ctx
)
635 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
636 ctx
->rasterizer
= ctx
->rasterizer_saved
;
637 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
639 ctx
->rasterizer_saved
= NULL
;
643 void cso_set_fragment_shader_handle(struct cso_context
*ctx
, void *handle
)
645 if (ctx
->fragment_shader
!= handle
) {
646 ctx
->fragment_shader
= handle
;
647 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
651 void cso_delete_fragment_shader(struct cso_context
*ctx
, void *handle
)
653 if (handle
== ctx
->fragment_shader
) {
654 /* unbind before deleting */
655 ctx
->pipe
->bind_fs_state(ctx
->pipe
, NULL
);
656 ctx
->fragment_shader
= NULL
;
658 ctx
->pipe
->delete_fs_state(ctx
->pipe
, handle
);
662 cso_save_fragment_shader(struct cso_context
*ctx
)
664 assert(!ctx
->fragment_shader_saved
);
665 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
669 cso_restore_fragment_shader(struct cso_context
*ctx
)
671 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
672 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
673 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
675 ctx
->fragment_shader_saved
= NULL
;
679 void cso_set_vertex_shader_handle(struct cso_context
*ctx
, void *handle
)
681 if (ctx
->vertex_shader
!= handle
) {
682 ctx
->vertex_shader
= handle
;
683 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
687 void cso_delete_vertex_shader(struct cso_context
*ctx
, void *handle
)
689 if (handle
== ctx
->vertex_shader
) {
690 /* unbind before deleting */
691 ctx
->pipe
->bind_vs_state(ctx
->pipe
, NULL
);
692 ctx
->vertex_shader
= NULL
;
694 ctx
->pipe
->delete_vs_state(ctx
->pipe
, handle
);
698 cso_save_vertex_shader(struct cso_context
*ctx
)
700 assert(!ctx
->vertex_shader_saved
);
701 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
705 cso_restore_vertex_shader(struct cso_context
*ctx
)
707 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
708 ctx
->pipe
->bind_vs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
709 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
711 ctx
->vertex_shader_saved
= NULL
;
715 void cso_set_framebuffer(struct cso_context
*ctx
,
716 const struct pipe_framebuffer_state
*fb
)
718 if (memcmp(&ctx
->fb
, fb
, sizeof(*fb
)) != 0) {
719 util_copy_framebuffer_state(&ctx
->fb
, fb
);
720 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
725 cso_save_framebuffer(struct cso_context
*ctx
)
727 util_copy_framebuffer_state(&ctx
->fb_saved
, &ctx
->fb
);
731 cso_restore_framebuffer(struct cso_context
*ctx
)
733 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
734 util_copy_framebuffer_state(&ctx
->fb
, &ctx
->fb_saved
);
735 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
736 util_unreference_framebuffer_state(&ctx
->fb_saved
);
741 void cso_set_viewport(struct cso_context
*ctx
,
742 const struct pipe_viewport_state
*vp
)
744 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
746 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, vp
);
751 * Setup viewport state for given width and height (position is always (0,0)).
752 * Invert the Y axis if 'invert' is true.
755 cso_set_viewport_dims(struct cso_context
*ctx
,
756 float width
, float height
, boolean invert
)
758 struct pipe_viewport_state vp
;
759 vp
.scale
[0] = width
* 0.5f
;
760 vp
.scale
[1] = height
* (invert
? -0.5f
: 0.5f
);
762 vp
.translate
[0] = 0.5f
* width
;
763 vp
.translate
[1] = 0.5f
* height
;
764 vp
.translate
[2] = 0.5f
;
765 cso_set_viewport(ctx
, &vp
);
769 cso_save_viewport(struct cso_context
*ctx
)
771 ctx
->vp_saved
= ctx
->vp
;
776 cso_restore_viewport(struct cso_context
*ctx
)
778 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
779 ctx
->vp
= ctx
->vp_saved
;
780 ctx
->pipe
->set_viewport_states(ctx
->pipe
, 0, 1, &ctx
->vp
);
785 void cso_set_blend_color(struct cso_context
*ctx
,
786 const struct pipe_blend_color
*bc
)
788 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
789 ctx
->blend_color
= *bc
;
790 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);
794 void cso_set_sample_mask(struct cso_context
*ctx
, unsigned sample_mask
)
796 if (ctx
->sample_mask
!= sample_mask
) {
797 ctx
->sample_mask
= sample_mask
;
798 ctx
->pipe
->set_sample_mask(ctx
->pipe
, sample_mask
);
803 cso_save_sample_mask(struct cso_context
*ctx
)
805 ctx
->sample_mask_saved
= ctx
->sample_mask
;
809 cso_restore_sample_mask(struct cso_context
*ctx
)
811 cso_set_sample_mask(ctx
, ctx
->sample_mask_saved
);
814 void cso_set_min_samples(struct cso_context
*ctx
, unsigned min_samples
)
816 if (ctx
->min_samples
!= min_samples
&& ctx
->pipe
->set_min_samples
) {
817 ctx
->min_samples
= min_samples
;
818 ctx
->pipe
->set_min_samples(ctx
->pipe
, min_samples
);
823 cso_save_min_samples(struct cso_context
*ctx
)
825 ctx
->min_samples_saved
= ctx
->min_samples
;
829 cso_restore_min_samples(struct cso_context
*ctx
)
831 cso_set_min_samples(ctx
, ctx
->min_samples_saved
);
834 void cso_set_stencil_ref(struct cso_context
*ctx
,
835 const struct pipe_stencil_ref
*sr
)
837 if (memcmp(&ctx
->stencil_ref
, sr
, sizeof(ctx
->stencil_ref
))) {
838 ctx
->stencil_ref
= *sr
;
839 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, sr
);
844 cso_save_stencil_ref(struct cso_context
*ctx
)
846 ctx
->stencil_ref_saved
= ctx
->stencil_ref
;
851 cso_restore_stencil_ref(struct cso_context
*ctx
)
853 if (memcmp(&ctx
->stencil_ref
, &ctx
->stencil_ref_saved
,
854 sizeof(ctx
->stencil_ref
))) {
855 ctx
->stencil_ref
= ctx
->stencil_ref_saved
;
856 ctx
->pipe
->set_stencil_ref(ctx
->pipe
, &ctx
->stencil_ref
);
860 void cso_set_render_condition(struct cso_context
*ctx
,
861 struct pipe_query
*query
,
863 enum pipe_render_cond_flag mode
)
865 struct pipe_context
*pipe
= ctx
->pipe
;
867 if (ctx
->render_condition
!= query
||
868 ctx
->render_condition_mode
!= mode
||
869 ctx
->render_condition_cond
!= condition
) {
870 pipe
->render_condition(pipe
, query
, condition
, mode
);
871 ctx
->render_condition
= query
;
872 ctx
->render_condition_cond
= condition
;
873 ctx
->render_condition_mode
= mode
;
878 cso_save_render_condition(struct cso_context
*ctx
)
880 ctx
->render_condition_saved
= ctx
->render_condition
;
881 ctx
->render_condition_cond_saved
= ctx
->render_condition_cond
;
882 ctx
->render_condition_mode_saved
= ctx
->render_condition_mode
;
886 cso_restore_render_condition(struct cso_context
*ctx
)
888 cso_set_render_condition(ctx
, ctx
->render_condition_saved
,
889 ctx
->render_condition_cond_saved
,
890 ctx
->render_condition_mode_saved
);
893 void cso_set_geometry_shader_handle(struct cso_context
*ctx
, void *handle
)
895 assert(ctx
->has_geometry_shader
|| !handle
);
897 if (ctx
->has_geometry_shader
&& ctx
->geometry_shader
!= handle
) {
898 ctx
->geometry_shader
= handle
;
899 ctx
->pipe
->bind_gs_state(ctx
->pipe
, handle
);
903 void cso_delete_geometry_shader(struct cso_context
*ctx
, void *handle
)
905 if (handle
== ctx
->geometry_shader
) {
906 /* unbind before deleting */
907 ctx
->pipe
->bind_gs_state(ctx
->pipe
, NULL
);
908 ctx
->geometry_shader
= NULL
;
910 ctx
->pipe
->delete_gs_state(ctx
->pipe
, handle
);
914 cso_save_geometry_shader(struct cso_context
*ctx
)
916 if (!ctx
->has_geometry_shader
) {
920 assert(!ctx
->geometry_shader_saved
);
921 ctx
->geometry_shader_saved
= ctx
->geometry_shader
;
925 cso_restore_geometry_shader(struct cso_context
*ctx
)
927 if (!ctx
->has_geometry_shader
) {
931 if (ctx
->geometry_shader_saved
!= ctx
->geometry_shader
) {
932 ctx
->pipe
->bind_gs_state(ctx
->pipe
, ctx
->geometry_shader_saved
);
933 ctx
->geometry_shader
= ctx
->geometry_shader_saved
;
935 ctx
->geometry_shader_saved
= NULL
;
938 void cso_set_tessctrl_shader_handle(struct cso_context
*ctx
, void *handle
)
940 assert(ctx
->has_tessellation
|| !handle
);
942 if (ctx
->has_tessellation
&& ctx
->tessctrl_shader
!= handle
) {
943 ctx
->tessctrl_shader
= handle
;
944 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, handle
);
948 void cso_delete_tessctrl_shader(struct cso_context
*ctx
, void *handle
)
950 if (handle
== ctx
->tessctrl_shader
) {
951 /* unbind before deleting */
952 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, NULL
);
953 ctx
->tessctrl_shader
= NULL
;
955 ctx
->pipe
->delete_tcs_state(ctx
->pipe
, handle
);
959 cso_save_tessctrl_shader(struct cso_context
*ctx
)
961 if (!ctx
->has_tessellation
) {
965 assert(!ctx
->tessctrl_shader_saved
);
966 ctx
->tessctrl_shader_saved
= ctx
->tessctrl_shader
;
970 cso_restore_tessctrl_shader(struct cso_context
*ctx
)
972 if (!ctx
->has_tessellation
) {
976 if (ctx
->tessctrl_shader_saved
!= ctx
->tessctrl_shader
) {
977 ctx
->pipe
->bind_tcs_state(ctx
->pipe
, ctx
->tessctrl_shader_saved
);
978 ctx
->tessctrl_shader
= ctx
->tessctrl_shader_saved
;
980 ctx
->tessctrl_shader_saved
= NULL
;
983 void cso_set_tesseval_shader_handle(struct cso_context
*ctx
, void *handle
)
985 assert(ctx
->has_tessellation
|| !handle
);
987 if (ctx
->has_tessellation
&& ctx
->tesseval_shader
!= handle
) {
988 ctx
->tesseval_shader
= handle
;
989 ctx
->pipe
->bind_tes_state(ctx
->pipe
, handle
);
993 void cso_delete_tesseval_shader(struct cso_context
*ctx
, void *handle
)
995 if (handle
== ctx
->tesseval_shader
) {
996 /* unbind before deleting */
997 ctx
->pipe
->bind_tes_state(ctx
->pipe
, NULL
);
998 ctx
->tesseval_shader
= NULL
;
1000 ctx
->pipe
->delete_tes_state(ctx
->pipe
, handle
);
1004 cso_save_tesseval_shader(struct cso_context
*ctx
)
1006 if (!ctx
->has_tessellation
) {
1010 assert(!ctx
->tesseval_shader_saved
);
1011 ctx
->tesseval_shader_saved
= ctx
->tesseval_shader
;
1015 cso_restore_tesseval_shader(struct cso_context
*ctx
)
1017 if (!ctx
->has_tessellation
) {
1021 if (ctx
->tesseval_shader_saved
!= ctx
->tesseval_shader
) {
1022 ctx
->pipe
->bind_tes_state(ctx
->pipe
, ctx
->tesseval_shader_saved
);
1023 ctx
->tesseval_shader
= ctx
->tesseval_shader_saved
;
1025 ctx
->tesseval_shader_saved
= NULL
;
1028 void cso_set_compute_shader_handle(struct cso_context
*ctx
, void *handle
)
1030 assert(ctx
->has_compute_shader
|| !handle
);
1032 if (ctx
->has_compute_shader
&& ctx
->compute_shader
!= handle
) {
1033 ctx
->compute_shader
= handle
;
1034 ctx
->pipe
->bind_compute_state(ctx
->pipe
, handle
);
1038 void cso_delete_compute_shader(struct cso_context
*ctx
, void *handle
)
1040 if (handle
== ctx
->compute_shader
) {
1041 /* unbind before deleting */
1042 ctx
->pipe
->bind_compute_state(ctx
->pipe
, NULL
);
1043 ctx
->compute_shader
= NULL
;
1045 ctx
->pipe
->delete_compute_state(ctx
->pipe
, handle
);
1049 cso_set_vertex_elements(struct cso_context
*ctx
,
1051 const struct pipe_vertex_element
*states
)
1053 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1054 unsigned key_size
, hash_key
;
1055 struct cso_hash_iter iter
;
1057 struct cso_velems_state velems_state
;
1060 u_vbuf_set_vertex_elements(vbuf
, count
, states
);
1064 /* Need to include the count into the stored state data too.
1065 * Otherwise first few count pipe_vertex_elements could be identical
1066 * even if count is different, and there's no guarantee the hash would
1067 * be different in that case neither.
1069 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
1070 velems_state
.count
= count
;
1071 memcpy(velems_state
.velems
, states
,
1072 sizeof(struct pipe_vertex_element
) * count
);
1073 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
1074 iter
= cso_find_state_template(ctx
->cache
, hash_key
, CSO_VELEMENTS
,
1075 (void*)&velems_state
, key_size
);
1077 if (cso_hash_iter_is_null(iter
)) {
1078 struct cso_velements
*cso
= MALLOC(sizeof(struct cso_velements
));
1080 return PIPE_ERROR_OUT_OF_MEMORY
;
1082 memcpy(&cso
->state
, &velems_state
, key_size
);
1083 cso
->data
= ctx
->pipe
->create_vertex_elements_state(ctx
->pipe
, count
,
1084 &cso
->state
.velems
[0]);
1086 (cso_state_callback
) ctx
->pipe
->delete_vertex_elements_state
;
1087 cso
->context
= ctx
->pipe
;
1089 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VELEMENTS
, cso
);
1090 if (cso_hash_iter_is_null(iter
)) {
1092 return PIPE_ERROR_OUT_OF_MEMORY
;
1098 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
1101 if (ctx
->velements
!= handle
) {
1102 ctx
->velements
= handle
;
1103 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, handle
);
1109 cso_save_vertex_elements(struct cso_context
*ctx
)
1111 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1114 u_vbuf_save_vertex_elements(vbuf
);
1118 assert(!ctx
->velements_saved
);
1119 ctx
->velements_saved
= ctx
->velements
;
1123 cso_restore_vertex_elements(struct cso_context
*ctx
)
1125 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1128 u_vbuf_restore_vertex_elements(vbuf
);
1132 if (ctx
->velements
!= ctx
->velements_saved
) {
1133 ctx
->velements
= ctx
->velements_saved
;
1134 ctx
->pipe
->bind_vertex_elements_state(ctx
->pipe
, ctx
->velements_saved
);
1136 ctx
->velements_saved
= NULL
;
1139 /* vertex buffers */
1141 void cso_set_vertex_buffers(struct cso_context
*ctx
,
1142 unsigned start_slot
, unsigned count
,
1143 const struct pipe_vertex_buffer
*buffers
)
1145 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1148 u_vbuf_set_vertex_buffers(vbuf
, start_slot
, count
, buffers
);
1152 /* Save what's in the auxiliary slot, so that we can save and restore it
1154 if (start_slot
<= ctx
->aux_vertex_buffer_index
&&
1155 start_slot
+count
> ctx
->aux_vertex_buffer_index
) {
1157 const struct pipe_vertex_buffer
*vb
=
1158 buffers
+ (ctx
->aux_vertex_buffer_index
- start_slot
);
1160 pipe_vertex_buffer_reference(&ctx
->aux_vertex_buffer_current
, vb
);
1162 pipe_vertex_buffer_unreference(&ctx
->aux_vertex_buffer_current
);
1166 ctx
->pipe
->set_vertex_buffers(ctx
->pipe
, start_slot
, count
, buffers
);
1170 cso_save_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1172 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1175 u_vbuf_save_aux_vertex_buffer_slot(vbuf
);
1179 pipe_vertex_buffer_reference(&ctx
->aux_vertex_buffer_saved
,
1180 &ctx
->aux_vertex_buffer_current
);
1184 cso_restore_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1186 struct u_vbuf
*vbuf
= ctx
->vbuf
;
1189 u_vbuf_restore_aux_vertex_buffer_slot(vbuf
);
1193 cso_set_vertex_buffers(ctx
, ctx
->aux_vertex_buffer_index
, 1,
1194 &ctx
->aux_vertex_buffer_saved
);
1195 pipe_vertex_buffer_unreference(&ctx
->aux_vertex_buffer_saved
);
1198 unsigned cso_get_aux_vertex_buffer_slot(struct cso_context
*ctx
)
1200 return ctx
->aux_vertex_buffer_index
;
1206 cso_single_sampler(struct cso_context
*ctx
, enum pipe_shader_type shader_stage
,
1207 unsigned idx
, const struct pipe_sampler_state
*templ
)
1210 unsigned key_size
= sizeof(struct pipe_sampler_state
);
1211 unsigned hash_key
= cso_construct_key((void*)templ
, key_size
);
1212 struct cso_sampler
*cso
;
1213 struct cso_hash_iter iter
=
1214 cso_find_state_template(ctx
->cache
,
1215 hash_key
, CSO_SAMPLER
,
1216 (void *) templ
, key_size
);
1218 if (cso_hash_iter_is_null(iter
)) {
1219 cso
= MALLOC(sizeof(struct cso_sampler
));
1223 memcpy(&cso
->state
, templ
, sizeof(*templ
));
1224 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
1226 (cso_state_callback
) ctx
->pipe
->delete_sampler_state
;
1227 cso
->context
= ctx
->pipe
;
1228 cso
->hash_key
= hash_key
;
1230 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
1231 if (cso_hash_iter_is_null(iter
)) {
1237 cso
= cso_hash_iter_data(iter
);
1240 ctx
->samplers
[shader_stage
].cso_samplers
[idx
] = cso
;
1241 ctx
->samplers
[shader_stage
].samplers
[idx
] = cso
->data
;
1242 ctx
->max_sampler_seen
= MAX2(ctx
->max_sampler_seen
, (int)idx
);
1248 * Send staged sampler state to the driver.
1251 cso_single_sampler_done(struct cso_context
*ctx
,
1252 enum pipe_shader_type shader_stage
)
1254 struct sampler_info
*info
= &ctx
->samplers
[shader_stage
];
1256 if (ctx
->max_sampler_seen
== -1)
1259 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, shader_stage
, 0,
1260 ctx
->max_sampler_seen
+ 1,
1262 ctx
->max_sampler_seen
= -1;
1267 * If the function encouters any errors it will return the
1268 * last one. Done to always try to set as many samplers
1272 cso_set_samplers(struct cso_context
*ctx
,
1273 enum pipe_shader_type shader_stage
,
1275 const struct pipe_sampler_state
**templates
)
1277 for (unsigned i
= 0; i
< nr
; i
++)
1278 cso_single_sampler(ctx
, shader_stage
, i
, templates
[i
]);
1280 cso_single_sampler_done(ctx
, shader_stage
);
1284 cso_save_fragment_samplers(struct cso_context
*ctx
)
1286 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1287 struct sampler_info
*saved
= &ctx
->fragment_samplers_saved
;
1289 memcpy(saved
->cso_samplers
, info
->cso_samplers
,
1290 sizeof(info
->cso_samplers
));
1291 memcpy(saved
->samplers
, info
->samplers
, sizeof(info
->samplers
));
1296 cso_restore_fragment_samplers(struct cso_context
*ctx
)
1298 struct sampler_info
*info
= &ctx
->samplers
[PIPE_SHADER_FRAGMENT
];
1299 struct sampler_info
*saved
= &ctx
->fragment_samplers_saved
;
1301 memcpy(info
->cso_samplers
, saved
->cso_samplers
,
1302 sizeof(info
->cso_samplers
));
1303 memcpy(info
->samplers
, saved
->samplers
, sizeof(info
->samplers
));
1305 for (int i
= PIPE_MAX_SAMPLERS
- 1; i
>= 0; i
--) {
1306 if (info
->samplers
[i
]) {
1307 ctx
->max_sampler_seen
= i
;
1312 cso_single_sampler_done(ctx
, PIPE_SHADER_FRAGMENT
);
1317 cso_set_sampler_views(struct cso_context
*ctx
,
1318 enum pipe_shader_type shader_stage
,
1320 struct pipe_sampler_view
**views
)
1322 if (shader_stage
== PIPE_SHADER_FRAGMENT
) {
1324 boolean any_change
= FALSE
;
1326 /* reference new views */
1327 for (i
= 0; i
< count
; i
++) {
1328 any_change
|= ctx
->fragment_views
[i
] != views
[i
];
1329 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], views
[i
]);
1331 /* unref extra old views, if any */
1332 for (; i
< ctx
->nr_fragment_views
; i
++) {
1333 any_change
|= ctx
->fragment_views
[i
] != NULL
;
1334 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1337 /* bind the new sampler views */
1339 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0,
1340 MAX2(ctx
->nr_fragment_views
, count
),
1341 ctx
->fragment_views
);
1344 ctx
->nr_fragment_views
= count
;
1347 ctx
->pipe
->set_sampler_views(ctx
->pipe
, shader_stage
, 0, count
, views
);
1352 cso_save_fragment_sampler_views(struct cso_context
*ctx
)
1356 ctx
->nr_fragment_views_saved
= ctx
->nr_fragment_views
;
1358 for (i
= 0; i
< ctx
->nr_fragment_views
; i
++) {
1359 assert(!ctx
->fragment_views_saved
[i
]);
1360 pipe_sampler_view_reference(&ctx
->fragment_views_saved
[i
],
1361 ctx
->fragment_views
[i
]);
1367 cso_restore_fragment_sampler_views(struct cso_context
*ctx
)
1369 unsigned i
, nr_saved
= ctx
->nr_fragment_views_saved
;
1372 for (i
= 0; i
< nr_saved
; i
++) {
1373 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1374 /* move the reference from one pointer to another */
1375 ctx
->fragment_views
[i
] = ctx
->fragment_views_saved
[i
];
1376 ctx
->fragment_views_saved
[i
] = NULL
;
1378 for (; i
< ctx
->nr_fragment_views
; i
++) {
1379 pipe_sampler_view_reference(&ctx
->fragment_views
[i
], NULL
);
1382 num
= MAX2(ctx
->nr_fragment_views
, nr_saved
);
1384 /* bind the old/saved sampler views */
1385 ctx
->pipe
->set_sampler_views(ctx
->pipe
, PIPE_SHADER_FRAGMENT
, 0, num
,
1386 ctx
->fragment_views
);
1388 ctx
->nr_fragment_views
= nr_saved
;
1389 ctx
->nr_fragment_views_saved
= 0;
1394 cso_set_shader_images(struct cso_context
*ctx
,
1395 enum pipe_shader_type shader_stage
,
1396 unsigned start
, unsigned count
,
1397 struct pipe_image_view
*images
)
1399 if (shader_stage
== PIPE_SHADER_FRAGMENT
&& start
== 0 && count
>= 1) {
1400 util_copy_image_view(&ctx
->fragment_image0_current
, &images
[0]);
1403 ctx
->pipe
->set_shader_images(ctx
->pipe
, shader_stage
, start
, count
, images
);
1408 cso_save_fragment_image0(struct cso_context
*ctx
)
1410 util_copy_image_view(&ctx
->fragment_image0_saved
,
1411 &ctx
->fragment_image0_current
);
1416 cso_restore_fragment_image0(struct cso_context
*ctx
)
1418 cso_set_shader_images(ctx
, PIPE_SHADER_FRAGMENT
, 0, 1,
1419 &ctx
->fragment_image0_saved
);
1424 cso_set_stream_outputs(struct cso_context
*ctx
,
1425 unsigned num_targets
,
1426 struct pipe_stream_output_target
**targets
,
1427 const unsigned *offsets
)
1429 struct pipe_context
*pipe
= ctx
->pipe
;
1432 if (!ctx
->has_streamout
) {
1433 assert(num_targets
== 0);
1437 if (ctx
->nr_so_targets
== 0 && num_targets
== 0) {
1438 /* Nothing to do. */
1442 /* reference new targets */
1443 for (i
= 0; i
< num_targets
; i
++) {
1444 pipe_so_target_reference(&ctx
->so_targets
[i
], targets
[i
]);
1446 /* unref extra old targets, if any */
1447 for (; i
< ctx
->nr_so_targets
; i
++) {
1448 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1451 pipe
->set_stream_output_targets(pipe
, num_targets
, targets
,
1453 ctx
->nr_so_targets
= num_targets
;
1457 cso_save_stream_outputs(struct cso_context
*ctx
)
1461 if (!ctx
->has_streamout
) {
1465 ctx
->nr_so_targets_saved
= ctx
->nr_so_targets
;
1467 for (i
= 0; i
< ctx
->nr_so_targets
; i
++) {
1468 assert(!ctx
->so_targets_saved
[i
]);
1469 pipe_so_target_reference(&ctx
->so_targets_saved
[i
], ctx
->so_targets
[i
]);
1474 cso_restore_stream_outputs(struct cso_context
*ctx
)
1476 struct pipe_context
*pipe
= ctx
->pipe
;
1478 unsigned offset
[PIPE_MAX_SO_BUFFERS
];
1480 if (!ctx
->has_streamout
) {
1484 if (ctx
->nr_so_targets
== 0 && ctx
->nr_so_targets_saved
== 0) {
1485 /* Nothing to do. */
1489 assert(ctx
->nr_so_targets_saved
<= PIPE_MAX_SO_BUFFERS
);
1490 for (i
= 0; i
< ctx
->nr_so_targets_saved
; i
++) {
1491 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1492 /* move the reference from one pointer to another */
1493 ctx
->so_targets
[i
] = ctx
->so_targets_saved
[i
];
1494 ctx
->so_targets_saved
[i
] = NULL
;
1495 /* -1 means append */
1496 offset
[i
] = (unsigned)-1;
1498 for (; i
< ctx
->nr_so_targets
; i
++) {
1499 pipe_so_target_reference(&ctx
->so_targets
[i
], NULL
);
1502 pipe
->set_stream_output_targets(pipe
, ctx
->nr_so_targets_saved
,
1503 ctx
->so_targets
, offset
);
1505 ctx
->nr_so_targets
= ctx
->nr_so_targets_saved
;
1506 ctx
->nr_so_targets_saved
= 0;
1509 /* constant buffers */
1512 cso_set_constant_buffer(struct cso_context
*cso
,
1513 enum pipe_shader_type shader_stage
,
1514 unsigned index
, struct pipe_constant_buffer
*cb
)
1516 struct pipe_context
*pipe
= cso
->pipe
;
1518 pipe
->set_constant_buffer(pipe
, shader_stage
, index
, cb
);
1521 util_copy_constant_buffer(&cso
->aux_constbuf_current
[shader_stage
], cb
);
1526 cso_set_constant_buffer_resource(struct cso_context
*cso
,
1527 enum pipe_shader_type shader_stage
,
1529 struct pipe_resource
*buffer
)
1532 struct pipe_constant_buffer cb
;
1534 cb
.buffer_offset
= 0;
1535 cb
.buffer_size
= buffer
->width0
;
1536 cb
.user_buffer
= NULL
;
1537 cso_set_constant_buffer(cso
, shader_stage
, index
, &cb
);
1539 cso_set_constant_buffer(cso
, shader_stage
, index
, NULL
);
1544 cso_save_constant_buffer_slot0(struct cso_context
*cso
,
1545 enum pipe_shader_type shader_stage
)
1547 util_copy_constant_buffer(&cso
->aux_constbuf_saved
[shader_stage
],
1548 &cso
->aux_constbuf_current
[shader_stage
]);
1552 cso_restore_constant_buffer_slot0(struct cso_context
*cso
,
1553 enum pipe_shader_type shader_stage
)
1555 cso_set_constant_buffer(cso
, shader_stage
, 0,
1556 &cso
->aux_constbuf_saved
[shader_stage
]);
1557 pipe_resource_reference(&cso
->aux_constbuf_saved
[shader_stage
].buffer
,
1563 * Save all the CSO state items specified by the state_mask bitmask
1564 * of CSO_BIT_x flags.
1567 cso_save_state(struct cso_context
*cso
, unsigned state_mask
)
1569 assert(cso
->saved_state
== 0);
1571 cso
->saved_state
= state_mask
;
1573 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1574 cso_save_aux_vertex_buffer_slot(cso
);
1575 if (state_mask
& CSO_BIT_BLEND
)
1576 cso_save_blend(cso
);
1577 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1578 cso_save_depth_stencil_alpha(cso
);
1579 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1580 cso_save_fragment_samplers(cso
);
1581 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1582 cso_save_fragment_sampler_views(cso
);
1583 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1584 cso_save_fragment_shader(cso
);
1585 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1586 cso_save_framebuffer(cso
);
1587 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1588 cso_save_geometry_shader(cso
);
1589 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1590 cso_save_min_samples(cso
);
1591 if (state_mask
& CSO_BIT_RASTERIZER
)
1592 cso_save_rasterizer(cso
);
1593 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1594 cso_save_render_condition(cso
);
1595 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1596 cso_save_sample_mask(cso
);
1597 if (state_mask
& CSO_BIT_STENCIL_REF
)
1598 cso_save_stencil_ref(cso
);
1599 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1600 cso_save_stream_outputs(cso
);
1601 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1602 cso_save_tessctrl_shader(cso
);
1603 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1604 cso_save_tesseval_shader(cso
);
1605 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1606 cso_save_vertex_elements(cso
);
1607 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1608 cso_save_vertex_shader(cso
);
1609 if (state_mask
& CSO_BIT_VIEWPORT
)
1610 cso_save_viewport(cso
);
1611 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1612 cso
->pipe
->set_active_query_state(cso
->pipe
, false);
1613 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1614 cso_save_fragment_image0(cso
);
1619 * Restore the state which was saved by cso_save_state().
1622 cso_restore_state(struct cso_context
*cso
)
1624 unsigned state_mask
= cso
->saved_state
;
1628 if (state_mask
& CSO_BIT_AUX_VERTEX_BUFFER_SLOT
)
1629 cso_restore_aux_vertex_buffer_slot(cso
);
1630 if (state_mask
& CSO_BIT_BLEND
)
1631 cso_restore_blend(cso
);
1632 if (state_mask
& CSO_BIT_DEPTH_STENCIL_ALPHA
)
1633 cso_restore_depth_stencil_alpha(cso
);
1634 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLERS
)
1635 cso_restore_fragment_samplers(cso
);
1636 if (state_mask
& CSO_BIT_FRAGMENT_SAMPLER_VIEWS
)
1637 cso_restore_fragment_sampler_views(cso
);
1638 if (state_mask
& CSO_BIT_FRAGMENT_SHADER
)
1639 cso_restore_fragment_shader(cso
);
1640 if (state_mask
& CSO_BIT_FRAMEBUFFER
)
1641 cso_restore_framebuffer(cso
);
1642 if (state_mask
& CSO_BIT_GEOMETRY_SHADER
)
1643 cso_restore_geometry_shader(cso
);
1644 if (state_mask
& CSO_BIT_MIN_SAMPLES
)
1645 cso_restore_min_samples(cso
);
1646 if (state_mask
& CSO_BIT_RASTERIZER
)
1647 cso_restore_rasterizer(cso
);
1648 if (state_mask
& CSO_BIT_RENDER_CONDITION
)
1649 cso_restore_render_condition(cso
);
1650 if (state_mask
& CSO_BIT_SAMPLE_MASK
)
1651 cso_restore_sample_mask(cso
);
1652 if (state_mask
& CSO_BIT_STENCIL_REF
)
1653 cso_restore_stencil_ref(cso
);
1654 if (state_mask
& CSO_BIT_STREAM_OUTPUTS
)
1655 cso_restore_stream_outputs(cso
);
1656 if (state_mask
& CSO_BIT_TESSCTRL_SHADER
)
1657 cso_restore_tessctrl_shader(cso
);
1658 if (state_mask
& CSO_BIT_TESSEVAL_SHADER
)
1659 cso_restore_tesseval_shader(cso
);
1660 if (state_mask
& CSO_BIT_VERTEX_ELEMENTS
)
1661 cso_restore_vertex_elements(cso
);
1662 if (state_mask
& CSO_BIT_VERTEX_SHADER
)
1663 cso_restore_vertex_shader(cso
);
1664 if (state_mask
& CSO_BIT_VIEWPORT
)
1665 cso_restore_viewport(cso
);
1666 if (state_mask
& CSO_BIT_PAUSE_QUERIES
)
1667 cso
->pipe
->set_active_query_state(cso
->pipe
, true);
1668 if (state_mask
& CSO_BIT_FRAGMENT_IMAGE0
)
1669 cso_restore_fragment_image0(cso
);
1671 cso
->saved_state
= 0;
1679 cso_draw_vbo(struct cso_context
*cso
,
1680 const struct pipe_draw_info
*info
)
1682 struct u_vbuf
*vbuf
= cso
->vbuf
;
1685 u_vbuf_draw_vbo(vbuf
, info
);
1687 struct pipe_context
*pipe
= cso
->pipe
;
1688 pipe
->draw_vbo(pipe
, info
);
1693 cso_draw_arrays(struct cso_context
*cso
, uint mode
, uint start
, uint count
)
1695 struct pipe_draw_info info
;
1697 util_draw_init_info(&info
);
1702 info
.min_index
= start
;
1703 info
.max_index
= start
+ count
- 1;
1705 cso_draw_vbo(cso
, &info
);
1709 cso_draw_arrays_instanced(struct cso_context
*cso
, uint mode
,
1710 uint start
, uint count
,
1711 uint start_instance
, uint instance_count
)
1713 struct pipe_draw_info info
;
1715 util_draw_init_info(&info
);
1720 info
.min_index
= start
;
1721 info
.max_index
= start
+ count
- 1;
1722 info
.start_instance
= start_instance
;
1723 info
.instance_count
= instance_count
;
1725 cso_draw_vbo(cso
, &info
);