i965: always call dri_emit_reloc when creating clip unit state
[mesa.git] / src / mesa / drivers / dri / i810 / i810texmem.c
1 /*
2 * GLX Hardware Device Driver for Intel i810
3 * Copyright (C) 1999 Keith Whitwell
4 * Texmem interface changes (C) 2003 Dave Airlie
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * KEITH WHITWELL, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
22 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include "glheader.h"
27 #include "macros.h"
28 #include "mtypes.h"
29 #include "simple_list.h"
30 #include "enums.h"
31 #include "colormac.h"
32 #include "mm.h"
33 #include "texformat.h"
34
35 #include "i810screen.h"
36 #include "i810_dri.h"
37
38 #include "i810context.h"
39 #include "i810tex.h"
40 #include "i810state.h"
41 #include "i810ioctl.h"
42
43
44 void i810DestroyTexObj(i810ContextPtr imesa, i810TextureObjectPtr t)
45 {
46 /* See if it was the driver's current object.
47 */
48 if ( imesa != NULL ) {
49 if (imesa->CurrentTexObj[0] == t) {
50 imesa->CurrentTexObj[0] = 0;
51 imesa->dirty &= ~I810_UPLOAD_TEX0;
52 }
53
54 if (imesa->CurrentTexObj[1] == t) {
55 imesa->CurrentTexObj[1] = 0;
56 imesa->dirty &= ~I810_UPLOAD_TEX1;
57 }
58 }
59 }
60
61
62
63 #if defined(i386) || defined(__i386__)
64 /* From linux kernel i386 header files, copes with odd sizes better
65 * than COPY_DWORDS would:
66 */
67 static __inline__ void * __memcpy(void * to, const void * from, size_t n)
68 {
69 int d0, d1, d2;
70 __asm__ __volatile__(
71 "rep ; movsl\n\t"
72 "testb $2,%b4\n\t"
73 "je 1f\n\t"
74 "movsw\n"
75 "1:\ttestb $1,%b4\n\t"
76 "je 2f\n\t"
77 "movsb\n"
78 "2:"
79 : "=&c" (d0), "=&D" (d1), "=&S" (d2)
80 :"0" (n/4), "q" (n),"1" ((long) to),"2" ((long) from)
81 : "memory");
82 return (to);
83 }
84 #else
85 /* Allow compilation on other architectures */
86 #define __memcpy memcpy
87 #endif
88
89 /* Upload an image from mesa's internal copy.
90 */
91 static void i810UploadTexLevel( i810ContextPtr imesa,
92 i810TextureObjectPtr t, int hwlevel )
93 {
94 const struct gl_texture_image *image = t->image[hwlevel].image;
95 int j;
96
97 if (!image || !image->Data)
98 return;
99
100 if (image->Width * image->TexFormat->TexelBytes == t->Pitch) {
101 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[hwlevel].offset);
102 GLubyte *src = (GLubyte *)image->Data;
103
104 memcpy( dst, src, t->Pitch * image->Height );
105 }
106 else switch (image->TexFormat->TexelBytes) {
107 case 1:
108 {
109 GLubyte *dst = (GLubyte *)(t->BufAddr + t->image[hwlevel].offset);
110 GLubyte *src = (GLubyte *)image->Data;
111
112 for (j = 0 ; j < image->Height ; j++, dst += t->Pitch) {
113 __memcpy(dst, src, image->Width );
114 src += image->Width;
115 }
116 }
117 break;
118
119 case 2:
120 {
121 GLushort *dst = (GLushort *)(t->BufAddr + t->image[hwlevel].offset);
122 GLushort *src = (GLushort *)image->Data;
123
124 for (j = 0 ; j < image->Height ; j++, dst += (t->Pitch/2)) {
125 __memcpy(dst, src, image->Width * 2 );
126 src += image->Width;
127 }
128 }
129 break;
130
131 default:
132 fprintf(stderr, "%s: Not supported texel size %d\n",
133 __FUNCTION__, image->TexFormat->TexelBytes);
134 }
135 }
136
137 /* This is called with the lock held. May have to eject our own and/or
138 * other client's texture objects to make room for the upload.
139 */
140 int i810UploadTexImagesLocked( i810ContextPtr imesa, i810TextureObjectPtr t )
141 {
142 int i;
143 int ofs;
144 int numLevels;
145
146 /* Do we need to eject LRU texture objects?
147 */
148 if (!t->base.memBlock) {
149 int heap;
150
151 heap = driAllocateTexture( imesa->texture_heaps, imesa->nr_heaps,
152 (driTextureObject *) t);
153
154 if ( heap == -1 ) {
155 return -1;
156 }
157
158 ofs = t->base.memBlock->ofs;
159 t->BufAddr = imesa->i810Screen->tex.map + ofs;
160 t->Setup[I810_TEXREG_MI3] = imesa->i810Screen->textureOffset + ofs;
161
162 if (t == imesa->CurrentTexObj[0])
163 I810_STATECHANGE(imesa, I810_UPLOAD_TEX0);
164
165 if (t == imesa->CurrentTexObj[1])
166 I810_STATECHANGE(imesa, I810_UPLOAD_TEX1);
167
168 /* i810UpdateTexLRU( imesa, t );*/
169 }
170 driUpdateTextureLRU( (driTextureObject *) t );
171
172 if (imesa->texture_heaps[0]->timestamp >= GET_DISPATCH_AGE(imesa))
173 i810WaitAgeLocked( imesa, imesa->texture_heaps[0]->timestamp );
174
175 numLevels = t->base.lastLevel - t->base.firstLevel + 1;
176 for (i = 0 ; i < numLevels ; i++)
177 if (t->base.dirty_images[0] & (1<<i))
178 i810UploadTexLevel( imesa, t, i );
179
180 t->base.dirty_images[0] = 0;
181
182 return 0;
183 }