1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 /* Wrap the cso cache & hash mechanisms in a simplified
29 * pipe-driver-specific interface.
32 * Zack Rusin <zack@tungstengraphics.com>
33 * Keith Whitwell <keith@tungstengraphics.com>
36 #include "pipe/p_state.h"
37 #include "pipe/p_util.h"
39 #include "cso_cache/cso_context.h"
40 #include "cso_cache/cso_cache.h"
41 #include "cso_cache/cso_hash.h"
44 struct pipe_context
*pipe
;
45 struct cso_cache
*cache
;
48 void *samplers
[PIPE_MAX_SAMPLERS
];
52 void *samplers
[PIPE_MAX_SAMPLERS
];
55 void *samplers_saved
[PIPE_MAX_SAMPLERS
];
56 unsigned nr_samplers_saved
;
58 struct pipe_texture
*textures
[PIPE_MAX_SAMPLERS
];
61 struct pipe_texture
*textures_saved
[PIPE_MAX_SAMPLERS
];
62 uint nr_textures_saved
;
64 /** Current and saved state.
65 * The saved state is used as a 1-deep stack.
67 void *blend
, *blend_saved
;
68 void *depth_stencil
, *depth_stencil_saved
;
69 void *rasterizer
, *rasterizer_saved
;
70 void *fragment_shader
, *fragment_shader_saved
;
71 void *vertex_shader
, *vertex_shader_saved
;
73 struct pipe_framebuffer_state fb
, fb_saved
;
74 struct pipe_viewport_state vp
, vp_saved
;
75 struct pipe_blend_color blend_color
;
79 struct cso_context
*cso_create_context( struct pipe_context
*pipe
)
81 struct cso_context
*ctx
= CALLOC_STRUCT(cso_context
);
85 ctx
->cache
= cso_cache_create();
86 if (ctx
->cache
== NULL
)
91 /* Enable for testing: */
92 if (0) cso_set_maximum_cache_size( ctx
->cache
, 4 );
97 cso_destroy_context( ctx
);
101 static void cso_release_all( struct cso_context
*ctx
)
104 ctx
->pipe
->bind_blend_state( ctx
->pipe
, NULL
);
105 ctx
->pipe
->bind_rasterizer_state( ctx
->pipe
, NULL
);
106 ctx
->pipe
->bind_sampler_states( ctx
->pipe
, 0, NULL
);
107 ctx
->pipe
->bind_depth_stencil_alpha_state( ctx
->pipe
, NULL
);
108 ctx
->pipe
->bind_fs_state( ctx
->pipe
, NULL
);
109 ctx
->pipe
->bind_vs_state( ctx
->pipe
, NULL
);
113 cso_cache_delete( ctx
->cache
);
119 void cso_destroy_context( struct cso_context
*ctx
)
122 cso_release_all( ctx
);
128 /* Those function will either find the state of the given template
129 * in the cache or they will create a new state from the given
130 * template, insert it in the cache and return it.
134 * If the driver returns 0 from the create method then they will assign
135 * the data member of the cso to be the template itself.
138 void cso_set_blend(struct cso_context
*ctx
,
139 const struct pipe_blend_state
*templ
)
141 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_blend_state
));
142 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
147 if (cso_hash_iter_is_null(iter
)) {
148 struct cso_blend
*cso
= MALLOC(sizeof(struct cso_blend
));
151 cso
->data
= ctx
->pipe
->create_blend_state(ctx
->pipe
, &cso
->state
);
152 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_blend_state
;
153 cso
->context
= ctx
->pipe
;
155 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_BLEND
, cso
);
159 handle
= ((struct cso_blend
*)cso_hash_iter_data(iter
))->data
;
162 if (ctx
->blend
!= handle
) {
164 ctx
->pipe
->bind_blend_state(ctx
->pipe
, handle
);
168 void cso_save_blend(struct cso_context
*ctx
)
170 assert(!ctx
->blend_saved
);
171 ctx
->blend_saved
= ctx
->blend
;
174 void cso_restore_blend(struct cso_context
*ctx
)
176 if (ctx
->blend
!= ctx
->blend_saved
) {
177 ctx
->blend
= ctx
->blend_saved
;
178 ctx
->pipe
->bind_blend_state(ctx
->pipe
, ctx
->blend_saved
);
180 ctx
->blend_saved
= NULL
;
185 void cso_single_sampler(struct cso_context
*ctx
,
187 const struct pipe_sampler_state
*templ
)
192 unsigned hash_key
= cso_construct_key((void*)templ
, sizeof(struct pipe_sampler_state
));
193 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
194 hash_key
, CSO_SAMPLER
,
197 if (cso_hash_iter_is_null(iter
)) {
198 struct cso_sampler
*cso
= MALLOC(sizeof(struct cso_sampler
));
201 cso
->data
= ctx
->pipe
->create_sampler_state(ctx
->pipe
, &cso
->state
);
202 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_sampler_state
;
203 cso
->context
= ctx
->pipe
;
205 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_SAMPLER
, cso
);
209 handle
= ((struct cso_sampler
*)cso_hash_iter_data(iter
))->data
;
213 ctx
->samplers
[idx
] = handle
;
216 void cso_single_sampler_done( struct cso_context
*ctx
)
220 /* find highest non-null sampler */
221 for (i
= PIPE_MAX_SAMPLERS
; i
> 0; i
--) {
222 if (ctx
->samplers
[i
- 1] != NULL
)
226 ctx
->nr_samplers
= i
;
228 if (ctx
->hw
.nr_samplers
!= ctx
->nr_samplers
||
229 memcmp(ctx
->hw
.samplers
,
231 ctx
->nr_samplers
* sizeof(void *)) != 0)
233 memcpy(ctx
->hw
.samplers
, ctx
->samplers
, ctx
->nr_samplers
* sizeof(void *));
234 ctx
->hw
.nr_samplers
= ctx
->nr_samplers
;
236 ctx
->pipe
->bind_sampler_states(ctx
->pipe
, ctx
->nr_samplers
, ctx
->samplers
);
240 void cso_set_samplers( struct cso_context
*ctx
,
242 const struct pipe_sampler_state
**templates
)
249 for (i
= 0; i
< nr
; i
++)
250 cso_single_sampler( ctx
, i
, templates
[i
] );
252 for ( ; i
< ctx
->nr_samplers
; i
++)
253 cso_single_sampler( ctx
, i
, NULL
);
255 cso_single_sampler_done( ctx
);
258 void cso_save_samplers(struct cso_context
*ctx
)
260 ctx
->nr_samplers_saved
= ctx
->nr_samplers
;
261 memcpy(ctx
->samplers_saved
, ctx
->samplers
, sizeof(ctx
->samplers
));
264 void cso_restore_samplers(struct cso_context
*ctx
)
266 cso_set_samplers(ctx
, ctx
->nr_samplers_saved
,
267 (const struct pipe_sampler_state
**) ctx
->samplers_saved
);
271 void cso_set_sampler_textures( struct cso_context
*ctx
,
273 struct pipe_texture
**textures
)
277 ctx
->nr_textures
= count
;
279 for (i
= 0; i
< count
; i
++)
280 ctx
->textures
[i
] = textures
[i
];
281 for ( ; i
< PIPE_MAX_SAMPLERS
; i
++)
282 ctx
->textures
[i
] = NULL
;
284 ctx
->pipe
->set_sampler_textures(ctx
->pipe
, count
, textures
);
287 void cso_save_sampler_textures( struct cso_context
*ctx
)
289 ctx
->nr_textures_saved
= ctx
->nr_textures
;
290 memcpy(ctx
->textures_saved
, ctx
->textures
, sizeof(ctx
->textures
));
293 void cso_restore_sampler_textures( struct cso_context
*ctx
)
295 cso_set_sampler_textures(ctx
, ctx
->nr_textures_saved
, ctx
->textures_saved
);
296 ctx
->nr_textures_saved
= 0;
302 void cso_set_depth_stencil_alpha(struct cso_context
*ctx
,
303 const struct pipe_depth_stencil_alpha_state
*templ
)
305 unsigned hash_key
= cso_construct_key((void*)templ
,
306 sizeof(struct pipe_depth_stencil_alpha_state
));
307 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
309 CSO_DEPTH_STENCIL_ALPHA
,
313 if (cso_hash_iter_is_null(iter
)) {
314 struct cso_depth_stencil_alpha
*cso
= MALLOC(sizeof(struct cso_depth_stencil_alpha
));
317 cso
->data
= ctx
->pipe
->create_depth_stencil_alpha_state(ctx
->pipe
, &cso
->state
);
318 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_depth_stencil_alpha_state
;
319 cso
->context
= ctx
->pipe
;
321 cso_insert_state(ctx
->cache
, hash_key
, CSO_DEPTH_STENCIL_ALPHA
, cso
);
325 handle
= ((struct cso_depth_stencil_alpha
*)cso_hash_iter_data(iter
))->data
;
328 if (ctx
->depth_stencil
!= handle
) {
329 ctx
->depth_stencil
= handle
;
330 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, handle
);
334 void cso_save_depth_stencil_alpha(struct cso_context
*ctx
)
336 assert(!ctx
->depth_stencil_saved
);
337 ctx
->depth_stencil_saved
= ctx
->depth_stencil
;
340 void cso_restore_depth_stencil_alpha(struct cso_context
*ctx
)
342 if (ctx
->depth_stencil
!= ctx
->depth_stencil_saved
) {
343 ctx
->depth_stencil
= ctx
->depth_stencil_saved
;
344 ctx
->pipe
->bind_depth_stencil_alpha_state(ctx
->pipe
, ctx
->depth_stencil_saved
);
346 ctx
->depth_stencil_saved
= NULL
;
351 void cso_set_rasterizer(struct cso_context
*ctx
,
352 const struct pipe_rasterizer_state
*templ
)
354 unsigned hash_key
= cso_construct_key((void*)templ
,
355 sizeof(struct pipe_rasterizer_state
));
356 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
357 hash_key
, CSO_RASTERIZER
,
361 if (cso_hash_iter_is_null(iter
)) {
362 struct cso_rasterizer
*cso
= MALLOC(sizeof(struct cso_rasterizer
));
365 cso
->data
= ctx
->pipe
->create_rasterizer_state(ctx
->pipe
, &cso
->state
);
366 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_rasterizer_state
;
367 cso
->context
= ctx
->pipe
;
369 cso_insert_state(ctx
->cache
, hash_key
, CSO_RASTERIZER
, cso
);
373 handle
= ((struct cso_rasterizer
*)cso_hash_iter_data(iter
))->data
;
376 if (ctx
->rasterizer
!= handle
) {
377 ctx
->rasterizer
= handle
;
378 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, handle
);
382 void cso_save_rasterizer(struct cso_context
*ctx
)
384 assert(!ctx
->rasterizer_saved
);
385 ctx
->rasterizer_saved
= ctx
->rasterizer
;
388 void cso_restore_rasterizer(struct cso_context
*ctx
)
390 if (ctx
->rasterizer
!= ctx
->rasterizer_saved
) {
391 ctx
->rasterizer
= ctx
->rasterizer_saved
;
392 ctx
->pipe
->bind_rasterizer_state(ctx
->pipe
, ctx
->rasterizer_saved
);
394 ctx
->rasterizer_saved
= NULL
;
398 void cso_set_fragment_shader(struct cso_context
*ctx
,
399 const struct pipe_shader_state
*templ
)
401 unsigned hash_key
= cso_construct_key((void*)templ
,
402 sizeof(struct pipe_shader_state
));
403 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
404 hash_key
, CSO_FRAGMENT_SHADER
,
408 if (cso_hash_iter_is_null(iter
)) {
409 struct cso_fragment_shader
*cso
= MALLOC(sizeof(struct cso_fragment_shader
));
412 cso
->data
= ctx
->pipe
->create_fs_state(ctx
->pipe
, &cso
->state
);
413 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_fs_state
;
414 cso
->context
= ctx
->pipe
;
416 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_FRAGMENT_SHADER
, cso
);
420 handle
= ((struct cso_fragment_shader
*)cso_hash_iter_data(iter
))->data
;
423 if (ctx
->fragment_shader
!= handle
) {
424 ctx
->fragment_shader
= handle
;
425 ctx
->pipe
->bind_fs_state(ctx
->pipe
, handle
);
429 void cso_save_fragment_shader(struct cso_context
*ctx
)
431 assert(!ctx
->fragment_shader_saved
);
432 ctx
->fragment_shader_saved
= ctx
->fragment_shader
;
435 void cso_restore_fragment_shader(struct cso_context
*ctx
)
437 assert(ctx
->fragment_shader_saved
);
438 if (ctx
->fragment_shader_saved
!= ctx
->fragment_shader
) {
439 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->fragment_shader_saved
);
440 ctx
->fragment_shader
= ctx
->fragment_shader_saved
;
442 ctx
->fragment_shader_saved
= NULL
;
447 void cso_set_vertex_shader(struct cso_context
*ctx
,
448 const struct pipe_shader_state
*templ
)
450 unsigned hash_key
= cso_construct_key((void*)templ
,
451 sizeof(struct pipe_shader_state
));
452 struct cso_hash_iter iter
= cso_find_state_template(ctx
->cache
,
453 hash_key
, CSO_VERTEX_SHADER
,
457 if (cso_hash_iter_is_null(iter
)) {
458 struct cso_vertex_shader
*cso
= MALLOC(sizeof(struct cso_vertex_shader
));
461 cso
->data
= ctx
->pipe
->create_vs_state(ctx
->pipe
, &cso
->state
);
462 cso
->delete_state
= (cso_state_callback
)ctx
->pipe
->delete_vs_state
;
463 cso
->context
= ctx
->pipe
;
465 iter
= cso_insert_state(ctx
->cache
, hash_key
, CSO_VERTEX_SHADER
, cso
);
469 handle
= ((struct cso_vertex_shader
*)cso_hash_iter_data(iter
))->data
;
472 if (ctx
->vertex_shader
!= handle
) {
473 ctx
->vertex_shader
= handle
;
474 ctx
->pipe
->bind_vs_state(ctx
->pipe
, handle
);
478 void cso_save_vertex_shader(struct cso_context
*ctx
)
480 assert(!ctx
->vertex_shader_saved
);
481 ctx
->vertex_shader_saved
= ctx
->vertex_shader
;
484 void cso_restore_vertex_shader(struct cso_context
*ctx
)
486 assert(ctx
->vertex_shader_saved
);
487 if (ctx
->vertex_shader_saved
!= ctx
->vertex_shader
) {
488 ctx
->pipe
->bind_fs_state(ctx
->pipe
, ctx
->vertex_shader_saved
);
489 ctx
->vertex_shader
= ctx
->vertex_shader_saved
;
491 ctx
->vertex_shader_saved
= NULL
;
496 void cso_set_framebuffer(struct cso_context
*ctx
,
497 const struct pipe_framebuffer_state
*fb
)
499 /* XXX this memcmp() fails to detect buffer size changes */
500 if (1/*memcmp(&ctx->fb, fb, sizeof(*fb))*/) {
502 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, fb
);
506 void cso_save_framebuffer(struct cso_context
*ctx
)
508 ctx
->fb_saved
= ctx
->fb
;
511 void cso_restore_framebuffer(struct cso_context
*ctx
)
513 if (memcmp(&ctx
->fb
, &ctx
->fb_saved
, sizeof(ctx
->fb
))) {
514 ctx
->fb
= ctx
->fb_saved
;
515 ctx
->pipe
->set_framebuffer_state(ctx
->pipe
, &ctx
->fb
);
520 void cso_set_viewport(struct cso_context
*ctx
,
521 const struct pipe_viewport_state
*vp
)
523 if (memcmp(&ctx
->vp
, vp
, sizeof(*vp
))) {
525 ctx
->pipe
->set_viewport_state(ctx
->pipe
, vp
);
529 void cso_save_viewport(struct cso_context
*ctx
)
531 ctx
->vp_saved
= ctx
->vp
;
535 void cso_restore_viewport(struct cso_context
*ctx
)
537 if (memcmp(&ctx
->vp
, &ctx
->vp_saved
, sizeof(ctx
->vp
))) {
538 ctx
->vp
= ctx
->vp_saved
;
539 ctx
->pipe
->set_viewport_state(ctx
->pipe
, &ctx
->vp
);
546 void cso_set_blend_color(struct cso_context
*ctx
,
547 const struct pipe_blend_color
*bc
)
549 if (memcmp(&ctx
->blend_color
, bc
, sizeof(ctx
->blend_color
))) {
550 ctx
->blend_color
= *bc
;
551 ctx
->pipe
->set_blend_color(ctx
->pipe
, bc
);