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