(Stephane Marchesin, me) add hyperz support to radeon and r200 drivers. Only fast...
[mesa.git] / src / mesa / drivers / dri / i830 / i830_texmem.c
1 /**************************************************************************
2
3 Copyright 2001 2d3d Inc., Delray Beach, FL
4
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, VA LINUX SYSTEMS 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 /* $XFree86: xc/lib/GL/mesa/src/drv/i830/i830_texmem.c,v 1.3 2002/12/10 01:26:53 dawes Exp $ */
29
30 /*
31 * Author:
32 * Jeff Hartmann <jhartmann@2d3d.com>
33 *
34 * Heavily based on the I810 driver, which was written by:
35 * Keith Whitwell <keithw@tungstengraphics.com>
36 */
37
38 #include "glheader.h"
39 #include "macros.h"
40 #include "mtypes.h"
41 #include "simple_list.h"
42 #include "enums.h"
43 #include "texformat.h"
44
45 #include "i830_screen.h"
46 #include "i830_dri.h"
47
48 #include "i830_context.h"
49 #include "i830_tex.h"
50 #include "i830_state.h"
51 #include "i830_ioctl.h"
52
53
54 void i830DestroyTexObj(i830ContextPtr imesa, i830TextureObjectPtr t)
55 {
56 unsigned i;
57
58
59 /* See if it was the driver's current object.
60 */
61 if ( imesa != NULL ) {
62 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
63 if ( t == imesa->CurrentTexObj[ i ] ) {
64 imesa->CurrentTexObj[ i ] = NULL;
65 imesa->dirty &= ~I830_UPLOAD_TEX_N( i );
66 }
67 }
68 }
69 }
70
71 #if defined(i386) || defined(__i386__)
72 /* From linux kernel i386 header files, copes with odd sizes better
73 * than COPY_DWORDS would:
74 */
75 static __inline__ void * __memcpy(void * to, const void * from, size_t n)
76 {
77 int d0, d1, d2;
78 __asm__ __volatile__(
79 "rep ; movsl\n\t"
80 "testb $2,%b4\n\t"
81 "je 1f\n\t"
82 "movsw\n"
83 "1:\ttestb $1,%b4\n\t"
84 "je 2f\n\t"
85 "movsb\n"
86 "2:"
87 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
88 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
89 : "memory");
90 return (to);
91 }
92 #else
93 /* Allow compilation on other architectures */
94 #define __memcpy memcpy
95 #endif
96
97 /* Upload an image from mesa's internal copy.
98 */
99 static void i830UploadTexLevel( i830ContextPtr imesa,
100 i830TextureObjectPtr t, int hwlevel )
101 {
102 const struct gl_texture_image *image = t->image[0][hwlevel].image;
103 int j;
104
105 if (!image || !image->Data)
106 return;
107
108 if (image->IsCompressed) {
109 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
110 GLubyte *src = (GLubyte *)image->Data;
111
112 if ((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_FXT1)
113 {
114 for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
115 __memcpy(dst, src, (image->Width*2) );
116 src += image->Width*2;
117 }
118 }
119 else if ((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_DXT1)
120 {
121 for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
122 __memcpy(dst, src, (image->Width*2) );
123 src += image->Width*2;
124 }
125 }
126 else if (((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_DXT2_3) || ((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_DXT4_5))
127 {
128 for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
129 __memcpy(dst, src, (image->Width*4) );
130 src += image->Width*4;
131 }
132 }
133 }
134 else if (image->Width * image->TexFormat->TexelBytes == t->Pitch) {
135 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
136 GLubyte *src = (GLubyte *)image->Data;
137
138 memcpy( dst, src, t->Pitch * image->Height );
139 }
140 else switch (image->TexFormat->TexelBytes) {
141 case 1:
142 {
143 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
144 GLubyte *src = (GLubyte *)image->Data;
145
146 for (j = 0 ; j < image->Height ; j++, dst += t->Pitch) {
147 __memcpy(dst, src, image->Width );
148 src += image->Width;
149 }
150 }
151 break;
152
153 case 2:
154 {
155 GLushort *dst = (GLushort *)(t->BufAddr + t->image[0][hwlevel].offset);
156 GLushort *src = (GLushort *)image->Data;
157
158 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/2)) {
159 __memcpy(dst, src, image->Width * 2 );
160 src += image->Width;
161 }
162 }
163 break;
164
165 case 4:
166 {
167 GLuint *dst = (GLuint *)(t->BufAddr + t->image[0][hwlevel].offset);
168 GLuint *src = (GLuint *)image->Data;
169
170 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/4)) {
171 __memcpy(dst, src, image->Width * 4 );
172 src += image->Width;
173 }
174 }
175 break;
176
177 default:
178 fprintf(stderr, "%s: Not supported texel size %d\n",
179 __FUNCTION__, image->TexFormat->TexelBytes);
180 }
181 }
182
183
184 /* This is called with the lock held. May have to eject our own and/or
185 * other client's texture objects to make room for the upload.
186 */
187
188 int i830UploadTexImagesLocked( i830ContextPtr imesa, i830TextureObjectPtr t )
189 {
190 int ofs;
191 int i;
192
193 if ( t->base.memBlock == NULL ) {
194 int heap;
195
196 heap = driAllocateTexture( imesa->texture_heaps, imesa->nr_heaps,
197 (driTextureObject *) t );
198 if ( heap == -1 ) {
199 return -1;
200 }
201
202 /* Set the base offset of the texture image */
203 ofs = t->base.memBlock->ofs;
204 t->BufAddr = imesa->i830Screen->tex.map + ofs;
205 t->Setup[I830_TEXREG_TM0S0] = (TM0S0_USE_FENCE |
206 (imesa->i830Screen->textureOffset + ofs));
207
208 for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
209 if (t == imesa->CurrentTexObj[i]) {
210 imesa->dirty |= I830_UPLOAD_TEX_N( i );
211 }
212 }
213 }
214
215
216 /* Let the world know we've used this memory recently.
217 */
218 driUpdateTextureLRU( (driTextureObject *) t );
219
220 if (imesa->texture_heaps[0]->timestamp >= GET_DISPATCH_AGE(imesa))
221 i830WaitAgeLocked( imesa, imesa->texture_heaps[0]->timestamp );
222
223 /* Upload any images that are new */
224 if (t->base.dirty_images[0]) {
225 const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;
226
227 for (i = 0 ; i < numLevels ; i++) {
228 if ( (t->base.dirty_images[0] & (1 << (i+t->base.firstLevel))) != 0 ) {
229 i830UploadTexLevel( imesa, t, i );
230 }
231 }
232 t->base.dirty_images[0] = 0;
233 imesa->sarea->perf_boxes |= I830_BOX_TEXTURE_LOAD;
234 }
235
236 return 0;
237 }