virgl: Support copy transfers
[mesa.git] / src / gallium / drivers / virgl / virgl_resource.h
1 /*
2 * Copyright 2014, 2015 Red Hat.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #ifndef VIRGL_RESOURCE_H
25 #define VIRGL_RESOURCE_H
26
27 #include "util/u_resource.h"
28 #include "util/u_range.h"
29 #include "util/list.h"
30 #include "util/u_transfer.h"
31
32 #include "virgl_hw.h"
33 #include "virgl_screen.h"
34 #define VR_MAX_TEXTURE_2D_LEVELS 15
35
36 /* Indicates that the resource will be used as a staging buffer, not requiring
37 * dedicated host-side storage. Can only be used with PIPE_BUFFER resources
38 * that have a PIPE_BIND_CUSTOM bind type.
39 */
40 #define VIRGL_RESOURCE_FLAG_STAGING (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
41
42 struct winsys_handle;
43 struct virgl_screen;
44 struct virgl_context;
45
46 struct virgl_resource_metadata
47 {
48 unsigned long level_offset[VR_MAX_TEXTURE_2D_LEVELS];
49 unsigned stride[VR_MAX_TEXTURE_2D_LEVELS];
50 unsigned layer_stride[VR_MAX_TEXTURE_2D_LEVELS];
51 uint32_t total_size;
52 };
53
54 struct virgl_resource {
55 struct u_resource u;
56 uint16_t clean_mask;
57 struct virgl_hw_res *hw_res;
58 struct virgl_resource_metadata metadata;
59
60 /* For PIPE_BUFFER only. Data outside of this range are uninitialized. */
61 struct util_range valid_buffer_range;
62 };
63
64 enum virgl_transfer_map_type {
65 VIRGL_TRANSFER_MAP_ERROR = -1,
66 VIRGL_TRANSFER_MAP_HW_RES,
67 };
68
69 struct virgl_transfer {
70 struct pipe_transfer base;
71 uint32_t offset, l_stride;
72 struct util_range range;
73 struct list_head queue_link;
74 struct pipe_transfer *resolve_transfer;
75 void *hw_res_map;
76 /* If not NULL, denotes that this is a copy transfer, i.e.,
77 * that the transfer source data should be taken from this
78 * resource instead of the original transfer resource.
79 */
80 struct pipe_resource *copy_src_res;
81 /* The offset in the copy source resource to copy data from. */
82 uint32_t copy_src_offset;
83 };
84
85 void virgl_resource_destroy(struct pipe_screen *screen,
86 struct pipe_resource *resource);
87
88 void virgl_init_screen_resource_functions(struct pipe_screen *screen);
89
90 void virgl_init_context_resource_functions(struct pipe_context *ctx);
91
92 void virgl_texture_init(struct virgl_resource *res);
93
94 static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
95 {
96 return (struct virgl_resource *)r;
97 }
98
99 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
100 {
101 return (struct virgl_transfer *)trans;
102 }
103
104 void virgl_buffer_init(struct virgl_resource *res);
105
106 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs,
107 unsigned pbind, unsigned flags)
108 {
109 unsigned outbind = 0;
110 if (pbind & PIPE_BIND_DEPTH_STENCIL)
111 outbind |= VIRGL_BIND_DEPTH_STENCIL;
112 if (pbind & PIPE_BIND_RENDER_TARGET)
113 outbind |= VIRGL_BIND_RENDER_TARGET;
114 if (pbind & PIPE_BIND_SAMPLER_VIEW)
115 outbind |= VIRGL_BIND_SAMPLER_VIEW;
116 if (pbind & PIPE_BIND_VERTEX_BUFFER)
117 outbind |= VIRGL_BIND_VERTEX_BUFFER;
118 if (pbind & PIPE_BIND_INDEX_BUFFER)
119 outbind |= VIRGL_BIND_INDEX_BUFFER;
120 if (pbind & PIPE_BIND_CONSTANT_BUFFER)
121 outbind |= VIRGL_BIND_CONSTANT_BUFFER;
122 if (pbind & PIPE_BIND_DISPLAY_TARGET)
123 outbind |= VIRGL_BIND_DISPLAY_TARGET;
124 if (pbind & PIPE_BIND_STREAM_OUTPUT)
125 outbind |= VIRGL_BIND_STREAM_OUTPUT;
126 if (pbind & PIPE_BIND_CURSOR)
127 outbind |= VIRGL_BIND_CURSOR;
128 if (pbind & PIPE_BIND_CUSTOM) {
129 if (flags & VIRGL_RESOURCE_FLAG_STAGING)
130 outbind |= VIRGL_BIND_STAGING;
131 else
132 outbind |= VIRGL_BIND_CUSTOM;
133 }
134 if (pbind & PIPE_BIND_SCANOUT)
135 outbind |= VIRGL_BIND_SCANOUT;
136 if (pbind & PIPE_BIND_SHADER_BUFFER)
137 outbind |= VIRGL_BIND_SHADER_BUFFER;
138 if (pbind & PIPE_BIND_QUERY_BUFFER)
139 outbind |= VIRGL_BIND_QUERY_BUFFER;
140 if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
141 if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
142 outbind |= VIRGL_BIND_COMMAND_ARGS;
143 return outbind;
144 }
145
146 enum virgl_transfer_map_type
147 virgl_resource_transfer_prepare(struct virgl_context *vctx,
148 struct virgl_transfer *xfer);
149
150 void virgl_resource_layout(struct pipe_resource *pt,
151 struct virgl_resource_metadata *metadata);
152
153 struct virgl_transfer *
154 virgl_resource_create_transfer(struct slab_child_pool *pool,
155 struct pipe_resource *pres,
156 const struct virgl_resource_metadata *metadata,
157 unsigned level, unsigned usage,
158 const struct pipe_box *box);
159
160 void virgl_resource_destroy_transfer(struct slab_child_pool *pool,
161 struct virgl_transfer *trans);
162
163 void virgl_resource_destroy(struct pipe_screen *screen,
164 struct pipe_resource *resource);
165
166 boolean virgl_resource_get_handle(struct pipe_screen *screen,
167 struct pipe_resource *resource,
168 struct winsys_handle *whandle);
169
170 void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
171
172 #endif