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