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