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"
32 #include "virtio-gpu/virgl_hw.h"
33 #include "virgl_screen.h"
34 #define VR_MAX_TEXTURE_2D_LEVELS 15
40 struct virgl_resource_metadata
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 plane
, plane_offset
, total_size
;
49 struct virgl_resource
{
52 struct virgl_hw_res
*hw_res
;
53 struct virgl_resource_metadata metadata
;
55 /* For PIPE_BUFFER only. Data outside of this range are uninitialized. */
56 struct util_range valid_buffer_range
;
58 /* This mask indicates where the resource has been bound to, excluding
61 * This is more accurate than pipe_resource::bind. Besides,
62 * pipe_resource::bind can be 0 with direct state access, and is not
65 unsigned bind_history
;
68 struct virgl_transfer
{
69 struct pipe_transfer base
;
70 uint32_t offset
, l_stride
;
71 struct util_range range
;
72 struct list_head queue_link
;
73 struct pipe_transfer
*resolve_transfer
;
75 struct virgl_hw_res
*hw_res
;
77 /* If not NULL, denotes that this is a copy transfer, i.e.,
78 * that the transfer source data should be taken from this
79 * resource instead of the original transfer resource.
81 struct virgl_hw_res
*copy_src_hw_res
;
82 /* The offset in the copy source resource to copy data from. */
83 uint32_t copy_src_offset
;
86 void virgl_resource_destroy(struct pipe_screen
*screen
,
87 struct pipe_resource
*resource
);
89 void virgl_init_screen_resource_functions(struct pipe_screen
*screen
);
91 void virgl_init_context_resource_functions(struct pipe_context
*ctx
);
93 void virgl_texture_init(struct virgl_resource
*res
);
95 static inline struct virgl_resource
*virgl_resource(struct pipe_resource
*r
)
97 return (struct virgl_resource
*)r
;
100 static inline struct virgl_transfer
*virgl_transfer(struct pipe_transfer
*trans
)
102 return (struct virgl_transfer
*)trans
;
105 void virgl_buffer_init(struct virgl_resource
*res
);
107 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen
*vs
,
108 unsigned pbind
, unsigned flags
)
110 unsigned outbind
= 0;
111 if (pbind
& PIPE_BIND_DEPTH_STENCIL
)
112 outbind
|= VIRGL_BIND_DEPTH_STENCIL
;
113 if (pbind
& PIPE_BIND_RENDER_TARGET
)
114 outbind
|= VIRGL_BIND_RENDER_TARGET
;
115 if (pbind
& PIPE_BIND_SAMPLER_VIEW
)
116 outbind
|= VIRGL_BIND_SAMPLER_VIEW
;
117 if (pbind
& PIPE_BIND_VERTEX_BUFFER
)
118 outbind
|= VIRGL_BIND_VERTEX_BUFFER
;
119 if (pbind
& PIPE_BIND_INDEX_BUFFER
)
120 outbind
|= VIRGL_BIND_INDEX_BUFFER
;
121 if (pbind
& PIPE_BIND_CONSTANT_BUFFER
)
122 outbind
|= VIRGL_BIND_CONSTANT_BUFFER
;
123 if (pbind
& PIPE_BIND_DISPLAY_TARGET
)
124 outbind
|= VIRGL_BIND_DISPLAY_TARGET
;
125 if (pbind
& PIPE_BIND_STREAM_OUTPUT
)
126 outbind
|= VIRGL_BIND_STREAM_OUTPUT
;
127 if (pbind
& PIPE_BIND_CURSOR
)
128 outbind
|= VIRGL_BIND_CURSOR
;
129 if (pbind
& PIPE_BIND_CUSTOM
)
130 outbind
|= VIRGL_BIND_CUSTOM
;
131 if (pbind
& PIPE_BIND_SCANOUT
)
132 outbind
|= VIRGL_BIND_SCANOUT
;
133 if (pbind
& PIPE_BIND_SHARED
)
134 outbind
|= VIRGL_BIND_SHARED
;
135 if (pbind
& PIPE_BIND_SHADER_BUFFER
)
136 outbind
|= VIRGL_BIND_SHADER_BUFFER
;
137 if (pbind
& PIPE_BIND_QUERY_BUFFER
)
138 outbind
|= VIRGL_BIND_QUERY_BUFFER
;
139 if (pbind
& PIPE_BIND_COMMAND_ARGS_BUFFER
)
140 if (vs
->caps
.caps
.v2
.capability_bits
& VIRGL_CAP_BIND_COMMAND_ARGS
)
141 outbind
|= VIRGL_BIND_COMMAND_ARGS
;
143 /* Staging resources should only be created directly through the winsys,
144 * not using pipe_resources.
146 assert(!(outbind
& VIRGL_BIND_STAGING
));
152 virgl_resource_transfer_map(struct pipe_context
*ctx
,
153 struct pipe_resource
*resource
,
156 const struct pipe_box
*box
,
157 struct pipe_transfer
**transfer
);
159 struct virgl_transfer
*
160 virgl_resource_create_transfer(struct virgl_context
*vctx
,
161 struct pipe_resource
*pres
,
162 const struct virgl_resource_metadata
*metadata
,
163 unsigned level
, unsigned usage
,
164 const struct pipe_box
*box
);
166 void virgl_resource_destroy_transfer(struct virgl_context
*vctx
,
167 struct virgl_transfer
*trans
);
169 void virgl_resource_destroy(struct pipe_screen
*screen
,
170 struct pipe_resource
*resource
);
172 bool virgl_resource_get_handle(struct pipe_screen
*screen
,
173 struct pipe_resource
*resource
,
174 struct winsys_handle
*whandle
);
176 void virgl_resource_dirty(struct virgl_resource
*res
, uint32_t level
);