Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 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 "svga_cmd.h" |
| 27 | |
| 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" |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 35 | #include "util/u_string.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 36 | |
José Fonseca | 974b641 | 2011-04-27 12:02:08 +0100 | [diff] [blame] | 37 | #include "svga_format.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 38 | #include "svga_screen.h" |
| 39 | #include "svga_context.h" |
| 40 | #include "svga_resource_texture.h" |
| 41 | #include "svga_sampler_view.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 42 | #include "svga_debug.h" |
| 43 | #include "svga_surface.h" |
| 44 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 45 | |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 46 | void |
| 47 | svga_debug_describe_sampler_view(char *buf, const struct svga_sampler_view *sv) |
| 48 | { |
| 49 | char res[128]; |
| 50 | debug_describe_resource(res, sv->texture); |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 51 | util_sprintf(buf, "svga_sampler_view<%s,[%u,%u]>", |
| 52 | res, sv->min_lod, sv->max_lod); |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 55 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 56 | struct svga_sampler_view * |
| 57 | svga_get_tex_sampler_view(struct pipe_context *pipe, |
| 58 | struct pipe_resource *pt, |
| 59 | unsigned min_lod, unsigned max_lod) |
| 60 | { |
José Fonseca | e16e706 | 2011-02-18 19:03:08 +0000 | [diff] [blame] | 61 | struct svga_context *svga = svga_context(pipe); |
| 62 | struct svga_screen *ss = svga_screen(pipe->screen); |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 63 | struct svga_texture *tex = svga_texture(pt); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 64 | struct svga_sampler_view *sv = NULL; |
José Fonseca | 9305e93 | 2011-02-11 21:55:29 +0000 | [diff] [blame] | 65 | SVGA3dSurfaceFlags flags = SVGA3D_SURFACE_HINT_TEXTURE; |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 66 | SVGA3dSurfaceFormat format = svga_translate_format(ss, pt->format, |
| 67 | PIPE_BIND_SAMPLER_VIEW); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 68 | boolean view = TRUE; |
| 69 | |
| 70 | assert(pt); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 71 | assert(min_lod <= max_lod); |
| 72 | assert(max_lod <= pt->last_level); |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 73 | assert(!svga_have_vgpu10(svga)); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 74 | |
| 75 | /* Is a view needed */ |
| 76 | { |
| 77 | /* |
| 78 | * Can't control max lod. For first level views and when we only |
| 79 | * look at one level we disable mip filtering to achive the same |
| 80 | * results as a view. |
| 81 | */ |
| 82 | if (min_lod == 0 && max_lod >= pt->last_level) |
| 83 | view = FALSE; |
| 84 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 85 | if (ss->debug.no_sampler_view) |
| 86 | view = FALSE; |
| 87 | |
| 88 | if (ss->debug.force_sampler_view) |
| 89 | view = TRUE; |
| 90 | } |
| 91 | |
| 92 | /* First try the cache */ |
| 93 | if (view) { |
Timothy Arceri | ba72554 | 2017-03-05 12:12:30 +1100 | [diff] [blame] | 94 | mtx_lock(&ss->tex_mutex); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 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); |
Timothy Arceri | 628e84a | 2017-03-05 12:32:06 +1100 | [diff] [blame] | 99 | mtx_unlock(&ss->tex_mutex); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 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); |
| 103 | return sv; |
| 104 | } |
Timothy Arceri | 628e84a | 2017-03-05 12:32:06 +1100 | [diff] [blame] | 105 | mtx_unlock(&ss->tex_mutex); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | sv = CALLOC_STRUCT(svga_sampler_view); |
Brian Paul | b03f394 | 2013-06-21 16:09:05 -0600 | [diff] [blame] | 109 | if (!sv) |
| 110 | return NULL; |
| 111 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 112 | pipe_reference_init(&sv->reference, 1); |
Brian Paul | dce4c36 | 2012-01-11 19:52:23 +0100 | [diff] [blame] | 113 | |
| 114 | /* Note: we're not refcounting the texture resource here to avoid |
| 115 | * a circular dependency. |
| 116 | */ |
| 117 | sv->texture = pt; |
| 118 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 119 | sv->min_lod = min_lod; |
| 120 | sv->max_lod = max_lod; |
| 121 | |
| 122 | /* No view needed just use the whole texture */ |
| 123 | if (!view) { |
| 124 | SVGA_DBG(DEBUG_VIEWS, |
| 125 | "svga: Sampler view: no %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n", |
| 126 | pt, min_lod, max_lod, |
| 127 | max_lod - min_lod + 1, |
| 128 | pt->width0, |
| 129 | pt->height0, |
| 130 | pt->depth0, |
| 131 | pt->last_level); |
| 132 | sv->key.cachable = 0; |
| 133 | sv->handle = tex->handle; |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 134 | debug_reference(&sv->reference, |
| 135 | (debug_reference_descriptor)svga_debug_describe_sampler_view, 0); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 136 | return sv; |
| 137 | } |
| 138 | |
| 139 | SVGA_DBG(DEBUG_VIEWS, |
| 140 | "svga: Sampler view: yes %p, mips %u..%u, nr %u, size (%ux%ux%u), last %u\n", |
| 141 | pt, min_lod, max_lod, |
| 142 | max_lod - min_lod + 1, |
| 143 | pt->width0, |
| 144 | pt->height0, |
| 145 | pt->depth0, |
| 146 | pt->last_level); |
| 147 | |
| 148 | sv->age = tex->age; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 149 | sv->handle = svga_texture_view_surface(svga, tex, |
| 150 | PIPE_BIND_SAMPLER_VIEW, |
| 151 | flags, format, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 152 | min_lod, |
| 153 | max_lod - min_lod + 1, |
Charmaine Lee | 3626112 | 2017-04-25 14:32:59 -0600 | [diff] [blame] | 154 | -1, 1, -1, FALSE, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 155 | &sv->key); |
| 156 | |
| 157 | if (!sv->handle) { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 158 | sv->key.cachable = 0; |
| 159 | sv->handle = tex->handle; |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 160 | debug_reference(&sv->reference, |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 161 | (debug_reference_descriptor) |
| 162 | svga_debug_describe_sampler_view, 0); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 163 | return sv; |
| 164 | } |
| 165 | |
Timothy Arceri | ba72554 | 2017-03-05 12:12:30 +1100 | [diff] [blame] | 166 | mtx_lock(&ss->tex_mutex); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 167 | svga_sampler_view_reference(&tex->cached_view, sv); |
Timothy Arceri | 628e84a | 2017-03-05 12:32:06 +1100 | [diff] [blame] | 168 | mtx_unlock(&ss->tex_mutex); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 169 | |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 170 | debug_reference(&sv->reference, |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 171 | (debug_reference_descriptor) |
| 172 | svga_debug_describe_sampler_view, 0); |
Jakob Bornecrantz | 912ad88 | 2011-02-17 14:58:55 +0000 | [diff] [blame] | 173 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 174 | return sv; |
| 175 | } |
| 176 | |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 177 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 178 | void |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 179 | svga_validate_sampler_view(struct svga_context *svga, |
| 180 | struct svga_sampler_view *v) |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 181 | { |
| 182 | struct svga_texture *tex = svga_texture(v->texture); |
| 183 | unsigned numFaces; |
| 184 | unsigned age = 0; |
Brian Paul | 25cd2c2 | 2012-10-16 17:54:37 -0600 | [diff] [blame] | 185 | int i; |
| 186 | unsigned k; |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 187 | |
| 188 | assert(svga); |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 189 | assert(!svga_have_vgpu10(svga)); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 190 | |
| 191 | if (v->handle == tex->handle) |
| 192 | return; |
| 193 | |
| 194 | age = tex->age; |
| 195 | |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 196 | if (tex->b.b.target == PIPE_TEXTURE_CUBE) |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 197 | numFaces = 6; |
| 198 | else |
| 199 | numFaces = 1; |
| 200 | |
| 201 | for (i = v->min_lod; i <= v->max_lod; i++) { |
| 202 | for (k = 0; k < numFaces; k++) { |
Brian Paul | e0184b3 | 2016-04-25 09:34:40 -0600 | [diff] [blame] | 203 | assert(i < ARRAY_SIZE(tex->view_age)); |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 204 | if (v->age < tex->view_age[i]) |
José Fonseca | b845909 | 2010-05-02 23:54:42 +0100 | [diff] [blame] | 205 | svga_texture_copy_handle(svga, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 206 | tex->handle, 0, 0, 0, i, k, |
| 207 | v->handle, 0, 0, 0, i - v->min_lod, k, |
| 208 | u_minify(tex->b.b.width0, i), |
| 209 | u_minify(tex->b.b.height0, i), |
| 210 | u_minify(tex->b.b.depth0, i)); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | v->age = age; |
| 215 | } |
| 216 | |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 217 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 218 | void |
| 219 | svga_destroy_sampler_view_priv(struct svga_sampler_view *v) |
| 220 | { |
| 221 | struct svga_texture *tex = svga_texture(v->texture); |
| 222 | |
Brian Paul | 6ed8fd3 | 2015-10-06 16:55:39 -0600 | [diff] [blame] | 223 | if (v->handle != tex->handle) { |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 224 | struct svga_screen *ss = svga_screen(v->texture->screen); |
| 225 | SVGA_DBG(DEBUG_DMA, "unref sid %p (sampler view)\n", v->handle); |
| 226 | svga_screen_surface_destroy(ss, &v->key, &v->handle); |
| 227 | } |
Brian Paul | dce4c36 | 2012-01-11 19:52:23 +0100 | [diff] [blame] | 228 | |
| 229 | /* Note: we're not refcounting the texture resource here to avoid |
| 230 | * a circular dependency. |
| 231 | */ |
| 232 | v->texture = NULL; |
| 233 | |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 234 | FREE(v); |
| 235 | } |