Merge branch '7.8'
[mesa.git] / src / gallium / drivers / i915 / i915_texture.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 * Michel Dänzer <michel@tungstengraphics.com>
31 */
32
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"
40
41 #include "i915_context.h"
42 #include "i915_texture.h"
43 #include "i915_screen.h"
44 #include "i915_winsys.h"
45
46
47 /*
48 * Helper function and arrays
49 */
50
51
52 /**
53 * Initial offset for Cube map.
54 */
55 static const int initial_offsets[6][2] = {
56 {0, 0},
57 {0, 2},
58 {1, 0},
59 {1, 2},
60 {1, 1},
61 {1, 3}
62 };
63
64 /**
65 * Step offsets for Cube map.
66 */
67 static const int step_offsets[6][2] = {
68 {0, 2},
69 {0, 2},
70 {-1, 2},
71 {-1, 2},
72 {-1, 1},
73 {-1, 1}
74 };
75
76 /* XXX really need twice the size if x is already pot?
77 Otherwise just use util_next_power_of_two?
78 */
79 static unsigned
80 power_of_two(unsigned x)
81 {
82 unsigned value = 1;
83 while (value < x)
84 value = value << 1;
85 return value;
86 }
87
88 /*
89 * More advanced helper funcs
90 */
91
92
93 static void
94 i915_miptree_set_level_info(struct i915_texture *tex,
95 unsigned level,
96 unsigned nr_images,
97 unsigned w, unsigned h, unsigned d)
98 {
99 assert(level < Elements(tex->nr_images));
100
101 tex->nr_images[level] = nr_images;
102
103 /*
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]);
106 */
107
108 /* Not sure when this would happen, but anyway:
109 */
110 if (tex->image_offset[level]) {
111 FREE(tex->image_offset[level]);
112 tex->image_offset[level] = NULL;
113 }
114
115 assert(nr_images);
116 assert(!tex->image_offset[level]);
117
118 tex->image_offset[level] = (unsigned *) MALLOC(nr_images * sizeof(unsigned));
119 tex->image_offset[level][0] = 0;
120 }
121
122 static void
123 i915_miptree_set_image_offset(struct i915_texture *tex,
124 unsigned level, unsigned img, unsigned x, unsigned y)
125 {
126 if (img == 0 && level == 0)
127 assert(x == 0 && y == 0);
128
129 assert(img < tex->nr_images[level]);
130
131 tex->image_offset[level][img] = y * tex->stride + x * util_format_get_blocksize(tex->base.format);
132
133 /*
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]);
136 */
137 }
138
139
140 /*
141 * i915 layout functions, some used by i945
142 */
143
144
145 /**
146 * Special case to deal with scanout textures.
147 */
148 static boolean
149 i915_scanout_layout(struct i915_texture *tex)
150 {
151 struct pipe_texture *pt = &tex->base;
152
153 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
154 return FALSE;
155
156 i915_miptree_set_level_info(tex, 0, 1,
157 pt->width0,
158 pt->height0,
159 1);
160 i915_miptree_set_image_offset(tex, 0, 0, 0, 0);
161
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);
169 } else {
170 return FALSE;
171 }
172
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);
176
177 return TRUE;
178 }
179
180 /**
181 * Special case to deal with shared textures.
182 */
183 static boolean
184 i915_display_target_layout(struct i915_texture *tex)
185 {
186 struct pipe_texture *pt = &tex->base;
187
188 if (pt->last_level > 0 || util_format_get_blocksize(pt->format) != 4)
189 return FALSE;
190
191 /* fallback to normal textures for small textures */
192 if (pt->width0 < 240)
193 return FALSE;
194
195 i915_miptree_set_level_info(tex, 0, 1,
196 pt->width0,
197 pt->height0,
198 1);
199 i915_miptree_set_image_offset(tex, 0, 0, 0, 0);
200
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;
204
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);
208
209 return TRUE;
210 }
211
212 static void
213 i915_miptree_layout_2d(struct i915_texture *tex)
214 {
215 struct pipe_texture *pt = &tex->base;
216 unsigned level;
217 unsigned width = pt->width0;
218 unsigned height = pt->height0;
219 unsigned nblocksy = util_format_get_nblocksy(pt->format, pt->width0);
220
221 /* used for scanouts that need special layouts */
222 if (pt->tex_usage & PIPE_TEXTURE_USAGE_SCANOUT)
223 if (i915_scanout_layout(tex))
224 return;
225
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))
229 return;
230
231 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
232 tex->total_nblocksy = 0;
233
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);
237
238 nblocksy = align(MAX2(2, nblocksy), 2);
239
240 tex->total_nblocksy += nblocksy;
241
242 width = u_minify(width, 1);
243 height = u_minify(height, 1);
244 nblocksy = util_format_get_nblocksy(pt->format, height);
245 }
246 }
247
248 static void
249 i915_miptree_layout_3d(struct i915_texture *tex)
250 {
251 struct pipe_texture *pt = &tex->base;
252 unsigned level;
253
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;
259
260 /* Calculate the size of a single slice.
261 */
262 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
263
264 /* XXX: hardware expects/requires 9 levels at minimum.
265 */
266 for (level = 0; level <= MAX2(8, pt->last_level); level++) {
267 i915_miptree_set_level_info(tex, level, depth, width, height, depth);
268
269 stack_nblocksy += MAX2(2, nblocksy);
270
271 width = u_minify(width, 1);
272 height = u_minify(height, 1);
273 nblocksy = util_format_get_nblocksy(pt->format, height);
274 }
275
276 /* Fixup depth image_offsets:
277 */
278 for (level = 0; level <= pt->last_level; level++) {
279 unsigned i;
280 for (i = 0; i < depth; i++)
281 i915_miptree_set_image_offset(tex, level, i, 0, i * stack_nblocksy);
282
283 depth = u_minify(depth, 1);
284 }
285
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.
289 */
290 tex->total_nblocksy = stack_nblocksy * pt->depth0;
291 }
292
293 static void
294 i915_miptree_layout_cube(struct i915_texture *tex)
295 {
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);
299 unsigned level;
300 unsigned face;
301
302 assert(width == height); /* cubemap images are square */
303
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;
307
308 for (level = 0; level <= pt->last_level; level++) {
309 i915_miptree_set_level_info(tex, level, 6, width, height, 1);
310 width /= 2;
311 height /= 2;
312 }
313
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;
318
319 for (level = 0; level <= pt->last_level; level++) {
320 i915_miptree_set_image_offset(tex, level, face, x, y);
321 d >>= 1;
322 x += step_offsets[face][0] * d;
323 y += step_offsets[face][1] * d;
324 }
325 }
326 }
327
328 static boolean
329 i915_miptree_layout(struct i915_texture * tex)
330 {
331 struct pipe_texture *pt = &tex->base;
332
333 switch (pt->target) {
334 case PIPE_TEXTURE_1D:
335 case PIPE_TEXTURE_2D:
336 i915_miptree_layout_2d(tex);
337 break;
338 case PIPE_TEXTURE_3D:
339 i915_miptree_layout_3d(tex);
340 break;
341 case PIPE_TEXTURE_CUBE:
342 i915_miptree_layout_cube(tex);
343 break;
344 default:
345 assert(0);
346 return FALSE;
347 }
348
349 return TRUE;
350 }
351
352
353 /*
354 * i945 layout functions
355 */
356
357
358 static void
359 i945_miptree_layout_2d(struct i915_texture *tex)
360 {
361 struct pipe_texture *pt = &tex->base;
362 const int align_x = 2, align_y = 4;
363 unsigned level;
364 unsigned x = 0;
365 unsigned y = 0;
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);
370
371 /* used for scanouts that need special layouts */
372 if (tex->base.tex_usage & PIPE_TEXTURE_USAGE_SCANOUT)
373 if (i915_scanout_layout(tex))
374 return;
375
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))
379 return;
380
381 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
382
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.
387 */
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));
392
393 if (mip1_nblocksx > nblocksx)
394 tex->stride = mip1_nblocksx * util_format_get_blocksize(pt->format);
395 }
396
397 /* Pitch must be a whole number of dwords
398 */
399 tex->stride = align(tex->stride, 64);
400 tex->total_nblocksy = 0;
401
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);
405
406 nblocksy = align(nblocksy, align_y);
407
408 /* Because the images are packed better, the final offset
409 * might not be the maximal one:
410 */
411 tex->total_nblocksy = MAX2(tex->total_nblocksy, y + nblocksy);
412
413 /* Layout_below: step right after second mipmap level.
414 */
415 if (level == 1) {
416 x += align(nblocksx, align_x);
417 }
418 else {
419 y += nblocksy;
420 }
421
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);
426 }
427 }
428
429 static void
430 i945_miptree_layout_3d(struct i915_texture *tex)
431 {
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;
439 unsigned level;
440
441 tex->stride = align(util_format_get_stride(pt->format, pt->width0), 4);
442 tex->total_nblocksy = 0;
443
444 pack_y_pitch = MAX2(nblocksy, 2);
445 pack_x_pitch = tex->stride / util_format_get_blocksize(pt->format);
446 pack_x_nr = 1;
447
448 for (level = 0; level <= pt->last_level; level++) {
449 int x = 0;
450 int y = 0;
451 unsigned q, j;
452
453 i915_miptree_set_level_info(tex, level, depth, width, height, depth);
454
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);
458 x += pack_x_pitch;
459 }
460
461 x = 0;
462 y += pack_y_pitch;
463 }
464
465 tex->total_nblocksy += y;
466
467 if (pack_x_pitch > 4) {
468 pack_x_pitch >>= 1;
469 pack_x_nr <<= 1;
470 assert(pack_x_pitch * pack_x_nr * util_format_get_blocksize(pt->format) <= tex->stride);
471 }
472
473 if (pack_y_pitch > 2) {
474 pack_y_pitch >>= 1;
475 }
476
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);
481 }
482 }
483
484 static void
485 i945_miptree_layout_cube(struct i915_texture *tex)
486 {
487 struct pipe_texture *pt = &tex->base;
488 unsigned level;
489
490 const unsigned nblocks = util_format_get_nblocksx(pt->format, pt->width0);
491 unsigned face;
492 unsigned width = pt->width0;
493 unsigned height = pt->height0;
494
495 /*
496 printf("%s %i, %i\n", __FUNCTION__, pt->width0, pt->height0);
497 */
498
499 assert(width == height); /* cubemap images are square */
500
501 /*
502 * XXX Should only be used for compressed formats. But lets
503 * keep this code active just in case.
504 *
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.
508 */
509 if (nblocks > 32)
510 tex->stride = align(nblocks * util_format_get_blocksize(pt->format) * 2, 4);
511 else
512 tex->stride = 14 * 8 * util_format_get_blocksize(pt->format);
513
514 tex->total_nblocksy = nblocks * 4;
515
516 /* Set all the levels to effectively occupy the whole rectangular region.
517 */
518 for (level = 0; level <= pt->last_level; level++) {
519 i915_miptree_set_level_info(tex, level, 6, width, height, 1);
520 width /= 2;
521 height /= 2;
522 }
523
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;
528
529 #if 0 /* Fix and enable this code for compressed formats */
530 if (nblocks == 4 && face >= 4) {
531 y = tex->total_height - 4;
532 x = (face - 4) * 8;
533 }
534 else if (nblocks < 4 && (face > 0)) {
535 y = tex->total_height - 4;
536 x = face * 8;
537 }
538 #endif
539
540 for (level = 0; level <= pt->last_level; level++) {
541 i915_miptree_set_image_offset(tex, level, face, x, y);
542
543 d >>= 1;
544
545 #if 0 /* Fix and enable this code for compressed formats */
546 switch (d) {
547 case 4:
548 switch (face) {
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;
553 break;
554 case PIPE_TEX_FACE_POS_Y:
555 case PIPE_TEX_FACE_NEG_Y:
556 y += 12;
557 x -= 8;
558 break;
559 case PIPE_TEX_FACE_POS_Z:
560 case PIPE_TEX_FACE_NEG_Z:
561 y = tex->total_height - 4;
562 x = (face - 4) * 8;
563 break;
564 }
565 case 2:
566 y = tex->total_height - 4;
567 x = 16 + face * 8;
568 break;
569
570 case 1:
571 x += 48;
572 break;
573 default:
574 #endif
575 x += step_offsets[face][0] * d;
576 y += step_offsets[face][1] * d;
577 #if 0
578 break;
579 }
580 #endif
581 }
582 }
583 }
584
585 static boolean
586 i945_miptree_layout(struct i915_texture * tex)
587 {
588 struct pipe_texture *pt = &tex->base;
589
590 switch (pt->target) {
591 case PIPE_TEXTURE_1D:
592 case PIPE_TEXTURE_2D:
593 i945_miptree_layout_2d(tex);
594 break;
595 case PIPE_TEXTURE_3D:
596 i945_miptree_layout_3d(tex);
597 break;
598 case PIPE_TEXTURE_CUBE:
599 i945_miptree_layout_cube(tex);
600 break;
601 default:
602 assert(0);
603 return FALSE;
604 }
605
606 return TRUE;
607 }
608
609
610 /*
611 * Screen texture functions
612 */
613
614
615 static struct pipe_texture *
616 i915_texture_create(struct pipe_screen *screen,
617 const struct pipe_texture *templat)
618 {
619 struct i915_screen *is = i915_screen(screen);
620 struct i915_winsys *iws = is->iws;
621 struct i915_texture *tex = CALLOC_STRUCT(i915_texture);
622 size_t tex_size;
623 unsigned buf_usage = 0;
624
625 if (!tex)
626 return NULL;
627
628 tex->base = *templat;
629 pipe_reference_init(&tex->base.reference, 1);
630 tex->base.screen = screen;
631
632 if (is->is_i945) {
633 if (!i945_miptree_layout(tex))
634 goto fail;
635 } else {
636 if (!i915_miptree_layout(tex))
637 goto fail;
638 }
639
640 tex_size = tex->stride * tex->total_nblocksy;
641
642
643
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;
647 else
648 buf_usage = I915_NEW_TEXTURE;
649
650 tex->buffer = iws->buffer_create(iws, tex_size, 64, buf_usage);
651 if (!tex->buffer)
652 goto fail;
653
654 /* setup any hw fences */
655 if (tex->hw_tiled) {
656 assert(tex->sw_tiled == I915_TILE_NONE);
657 iws->buffer_set_fence_reg(iws, tex->buffer, tex->stride, tex->hw_tiled);
658 }
659
660
661 #if 0
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);
666 #endif
667
668 return &tex->base;
669
670 fail:
671 FREE(tex);
672 return NULL;
673 }
674
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)
679 {
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;
684 unsigned stride;
685
686 assert(screen);
687
688 buffer = iws->buffer_from_handle(iws, whandle, &stride);
689
690 /* Only supports one type */
691 if (templat->target != PIPE_TEXTURE_2D ||
692 templat->last_level != 0 ||
693 templat->depth0 != 1) {
694 return NULL;
695 }
696
697 tex = CALLOC_STRUCT(i915_texture);
698 if (!tex)
699 return NULL;
700
701 tex->base = *templat;
702 pipe_reference_init(&tex->base.reference, 1);
703 tex->base.screen = screen;
704
705 tex->stride = stride;
706
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);
709
710 tex->buffer = buffer;
711
712 return &tex->base;
713 }
714
715 static boolean
716 i915_texture_get_handle(struct pipe_screen * screen,
717 struct pipe_texture *texture,
718 struct winsys_handle *whandle)
719 {
720 struct i915_screen *is = i915_screen(screen);
721 struct i915_texture *tex = (struct i915_texture *)texture;
722 struct i915_winsys *iws = is->iws;
723
724 return iws->buffer_get_handle(iws, tex->buffer, whandle, tex->stride);
725 }
726
727
728 static void
729 i915_texture_destroy(struct pipe_texture *pt)
730 {
731 struct i915_texture *tex = (struct i915_texture *)pt;
732 struct i915_winsys *iws = i915_screen(pt->screen)->iws;
733 uint i;
734
735 /*
736 DBG("%s deleting %p\n", __FUNCTION__, (void *) tex);
737 */
738
739 iws->buffer_destroy(iws, tex->buffer);
740
741 for (i = 0; i < Elements(tex->image_offset); i++)
742 if (tex->image_offset[i])
743 FREE(tex->image_offset[i]);
744
745 FREE(tex);
746 }
747
748
749 /*
750 * Screen surface functions
751 */
752
753
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,
758 unsigned flags)
759 {
760 struct i915_texture *tex = (struct i915_texture *)pt;
761 struct pipe_surface *ps;
762 unsigned offset; /* in bytes */
763
764 if (pt->target == PIPE_TEXTURE_CUBE) {
765 offset = tex->image_offset[level][face];
766 }
767 else if (pt->target == PIPE_TEXTURE_3D) {
768 offset = tex->image_offset[level][zslice];
769 }
770 else {
771 offset = tex->image_offset[level][0];
772 assert(face == 0);
773 assert(zslice == 0);
774 }
775
776 ps = CALLOC_STRUCT(pipe_surface);
777 if (ps) {
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);
783 ps->offset = offset;
784 ps->usage = flags;
785 }
786 return ps;
787 }
788
789 static void
790 i915_tex_surface_destroy(struct pipe_surface *surf)
791 {
792 pipe_texture_reference(&surf->texture, NULL);
793 FREE(surf);
794 }
795
796
797 /*
798 * Texture transfer functions
799 */
800
801
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)
808 {
809 struct i915_texture *tex = (struct i915_texture *)texture;
810 struct i915_transfer *trans;
811 unsigned offset; /* in bytes */
812
813 if (texture->target == PIPE_TEXTURE_CUBE) {
814 offset = tex->image_offset[level][face];
815 }
816 else if (texture->target == PIPE_TEXTURE_3D) {
817 offset = tex->image_offset[level][zslice];
818 }
819 else {
820 offset = tex->image_offset[level][0];
821 assert(face == 0);
822 assert(zslice == 0);
823 }
824
825 trans = CALLOC_STRUCT(i915_transfer);
826 if (trans) {
827 pipe_texture_reference(&trans->base.texture, texture);
828 trans->base.x = x;
829 trans->base.y = y;
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;
835 }
836 return &trans->base;
837 }
838
839 static void *
840 i915_transfer_map(struct pipe_context *pipe,
841 struct pipe_transfer *transfer)
842 {
843 struct i915_texture *tex = (struct i915_texture *)transfer->texture;
844 struct i915_winsys *iws = i915_screen(tex->base.screen)->iws;
845 char *map;
846 boolean write = FALSE;
847 enum pipe_format format = tex->base.format;
848
849 if (transfer->usage & PIPE_TRANSFER_WRITE)
850 write = TRUE;
851
852 map = iws->buffer_map(iws, tex->buffer, write);
853 if (map == NULL)
854 return NULL;
855
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);
859 }
860
861 static void
862 i915_transfer_unmap(struct pipe_context *pipe,
863 struct pipe_transfer *transfer)
864 {
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);
868 }
869
870 static void
871 i915_tex_transfer_destroy(struct pipe_context *pipe,
872 struct pipe_transfer *trans)
873 {
874 pipe_texture_reference(&trans->texture, NULL);
875 FREE(trans);
876 }
877
878
879 /*
880 * Other texture functions
881 */
882
883 void
884 i915_init_texture_functions(struct i915_context *i915 )
885 {
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;
890 }
891
892 void
893 i915_init_screen_texture_functions(struct i915_screen *is)
894 {
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;
901 }