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/internal/p_winsys_screen.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 #include "i915_context.h"
42 #include "i915_texture.h"
43 #include "i915_debug.h"
44 #include "i915_screen.h"
45 #include "i915_winsys.h"
48 * Helper function and arrays
52 * Initial offset for Cube map.
54 static const int initial_offsets
[6][2] = {
64 * Step offsets for Cube map.
66 static const int step_offsets
[6][2] = {
76 power_of_two(unsigned x
)
85 round_up(unsigned n
, unsigned multiple
)
87 return (n
+ multiple
- 1) & ~(multiple
- 1);
92 * More advanced helper funcs
97 i915_miptree_set_level_info(struct i915_texture
*tex
,
100 unsigned w
, unsigned h
, unsigned d
)
102 struct pipe_texture
*pt
= &tex
->base
;
104 assert(level
< PIPE_MAX_TEXTURE_LEVELS
);
106 pt
->width
[level
] = w
;
107 pt
->height
[level
] = h
;
108 pt
->depth
[level
] = d
;
110 pt
->nblocksx
[level
] = pf_get_nblocksx(&pt
->block
, w
);
111 pt
->nblocksy
[level
] = pf_get_nblocksy(&pt
->block
, h
);
113 tex
->nr_images
[level
] = nr_images
;
116 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
117 level, w, h, d, x, y, tex->level_offset[level]);
120 /* Not sure when this would happen, but anyway:
122 if (tex
->image_offset
[level
]) {
123 FREE(tex
->image_offset
[level
]);
124 tex
->image_offset
[level
] = NULL
;
128 assert(!tex
->image_offset
[level
]);
130 tex
->image_offset
[level
] = (unsigned *) MALLOC(nr_images
* sizeof(unsigned));
131 tex
->image_offset
[level
][0] = 0;
135 i915_miptree_set_image_offset(struct i915_texture
*tex
,
136 unsigned level
, unsigned img
, unsigned x
, unsigned y
)
138 if (img
== 0 && level
== 0)
139 assert(x
== 0 && y
== 0);
141 assert(img
< tex
->nr_images
[level
]);
143 tex
->image_offset
[level
][img
] = y
* tex
->stride
+ x
* tex
->base
.block
.size
;
146 printf("%s level %d img %d pos %d,%d image_offset %x\n",
147 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
158 * Special case to deal with scanout textures.
161 i915_scanout_layout(struct i915_texture
*tex
)
163 struct pipe_texture
*pt
= &tex
->base
;
165 if (pt
->last_level
> 0 || pt
->block
.size
!= 4)
168 i915_miptree_set_level_info(tex
, 0, 1,
172 i915_miptree_set_image_offset(tex
, 0, 0, 0, 0);
174 #if 0 /* TODO use this code when backend is smarter */
175 if (tex
->base
.width
[0] >= 240) {
176 tex
->stride
= power_of_two(tex
->base
.nblocksx
[0] * pt
->block
.size
);
177 tex
->total_nblocksy
= round_up(tex
->base
.nblocksy
[0], 8);
179 if (tex
->base
.width
[0] >= 240) {
180 tex
->stride
= 2048 * 4;
181 tex
->total_nblocksy
= round_up(tex
->base
.nblocksy
[0], 8);
183 } else if (tex
->base
.width
[0] == 64 && tex
->base
.height
[0] == 64) {
184 tex
->stride
= power_of_two(tex
->base
.nblocksx
[0] * pt
->block
.size
);
185 tex
->total_nblocksy
= round_up(tex
->base
.nblocksy
[0], 8);
190 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__
,
191 tex
->base
.width
[0], tex
->base
.height
[0], pt
->block
.size
,
192 tex
->stride
, tex
->total_nblocksy
, tex
->stride
* tex
->total_nblocksy
);
198 i945_miptree_layout_2d(struct i915_texture
*tex
)
200 struct pipe_texture
*pt
= &tex
->base
;
201 const int align_x
= 2, align_y
= 4;
205 unsigned width
= pt
->width
[0];
206 unsigned height
= pt
->height
[0];
207 unsigned nblocksx
= pt
->nblocksx
[0];
208 unsigned nblocksy
= pt
->nblocksy
[0];
210 /* used for scanouts that need special layouts */
211 if (tex
->base
.tex_usage
& PIPE_TEXTURE_USAGE_PRIMARY
)
212 if (i915_scanout_layout(tex
))
215 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
217 /* May need to adjust pitch to accomodate the placement of
218 * the 2nd mipmap level. This occurs when the alignment
219 * constraints of mipmap placement push the right edge of the
220 * 2nd mipmap level out past the width of its parent.
222 if (pt
->last_level
> 0) {
223 unsigned mip1_nblocksx
224 = align(pf_get_nblocksx(&pt
->block
, minify(width
)), align_x
)
225 + pf_get_nblocksx(&pt
->block
, minify(minify(width
)));
227 if (mip1_nblocksx
> nblocksx
)
228 tex
->stride
= mip1_nblocksx
* pt
->block
.size
;
231 /* Pitch must be a whole number of dwords
233 tex
->stride
= align(tex
->stride
, 64);
234 tex
->total_nblocksy
= 0;
236 for (level
= 0; level
<= pt
->last_level
; level
++) {
237 i915_miptree_set_level_info(tex
, level
, 1, width
, height
, 1);
238 i915_miptree_set_image_offset(tex
, level
, 0, x
, y
);
240 nblocksy
= align(nblocksy
, align_y
);
242 /* Because the images are packed better, the final offset
243 * might not be the maximal one:
245 tex
->total_nblocksy
= MAX2(tex
->total_nblocksy
, y
+ nblocksy
);
247 /* Layout_below: step right after second mipmap level.
250 x
+= align(nblocksx
, align_x
);
256 width
= minify(width
);
257 height
= minify(height
);
258 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
259 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
264 i945_miptree_layout_cube(struct i915_texture
*tex
)
266 struct pipe_texture
*pt
= &tex
->base
;
269 const unsigned nblocks
= pt
->nblocksx
[0];
271 unsigned width
= pt
->width
[0];
272 unsigned height
= pt
->height
[0];
275 printf("%s %i, %i\n", __FUNCTION__, pt->width[0], pt->height[0]);
278 assert(width
== height
); /* cubemap images are square */
281 * XXX Should only be used for compressed formats. But lets
282 * keep this code active just in case.
284 * Depending on the size of the largest images, pitch can be
285 * determined either by the old-style packing of cubemap faces,
286 * or the final row of 4x4, 2x2 and 1x1 faces below this.
289 tex
->stride
= round_up(nblocks
* pt
->block
.size
* 2, 4);
291 tex
->stride
= 14 * 8 * pt
->block
.size
;
293 tex
->total_nblocksy
= nblocks
* 4;
295 /* Set all the levels to effectively occupy the whole rectangular region.
297 for (level
= 0; level
<= pt
->last_level
; level
++) {
298 i915_miptree_set_level_info(tex
, level
, 6, width
, height
, 1);
303 for (face
= 0; face
< 6; face
++) {
304 unsigned x
= initial_offsets
[face
][0] * nblocks
;
305 unsigned y
= initial_offsets
[face
][1] * nblocks
;
306 unsigned d
= nblocks
;
308 #if 0 /* Fix and enable this code for compressed formats */
309 if (nblocks
== 4 && face
>= 4) {
310 y
= tex
->total_height
- 4;
313 else if (nblocks
< 4 && (face
> 0)) {
314 y
= tex
->total_height
- 4;
319 for (level
= 0; level
<= pt
->last_level
; level
++) {
320 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
324 #if 0 /* Fix and enable this code for compressed formats */
328 case PIPE_TEX_FACE_POS_X
:
329 case PIPE_TEX_FACE_NEG_X
:
330 x
+= step_offsets
[face
][0] * d
;
331 y
+= step_offsets
[face
][1] * d
;
333 case PIPE_TEX_FACE_POS_Y
:
334 case PIPE_TEX_FACE_NEG_Y
:
338 case PIPE_TEX_FACE_POS_Z
:
339 case PIPE_TEX_FACE_NEG_Z
:
340 y
= tex
->total_height
- 4;
345 y
= tex
->total_height
- 4;
354 x
+= step_offsets
[face
][0] * d
;
355 y
+= step_offsets
[face
][1] * d
;
365 i915_miptree_layout(struct i915_texture
* tex
)
367 struct pipe_texture
*pt
= &tex
->base
;
370 switch (pt
->target
) {
371 case PIPE_TEXTURE_CUBE
: {
372 const unsigned nblocks
= pt
->nblocksx
[0];
374 unsigned width
= pt
->width
[0], height
= pt
->height
[0];
376 assert(width
== height
); /* cubemap images are square */
378 /* double pitch for cube layouts */
379 tex
->stride
= round_up(nblocks
* pt
->block
.size
* 2, 4);
380 tex
->total_nblocksy
= nblocks
* 4;
382 for (level
= 0; level
<= pt
->last_level
; level
++) {
383 i915_miptree_set_level_info(tex
, level
, 6,
390 for (face
= 0; face
< 6; face
++) {
391 unsigned x
= initial_offsets
[face
][0] * nblocks
;
392 unsigned y
= initial_offsets
[face
][1] * nblocks
;
393 unsigned d
= nblocks
;
395 for (level
= 0; level
<= pt
->last_level
; level
++) {
396 i915_miptree_set_image_offset(tex
, level
, face
, x
, y
);
398 x
+= step_offsets
[face
][0] * d
;
399 y
+= step_offsets
[face
][1] * d
;
404 case PIPE_TEXTURE_3D
:{
405 unsigned width
= pt
->width
[0];
406 unsigned height
= pt
->height
[0];
407 unsigned depth
= pt
->depth
[0];
408 unsigned nblocksx
= pt
->nblocksx
[0];
409 unsigned nblocksy
= pt
->nblocksy
[0];
410 unsigned stack_nblocksy
= 0;
412 /* Calculate the size of a single slice.
414 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
416 /* XXX: hardware expects/requires 9 levels at minimum.
418 for (level
= 0; level
<= MAX2(8, pt
->last_level
);
420 i915_miptree_set_level_info(tex
, level
, depth
,
421 width
, height
, depth
);
424 stack_nblocksy
+= MAX2(2, nblocksy
);
426 width
= minify(width
);
427 height
= minify(height
);
428 depth
= minify(depth
);
429 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
430 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
433 /* Fixup depth image_offsets:
435 depth
= pt
->depth
[0];
436 for (level
= 0; level
<= pt
->last_level
; level
++) {
438 for (i
= 0; i
< depth
; i
++)
439 i915_miptree_set_image_offset(tex
, level
, i
,
440 0, i
* stack_nblocksy
);
442 depth
= minify(depth
);
446 /* Multiply slice size by texture depth for total size. It's
447 * remarkable how wasteful of memory the i915 texture layouts
448 * are. They are largely fixed in the i945.
450 tex
->total_nblocksy
= stack_nblocksy
* pt
->depth
[0];
455 unsigned width
= pt
->width
[0];
456 unsigned height
= pt
->height
[0];
457 unsigned nblocksx
= pt
->nblocksx
[0];
458 unsigned nblocksy
= pt
->nblocksy
[0];
460 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
461 tex
->total_nblocksy
= 0;
463 for (level
= 0; level
<= pt
->last_level
; level
++) {
464 i915_miptree_set_level_info(tex
, level
, 1,
466 i915_miptree_set_image_offset(tex
, level
, 0,
467 0, tex
->total_nblocksy
);
469 nblocksy
= round_up(MAX2(2, nblocksy
), 2);
471 tex
->total_nblocksy
+= nblocksy
;
473 width
= minify(width
);
474 height
= minify(height
);
475 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
476 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
482 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
484 tex->total_nblocksy, pt->block.size, tex->stride * tex->total_nblocksy);
492 i945_miptree_layout(struct i915_texture
* tex
)
494 struct pipe_texture
*pt
= &tex
->base
;
497 switch (pt
->target
) {
498 case PIPE_TEXTURE_CUBE
:
499 i945_miptree_layout_cube(tex
);
501 case PIPE_TEXTURE_3D
:{
502 unsigned width
= pt
->width
[0];
503 unsigned height
= pt
->height
[0];
504 unsigned depth
= pt
->depth
[0];
505 unsigned nblocksx
= pt
->nblocksx
[0];
506 unsigned nblocksy
= pt
->nblocksy
[0];
507 unsigned pack_x_pitch
, pack_x_nr
;
508 unsigned pack_y_pitch
;
510 tex
->stride
= round_up(pt
->nblocksx
[0] * pt
->block
.size
, 4);
511 tex
->total_nblocksy
= 0;
513 pack_y_pitch
= MAX2(pt
->nblocksy
[0], 2);
514 pack_x_pitch
= tex
->stride
/ pt
->block
.size
;
517 for (level
= 0; level
<= pt
->last_level
; level
++) {
518 unsigned nr_images
= pt
->target
== PIPE_TEXTURE_3D
? depth
: 6;
523 i915_miptree_set_level_info(tex
, level
, nr_images
,
524 width
, height
, depth
);
526 for (q
= 0; q
< nr_images
;) {
527 for (j
= 0; j
< pack_x_nr
&& q
< nr_images
; j
++, q
++) {
528 i915_miptree_set_image_offset(tex
, level
, q
, x
, y
+ tex
->total_nblocksy
);
537 tex
->total_nblocksy
+= y
;
539 if (pack_x_pitch
> 4) {
542 assert(pack_x_pitch
* pack_x_nr
* pt
->block
.size
<= tex
->stride
);
545 if (pack_y_pitch
> 2) {
549 width
= minify(width
);
550 height
= minify(height
);
551 depth
= minify(depth
);
552 nblocksx
= pf_get_nblocksx(&pt
->block
, width
);
553 nblocksy
= pf_get_nblocksy(&pt
->block
, height
);
558 case PIPE_TEXTURE_1D
:
559 case PIPE_TEXTURE_2D
:
560 // case PIPE_TEXTURE_RECTANGLE:
561 i945_miptree_layout_2d(tex
);
569 DBG("%s: %dx%dx%d - sz 0x%x\n", __FUNCTION__,
571 tex->total_nblocksy, pt->block.size, tex->stride * tex->total_nblocksy);
578 static struct pipe_texture
*
579 i915_texture_create(struct pipe_screen
*screen
,
580 const struct pipe_texture
*templat
)
582 struct i915_screen
*i915screen
= i915_screen(screen
);
583 struct i915_texture
*tex
= CALLOC_STRUCT(i915_texture
);
585 unsigned buf_usage
= 0;
590 tex
->base
= *templat
;
591 pipe_reference_init(&tex
->base
.reference
, 1);
592 tex
->base
.screen
= screen
;
594 tex
->base
.nblocksx
[0] = pf_get_nblocksx(&tex
->base
.block
, tex
->base
.width
[0]);
595 tex
->base
.nblocksy
[0] = pf_get_nblocksy(&tex
->base
.block
, tex
->base
.height
[0]);
597 if (i915screen
->is_i945
) {
598 if (!i945_miptree_layout(tex
))
601 if (!i915_miptree_layout(tex
))
605 tex_size
= tex
->stride
* tex
->total_nblocksy
;
607 buf_usage
= PIPE_BUFFER_USAGE_PIXEL
;
609 /* for scanouts and cursors, cursors don't have the scanout tag */
610 if (templat
->tex_usage
& PIPE_TEXTURE_USAGE_PRIMARY
&& templat
->width
[0] != 64)
611 buf_usage
|= I915_BUFFER_USAGE_SCANOUT
;
613 tex
->buffer
= screen
->buffer_create(screen
, 64, buf_usage
, tex_size
);
619 void *ptr
= ws
->buffer_map(ws
, tex
->buffer
,
620 PIPE_BUFFER_USAGE_CPU_WRITE
);
621 memset(ptr
, 0x80, tex_size
);
622 ws
->buffer_unmap(ws
, tex
->buffer
);
634 i915_texture_destroy(struct pipe_texture
*pt
)
636 struct i915_texture
*tex
= (struct i915_texture
*)pt
;
640 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
643 pipe_buffer_reference(&tex
->buffer
, NULL
);
645 for (i
= 0; i
< PIPE_MAX_TEXTURE_LEVELS
; i
++)
646 if (tex
->image_offset
[i
])
647 FREE(tex
->image_offset
[i
]);
652 static struct pipe_surface
*
653 i915_get_tex_surface(struct pipe_screen
*screen
,
654 struct pipe_texture
*pt
,
655 unsigned face
, unsigned level
, unsigned zslice
,
658 struct i915_texture
*tex
= (struct i915_texture
*)pt
;
659 struct pipe_surface
*ps
;
660 unsigned offset
; /* in bytes */
662 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
663 offset
= tex
->image_offset
[level
][face
];
665 else if (pt
->target
== PIPE_TEXTURE_3D
) {
666 offset
= tex
->image_offset
[level
][zslice
];
669 offset
= tex
->image_offset
[level
][0];
674 ps
= CALLOC_STRUCT(pipe_surface
);
676 pipe_reference_init(&ps
->reference
, 1);
677 pipe_texture_reference(&ps
->texture
, pt
);
678 ps
->format
= pt
->format
;
679 ps
->width
= pt
->width
[level
];
680 ps
->height
= pt
->height
[level
];
687 static struct pipe_texture
*
688 i915_texture_blanket(struct pipe_screen
* screen
,
689 const struct pipe_texture
*base
,
690 const unsigned *stride
,
691 struct pipe_buffer
*buffer
)
693 struct i915_texture
*tex
;
696 /* Only supports one type */
697 if (base
->target
!= PIPE_TEXTURE_2D
||
698 base
->last_level
!= 0 ||
699 base
->depth
[0] != 1) {
703 tex
= CALLOC_STRUCT(i915_texture
);
708 pipe_reference_init(&tex
->base
.reference
, 1);
709 tex
->base
.screen
= screen
;
711 tex
->stride
= stride
[0];
713 i915_miptree_set_level_info(tex
, 0, 1, base
->width
[0], base
->height
[0], 1);
714 i915_miptree_set_image_offset(tex
, 0, 0, 0, 0);
716 pipe_buffer_reference(&tex
->buffer
, buffer
);
722 i915_tex_surface_destroy(struct pipe_surface
*surf
)
724 pipe_texture_reference(&surf
->texture
, NULL
);
729 i915_init_screen_texture_functions(struct i915_screen
*is
)
731 is
->base
.texture_create
= i915_texture_create
;
732 is
->base
.texture_destroy
= i915_texture_destroy
;
733 is
->base
.get_tex_surface
= i915_get_tex_surface
;
734 is
->base
.texture_blanket
= i915_texture_blanket
;
735 is
->base
.tex_surface_destroy
= i915_tex_surface_destroy
;
738 boolean
i915_get_texture_buffer(struct pipe_texture
*texture
,
739 struct pipe_buffer
**buf
,
742 struct i915_texture
*tex
= (struct i915_texture
*)texture
;
747 pipe_buffer_reference(buf
, tex
->buffer
);
750 *stride
= tex
->stride
;