Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +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 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 26 | #include "pipe/p_defines.h" |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 27 | #include "util/u_bitmask.h" |
Brian Paul | 1a35fde | 2011-07-27 16:08:58 -0600 | [diff] [blame] | 28 | #include "util/u_format.h" |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 29 | #include "util/u_inlines.h" |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 30 | #include "util/u_math.h" |
| 31 | #include "util/u_memory.h" |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 32 | #include "tgsi/tgsi_parse.h" |
| 33 | |
| 34 | #include "svga_context.h" |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 35 | #include "svga_cmd.h" |
| 36 | #include "svga_debug.h" |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 37 | #include "svga_resource_texture.h" |
Charmaine Lee | b2fd41c | 2015-08-13 15:08:22 -0700 | [diff] [blame] | 38 | #include "svga_surface.h" |
| 39 | #include "svga_sampler_view.h" |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 40 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 41 | |
Ilia Mirkin | a2a1a58 | 2015-07-20 19:58:43 -0400 | [diff] [blame] | 42 | static inline unsigned |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 43 | translate_wrap_mode(unsigned wrap) |
| 44 | { |
| 45 | switch (wrap) { |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 46 | case PIPE_TEX_WRAP_REPEAT: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 47 | return SVGA3D_TEX_ADDRESS_WRAP; |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 48 | case PIPE_TEX_WRAP_CLAMP: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 49 | return SVGA3D_TEX_ADDRESS_CLAMP; |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 50 | case PIPE_TEX_WRAP_CLAMP_TO_EDGE: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 51 | /* Unfortunately SVGA3D_TEX_ADDRESS_EDGE not respected by |
| 52 | * hardware. |
| 53 | */ |
| 54 | return SVGA3D_TEX_ADDRESS_CLAMP; |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 55 | case PIPE_TEX_WRAP_CLAMP_TO_BORDER: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 56 | return SVGA3D_TEX_ADDRESS_BORDER; |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 57 | case PIPE_TEX_WRAP_MIRROR_REPEAT: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 58 | return SVGA3D_TEX_ADDRESS_MIRROR; |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 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: |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 62 | return SVGA3D_TEX_ADDRESS_MIRRORONCE; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 63 | default: |
| 64 | assert(0); |
| 65 | return SVGA3D_TEX_ADDRESS_WRAP; |
| 66 | } |
| 67 | } |
| 68 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 69 | |
| 70 | static inline unsigned |
| 71 | translate_img_filter(unsigned filter) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 72 | { |
| 73 | switch (filter) { |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 74 | case PIPE_TEX_FILTER_NEAREST: |
| 75 | return SVGA3D_TEX_FILTER_NEAREST; |
| 76 | case PIPE_TEX_FILTER_LINEAR: |
| 77 | return SVGA3D_TEX_FILTER_LINEAR; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 78 | default: |
| 79 | assert(0); |
| 80 | return SVGA3D_TEX_FILTER_NEAREST; |
| 81 | } |
| 82 | } |
| 83 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 84 | |
| 85 | static inline unsigned |
| 86 | translate_mip_filter(unsigned filter) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 87 | { |
| 88 | switch (filter) { |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 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; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 95 | default: |
| 96 | assert(0); |
| 97 | return SVGA3D_TEX_FILTER_NONE; |
| 98 | } |
| 99 | } |
| 100 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 101 | |
| 102 | static uint8 |
| 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 |
| 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 |
| 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 | unsigned try; |
| 170 | float min_lod, max_lod; |
| 171 | |
| 172 | assert(svga_have_vgpu10(svga)); |
| 173 | |
| 174 | anisotropic = ss->aniso_level > 1.0f; |
| 175 | |
| 176 | filter = translate_filter_mode(ps->min_mip_filter, |
| 177 | ps->min_img_filter, |
| 178 | ps->mag_img_filter, |
| 179 | anisotropic, |
| 180 | ss->compare_mode); |
| 181 | |
| 182 | compare_func = translate_comparison_func(ss->compare_func); |
| 183 | |
| 184 | COPY_4V(bcolor.value, ps->border_color.f); |
| 185 | |
| 186 | ss->id = util_bitmask_add(svga->sampler_object_id_bm); |
| 187 | |
| 188 | assert(ps->min_lod <= ps->max_lod); |
| 189 | |
| 190 | if (ps->min_mip_filter == PIPE_TEX_MIPFILTER_NONE) { |
| 191 | /* just use the base level image */ |
| 192 | min_lod = max_lod = 0.0f; |
| 193 | } |
| 194 | else { |
| 195 | min_lod = ps->min_lod; |
| 196 | max_lod = ps->max_lod; |
| 197 | } |
| 198 | |
| 199 | /* Loop in case command buffer is full and we need to flush and retry */ |
| 200 | for (try = 0; try < 2; try++) { |
| 201 | enum pipe_error ret = |
| 202 | SVGA3D_vgpu10_DefineSamplerState(svga->swc, |
| 203 | ss->id, |
| 204 | filter, |
| 205 | ss->addressu, |
| 206 | ss->addressv, |
| 207 | ss->addressw, |
| 208 | ss->lod_bias, /* float */ |
| 209 | max_aniso, |
| 210 | compare_func, |
| 211 | bcolor, |
| 212 | min_lod, /* float */ |
| 213 | max_lod); /* float */ |
| 214 | if (ret == PIPE_OK) |
| 215 | return; |
| 216 | svga_context_flush(svga, NULL); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 221 | static void * |
| 222 | svga_create_sampler_state(struct pipe_context *pipe, |
| 223 | const struct pipe_sampler_state *sampler) |
| 224 | { |
| 225 | struct svga_context *svga = svga_context(pipe); |
| 226 | struct svga_sampler_state *cso = CALLOC_STRUCT( svga_sampler_state ); |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 227 | |
Brian Paul | 9227c53 | 2013-01-24 16:53:33 -0700 | [diff] [blame] | 228 | if (!cso) |
| 229 | return NULL; |
| 230 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 231 | cso->mipfilter = translate_mip_filter(sampler->min_mip_filter); |
| 232 | cso->magfilter = translate_img_filter( sampler->mag_img_filter ); |
| 233 | cso->minfilter = translate_img_filter( sampler->min_img_filter ); |
Roland Scheidegger | ebe12d5 | 2010-02-12 00:43:38 +0100 | [diff] [blame] | 234 | cso->aniso_level = MAX2( sampler->max_anisotropy, 1 ); |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 235 | if (sampler->max_anisotropy) |
Luca Barbieri | f02347374 | 2010-01-06 10:35:47 +0000 | [diff] [blame] | 236 | cso->magfilter = cso->minfilter = SVGA3D_TEX_FILTER_ANISOTROPIC; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 237 | cso->lod_bias = sampler->lod_bias; |
| 238 | cso->addressu = translate_wrap_mode(sampler->wrap_s); |
| 239 | cso->addressv = translate_wrap_mode(sampler->wrap_t); |
| 240 | cso->addressw = translate_wrap_mode(sampler->wrap_r); |
| 241 | cso->normalized_coords = sampler->normalized_coords; |
| 242 | cso->compare_mode = sampler->compare_mode; |
| 243 | cso->compare_func = sampler->compare_func; |
| 244 | |
| 245 | { |
Dave Airlie | 9f61e43 | 2011-09-27 10:08:34 +0100 | [diff] [blame] | 246 | uint32 r = float_to_ubyte(sampler->border_color.f[0]); |
| 247 | uint32 g = float_to_ubyte(sampler->border_color.f[1]); |
| 248 | uint32 b = float_to_ubyte(sampler->border_color.f[2]); |
| 249 | uint32 a = float_to_ubyte(sampler->border_color.f[3]); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 250 | |
José Fonseca | 6b11938 | 2010-02-13 09:16:57 +0000 | [diff] [blame] | 251 | cso->bordercolor = (a << 24) | (r << 16) | (g << 8) | b; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* No SVGA3D support for: |
| 255 | * - min/max LOD clamping |
| 256 | */ |
| 257 | cso->min_lod = 0; |
Brian Paul | 5abcd19 | 2012-01-18 10:05:25 -0700 | [diff] [blame] | 258 | cso->view_min_lod = MAX2((int) (sampler->min_lod + 0.5), 0); |
| 259 | cso->view_max_lod = MAX2((int) (sampler->max_lod + 0.5), 0); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 260 | |
| 261 | /* Use min_mipmap */ |
| 262 | if (svga->debug.use_min_mipmap) { |
| 263 | if (cso->view_min_lod == cso->view_max_lod) { |
| 264 | cso->min_lod = cso->view_min_lod; |
| 265 | cso->view_min_lod = 0; |
| 266 | cso->view_max_lod = 1000; /* Just a high number */ |
| 267 | cso->mipfilter = SVGA3D_TEX_FILTER_NONE; |
| 268 | } |
| 269 | } |
| 270 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 271 | if (svga_have_vgpu10(svga)) { |
| 272 | define_sampler_state_object(svga, cso, sampler); |
| 273 | } |
| 274 | |
Brian Paul | 943f4f4 | 2017-04-10 13:48:21 -0600 | [diff] [blame] | 275 | SVGA_DBG(DEBUG_SAMPLERS, |
| 276 | "New sampler: min %u, view(min %u, max %u) lod, mipfilter %s\n", |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 277 | cso->min_lod, cso->view_min_lod, cso->view_max_lod, |
| 278 | cso->mipfilter == SVGA3D_TEX_FILTER_NONE ? "SVGA3D_TEX_FILTER_NONE" : "SOMETHING"); |
| 279 | |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 280 | svga->hud.num_sampler_objects++; |
Charmaine Lee | 2e1cfcc | 2016-08-19 08:49:17 -0600 | [diff] [blame] | 281 | SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws, |
| 282 | SVGA_STATS_COUNT_SAMPLER); |
Neha Bhende | 9bc7e31 | 2015-10-09 16:10:16 -0600 | [diff] [blame] | 283 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 284 | return cso; |
| 285 | } |
| 286 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 287 | |
Brian Paul | 5026841 | 2011-02-03 12:41:16 -0700 | [diff] [blame] | 288 | static void |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 289 | svga_bind_sampler_states(struct pipe_context *pipe, |
Kai Wasserbäch | 7413625 | 2016-08-27 04:08:00 -0600 | [diff] [blame] | 290 | enum pipe_shader_type shader, |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 291 | unsigned start, |
| 292 | unsigned num, |
| 293 | void **samplers) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 294 | { |
| 295 | struct svga_context *svga = svga_context(pipe); |
| 296 | unsigned i; |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 297 | boolean any_change = FALSE; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 298 | |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 299 | assert(shader < PIPE_SHADER_TYPES); |
| 300 | assert(start + num <= PIPE_MAX_SAMPLERS); |
| 301 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 302 | /* Pre-VGPU10 only supports FS textures */ |
| 303 | if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 304 | return; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 305 | |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 306 | for (i = 0; i < num; i++) { |
| 307 | if (svga->curr.sampler[shader][start + i] != samplers[i]) |
| 308 | any_change = TRUE; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 309 | svga->curr.sampler[shader][start + i] = samplers[i]; |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | if (!any_change) { |
| 313 | return; |
| 314 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 315 | |
Brian Paul | 82246f7 | 2014-03-24 13:53:26 -0600 | [diff] [blame] | 316 | /* find highest non-null sampler[] entry */ |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 317 | { |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 318 | unsigned j = MAX2(svga->curr.num_samplers[shader], start + num); |
| 319 | while (j > 0 && svga->curr.sampler[shader][j - 1] == NULL) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 320 | j--; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 321 | svga->curr.num_samplers[shader] = j; |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 322 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 323 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 324 | svga->dirty |= SVGA_NEW_SAMPLER; |
| 325 | } |
| 326 | |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 327 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 328 | static void |
| 329 | svga_delete_sampler_state(struct pipe_context *pipe, void *sampler) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 330 | { |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 331 | struct svga_sampler_state *ss = (struct svga_sampler_state *) sampler; |
| 332 | struct svga_context *svga = svga_context(pipe); |
| 333 | |
| 334 | if (svga_have_vgpu10(svga)) { |
| 335 | enum pipe_error ret; |
| 336 | |
| 337 | svga_hwtnl_flush_retry(svga); |
| 338 | |
| 339 | ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id); |
| 340 | if (ret != PIPE_OK) { |
| 341 | svga_context_flush(svga, NULL); |
| 342 | ret = SVGA3D_vgpu10_DestroySamplerState(svga->swc, ss->id); |
| 343 | } |
| 344 | util_bitmask_clear(svga->sampler_object_id_bm, ss->id); |
| 345 | } |
| 346 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 347 | FREE(sampler); |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 348 | svga->hud.num_sampler_objects--; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 352 | static struct pipe_sampler_view * |
| 353 | svga_create_sampler_view(struct pipe_context *pipe, |
Keith Whitwell | 287c94e | 2010-04-10 16:05:54 +0100 | [diff] [blame] | 354 | struct pipe_resource *texture, |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 355 | const struct pipe_sampler_view *templ) |
| 356 | { |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 357 | struct svga_context *svga = svga_context(pipe); |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 358 | struct svga_pipe_sampler_view *sv = CALLOC_STRUCT(svga_pipe_sampler_view); |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 359 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 360 | if (!sv) { |
| 361 | return NULL; |
Michal Krol | 530b991 | 2010-03-11 15:30:21 +0100 | [diff] [blame] | 362 | } |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 363 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 364 | sv->base = *templ; |
| 365 | sv->base.reference.count = 1; |
| 366 | sv->base.texture = NULL; |
| 367 | pipe_resource_reference(&sv->base.texture, texture); |
| 368 | |
| 369 | sv->base.context = pipe; |
| 370 | sv->id = SVGA3D_INVALID_ID; |
| 371 | |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 372 | svga->hud.num_samplerview_objects++; |
Charmaine Lee | 2e1cfcc | 2016-08-19 08:49:17 -0600 | [diff] [blame] | 373 | SVGA_STATS_COUNT_INC(svga_screen(svga->pipe.screen)->sws, |
| 374 | SVGA_STATS_COUNT_SAMPLERVIEW); |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 375 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 376 | return &sv->base; |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | |
| 380 | static void |
| 381 | svga_sampler_view_destroy(struct pipe_context *pipe, |
| 382 | struct pipe_sampler_view *view) |
| 383 | { |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 384 | struct svga_context *svga = svga_context(pipe); |
| 385 | struct svga_pipe_sampler_view *sv = svga_pipe_sampler_view(view); |
| 386 | |
| 387 | if (svga_have_vgpu10(svga) && sv->id != SVGA3D_INVALID_ID) { |
| 388 | if (view->context != pipe) { |
| 389 | /* The SVGA3D device will generate an error (and on Linux, cause |
| 390 | * us to abort) if we try to destroy a shader resource view from |
| 391 | * a context other than the one it was created with. Skip the |
| 392 | * SVGA3D_vgpu10_DestroyShaderResourceView() and leak the sampler |
| 393 | * view for now. This should only sometimes happen when a shared |
| 394 | * texture is deleted. |
| 395 | */ |
| 396 | _debug_printf("context mismatch in %s\n", __func__); |
| 397 | } |
| 398 | else { |
| 399 | enum pipe_error ret; |
| 400 | |
| 401 | svga_hwtnl_flush_retry(svga); /* XXX is this needed? */ |
| 402 | |
| 403 | ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id); |
| 404 | if (ret != PIPE_OK) { |
| 405 | svga_context_flush(svga, NULL); |
| 406 | ret = SVGA3D_vgpu10_DestroyShaderResourceView(svga->swc, sv->id); |
| 407 | } |
| 408 | util_bitmask_clear(svga->sampler_view_id_bm, sv->id); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | pipe_resource_reference(&sv->base.texture, NULL); |
| 413 | |
| 414 | FREE(sv); |
Brian Paul | 464d608 | 2016-04-15 15:30:34 -0600 | [diff] [blame] | 415 | svga->hud.num_samplerview_objects--; |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 416 | } |
| 417 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 418 | |
Brian Paul | 5026841 | 2011-02-03 12:41:16 -0700 | [diff] [blame] | 419 | static void |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 420 | svga_set_sampler_views(struct pipe_context *pipe, |
Kai Wasserbäch | 532db3b | 2016-08-27 04:08:00 -0600 | [diff] [blame] | 421 | enum pipe_shader_type shader, |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 422 | unsigned start, |
| 423 | unsigned num, |
| 424 | struct pipe_sampler_view **views) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 425 | { |
| 426 | struct svga_context *svga = svga_context(pipe); |
| 427 | unsigned flag_1d = 0; |
| 428 | unsigned flag_srgb = 0; |
Charmaine Lee | c4cb879 | 2016-04-22 16:06:32 -0700 | [diff] [blame] | 429 | unsigned flag_rect = 0; |
| 430 | unsigned flag_buf = 0; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 431 | uint i; |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 432 | boolean any_change = FALSE; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 433 | |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 434 | assert(shader < PIPE_SHADER_TYPES); |
Brian Paul | e0184b3 | 2016-04-25 09:34:40 -0600 | [diff] [blame] | 435 | assert(start + num <= ARRAY_SIZE(svga->curr.sampler_views[shader])); |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 436 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 437 | /* Pre-VGPU10 only supports FS textures */ |
| 438 | if (!svga_have_vgpu10(svga) && shader != PIPE_SHADER_FRAGMENT) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 439 | return; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 440 | |
Charmaine Lee | 2e1cfcc | 2016-08-19 08:49:17 -0600 | [diff] [blame] | 441 | SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_SETSAMPLERVIEWS); |
| 442 | |
Brian Paul | 877a802 | 2016-05-06 09:46:29 -0600 | [diff] [blame] | 443 | /* This bit of code works around a quirk in the CSO module. |
| 444 | * If start=num=0 it means all sampler views should be released. |
| 445 | * Note that the CSO module treats sampler views for fragment shaders |
| 446 | * differently than other shader types. |
| 447 | */ |
| 448 | if (start == 0 && num == 0 && svga->curr.num_sampler_views[shader] > 0) { |
| 449 | for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) { |
| 450 | pipe_sampler_view_release(pipe, &svga->curr.sampler_views[shader][i]); |
| 451 | } |
| 452 | any_change = TRUE; |
| 453 | } |
| 454 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 455 | for (i = 0; i < num; i++) { |
Charmaine Lee | c4cb879 | 2016-04-22 16:06:32 -0700 | [diff] [blame] | 456 | enum pipe_texture_target target; |
| 457 | |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 458 | if (svga->curr.sampler_views[shader][start + i] != views[i]) { |
Brian Paul | 8105842 | 2012-02-17 10:18:55 -0700 | [diff] [blame] | 459 | /* Note: we're using pipe_sampler_view_release() here to work around |
| 460 | * a possible crash when the old view belongs to another context that |
| 461 | * was already destroyed. |
| 462 | */ |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 463 | pipe_sampler_view_release(pipe, &svga->curr.sampler_views[shader][start + i]); |
| 464 | pipe_sampler_view_reference(&svga->curr.sampler_views[shader][start + i], |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 465 | views[i]); |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 466 | any_change = TRUE; |
Brian Paul | 8105842 | 2012-02-17 10:18:55 -0700 | [diff] [blame] | 467 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 468 | |
Michal Krol | f610656 | 2010-02-19 19:00:26 +0100 | [diff] [blame] | 469 | if (!views[i]) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 470 | continue; |
| 471 | |
Brian Paul | 1a35fde | 2011-07-27 16:08:58 -0600 | [diff] [blame] | 472 | if (util_format_is_srgb(views[i]->format)) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 473 | flag_srgb |= 1 << (start + i); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 474 | |
Charmaine Lee | c4cb879 | 2016-04-22 16:06:32 -0700 | [diff] [blame] | 475 | target = views[i]->texture->target; |
| 476 | if (target == PIPE_TEXTURE_1D) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 477 | flag_1d |= 1 << (start + i); |
Charmaine Lee | c4cb879 | 2016-04-22 16:06:32 -0700 | [diff] [blame] | 478 | else if (target == PIPE_TEXTURE_RECT) |
| 479 | flag_rect |= 1 << (start + i); |
| 480 | else if (target == PIPE_BUFFER) |
| 481 | flag_buf |= 1 << (start + i); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 482 | } |
| 483 | |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 484 | if (!any_change) { |
Charmaine Lee | 2e1cfcc | 2016-08-19 08:49:17 -0600 | [diff] [blame] | 485 | goto done; |
Brian Paul | b11bd20 | 2016-01-05 13:03:04 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 488 | /* find highest non-null sampler_views[] entry */ |
| 489 | { |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 490 | unsigned j = MAX2(svga->curr.num_sampler_views[shader], start + num); |
| 491 | while (j > 0 && svga->curr.sampler_views[shader][j - 1] == NULL) |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 492 | j--; |
Brian Paul | e054251 | 2015-08-13 11:00:58 -0700 | [diff] [blame] | 493 | svga->curr.num_sampler_views[shader] = j; |
Brian Paul | bd3733c | 2012-08-09 20:59:44 -0600 | [diff] [blame] | 494 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 495 | |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 496 | svga->dirty |= SVGA_NEW_TEXTURE_BINDING; |
| 497 | |
| 498 | if (flag_srgb != svga->curr.tex_flags.flag_srgb || |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 499 | flag_1d != svga->curr.tex_flags.flag_1d) { |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 500 | svga->dirty |= SVGA_NEW_TEXTURE_FLAGS; |
| 501 | svga->curr.tex_flags.flag_1d = flag_1d; |
| 502 | svga->curr.tex_flags.flag_srgb = flag_srgb; |
Charmaine Lee | b2fd41c | 2015-08-13 15:08:22 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Charmaine Lee | c4cb879 | 2016-04-22 16:06:32 -0700 | [diff] [blame] | 505 | if (flag_rect != svga->curr.tex_flags.flag_rect || |
| 506 | flag_buf != svga->curr.tex_flags.flag_buf) |
| 507 | { |
| 508 | /* Need to re-emit texture constants */ |
| 509 | svga->dirty |= SVGA_NEW_TEXTURE_CONSTS; |
| 510 | svga->curr.tex_flags.flag_rect = flag_rect; |
| 511 | svga->curr.tex_flags.flag_buf = flag_buf; |
| 512 | } |
| 513 | |
Charmaine Lee | b2fd41c | 2015-08-13 15:08:22 -0700 | [diff] [blame] | 514 | /* Check if any of the sampler view resources collide with the framebuffer |
Brian Paul | dcf6333 | 2016-08-25 18:02:43 -0600 | [diff] [blame] | 515 | * color buffers or depth stencil resource. If so, set the NEW_FRAME_BUFFER |
Charmaine Lee | b2fd41c | 2015-08-13 15:08:22 -0700 | [diff] [blame] | 516 | * dirty bit so that emit_framebuffer can be invoked to create backed view |
| 517 | * for the conflicted surface view. |
| 518 | */ |
Brian Paul | dcf6333 | 2016-08-25 18:02:43 -0600 | [diff] [blame] | 519 | if (svga_check_sampler_framebuffer_resource_collision(svga, shader)) { |
| 520 | svga->dirty |= SVGA_NEW_FRAME_BUFFER; |
Charmaine Lee | b2fd41c | 2015-08-13 15:08:22 -0700 | [diff] [blame] | 521 | } |
Charmaine Lee | 2e1cfcc | 2016-08-19 08:49:17 -0600 | [diff] [blame] | 522 | |
| 523 | done: |
| 524 | SVGA_STATS_TIME_POP(svga_sws(svga)); |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Charmaine Lee | 5313b29 | 2016-08-17 14:53:38 -0700 | [diff] [blame] | 527 | /** |
| 528 | * Clean up sampler, sampler view state at context destruction time |
| 529 | */ |
| 530 | void |
| 531 | svga_cleanup_sampler_state(struct svga_context *svga) |
| 532 | { |
Brian Paul | f5602c2 | 2016-08-29 10:15:36 -0600 | [diff] [blame] | 533 | enum pipe_shader_type shader; |
Charmaine Lee | 2781d60 | 2016-08-17 16:50:23 -0700 | [diff] [blame] | 534 | |
Charmaine Lee | 2781d60 | 2016-08-17 16:50:23 -0700 | [diff] [blame] | 535 | for (shader = 0; shader <= PIPE_SHADER_GEOMETRY; shader++) { |
| 536 | unsigned i; |
| 537 | |
| 538 | for (i = 0; i < svga->state.hw_draw.num_sampler_views[shader]; i++) { |
| 539 | pipe_sampler_view_release(&svga->pipe, |
| 540 | &svga->state.hw_draw.sampler_views[shader][i]); |
| 541 | } |
| 542 | } |
| 543 | |
Charmaine Lee | 5313b29 | 2016-08-17 14:53:38 -0700 | [diff] [blame] | 544 | /* free polygon stipple state */ |
| 545 | if (svga->polygon_stipple.sampler) { |
| 546 | svga->pipe.delete_sampler_state(&svga->pipe, svga->polygon_stipple.sampler); |
| 547 | } |
| 548 | |
| 549 | if (svga->polygon_stipple.sampler_view) { |
| 550 | svga->pipe.sampler_view_destroy(&svga->pipe, |
| 551 | &svga->polygon_stipple.sampler_view->base); |
| 552 | } |
| 553 | pipe_resource_reference(&svga->polygon_stipple.texture, NULL); |
| 554 | } |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 555 | |
Brian Paul | 595fbc8 | 2016-04-15 15:54:15 -0600 | [diff] [blame] | 556 | void |
| 557 | svga_init_sampler_functions( struct svga_context *svga ) |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 558 | { |
| 559 | svga->pipe.create_sampler_state = svga_create_sampler_state; |
Brian Paul | 0de99d5 | 2013-09-12 15:08:42 -0600 | [diff] [blame] | 560 | svga->pipe.bind_sampler_states = svga_bind_sampler_states; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 561 | svga->pipe.delete_sampler_state = svga_delete_sampler_state; |
Brian Paul | a3ed98f | 2013-10-07 18:16:22 -0600 | [diff] [blame] | 562 | svga->pipe.set_sampler_views = svga_set_sampler_views; |
Michal Krol | ad230a1 | 2010-02-23 17:03:56 +0100 | [diff] [blame] | 563 | svga->pipe.create_sampler_view = svga_create_sampler_view; |
| 564 | svga->pipe.sampler_view_destroy = svga_sampler_view_destroy; |
Jakob Bornecrantz | 3192633 | 2009-11-16 19:56:18 +0100 | [diff] [blame] | 565 | } |