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 "util/u_inlines.h"
37 #include "util/u_format.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_screen.h"
44 #include "i915_winsys.h"
48 * Helper function and arrays
53 * Initial offset for Cube map.
55 static const int initial_offsets[6][2] = {
65 * Step offsets for Cube map.
67 static const int step_offsets[6][2] = {
76 /* XXX really need twice the size if x is already pot?
77 Otherwise just use util_next_power_of_two?
80 power_of_two(unsigned x)
89 * More advanced helper funcs
94 i915_miptree_set_level_info(struct i915_texture *tex,
97 unsigned w, unsigned h, unsigned d)
99 assert(level < Elements(tex->nr_images));
101 tex->nr_images[level] = nr_images;
104 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
105 level, w, h, d, x, y, tex->level_offset[level]);
108 /* Not sure when this would happen, but anyway:
110 if (tex->image_offset[level]) {
111 FREE(tex->image_offset[level]);
112 tex->image_offset[level] = NULL;
116 assert(!tex->image_offset[level]);
118 tex->image_offset[level] = (unsigned *) MALLOC(nr_images * sizeof(unsigned));
119 tex->image_offset[level][0] = 0;
123 i915_miptree_set_image_offset(struct i915_texture *tex,
124 unsigned level, unsigned img, unsigned x, unsigned y)
126 if (img == 0 && level == 0)
127 assert(x == 0 && y == 0);
129 assert(img < tex->nr_images[level]);
131 tex->image_offset[level][img] = y * tex->stride + x * util_format_get_blocksize(tex->base.format);
134 printf("%s level %d img %d pos %d,%d image_offset %x\n",
135 __FUNCTION__, level, img, x, y, tex->image_offset[level][img]);
141 * i915 layout functions, some used by i945
146 * Special case to deal with scanout textures.
149 i915_scanout_layout(struct i915_texture *tex)
151 struct pipe_texture *pt = &tex->base;
153 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
156 i915_miptree_set_level_info(tex, 0, 1,
160 i915_miptree_set_image_offset(tex, 0, 0, 0, 0);
162 if (pt->width0 >= 240) {
163 tex->stride = power_of_two(util_format_get_stride(pt->format, pt->width0));
164 tex->total_nblocksy = align(util_format_get_nblocksy(pt->format, pt->height0), 8);
165 tex->hw_tiled = I915_TILE_X;
166 } else if (pt->width0 == 64 && pt->height0 == 64) {
167 tex->stride = power_of_two(util_format_get_stride(pt->format, pt->width0));
168 tex->total_nblocksy = align(util_format_get_nblocksy(pt->format, pt->height0), 8);
173 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
174 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
175 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
181 * Special case to deal with shared textures.
184 i915_display_target_layout(struct i915_texture *tex)
186 struct pipe_texture *pt = &tex->base;
188 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
191 /* fallback to normal textures for small textures */
192 if (pt->width0 < 240)
195 i915_miptree_set_level_info(tex, 0, 1,
199 i915_miptree_set_image_offset(tex, 0, 0, 0, 0);
201 tex->stride = power_of_two(util_format_get_stride(pt->format, pt->width0));
202 tex->total_nblocksy = align(util_format_get_nblocksy(pt->format, pt->height0), 8);
203 tex->hw_tiled = I915_TILE_X;
205 debug_printf("%s size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
206 pt->width0, pt->height0, util_format_get_blocksize(pt->format),
207 tex->stride, tex->total_nblocksy, tex->stride * tex->total_nblocksy);
213 i915_miptree_layout_2d(struct i915_texture *tex)
215 struct pipe_texture *pt = &tex->base;
217 unsigned width = pt->width0;
218 unsigned height = pt->height0;
219 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
221 /* used for scanouts that need special layouts */
222 if (pt->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT)
223 if (i915_scanout_layout(tex))
226 /* shared buffers needs to be compatible with X servers */
227 if (pt->tex_usage & PIPE_TEXTURE_USAGE_SHARED)
228 if (i915_display_target_layout(tex))
231 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
232 tex->total_nblocksy = 0;
234 for (level = 0; level <= pt->last_level; level++) {
235 i915_miptree_set_level_info(tex, level, 1, width, height, 1);
236 i915_miptree_set_image_offset(tex, level, 0, 0, tex->total_nblocksy);
238 nblocksy = align(MAX2(2, nblocksy), 2);
240 tex->total_nblocksy += nblocksy;
242 width = u_minify(width, 1);
243 height = u_minify(height, 1);
244 nblocksy = util_format_get_nblocksy(pt->format, height);
249 i915_miptree_layout_3d(struct i915_texture *tex)
251 struct pipe_texture *pt = &tex->base;
254 unsigned width = pt->width0;
255 unsigned height = pt->height0;
256 unsigned depth = pt->depth0;
257 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
258 unsigned stack_nblocksy = 0;
260 /* Calculate the size of a single slice.
262 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
264 /* XXX: hardware expects/requires 9 levels at minimum.
266 for (level = 0; level <= MAX2(8, pt->last_level); level++) {
267 i915_miptree_set_level_info(tex, level, depth, width, height, depth);
269 stack_nblocksy += MAX2(2, nblocksy);
271 width = u_minify(width, 1);
272 height = u_minify(height, 1);
273 nblocksy = util_format_get_nblocksy(pt->format, height);
276 /* Fixup depth image_offsets:
278 for (level = 0; level <= pt->last_level; level++) {
280 for (i = 0; i < depth; i++)
281 i915_miptree_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
283 depth = u_minify(depth, 1);
286 /* Multiply slice size by texture depth for total size. It's
287 * remarkable how wasteful of memory the i915 texture layouts
288 * are. They are largely fixed in the i945.
290 tex->total_nblocksy = stack_nblocksy * pt->depth0;
294 i915_miptree_layout_cube(struct i915_texture *tex)
296 struct pipe_texture *pt = &tex->base;
297 unsigned width = pt->width0, height = pt->height0;
298 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
302 assert(width == height); /* cubemap images are square */
304 /* double pitch for cube layouts */
305 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
306 tex->total_nblocksy = nblocks * 4;
308 for (level = 0; level <= pt->last_level; level++) {
309 i915_miptree_set_level_info(tex, level, 6, width, height, 1);
314 for (face = 0; face < 6; face++) {
315 unsigned x = initial_offsets[face][0] * nblocks;
316 unsigned y = initial_offsets[face][1] * nblocks;
317 unsigned d = nblocks;
319 for (level = 0; level <= pt->last_level; level++) {
320 i915_miptree_set_image_offset(tex, level, face, x, y);
322 x += step_offsets[face][0] * d;
323 y += step_offsets[face][1] * d;
329 i915_miptree_layout(struct i915_texture * tex)
331 struct pipe_texture *pt = &tex->base;
333 switch (pt->target) {
334 case PIPE_TEXTURE_1D:
335 case PIPE_TEXTURE_2D:
336 i915_miptree_layout_2d(tex);
338 case PIPE_TEXTURE_3D:
339 i915_miptree_layout_3d(tex);
341 case PIPE_TEXTURE_CUBE:
342 i915_miptree_layout_cube(tex);
354 * i945 layout functions
359 i945_miptree_layout_2d(struct i915_texture *tex)
361 struct pipe_texture *pt = &tex->base;
362 const int align_x = 2, align_y = 4;
366 unsigned width = pt->width0;
367 unsigned height = pt->height0;
368 unsigned nblocksx = util_format_get_nblocksx(pt->format, pt->width0);
369 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->height0);
371 /* used for scanouts that need special layouts */
372 if (tex->base.tex_usage & PIPE_TEXTURE_USAGE_SCANOUT)
373 if (i915_scanout_layout(tex))
376 /* shared buffers needs to be compatible with X servers */
377 if (tex->base.tex_usage & PIPE_TEXTURE_USAGE_SHARED)
378 if (i915_display_target_layout(tex))
381 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
383 /* May need to adjust pitch to accomodate the placement of
384 * the 2nd mipmap level. This occurs when the alignment
385 * constraints of mipmap placement push the right edge of the
386 * 2nd mipmap level out past the width of its parent.
388 if (pt->last_level > 0) {
389 unsigned mip1_nblocksx
390 = align(util_format_get_nblocksx(pt->format, u_minify(width, 1)), align_x)
391 + util_format_get_nblocksx(pt->format, u_minify(width, 2));
393 if (mip1_nblocksx > nblocksx)
394 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
397 /* Pitch must be a whole number of dwords
399 tex->stride = align(tex->stride, 64);
400 tex->total_nblocksy = 0;
402 for (level = 0; level <= pt->last_level; level++) {
403 i915_miptree_set_level_info(tex, level, 1, width, height, 1);
404 i915_miptree_set_image_offset(tex, level, 0, x, y);
406 nblocksy = align(nblocksy, align_y);
408 /* Because the images are packed better, the final offset
409 * might not be the maximal one:
411 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
413 /* Layout_below: step right after second mipmap level.
416 x += align(nblocksx, align_x);
422 width = u_minify(width, 1);
423 height = u_minify(height, 1);
424 nblocksx = util_format_get_nblocksx(pt->format, width);
425 nblocksy = util_format_get_nblocksy(pt->format, height);
430 i945_miptree_layout_3d(struct i915_texture *tex)
432 struct pipe_texture *pt = &tex->base;
433 unsigned width = pt->width0;
434 unsigned height = pt->height0;
435 unsigned depth = pt->depth0;
436 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
437 unsigned pack_x_pitch, pack_x_nr;
438 unsigned pack_y_pitch;
441 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
442 tex->total_nblocksy = 0;
444 pack_y_pitch = MAX2(nblocksy, 2);
445 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
448 for (level = 0; level <= pt->last_level; level++) {
453 i915_miptree_set_level_info(tex, level, depth, width, height, depth);
455 for (q = 0; q < depth;) {
456 for (j = 0; j < pack_x_nr && q < depth; j++, q++) {
457 i915_miptree_set_image_offset(tex, level, q, x, y + tex->total_nblocksy);
465 tex->total_nblocksy += y;
467 if (pack_x_pitch > 4) {
470 assert(pack_x_pitch * pack_x_nr * util_format_get_blocksize(pt->format) <= tex->stride);
473 if (pack_y_pitch > 2) {
477 width = u_minify(width, 1);
478 height = u_minify(height, 1);
479 depth = u_minify(depth, 1);
480 nblocksy = util_format_get_nblocksy(pt->format, height);
485 i945_miptree_layout_cube(struct i915_texture *tex)
487 struct pipe_texture *pt = &tex->base;
490 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
492 unsigned width = pt->width0;
493 unsigned height = pt->height0;
496 printf("%s %i, %i\n", __FUNCTION__, pt->width0, pt->height0);
499 assert(width == height); /* cubemap images are square */
502 * XXX Should only be used for compressed formats. But lets
503 * keep this code active just in case.
505 * Depending on the size of the largest images, pitch can be
506 * determined either by the old-style packing of cubemap faces,
507 * or the final row of 4x4, 2x2 and 1x1 faces below this.
510 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
512 tex->stride = 14 * 8 * util_format_get_blocksize(pt->format);
514 tex->total_nblocksy = nblocks * 4;
516 /* Set all the levels to effectively occupy the whole rectangular region.
518 for (level = 0; level <= pt->last_level; level++) {
519 i915_miptree_set_level_info(tex, level, 6, width, height, 1);
524 for (face = 0; face < 6; face++) {
525 unsigned x = initial_offsets[face][0] * nblocks;
526 unsigned y = initial_offsets[face][1] * nblocks;
527 unsigned d = nblocks;
529 #if 0 /* Fix and enable this code for compressed formats */
530 if (nblocks == 4 && face >= 4) {
531 y = tex->total_height - 4;
534 else if (nblocks < 4 && (face > 0)) {
535 y = tex->total_height - 4;
540 for (level = 0; level <= pt->last_level; level++) {
541 i915_miptree_set_image_offset(tex, level, face, x, y);
545 #if 0 /* Fix and enable this code for compressed formats */
549 case PIPE_TEX_FACE_POS_X:
550 case PIPE_TEX_FACE_NEG_X:
551 x += step_offsets[face][0] * d;
552 y += step_offsets[face][1] * d;
554 case PIPE_TEX_FACE_POS_Y:
555 case PIPE_TEX_FACE_NEG_Y:
559 case PIPE_TEX_FACE_POS_Z:
560 case PIPE_TEX_FACE_NEG_Z:
561 y = tex->total_height - 4;
566 y = tex->total_height - 4;
575 x += step_offsets[face][0] * d;
576 y += step_offsets[face][1] * d;
586 i945_miptree_layout(struct i915_texture * tex)
588 struct pipe_texture *pt = &tex->base;
590 switch (pt->target) {
591 case PIPE_TEXTURE_1D:
592 case PIPE_TEXTURE_2D:
593 i945_miptree_layout_2d(tex);
595 case PIPE_TEXTURE_3D:
596 i945_miptree_layout_3d(tex);
598 case PIPE_TEXTURE_CUBE:
599 i945_miptree_layout_cube(tex);
611 * Screen texture functions
615 static struct pipe_texture *
616 i915_texture_create(struct pipe_screen *screen,
617 const struct pipe_texture *templat)
619 struct i915_screen *is = i915_screen(screen);
620 struct i915_winsys *iws = is->iws;
621 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
623 unsigned buf_usage = 0;
628 tex->base = *templat;
629 pipe_reference_init(&tex->base.reference, 1);
630 tex->base.screen = screen;
633 if (!i945_miptree_layout(tex))
636 if (!i915_miptree_layout(tex))
640 tex_size = tex->stride * tex->total_nblocksy;
644 /* for scanouts and cursors, cursors arn't scanouts */
645 if (templat->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT && templat->width0 != 64)
646 buf_usage = I915_NEW_SCANOUT;
648 buf_usage = I915_NEW_TEXTURE;
650 tex->buffer = iws->buffer_create(iws, tex_size, 64, buf_usage);
654 /* setup any hw fences */
656 assert(tex->sw_tiled == I915_TILE_NONE);
657 iws->buffer_set_fence_reg(iws, tex->buffer, tex->stride, tex->hw_tiled);
662 void *ptr = ws->buffer_map(ws, tex->buffer,
663 PIPE_BUFFER_USAGE_CPU_WRITE);
664 memset(ptr, 0x80, tex_size);
665 ws->buffer_unmap(ws, tex->buffer);
675 static struct pipe_texture *
676 i915_texture_from_handle(struct pipe_screen * screen,
677 const struct pipe_texture *templat,
678 struct winsys_handle *whandle)
680 struct i915_screen *is = i915_screen(screen);
681 struct i915_texture *tex;
682 struct i915_winsys *iws = is->iws;
683 struct i915_winsys_buffer *buffer;
688 buffer = iws->buffer_from_handle(iws, whandle, &stride);
690 /* Only supports one type */
691 if (templat->target != PIPE_TEXTURE_2D ||
692 templat->last_level != 0 ||
693 templat->depth0 != 1) {
697 tex = CALLOC_STRUCT(i915_texture);
701 tex->base = *templat;
702 pipe_reference_init(&tex->base.reference, 1);
703 tex->base.screen = screen;
705 tex->stride = stride;
707 i915_miptree_set_level_info(tex, 0, 1, templat->width0, templat->height0, 1);
708 i915_miptree_set_image_offset(tex, 0, 0, 0, 0);
710 tex->buffer = buffer;
716 i915_texture_get_handle(struct pipe_screen * screen,
717 struct pipe_texture *texture,
718 struct winsys_handle *whandle)
720 struct i915_screen *is = i915_screen(screen);
721 struct i915_texture *tex = (struct i915_texture *)texture;
722 struct i915_winsys *iws = is->iws;
724 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
729 i915_texture_destroy(struct pipe_texture *pt)
731 struct i915_texture *tex = (struct i915_texture *)pt;
732 struct i915_winsys *iws = i915_screen(pt->screen)->iws;
736 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
739 iws->buffer_destroy(iws, tex->buffer);
741 for (i = 0; i < Elements(tex->image_offset); i++)
742 if (tex->image_offset[i])
743 FREE(tex->image_offset[i]);
750 * Screen surface functions
754 static struct pipe_surface *
755 i915_get_tex_surface(struct pipe_screen *screen,
756 struct pipe_texture *pt,
757 unsigned face, unsigned level, unsigned zslice,
760 struct i915_texture *tex = (struct i915_texture *)pt;
761 struct pipe_surface *ps;
762 unsigned offset; /* in bytes */
764 if (pt->target == PIPE_TEXTURE_CUBE) {
765 offset = tex->image_offset[level][face];
767 else if (pt->target == PIPE_TEXTURE_3D) {
768 offset = tex->image_offset[level][zslice];
771 offset = tex->image_offset[level][0];
776 ps = CALLOC_STRUCT(pipe_surface);
778 pipe_reference_init(&ps->reference, 1);
779 pipe_texture_reference(&ps->texture, pt);
780 ps->format = pt->format;
781 ps->width = u_minify(pt->width0, level);
782 ps->height = u_minify(pt->height0, level);
790 i915_tex_surface_destroy(struct pipe_surface *surf)
792 pipe_texture_reference(&surf->texture, NULL);
798 * Texture transfer functions
802 static struct pipe_transfer *
803 i915_get_tex_transfer(struct pipe_context *pipe,
804 struct pipe_texture *texture,
805 unsigned face, unsigned level, unsigned zslice,
806 enum pipe_transfer_usage usage, unsigned x, unsigned y,
807 unsigned w, unsigned h)
809 struct i915_texture *tex = (struct i915_texture *)texture;
810 struct i915_transfer *trans;
811 unsigned offset; /* in bytes */
813 if (texture->target == PIPE_TEXTURE_CUBE) {
814 offset = tex->image_offset[level][face];
816 else if (texture->target == PIPE_TEXTURE_3D) {
817 offset = tex->image_offset[level][zslice];
820 offset = tex->image_offset[level][0];
825 trans = CALLOC_STRUCT(i915_transfer);
827 pipe_texture_reference(&trans->base.texture, texture);
830 trans->base.width = w;
831 trans->base.height = h;
832 trans->base.stride = tex->stride;
833 trans->offset = offset;
834 trans->base.usage = usage;
840 i915_transfer_map(struct pipe_context *pipe,
841 struct pipe_transfer *transfer)
843 struct i915_texture *tex = (struct i915_texture *)transfer->texture;
844 struct i915_winsys *iws = i915_screen(tex->base.screen)->iws;
846 boolean write = FALSE;
847 enum pipe_format format = tex->base.format;
849 if (transfer->usage & PIPE_TRANSFER_WRITE)
852 map = iws->buffer_map(iws, tex->buffer, write);
856 return map + i915_transfer(transfer)->offset +
857 transfer->y / util_format_get_blockheight(format) * transfer->stride +
858 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
862 i915_transfer_unmap(struct pipe_context *pipe,
863 struct pipe_transfer *transfer)
865 struct i915_texture *tex = (struct i915_texture *)transfer->texture;
866 struct i915_winsys *iws = i915_screen(tex->base.screen)->iws;
867 iws->buffer_unmap(iws, tex->buffer);
871 i915_tex_transfer_destroy(struct pipe_context *pipe,
872 struct pipe_transfer *trans)
874 pipe_texture_reference(&trans->texture, NULL);
880 * Other texture functions
884 i915_init_texture_functions(struct i915_context *i915 )
886 i915->base.get_tex_transfer = i915_get_tex_transfer;
887 i915->base.transfer_map = i915_transfer_map;
888 i915->base.transfer_unmap = i915_transfer_unmap;
889 i915->base.tex_transfer_destroy = i915_tex_transfer_destroy;
893 i915_init_screen_texture_functions(struct i915_screen *is)
895 is->base.texture_create = i915_texture_create;
896 is->base.texture_from_handle = i915_texture_from_handle;
897 is->base.texture_get_handle = i915_texture_get_handle;
898 is->base.texture_destroy = i915_texture_destroy;
899 is->base.get_tex_surface = i915_get_tex_surface;
900 is->base.tex_surface_destroy = i915_tex_surface_destroy;