1 #include "main/mtypes.h"
2 #include "main/macros.h"
3 #include "main/samplerobj.h"
4 #include "main/teximage.h"
5 #include "main/texobj.h"
6 
7 #include "intel_context.h"
8 #include "intel_mipmap_tree.h"
9 #include "intel_blit.h"
10 #include "intel_tex.h"
11 #include "intel_tex_layout.h"
12 
13 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
14 
15 /**
16  * When validating, we only care about the texture images that could
17  * be seen, so for non-mipmapped modes we want to ignore everything
18  * but BaseLevel.
19  */
20 static void
intel_update_max_level(struct intel_texture_object * intelObj,struct gl_sampler_object * sampler)21 intel_update_max_level(struct intel_texture_object *intelObj,
22 		       struct gl_sampler_object *sampler)
23 {
24    struct gl_texture_object *tObj = &intelObj->base;
25    int maxlevel;
26 
27    if (sampler->Attrib.MinFilter == GL_NEAREST ||
28        sampler->Attrib.MinFilter == GL_LINEAR) {
29       maxlevel = tObj->Attrib.BaseLevel;
30    } else {
31       maxlevel = tObj->_MaxLevel;
32    }
33 
34    if (intelObj->_MaxLevel != maxlevel) {
35       intelObj->_MaxLevel = maxlevel;
36       intelObj->needs_validate = true;
37    }
38 }
39 
40 /*
41  */
42 GLuint
intel_finalize_mipmap_tree(struct intel_context * intel,GLuint unit)43 intel_finalize_mipmap_tree(struct intel_context *intel, GLuint unit)
44 {
45    struct gl_context *ctx = &intel->ctx;
46    struct gl_texture_object *tObj = intel->ctx.Texture.Unit[unit]._Current;
47    struct intel_texture_object *intelObj = intel_texture_object(tObj);
48    struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
49    GLuint face, i;
50    GLuint nr_faces = 0;
51    struct intel_texture_image *firstImage;
52    int width, height, depth;
53 
54    /* TBOs require no validation -- they always just point to their BO. */
55    if (tObj->Target == GL_TEXTURE_BUFFER)
56       return true;
57 
58    /* We know/require this is true by now:
59     */
60    assert(intelObj->base._BaseComplete);
61 
62    /* What levels must the tree include at a minimum?
63     */
64    intel_update_max_level(intelObj, sampler);
65    if (intelObj->mt && intelObj->mt->first_level != tObj->Attrib.BaseLevel)
66       intelObj->needs_validate = true;
67 
68    if (!intelObj->needs_validate)
69       return true;
70 
71    firstImage = intel_texture_image(tObj->Image[0][tObj->Attrib.BaseLevel]);
72 
73    /* Check tree can hold all active levels.  Check tree matches
74     * target, imageFormat, etc.
75     *
76     * For pre-gen4, we have to match first_level == tObj->BaseLevel,
77     * because we don't have the control that gen4 does to make min/mag
78     * determination happen at a nonzero (hardware) baselevel.  Because
79     * of that, we just always relayout on baselevel change.
80     */
81    if (intelObj->mt &&
82        (!intel_miptree_match_image(intelObj->mt, &firstImage->base.Base) ||
83 	intelObj->mt->first_level != tObj->Attrib.BaseLevel ||
84 	intelObj->mt->last_level < intelObj->_MaxLevel)) {
85       intel_miptree_release(&intelObj->mt);
86    }
87 
88 
89    /* May need to create a new tree:
90     */
91    if (!intelObj->mt) {
92       intel_miptree_get_dimensions_for_image(&firstImage->base.Base,
93 					     &width, &height, &depth);
94 
95       perf_debug("Creating new %s %dx%dx%d %d..%d miptree to handle finalized "
96                  "texture miptree.\n",
97                  _mesa_get_format_name(firstImage->base.Base.TexFormat),
98                  width, height, depth, tObj->Attrib.BaseLevel, intelObj->_MaxLevel);
99 
100       intelObj->mt = intel_miptree_create(intel,
101                                           intelObj->base.Target,
102 					  firstImage->base.Base.TexFormat,
103                                           tObj->Attrib.BaseLevel,
104                                           intelObj->_MaxLevel,
105                                           width,
106                                           height,
107                                           depth,
108 					  true,
109                                           INTEL_MIPTREE_TILING_ANY);
110       if (!intelObj->mt)
111          return false;
112    }
113 
114    /* Pull in any images not in the object's tree:
115     */
116    nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
117    for (face = 0; face < nr_faces; face++) {
118       for (i = tObj->Attrib.BaseLevel; i <= intelObj->_MaxLevel; i++) {
119          struct intel_texture_image *intelImage =
120             intel_texture_image(intelObj->base.Image[face][i]);
121 	 /* skip too small size mipmap */
122  	 if (intelImage == NULL)
123 		 break;
124 
125          if (intelObj->mt != intelImage->mt) {
126             intel_miptree_copy_teximage(intel, intelImage, intelObj->mt,
127                                         false /* invalidate */);
128          }
129 
130          /* After we're done, we'd better agree that our layout is
131           * appropriate, or we'll end up hitting this function again on the
132           * next draw
133           */
134          assert(intel_miptree_match_image(intelObj->mt, &intelImage->base.Base));
135       }
136    }
137 
138    intelObj->needs_validate = false;
139 
140    return true;
141 }
142