edca84c64e7833577d3030888427e70f577d0c41
[mesa.git] / src / mesa / drivers / dri / i965 / 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 "dri_bufmgr.h"
32 #include "enums.h"
33 #include "imports.h"
34
35 #define FILE_DEBUG_FLAG DEBUG_MIPTREE
36
37 static GLenum 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 struct intel_mipmap_tree *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,
60 GLuint cpp,
61 GLboolean compressed)
62 {
63 GLboolean ok;
64 struct intel_mipmap_tree *mt = calloc(sizeof(*mt), 1);
65
66 if (INTEL_DEBUG & DEBUG_TEXTURE)
67 _mesa_printf("%s target %s format %s level %d..%d\n", __FUNCTION__,
68 _mesa_lookup_enum_by_nr(target),
69 _mesa_lookup_enum_by_nr(internal_format),
70 first_level,
71 last_level);
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 = cpp;
81 mt->compressed = compressed;
82
83 switch (intel->intelScreen->deviceID) {
84 #if 0
85 case PCI_CHIP_I945_G:
86 ok = i945_miptree_layout( mt );
87 break;
88 case PCI_CHIP_I915_G:
89 case PCI_CHIP_I915_GM:
90 ok = i915_miptree_layout( mt );
91 break;
92 #endif
93 default:
94 if (INTEL_DEBUG & DEBUG_TEXTURE)
95 _mesa_printf("assuming BRW texture layouts\n");
96 ok = brw_miptree_layout( mt );
97 break;
98 }
99
100 if (ok)
101 mt->region = intel_region_alloc( intel,
102 mt->cpp,
103 mt->pitch,
104 mt->total_height );
105
106 if (!mt->region) {
107 free(mt);
108 return NULL;
109 }
110
111 return mt;
112 }
113
114
115
116 void intel_miptree_destroy( struct intel_context *intel,
117 struct intel_mipmap_tree *mt )
118 {
119 if (mt) {
120 GLuint i;
121
122 intel_region_release(intel, &(mt->region));
123
124 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
125 if (mt->level[i].image_offset)
126 free(mt->level[i].image_offset);
127
128 free(mt);
129 }
130 }
131
132
133
134
135 void intel_miptree_set_level_info(struct intel_mipmap_tree *mt,
136 GLuint level,
137 GLuint nr_images,
138 GLuint x, GLuint y,
139 GLuint w, GLuint h, GLuint d)
140 {
141 mt->level[level].width = w;
142 mt->level[level].height = h;
143 mt->level[level].depth = d;
144 mt->level[level].level_offset = (x + y * mt->pitch) * mt->cpp;
145 mt->level[level].nr_images = nr_images;
146
147 if (INTEL_DEBUG & DEBUG_TEXTURE)
148 _mesa_printf("%s level %d img size: %d,%d level_offset 0x%x\n", __FUNCTION__, level, w, h,
149 mt->level[level].level_offset);
150
151 /* Not sure when this would happen, but anyway:
152 */
153 if (mt->level[level].image_offset) {
154 free(mt->level[level].image_offset);
155 mt->level[level].image_offset = NULL;
156 }
157
158 if (nr_images > 1) {
159 mt->level[level].image_offset = malloc(nr_images * sizeof(GLuint));
160 mt->level[level].image_offset[0] = 0;
161 }
162 }
163
164
165
166 void intel_miptree_set_image_offset(struct intel_mipmap_tree *mt,
167 GLuint level,
168 GLuint img,
169 GLuint x, GLuint y)
170 {
171 if (INTEL_DEBUG & DEBUG_TEXTURE)
172 _mesa_printf("%s level %d img %d pos %d,%d\n", __FUNCTION__, level, img, x, y);
173
174 if (img == 0)
175 assert(x == 0 && y == 0);
176
177 if (img > 0)
178 mt->level[level].image_offset[img] = (x + y * mt->pitch) * mt->cpp;
179 }
180
181
182 /* Although we use the image_offset[] array to store relative offsets
183 * to cube faces, Mesa doesn't know anything about this and expects
184 * each cube face to be treated as a separate image.
185 *
186 * These functions present that view to mesa:
187 */
188 const GLuint *intel_miptree_depth_offsets(struct intel_mipmap_tree *mt,
189 GLuint level)
190 {
191 static const GLuint zero = 0;
192
193 if (mt->target != GL_TEXTURE_3D ||
194 mt->level[level].nr_images == 1)
195 return &zero;
196 else
197 return mt->level[level].image_offset;
198 }
199
200
201 GLuint intel_miptree_image_offset(struct intel_mipmap_tree *mt,
202 GLuint face,
203 GLuint level)
204 {
205 if (mt->target == GL_TEXTURE_CUBE_MAP_ARB)
206 return (mt->level[level].level_offset +
207 mt->level[level].image_offset[face]);
208 else
209 return mt->level[level].level_offset;
210 }
211
212
213
214
215
216 extern GLuint intel_compressed_alignment(GLenum);
217 /* Upload data for a particular image.
218 */
219 GLboolean intel_miptree_image_data(struct intel_context *intel,
220 struct intel_mipmap_tree *dst,
221 GLuint face,
222 GLuint level,
223 const void *src,
224 GLuint src_row_pitch,
225 GLuint src_image_pitch)
226 {
227 GLuint depth = dst->level[level].depth;
228 GLuint dst_offset = intel_miptree_image_offset(dst, face, level);
229 const GLuint *dst_depth_offset = intel_miptree_depth_offsets(dst, level);
230 GLuint i;
231 GLuint width, height, alignment;
232
233 width = dst->level[level].width;
234 height = dst->level[level].height;
235
236 if (dst->compressed) {
237 alignment = intel_compressed_alignment(dst->internal_format);
238 src_row_pitch = ALIGN(src_row_pitch, alignment);
239 width = ALIGN(width, alignment);
240 height = (height + 3) / 4;
241 }
242
243 DBG("%s\n", __FUNCTION__);
244 for (i = 0; i < depth; i++) {
245 if (!intel_region_data(intel,
246 dst->region,
247 dst_offset + dst_depth_offset[i],
248 0,
249 0,
250 src,
251 src_row_pitch,
252 0, 0, /* source x,y */
253 width,
254 height))
255 return GL_FALSE;
256 src += src_image_pitch;
257 }
258 return GL_TRUE;
259 }
260