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