1 /**************************************************************************
3 * Copyright 2010 Luca Barbieri
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:
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.
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.
25 **************************************************************************/
27 #include "util/u_staging.h"
28 #include "pipe/p_context.h"
29 #include "util/u_memory.h"
30 #include "util/u_inlines.h"
33 util_staging_resource_template(struct pipe_resource
*pt
, unsigned width
, unsigned height
, unsigned depth
, struct pipe_resource
*template)
35 memset(template, 0, sizeof(struct pipe_resource
));
36 if(pt
->target
!= PIPE_BUFFER
&& depth
<= 1)
37 template->target
= PIPE_TEXTURE_RECT
;
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
;
47 template->usage
= PIPE_USAGE_STAGING
;
51 struct util_staging_transfer
*
52 util_staging_transfer_init(struct pipe_context
*pipe
,
53 struct pipe_resource
*pt
,
54 struct pipe_subresource sr
,
56 const struct pipe_box
*box
,
57 bool direct
, struct util_staging_transfer
*tx
)
59 struct pipe_screen
*pscreen
= pipe
->screen
;
61 struct pipe_resource staging_resource_template
;
63 pipe_resource_reference(&tx
->base
.resource
, pt
);
65 tx
->base
.usage
= usage
;
70 tx
->staging_resource
= pt
;
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
)
78 pipe_resource_reference(&tx
->base
.resource
, NULL
);
83 if (usage
& PIPE_TRANSFER_READ
)
85 struct pipe_subresource dstsr
;
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
);
97 util_staging_transfer_destroy(struct pipe_context
*pipe
, struct pipe_transfer
*ptx
)
99 struct util_staging_transfer
*tx
= (struct util_staging_transfer
*)ptx
;
101 if (tx
->staging_resource
!= tx
->base
.resource
)
103 if(tx
->base
.usage
& PIPE_TRANSFER_WRITE
) {
104 struct pipe_subresource srcsr
;
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
);
112 pipe_resource_reference(&tx
->staging_resource
, NULL
);
115 pipe_resource_reference(&ptx
->resource
, NULL
);