virgl: fixup_readback_format --> fixup_formats
[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 struct winsys_handle;
37 struct virgl_screen;
38 struct virgl_context;
39
40 struct virgl_resource_metadata
41 {
42 unsigned long level_offset[VR_MAX_TEXTURE_2D_LEVELS];
43 unsigned stride[VR_MAX_TEXTURE_2D_LEVELS];
44 unsigned layer_stride[VR_MAX_TEXTURE_2D_LEVELS];
45 uint32_t total_size;
46 };
47
48 struct virgl_resource {
49 struct u_resource u;
50 uint16_t clean_mask;
51 struct virgl_hw_res *hw_res;
52 struct virgl_resource_metadata metadata;
53
54 /* For PIPE_BUFFER only. Data outside of this range are uninitialized. */
55 struct util_range valid_buffer_range;
56
57 /* This mask indicates where the resource has been bound to, excluding
58 * pipe_surface binds.
59 *
60 * This is more accurate than pipe_resource::bind. Besides,
61 * pipe_resource::bind can be 0 with direct state access, and is not
62 * usable.
63 */
64 unsigned bind_history;
65 };
66
67 struct virgl_transfer {
68 struct pipe_transfer base;
69 uint32_t offset, l_stride;
70 struct util_range range;
71 struct list_head queue_link;
72 struct pipe_transfer *resolve_transfer;
73
74 struct virgl_hw_res *hw_res;
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 virgl_hw_res *copy_src_hw_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 outbind |= VIRGL_BIND_CUSTOM;
130 if (pbind & PIPE_BIND_SCANOUT)
131 outbind |= VIRGL_BIND_SCANOUT;
132 if (pbind & PIPE_BIND_SHARED)
133 outbind |= VIRGL_BIND_SHARED;
134 if (pbind & PIPE_BIND_SHADER_BUFFER)
135 outbind |= VIRGL_BIND_SHADER_BUFFER;
136 if (pbind & PIPE_BIND_QUERY_BUFFER)
137 outbind |= VIRGL_BIND_QUERY_BUFFER;
138 if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
139 if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
140 outbind |= VIRGL_BIND_COMMAND_ARGS;
141
142 /* Staging resources should only be created directly through the winsys,
143 * not using pipe_resources.
144 */
145 assert(!(outbind & VIRGL_BIND_STAGING));
146
147 return outbind;
148 }
149
150 void *
151 virgl_resource_transfer_map(struct pipe_context *ctx,
152 struct pipe_resource *resource,
153 unsigned level,
154 unsigned usage,
155 const struct pipe_box *box,
156 struct pipe_transfer **transfer);
157
158 void virgl_resource_layout(struct pipe_resource *pt,
159 struct virgl_resource_metadata *metadata);
160
161 struct virgl_transfer *
162 virgl_resource_create_transfer(struct virgl_context *vctx,
163 struct pipe_resource *pres,
164 const struct virgl_resource_metadata *metadata,
165 unsigned level, unsigned usage,
166 const struct pipe_box *box);
167
168 void virgl_resource_destroy_transfer(struct virgl_context *vctx,
169 struct virgl_transfer *trans);
170
171 void virgl_resource_destroy(struct pipe_screen *screen,
172 struct pipe_resource *resource);
173
174 bool virgl_resource_get_handle(struct pipe_screen *screen,
175 struct pipe_resource *resource,
176 struct winsys_handle *whandle);
177
178 void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
179
180 #endif