i965: fix for FXT1 & S3TC texture format
[mesa.git] / src / mesa / drivers / dri / i965 / brw_tex.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "glheader.h"
34 #include "mtypes.h"
35 #include "imports.h"
36 #include "simple_list.h"
37 #include "enums.h"
38 #include "image.h"
39 #include "teximage.h"
40 #include "texstore.h"
41 #include "texformat.h"
42 #include "texmem.h"
43
44 #include "intel_context.h"
45 #include "intel_ioctl.h"
46 #include "intel_regions.h"
47 #include "brw_context.h"
48 #include "brw_defines.h"
49
50
51
52
53 static const struct gl_texture_format *
54 brwChooseTextureFormat( GLcontext *ctx, GLint internalFormat,
55 GLenum srcFormat, GLenum srcType )
56 {
57 switch ( internalFormat ) {
58 case 4:
59 case GL_RGBA:
60 case GL_COMPRESSED_RGBA:
61 if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV)
62 return &_mesa_texformat_argb4444;
63 else if (srcFormat == GL_BGRA && srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV)
64 return &_mesa_texformat_argb1555;
65 else if ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
66 (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE) ||
67 (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8))
68 return &_mesa_texformat_rgba8888_rev;
69 else
70 return &_mesa_texformat_argb8888;
71
72 case GL_RGBA8:
73 case GL_RGB10_A2:
74 case GL_RGBA12:
75 case GL_RGBA16:
76 return &_mesa_texformat_argb8888;
77
78 case GL_RGB8:
79 case GL_RGB10:
80 case GL_RGB12:
81 case GL_RGB16:
82 /* Broadwater doesn't support RGB888 textures, so these must be
83 * stored as ARGB.
84 */
85 return &_mesa_texformat_argb8888;
86
87 case 3:
88 case GL_COMPRESSED_RGB:
89 case GL_RGB:
90 if (srcFormat == GL_RGB &&
91 srcType == GL_UNSIGNED_SHORT_5_6_5)
92 return &_mesa_texformat_rgb565;
93 else
94 return &_mesa_texformat_argb8888;
95
96
97 case GL_RGB5:
98 case GL_RGB5_A1:
99 return &_mesa_texformat_argb1555;
100
101 case GL_R3_G3_B2:
102 case GL_RGBA2:
103 case GL_RGBA4:
104 case GL_RGB4:
105 return &_mesa_texformat_argb4444;
106
107 case GL_ALPHA:
108 case GL_ALPHA4:
109 case GL_ALPHA8:
110 case GL_ALPHA12:
111 case GL_ALPHA16:
112 case GL_COMPRESSED_ALPHA:
113 return &_mesa_texformat_a8;
114
115 case 1:
116 case GL_LUMINANCE:
117 case GL_LUMINANCE4:
118 case GL_LUMINANCE8:
119 case GL_LUMINANCE12:
120 case GL_LUMINANCE16:
121 case GL_COMPRESSED_LUMINANCE:
122 return &_mesa_texformat_l8;
123
124 case 2:
125 case GL_LUMINANCE_ALPHA:
126 case GL_LUMINANCE4_ALPHA4:
127 case GL_LUMINANCE6_ALPHA2:
128 case GL_LUMINANCE8_ALPHA8:
129 case GL_LUMINANCE12_ALPHA4:
130 case GL_LUMINANCE12_ALPHA12:
131 case GL_LUMINANCE16_ALPHA16:
132 case GL_COMPRESSED_LUMINANCE_ALPHA:
133 return &_mesa_texformat_al88;
134
135 case GL_INTENSITY:
136 case GL_INTENSITY4:
137 case GL_INTENSITY8:
138 case GL_INTENSITY12:
139 case GL_INTENSITY16:
140 case GL_COMPRESSED_INTENSITY:
141 return &_mesa_texformat_i8;
142
143 case GL_YCBCR_MESA:
144 if (srcType == GL_UNSIGNED_SHORT_8_8_MESA ||
145 srcType == GL_UNSIGNED_BYTE)
146 return &_mesa_texformat_ycbcr;
147 else
148 return &_mesa_texformat_ycbcr_rev;
149
150 case GL_COMPRESSED_RGB_FXT1_3DFX:
151 return &_mesa_texformat_rgb_fxt1;
152 case GL_COMPRESSED_RGBA_FXT1_3DFX:
153 return &_mesa_texformat_rgba_fxt1;
154
155 case GL_RGB_S3TC:
156 case GL_RGB4_S3TC:
157 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
158 return &_mesa_texformat_rgb_dxt1;
159
160 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
161 return &_mesa_texformat_rgba_dxt1;
162
163 case GL_RGBA_S3TC:
164 case GL_RGBA4_S3TC:
165 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
166 return &_mesa_texformat_rgba_dxt3;
167
168 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
169 return &_mesa_texformat_rgba_dxt5;
170
171 case GL_DEPTH_COMPONENT:
172 case GL_DEPTH_COMPONENT16:
173 case GL_DEPTH_COMPONENT24:
174 case GL_DEPTH_COMPONENT32:
175 return &_mesa_texformat_z16;
176
177 default:
178 fprintf(stderr, "unexpected texture format %s in %s\n",
179 _mesa_lookup_enum_by_nr(internalFormat),
180 __FUNCTION__);
181 return NULL;
182 }
183
184 return NULL; /* never get here */
185 }
186
187
188 void brwInitTextureFuncs( struct dd_function_table *functions )
189 {
190 functions->ChooseTextureFormat = brwChooseTextureFormat;
191 }
192
193 void brw_FrameBufferTexInit( struct brw_context *brw )
194 {
195 struct intel_context *intel = &brw->intel;
196 GLcontext *ctx = &intel->ctx;
197 struct intel_region *region = intel->front_region;
198 struct gl_texture_object *obj;
199 struct gl_texture_image *img;
200
201 intel->frame_buffer_texobj = obj =
202 ctx->Driver.NewTextureObject( ctx, (GLuint) -1, GL_TEXTURE_2D );
203
204 obj->MinFilter = GL_NEAREST;
205 obj->MagFilter = GL_NEAREST;
206
207 img = ctx->Driver.NewTextureImage( ctx );
208
209 _mesa_init_teximage_fields( ctx, GL_TEXTURE_2D, img,
210 region->pitch, region->height, 1, 0,
211 region->cpp == 4 ? GL_RGBA : GL_RGB );
212
213 _mesa_set_tex_image( obj, GL_TEXTURE_2D, 0, img );
214 }
215
216 void brw_FrameBufferTexDestroy( struct brw_context *brw )
217 {
218 brw->intel.ctx.Driver.DeleteTexture( &brw->intel.ctx,
219 brw->intel.frame_buffer_texobj );
220 }