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->array_size
= 1;
45 template->last_level
= 0;
46 template->nr_samples
= pt
->nr_samples
;
48 template->usage
= PIPE_USAGE_STAGING
;
52 struct util_staging_transfer
*
53 util_staging_transfer_init(struct pipe_context
*pipe
,
54 struct pipe_resource
*pt
,
57 const struct pipe_box
*box
,
58 boolean direct
, struct util_staging_transfer
*tx
)
60 struct pipe_screen
*pscreen
= pipe
->screen
;
62 struct pipe_resource staging_resource_template
;
64 pipe_resource_reference(&tx
->base
.resource
, pt
);
65 tx
->base
.level
= level
;
66 tx
->base
.usage
= usage
;
71 tx
->staging_resource
= pt
;
75 util_staging_resource_template(pt
, box
->width
, box
->height
, box
->depth
, &staging_resource_template
);
76 tx
->staging_resource
= pscreen
->resource_create(pscreen
, &staging_resource_template
);
77 if (!tx
->staging_resource
)
79 pipe_resource_reference(&tx
->base
.resource
, NULL
);
84 if (usage
& PIPE_TRANSFER_READ
)
86 /* XXX this looks wrong dst is always the same but looping over src z? */
92 sbox
.width
= box
->width
;
93 sbox
.height
= box
->height
;
95 for(zi
= 0; zi
< box
->depth
; ++zi
) {
97 pipe
->resource_copy_region(pipe
, tx
->staging_resource
, 0, 0, 0, 0,
98 tx
->base
.resource
, level
, &sbox
);
106 util_staging_transfer_destroy(struct pipe_context
*pipe
, struct pipe_transfer
*ptx
)
108 struct util_staging_transfer
*tx
= (struct util_staging_transfer
*)ptx
;
110 if (tx
->staging_resource
!= tx
->base
.resource
)
112 if(tx
->base
.usage
& PIPE_TRANSFER_WRITE
) {
113 /* XXX this looks wrong src is always the same but looping over dst z? */
115 struct pipe_box sbox
;
119 sbox
.width
= tx
->base
.box
.width
;
120 sbox
.height
= tx
->base
.box
.height
;
122 for(zi
= 0; zi
< tx
->base
.box
.depth
; ++zi
)
123 pipe
->resource_copy_region(pipe
, tx
->base
.resource
, tx
->base
.level
, tx
->base
.box
.x
, tx
->base
.box
.y
, tx
->base
.box
.z
+ zi
,
124 tx
->staging_resource
, 0, &sbox
);
127 pipe_resource_reference(&tx
->staging_resource
, NULL
);
130 pipe_resource_reference(&ptx
->resource
, NULL
);