Large update
[mesa.git] / src / mesa / drivers / dri / unichrome / via_tex.c
1 /*
2 * Copyright 1998-2003 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2003 S3 Graphics, Inc. All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * VIA, S3 GRAPHICS, AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25
26 #include <stdlib.h>
27 #include <stdio.h>
28
29 #include "glheader.h"
30 #include "mtypes.h"
31 #include "simple_list.h"
32 #include "enums.h"
33 #include "teximage.h"
34 #include "texobj.h"
35 #include "texstore.h"
36 #include "texformat.h"
37 #include "swrast/swrast.h"
38 #include "context.h"
39 #include "via_context.h"
40 #include "via_tex.h"
41 #include "via_state.h"
42 #include "via_ioctl.h"
43
44
45 /*
46 * Compute the 'S2.4' lod bias factor from the floating point OpenGL bias.
47 */
48 /*
49 static GLuint viaComputeLodBias(GLfloat bias)
50 {
51 int b = (int)(bias * 16.0) + 12;
52 if (b > 63)
53 b = 63;
54 else if (b < -64)
55 b = -64;
56 return (GLuint)(b & MLC_LOD_BIAS_MASK);
57 }
58 */
59
60 viaTextureObjectPtr viaAllocTextureObject(struct gl_texture_object *texObj)
61 {
62 viaTextureObjectPtr t;
63
64 t = (viaTextureObjectPtr)CALLOC_STRUCT(via_texture_object_t);
65 if (!t)
66 return NULL;
67
68 /* Initialize non-image-dependent parts of the state:
69 */
70 t->bufAddr = NULL;
71 t->dirtyImages = ~0;
72 t->actualLevel = 0;
73 t->globj = texObj;
74 make_empty_list(t);
75
76 return t;
77 }
78
79 static void viaTexParameter(GLcontext *ctx, GLenum target,
80 struct gl_texture_object *texObj,
81 GLenum pname, const GLfloat *params)
82 {
83 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
84 if (!t)
85 return;
86
87 if (target != GL_TEXTURE_2D)
88 return;
89 }
90
91 static void viaTexEnv(GLcontext *ctx, GLenum target,
92 GLenum pname, const GLfloat *param)
93 {
94 viaContextPtr vmesa = VIA_CONTEXT(ctx);
95 vmesa = vmesa;
96 }
97
98 static void viaTexImage1D(GLcontext *ctx, GLenum target, GLint level,
99 GLint internalFormat,
100 GLint width, GLint border,
101 GLenum format, GLenum type,
102 const GLvoid *pixels,
103 const struct gl_pixelstore_attrib *packing,
104 struct gl_texture_object *texObj,
105 struct gl_texture_image *texImage)
106 {
107 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
108 if (VIA_DEBUG) fprintf(stderr, "viaTexImage1D - in\n");
109 if (t) {
110 if (level == 0) {
111 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
112 t->actualLevel = 0;
113 }
114 else
115 t->actualLevel++;
116 }
117 else {
118 t = viaAllocTextureObject(texObj);
119 if (!t) {
120 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaTexImage1D");
121 return;
122 }
123 texObj->DriverData = t;
124 }
125 _mesa_store_teximage1d(ctx, target, level, internalFormat,
126 width, border, format, type,
127 pixels, packing, texObj, texImage);
128 if (VIA_DEBUG) fprintf(stderr, "viaTexImage1D - out\n");
129 }
130 static void viaTexSubImage1D(GLcontext *ctx,
131 GLenum target,
132 GLint level,
133 GLint xoffset,
134 GLsizei width,
135 GLenum format, GLenum type,
136 const GLvoid *pixels,
137 const struct gl_pixelstore_attrib *packing,
138 struct gl_texture_object *texObj,
139 struct gl_texture_image *texImage)
140
141 {
142 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
143
144 if (t) {
145 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
146 }
147 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
148 format, type, pixels, packing, texObj,
149 texImage);
150
151 }
152
153
154 static void viaTexImage2D(GLcontext *ctx, GLenum target, GLint level,
155 GLint internalFormat,
156 GLint width, GLint height, GLint border,
157 GLenum format, GLenum type, const GLvoid *pixels,
158 const struct gl_pixelstore_attrib *packing,
159 struct gl_texture_object *texObj,
160 struct gl_texture_image *texImage)
161 {
162 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
163 if (VIA_DEBUG) fprintf(stderr, "viaTexImage2D - in\n");
164 if (t) {
165 if (level == 0) {
166 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
167 t->actualLevel = 0;
168 }
169 else
170 t->actualLevel++;
171 }
172 else {
173 t = viaAllocTextureObject(texObj);
174 if (!t) {
175 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaTexImage2D");
176 return;
177 }
178 texObj->DriverData = t;
179 }
180 _mesa_store_teximage2d(ctx, target, level, internalFormat,
181 width, height, border, format, type,
182 pixels, packing, texObj, texImage);
183 if (VIA_DEBUG) fprintf(stderr, "viaTexImage2D - out\n");
184 }
185
186 static void viaTexSubImage2D(GLcontext *ctx,
187 GLenum target,
188 GLint level,
189 GLint xoffset, GLint yoffset,
190 GLsizei width, GLsizei height,
191 GLenum format, GLenum type,
192 const GLvoid *pixels,
193 const struct gl_pixelstore_attrib *packing,
194 struct gl_texture_object *texObj,
195 struct gl_texture_image *texImage)
196 {
197 viaContextPtr vmesa = VIA_CONTEXT(ctx);
198
199 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
200
201 if (t) {
202 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
203 }
204 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
205 height, format, type, pixels, packing, texObj,
206 texImage);
207
208 if(vmesa->shareCtx)
209 vmesa->shareCtx->NewState |= _NEW_TEXTURE;
210
211 }
212
213 static void viaBindTexture(GLcontext *ctx, GLenum target,
214 struct gl_texture_object *texObj)
215 {
216 if (VIA_DEBUG) fprintf(stderr, "viaBindTexture - in\n");
217 if (target == GL_TEXTURE_2D) {
218 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
219
220 if (!t) {
221
222 t = viaAllocTextureObject(texObj);
223 if (!t) {
224 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaBindTexture");
225 return;
226 }
227 texObj->DriverData = t;
228 }
229 }
230 if (VIA_DEBUG) fprintf(stderr, "viaBindTexture - out\n");
231 }
232
233 static void viaDeleteTexture(GLcontext *ctx, struct gl_texture_object *texObj)
234 {
235 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
236 if (VIA_DEBUG) fprintf(stderr, "viaDeleteTexture - in\n");
237 if (t) {
238 viaContextPtr vmesa = VIA_CONTEXT(ctx);
239 if (vmesa) {
240 if (vmesa->dma) { /* imply vmesa is not under destroying */
241 VIA_FLUSH_DMA(vmesa);
242 }
243 viaDestroyTexObj(vmesa, t);
244 }
245 texObj->DriverData = 0;
246 }
247 if (VIA_DEBUG) fprintf(stderr, "viaDeleteTexture - out\n");
248
249 /* Free mipmap images and the texture object itself */
250 _mesa_delete_texture_object(ctx, texObj);
251 }
252
253 static GLboolean viaIsTextureResident(GLcontext *ctx,
254 struct gl_texture_object *texObj)
255 {
256 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
257
258 return t && t->bufAddr;
259 }
260
261 static const struct gl_texture_format *
262 viaChooseTexFormat(GLcontext *ctx, GLint internalFormat,
263 GLenum format, GLenum type)
264 {
265 viaContextPtr vmesa = VIA_CONTEXT(ctx);
266 (void)format;
267 (void)type;
268 if (VIA_DEBUG) fprintf(stderr, "%s in\n", __FUNCTION__);
269 if (VIA_DEBUG) fprintf(stderr, "internalFormat:%d format:%d\n", internalFormat, format);
270 switch (internalFormat) {
271 case 1:
272 case GL_LUMINANCE:
273 case GL_LUMINANCE4:
274 case GL_LUMINANCE8:
275 case GL_LUMINANCE12:
276 case GL_LUMINANCE16:
277 return &_mesa_texformat_l8;
278 case 2:
279 case GL_LUMINANCE_ALPHA:
280 case GL_LUMINANCE4_ALPHA4:
281 case GL_LUMINANCE6_ALPHA2:
282 case GL_LUMINANCE8_ALPHA8:
283 case GL_LUMINANCE12_ALPHA4:
284 case GL_LUMINANCE12_ALPHA12:
285 case GL_LUMINANCE16_ALPHA16:
286 return &_mesa_texformat_al88;
287 case GL_R3_G3_B2:
288 case GL_RGB4:
289 case GL_RGB5:
290 if (VIA_DEBUG) fprintf(stderr, "2 &_mesa_texformat_arg565\n");
291 return &_mesa_texformat_rgb565;
292 case 3:
293 case GL_RGB:
294 case GL_RGB8:
295 case GL_RGB10:
296 case GL_RGB12:
297 case GL_RGB16:
298 if (vmesa->viaScreen->bitsPerPixel == 0x20) {
299 if (VIA_DEBUG) fprintf(stderr,"3 argb8888\n");
300 return &_mesa_texformat_argb8888;
301 }
302 else {
303 if (VIA_DEBUG) fprintf(stderr,"3 rgb565\n");
304 return &_mesa_texformat_rgb565;
305 }
306 case 4:
307 if (vmesa->viaScreen->bitsPerPixel == 0x20) {
308 if (VIA_DEBUG) fprintf(stderr, "4 &_mesa_texformat_argb8888\n");
309 return &_mesa_texformat_argb8888;
310 }
311 else {
312 if (VIA_DEBUG) fprintf(stderr, "4 &_mesa_texformat_argb4444\n");
313 return &_mesa_texformat_argb4444;
314 }
315 case GL_RGBA2:
316 case GL_RGBA4:
317 if (VIA_DEBUG) fprintf(stderr, "GL_RGBA4 &_mesa_texformat_argb4444\n");
318 return &_mesa_texformat_argb4444;
319
320 case GL_RGB5_A1:
321 if (VIA_DEBUG) fprintf(stderr, "GL_RGB5_A1 &_mesa_texformat_argb1555\n");
322 return &_mesa_texformat_argb1555;
323 case GL_RGBA:
324 case GL_RGBA8:
325 case GL_RGBA12:
326 case GL_RGBA16:
327 case GL_RGB10_A2:
328 if (VIA_DEBUG) fprintf(stderr, "GL_RGBA &_mesa_texformat_argb8888\n");
329 return &_mesa_texformat_argb8888;
330 case GL_ALPHA:
331 case GL_ALPHA4:
332 case GL_ALPHA8:
333 case GL_ALPHA12:
334 case GL_ALPHA16:
335 return &_mesa_texformat_a8;
336 case GL_INTENSITY:
337 case GL_INTENSITY4:
338 case GL_INTENSITY8:
339 case GL_INTENSITY12:
340 case GL_INTENSITY16:
341 return &_mesa_texformat_i8;
342 case GL_COLOR_INDEX:
343 case GL_COLOR_INDEX1_EXT:
344 case GL_COLOR_INDEX2_EXT:
345 case GL_COLOR_INDEX4_EXT:
346 case GL_COLOR_INDEX8_EXT:
347 case GL_COLOR_INDEX12_EXT:
348 case GL_COLOR_INDEX16_EXT:
349 return &_mesa_texformat_ci8;
350 default:
351 _mesa_problem(ctx, "unexpected format in viaChooseTextureFormat");
352 return NULL;
353 }
354 }
355
356 void viaInitTextureFuncs(struct dd_function_table * functions)
357 {
358 if (VIA_DEBUG) fprintf(stderr, "viaInitTextureFuncs - in\n");
359 functions->TexEnv = viaTexEnv;
360 functions->ChooseTextureFormat = viaChooseTexFormat;
361 functions->TexImage1D = viaTexImage1D;
362 functions->TexImage2D = viaTexImage2D;
363 functions->TexImage3D = _mesa_store_teximage3d;
364 functions->TexSubImage1D = viaTexSubImage1D;
365 functions->TexSubImage2D = viaTexSubImage2D;
366 functions->TexSubImage3D = _mesa_store_texsubimage3d;
367 functions->CopyTexImage1D = _swrast_copy_teximage1d;
368 functions->CopyTexImage2D = _swrast_copy_teximage2d;
369 functions->CopyTexSubImage1D = _swrast_copy_texsubimage1d;
370 functions->CopyTexSubImage2D = _swrast_copy_texsubimage2d;
371 functions->CopyTexSubImage3D = _swrast_copy_texsubimage3d;
372
373 functions->NewTextureObject = _mesa_new_texture_object;
374 functions->BindTexture = viaBindTexture;
375 functions->DeleteTexture = viaDeleteTexture;
376 functions->TexParameter = viaTexParameter;
377 functions->UpdateTexturePalette = 0;
378 functions->IsTextureResident = viaIsTextureResident;
379 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
380
381 if (VIA_DEBUG) fprintf(stderr, "viaInitTextureFuncs - out\n");
382 }
383
384 void viaInitTextures(GLcontext *ctx)
385 {
386 GLuint tmp = ctx->Texture.CurrentUnit;
387 ctx->Texture.CurrentUnit = 0;
388 viaBindTexture(ctx, GL_TEXTURE_1D, ctx->Texture.Unit[0].Current1D);
389 viaBindTexture(ctx, GL_TEXTURE_2D, ctx->Texture.Unit[0].Current2D);
390 ctx->Texture.CurrentUnit = 1;
391 viaBindTexture(ctx, GL_TEXTURE_1D, ctx->Texture.Unit[1].Current1D);
392 viaBindTexture(ctx, GL_TEXTURE_2D, ctx->Texture.Unit[1].Current2D);
393 ctx->Texture.CurrentUnit = tmp;
394 }