1 /**************************************************************************
2 *
3 * Copyright (C) 2014 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 **************************************************************************/
24
25 #ifndef VREND_RENDERER_H
26 #define VREND_RENDERER_H
27
28 #include "pipe/p_state.h"
29 #include "util/u_inlines.h"
30 #include "virgl_protocol.h"
31 #include "vrend_iov.h"
32 #include "virgl_hw.h"
33 #include <epoxy/gl.h>
34
35 typedef void *virgl_gl_context;
36 typedef void *virgl_gl_drawable;
37
38 struct virgl_gl_ctx_param {
39 int major_ver;
40 int minor_ver;
41 bool shared;
42 };
43
44 extern int vrend_dump_shaders;
45 struct vrend_context;
46
47 /* Number of mipmap levels for which to keep the backing iov offsets.
48 * Value mirrored from mesa/virgl
49 */
50 #define VR_MAX_TEXTURE_2D_LEVELS 15
51
52 struct vrend_resource {
53 struct pipe_resource base;
54 GLuint id;
55 GLenum target;
56 /* fb id if we need to readback this resource */
57 GLuint readback_fb_id;
58 GLuint readback_fb_level;
59 GLuint readback_fb_z;
60
61 GLuint tbo_tex_id;/* tbos have two ids to track */
62 bool y_0_top;
63 bool is_buffer;
64
65 GLuint handle;
66
67 char *ptr;
68 struct iovec *iov;
69 uint32_t num_iovs;
70 uint64_t mipmap_offsets[VR_MAX_TEXTURE_2D_LEVELS];
71 };
72
73 #define VIRGL_BIND_NEED_SWIZZLE (1 << 28)
74 #define VIRGL_BIND_CAN_TEXTURE_STORAGE (1 << 29)
75
76
77 struct vrend_format_table {
78 enum virgl_formats format;
79 GLenum internalformat;
80 GLenum glformat;
81 GLenum gltype;
82 uint8_t swizzle[4];
83 uint32_t bindings;
84 int flags;
85 };
86
87 struct vrend_transfer_info {
88 uint32_t handle;
89 uint32_t ctx_id;
90 int level;
91 uint32_t stride;
92 uint32_t layer_stride;
93 unsigned int iovec_cnt;
94 struct iovec *iovec;
95 uint64_t offset;
96 struct pipe_box *box;
97 };
98
99 struct vrend_if_cbs {
100 void (*write_fence)(unsigned fence_id);
101
102 virgl_gl_context (*create_gl_context)(int scanout, struct virgl_gl_ctx_param *params);
103 void (*destroy_gl_context)(virgl_gl_context ctx);
104 int (*make_current)(int scanout, virgl_gl_context ctx);
105 };
106
107 #define VREND_USE_THREAD_SYNC 1
108
109 int vrend_renderer_init(struct vrend_if_cbs *cbs, uint32_t flags);
110
111 void vrend_insert_format(struct vrend_format_table *entry, uint32_t bindings);
112 void vrend_insert_format_swizzle(int override_format, struct vrend_format_table *entry, uint32_t bindings, uint8_t swizzle[4]);
113 const struct vrend_format_table *vrend_get_format_table_entry(enum virgl_formats format);
114 int vrend_create_shader(struct vrend_context *ctx,
115 uint32_t handle,
116 const struct pipe_stream_output_info *stream_output,
117 uint32_t req_local_mem,
118 const char *shd_text, uint32_t offlen, uint32_t num_tokens,
119 uint32_t type, uint32_t pkt_length);
120
121 void vrend_bind_shader(struct vrend_context *ctx,
122 uint32_t type,
123 uint32_t handle);
124
125 void vrend_bind_vs_so(struct vrend_context *ctx,
126 uint32_t handle);
127 void vrend_clear(struct vrend_context *ctx,
128 unsigned buffers,
129 const union pipe_color_union *color,
130 double depth, unsigned stencil);
131
132 int vrend_draw_vbo(struct vrend_context *ctx,
133 const struct pipe_draw_info *info,
134 uint32_t cso, uint32_t indirect_handle, uint32_t indirect_draw_count_handle);
135
136 void vrend_set_framebuffer_state(struct vrend_context *ctx,
137 uint32_t nr_cbufs, uint32_t surf_handle[PIPE_MAX_COLOR_BUFS],
138 uint32_t zsurf_handle);
139
140 struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *debug_name);
141 bool vrend_destroy_context(struct vrend_context *ctx);
142 int vrend_renderer_context_create(uint32_t handle, uint32_t nlen, const char *name);
143 void vrend_renderer_context_create_internal(uint32_t handle, uint32_t nlen, const char *name);
144 void vrend_renderer_context_destroy(uint32_t handle);
145
146 struct vrend_renderer_resource_create_args {
147 uint32_t handle;
148 enum pipe_texture_target target;
149 uint32_t format;
150 uint32_t bind;
151 uint32_t width;
152 uint32_t height;
153 uint32_t depth;
154 uint32_t array_size;
155 uint32_t last_level;
156 uint32_t nr_samples;
157 uint32_t flags;
158 };
159
160 int vrend_renderer_resource_create(struct vrend_renderer_resource_create_args *args, struct iovec *iov, uint32_t num_iovs, void *image_eos);
161
162 void vrend_renderer_resource_unref(uint32_t handle);
163
164 int vrend_create_surface(struct vrend_context *ctx,
165 uint32_t handle,
166 uint32_t res_handle, uint32_t format,
167 uint32_t val0, uint32_t val1);
168 int vrend_create_sampler_view(struct vrend_context *ctx,
169 uint32_t handle,
170 uint32_t res_handle, uint32_t format,
171 uint32_t val0, uint32_t val1, uint32_t swizzle_packed);
172
173 int vrend_create_sampler_state(struct vrend_context *ctx,
174 uint32_t handle,
175 struct pipe_sampler_state *templ);
176
177 int vrend_create_so_target(struct vrend_context *ctx,
178 uint32_t handle,
179 uint32_t res_handle,
180 uint32_t buffer_offset,
181 uint32_t buffer_size);
182
183 void vrend_set_streamout_targets(struct vrend_context *ctx,
184 uint32_t append_bitmask,
185 uint32_t num_targets,
186 uint32_t *handles);
187
188 int vrend_create_vertex_elements_state(struct vrend_context *ctx,
189 uint32_t handle,
190 unsigned num_elements,
191 const struct pipe_vertex_element *elements);
192 void vrend_bind_vertex_elements_state(struct vrend_context *ctx,
193 uint32_t handle);
194
195 void vrend_set_single_vbo(struct vrend_context *ctx,
196 int index,
197 uint32_t stride,
198 uint32_t buffer_offset,
199 uint32_t res_handle);
200 void vrend_set_num_vbo(struct vrend_context *ctx,
201 int num_vbo);
202
203 int vrend_transfer_inline_write(struct vrend_context *ctx,
204 struct vrend_transfer_info *info,
205 unsigned usage);
206
207 void vrend_set_viewport_states(struct vrend_context *ctx,
208 uint32_t start_slot, uint32_t num_viewports,
209 const struct pipe_viewport_state *state);
210 void vrend_set_num_sampler_views(struct vrend_context *ctx,
211 uint32_t shader_type,
212 uint32_t start_slot,
213 int num_sampler_views);
214 void vrend_set_single_sampler_view(struct vrend_context *ctx,
215 uint32_t shader_type,
216 uint32_t index,
217 uint32_t res_handle);
218
219 void vrend_object_bind_blend(struct vrend_context *ctx,
220 uint32_t handle);
221 void vrend_object_bind_dsa(struct vrend_context *ctx,
222 uint32_t handle);
223 void vrend_object_bind_rasterizer(struct vrend_context *ctx,
224 uint32_t handle);
225
226 void vrend_bind_sampler_states(struct vrend_context *ctx,
227 uint32_t shader_type,
228 uint32_t start_slot,
229 uint32_t num_states,
230 uint32_t *handles);
231 void vrend_set_index_buffer(struct vrend_context *ctx,
232 uint32_t res_handle,
233 uint32_t index_size,
234 uint32_t offset);
235 void vrend_set_single_image_view(struct vrend_context *ctx,
236 uint32_t shader_type,
237 int index,
238 uint32_t format, uint32_t access,
239 uint32_t layer_offset, uint32_t level_size,
240 uint32_t handle);
241 void vrend_set_single_ssbo(struct vrend_context *ctx,
242 uint32_t shader_type,
243 int index,
244 uint32_t offset, uint32_t length,
245 uint32_t handle);
246 void vrend_memory_barrier(struct vrend_context *ctx,
247 unsigned flags);
248 void vrend_launch_grid(struct vrend_context *ctx,
249 uint32_t *block,
250 uint32_t *grid,
251 uint32_t indirect_handle,
252 uint32_t indirect_offset);
253 void vrend_set_framebuffer_state_no_attach(struct vrend_context *ctx,
254 uint32_t width, uint32_t height,
255 uint32_t layers, uint32_t samples);
256 void vrend_texture_barrier(struct vrend_context *ctx,
257 unsigned flags);
258 #define VREND_TRANSFER_WRITE 1
259 #define VREND_TRANSFER_READ 2
260 int vrend_renderer_transfer_iov(const struct vrend_transfer_info *info, int transfer_mode);
261
262 void vrend_renderer_resource_copy_region(struct vrend_context *ctx,
263 uint32_t dst_handle, uint32_t dst_level,
264 uint32_t dstx, uint32_t dsty, uint32_t dstz,
265 uint32_t src_handle, uint32_t src_level,
266 const struct pipe_box *src_box);
267
268 void vrend_renderer_blit(struct vrend_context *ctx,
269 uint32_t dst_handle, uint32_t src_handle,
270 const struct pipe_blit_info *info);
271
272 void vrend_set_stencil_ref(struct vrend_context *ctx, struct pipe_stencil_ref *ref);
273 void vrend_set_blend_color(struct vrend_context *ctx, struct pipe_blend_color *color);
274 void vrend_set_scissor_state(struct vrend_context *ctx,
275 uint32_t start_slot,
276 uint32_t num_scissor,
277 struct pipe_scissor_state *ss);
278
279 void vrend_set_polygon_stipple(struct vrend_context *ctx, struct pipe_poly_stipple *ps);
280
281 void vrend_set_clip_state(struct vrend_context *ctx, struct pipe_clip_state *ucp);
282 void vrend_set_sample_mask(struct vrend_context *ctx, unsigned sample_mask);
283 void vrend_set_min_samples(struct vrend_context *ctx, unsigned min_samples);
284
285 void vrend_set_constants(struct vrend_context *ctx,
286 uint32_t shader,
287 uint32_t index,
288 uint32_t num_constant,
289 float *data);
290
291 void vrend_set_uniform_buffer(struct vrend_context *ctx, uint32_t shader,
292 uint32_t index, uint32_t offset, uint32_t length,
293 uint32_t res_handle);
294
295 void vrend_set_tess_state(struct vrend_context *ctx, const float tess_factors[6]);
296
297 void vrend_renderer_fini(void);
298
299 int vrend_decode_block(uint32_t ctx_id, uint32_t *block, int ndw);
300 struct vrend_context *vrend_lookup_renderer_ctx(uint32_t ctx_id);
301
302 int vrend_renderer_create_fence(int client_fence_id, uint32_t ctx_id);
303
304 void vrend_renderer_check_fences(void);
305 void vrend_renderer_check_queries(void);
306
307 bool vrend_hw_switch_context(struct vrend_context *ctx, bool now);
308 uint32_t vrend_renderer_object_insert(struct vrend_context *ctx, void *data,
309 uint32_t size, uint32_t handle, enum virgl_object_type type);
310 void vrend_renderer_object_destroy(struct vrend_context *ctx, uint32_t handle);
311
312 int vrend_create_query(struct vrend_context *ctx, uint32_t handle,
313 uint32_t query_type, uint32_t query_index,
314 uint32_t res_handle, uint32_t offset);
315
316 int vrend_begin_query(struct vrend_context *ctx, uint32_t handle);
317 int vrend_end_query(struct vrend_context *ctx, uint32_t handle);
318 void vrend_get_query_result(struct vrend_context *ctx, uint32_t handle,
319 uint32_t wait);
320 void vrend_render_condition(struct vrend_context *ctx,
321 uint32_t handle,
322 bool condtion,
323 uint mode);
324 void *vrend_renderer_get_cursor_contents(uint32_t res_handle, uint32_t *width, uint32_t *height);
325 void vrend_bind_va(GLuint vaoid);
326 int vrend_renderer_flush_buffer_res(struct vrend_resource *res,
327 struct pipe_box *box);
328
329 void vrend_renderer_fill_caps(uint32_t set, uint32_t version,
330 union virgl_caps *caps);
331
332 GLint64 vrend_renderer_get_timestamp(void);
333
334 void vrend_build_format_list_common(void);
335 void vrend_build_format_list_gl(void);
336 void vrend_build_format_list_gles(void);
337 void vrend_check_texture_storage(struct vrend_format_table *table);
338
339 int vrend_renderer_resource_attach_iov(int res_handle, struct iovec *iov,
340 int num_iovs);
341 void vrend_renderer_resource_detach_iov(int res_handle,
342 struct iovec **iov_p,
343 int *num_iovs_p);
344 void vrend_renderer_resource_destroy(struct vrend_resource *res, bool remove);
345
346 static inline void
vrend_resource_reference(struct vrend_resource ** ptr,struct vrend_resource * tex)347 vrend_resource_reference(struct vrend_resource **ptr, struct vrend_resource *tex)
348 {
349 struct vrend_resource *old_tex = *ptr;
350
351 if (pipe_reference(&(*ptr)->base.reference, &tex->base.reference))
352 vrend_renderer_resource_destroy(old_tex, true);
353 *ptr = tex;
354 }
355
356 void vrend_renderer_force_ctx_0(void);
357
358 void vrend_renderer_get_rect(int resource_id, struct iovec *iov, unsigned int num_iovs,
359 uint32_t offset, int x, int y, int width, int height);
360 void vrend_renderer_attach_res_ctx(int ctx_id, int resource_id);
361 void vrend_renderer_detach_res_ctx(int ctx_id, int resource_id);
362
363 struct vrend_renderer_resource_info {
364 uint32_t handle;
365 uint32_t format;
366 uint32_t width;
367 uint32_t height;
368 uint32_t depth;
369 uint32_t flags;
370 uint32_t tex_id;
371 uint32_t stride;
372 };
373
374 int vrend_renderer_resource_get_info(int res_handle,
375 struct vrend_renderer_resource_info *info);
376
377 #define VREND_CAP_SET 1
378 #define VREND_CAP_SET2 2
379
380 void vrend_renderer_get_cap_set(uint32_t cap_set, uint32_t *max_ver,
381 uint32_t *max_size);
382
383 void vrend_renderer_create_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
384 void vrend_renderer_destroy_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
385 void vrend_renderer_set_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
386 void vrend_report_buffer_error(struct vrend_context *ctx, int cmd);
387
388 void vrend_fb_bind_texture(struct vrend_resource *res,
389 int idx,
390 uint32_t level, uint32_t layer);
391 bool vrend_is_ds_format(enum virgl_formats format);
392 bool vrend_format_is_emulated_alpha(enum virgl_formats format);
393 boolean format_is_copy_compatible(enum pipe_format src, enum pipe_format dst,
394 boolean allow_compressed);
395
396 /* blitter interface */
397 void vrend_renderer_blit_gl(struct vrend_context *ctx,
398 struct vrend_resource *src_res,
399 struct vrend_resource *dst_res,
400 const struct pipe_blit_info *info,
401 bool has_texture_srgb_decode);
402
403 void vrend_renderer_reset(void);
404 int vrend_renderer_get_poll_fd(void);
405 void vrend_decode_reset(bool ctx_0_only);
406
407 unsigned vrend_renderer_query_multisample_caps(unsigned max_samples,
408 struct virgl_caps_v2 *caps);
409
410 struct gl_version {
411 uint32_t major;
412 uint32_t minor;
413 };
414
415 static const struct gl_version gl_versions[] = { {4,5}, {4,4}, {4,3}, {4,2}, {4,1}, {4,0},
416 {3,3}, {3,2}, {3,1}, {3,0} };
417
418 extern struct vrend_if_cbs *vrend_clicbs;
419 #endif
420