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/format/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
translate_wrap_mode(unsigned wrap)43 translate_wrap_mode(unsigned wrap)
44 {
45 switch (wrap) {
46 case PIPE_TEX_WRAP_REPEAT:
47 return SVGA3D_TEX_ADDRESS_WRAP;
48 case PIPE_TEX_WRAP_CLAMP:
49 return SVGA3D_TEX_ADDRESS_CLAMP;
50 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
51 /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by
52 * hardware.
53 */
54 return SVGA3D_TEX_ADDRESS_CLAMP;
55 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
56 return SVGA3D_TEX_ADDRESS_BORDER;
57 case PIPE_TEX_WRAP_MIRROR_REPEAT:
58 return SVGA3D_TEX_ADDRESS_MIRROR;
59 case PIPE_TEX_WRAP_MIRROR_CLAMP:
60 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
61 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
62 return SVGA3D_TEX_ADDRESS_MIRRORONCE;
63 default:
64 assert(0);
65 return SVGA3D_TEX_ADDRESS_WRAP;
66 }
67 }
68
69
70 static inline unsigned
translate_img_filter(unsigned filter)71 translate_img_filter(unsigned filter)
72 {
73 switch (filter) {
74 case PIPE_TEX_FILTER_NEAREST:
75 return SVGA3D_TEX_FILTER_NEAREST;
76 case PIPE_TEX_FILTER_LINEAR:
77 return SVGA3D_TEX_FILTER_LINEAR;
78 default:
79 assert(0);
80 return SVGA3D_TEX_FILTER_NEAREST;
81 }
82 }
83
84
85 static inline unsigned
translate_mip_filter(unsigned filter)86 translate_mip_filter(unsigned filter)
87 {
88 switch (filter) {
89 case PIPE_TEX_MIPFILTER_NONE:
90 return SVGA3D_TEX_FILTER_NONE;
91 case PIPE_TEX_MIPFILTER_NEAREST:
92 return SVGA3D_TEX_FILTER_NEAREST;
93 case PIPE_TEX_MIPFILTER_LINEAR:
94 return SVGA3D_TEX_FILTER_LINEAR;
95 default:
96 assert(0);
97 return SVGA3D_TEX_FILTER_NONE;
98 }
99 }
100
101
102 static uint8
translate_comparison_func(unsigned func)103 translate_comparison_func(unsigned func)
104 {
105 switch (func) {
106 case PIPE_FUNC_NEVER:
107 return SVGA3D_COMPARISON_NEVER;
108 case PIPE_FUNC_LESS:
109 return SVGA3D_COMPARISON_LESS;
110 case PIPE_FUNC_EQUAL:
111 return SVGA3D_COMPARISON_EQUAL;
112 case PIPE_FUNC_LEQUAL:
113 return SVGA3D_COMPARISON_LESS_EQUAL;
114 case PIPE_FUNC_GREATER:
115 return SVGA3D_COMPARISON_GREATER;
116 case PIPE_FUNC_NOTEQUAL:
117 return SVGA3D_COMPARISON_NOT_EQUAL;
118 case PIPE_FUNC_GEQUAL:
119 return SVGA3D_COMPARISON_GREATER_EQUAL;
120 case PIPE_FUNC_ALWAYS:
121 return SVGA3D_COMPARISON_ALWAYS;
122 default:
123 assert(!"Invalid comparison function");
124 return SVGA3D_COMPARISON_ALWAYS;
125 }
126 }
127
128
129 /**
130 * Translate filtering state to vgpu10 format.
131 */
132 static SVGA3dFilter
translate_filter_mode(unsigned img_filter,unsigned min_filter,unsigned mag_filter,boolean anisotropic,boolean compare)133 translate_filter_mode(unsigned img_filter,
134 unsigned min_filter,
135 unsigned mag_filter,
136 boolean anisotropic,
137 boolean compare)
138 {
139 SVGA3dFilter mode = 0;
140
141 if (img_filter == PIPE_TEX_FILTER_LINEAR)
142 mode |= SVGA3D_FILTER_MIP_LINEAR;
143 if (min_filter == PIPE_TEX_FILTER_LINEAR)
144 mode |= SVGA3D_FILTER_MIN_LINEAR;
145 if (mag_filter == PIPE_TEX_FILTER_LINEAR)
146 mode |= SVGA3D_FILTER_MAG_LINEAR;
147 if (anisotropic)
148 mode |= SVGA3D_FILTER_ANISOTROPIC;
149 if (compare)
150 mode |= SVGA3D_FILTER_COMPARE;
151
152 return mode;
153 }
154
155
156 /**
157 * Define a vgpu10 sampler state.
158 */
159 static void
define_sampler_state_object(struct svga_context * svga,struct svga_sampler_state * ss,const struct pipe_sampler_state * ps)160 define_sampler_state_object(struct svga_context *svga,
161 struct svga_sampler_state *ss,
162 const struct pipe_sampler_state *ps)
163 {
164 uint8_t max_aniso = (uint8_t) 255; /* XXX fix me */
165 boolean anisotropic;
166 uint8 compare_func;
167 SVGA3dFilter filter;
168 SVGA3dRGBAFloat bcolor;
169 float min_lod, max_lod;
170
171 assert(svga_have_vgpu10(svga));
172
173 anisotropic = ss->aniso_level > 1.0f;
174
175 filter = translate_filter_mode(ps->min_mip_filter,
176 ps->min_img_filter,
177 ps->mag_img_filter,
178 anisotropic,
179 ss->compare_mode);
180
181 compare_func = translate_comparison_func(ss->compare_func);
182
183 COPY_4V(bcolor.value, ps->border_color.f);
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 /* If shadow comparisons are enabled, create two sampler states: one
197 * with the given shadow compare mode, another with shadow comparison off.
198 * We need the later because in some cases, we have to do the shadow
199 * compare in the shader. So, we don't want to do it twice.
200 */
201 STATIC_ASSERT(PIPE_TEX_COMPARE_NONE == 0);
202 STATIC_ASSERT(PIPE_TEX_COMPARE_R_TO_TEXTURE == 1);
203 ss->id[1] = SVGA3D_INVALID_ID;
204
205 unsigned i;
206 for (i = 0; i <= ss->compare_mode; i++) {
207 ss->id[i] = util_bitmask_add(svga->sampler_object_id_bm);
208
209 SVGA_RETRY(svga, SVGA3D_vgpu10_DefineSamplerState
210 (svga->swc,
211 ss->id[i],
212 filter,
213 ss->addressu,
214 ss->addressv,
215 ss->addressw,
216 ss->lod_bias, /* float */
217 max_aniso,
218 compare_func,
219 bcolor,
220 min_lod, /* float */
221 max_lod)); /* float */
222
223 /* turn off the shadow compare option for second iteration */
224 filter &= ~SVGA3D_FILTER_COMPARE;
225 }
226 }
227
228
229 static void *
svga_create_sampler_state(struct pipe_context * pipe,const struct pipe_sampler_state * sampler)230 svga_create_sampler_state(struct pipe_context *pipe,
231 const struct pipe_sampler_state *sampler)
232 {
233 struct svga_context *svga = svga_context(pipe);
234 struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state );
235
236 if (!cso)
237 return NULL;
238
239 cso->mipfilter = translate_mip_filter(sampler->min_mip_filter);
240 cso->magfilter = translate_img_filter( sampler->mag_img_filter );
241 cso->minfilter = translate_img_filter( sampler->min_img_filter );
242 cso->aniso_level = MAX2( sampler->max_anisotropy, 1 );
243 if (sampler->max_anisotropy)
244 cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC;
245 cso->lod_bias = sampler->lod_bias;
246 cso->addressu = translate_wrap_mode(sampler->wrap_s);
247 cso->addressv = translate_wrap_mode(sampler->wrap_t);
248 cso->addressw = translate_wrap_mode(sampler->wrap_r);
249 cso->normalized_coords = sampler->normalized_coords;
250 cso->compare_mode = sampler->compare_mode;
251 cso->compare_func = sampler->compare_func;
252
253 {
254 uint32 r = float_to_ubyte(sampler->border_color.f[0]);
255 uint32 g = float_to_ubyte(sampler->border_color.f[1]);
256 uint32 b = float_to_ubyte(sampler->border_color.f[2]);
257 uint32 a = float_to_ubyte(sampler->border_color.f[3]);
258
259 cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b;
260 }
261
262 /* No SVGA3D support for:
263 * - min/max LOD clamping
264 */
265 cso->min_lod = 0;
266 cso->view_min_lod = MAX2((int) (sampler->min_lod + 0.5), 0);
267 cso->view_max_lod = MAX2((int) (sampler->max_lod + 0.5), 0);
268
269 /* Use min_mipmap */
270 if (svga->debug.use_min_mipmap) {
271 if (cso->view_min_lod == cso->view_max_lod) {
272 cso->min_lod = cso->view_min_lod;
273 cso->view_min_lod = 0;
274 cso->view_max_lod = 1000; /* Just a high number */
275 cso->mipfilter = SVGA3D_TEX_FILTER_NONE;
276 }
277 }
278
279 if (svga_have_vgpu10(svga)) {
280 define_sampler_state_object(svga, cso, sampler);
281 }
282
283 SVGA_DBG(DEBUG_SAMPLERS,
284 "New sampler: min %u, view(min %u, max %u) lod, mipfilter %s\n",
285 cso->min_lod, cso->view_min_lod, cso->view_max_lod,
286 cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING");
287
288 svga->hud.num_sampler_objects++;
289 SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
290 SVGA_STATS_COUNT_SAMPLER);
291
292 return cso;
293 }
294
295
296 static void
svga_bind_sampler_states(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,void ** samplers)297 svga_bind_sampler_states(struct pipe_context *pipe,
298 enum pipe_shader_type shader,
299 unsigned start,
300 unsigned num,
301 void **samplers)
302 {
303 struct svga_context *svga = svga_context(pipe);
304 unsigned i;
305 boolean any_change = FALSE;
306
307 assert(shader < PIPE_SHADER_TYPES);
308 assert(start + num <= PIPE_MAX_SAMPLERS);
309
310 /* Pre-VGPU10 only supports FS textures */
311 if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT)
312 return;
313
314 for (i = 0; i < num; i++) {
315 if (svga->curr.sampler[shader][start + i] != samplers[i])
316 any_change = TRUE;
317 svga->curr.sampler[shader][start + i] = samplers[i];
318 }
319
320 if (!any_change) {
321 return;
322 }
323
324 /* find highest non-null sampler[] entry */
325 {
326 unsigned j = MAX2(svga->curr.num_samplers[shader], start + num);
327 while (j > 0 && svga->curr.sampler[shader][j - 1] == NULL)
328 j--;
329 svga->curr.num_samplers[shader] = j;
330 }
331
332 svga->dirty |= SVGA_NEW_SAMPLER;
333 }
334
335
336 static void
svga_delete_sampler_state(struct pipe_context * pipe,void * sampler)337 svga_delete_sampler_state(struct pipe_context *pipe, void *sampler)
338 {
339 struct svga_sampler_state *ss = (struct svga_sampler_state *) sampler;
340 struct svga_context *svga = svga_context(pipe);
341
342 if (svga_have_vgpu10(svga)) {
343 unsigned i;
344 for (i = 0; i < ARRAY_SIZE(ss->id); i++) {
345 if (ss->id[i] != SVGA3D_INVALID_ID) {
346 svga_hwtnl_flush_retry(svga);
347
348 SVGA_RETRY(svga, SVGA3D_vgpu10_DestroySamplerState(svga->swc,
349 ss->id[i]));
350 util_bitmask_clear(svga->sampler_object_id_bm, ss->id[i]);
351 }
352 }
353 }
354
355 FREE(sampler);
356 svga->hud.num_sampler_objects--;
357 }
358
359
360 static struct pipe_sampler_view *
svga_create_sampler_view(struct pipe_context * pipe,struct pipe_resource * texture,const struct pipe_sampler_view * templ)361 svga_create_sampler_view(struct pipe_context *pipe,
362 struct pipe_resource *texture,
363 const struct pipe_sampler_view *templ)
364 {
365 struct svga_context *svga = svga_context(pipe);
366 struct svga_pipe_sampler_view *sv = CALLOC_STRUCT(svga_pipe_sampler_view);
367
368 if (!sv) {
369 return NULL;
370 }
371
372 sv->base = *templ;
373 sv->base.reference.count = 1;
374 sv->base.texture = NULL;
375 pipe_resource_reference(&sv->base.texture, texture);
376
377 sv->base.context = pipe;
378 sv->id = SVGA3D_INVALID_ID;
379
380 svga->hud.num_samplerview_objects++;
381 SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws,
382 SVGA_STATS_COUNT_SAMPLERVIEW);
383
384 return &sv->base;
385 }
386
387
388 static void
svga_sampler_view_destroy(struct pipe_context * pipe,struct pipe_sampler_view * view)389 svga_sampler_view_destroy(struct pipe_context *pipe,
390 struct pipe_sampler_view *view)
391 {
392 struct svga_context *svga = svga_context(pipe);
393 struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view);
394
395 if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) {
396 assert(view->context == pipe);
397
398 svga_hwtnl_flush_retry(svga);
399
400 SVGA_RETRY(svga, SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc,
401 sv->id));
402 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
403 }
404
405 pipe_resource_reference(&sv->base.texture, NULL);
406
407 FREE(sv);
408 svga->hud.num_samplerview_objects--;
409 }
410
411
412 static void
svga_set_sampler_views(struct pipe_context * pipe,enum pipe_shader_type shader,unsigned start,unsigned num,unsigned unbind_num_trailing_slots,bool take_ownership,struct pipe_sampler_view ** views)413 svga_set_sampler_views(struct pipe_context *pipe,
414 enum pipe_shader_type shader,
415 unsigned start,
416 unsigned num,
417 unsigned unbind_num_trailing_slots,
418 bool take_ownership,
419 struct pipe_sampler_view **views)
420 {
421 struct svga_context *svga = svga_context(pipe);
422 unsigned flag_1d = 0;
423 unsigned flag_srgb = 0;
424 uint i;
425 boolean any_change = FALSE;
426
427 assert(shader < PIPE_SHADER_TYPES);
428 assert(start + num <= ARRAY_SIZE(svga->curr.sampler_views[shader]));
429
430 /* Pre-VGPU10 only supports FS textures */
431 if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT) {
432 for (unsigned i = 0; i < num; i++) {
433 struct pipe_sampler_view *view = views[i];
434 pipe_sampler_view_reference(&view, NULL);
435 }
436 return;
437 }
438
439 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SETSAMPLERVIEWS);
440
441 /* This bit of code works around a quirk in the CSO module.
442 * If start=num=0 it means all sampler views should be released.
443 * Note that the CSO module treats sampler views for fragment shaders
444 * differently than other shader types.
445 */
446 if (start == 0 && num == 0 && svga->curr.num_sampler_views[shader] > 0) {
447 for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
448 pipe_sampler_view_reference(&svga->curr.sampler_views[shader][i],
449 NULL);
450 }
451 any_change = TRUE;
452 }
453
454 for (i = 0; i < num; i++) {
455 enum pipe_texture_target target;
456
457 any_change |= svga->curr.sampler_views[shader][start + i] != views[i];
458
459 if (take_ownership) {
460 pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
461 NULL);
462 svga->curr.sampler_views[shader][start + i] = views[i];
463 } else if (svga->curr.sampler_views[shader][start + i] != views[i]) {
464 pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
465 views[i]);
466 }
467
468 if (!views[i])
469 continue;
470
471 if (util_format_is_srgb(views[i]->format))
472 flag_srgb |= 1 << (start + i);
473
474 target = views[i]->target;
475 if (target == PIPE_TEXTURE_1D) {
476 flag_1d |= 1 << (start + i);
477 } else if (target == PIPE_TEXTURE_RECT) {
478 /* If the size of the bound texture changes, we need to emit new
479 * const buffer values.
480 */
481 svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
482 } else if (target == PIPE_BUFFER) {
483 /* If the size of the bound buffer changes, we need to emit new
484 * const buffer values.
485 */
486 svga->dirty |= SVGA_NEW_TEXTURE_CONSTS;
487 }
488 }
489
490 for (; i < num + unbind_num_trailing_slots; i++) {
491 if (svga->curr.sampler_views[shader][start + i]) {
492 pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i],
493 NULL);
494 any_change = TRUE;
495 }
496 }
497
498 if (!any_change) {
499 goto done;
500 }
501
502 /* find highest non-null sampler_views[] entry */
503 {
504 unsigned j = MAX2(svga->curr.num_sampler_views[shader], start + num);
505 while (j > 0 && svga->curr.sampler_views[shader][j - 1] == NULL)
506 j--;
507 svga->curr.num_sampler_views[shader] = j;
508 }
509
510 svga->dirty |= SVGA_NEW_TEXTURE_BINDING;
511
512 if (flag_srgb != svga->curr.tex_flags.flag_srgb ||
513 flag_1d != svga->curr.tex_flags.flag_1d) {
514 svga->dirty |= SVGA_NEW_TEXTURE_FLAGS;
515 svga->curr.tex_flags.flag_1d = flag_1d;
516 svga->curr.tex_flags.flag_srgb = flag_srgb;
517 }
518
519 /* Check if any of the sampler view resources collide with the framebuffer
520 * color buffers or depth stencil resource. If so, set the NEW_FRAME_BUFFER
521 * dirty bit so that emit_framebuffer can be invoked to create backed view
522 * for the conflicted surface view.
523 */
524 if (svga_check_sampler_framebuffer_resource_collision(svga, shader)) {
525 svga->dirty |= SVGA_NEW_FRAME_BUFFER;
526 }
527
528 done:
529 SVGA_STATS_TIME_POP(svga_sws(svga));
530 }
531
532 /**
533 * Clean up sampler, sampler view state at context destruction time
534 */
535 void
svga_cleanup_sampler_state(struct svga_context * svga)536 svga_cleanup_sampler_state(struct svga_context *svga)
537 {
538 enum pipe_shader_type shader;
539
540 for (shader = 0; shader <= PIPE_SHADER_COMPUTE; shader++) {
541 unsigned i;
542
543 for (i = 0; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
544 pipe_sampler_view_reference(&svga->state.hw_draw.sampler_views[shader][i],
545 NULL);
546 }
547 }
548
549 /* free polygon stipple state */
550 if (svga->polygon_stipple.sampler) {
551 svga->pipe.delete_sampler_state(&svga->pipe, svga->polygon_stipple.sampler);
552 }
553
554 if (svga->polygon_stipple.sampler_view) {
555 svga->pipe.sampler_view_destroy(&svga->pipe,
556 &svga->polygon_stipple.sampler_view->base);
557 }
558 pipe_resource_reference(&svga->polygon_stipple.texture, NULL);
559 }
560
561 void
svga_init_sampler_functions(struct svga_context * svga)562 svga_init_sampler_functions( struct svga_context *svga )
563 {
564 svga->pipe.create_sampler_state = svga_create_sampler_state;
565 svga->pipe.bind_sampler_states = svga_bind_sampler_states;
566 svga->pipe.delete_sampler_state = svga_delete_sampler_state;
567 svga->pipe.set_sampler_views = svga_set_sampler_views;
568 svga->pipe.create_sampler_view = svga_create_sampler_view;
569 svga->pipe.sampler_view_destroy = svga_sampler_view_destroy;
570 }
571