i965/icl: Update the assert in brw_memory_barrier()
[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/teximage.h"
28 #include "main/texobj.h"
29
30 #include "brw_context.h"
31 #include "intel_mipmap_tree.h"
32 #include "intel_blit.h"
33 #include "intel_tex.h"
34
35 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
36
37 /**
38 * Sets our driver-specific variant of tObj->_MaxLevel for later surface state
39 * upload.
40 *
41 * If we're only ensuring that there is storage for the first miplevel of a
42 * texture, then in texture setup we're going to have to make sure we don't
43 * allow sampling beyond level 0.
44 */
45 static void
46 intel_update_max_level(struct gl_texture_object *tObj,
47 struct gl_sampler_object *sampler)
48 {
49 struct intel_texture_object *intelObj = intel_texture_object(tObj);
50
51 if (!tObj->_MipmapComplete ||
52 (tObj->_RenderToTexture &&
53 (sampler->MinFilter == GL_NEAREST ||
54 sampler->MinFilter == GL_LINEAR))) {
55 intelObj->_MaxLevel = tObj->BaseLevel;
56 } else {
57 intelObj->_MaxLevel = tObj->_MaxLevel;
58 }
59 }
60
61 /**
62 * At rendering-from-a-texture time, make sure that the texture object has a
63 * miptree that can hold the entire texture based on
64 * BaseLevel/MaxLevel/filtering, and copy in any texture images that are
65 * stored in other miptrees.
66 */
67 void
68 intel_finalize_mipmap_tree(struct brw_context *brw,
69 struct gl_texture_object *tObj)
70 {
71 struct intel_texture_object *intelObj = intel_texture_object(tObj);
72 GLuint face, i;
73 GLuint nr_faces = 0;
74 struct intel_texture_image *firstImage;
75 int width, height, depth;
76
77 /* TBOs require no validation -- they always just point to their BO. */
78 if (tObj->Target == GL_TEXTURE_BUFFER)
79 return;
80
81 /* What levels does this validated texture image require? */
82 int validate_first_level = tObj->BaseLevel;
83 int validate_last_level = intelObj->_MaxLevel;
84
85 /* Skip the loop over images in the common case of no images having
86 * changed. But if the GL_BASE_LEVEL or GL_MAX_LEVEL change to something we
87 * haven't looked at, then we do need to look at those new images.
88 */
89 if (!intelObj->needs_validate &&
90 validate_first_level >= intelObj->validated_first_level &&
91 validate_last_level <= intelObj->validated_last_level) {
92 return;
93 }
94
95 /* On recent generations, immutable textures should not get this far
96 * -- they should have been created in a validated state, and nothing
97 * can invalidate them.
98 *
99 * Unfortunately, this is not true on pre-Sandybridge hardware -- when
100 * rendering into an immutable-format depth texture we may have to rebase
101 * the rendered levels to meet alignment requirements.
102 *
103 * FINISHME: Avoid doing this.
104 */
105 assert(!tObj->Immutable || brw->screen->devinfo.gen < 6);
106
107 firstImage = intel_texture_image(tObj->Image[0][tObj->BaseLevel]);
108
109 /* Check tree can hold all active levels. Check tree matches
110 * target, imageFormat, etc.
111 */
112 if (intelObj->mt &&
113 (!intel_miptree_match_image(intelObj->mt, &firstImage->base.Base) ||
114 validate_first_level < intelObj->mt->first_level ||
115 validate_last_level > intelObj->mt->last_level)) {
116 intel_miptree_release(&intelObj->mt);
117 }
118
119
120 /* May need to create a new tree:
121 */
122 if (!intelObj->mt) {
123 intel_get_image_dims(&firstImage->base.Base, &width, &height, &depth);
124
125 perf_debug("Creating new %s %dx%dx%d %d-level miptree to handle "
126 "finalized texture miptree.\n",
127 _mesa_get_format_name(firstImage->base.Base.TexFormat),
128 width, height, depth, validate_last_level + 1);
129
130 intelObj->mt = intel_miptree_create(brw,
131 intelObj->base.Target,
132 firstImage->base.Base.TexFormat,
133 0, /* first_level */
134 validate_last_level,
135 width,
136 height,
137 depth,
138 1 /* num_samples */,
139 MIPTREE_CREATE_BUSY);
140 if (!intelObj->mt)
141 return;
142 }
143
144 /* Pull in any images not in the object's tree:
145 */
146 nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
147 for (face = 0; face < nr_faces; face++) {
148 for (i = validate_first_level; i <= validate_last_level; i++) {
149 struct intel_texture_image *intelImage =
150 intel_texture_image(intelObj->base.Image[face][i]);
151 /* skip too small size mipmap */
152 if (intelImage == NULL)
153 break;
154
155 if (intelObj->mt != intelImage->mt)
156 intel_miptree_copy_teximage(brw, intelImage, intelObj->mt);
157
158 /* After we're done, we'd better agree that our layout is
159 * appropriate, or we'll end up hitting this function again on the
160 * next draw
161 */
162 assert(intel_miptree_match_image(intelObj->mt, &intelImage->base.Base));
163 }
164 }
165
166 intelObj->validated_first_level = validate_first_level;
167 intelObj->validated_last_level = validate_last_level;
168 intelObj->_Format = intelObj->mt->format;
169 intelObj->needs_validate = false;
170 }
171
172 /**
173 * Finalizes all textures, completing any rendering that needs to be done
174 * to prepare them.
175 */
176 void
177 brw_validate_textures(struct brw_context *brw)
178 {
179 struct gl_context *ctx = &brw->ctx;
180 const int max_enabled_unit = ctx->Texture._MaxEnabledTexImageUnit;
181
182 for (int unit = 0; unit <= max_enabled_unit; unit++) {
183 struct gl_texture_object *tex_obj = ctx->Texture.Unit[unit]._Current;
184
185 if (!tex_obj)
186 continue;
187
188 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
189
190 /* We know that this is true by now, and if it wasn't, we might have
191 * mismatched level sizes and the copies would fail.
192 */
193 assert(tex_obj->_BaseComplete);
194
195 intel_update_max_level(tex_obj, sampler);
196 intel_finalize_mipmap_tree(brw, tex_obj);
197 }
198 }