intel: Remove pointless boolean return value from *_miptree_layout.
[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 "main/enums.h"
34 #include "main/formats.h"
35
36 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
37
38
39 static GLenum
40 target_to_target(GLenum target)
41 {
42 switch (target) {
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
45 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
46 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
47 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
48 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
49 return GL_TEXTURE_CUBE_MAP_ARB;
50 default:
51 return target;
52 }
53 }
54
55
56 static struct intel_mipmap_tree *
57 intel_miptree_create_internal(struct intel_context *intel,
58 GLenum target,
59 gl_format format,
60 GLuint first_level,
61 GLuint last_level,
62 GLuint width0,
63 GLuint height0,
64 GLuint depth0)
65 {
66 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
67 int compress_byte = 0;
68
69 DBG("%s target %s format %s level %d..%d <-- %p\n", __FUNCTION__,
70 _mesa_lookup_enum_by_nr(target),
71 _mesa_get_format_name(format),
72 first_level, last_level, mt);
73
74 if (_mesa_is_format_compressed(format))
75 compress_byte = intel_compressed_num_bytes(format);
76
77 mt->target = target_to_target(target);
78 mt->format = format;
79 mt->first_level = first_level;
80 mt->last_level = last_level;
81 mt->width0 = width0;
82 mt->height0 = height0;
83 mt->depth0 = depth0;
84 mt->cpp = compress_byte ? compress_byte : _mesa_get_format_bytes(mt->format);
85 mt->compressed = compress_byte ? 1 : 0;
86 mt->refcount = 1;
87
88 #ifdef I915
89 (void) intel;
90 if (intel->is_945)
91 i945_miptree_layout(mt);
92 else
93 i915_miptree_layout(mt);
94 #else
95 brw_miptree_layout(intel, mt);
96 #endif
97
98 return mt;
99 }
100
101
102 struct intel_mipmap_tree *
103 intel_miptree_create(struct intel_context *intel,
104 GLenum target,
105 gl_format format,
106 GLuint first_level,
107 GLuint last_level,
108 GLuint width0,
109 GLuint height0,
110 GLuint depth0,
111 GLboolean expect_accelerated_upload)
112 {
113 struct intel_mipmap_tree *mt;
114 uint32_t tiling = I915_TILING_NONE;
115 GLenum base_format = _mesa_get_format_base_format(format);
116
117 if (intel->use_texture_tiling && !_mesa_is_format_compressed(format)) {
118 if (intel->gen >= 4 &&
119 (base_format == GL_DEPTH_COMPONENT ||
120 base_format == GL_DEPTH_STENCIL_EXT))
121 tiling = I915_TILING_Y;
122 else if (width0 >= 64)
123 tiling = I915_TILING_X;
124 }
125
126 mt = intel_miptree_create_internal(intel, target, format,
127 first_level, last_level, width0,
128 height0, depth0);
129 /*
130 * pitch == 0 || height == 0 indicates the null texture
131 */
132 if (!mt || !mt->total_width || !mt->total_height) {
133 free(mt);
134 return NULL;
135 }
136
137 mt->region = intel_region_alloc(intel->intelScreen,
138 tiling,
139 mt->cpp,
140 mt->total_width,
141 mt->total_height,
142 expect_accelerated_upload);
143
144 if (!mt->region) {
145 free(mt);
146 return NULL;
147 }
148
149 return mt;
150 }
151
152
153 struct intel_mipmap_tree *
154 intel_miptree_create_for_region(struct intel_context *intel,
155 GLenum target,
156 gl_format format,
157 struct intel_region *region)
158 {
159 struct intel_mipmap_tree *mt;
160
161 mt = intel_miptree_create_internal(intel, target, format,
162 0, 0,
163 region->width, region->height, 1);
164 if (!mt)
165 return mt;
166
167 intel_region_reference(&mt->region, region);
168
169 return mt;
170 }
171
172 void
173 intel_miptree_reference(struct intel_mipmap_tree **dst,
174 struct intel_mipmap_tree *src)
175 {
176 src->refcount++;
177 *dst = src;
178 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
179 }
180
181
182 void
183 intel_miptree_release(struct intel_context *intel,
184 struct intel_mipmap_tree **mt)
185 {
186 if (!*mt)
187 return;
188
189 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
190 if (--(*mt)->refcount <= 0) {
191 GLuint i;
192
193 DBG("%s deleting %p\n", __FUNCTION__, *mt);
194
195 intel_region_release(&((*mt)->region));
196 intel_region_release(&((*mt)->hiz_region));
197
198 for (i = 0; i < MAX_TEXTURE_LEVELS; i++) {
199 free((*mt)->level[i].x_offset);
200 free((*mt)->level[i].y_offset);
201 }
202
203 free(*mt);
204 }
205 *mt = NULL;
206 }
207
208
209 /**
210 * Can the image be pulled into a unified mipmap tree? This mirrors
211 * the completeness test in a lot of ways.
212 *
213 * Not sure whether I want to pass gl_texture_image here.
214 */
215 GLboolean
216 intel_miptree_match_image(struct intel_mipmap_tree *mt,
217 struct gl_texture_image *image)
218 {
219 struct intel_texture_image *intelImage = intel_texture_image(image);
220 GLuint level = intelImage->base.Base.Level;
221
222 /* Images with borders are never pulled into mipmap trees. */
223 if (image->Border)
224 return GL_FALSE;
225
226 if (image->TexFormat != mt->format)
227 return GL_FALSE;
228
229 /* Test image dimensions against the base level image adjusted for
230 * minification. This will also catch images not present in the
231 * tree, changed targets, etc.
232 */
233 if (image->Width != mt->level[level].width ||
234 image->Height != mt->level[level].height ||
235 image->Depth != mt->level[level].depth)
236 return GL_FALSE;
237
238 return GL_TRUE;
239 }
240
241
242 void
243 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
244 GLuint level,
245 GLuint nr_images,
246 GLuint x, GLuint y,
247 GLuint w, GLuint h, GLuint d)
248 {
249 mt->level[level].width = w;
250 mt->level[level].height = h;
251 mt->level[level].depth = d;
252 mt->level[level].level_x = x;
253 mt->level[level].level_y = y;
254 mt->level[level].nr_images = nr_images;
255
256 DBG("%s level %d size: %d,%d,%d offset %d,%d\n", __FUNCTION__,
257 level, w, h, d, x, y);
258
259 assert(nr_images);
260 assert(!mt->level[level].x_offset);
261
262 mt->level[level].x_offset = malloc(nr_images * sizeof(GLuint));
263 mt->level[level].x_offset[0] = mt->level[level].level_x;
264 mt->level[level].y_offset = malloc(nr_images * sizeof(GLuint));
265 mt->level[level].y_offset[0] = mt->level[level].level_y;
266 }
267
268
269 void
270 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
271 GLuint level, GLuint img,
272 GLuint x, GLuint y)
273 {
274 if (img == 0 && level == 0)
275 assert(x == 0 && y == 0);
276
277 assert(img < mt->level[level].nr_images);
278
279 mt->level[level].x_offset[img] = mt->level[level].level_x + x;
280 mt->level[level].y_offset[img] = mt->level[level].level_y + y;
281
282 DBG("%s level %d img %d pos %d,%d\n",
283 __FUNCTION__, level, img,
284 mt->level[level].x_offset[img], mt->level[level].y_offset[img]);
285 }
286
287
288 void
289 intel_miptree_get_image_offset(struct intel_mipmap_tree *mt,
290 GLuint level, GLuint face, GLuint depth,
291 GLuint *x, GLuint *y)
292 {
293 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB) {
294 *x = mt->level[level].x_offset[face];
295 *y = mt->level[level].y_offset[face];
296 } else if (mt->target == GL_TEXTURE_3D) {
297 *x = mt->level[level].x_offset[depth];
298 *y = mt->level[level].y_offset[depth];
299 } else {
300 *x = mt->level[level].x_offset[0];
301 *y = mt->level[level].y_offset[0];
302 }
303 }
304
305 /**
306 * Map a teximage in a mipmap tree.
307 * \param row_stride returns row stride in bytes
308 * \param image_stride returns image stride in bytes (for 3D textures).
309 * \param image_offsets pointer to array of pixel offsets from the returned
310 * pointer to each depth image
311 * \return address of mapping
312 */
313 GLubyte *
314 intel_miptree_image_map(struct intel_context * intel,
315 struct intel_mipmap_tree * mt,
316 GLuint face,
317 GLuint level,
318 GLuint * row_stride, GLuint * image_offsets)
319 {
320 GLuint x, y;
321
322 if (row_stride)
323 *row_stride = mt->region->pitch * mt->cpp;
324
325 if (mt->target == GL_TEXTURE_3D) {
326 int i;
327
328 for (i = 0; i < mt->level[level].depth; i++) {
329
330 intel_miptree_get_image_offset(mt, level, face, i,
331 &x, &y);
332 image_offsets[i] = x + y * mt->region->pitch;
333 }
334
335 DBG("%s \n", __FUNCTION__);
336
337 return intel_region_map(intel, mt->region);
338 } else {
339 assert(mt->level[level].depth == 1);
340 intel_miptree_get_image_offset(mt, level, face, 0,
341 &x, &y);
342 image_offsets[0] = 0;
343
344 DBG("%s: (%d,%d) -> (%d, %d)/%d\n",
345 __FUNCTION__, face, level, x, y, mt->region->pitch * mt->cpp);
346
347 return intel_region_map(intel, mt->region) +
348 (x + y * mt->region->pitch) * mt->cpp;
349 }
350 }
351
352
353 void
354 intel_miptree_image_unmap(struct intel_context *intel,
355 struct intel_mipmap_tree *mt)
356 {
357 DBG("%s\n", __FUNCTION__);
358 intel_region_unmap(intel, mt->region);
359 }
360
361
362 /**
363 * Upload data for a particular image.
364 */
365 void
366 intel_miptree_image_data(struct intel_context *intel,
367 struct intel_mipmap_tree *dst,
368 GLuint face,
369 GLuint level,
370 void *src,
371 GLuint src_row_pitch,
372 GLuint src_image_pitch)
373 {
374 const GLuint depth = dst->level[level].depth;
375 GLuint i;
376
377 for (i = 0; i < depth; i++) {
378 GLuint dst_x, dst_y, height, width;
379
380 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
381
382 height = dst->level[level].height;
383 width = dst->level[level].width;
384 if (dst->compressed) {
385 unsigned int align_w, align_h;
386
387 intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
388 height = (height + align_h - 1) / align_h;
389 width = ALIGN(width, align_w);
390 }
391
392 DBG("%s: %d/%d %p/%d -> (%d, %d)/%d (%d, %d)\n",
393 __FUNCTION__, face, level,
394 src, src_row_pitch * dst->cpp,
395 dst_x, dst_y, dst->region->pitch * dst->cpp,
396 width, height);
397
398 intel_region_data(intel,
399 dst->region, 0, dst_x, dst_y,
400 src,
401 src_row_pitch,
402 0, 0, /* source x, y */
403 width, height);
404
405 src = (char *)src + src_image_pitch * dst->cpp;
406 }
407 }
408
409
410 /**
411 * Copy mipmap image between trees
412 */
413 void
414 intel_miptree_image_copy(struct intel_context *intel,
415 struct intel_mipmap_tree *dst,
416 GLuint face, GLuint level,
417 struct intel_mipmap_tree *src)
418 {
419 GLuint width = src->level[level].width;
420 GLuint height = src->level[level].height;
421 GLuint depth = src->level[level].depth;
422 GLuint src_x, src_y, dst_x, dst_y;
423 GLuint i;
424 GLboolean success;
425
426 if (dst->compressed) {
427 GLuint align_w, align_h;
428
429 intel_get_texture_alignment_unit(dst->format, &align_w, &align_h);
430 height = (height + 3) / 4;
431 width = ALIGN(width, align_w);
432 }
433
434 intel_prepare_render(intel);
435
436 for (i = 0; i < depth; i++) {
437 intel_miptree_get_image_offset(src, level, face, i, &src_x, &src_y);
438 intel_miptree_get_image_offset(dst, level, face, i, &dst_x, &dst_y);
439 success = intel_region_copy(intel,
440 dst->region, 0, dst_x, dst_y,
441 src->region, 0, src_x, src_y,
442 width, height, GL_FALSE,
443 GL_COPY);
444 if (!success) {
445 GLubyte *src_ptr, *dst_ptr;
446
447 src_ptr = intel_region_map(intel, src->region);
448 dst_ptr = intel_region_map(intel, dst->region);
449
450 _mesa_copy_rect(dst_ptr,
451 dst->cpp,
452 dst->region->pitch,
453 dst_x, dst_y, width, height,
454 src_ptr,
455 src->region->pitch,
456 src_x, src_y);
457 intel_region_unmap(intel, src->region);
458 intel_region_unmap(intel, dst->region);
459 }
460 }
461 }