svga: fix referencing a NULL framebuffer cbuf
[mesa.git] / src / gallium / drivers / svga / svga_pipe_sampler.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 "pipe/p_defines.h"
27 #include "util/u_bitmask.h"
28 #include "util/u_format.h"
29 #include "util/u_inlines.h"
30 #include "util/u_math.h"
31 #include "util/u_memory.h"
32 #include "tgsi/tgsi_parse.h"
33
34 #include "svga_context.h"
35 #include "svga_cmd.h"
36 #include "svga_debug.h"
37 #include "svga_resource_texture.h"
38 #include "svga_surface.h"
39 #include "svga_sampler_view.h"
40
41
42 static inline unsigned
43 translate_wrap_mode(unsigned wrap)
44 {
45 switch (wrap) {
46 case PIPE_TEX_WRAP_REPEAT:
47 return SVGA3D_TEX_ADDRESS_WRAP;
48
49 case PIPE_TEX_WRAP_CLAMP:
50 return SVGA3D_TEX_ADDRESS_CLAMP;
51
52 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
53 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
54 * hardware.
55 */
56 return SVGA3D_TEX_ADDRESS_CLAMP;
57
58 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
59 return SVGA3D_TEX_ADDRESS_BORDER;
60
61 case PIPE_TEX_WRAP_MIRROR_REPEAT:
62 return SVGA3D_TEX_ADDRESS_MIRROR;
63
64 case PIPE_TEX_WRAP_MIRROR_CLAMP:
65 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
66 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
67 return SVGA3D_TEX_ADDRESS_MIRRORONCE;
68
69 default:
70 assert(0);
71 return SVGA3D_TEX_ADDRESS_WRAP;
72 }
73 }
74
75 static inline unsigned translate_img_filter( unsigned filter )
76 {
77 switch (filter) {
78 case PIPE_TEX_FILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
79 case PIPE_TEX_FILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
80 default:
81 assert(0);
82 return SVGA3D_TEX_FILTER_NEAREST;
83 }
84 }
85
86 static inline unsigned translate_mip_filter( unsigned filter )
87 {
88 switch (filter) {
89 case PIPE_TEX_MIPFILTER_NONE: return SVGA3D_TEX_FILTER_NONE;
90 case PIPE_TEX_MIPFILTER_NEAREST: return SVGA3D_TEX_FILTER_NEAREST;
91 case PIPE_TEX_MIPFILTER_LINEAR: return SVGA3D_TEX_FILTER_LINEAR;
92 default:
93 assert(0);
94 return SVGA3D_TEX_FILTER_NONE;
95 }
96 }
97
98
99 static uint8
100 translate_comparison_func(unsigned func)
101 {
102 switch (func) {
103 case PIPE_FUNC_NEVER:
104 return SVGA3D_COMPARISON_NEVER;
105 case PIPE_FUNC_LESS:
106 return SVGA3D_COMPARISON_LESS;
107 case PIPE_FUNC_EQUAL:
108 return SVGA3D_COMPARISON_EQUAL;
109 case PIPE_FUNC_LEQUAL:
110 return SVGA3D_COMPARISON_LESS_EQUAL;
111 case PIPE_FUNC_GREATER:
112 return SVGA3D_COMPARISON_GREATER;
113 case PIPE_FUNC_NOTEQUAL:
114 return SVGA3D_COMPARISON_NOT_EQUAL;
115 case PIPE_FUNC_GEQUAL:
116 return SVGA3D_COMPARISON_GREATER_EQUAL;
117 case PIPE_FUNC_ALWAYS:
118 return SVGA3D_COMPARISON_ALWAYS;
119 default:
120 assert(!"Invalid comparison function");
121 return SVGA3D_COMPARISON_ALWAYS;
122 }
123 }
124
125
126 /**
127 * Translate filtering state to vgpu10 format.
128 */
129 static SVGA3dFilter
130 translate_filter_mode(unsigned img_filter,
131 unsigned min_filter,
132 unsigned mag_filter,
133 boolean anisotropic,
134 boolean compare)
135 {
136 SVGA3dFilter mode = 0;
137
138 if (img_filter == PIPE_TEX_FILTER_LINEAR)
139 mode |= SVGA3D_FILTER_MIP_LINEAR;
140 if (min_filter == PIPE_TEX_FILTER_LINEAR)
141 mode |= SVGA3D_FILTER_MIN_LINEAR;
142 if (mag_filter == PIPE_TEX_FILTER_LINEAR)
143 mode |= SVGA3D_FILTER_MAG_LINEAR;
144 if (anisotropic)
145 mode |= SVGA3D_FILTER_ANISOTROPIC;
146 if (compare)
147 mode |= SVGA3D_FILTER_COMPARE;
148
149 return mode;
150 }
151
152
153 /**
154 * Define a vgpu10 sampler state.
155 */
156 static void
157 define_sampler_state_object(struct svga_context *svga,
158 struct svga_sampler_state *ss,
159 const struct pipe_sampler_state *ps)
160 {
161 uint8_t max_aniso = (uint8_t) 255; /* XXX fix me */
162 boolean anisotropic;
163 uint8 compare_func;
164 SVGA3dFilter filter;
165 SVGA3dRGBAFloat bcolor;
166 unsigned try;
167 float min_lod, max_lod;
168
169 assert(svga_have_vgpu10(svga));
170
171 anisotropic = ss->aniso_level > 1.0f;
172
173 filter = translate_filter_mode(ps->min_mip_filter,
174 ps->min_img_filter,
175 ps->mag_img_filter,
176 anisotropic,
177 ss->compare_mode);
178
179 compare_func = translate_comparison_func(ss->compare_func);
180
181 COPY_4V(bcolor.value, ps->border_color.f);
182
183 ss->id = util_bitmask_add(svga->sampler_object_id_bm);
184
185 assert(ps->min_lod <= ps->max_lod);
186
187 if (ps->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) {
188 /* just use the base level image */
189 min_lod = max_lod = 0.0f;
190 }
191 else {
192 min_lod = ps->min_lod;
193 max_lod = ps->max_lod;
194 }
195
196 /* Loop in case command buffer is full and we need to flush and retry */
197 for (try = 0; try < 2; try++) {
198 enum pipe_error ret =
199 SVGA3D_vgpu10_DefineSamplerState(svga->swc,
200 ss->id,
201 filter,
202 ss->addressu,
203 ss->addressv,
204 ss->addressw,
205 ss->lod_bias, /* float */
206 max_aniso,
207 compare_func,
208 bcolor,
209 min_lod, /* float */
210 max_lod); /* float */
211 if (ret == PIPE_OK)
212 return;
213 svga_context_flush(svga, NULL);
214 }
215 }
216
217
218 static void *
219 svga_create_sampler_state(struct pipe_context *pipe,
220 const struct pipe_sampler_state *sampler)
221 {
222 struct svga_context *svga = svga_context(pipe);
223 struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
224
225 if (!cso)
226 return NULL;
227
228 cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
229 cso->magfilter = translate_img_filter( sampler->mag_img_filter );
230 cso->minfilter = translate_img_filter( sampler->min_img_filter );
231 cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );
232 if(sampler->max_anisotropy)
233 cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
234 cso->lod_bias = sampler->lod_bias;
235 cso->addressu = translate_wrap_mode(sampler->wrap_s);
236 cso->addressv = translate_wrap_mode(sampler->wrap_t);
237 cso->addressw = translate_wrap_mode(sampler->wrap_r);
238 cso->normalized_coords = sampler->normalized_coords;
239 cso->compare_mode = sampler->compare_mode;
240 cso->compare_func = sampler->compare_func;
241
242 {
243 uint32 r = float_to_ubyte(sampler->border_color.f[0]);
244 uint32 g = float_to_ubyte(sampler->border_color.f[1]);
245 uint32 b = float_to_ubyte(sampler->border_color.f[2]);
246 uint32 a = float_to_ubyte(sampler->border_color.f[3]);
247
248 cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;
249 }
250
251 /* No SVGA3D support for:
252 * - min/max LOD clamping
253 */
254 cso->min_lod = 0;
255 cso->view_min_lod = MAX2((int) (sampler->min_lod + 0.5), 0);
256 cso->view_max_lod = MAX2((int) (sampler->max_lod + 0.5), 0);
257
258 /* Use min_mipmap */
259 if (svga->debug.use_min_mipmap) {
260 if (cso->view_min_lod == cso->view_max_lod) {
261 cso->min_lod = cso->view_min_lod;
262 cso->view_min_lod = 0;
263 cso->view_max_lod = 1000; /* Just a high number */
264 cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
265 }
266 }
267
268 if (svga_have_vgpu10(svga)) {
269 define_sampler_state_object(svga, cso, sampler);
270 }
271
272 SVGA_DBG(DEBUG_VIEWS, "min %u, view(min %u, max %u) lod, mipfilter %s\n",
273 cso->min_lod, cso->view_min_lod, cso->view_max_lod,
274 cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
275
276 return cso;
277 }
278
279 static void
280 svga_bind_sampler_states(struct pipe_context *pipe,
281 unsigned shader,
282 unsigned start,
283 unsigned num,
284 void **samplers)
285 {
286 struct svga_context *svga = svga_context(pipe);
287 unsigned i;
288
289 assert(shader < PIPE_SHADER_TYPES);
290 assert(start + num <= PIPE_MAX_SAMPLERS);
291
292 /* Pre-VGPU10 only supports FS textures */
293 if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)
294 return;
295
296 for (i = 0; i < num; i++)
297 svga->curr.sampler[shader][start + i] = samplers[i];
298
299 /* find highest non-null sampler[] entry */
300 {
301 unsigned j = MAX2(svga->curr.num_samplers[shader], start + num);
302 while (j > 0 && svga->curr.sampler[shader][j - 1] == NULL)
303 j--;
304 svga->curr.num_samplers[shader] = j;
305 }
306
307 svga->dirty |= SVGA_NEW_SAMPLER;
308 }
309
310
311 static void svga_delete_sampler_state(struct pipe_context *pipe,
312 void *sampler)
313 {
314 struct svga_sampler_state *ss = (struct svga_sampler_state *) sampler;
315 struct svga_context *svga = svga_context(pipe);
316
317 if (svga_have_vgpu10(svga)) {
318 enum pipe_error ret;
319
320 svga_hwtnl_flush_retry(svga);
321
322 ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id);
323 if (ret != PIPE_OK) {
324 svga_context_flush(svga, NULL);
325 ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id);
326 }
327 util_bitmask_clear(svga->sampler_object_id_bm, ss->id);
328 }
329
330 FREE(sampler);
331 }
332
333
334 static struct pipe_sampler_view *
335 svga_create_sampler_view(struct pipe_context *pipe,
336 struct pipe_resource *texture,
337 const struct pipe_sampler_view *templ)
338 {
339 struct svga_pipe_sampler_view *sv = CALLOC_STRUCT(svga_pipe_sampler_view);
340
341 if (!sv) {
342 return NULL;
343 }
344
345 sv->base = *templ;
346 sv->base.reference.count = 1;
347 sv->base.texture = NULL;
348 pipe_resource_reference(&sv->base.texture, texture);
349
350 sv->base.context = pipe;
351 sv->id = SVGA3D_INVALID_ID;
352
353 return &sv->base;
354 }
355
356
357 static void
358 svga_sampler_view_destroy(struct pipe_context *pipe,
359 struct pipe_sampler_view *view)
360 {
361 struct svga_context *svga = svga_context(pipe);
362 struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view);
363
364 if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) {
365 if (view->context != pipe) {
366 /* The SVGA3D device will generate an error (and on Linux, cause
367 * us to abort) if we try to destroy a shader resource view from
368 * a context other than the one it was created with. Skip the
369 * SVGA3D_vgpu10_DestroyShaderResourceView() and leak the sampler
370 * view for now. This should only sometimes happen when a shared
371 * texture is deleted.
372 */
373 _debug_printf("context mismatch in %s\n", __func__);
374 }
375 else {
376 enum pipe_error ret;
377
378 svga_hwtnl_flush_retry(svga); /* XXX is this needed? */
379
380 ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id);
381 if (ret != PIPE_OK) {
382 svga_context_flush(svga, NULL);
383 ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id);
384 }
385 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
386 }
387 }
388
389 pipe_resource_reference(&sv->base.texture, NULL);
390
391 FREE(sv);
392 }
393
394 static void
395 svga_set_sampler_views(struct pipe_context *pipe,
396 unsigned shader,
397 unsigned start,
398 unsigned num,
399 struct pipe_sampler_view **views)
400 {
401 struct svga_context *svga = svga_context(pipe);
402 unsigned flag_1d = 0;
403 unsigned flag_srgb = 0;
404 uint i;
405
406 assert(shader < PIPE_SHADER_TYPES);
407 assert(start + num <= Elements(svga->curr.sampler_views[shader]));
408
409 /* Pre-VGPU10 only supports FS textures */
410 if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)
411 return;
412
413 for (i = 0; i < num; i++) {
414 if (svga->curr.sampler_views[shader][start + i] != views[i]) {
415 /* Note: we're using pipe_sampler_view_release() here to work around
416 * a possible crash when the old view belongs to another context that
417 * was already destroyed.
418 */
419 pipe_sampler_view_release(pipe, &svga->curr.sampler_views[shader][start + i]);
420 pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
421 views[i]);
422 }
423
424 if (!views[i])
425 continue;
426
427 if (util_format_is_srgb(views[i]->format))
428 flag_srgb |= 1 << (start + i);
429
430 if (views[i]->texture->target == PIPE_TEXTURE_1D)
431 flag_1d |= 1 << (start + i);
432 }
433
434 /* find highest non-null sampler_views[] entry */
435 {
436 unsigned j = MAX2(svga->curr.num_sampler_views[shader], start + num);
437 while (j > 0 && svga->curr.sampler_views[shader][j - 1] == NULL)
438 j--;
439 svga->curr.num_sampler_views[shader] = j;
440 }
441
442 svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
443
444 if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
445 flag_1d != svga->curr.tex_flags.flag_1d)
446 {
447 svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
448 svga->curr.tex_flags.flag_1d = flag_1d;
449 svga->curr.tex_flags.flag_srgb = flag_srgb;
450 }
451
452 /* Check if any of the sampler view resources collide with the framebuffer
453 * color buffers or depth stencil resource. If so, enable the NEW_FRAME_BUFFER
454 * dirty bit so that emit_framebuffer can be invoked to create backed view
455 * for the conflicted surface view.
456 */
457 for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
458 if (svga->curr.framebuffer.cbufs[i]) {
459 struct svga_surface *s = svga_surface(svga->curr.framebuffer.cbufs[i]);
460 if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
461 svga->dirty |= SVGA_NEW_FRAME_BUFFER;
462 break;
463 }
464 }
465 }
466
467 if (svga->curr.framebuffer.zsbuf) {
468 struct svga_surface *s = svga_surface(svga->curr.framebuffer.zsbuf);
469 if (s) {
470 if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
471 svga->dirty |= SVGA_NEW_FRAME_BUFFER;
472 }
473 }
474 }
475 }
476
477
478 void svga_init_sampler_functions( struct svga_context *svga )
479 {
480 svga->pipe.create_sampler_state = svga_create_sampler_state;
481 svga->pipe.bind_sampler_states = svga_bind_sampler_states;
482 svga->pipe.delete_sampler_state = svga_delete_sampler_state;
483 svga->pipe.set_sampler_views = svga_set_sampler_views;
484 svga->pipe.create_sampler_view = svga_create_sampler_view;
485 svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
486 }
487
488
489