svga: add support for cubemap array
[mesa.git] / src / gallium / drivers / svga / svga_surface.c
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_bitmask.h"
33 #include "util/u_format.h"
34 #include "util/u_math.h"
35 #include "util/u_memory.h"
36
37 #include "svga_format.h"
38 #include "svga_screen.h"
39 #include "svga_context.h"
40 #include "svga_sampler_view.h"
41 #include "svga_resource_texture.h"
42 #include "svga_surface.h"
43 #include "svga_debug.h"
44
45 static void svga_mark_surface_dirty(struct pipe_surface *surf);
46
47 void
48 svga_texture_copy_handle(struct svga_context *svga,
49 struct svga_winsys_surface *src_handle,
50 unsigned src_x, unsigned src_y, unsigned src_z,
51 unsigned src_level, unsigned src_layer,
52 struct svga_winsys_surface *dst_handle,
53 unsigned dst_x, unsigned dst_y, unsigned dst_z,
54 unsigned dst_level, unsigned dst_layer,
55 unsigned width, unsigned height, unsigned depth)
56 {
57 struct svga_surface dst, src;
58 enum pipe_error ret;
59 SVGA3dCopyBox box, *boxes;
60
61 assert(svga);
62
63 src.handle = src_handle;
64 src.real_level = src_level;
65 src.real_layer = src_layer;
66 src.real_zslice = 0;
67
68 dst.handle = dst_handle;
69 dst.real_level = dst_level;
70 dst.real_layer = dst_layer;
71 dst.real_zslice = 0;
72
73 box.x = dst_x;
74 box.y = dst_y;
75 box.z = dst_z;
76 box.w = width;
77 box.h = height;
78 box.d = depth;
79 box.srcx = src_x;
80 box.srcy = src_y;
81 box.srcz = src_z;
82
83 /*
84 SVGA_DBG(DEBUG_VIEWS, "mipcopy src: %p %u (%ux%ux%u), dst: %p %u (%ux%ux%u)\n",
85 src_handle, src_level, src_x, src_y, src_z,
86 dst_handle, dst_level, dst_x, dst_y, dst_z);
87 */
88
89 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
90 &src.base,
91 &dst.base,
92 &boxes, 1);
93 if (ret != PIPE_OK) {
94 svga_context_flush(svga, NULL);
95 ret = SVGA3D_BeginSurfaceCopy(svga->swc,
96 &src.base,
97 &dst.base,
98 &boxes, 1);
99 assert(ret == PIPE_OK);
100 }
101 *boxes = box;
102 SVGA_FIFOCommitAll(svga->swc);
103 }
104
105
106 /* A helper function to sync up the two surface handles.
107 */
108 static void
109 svga_texture_copy_handle_resource(struct svga_context *svga,
110 struct svga_texture *src_tex,
111 struct svga_winsys_surface *dst,
112 unsigned int numMipLevels,
113 unsigned int numLayers,
114 int zslice_pick,
115 unsigned int mipoffset,
116 unsigned int layeroffset)
117 {
118 unsigned int i, j;
119 unsigned int zoffset = 0;
120
121 /* A negative zslice_pick implies zoffset at 0, and depth to copy is
122 * from the depth of the texture at the particular mipmap level.
123 */
124 if (zslice_pick >= 0)
125 zoffset = zslice_pick;
126
127 for (i = 0; i < numMipLevels; i++) {
128 unsigned int miplevel = i + mipoffset;
129
130 for (j = 0; j < numLayers; j++) {
131 if (svga_is_texture_level_defined(src_tex, j+layeroffset, miplevel)) {
132 unsigned depth = (zslice_pick < 0 ?
133 u_minify(src_tex->b.b.depth0, miplevel) : 1);
134
135 svga_texture_copy_handle(svga,
136 src_tex->handle,
137 0, 0, zoffset,
138 miplevel,
139 j + layeroffset,
140 dst, 0, 0, 0, i, j,
141 u_minify(src_tex->b.b.width0, miplevel),
142 u_minify(src_tex->b.b.height0, miplevel),
143 depth);
144 }
145 }
146 }
147 }
148
149
150 struct svga_winsys_surface *
151 svga_texture_view_surface(struct svga_context *svga,
152 struct svga_texture *tex,
153 unsigned bind_flags,
154 SVGA3dSurfaceAllFlags flags,
155 SVGA3dSurfaceFormat format,
156 unsigned start_mip,
157 unsigned num_mip,
158 int layer_pick,
159 unsigned num_layers,
160 int zslice_pick,
161 boolean cacheable,
162 struct svga_host_surface_cache_key *key) /* OUT */
163 {
164 struct svga_screen *ss = svga_screen(svga->pipe.screen);
165 struct svga_winsys_surface *handle = NULL;
166 boolean validated;
167 boolean needCopyResource;
168
169 SVGA_DBG(DEBUG_PERF,
170 "svga: Create surface view: layer %d zslice %d mips %d..%d\n",
171 layer_pick, zslice_pick, start_mip, start_mip+num_mip-1);
172
173 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_EMULATESURFACEVIEW);
174
175 key->flags = flags;
176 key->format = format;
177 key->numMipLevels = num_mip;
178 key->size.width = u_minify(tex->b.b.width0, start_mip);
179 key->size.height = u_minify(tex->b.b.height0, start_mip);
180 key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
181 key->cachable = 1;
182 key->arraySize = 1;
183 key->numFaces = 1;
184
185 /* single sample surface can be treated as non-multisamples surface */
186 key->sampleCount = tex->b.b.nr_samples > 1 ? tex->b.b.nr_samples : 0;
187
188 if (key->sampleCount > 1) {
189 key->flags |= SVGA3D_SURFACE_MASKABLE_ANTIALIAS;
190 }
191
192 if (tex->b.b.target == PIPE_TEXTURE_CUBE && layer_pick < 0) {
193 key->flags |= SVGA3D_SURFACE_CUBEMAP;
194 key->numFaces = 6;
195 } else if (tex->b.b.target == PIPE_TEXTURE_1D_ARRAY ||
196 tex->b.b.target == PIPE_TEXTURE_2D_ARRAY) {
197 key->arraySize = num_layers;
198 }
199
200 if (key->format == SVGA3D_FORMAT_INVALID) {
201 key->cachable = 0;
202 goto done;
203 }
204
205 if (cacheable && tex->backed_handle &&
206 memcmp(key, &tex->backed_key, sizeof *key) == 0) {
207 handle = tex->backed_handle;
208 needCopyResource = tex->backed_age < tex->age;
209 } else {
210 SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
211 handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT,
212 &validated, key);
213 needCopyResource = TRUE;
214
215 if (cacheable && !tex->backed_handle) {
216 tex->backed_handle = handle;
217 memcpy(&tex->backed_key, key, sizeof *key);
218 }
219 }
220
221 if (!handle) {
222 key->cachable = 0;
223 goto done;
224 }
225
226 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
227
228 if (layer_pick < 0)
229 layer_pick = 0;
230
231 if (needCopyResource) {
232 svga_texture_copy_handle_resource(svga, tex, handle,
233 key->numMipLevels,
234 key->numFaces * key->arraySize,
235 zslice_pick, start_mip, layer_pick);
236 tex->backed_age = tex->age;
237 }
238
239 done:
240 SVGA_STATS_TIME_POP(ss->sws);
241
242 return handle;
243 }
244
245
246 /**
247 * A helper function to create a surface view.
248 * The view boolean flag specifies whether svga_texture_view_surface()
249 * will be called to create a cloned surface and resource for the view.
250 */
251 static struct pipe_surface *
252 svga_create_surface_view(struct pipe_context *pipe,
253 struct pipe_resource *pt,
254 const struct pipe_surface *surf_tmpl,
255 boolean view)
256 {
257 struct svga_context *svga = svga_context(pipe);
258 struct svga_texture *tex = svga_texture(pt);
259 struct pipe_screen *screen = pipe->screen;
260 struct svga_screen *ss = svga_screen(screen);
261 struct svga_surface *s;
262 unsigned layer, zslice, bind;
263 unsigned nlayers = 1;
264 SVGA3dSurfaceAllFlags flags = 0;
265 SVGA3dSurfaceFormat format;
266 struct pipe_surface *retVal = NULL;
267
268 s = CALLOC_STRUCT(svga_surface);
269 if (!s)
270 return NULL;
271
272 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATESURFACEVIEW);
273
274 if (pt->target == PIPE_TEXTURE_CUBE) {
275 layer = surf_tmpl->u.tex.first_layer;
276 zslice = 0;
277 }
278 else if (pt->target == PIPE_TEXTURE_1D_ARRAY ||
279 pt->target == PIPE_TEXTURE_2D_ARRAY ||
280 pt->target == PIPE_TEXTURE_CUBE_ARRAY) {
281 layer = surf_tmpl->u.tex.first_layer;
282 zslice = 0;
283 nlayers = surf_tmpl->u.tex.last_layer - surf_tmpl->u.tex.first_layer + 1;
284 }
285 else {
286 layer = 0;
287 zslice = surf_tmpl->u.tex.first_layer;
288 }
289
290 pipe_reference_init(&s->base.reference, 1);
291 pipe_resource_reference(&s->base.texture, pt);
292 s->base.context = pipe;
293 s->base.format = surf_tmpl->format;
294 s->base.width = u_minify(pt->width0, surf_tmpl->u.tex.level);
295 s->base.height = u_minify(pt->height0, surf_tmpl->u.tex.level);
296 s->base.u.tex.level = surf_tmpl->u.tex.level;
297 s->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
298 s->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
299 s->view_id = SVGA3D_INVALID_ID;
300
301 s->backed = NULL;
302
303 if (util_format_is_depth_or_stencil(surf_tmpl->format)) {
304 flags = SVGA3D_SURFACE_HINT_DEPTHSTENCIL |
305 SVGA3D_SURFACE_BIND_DEPTH_STENCIL;
306 bind = PIPE_BIND_DEPTH_STENCIL;
307 }
308 else {
309 flags = SVGA3D_SURFACE_HINT_RENDERTARGET |
310 SVGA3D_SURFACE_BIND_RENDER_TARGET;
311 bind = PIPE_BIND_RENDER_TARGET;
312 }
313
314 if (tex->imported) {
315 /* imported resource (a window) */
316 format = tex->key.format;
317 if (util_format_is_srgb(surf_tmpl->format)) {
318 /* sRGB rendering to window */
319 format = svga_linear_to_srgb(format);
320 }
321 }
322 else {
323 format = svga_translate_format(ss, surf_tmpl->format, bind);
324 }
325
326 assert(format != SVGA3D_FORMAT_INVALID);
327
328 if (view) {
329 SVGA_DBG(DEBUG_VIEWS,
330 "New backed surface view: resource %p, level %u layer %u z %u, %p\n",
331 pt, surf_tmpl->u.tex.level, layer, zslice, s);
332
333 if (svga_have_vgpu10(svga)) {
334 switch (pt->target) {
335 case PIPE_TEXTURE_1D:
336 flags |= SVGA3D_SURFACE_1D;
337 break;
338 case PIPE_TEXTURE_1D_ARRAY:
339 flags |= SVGA3D_SURFACE_1D | SVGA3D_SURFACE_ARRAY;
340 break;
341 case PIPE_TEXTURE_2D_ARRAY:
342 flags |= SVGA3D_SURFACE_ARRAY;
343 break;
344 case PIPE_TEXTURE_3D:
345 flags |= SVGA3D_SURFACE_VOLUME;
346 break;
347 case PIPE_TEXTURE_CUBE:
348 if (nlayers == 6)
349 flags |= SVGA3D_SURFACE_CUBEMAP;
350 break;
351 case PIPE_TEXTURE_CUBE_ARRAY:
352 flags |= SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_ARRAY;
353 break;
354 default:
355 break;
356 }
357 }
358
359 /* When we clone the surface view resource, use the format used in
360 * the creation of the original resource.
361 */
362 s->handle = svga_texture_view_surface(svga, tex, bind, flags,
363 tex->key.format,
364 surf_tmpl->u.tex.level, 1,
365 layer, nlayers, zslice,
366 TRUE, &s->key);
367 if (!s->handle) {
368 FREE(s);
369 goto done;
370 }
371
372 s->key.format = format;
373 s->real_layer = 0;
374 s->real_level = 0;
375 s->real_zslice = 0;
376 } else {
377 SVGA_DBG(DEBUG_VIEWS,
378 "New surface view: resource %p, level %u, layer %u, z %u, %p\n",
379 pt, surf_tmpl->u.tex.level, layer, zslice, s);
380
381 memset(&s->key, 0, sizeof s->key);
382 s->key.format = format;
383 s->handle = tex->handle;
384 s->real_layer = layer;
385 s->real_zslice = zslice;
386 s->real_level = surf_tmpl->u.tex.level;
387 }
388
389 svga->hud.num_surface_views++;
390 retVal = &s->base;
391
392 done:
393 SVGA_STATS_TIME_POP(ss->sws);
394 return retVal;
395 }
396
397
398 static struct pipe_surface *
399 svga_create_surface(struct pipe_context *pipe,
400 struct pipe_resource *pt,
401 const struct pipe_surface *surf_tmpl)
402 {
403 struct svga_context *svga = svga_context(pipe);
404 struct pipe_screen *screen = pipe->screen;
405 struct pipe_surface *surf = NULL;
406 boolean view = FALSE;
407
408 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATESURFACE);
409
410 if (svga_screen(screen)->debug.force_surface_view)
411 view = TRUE;
412
413 if (surf_tmpl->u.tex.level != 0 &&
414 svga_screen(screen)->debug.force_level_surface_view)
415 view = TRUE;
416
417 if (pt->target == PIPE_TEXTURE_3D)
418 view = TRUE;
419
420 if (svga_have_vgpu10(svga) || svga_screen(screen)->debug.no_surface_view)
421 view = FALSE;
422
423 surf = svga_create_surface_view(pipe, pt, surf_tmpl, view);
424
425 SVGA_STATS_TIME_POP(svga_sws(svga));
426
427 return surf;
428 }
429
430
431 /**
432 * Clone the surface view and its associated resource.
433 */
434 static struct svga_surface *
435 create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
436 {
437 struct svga_texture *tex = svga_texture(s->base.texture);
438
439 if (!s->backed) {
440 struct pipe_surface *backed_view;
441
442 SVGA_STATS_TIME_PUSH(svga_sws(svga),
443 SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
444
445 backed_view = svga_create_surface_view(&svga->pipe,
446 &tex->b.b,
447 &s->base,
448 TRUE);
449 if (!backed_view)
450 goto done;
451
452 s->backed = svga_surface(backed_view);
453
454 SVGA_STATS_TIME_POP(svga_sws(svga));
455 }
456 else if (s->backed->age < tex->age) {
457 /*
458 * There is already an existing backing surface, but we still need to
459 * sync the backing resource if the original resource has been modified
460 * since the last copy.
461 */
462 struct svga_surface *bs = s->backed;
463 unsigned int layer, zslice;
464
465 assert(bs->handle);
466
467 switch (tex->b.b.target) {
468 case PIPE_TEXTURE_CUBE:
469 case PIPE_TEXTURE_1D_ARRAY:
470 case PIPE_TEXTURE_2D_ARRAY:
471 layer = s->base.u.tex.first_layer;
472 zslice = 0;
473 break;
474 default:
475 layer = 0;
476 zslice = s->base.u.tex.first_layer;
477 }
478
479 svga_texture_copy_handle_resource(svga, tex, bs->handle,
480 bs->key.numMipLevels,
481 bs->key.numFaces * bs->key.arraySize,
482 zslice, s->base.u.tex.level, layer);
483 }
484
485 svga_mark_surface_dirty(&s->backed->base);
486 s->backed->age = tex->age;
487
488 done:
489 return s->backed;
490 }
491
492 /**
493 * Create a DX RenderTarget/DepthStencil View for the given surface,
494 * if needed.
495 */
496 struct pipe_surface *
497 svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
498 {
499 enum pipe_error ret = PIPE_OK;
500 enum pipe_shader_type shader;
501
502 assert(svga_have_vgpu10(svga));
503 assert(s);
504
505 SVGA_STATS_TIME_PUSH(svga_sws(svga),
506 SVGA_STATS_TIME_VALIDATESURFACEVIEW);
507
508 /**
509 * DX spec explicitly specifies that no resource can be bound to a render
510 * target view and a shader resource view simultanously.
511 * So first check if the resource bound to this surface view collides with
512 * a sampler view. If so, then we will clone this surface view and its
513 * associated resource. We will then use the cloned surface view for
514 * render target.
515 */
516 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
517 if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
518 SVGA_DBG(DEBUG_VIEWS,
519 "same resource used in shaderResource and renderTarget 0x%x\n",
520 s->handle);
521 s = create_backed_surface_view(svga, s);
522
523 if (s)
524 svga->state.hw_draw.has_backed_views = TRUE;
525
526 /* s may be null here if the function failed */
527 break;
528 }
529 }
530
531 if (s && s->view_id == SVGA3D_INVALID_ID) {
532 SVGA3dResourceType resType;
533 SVGA3dRenderTargetViewDesc desc;
534 struct svga_texture *stex = svga_texture(s->base.texture);
535
536 if (stex->validated == FALSE) {
537 assert(stex->handle);
538
539 /* We are about to render into a surface that has not been validated.
540 * First invalidate the surface so that the device does not
541 * need to update the host-side copy with the invalid
542 * content when the associated mob is first bound to the surface.
543 */
544 if (svga->swc->surface_invalidate(svga->swc, stex->handle) != PIPE_OK) {
545 svga_context_flush(svga, NULL);
546 ret = svga->swc->surface_invalidate(svga->swc, stex->handle);
547 assert(ret == PIPE_OK);
548 }
549 stex->validated = TRUE;
550 }
551
552 desc.tex.mipSlice = s->real_level;
553 desc.tex.firstArraySlice = s->real_layer + s->real_zslice;
554 desc.tex.arraySize =
555 s->base.u.tex.last_layer - s->base.u.tex.first_layer + 1;
556
557 s->view_id = util_bitmask_add(svga->surface_view_id_bm);
558
559 resType = svga_resource_type(s->base.texture->target);
560
561 if (util_format_is_depth_or_stencil(s->base.format)) {
562 ret = SVGA3D_vgpu10_DefineDepthStencilView(svga->swc,
563 s->view_id,
564 s->handle,
565 s->key.format,
566 resType,
567 &desc);
568 }
569 else {
570 SVGA3dSurfaceFormat view_format = s->key.format;
571 const struct svga_texture *stex = svga_texture(s->base.texture);
572
573 /* Can't create RGBA render target view of a RGBX surface so adjust
574 * the view format. We do something similar for texture samplers in
575 * svga_validate_pipe_sampler_view().
576 */
577 if (view_format == SVGA3D_B8G8R8A8_UNORM &&
578 (stex->key.format == SVGA3D_B8G8R8X8_UNORM ||
579 stex->key.format == SVGA3D_B8G8R8X8_TYPELESS)) {
580 view_format = SVGA3D_B8G8R8X8_UNORM;
581 }
582
583 ret = SVGA3D_vgpu10_DefineRenderTargetView(svga->swc,
584 s->view_id,
585 s->handle,
586 view_format,
587 resType,
588 &desc);
589 }
590
591 if (ret != PIPE_OK) {
592 util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
593 s->view_id = SVGA3D_INVALID_ID;
594 s = NULL;
595 }
596 }
597
598 SVGA_STATS_TIME_POP(svga_sws(svga));
599
600 return s ? &s->base : NULL;
601 }
602
603
604
605 static void
606 svga_surface_destroy(struct pipe_context *pipe,
607 struct pipe_surface *surf)
608 {
609 struct svga_context *svga = svga_context(pipe);
610 struct svga_surface *s = svga_surface(surf);
611 struct svga_texture *t = svga_texture(surf->texture);
612 struct svga_screen *ss = svga_screen(surf->texture->screen);
613 enum pipe_error ret = PIPE_OK;
614
615 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_DESTROYSURFACE);
616
617 /* Destroy the backed view surface if it exists */
618 if (s->backed) {
619 svga_surface_destroy(pipe, &s->backed->base);
620 s->backed = NULL;
621 }
622
623 /* Destroy the surface handle if this is a backed handle and
624 * it is not being cached in the texture.
625 */
626 if (s->handle != t->handle && s->handle != t->backed_handle) {
627 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
628 svga_screen_surface_destroy(ss, &s->key, &s->handle);
629 }
630
631 if (s->view_id != SVGA3D_INVALID_ID) {
632 unsigned try;
633
634 /* The SVGA3D device will generate a device error if the
635 * render target view or depth stencil view is destroyed from
636 * a context other than the one it was created with.
637 * Similar to shader resource view, in this case, we will skip
638 * the destroy for now.
639 */
640 if (surf->context != pipe) {
641 _debug_printf("context mismatch in %s\n", __func__);
642 }
643 else {
644 assert(svga_have_vgpu10(svga));
645 for (try = 0; try < 2; try++) {
646 if (util_format_is_depth_or_stencil(s->base.format)) {
647 ret = SVGA3D_vgpu10_DestroyDepthStencilView(svga->swc, s->view_id);
648 }
649 else {
650 ret = SVGA3D_vgpu10_DestroyRenderTargetView(svga->swc, s->view_id);
651 }
652 if (ret == PIPE_OK)
653 break;
654 svga_context_flush(svga, NULL);
655 }
656 assert(ret == PIPE_OK);
657 util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
658 }
659 }
660
661 pipe_resource_reference(&surf->texture, NULL);
662 FREE(surf);
663
664 svga->hud.num_surface_views--;
665 SVGA_STATS_TIME_POP(ss->sws);
666 }
667
668
669 static void
670 svga_mark_surface_dirty(struct pipe_surface *surf)
671 {
672 struct svga_surface *s = svga_surface(surf);
673 struct svga_texture *tex = svga_texture(surf->texture);
674
675 if (!s->dirty) {
676 s->dirty = TRUE;
677
678 if (s->handle == tex->handle) {
679 /* hmm so 3d textures always have all their slices marked ? */
680 svga_define_texture_level(tex, surf->u.tex.first_layer,
681 surf->u.tex.level);
682 }
683 else {
684 /* this will happen later in svga_propagate_surface */
685 }
686 }
687
688 /* Increment the view_age and texture age for this surface's mipmap
689 * level so that any sampler views into the texture are re-validated too.
690 * Note: we age the texture for backed surface view only when the
691 * backed surface is propagated to the original surface.
692 */
693 if (s->handle == tex->handle)
694 svga_age_texture_view(tex, surf->u.tex.level);
695 }
696
697
698 void
699 svga_mark_surfaces_dirty(struct svga_context *svga)
700 {
701 unsigned i;
702 struct svga_hw_clear_state *hw = &svga->state.hw_clear;
703
704 if (svga_have_vgpu10(svga)) {
705
706 /* For VGPU10, mark the dirty bit in the rendertarget/depth stencil view surface.
707 * This surface can be the backed surface.
708 */
709 for (i = 0; i < hw->num_rendertargets; i++) {
710 if (hw->rtv[i])
711 svga_mark_surface_dirty(hw->rtv[i]);
712 }
713 if (hw->dsv)
714 svga_mark_surface_dirty(hw->dsv);
715 } else {
716 for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
717 if (svga->curr.framebuffer.cbufs[i])
718 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
719 }
720 if (svga->curr.framebuffer.zsbuf)
721 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
722 }
723 }
724
725
726 /**
727 * Progagate any changes from surfaces to texture.
728 * pipe is optional context to inline the blit command in.
729 */
730 void
731 svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
732 boolean reset)
733 {
734 struct svga_surface *s = svga_surface(surf);
735 struct svga_texture *tex = svga_texture(surf->texture);
736 struct svga_screen *ss = svga_screen(surf->texture->screen);
737
738 if (!s->dirty)
739 return;
740
741 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_PROPAGATESURFACE);
742
743 /* Reset the dirty flag if specified. This is to ensure that
744 * the dirty flag will not be reset and stay unset when the backing
745 * surface is still being bound and rendered to.
746 * The reset flag will be set to TRUE when the surface is propagated
747 * and will be unbound.
748 */
749 s->dirty = !reset;
750
751 ss->texture_timestamp++;
752 svga_age_texture_view(tex, surf->u.tex.level);
753
754 if (s->handle != tex->handle) {
755 unsigned zslice, layer;
756 unsigned nlayers = 1;
757 unsigned i;
758
759 if (surf->texture->target == PIPE_TEXTURE_CUBE) {
760 zslice = 0;
761 layer = surf->u.tex.first_layer;
762 }
763 else if (surf->texture->target == PIPE_TEXTURE_1D_ARRAY ||
764 surf->texture->target == PIPE_TEXTURE_2D_ARRAY) {
765 zslice = 0;
766 layer = surf->u.tex.first_layer;
767 nlayers = surf->u.tex.last_layer - surf->u.tex.first_layer + 1;
768 }
769 else {
770 zslice = surf->u.tex.first_layer;
771 layer = 0;
772 }
773
774 SVGA_DBG(DEBUG_VIEWS,
775 "Propagate surface %p to resource %p, level %u\n",
776 surf, tex, surf->u.tex.level);
777 for (i = 0; i < nlayers; i++) {
778 svga_texture_copy_handle(svga,
779 s->handle, 0, 0, 0, s->real_level,
780 s->real_layer + i,
781 tex->handle, 0, 0, zslice, surf->u.tex.level,
782 layer + i,
783 u_minify(tex->b.b.width0, surf->u.tex.level),
784 u_minify(tex->b.b.height0, surf->u.tex.level),
785 1);
786 svga_define_texture_level(tex, layer + i, surf->u.tex.level);
787 }
788
789 /* Sync the surface view age with the texture age */
790 s->age = tex->age;
791
792 /* If this backed surface is cached in the texture,
793 * update the backed age as well.
794 */
795 if (tex->backed_handle == s->handle) {
796 tex->backed_age = tex->age;
797 }
798 }
799
800 SVGA_STATS_TIME_POP(ss->sws);
801 }
802
803
804 /**
805 * If any of the render targets are in backing texture views, propagate any
806 * changes to them back to the original texture.
807 */
808 void
809 svga_propagate_rendertargets(struct svga_context *svga)
810 {
811 unsigned i;
812
813 /* Early exit if there is no backing texture views in use */
814 if (!svga->state.hw_draw.has_backed_views)
815 return;
816
817 /* Note that we examine the svga->state.hw_draw.framebuffer surfaces,
818 * not the svga->curr.framebuffer surfaces, because it's the former
819 * surfaces which may be backing surface views (the actual render targets).
820 */
821 for (i = 0; i < svga->state.hw_clear.num_rendertargets; i++) {
822 struct pipe_surface *s = svga->state.hw_clear.rtv[i];
823 if (s) {
824 svga_propagate_surface(svga, s, FALSE);
825 }
826 }
827
828 if (svga->state.hw_clear.dsv) {
829 svga_propagate_surface(svga, svga->state.hw_clear.dsv, FALSE);
830 }
831 }
832
833
834 /**
835 * Check if we should call svga_propagate_surface on the surface.
836 */
837 boolean
838 svga_surface_needs_propagation(const struct pipe_surface *surf)
839 {
840 const struct svga_surface *s = svga_surface_const(surf);
841 struct svga_texture *tex = svga_texture(surf->texture);
842
843 return s->dirty && s->handle != tex->handle;
844 }
845
846
847 static void
848 svga_get_sample_position(struct pipe_context *context,
849 unsigned sample_count, unsigned sample_index,
850 float *pos_out)
851 {
852 /* We can't actually query the device to learn the sample positions.
853 * These were grabbed from nvidia's driver.
854 */
855 static const float pos1[1][2] = {
856 { 0.5, 0.5 }
857 };
858 static const float pos4[4][2] = {
859 { 0.375000, 0.125000 },
860 { 0.875000, 0.375000 },
861 { 0.125000, 0.625000 },
862 { 0.625000, 0.875000 }
863 };
864 static const float pos8[8][2] = {
865 { 0.562500, 0.312500 },
866 { 0.437500, 0.687500 },
867 { 0.812500, 0.562500 },
868 { 0.312500, 0.187500 },
869 { 0.187500, 0.812500 },
870 { 0.062500, 0.437500 },
871 { 0.687500, 0.937500 },
872 { 0.937500, 0.062500 }
873 };
874 static const float pos16[16][2] = {
875 { 0.187500, 0.062500 },
876 { 0.437500, 0.187500 },
877 { 0.062500, 0.312500 },
878 { 0.312500, 0.437500 },
879 { 0.687500, 0.062500 },
880 { 0.937500, 0.187500 },
881 { 0.562500, 0.312500 },
882 { 0.812500, 0.437500 },
883 { 0.187500, 0.562500 },
884 { 0.437500, 0.687500 },
885 { 0.062500, 0.812500 },
886 { 0.312500, 0.937500 },
887 { 0.687500, 0.562500 },
888 { 0.937500, 0.687500 },
889 { 0.562500, 0.812500 },
890 { 0.812500, 0.937500 }
891 };
892 const float (*positions)[2];
893
894 switch (sample_count) {
895 case 4:
896 positions = pos4;
897 break;
898 case 8:
899 positions = pos8;
900 break;
901 case 16:
902 positions = pos16;
903 break;
904 default:
905 positions = pos1;
906 }
907
908 pos_out[0] = positions[sample_index][0];
909 pos_out[1] = positions[sample_index][1];
910 }
911
912
913 void
914 svga_init_surface_functions(struct svga_context *svga)
915 {
916 svga->pipe.create_surface = svga_create_surface;
917 svga->pipe.surface_destroy = svga_surface_destroy;
918 svga->pipe.get_sample_position = svga_get_sample_position;
919 }