summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/i830/i830_texmem.c
blob: 7faf90874ce1099dc95823fe041b2c38b9334e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**************************************************************************

Copyright 2001 2d3d Inc., Delray Beach, FL

All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
on the rights to use, copy, modify, merge, publish, distribute, sub
license, and/or sell copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
ATI, VA LINUX SYSTEMS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.

**************************************************************************/

/* $XFree86: xc/lib/GL/mesa/src/drv/i830/i830_texmem.c,v 1.3 2002/12/10 01:26:53 dawes Exp $ */

/*
 * Author:
 *   Jeff Hartmann <jhartmann@2d3d.com>
 *
 * Heavily based on the I810 driver, which was written by:
 *   Keith Whitwell <keithw@tungstengraphics.com>
 */

#include "glheader.h"
#include "macros.h"
#include "mtypes.h"
#include "simple_list.h"
#include "enums.h"
#include "texformat.h"

#include "i830_screen.h"
#include "i830_dri.h"

#include "i830_context.h"
#include "i830_tex.h"
#include "i830_state.h"
#include "i830_ioctl.h"


void i830DestroyTexObj(i830ContextPtr imesa, i830TextureObjectPtr t)
{
   unsigned   i;


   /* See if it was the driver's current object.
    */
   if ( imesa != NULL ) { 
      for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
	 if ( t == imesa->CurrentTexObj[ i ] ) {
	    imesa->CurrentTexObj[ i ] = NULL;
	    imesa->dirty &= ~I830_UPLOAD_TEX_N( i );
	 }
      }
   }
}

#if defined(i386) || defined(__i386__)
/* From linux kernel i386 header files, copes with odd sizes better
 * than COPY_DWORDS would:
 */
static __inline__ void * __memcpy(void * to, const void * from, size_t n)
{
int d0, d1, d2;
__asm__ __volatile__(
	"rep ; movsl\n\t"
	"testb $2,%b4\n\t"
	"je 1f\n\t"
	"movsw\n"
	"1:\ttestb $1,%b4\n\t"
	"je 2f\n\t"
	"movsb\n"
	"2:"
	: "=&c" (d0), "=&D" (d1), "=&S" (d2)
	:"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
	: "memory");
return (to);
}
#else
/* Allow compilation on other architectures */
#define __memcpy memcpy
#endif

/* Upload an image from mesa's internal copy.
 */
static void i830UploadTexLevel( i830ContextPtr imesa,
				i830TextureObjectPtr t, int hwlevel )
{
   const struct gl_texture_image *image = t->image[0][hwlevel].image;
   int j;

   if (!image || !image->Data)
      return;

   if (image->IsCompressed) {
	 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
	 GLubyte *src = (GLubyte *)image->Data;

	 if ((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_FXT1)
	   {
	     for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
	       __memcpy(dst, src, (image->Width*2) );
	       src += image->Width*2;
	     }
	   }
	 else if ((t->Setup[I830_TEXREG_TM0S1] & TM0S1_MT_FORMAT_MASK)==MT_COMPRESS_DXT1)
	   {
	     for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
	       __memcpy(dst, src, (image->Width*2) );
	       src += image->Width*2;
	     }
	   }
	 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))
	   {
	     for (j = 0 ; j < image->Height/4 ; j++, dst += (t->Pitch)) {
	       __memcpy(dst, src, (image->Width*4) );
	       src += image->Width*4;
	     }
	   }
   }
   else if (image->Width * image->TexFormat->TexelBytes == t->Pitch) {
	 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
	 GLubyte *src = (GLubyte *)image->Data;
	 
	 memcpy( dst, src, t->Pitch * image->Height );
   }
   else switch (image->TexFormat->TexelBytes) {
   case 1:
      {
	 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[0][hwlevel].offset);
	 GLubyte *src = (GLubyte *)image->Data;

	 for (j = 0 ; j < image->Height ; j++, dst += t->Pitch) {
	    __memcpy(dst, src, image->Width );
	    src += image->Width;
	 }
      }
      break;

   case 2:
      {
	 GLushort *dst = (GLushort *)(t->BufAddr + t->image[0][hwlevel].offset);
	 GLushort *src = (GLushort *)image->Data;

	 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/2)) {
	    __memcpy(dst, src, image->Width * 2 );
	    src += image->Width;
	 }
      }
      break;

   case 4:
      {
	 GLuint *dst = (GLuint *)(t->BufAddr + t->image[0][hwlevel].offset);
	 GLuint *src = (GLuint *)image->Data;

	 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/4)) {
	    __memcpy(dst, src, image->Width * 4 );
	    src += image->Width;
	 }
      }
      break;

   default:
      fprintf(stderr, "%s: Not supported texel size %d\n",
	      __FUNCTION__, image->TexFormat->TexelBytes);
   }
}


/* This is called with the lock held.  May have to eject our own and/or
 * other client's texture objects to make room for the upload.
 */

int i830UploadTexImagesLocked( i830ContextPtr imesa, i830TextureObjectPtr t )
{
   int ofs;
   int i;

   if ( t->base.memBlock == NULL ) {
      int heap;

      heap = driAllocateTexture( imesa->texture_heaps, imesa->nr_heaps,
				 (driTextureObject *) t );
      if ( heap == -1 ) {
	 return -1;
      }

      /* Set the base offset of the texture image */
      ofs = t->base.memBlock->ofs;
      t->BufAddr = imesa->i830Screen->tex.map + ofs;
      t->Setup[I830_TEXREG_TM0S0] = (TM0S0_USE_FENCE |
				     (imesa->i830Screen->textureOffset + ofs));

      for ( i = 0 ; i < imesa->glCtx->Const.MaxTextureUnits ; i++ ) {
	 if (t == imesa->CurrentTexObj[i]) {
	     imesa->dirty |= I830_UPLOAD_TEX_N( i );
	 }
      }
   }


   /* Let the world know we've used this memory recently.
    */
   driUpdateTextureLRU( (driTextureObject *) t );

   if (imesa->texture_heaps[0]->timestamp >= GET_DISPATCH_AGE(imesa)) 
      i830WaitAgeLocked( imesa, imesa->texture_heaps[0]->timestamp ); 

   /* Upload any images that are new */
   if (t->base.dirty_images[0]) {
      const int numLevels = t->base.lastLevel - t->base.firstLevel + 1;

      for (i = 0 ; i < numLevels ; i++) { 
         if ( (t->base.dirty_images[0] & (1 << (i+t->base.firstLevel))) != 0 ) {
	    i830UploadTexLevel( imesa, t, i );
         }
      }
      t->base.dirty_images[0] = 0;
      imesa->sarea->perf_boxes |= I830_BOX_TEXTURE_LOAD;
   }

   return 0;
}