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