i965/msaa: Add an enum to describe MSAA layout.
[profile/ivi/mesa.git] / src / mesa / drivers / dri / intel / 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 #include "intel_resolve_map.h"
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 /* A layer on top of the intel_regions code which adds:
41  *
42  * - Code to size and layout a region to hold a set of mipmaps.
43  * - Query to determine if a new image fits in an existing tree.
44  * - More refcounting 
45  *     - maybe able to remove refcounting from intel_region?
46  * - ?
47  *
48  * The fixed mipmap layout of intel hardware where one offset
49  * specifies the position of all images in a mipmap hierachy
50  * complicates the implementation of GL texture image commands,
51  * compared to hardware where each image is specified with an
52  * independent offset.
53  *
54  * In an ideal world, each texture object would be associated with a
55  * single bufmgr buffer or 2d intel_region, and all the images within
56  * the texture object would slot into the tree as they arrive.  The
57  * reality can be a little messier, as images can arrive from the user
58  * with sizes that don't fit in the existing tree, or in an order
59  * where the tree layout cannot be guessed immediately.  
60  * 
61  * This structure encodes an idealized mipmap tree.  The GL image
62  * commands build these where possible, otherwise store the images in
63  * temporary system buffers.
64  */
65
66 struct intel_resolve_map;
67 struct intel_texture_image;
68
69 struct intel_miptree_map {
70    /** Bitfield of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT, GL_MAP_INVALIDATE_BIT */
71    GLbitfield mode;
72    /** Region of interest for the map. */
73    int x, y, w, h;
74    /** Possibly malloced temporary buffer for the mapping. */
75    void *buffer;
76    /** Possible pointer to a BO temporary for the mapping. */
77    drm_intel_bo *bo;
78    /** Pointer to the start of (map_x, map_y) returned by the mapping. */
79    void *ptr;
80    /** Stride of the mapping. */
81    int stride;
82 };
83
84 /**
85  * Describes the location of each texture image within a texture region.
86  */
87 struct intel_mipmap_level
88 {
89    /** Offset to this miptree level, used in computing x_offset. */
90    GLuint level_x;
91    /** Offset to this miptree level, used in computing y_offset. */
92    GLuint level_y;
93    GLuint width;
94    GLuint height;
95
96    /**
97     * \brief Number of 2D slices in this miplevel.
98     *
99     * The exact semantics of depth varies according to the texture target:
100     *    - For GL_TEXTURE_CUBE_MAP, depth is 6.
101     *    - For GL_TEXTURE_2D_ARRAY, depth is the number of array slices. It is
102     *      identical for all miplevels in the texture.
103     *    - For GL_TEXTURE_3D, it is the texture's depth at this miplevel. Its
104     *      value, like width and height, varies with miplevel.
105     *    - For other texture types, depth is 1.
106     */
107    GLuint depth;
108
109    /**
110     * \brief List of 2D images in this mipmap level.
111     *
112     * This may be a list of cube faces, array slices in 2D array texture, or
113     * layers in a 3D texture. The list's length is \c depth.
114     */
115    struct intel_mipmap_slice {
116       /**
117        * \name Offset to slice
118        * \{
119        *
120        * Hardware formats are so diverse that that there is no unified way to
121        * compute the slice offsets, so we store them in this table.
122        *
123        * The (x, y) offset to slice \c s at level \c l relative the miptrees
124        * base address is
125        * \code
126        *     x = mt->level[l].slice[s].x_offset
127        *     y = mt->level[l].slice[s].y_offset
128        */
129       GLuint x_offset;
130       GLuint y_offset;
131       /** \} */
132
133       /**
134        * Pointer to mapping information, present across
135        * intel_tex_image_map()/unmap of the slice.
136        */
137       struct intel_miptree_map *map;
138    } *slice;
139 };
140
141 /**
142  * Enum for keeping track of the different MSAA layouts supported by Gen7.
143  */
144 enum intel_msaa_layout
145 {
146    /**
147     * Ordinary surface with no MSAA.
148     */
149    INTEL_MSAA_LAYOUT_NONE,
150
151    /**
152     * Interleaved Multisample Surface.  The additional samples are
153     * accommodated by scaling up the width and the height of the surface so
154     * that all the samples corresponding to a pixel are located at nearby
155     * memory locations.
156     */
157    INTEL_MSAA_LAYOUT_IMS,
158
159    /**
160     * Uncompressed Multisample Surface.  The surface is stored as a 2D array,
161     * with array slice n containing all pixel data for sample n.
162     */
163    INTEL_MSAA_LAYOUT_UMS,
164
165    /**
166     * Compressed Multisample Surface.  The surface is stored as in
167     * INTEL_MSAA_LAYOUT_UMS, but there is an additional buffer called the MCS
168     * (Multisample Control Surface) buffer.  Each pixel in the MCS buffer
169     * indicates the mapping from sample number to array slice.  This allows
170     * the common case (where all samples constituting a pixel have the same
171     * color value) to be stored efficiently by just using a single array
172     * slice.
173     */
174    INTEL_MSAA_LAYOUT_CMS,
175 };
176
177 struct intel_mipmap_tree
178 {
179    /* Effectively the key:
180     */
181    GLenum target;
182
183    /**
184     * Generally, this is just the same as the gl_texture_image->TexFormat or
185     * gl_renderbuffer->Format.
186     *
187     * However, for textures and renderbuffers with packed depth/stencil formats
188     * on hardware where we want or need to use separate stencil, there will be
189     * two miptrees for storing the data.  If the depthstencil texture or rb is
190     * MESA_FORMAT_Z32_FLOAT_X24S8, then mt->format will be
191     * MESA_FORMAT_Z32_FLOAT, otherwise for MESA_FORMAT_S8_Z24 objects it will be
192     * MESA_FORMAT_X8_Z24.
193     */
194    gl_format format;
195
196    /**
197     * The X offset of each image in the miptree must be aligned to this. See
198     * the "Alignment Unit Size" section of the BSpec.
199     */
200    unsigned int align_w;
201    unsigned int align_h; /**< \see align_w */
202
203    GLuint first_level;
204    GLuint last_level;
205
206    GLuint width0, height0, depth0; /**< Level zero image dimensions */
207    GLuint cpp;
208    GLuint num_samples;
209    bool compressed;
210
211    /**
212     * For 1D array, 2D array, cube, and 2D multisampled surfaces on Gen7: true
213     * if the surface only contains LOD 0, and hence no space is for LOD's
214     * other than 0 in between array slices.
215     *
216     * Corresponds to the surface_array_spacing bit in gen7_surface_state.
217     */
218    bool array_spacing_lod0;
219
220    /**
221     * MSAA layout used by this buffer.
222     */
223    enum intel_msaa_layout msaa_layout;
224
225    /* Derived from the above:
226     */
227    GLuint total_width;
228    GLuint total_height;
229
230    /* The 3DSTATE_CLEAR_PARAMS value associated with the last depth clear to
231     * this depth mipmap tree, if any.
232     */
233    uint32_t depth_clear_value;
234
235    /* Includes image offset tables:
236     */
237    struct intel_mipmap_level level[MAX_TEXTURE_LEVELS];
238
239    /* The data is held here:
240     */
241    struct intel_region *region;
242
243    /* Offset into region bo where miptree starts:
244     */
245    uint32_t offset;
246
247    /**
248     * \brief HiZ miptree
249     *
250     * This is non-null only if HiZ is enabled for this miptree.
251     *
252     * \see intel_miptree_alloc_hiz()
253     */
254    struct intel_mipmap_tree *hiz_mt;
255
256    /**
257     * \brief Map of miptree slices to needed resolves.
258     *
259     * This is used only when the miptree has a child HiZ miptree.
260     *
261     * Let \c mt be a depth miptree with HiZ enabled. Then the resolve map is
262     * \c mt->hiz_map. The resolve map of the child HiZ miptree, \c
263     * mt->hiz_mt->hiz_map, is unused.
264     */
265    struct intel_resolve_map hiz_map;
266
267    /**
268     * \brief Stencil miptree for depthstencil textures.
269     *
270     * This miptree is used for depthstencil textures and renderbuffers that
271     * require separate stencil.  It always has the true copy of the stencil
272     * bits, regardless of mt->format.
273     *
274     * \see intel_miptree_map_depthstencil()
275     * \see intel_miptree_unmap_depthstencil()
276     */
277    struct intel_mipmap_tree *stencil_mt;
278
279    /* These are also refcounted:
280     */
281    GLuint refcount;
282 };
283
284
285
286 struct intel_mipmap_tree *intel_miptree_create(struct intel_context *intel,
287                                                GLenum target,
288                                                gl_format format,
289                                                GLuint first_level,
290                                                GLuint last_level,
291                                                GLuint width0,
292                                                GLuint height0,
293                                                GLuint depth0,
294                                                bool expect_accelerated_upload,
295                                                GLuint num_samples,
296                                                enum intel_msaa_layout msaa_layout);
297
298 struct intel_mipmap_tree *
299 intel_miptree_create_for_region(struct intel_context *intel,
300                                 GLenum target,
301                                 gl_format format,
302                                 struct intel_region *region);
303
304 /**
305  * Create a miptree appropriate as the storage for a non-texture renderbuffer.
306  * The miptree has the following properties:
307  *     - The target is GL_TEXTURE_2D.
308  *     - There are no levels other than the base level 0.
309  *     - Depth is 1.
310  */
311 struct intel_mipmap_tree*
312 intel_miptree_create_for_renderbuffer(struct intel_context *intel,
313                                       gl_format format,
314                                       uint32_t width,
315                                       uint32_t height,
316                                       uint32_t num_samples);
317
318 /** \brief Assert that the level and layer are valid for the miptree. */
319 static inline void
320 intel_miptree_check_level_layer(struct intel_mipmap_tree *mt,
321                                 uint32_t level,
322                                 uint32_t layer)
323 {
324    assert(level >= mt->first_level);
325    assert(level <= mt->last_level);
326    assert(layer < mt->level[level].depth);
327 }
328
329 int intel_miptree_pitch_align (struct intel_context *intel,
330                                struct intel_mipmap_tree *mt,
331                                uint32_t tiling,
332                                int pitch);
333
334 void intel_miptree_reference(struct intel_mipmap_tree **dst,
335                              struct intel_mipmap_tree *src);
336
337 void intel_miptree_release(struct intel_mipmap_tree **mt);
338
339 /* Check if an image fits an existing mipmap tree layout
340  */
341 bool intel_miptree_match_image(struct intel_mipmap_tree *mt,
342                                     struct gl_texture_image *image);
343
344 void
345 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
346                                GLuint level, GLuint face, GLuint depth,
347                                GLuint *x, GLuint *y);
348
349 void
350 intel_miptree_get_dimensions_for_image(struct gl_texture_image *image,
351                                        int *width, int *height, int *depth);
352
353 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
354                                   GLuint level,
355                                   GLuint x, GLuint y,
356                                   GLuint w, GLuint h, GLuint d);
357
358 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
359                                     GLuint level,
360                                     GLuint img, GLuint x, GLuint y);
361
362 void
363 intel_miptree_copy_teximage(struct intel_context *intel,
364                             struct intel_texture_image *intelImage,
365                             struct intel_mipmap_tree *dst_mt);
366
367 /**
368  * Copy the stencil data from \c mt->stencil_mt->region to \c mt->region for
369  * the given miptree slice.
370  *
371  * \see intel_mipmap_tree::stencil_mt
372  */
373 void
374 intel_miptree_s8z24_scatter(struct intel_context *intel,
375                             struct intel_mipmap_tree *mt,
376                             uint32_t level,
377                             uint32_t slice);
378
379 /**
380  * Copy the stencil data in \c mt->stencil_mt->region to \c mt->region for the
381  * given miptree slice.
382  *
383  * \see intel_mipmap_tree::stencil_mt
384  */
385 void
386 intel_miptree_s8z24_gather(struct intel_context *intel,
387                            struct intel_mipmap_tree *mt,
388                            uint32_t level,
389                            uint32_t layer);
390
391 /**
392  * \name Miptree HiZ functions
393  * \{
394  *
395  * It is safe to call the "slice_set_need_resolve" and "slice_resolve"
396  * functions on a miptree without HiZ. In that case, each function is a no-op.
397  */
398
399 /**
400  * \brief Allocate the miptree's embedded HiZ miptree.
401  * \see intel_mipmap_tree:hiz_mt
402  * \return false if allocation failed
403  */
404
405 bool
406 intel_miptree_alloc_hiz(struct intel_context *intel,
407                         struct intel_mipmap_tree *mt,
408                         GLuint num_samples);
409
410 void
411 intel_miptree_slice_set_needs_hiz_resolve(struct intel_mipmap_tree *mt,
412                                           uint32_t level,
413                                           uint32_t depth);
414 void
415 intel_miptree_slice_set_needs_depth_resolve(struct intel_mipmap_tree *mt,
416                                             uint32_t level,
417                                             uint32_t depth);
418 void
419 intel_miptree_all_slices_set_need_hiz_resolve(struct intel_mipmap_tree *mt);
420
421 void
422 intel_miptree_all_slices_set_need_depth_resolve(struct intel_mipmap_tree *mt);
423
424 /**
425  * \return false if no resolve was needed
426  */
427 bool
428 intel_miptree_slice_resolve_hiz(struct intel_context *intel,
429                                 struct intel_mipmap_tree *mt,
430                                 unsigned int level,
431                                 unsigned int depth);
432
433 /**
434  * \return false if no resolve was needed
435  */
436 bool
437 intel_miptree_slice_resolve_depth(struct intel_context *intel,
438                                   struct intel_mipmap_tree *mt,
439                                   unsigned int level,
440                                   unsigned int depth);
441
442 /**
443  * \return false if no resolve was needed
444  */
445 bool
446 intel_miptree_all_slices_resolve_hiz(struct intel_context *intel,
447                                      struct intel_mipmap_tree *mt);
448
449 /**
450  * \return false if no resolve was needed
451  */
452 bool
453 intel_miptree_all_slices_resolve_depth(struct intel_context *intel,
454                                        struct intel_mipmap_tree *mt);
455
456 /**\}*/
457
458 /* i915_mipmap_tree.c:
459  */
460 void i915_miptree_layout(struct intel_mipmap_tree *mt);
461 void i945_miptree_layout(struct intel_mipmap_tree *mt);
462 void brw_miptree_layout(struct intel_context *intel,
463                         struct intel_mipmap_tree *mt);
464
465 void
466 intel_miptree_map(struct intel_context *intel,
467                   struct intel_mipmap_tree *mt,
468                   unsigned int level,
469                   unsigned int slice,
470                   unsigned int x,
471                   unsigned int y,
472                   unsigned int w,
473                   unsigned int h,
474                   GLbitfield mode,
475                   void **out_ptr,
476                   int *out_stride);
477
478 void
479 intel_miptree_unmap(struct intel_context *intel,
480                     struct intel_mipmap_tree *mt,
481                     unsigned int level,
482                     unsigned int slice);
483
484 #ifdef I915
485 static inline void
486 intel_hiz_exec(struct intel_context *intel, struct intel_mipmap_tree *mt,
487                unsigned int level, unsigned int layer, enum gen6_hiz_op op)
488 {
489    /* Stub on i915.  It would be nice if we didn't execute resolve code at all
490     * there.
491     */
492 }
493 #else
494 void
495 intel_hiz_exec(struct intel_context *intel, struct intel_mipmap_tree *mt,
496                unsigned int level, unsigned int layer, enum gen6_hiz_op op);
497 #endif
498
499 #ifdef __cplusplus
500 }
501 #endif
502
503 #endif