1e43bbecb06e5f31b717bdb3d998e28ea301565d
[mesa.git] / src / mesa / drivers / dri / i915 / intel_mipmap_tree.h
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 #ifndef INTEL_MIPMAP_TREE_H
29 #define INTEL_MIPMAP_TREE_H
30
31 #include <assert.h>
32
33 #include "intel_regions.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* A layer on top of the intel_regions code which adds:
40 *
41 * - Code to size and layout a region to hold a set of mipmaps.
42 * - Query to determine if a new image fits in an existing tree.
43 * - More refcounting
44 * - maybe able to remove refcounting from intel_region?
45 * - ?
46 *
47 * The fixed mipmap layout of intel hardware where one offset
48 * specifies the position of all images in a mipmap hierachy
49 * complicates the implementation of GL texture image commands,
50 * compared to hardware where each image is specified with an
51 * independent offset.
52 *
53 * In an ideal world, each texture object would be associated with a
54 * single bufmgr buffer or 2d intel_region, and all the images within
55 * the texture object would slot into the tree as they arrive. The
56 * reality can be a little messier, as images can arrive from the user
57 * with sizes that don't fit in the existing tree, or in an order
58 * where the tree layout cannot be guessed immediately.
59 *
60 * This structure encodes an idealized mipmap tree. The GL image
61 * commands build these where possible, otherwise store the images in
62 * temporary system buffers.
63 */
64
65 struct intel_texture_image;
66
67 /**
68 * When calling intel_miptree_map() on an ETC-transcoded-to-RGB miptree or a
69 * depthstencil-split-to-separate-stencil miptree, we'll normally make a
70 * tmeporary and recreate the kind of data requested by Mesa core, since we're
71 * satisfying some glGetTexImage() request or something.
72 *
73 * However, occasionally you want to actually map the miptree's current data
74 * without transcoding back. This flag to intel_miptree_map() gets you that.
75 */
76 #define BRW_MAP_DIRECT_BIT 0x80000000
77
78 struct intel_miptree_map {
79 /** Bitfield of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_BIT */
80 GLbitfield mode;
81 /** Region of interest for the map. */
82 int x, y, w, h;
83 /** Possibly malloced temporary buffer for the mapping. */
84 void *buffer;
85 /** Possible pointer to a temporary linear miptree for the mapping. */
86 struct intel_mipmap_tree *mt;
87 /** Pointer to the start of (map_x, map_y) returned by the mapping. */
88 void *ptr;
89 /** Stride of the mapping. */
90 int stride;
91
92 /**
93 * intel_mipmap_tree::singlesample_mt is temporary storage that persists
94 * only for the duration of the map.
95 */
96 bool singlesample_mt_is_tmp;
97 };
98
99 /**
100 * Describes the location of each texture image within a texture region.
101 */
102 struct intel_mipmap_level
103 {
104 /** Offset to this miptree level, used in computing x_offset. */
105 GLuint level_x;
106 /** Offset to this miptree level, used in computing y_offset. */
107 GLuint level_y;
108 GLuint width;
109 GLuint height;
110
111 /**
112 * \brief Number of 2D slices in this miplevel.
113 *
114 * The exact semantics of depth varies according to the texture target:
115 * - For GL_TEXTURE_CUBE_MAP, depth is 6.
116 * - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
117 * identical for all miplevels in the texture.
118 * - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
119 * value, like width and height, varies with miplevel.
120 * - For other texture types, depth is 1.
121 */
122 GLuint depth;
123
124 /**
125 * \brief List of 2D images in this mipmap level.
126 *
127 * This may be a list of cube faces, array slices in 2D array texture, or
128 * layers in a 3D texture. The list's length is \c depth.
129 */
130 struct intel_mipmap_slice {
131 /**
132 * \name Offset to slice
133 * \{
134 *
135 * Hardware formats are so diverse that that there is no unified way to
136 * compute the slice offsets, so we store them in this table.
137 *
138 * The (x, y) offset to slice \c s at level \c l relative the miptrees
139 * base address is
140 * \code
141 * x = mt->level[l].slice[s].x_offset
142 * y = mt->level[l].slice[s].y_offset
143 */
144 GLuint x_offset;
145 GLuint y_offset;
146 /** \} */
147
148 /**
149 * Mapping information. Persistent for the duration of
150 * intel_miptree_map/unmap on this slice.
151 */
152 struct intel_miptree_map *map;
153 } *slice;
154 };
155
156 /**
157 * Enum for keeping track of the different MSAA layouts supported by Gen7.
158 */
159 enum intel_msaa_layout
160 {
161 /**
162 * Ordinary surface with no MSAA.
163 */
164 INTEL_MSAA_LAYOUT_NONE,
165
166 /**
167 * Interleaved Multisample Surface. The additional samples are
168 * accommodated by scaling up the width and the height of the surface so
169 * that all the samples corresponding to a pixel are located at nearby
170 * memory locations.
171 */
172 INTEL_MSAA_LAYOUT_IMS,
173
174 /**
175 * Uncompressed Multisample Surface. The surface is stored as a 2D array,
176 * with array slice n containing all pixel data for sample n.
177 */
178 INTEL_MSAA_LAYOUT_UMS,
179
180 /**
181 * Compressed Multisample Surface. The surface is stored as in
182 * INTEL_MSAA_LAYOUT_UMS, but there is an additional buffer called the MCS
183 * (Multisample Control Surface) buffer. Each pixel in the MCS buffer
184 * indicates the mapping from sample number to array slice. This allows
185 * the common case (where all samples constituting a pixel have the same
186 * color value) to be stored efficiently by just using a single array
187 * slice.
188 */
189 INTEL_MSAA_LAYOUT_CMS,
190 };
191
192
193 #ifndef I915
194 /**
195 * Enum for keeping track of the state of an MCS buffer associated with a
196 * miptree. This determines when fast clear related operations are needed.
197 *
198 * Fast clear works by deferring the memory writes that would be used to clear
199 * the buffer, so that instead of performing them at the time of the clear
200 * operation, the hardware automatically performs them at the time that the
201 * buffer is later accessed for rendering. The MCS buffer keeps track of
202 * which regions of the buffer still have pending clear writes.
203 *
204 * This enum keeps track of the driver's knowledge of the state of the MCS
205 * buffer.
206 *
207 * MCS buffers only exist on Gen7+.
208 */
209 enum intel_mcs_state
210 {
211 /**
212 * There is no MCS buffer for this miptree, and one should never be
213 * allocated.
214 */
215 INTEL_MCS_STATE_NONE,
216
217 /**
218 * An MCS buffer exists for this miptree, and it is used for MSAA purposes.
219 */
220 INTEL_MCS_STATE_MSAA,
221
222 /**
223 * No deferred clears are pending for this miptree, and the contents of the
224 * color buffer are entirely correct. An MCS buffer may or may not exist
225 * for this miptree. If it does exist, it is entirely in the "no deferred
226 * clears pending" state. If it does not exist, it will be created the
227 * first time a fast color clear is executed.
228 *
229 * In this state, the color buffer can be used for purposes other than
230 * rendering without needing a render target resolve.
231 */
232 INTEL_MCS_STATE_RESOLVED,
233
234 /**
235 * An MCS buffer exists for this miptree, and deferred clears are pending
236 * for some regions of the color buffer, as indicated by the MCS buffer.
237 * The contents of the color buffer are only correct for the regions where
238 * the MCS buffer doesn't indicate a deferred clear.
239 *
240 * In this state, a render target resolve must be performed before the
241 * color buffer can be used for purposes other than rendering.
242 */
243 INTEL_MCS_STATE_UNRESOLVED,
244
245 /**
246 * An MCS buffer exists for this miptree, and deferred clears are pending
247 * for the entire color buffer, and the contents of the MCS buffer reflect
248 * this. The contents of the color buffer are undefined.
249 *
250 * In this state, a render target resolve must be performed before the
251 * color buffer can be used for purposes other than rendering.
252 *
253 * If the client attempts to clear a buffer which is already in this state,
254 * the clear can be safely skipped, since the buffer is already clear.
255 */
256 INTEL_MCS_STATE_CLEAR,
257 };
258 #endif
259
260 struct intel_mipmap_tree
261 {
262 /* Effectively the key:
263 */
264 GLenum target;
265
266 /**
267 * Generally, this is just the same as the gl_texture_image->TexFormat or
268 * gl_renderbuffer->Format.
269 *
270 * However, for textures and renderbuffers with packed depth/stencil formats
271 * on hardware where we want or need to use separate stencil, there will be
272 * two miptrees for storing the data. If the depthstencil texture or rb is
273 * MESA_FORMAT_Z32_FLOAT_X24S8, then mt->format will be
274 * MESA_FORMAT_Z32_FLOAT, otherwise for MESA_FORMAT_S8_Z24 objects it will be
275 * MESA_FORMAT_X8_Z24.
276 *
277 * For ETC1/ETC2 textures, this is one of the uncompressed mesa texture
278 * formats if the hardware lacks support for ETC1/ETC2. See @ref wraps_etc.
279 */
280 gl_format format;
281
282 /** This variable stores the value of ETC compressed texture format */
283 gl_format etc_format;
284
285 /**
286 * The X offset of each image in the miptree must be aligned to this. See
287 * the "Alignment Unit Size" section of the BSpec.
288 */
289 unsigned int align_w;
290 unsigned int align_h; /**< \see align_w */
291
292 GLuint first_level;
293 GLuint last_level;
294
295 /**
296 * Level zero image dimensions. These dimensions correspond to the
297 * physical layout of data in memory. Accordingly, they account for the
298 * extra width, height, and or depth that must be allocated in order to
299 * accommodate multisample formats, and they account for the extra factor
300 * of 6 in depth that must be allocated in order to accommodate cubemap
301 * textures.
302 */
303 GLuint physical_width0, physical_height0, physical_depth0;
304
305 GLuint cpp;
306 GLuint num_samples;
307 bool compressed;
308
309 /**
310 * Level zero image dimensions. These dimensions correspond to the
311 * logical width, height, and depth of the region as seen by client code.
312 * Accordingly, they do not account for the extra width, height, and/or
313 * depth that must be allocated in order to accommodate multisample
314 * formats, nor do they account for the extra factor of 6 in depth that
315 * must be allocated in order to accommodate cubemap textures.
316 */
317 uint32_t logical_width0, logical_height0, logical_depth0;
318
319 /**
320 * For 1D array, 2D array, cube, and 2D multisampled surfaces on Gen7: true
321 * if the surface only contains LOD 0, and hence no space is for LOD's
322 * other than 0 in between array slices.
323 *
324 * Corresponds to the surface_array_spacing bit in gen7_surface_state.
325 */
326 bool array_spacing_lod0;
327
328 /**
329 * MSAA layout used by this buffer.
330 */
331 enum intel_msaa_layout msaa_layout;
332
333 /* Derived from the above:
334 */
335 GLuint total_width;
336 GLuint total_height;
337
338 /* Includes image offset tables:
339 */
340 struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
341
342 /* The data is held here:
343 */
344 struct intel_region *region;
345
346 /* Offset into region bo where miptree starts:
347 */
348 uint32_t offset;
349
350 /**
351 * \brief Singlesample miptree.
352 *
353 * This is used under two cases.
354 *
355 * --- Case 1: As persistent singlesample storage for multisample window
356 * system front and back buffers ---
357 *
358 * Suppose that the window system FBO was created with a multisample
359 * config. Let `back_irb` be the `intel_renderbuffer` for the FBO's back
360 * buffer. Then `back_irb` contains two miptrees: a parent multisample
361 * miptree (back_irb->mt) and a child singlesample miptree
362 * (back_irb->mt->singlesample_mt). The DRM buffer shared with DRI2
363 * belongs to `back_irb->mt->singlesample_mt` and contains singlesample
364 * data. The singlesample miptree is created at the same time as and
365 * persists for the lifetime of its parent multisample miptree.
366 *
367 * When access to the singlesample data is needed, such as at
368 * eglSwapBuffers and glReadPixels, an automatic downsample occurs from
369 * `back_rb->mt` to `back_rb->mt->singlesample_mt` when necessary.
370 *
371 * This description of the back buffer applies analogously to the front
372 * buffer.
373 *
374 *
375 * --- Case 2: As temporary singlesample storage for mapping multisample
376 * miptrees ---
377 *
378 * Suppose the intel_miptree_map is called on a multisample miptree, `mt`,
379 * for which case 1 does not apply (that is, `mt` does not belong to
380 * a front or back buffer). Then `mt->singlesample_mt` is null at the
381 * start of the call. intel_miptree_map will create a temporary
382 * singlesample miptree, store it at `mt->singlesample_mt`, downsample from
383 * `mt` to `mt->singlesample_mt` if necessary, then map
384 * `mt->singlesample_mt`. The temporary miptree is later deleted during
385 * intel_miptree_unmap.
386 */
387 struct intel_mipmap_tree *singlesample_mt;
388
389 /**
390 * \brief A downsample is needed from this miptree to singlesample_mt.
391 */
392 bool need_downsample;
393
394 /**
395 * \brief Stencil miptree for depthstencil textures.
396 *
397 * This miptree is used for depthstencil textures and renderbuffers that
398 * require separate stencil. It always has the true copy of the stencil
399 * bits, regardless of mt->format.
400 *
401 * \see intel_miptree_map_depthstencil()
402 * \see intel_miptree_unmap_depthstencil()
403 */
404 struct intel_mipmap_tree *stencil_mt;
405
406 #ifndef I915
407 /**
408 * \brief MCS miptree.
409 *
410 * This miptree contains the "multisample control surface", which stores
411 * the necessary information to implement compressed MSAA
412 * (INTEL_MSAA_FORMAT_CMS) and "fast color clear" behaviour on Gen7+.
413 *
414 * NULL if no MCS miptree is in use for this surface.
415 */
416 struct intel_mipmap_tree *mcs_mt;
417
418 /**
419 * MCS state for this buffer.
420 */
421 enum intel_mcs_state mcs_state;
422 #endif
423
424 /**
425 * The SURFACE_STATE bits associated with the last fast color clear to this
426 * color mipmap tree, if any.
427 *
428 * This value will only ever contain ones in bits 28-31, so it is safe to
429 * OR into dword 7 of SURFACE_STATE.
430 */
431 uint32_t fast_clear_color_value;
432
433 /* These are also refcounted:
434 */
435 GLuint refcount;
436 };
437
438 enum intel_miptree_tiling_mode {
439 INTEL_MIPTREE_TILING_ANY,
440 INTEL_MIPTREE_TILING_Y,
441 INTEL_MIPTREE_TILING_NONE,
442 };
443
444 bool
445 intel_is_non_msrt_mcs_buffer_supported(struct intel_context *intel,
446 struct intel_mipmap_tree *mt);
447
448 void
449 intel_get_non_msrt_mcs_alignment(struct intel_context *intel,
450 struct intel_mipmap_tree *mt,
451 unsigned *width_px, unsigned *height);
452
453 bool
454 intel_miptree_alloc_non_msrt_mcs(struct intel_context *intel,
455 struct intel_mipmap_tree *mt);
456
457 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
458 GLenum target,
459 gl_format format,
460 GLuint first_level,
461 GLuint last_level,
462 GLuint width0,
463 GLuint height0,
464 GLuint depth0,
465 bool expect_accelerated_upload,
466 GLuint num_samples,
467 enum intel_miptree_tiling_mode);
468
469 struct intel_mipmap_tree *
470 intel_miptree_create_layout(struct intel_context *intel,
471 GLenum target,
472 gl_format format,
473 GLuint first_level,
474 GLuint last_level,
475 GLuint width0,
476 GLuint height0,
477 GLuint depth0,
478 bool for_bo,
479 GLuint num_samples);
480
481 struct intel_mipmap_tree *
482 intel_miptree_create_for_bo(struct intel_context *intel,
483 drm_intel_bo *bo,
484 gl_format format,
485 uint32_t offset,
486 uint32_t width,
487 uint32_t height,
488 int pitch,
489 uint32_t tiling);
490
491 struct intel_mipmap_tree*
492 intel_miptree_create_for_dri2_buffer(struct intel_context *intel,
493 unsigned dri_attachment,
494 gl_format format,
495 uint32_t num_samples,
496 struct intel_region *region);
497
498 /**
499 * Create a miptree appropriate as the storage for a non-texture renderbuffer.
500 * The miptree has the following properties:
501 * - The target is GL_TEXTURE_2D.
502 * - There are no levels other than the base level 0.
503 * - Depth is 1.
504 */
505 struct intel_mipmap_tree*
506 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
507 gl_format format,
508 uint32_t width,
509 uint32_t height,
510 uint32_t num_samples);
511
512 /** \brief Assert that the level and layer are valid for the miptree. */
513 static inline void
514 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
515 uint32_t level,
516 uint32_t layer)
517 {
518 assert(level >= mt->first_level);
519 assert(level <= mt->last_level);
520 assert(layer < mt->level[level].depth);
521 }
522
523 int intel_miptree_pitch_align (struct intel_context *intel,
524 struct intel_mipmap_tree *mt,
525 uint32_t tiling,
526 int pitch);
527
528 void intel_miptree_reference(struct intel_mipmap_tree **dst,
529 struct intel_mipmap_tree *src);
530
531 void intel_miptree_release(struct intel_mipmap_tree **mt);
532
533 /* Check if an image fits an existing mipmap tree layout
534 */
535 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
536 struct gl_texture_image *image);
537
538 void
539 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
540 GLuint level, GLuint slice,
541 GLuint *x, GLuint *y);
542
543 void
544 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
545 int *width, int *height, int *depth);
546
547 uint32_t
548 intel_miptree_get_tile_offsets(struct intel_mipmap_tree *mt,
549 GLuint level, GLuint slice,
550 uint32_t *tile_x,
551 uint32_t *tile_y);
552
553 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
554 GLuint level,
555 GLuint x, GLuint y,
556 GLuint w, GLuint h, GLuint d);
557
558 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
559 GLuint level,
560 GLuint img, GLuint x, GLuint y);
561
562 void
563 intel_miptree_copy_teximage(struct intel_context *intel,
564 struct intel_texture_image *intelImage,
565 struct intel_mipmap_tree *dst_mt, bool invalidate);
566
567 /**
568 * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
569 * the given miptree slice.
570 *
571 * \see intel_mipmap_tree::stencil_mt
572 */
573 void
574 intel_miptree_s8z24_scatter(struct intel_context *intel,
575 struct intel_mipmap_tree *mt,
576 uint32_t level,
577 uint32_t slice);
578
579 /**
580 * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
581 * given miptree slice.
582 *
583 * \see intel_mipmap_tree::stencil_mt
584 */
585 void
586 intel_miptree_s8z24_gather(struct intel_context *intel,
587 struct intel_mipmap_tree *mt,
588 uint32_t level,
589 uint32_t layer);
590
591 bool
592 intel_miptree_alloc_mcs(struct intel_context *intel,
593 struct intel_mipmap_tree *mt,
594 GLuint num_samples);
595
596 /**\}*/
597
598 /**
599 * Update the fast clear state for a miptree to indicate that it has been used
600 * for rendering.
601 */
602 static inline void
603 intel_miptree_used_for_rendering(struct intel_mipmap_tree *mt)
604 {
605 #ifdef I915
606 /* Nothing needs to be done for I915, since it doesn't support fast
607 * clear.
608 */
609 #else
610 /* If the buffer was previously in fast clear state, change it to
611 * unresolved state, since it won't be guaranteed to be clear after
612 * rendering occurs.
613 */
614 if (mt->mcs_state == INTEL_MCS_STATE_CLEAR)
615 mt->mcs_state = INTEL_MCS_STATE_UNRESOLVED;
616 #endif
617 }
618
619 void
620 intel_miptree_resolve_color(struct intel_context *intel,
621 struct intel_mipmap_tree *mt);
622
623 void
624 intel_miptree_make_shareable(struct intel_context *intel,
625 struct intel_mipmap_tree *mt);
626
627 void
628 intel_miptree_downsample(struct intel_context *intel,
629 struct intel_mipmap_tree *mt);
630
631 void
632 intel_miptree_upsample(struct intel_context *intel,
633 struct intel_mipmap_tree *mt);
634
635 /* i915_mipmap_tree.c:
636 */
637 void i915_miptree_layout(struct intel_mipmap_tree *mt);
638 void i945_miptree_layout(struct intel_mipmap_tree *mt);
639 void brw_miptree_layout(struct intel_context *intel,
640 struct intel_mipmap_tree *mt);
641
642 void *intel_miptree_map_raw(struct intel_context *intel,
643 struct intel_mipmap_tree *mt);
644
645 void intel_miptree_unmap_raw(struct intel_context *intel,
646 struct intel_mipmap_tree *mt);
647
648 void
649 intel_miptree_map(struct intel_context *intel,
650 struct intel_mipmap_tree *mt,
651 unsigned int level,
652 unsigned int slice,
653 unsigned int x,
654 unsigned int y,
655 unsigned int w,
656 unsigned int h,
657 GLbitfield mode,
658 void **out_ptr,
659 int *out_stride);
660
661 void
662 intel_miptree_unmap(struct intel_context *intel,
663 struct intel_mipmap_tree *mt,
664 unsigned int level,
665 unsigned int slice);
666
667
668 #ifdef __cplusplus
669 }
670 #endif
671
672 #endif