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