e42c4f7fce75c95fa974ee2f9b9c5e7d81cbf942
[mesa.git] / src / gallium / drivers / svga / svga_state_tss.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29
30 #include "svga_sampler_view.h"
31 #include "svga_winsys.h"
32 #include "svga_context.h"
33 #include "svga_state.h"
34 #include "svga_cmd.h"
35
36
37 void svga_cleanup_tss_binding(struct svga_context *svga)
38 {
39 int i;
40 unsigned count = MAX2( svga->curr.num_sampler_views,
41 svga->state.hw_draw.num_views );
42
43 for (i = 0; i < count; i++) {
44 struct svga_hw_view_state *view = &svga->state.hw_draw.views[i];
45
46 svga_sampler_view_reference(&view->v, NULL);
47 pipe_sampler_view_reference( &svga->curr.sampler_views[i], NULL );
48 pipe_resource_reference( &view->texture, NULL );
49
50 view->dirty = 1;
51 }
52 }
53
54
55 static int
56 update_tss_binding(struct svga_context *svga,
57 unsigned dirty )
58 {
59 unsigned i;
60 unsigned count = MAX2( svga->curr.num_sampler_views,
61 svga->state.hw_draw.num_views );
62 unsigned min_lod;
63 unsigned max_lod;
64
65
66 struct {
67 struct {
68 unsigned unit;
69 struct svga_hw_view_state *view;
70 } bind[PIPE_MAX_SAMPLERS];
71
72 unsigned bind_count;
73 } queue;
74
75 queue.bind_count = 0;
76
77 for (i = 0; i < count; i++) {
78 const struct svga_sampler_state *s = svga->curr.sampler[i];
79 struct svga_hw_view_state *view = &svga->state.hw_draw.views[i];
80 struct pipe_resource *texture = NULL;
81
82 /* get min max lod */
83 if (svga->curr.sampler_views[i]) {
84 min_lod = MAX2(s->view_min_lod, 0);
85 max_lod = MIN2(s->view_max_lod, svga->curr.sampler_views[i]->texture->last_level);
86 texture = svga->curr.sampler_views[i]->texture;
87 } else {
88 min_lod = 0;
89 max_lod = 0;
90 }
91
92 if (view->texture != texture ||
93 view->min_lod != min_lod ||
94 view->max_lod != max_lod) {
95
96 svga_sampler_view_reference(&view->v, NULL);
97 pipe_resource_reference( &view->texture, texture );
98
99 view->dirty = TRUE;
100 view->min_lod = min_lod;
101 view->max_lod = max_lod;
102
103 if (texture)
104 view->v = svga_get_tex_sampler_view(&svga->pipe,
105 texture,
106 min_lod,
107 max_lod);
108 }
109
110 if (view->dirty) {
111 queue.bind[queue.bind_count].unit = i;
112 queue.bind[queue.bind_count].view = view;
113 queue.bind_count++;
114 }
115 else if (view->v) {
116 svga_validate_sampler_view(svga, view->v);
117 }
118 }
119
120 svga->state.hw_draw.num_views = svga->curr.num_sampler_views;
121
122 if (queue.bind_count) {
123 SVGA3dTextureState *ts;
124
125 if (SVGA3D_BeginSetTextureState( svga->swc,
126 &ts,
127 queue.bind_count ) != PIPE_OK)
128 goto fail;
129
130 for (i = 0; i < queue.bind_count; i++) {
131 struct svga_winsys_surface *handle;
132
133 ts[i].stage = queue.bind[i].unit;
134 ts[i].name = SVGA3D_TS_BIND_TEXTURE;
135
136 if (queue.bind[i].view->v) {
137 handle = queue.bind[i].view->v->handle;
138 }
139 else {
140 handle = NULL;
141 }
142 svga->swc->surface_relocation(svga->swc,
143 &ts[i].value,
144 handle,
145 SVGA_RELOC_READ);
146
147 queue.bind[i].view->dirty = FALSE;
148 }
149
150 SVGA_FIFOCommitAll( svga->swc );
151 }
152
153 return 0;
154
155 fail:
156 return PIPE_ERROR_OUT_OF_MEMORY;
157 }
158
159
160 struct svga_tracked_state svga_hw_tss_binding = {
161 "texture binding emit",
162 SVGA_NEW_TEXTURE_BINDING |
163 SVGA_NEW_SAMPLER,
164 update_tss_binding
165 };
166
167
168 /***********************************************************************
169 */
170
171 struct ts_queue {
172 unsigned ts_count;
173 SVGA3dTextureState ts[PIPE_MAX_SAMPLERS*SVGA3D_TS_MAX];
174 };
175
176
177 #define EMIT_TS(svga, unit, val, token, fail) \
178 do { \
179 if (svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] != val) { \
180 svga_queue_tss( &queue, unit, SVGA3D_TS_##token, val ); \
181 svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] = val; \
182 } \
183 } while (0)
184
185 #define EMIT_TS_FLOAT(svga, unit, fvalue, token, fail) \
186 do { \
187 unsigned val = fui(fvalue); \
188 if (svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] != val) { \
189 svga_queue_tss( &queue, unit, SVGA3D_TS_##token, val ); \
190 svga->state.hw_draw.ts[unit][SVGA3D_TS_##token] = val; \
191 } \
192 } while (0)
193
194
195 static INLINE void
196 svga_queue_tss( struct ts_queue *q,
197 unsigned unit,
198 unsigned tss,
199 unsigned value )
200 {
201 assert(q->ts_count < sizeof(q->ts)/sizeof(q->ts[0]));
202 q->ts[q->ts_count].stage = unit;
203 q->ts[q->ts_count].name = tss;
204 q->ts[q->ts_count].value = value;
205 q->ts_count++;
206 }
207
208
209 static int
210 update_tss(struct svga_context *svga,
211 unsigned dirty )
212 {
213 unsigned i;
214 struct ts_queue queue;
215
216 queue.ts_count = 0;
217 for (i = 0; i < svga->curr.num_samplers; i++) {
218 if (svga->curr.sampler[i]) {
219 const struct svga_sampler_state *curr = svga->curr.sampler[i];
220
221 EMIT_TS(svga, i, curr->mipfilter, MIPFILTER, fail);
222 EMIT_TS(svga, i, curr->min_lod, TEXTURE_MIPMAP_LEVEL, fail);
223 EMIT_TS(svga, i, curr->magfilter, MAGFILTER, fail);
224 EMIT_TS(svga, i, curr->minfilter, MINFILTER, fail);
225 EMIT_TS(svga, i, curr->aniso_level, TEXTURE_ANISOTROPIC_LEVEL, fail);
226 EMIT_TS_FLOAT(svga, i, curr->lod_bias, TEXTURE_LOD_BIAS, fail);
227 EMIT_TS(svga, i, curr->addressu, ADDRESSU, fail);
228 EMIT_TS(svga, i, curr->addressw, ADDRESSW, fail);
229 EMIT_TS(svga, i, curr->bordercolor, BORDERCOLOR, fail);
230 // TEXCOORDINDEX -- hopefully not needed
231
232 if (svga->curr.tex_flags.flag_1d & (1 << i)) {
233 debug_printf("wrap 1d tex %d\n", i);
234 EMIT_TS(svga, i, SVGA3D_TEX_ADDRESS_WRAP, ADDRESSV, fail);
235 }
236 else
237 EMIT_TS(svga, i, curr->addressv, ADDRESSV, fail);
238
239 if (svga->curr.tex_flags.flag_srgb & (1 << i))
240 EMIT_TS_FLOAT(svga, i, 2.2f, GAMMA, fail);
241 else
242 EMIT_TS_FLOAT(svga, i, 1.0f, GAMMA, fail);
243
244 }
245 }
246
247 if (queue.ts_count) {
248 SVGA3dTextureState *ts;
249
250 if (SVGA3D_BeginSetTextureState( svga->swc,
251 &ts,
252 queue.ts_count ) != PIPE_OK)
253 goto fail;
254
255 memcpy( ts,
256 queue.ts,
257 queue.ts_count * sizeof queue.ts[0]);
258
259 SVGA_FIFOCommitAll( svga->swc );
260 }
261
262 return 0;
263
264 fail:
265 /* XXX: need to poison cached hardware state on failure to ensure
266 * dirty state gets re-emitted. Fix this by re-instating partial
267 * FIFOCommit command and only updating cached hw state once the
268 * initial allocation has succeeded.
269 */
270 memset(svga->state.hw_draw.ts, 0xcd, sizeof(svga->state.hw_draw.ts));
271
272 return PIPE_ERROR_OUT_OF_MEMORY;
273 }
274
275
276 struct svga_tracked_state svga_hw_tss = {
277 "texture state emit",
278 (SVGA_NEW_SAMPLER |
279 SVGA_NEW_TEXTURE_FLAGS),
280 update_tss
281 };
282