Merge branch 'gallium-0.1' into gallium-0.2
[mesa.git] / src / mesa / drivers / dri / r128 / r128_texmem.c
1 /**************************************************************************
2
3 Copyright 1999, 2000 ATI Technologies Inc. and Precision Insight, Inc.,
4 Cedar Park, Texas.
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 ATI, PRECISION INSIGHT AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Gareth Hughes <gareth@valinux.com>
31 * Kevin E. Martin <martin@valinux.com>
32 * Brian Paul <brianp@valinux.com>
33 */
34
35 #include "r128_context.h"
36 #include "r128_state.h"
37 #include "r128_ioctl.h"
38 #include "r128_tris.h"
39 #include "r128_tex.h"
40
41 #include "context.h"
42 #include "macros.h"
43 #include "simple_list.h"
44 #include "texformat.h"
45 #include "imports.h"
46
47 #define TEX_0 1
48 #define TEX_1 2
49
50
51 /* Destroy hardware state associated with texture `t'.
52 */
53 void r128DestroyTexObj( r128ContextPtr rmesa, r128TexObjPtr t )
54 {
55 unsigned i;
56
57
58 /* See if it was the driver's current object.
59 */
60
61 if ( rmesa != NULL )
62 {
63 for ( i = 0 ; i < rmesa->glCtx->Const.MaxTextureUnits ; i++ )
64 {
65 if ( t == rmesa->CurrentTexObj[ i ] ) {
66 assert( t->base.bound & (1 << i) );
67 rmesa->CurrentTexObj[ i ] = NULL;
68 }
69 }
70 }
71 }
72
73
74 /**
75 * Upload the texture image associated with texture \a t at the specified
76 * level at the address relative to \a start.
77 */
78 static void uploadSubImage( r128ContextPtr rmesa, r128TexObjPtr t,
79 GLint level,
80 GLint x, GLint y, GLint width, GLint height )
81 {
82 struct gl_texture_image *image;
83 int texelsPerDword = 0;
84 int imageWidth, imageHeight;
85 int remaining, rows;
86 int format, dwords;
87 uint32_t pitch, offset;
88 int i;
89
90 /* Ensure we have a valid texture to upload */
91 if ( ( level < 0 ) || ( level > R128_MAX_TEXTURE_LEVELS ) )
92 return;
93
94 image = t->base.tObj->Image[0][level];
95 if ( !image )
96 return;
97
98 switch ( image->TexFormat->TexelBytes ) {
99 case 1: texelsPerDword = 4; break;
100 case 2: texelsPerDword = 2; break;
101 case 4: texelsPerDword = 1; break;
102 }
103
104 #if 1
105 /* FIXME: The subimage index calcs are wrong... */
106 x = 0;
107 y = 0;
108 width = image->Width;
109 height = image->Height;
110 #endif
111
112 imageWidth = image->Width;
113 imageHeight = image->Height;
114
115 format = t->textureFormat >> 16;
116
117 /* The texel upload routines have a minimum width, so force the size
118 * if needed.
119 */
120 if ( imageWidth < texelsPerDword ) {
121 int factor;
122
123 factor = texelsPerDword / imageWidth;
124 imageWidth = texelsPerDword;
125 imageHeight /= factor;
126 if ( imageHeight == 0 ) {
127 /* In this case, the texel converter will actually walk a
128 * texel or two off the end of the image, but normal malloc
129 * alignment should prevent it from ever causing a fault.
130 */
131 imageHeight = 1;
132 }
133 }
134
135 /* We can't upload to a pitch less than 8 texels so we will need to
136 * linearly upload all modified rows for textures smaller than this.
137 * This makes the x/y/width/height different for the blitter and the
138 * texture walker.
139 */
140 if ( imageWidth >= 8 ) {
141 /* The texture walker and the blitter look identical */
142 pitch = imageWidth >> 3;
143 } else {
144 int factor;
145 int y2;
146 int start, end;
147
148 start = (y * imageWidth) & ~7;
149 end = (y + height) * imageWidth;
150
151 if ( end - start < 8 ) {
152 /* Handle the case where the total number of texels
153 * uploaded is < 8.
154 */
155 x = 0;
156 y = start / 8;
157 width = end - start;
158 height = 1;
159 } else {
160 /* Upload some number of full 8 texel blit rows */
161 factor = 8 / imageWidth;
162
163 y2 = y + height - 1;
164 y /= factor;
165 y2 /= factor;
166
167 x = 0;
168 width = 8;
169 height = y2 - y + 1;
170 }
171
172 /* Fixed pitch of 8 */
173 pitch = 1;
174 }
175
176 dwords = width * height / texelsPerDword;
177 offset = t->bufAddr + t->image[level - t->base.firstLevel].offset;
178
179 #if ENABLE_PERF_BOXES
180 /* Bump the performace counter */
181 rmesa->c_textureBytes += (dwords << 2);
182 #endif
183
184 if ( R128_DEBUG & DEBUG_VERBOSE_API ) {
185 fprintf( stderr, "r128UploadSubImage: %d,%d of %d,%d at %d,%d\n",
186 width, height, image->Width, image->Height, x, y );
187 fprintf( stderr, " blit ofs: 0x%07x pitch: 0x%x dwords: %d "
188 "level: %d format: %x\n",
189 (GLuint)offset, (GLuint)pitch, dwords, level, format );
190 }
191
192 /* Subdivide the texture if required */
193 if ( dwords <= R128_BUFFER_MAX_DWORDS / 2 ) {
194 rows = height;
195 } else {
196 rows = (R128_BUFFER_MAX_DWORDS * texelsPerDword) / (2 * width);
197 }
198
199 for ( i = 0, remaining = height ;
200 remaining > 0 ;
201 remaining -= rows, y += rows, i++ )
202 {
203 uint32_t *dst;
204 drmBufPtr buffer;
205
206 assert(image->Data);
207
208 height = MIN2(remaining, rows);
209
210 /* Grab the indirect buffer for the texture blit */
211 LOCK_HARDWARE( rmesa );
212 buffer = r128GetBufferLocked( rmesa );
213
214 dst = (uint32_t *)((char *)buffer->address + R128_HOSTDATA_BLIT_OFFSET);
215
216 /* Copy the next chunck of the texture image into the blit buffer */
217 {
218 const GLubyte *src = (const GLubyte *) image->Data +
219 (y * image->Width + x) * image->TexFormat->TexelBytes;
220 const GLuint bytes = width * height * image->TexFormat->TexelBytes;
221 memcpy(dst, src, bytes);
222 }
223
224 r128FireBlitLocked( rmesa, buffer,
225 offset, pitch, format,
226 x, y, width, height );
227 UNLOCK_HARDWARE( rmesa );
228 }
229
230 rmesa->new_state |= R128_NEW_CONTEXT;
231 rmesa->dirty |= R128_UPLOAD_CONTEXT | R128_UPLOAD_MASKS;
232 }
233
234
235 /* Upload the texture images associated with texture `t'. This might
236 * require removing our own and/or other client's texture objects to
237 * make room for these images.
238 */
239 void r128UploadTexImages( r128ContextPtr rmesa, r128TexObjPtr t )
240 {
241 const GLint numLevels = t->base.lastLevel - t->base.firstLevel + 1;
242 GLint i;
243
244 if ( R128_DEBUG & DEBUG_VERBOSE_API ) {
245 fprintf( stderr, "%s( %p, %p )\n",
246 __FUNCTION__, (void *) rmesa->glCtx, (void *) t );
247 }
248
249 assert(t);
250
251 LOCK_HARDWARE( rmesa );
252
253 if ( !t->base.memBlock ) {
254 int heap;
255
256
257 heap = driAllocateTexture( rmesa->texture_heaps, rmesa->nr_heaps,
258 (driTextureObject *) t );
259 if ( heap == -1 ) {
260 UNLOCK_HARDWARE( rmesa );
261 return;
262 }
263
264 /* Set the base offset of the texture image */
265 t->bufAddr = rmesa->r128Screen->texOffset[heap]
266 + t->base.memBlock->ofs;
267
268 /* Set texture offsets for each mipmap level */
269 if ( t->setup.tex_cntl & R128_MIP_MAP_DISABLE ) {
270 for ( i = 0 ; i < R128_MAX_TEXTURE_LEVELS ; i++ ) {
271 t->setup.tex_offset[i] = t->bufAddr;
272 }
273 } else {
274 for ( i = 0; i < numLevels; i++ ) {
275 const int j = numLevels - i - 1;
276 t->setup.tex_offset[j] = t->bufAddr + t->image[i].offset;
277 }
278 }
279 }
280
281 /* Let the world know we've used this memory recently.
282 */
283 driUpdateTextureLRU( (driTextureObject *) t );
284 UNLOCK_HARDWARE( rmesa );
285
286 /* Upload any images that are new */
287 if ( t->base.dirty_images[0] ) {
288 for ( i = 0 ; i < numLevels; i++ ) {
289 const GLint j = t->base.firstLevel + i; /* the texObj's level */
290 if ( t->base.dirty_images[0] & (1 << j) ) {
291 uploadSubImage( rmesa, t, j, 0, 0,
292 t->image[i].width, t->image[i].height );
293 }
294 }
295
296 rmesa->setup.tex_cntl_c |= R128_TEX_CACHE_FLUSH;
297 rmesa->dirty |= R128_UPLOAD_CONTEXT;
298 t->base.dirty_images[0] = 0;
299 }
300 }