virgl: factor out format host bits check
[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 /* This mask indicates where the resource has been bound to, excluding
64 * pipe_surface binds.
65 *
66 * This is more accurate than pipe_resource::bind. Besides,
67 * pipe_resource::bind can be 0 with direct state access, and is not
68 * usable.
69 */
70 unsigned bind_history;
71 };
72
73 enum virgl_transfer_map_type {
74 VIRGL_TRANSFER_MAP_ERROR = -1,
75 VIRGL_TRANSFER_MAP_HW_RES,
76
77 /* Map a range of a staging buffer. The updated contents should be transferred
78 * with a copy transfer.
79 */
80 VIRGL_TRANSFER_MAP_STAGING,
81
82 /* Reallocate the underlying virgl_hw_res. */
83 VIRGL_TRANSFER_MAP_REALLOC,
84 };
85
86 struct virgl_transfer {
87 struct pipe_transfer base;
88 uint32_t offset, l_stride;
89 struct util_range range;
90 struct list_head queue_link;
91 struct pipe_transfer *resolve_transfer;
92
93 struct virgl_hw_res *hw_res;
94 void *hw_res_map;
95 /* If not NULL, denotes that this is a copy transfer, i.e.,
96 * that the transfer source data should be taken from this
97 * resource instead of the original transfer resource.
98 */
99 struct pipe_resource *copy_src_res;
100 /* The offset in the copy source resource to copy data from. */
101 uint32_t copy_src_offset;
102 };
103
104 void virgl_resource_destroy(struct pipe_screen *screen,
105 struct pipe_resource *resource);
106
107 void virgl_init_screen_resource_functions(struct pipe_screen *screen);
108
109 void virgl_init_context_resource_functions(struct pipe_context *ctx);
110
111 void virgl_texture_init(struct virgl_resource *res);
112
113 static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
114 {
115 return (struct virgl_resource *)r;
116 }
117
118 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
119 {
120 return (struct virgl_transfer *)trans;
121 }
122
123 void virgl_buffer_init(struct virgl_resource *res);
124
125 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs,
126 unsigned pbind, unsigned flags)
127 {
128 unsigned outbind = 0;
129 if (pbind & PIPE_BIND_DEPTH_STENCIL)
130 outbind |= VIRGL_BIND_DEPTH_STENCIL;
131 if (pbind & PIPE_BIND_RENDER_TARGET)
132 outbind |= VIRGL_BIND_RENDER_TARGET;
133 if (pbind & PIPE_BIND_SAMPLER_VIEW)
134 outbind |= VIRGL_BIND_SAMPLER_VIEW;
135 if (pbind & PIPE_BIND_VERTEX_BUFFER)
136 outbind |= VIRGL_BIND_VERTEX_BUFFER;
137 if (pbind & PIPE_BIND_INDEX_BUFFER)
138 outbind |= VIRGL_BIND_INDEX_BUFFER;
139 if (pbind & PIPE_BIND_CONSTANT_BUFFER)
140 outbind |= VIRGL_BIND_CONSTANT_BUFFER;
141 if (pbind & PIPE_BIND_DISPLAY_TARGET)
142 outbind |= VIRGL_BIND_DISPLAY_TARGET;
143 if (pbind & PIPE_BIND_STREAM_OUTPUT)
144 outbind |= VIRGL_BIND_STREAM_OUTPUT;
145 if (pbind & PIPE_BIND_CURSOR)
146 outbind |= VIRGL_BIND_CURSOR;
147 if (pbind & PIPE_BIND_CUSTOM) {
148 if (flags & VIRGL_RESOURCE_FLAG_STAGING)
149 outbind |= VIRGL_BIND_STAGING;
150 else
151 outbind |= VIRGL_BIND_CUSTOM;
152 }
153 if (pbind & PIPE_BIND_SCANOUT)
154 outbind |= VIRGL_BIND_SCANOUT;
155 if (pbind & PIPE_BIND_SHARED)
156 outbind |= VIRGL_BIND_SHARED;
157 if (pbind & PIPE_BIND_SHADER_BUFFER)
158 outbind |= VIRGL_BIND_SHADER_BUFFER;
159 if (pbind & PIPE_BIND_QUERY_BUFFER)
160 outbind |= VIRGL_BIND_QUERY_BUFFER;
161 if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
162 if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
163 outbind |= VIRGL_BIND_COMMAND_ARGS;
164 return outbind;
165 }
166
167 enum virgl_transfer_map_type
168 virgl_resource_transfer_prepare(struct virgl_context *vctx,
169 struct virgl_transfer *xfer);
170
171 void virgl_resource_layout(struct pipe_resource *pt,
172 struct virgl_resource_metadata *metadata);
173
174 struct virgl_transfer *
175 virgl_resource_create_transfer(struct virgl_context *vctx,
176 struct pipe_resource *pres,
177 const struct virgl_resource_metadata *metadata,
178 unsigned level, unsigned usage,
179 const struct pipe_box *box);
180
181 void virgl_resource_destroy_transfer(struct virgl_context *vctx,
182 struct virgl_transfer *trans);
183
184 void virgl_resource_destroy(struct pipe_screen *screen,
185 struct pipe_resource *resource);
186
187 boolean virgl_resource_get_handle(struct pipe_screen *screen,
188 struct pipe_resource *resource,
189 struct winsys_handle *whandle);
190
191 void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
192
193 void *virgl_transfer_uploader_map(struct virgl_context *vctx,
194 struct virgl_transfer *vtransfer);
195
196 bool
197 virgl_resource_realloc(struct virgl_context *vctx,
198 struct virgl_resource *res);
199
200 #endif