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_double_list.h"
30 #include "util/u_inlines.h"
31 #include "virgl_protocol.h"
32 #include "vrend_debug.h"
33 #include "vrend_tweaks.h"
34 #include "vrend_iov.h"
35 #include "vrend_winsys_gbm.h"
36 #include "virgl_hw.h"
37 #include <epoxy/gl.h>
38
39 typedef void *virgl_gl_context;
40 typedef void *virgl_gl_drawable;
41
42 struct virgl_gl_ctx_param {
43 int major_ver;
44 int minor_ver;
45 bool shared;
46 };
47
48 struct virgl_context;
49 struct virgl_resource;
50 struct vrend_context;
51
52 /* Number of mipmap levels for which to keep the backing iov offsets.
53 * Value mirrored from mesa/virgl
54 */
55 #define VR_MAX_TEXTURE_2D_LEVELS 15
56
57 #define VREND_STORAGE_GUEST_MEMORY BIT(0)
58 #define VREND_STORAGE_GL_TEXTURE BIT(1)
59 #define VREND_STORAGE_GL_BUFFER BIT(2)
60 #define VREND_STORAGE_EGL_IMAGE BIT(3)
61 #define VREND_STORAGE_GBM_BUFFER BIT(4)
62 #define VREND_STORAGE_HOST_SYSTEM_MEMORY BIT(5)
63 #define VREND_STORAGE_GL_IMMUTABLE BIT(6)
64 #define VREND_STORAGE_GL_MEMOBJ BIT(7)
65
66 struct vrend_resource {
67 struct pipe_resource base;
68 uint32_t storage_bits;
69 uint32_t map_info;
70
71 GLuint id;
72 GLenum target;
73
74 GLuint tbo_tex_id;/* tbos have two ids to track */
75 bool y_0_top;
76
77 /* used for keeping track of multisampled renderbuffer for
78 * GL_EXT_multisampled_render_to_texture. */
79 GLuint rbo_id;
80
81 /* Pointer to system memory storage for this resource. Only valid for
82 * VREND_RESOURCE_STORAGE_GUEST_ELSE_SYSTEM buffer storage.
83 */
84 char *ptr;
85 /* IOV pointing to shared guest memory storage for this resource. */
86 const struct iovec *iov;
87 uint32_t num_iovs;
88 uint64_t mipmap_offsets[VR_MAX_TEXTURE_2D_LEVELS];
89 void *gbm_bo, *egl_image;
90 void *aux_plane_egl_image[VIRGL_GBM_MAX_PLANES];
91
92 uint64_t size;
93 GLbitfield buffer_storage_flags;
94 GLuint memobj;
95
96 uint32_t blob_id;
97 struct list_head head;
98 };
99
100 #define VIRGL_TEXTURE_NEED_SWIZZLE (1 << 0)
101 #define VIRGL_TEXTURE_CAN_TEXTURE_STORAGE (1 << 1)
102 #define VIRGL_TEXTURE_CAN_READBACK (1 << 2)
103 #define VIRGL_TEXTURE_CAN_TARGET_RECTANGLE (1 << 3)
104 #define VIRGL_TEXTURE_CAN_MULTISAMPLE (1 << 4)
105
106 struct vrend_format_table {
107 enum virgl_formats format;
108 GLenum internalformat;
109 GLenum glformat;
110 GLenum gltype;
111 uint8_t swizzle[4];
112 uint32_t bindings;
113 uint32_t flags;
114 };
115
116 typedef void (*vrend_context_fence_retire)(uint64_t fence_id,
117 void *retire_data);
118
119 struct vrend_if_cbs {
120 vrend_context_fence_retire ctx0_fence_retire;
121
122 virgl_gl_context (*create_gl_context)(int scanout, struct virgl_gl_ctx_param *params);
123 void (*destroy_gl_context)(virgl_gl_context ctx);
124 int (*make_current)(virgl_gl_context ctx);
125 int (*get_drm_fd)(void);
126 };
127
128 #define VREND_USE_THREAD_SYNC (1 << 0)
129 #define VREND_USE_EXTERNAL_BLOB (1 << 1)
130 #define VREND_USE_ASYNC_FENCE_CB (1 << 2)
131 #define VREND_USE_VIDEO (1 << 3)
132
133 bool vrend_check_no_error(struct vrend_context *ctx);
134
135 const struct virgl_resource_pipe_callbacks *
136 vrend_renderer_get_pipe_callbacks(void);
137
138 int vrend_renderer_init(const struct vrend_if_cbs *cbs, uint32_t flags);
139
140 void vrend_insert_format(struct vrend_format_table *entry, uint32_t bindings, uint32_t flags);
141 bool vrend_check_framebuffer_mixed_color_attachements(void);
142
143 void vrend_insert_format_swizzle(int override_format, struct vrend_format_table *entry,
144 uint32_t bindings, uint8_t swizzle[4], uint32_t flags);
145 const struct vrend_format_table *vrend_get_format_table_entry(enum virgl_formats format);
146
147 int vrend_create_shader(struct vrend_context *ctx,
148 uint32_t handle,
149 const struct pipe_stream_output_info *stream_output,
150 uint32_t req_local_mem,
151 const char *shd_text, uint32_t offlen, uint32_t num_tokens,
152 uint32_t type, uint32_t pkt_length);
153
154 void vrend_link_program_hook(struct vrend_context *ctx, uint32_t *handles);
155
156 void vrend_bind_shader(struct vrend_context *ctx,
157 uint32_t type,
158 uint32_t handle);
159
160 void vrend_bind_vs_so(struct vrend_context *ctx,
161 uint32_t handle);
162 void vrend_clear(struct vrend_context *ctx,
163 unsigned buffers,
164 const union pipe_color_union *color,
165 double depth, unsigned stencil);
166
167 int vrend_clear_texture(struct vrend_context* ctx,
168 uint32_t handle, uint32_t level,
169 const struct pipe_box *box,
170 const void * data);
171
172 int vrend_draw_vbo(struct vrend_context *ctx,
173 const struct pipe_draw_info *info,
174 uint32_t cso, uint32_t indirect_handle, uint32_t indirect_draw_count_handle);
175
176 void vrend_set_framebuffer_state(struct vrend_context *ctx,
177 uint32_t nr_cbufs, uint32_t surf_handle[PIPE_MAX_COLOR_BUFS],
178 uint32_t zsurf_handle);
179
180 struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *debug_name);
181 void vrend_destroy_context(struct vrend_context *ctx);
182 struct virgl_context *vrend_renderer_context_create(uint32_t handle,
183 uint32_t nlen,
184 const char *name);
185
186 struct vrend_renderer_resource_create_args {
187 enum pipe_texture_target target;
188 uint32_t format;
189 uint32_t bind;
190 uint32_t width;
191 uint32_t height;
192 uint32_t depth;
193 uint32_t array_size;
194 uint32_t last_level;
195 uint32_t nr_samples;
196 uint32_t flags;
197 };
198
199 /* set the type info of an untyped blob resource */
200 struct vrend_renderer_resource_set_type_args {
201 uint32_t format;
202 uint32_t bind;
203 uint32_t width;
204 uint32_t height;
205 uint32_t usage;
206 uint64_t modifier;
207 uint32_t plane_count;
208 uint32_t plane_strides[VIRGL_GBM_MAX_PLANES];
209 uint32_t plane_offsets[VIRGL_GBM_MAX_PLANES];
210 };
211
212 struct pipe_resource *
213 vrend_renderer_resource_create(const struct vrend_renderer_resource_create_args *args,
214 void *image_eos);
215
216 int vrend_create_surface(struct vrend_context *ctx,
217 uint32_t handle,
218 uint32_t res_handle, uint32_t format,
219 uint32_t val0, uint32_t val1,
220 uint32_t nr_samples);
221 int vrend_create_sampler_view(struct vrend_context *ctx,
222 uint32_t handle,
223 uint32_t res_handle, uint32_t format,
224 uint32_t val0, uint32_t val1, uint32_t swizzle_packed);
225
226 int vrend_create_sampler_state(struct vrend_context *ctx,
227 uint32_t handle,
228 struct pipe_sampler_state *templ);
229
230 int vrend_create_so_target(struct vrend_context *ctx,
231 uint32_t handle,
232 uint32_t res_handle,
233 uint32_t buffer_offset,
234 uint32_t buffer_size);
235
236 void vrend_set_streamout_targets(struct vrend_context *ctx,
237 uint32_t append_bitmask,
238 uint32_t num_targets,
239 uint32_t *handles);
240
241 int vrend_create_vertex_elements_state(struct vrend_context *ctx,
242 uint32_t handle,
243 unsigned num_elements,
244 const struct pipe_vertex_element *elements);
245 void vrend_bind_vertex_elements_state(struct vrend_context *ctx,
246 uint32_t handle);
247
248 void vrend_set_single_vbo(struct vrend_context *ctx,
249 uint32_t index,
250 uint32_t stride,
251 uint32_t buffer_offset,
252 uint32_t res_handle);
253 void vrend_set_num_vbo(struct vrend_context *ctx,
254 int num_vbo);
255
256 int vrend_transfer_inline_write(struct vrend_context *ctx,
257 uint32_t dst_handle,
258 const struct vrend_transfer_info *info);
259
260 int vrend_renderer_copy_transfer3d(struct vrend_context *ctx,
261 uint32_t dst_handle,
262 uint32_t src_handle,
263 const struct vrend_transfer_info *info);
264
265 int vrend_renderer_copy_transfer3d_from_host(struct vrend_context *ctx,
266 uint32_t dst_handle,
267 uint32_t src_handle,
268 const struct vrend_transfer_info *info);
269
270 void vrend_set_viewport_states(struct vrend_context *ctx,
271 uint32_t start_slot, uint32_t num_viewports,
272 const struct pipe_viewport_state *state);
273 void vrend_set_num_sampler_views(struct vrend_context *ctx,
274 uint32_t shader_type,
275 uint32_t start_slot,
276 uint32_t num_sampler_views);
277 void vrend_set_single_sampler_view(struct vrend_context *ctx,
278 uint32_t shader_type,
279 uint32_t index,
280 uint32_t res_handle);
281
282 void vrend_object_bind_blend(struct vrend_context *ctx,
283 uint32_t handle);
284 void vrend_object_bind_dsa(struct vrend_context *ctx,
285 uint32_t handle);
286 void vrend_object_bind_rasterizer(struct vrend_context *ctx,
287 uint32_t handle);
288
289 void vrend_bind_sampler_states(struct vrend_context *ctx,
290 uint32_t shader_type,
291 uint32_t start_slot,
292 uint32_t num_states,
293 const uint32_t *handles);
294 void vrend_set_index_buffer(struct vrend_context *ctx,
295 uint32_t res_handle,
296 uint32_t index_size,
297 uint32_t offset);
298 void vrend_set_single_image_view(struct vrend_context *ctx,
299 uint32_t shader_type,
300 uint32_t index,
301 uint32_t format, uint32_t access,
302 uint32_t layer_offset, uint32_t level_size,
303 uint32_t handle);
304 void vrend_set_single_ssbo(struct vrend_context *ctx,
305 uint32_t shader_type,
306 uint32_t index,
307 uint32_t offset, uint32_t length,
308 uint32_t handle);
309 void vrend_set_single_abo(struct vrend_context *ctx,
310 uint32_t index,
311 uint32_t offset, uint32_t length,
312 uint32_t handle);
313 void vrend_memory_barrier(struct vrend_context *ctx,
314 unsigned flags);
315 void vrend_launch_grid(struct vrend_context *ctx,
316 uint32_t *block,
317 uint32_t *grid,
318 uint32_t indirect_handle,
319 uint32_t indirect_offset);
320 void vrend_set_framebuffer_state_no_attach(struct vrend_context *ctx,
321 uint32_t width, uint32_t height,
322 uint32_t layers, uint32_t samples);
323 void vrend_texture_barrier(struct vrend_context *ctx,
324 unsigned flags);
325
326 int vrend_renderer_transfer_iov(struct vrend_context *ctx,
327 uint32_t dst_handle,
328 const struct vrend_transfer_info *info,
329 int transfer_mode);
330
331 int vrend_renderer_transfer_pipe(struct pipe_resource *pres,
332 const struct vrend_transfer_info *info,
333 int transfer_mode);
334
335 void vrend_renderer_resource_copy_region(struct vrend_context *ctx,
336 uint32_t dst_handle, uint32_t dst_level,
337 uint32_t dstx, uint32_t dsty, uint32_t dstz,
338 uint32_t src_handle, uint32_t src_level,
339 const struct pipe_box *src_box);
340
341 void vrend_renderer_blit(struct vrend_context *ctx,
342 uint32_t dst_handle, uint32_t src_handle,
343 const struct pipe_blit_info *info);
344
345 void vrend_set_stencil_ref(struct vrend_context *ctx, struct pipe_stencil_ref *ref);
346 void vrend_set_blend_color(struct vrend_context *ctx, struct pipe_blend_color *color);
347 void vrend_set_scissor_state(struct vrend_context *ctx,
348 uint32_t start_slot,
349 uint32_t num_scissor,
350 struct pipe_scissor_state *ss);
351
352 void vrend_set_polygon_stipple(struct vrend_context *ctx, struct pipe_poly_stipple *ps);
353
354 void vrend_set_clip_state(struct vrend_context *ctx, struct pipe_clip_state *ucp);
355 void vrend_set_sample_mask(struct vrend_context *ctx, unsigned sample_mask);
356 void vrend_set_min_samples(struct vrend_context *ctx, unsigned min_samples);
357
358 void vrend_set_constants(struct vrend_context *ctx,
359 uint32_t shader,
360 uint32_t num_constant,
361 const float *data);
362
363 void vrend_set_uniform_buffer(struct vrend_context *ctx, uint32_t shader,
364 uint32_t index, uint32_t offset, uint32_t length,
365 uint32_t res_handle);
366
367 void vrend_fb_bind_texture_id(struct vrend_resource *res,
368 int id, int idx, uint32_t level,
369 uint32_t layer, uint32_t samples);
370
371 void vrend_set_tess_state(struct vrend_context *ctx, const float tess_factors[6]);
372
373 void vrend_renderer_fini(void);
374
375 void vrend_renderer_set_fence_retire(struct vrend_context *ctx,
376 vrend_context_fence_retire retire,
377 void *retire_data);
378
379 int vrend_renderer_create_fence(struct vrend_context *ctx,
380 uint32_t flags,
381 uint64_t fence_id);
382
383 void vrend_renderer_check_fences(void);
384
385 int vrend_renderer_create_ctx0_fence(uint32_t fence_id);
386 int vrend_renderer_export_ctx0_fence(uint32_t fence_id, int* out_fd);
387
388 bool vrend_hw_switch_context(struct vrend_context *ctx, bool now);
389 uint32_t vrend_renderer_object_insert(struct vrend_context *ctx, void *data,
390 uint32_t handle, enum virgl_object_type type);
391 void vrend_renderer_object_destroy(struct vrend_context *ctx, uint32_t handle);
392
393 int vrend_create_query(struct vrend_context *ctx, uint32_t handle,
394 uint32_t query_type, uint32_t query_index,
395 uint32_t res_handle, uint32_t offset);
396
397 int vrend_begin_query(struct vrend_context *ctx, uint32_t handle);
398 int vrend_end_query(struct vrend_context *ctx, uint32_t handle);
399 void vrend_get_query_result(struct vrend_context *ctx, uint32_t handle,
400 uint32_t wait);
401 void vrend_get_query_result_qbo(struct vrend_context *ctx, uint32_t handle,
402 uint32_t qbo_handle,
403 uint32_t wait, uint32_t result_type, uint32_t offset,
404 int32_t index);
405 void vrend_render_condition(struct vrend_context *ctx,
406 uint32_t handle,
407 bool condtion,
408 uint mode);
409 void *vrend_renderer_get_cursor_contents(struct pipe_resource *pres,
410 uint32_t *width,
411 uint32_t *height);
412
413 void vrend_renderer_fill_caps(uint32_t set, uint32_t version,
414 union virgl_caps *caps);
415
416 GLint64 vrend_renderer_get_timestamp(void);
417
418 void vrend_build_format_list_common(void);
419 void vrend_build_format_list_gl(void);
420 void vrend_build_format_list_gles(void);
421 void vrend_build_emulated_format_list_gles(void);
422 void vrend_check_texture_storage(struct vrend_format_table *table);
423 void vrend_check_texture_multisample(struct vrend_format_table *table,
424 bool enable_storage);
425
426 struct vrend_resource *vrend_renderer_ctx_res_lookup(struct vrend_context *ctx,
427 int res_handle);
428
429 void vrend_renderer_resource_destroy(struct vrend_resource *res);
430
431 static inline void
vrend_resource_reference(struct vrend_resource ** ptr,struct vrend_resource * tex)432 vrend_resource_reference(struct vrend_resource **ptr, struct vrend_resource *tex)
433 {
434 struct vrend_resource *old_tex = *ptr;
435
436 if (pipe_reference(&(*ptr)->base.reference, &tex->base.reference))
437 vrend_renderer_resource_destroy(old_tex);
438 *ptr = tex;
439 }
440
441 void vrend_renderer_force_ctx_0(void);
442
443 void vrend_renderer_get_rect(struct pipe_resource *pres,
444 const struct iovec *iov, unsigned int num_iovs,
445 uint32_t offset,
446 int x, int y, int width, int height);
447
448 void vrend_renderer_attach_res_ctx(struct vrend_context *ctx,
449 struct virgl_resource *res);
450 void vrend_renderer_detach_res_ctx(struct vrend_context *ctx,
451 struct virgl_resource *res);
452
453 struct vrend_context_tweaks *vrend_get_context_tweaks(struct vrend_context *ctx);
454
455 struct vrend_renderer_resource_info {
456 uint32_t handle;
457 uint32_t format;
458 uint32_t width;
459 uint32_t height;
460 uint32_t depth;
461 uint32_t flags;
462 uint32_t tex_id;
463 uint32_t stride;
464 };
465
466 struct vrend_blit_info {
467 const struct pipe_blit_info b;
468 GLuint src_view;
469 GLuint dst_view;
470 uint8_t swizzle[4];
471 int src_y1, src_y2, dst_y1, dst_y2;
472 GLenum gl_filter;
473 bool needs_swizzle;
474 bool can_fbo_blit;
475 bool has_texture_srgb_decode;
476 bool has_srgb_write_control;
477 bool needs_manual_srgb_decode;
478 bool needs_manual_srgb_encode;
479 };
480
481 void vrend_renderer_resource_get_info(struct pipe_resource *pres,
482 struct vrend_renderer_resource_info *info);
483
484 void vrend_renderer_get_cap_set(uint32_t cap_set, uint32_t *max_ver,
485 uint32_t *max_size);
486
487 void vrend_renderer_create_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
488 void vrend_renderer_destroy_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
489 void vrend_renderer_set_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
490
491 void vrend_report_context_error_internal(const char *fname, struct vrend_context *ctx,
492 enum virgl_ctx_errors error, uint32_t value);
493
494 #define vrend_report_context_error(ctx, error, value) \
495 vrend_report_context_error_internal(__func__, ctx, error, value)
496
497 #define vrend_report_buffer_error(ctx, cmd) \
498 vrend_report_context_error(ctx, VIRGL_ERROR_CTX_ILLEGAL_CMD_BUFFER, cmd)
499
500 void vrend_fb_bind_texture(struct vrend_resource *res,
501 int idx,
502 uint32_t level, uint32_t layer);
503 bool vrend_format_is_emulated_alpha(enum virgl_formats format);
504 bool vrend_format_is_bgra(enum virgl_formats format);
505
506 #define VREND_COPY_COMPAT_FLAG_ALLOW_COMPRESSED (1u << 0)
507 #define VREND_COPY_COMPAT_FLAG_ONE_IS_EGL_IMAGE (1u << 1)
508 boolean format_is_copy_compatible(enum virgl_formats src, enum virgl_formats dst,
509 unsigned int flags);
510
511 void vrend_renderer_prepare_reset(void);
512 void vrend_renderer_reset(void);
513 void vrend_renderer_poll(void);
514 int vrend_renderer_get_poll_fd(void);
515
516 unsigned vrend_context_has_debug_flag(const struct vrend_context *ctx,
517 enum virgl_debug_flags flag);
518
519 unsigned vrend_renderer_query_multisample_caps(unsigned max_samples,
520 struct virgl_caps_v2 *caps);
521
522 struct gl_version {
523 uint32_t major;
524 uint32_t minor;
525 };
526
527 static const struct gl_version gl_versions[] = { {4,6}, {4,5}, {4,4}, {4,3}, {4,2}, {4,1}, {4,0},
528 {3,3}, {3,2}, {3,1}, {3,0} };
529
530 extern const struct vrend_if_cbs *vrend_clicbs;
531
532 int vrend_renderer_export_query(struct pipe_resource *pres,
533 struct virgl_renderer_export_query *export_query);
534
535 void vrend_sync_make_current(virgl_gl_context);
536
537 int
538 vrend_renderer_pipe_resource_create(struct vrend_context *ctx, uint32_t blob_id,
539 const struct vrend_renderer_resource_create_args *args);
540
541 struct pipe_resource *vrend_get_blob_pipe(struct vrend_context *ctx, uint64_t blob_id);
542
543 int
544 vrend_renderer_pipe_resource_set_type(struct vrend_context *ctx,
545 uint32_t res_id,
546 const struct vrend_renderer_resource_set_type_args *args);
547
548 uint32_t vrend_renderer_resource_get_map_info(struct pipe_resource *pres);
549
550 int vrend_renderer_resource_map(struct pipe_resource *pres, void **map, uint64_t *out_size);
551
552 int vrend_renderer_resource_unmap(struct pipe_resource *pres);
553
554 void vrend_renderer_get_meminfo(struct vrend_context *ctx, uint32_t res_handle);
555
556 void vrend_context_emit_string_marker(struct vrend_context *ctx, GLsizei length, const char * message);
557
558 struct vrend_video_context *vrend_context_get_video_ctx(struct vrend_context *ctx);
559
560 #endif
561