1 /**************************************************************************
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
33 #include "pipe/p_context.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_inlines.h"
36 #include "pipe/p_util.h"
37 #include "pipe/p_winsys.h"
39 #include "sp_context.h"
41 #include "sp_texture.h"
44 /* At the moment, just make softpipe use the same layout for its
45 * textures as the i945. Softpipe needs some sort of texture layout,
46 * this one was handy. May be worthwhile to simplify this code a
50 static unsigned minify( unsigned d
)
55 static int align(int value
, int alignment
)
57 return (value
+ alignment
- 1) & ~(alignment
- 1);
62 sp_miptree_set_level_info(struct softpipe_texture
*spt
,
65 unsigned x
, unsigned y
, unsigned w
, unsigned h
,
68 struct pipe_texture
*pt
= &spt
->base
;
70 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
73 pt
->height
[level
] = h
;
76 spt
->nr_images
[level
] = nr_images
;
77 spt
->level_offset
[level
] = (x
+ y
* spt
->pitch
) * pt
->cpp
;
80 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
81 level, w, h, d, x, y, spt->level_offset[level]);
84 /* Not sure when this would happen, but anyway:
86 if (spt
->image_offset
[level
]) {
87 FREE( spt
->image_offset
[level
] );
88 spt
->image_offset
[level
] = NULL
;
92 assert(!spt
->image_offset
[level
]);
94 spt
->image_offset
[level
] = (unsigned *) MALLOC( nr_images
* sizeof(unsigned) );
95 spt
->image_offset
[level
][0] = 0;
100 sp_miptree_set_image_offset(struct softpipe_texture
*spt
,
101 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
103 if (img
== 0 && level
== 0)
104 assert(x
== 0 && y
== 0);
106 assert(img
< spt
->nr_images
[level
]);
108 spt
->image_offset
[level
][img
] = (x
+ y
* spt
->pitch
);
111 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
112 __FUNCTION__, level, img, x, y, spt->image_offset[level][img]);
118 sp_miptree_layout_2d( struct softpipe_texture
*spt
)
120 struct pipe_texture
*pt
= &spt
->base
;
121 int align_h
= 2, align_w
= 4;
125 unsigned width
= pt
->width
[0];
126 unsigned height
= pt
->height
[0];
128 spt
->pitch
= pt
->width
[0];
130 * we use alignment=64 bytes in sp_region_alloc(). If we change
131 * that, change this too.
136 /* May need to adjust pitch to accomodate the placement of
137 * the 2nd mipmap. This occurs when the alignment
138 * constraints of mipmap placement push the right edge of the
139 * 2nd mipmap out past the width of its parent.
141 if (pt
->first_level
!= pt
->last_level
) {
142 unsigned mip1_width
= align(minify(pt
->width
[0]), align_w
)
143 + minify(minify(pt
->width
[0]));
145 if (mip1_width
> pt
->width
[0])
146 spt
->pitch
= mip1_width
;
149 /* Pitch must be a whole number of dwords, even though we
150 * express it in texels.
152 spt
->pitch
= align(spt
->pitch
* pt
->cpp
, 4) / pt
->cpp
;
153 spt
->total_height
= 0;
155 for ( level
= pt
->first_level
; level
<= pt
->last_level
; level
++ ) {
158 sp_miptree_set_level_info(spt
, level
, 1, x
, y
, width
, height
, 1);
161 img_height
= MAX2(1, height
/4);
163 img_height
= align(height
, align_h
);
166 /* Because the images are packed better, the final offset
167 * might not be the maximal one:
169 spt
->total_height
= MAX2(spt
->total_height
, y
+ img_height
);
171 /* Layout_below: step right after second mipmap.
173 if (level
== pt
->first_level
+ 1) {
174 x
+= align(width
, align_w
);
180 width
= minify(width
);
181 height
= minify(height
);
186 static const int initial_offsets
[6][2] = {
195 static const int step_offsets
[6][2] = {
207 softpipe_mipmap_tree_layout(struct pipe_context
*pipe
, struct softpipe_texture
* spt
)
209 struct pipe_texture
*pt
= &spt
->base
;
212 switch (pt
->target
) {
213 case PIPE_TEXTURE_CUBE
:{
214 const unsigned dim
= pt
->width
[0];
216 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
218 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
220 /* Depending on the size of the largest images, pitch can be
221 * determined either by the old-style packing of cubemap faces,
222 * or the final row of 4x4, 2x2 and 1x1 faces below this.
225 spt
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
229 spt
->total_height
= dim
* 4 + 4;
231 /* Set all the levels to effectively occupy the whole rectangular region.
233 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
234 sp_miptree_set_level_info(spt
, level
, 6,
236 lvlWidth
, lvlHeight
, 1);
242 for (face
= 0; face
< 6; face
++) {
243 unsigned x
= initial_offsets
[face
][0] * dim
;
244 unsigned y
= initial_offsets
[face
][1] * dim
;
247 if (dim
== 4 && face
>= 4) {
248 y
= spt
->total_height
- 4;
251 else if (dim
< 4 && (face
> 0 || pt
->first_level
> 0)) {
252 y
= spt
->total_height
- 4;
256 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
257 sp_miptree_set_image_offset(spt
, level
, face
, x
, y
);
264 case PIPE_TEX_FACE_POS_X
:
265 case PIPE_TEX_FACE_NEG_X
:
266 x
+= step_offsets
[face
][0] * d
;
267 y
+= step_offsets
[face
][1] * d
;
269 case PIPE_TEX_FACE_POS_Y
:
270 case PIPE_TEX_FACE_NEG_Y
:
274 case PIPE_TEX_FACE_POS_Z
:
275 case PIPE_TEX_FACE_NEG_Z
:
276 y
= spt
->total_height
- 4;
282 y
= spt
->total_height
- 4;
291 x
+= step_offsets
[face
][0] * d
;
292 y
+= step_offsets
[face
][1] * d
;
299 case PIPE_TEXTURE_3D
:{
300 unsigned width
= pt
->width
[0];
301 unsigned height
= pt
->height
[0];
302 unsigned depth
= pt
->depth
[0];
303 unsigned pack_x_pitch
, pack_x_nr
;
304 unsigned pack_y_pitch
;
307 spt
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
308 spt
->total_height
= 0;
310 pack_y_pitch
= MAX2(pt
->height
[0], 2);
311 pack_x_pitch
= spt
->pitch
;
314 for (level
= pt
->first_level
; level
<= pt
->last_level
; level
++) {
315 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
320 sp_miptree_set_level_info(spt
, level
, nr_images
,
321 0, spt
->total_height
,
322 width
, height
, depth
);
324 for (q
= 0; q
< nr_images
;) {
325 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
326 sp_miptree_set_image_offset(spt
, level
, q
, x
, y
);
335 spt
->total_height
+= y
;
337 if (pack_x_pitch
> 4) {
340 assert(pack_x_pitch
* pack_x_nr
<= spt
->pitch
);
343 if (pack_y_pitch
> 2) {
347 width
= minify(width
);
348 height
= minify(height
);
349 depth
= minify(depth
);
354 case PIPE_TEXTURE_1D
:
355 case PIPE_TEXTURE_2D
:
356 // case PIPE_TEXTURE_RECTANGLE:
357 sp_miptree_layout_2d(spt
);
365 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
367 spt->total_height, pt->cpp, spt->pitch * spt->total_height * pt->cpp);
374 softpipe_texture_create(struct pipe_context
*pipe
, struct pipe_texture
**pt
)
376 struct softpipe_texture
*spt
= REALLOC(*pt
, sizeof(struct pipe_texture
),
377 sizeof(struct softpipe_texture
));
380 memset(&spt
->base
+ 1, 0,
381 sizeof(struct softpipe_texture
) - sizeof(struct pipe_texture
));
383 if (softpipe_mipmap_tree_layout(pipe
, spt
)) {
384 spt
->buffer
= pipe
->winsys
->buffer_create(pipe
->winsys
,
385 PIPE_SURFACE_FLAG_TEXTURE
);
388 pipe
->winsys
->buffer_data(pipe
->winsys
, spt
->buffer
,
389 spt
->pitch
* (*pt
)->cpp
*
390 spt
->total_height
, NULL
,
391 PIPE_BUFFER_USAGE_PIXEL
);
405 softpipe_texture_release(struct pipe_context
*pipe
, struct pipe_texture
**pt
)
411 DBG("%s %p refcount will be %d\n",
412 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
414 if (--(*pt
)->refcount
<= 0) {
415 struct softpipe_texture
*spt
= (struct softpipe_texture
*)*pt
;
419 DBG("%s deleting %p\n", __FUNCTION__, (void *) spt);
422 pipe
->winsys
->buffer_reference(pipe
->winsys
, &spt
->buffer
, NULL
);
424 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
425 if (spt
->image_offset
[i
])
426 free(spt
->image_offset
[i
]);