virgl_resource.h revision b8e80941
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
36struct winsys_handle;
37struct virgl_screen;
38struct virgl_context;
39
40struct 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
48struct 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
55struct virgl_transfer {
56   struct pipe_transfer base;
57   uint32_t offset, l_stride;
58   struct util_range range;
59   struct list_head queue_link;
60   struct pipe_transfer *resolve_transfer;
61   void *hw_res_map;
62};
63
64void virgl_resource_destroy(struct pipe_screen *screen,
65                            struct pipe_resource *resource);
66
67void virgl_init_screen_resource_functions(struct pipe_screen *screen);
68
69void virgl_init_context_resource_functions(struct pipe_context *ctx);
70
71void virgl_texture_init(struct virgl_resource *res);
72
73static inline struct virgl_resource *virgl_resource(struct pipe_resource *r)
74{
75   return (struct virgl_resource *)r;
76}
77
78static inline struct virgl_transfer *virgl_transfer(struct pipe_transfer *trans)
79{
80   return (struct virgl_transfer *)trans;
81}
82
83void virgl_buffer_init(struct virgl_resource *res);
84
85static inline unsigned pipe_to_virgl_bind(const struct virgl_screen *vs, unsigned pbind)
86{
87   unsigned outbind = 0;
88   if (pbind & PIPE_BIND_DEPTH_STENCIL)
89      outbind |= VIRGL_BIND_DEPTH_STENCIL;
90   if (pbind & PIPE_BIND_RENDER_TARGET)
91      outbind |= VIRGL_BIND_RENDER_TARGET;
92   if (pbind & PIPE_BIND_SAMPLER_VIEW)
93      outbind |= VIRGL_BIND_SAMPLER_VIEW;
94   if (pbind & PIPE_BIND_VERTEX_BUFFER)
95      outbind |= VIRGL_BIND_VERTEX_BUFFER;
96   if (pbind & PIPE_BIND_INDEX_BUFFER)
97      outbind |= VIRGL_BIND_INDEX_BUFFER;
98   if (pbind & PIPE_BIND_CONSTANT_BUFFER)
99      outbind |= VIRGL_BIND_CONSTANT_BUFFER;
100   if (pbind & PIPE_BIND_DISPLAY_TARGET)
101      outbind |= VIRGL_BIND_DISPLAY_TARGET;
102   if (pbind & PIPE_BIND_STREAM_OUTPUT)
103      outbind |= VIRGL_BIND_STREAM_OUTPUT;
104   if (pbind & PIPE_BIND_CURSOR)
105      outbind |= VIRGL_BIND_CURSOR;
106   if (pbind & PIPE_BIND_CUSTOM)
107      outbind |= VIRGL_BIND_CUSTOM;
108   if (pbind & PIPE_BIND_SCANOUT)
109      outbind |= VIRGL_BIND_SCANOUT;
110   if (pbind & PIPE_BIND_SHADER_BUFFER)
111      outbind |= VIRGL_BIND_SHADER_BUFFER;
112   if (pbind & PIPE_BIND_QUERY_BUFFER)
113      outbind |= VIRGL_BIND_QUERY_BUFFER;
114   if (pbind & PIPE_BIND_COMMAND_ARGS_BUFFER)
115      if (vs->caps.caps.v2.capability_bits & VIRGL_CAP_BIND_COMMAND_ARGS)
116         outbind |= VIRGL_BIND_COMMAND_ARGS;
117   return outbind;
118}
119
120bool virgl_res_needs_flush(struct virgl_context *vctx,
121                           struct virgl_transfer *transfer);
122bool virgl_res_needs_readback(struct virgl_context *vctx,
123                              struct virgl_resource *res,
124                              unsigned usage, unsigned level);
125
126void virgl_resource_layout(struct pipe_resource *pt,
127                           struct virgl_resource_metadata *metadata);
128
129struct virgl_transfer *
130virgl_resource_create_transfer(struct slab_child_pool *pool,
131                               struct pipe_resource *pres,
132                               const struct virgl_resource_metadata *metadata,
133                               unsigned level, unsigned usage,
134                               const struct pipe_box *box);
135
136void virgl_resource_destroy_transfer(struct slab_child_pool *pool,
137                                     struct virgl_transfer *trans);
138
139void virgl_resource_destroy(struct pipe_screen *screen,
140                            struct pipe_resource *resource);
141
142boolean virgl_resource_get_handle(struct pipe_screen *screen,
143                                  struct pipe_resource *resource,
144                                  struct winsys_handle *whandle);
145
146void virgl_resource_dirty(struct virgl_resource *res, uint32_t level);
147
148#endif
149