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 struct llvmpipe_memory_object
118 {
119 struct pipe_memory_object b;
120 struct pipe_memory_allocation *data;
121 uint64_t size;
122 };
123
124
125 /** cast wrappers */
126 static inline struct llvmpipe_resource *
llvmpipe_resource(struct pipe_resource * pt)127 llvmpipe_resource(struct pipe_resource *pt)
128 {
129 return (struct llvmpipe_resource *) pt;
130 }
131
132
133 static inline const struct llvmpipe_resource *
llvmpipe_resource_const(const struct pipe_resource * pt)134 llvmpipe_resource_const(const struct pipe_resource *pt)
135 {
136 return (const struct llvmpipe_resource *) pt;
137 }
138
139
140 static inline struct llvmpipe_transfer *
llvmpipe_transfer(struct pipe_transfer * pt)141 llvmpipe_transfer(struct pipe_transfer *pt)
142 {
143 return (struct llvmpipe_transfer *) pt;
144 }
145
146 static inline struct llvmpipe_memory_object *
llvmpipe_memory_object(struct pipe_memory_object * pt)147 llvmpipe_memory_object(struct pipe_memory_object *pt)
148 {
149 return (struct llvmpipe_memory_object *) pt;
150 }
151
152
153 void llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen);
154 void llvmpipe_init_context_resource_funcs(struct pipe_context *pipe);
155
156
157 static inline boolean
llvmpipe_resource_is_texture(const struct pipe_resource * resource)158 llvmpipe_resource_is_texture(const struct pipe_resource *resource)
159 {
160 switch (resource->target) {
161 case PIPE_BUFFER:
162 return FALSE;
163 case PIPE_TEXTURE_1D:
164 case PIPE_TEXTURE_1D_ARRAY:
165 case PIPE_TEXTURE_2D:
166 case PIPE_TEXTURE_2D_ARRAY:
167 case PIPE_TEXTURE_RECT:
168 case PIPE_TEXTURE_3D:
169 case PIPE_TEXTURE_CUBE:
170 case PIPE_TEXTURE_CUBE_ARRAY:
171 return TRUE;
172 default:
173 assert(0);
174 return FALSE;
175 }
176 }
177
178
179 static inline boolean
llvmpipe_resource_is_1d(const struct pipe_resource * resource)180 llvmpipe_resource_is_1d(const struct pipe_resource *resource)
181 {
182 switch (resource->target) {
183 case PIPE_BUFFER:
184 case PIPE_TEXTURE_1D:
185 case PIPE_TEXTURE_1D_ARRAY:
186 return TRUE;
187 case PIPE_TEXTURE_2D:
188 case PIPE_TEXTURE_2D_ARRAY:
189 case PIPE_TEXTURE_RECT:
190 case PIPE_TEXTURE_3D:
191 case PIPE_TEXTURE_CUBE:
192 case PIPE_TEXTURE_CUBE_ARRAY:
193 return FALSE;
194 default:
195 assert(0);
196 return FALSE;
197 }
198 }
199
200
201 static inline unsigned
llvmpipe_layer_stride(struct pipe_resource * resource,unsigned level)202 llvmpipe_layer_stride(struct pipe_resource *resource,
203 unsigned level)
204 {
205 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
206 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
207 return lpr->img_stride[level];
208 }
209
210
211 static inline unsigned
llvmpipe_resource_stride(struct pipe_resource * resource,unsigned level)212 llvmpipe_resource_stride(struct pipe_resource *resource,
213 unsigned level)
214 {
215 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
216 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
217 return lpr->row_stride[level];
218 }
219
220 static inline unsigned
llvmpipe_sample_stride(struct pipe_resource * resource)221 llvmpipe_sample_stride(struct pipe_resource *resource)
222 {
223 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
224 return lpr->sample_stride;
225 }
226
227 void *
228 llvmpipe_resource_map(struct pipe_resource *resource,
229 unsigned level,
230 unsigned layer,
231 enum lp_texture_usage tex_usage);
232
233 void
234 llvmpipe_resource_unmap(struct pipe_resource *resource,
235 unsigned level,
236 unsigned layer);
237
238
239 void *
240 llvmpipe_resource_data(struct pipe_resource *resource);
241
242
243 unsigned
244 llvmpipe_resource_size(const struct pipe_resource *resource);
245
246
247 ubyte *
248 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
249 unsigned face_slice, unsigned level);
250
251
252 extern void
253 llvmpipe_print_resources(void);
254
255
256 #define LP_UNREFERENCED 0
257 #define LP_REFERENCED_FOR_READ (1 << 0)
258 #define LP_REFERENCED_FOR_WRITE (1 << 1)
259
260 unsigned int
261 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
262 struct pipe_resource *presource,
263 unsigned level);
264
265 unsigned
266 llvmpipe_get_format_alignment(enum pipe_format format);
267
268 void *
269 llvmpipe_transfer_map_ms( struct pipe_context *pipe,
270 struct pipe_resource *resource,
271 unsigned level,
272 unsigned usage,
273 unsigned sample,
274 const struct pipe_box *box,
275 struct pipe_transfer **transfer );
276 #endif /* LP_TEXTURE_H */
277