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 "pipe/p_state.h"
37 #include "pipe/p_context.h"
38 #include "pipe/p_defines.h"
39 #include "pipe/p_inlines.h"
40 #include "pipe/p_winsys.h"
41 #include "util/u_math.h"
42 #include "util/u_memory.h"
43 #include "brw_context.h"
44 #include "brw_tex_layout.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 void intel_miptree_set_image_offset(struct brw_texture
*tex
,
77 unsigned x
, unsigned y
)
79 struct pipe_texture
*pt
= &tex
->base
;
80 if (img
== 0 && level
== 0)
81 assert(x
== 0 && y
== 0);
82 assert(img
< tex
->nr_images
[level
]);
84 tex
->image_offset
[level
][img
] = y
* tex
->stride
+ x
* pt
->block
.size
;
87 static void intel_miptree_set_level_info(struct brw_texture
*tex
,
90 unsigned x
, unsigned y
,
91 unsigned w
, unsigned h
, unsigned d
)
93 struct pipe_texture
*pt
= &tex
->base
;
95 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
98 pt
->height
[level
] = h
;
101 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w
);
102 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h
);
104 tex
->level_offset
[level
] = y
* tex
->stride
+ x
* tex
->base
.block
.size
;
105 tex
->nr_images
[level
] = nr_images
;
108 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
109 level, w, h, d, x, y, tex->level_offset[level]);
112 /* Not sure when this would happen, but anyway:
114 if (tex
->image_offset
[level
]) {
115 FREE(tex
->image_offset
[level
]);
116 tex
->image_offset
[level
] = NULL
;
120 assert(!tex
->image_offset
[level
]);
122 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
123 tex
->image_offset
[level
][0] = 0;
126 static void i945_miptree_layout_2d(struct brw_texture
*tex
)
128 struct pipe_texture
*pt
= &tex
->base
;
129 const int align_x
= 2, align_y
= 4;
133 unsigned width
= pt
->width
[0];
134 unsigned height
= pt
->height
[0];
135 unsigned nblocksx
= pt
->nblocksx
[0];
136 unsigned nblocksy
= pt
->nblocksy
[0];
138 tex
->stride
= align(pt
->nblocksx
[0] * pt
->block
.size
, 4);
140 /* May need to adjust pitch to accomodate the placement of
141 * the 2nd mipmap level. This occurs when the alignment
142 * constraints of mipmap placement push the right edge of the
143 * 2nd mipmap level out past the width of its parent.
145 if (pt
->last_level
> 0) {
146 unsigned mip1_nblocksx
147 = align(pf_get_nblocksx(&pt
->block
, minify(width
)), align_x
)
148 + pf_get_nblocksx(&pt
->block
, minify(minify(width
)));
150 if (mip1_nblocksx
> nblocksx
)
151 tex
->stride
= mip1_nblocksx
* pt
->block
.size
;
154 /* Pitch must be a whole number of dwords
156 tex
->stride
= align(tex
->stride
, 64);
157 tex
->total_nblocksy
= 0;
159 for (level
= 0; level
<= pt
->last_level
; level
++) {
160 intel_miptree_set_level_info(tex
, level
, 1, x
, y
, width
,
163 nblocksy
= align(nblocksy
, align_y
);
165 /* Because the images are packed better, the final offset
166 * might not be the maximal one:
168 tex
->total_nblocksy
= MAX2(tex
->total_nblocksy
, y
+ nblocksy
);
170 /* Layout_below: step right after second mipmap level.
173 x
+= align(nblocksx
, align_x
);
179 width
= minify(width
);
180 height
= minify(height
);
181 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
182 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
186 static boolean
brw_miptree_layout(struct brw_texture
*tex
)
188 struct pipe_texture
*pt
= &tex
->base
;
189 /* XXX: these vary depending on image format:
191 /* int align_w = 4; */
193 switch (pt
->target
) {
194 case PIPE_TEXTURE_CUBE
:
195 case PIPE_TEXTURE_3D
: {
196 unsigned width
= pt
->width
[0];
197 unsigned height
= pt
->height
[0];
198 unsigned depth
= pt
->depth
[0];
199 unsigned nblocksx
= pt
->nblocksx
[0];
200 unsigned nblocksy
= pt
->nblocksy
[0];
201 unsigned pack_x_pitch
, pack_x_nr
;
202 unsigned pack_y_pitch
;
204 unsigned align_h
= 2;
205 unsigned align_w
= 4;
207 tex
->total_nblocksy
= 0;
209 tex
->stride
= align(pt
->nblocksx
[0], 4);
210 pack_y_pitch
= align(pt
->nblocksy
[0], align_h
);
212 pack_x_pitch
= tex
->stride
/ pt
->block
.size
;
215 for (level
= 0; level
<= pt
->last_level
; level
++) {
216 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
221 intel_miptree_set_level_info(tex
, level
, nr_images
,
222 0, tex
->total_nblocksy
,
223 width
, height
, depth
);
225 for (q
= 0; q
< nr_images
;) {
226 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
227 intel_miptree_set_image_offset(tex
, level
, q
, x
, y
);
236 tex
->total_nblocksy
+= y
;
237 width
= minify(width
);
238 height
= minify(height
);
239 depth
= minify(depth
);
240 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
241 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
243 if (pt
->compressed
) {
244 pack_y_pitch
= (height
+ 3) / 4;
246 if (pack_x_pitch
> align(width
, align_w
)) {
247 pack_x_pitch
= align(width
, align_w
);
251 if (pack_x_pitch
> 4) {
254 assert(pack_x_pitch
* pack_x_nr
* pt
->block
.size
<= tex
->stride
);
257 if (pack_y_pitch
> 2) {
259 pack_y_pitch
= align(pack_y_pitch
, align_h
);
268 i945_miptree_layout_2d(tex
);
272 PRINT("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__
,
276 pt
->stride
* pt
->total_nblocksy
);
283 static struct pipe_texture
*
284 brw_texture_create_screen(struct pipe_screen
*screen
,
285 const struct pipe_texture
*templat
)
287 struct pipe_winsys
*ws
= screen
->winsys
;
288 struct brw_texture
*tex
= CALLOC_STRUCT(brw_texture
);
291 tex
->base
= *templat
;
292 tex
->base
.refcount
= 1;
294 tex
->base
.nblocksx
[0] = pf_get_nblocksx(&tex
->base
.block
, tex
->base
.width
[0]);
295 tex
->base
.nblocksy
[0] = pf_get_nblocksy(&tex
->base
.block
, tex
->base
.height
[0]);
297 if (brw_miptree_layout(tex
))
298 tex
->buffer
= ws
->buffer_create(ws
, 64,
299 PIPE_BUFFER_USAGE_PIXEL
,
301 tex
->total_nblocksy
);
314 brw_texture_release_screen(struct pipe_screen
*screen
,
315 struct pipe_texture
**pt
)
321 DBG("%s %p refcount will be %d\n",
322 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
324 if (--(*pt
)->refcount
<= 0) {
325 struct pipe_winsys
*ws
= screen
->winsys
;
326 struct brw_texture
*tex
= (struct brw_texture
*)*pt
;
330 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
333 winsys_buffer_reference(ws
, &tex
->buffer
, NULL
);
335 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
336 if (tex
->image_offset
[i
])
337 free(tex
->image_offset
[i
]);
345 static struct pipe_surface
*
346 brw_get_tex_surface_screen(struct pipe_screen
*screen
,
347 struct pipe_texture
*pt
,
348 unsigned face
, unsigned level
, unsigned zslice
)
350 struct pipe_winsys
*ws
= screen
->winsys
;
351 struct brw_texture
*tex
= (struct brw_texture
*)pt
;
352 struct pipe_surface
*ps
;
353 unsigned offset
; /* in bytes */
355 offset
= tex
->level_offset
[level
];
357 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
358 offset
+= tex
->image_offset
[level
][face
];
360 else if (pt
->target
== PIPE_TEXTURE_3D
) {
361 offset
+= tex
->image_offset
[level
][zslice
];
368 ps
= ws
->surface_alloc(ws
);
371 assert(ps
->refcount
);
372 winsys_buffer_reference(ws
, &ps
->buffer
, tex
->buffer
);
373 ps
->format
= pt
->format
;
374 ps
->width
= pt
->width
[level
];
375 ps
->height
= pt
->height
[level
];
376 ps
->block
= pt
->block
;
377 ps
->nblocksx
= pt
->nblocksx
[level
];
378 ps
->nblocksy
= pt
->nblocksy
[level
];
379 ps
->stride
= tex
->stride
;
387 brw_init_texture_functions(struct brw_context
*brw
)
389 // brw->pipe.texture_update = brw_texture_update;
394 brw_init_screen_texture_funcs(struct pipe_screen
*screen
)
396 screen
->texture_create
= brw_texture_create_screen
;
397 screen
->texture_release
= brw_texture_release_screen
;
398 screen
->get_tex_surface
= brw_get_tex_surface_screen
;