Merge commit 'origin/gallium-0.1' into gallium-0.1
[mesa.git] / src / gallium / drivers / i965simple / brw_tex_layout.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 /* Code to layout images in a mipmap tree for i965.
34 */
35
36 #include "brw_tex_layout.h"
37
38 #include "pipe/p_state.h"
39 #include "pipe/p_context.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_util.h"
42 #include "pipe/p_inlines.h"
43 #include "pipe/p_winsys.h"
44
45 #include "brw_context.h"
46
47 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
48
49 #if 0
50 unsigned intel_compressed_alignment(unsigned internalFormat)
51 {
52 unsigned alignment = 4;
53
54 switch (internalFormat) {
55 case GL_COMPRESSED_RGB_FXT1_3DFX:
56 case GL_COMPRESSED_RGBA_FXT1_3DFX:
57 alignment = 8;
58 break;
59
60 default:
61 break;
62 }
63
64 return alignment;
65 }
66 #endif
67
68 static unsigned minify( unsigned d )
69 {
70 return MAX2(1, d>>1);
71 }
72
73
74 static boolean brw_miptree_layout(struct pipe_context *, struct brw_texture *);
75
76 static void intel_miptree_set_image_offset(struct brw_texture *tex,
77 unsigned level,
78 unsigned img,
79 unsigned x, unsigned y)
80 {
81 struct pipe_texture *pt = &tex->base;
82 if (img == 0 && level == 0)
83 assert(x == 0 && y == 0);
84 assert(img < tex->nr_images[level]);
85
86 tex->image_offset[level][img] = (x + y * tex->pitch) * pt->cpp;
87 }
88
89 static void intel_miptree_set_level_info(struct brw_texture *tex,
90 unsigned level,
91 unsigned nr_images,
92 unsigned x, unsigned y,
93 unsigned w, unsigned h, unsigned d)
94 {
95 struct pipe_texture *pt = &tex->base;
96
97 assert(level < PIPE_MAX_TEXTURE_LEVELS);
98
99 pt->width[level] = w;
100 pt->height[level] = h;
101 pt->depth[level] = d;
102
103 tex->level_offset[level] = (x + y * tex->pitch) * pt->cpp;
104 tex->nr_images[level] = nr_images;
105
106 /*
107 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
108 level, w, h, d, x, y, tex->level_offset[level]);
109 */
110
111 /* Not sure when this would happen, but anyway:
112 */
113 if (tex->image_offset[level]) {
114 FREE(tex->image_offset[level]);
115 tex->image_offset[level] = NULL;
116 }
117
118 assert(nr_images);
119 assert(!tex->image_offset[level]);
120
121 tex->image_offset[level] = (unsigned *) MALLOC(nr_images * sizeof(unsigned));
122 tex->image_offset[level][0] = 0;
123 }
124
125 static void i945_miptree_layout_2d(struct brw_texture *tex)
126 {
127 struct pipe_texture *pt = &tex->base;
128 unsigned align_h = 2, align_w = 4;
129 unsigned level;
130 unsigned x = 0;
131 unsigned y = 0;
132 unsigned width = pt->width[0];
133 unsigned height = pt->height[0];
134
135 tex->pitch = pt->width[0];
136
137 #if 0
138 if (pt->compressed) {
139 align_w = intel_compressed_alignment(pt->internal_format);
140 tex->pitch = ALIGN(pt->width[0], align_w);
141 }
142 #endif
143
144 /* May need to adjust pitch to accomodate the placement of
145 * the 2nd mipmap. This occurs when the alignment
146 * constraints of mipmap placement push the right edge of the
147 * 2nd mipmap out past the width of its parent.
148 */
149 if (pt->last_level > 0) {
150 unsigned mip1_width;
151
152 if (pt->compressed) {
153 mip1_width = align(minify(pt->width[0]), align_w)
154 + align(minify(minify(pt->width[0])), align_w);
155 } else {
156 mip1_width = align(minify(pt->width[0]), align_w)
157 + minify(minify(pt->width[0]));
158 }
159
160 if (mip1_width > tex->pitch) {
161 tex->pitch = mip1_width;
162 }
163 }
164
165 /* Pitch must be a whole number of dwords, even though we
166 * express it in texels.
167 */
168 tex->pitch = align(tex->pitch * pt->cpp, 4) / pt->cpp;
169 tex->total_height = 0;
170
171 for (level = 0; level <= pt->last_level; level++) {
172 unsigned img_height;
173
174 intel_miptree_set_level_info(tex, level, 1, x, y, width,
175 height, 1);
176
177 if (pt->compressed)
178 img_height = MAX2(1, height/4);
179 else
180 img_height = align(height, align_h);
181
182
183 /* Because the images are packed better, the final offset
184 * might not be the maximal one:
185 */
186 tex->total_height = MAX2(tex->total_height, y + img_height);
187
188 /* Layout_below: step right after second mipmap.
189 */
190 if (level == 1) {
191 x += align(width, align_w);
192 }
193 else {
194 y += img_height;
195 }
196
197 width = minify(width);
198 height = minify(height);
199 }
200 }
201
202 static boolean brw_miptree_layout(struct pipe_context *pipe, struct brw_texture *tex)
203 {
204 struct pipe_texture *pt = &tex->base;
205 /* XXX: these vary depending on image format:
206 */
207 /* int align_w = 4; */
208
209 switch (pt->target) {
210 case PIPE_TEXTURE_CUBE:
211 case PIPE_TEXTURE_3D: {
212 unsigned width = pt->width[0];
213 unsigned height = pt->height[0];
214 unsigned depth = pt->depth[0];
215 unsigned pack_x_pitch, pack_x_nr;
216 unsigned pack_y_pitch;
217 unsigned level;
218 unsigned align_h = 2;
219 unsigned align_w = 4;
220
221 tex->total_height = 0;
222 #if 0
223 if (pt->compressed) {
224 align_w = intel_compressed_alignment(pt->internal_format);
225 pt->pitch = align(width, align_w);
226 pack_y_pitch = (height + 3) / 4;
227 } else
228 #endif
229 {
230 tex->pitch = align(pt->width[0] * pt->cpp, 4) / pt->cpp;
231 pack_y_pitch = align(pt->height[0], align_h);
232 }
233
234 pack_x_pitch = tex->pitch;
235 pack_x_nr = 1;
236
237 for (level = 0; level <= pt->last_level; level++) {
238 unsigned nr_images = pt->target == PIPE_TEXTURE_3D ? depth : 6;
239 int x = 0;
240 int y = 0;
241 uint q, j;
242
243 intel_miptree_set_level_info(tex, level, nr_images,
244 0, tex->total_height,
245 width, height, depth);
246
247 for (q = 0; q < nr_images;) {
248 for (j = 0; j < pack_x_nr && q < nr_images; j++, q++) {
249 intel_miptree_set_image_offset(tex, level, q, x, y);
250 x += pack_x_pitch;
251 }
252
253 x = 0;
254 y += pack_y_pitch;
255 }
256
257
258 tex->total_height += y;
259 width = minify(width);
260 height = minify(height);
261 depth = minify(depth);
262
263 if (pt->compressed) {
264 pack_y_pitch = (height + 3) / 4;
265
266 if (pack_x_pitch > align(width, align_w)) {
267 pack_x_pitch = align(width, align_w);
268 pack_x_nr <<= 1;
269 }
270 } else {
271 if (pack_x_pitch > 4) {
272 pack_x_pitch >>= 1;
273 pack_x_nr <<= 1;
274 assert(pack_x_pitch * pack_x_nr <= tex->pitch);
275 }
276
277 if (pack_y_pitch > 2) {
278 pack_y_pitch >>= 1;
279 pack_y_pitch = align(pack_y_pitch, align_h);
280 }
281 }
282
283 }
284 break;
285 }
286
287 default:
288 i945_miptree_layout_2d(tex);
289 break;
290 }
291 #if 0
292 PRINT("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
293 pt->pitch,
294 pt->total_height,
295 pt->cpp,
296 pt->pitch * pt->total_height * pt->cpp );
297 #endif
298
299 return TRUE;
300 }
301
302
303 struct pipe_texture *
304 brw_texture_create(struct pipe_context *pipe, const struct pipe_texture *templat)
305 {
306 struct brw_texture *tex = CALLOC_STRUCT(brw_texture);
307
308 if (tex) {
309 tex->base = *templat;
310
311 if (brw_miptree_layout(pipe, tex))
312 tex->buffer = pipe->winsys->buffer_create(pipe->winsys, 64,
313 PIPE_BUFFER_USAGE_PIXEL,
314 tex->pitch * tex->base.cpp *
315 tex->total_height);
316
317 if (!tex->buffer) {
318 FREE(tex);
319 return NULL;
320 }
321 }
322
323 return &tex->base;
324 }
325
326 void
327 brw_texture_release(struct pipe_context *pipe, struct pipe_texture **pt)
328 {
329 if (!*pt)
330 return;
331
332 /*
333 DBG("%s %p refcount will be %d\n",
334 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
335 */
336 if (--(*pt)->refcount <= 0) {
337 struct brw_texture *tex = (struct brw_texture *)*pt;
338 uint i;
339
340 /*
341 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
342 */
343
344 pipe_buffer_reference(pipe->winsys, &tex->buffer, NULL);
345
346 for (i = 0; i < PIPE_MAX_TEXTURE_LEVELS; i++)
347 if (tex->image_offset[i])
348 free(tex->image_offset[i]);
349
350 free(tex);
351 }
352 *pt = NULL;
353 }