Merge commit 'origin/perrtblend'
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_context.c
1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 *
31 * Wrap the cso cache & hash mechanisms in a simplified
32 * pipe-driver-specific interface.
33 *
34 * @author Zack Rusin <zack@tungstengraphics.com>
35 * @author Keith Whitwell <keith@tungstengraphics.com>
36 */
37
38 #include "pipe/p_state.h"
39 #include "util/u_memory.h"
40 #include "tgsi/tgsi_parse.h"
41
42 #include "cso_cache/cso_context.h"
43 #include "cso_cache/cso_cache.h"
44 #include "cso_cache/cso_hash.h"
45 #include "cso_context.h"
46
47 struct cso_context {
48 struct pipe_context *pipe;
49 struct cso_cache *cache;
50
51 struct {
52 void *samplers[PIPE_MAX_SAMPLERS];
53 unsigned nr_samplers;
54
55 void *vertex_samplers[PIPE_MAX_VERTEX_SAMPLERS];
56 unsigned nr_vertex_samplers;
57 } hw;
58
59 void *samplers[PIPE_MAX_SAMPLERS];
60 unsigned nr_samplers;
61
62 void *vertex_samplers[PIPE_MAX_VERTEX_SAMPLERS];
63 unsigned nr_vertex_samplers;
64
65 unsigned nr_samplers_saved;
66 void *samplers_saved[PIPE_MAX_SAMPLERS];
67
68 unsigned nr_vertex_samplers_saved;
69 void *vertex_samplers_saved[PIPE_MAX_VERTEX_SAMPLERS];
70
71 struct pipe_texture *textures[PIPE_MAX_SAMPLERS];
72 uint nr_textures;
73
74 struct pipe_texture *vertex_textures[PIPE_MAX_VERTEX_SAMPLERS];
75 uint nr_vertex_textures;
76
77 uint nr_textures_saved;
78 struct pipe_texture *textures_saved[PIPE_MAX_SAMPLERS];
79
80 uint nr_vertex_textures_saved;
81 struct pipe_texture *vertex_textures_saved[PIPE_MAX_SAMPLERS];
82
83 /** Current and saved state.
84 * The saved state is used as a 1-deep stack.
85 */
86 void *blend, *blend_saved;
87 void *depth_stencil, *depth_stencil_saved;
88 void *rasterizer, *rasterizer_saved;
89 void *fragment_shader, *fragment_shader_saved, *geometry_shader;
90 void *vertex_shader, *vertex_shader_saved, *geometry_shader_saved;
91
92 struct pipe_framebuffer_state fb, fb_saved;
93 struct pipe_viewport_state vp, vp_saved;
94 struct pipe_blend_color blend_color;
95 };
96
97
98 static void
99 free_framebuffer_state(struct pipe_framebuffer_state *fb);
100
101
102 static boolean delete_blend_state(struct cso_context *ctx, void *state)
103 {
104 struct cso_blend *cso = (struct cso_blend *)state;
105
106 if (ctx->blend == cso->data)
107 return FALSE;
108
109 if (cso->delete_state)
110 cso->delete_state(cso->context, cso->data);
111 FREE(state);
112 return TRUE;
113 }
114
115 static boolean delete_depth_stencil_state(struct cso_context *ctx, void *state)
116 {
117 struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
118
119 if (ctx->depth_stencil == cso->data)
120 return FALSE;
121
122 if (cso->delete_state)
123 cso->delete_state(cso->context, cso->data);
124 FREE(state);
125
126 return TRUE;
127 }
128
129 static boolean delete_sampler_state(struct cso_context *ctx, void *state)
130 {
131 struct cso_sampler *cso = (struct cso_sampler *)state;
132 if (cso->delete_state)
133 cso->delete_state(cso->context, cso->data);
134 FREE(state);
135 return TRUE;
136 }
137
138 static boolean delete_rasterizer_state(struct cso_context *ctx, void *state)
139 {
140 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
141
142 if (ctx->rasterizer == cso->data)
143 return FALSE;
144 if (cso->delete_state)
145 cso->delete_state(cso->context, cso->data);
146 FREE(state);
147 return TRUE;
148 }
149
150 static boolean delete_fs_state(struct cso_context *ctx, void *state)
151 {
152 struct cso_fragment_shader *cso = (struct cso_fragment_shader *)state;
153 if (ctx->fragment_shader == cso->data)
154 return FALSE;
155 if (cso->delete_state)
156 cso->delete_state(cso->context, cso->data);
157 FREE(state);
158 return TRUE;
159 }
160
161 static boolean delete_vs_state(struct cso_context *ctx, void *state)
162 {
163 struct cso_vertex_shader *cso = (struct cso_vertex_shader *)state;
164 if (ctx->vertex_shader == cso->data)
165 return TRUE;
166 if (cso->delete_state)
167 cso->delete_state(cso->context, cso->data);
168 FREE(state);
169 return FALSE;
170 }
171
172
173 static INLINE boolean delete_cso(struct cso_context *ctx,
174 void *state, enum cso_cache_type type)
175 {
176 switch (type) {
177 case CSO_BLEND:
178 return delete_blend_state(ctx, state);
179 break;
180 case CSO_SAMPLER:
181 return delete_sampler_state(ctx, state);
182 break;
183 case CSO_DEPTH_STENCIL_ALPHA:
184 return delete_depth_stencil_state(ctx, state);
185 break;
186 case CSO_RASTERIZER:
187 return delete_rasterizer_state(ctx, state);
188 break;
189 case CSO_FRAGMENT_SHADER:
190 return delete_fs_state(ctx, state);
191 break;
192 case CSO_VERTEX_SHADER:
193 return delete_vs_state(ctx, state);
194 break;
195 default:
196 assert(0);
197 FREE(state);
198 }
199 return FALSE;
200 }
201
202 static INLINE void sanitize_hash(struct cso_hash *hash, enum cso_cache_type type,
203 int max_size, void *user_data)
204 {
205 struct cso_context *ctx = (struct cso_context *)user_data;
206 /* if we're approach the maximum size, remove fourth of the entries
207 * otherwise every subsequent call will go through the same */
208 int hash_size = cso_hash_size(hash);
209 int max_entries = (max_size > hash_size) ? max_size : hash_size;
210 int to_remove = (max_size < max_entries) * max_entries/4;
211 struct cso_hash_iter iter = cso_hash_first_node(hash);
212 if (hash_size > max_size)
213 to_remove += hash_size - max_size;
214 while (to_remove) {
215 /*remove elements until we're good */
216 /*fixme: currently we pick the nodes to remove at random*/
217 void *cso = cso_hash_iter_data(iter);
218 if (delete_cso(ctx, cso, type)) {
219 iter = cso_hash_erase(hash, iter);
220 --to_remove;
221 } else
222 iter = cso_hash_iter_next(iter);
223 }
224 }
225
226
227 struct cso_context *cso_create_context( struct pipe_context *pipe )
228 {
229 struct cso_context *ctx = CALLOC_STRUCT(cso_context);
230 if (ctx == NULL)
231 goto out;
232
233 ctx->cache = cso_cache_create();
234 if (ctx->cache == NULL)
235 goto out;
236 cso_cache_set_sanitize_callback(ctx->cache,
237 sanitize_hash,
238 ctx);
239
240 ctx->pipe = pipe;
241
242 /* Enable for testing: */
243 if (0) cso_set_maximum_cache_size( ctx->cache, 4 );
244
245 return ctx;
246
247 out:
248 cso_destroy_context( ctx );
249 return NULL;
250 }
251
252
253 /**
254 * Prior to context destruction, this function unbinds all state objects.
255 */
256 void cso_release_all( struct cso_context *ctx )
257 {
258 unsigned i;
259
260 if (ctx->pipe) {
261 ctx->pipe->bind_blend_state( ctx->pipe, NULL );
262 ctx->pipe->bind_rasterizer_state( ctx->pipe, NULL );
263 ctx->pipe->bind_fragment_sampler_states( ctx->pipe, 0, NULL );
264 if (ctx->pipe->bind_vertex_sampler_states)
265 ctx->pipe->bind_vertex_sampler_states(ctx->pipe, 0, NULL);
266 ctx->pipe->bind_depth_stencil_alpha_state( ctx->pipe, NULL );
267 ctx->pipe->bind_fs_state( ctx->pipe, NULL );
268 ctx->pipe->bind_vs_state( ctx->pipe, NULL );
269 }
270
271 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
272 pipe_texture_reference(&ctx->textures[i], NULL);
273 pipe_texture_reference(&ctx->textures_saved[i], NULL);
274 }
275
276 for (i = 0; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
277 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
278 pipe_texture_reference(&ctx->vertex_textures_saved[i], NULL);
279 }
280
281 free_framebuffer_state(&ctx->fb);
282 free_framebuffer_state(&ctx->fb_saved);
283
284 if (ctx->cache) {
285 cso_cache_delete( ctx->cache );
286 ctx->cache = NULL;
287 }
288 }
289
290
291 void cso_destroy_context( struct cso_context *ctx )
292 {
293 if (ctx) {
294 /*cso_release_all( ctx );*/
295 FREE( ctx );
296 }
297 }
298
299
300 /* Those function will either find the state of the given template
301 * in the cache or they will create a new state from the given
302 * template, insert it in the cache and return it.
303 */
304
305 /*
306 * If the driver returns 0 from the create method then they will assign
307 * the data member of the cso to be the template itself.
308 */
309
310 enum pipe_error cso_set_blend(struct cso_context *ctx,
311 const struct pipe_blend_state *templ)
312 {
313 unsigned key_size, hash_key;
314 struct cso_hash_iter iter;
315 void *handle;
316
317 key_size = templ->independent_blend_enable ? sizeof(struct pipe_blend_state) :
318 (char *)&(templ->rt[1]) - (char *)templ;
319 hash_key = cso_construct_key((void*)templ, key_size);
320 iter = cso_find_state_template(ctx->cache, hash_key, CSO_BLEND, (void*)templ);
321
322 if (cso_hash_iter_is_null(iter)) {
323 struct cso_blend *cso = MALLOC(sizeof(struct cso_blend));
324 if (!cso)
325 return PIPE_ERROR_OUT_OF_MEMORY;
326
327 memcpy(&cso->state, templ, key_size);
328 cso->data = ctx->pipe->create_blend_state(ctx->pipe, &cso->state);
329 cso->delete_state = (cso_state_callback)ctx->pipe->delete_blend_state;
330 cso->context = ctx->pipe;
331
332 iter = cso_insert_state(ctx->cache, hash_key, CSO_BLEND, cso);
333 if (cso_hash_iter_is_null(iter)) {
334 FREE(cso);
335 return PIPE_ERROR_OUT_OF_MEMORY;
336 }
337
338 handle = cso->data;
339 }
340 else {
341 handle = ((struct cso_blend *)cso_hash_iter_data(iter))->data;
342 }
343
344 if (ctx->blend != handle) {
345 ctx->blend = handle;
346 ctx->pipe->bind_blend_state(ctx->pipe, handle);
347 }
348 return PIPE_OK;
349 }
350
351 void cso_save_blend(struct cso_context *ctx)
352 {
353 assert(!ctx->blend_saved);
354 ctx->blend_saved = ctx->blend;
355 }
356
357 void cso_restore_blend(struct cso_context *ctx)
358 {
359 if (ctx->blend != ctx->blend_saved) {
360 ctx->blend = ctx->blend_saved;
361 ctx->pipe->bind_blend_state(ctx->pipe, ctx->blend_saved);
362 }
363 ctx->blend_saved = NULL;
364 }
365
366
367
368 enum pipe_error cso_single_sampler(struct cso_context *ctx,
369 unsigned idx,
370 const struct pipe_sampler_state *templ)
371 {
372 void *handle = NULL;
373
374 if (templ != NULL) {
375 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
376 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
377 hash_key, CSO_SAMPLER,
378 (void*)templ);
379
380 if (cso_hash_iter_is_null(iter)) {
381 struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler));
382 if (!cso)
383 return PIPE_ERROR_OUT_OF_MEMORY;
384
385 memcpy(&cso->state, templ, sizeof(*templ));
386 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
387 cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state;
388 cso->context = ctx->pipe;
389
390 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
391 if (cso_hash_iter_is_null(iter)) {
392 FREE(cso);
393 return PIPE_ERROR_OUT_OF_MEMORY;
394 }
395
396 handle = cso->data;
397 }
398 else {
399 handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;
400 }
401 }
402
403 ctx->samplers[idx] = handle;
404 return PIPE_OK;
405 }
406
407 enum pipe_error
408 cso_single_vertex_sampler(struct cso_context *ctx,
409 unsigned idx,
410 const struct pipe_sampler_state *templ)
411 {
412 void *handle = NULL;
413
414 if (templ != NULL) {
415 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_sampler_state));
416 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
417 hash_key, CSO_SAMPLER,
418 (void*)templ);
419
420 if (cso_hash_iter_is_null(iter)) {
421 struct cso_sampler *cso = MALLOC(sizeof(struct cso_sampler));
422 if (!cso)
423 return PIPE_ERROR_OUT_OF_MEMORY;
424
425 memcpy(&cso->state, templ, sizeof(*templ));
426 cso->data = ctx->pipe->create_sampler_state(ctx->pipe, &cso->state);
427 cso->delete_state = (cso_state_callback)ctx->pipe->delete_sampler_state;
428 cso->context = ctx->pipe;
429
430 iter = cso_insert_state(ctx->cache, hash_key, CSO_SAMPLER, cso);
431 if (cso_hash_iter_is_null(iter)) {
432 FREE(cso);
433 return PIPE_ERROR_OUT_OF_MEMORY;
434 }
435
436 handle = cso->data;
437 }
438 else {
439 handle = ((struct cso_sampler *)cso_hash_iter_data(iter))->data;
440 }
441 }
442
443 ctx->vertex_samplers[idx] = handle;
444 return PIPE_OK;
445 }
446
447 void cso_single_sampler_done( struct cso_context *ctx )
448 {
449 unsigned i;
450
451 /* find highest non-null sampler */
452 for (i = PIPE_MAX_SAMPLERS; i > 0; i--) {
453 if (ctx->samplers[i - 1] != NULL)
454 break;
455 }
456
457 ctx->nr_samplers = i;
458
459 if (ctx->hw.nr_samplers != ctx->nr_samplers ||
460 memcmp(ctx->hw.samplers,
461 ctx->samplers,
462 ctx->nr_samplers * sizeof(void *)) != 0)
463 {
464 memcpy(ctx->hw.samplers, ctx->samplers, ctx->nr_samplers * sizeof(void *));
465 ctx->hw.nr_samplers = ctx->nr_samplers;
466
467 ctx->pipe->bind_fragment_sampler_states(ctx->pipe, ctx->nr_samplers, ctx->samplers);
468 }
469 }
470
471 void
472 cso_single_vertex_sampler_done(struct cso_context *ctx)
473 {
474 unsigned i;
475
476 /* find highest non-null sampler */
477 for (i = PIPE_MAX_VERTEX_SAMPLERS; i > 0; i--) {
478 if (ctx->vertex_samplers[i - 1] != NULL)
479 break;
480 }
481
482 ctx->nr_vertex_samplers = i;
483
484 if (ctx->hw.nr_vertex_samplers != ctx->nr_vertex_samplers ||
485 memcmp(ctx->hw.vertex_samplers,
486 ctx->vertex_samplers,
487 ctx->nr_vertex_samplers * sizeof(void *)) != 0)
488 {
489 memcpy(ctx->hw.vertex_samplers,
490 ctx->vertex_samplers,
491 ctx->nr_vertex_samplers * sizeof(void *));
492 ctx->hw.nr_vertex_samplers = ctx->nr_vertex_samplers;
493
494 ctx->pipe->bind_vertex_sampler_states(ctx->pipe,
495 ctx->nr_vertex_samplers,
496 ctx->vertex_samplers);
497 }
498 }
499
500 /*
501 * If the function encouters any errors it will return the
502 * last one. Done to always try to set as many samplers
503 * as possible.
504 */
505 enum pipe_error cso_set_samplers( struct cso_context *ctx,
506 unsigned nr,
507 const struct pipe_sampler_state **templates )
508 {
509 unsigned i;
510 enum pipe_error temp, error = PIPE_OK;
511
512 /* TODO: fastpath
513 */
514
515 for (i = 0; i < nr; i++) {
516 temp = cso_single_sampler( ctx, i, templates[i] );
517 if (temp != PIPE_OK)
518 error = temp;
519 }
520
521 for ( ; i < ctx->nr_samplers; i++) {
522 temp = cso_single_sampler( ctx, i, NULL );
523 if (temp != PIPE_OK)
524 error = temp;
525 }
526
527 cso_single_sampler_done( ctx );
528
529 return error;
530 }
531
532 void cso_save_samplers(struct cso_context *ctx)
533 {
534 ctx->nr_samplers_saved = ctx->nr_samplers;
535 memcpy(ctx->samplers_saved, ctx->samplers, sizeof(ctx->samplers));
536 }
537
538 void cso_restore_samplers(struct cso_context *ctx)
539 {
540 ctx->nr_samplers = ctx->nr_samplers_saved;
541 memcpy(ctx->samplers, ctx->samplers_saved, sizeof(ctx->samplers));
542 cso_single_sampler_done( ctx );
543 }
544
545 /*
546 * If the function encouters any errors it will return the
547 * last one. Done to always try to set as many samplers
548 * as possible.
549 */
550 enum pipe_error cso_set_vertex_samplers(struct cso_context *ctx,
551 unsigned nr,
552 const struct pipe_sampler_state **templates)
553 {
554 unsigned i;
555 enum pipe_error temp, error = PIPE_OK;
556
557 /* TODO: fastpath
558 */
559
560 for (i = 0; i < nr; i++) {
561 temp = cso_single_vertex_sampler( ctx, i, templates[i] );
562 if (temp != PIPE_OK)
563 error = temp;
564 }
565
566 for ( ; i < ctx->nr_samplers; i++) {
567 temp = cso_single_vertex_sampler( ctx, i, NULL );
568 if (temp != PIPE_OK)
569 error = temp;
570 }
571
572 cso_single_vertex_sampler_done( ctx );
573
574 return error;
575 }
576
577 void
578 cso_save_vertex_samplers(struct cso_context *ctx)
579 {
580 ctx->nr_vertex_samplers_saved = ctx->nr_vertex_samplers;
581 memcpy(ctx->vertex_samplers_saved, ctx->vertex_samplers, sizeof(ctx->vertex_samplers));
582 }
583
584 void
585 cso_restore_vertex_samplers(struct cso_context *ctx)
586 {
587 ctx->nr_vertex_samplers = ctx->nr_vertex_samplers_saved;
588 memcpy(ctx->vertex_samplers, ctx->vertex_samplers_saved, sizeof(ctx->vertex_samplers));
589 cso_single_vertex_sampler_done(ctx);
590 }
591
592
593 enum pipe_error cso_set_sampler_textures( struct cso_context *ctx,
594 uint count,
595 struct pipe_texture **textures )
596 {
597 uint i;
598
599 ctx->nr_textures = count;
600
601 for (i = 0; i < count; i++)
602 pipe_texture_reference(&ctx->textures[i], textures[i]);
603 for ( ; i < PIPE_MAX_SAMPLERS; i++)
604 pipe_texture_reference(&ctx->textures[i], NULL);
605
606 ctx->pipe->set_fragment_sampler_textures(ctx->pipe, count, textures);
607
608 return PIPE_OK;
609 }
610
611 void cso_save_sampler_textures( struct cso_context *ctx )
612 {
613 uint i;
614
615 ctx->nr_textures_saved = ctx->nr_textures;
616 for (i = 0; i < ctx->nr_textures; i++) {
617 assert(!ctx->textures_saved[i]);
618 pipe_texture_reference(&ctx->textures_saved[i], ctx->textures[i]);
619 }
620 }
621
622 void cso_restore_sampler_textures( struct cso_context *ctx )
623 {
624 uint i;
625
626 ctx->nr_textures = ctx->nr_textures_saved;
627
628 for (i = 0; i < ctx->nr_textures; i++) {
629 pipe_texture_reference(&ctx->textures[i], NULL);
630 ctx->textures[i] = ctx->textures_saved[i];
631 ctx->textures_saved[i] = NULL;
632 }
633 for ( ; i < PIPE_MAX_SAMPLERS; i++)
634 pipe_texture_reference(&ctx->textures[i], NULL);
635
636 ctx->pipe->set_fragment_sampler_textures(ctx->pipe, ctx->nr_textures, ctx->textures);
637
638 ctx->nr_textures_saved = 0;
639 }
640
641
642
643 enum pipe_error
644 cso_set_vertex_sampler_textures(struct cso_context *ctx,
645 uint count,
646 struct pipe_texture **textures)
647 {
648 uint i;
649
650 ctx->nr_vertex_textures = count;
651
652 for (i = 0; i < count; i++) {
653 pipe_texture_reference(&ctx->vertex_textures[i], textures[i]);
654 }
655 for ( ; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
656 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
657 }
658
659 ctx->pipe->set_vertex_sampler_textures(ctx->pipe, count, textures);
660
661 return PIPE_OK;
662 }
663
664 void
665 cso_save_vertex_sampler_textures(struct cso_context *ctx)
666 {
667 uint i;
668
669 ctx->nr_vertex_textures_saved = ctx->nr_vertex_textures;
670 for (i = 0; i < ctx->nr_vertex_textures; i++) {
671 assert(!ctx->vertex_textures_saved[i]);
672 pipe_texture_reference(&ctx->vertex_textures_saved[i], ctx->vertex_textures[i]);
673 }
674 }
675
676 void
677 cso_restore_vertex_sampler_textures(struct cso_context *ctx)
678 {
679 uint i;
680
681 ctx->nr_vertex_textures = ctx->nr_vertex_textures_saved;
682
683 for (i = 0; i < ctx->nr_vertex_textures; i++) {
684 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
685 ctx->vertex_textures[i] = ctx->vertex_textures_saved[i];
686 ctx->vertex_textures_saved[i] = NULL;
687 }
688 for ( ; i < PIPE_MAX_VERTEX_SAMPLERS; i++) {
689 pipe_texture_reference(&ctx->vertex_textures[i], NULL);
690 }
691
692 ctx->pipe->set_vertex_sampler_textures(ctx->pipe,
693 ctx->nr_vertex_textures,
694 ctx->vertex_textures);
695
696 ctx->nr_vertex_textures_saved = 0;
697 }
698
699
700
701 enum pipe_error cso_set_depth_stencil_alpha(struct cso_context *ctx,
702 const struct pipe_depth_stencil_alpha_state *templ)
703 {
704 unsigned hash_key = cso_construct_key((void*)templ,
705 sizeof(struct pipe_depth_stencil_alpha_state));
706 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
707 hash_key,
708 CSO_DEPTH_STENCIL_ALPHA,
709 (void*)templ);
710 void *handle;
711
712 if (cso_hash_iter_is_null(iter)) {
713 struct cso_depth_stencil_alpha *cso = MALLOC(sizeof(struct cso_depth_stencil_alpha));
714 if (!cso)
715 return PIPE_ERROR_OUT_OF_MEMORY;
716
717 memcpy(&cso->state, templ, sizeof(*templ));
718 cso->data = ctx->pipe->create_depth_stencil_alpha_state(ctx->pipe, &cso->state);
719 cso->delete_state = (cso_state_callback)ctx->pipe->delete_depth_stencil_alpha_state;
720 cso->context = ctx->pipe;
721
722 iter = cso_insert_state(ctx->cache, hash_key, CSO_DEPTH_STENCIL_ALPHA, cso);
723 if (cso_hash_iter_is_null(iter)) {
724 FREE(cso);
725 return PIPE_ERROR_OUT_OF_MEMORY;
726 }
727
728 handle = cso->data;
729 }
730 else {
731 handle = ((struct cso_depth_stencil_alpha *)cso_hash_iter_data(iter))->data;
732 }
733
734 if (ctx->depth_stencil != handle) {
735 ctx->depth_stencil = handle;
736 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, handle);
737 }
738 return PIPE_OK;
739 }
740
741 void cso_save_depth_stencil_alpha(struct cso_context *ctx)
742 {
743 assert(!ctx->depth_stencil_saved);
744 ctx->depth_stencil_saved = ctx->depth_stencil;
745 }
746
747 void cso_restore_depth_stencil_alpha(struct cso_context *ctx)
748 {
749 if (ctx->depth_stencil != ctx->depth_stencil_saved) {
750 ctx->depth_stencil = ctx->depth_stencil_saved;
751 ctx->pipe->bind_depth_stencil_alpha_state(ctx->pipe, ctx->depth_stencil_saved);
752 }
753 ctx->depth_stencil_saved = NULL;
754 }
755
756
757
758 enum pipe_error cso_set_rasterizer(struct cso_context *ctx,
759 const struct pipe_rasterizer_state *templ)
760 {
761 unsigned hash_key = cso_construct_key((void*)templ,
762 sizeof(struct pipe_rasterizer_state));
763 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
764 hash_key, CSO_RASTERIZER,
765 (void*)templ);
766 void *handle = NULL;
767
768 if (cso_hash_iter_is_null(iter)) {
769 struct cso_rasterizer *cso = MALLOC(sizeof(struct cso_rasterizer));
770 if (!cso)
771 return PIPE_ERROR_OUT_OF_MEMORY;
772
773 memcpy(&cso->state, templ, sizeof(*templ));
774 cso->data = ctx->pipe->create_rasterizer_state(ctx->pipe, &cso->state);
775 cso->delete_state = (cso_state_callback)ctx->pipe->delete_rasterizer_state;
776 cso->context = ctx->pipe;
777
778 iter = cso_insert_state(ctx->cache, hash_key, CSO_RASTERIZER, cso);
779 if (cso_hash_iter_is_null(iter)) {
780 FREE(cso);
781 return PIPE_ERROR_OUT_OF_MEMORY;
782 }
783
784 handle = cso->data;
785 }
786 else {
787 handle = ((struct cso_rasterizer *)cso_hash_iter_data(iter))->data;
788 }
789
790 if (ctx->rasterizer != handle) {
791 ctx->rasterizer = handle;
792 ctx->pipe->bind_rasterizer_state(ctx->pipe, handle);
793 }
794 return PIPE_OK;
795 }
796
797 void cso_save_rasterizer(struct cso_context *ctx)
798 {
799 assert(!ctx->rasterizer_saved);
800 ctx->rasterizer_saved = ctx->rasterizer;
801 }
802
803 void cso_restore_rasterizer(struct cso_context *ctx)
804 {
805 if (ctx->rasterizer != ctx->rasterizer_saved) {
806 ctx->rasterizer = ctx->rasterizer_saved;
807 ctx->pipe->bind_rasterizer_state(ctx->pipe, ctx->rasterizer_saved);
808 }
809 ctx->rasterizer_saved = NULL;
810 }
811
812
813
814 enum pipe_error cso_set_fragment_shader_handle(struct cso_context *ctx,
815 void *handle )
816 {
817 if (ctx->fragment_shader != handle) {
818 ctx->fragment_shader = handle;
819 ctx->pipe->bind_fs_state(ctx->pipe, handle);
820 }
821 return PIPE_OK;
822 }
823
824 void cso_delete_fragment_shader(struct cso_context *ctx, void *handle )
825 {
826 if (handle == ctx->fragment_shader) {
827 /* unbind before deleting */
828 ctx->pipe->bind_fs_state(ctx->pipe, NULL);
829 ctx->fragment_shader = NULL;
830 }
831 ctx->pipe->delete_fs_state(ctx->pipe, handle);
832 }
833
834 /* Not really working:
835 */
836 #if 0
837 enum pipe_error cso_set_fragment_shader(struct cso_context *ctx,
838 const struct pipe_shader_state *templ)
839 {
840 const struct tgsi_token *tokens = templ->tokens;
841 unsigned num_tokens = tgsi_num_tokens(tokens);
842 size_t tokens_size = num_tokens*sizeof(struct tgsi_token);
843 unsigned hash_key = cso_construct_key((void*)tokens, tokens_size);
844 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
845 hash_key,
846 CSO_FRAGMENT_SHADER,
847 (void*)tokens);
848 void *handle = NULL;
849
850 if (cso_hash_iter_is_null(iter)) {
851 struct cso_fragment_shader *cso = MALLOC(sizeof(struct cso_fragment_shader) + tokens_size);
852 struct tgsi_token *cso_tokens = (struct tgsi_token *)((char *)cso + sizeof(*cso));
853
854 if (!cso)
855 return PIPE_ERROR_OUT_OF_MEMORY;
856
857 memcpy(cso_tokens, tokens, tokens_size);
858 cso->state.tokens = cso_tokens;
859 cso->data = ctx->pipe->create_fs_state(ctx->pipe, &cso->state);
860 cso->delete_state = (cso_state_callback)ctx->pipe->delete_fs_state;
861 cso->context = ctx->pipe;
862
863 iter = cso_insert_state(ctx->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
864 if (cso_hash_iter_is_null(iter)) {
865 FREE(cso);
866 return PIPE_ERROR_OUT_OF_MEMORY;
867 }
868
869 handle = cso->data;
870 }
871 else {
872 handle = ((struct cso_fragment_shader *)cso_hash_iter_data(iter))->data;
873 }
874
875 return cso_set_fragment_shader_handle( ctx, handle );
876 }
877 #endif
878
879 void cso_save_fragment_shader(struct cso_context *ctx)
880 {
881 assert(!ctx->fragment_shader_saved);
882 ctx->fragment_shader_saved = ctx->fragment_shader;
883 }
884
885 void cso_restore_fragment_shader(struct cso_context *ctx)
886 {
887 if (ctx->fragment_shader_saved != ctx->fragment_shader) {
888 ctx->pipe->bind_fs_state(ctx->pipe, ctx->fragment_shader_saved);
889 ctx->fragment_shader = ctx->fragment_shader_saved;
890 }
891 ctx->fragment_shader_saved = NULL;
892 }
893
894
895 enum pipe_error cso_set_vertex_shader_handle(struct cso_context *ctx,
896 void *handle )
897 {
898 if (ctx->vertex_shader != handle) {
899 ctx->vertex_shader = handle;
900 ctx->pipe->bind_vs_state(ctx->pipe, handle);
901 }
902 return PIPE_OK;
903 }
904
905 void cso_delete_vertex_shader(struct cso_context *ctx, void *handle )
906 {
907 if (handle == ctx->vertex_shader) {
908 /* unbind before deleting */
909 ctx->pipe->bind_vs_state(ctx->pipe, NULL);
910 ctx->vertex_shader = NULL;
911 }
912 ctx->pipe->delete_vs_state(ctx->pipe, handle);
913 }
914
915
916 /* Not really working:
917 */
918 #if 0
919 enum pipe_error cso_set_vertex_shader(struct cso_context *ctx,
920 const struct pipe_shader_state *templ)
921 {
922 unsigned hash_key = cso_construct_key((void*)templ,
923 sizeof(struct pipe_shader_state));
924 struct cso_hash_iter iter = cso_find_state_template(ctx->cache,
925 hash_key, CSO_VERTEX_SHADER,
926 (void*)templ);
927 void *handle = NULL;
928
929 if (cso_hash_iter_is_null(iter)) {
930 struct cso_vertex_shader *cso = MALLOC(sizeof(struct cso_vertex_shader));
931
932 if (!cso)
933 return PIPE_ERROR_OUT_OF_MEMORY;
934
935 memcpy(cso->state, templ, sizeof(*templ));
936 cso->data = ctx->pipe->create_vs_state(ctx->pipe, &cso->state);
937 cso->delete_state = (cso_state_callback)ctx->pipe->delete_vs_state;
938 cso->context = ctx->pipe;
939
940 iter = cso_insert_state(ctx->cache, hash_key, CSO_VERTEX_SHADER, cso);
941 if (cso_hash_iter_is_null(iter)) {
942 FREE(cso);
943 return PIPE_ERROR_OUT_OF_MEMORY;
944 }
945
946 handle = cso->data;
947 }
948 else {
949 handle = ((struct cso_vertex_shader *)cso_hash_iter_data(iter))->data;
950 }
951
952 return cso_set_vertex_shader_handle( ctx, handle );
953 }
954 #endif
955
956
957
958 void cso_save_vertex_shader(struct cso_context *ctx)
959 {
960 assert(!ctx->vertex_shader_saved);
961 ctx->vertex_shader_saved = ctx->vertex_shader;
962 }
963
964 void cso_restore_vertex_shader(struct cso_context *ctx)
965 {
966 if (ctx->vertex_shader_saved != ctx->vertex_shader) {
967 ctx->pipe->bind_vs_state(ctx->pipe, ctx->vertex_shader_saved);
968 ctx->vertex_shader = ctx->vertex_shader_saved;
969 }
970 ctx->vertex_shader_saved = NULL;
971 }
972
973
974 /**
975 * Copy framebuffer state from src to dst with refcounting of surfaces.
976 */
977 static void
978 copy_framebuffer_state(struct pipe_framebuffer_state *dst,
979 const struct pipe_framebuffer_state *src)
980 {
981 uint i;
982
983 dst->width = src->width;
984 dst->height = src->height;
985 dst->nr_cbufs = src->nr_cbufs;
986 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
987 pipe_surface_reference(&dst->cbufs[i], src->cbufs[i]);
988 }
989 pipe_surface_reference(&dst->zsbuf, src->zsbuf);
990 }
991
992
993 static void
994 free_framebuffer_state(struct pipe_framebuffer_state *fb)
995 {
996 uint i;
997
998 for (i = 0; i < PIPE_MAX_COLOR_BUFS; i++) {
999 pipe_surface_reference(&fb->cbufs[i], NULL);
1000 }
1001 pipe_surface_reference(&fb->zsbuf, NULL);
1002 }
1003
1004
1005 enum pipe_error cso_set_framebuffer(struct cso_context *ctx,
1006 const struct pipe_framebuffer_state *fb)
1007 {
1008 if (memcmp(&ctx->fb, fb, sizeof(*fb)) != 0) {
1009 copy_framebuffer_state(&ctx->fb, fb);
1010 ctx->pipe->set_framebuffer_state(ctx->pipe, fb);
1011 }
1012 return PIPE_OK;
1013 }
1014
1015 void cso_save_framebuffer(struct cso_context *ctx)
1016 {
1017 copy_framebuffer_state(&ctx->fb_saved, &ctx->fb);
1018 }
1019
1020 void cso_restore_framebuffer(struct cso_context *ctx)
1021 {
1022 if (memcmp(&ctx->fb, &ctx->fb_saved, sizeof(ctx->fb))) {
1023 copy_framebuffer_state(&ctx->fb, &ctx->fb_saved);
1024 ctx->pipe->set_framebuffer_state(ctx->pipe, &ctx->fb);
1025 free_framebuffer_state(&ctx->fb_saved);
1026 }
1027 }
1028
1029
1030 enum pipe_error cso_set_viewport(struct cso_context *ctx,
1031 const struct pipe_viewport_state *vp)
1032 {
1033 if (memcmp(&ctx->vp, vp, sizeof(*vp))) {
1034 ctx->vp = *vp;
1035 ctx->pipe->set_viewport_state(ctx->pipe, vp);
1036 }
1037 return PIPE_OK;
1038 }
1039
1040 void cso_save_viewport(struct cso_context *ctx)
1041 {
1042 ctx->vp_saved = ctx->vp;
1043 }
1044
1045
1046 void cso_restore_viewport(struct cso_context *ctx)
1047 {
1048 if (memcmp(&ctx->vp, &ctx->vp_saved, sizeof(ctx->vp))) {
1049 ctx->vp = ctx->vp_saved;
1050 ctx->pipe->set_viewport_state(ctx->pipe, &ctx->vp);
1051 }
1052 }
1053
1054
1055
1056
1057 enum pipe_error cso_set_blend_color(struct cso_context *ctx,
1058 const struct pipe_blend_color *bc)
1059 {
1060 if (memcmp(&ctx->blend_color, bc, sizeof(ctx->blend_color))) {
1061 ctx->blend_color = *bc;
1062 ctx->pipe->set_blend_color(ctx->pipe, bc);
1063 }
1064 return PIPE_OK;
1065 }
1066
1067 enum pipe_error cso_set_geometry_shader_handle(struct cso_context *ctx,
1068 void *handle)
1069 {
1070 if (ctx->geometry_shader != handle) {
1071 ctx->geometry_shader = handle;
1072 ctx->pipe->bind_gs_state(ctx->pipe, handle);
1073 }
1074 return PIPE_OK;
1075 }
1076
1077 void cso_delete_geometry_shader(struct cso_context *ctx, void *handle)
1078 {
1079 if (handle == ctx->geometry_shader) {
1080 /* unbind before deleting */
1081 ctx->pipe->bind_gs_state(ctx->pipe, NULL);
1082 ctx->geometry_shader = NULL;
1083 }
1084 ctx->pipe->delete_gs_state(ctx->pipe, handle);
1085 }
1086
1087 void cso_save_geometry_shader(struct cso_context *ctx)
1088 {
1089 assert(!ctx->geometry_shader_saved);
1090 ctx->geometry_shader_saved = ctx->geometry_shader;
1091 }
1092
1093 void cso_restore_geometry_shader(struct cso_context *ctx)
1094 {
1095 if (ctx->geometry_shader_saved != ctx->geometry_shader) {
1096 ctx->pipe->bind_gs_state(ctx->pipe, ctx->geometry_shader_saved);
1097 ctx->geometry_shader = ctx->geometry_shader_saved;
1098 }
1099 ctx->geometry_shader_saved = NULL;
1100 }