0731918933830784145a1ced4a4a9887f418002e
[mesa.git] / src / gallium / drivers / llvmpipe / lp_texture.h
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
49 struct sw_displaytarget;
50
51
52 /**
53 * llvmpipe subclass of pipe_resource. A texture, drawing surface,
54 * vertex buffer, const buffer, etc.
55 * Textures are stored differently than other types of objects such as
56 * vertex buffers and const buffers.
57 * The latter are simple malloc'd blocks of memory.
58 */
59 struct llvmpipe_resource
60 {
61 struct pipe_resource base;
62
63 /** Row stride in bytes */
64 unsigned row_stride[LP_MAX_TEXTURE_LEVELS];
65 /** Image stride (for cube maps, array or 3D textures) in bytes */
66 unsigned img_stride[LP_MAX_TEXTURE_LEVELS];
67 /** Number of 3D slices or cube faces per level */
68 unsigned num_slices_faces[LP_MAX_TEXTURE_LEVELS];
69 /** Offset to start of mipmap level, in bytes */
70 unsigned mip_offsets[LP_MAX_TEXTURE_LEVELS];
71
72 /**
73 * Display target, for textures with the PIPE_BIND_DISPLAY_TARGET
74 * usage.
75 */
76 struct sw_displaytarget *dt;
77
78 /**
79 * Malloc'ed data for regular textures, or a mapping to dt above.
80 */
81 void *tex_data;
82
83 /**
84 * Data for non-texture resources.
85 */
86 void *data;
87
88 boolean userBuffer; /** Is this a user-space buffer? */
89 unsigned timestamp;
90
91 unsigned id; /**< temporary, for debugging */
92
93 #ifdef DEBUG
94 /** for linked list */
95 struct llvmpipe_resource *prev, *next;
96 #endif
97 };
98
99
100 struct llvmpipe_transfer
101 {
102 struct pipe_transfer base;
103
104 unsigned long offset;
105 };
106
107
108 /** cast wrappers */
109 static INLINE struct llvmpipe_resource *
110 llvmpipe_resource(struct pipe_resource *pt)
111 {
112 return (struct llvmpipe_resource *) pt;
113 }
114
115
116 static INLINE const struct llvmpipe_resource *
117 llvmpipe_resource_const(const struct pipe_resource *pt)
118 {
119 return (const struct llvmpipe_resource *) pt;
120 }
121
122
123 static INLINE struct llvmpipe_transfer *
124 llvmpipe_transfer(struct pipe_transfer *pt)
125 {
126 return (struct llvmpipe_transfer *) pt;
127 }
128
129
130 void llvmpipe_init_screen_resource_funcs(struct pipe_screen *screen);
131 void llvmpipe_init_context_resource_funcs(struct pipe_context *pipe);
132
133
134 static INLINE boolean
135 llvmpipe_resource_is_texture(const struct pipe_resource *resource)
136 {
137 switch (resource->target) {
138 case PIPE_BUFFER:
139 return FALSE;
140 case PIPE_TEXTURE_1D:
141 case PIPE_TEXTURE_1D_ARRAY:
142 case PIPE_TEXTURE_2D:
143 case PIPE_TEXTURE_2D_ARRAY:
144 case PIPE_TEXTURE_RECT:
145 case PIPE_TEXTURE_3D:
146 case PIPE_TEXTURE_CUBE:
147 return TRUE;
148 default:
149 assert(0);
150 return FALSE;
151 }
152 }
153
154
155 static INLINE boolean
156 llvmpipe_resource_is_1d(const struct pipe_resource *resource)
157 {
158 switch (resource->target) {
159 case PIPE_BUFFER:
160 case PIPE_TEXTURE_1D:
161 case PIPE_TEXTURE_1D_ARRAY:
162 return TRUE;
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 return FALSE;
169 default:
170 assert(0);
171 return FALSE;
172 }
173 }
174
175
176 static INLINE unsigned
177 llvmpipe_layer_stride(struct pipe_resource *resource,
178 unsigned level)
179 {
180 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
181 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
182 return lpr->img_stride[level];
183 }
184
185
186 static INLINE unsigned
187 llvmpipe_resource_stride(struct pipe_resource *resource,
188 unsigned level)
189 {
190 struct llvmpipe_resource *lpr = llvmpipe_resource(resource);
191 assert(level < LP_MAX_TEXTURE_2D_LEVELS);
192 return lpr->row_stride[level];
193 }
194
195
196 void *
197 llvmpipe_resource_map(struct pipe_resource *resource,
198 unsigned level,
199 unsigned layer,
200 enum lp_texture_usage tex_usage);
201
202 void
203 llvmpipe_resource_unmap(struct pipe_resource *resource,
204 unsigned level,
205 unsigned layer);
206
207
208 void *
209 llvmpipe_resource_data(struct pipe_resource *resource);
210
211
212 unsigned
213 llvmpipe_resource_size(const struct pipe_resource *resource);
214
215
216 ubyte *
217 llvmpipe_get_texture_image_address(struct llvmpipe_resource *lpr,
218 unsigned face_slice, unsigned level);
219
220 void *
221 llvmpipe_get_texture_image(struct llvmpipe_resource *resource,
222 unsigned face_slice, unsigned level,
223 enum lp_texture_usage usage);
224
225 void *
226 llvmpipe_get_texture_image_all(struct llvmpipe_resource *lpr,
227 unsigned level,
228 enum lp_texture_usage usage);
229
230 ubyte *
231 llvmpipe_get_texture_tile_linear(struct llvmpipe_resource *lpr,
232 unsigned face_slice, unsigned level,
233 enum lp_texture_usage usage,
234 unsigned x, unsigned y);
235
236
237 extern void
238 llvmpipe_print_resources(void);
239
240
241 #define LP_UNREFERENCED 0
242 #define LP_REFERENCED_FOR_READ (1 << 0)
243 #define LP_REFERENCED_FOR_WRITE (1 << 1)
244
245 unsigned int
246 llvmpipe_is_resource_referenced( struct pipe_context *pipe,
247 struct pipe_resource *presource,
248 unsigned level);
249
250 unsigned
251 llvmpipe_get_format_alignment(enum pipe_format format);
252
253 #endif /* LP_TEXTURE_H */