Unichrome DRI:
[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 #ifdef DEBUG
109 if (VIA_DEBUG) fprintf(stderr, "viaTexImage1D - in\n");
110 #endif
111 if (t) {
112 if (level == 0) {
113 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
114 t->actualLevel = 0;
115 }
116 else
117 t->actualLevel++;
118 }
119 else {
120 t = viaAllocTextureObject(texObj);
121 if (!t) {
122 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaTexImage1D");
123 return;
124 }
125 texObj->DriverData = t;
126 }
127 _mesa_store_teximage1d(ctx, target, level, internalFormat,
128 width, border, format, type,
129 pixels, packing, texObj, texImage);
130 #ifdef DEBUG
131 if (VIA_DEBUG) fprintf(stderr, "viaTexImage1D - out\n");
132 #endif
133 }
134 static void viaTexSubImage1D(GLcontext *ctx,
135 GLenum target,
136 GLint level,
137 GLint xoffset,
138 GLsizei width,
139 GLenum format, GLenum type,
140 const GLvoid *pixels,
141 const struct gl_pixelstore_attrib *packing,
142 struct gl_texture_object *texObj,
143 struct gl_texture_image *texImage)
144
145 {
146 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
147
148 if (t) {
149 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
150 }
151 _mesa_store_texsubimage1d(ctx, target, level, xoffset, width,
152 format, type, pixels, packing, texObj,
153 texImage);
154
155 }
156
157
158 static void viaTexImage2D(GLcontext *ctx, GLenum target, GLint level,
159 GLint internalFormat,
160 GLint width, GLint height, GLint border,
161 GLenum format, GLenum type, const GLvoid *pixels,
162 const struct gl_pixelstore_attrib *packing,
163 struct gl_texture_object *texObj,
164 struct gl_texture_image *texImage)
165 {
166 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
167 #ifdef DEBUG
168 if (VIA_DEBUG) fprintf(stderr, "viaTexImage2D - in\n");
169 #endif
170 if (t) {
171 if (level == 0) {
172 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
173 t->actualLevel = 0;
174 }
175 else
176 t->actualLevel++;
177 }
178 else {
179 t = viaAllocTextureObject(texObj);
180 if (!t) {
181 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaTexImage2D");
182 return;
183 }
184 texObj->DriverData = t;
185 }
186 _mesa_store_teximage2d(ctx, target, level, internalFormat,
187 width, height, border, format, type,
188 pixels, packing, texObj, texImage);
189 #ifdef DEBUG
190 if (VIA_DEBUG) fprintf(stderr, "viaTexImage2D - out\n");
191 #endif
192 }
193
194 static void viaTexSubImage2D(GLcontext *ctx,
195 GLenum target,
196 GLint level,
197 GLint xoffset, GLint yoffset,
198 GLsizei width, GLsizei height,
199 GLenum format, GLenum type,
200 const GLvoid *pixels,
201 const struct gl_pixelstore_attrib *packing,
202 struct gl_texture_object *texObj,
203 struct gl_texture_image *texImage)
204 {
205 viaContextPtr vmesa = VIA_CONTEXT(ctx);
206
207 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
208
209 if (t) {
210 viaSwapOutTexObj(VIA_CONTEXT(ctx), t);
211 }
212 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset, width,
213 height, format, type, pixels, packing, texObj,
214 texImage);
215
216 if(vmesa->shareCtx)
217 vmesa->shareCtx->NewState |= _NEW_TEXTURE;
218
219 }
220
221 static void viaBindTexture(GLcontext *ctx, GLenum target,
222 struct gl_texture_object *texObj)
223 {
224 #ifdef DEBUG
225 if (VIA_DEBUG) fprintf(stderr, "viaBindTexture - in\n");
226 #endif
227 if (target == GL_TEXTURE_2D) {
228 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
229
230 if (!t) {
231
232 t = viaAllocTextureObject(texObj);
233 if (!t) {
234 _mesa_error(ctx, GL_OUT_OF_MEMORY, "viaBindTexture");
235 return;
236 }
237 texObj->DriverData = t;
238 }
239 }
240 #ifdef DEBUG
241 if (VIA_DEBUG) fprintf(stderr, "viaBindTexture - out\n");
242 #endif
243 }
244
245 static void viaDeleteTexture(GLcontext *ctx, struct gl_texture_object *texObj)
246 {
247 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
248 #ifdef DEBUG
249 if (VIA_DEBUG) fprintf(stderr, "viaDeleteTexture - in\n");
250 #endif
251 if (t) {
252 viaContextPtr vmesa = VIA_CONTEXT(ctx);
253 if (vmesa) {
254 /*=* John Sheng [2003.7.18] viewperf frames/sec *=*/
255 /*VIA_FIREVERTICES(vmesa);*/
256 if (vmesa->dma) { /* imply vmesa is not under destroying */
257 VIA_FIREVERTICES(vmesa);
258 }
259 viaDestroyTexObj(vmesa, t);
260 }
261 texObj->DriverData = 0;
262 }
263 #ifdef DEBUG
264 if (VIA_DEBUG) fprintf(stderr, "viaDeleteTexture - out\n");
265 #endif
266
267 /* Free mipmap images and the texture object itself */
268 _mesa_delete_texture_object(ctx, texObj);
269 }
270
271 static GLboolean viaIsTextureResident(GLcontext *ctx,
272 struct gl_texture_object *texObj)
273 {
274 viaTextureObjectPtr t = (viaTextureObjectPtr)texObj->DriverData;
275
276 return t && t->bufAddr;
277 }
278
279 static const struct gl_texture_format *
280 viaChooseTexFormat(GLcontext *ctx, GLint internalFormat,
281 GLenum format, GLenum type)
282 {
283 viaContextPtr vmesa = VIA_CONTEXT(ctx);
284 (void)format;
285 (void)type;
286 #ifdef DEBUG
287 if (VIA_DEBUG) fprintf(stderr, "%s in\n", __FUNCTION__);
288 if (VIA_DEBUG) fprintf(stderr, "internalFormat:%d format:%d\n", internalFormat, format);
289 #endif
290 switch (internalFormat) {
291 case 1:
292 case GL_LUMINANCE:
293 case GL_LUMINANCE4:
294 case GL_LUMINANCE8:
295 case GL_LUMINANCE12:
296 case GL_LUMINANCE16:
297 return &_mesa_texformat_l8;
298 case 2:
299 case GL_LUMINANCE_ALPHA:
300 case GL_LUMINANCE4_ALPHA4:
301 case GL_LUMINANCE6_ALPHA2:
302 case GL_LUMINANCE8_ALPHA8:
303 case GL_LUMINANCE12_ALPHA4:
304 case GL_LUMINANCE12_ALPHA12:
305 case GL_LUMINANCE16_ALPHA16:
306 return &_mesa_texformat_al88;
307 case GL_R3_G3_B2:
308 case GL_RGB4:
309 case GL_RGB5:
310 #ifdef DEBUG
311 if (VIA_DEBUG) fprintf(stderr, "2 &_mesa_texformat_arg565\n");
312 #endif
313 return &_mesa_texformat_rgb565;
314 case 3:
315 case GL_RGB:
316 case GL_RGB8:
317 case GL_RGB10:
318 case GL_RGB12:
319 case GL_RGB16:
320 if (vmesa->viaScreen->bitsPerPixel == 0x20) {
321 #ifdef DEBUG
322 if (VIA_DEBUG) fprintf(stderr,"3 argb8888\n");
323 #endif
324 return &_mesa_texformat_argb8888;
325 }
326 else {
327 #ifdef DEBUG
328 if (VIA_DEBUG) fprintf(stderr,"3 rgb565\n");
329 #endif
330 return &_mesa_texformat_rgb565;
331 }
332 case 4:
333 if (vmesa->viaScreen->bitsPerPixel == 0x20) {
334 #ifdef DEBUG
335 if (VIA_DEBUG) fprintf(stderr, "4 &_mesa_texformat_argb8888\n");
336 #endif
337 return &_mesa_texformat_argb8888;
338 }
339 else {
340 #ifdef DEBUG
341 if (VIA_DEBUG) fprintf(stderr, "4 &_mesa_texformat_argb4444\n");
342 #endif
343 return &_mesa_texformat_argb4444;
344 }
345 case GL_RGBA2:
346 case GL_RGBA4:
347 #ifdef DEBUG
348 if (VIA_DEBUG) fprintf(stderr, "GL_RGBA4 &_mesa_texformat_argb4444\n");
349 #endif
350 return &_mesa_texformat_argb4444;
351
352 case GL_RGB5_A1:
353 #ifdef DEBUG
354 if (VIA_DEBUG) fprintf(stderr, "GL_RGB5_A1 &_mesa_texformat_argb1555\n");
355 #endif
356 return &_mesa_texformat_argb1555;
357 case GL_RGBA:
358 case GL_RGBA8:
359 case GL_RGBA12:
360 case GL_RGBA16:
361 case GL_RGB10_A2:
362 #ifdef DEBUG
363 if (VIA_DEBUG) fprintf(stderr, "GL_RGBA &_mesa_texformat_argb8888\n");
364 #endif
365 return &_mesa_texformat_argb8888;
366 case GL_ALPHA:
367 case GL_ALPHA4:
368 case GL_ALPHA8:
369 case GL_ALPHA12:
370 case GL_ALPHA16:
371 return &_mesa_texformat_a8;
372 case GL_INTENSITY:
373 case GL_INTENSITY4:
374 case GL_INTENSITY8:
375 case GL_INTENSITY12:
376 case GL_INTENSITY16:
377 return &_mesa_texformat_i8;
378 case GL_COLOR_INDEX:
379 case GL_COLOR_INDEX1_EXT:
380 case GL_COLOR_INDEX2_EXT:
381 case GL_COLOR_INDEX4_EXT:
382 case GL_COLOR_INDEX8_EXT:
383 case GL_COLOR_INDEX12_EXT:
384 case GL_COLOR_INDEX16_EXT:
385 return &_mesa_texformat_ci8;
386 default:
387 _mesa_problem(ctx, "unexpected format in viaChooseTextureFormat");
388 return NULL;
389 }
390 }
391
392 void viaInitTextureFuncs(struct dd_function_table * functions)
393 {
394 #ifdef DEBUG
395 if (VIA_DEBUG) fprintf(stderr, "viaInitTextureFuncs - in\n");
396 #endif
397 functions->TexEnv = viaTexEnv;
398 functions->ChooseTextureFormat = viaChooseTexFormat;
399 functions->TexImage1D = viaTexImage1D;
400 functions->TexImage2D = viaTexImage2D;
401 functions->TexImage3D = _mesa_store_teximage3d;
402 functions->TexSubImage1D = viaTexSubImage1D;
403 functions->TexSubImage2D = viaTexSubImage2D;
404 functions->TexSubImage3D = _mesa_store_texsubimage3d;
405 functions->CopyTexImage1D = _swrast_copy_teximage1d;
406 functions->CopyTexImage2D = _swrast_copy_teximage2d;
407 functions->CopyTexSubImage1D = _swrast_copy_texsubimage1d;
408 functions->CopyTexSubImage2D = _swrast_copy_texsubimage2d;
409 functions->CopyTexSubImage3D = _swrast_copy_texsubimage3d;
410
411 functions->NewTextureObject = _mesa_new_texture_object;
412 functions->BindTexture = viaBindTexture;
413 functions->DeleteTexture = viaDeleteTexture;
414 functions->TexParameter = viaTexParameter;
415 functions->UpdateTexturePalette = 0;
416 functions->IsTextureResident = viaIsTextureResident;
417 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
418
419 #ifdef DEBUG
420 if (VIA_DEBUG) fprintf(stderr, "viaInitTextureFuncs - out\n");
421 #endif
422 }
423
424 void viaInitTextures(GLcontext *ctx)
425 {
426 GLuint tmp = ctx->Texture.CurrentUnit;
427 ctx->Texture.CurrentUnit = 0;
428 viaBindTexture(ctx, GL_TEXTURE_1D, ctx->Texture.Unit[0].Current1D);
429 viaBindTexture(ctx, GL_TEXTURE_2D, ctx->Texture.Unit[0].Current2D);
430 ctx->Texture.CurrentUnit = 1;
431 viaBindTexture(ctx, GL_TEXTURE_1D, ctx->Texture.Unit[1].Current1D);
432 viaBindTexture(ctx, GL_TEXTURE_2D, ctx->Texture.Unit[1].Current2D);
433 ctx->Texture.CurrentUnit = tmp;
434 }