1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
28 #include "pipe/p_state.h"
29 #include "pipe/p_defines.h"
30 #include "util/u_inlines.h"
31 #include "os/os_thread.h"
32 #include "util/u_format.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/u_string.h"
37 #include "svga_screen.h"
38 #include "svga_context.h"
39 #include "svga_resource_texture.h"
40 #include "svga_sampler_view.h"
41 #include "svga_debug.h"
42 #include "svga_surface.h"
46 svga_debug_describe_sampler_view(char *buf
, const struct svga_sampler_view
*sv
)
49 debug_describe_resource(res
, sv
->texture
);
50 util_sprintf(buf
, "svga_sampler_view<%s,[%u,%u]>", res
, sv
->min_lod
, sv
->max_lod
);
53 struct svga_sampler_view
*
54 svga_get_tex_sampler_view(struct pipe_context
*pipe
,
55 struct pipe_resource
*pt
,
56 unsigned min_lod
, unsigned max_lod
)
58 struct svga_screen
*ss
= svga_screen(pt
->screen
);
59 struct svga_texture
*tex
= svga_texture(pt
);
60 struct svga_sampler_view
*sv
= NULL
;
61 SVGA3dSurfaceFlags flags
= SVGA3D_SURFACE_HINT_TEXTURE
;
62 SVGA3dSurfaceFormat format
= svga_translate_format(pt
->format
);
67 assert(min_lod
<= max_lod
);
68 assert(max_lod
<= pt
->last_level
);
71 /* Is a view needed */
74 * Can't control max lod. For first level views and when we only
75 * look at one level we disable mip filtering to achive the same
78 if (min_lod
== 0 && max_lod
>= pt
->last_level
)
81 if (util_format_is_s3tc(pt
->format
) && view
) {
82 format
= svga_translate_format_render(pt
->format
);
85 if (ss
->debug
.no_sampler_view
)
88 if (ss
->debug
.force_sampler_view
)
92 /* First try the cache */
94 pipe_mutex_lock(ss
->tex_mutex
);
95 if (tex
->cached_view
&&
96 tex
->cached_view
->min_lod
== min_lod
&&
97 tex
->cached_view
->max_lod
== max_lod
) {
98 svga_sampler_view_reference(&sv
, tex
->cached_view
);
99 pipe_mutex_unlock(ss
->tex_mutex
);
100 SVGA_DBG(DEBUG_VIEWS
, "svga: Sampler view: reuse %p, %u %u, last %u\n",
101 pt
, min_lod
, max_lod
, pt
->last_level
);
102 svga_validate_sampler_view(svga_context(pipe
), sv
);
105 pipe_mutex_unlock(ss
->tex_mutex
);
108 sv
= CALLOC_STRUCT(svga_sampler_view
);
109 pipe_reference_init(&sv
->reference
, 1);
110 pipe_resource_reference(&sv
->texture
, pt
);
111 sv
->min_lod
= min_lod
;
112 sv
->max_lod
= max_lod
;
114 /* No view needed just use the whole texture */
116 SVGA_DBG(DEBUG_VIEWS
,
117 "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
118 pt
, min_lod
, max_lod
,
119 max_lod
- min_lod
+ 1,
124 sv
->key
.cachable
= 0;
125 sv
->handle
= tex
->handle
;
126 debug_reference(&sv
->reference
,
127 (debug_reference_descriptor
)svga_debug_describe_sampler_view
, 0);
131 SVGA_DBG(DEBUG_VIEWS
,
132 "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n",
133 pt
, min_lod
, max_lod
,
134 max_lod
- min_lod
+ 1,
141 sv
->handle
= svga_texture_view_surface(pipe
, tex
, flags
, format
,
143 max_lod
- min_lod
+ 1,
149 sv
->key
.cachable
= 0;
150 sv
->handle
= tex
->handle
;
151 debug_reference(&sv
->reference
,
152 (debug_reference_descriptor
)svga_debug_describe_sampler_view
, 0);
156 pipe_mutex_lock(ss
->tex_mutex
);
157 svga_sampler_view_reference(&tex
->cached_view
, sv
);
158 pipe_mutex_unlock(ss
->tex_mutex
);
160 debug_reference(&sv
->reference
,
161 (debug_reference_descriptor
)svga_debug_describe_sampler_view
, 0);
167 svga_validate_sampler_view(struct svga_context
*svga
, struct svga_sampler_view
*v
)
169 struct svga_texture
*tex
= svga_texture(v
->texture
);
176 if (v
->handle
== tex
->handle
)
181 if(tex
->b
.b
.target
== PIPE_TEXTURE_CUBE
)
186 for (i
= v
->min_lod
; i
<= v
->max_lod
; i
++) {
187 for (k
= 0; k
< numFaces
; k
++) {
188 if (v
->age
< tex
->view_age
[i
])
189 svga_texture_copy_handle(svga
,
190 tex
->handle
, 0, 0, 0, i
, k
,
191 v
->handle
, 0, 0, 0, i
- v
->min_lod
, k
,
192 u_minify(tex
->b
.b
.width0
, i
),
193 u_minify(tex
->b
.b
.height0
, i
),
194 u_minify(tex
->b
.b
.depth0
, i
));
202 svga_destroy_sampler_view_priv(struct svga_sampler_view
*v
)
204 struct svga_texture
*tex
= svga_texture(v
->texture
);
206 if(v
->handle
!= tex
->handle
) {
207 struct svga_screen
*ss
= svga_screen(v
->texture
->screen
);
208 SVGA_DBG(DEBUG_DMA
, "unref sid %p (sampler view)\n", v
->handle
);
209 svga_screen_surface_destroy(ss
, &v
->key
, &v
->handle
);
211 pipe_resource_reference(&v
->texture
, NULL
);