i965: Don't relayout a texture just for baselevel changes.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_validate.c
1 #include "main/mtypes.h"
2 #include "main/macros.h"
3 #include "main/samplerobj.h"
4 #include "main/texobj.h"
5
6 #include "brw_context.h"
7 #include "intel_mipmap_tree.h"
8 #include "intel_blit.h"
9 #include "intel_tex.h"
10
11 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
12
13 /**
14 * Sets our driver-specific variant of tObj->_MaxLevel for later surface state
15 * upload.
16 *
17 * If we're only ensuring that there is storage for the first miplevel of a
18 * texture, then in texture setup we're going to have to make sure we don't
19 * allow sampling beyond level 0.
20 */
21 static void
22 intel_update_max_level(struct intel_texture_object *intelObj,
23 struct gl_sampler_object *sampler)
24 {
25 struct gl_texture_object *tObj = &intelObj->base;
26
27 if (sampler->MinFilter == GL_NEAREST ||
28 sampler->MinFilter == GL_LINEAR) {
29 intelObj->_MaxLevel = tObj->BaseLevel;
30 } else {
31 intelObj->_MaxLevel = tObj->_MaxLevel;
32 }
33 }
34
35 /**
36 * At rendering-from-a-texture time, make sure that the texture object has a
37 * miptree that can hold the entire texture based on
38 * BaseLevel/MaxLevel/filtering, and copy in any texture images that are
39 * stored in other miptrees.
40 */
41 GLuint
42 intel_finalize_mipmap_tree(struct brw_context *brw, GLuint unit)
43 {
44 struct gl_context *ctx = &brw->ctx;
45 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
46 struct intel_texture_object *intelObj = intel_texture_object(tObj);
47 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
48 GLuint face, i;
49 GLuint nr_faces = 0;
50 struct intel_texture_image *firstImage;
51 int width, height, depth;
52
53 /* TBOs require no validation -- they always just point to their BO. */
54 if (tObj->Target == GL_TEXTURE_BUFFER)
55 return true;
56
57 /* We know that this is true by now, and if it wasn't, we might have
58 * mismatched level sizes and the copies would fail.
59 */
60 assert(intelObj->base._BaseComplete);
61
62 intel_update_max_level(intelObj, sampler);
63
64 /* What levels does this validated texture image require? */
65 int validate_first_level = tObj->BaseLevel;
66 int validate_last_level = intelObj->_MaxLevel;
67
68 /* Skip the loop over images in the common case of no images having
69 * changed. But if the GL_BASE_LEVEL / GL_MAX_LEVEL change to something we
70 * haven't looked at, then we do need to look at those new images.
71 */
72 if (!intelObj->needs_validate &&
73 validate_first_level >= intelObj->validated_first_level &&
74 validate_last_level <= intelObj->validated_last_level) {
75 return true;
76 }
77
78 firstImage = intel_texture_image(tObj->Image[0][tObj->BaseLevel]);
79
80 /* Check tree can hold all active levels. Check tree matches
81 * target, imageFormat, etc.
82 *
83 * For pre-gen4, we have to match first_level == tObj->BaseLevel,
84 * because we don't have the control that gen4 does to make min/mag
85 * determination happen at a nonzero (hardware) baselevel. Because
86 * of that, we just always relayout on baselevel change.
87 */
88 if (intelObj->mt &&
89 (!intel_miptree_match_image(intelObj->mt, &firstImage->base.Base) ||
90 validate_first_level < intelObj->mt->first_level ||
91 validate_last_level > intelObj->mt->last_level)) {
92 intel_miptree_release(&intelObj->mt);
93 }
94
95
96 /* May need to create a new tree:
97 */
98 if (!intelObj->mt) {
99 intel_miptree_get_dimensions_for_image(&firstImage->base.Base,
100 &width, &height, &depth);
101
102 perf_debug("Creating new %s %dx%dx%d %d..%d miptree to handle finalized "
103 "texture miptree.\n",
104 _mesa_get_format_name(firstImage->base.Base.TexFormat),
105 width, height, depth,
106 validate_first_level, validate_last_level);
107
108 intelObj->mt = intel_miptree_create(brw,
109 intelObj->base.Target,
110 firstImage->base.Base.TexFormat,
111 validate_first_level,
112 validate_last_level,
113 width,
114 height,
115 depth,
116 true,
117 0 /* num_samples */,
118 INTEL_MIPTREE_TILING_ANY);
119 if (!intelObj->mt)
120 return false;
121 }
122
123 /* Pull in any images not in the object's tree:
124 */
125 nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
126 for (face = 0; face < nr_faces; face++) {
127 for (i = validate_first_level; i <= validate_last_level; i++) {
128 struct intel_texture_image *intelImage =
129 intel_texture_image(intelObj->base.Image[face][i]);
130 /* skip too small size mipmap */
131 if (intelImage == NULL)
132 break;
133
134 if (intelObj->mt != intelImage->mt) {
135 intel_miptree_copy_teximage(brw, intelImage, intelObj->mt,
136 false /* invalidate */);
137 }
138
139 /* After we're done, we'd better agree that our layout is
140 * appropriate, or we'll end up hitting this function again on the
141 * next draw
142 */
143 assert(intel_miptree_match_image(intelObj->mt, &intelImage->base.Base));
144 }
145 }
146
147 intelObj->validated_first_level = validate_first_level;
148 intelObj->validated_last_level = validate_last_level;
149 intelObj->needs_validate = false;
150
151 return true;
152 }