3f21601a8a97693a6e896b4bbbd39d6bdc0467b9
[mesa.git] / src / mesa / drivers / dri / intel / 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 "intel_context.h"
7 #include "intel_mipmap_tree.h"
8 #include "intel_blit.h"
9 #include "intel_tex.h"
10 #include "intel_tex_layout.h"
11
12 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
13
14 /**
15 * When validating, we only care about the texture images that could
16 * be seen, so for non-mipmapped modes we want to ignore everything
17 * but BaseLevel.
18 */
19 static void
20 intel_update_max_level(struct intel_texture_object *intelObj,
21 struct gl_sampler_object *sampler)
22 {
23 struct gl_texture_object *tObj = &intelObj->base;
24
25 if (sampler->MinFilter == GL_NEAREST ||
26 sampler->MinFilter == GL_LINEAR) {
27 intelObj->_MaxLevel = tObj->BaseLevel;
28 } else {
29 intelObj->_MaxLevel = tObj->_MaxLevel;
30 }
31 }
32
33 /*
34 */
35 GLuint
36 intel_finalize_mipmap_tree(struct intel_context *intel, GLuint unit)
37 {
38 struct gl_context *ctx = &intel->ctx;
39 struct gl_texture_object *tObj = intel->ctx.Texture.Unit[unit]._Current;
40 struct intel_texture_object *intelObj = intel_texture_object(tObj);
41 struct gl_sampler_object *sampler = _mesa_get_samplerobj(ctx, unit);
42 GLuint face, i;
43 GLuint nr_faces = 0;
44 struct intel_texture_image *firstImage;
45 int width, height, depth;
46
47 /* TBOs require no validation -- they always just point to their BO. */
48 if (tObj->Target == GL_TEXTURE_BUFFER)
49 return true;
50
51 /* We know/require this is true by now:
52 */
53 assert(intelObj->base._BaseComplete);
54
55 /* What levels must the tree include at a minimum?
56 */
57 intel_update_max_level(intelObj, sampler);
58 firstImage = intel_texture_image(tObj->Image[0][tObj->BaseLevel]);
59
60 /* Check tree can hold all active levels. Check tree matches
61 * target, imageFormat, etc.
62 *
63 * For pre-gen4, we have to match first_level == tObj->BaseLevel,
64 * because we don't have the control that gen4 does to make min/mag
65 * determination happen at a nonzero (hardware) baselevel. Because
66 * of that, we just always relayout on baselevel change.
67 */
68 if (intelObj->mt &&
69 (!intel_miptree_match_image(intelObj->mt, &firstImage->base.Base) ||
70 intelObj->mt->first_level != tObj->BaseLevel ||
71 intelObj->mt->last_level < intelObj->_MaxLevel)) {
72 intel_miptree_release(&intelObj->mt);
73 }
74
75
76 /* May need to create a new tree:
77 */
78 if (!intelObj->mt) {
79 intel_miptree_get_dimensions_for_image(&firstImage->base.Base,
80 &width, &height, &depth);
81
82 perf_debug("Creating new %s %dx%dx%d %d..%d miptree to handle finalized "
83 "texture miptree.\n",
84 _mesa_get_format_name(firstImage->base.Base.TexFormat),
85 width, height, depth, tObj->BaseLevel, intelObj->_MaxLevel);
86
87 intelObj->mt = intel_miptree_create(intel,
88 intelObj->base.Target,
89 firstImage->base.Base.TexFormat,
90 tObj->BaseLevel,
91 intelObj->_MaxLevel,
92 width,
93 height,
94 depth,
95 true,
96 0 /* num_samples */,
97 INTEL_MSAA_LAYOUT_NONE);
98 if (!intelObj->mt)
99 return false;
100 }
101
102 /* Pull in any images not in the object's tree:
103 */
104 nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
105 for (face = 0; face < nr_faces; face++) {
106 for (i = tObj->BaseLevel; i <= intelObj->_MaxLevel; i++) {
107 struct intel_texture_image *intelImage =
108 intel_texture_image(intelObj->base.Image[face][i]);
109 /* skip too small size mipmap */
110 if (intelImage == NULL)
111 break;
112
113 if (intelObj->mt != intelImage->mt) {
114 intel_miptree_copy_teximage(intel, intelImage, intelObj->mt);
115 }
116
117 /* After we're done, we'd better agree that our layout is
118 * appropriate, or we'll end up hitting this function again on the
119 * next draw
120 */
121 assert(intel_miptree_match_image(intelObj->mt, &intelImage->base.Base));
122 }
123 }
124
125 return true;
126 }
127
128 /**
129 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
130 */
131 static void
132 intel_tex_map_image_for_swrast(struct intel_context *intel,
133 struct intel_texture_image *intel_image,
134 GLbitfield mode)
135 {
136 int level;
137 int face;
138 struct intel_mipmap_tree *mt;
139 unsigned int x, y;
140
141 if (!intel_image || !intel_image->mt)
142 return;
143
144 level = intel_image->base.Base.Level;
145 face = intel_image->base.Base.Face;
146 mt = intel_image->mt;
147
148 for (int i = 0; i < mt->level[level].depth; i++)
149 intel_miptree_slice_resolve_depth(intel, mt, level, i);
150
151 if (mt->target == GL_TEXTURE_3D ||
152 mt->target == GL_TEXTURE_2D_ARRAY ||
153 mt->target == GL_TEXTURE_1D_ARRAY) {
154 int i;
155
156 /* ImageOffsets[] is only used for swrast's fetch_texel_3d, so we can't
157 * share code with the normal path.
158 */
159 for (i = 0; i < mt->level[level].depth; i++) {
160 intel_miptree_get_image_offset(mt, level, i, &x, &y);
161 intel_image->base.ImageOffsets[i] = x + y * mt->region->pitch;
162 }
163
164 DBG("%s \n", __FUNCTION__);
165
166 intel_image->base.Map = intel_region_map(intel, mt->region, mode);
167 } else {
168 assert(intel_image->base.Base.Depth == 1);
169 intel_miptree_get_image_offset(mt, level, face, &x, &y);
170
171 DBG("%s: (%d,%d) -> (%d, %d)/%d\n",
172 __FUNCTION__, face, level, x, y, mt->region->pitch * mt->cpp);
173
174 intel_image->base.Map = intel_region_map(intel, mt->region, mode) +
175 (x + y * mt->region->pitch) * mt->cpp;
176 }
177
178 intel_image->base.RowStride = mt->region->pitch;
179 }
180
181 static void
182 intel_tex_unmap_image_for_swrast(struct intel_context *intel,
183 struct intel_texture_image *intel_image)
184 {
185 if (intel_image && intel_image->mt) {
186 intel_region_unmap(intel, intel_image->mt->region);
187 intel_image->base.Map = NULL;
188 }
189 }
190
191 /**
192 * \param mode bitmask of GL_MAP_READ_BIT, GL_MAP_WRITE_BIT
193 */
194 void
195 intel_tex_map_images(struct intel_context *intel,
196 struct intel_texture_object *intelObj,
197 GLbitfield mode)
198 {
199 GLuint nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
200 int i, face;
201
202 DBG("%s\n", __FUNCTION__);
203
204 for (i = intelObj->base.BaseLevel; i <= intelObj->_MaxLevel; i++) {
205 for (face = 0; face < nr_faces; face++) {
206 struct intel_texture_image *intel_image =
207 intel_texture_image(intelObj->base.Image[face][i]);
208
209 intel_tex_map_image_for_swrast(intel, intel_image, mode);
210 }
211 }
212 }
213
214 void
215 intel_tex_unmap_images(struct intel_context *intel,
216 struct intel_texture_object *intelObj)
217 {
218 GLuint nr_faces = _mesa_num_tex_faces(intelObj->base.Target);
219 int i, face;
220
221 for (i = intelObj->base.BaseLevel; i <= intelObj->_MaxLevel; i++) {
222 for (face = 0; face < nr_faces; face++) {
223 struct intel_texture_image *intel_image =
224 intel_texture_image(intelObj->base.Image[face][i]);
225
226 intel_tex_unmap_image_for_swrast(intel, intel_image);
227 }
228 }
229 }