Fix previous commit
[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_RGBA_S3TC:
158 case GL_RGBA4_S3TC:
159 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
160 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
161 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
162 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
163 return &_mesa_texformat_rgb_dxt1; /* there is no rgba support? */
164
165 case GL_DEPTH_COMPONENT:
166 case GL_DEPTH_COMPONENT16:
167 case GL_DEPTH_COMPONENT24:
168 case GL_DEPTH_COMPONENT32:
169 return &_mesa_texformat_z16;
170
171 case GL_SRGB_EXT:
172 case GL_SRGB8_EXT:
173 case GL_SRGB_ALPHA_EXT:
174 case GL_SRGB8_ALPHA8_EXT:
175 case GL_SLUMINANCE_EXT:
176 case GL_SLUMINANCE8_EXT:
177 case GL_SLUMINANCE_ALPHA_EXT:
178 case GL_SLUMINANCE8_ALPHA8_EXT:
179 case GL_COMPRESSED_SRGB_EXT:
180 case GL_COMPRESSED_SRGB_ALPHA_EXT:
181 case GL_COMPRESSED_SLUMINANCE_EXT:
182 case GL_COMPRESSED_SLUMINANCE_ALPHA_EXT:
183 return &_mesa_texformat_srgba8;
184 case GL_COMPRESSED_SRGB_S3TC_DXT1_EXT:
185 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT:
186 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT:
187 case GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT:
188 return &_mesa_texformat_srgb_dxt1;
189
190 default:
191 fprintf(stderr, "unexpected texture format %s in %s\n",
192 _mesa_lookup_enum_by_nr(internalFormat),
193 __FUNCTION__);
194 return NULL;
195 }
196
197 return NULL; /* never get here */
198 }
199
200
201 void brwInitTextureFuncs( struct dd_function_table *functions )
202 {
203 functions->ChooseTextureFormat = brwChooseTextureFormat;
204 }
205
206 void brw_FrameBufferTexInit( struct brw_context *brw )
207 {
208 struct intel_context *intel = &brw->intel;
209 GLcontext *ctx = &intel->ctx;
210 struct intel_region *region = intel->front_region;
211 struct gl_texture_object *obj;
212 struct gl_texture_image *img;
213
214 intel->frame_buffer_texobj = obj =
215 ctx->Driver.NewTextureObject( ctx, (GLuint) -1, GL_TEXTURE_2D );
216
217 obj->MinFilter = GL_NEAREST;
218 obj->MagFilter = GL_NEAREST;
219
220 img = ctx->Driver.NewTextureImage( ctx );
221
222 _mesa_init_teximage_fields( ctx, GL_TEXTURE_2D, img,
223 region->pitch, region->height, 1, 0,
224 region->cpp == 4 ? GL_RGBA : GL_RGB );
225
226 _mesa_set_tex_image( obj, GL_TEXTURE_2D, 0, img );
227 }
228
229 void brw_FrameBufferTexDestroy( struct brw_context *brw )
230 {
231 brw->intel.ctx.Driver.DeleteTexture( &brw->intel.ctx,
232 brw->intel.frame_buffer_texobj );
233 }