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
104 struct vrend_format_table {
105 enum virgl_formats format;
106 GLenum internalformat;
107 GLenum glformat;
108 GLenum gltype;
109 uint8_t swizzle[4];
110 uint32_t bindings;
111 uint32_t flags;
112 };
113
114 typedef void (*vrend_context_fence_retire)(void *fence_cookie,
115 void *retire_data);
116
117 struct vrend_if_cbs {
118 vrend_context_fence_retire ctx0_fence_retire;
119
120 virgl_gl_context (*create_gl_context)(int scanout, struct virgl_gl_ctx_param *params);
121 void (*destroy_gl_context)(virgl_gl_context ctx);
122 int (*make_current)(virgl_gl_context ctx);
123 };
124
125 #define VREND_USE_THREAD_SYNC (1 << 0)
126 #define VREND_USE_EXTERNAL_BLOB (1 << 1)
127 #define VREND_USE_ASYNC_FENCE_CB (1 << 2)
128
129 const struct virgl_resource_pipe_callbacks *
130 vrend_renderer_get_pipe_callbacks(void);
131
132 int vrend_renderer_init(const struct vrend_if_cbs *cbs, uint32_t flags);
133
134 void vrend_insert_format(struct vrend_format_table *entry, uint32_t bindings, uint32_t flags);
135 bool vrend_check_framebuffer_mixed_color_attachements(void);
136
137 void vrend_insert_format_swizzle(int override_format, struct vrend_format_table *entry,
138 uint32_t bindings, uint8_t swizzle[4], uint32_t flags);
139 const struct vrend_format_table *vrend_get_format_table_entry(enum virgl_formats format);
140
141 int vrend_create_shader(struct vrend_context *ctx,
142 uint32_t handle,
143 const struct pipe_stream_output_info *stream_output,
144 uint32_t req_local_mem,
145 const char *shd_text, uint32_t offlen, uint32_t num_tokens,
146 uint32_t type, uint32_t pkt_length);
147
148 void vrend_bind_shader(struct vrend_context *ctx,
149 uint32_t type,
150 uint32_t handle);
151
152 void vrend_bind_vs_so(struct vrend_context *ctx,
153 uint32_t handle);
154 void vrend_clear(struct vrend_context *ctx,
155 unsigned buffers,
156 const union pipe_color_union *color,
157 double depth, unsigned stencil);
158
159 void vrend_clear_texture(struct vrend_context* ctx,
160 uint32_t handle, uint32_t level,
161 const struct pipe_box *box,
162 const void * data);
163
164 int vrend_draw_vbo(struct vrend_context *ctx,
165 const struct pipe_draw_info *info,
166 uint32_t cso, uint32_t indirect_handle, uint32_t indirect_draw_count_handle);
167
168 void vrend_set_framebuffer_state(struct vrend_context *ctx,
169 uint32_t nr_cbufs, uint32_t surf_handle[PIPE_MAX_COLOR_BUFS],
170 uint32_t zsurf_handle);
171
172 struct vrend_context *vrend_create_context(int id, uint32_t nlen, const char *debug_name);
173 void vrend_destroy_context(struct vrend_context *ctx);
174 struct virgl_context *vrend_renderer_context_create(uint32_t handle,
175 uint32_t nlen,
176 const char *name);
177
178 struct vrend_renderer_resource_create_args {
179 enum pipe_texture_target target;
180 uint32_t format;
181 uint32_t bind;
182 uint32_t width;
183 uint32_t height;
184 uint32_t depth;
185 uint32_t array_size;
186 uint32_t last_level;
187 uint32_t nr_samples;
188 uint32_t flags;
189 };
190
191 /* set the type info of an untyped blob resource */
192 struct vrend_renderer_resource_set_type_args {
193 uint32_t format;
194 uint32_t bind;
195 uint32_t width;
196 uint32_t height;
197 uint32_t usage;
198 uint64_t modifier;
199 uint32_t plane_count;
200 uint32_t plane_strides[VIRGL_GBM_MAX_PLANES];
201 uint32_t plane_offsets[VIRGL_GBM_MAX_PLANES];
202 };
203
204 struct pipe_resource *
205 vrend_renderer_resource_create(const struct vrend_renderer_resource_create_args *args,
206 void *image_eos);
207
208 int vrend_create_surface(struct vrend_context *ctx,
209 uint32_t handle,
210 uint32_t res_handle, uint32_t format,
211 uint32_t val0, uint32_t val1,
212 uint32_t nr_samples);
213 int vrend_create_sampler_view(struct vrend_context *ctx,
214 uint32_t handle,
215 uint32_t res_handle, uint32_t format,
216 uint32_t val0, uint32_t val1, uint32_t swizzle_packed);
217
218 int vrend_create_sampler_state(struct vrend_context *ctx,
219 uint32_t handle,
220 struct pipe_sampler_state *templ);
221
222 int vrend_create_so_target(struct vrend_context *ctx,
223 uint32_t handle,
224 uint32_t res_handle,
225 uint32_t buffer_offset,
226 uint32_t buffer_size);
227
228 void vrend_set_streamout_targets(struct vrend_context *ctx,
229 uint32_t append_bitmask,
230 uint32_t num_targets,
231 uint32_t *handles);
232
233 int vrend_create_vertex_elements_state(struct vrend_context *ctx,
234 uint32_t handle,
235 unsigned num_elements,
236 const struct pipe_vertex_element *elements);
237 void vrend_bind_vertex_elements_state(struct vrend_context *ctx,
238 uint32_t handle);
239
240 void vrend_set_single_vbo(struct vrend_context *ctx,
241 uint32_t index,
242 uint32_t stride,
243 uint32_t buffer_offset,
244 uint32_t res_handle);
245 void vrend_set_num_vbo(struct vrend_context *ctx,
246 int num_vbo);
247
248 int vrend_transfer_inline_write(struct vrend_context *ctx,
249 uint32_t dst_handle,
250 const struct vrend_transfer_info *info);
251
252 int vrend_renderer_copy_transfer3d(struct vrend_context *ctx,
253 uint32_t dst_handle,
254 uint32_t src_handle,
255 const struct vrend_transfer_info *info);
256
257 void vrend_set_viewport_states(struct vrend_context *ctx,
258 uint32_t start_slot, uint32_t num_viewports,
259 const struct pipe_viewport_state *state);
260 void vrend_set_num_sampler_views(struct vrend_context *ctx,
261 uint32_t shader_type,
262 uint32_t start_slot,
263 uint32_t num_sampler_views);
264 void vrend_set_single_sampler_view(struct vrend_context *ctx,
265 uint32_t shader_type,
266 uint32_t index,
267 uint32_t res_handle);
268
269 void vrend_object_bind_blend(struct vrend_context *ctx,
270 uint32_t handle);
271 void vrend_object_bind_dsa(struct vrend_context *ctx,
272 uint32_t handle);
273 void vrend_object_bind_rasterizer(struct vrend_context *ctx,
274 uint32_t handle);
275
276 void vrend_bind_sampler_states(struct vrend_context *ctx,
277 uint32_t shader_type,
278 uint32_t start_slot,
279 uint32_t num_states,
280 const uint32_t *handles);
281 void vrend_set_index_buffer(struct vrend_context *ctx,
282 uint32_t res_handle,
283 uint32_t index_size,
284 uint32_t offset);
285 void vrend_set_single_image_view(struct vrend_context *ctx,
286 uint32_t shader_type,
287 uint32_t index,
288 uint32_t format, uint32_t access,
289 uint32_t layer_offset, uint32_t level_size,
290 uint32_t handle);
291 void vrend_set_single_ssbo(struct vrend_context *ctx,
292 uint32_t shader_type,
293 uint32_t index,
294 uint32_t offset, uint32_t length,
295 uint32_t handle);
296 void vrend_set_single_abo(struct vrend_context *ctx,
297 uint32_t index,
298 uint32_t offset, uint32_t length,
299 uint32_t handle);
300 void vrend_memory_barrier(struct vrend_context *ctx,
301 unsigned flags);
302 void vrend_launch_grid(struct vrend_context *ctx,
303 uint32_t *block,
304 uint32_t *grid,
305 uint32_t indirect_handle,
306 uint32_t indirect_offset);
307 void vrend_set_framebuffer_state_no_attach(struct vrend_context *ctx,
308 uint32_t width, uint32_t height,
309 uint32_t layers, uint32_t samples);
310 void vrend_texture_barrier(struct vrend_context *ctx,
311 unsigned flags);
312
313 int vrend_renderer_transfer_iov(struct vrend_context *ctx,
314 uint32_t dst_handle,
315 const struct vrend_transfer_info *info,
316 int transfer_mode);
317
318 int vrend_renderer_transfer_pipe(struct pipe_resource *pres,
319 const struct vrend_transfer_info *info,
320 int transfer_mode);
321
322 void vrend_renderer_resource_copy_region(struct vrend_context *ctx,
323 uint32_t dst_handle, uint32_t dst_level,
324 uint32_t dstx, uint32_t dsty, uint32_t dstz,
325 uint32_t src_handle, uint32_t src_level,
326 const struct pipe_box *src_box);
327
328 void vrend_renderer_blit(struct vrend_context *ctx,
329 uint32_t dst_handle, uint32_t src_handle,
330 const struct pipe_blit_info *info);
331
332 void vrend_set_stencil_ref(struct vrend_context *ctx, struct pipe_stencil_ref *ref);
333 void vrend_set_blend_color(struct vrend_context *ctx, struct pipe_blend_color *color);
334 void vrend_set_scissor_state(struct vrend_context *ctx,
335 uint32_t start_slot,
336 uint32_t num_scissor,
337 struct pipe_scissor_state *ss);
338
339 void vrend_set_polygon_stipple(struct vrend_context *ctx, struct pipe_poly_stipple *ps);
340
341 void vrend_set_clip_state(struct vrend_context *ctx, struct pipe_clip_state *ucp);
342 void vrend_set_sample_mask(struct vrend_context *ctx, unsigned sample_mask);
343 void vrend_set_min_samples(struct vrend_context *ctx, unsigned min_samples);
344
345 void vrend_set_constants(struct vrend_context *ctx,
346 uint32_t shader,
347 uint32_t num_constant,
348 const float *data);
349
350 void vrend_set_uniform_buffer(struct vrend_context *ctx, uint32_t shader,
351 uint32_t index, uint32_t offset, uint32_t length,
352 uint32_t res_handle);
353
354 void vrend_fb_bind_texture_id(struct vrend_resource *res,
355 int id, int idx, uint32_t level,
356 uint32_t layer, uint32_t samples);
357
358 void vrend_set_tess_state(struct vrend_context *ctx, const float tess_factors[6]);
359
360 void vrend_renderer_fini(void);
361
362 void vrend_renderer_set_fence_retire(struct vrend_context *ctx,
363 vrend_context_fence_retire retire,
364 void *retire_data);
365
366 int vrend_renderer_create_fence(struct vrend_context *ctx,
367 uint32_t flags,
368 void *fence_cookie);
369
370 void vrend_renderer_check_fences(void);
371
372 int vrend_renderer_create_ctx0_fence(uint32_t fence_id);
373 int vrend_renderer_export_ctx0_fence(uint32_t fence_id, int* out_fd);
374
375 bool vrend_hw_switch_context(struct vrend_context *ctx, bool now);
376 uint32_t vrend_renderer_object_insert(struct vrend_context *ctx, void *data,
377 uint32_t handle, enum virgl_object_type type);
378 void vrend_renderer_object_destroy(struct vrend_context *ctx, uint32_t handle);
379
380 int vrend_create_query(struct vrend_context *ctx, uint32_t handle,
381 uint32_t query_type, uint32_t query_index,
382 uint32_t res_handle, uint32_t offset);
383
384 int vrend_begin_query(struct vrend_context *ctx, uint32_t handle);
385 int vrend_end_query(struct vrend_context *ctx, uint32_t handle);
386 void vrend_get_query_result(struct vrend_context *ctx, uint32_t handle,
387 uint32_t wait);
388 void vrend_get_query_result_qbo(struct vrend_context *ctx, uint32_t handle,
389 uint32_t qbo_handle,
390 uint32_t wait, uint32_t result_type, uint32_t offset,
391 int32_t index);
392 void vrend_render_condition(struct vrend_context *ctx,
393 uint32_t handle,
394 bool condtion,
395 uint mode);
396 void *vrend_renderer_get_cursor_contents(struct pipe_resource *pres,
397 uint32_t *width,
398 uint32_t *height);
399
400 void vrend_renderer_fill_caps(uint32_t set, uint32_t version,
401 union virgl_caps *caps);
402
403 GLint64 vrend_renderer_get_timestamp(void);
404
405 void vrend_build_format_list_common(void);
406 void vrend_build_format_list_gl(void);
407 void vrend_build_format_list_gles(void);
408 void vrend_build_emulated_format_list_gles(void);
409 void vrend_check_texture_storage(struct vrend_format_table *table);
410
411 void vrend_renderer_resource_destroy(struct vrend_resource *res);
412
413 static inline void
vrend_resource_reference(struct vrend_resource ** ptr,struct vrend_resource * tex)414 vrend_resource_reference(struct vrend_resource **ptr, struct vrend_resource *tex)
415 {
416 struct vrend_resource *old_tex = *ptr;
417
418 if (pipe_reference(&(*ptr)->base.reference, &tex->base.reference))
419 vrend_renderer_resource_destroy(old_tex);
420 *ptr = tex;
421 }
422
423 void vrend_renderer_force_ctx_0(void);
424
425 void vrend_renderer_get_rect(struct pipe_resource *pres,
426 const struct iovec *iov, unsigned int num_iovs,
427 uint32_t offset,
428 int x, int y, int width, int height);
429
430 void vrend_renderer_attach_res_ctx(struct vrend_context *ctx,
431 struct virgl_resource *res);
432 void vrend_renderer_detach_res_ctx(struct vrend_context *ctx,
433 struct virgl_resource *res);
434
435 struct vrend_context_tweaks *vrend_get_context_tweaks(struct vrend_context *ctx);
436
437 struct vrend_renderer_resource_info {
438 uint32_t handle;
439 uint32_t format;
440 uint32_t width;
441 uint32_t height;
442 uint32_t depth;
443 uint32_t flags;
444 uint32_t tex_id;
445 uint32_t stride;
446 };
447
448 void vrend_renderer_resource_get_info(struct pipe_resource *pres,
449 struct vrend_renderer_resource_info *info);
450
451 void vrend_renderer_get_cap_set(uint32_t cap_set, uint32_t *max_ver,
452 uint32_t *max_size);
453
454 void vrend_renderer_create_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
455 void vrend_renderer_destroy_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
456 void vrend_renderer_set_sub_ctx(struct vrend_context *ctx, int sub_ctx_id);
457
458 void vrend_report_context_error_internal(const char *fname, struct vrend_context *ctx,
459 enum virgl_ctx_errors error, uint32_t value);
460
461 #define vrend_report_context_error(ctx, error, value) \
462 vrend_report_context_error_internal(__func__, ctx, error, value)
463
464 #define vrend_report_buffer_error(ctx, cmd) \
465 vrend_report_context_error(ctx, VIRGL_ERROR_CTX_ILLEGAL_CMD_BUFFER, cmd)
466
467 void vrend_fb_bind_texture(struct vrend_resource *res,
468 int idx,
469 uint32_t level, uint32_t layer);
470 bool vrend_format_is_emulated_alpha(enum virgl_formats format);
471 bool vrend_format_is_bgra(enum virgl_formats format);
472
473 #define VREND_COPY_COMPAT_FLAG_ALLOW_COMPRESSED (1u << 0)
474 #define VREND_COPY_COMPAT_FLAG_ONE_IS_EGL_IMAGE (1u << 1)
475 boolean format_is_copy_compatible(enum virgl_formats src, enum virgl_formats dst,
476 unsigned int flags);
477
478 /* blitter interface */
479 void vrend_renderer_blit_gl(struct vrend_context *ctx,
480 struct vrend_resource *src_res,
481 struct vrend_resource *dst_res,
482 GLenum blit_views[2],
483 const struct pipe_blit_info *info,
484 bool has_texture_srgb_decode,
485 bool has_srgb_write_control,
486 uint8_t swizzle[static 4]);
487 void vrend_blitter_fini(void);
488
489 void vrend_renderer_prepare_reset(void);
490 void vrend_renderer_reset(void);
491 int vrend_renderer_get_poll_fd(void);
492
493 unsigned vrend_context_has_debug_flag(const struct vrend_context *ctx,
494 enum virgl_debug_flags flag);
495
496 unsigned vrend_renderer_query_multisample_caps(unsigned max_samples,
497 struct virgl_caps_v2 *caps);
498
499 struct gl_version {
500 uint32_t major;
501 uint32_t minor;
502 };
503
504 static const struct gl_version gl_versions[] = { {4,6}, {4,5}, {4,4}, {4,3}, {4,2}, {4,1}, {4,0},
505 {3,3}, {3,2}, {3,1}, {3,0} };
506
507 extern const struct vrend_if_cbs *vrend_clicbs;
508
509 int vrend_renderer_export_query(struct pipe_resource *pres,
510 struct virgl_renderer_export_query *export_query);
511
512 void vrend_sync_make_current(virgl_gl_context);
513
514 int
515 vrend_renderer_pipe_resource_create(struct vrend_context *ctx, uint32_t blob_id,
516 const struct vrend_renderer_resource_create_args *args);
517
518 struct pipe_resource *vrend_get_blob_pipe(struct vrend_context *ctx, uint64_t blob_id);
519
520 int
521 vrend_renderer_pipe_resource_set_type(struct vrend_context *ctx,
522 uint32_t res_id,
523 const struct vrend_renderer_resource_set_type_args *args);
524
525 uint32_t vrend_renderer_resource_get_map_info(struct pipe_resource *pres);
526
527 int vrend_renderer_resource_map(struct pipe_resource *pres, void **map, uint64_t *out_size);
528
529 int vrend_renderer_resource_unmap(struct pipe_resource *pres);
530
531 void vrend_renderer_get_meminfo(struct vrend_context *ctx, uint32_t res_handle);
532
533 void vrend_context_emit_string_marker(struct vrend_context *ctx, GLsizei length, const char * message);
534 #endif
535