Switch fragment/vertex shaders to the new caching semantics.
[mesa.git] / src / mesa / state_tracker / st_cache.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 * Authors:
30 * Zack Rusin <zack@tungstengraphics.com>
31 */
32
33 #include "st_cache.h"
34
35 #include "st_context.h"
36
37 #include "pipe/p_state.h"
38
39 #include "pipe/cso_cache/cso_cache.h"
40 #include "pipe/cso_cache/cso_hash.h"
41
42 /* This function will either find the state of the given template
43 * in the cache or it will create a new state state from the given
44 * template, will insert it in the cache and return it.
45 */
46 const struct cso_blend * st_cached_blend_state(struct st_context *st,
47 const struct pipe_blend_state *templ)
48 {
49 unsigned hash_key = cso_construct_key((void*)templ, sizeof(struct pipe_blend_state));
50 struct cso_hash_iter iter = cso_find_state_template(st->cache,
51 hash_key, CSO_BLEND,
52 (void*)templ);
53 if (cso_hash_iter_is_null(iter)) {
54 struct cso_blend *cso = malloc(sizeof(struct cso_blend));
55 memcpy(&cso->state, templ, sizeof(struct pipe_blend_state));
56 cso->data = st->pipe->create_blend_state(st->pipe, templ);
57 if (!cso->data)
58 cso->data = &cso->state;
59 iter = cso_insert_state(st->cache, hash_key, CSO_BLEND, cso);
60 }
61 return ((struct cso_blend *)cso_hash_iter_data(iter));
62 }
63
64 struct pipe_sampler_state * st_cached_sampler_state(
65 struct st_context *st,
66 const struct pipe_sampler_state *sampler)
67 {
68 unsigned hash_key = cso_construct_key((void*)sampler, sizeof(struct pipe_sampler_state));
69 struct cso_hash_iter iter = cso_find_state_template(st->cache,
70 hash_key, CSO_SAMPLER,
71 (void*)sampler);
72 if (cso_hash_iter_is_null(iter)) {
73 const struct pipe_sampler_state *created_state = st->pipe->create_sampler_state(
74 st->pipe, sampler);
75 iter = cso_insert_state(st->cache, hash_key, CSO_SAMPLER,
76 (void*)created_state);
77 }
78 return (struct pipe_sampler_state*)(cso_hash_iter_data(iter));
79 }
80
81 struct pipe_depth_stencil_state * st_cached_depth_stencil_state(
82 struct st_context *st,
83 const struct pipe_depth_stencil_state *depth_stencil)
84 {
85 unsigned hash_key = cso_construct_key((void*)depth_stencil, sizeof(struct pipe_depth_stencil_state));
86 struct cso_hash_iter iter = cso_find_state_template(st->cache,
87 hash_key, CSO_DEPTH_STENCIL,
88 (void*)depth_stencil);
89 if (cso_hash_iter_is_null(iter)) {
90 const struct pipe_depth_stencil_state *created_state = st->pipe->create_depth_stencil_state(
91 st->pipe, depth_stencil);
92 iter = cso_insert_state(st->cache, hash_key, CSO_DEPTH_STENCIL,
93 (void*)created_state);
94 }
95 return (struct pipe_depth_stencil_state*)(cso_hash_iter_data(iter));
96 }
97
98 const struct cso_rasterizer* st_cached_rasterizer_state(
99 struct st_context *st,
100 const struct pipe_rasterizer_state *templ)
101 {
102 unsigned hash_key = cso_construct_key((void*)templ,
103 sizeof(struct pipe_rasterizer_state));
104 struct cso_hash_iter iter = cso_find_state_template(st->cache,
105 hash_key, CSO_RASTERIZER,
106 (void*)templ);
107 if (cso_hash_iter_is_null(iter)) {
108 struct cso_rasterizer *cso = malloc(sizeof(struct cso_rasterizer));
109 memcpy(&cso->state, templ, sizeof(struct pipe_rasterizer_state));
110 cso->data = st->pipe->create_rasterizer_state(st->pipe, templ);
111 if (!cso->data)
112 cso->data = &cso->state;
113 iter = cso_insert_state(st->cache, hash_key, CSO_RASTERIZER, cso);
114 }
115 return (struct cso_rasterizer*)(cso_hash_iter_data(iter));
116 }
117
118 const struct cso_fragment_shader *
119 st_cached_fs_state(struct st_context *st,
120 const struct pipe_shader_state *templ)
121 {
122 unsigned hash_key = cso_construct_key((void*)templ,
123 sizeof(struct pipe_shader_state));
124 struct cso_hash_iter iter = cso_find_state_template(st->cache,
125 hash_key, CSO_FRAGMENT_SHADER,
126 (void*)templ);
127 if (cso_hash_iter_is_null(iter)) {
128 struct cso_fragment_shader *cso = malloc(sizeof(struct cso_fragment_shader));
129 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
130 cso->data = st->pipe->create_fs_state(st->pipe, templ);
131 if (!cso->data)
132 cso->data = &cso->state;
133 iter = cso_insert_state(st->cache, hash_key, CSO_FRAGMENT_SHADER, cso);
134 }
135 return (struct cso_fragment_shader*)(cso_hash_iter_data(iter));
136 }
137
138 const struct cso_vertex_shader *
139 st_cached_vs_state(struct st_context *st,
140 const struct pipe_shader_state *templ)
141 {
142 unsigned hash_key = cso_construct_key((void*)templ,
143 sizeof(struct pipe_shader_state));
144 struct cso_hash_iter iter = cso_find_state_template(st->cache,
145 hash_key, CSO_VERTEX_SHADER,
146 (void*)templ);
147 if (cso_hash_iter_is_null(iter)) {
148 struct cso_vertex_shader *cso = malloc(sizeof(struct cso_vertex_shader));
149 memcpy(&cso->state, templ, sizeof(struct pipe_shader_state));
150 cso->data = st->pipe->create_vs_state(st->pipe, templ);
151 if (!cso->data)
152 cso->data = &cso->state;
153 iter = cso_insert_state(st->cache, hash_key, CSO_VERTEX_SHADER, cso);
154 }
155 return (struct cso_vertex_shader*)(cso_hash_iter_data(iter));
156 }