1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #ifndef LP_TEXTURE_H
29 #define LP_TEXTURE_H
30
31
32 #include "pipe/p_state.h"
33 #include "util/u_debug.h"
34 #include "lp_limits.h"
35 #include "util/bitset.h"
36 #if MESA_DEBUG
37 #include "util/list.h"
38 #endif
39
40
41 enum lp_texture_usage
42 {
43 LP_TEX_USAGE_READ = 100,
44 LP_TEX_USAGE_READ_WRITE,
45 LP_TEX_USAGE_WRITE_ALL
46 };
47
48 enum llvmpipe_memory_fd_type
49 {
50 LLVMPIPE_MEMORY_FD_TYPE_OPAQUE,
51 LLVMPIPE_MEMORY_FD_TYPE_DMA_BUF,
52 };
53
54 struct pipe_context;
55 struct pipe_screen;
56 struct llvmpipe_context;
57 struct llvmpipe_screen;
58
59 struct sw_displaytarget;
60
61 /**
62 * llvmpipe subclass of pipe_resource. A texture, drawing surface,
63 * vertex buffer, const buffer, etc.
64 * Textures are stored differently than other types of objects such as
65 * vertex buffers and const buffers.
66 * The latter are simple malloc'd blocks of memory.
67 */
68 struct llvmpipe_resource
69 {
70 struct pipe_resource base;
71
72 /** an extra screen pointer to avoid crashing in driver trace */
73 struct llvmpipe_screen *screen;
74
75 /** Row stride in bytes */
76 unsigned row_stride[LP_MAX_TEXTURE_LEVELS];
77 /** Image stride (for cube maps, array or 3D textures) in bytes */
78 uint64_t img_stride[LP_MAX_TEXTURE_LEVELS];
79 /** Offset to start of mipmap level, in bytes */
80 uint64_t mip_offsets[LP_MAX_TEXTURE_LEVELS];
81 /** allocated total size (for non-display target texture resources only) */
82 uint64_t total_alloc_size;
83
84 /**
85 * Display target, for textures with the PIPE_BIND_DISPLAY_TARGET
86 * usage.
87 */
88 struct sw_displaytarget *dt;
89 enum pipe_format dt_format;
90
91 /**
92 * Malloc'ed data for regular textures, or a mapping to dt above.
93 */
94 void *tex_data;
95
96 BITSET_WORD *residency;
97
98 /**
99 * Data for non-texture resources.
100 */
101 void *data;
102
103 bool user_ptr; /** Is this a user-space buffer? */
104 unsigned timestamp;
105
106 unsigned id; /**< temporary, for debugging */
107
108 unsigned sample_stride;
109
110 uint64_t size_required;
111 uint64_t backing_offset;
112 #ifdef HAVE_LIBDRM
113 struct llvmpipe_memory_allocation *dmabuf_alloc;
114 #endif
115 bool backable;
116 bool imported_memory;
117 bool dmabuf;
118 #if MESA_DEBUG
119 struct list_head list;
120 #endif
121 };
122
123
124 struct llvmpipe_transfer
125 {
126 struct pipe_transfer base;
127 void *map;
128 struct pipe_box block_box;
129 };
130
131 struct llvmpipe_memory_allocation
132 {
133 int fd;
134 uint64_t offset;
135 void *cpu_addr;
136 uint64_t size;
137 enum llvmpipe_memory_fd_type type;
138 int mem_fd;
139 int dmabuf_fd;
140 };
141
142 struct llvmpipe_memory_object
143 {
144 struct pipe_memory_object b;
145 struct pipe_memory_allocation *data;
146 uint64_t size;
147 };
148
149
150 /** cast wrappers */
151 static inline struct llvmpipe_resource *
llvmpipe_resource(struct pipe_resource * pt)152 llvmpipe_resource(struct pipe_resource *pt)
153 {
154 return (struct llvmpipe_resource *) pt;
155 }
156
157
158 static inline const struct llvmpipe_resource *
llvmpipe_resource_const(const struct pipe_resource * pt)159 llvmpipe_resource_const(const struct pipe_resource *pt)
160 {
161 return (const struct llvmpipe_resource *) pt;
162 }
163
164
165 static inline struct llvmpipe_transfer *
llvmpipe_transfer(struct pipe_transfer * pt)166 llvmpipe_transfer(struct pipe_transfer *pt)
167 {
168 return (struct llvmpipe_transfer *) pt;
169 }
170
171
172 static inline struct llvmpipe_memory_object *
llvmpipe_memory_object(struct pipe_memory_object * pt)173 llvmpipe_memory_object(struct pipe_memory_object *pt)
174 {
175 return (struct llvmpipe_memory_object *) pt;
176 }
177
178
179 void llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen);
180 void llvmpipe_init_context_resource_funcs(struct pipe_context *pipe);
181
182
183 static inline bool
llvmpipe_resource_is_texture(const struct pipe_resource * resource)184 llvmpipe_resource_is_texture(const struct pipe_resource *resource)
185 {
186 switch (resource->target) {
187 case PIPE_BUFFER:
188 return false;
189 case PIPE_TEXTURE_1D:
190 case PIPE_TEXTURE_1D_ARRAY:
191 case PIPE_TEXTURE_2D:
192 case PIPE_TEXTURE_2D_ARRAY:
193 case PIPE_TEXTURE_RECT:
194 case PIPE_TEXTURE_3D:
195 case PIPE_TEXTURE_CUBE:
196 case PIPE_TEXTURE_CUBE_ARRAY:
197 return true;
198 default:
199 assert(0);
200 return false;
201 }
202 }
203
204
205 static inline bool
llvmpipe_resource_is_1d(const struct pipe_resource * resource)206 llvmpipe_resource_is_1d(const struct pipe_resource *resource)
207 {
208 switch (resource->target) {
209 case PIPE_BUFFER:
210 case PIPE_TEXTURE_1D:
211 case PIPE_TEXTURE_1D_ARRAY:
212 return true;
213 case PIPE_TEXTURE_2D:
214 case PIPE_TEXTURE_2D_ARRAY:
215 case PIPE_TEXTURE_RECT:
216 case PIPE_TEXTURE_3D:
217 case PIPE_TEXTURE_CUBE:
218 case PIPE_TEXTURE_CUBE_ARRAY:
219 return false;
220 default:
221 assert(0);
222 return false;
223 }
224 }
225
226
227 static inline unsigned
llvmpipe_layer_stride(struct pipe_resource * resource,unsigned level)228 llvmpipe_layer_stride(struct pipe_resource *resource,
229 unsigned level)
230 {
231 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
232 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
233 return lpr->img_stride[level];
234 }
235
236
237 static inline unsigned
llvmpipe_resource_stride(struct pipe_resource * resource,unsigned level)238 llvmpipe_resource_stride(struct pipe_resource *resource,
239 unsigned level)
240 {
241 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
242 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
243 return lpr->row_stride[level];
244 }
245
246
247 static inline unsigned
llvmpipe_sample_stride(struct pipe_resource * resource)248 llvmpipe_sample_stride(struct pipe_resource *resource)
249 {
250 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
251 return lpr->sample_stride;
252 }
253
254
255 void *
256 llvmpipe_resource_map(struct pipe_resource *resource,
257 unsigned level,
258 unsigned layer,
259 enum lp_texture_usage tex_usage);
260
261 void
262 llvmpipe_resource_unmap(struct pipe_resource *resource,
263 unsigned level,
264 unsigned layer);
265
266
267 void *
268 llvmpipe_resource_data(struct pipe_resource *resource);
269
270
271 unsigned
272 llvmpipe_resource_size(const struct pipe_resource *resource);
273
274
275 uint8_t *
276 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
277 unsigned face_slice, unsigned level);
278
279
280 extern void
281 llvmpipe_print_resources(void);
282
283
284 #define LP_UNREFERENCED 0
285 #define LP_REFERENCED_FOR_READ (1 << 0)
286 #define LP_REFERENCED_FOR_WRITE (1 << 1)
287
288
289 unsigned int
290 llvmpipe_is_resource_referenced(struct pipe_context *pipe,
291 struct pipe_resource *presource,
292 unsigned level);
293
294 unsigned
295 llvmpipe_get_format_alignment(enum pipe_format format);
296
297
298 void *
299 llvmpipe_transfer_map_ms(struct pipe_context *pipe,
300 struct pipe_resource *resource,
301 unsigned level,
302 unsigned usage,
303 unsigned sample,
304 const struct pipe_box *box,
305 struct pipe_transfer **transfer);
306
307 uint32_t
308 llvmpipe_get_texel_offset(struct pipe_resource *resource,
309 uint32_t level, uint32_t x,
310 uint32_t y, uint32_t z);
311
312 #endif /* LP_TEXTURE_H */
313