intel: Consolidate texture validation copy code, and reuse it correctly.
[mesa.git] / src / mesa / drivers / dri / intel / intel_mipmap_tree.c
1 /**************************************************************************
2 *
3 * Copyright 2006 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "intel_context.h"
29 #include "intel_mipmap_tree.h"
30 #include "intel_regions.h"
31 #include "intel_tex_layout.h"
32 #include "intel_tex.h"
33 #include "intel_blit.h"
34 #include "main/enums.h"
35 #include "main/formats.h"
36 #include "main/teximage.h"
37
38 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
39
40
41 static GLenum
42 target_to_target(GLenum target)
43 {
44 switch (target) {
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
51 return GL_TEXTURE_CUBE_MAP_ARB;
52 default:
53 return target;
54 }
55 }
56
57
58 static struct intel_mipmap_tree *
59 intel_miptree_create_internal(struct intel_context *intel,
60 GLenum target,
61 gl_format format,
62 GLuint first_level,
63 GLuint last_level,
64 GLuint width0,
65 GLuint height0,
66 GLuint depth0)
67 {
68 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
69 int compress_byte = 0;
70
71 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
72 _mesa_lookup_enum_by_nr(target),
73 _mesa_get_format_name(format),
74 first_level, last_level, mt);
75
76 if (_mesa_is_format_compressed(format))
77 compress_byte = intel_compressed_num_bytes(format);
78
79 mt->target = target_to_target(target);
80 mt->format = format;
81 mt->first_level = first_level;
82 mt->last_level = last_level;
83 mt->width0 = width0;
84 mt->height0 = height0;
85 mt->depth0 = depth0;
86 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
87 mt->compressed = compress_byte ? 1 : 0;
88 mt->refcount = 1;
89
90 #ifdef I915
91 (void) intel;
92 if (intel->is_945)
93 i945_miptree_layout(mt);
94 else
95 i915_miptree_layout(mt);
96 #else
97 brw_miptree_layout(intel, mt);
98 #endif
99
100 return mt;
101 }
102
103
104 struct intel_mipmap_tree *
105 intel_miptree_create(struct intel_context *intel,
106 GLenum target,
107 gl_format format,
108 GLuint first_level,
109 GLuint last_level,
110 GLuint width0,
111 GLuint height0,
112 GLuint depth0,
113 GLboolean expect_accelerated_upload)
114 {
115 struct intel_mipmap_tree *mt;
116 uint32_t tiling = I915_TILING_NONE;
117 GLenum base_format = _mesa_get_format_base_format(format);
118
119 if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
120 if (intel->gen >= 4 &&
121 (base_format == GL_DEPTH_COMPONENT ||
122 base_format == GL_DEPTH_STENCIL_EXT))
123 tiling = I915_TILING_Y;
124 else if (width0 >= 64)
125 tiling = I915_TILING_X;
126 }
127
128 mt = intel_miptree_create_internal(intel, target, format,
129 first_level, last_level, width0,
130 height0, depth0);
131 /*
132 * pitch == 0 || height == 0 indicates the null texture
133 */
134 if (!mt || !mt->total_width || !mt->total_height) {
135 free(mt);
136 return NULL;
137 }
138
139 mt->region = intel_region_alloc(intel->intelScreen,
140 tiling,
141 mt->cpp,
142 mt->total_width,
143 mt->total_height,
144 expect_accelerated_upload);
145
146 if (!mt->region) {
147 free(mt);
148 return NULL;
149 }
150
151 return mt;
152 }
153
154
155 struct intel_mipmap_tree *
156 intel_miptree_create_for_region(struct intel_context *intel,
157 GLenum target,
158 gl_format format,
159 struct intel_region *region)
160 {
161 struct intel_mipmap_tree *mt;
162
163 mt = intel_miptree_create_internal(intel, target, format,
164 0, 0,
165 region->width, region->height, 1);
166 if (!mt)
167 return mt;
168
169 intel_region_reference(&mt->region, region);
170
171 return mt;
172 }
173
174 void
175 intel_miptree_reference(struct intel_mipmap_tree **dst,
176 struct intel_mipmap_tree *src)
177 {
178 if (*dst == src)
179 return;
180
181 intel_miptree_release(dst);
182
183 if (src) {
184 src->refcount++;
185 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
186 }
187
188 *dst = src;
189 }
190
191
192 void
193 intel_miptree_release(struct intel_mipmap_tree **mt)
194 {
195 if (!*mt)
196 return;
197
198 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
199 if (--(*mt)->refcount <= 0) {
200 GLuint i;
201
202 DBG("%s deleting %p\n", __FUNCTION__, *mt);
203
204 intel_region_release(&((*mt)->region));
205 intel_region_release(&((*mt)->hiz_region));
206
207 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
208 free((*mt)->level[i].x_offset);
209 free((*mt)->level[i].y_offset);
210 }
211
212 free(*mt);
213 }
214 *mt = NULL;
215 }
216
217
218 /**
219 * Can the image be pulled into a unified mipmap tree? This mirrors
220 * the completeness test in a lot of ways.
221 *
222 * Not sure whether I want to pass gl_texture_image here.
223 */
224 GLboolean
225 intel_miptree_match_image(struct intel_mipmap_tree *mt,
226 struct gl_texture_image *image)
227 {
228 struct intel_texture_image *intelImage = intel_texture_image(image);
229 GLuint level = intelImage->base.Base.Level;
230
231 /* Images with borders are never pulled into mipmap trees. */
232 if (image->Border)
233 return GL_FALSE;
234
235 if (image->TexFormat != mt->format)
236 return GL_FALSE;
237
238 /* Test image dimensions against the base level image adjusted for
239 * minification. This will also catch images not present in the
240 * tree, changed targets, etc.
241 */
242 if (image->Width != mt->level[level].width ||
243 image->Height != mt->level[level].height ||
244 image->Depth != mt->level[level].depth)
245 return GL_FALSE;
246
247 return GL_TRUE;
248 }
249
250
251 void
252 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
253 GLuint level,
254 GLuint nr_images,
255 GLuint x, GLuint y,
256 GLuint w, GLuint h, GLuint d)
257 {
258 mt->level[level].width = w;
259 mt->level[level].height = h;
260 mt->level[level].depth = d;
261 mt->level[level].level_x = x;
262 mt->level[level].level_y = y;
263 mt->level[level].nr_images = nr_images;
264
265 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
266 level, w, h, d, x, y);
267
268 assert(nr_images);
269 assert(!mt->level[level].x_offset);
270
271 mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint));
272 mt->level[level].x_offset[0] = mt->level[level].level_x;
273 mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint));
274 mt->level[level].y_offset[0] = mt->level[level].level_y;
275 }
276
277
278 void
279 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
280 GLuint level, GLuint img,
281 GLuint x, GLuint y)
282 {
283 if (img == 0 && level == 0)
284 assert(x == 0 && y == 0);
285
286 assert(img < mt->level[level].nr_images);
287
288 mt->level[level].x_offset[img] = mt->level[level].level_x + x;
289 mt->level[level].y_offset[img] = mt->level[level].level_y + y;
290
291 DBG("%s level %d img %d pos %d,%d\n",
292 __FUNCTION__, level, img,
293 mt->level[level].x_offset[img], mt->level[level].y_offset[img]);
294 }
295
296
297 void
298 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
299 GLuint level, GLuint face, GLuint depth,
300 GLuint *x, GLuint *y)
301 {
302 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
303 *x = mt->level[level].x_offset[face];
304 *y = mt->level[level].y_offset[face];
305 } else if (mt->target == GL_TEXTURE_3D) {
306 *x = mt->level[level].x_offset[depth];
307 *y = mt->level[level].y_offset[depth];
308 } else {
309 *x = mt->level[level].x_offset[0];
310 *y = mt->level[level].y_offset[0];
311 }
312 }
313
314 /**
315 * Copies the image's current data to the given miptree, and associates that
316 * miptree with the image.
317 */
318 void
319 intel_miptree_copy_teximage(struct intel_context *intel,
320 struct intel_texture_image *intelImage,
321 struct intel_mipmap_tree *dst_mt)
322 {
323 struct intel_mipmap_tree *src_mt = intelImage->mt;
324 int level = intelImage->base.Base.Level;
325 int face = intelImage->base.Base.Face;
326 GLuint width = src_mt->level[level].width;
327 GLuint height = src_mt->level[level].height;
328 GLuint depth = src_mt->level[level].depth;
329 int slice;
330 void *src, *dst;
331
332 if (dst_mt->compressed) {
333 unsigned int align_w, align_h;
334
335 intel_get_texture_alignment_unit(intelImage->base.Base.TexFormat,
336 &align_w, &align_h);
337 height = ALIGN(height, align_h) / align_h;
338 width = ALIGN(width, align_w);
339 }
340
341 for (slice = 0; slice < depth; slice++) {
342 unsigned int dst_x, dst_y, src_x, src_y;
343
344 intel_miptree_get_image_offset(dst_mt, level, face, slice,
345 &dst_x, &dst_y);
346
347 if (src_mt) {
348 /* Copy potentially with the blitter:
349 */
350 intel_miptree_get_image_offset(src_mt, level, face, slice,
351 &src_x, &src_y);
352
353 DBG("validate blit mt %p %d,%d/%d -> mt %p %d,%d/%d (%dx%d)\n",
354 src_mt, src_x, src_y, src_mt->region->pitch * src_mt->region->cpp,
355 dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
356 width, height);
357
358 if (!intelEmitCopyBlit(intel,
359 dst_mt->region->cpp,
360 src_mt->region->pitch, src_mt->region->bo,
361 0, src_mt->region->tiling,
362 dst_mt->region->pitch, dst_mt->region->bo,
363 0, dst_mt->region->tiling,
364 src_x, src_y,
365 dst_x, dst_y,
366 width, height,
367 GL_COPY)) {
368
369 fallback_debug("miptree validate blit for %s failed\n",
370 _mesa_get_format_name(intelImage->base.Base.TexFormat));
371 dst = intel_region_map(intel, dst_mt->region);
372 src = intel_region_map(intel, src_mt->region);
373
374 _mesa_copy_rect(dst,
375 dst_mt->cpp,
376 dst_mt->region->pitch,
377 dst_x, dst_y,
378 width, height,
379 src, src_mt->region->pitch,
380 src_x, src_y);
381
382 intel_region_unmap(intel, dst_mt->region);
383 intel_region_unmap(intel, src_mt->region);
384 }
385 } else {
386 dst = intel_region_map(intel, dst_mt->region);
387
388 DBG("validate upload mt %p -> mt %p %d,%d/%d (%dx%d)\n",
389 src,
390 dst_mt, dst_x, dst_y, dst_mt->region->pitch * dst_mt->region->cpp,
391 width, height);
392
393 src = intelImage->base.Base.Data;
394 src += (intelImage->base.Base.RowStride *
395 intelImage->base.Base.Height *
396 dst_mt->region->cpp *
397 slice);
398
399 _mesa_copy_rect(dst,
400 dst_mt->region->cpp,
401 dst_mt->region->pitch,
402 dst_x, dst_y,
403 width, height,
404 src,
405 intelImage->base.Base.RowStride,
406 0, 0);
407
408 intel_region_unmap(intel, dst_mt->region);
409 }
410 }
411
412 if (!src_mt) {
413 _mesa_free_texmemory(intelImage->base.Base.Data);
414 intelImage->base.Base.Data = NULL;
415 }
416
417 intel_miptree_reference(&intelImage->mt, dst_mt);
418 }