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 pipe_memory_object;
57 struct llvmpipe_context;
58 struct llvmpipe_screen;
59
60 struct sw_displaytarget;
61
62 /**
63 * llvmpipe subclass of pipe_resource. A texture, drawing surface,
64 * vertex buffer, const buffer, etc.
65 * Textures are stored differently than other types of objects such as
66 * vertex buffers and const buffers.
67 * The latter are simple malloc'd blocks of memory.
68 */
69 struct llvmpipe_resource
70 {
71 struct pipe_resource base;
72
73 /** an extra screen pointer to avoid crashing in driver trace */
74 struct llvmpipe_screen *screen;
75
76 /** Row stride in bytes */
77 unsigned row_stride[LP_MAX_TEXTURE_LEVELS];
78 /** Image stride (for cube maps, array or 3D textures) in bytes */
79 uint64_t img_stride[LP_MAX_TEXTURE_LEVELS];
80 /** Offset to start of mipmap level, in bytes */
81 uint64_t mip_offsets[LP_MAX_TEXTURE_LEVELS];
82 /** allocated total size (for non-display target texture resources only) */
83 uint64_t total_alloc_size;
84
85 /**
86 * Display target, for textures with the PIPE_BIND_DISPLAY_TARGET
87 * usage.
88 */
89 struct sw_displaytarget *dt;
90 enum pipe_format dt_format;
91
92 /**
93 * Malloc'ed data for regular textures, or a mapping to dt above.
94 */
95 void *tex_data;
96
97 BITSET_WORD *residency;
98
99 /**
100 * Data for non-texture resources.
101 */
102 void *data;
103
104 bool user_ptr; /** Is this a user-space buffer? */
105 unsigned timestamp;
106
107 unsigned id; /**< temporary, for debugging */
108
109 unsigned sample_stride;
110
111 uint64_t size_required;
112 uint64_t backing_offset;
113 #ifdef HAVE_LIBDRM
114 struct llvmpipe_memory_allocation *dmabuf_alloc;
115 #endif
116 bool backable;
117 struct pipe_memory_object *imported_memory;
118 bool dmabuf;
119 #if MESA_DEBUG
120 struct list_head list;
121 #endif
122 };
123
124
125 struct llvmpipe_transfer
126 {
127 struct pipe_transfer base;
128 void *map;
129 struct pipe_box block_box;
130 };
131
132 struct llvmpipe_memory_allocation
133 {
134 int fd;
135 uint64_t offset;
136 void *cpu_addr;
137 uint64_t size;
138 enum llvmpipe_memory_fd_type type;
139 int mem_fd;
140 int dmabuf_fd;
141 };
142
143 struct llvmpipe_memory_object
144 {
145 struct pipe_memory_object b;
146 struct pipe_reference reference;
147 struct llvmpipe_memory_allocation *mem_alloc;
148 uint64_t size;
149 };
150
151
152 /** cast wrappers */
153 static inline struct llvmpipe_resource *
llvmpipe_resource(struct pipe_resource * pt)154 llvmpipe_resource(struct pipe_resource *pt)
155 {
156 return (struct llvmpipe_resource *) pt;
157 }
158
159
160 static inline const struct llvmpipe_resource *
llvmpipe_resource_const(const struct pipe_resource * pt)161 llvmpipe_resource_const(const struct pipe_resource *pt)
162 {
163 return (const struct llvmpipe_resource *) pt;
164 }
165
166
167 static inline struct llvmpipe_transfer *
llvmpipe_transfer(struct pipe_transfer * pt)168 llvmpipe_transfer(struct pipe_transfer *pt)
169 {
170 return (struct llvmpipe_transfer *) pt;
171 }
172
173
174 static inline struct llvmpipe_memory_object *
llvmpipe_memory_object(struct pipe_memory_object * pt)175 llvmpipe_memory_object(struct pipe_memory_object *pt)
176 {
177 return (struct llvmpipe_memory_object *) pt;
178 }
179
180
181 void llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen);
182 void llvmpipe_init_context_resource_funcs(struct pipe_context *pipe);
183
184
185 static inline bool
llvmpipe_resource_is_texture(const struct pipe_resource * resource)186 llvmpipe_resource_is_texture(const struct pipe_resource *resource)
187 {
188 switch (resource->target) {
189 case PIPE_BUFFER:
190 return false;
191 case PIPE_TEXTURE_1D:
192 case PIPE_TEXTURE_1D_ARRAY:
193 case PIPE_TEXTURE_2D:
194 case PIPE_TEXTURE_2D_ARRAY:
195 case PIPE_TEXTURE_RECT:
196 case PIPE_TEXTURE_3D:
197 case PIPE_TEXTURE_CUBE:
198 case PIPE_TEXTURE_CUBE_ARRAY:
199 return true;
200 default:
201 assert(0);
202 return false;
203 }
204 }
205
206
207 static inline bool
llvmpipe_resource_is_1d(const struct pipe_resource * resource)208 llvmpipe_resource_is_1d(const struct pipe_resource *resource)
209 {
210 switch (resource->target) {
211 case PIPE_BUFFER:
212 case PIPE_TEXTURE_1D:
213 case PIPE_TEXTURE_1D_ARRAY:
214 return true;
215 case PIPE_TEXTURE_2D:
216 case PIPE_TEXTURE_2D_ARRAY:
217 case PIPE_TEXTURE_RECT:
218 case PIPE_TEXTURE_3D:
219 case PIPE_TEXTURE_CUBE:
220 case PIPE_TEXTURE_CUBE_ARRAY:
221 return false;
222 default:
223 assert(0);
224 return false;
225 }
226 }
227
228
229 static inline unsigned
llvmpipe_layer_stride(struct pipe_resource * resource,unsigned level)230 llvmpipe_layer_stride(struct pipe_resource *resource,
231 unsigned level)
232 {
233 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
234 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
235 return lpr->img_stride[level];
236 }
237
238
239 static inline unsigned
llvmpipe_resource_stride(struct pipe_resource * resource,unsigned level)240 llvmpipe_resource_stride(struct pipe_resource *resource,
241 unsigned level)
242 {
243 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
244 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
245 return lpr->row_stride[level];
246 }
247
248
249 static inline unsigned
llvmpipe_sample_stride(struct pipe_resource * resource)250 llvmpipe_sample_stride(struct pipe_resource *resource)
251 {
252 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
253 return lpr->sample_stride;
254 }
255
256
257 void *
258 llvmpipe_resource_map(struct pipe_resource *resource,
259 unsigned level,
260 unsigned layer,
261 enum lp_texture_usage tex_usage);
262
263 void
264 llvmpipe_resource_unmap(struct pipe_resource *resource,
265 unsigned level,
266 unsigned layer);
267
268
269 void *
270 llvmpipe_resource_data(struct pipe_resource *resource);
271
272
273 unsigned
274 llvmpipe_resource_size(const struct pipe_resource *resource);
275
276
277 uint8_t *
278 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
279 unsigned face_slice, unsigned level);
280
281
282 extern void
283 llvmpipe_print_resources(void);
284
285
286 #define LP_UNREFERENCED 0
287 #define LP_REFERENCED_FOR_READ (1 << 0)
288 #define LP_REFERENCED_FOR_WRITE (1 << 1)
289
290
291 unsigned int
292 llvmpipe_is_resource_referenced(struct pipe_context *pipe,
293 struct pipe_resource *presource,
294 unsigned level);
295
296 unsigned
297 llvmpipe_get_format_alignment(enum pipe_format format);
298
299
300 void *
301 llvmpipe_transfer_map_ms(struct pipe_context *pipe,
302 struct pipe_resource *resource,
303 unsigned level,
304 unsigned usage,
305 unsigned sample,
306 const struct pipe_box *box,
307 struct pipe_transfer **transfer);
308
309 uint32_t
310 llvmpipe_get_texel_offset(struct pipe_resource *resource,
311 uint32_t level, uint32_t x,
312 uint32_t y, uint32_t z);
313
314 #endif /* LP_TEXTURE_H */
315