363e1c864babe776ef00c6a36c2d0f8952d254cc
[mesa.git] / src / gallium / auxiliary / util / u_staging.c
1 #include "util/u_staging.h"
2 #include "pipe/p_context.h"
3 #include "util/u_memory.h"
4 #include "util/u_inlines.h"
5
6 static void
7 util_staging_resource_template(struct pipe_resource *pt, unsigned width, unsigned height, unsigned depth, struct pipe_resource *template)
8 {
9 memset(template, 0, sizeof(struct pipe_resource));
10 if(pt->target != PIPE_BUFFER && depth <= 1)
11 template->target = PIPE_TEXTURE_RECT;
12 else
13 template->target = pt->target;
14 template->format = pt->format;
15 template->width0 = width;
16 template->height0 = height;
17 template->depth0 = depth;
18 template->last_level = 0;
19 template->nr_samples = pt->nr_samples;
20 template->bind = 0;
21 template->usage = PIPE_USAGE_STAGING;
22 template->flags = 0;
23 }
24
25 struct util_staging_transfer *
26 util_staging_transfer_init(struct pipe_context *pipe,
27 struct pipe_resource *pt,
28 struct pipe_subresource sr,
29 unsigned usage,
30 const struct pipe_box *box,
31 bool direct, struct util_staging_transfer *tx)
32 {
33 struct pipe_screen *pscreen = pipe->screen;
34
35 struct pipe_resource staging_resource_template;
36
37 pipe_resource_reference(&tx->base.resource, pt);
38 tx->base.sr = sr;
39 tx->base.usage = usage;
40 tx->base.box = *box;
41
42 if (direct)
43 {
44 tx->staging_resource = pt;
45 return tx;
46 }
47
48 util_staging_resource_template(pt, box->width, box->height, box->depth, &staging_resource_template);
49 tx->staging_resource = pscreen->resource_create(pscreen, &staging_resource_template);
50 if (!tx->staging_resource)
51 {
52 pipe_resource_reference(&tx->base.resource, NULL);
53 FREE(tx);
54 return NULL;
55 }
56
57 if (usage & PIPE_TRANSFER_READ)
58 {
59 struct pipe_subresource dstsr;
60 unsigned zi;
61 dstsr.face = 0;
62 dstsr.level = 0;
63 for(zi = 0; zi < box->depth; ++zi)
64 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);
65 }
66
67 return tx;
68 }
69
70 void
71 util_staging_transfer_destroy(struct pipe_context *pipe, struct pipe_transfer *ptx)
72 {
73 struct util_staging_transfer *tx = (struct util_staging_transfer *)ptx;
74
75 if (tx->staging_resource != tx->base.resource)
76 {
77 if(tx->base.usage & PIPE_TRANSFER_WRITE) {
78 struct pipe_subresource srcsr;
79 unsigned zi;
80 srcsr.face = 0;
81 srcsr.level = 0;
82 for(zi = 0; zi < tx->base.box.depth; ++zi)
83 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);
84 }
85
86 pipe_resource_reference(&tx->staging_resource, NULL);
87 }
88
89 pipe_resource_reference(&ptx->resource, NULL);
90 FREE(ptx);
91 }