Move i915tex driver into place as just i915.
[mesa.git] / src / mesa / drivers / dri / i915 / 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 "enums.h"
32
33 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
34
35 static GLenum
36 target_to_target(GLenum target)
37 {
38 switch (target) {
39 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
40 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
41 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
42 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
43 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
44 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
45 return GL_TEXTURE_CUBE_MAP_ARB;
46 default:
47 return target;
48 }
49 }
50
51 struct intel_mipmap_tree *
52 intel_miptree_create(struct intel_context *intel,
53 GLenum target,
54 GLenum internal_format,
55 GLuint first_level,
56 GLuint last_level,
57 GLuint width0,
58 GLuint height0,
59 GLuint depth0, GLuint cpp, GLuint compress_byte)
60 {
61 GLboolean ok;
62 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
63
64 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__,
65 _mesa_lookup_enum_by_nr(target),
66 _mesa_lookup_enum_by_nr(internal_format), first_level, last_level);
67
68 mt->target = target_to_target(target);
69 mt->internal_format = internal_format;
70 mt->first_level = first_level;
71 mt->last_level = last_level;
72 mt->width0 = width0;
73 mt->height0 = height0;
74 mt->depth0 = depth0;
75 mt->cpp = compress_byte ? compress_byte : cpp;
76 mt->compressed = compress_byte ? 1 : 0;
77 mt->refcount = 1;
78
79 switch (intel->intelScreen->deviceID) {
80 case PCI_CHIP_I945_G:
81 case PCI_CHIP_I945_GM:
82 case PCI_CHIP_I945_GME:
83 case PCI_CHIP_G33_G:
84 case PCI_CHIP_Q33_G:
85 case PCI_CHIP_Q35_G:
86 ok = i945_miptree_layout(mt);
87 break;
88 case PCI_CHIP_I915_G:
89 case PCI_CHIP_I915_GM:
90 case PCI_CHIP_I830_M:
91 case PCI_CHIP_I855_GM:
92 case PCI_CHIP_I865_G:
93 default:
94 /* All the i830 chips and the i915 use this layout:
95 */
96 ok = i915_miptree_layout(mt);
97 break;
98 }
99
100 if (ok) {
101 if (!mt->compressed) {
102 /* XXX: Align pitch to multiple of 64 bytes for now to allow
103 * render-to-texture to work in all cases. This should probably be
104 * replaced at some point by some scheme to only do this when really
105 * necessary.
106 */
107 mt->pitch = ((mt->pitch * cpp + 63) & ~63) / cpp;
108 }
109
110 mt->region = intel_region_alloc(intel->intelScreen,
111 mt->cpp, mt->pitch, mt->total_height);
112 }
113
114 if (!mt->region) {
115 free(mt);
116 return NULL;
117 }
118
119 return mt;
120 }
121
122
123 void
124 intel_miptree_reference(struct intel_mipmap_tree **dst,
125 struct intel_mipmap_tree *src)
126 {
127 src->refcount++;
128 *dst = src;
129 DBG("%s %p refcount now %d\n", __FUNCTION__, src, src->refcount);
130 }
131
132 void
133 intel_miptree_release(struct intel_context *intel,
134 struct intel_mipmap_tree **mt)
135 {
136 if (!*mt)
137 return;
138
139 DBG("%s %p refcount will be %d\n", __FUNCTION__, *mt, (*mt)->refcount - 1);
140 if (--(*mt)->refcount <= 0) {
141 GLuint i;
142
143 DBG("%s deleting %p\n", __FUNCTION__, *mt);
144
145 intel_region_release(&((*mt)->region));
146
147 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
148 if ((*mt)->level[i].image_offset)
149 free((*mt)->level[i].image_offset);
150
151 free(*mt);
152 }
153 *mt = NULL;
154 }
155
156
157
158
159 /* Can the image be pulled into a unified mipmap tree. This mirrors
160 * the completeness test in a lot of ways.
161 *
162 * Not sure whether I want to pass gl_texture_image here.
163 */
164 GLboolean
165 intel_miptree_match_image(struct intel_mipmap_tree *mt,
166 struct gl_texture_image *image,
167 GLuint face, GLuint level)
168 {
169 /* Images with borders are never pulled into mipmap trees.
170 */
171 if (image->Border)
172 return GL_FALSE;
173
174 if (image->InternalFormat != mt->internal_format ||
175 image->IsCompressed != mt->compressed)
176 return GL_FALSE;
177
178 /* Test image dimensions against the base level image adjusted for
179 * minification. This will also catch images not present in the
180 * tree, changed targets, etc.
181 */
182 if (image->Width != mt->level[level].width ||
183 image->Height != mt->level[level].height ||
184 image->Depth != mt->level[level].depth)
185 return GL_FALSE;
186
187 return GL_TRUE;
188 }
189
190
191 void
192 intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
193 GLuint level,
194 GLuint nr_images,
195 GLuint x, GLuint y, GLuint w, GLuint h, GLuint d)
196 {
197
198 mt->level[level].width = w;
199 mt->level[level].height = h;
200 mt->level[level].depth = d;
201 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
202 mt->level[level].nr_images = nr_images;
203
204 DBG("%s level %d size: %d,%d,%d offset %d,%d (0x%x)\n", __FUNCTION__,
205 level, w, h, d, x, y, mt->level[level].level_offset);
206
207 /* Not sure when this would happen, but anyway:
208 */
209 if (mt->level[level].image_offset) {
210 free(mt->level[level].image_offset);
211 mt->level[level].image_offset = NULL;
212 }
213
214 assert(nr_images);
215
216 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
217 mt->level[level].image_offset[0] = 0;
218 }
219
220
221
222 void
223 intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
224 GLuint level, GLuint img, GLuint x, GLuint y)
225 {
226 if (img == 0 && level == 0)
227 assert(x == 0 && y == 0);
228
229 assert(img < mt->level[level].nr_images);
230
231 mt->level[level].image_offset[img] = (x + y * mt->pitch);
232
233 DBG("%s level %d img %d pos %d,%d image_offset %x\n",
234 __FUNCTION__, level, img, x, y, mt->level[level].image_offset[img]);
235 }
236
237
238 /* Although we use the image_offset[] array to store relative offsets
239 * to cube faces, Mesa doesn't know anything about this and expects
240 * each cube face to be treated as a separate image.
241 *
242 * These functions present that view to mesa:
243 */
244 const GLuint *
245 intel_miptree_depth_offsets(struct intel_mipmap_tree *mt, GLuint level)
246 {
247 static const GLuint zero = 0;
248
249 if (mt->target != GL_TEXTURE_3D || mt->level[level].nr_images == 1)
250 return &zero;
251 else
252 return mt->level[level].image_offset;
253 }
254
255
256 GLuint
257 intel_miptree_image_offset(struct intel_mipmap_tree * mt,
258 GLuint face, GLuint level)
259 {
260 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
261 return (mt->level[level].level_offset +
262 mt->level[level].image_offset[face] * mt->cpp);
263 else
264 return mt->level[level].level_offset;
265 }
266
267
268
269 /**
270 * Map a teximage in a mipmap tree.
271 * \param row_stride returns row stride in bytes
272 * \param image_stride returns image stride in bytes (for 3D textures).
273 * \return address of mapping
274 */
275 GLubyte *
276 intel_miptree_image_map(struct intel_context * intel,
277 struct intel_mipmap_tree * mt,
278 GLuint face,
279 GLuint level,
280 GLuint * row_stride, GLuint * image_offsets)
281 {
282 DBG("%s \n", __FUNCTION__);
283
284 if (row_stride)
285 *row_stride = mt->pitch * mt->cpp;
286
287 if (image_offsets)
288 memcpy(image_offsets, mt->level[level].image_offset,
289 mt->level[level].depth * sizeof(GLuint));
290
291 return (intel_region_map(intel->intelScreen, mt->region) +
292 intel_miptree_image_offset(mt, face, level));
293 }
294
295 void
296 intel_miptree_image_unmap(struct intel_context *intel,
297 struct intel_mipmap_tree *mt)
298 {
299 DBG("%s\n", __FUNCTION__);
300 intel_region_unmap(intel->intelScreen, mt->region);
301 }
302
303
304
305 /* Upload data for a particular image.
306 */
307 void
308 intel_miptree_image_data(struct intel_context *intel,
309 struct intel_mipmap_tree *dst,
310 GLuint face,
311 GLuint level,
312 void *src,
313 GLuint src_row_pitch, GLuint src_image_pitch)
314 {
315 GLuint depth = dst->level[level].depth;
316 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
317 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
318 GLuint i;
319 GLuint height = 0;
320
321 DBG("%s\n", __FUNCTION__);
322 for (i = 0; i < depth; i++) {
323 height = dst->level[level].height;
324 if(dst->compressed)
325 height /= 4;
326 intel_region_data(intel->intelScreen, dst->region,
327 dst_offset + dst_depth_offset[i], /* dst_offset */
328 0, 0, /* dstx, dsty */
329 src,
330 src_row_pitch,
331 0, 0, /* source x, y */
332 dst->level[level].width, height); /* width, height */
333
334 src += src_image_pitch * dst->cpp;
335 }
336 }
337
338 extern GLuint intel_compressed_alignment(GLenum);
339 /* Copy mipmap image between trees
340 */
341 void
342 intel_miptree_image_copy(struct intel_context *intel,
343 struct intel_mipmap_tree *dst,
344 GLuint face, GLuint level,
345 struct intel_mipmap_tree *src)
346 {
347 GLuint width = src->level[level].width;
348 GLuint height = src->level[level].height;
349 GLuint depth = src->level[level].depth;
350 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
351 GLuint src_offset = intel_miptree_image_offset(src, face, level);
352 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
353 const GLuint *src_depth_offset = intel_miptree_depth_offsets(src, level);
354 GLuint i;
355
356 if (dst->compressed) {
357 GLuint alignment = intel_compressed_alignment(dst->internal_format);
358 height = (height + 3) / 4;
359 width = ((width + alignment - 1) & ~(alignment - 1));
360 }
361
362 for (i = 0; i < depth; i++) {
363 intel_region_copy(intel->intelScreen,
364 dst->region, dst_offset + dst_depth_offset[i],
365 0,
366 0,
367 src->region, src_offset + src_depth_offset[i],
368 0, 0, width, height);
369 }
370
371 }