intel_tex_validate.c revision 6dcc398ac0837025cf60b4d6a056fa3b0a16466f
1#include "main/mtypes.h"
2#include "main/macros.h"
3#include "main/samplerobj.h"
4
5#include "intel_context.h"
6#include "intel_mipmap_tree.h"
7#include "intel_tex.h"
8
9#define FILE_DEBUG_FLAG DEBUG_TEXTURE
10
11/**
12 * When validating, we only care about the texture images that could
13 * be seen, so for non-mipmapped modes we want to ignore everything
14 * but BaseLevel.
15 */
16static void
17intel_update_max_level(struct intel_context *intel,
18		       struct intel_texture_object *intelObj,
19		       struct gl_sampler_object *sampler)
20{
21   struct gl_texture_object *tObj = &intelObj->base;
22
23   if (sampler->MinFilter == GL_NEAREST ||
24       sampler->MinFilter == GL_LINEAR) {
25      intelObj->_MaxLevel = tObj->BaseLevel;
26   } else {
27      intelObj->_MaxLevel = tObj->_MaxLevel;
28   }
29}
30
31/**
32 * Copies the image's contents at its level into the object's miptree,
33 * and updates the image to point at the object's miptree.
34 */
35static void
36copy_image_data_to_tree(struct intel_context *intel,
37                        struct intel_texture_object *intelObj,
38                        struct intel_texture_image *intelImage)
39{
40   if (intelImage->mt) {
41      /* Copy potentially with the blitter:
42       */
43      intel_miptree_image_copy(intel,
44                               intelObj->mt,
45                               intelImage->face,
46                               intelImage->level, intelImage->mt);
47
48      intel_miptree_release(intel, &intelImage->mt);
49   }
50   else {
51      assert(intelImage->base.Data != NULL);
52
53      /* More straightforward upload.
54       */
55      intel_miptree_image_data(intel,
56                               intelObj->mt,
57                               intelImage->face,
58                               intelImage->level,
59                               intelImage->base.Data,
60                               intelImage->base.RowStride,
61                               intelImage->base.RowStride *
62                               intelImage->base.Height);
63      _mesa_align_free(intelImage->base.Data);
64      intelImage->base.Data = NULL;
65   }
66
67   intel_miptree_reference(&intelImage->mt, intelObj->mt);
68}
69
70
71/*
72 */
73GLuint
74intel_finalize_mipmap_tree(struct intel_context *intel, GLuint unit)
75{
76   struct gl_context *ctx = &intel->ctx;
77   struct gl_texture_object *tObj = intel->ctx.Texture.Unit[unit]._Current;
78   struct intel_texture_object *intelObj = intel_texture_object(tObj);
79   struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
80   int cpp;
81   GLuint face, i;
82   GLuint nr_faces = 0;
83   struct intel_texture_image *firstImage;
84
85   /* We know/require this is true by now:
86    */
87   assert(intelObj->base._Complete);
88
89   /* What levels must the tree include at a minimum?
90    */
91   intel_update_max_level(intel, intelObj, sampler);
92   firstImage = intel_texture_image(tObj->Image[0][tObj->BaseLevel]);
93
94   /* Fallback case:
95    */
96   if (firstImage->base.Border) {
97      if (intelObj->mt) {
98         intel_miptree_release(intel, &intelObj->mt);
99      }
100      return GL_FALSE;
101   }
102
103   if (_mesa_is_format_compressed(firstImage->base.TexFormat)) {
104      cpp = intel_compressed_num_bytes(firstImage->base.TexFormat);
105   }
106   else
107      cpp = _mesa_get_format_bytes(firstImage->base.TexFormat);
108
109   /* Check tree can hold all active levels.  Check tree matches
110    * target, imageFormat, etc.
111    *
112    * For pre-gen4, we have to match first_level == tObj->BaseLevel,
113    * because we don't have the control that gen4 does to make min/mag
114    * determination happen at a nonzero (hardware) baselevel.  Because
115    * of that, we just always relayout on baselevel change.
116    */
117   if (intelObj->mt &&
118       (intelObj->mt->target != intelObj->base.Target ||
119	intelObj->mt->format != firstImage->base.TexFormat ||
120	intelObj->mt->first_level != tObj->BaseLevel ||
121	intelObj->mt->last_level < intelObj->_MaxLevel ||
122	intelObj->mt->width0 != firstImage->base.Width ||
123	intelObj->mt->height0 != firstImage->base.Height ||
124	intelObj->mt->depth0 != firstImage->base.Depth)) {
125      intel_miptree_release(intel, &intelObj->mt);
126   }
127
128
129   /* May need to create a new tree:
130    */
131   if (!intelObj->mt) {
132      intelObj->mt = intel_miptree_create(intel,
133                                          intelObj->base.Target,
134					  firstImage->base.TexFormat,
135                                          tObj->BaseLevel,
136                                          intelObj->_MaxLevel,
137                                          firstImage->base.Width,
138                                          firstImage->base.Height,
139                                          firstImage->base.Depth,
140                                          cpp,
141					  GL_TRUE);
142      if (!intelObj->mt)
143         return GL_FALSE;
144   }
145
146   /* Pull in any images not in the object's tree:
147    */
148   nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
149   for (face = 0; face < nr_faces; face++) {
150      for (i = tObj->BaseLevel; i <= intelObj->_MaxLevel; i++) {
151         struct intel_texture_image *intelImage =
152            intel_texture_image(intelObj->base.Image[face][i]);
153	 /* skip too small size mipmap */
154 	 if (intelImage == NULL)
155		 break;
156	 /* Need to import images in main memory or held in other trees.
157	  * If it's a render target, then its data isn't needed to be in
158	  * the object tree (otherwise we'd be FBO incomplete), and we need
159	  * to keep track of the image's MT as needing to be pulled in still,
160	  * or we'll lose the rendering that's done to it.
161          */
162         if (intelObj->mt != intelImage->mt &&
163	     !intelImage->used_as_render_target) {
164            copy_image_data_to_tree(intel, intelObj, intelImage);
165         }
166      }
167   }
168
169   return GL_TRUE;
170}
171
172void
173intel_tex_map_level_images(struct intel_context *intel,
174			   struct intel_texture_object *intelObj,
175			   int level)
176{
177   GLuint nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
178   GLuint face;
179
180   for (face = 0; face < nr_faces; face++) {
181      struct intel_texture_image *intelImage =
182	 intel_texture_image(intelObj->base.Image[face][level]);
183
184      if (intelImage && intelImage->mt) {
185	 intelImage->base.Data =
186	    intel_miptree_image_map(intel,
187				    intelImage->mt,
188				    intelImage->face,
189				    intelImage->level,
190				    &intelImage->base.RowStride,
191				    intelImage->base.ImageOffsets);
192	 /* convert stride to texels, not bytes */
193	 intelImage->base.RowStride /= intelImage->mt->cpp;
194	 /* intelImage->base.ImageStride /= intelImage->mt->cpp; */
195      }
196   }
197}
198
199void
200intel_tex_unmap_level_images(struct intel_context *intel,
201			     struct intel_texture_object *intelObj,
202			     int level)
203{
204   GLuint nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
205   GLuint face;
206
207   for (face = 0; face < nr_faces; face++) {
208      struct intel_texture_image *intelImage =
209	 intel_texture_image(intelObj->base.Image[face][level]);
210
211      if (intelImage && intelImage->mt) {
212	 intel_miptree_image_unmap(intel, intelImage->mt);
213	 intelImage->base.Data = NULL;
214      }
215   }
216}
217
218void
219intel_tex_map_images(struct intel_context *intel,
220                     struct intel_texture_object *intelObj)
221{
222   int i;
223
224   DBG("%s\n", __FUNCTION__);
225
226   for (i = intelObj->base.BaseLevel; i <= intelObj->_MaxLevel; i++)
227      intel_tex_map_level_images(intel, intelObj, i);
228}
229
230void
231intel_tex_unmap_images(struct intel_context *intel,
232                       struct intel_texture_object *intelObj)
233{
234   int i;
235
236   for (i = intelObj->base.BaseLevel; i <= intelObj->_MaxLevel; i++)
237      intel_tex_unmap_level_images(intel, intelObj, i);
238}
239