Minor r200 vertex program cleanups. Remove disabled leftovers from r300 vertex progra...
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_validate.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 "mtypes.h"
29 #include "macros.h"
30
31 #include "intel_context.h"
32 #include "intel_mipmap_tree.h"
33 #include "intel_tex.h"
34 #include "bufmgr.h"
35
36 /**
37 * Compute which mipmap levels that really need to be sent to the hardware.
38 * This depends on the base image size, GL_TEXTURE_MIN_LOD,
39 * GL_TEXTURE_MAX_LOD, GL_TEXTURE_BASE_LEVEL, and GL_TEXTURE_MAX_LEVEL.
40 */
41 static void intel_calculate_first_last_level( struct intel_texture_object *intelObj )
42 {
43 struct gl_texture_object *tObj = &intelObj->base;
44 const struct gl_texture_image * const baseImage =
45 tObj->Image[0][tObj->BaseLevel];
46
47 /* These must be signed values. MinLod and MaxLod can be negative numbers,
48 * and having firstLevel and lastLevel as signed prevents the need for
49 * extra sign checks.
50 */
51 int firstLevel;
52 int lastLevel;
53
54 /* Yes, this looks overly complicated, but it's all needed.
55 */
56 switch (tObj->Target) {
57 case GL_TEXTURE_1D:
58 case GL_TEXTURE_2D:
59 case GL_TEXTURE_3D:
60 case GL_TEXTURE_CUBE_MAP:
61 if (tObj->MinFilter == GL_NEAREST || tObj->MinFilter == GL_LINEAR) {
62 /* GL_NEAREST and GL_LINEAR only care about GL_TEXTURE_BASE_LEVEL.
63 */
64 firstLevel = lastLevel = tObj->BaseLevel;
65 }
66 else {
67 /* Currently not taking min/max lod into account here, those
68 * values are programmed as sampler state elsewhere and we
69 * upload the same mipmap levels regardless. Not sure if
70 * this makes sense as it means it isn't possible for the app
71 * to use min/max lod to reduce texture memory pressure:
72 */
73 firstLevel = tObj->BaseLevel;
74 lastLevel = MIN2(tObj->BaseLevel + baseImage->MaxLog2,
75 tObj->MaxLevel);
76 lastLevel = MAX2(firstLevel, lastLevel); /* need at least one level */
77 }
78 break;
79 case GL_TEXTURE_RECTANGLE_NV:
80 case GL_TEXTURE_4D_SGIS:
81 firstLevel = lastLevel = 0;
82 break;
83 default:
84 return;
85 }
86
87 /* save these values */
88 intelObj->firstLevel = firstLevel;
89 intelObj->lastLevel = lastLevel;
90 }
91
92 static void copy_image_data_to_tree( struct intel_context *intel,
93 struct intel_texture_object *intelObj,
94 struct gl_texture_image *texImage,
95 GLuint face,
96 GLuint level)
97 {
98 intel_miptree_image_data(intel,
99 intelObj->mt,
100 face,
101 level,
102 texImage->Data,
103 texImage->RowStride,
104 (texImage->RowStride *
105 texImage->Height *
106 texImage->TexFormat->TexelBytes));
107 }
108
109 static void intel_texture_invalidate( struct intel_texture_object *intelObj )
110 {
111 GLint nr_faces, face;
112 intelObj->dirty = ~0;
113
114 nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
115 for (face = 0; face < nr_faces; face++)
116 intelObj->dirty_images[face] = ~0;
117 }
118
119 static void intel_texture_invalidate_cb( struct intel_context *intel,
120 void *ptr )
121 {
122 intel_texture_invalidate( (struct intel_texture_object *) ptr );
123 }
124
125
126 /*
127 */
128 GLuint intel_finalize_mipmap_tree( struct intel_context *intel,
129 struct gl_texture_object *tObj )
130 {
131 struct intel_texture_object *intelObj = intel_texture_object(tObj);
132
133 GLuint face, i;
134 GLuint nr_faces = 0;
135 struct gl_texture_image *firstImage;
136
137 /* We know/require this is true by now:
138 */
139 assert(intelObj->base.Complete);
140
141 /* What levels must the tree include at a minimum?
142 */
143 if (intelObj->dirty) {
144 intel_calculate_first_last_level( intelObj );
145 /* intel_miptree_destroy(intel, intelObj->mt); */
146 /* intelObj->mt = NULL; */
147 }
148
149 firstImage = intelObj->base.Image[0][intelObj->firstLevel];
150
151 /* Fallback case:
152 */
153 if (firstImage->Border) {
154 if (intelObj->mt) {
155 intel_miptree_destroy(intel, intelObj->mt);
156 intelObj->mt = NULL;
157 /* Set all images dirty:
158 */
159 intel_texture_invalidate(intelObj);
160 }
161 return GL_FALSE;
162 }
163
164
165
166 /* Check tree can hold all active levels. Check tree matches
167 * target, imageFormat, etc.
168 */
169 if (intelObj->mt &&
170 (intelObj->mt->first_level != intelObj->firstLevel ||
171 intelObj->mt->last_level != intelObj->lastLevel ||
172 intelObj->mt->internal_format != firstImage->InternalFormat ||
173 intelObj->mt->width0 != firstImage->Width ||
174 intelObj->mt->height0 != firstImage->Height ||
175 intelObj->mt->depth0 != firstImage->Depth))
176 {
177 intel_miptree_destroy(intel, intelObj->mt);
178 intelObj->mt = NULL;
179
180 /* Set all images dirty:
181 */
182 intel_texture_invalidate(intelObj);
183 }
184
185
186 /* May need to create a new tree:
187 */
188 if (!intelObj->mt) {
189 intelObj->mt = intel_miptree_create(intel,
190 intelObj->base.Target,
191 firstImage->InternalFormat,
192 intelObj->firstLevel,
193 intelObj->lastLevel,
194 firstImage->Width,
195 firstImage->Height,
196 firstImage->Depth,
197 firstImage->TexFormat->TexelBytes,
198 firstImage->IsCompressed);
199
200 /* Tell the buffer manager that we will manage the backing
201 * store, but we still want it to do fencing for us.
202 */
203 bmBufferSetInvalidateCB(intel,
204 intelObj->mt->region->buffer,
205 intel_texture_invalidate_cb,
206 intelObj,
207 GL_FALSE);
208 }
209
210 /* Pull in any images not in the object's tree:
211 */
212 if (intelObj->dirty) {
213 nr_faces = (intelObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
214 for (face = 0; face < nr_faces; face++) {
215 if (intelObj->dirty_images[face]) {
216 for (i = intelObj->firstLevel; i <= intelObj->lastLevel; i++) {
217 struct gl_texture_image *texImage = intelObj->base.Image[face][i];
218
219 /* Need to import images in main memory or held in other trees.
220 */
221 if (intelObj->dirty_images[face] & (1<<i) &&
222 texImage) {
223
224 if (INTEL_DEBUG & DEBUG_TEXTURE)
225 _mesa_printf("copy data from image %d (%p) into object miptree\n",
226 i,
227 texImage->Data);
228
229 copy_image_data_to_tree(intel,
230 intelObj,
231 texImage,
232 face,
233 i);
234
235 }
236 }
237 intelObj->dirty_images[face] = 0;
238 }
239 }
240
241 intelObj->dirty = 0;
242 }
243
244 return GL_TRUE;
245 }