virgl: track valid buffer range for transfer sync
[mesa.git] / src / gallium / drivers / virgl / virgl_resource.h
1 /*
2 * Copyright 2014, 2015 Red Hat.
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 #ifndef VIRGL_RESOURCE_H
25 #define VIRGL_RESOURCE_H
26
27 #include "util/u_resource.h"
28 #include "util/u_range.h"
29 #include "util/list.h"
30 #include "util/u_transfer.h"
31
32 #include "virgl_hw.h"
33 #include "virgl_screen.h"
34 #define VR_MAX_TEXTURE_2D_LEVELS 15
35
36 struct winsys_handle;
37 struct virgl_screen;
38 struct virgl_context;
39
40 struct virgl_resource_metadata
41 {
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 total_size;
46 };
47
48 struct virgl_resource {
49 struct u_resource u;
50 uint16_t clean_mask;
51 struct virgl_hw_res *hw_res;
52 struct virgl_resource_metadata metadata;
53
54 /* For PIPE_BUFFER only. Data outside of this range are uninitialized. */
55 struct util_range valid_buffer_range;
56 };
57
58 enum virgl_transfer_map_type {
59 VIRGL_TRANSFER_MAP_ERROR = -1,
60 VIRGL_TRANSFER_MAP_HW_RES,
61 };
62
63 struct virgl_transfer {
64 struct pipe_transfer base;
65 uint32_t offset, l_stride;
66 struct util_range range;
67 struct list_head queue_link;
68 struct pipe_transfer *resolve_transfer;
69 void *hw_res_map;
70 };
71
72 void virgl_resource_destroy(struct pipe_screen *screen,
73 struct pipe_resource *resource);
74
75 void virgl_init_screen_resource_functions(struct pipe_screen *screen);
76
77 void virgl_init_context_resource_functions(struct pipe_context *ctx);
78
79 void virgl_texture_init(struct virgl_resource *res);
80
81 static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
82 {
83 return (struct virgl_resource *)r;
84 }
85
86 static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
87 {
88 return (struct virgl_transfer *)trans;
89 }
90
91 void virgl_buffer_init(struct virgl_resource *res);
92
93 static inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs, unsigned pbind)
94 {
95 unsigned outbind = 0;
96 if (pbind & PIPE_BIND_DEPTH_STENCIL)
97 outbind |= VIRGL_BIND_DEPTH_STENCIL;
98 if (pbind & PIPE_BIND_RENDER_TARGET)
99 outbind |= VIRGL_BIND_RENDER_TARGET;
100 if (pbind & PIPE_BIND_SAMPLER_VIEW)
101 outbind |= VIRGL_BIND_SAMPLER_VIEW;
102 if (pbind & PIPE_BIND_VERTEX_BUFFER)
103 outbind |= VIRGL_BIND_VERTEX_BUFFER;
104 if (pbind & PIPE_BIND_INDEX_BUFFER)
105 outbind |= VIRGL_BIND_INDEX_BUFFER;
106 if (pbind & PIPE_BIND_CONSTANT_BUFFER)
107 outbind |= VIRGL_BIND_CONSTANT_BUFFER;
108 if (pbind & PIPE_BIND_DISPLAY_TARGET)
109 outbind |= VIRGL_BIND_DISPLAY_TARGET;
110 if (pbind & PIPE_BIND_STREAM_OUTPUT)
111 outbind |= VIRGL_BIND_STREAM_OUTPUT;
112 if (pbind & PIPE_BIND_CURSOR)
113 outbind |= VIRGL_BIND_CURSOR;
114 if (pbind & PIPE_BIND_CUSTOM)
115 outbind |= VIRGL_BIND_CUSTOM;
116 if (pbind & PIPE_BIND_SCANOUT)
117 outbind |= VIRGL_BIND_SCANOUT;
118 if (pbind & PIPE_BIND_SHADER_BUFFER)
119 outbind |= VIRGL_BIND_SHADER_BUFFER;
120 if (pbind & PIPE_BIND_QUERY_BUFFER)
121 outbind |= VIRGL_BIND_QUERY_BUFFER;
122 if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
123 if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
124 outbind |= VIRGL_BIND_COMMAND_ARGS;
125 return outbind;
126 }
127
128 enum virgl_transfer_map_type
129 virgl_resource_transfer_prepare(struct virgl_context *vctx,
130 struct virgl_transfer *xfer);
131
132 void virgl_resource_layout(struct pipe_resource *pt,
133 struct virgl_resource_metadata *metadata);
134
135 struct virgl_transfer *
136 virgl_resource_create_transfer(struct slab_child_pool *pool,
137 struct pipe_resource *pres,
138 const struct virgl_resource_metadata *metadata,
139 unsigned level, unsigned usage,
140 const struct pipe_box *box);
141
142 void virgl_resource_destroy_transfer(struct slab_child_pool *pool,
143 struct virgl_transfer *trans);
144
145 void virgl_resource_destroy(struct pipe_screen *screen,
146 struct pipe_resource *resource);
147
148 boolean virgl_resource_get_handle(struct pipe_screen *screen,
149 struct pipe_resource *resource,
150 struct winsys_handle *whandle);
151
152 void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
153
154 #endif