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