i965: Ensure that texture validation is skipped for immutable textures.
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_validate.c
1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "main/mtypes.h"
25 #include "main/macros.h"
26 #include "main/samplerobj.h"
27 #include "main/texobj.h"
28
29 #include "brw_context.h"
30 #include "intel_mipmap_tree.h"
31 #include "intel_blit.h"
32 #include "intel_tex.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
35
36 /**
37 * Sets our driver-specific variant of tObj->_MaxLevel for later surface state
38 * upload.
39 *
40 * If we're only ensuring that there is storage for the first miplevel of a
41 * texture, then in texture setup we're going to have to make sure we don't
42 * allow sampling beyond level 0.
43 */
44 static void
45 intel_update_max_level(struct intel_texture_object *intelObj,
46 struct gl_sampler_object *sampler)
47 {
48 struct gl_texture_object *tObj = &intelObj->base;
49
50 if (sampler->MinFilter == GL_NEAREST ||
51 sampler->MinFilter == GL_LINEAR) {
52 intelObj->_MaxLevel = tObj->BaseLevel;
53 } else {
54 intelObj->_MaxLevel = tObj->_MaxLevel;
55 }
56 }
57
58 /**
59 * At rendering-from-a-texture time, make sure that the texture object has a
60 * miptree that can hold the entire texture based on
61 * BaseLevel/MaxLevel/filtering, and copy in any texture images that are
62 * stored in other miptrees.
63 */
64 GLuint
65 intel_finalize_mipmap_tree(struct brw_context *brw, GLuint unit)
66 {
67 struct gl_context *ctx = &brw->ctx;
68 struct gl_texture_object *tObj = ctx->Texture.Unit[unit]._Current;
69 struct intel_texture_object *intelObj = intel_texture_object(tObj);
70 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
71 GLuint face, i;
72 GLuint nr_faces = 0;
73 struct intel_texture_image *firstImage;
74 int width, height, depth;
75
76 /* TBOs require no validation -- they always just point to their BO. */
77 if (tObj->Target == GL_TEXTURE_BUFFER)
78 return true;
79
80 /* We know that this is true by now, and if it wasn't, we might have
81 * mismatched level sizes and the copies would fail.
82 */
83 assert(intelObj->base._BaseComplete);
84
85 intel_update_max_level(intelObj, sampler);
86
87 /* What levels does this validated texture image require? */
88 int validate_first_level = tObj->BaseLevel;
89 int validate_last_level = intelObj->_MaxLevel;
90
91 /* Skip the loop over images in the common case of no images having
92 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
93 * haven't looked at, then we do need to look at those new images.
94 */
95 if (!intelObj->needs_validate &&
96 validate_first_level >= intelObj->validated_first_level &&
97 validate_last_level <= intelObj->validated_last_level) {
98 return true;
99 }
100
101 /* Immutable textures should not get this far -- they should have been
102 * created in a validated state, and nothing can invalidate them.
103 */
104 assert(!tObj->Immutable);
105
106 firstImage = intel_texture_image(tObj->Image[0][tObj->BaseLevel]);
107
108 /* Check tree can hold all active levels. Check tree matches
109 * target, imageFormat, etc.
110 *
111 * For pre-gen4, we have to match first_level == tObj->BaseLevel,
112 * because we don't have the control that gen4 does to make min/mag
113 * determination happen at a nonzero (hardware) baselevel. Because
114 * of that, we just always relayout on baselevel change.
115 */
116 if (intelObj->mt &&
117 (!intel_miptree_match_image(intelObj->mt, &firstImage->base.Base) ||
118 validate_first_level < intelObj->mt->first_level ||
119 validate_last_level > intelObj->mt->last_level)) {
120 intel_miptree_release(&intelObj->mt);
121 }
122
123
124 /* May need to create a new tree:
125 */
126 if (!intelObj->mt) {
127 intel_miptree_get_dimensions_for_image(&firstImage->base.Base,
128 &width, &height, &depth);
129
130 perf_debug("Creating new %s %dx%dx%d %d-level miptree to handle "
131 "finalized texture miptree.\n",
132 _mesa_get_format_name(firstImage->base.Base.TexFormat),
133 width, height, depth, validate_last_level + 1);
134
135 intelObj->mt = intel_miptree_create(brw,
136 intelObj->base.Target,
137 firstImage->base.Base.TexFormat,
138 0, /* first_level */
139 validate_last_level,
140 width,
141 height,
142 depth,
143 true,
144 0 /* num_samples */,
145 INTEL_MIPTREE_TILING_ANY);
146 if (!intelObj->mt)
147 return false;
148 }
149
150 /* Pull in any images not in the object's tree:
151 */
152 nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
153 for (face = 0; face < nr_faces; face++) {
154 for (i = validate_first_level; i <= validate_last_level; i++) {
155 struct intel_texture_image *intelImage =
156 intel_texture_image(intelObj->base.Image[face][i]);
157 /* skip too small size mipmap */
158 if (intelImage == NULL)
159 break;
160
161 if (intelObj->mt != intelImage->mt) {
162 intel_miptree_copy_teximage(brw, intelImage, intelObj->mt,
163 false /* invalidate */);
164 }
165
166 /* After we're done, we'd better agree that our layout is
167 * appropriate, or we'll end up hitting this function again on the
168 * next draw
169 */
170 assert(intel_miptree_match_image(intelObj->mt, &intelImage->base.Base));
171 }
172 }
173
174 intelObj->validated_first_level = validate_first_level;
175 intelObj->validated_last_level = validate_last_level;
176 intelObj->needs_validate = false;
177
178 return true;
179 }