1 /*
2 * Copyright 2013 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 /**
27 * VGPU10 sampler and sampler view functions.
28 */
29
30
31 #include "pipe/p_defines.h"
32 #include "util/u_bitmask.h"
33 #include "util/format/u_format.h"
34 #include "util/u_inlines.h"
35 #include "util/u_math.h"
36 #include "util/u_memory.h"
37
38 #include "svga_cmd.h"
39 #include "svga_context.h"
40 #include "svga_format.h"
41 #include "svga_resource_buffer.h"
42 #include "svga_resource_texture.h"
43 #include "svga_sampler_view.h"
44 #include "svga_shader.h"
45 #include "svga_state.h"
46 #include "svga_surface.h"
47 #include "svga3d_surfacedefs.h"
48
49 /** Get resource handle for a texture or buffer */
50 static inline struct svga_winsys_surface *
svga_resource_handle(struct pipe_resource * res)51 svga_resource_handle(struct pipe_resource *res)
52 {
53 if (res->target == PIPE_BUFFER) {
54 return svga_buffer(res)->handle;
55 }
56 else {
57 return svga_texture(res)->handle;
58 }
59 }
60
61
62 /**
63 * This helper function returns TRUE if the specified resource collides with
64 * any of the resources bound to any of the currently bound sampler views.
65 */
66 boolean
svga_check_sampler_view_resource_collision(const struct svga_context * svga,const struct svga_winsys_surface * res,enum pipe_shader_type shader)67 svga_check_sampler_view_resource_collision(const struct svga_context *svga,
68 const struct svga_winsys_surface *res,
69 enum pipe_shader_type shader)
70 {
71 struct pipe_screen *screen = svga->pipe.screen;
72 unsigned i;
73
74 if (svga_screen(screen)->debug.no_surface_view) {
75 return FALSE;
76 }
77
78 for (i = 0; i < svga->curr.num_sampler_views[shader]; i++) {
79 struct svga_pipe_sampler_view *sv =
80 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
81
82 if (sv && res == svga_resource_handle(sv->base.texture)) {
83 return TRUE;
84 }
85 }
86
87 return FALSE;
88 }
89
90
91 /**
92 * Check if there are any resources that are both bound to a render target
93 * and bound as a shader resource for the given type of shader.
94 */
95 boolean
svga_check_sampler_framebuffer_resource_collision(struct svga_context * svga,enum pipe_shader_type shader)96 svga_check_sampler_framebuffer_resource_collision(struct svga_context *svga,
97 enum pipe_shader_type shader)
98 {
99 struct svga_surface *surf;
100 unsigned i;
101
102 for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
103 surf = svga_surface(svga->curr.framebuffer.cbufs[i]);
104 if (surf &&
105 svga_check_sampler_view_resource_collision(svga, surf->handle,
106 shader)) {
107 return TRUE;
108 }
109 }
110
111 surf = svga_surface(svga->curr.framebuffer.zsbuf);
112 if (surf &&
113 svga_check_sampler_view_resource_collision(svga, surf->handle, shader)) {
114 return TRUE;
115 }
116
117 return FALSE;
118 }
119
120
121 /**
122 * Create a DX ShaderResourceSamplerView for the given pipe_sampler_view,
123 * if needed.
124 */
125 enum pipe_error
svga_validate_pipe_sampler_view(struct svga_context * svga,struct svga_pipe_sampler_view * sv)126 svga_validate_pipe_sampler_view(struct svga_context *svga,
127 struct svga_pipe_sampler_view *sv)
128 {
129 enum pipe_error ret = PIPE_OK;
130
131 if (sv->id == SVGA3D_INVALID_ID) {
132 struct svga_screen *ss = svga_screen(svga->pipe.screen);
133 struct pipe_resource *texture = sv->base.texture;
134 struct svga_winsys_surface *surface;
135 SVGA3dSurfaceFormat format;
136 SVGA3dResourceType resourceDim;
137 SVGA3dShaderResourceViewDesc viewDesc;
138 enum pipe_format viewFormat = sv->base.format;
139
140 /* vgpu10 cannot create a BGRX view for a BGRA resource, so force it to
141 * create a BGRA view (and vice versa).
142 */
143 if (viewFormat == PIPE_FORMAT_B8G8R8X8_UNORM &&
144 svga_texture_device_format_has_alpha(texture)) {
145 viewFormat = PIPE_FORMAT_B8G8R8A8_UNORM;
146 }
147 else if (viewFormat == PIPE_FORMAT_B8G8R8A8_UNORM &&
148 !svga_texture_device_format_has_alpha(texture)) {
149 viewFormat = PIPE_FORMAT_B8G8R8X8_UNORM;
150 }
151
152 if (texture->target == PIPE_BUFFER) {
153 unsigned pf_flags;
154 svga_translate_texture_buffer_view_format(viewFormat,
155 &format,
156 &pf_flags);
157 surface = svga_buffer_handle(svga, texture, PIPE_BIND_SAMPLER_VIEW);
158 }
159 else {
160 format = svga_translate_format(ss, viewFormat,
161 PIPE_BIND_SAMPLER_VIEW);
162
163 /* Convert the format to a sampler-friendly format, if needed */
164 format = svga_sampler_format(format);
165
166 surface = svga_texture(texture)->handle;
167 }
168
169 assert(format != SVGA3D_FORMAT_INVALID);
170
171 if (texture->target == PIPE_BUFFER) {
172 unsigned elem_size = util_format_get_blocksize(sv->base.format);
173
174 viewDesc.buffer.firstElement = sv->base.u.buf.offset / elem_size;
175 viewDesc.buffer.numElements = sv->base.u.buf.size / elem_size;
176 }
177 else {
178 viewDesc.tex.mostDetailedMip = sv->base.u.tex.first_level;
179 viewDesc.tex.firstArraySlice = sv->base.u.tex.first_layer;
180 viewDesc.tex.mipLevels = (sv->base.u.tex.last_level -
181 sv->base.u.tex.first_level + 1);
182 }
183
184 /* arraySize in viewDesc specifies the number of array slices in a
185 * texture array. For 3D texture, last_layer in
186 * pipe_sampler_view specifies the last slice of the texture
187 * which is different from the last slice in a texture array,
188 * hence we need to set arraySize to 1 explicitly.
189 */
190 viewDesc.tex.arraySize =
191 (texture->target == PIPE_TEXTURE_3D ||
192 texture->target == PIPE_BUFFER) ? 1 :
193 (sv->base.u.tex.last_layer - sv->base.u.tex.first_layer + 1);
194
195 switch (texture->target) {
196 case PIPE_BUFFER:
197 resourceDim = SVGA3D_RESOURCE_BUFFER;
198 break;
199 case PIPE_TEXTURE_1D:
200 case PIPE_TEXTURE_1D_ARRAY:
201 resourceDim = SVGA3D_RESOURCE_TEXTURE1D;
202 break;
203 case PIPE_TEXTURE_RECT:
204 case PIPE_TEXTURE_2D:
205 case PIPE_TEXTURE_2D_ARRAY:
206 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
207 break;
208 case PIPE_TEXTURE_3D:
209 resourceDim = SVGA3D_RESOURCE_TEXTURE3D;
210 break;
211 case PIPE_TEXTURE_CUBE:
212 case PIPE_TEXTURE_CUBE_ARRAY:
213 resourceDim = SVGA3D_RESOURCE_TEXTURECUBE;
214 break;
215
216 default:
217 assert(!"Unexpected texture type");
218 resourceDim = SVGA3D_RESOURCE_TEXTURE2D;
219 }
220
221 sv->id = util_bitmask_add(svga->sampler_view_id_bm);
222
223 ret = SVGA3D_vgpu10_DefineShaderResourceView(svga->swc,
224 sv->id,
225 surface,
226 format,
227 resourceDim,
228 &viewDesc);
229 if (ret != PIPE_OK) {
230 util_bitmask_clear(svga->sampler_view_id_bm, sv->id);
231 sv->id = SVGA3D_INVALID_ID;
232 }
233 }
234
235 return ret;
236 }
237
238
239 static enum pipe_error
update_sampler_resources(struct svga_context * svga,uint64_t dirty)240 update_sampler_resources(struct svga_context *svga, uint64_t dirty)
241 {
242 enum pipe_error ret = PIPE_OK;
243 enum pipe_shader_type shader;
244
245 assert(svga_have_vgpu10(svga));
246
247 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_TESS_EVAL; shader++) {
248 SVGA3dShaderResourceViewId ids[PIPE_MAX_SAMPLERS];
249 struct svga_winsys_surface *surfaces[PIPE_MAX_SAMPLERS];
250 struct pipe_sampler_view *sampler_views[PIPE_MAX_SAMPLERS];
251 unsigned count;
252 unsigned nviews;
253 unsigned i;
254
255 count = svga->curr.num_sampler_views[shader];
256 for (i = 0; i < count; i++) {
257 struct svga_pipe_sampler_view *sv =
258 svga_pipe_sampler_view(svga->curr.sampler_views[shader][i]);
259
260 if (sv) {
261 surfaces[i] = svga_resource_handle(sv->base.texture);
262
263 ret = svga_validate_pipe_sampler_view(svga, sv);
264 if (ret != PIPE_OK)
265 return ret;
266
267 assert(sv->id != SVGA3D_INVALID_ID);
268 ids[i] = sv->id;
269 sampler_views[i] = &sv->base;
270 }
271 else {
272 surfaces[i] = NULL;
273 ids[i] = SVGA3D_INVALID_ID;
274 sampler_views[i] = NULL;
275 }
276 }
277
278 for (; i < svga->state.hw_draw.num_sampler_views[shader]; i++) {
279 ids[i] = SVGA3D_INVALID_ID;
280 surfaces[i] = NULL;
281 sampler_views[i] = NULL;
282 }
283
284 /* Number of ShaderResources that need to be modified. This includes
285 * the one that need to be unbound.
286 */
287 nviews = MAX2(svga->state.hw_draw.num_sampler_views[shader], count);
288 if (nviews > 0) {
289 if (count != svga->state.hw_draw.num_sampler_views[shader] ||
290 memcmp(sampler_views, svga->state.hw_draw.sampler_views[shader],
291 count * sizeof(sampler_views[0])) != 0) {
292 SVGA3dShaderResourceViewId *pIds = ids;
293 struct svga_winsys_surface **pSurf = surfaces;
294 unsigned numSR = 0;
295
296 /* Loop through the sampler view list to only emit
297 * the sampler views that are not already in the
298 * corresponding entries in the device's
299 * shader resource list.
300 */
301 for (i = 0; i < nviews; i++) {
302 boolean emit;
303
304 emit = sampler_views[i] ==
305 svga->state.hw_draw.sampler_views[shader][i];
306
307 if (!emit && i == nviews-1) {
308 /* Include the last sampler view in the next emit
309 * if it is different.
310 */
311 emit = TRUE;
312 numSR++;
313 i++;
314 }
315
316 if (emit) {
317 /* numSR can only be 0 if the first entry of the list
318 * is the same as the one in the device list.
319 * In this case, * there is nothing to send yet.
320 */
321 if (numSR) {
322 ret = SVGA3D_vgpu10_SetShaderResources(
323 svga->swc,
324 svga_shader_type(shader),
325 i - numSR, /* startView */
326 numSR,
327 pIds,
328 pSurf);
329
330 if (ret != PIPE_OK)
331 return ret;
332 }
333 pIds += (numSR + 1);
334 pSurf += (numSR + 1);
335 numSR = 0;
336 }
337 else
338 numSR++;
339 }
340
341 /* Save referenced sampler views in the hw draw state. */
342 svga->state.hw_draw.num_sampler_views[shader] = count;
343 for (i = 0; i < nviews; i++) {
344 pipe_sampler_view_reference(
345 &svga->state.hw_draw.sampler_views[shader][i],
346 sampler_views[i]);
347 }
348 }
349 }
350 }
351
352 /* Handle polygon stipple sampler view */
353 if (svga->curr.rast->templ.poly_stipple_enable) {
354 const unsigned unit =
355 svga_fs_variant(svga->state.hw_draw.fs)->pstipple_sampler_unit;
356 struct svga_pipe_sampler_view *sv = svga->polygon_stipple.sampler_view;
357 struct svga_winsys_surface *surface;
358
359 assert(sv);
360 if (!sv) {
361 return PIPE_OK; /* probably out of memory */
362 }
363
364 ret = svga_validate_pipe_sampler_view(svga, sv);
365 if (ret != PIPE_OK)
366 return ret;
367
368 surface = svga_resource_handle(sv->base.texture);
369 ret = SVGA3D_vgpu10_SetShaderResources(
370 svga->swc,
371 svga_shader_type(PIPE_SHADER_FRAGMENT),
372 unit, /* startView */
373 1,
374 &sv->id,
375 &surface);
376 }
377 return ret;
378 }
379
380
381 struct svga_tracked_state svga_hw_sampler_bindings = {
382 "shader resources emit",
383 SVGA_NEW_STIPPLE |
384 SVGA_NEW_TEXTURE_BINDING,
385 update_sampler_resources
386 };
387
388
389
390 static enum pipe_error
update_samplers(struct svga_context * svga,uint64_t dirty)391 update_samplers(struct svga_context *svga, uint64_t dirty )
392 {
393 enum pipe_error ret = PIPE_OK;
394 enum pipe_shader_type shader;
395
396 assert(svga_have_vgpu10(svga));
397
398 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_TESS_EVAL; shader++) {
399 const unsigned count = svga->curr.num_samplers[shader];
400 SVGA3dSamplerId ids[PIPE_MAX_SAMPLERS];
401 unsigned i;
402 unsigned nsamplers;
403
404 for (i = 0; i < count; i++) {
405 bool fs_shadow = false;
406
407 /* _NEW_FS */
408 if (shader == PIPE_SHADER_FRAGMENT) {
409 struct svga_fs_variant *fs =
410 svga_fs_variant(svga->state.hw_draw.fs);
411 /* If the fragment shader is doing the shadow comparison
412 * for this texture unit, don't enable shadow compare in
413 * the texture sampler state.
414 */
415 if (fs && (fs->fs_shadow_compare_units & (1 << i))) {
416 fs_shadow = true;
417 }
418 }
419
420 if (svga->curr.sampler[shader][i]) {
421 ids[i] = svga->curr.sampler[shader][i]->id[fs_shadow];
422 assert(ids[i] != SVGA3D_INVALID_ID);
423 }
424 else {
425 ids[i] = SVGA3D_INVALID_ID;
426 }
427 }
428
429 for (; i < svga->state.hw_draw.num_samplers[shader]; i++) {
430 ids[i] = SVGA3D_INVALID_ID;
431 }
432
433 nsamplers = MAX2(svga->state.hw_draw.num_samplers[shader], count);
434 if (nsamplers > 0) {
435 if (count != svga->state.hw_draw.num_samplers[shader] ||
436 memcmp(ids, svga->state.hw_draw.samplers[shader],
437 count * sizeof(ids[0])) != 0) {
438 /* HW state is really changing */
439 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
440 nsamplers,
441 0, /* start */
442 svga_shader_type(shader), /* type */
443 ids);
444 if (ret != PIPE_OK)
445 return ret;
446 memcpy(svga->state.hw_draw.samplers[shader], ids,
447 nsamplers * sizeof(ids[0]));
448 svga->state.hw_draw.num_samplers[shader] = count;
449 }
450 }
451 }
452
453 /* Handle polygon stipple sampler texture */
454 if (svga->curr.rast->templ.poly_stipple_enable) {
455 const unsigned unit =
456 svga_fs_variant(svga->state.hw_draw.fs)->pstipple_sampler_unit;
457 struct svga_sampler_state *sampler = svga->polygon_stipple.sampler;
458
459 assert(sampler);
460 if (!sampler) {
461 return PIPE_OK; /* probably out of memory */
462 }
463
464 if (svga->state.hw_draw.samplers[PIPE_SHADER_FRAGMENT][unit]
465 != sampler->id[0]) {
466 ret = SVGA3D_vgpu10_SetSamplers(svga->swc,
467 1, /* count */
468 unit, /* start */
469 SVGA3D_SHADERTYPE_PS,
470 &sampler->id[0]);
471 if (ret != PIPE_OK)
472 return ret;
473
474 /* save the polygon stipple sampler in the hw draw state */
475 svga->state.hw_draw.samplers[PIPE_SHADER_FRAGMENT][unit] =
476 sampler->id[0];
477 }
478 }
479
480 return ret;
481 }
482
483
484 struct svga_tracked_state svga_hw_sampler = {
485 "texture sampler emit",
486 (SVGA_NEW_FS |
487 SVGA_NEW_SAMPLER |
488 SVGA_NEW_STIPPLE),
489 update_samplers
490 };
491