2 * Copyright 2014, 2015 Red Hat.
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:
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
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.
24 #ifndef VIRGL_RESOURCE_H
25 #define VIRGL_RESOURCE_H
27 #include "util/u_resource.h"
28 #include "util/u_range.h"
29 #include "util/list.h"
30 #include "util/u_transfer.h"
33 #include "virgl_screen.h"
34 #define VR_MAX_TEXTURE_2D_LEVELS 15
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.
40 #define VIRGL_RESOURCE_FLAG_STAGING (PIPE_RESOURCE_FLAG_DRV_PRIV << 0)
46 struct virgl_resource_metadata
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
];
54 struct virgl_resource
{
57 struct virgl_hw_res
*hw_res
;
58 struct virgl_resource_metadata metadata
;
60 /* For PIPE_BUFFER only. Data outside of this range are uninitialized. */
61 struct util_range valid_buffer_range
;
63 /* This mask indicates where the resource has been bound to, excluding
66 * This is more accurate than pipe_resource::bind. Besides,
67 * pipe_resource::bind can be 0 with direct state access, and is not
70 unsigned bind_history
;
73 enum virgl_transfer_map_type
{
74 VIRGL_TRANSFER_MAP_ERROR
= -1,
75 VIRGL_TRANSFER_MAP_HW_RES
,
77 /* Map a range of a staging buffer. The updated contents should be transferred
78 * with a copy transfer.
80 VIRGL_TRANSFER_MAP_STAGING
,
82 /* Reallocate the underlying virgl_hw_res. */
83 VIRGL_TRANSFER_MAP_REALLOC
,
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
;
93 struct virgl_hw_res
*hw_res
;
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.
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
;
104 void virgl_resource_destroy(struct pipe_screen
*screen
,
105 struct pipe_resource
*resource
);
107 void virgl_init_screen_resource_functions(struct pipe_screen
*screen
);
109 void virgl_init_context_resource_functions(struct pipe_context
*ctx
);
111 void virgl_texture_init(struct virgl_resource
*res
);
113 static inline struct virgl_resource
*virgl_resource(struct pipe_resource
*r
)
115 return (struct virgl_resource
*)r
;
118 static inline struct virgl_transfer
*virgl_transfer(struct pipe_transfer
*trans
)
120 return (struct virgl_transfer
*)trans
;
123 void virgl_buffer_init(struct virgl_resource
*res
);
125 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen
*vs
,
126 unsigned pbind
, unsigned flags
)
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
;
151 outbind
|= VIRGL_BIND_CUSTOM
;
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
;
167 enum virgl_transfer_map_type
168 virgl_resource_transfer_prepare(struct virgl_context
*vctx
,
169 struct virgl_transfer
*xfer
);
171 void virgl_resource_layout(struct pipe_resource
*pt
,
172 struct virgl_resource_metadata
*metadata
);
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
);
181 void virgl_resource_destroy_transfer(struct virgl_context
*vctx
,
182 struct virgl_transfer
*trans
);
184 void virgl_resource_destroy(struct pipe_screen
*screen
,
185 struct pipe_resource
*resource
);
187 boolean
virgl_resource_get_handle(struct pipe_screen
*screen
,
188 struct pipe_resource
*resource
,
189 struct winsys_handle
*whandle
);
191 void virgl_resource_dirty(struct virgl_resource
*res
, uint32_t level
);
193 void *virgl_transfer_uploader_map(struct virgl_context
*vctx
,
194 struct virgl_transfer
*vtransfer
);
197 virgl_resource_realloc(struct virgl_context
*vctx
,
198 struct virgl_resource
*res
);