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