intel: Move separate-stencil s8 mapping logic to intel_miptree_map.
[mesa.git] / src / mesa / drivers / dri / intel / intel_mipmap_tree.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 #include "intel_batchbuffer.h"
29 #include "intel_context.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_regions.h"
32 #include "intel_resolve_map.h"
33 #include "intel_span.h"
34 #include "intel_tex_layout.h"
35 #include "intel_tex.h"
36 #include "intel_blit.h"
37
38 #include "main/enums.h"
39 #include "main/formats.h"
40 #include "main/image.h"
41 #include "main/teximage.h"
42
43 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
44
45 static GLenum
46 target_to_target(GLenum target)
47 {
48 switch (target) {
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
55 return GL_TEXTURE_CUBE_MAP_ARB;
56 default:
57 return target;
58 }
59 }
60
61 static struct intel_mipmap_tree *
62 intel_miptree_create_internal(struct intel_context *intel,
63 GLenum target,
64 gl_format format,
65 GLuint first_level,
66 GLuint last_level,
67 GLuint width0,
68 GLuint height0,
69 GLuint depth0)
70 {
71 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
72 int compress_byte = 0;
73
74 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
75 _mesa_lookup_enum_by_nr(target),
76 _mesa_get_format_name(format),
77 first_level, last_level, mt);
78
79 if (_mesa_is_format_compressed(format))
80 compress_byte = intel_compressed_num_bytes(format);
81
82 mt->target = target_to_target(target);
83 mt->format = format;
84 mt->first_level = first_level;
85 mt->last_level = last_level;
86 mt->width0 = width0;
87 mt->height0 = height0;
88 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
89 mt->compressed = compress_byte ? 1 : 0;
90 mt->refcount = 1;
91
92 intel_get_texture_alignment_unit(intel, format,
93 &mt->align_w, &mt->align_h);
94
95 if (target == GL_TEXTURE_CUBE_MAP) {
96 assert(depth0 == 1);
97 mt->depth0 = 6;
98 } else {
99 mt->depth0 = depth0;
100 }
101
102 if (format == MESA_FORMAT_S8) {
103 /* The stencil buffer has quirky pitch requirements. From Vol 2a,
104 * 11.5.6.2.1 3DSTATE_STENCIL_BUFFER, field "Surface Pitch":
105 * The pitch must be set to 2x the value computed based on width, as
106 * the stencil buffer is stored with two rows interleaved.
107 */
108 assert(intel->has_separate_stencil);
109 mt->cpp = 2;
110 }
111
112 #ifdef I915
113 (void) intel;
114 if (intel->is_945)
115 i945_miptree_layout(mt);
116 else
117 i915_miptree_layout(mt);
118 #else
119 brw_miptree_layout(intel, mt);
120 #endif
121
122 if (_mesa_is_depthstencil_format(_mesa_get_format_base_format(format)) &&
123 (intel->must_use_separate_stencil ||
124 (intel->has_separate_stencil &&
125 intel->vtbl.is_hiz_depth_format(intel, format)))) {
126 mt->stencil_mt = intel_miptree_create(intel,
127 mt->target,
128 MESA_FORMAT_S8,
129 mt->first_level,
130 mt->last_level,
131 mt->width0,
132 mt->height0,
133 mt->depth0,
134 true);
135 if (!mt->stencil_mt) {
136 intel_miptree_release(&mt);
137 return NULL;
138 }
139 }
140
141 return mt;
142 }
143
144
145 struct intel_mipmap_tree *
146 intel_miptree_create(struct intel_context *intel,
147 GLenum target,
148 gl_format format,
149 GLuint first_level,
150 GLuint last_level,
151 GLuint width0,
152 GLuint height0,
153 GLuint depth0,
154 bool expect_accelerated_upload)
155 {
156 struct intel_mipmap_tree *mt;
157 uint32_t tiling = I915_TILING_NONE;
158 GLenum base_format = _mesa_get_format_base_format(format);
159
160 if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
161 if (intel->gen >= 4 &&
162 (base_format == GL_DEPTH_COMPONENT ||
163 base_format == GL_DEPTH_STENCIL_EXT))
164 tiling = I915_TILING_Y;
165 else if (format == MESA_FORMAT_S8)
166 tiling = I915_TILING_NONE;
167 else if (width0 >= 64)
168 tiling = I915_TILING_X;
169 }
170
171 mt = intel_miptree_create_internal(intel, target, format,
172 first_level, last_level, width0,
173 height0, depth0);
174 /*
175 * pitch == 0 || height == 0 indicates the null texture
176 */
177 if (!mt || !mt->total_width || !mt->total_height) {
178 free(mt);
179 return NULL;
180 }
181
182 mt->region = intel_region_alloc(intel->intelScreen,
183 tiling,
184 mt->cpp,
185 mt->total_width,
186 mt->total_height,
187 expect_accelerated_upload);
188
189 if (!mt->region) {
190 free(mt);
191 return NULL;
192 }
193
194 return mt;
195 }
196
197
198 struct intel_mipmap_tree *
199 intel_miptree_create_for_region(struct intel_context *intel,
200 GLenum target,
201 gl_format format,
202 struct intel_region *region)
203 {
204 struct intel_mipmap_tree *mt;
205
206 mt = intel_miptree_create_internal(intel, target, format,
207 0, 0,
208 region->width, region->height, 1);
209 if (!mt)
210 return mt;
211
212 intel_region_reference(&mt->region, region);
213
214 return mt;
215 }
216
217 struct intel_mipmap_tree*
218 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
219 gl_format format,
220 uint32_t tiling,
221 uint32_t cpp,
222 uint32_t width,
223 uint32_t height)
224 {
225 struct intel_region *region;
226 struct intel_mipmap_tree *mt;
227
228 region = intel_region_alloc(intel->intelScreen,
229 tiling, cpp, width, height, true);
230 if (!region)
231 return NULL;
232
233 mt = intel_miptree_create_for_region(intel, GL_TEXTURE_2D, format, region);
234 intel_region_release(&region);
235 return mt;
236 }
237
238 void
239 intel_miptree_reference(struct intel_mipmap_tree **dst,
240 struct intel_mipmap_tree *src)
241 {
242 if (*dst == src)
243 return;
244
245 intel_miptree_release(dst);
246
247 if (src) {
248 src->refcount++;
249 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
250 }
251
252 *dst = src;
253 }
254
255
256 void
257 intel_miptree_release(struct intel_mipmap_tree **mt)
258 {
259 if (!*mt)
260 return;
261
262 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
263 if (--(*mt)->refcount <= 0) {
264 GLuint i;
265
266 DBG("%s deleting %p\n", __FUNCTION__, *mt);
267
268 intel_region_release(&((*mt)->region));
269 intel_miptree_release(&(*mt)->stencil_mt);
270 intel_miptree_release(&(*mt)->hiz_mt);
271 intel_resolve_map_clear(&(*mt)->hiz_map);
272
273 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
274 free((*mt)->level[i].slice);
275 }
276
277 free(*mt);
278 }
279 *mt = NULL;
280 }
281
282 void
283 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
284 int *width, int *height, int *depth)
285 {
286 switch (image->TexObject->Target) {
287 case GL_TEXTURE_1D_ARRAY:
288 *width = image->Width;
289 *height = 1;
290 *depth = image->Height;
291 break;
292 default:
293 *width = image->Width;
294 *height = image->Height;
295 *depth = image->Depth;
296 break;
297 }
298 }
299
300 /**
301 * Can the image be pulled into a unified mipmap tree? This mirrors
302 * the completeness test in a lot of ways.
303 *
304 * Not sure whether I want to pass gl_texture_image here.
305 */
306 bool
307 intel_miptree_match_image(struct intel_mipmap_tree *mt,
308 struct gl_texture_image *image)
309 {
310 struct intel_texture_image *intelImage = intel_texture_image(image);
311 GLuint level = intelImage->base.Base.Level;
312 int width, height, depth;
313
314 if (image->TexFormat != mt->format)
315 return false;
316
317 intel_miptree_get_dimensions_for_image(image, &width, &height, &depth);
318
319 /* Test image dimensions against the base level image adjusted for
320 * minification. This will also catch images not present in the
321 * tree, changed targets, etc.
322 */
323 if (width != mt->level[level].width ||
324 height != mt->level[level].height ||
325 depth != mt->level[level].depth)
326 return false;
327
328 return true;
329 }
330
331
332 void
333 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
334 GLuint level,
335 GLuint x, GLuint y,
336 GLuint w, GLuint h, GLuint d)
337 {
338 mt->level[level].width = w;
339 mt->level[level].height = h;
340 mt->level[level].depth = d;
341 mt->level[level].level_x = x;
342 mt->level[level].level_y = y;
343
344 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
345 level, w, h, d, x, y);
346
347 assert(mt->level[level].slice == NULL);
348
349 mt->level[level].slice = calloc(d, sizeof(*mt->level[0].slice));
350 mt->level[level].slice[0].x_offset = mt->level[level].level_x;
351 mt->level[level].slice[0].y_offset = mt->level[level].level_y;
352 }
353
354
355 void
356 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
357 GLuint level, GLuint img,
358 GLuint x, GLuint y)
359 {
360 if (img == 0 && level == 0)
361 assert(x == 0 && y == 0);
362
363 assert(img < mt->level[level].depth);
364
365 mt->level[level].slice[img].x_offset = mt->level[level].level_x + x;
366 mt->level[level].slice[img].y_offset = mt->level[level].level_y + y;
367
368 DBG("%s level %d img %d pos %d,%d\n",
369 __FUNCTION__, level, img,
370 mt->level[level].slice[img].x_offset,
371 mt->level[level].slice[img].y_offset);
372 }
373
374
375 /**
376 * For cube map textures, either the \c face parameter can be used, of course,
377 * or the cube face can be interpreted as a depth layer and the \c layer
378 * parameter used.
379 */
380 void
381 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
382 GLuint level, GLuint face, GLuint layer,
383 GLuint *x, GLuint *y)
384 {
385 int slice;
386
387 if (face > 0) {
388 assert(mt->target == GL_TEXTURE_CUBE_MAP);
389 assert(face < 6);
390 assert(layer == 0);
391 slice = face;
392 } else {
393 /* This branch may be taken even if the texture target is a cube map. In
394 * that case, the caller chose to interpret each cube face as a layer.
395 */
396 assert(face == 0);
397 slice = layer;
398 }
399
400 *x = mt->level[level].slice[slice].x_offset;
401 *y = mt->level[level].slice[slice].y_offset;
402 }
403
404 static void
405 intel_miptree_copy_slice(struct intel_context *intel,
406 struct intel_mipmap_tree *dst_mt,
407 struct intel_mipmap_tree *src_mt,
408 int level,
409 int face,
410 int depth)
411
412 {
413 gl_format format = src_mt->format;
414 uint32_t width = src_mt->level[level].width;
415 uint32_t height = src_mt->level[level].height;
416
417 assert(depth < src_mt->level[level].depth);
418
419 if (dst_mt->compressed) {
420 height = ALIGN(height, dst_mt->align_h) / dst_mt->align_h;
421 width = ALIGN(width, dst_mt->align_w);
422 }
423
424 uint32_t dst_x, dst_y, src_x, src_y;
425 intel_miptree_get_image_offset(dst_mt, level, face, depth,
426 &dst_x, &dst_y);
427 intel_miptree_get_image_offset(src_mt, level, face, depth,
428 &src_x, &src_y);
429
430 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
431 src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
432 dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
433 width, height);
434
435 if (!intelEmitCopyBlit(intel,
436 dst_mt->region->cpp,
437 src_mt->region->pitch, src_mt->region->bo,
438 0, src_mt->region->tiling,
439 dst_mt->region->pitch, dst_mt->region->bo,
440 0, dst_mt->region->tiling,
441 src_x, src_y,
442 dst_x, dst_y,
443 width, height,
444 GL_COPY)) {
445
446 fallback_debug("miptree validate blit for %s failed\n",
447 _mesa_get_format_name(format));
448 void *dst = intel_region_map(intel, dst_mt->region, GL_MAP_WRITE_BIT);
449 void *src = intel_region_map(intel, src_mt->region, GL_MAP_READ_BIT);
450
451 _mesa_copy_rect(dst,
452 dst_mt->cpp,
453 dst_mt->region->pitch,
454 dst_x, dst_y,
455 width, height,
456 src, src_mt->region->pitch,
457 src_x, src_y);
458
459 intel_region_unmap(intel, dst_mt->region);
460 intel_region_unmap(intel, src_mt->region);
461 }
462
463 if (src_mt->stencil_mt) {
464 intel_miptree_copy_slice(intel,
465 dst_mt->stencil_mt, src_mt->stencil_mt,
466 level, face, depth);
467 }
468 }
469
470 /**
471 * Copies the image's current data to the given miptree, and associates that
472 * miptree with the image.
473 */
474 void
475 intel_miptree_copy_teximage(struct intel_context *intel,
476 struct intel_texture_image *intelImage,
477 struct intel_mipmap_tree *dst_mt)
478 {
479 struct intel_mipmap_tree *src_mt = intelImage->mt;
480 int level = intelImage->base.Base.Level;
481 int face = intelImage->base.Base.Face;
482 GLuint depth = intelImage->base.Base.Depth;
483
484 for (int slice = 0; slice < depth; slice++) {
485 intel_miptree_copy_slice(intel, dst_mt, src_mt, level, face, slice);
486 }
487
488 intel_miptree_reference(&intelImage->mt, dst_mt);
489 }
490
491 /**
492 * \param scatter Scatter if true. Gather if false.
493 *
494 * \see intel_miptree_s8z24_scatter()
495 * \see intel_miptree_s8z24_gather()
496 */
497 static void
498 intel_miptree_s8z24_scattergather(struct intel_context *intel,
499 struct intel_mipmap_tree *mt,
500 uint32_t level,
501 uint32_t layer,
502 bool scatter)
503 {
504 /* Check function inputs. */
505 assert(level >= mt->first_level);
506 assert(level <= mt->last_level);
507 assert(layer < mt->level[level].depth);
508
509 /* Label everything and its bit layout, just to make the code easier to
510 * read.
511 */
512 struct intel_mipmap_tree *s8_mt = mt->stencil_mt;
513 struct intel_mipmap_level *s8_level = &s8_mt->level[level];
514 struct intel_mipmap_slice *s8_slice = &s8_mt->level[level].slice[layer];
515
516 struct intel_mipmap_tree *s8z24_mt = mt;
517 struct intel_mipmap_level *s8z24_level = &s8z24_mt->level[level];
518 struct intel_mipmap_slice *s8z24_slice = &s8z24_mt->level[level].slice[layer];
519
520 /* Check that both miptree levels have the same dimensions. */
521 assert(s8_level->width == s8z24_level->width);
522 assert(s8_level->height == s8z24_level->height);
523 assert(s8_level->depth == s8z24_level->depth);
524
525 /* Map the buffers. */
526 if (drm_intel_bo_references(intel->batch.bo, s8_mt->region->bo) ||
527 drm_intel_bo_references(intel->batch.bo, s8z24_mt->region->bo)) {
528 intel_batchbuffer_flush(intel);
529 }
530 drm_intel_gem_bo_map_gtt(s8_mt->region->bo);
531 drm_intel_gem_bo_map_gtt(s8z24_mt->region->bo);
532
533 /* Define the invariant values outside the for loop, because I don't trust
534 * GCC to do it for us.
535 */
536 uint8_t *s8_map = s8_mt->region->bo->virtual
537 + s8_slice->x_offset
538 + s8_slice->y_offset;
539
540 uint8_t *s8z24_map = s8z24_mt->region->bo->virtual
541 + s8z24_slice->x_offset
542 + s8z24_slice->y_offset;
543
544 ptrdiff_t s8z24_stride = s8z24_mt->region->pitch * s8z24_mt->region->cpp;
545
546 uint32_t w = s8_level->width;
547 uint32_t h = s8_level->height;
548
549 for (uint32_t y = 0; y < h; ++y) {
550 for (uint32_t x = 0; x < w; ++x) {
551 ptrdiff_t s8_offset = intel_offset_S8(s8_mt->region->pitch, x, y);
552 ptrdiff_t s8z24_offset = y * s8z24_stride
553 + x * 4
554 + 3;
555 if (scatter) {
556 s8_map[s8_offset] = s8z24_map[s8z24_offset];
557 } else {
558 s8z24_map[s8z24_offset] = s8_map[s8_offset];
559 }
560 }
561 }
562
563 drm_intel_gem_bo_unmap_gtt(s8_mt->region->bo);
564 drm_intel_gem_bo_unmap_gtt(s8z24_mt->region->bo);
565 }
566
567 void
568 intel_miptree_s8z24_scatter(struct intel_context *intel,
569 struct intel_mipmap_tree *mt,
570 uint32_t level,
571 uint32_t layer)
572 {
573 intel_miptree_s8z24_scattergather(intel, mt, level, layer, true);
574 }
575
576 void
577 intel_miptree_s8z24_gather(struct intel_context *intel,
578 struct intel_mipmap_tree *mt,
579 uint32_t level,
580 uint32_t layer)
581 {
582 intel_miptree_s8z24_scattergather(intel, mt, level, layer, false);
583 }
584
585 bool
586 intel_miptree_alloc_hiz(struct intel_context *intel,
587 struct intel_mipmap_tree *mt)
588 {
589 assert(mt->hiz_mt == NULL);
590 mt->hiz_mt = intel_miptree_create(intel,
591 mt->target,
592 MESA_FORMAT_X8_Z24,
593 mt->first_level,
594 mt->last_level,
595 mt->width0,
596 mt->height0,
597 mt->depth0,
598 true);
599
600 if (!mt->hiz_mt)
601 return false;
602
603 /* Mark that all slices need a HiZ resolve. */
604 struct intel_resolve_map *head = &mt->hiz_map;
605 for (int level = mt->first_level; level <= mt->last_level; ++level) {
606 for (int layer = 0; layer < mt->level[level].depth; ++layer) {
607 head->next = malloc(sizeof(*head->next));
608 head->next->prev = head;
609 head->next->next = NULL;
610 head = head->next;
611
612 head->level = level;
613 head->layer = layer;
614 head->need = INTEL_NEED_HIZ_RESOLVE;
615 }
616 }
617
618 return true;
619 }
620
621 void
622 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
623 uint32_t level,
624 uint32_t layer)
625 {
626 intel_miptree_check_level_layer(mt, level, layer);
627
628 if (!mt->hiz_mt)
629 return;
630
631 intel_resolve_map_set(&mt->hiz_map,
632 level, layer, INTEL_NEED_HIZ_RESOLVE);
633 }
634
635
636 void
637 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
638 uint32_t level,
639 uint32_t layer)
640 {
641 intel_miptree_check_level_layer(mt, level, layer);
642
643 if (!mt->hiz_mt)
644 return;
645
646 intel_resolve_map_set(&mt->hiz_map,
647 level, layer, INTEL_NEED_DEPTH_RESOLVE);
648 }
649
650 typedef void (*resolve_func_t)(struct intel_context *intel,
651 struct intel_mipmap_tree *mt,
652 uint32_t level,
653 uint32_t layer);
654
655 static bool
656 intel_miptree_slice_resolve(struct intel_context *intel,
657 struct intel_mipmap_tree *mt,
658 uint32_t level,
659 uint32_t layer,
660 enum intel_need_resolve need,
661 resolve_func_t func)
662 {
663 intel_miptree_check_level_layer(mt, level, layer);
664
665 struct intel_resolve_map *item =
666 intel_resolve_map_get(&mt->hiz_map, level, layer);
667
668 if (!item || item->need != need)
669 return false;
670
671 func(intel, mt, level, layer);
672 intel_resolve_map_remove(item);
673 return true;
674 }
675
676 bool
677 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
678 struct intel_mipmap_tree *mt,
679 uint32_t level,
680 uint32_t layer)
681 {
682 return intel_miptree_slice_resolve(intel, mt, level, layer,
683 INTEL_NEED_HIZ_RESOLVE,
684 intel->vtbl.resolve_hiz_slice);
685 }
686
687 bool
688 intel_miptree_slice_resolve_depth(struct intel_context *intel,
689 struct intel_mipmap_tree *mt,
690 uint32_t level,
691 uint32_t layer)
692 {
693 return intel_miptree_slice_resolve(intel, mt, level, layer,
694 INTEL_NEED_DEPTH_RESOLVE,
695 intel->vtbl.resolve_depth_slice);
696 }
697
698 static bool
699 intel_miptree_all_slices_resolve(struct intel_context *intel,
700 struct intel_mipmap_tree *mt,
701 enum intel_need_resolve need,
702 resolve_func_t func)
703 {
704 bool did_resolve = false;
705 struct intel_resolve_map *i;
706
707 for (i = mt->hiz_map.next; i; i = i->next) {
708 if (i->need != need)
709 continue;
710 func(intel, mt, i->level, i->layer);
711 intel_resolve_map_remove(i);
712 did_resolve = true;
713 }
714
715 return did_resolve;
716 }
717
718 bool
719 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
720 struct intel_mipmap_tree *mt)
721 {
722 return intel_miptree_all_slices_resolve(intel, mt,
723 INTEL_NEED_HIZ_RESOLVE,
724 intel->vtbl.resolve_hiz_slice);
725 }
726
727 bool
728 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
729 struct intel_mipmap_tree *mt)
730 {
731 return intel_miptree_all_slices_resolve(intel, mt,
732 INTEL_NEED_DEPTH_RESOLVE,
733 intel->vtbl.resolve_depth_slice);
734 }
735
736 static void
737 intel_miptree_map_gtt(struct intel_context *intel,
738 struct intel_mipmap_tree *mt,
739 struct intel_miptree_map *map,
740 unsigned int level, unsigned int slice)
741 {
742 unsigned int bw, bh;
743 void *base;
744 unsigned int image_x, image_y;
745 int x = map->x;
746 int y = map->y;
747
748 if (mt->stencil_mt) {
749 /* The miptree has depthstencil format, but uses separate stencil. The
750 * embedded stencil miptree contains the real stencil data, so gather
751 * that into the depthstencil miptree.
752 *
753 * FIXME: Avoid the gather if the texture is mapped as write-only.
754 */
755 intel_miptree_s8z24_gather(intel, mt, level, slice);
756 }
757
758 /* For compressed formats, the stride is the number of bytes per
759 * row of blocks. intel_miptree_get_image_offset() already does
760 * the divide.
761 */
762 _mesa_get_format_block_size(mt->format, &bw, &bh);
763 assert(y % bh == 0);
764 y /= bh;
765
766 base = intel_region_map(intel, mt->region, map->mode);
767 /* Note that in the case of cube maps, the caller must have passed the slice
768 * number referencing the face.
769 */
770 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
771 x += image_x;
772 y += image_y;
773
774 map->stride = mt->region->pitch * mt->cpp;
775 map->ptr = base + y * map->stride + x * mt->cpp;
776
777 DBG("%s: %d,%d %dx%d from mt %p (%s) %d,%d = %p/%d\n", __FUNCTION__,
778 map->x, map->y, map->w, map->h,
779 mt, _mesa_get_format_name(mt->format),
780 x, y, map->ptr, map->stride);
781 }
782
783 static void
784 intel_miptree_unmap_gtt(struct intel_context *intel,
785 struct intel_mipmap_tree *mt,
786 struct intel_miptree_map *map,
787 unsigned int level,
788 unsigned int slice)
789 {
790 intel_region_unmap(intel, mt->region);
791
792 if (mt->stencil_mt) {
793 /* The miptree has depthstencil format, but uses separate stencil. The
794 * embedded stencil miptree must contain the real stencil data after
795 * unmapping, so copy it from the depthstencil miptree into the stencil
796 * miptree.
797 *
798 * FIXME: Avoid the scatter if the texture was mapped as read-only.
799 */
800 intel_miptree_s8z24_scatter(intel, mt, level, slice);
801 }
802 }
803
804 static void
805 intel_miptree_map_s8(struct intel_context *intel,
806 struct intel_mipmap_tree *mt,
807 struct intel_miptree_map *map,
808 unsigned int level, unsigned int slice)
809 {
810 map->stride = map->w;
811 map->buffer = map->ptr = malloc(map->stride * map->h);
812 if (!map->buffer)
813 return;
814
815 /* One of either READ_BIT or WRITE_BIT or both is set. READ_BIT implies no
816 * INVALIDATE_RANGE_BIT. WRITE_BIT needs the original values read in unless
817 * invalidate is set, since we'll be writing the whole rectangle from our
818 * temporary buffer back out.
819 */
820 if (!(map->mode & GL_MAP_INVALIDATE_RANGE_BIT)) {
821 uint8_t *untiled_s8_map = map->ptr;
822 uint8_t *tiled_s8_map = intel_region_map(intel, mt->region,
823 GL_MAP_READ_BIT);
824 unsigned int image_x, image_y;
825
826 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
827
828 for (uint32_t y = 0; y < map->h; y++) {
829 for (uint32_t x = 0; x < map->w; x++) {
830 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
831 x + image_x + map->x,
832 y + image_y + map->y);
833 untiled_s8_map[y * map->w + x] = tiled_s8_map[offset];
834 }
835 }
836
837 intel_region_unmap(intel, mt->region);
838
839 DBG("%s: %d,%d %dx%d from mt %p %d,%d = %p/%d\n", __FUNCTION__,
840 map->x, map->y, map->w, map->h,
841 mt, map->x + image_x, map->y + image_y, map->ptr, map->stride);
842 } else {
843 DBG("%s: %d,%d %dx%d from mt %p = %p/%d\n", __FUNCTION__,
844 map->x, map->y, map->w, map->h,
845 mt, map->ptr, map->stride);
846 }
847 }
848
849 static void
850 intel_miptree_unmap_s8(struct intel_context *intel,
851 struct intel_mipmap_tree *mt,
852 struct intel_miptree_map *map,
853 unsigned int level,
854 unsigned int slice)
855 {
856 if (map->mode & GL_MAP_WRITE_BIT) {
857 unsigned int image_x, image_y;
858 uint8_t *untiled_s8_map = map->ptr;
859 uint8_t *tiled_s8_map = intel_region_map(intel, mt->region, map->mode);
860
861 intel_miptree_get_image_offset(mt, level, 0, slice, &image_x, &image_y);
862
863 for (uint32_t y = 0; y < map->h; y++) {
864 for (uint32_t x = 0; x < map->w; x++) {
865 ptrdiff_t offset = intel_offset_S8(mt->region->pitch,
866 x + map->x,
867 y + map->y);
868 tiled_s8_map[offset] = untiled_s8_map[y * map->w + x];
869 }
870 }
871
872 intel_region_unmap(intel, mt->region);
873 }
874
875 free(map->buffer);
876 }
877
878 void
879 intel_miptree_map(struct intel_context *intel,
880 struct intel_mipmap_tree *mt,
881 unsigned int level,
882 unsigned int slice,
883 unsigned int x,
884 unsigned int y,
885 unsigned int w,
886 unsigned int h,
887 GLbitfield mode,
888 void **out_ptr,
889 int *out_stride)
890 {
891 struct intel_miptree_map *map;
892
893 map = calloc(1, sizeof(struct intel_miptree_map));
894 if (!map){
895 *out_ptr = NULL;
896 *out_stride = 0;
897 return;
898 }
899
900 assert(!mt->level[level].slice[slice].map);
901 mt->level[level].slice[slice].map = map;
902 map->mode = mode;
903 map->x = x;
904 map->y = y;
905 map->w = w;
906 map->h = h;
907
908 intel_miptree_slice_resolve_depth(intel, mt, level, slice);
909 if (map->mode & GL_MAP_WRITE_BIT) {
910 intel_miptree_slice_set_needs_hiz_resolve(mt, level, slice);
911 }
912
913 if (mt->format == MESA_FORMAT_S8) {
914 intel_miptree_map_s8(intel, mt, map, level, slice);
915 } else {
916 intel_miptree_map_gtt(intel, mt, map, level, slice);
917 }
918
919 *out_ptr = map->ptr;
920 *out_stride = map->stride;
921 }
922
923 void
924 intel_miptree_unmap(struct intel_context *intel,
925 struct intel_mipmap_tree *mt,
926 unsigned int level,
927 unsigned int slice)
928 {
929 struct intel_miptree_map *map = mt->level[level].slice[slice].map;
930
931 if (!map)
932 return;
933
934 DBG("%s: mt %p (%s) level %d slice %d\n", __FUNCTION__,
935 mt, _mesa_get_format_name(mt->format), level, slice);
936
937 if (mt->format == MESA_FORMAT_S8) {
938 intel_miptree_unmap_s8(intel, mt, map, level, slice);
939 } else {
940 intel_miptree_unmap_gtt(intel, mt, map, level, slice);
941 }
942
943 mt->level[level].slice[slice].map = NULL;
944 free(map);
945 }