Merge remote branch 'origin/master' into nv50-compiler
[mesa.git] / src / gallium / auxiliary / util / u_staging.c
1 /**************************************************************************
2 *
3 * Copyright 2010 Luca Barbieri
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #include "util/u_staging.h"
28 #include "pipe/p_context.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
31
32 static void
33 util_staging_resource_template(struct pipe_resource *pt, unsigned width, unsigned height, unsigned depth, struct pipe_resource *template)
34 {
35 memset(template, 0, sizeof(struct pipe_resource));
36 if(pt->target != PIPE_BUFFER && depth <= 1)
37 template->target = PIPE_TEXTURE_RECT;
38 else
39 template->target = pt->target;
40 template->format = pt->format;
41 template->width0 = width;
42 template->height0 = height;
43 template->depth0 = depth;
44 template->last_level = 0;
45 template->nr_samples = pt->nr_samples;
46 template->bind = 0;
47 template->usage = PIPE_USAGE_STAGING;
48 template->flags = 0;
49 }
50
51 struct util_staging_transfer *
52 util_staging_transfer_init(struct pipe_context *pipe,
53 struct pipe_resource *pt,
54 struct pipe_subresource sr,
55 unsigned usage,
56 const struct pipe_box *box,
57 bool direct, struct util_staging_transfer *tx)
58 {
59 struct pipe_screen *pscreen = pipe->screen;
60
61 struct pipe_resource staging_resource_template;
62
63 pipe_resource_reference(&tx->base.resource, pt);
64 tx->base.sr = sr;
65 tx->base.usage = usage;
66 tx->base.box = *box;
67
68 if (direct)
69 {
70 tx->staging_resource = pt;
71 return tx;
72 }
73
74 util_staging_resource_template(pt, box->width, box->height, box->depth, &staging_resource_template);
75 tx->staging_resource = pscreen->resource_create(pscreen, &staging_resource_template);
76 if (!tx->staging_resource)
77 {
78 pipe_resource_reference(&tx->base.resource, NULL);
79 FREE(tx);
80 return NULL;
81 }
82
83 if (usage & PIPE_TRANSFER_READ)
84 {
85 struct pipe_subresource dstsr;
86 unsigned zi;
87 dstsr.face = 0;
88 dstsr.level = 0;
89 for(zi = 0; zi < box->depth; ++zi)
90 pipe->resource_copy_region(pipe, tx->staging_resource, dstsr, 0, 0, 0, tx->base.resource, sr, box->x, box->y, box->z + zi, box->width, box->height);
91 }
92
93 return tx;
94 }
95
96 void
97 util_staging_transfer_destroy(struct pipe_context *pipe, struct pipe_transfer *ptx)
98 {
99 struct util_staging_transfer *tx = (struct util_staging_transfer *)ptx;
100
101 if (tx->staging_resource != tx->base.resource)
102 {
103 if(tx->base.usage & PIPE_TRANSFER_WRITE) {
104 struct pipe_subresource srcsr;
105 unsigned zi;
106 srcsr.face = 0;
107 srcsr.level = 0;
108 for(zi = 0; zi < tx->base.box.depth; ++zi)
109 pipe->resource_copy_region(pipe, tx->base.resource, tx->base.sr, tx->base.box.x, tx->base.box.y, tx->base.box.z + zi, tx->staging_resource, srcsr, 0, 0, 0, tx->base.box.width, tx->base.box.height);
110 }
111
112 pipe_resource_reference(&tx->staging_resource, NULL);
113 }
114
115 pipe_resource_reference(&ptx->resource, NULL);
116 FREE(ptx);
117 }