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