2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
33 /* Code to layout images in a mipmap tree for i965.
36 #include "brw_tex_layout.h"
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"
45 #include "brw_context.h"
47 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
50 unsigned intel_compressed_alignment(unsigned internalFormat
)
52 unsigned alignment
= 4;
54 switch (internalFormat
) {
55 case GL_COMPRESSED_RGB_FXT1_3DFX
:
56 case GL_COMPRESSED_RGBA_FXT1_3DFX
:
68 static unsigned minify( unsigned d
)
74 static boolean
brw_miptree_layout(struct pipe_context
*, struct brw_texture
*);
76 static void intel_miptree_set_image_offset(struct brw_texture
*tex
,
79 unsigned x
, unsigned y
)
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
]);
86 tex
->image_offset
[level
][img
] = (x
+ y
* tex
->pitch
) * pt
->cpp
;
89 static void intel_miptree_set_level_info(struct brw_texture
*tex
,
92 unsigned x
, unsigned y
,
93 unsigned w
, unsigned h
, unsigned d
)
95 struct pipe_texture
*pt
= &tex
->base
;
97 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
100 pt
->height
[level
] = h
;
101 pt
->depth
[level
] = d
;
103 tex
->level_offset
[level
] = (x
+ y
* tex
->pitch
) * pt
->cpp
;
104 tex
->nr_images
[level
] = nr_images
;
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]);
111 /* Not sure when this would happen, but anyway:
113 if (tex
->image_offset
[level
]) {
114 FREE(tex
->image_offset
[level
]);
115 tex
->image_offset
[level
] = NULL
;
119 assert(!tex
->image_offset
[level
]);
121 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
122 tex
->image_offset
[level
][0] = 0;
125 static void i945_miptree_layout_2d(struct brw_texture
*tex
)
127 struct pipe_texture
*pt
= &tex
->base
;
128 unsigned align_h
= 2, align_w
= 4;
132 unsigned width
= pt
->width
[0];
133 unsigned height
= pt
->height
[0];
135 tex
->pitch
= pt
->width
[0];
138 if (pt
->compressed
) {
139 align_w
= intel_compressed_alignment(pt
->internal_format
);
140 tex
->pitch
= ALIGN(pt
->width
[0], align_w
);
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.
149 if (pt
->last_level
> 0) {
152 if (pt
->compressed
) {
153 mip1_width
= align(minify(pt
->width
[0]), align_w
)
154 + align(minify(minify(pt
->width
[0])), align_w
);
156 mip1_width
= align(minify(pt
->width
[0]), align_w
)
157 + minify(minify(pt
->width
[0]));
160 if (mip1_width
> tex
->pitch
) {
161 tex
->pitch
= mip1_width
;
165 /* Pitch must be a whole number of dwords, even though we
166 * express it in texels.
168 tex
->pitch
= align(tex
->pitch
* pt
->cpp
, 4) / pt
->cpp
;
169 tex
->total_height
= 0;
171 for (level
= 0; level
<= pt
->last_level
; level
++) {
174 intel_miptree_set_level_info(tex
, level
, 1, x
, y
, width
,
178 img_height
= MAX2(1, height
/4);
180 img_height
= align(height
, align_h
);
183 /* Because the images are packed better, the final offset
184 * might not be the maximal one:
186 tex
->total_height
= MAX2(tex
->total_height
, y
+ img_height
);
188 /* Layout_below: step right after second mipmap.
191 x
+= align(width
, align_w
);
197 width
= minify(width
);
198 height
= minify(height
);
202 static boolean
brw_miptree_layout(struct pipe_context
*pipe
, struct brw_texture
*tex
)
204 struct pipe_texture
*pt
= &tex
->base
;
205 /* XXX: these vary depending on image format:
207 /* int align_w = 4; */
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
;
218 unsigned align_h
= 2;
219 unsigned align_w
= 4;
221 tex
->total_height
= 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;
230 tex
->pitch
= align(pt
->width
[0] * pt
->cpp
, 4) / pt
->cpp
;
231 pack_y_pitch
= align(pt
->height
[0], align_h
);
234 pack_x_pitch
= tex
->pitch
;
237 for (level
= 0; level
<= pt
->last_level
; level
++) {
238 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
243 intel_miptree_set_level_info(tex
, level
, nr_images
,
244 0, tex
->total_height
,
245 width
, height
, depth
);
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
);
258 tex
->total_height
+= y
;
259 width
= minify(width
);
260 height
= minify(height
);
261 depth
= minify(depth
);
263 if (pt
->compressed
) {
264 pack_y_pitch
= (height
+ 3) / 4;
266 if (pack_x_pitch
> align(width
, align_w
)) {
267 pack_x_pitch
= align(width
, align_w
);
271 if (pack_x_pitch
> 4) {
274 assert(pack_x_pitch
* pack_x_nr
<= tex
->pitch
);
277 if (pack_y_pitch
> 2) {
279 pack_y_pitch
= align(pack_y_pitch
, align_h
);
288 i945_miptree_layout_2d(tex
);
292 PRINT("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__
,
296 pt
->pitch
* pt
->total_height
* pt
->cpp
);
303 struct pipe_texture
*
304 brw_texture_create(struct pipe_context
*pipe
, const struct pipe_texture
*templat
)
306 struct brw_texture
*tex
= CALLOC_STRUCT(brw_texture
);
309 tex
->base
= *templat
;
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
*
327 brw_texture_release(struct pipe_context
*pipe
, struct pipe_texture
**pt
)
333 DBG("%s %p refcount will be %d\n",
334 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
336 if (--(*pt
)->refcount
<= 0) {
337 struct brw_texture
*tex
= (struct brw_texture
*)*pt
;
341 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
344 pipe_buffer_reference(pipe
->winsys
, &tex
->buffer
, NULL
);
346 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
347 if (tex
->image_offset
[i
])
348 free(tex
->image_offset
[i
]);