486d900e117b5ffe3f050e98ee42351675c0c3d2
[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 SVGA3dSurfaceFlags 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 struct svga_host_surface_cache_key *key) /* OUT */
162 {
163 struct svga_screen *ss = svga_screen(svga->pipe.screen);
164 struct svga_winsys_surface *handle;
165 boolean validated;
166
167 SVGA_DBG(DEBUG_PERF,
168 "svga: Create surface view: layer %d zslice %d mips %d..%d\n",
169 layer_pick, zslice_pick, start_mip, start_mip+num_mip-1);
170
171 key->flags = flags;
172 key->format = format;
173 key->numMipLevels = num_mip;
174 key->size.width = u_minify(tex->b.b.width0, start_mip);
175 key->size.height = u_minify(tex->b.b.height0, start_mip);
176 key->size.depth = zslice_pick < 0 ? u_minify(tex->b.b.depth0, start_mip) : 1;
177 key->cachable = 1;
178 key->arraySize = 1;
179 key->numFaces = 1;
180
181 /* single sample surface can be treated as non-multisamples surface */
182 key->sampleCount = tex->b.b.nr_samples > 1 ? tex->b.b.nr_samples : 0;
183
184 if (key->sampleCount > 1) {
185 key->flags |= SVGA3D_SURFACE_MASKABLE_ANTIALIAS;
186 }
187
188 if (tex->b.b.target == PIPE_TEXTURE_CUBE && layer_pick < 0) {
189 key->flags |= SVGA3D_SURFACE_CUBEMAP;
190 key->numFaces = 6;
191 } else if (tex->b.b.target == PIPE_TEXTURE_1D_ARRAY ||
192 tex->b.b.target == PIPE_TEXTURE_2D_ARRAY) {
193 key->arraySize = num_layers;
194 }
195
196 if (key->format == SVGA3D_FORMAT_INVALID) {
197 key->cachable = 0;
198 return NULL;
199 }
200
201 SVGA_DBG(DEBUG_DMA, "surface_create for texture view\n");
202 handle = svga_screen_surface_create(ss, bind_flags, PIPE_USAGE_DEFAULT,
203 &validated, key);
204 if (!handle) {
205 key->cachable = 0;
206 return NULL;
207 }
208
209 SVGA_DBG(DEBUG_DMA, " --> got sid %p (texture view)\n", handle);
210
211 if (layer_pick < 0)
212 layer_pick = 0;
213
214 svga_texture_copy_handle_resource(svga, tex, handle,
215 key->numMipLevels,
216 key->numFaces * key->arraySize,
217 zslice_pick, start_mip, layer_pick);
218
219 return handle;
220 }
221
222
223 /**
224 * A helper function to create a surface view.
225 * The view boolean flag specifies whether svga_texture_view_surface()
226 * will be called to create a cloned surface and resource for the view.
227 */
228 static struct pipe_surface *
229 svga_create_surface_view(struct pipe_context *pipe,
230 struct pipe_resource *pt,
231 const struct pipe_surface *surf_tmpl,
232 boolean view)
233 {
234 struct svga_context *svga = svga_context(pipe);
235 struct svga_texture *tex = svga_texture(pt);
236 struct pipe_screen *screen = pipe->screen;
237 struct svga_screen *ss = svga_screen(screen);
238 struct svga_surface *s;
239 unsigned layer, zslice, bind;
240 unsigned nlayers = 1;
241 SVGA3dSurfaceFlags flags = 0;
242 SVGA3dSurfaceFormat format;
243 struct pipe_surface *retVal = NULL;
244
245 s = CALLOC_STRUCT(svga_surface);
246 if (!s)
247 return NULL;
248
249 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_CREATESURFACEVIEW);
250
251 if (pt->target == PIPE_TEXTURE_CUBE) {
252 layer = surf_tmpl->u.tex.first_layer;
253 zslice = 0;
254 }
255 else if (pt->target == PIPE_TEXTURE_1D_ARRAY ||
256 pt->target == PIPE_TEXTURE_2D_ARRAY) {
257 layer = surf_tmpl->u.tex.first_layer;
258 zslice = 0;
259 nlayers = surf_tmpl->u.tex.last_layer - surf_tmpl->u.tex.first_layer + 1;
260 }
261 else {
262 layer = 0;
263 zslice = surf_tmpl->u.tex.first_layer;
264 }
265
266 pipe_reference_init(&s->base.reference, 1);
267 pipe_resource_reference(&s->base.texture, pt);
268 s->base.context = pipe;
269 s->base.format = surf_tmpl->format;
270 s->base.width = u_minify(pt->width0, surf_tmpl->u.tex.level);
271 s->base.height = u_minify(pt->height0, surf_tmpl->u.tex.level);
272 s->base.u.tex.level = surf_tmpl->u.tex.level;
273 s->base.u.tex.first_layer = surf_tmpl->u.tex.first_layer;
274 s->base.u.tex.last_layer = surf_tmpl->u.tex.last_layer;
275 s->view_id = SVGA3D_INVALID_ID;
276
277 s->backed = NULL;
278
279 if (util_format_is_depth_or_stencil(surf_tmpl->format)) {
280 flags = SVGA3D_SURFACE_HINT_DEPTHSTENCIL |
281 SVGA3D_SURFACE_BIND_DEPTH_STENCIL;
282 bind = PIPE_BIND_DEPTH_STENCIL;
283 }
284 else {
285 flags = SVGA3D_SURFACE_HINT_RENDERTARGET |
286 SVGA3D_SURFACE_BIND_RENDER_TARGET;
287 bind = PIPE_BIND_RENDER_TARGET;
288 }
289
290 if (tex->imported)
291 format = tex->key.format;
292 else
293 format = svga_translate_format(ss, surf_tmpl->format, bind);
294
295 assert(format != SVGA3D_FORMAT_INVALID);
296
297 if (view) {
298 SVGA_DBG(DEBUG_VIEWS,
299 "New backed surface view: resource %p, level %u layer %u z %u, %p\n",
300 pt, surf_tmpl->u.tex.level, layer, zslice, s);
301
302 if (svga_have_vgpu10(svga)) {
303 switch (pt->target) {
304 case PIPE_TEXTURE_1D:
305 flags |= SVGA3D_SURFACE_1D;
306 break;
307 case PIPE_TEXTURE_1D_ARRAY:
308 flags |= SVGA3D_SURFACE_1D | SVGA3D_SURFACE_ARRAY;
309 break;
310 case PIPE_TEXTURE_2D_ARRAY:
311 flags |= SVGA3D_SURFACE_ARRAY;
312 break;
313 case PIPE_TEXTURE_3D:
314 flags |= SVGA3D_SURFACE_VOLUME;
315 break;
316 case PIPE_TEXTURE_CUBE:
317 if (nlayers == 6)
318 flags |= SVGA3D_SURFACE_CUBEMAP;
319 break;
320 default:
321 break;
322 }
323 }
324
325 /* When we clone the surface view resource, use the format used in
326 * the creation of the original resource.
327 */
328 s->handle = svga_texture_view_surface(svga, tex, bind, flags,
329 tex->key.format,
330 surf_tmpl->u.tex.level, 1,
331 layer, nlayers, zslice, &s->key);
332 if (!s->handle) {
333 FREE(s);
334 goto done;
335 }
336
337 s->key.format = format;
338 s->real_layer = 0;
339 s->real_level = 0;
340 s->real_zslice = 0;
341 } else {
342 SVGA_DBG(DEBUG_VIEWS,
343 "New surface view: resource %p, level %u, layer %u, z %u, %p\n",
344 pt, surf_tmpl->u.tex.level, layer, zslice, s);
345
346 memset(&s->key, 0, sizeof s->key);
347 s->key.format = format;
348 s->handle = tex->handle;
349 s->real_layer = layer;
350 s->real_zslice = zslice;
351 s->real_level = surf_tmpl->u.tex.level;
352 }
353
354 svga->hud.num_surface_views++;
355 retVal = &s->base;
356
357 done:
358 SVGA_STATS_TIME_POP(ss->sws);
359 return retVal;
360 }
361
362
363 static struct pipe_surface *
364 svga_create_surface(struct pipe_context *pipe,
365 struct pipe_resource *pt,
366 const struct pipe_surface *surf_tmpl)
367 {
368 struct svga_context *svga = svga_context(pipe);
369 struct pipe_screen *screen = pipe->screen;
370 struct pipe_surface *surf = NULL;
371 boolean view = FALSE;
372
373 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_CREATESURFACE);
374
375 if (svga_screen(screen)->debug.force_surface_view)
376 view = TRUE;
377
378 if (surf_tmpl->u.tex.level != 0 &&
379 svga_screen(screen)->debug.force_level_surface_view)
380 view = TRUE;
381
382 if (pt->target == PIPE_TEXTURE_3D)
383 view = TRUE;
384
385 if (svga_have_vgpu10(svga) || svga_screen(screen)->debug.no_surface_view)
386 view = FALSE;
387
388 surf = svga_create_surface_view(pipe, pt, surf_tmpl, view);
389
390 SVGA_STATS_TIME_POP(svga_sws(svga));
391
392 return surf;
393 }
394
395
396 /**
397 * Clone the surface view and its associated resource.
398 */
399 static struct svga_surface *
400 create_backed_surface_view(struct svga_context *svga, struct svga_surface *s)
401 {
402 struct svga_texture *tex = svga_texture(s->base.texture);
403
404 SVGA_STATS_TIME_PUSH(svga_sws(svga),
405 SVGA_STATS_TIME_CREATEBACKEDSURFACEVIEW);
406
407 if (!s->backed) {
408 struct pipe_surface *backed_view;
409
410 backed_view = svga_create_surface_view(&svga->pipe,
411 &tex->b.b,
412 &s->base,
413 TRUE);
414 if (!backed_view)
415 return NULL;
416
417 s->backed = svga_surface(backed_view);
418 }
419 else {
420 /*
421 * There is already an existing backing surface, but we still need to
422 * sync the handles.
423 */
424 struct svga_surface *bs = s->backed;
425 unsigned int layer, zslice;
426
427 assert(bs->handle);
428
429 switch (tex->b.b.target) {
430 case PIPE_TEXTURE_CUBE:
431 case PIPE_TEXTURE_1D_ARRAY:
432 case PIPE_TEXTURE_2D_ARRAY:
433 layer = s->base.u.tex.first_layer;
434 zslice = 0;
435 break;
436 default:
437 layer = 0;
438 zslice = s->base.u.tex.first_layer;
439 }
440
441 svga_texture_copy_handle_resource(svga, tex, bs->handle,
442 bs->key.numMipLevels,
443 bs->key.numFaces * bs->key.arraySize,
444 zslice, s->base.u.tex.level, layer);
445 }
446
447 svga_mark_surface_dirty(&s->backed->base);
448
449 SVGA_STATS_TIME_POP(svga_sws(svga));
450
451 return s->backed;
452 }
453
454 /**
455 * Create a DX RenderTarget/DepthStencil View for the given surface,
456 * if needed.
457 */
458 struct pipe_surface *
459 svga_validate_surface_view(struct svga_context *svga, struct svga_surface *s)
460 {
461 enum pipe_error ret = PIPE_OK;
462 enum pipe_shader_type shader;
463
464 assert(svga_have_vgpu10(svga));
465 assert(s);
466
467 SVGA_STATS_TIME_PUSH(svga_sws(svga),
468 SVGA_STATS_TIME_VALIDATESURFACEVIEW);
469
470 /**
471 * DX spec explicitly specifies that no resource can be bound to a render
472 * target view and a shader resource view simultanously.
473 * So first check if the resource bound to this surface view collides with
474 * a sampler view. If so, then we will clone this surface view and its
475 * associated resource. We will then use the cloned surface view for
476 * render target.
477 */
478 for (shader = PIPE_SHADER_VERTEX; shader <= PIPE_SHADER_GEOMETRY; shader++) {
479 if (svga_check_sampler_view_resource_collision(svga, s->handle, shader)) {
480 SVGA_DBG(DEBUG_VIEWS,
481 "same resource used in shaderResource and renderTarget 0x%x\n",
482 s->handle);
483 s = create_backed_surface_view(svga, s);
484
485 if (s)
486 svga->state.hw_draw.has_backed_views = TRUE;
487
488 /* s may be null here if the function failed */
489 break;
490 }
491 }
492
493 if (s && s->view_id == SVGA3D_INVALID_ID) {
494 SVGA3dResourceType resType;
495 SVGA3dRenderTargetViewDesc desc;
496 struct svga_texture *stex = svga_texture(s->base.texture);
497
498 if (stex->validated == FALSE) {
499 assert(stex->handle);
500
501 /* We are about to render into a surface that has not been validated.
502 * First invalidate the surface so that the device does not
503 * need to update the host-side copy with the invalid
504 * content when the associated mob is first bound to the surface.
505 */
506 ret = SVGA3D_InvalidateGBSurface(svga->swc, stex->handle);
507 if (ret != PIPE_OK) {
508 s = NULL;
509 goto done;
510 }
511 stex->validated = TRUE;
512 }
513
514 desc.tex.mipSlice = s->real_level;
515 desc.tex.firstArraySlice = s->real_layer + s->real_zslice;
516 desc.tex.arraySize =
517 s->base.u.tex.last_layer - s->base.u.tex.first_layer + 1;
518
519 s->view_id = util_bitmask_add(svga->surface_view_id_bm);
520
521 resType = svga_resource_type(s->base.texture->target);
522
523 if (util_format_is_depth_or_stencil(s->base.format)) {
524 ret = SVGA3D_vgpu10_DefineDepthStencilView(svga->swc,
525 s->view_id,
526 s->handle,
527 s->key.format,
528 resType,
529 &desc);
530 }
531 else {
532 SVGA3dSurfaceFormat view_format = s->key.format;
533 const struct svga_texture *stex = svga_texture(s->base.texture);
534
535 /* Can't create RGBA render target view of a RGBX surface so adjust
536 * the view format. We do something similar for texture samplers in
537 * svga_validate_pipe_sampler_view().
538 */
539 if (view_format == SVGA3D_B8G8R8A8_UNORM &&
540 (stex->key.format == SVGA3D_B8G8R8X8_UNORM ||
541 stex->key.format == SVGA3D_B8G8R8X8_TYPELESS)) {
542 view_format = SVGA3D_B8G8R8X8_UNORM;
543 }
544
545 ret = SVGA3D_vgpu10_DefineRenderTargetView(svga->swc,
546 s->view_id,
547 s->handle,
548 view_format,
549 resType,
550 &desc);
551 }
552
553 if (ret != PIPE_OK) {
554 util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
555 s->view_id = SVGA3D_INVALID_ID;
556 s = NULL;
557 }
558 }
559
560 done:
561 SVGA_STATS_TIME_POP(svga_sws(svga));
562
563 return s ? &s->base : NULL;
564 }
565
566
567
568 static void
569 svga_surface_destroy(struct pipe_context *pipe,
570 struct pipe_surface *surf)
571 {
572 struct svga_context *svga = svga_context(pipe);
573 struct svga_surface *s = svga_surface(surf);
574 struct svga_texture *t = svga_texture(surf->texture);
575 struct svga_screen *ss = svga_screen(surf->texture->screen);
576 enum pipe_error ret = PIPE_OK;
577
578 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_DESTROYSURFACE);
579
580 /* Destroy the backed view surface if it exists */
581 if (s->backed) {
582 svga_surface_destroy(pipe, &s->backed->base);
583 s->backed = NULL;
584 }
585
586 if (s->handle != t->handle) {
587 SVGA_DBG(DEBUG_DMA, "unref sid %p (tex surface)\n", s->handle);
588 svga_screen_surface_destroy(ss, &s->key, &s->handle);
589 }
590
591 if (s->view_id != SVGA3D_INVALID_ID) {
592 unsigned try;
593
594 /* The SVGA3D device will generate a device error if the
595 * render target view or depth stencil view is destroyed from
596 * a context other than the one it was created with.
597 * Similar to shader resource view, in this case, we will skip
598 * the destroy for now.
599 */
600 if (surf->context != pipe) {
601 _debug_printf("context mismatch in %s\n", __func__);
602 }
603 else {
604 assert(svga_have_vgpu10(svga));
605 for (try = 0; try < 2; try++) {
606 if (util_format_is_depth_or_stencil(s->base.format)) {
607 ret = SVGA3D_vgpu10_DestroyDepthStencilView(svga->swc, s->view_id);
608 }
609 else {
610 ret = SVGA3D_vgpu10_DestroyRenderTargetView(svga->swc, s->view_id);
611 }
612 if (ret == PIPE_OK)
613 break;
614 svga_context_flush(svga, NULL);
615 }
616 assert(ret == PIPE_OK);
617 util_bitmask_clear(svga->surface_view_id_bm, s->view_id);
618 }
619 }
620
621 pipe_resource_reference(&surf->texture, NULL);
622 FREE(surf);
623
624 svga->hud.num_surface_views--;
625 SVGA_STATS_TIME_POP(ss->sws);
626 }
627
628
629 static void
630 svga_mark_surface_dirty(struct pipe_surface *surf)
631 {
632 struct svga_surface *s = svga_surface(surf);
633 struct svga_texture *tex = svga_texture(surf->texture);
634
635 if (!s->dirty) {
636 s->dirty = TRUE;
637
638 if (s->handle == tex->handle) {
639 /* hmm so 3d textures always have all their slices marked ? */
640 svga_define_texture_level(tex, surf->u.tex.first_layer,
641 surf->u.tex.level);
642 }
643 else {
644 /* this will happen later in svga_propagate_surface */
645 }
646 }
647
648 /* Increment the view_age and texture age for this surface's mipmap
649 * level so that any sampler views into the texture are re-validated too.
650 */
651 svga_age_texture_view(tex, surf->u.tex.level);
652 }
653
654
655 void
656 svga_mark_surfaces_dirty(struct svga_context *svga)
657 {
658 unsigned i;
659
660 for (i = 0; i < svga->curr.framebuffer.nr_cbufs; i++) {
661 if (svga->curr.framebuffer.cbufs[i])
662 svga_mark_surface_dirty(svga->curr.framebuffer.cbufs[i]);
663 }
664 if (svga->curr.framebuffer.zsbuf)
665 svga_mark_surface_dirty(svga->curr.framebuffer.zsbuf);
666 }
667
668
669 /**
670 * Progagate any changes from surfaces to texture.
671 * pipe is optional context to inline the blit command in.
672 */
673 void
674 svga_propagate_surface(struct svga_context *svga, struct pipe_surface *surf,
675 boolean reset)
676 {
677 struct svga_surface *s = svga_surface(surf);
678 struct svga_texture *tex = svga_texture(surf->texture);
679 struct svga_screen *ss = svga_screen(surf->texture->screen);
680
681 if (!s->dirty)
682 return;
683
684 SVGA_STATS_TIME_PUSH(ss->sws, SVGA_STATS_TIME_PROPAGATESURFACE);
685
686 /* Reset the dirty flag if specified. This is to ensure that
687 * the dirty flag will not be reset and stay unset when the backing
688 * surface is still being bound and rendered to.
689 * The reset flag will be set to TRUE when the surface is propagated
690 * and will be unbound.
691 */
692 s->dirty = !reset;
693
694 ss->texture_timestamp++;
695 svga_age_texture_view(tex, surf->u.tex.level);
696
697 if (s->handle != tex->handle) {
698 unsigned zslice, layer;
699 unsigned nlayers = 1;
700 unsigned i;
701
702 if (surf->texture->target == PIPE_TEXTURE_CUBE) {
703 zslice = 0;
704 layer = surf->u.tex.first_layer;
705 }
706 else if (surf->texture->target == PIPE_TEXTURE_1D_ARRAY ||
707 surf->texture->target == PIPE_TEXTURE_2D_ARRAY) {
708 zslice = 0;
709 layer = surf->u.tex.first_layer;
710 nlayers = surf->u.tex.last_layer - surf->u.tex.first_layer + 1;
711 }
712 else {
713 zslice = surf->u.tex.first_layer;
714 layer = 0;
715 }
716
717 SVGA_DBG(DEBUG_VIEWS,
718 "Propagate surface %p to resource %p, level %u\n",
719 surf, tex, surf->u.tex.level);
720 for (i = 0; i < nlayers; i++) {
721 svga_texture_copy_handle(svga,
722 s->handle, 0, 0, 0, s->real_level,
723 s->real_layer + i,
724 tex->handle, 0, 0, zslice, surf->u.tex.level,
725 layer + i,
726 u_minify(tex->b.b.width0, surf->u.tex.level),
727 u_minify(tex->b.b.height0, surf->u.tex.level),
728 1);
729 svga_define_texture_level(tex, layer + i, surf->u.tex.level);
730 }
731 }
732
733 SVGA_STATS_TIME_POP(ss->sws);
734 }
735
736
737 /**
738 * If any of the render targets are in backing texture views, propagate any
739 * changes to them back to the original texture.
740 */
741 void
742 svga_propagate_rendertargets(struct svga_context *svga)
743 {
744 unsigned i;
745
746 /* Early exit if there is no backing texture views in use */
747 if (!svga->state.hw_draw.has_backed_views)
748 return;
749
750 /* Note that we examine the svga->state.hw_draw.framebuffer surfaces,
751 * not the svga->curr.framebuffer surfaces, because it's the former
752 * surfaces which may be backing surface views (the actual render targets).
753 */
754 for (i = 0; i < svga->state.hw_draw.num_rendertargets; i++) {
755 struct pipe_surface *s = svga->state.hw_draw.rtv[i];
756 if (s) {
757 svga_propagate_surface(svga, s, FALSE);
758 }
759 }
760
761 if (svga->state.hw_draw.dsv) {
762 svga_propagate_surface(svga, svga->state.hw_draw.dsv, FALSE);
763 }
764 }
765
766
767 /**
768 * Check if we should call svga_propagate_surface on the surface.
769 */
770 boolean
771 svga_surface_needs_propagation(const struct pipe_surface *surf)
772 {
773 const struct svga_surface *s = svga_surface_const(surf);
774 struct svga_texture *tex = svga_texture(surf->texture);
775
776 return s->dirty && s->handle != tex->handle;
777 }
778
779
780 static void
781 svga_get_sample_position(struct pipe_context *context,
782 unsigned sample_count, unsigned sample_index,
783 float *pos_out)
784 {
785 /* We can't actually query the device to learn the sample positions.
786 * These were grabbed from nvidia's driver.
787 */
788 static const float pos1[1][2] = {
789 { 0.5, 0.5 }
790 };
791 static const float pos4[4][2] = {
792 { 0.375000, 0.125000 },
793 { 0.875000, 0.375000 },
794 { 0.125000, 0.625000 },
795 { 0.625000, 0.875000 }
796 };
797 static const float pos8[8][2] = {
798 { 0.562500, 0.312500 },
799 { 0.437500, 0.687500 },
800 { 0.812500, 0.562500 },
801 { 0.312500, 0.187500 },
802 { 0.187500, 0.812500 },
803 { 0.062500, 0.437500 },
804 { 0.687500, 0.937500 },
805 { 0.937500, 0.062500 }
806 };
807 static const float pos16[16][2] = {
808 { 0.187500, 0.062500 },
809 { 0.437500, 0.187500 },
810 { 0.062500, 0.312500 },
811 { 0.312500, 0.437500 },
812 { 0.687500, 0.062500 },
813 { 0.937500, 0.187500 },
814 { 0.562500, 0.312500 },
815 { 0.812500, 0.437500 },
816 { 0.187500, 0.562500 },
817 { 0.437500, 0.687500 },
818 { 0.062500, 0.812500 },
819 { 0.312500, 0.937500 },
820 { 0.687500, 0.562500 },
821 { 0.937500, 0.687500 },
822 { 0.562500, 0.812500 },
823 { 0.812500, 0.937500 }
824 };
825 const float (*positions)[2];
826
827 switch (sample_count) {
828 case 4:
829 positions = pos4;
830 break;
831 case 8:
832 positions = pos8;
833 break;
834 case 16:
835 positions = pos16;
836 break;
837 default:
838 positions = pos1;
839 }
840
841 pos_out[0] = positions[sample_index][0];
842 pos_out[1] = positions[sample_index][1];
843 }
844
845
846 void
847 svga_init_surface_functions(struct svga_context *svga)
848 {
849 svga->pipe.create_surface = svga_create_surface;
850 svga->pipe.surface_destroy = svga_surface_destroy;
851 svga->pipe.get_sample_position = svga_get_sample_position;
852 }