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_state.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_util.h"
38 #include "pipe/p_winsys.h"
40 #include "i915_context.h"
41 #include "i915_texture.h"
42 #include "i915_debug.h"
43 #include "i915_screen.h"
46 static unsigned minify( unsigned d
)
54 i915_miptree_set_level_info(struct i915_texture
*tex
,
57 unsigned x
, unsigned y
, unsigned w
, unsigned h
, unsigned d
)
59 struct pipe_texture
*pt
= &tex
->base
;
61 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
64 pt
->height
[level
] = h
;
67 tex
->level_offset
[level
] = (x
+ y
* tex
->pitch
) * pt
->cpp
;
68 tex
->nr_images
[level
] = nr_images
;
71 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
72 level, w, h, d, x, y, tex->level_offset[level]);
75 /* Not sure when this would happen, but anyway:
77 if (tex
->image_offset
[level
]) {
78 FREE(tex
->image_offset
[level
]);
79 tex
->image_offset
[level
] = NULL
;
83 assert(!tex
->image_offset
[level
]);
85 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
86 tex
->image_offset
[level
][0] = 0;
91 i915_miptree_set_image_offset(struct i915_texture
*tex
,
92 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
94 if (img
== 0 && level
== 0)
95 assert(x
== 0 && y
== 0);
97 assert(img
< tex
->nr_images
[level
]);
99 tex
->image_offset
[level
][img
] = (x
+ y
* tex
->pitch
);
102 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
103 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
108 /* Hack it up to use the old winsys->surface_alloc_storage()
112 i915_displaytarget_layout(struct pipe_screen
*screen
,
113 struct i915_texture
*tex
)
115 struct pipe_winsys
*ws
= screen
->winsys
;
116 struct pipe_surface surf
;
117 unsigned flags
= (PIPE_BUFFER_USAGE_CPU_READ
|
118 PIPE_BUFFER_USAGE_CPU_WRITE
|
119 PIPE_BUFFER_USAGE_GPU_READ
|
120 PIPE_BUFFER_USAGE_GPU_WRITE
);
123 memset(&surf
, 0, sizeof(surf
));
125 ws
->surface_alloc_storage( ws
,
131 tex
->base
.tex_usage
);
133 /* Now extract the goodies:
135 i915_miptree_set_image_offset( tex
, 0, 0, 0, 0 );
136 i915_miptree_set_level_info( tex
, 0, 0, 0, 0,
141 tex
->buffer
= surf
.buffer
;
142 tex
->pitch
= surf
.pitch
;
143 tex
->total_height
= 0;
146 return tex
->buffer
!= NULL
;
154 i945_miptree_layout_2d( struct i915_texture
*tex
)
156 struct pipe_texture
*pt
= &tex
->base
;
157 int align_h
= 2, align_w
= 4;
161 unsigned width
= pt
->width
[0];
162 unsigned height
= pt
->height
[0];
164 tex
->pitch
= pt
->width
[0];
166 /* May need to adjust pitch to accomodate the placement of
167 * the 2nd mipmap level. This occurs when the alignment
168 * constraints of mipmap placement push the right edge of the
169 * 2nd mipmap level out past the width of its parent.
171 if (pt
->last_level
> 0) {
172 unsigned mip1_width
= align_int(minify(pt
->width
[0]), align_w
)
173 + minify(minify(pt
->width
[0]));
175 if (mip1_width
> pt
->width
[0])
176 tex
->pitch
= mip1_width
;
179 /* Pitch must be a whole number of dwords, even though we
180 * express it in texels.
182 tex
->pitch
= align_int(tex
->pitch
* pt
->cpp
, 4) / pt
->cpp
;
183 tex
->total_height
= 0;
185 for (level
= 0; level
<= pt
->last_level
; level
++) {
188 i915_miptree_set_level_info(tex
, level
, 1, x
, y
, width
, height
, 1);
191 img_height
= MAX2(1, height
/4);
193 img_height
= align_int(height
, align_h
);
196 /* Because the images are packed better, the final offset
197 * might not be the maximal one:
199 tex
->total_height
= MAX2(tex
->total_height
, y
+ img_height
);
201 /* Layout_below: step right after second mipmap level.
204 x
+= align_int(width
, align_w
);
210 width
= minify(width
);
211 height
= minify(height
);
216 static const int initial_offsets
[6][2] = {
225 static const int step_offsets
[6][2] = {
236 i915_miptree_layout(struct i915_texture
* tex
)
238 struct pipe_texture
*pt
= &tex
->base
;
241 switch (pt
->target
) {
242 case PIPE_TEXTURE_CUBE
: {
243 const unsigned dim
= pt
->width
[0];
245 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
247 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
249 /* double pitch for cube layouts */
250 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
251 tex
->total_height
= dim
* 4;
253 for (level
= 0; level
<= pt
->last_level
; level
++) {
254 i915_miptree_set_level_info(tex
, level
, 6,
256 /*OLD: tex->pitch, tex->total_height,*/
263 for (face
= 0; face
< 6; face
++) {
264 unsigned x
= initial_offsets
[face
][0] * dim
;
265 unsigned y
= initial_offsets
[face
][1] * dim
;
268 for (level
= 0; level
<= pt
->last_level
; level
++) {
269 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
271 x
+= step_offsets
[face
][0] * d
;
272 y
+= step_offsets
[face
][1] * d
;
277 case PIPE_TEXTURE_3D
:{
278 unsigned width
= pt
->width
[0];
279 unsigned height
= pt
->height
[0];
280 unsigned depth
= pt
->depth
[0];
281 unsigned stack_height
= 0;
283 /* Calculate the size of a single slice.
285 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
287 /* XXX: hardware expects/requires 9 levels at minimum.
289 for (level
= 0; level
<= MAX2(8, pt
->last_level
);
291 i915_miptree_set_level_info(tex
, level
, depth
, 0, tex
->total_height
,
292 width
, height
, depth
);
295 stack_height
+= MAX2(2, height
);
297 width
= minify(width
);
298 height
= minify(height
);
299 depth
= minify(depth
);
302 /* Fixup depth image_offsets:
304 depth
= pt
->depth
[0];
305 for (level
= 0; level
<= pt
->last_level
; level
++) {
307 for (i
= 0; i
< depth
; i
++)
308 i915_miptree_set_image_offset(tex
, level
, i
,
309 0, i
* stack_height
);
311 depth
= minify(depth
);
315 /* Multiply slice size by texture depth for total size. It's
316 * remarkable how wasteful of memory the i915 texture layouts
317 * are. They are largely fixed in the i945.
319 tex
->total_height
= stack_height
* pt
->depth
[0];
324 unsigned width
= pt
->width
[0];
325 unsigned height
= pt
->height
[0];
328 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
329 tex
->total_height
= 0;
331 for (level
= 0; level
<= pt
->last_level
; level
++) {
332 i915_miptree_set_level_info(tex
, level
, 1,
333 0, tex
->total_height
,
337 img_height
= MAX2(1, height
/ 4);
339 img_height
= (MAX2(2, height
) + 1) & ~1;
341 tex
->total_height
+= img_height
;
343 width
= minify(width
);
344 height
= minify(height
);
350 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
352 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
360 i945_miptree_layout(struct i915_texture
* tex
)
362 struct pipe_texture
*pt
= &tex
->base
;
365 switch (pt
->target
) {
366 case PIPE_TEXTURE_CUBE
:{
367 const unsigned dim
= pt
->width
[0];
369 unsigned lvlWidth
= pt
->width
[0], lvlHeight
= pt
->height
[0];
371 assert(lvlWidth
== lvlHeight
); /* cubemap images are square */
373 /* Depending on the size of the largest images, pitch can be
374 * determined either by the old-style packing of cubemap faces,
375 * or the final row of 4x4, 2x2 and 1x1 faces below this.
378 tex
->pitch
= ((dim
* pt
->cpp
* 2 + 3) & ~3) / pt
->cpp
;
382 tex
->total_height
= dim
* 4 + 4;
384 /* Set all the levels to effectively occupy the whole rectangular region.
386 for (level
= 0; level
<= pt
->last_level
; level
++) {
387 i915_miptree_set_level_info(tex
, level
, 6,
389 lvlWidth
, lvlHeight
, 1);
395 for (face
= 0; face
< 6; face
++) {
396 unsigned x
= initial_offsets
[face
][0] * dim
;
397 unsigned y
= initial_offsets
[face
][1] * dim
;
400 if (dim
== 4 && face
>= 4) {
401 y
= tex
->total_height
- 4;
404 else if (dim
< 4 && (face
> 0)) {
405 y
= tex
->total_height
- 4;
409 for (level
= 0; level
<= pt
->last_level
; level
++) {
410 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
417 case PIPE_TEX_FACE_POS_X
:
418 case PIPE_TEX_FACE_NEG_X
:
419 x
+= step_offsets
[face
][0] * d
;
420 y
+= step_offsets
[face
][1] * d
;
422 case PIPE_TEX_FACE_POS_Y
:
423 case PIPE_TEX_FACE_NEG_Y
:
427 case PIPE_TEX_FACE_POS_Z
:
428 case PIPE_TEX_FACE_NEG_Z
:
429 y
= tex
->total_height
- 4;
435 y
= tex
->total_height
- 4;
444 x
+= step_offsets
[face
][0] * d
;
445 y
+= step_offsets
[face
][1] * d
;
452 case PIPE_TEXTURE_3D
:{
453 unsigned width
= pt
->width
[0];
454 unsigned height
= pt
->height
[0];
455 unsigned depth
= pt
->depth
[0];
456 unsigned pack_x_pitch
, pack_x_nr
;
457 unsigned pack_y_pitch
;
460 tex
->pitch
= ((pt
->width
[0] * pt
->cpp
+ 3) & ~3) / pt
->cpp
;
461 tex
->total_height
= 0;
463 pack_y_pitch
= MAX2(pt
->height
[0], 2);
464 pack_x_pitch
= tex
->pitch
;
467 for (level
= 0; level
<= pt
->last_level
; level
++) {
468 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
473 i915_miptree_set_level_info(tex
, level
, nr_images
,
474 0, tex
->total_height
,
475 width
, height
, depth
);
477 for (q
= 0; q
< nr_images
;) {
478 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
479 i915_miptree_set_image_offset(tex
, level
, q
, x
, y
);
488 tex
->total_height
+= y
;
490 if (pack_x_pitch
> 4) {
493 assert(pack_x_pitch
* pack_x_nr
<= tex
->pitch
);
496 if (pack_y_pitch
> 2) {
500 width
= minify(width
);
501 height
= minify(height
);
502 depth
= minify(depth
);
507 case PIPE_TEXTURE_1D
:
508 case PIPE_TEXTURE_2D
:
509 // case PIPE_TEXTURE_RECTANGLE:
510 i945_miptree_layout_2d(tex
);
518 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
520 tex->total_height, pt->cpp, tex->pitch * tex->total_height * pt->cpp);
527 static struct pipe_texture
*
528 i915_texture_create_screen(struct pipe_screen
*screen
,
529 const struct pipe_texture
*templat
)
531 struct i915_screen
*i915screen
= i915_screen(screen
);
532 struct pipe_winsys
*ws
= screen
->winsys
;
533 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
538 tex
->base
= *templat
;
539 tex
->base
.refcount
= 1;
540 tex
->base
.screen
= screen
;
542 if (tex
->base
.tex_usage
& PIPE_TEXTURE_USAGE_DISPLAY_TARGET
) {
543 if (!i915_displaytarget_layout(screen
, tex
))
547 if (i915screen
->is_i945
) {
548 if (!i945_miptree_layout(tex
))
552 if (!i915_miptree_layout(tex
))
556 tex
->buffer
= ws
->buffer_create(ws
, 64,
557 PIPE_BUFFER_USAGE_PIXEL
,
558 tex
->pitch
* tex
->base
.cpp
*
574 i915_texture_release_screen(struct pipe_screen
*screen
,
575 struct pipe_texture
**pt
)
581 DBG("%s %p refcount will be %d\n",
582 __FUNCTION__, (void *) *pt, (*pt)->refcount - 1);
584 if (--(*pt
)->refcount
<= 0) {
585 struct i915_texture
*tex
= (struct i915_texture
*)*pt
;
589 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
592 pipe_buffer_reference(screen
->winsys
, &tex
->buffer
, NULL
);
594 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
595 if (tex
->image_offset
[i
])
596 FREE(tex
->image_offset
[i
]);
606 * XXX note: same as code in sp_surface.c
608 static struct pipe_surface
*
609 i915_get_tex_surface_screen(struct pipe_screen
*screen
,
610 struct pipe_texture
*pt
,
611 unsigned face
, unsigned level
, unsigned zslice
,
614 struct i915_texture
*tex
= (struct i915_texture
*)pt
;
615 struct pipe_winsys
*ws
= screen
->winsys
;
616 struct pipe_surface
*ps
;
617 unsigned offset
; /* in bytes */
619 offset
= tex
->level_offset
[level
];
621 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
622 offset
+= tex
->image_offset
[level
][face
] * pt
->cpp
;
624 else if (pt
->target
== PIPE_TEXTURE_3D
) {
625 offset
+= tex
->image_offset
[level
][zslice
] * pt
->cpp
;
632 ps
= ws
->surface_alloc(ws
);
634 assert(ps
->refcount
);
636 pipe_buffer_reference(ws
, &ps
->buffer
, tex
->buffer
);
637 ps
->format
= pt
->format
;
639 ps
->width
= pt
->width
[level
];
640 ps
->height
= pt
->height
[level
];
641 ps
->pitch
= tex
->pitch
;
650 i915_init_texture_functions(struct i915_context
*i915
)
652 // i915->pipe.texture_update = i915_texture_update;
657 i915_init_screen_texture_functions(struct pipe_screen
*screen
)
659 screen
->texture_create
= i915_texture_create_screen
;
660 screen
->texture_release
= i915_texture_release_screen
;
661 screen
->get_tex_surface
= i915_get_tex_surface_screen
;