intel: tell libdrm whether we want a cpu-ready or gpu-ready BO for regions.
[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_chipset.h"
32 #include "main/enums.h"
33
34 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
35
36 static GLenum
37 target_to_target(GLenum target)
38 {
39 switch (target) {
40 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
42 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
44 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
45 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
46 return GL_TEXTURE_CUBE_MAP_ARB;
47 default:
48 return target;
49 }
50 }
51
52 static struct intel_mipmap_tree *
53 intel_miptree_create_internal(struct intel_context *intel,
54 GLenum target,
55 GLenum internal_format,
56 GLuint first_level,
57 GLuint last_level,
58 GLuint width0,
59 GLuint height0,
60 GLuint depth0, GLuint cpp, GLuint compress_byte)
61 {
62 GLboolean ok;
63 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
64
65 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
66 _mesa_lookup_enum_by_nr(target),
67 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
68
69 mt->target = target_to_target(target);
70 mt->internal_format = internal_format;
71 mt->first_level = first_level;
72 mt->last_level = last_level;
73 mt->width0 = width0;
74 mt->height0 = height0;
75 mt->depth0 = depth0;
76 mt->cpp = compress_byte ? compress_byte : cpp;
77 mt->compressed = compress_byte ? 1 : 0;
78 mt->refcount = 1;
79 mt->pitch = 0;
80
81 #ifdef I915
82 if (IS_945(intel->intelScreen->deviceID))
83 ok = i945_miptree_layout(intel, mt);
84 else
85 ok = i915_miptree_layout(intel, mt);
86 #else
87 ok = brw_miptree_layout(intel, mt);
88 #endif
89
90 if (!ok) {
91 free(mt);
92 return NULL;
93 }
94
95 return mt;
96 }
97
98 struct intel_mipmap_tree *
99 intel_miptree_create(struct intel_context *intel,
100 GLenum target,
101 GLenum internal_format,
102 GLuint first_level,
103 GLuint last_level,
104 GLuint width0,
105 GLuint height0,
106 GLuint depth0, GLuint cpp, GLuint compress_byte,
107 GLboolean expect_accelerated_upload)
108 {
109 struct intel_mipmap_tree *mt;
110
111 mt = intel_miptree_create_internal(intel, target, internal_format,
112 first_level, last_level, width0,
113 height0, depth0, cpp, compress_byte);
114 /*
115 * pitch == 0 || height == 0 indicates the null texture
116 */
117 if (!mt || !mt->pitch || !mt->total_height)
118 return NULL;
119
120 mt->region = intel_region_alloc(intel,
121 mt->cpp,
122 mt->pitch,
123 mt->total_height,
124 mt->pitch,
125 expect_accelerated_upload);
126
127 if (!mt->region) {
128 free(mt);
129 return NULL;
130 }
131
132 return mt;
133 }
134
135 struct intel_mipmap_tree *
136 intel_miptree_create_for_region(struct intel_context *intel,
137 GLenum target,
138 GLenum internal_format,
139 GLuint first_level,
140 GLuint last_level,
141 struct intel_region *region,
142 GLuint depth0,
143 GLuint compress_byte)
144 {
145 struct intel_mipmap_tree *mt;
146
147 mt = intel_miptree_create_internal(intel, target, internal_format,
148 first_level, last_level,
149 region->width, region->height, 1,
150 region->cpp, compress_byte);
151 if (!mt)
152 return mt;
153 #if 0
154 if (mt->pitch != region->pitch) {
155 fprintf(stderr,
156 "region pitch (%d) doesn't match mipmap tree pitch (%d)\n",
157 region->pitch, mt->pitch);
158 free(mt);
159 return NULL;
160 }
161 #else
162 /* The mipmap tree pitch is aligned to 64 bytes to make sure render
163 * to texture works, but we don't need that for texturing from a
164 * pixmap. Just override it here. */
165 mt->pitch = region->pitch;
166 #endif
167
168 intel_region_reference(&mt->region, region);
169
170 return mt;
171 }
172
173 /**
174 * intel_miptree_pitch_align:
175 *
176 * @intel: intel context pointer
177 *
178 * @mt: the miptree to compute pitch alignment for
179 *
180 * @pitch: the natural pitch value
181 *
182 * Given @pitch, compute a larger value which accounts for
183 * any necessary alignment required by the device
184 */
185
186 int intel_miptree_pitch_align (struct intel_context *intel,
187 struct intel_mipmap_tree *mt,
188 int pitch)
189 {
190 #ifdef I915
191 GLcontext *ctx = &intel->ctx;
192 #endif
193
194 if (!mt->compressed) {
195 int pitch_align;
196
197 if (intel->ttm) {
198 /* XXX: Align pitch to multiple of 64 bytes for now to allow
199 * render-to-texture to work in all cases. This should probably be
200 * replaced at some point by some scheme to only do this when really
201 * necessary.
202 */
203 pitch_align = 64;
204 } else {
205 pitch_align = 4;
206 }
207
208 pitch = ALIGN(pitch * mt->cpp, pitch_align);
209
210 #ifdef I915
211 /* XXX: At least the i915 seems very upset when the pitch is a multiple
212 * of 1024 and sometimes 512 bytes - performance can drop by several
213 * times. Go to the next multiple of the required alignment for now.
214 */
215 if (!(pitch & 511) &&
216 (pitch + pitch_align) < (1 << ctx->Const.MaxTextureLevels))
217 pitch += pitch_align;
218 #endif
219
220 pitch /= mt->cpp;
221 }
222 return pitch;
223 }
224
225 void
226 intel_miptree_reference(struct intel_mipmap_tree **dst,
227 struct intel_mipmap_tree *src)
228 {
229 src->refcount++;
230 *dst = src;
231 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
232 }
233
234 void
235 intel_miptree_release(struct intel_context *intel,
236 struct intel_mipmap_tree **mt)
237 {
238 if (!*mt)
239 return;
240
241 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
242 if (--(*mt)->refcount <= 0) {
243 GLuint i;
244
245 DBG("%s deleting %p\n", __FUNCTION__, *mt);
246
247 intel_region_release(&((*mt)->region));
248
249 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
250 if ((*mt)->level[i].image_offset)
251 free((*mt)->level[i].image_offset);
252
253 free(*mt);
254 }
255 *mt = NULL;
256 }
257
258
259
260
261 /* Can the image be pulled into a unified mipmap tree. This mirrors
262 * the completeness test in a lot of ways.
263 *
264 * Not sure whether I want to pass gl_texture_image here.
265 */
266 GLboolean
267 intel_miptree_match_image(struct intel_mipmap_tree *mt,
268 struct gl_texture_image *image,
269 GLuint face, GLuint level)
270 {
271 /* Images with borders are never pulled into mipmap trees.
272 */
273 if (image->Border ||
274 ((image->_BaseFormat == GL_DEPTH_COMPONENT) &&
275 ((image->TexObject->WrapS == GL_CLAMP_TO_BORDER) ||
276 (image->TexObject->WrapT == GL_CLAMP_TO_BORDER))))
277 return GL_FALSE;
278
279 if (image->InternalFormat != mt->internal_format ||
280 image->IsCompressed != mt->compressed)
281 return GL_FALSE;
282
283 if (!image->IsCompressed &&
284 !mt->compressed &&
285 image->TexFormat->TexelBytes != mt->cpp)
286 return GL_FALSE;
287
288 /* Test image dimensions against the base level image adjusted for
289 * minification. This will also catch images not present in the
290 * tree, changed targets, etc.
291 */
292 if (image->Width != mt->level[level].width ||
293 image->Height != mt->level[level].height ||
294 image->Depth != mt->level[level].depth)
295 return GL_FALSE;
296
297 return GL_TRUE;
298 }
299
300
301 void
302 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
303 GLuint level,
304 GLuint nr_images,
305 GLuint x, GLuint y,
306 GLuint w, GLuint h, GLuint d)
307 {
308 mt->level[level].width = w;
309 mt->level[level].height = h;
310 mt->level[level].depth = d;
311 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
312 mt->level[level].nr_images = nr_images;
313
314 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
315 level, w, h, d, x, y, mt->level[level].level_offset);
316
317 /* Not sure when this would happen, but anyway:
318 */
319 if (mt->level[level].image_offset) {
320 free(mt->level[level].image_offset);
321 mt->level[level].image_offset = NULL;
322 }
323
324 assert(nr_images);
325
326 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
327 mt->level[level].image_offset[0] = 0;
328 }
329
330
331
332 void
333 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
334 GLuint level, GLuint img,
335 GLuint x, GLuint y)
336 {
337 if (img == 0 && level == 0)
338 assert(x == 0 && y == 0);
339
340 assert(img < mt->level[level].nr_images);
341
342 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp;
343
344 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
345 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
346 }
347
348
349 /* Although we use the image_offset[] array to store relative offsets
350 * to cube faces, Mesa doesn't know anything about this and expects
351 * each cube face to be treated as a separate image.
352 *
353 * These functions present that view to mesa:
354 */
355 const GLuint *
356 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
357 {
358 static const GLuint zero = 0;
359
360 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
361 return &zero;
362 else
363 return mt->level[level].image_offset;
364 }
365
366
367 GLuint
368 intel_miptree_image_offset(struct intel_mipmap_tree *mt,
369 GLuint face, GLuint level)
370 {
371 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
372 return (mt->level[level].level_offset +
373 mt->level[level].image_offset[face]);
374 else
375 return mt->level[level].level_offset;
376 }
377
378
379
380 /**
381 * Map a teximage in a mipmap tree.
382 * \param row_stride returns row stride in bytes
383 * \param image_stride returns image stride in bytes (for 3D textures).
384 * \param image_offsets pointer to array of pixel offsets from the returned
385 * pointer to each depth image
386 * \return address of mapping
387 */
388 GLubyte *
389 intel_miptree_image_map(struct intel_context * intel,
390 struct intel_mipmap_tree * mt,
391 GLuint face,
392 GLuint level,
393 GLuint * row_stride, GLuint * image_offsets)
394 {
395 DBG("%s \n", __FUNCTION__);
396
397 if (row_stride)
398 *row_stride = mt->pitch * mt->cpp;
399
400 if (mt->target == GL_TEXTURE_3D) {
401 int i;
402
403 for (i = 0; i < mt->level[level].depth; i++)
404 image_offsets[i] = mt->level[level].image_offset[i] / mt->cpp;
405 } else {
406 assert(mt->level[level].depth == 1);
407 assert(mt->target == GL_TEXTURE_CUBE_MAP ||
408 mt->level[level].image_offset[0] == 0);
409 image_offsets[0] = 0;
410 }
411
412 return (intel_region_map(intel, mt->region) +
413 intel_miptree_image_offset(mt, face, level));
414 }
415
416 void
417 intel_miptree_image_unmap(struct intel_context *intel,
418 struct intel_mipmap_tree *mt)
419 {
420 DBG("%s\n", __FUNCTION__);
421 intel_region_unmap(intel, mt->region);
422 }
423
424
425
426 /* Upload data for a particular image.
427 */
428 void
429 intel_miptree_image_data(struct intel_context *intel,
430 struct intel_mipmap_tree *dst,
431 GLuint face,
432 GLuint level,
433 void *src,
434 GLuint src_row_pitch,
435 GLuint src_image_pitch)
436 {
437 GLuint depth = dst->level[level].depth;
438 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
439 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
440 GLuint i;
441 GLuint height = 0;
442
443 DBG("%s: %d/%d\n", __FUNCTION__, face, level);
444 for (i = 0; i < depth; i++) {
445 height = dst->level[level].height;
446 if(dst->compressed)
447 height = (height + 3) / 4;
448 intel_region_data(intel,
449 dst->region,
450 dst_offset + dst_depth_offset[i], /* dst_offset */
451 0, 0, /* dstx, dsty */
452 src,
453 src_row_pitch,
454 0, 0, /* source x, y */
455 dst->level[level].width, height); /* width, height */
456
457 src += src_image_pitch * dst->cpp;
458 }
459 }
460
461 extern GLuint intel_compressed_alignment(GLenum);
462 /* Copy mipmap image between trees
463 */
464 void
465 intel_miptree_image_copy(struct intel_context *intel,
466 struct intel_mipmap_tree *dst,
467 GLuint face, GLuint level,
468 struct intel_mipmap_tree *src)
469 {
470 GLuint width = src->level[level].width;
471 GLuint height = src->level[level].height;
472 GLuint depth = src->level[level].depth;
473 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
474 GLuint src_offset = intel_miptree_image_offset(src, face, level);
475 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
476 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
477 GLuint i;
478
479 if (dst->compressed) {
480 GLuint alignment = intel_compressed_alignment(dst->internal_format);
481 height = (height + 3) / 4;
482 width = ((width + alignment - 1) & ~(alignment - 1));
483 }
484
485 for (i = 0; i < depth; i++) {
486 intel_region_copy(intel,
487 dst->region, dst_offset + dst_depth_offset[i],
488 0,
489 0,
490 src->region, src_offset + src_depth_offset[i],
491 0, 0, width, height);
492 }
493
494 }