Merge branch 'mesa_7_6_branch'
[mesa.git] / src / gallium / state_trackers / wgl / stw_pixelformat.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/mtypes.h"
29 #include "main/context.h"
30
31 #include "pipe/p_format.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_screen.h"
34
35 #include "util/u_debug.h"
36
37 #include "stw_icd.h"
38 #include "stw_device.h"
39 #include "stw_pixelformat.h"
40 #include "stw_tls.h"
41
42
43 struct stw_pf_color_info
44 {
45 enum pipe_format format;
46 struct {
47 unsigned char red;
48 unsigned char green;
49 unsigned char blue;
50 unsigned char alpha;
51 } bits;
52 struct {
53 unsigned char red;
54 unsigned char green;
55 unsigned char blue;
56 unsigned char alpha;
57 } shift;
58 };
59
60 struct stw_pf_depth_info
61 {
62 enum pipe_format format;
63 struct {
64 unsigned char depth;
65 unsigned char stencil;
66 } bits;
67 };
68
69
70 /* NOTE: order matters, since in otherwise equal circumstances the first
71 * format listed will get chosen */
72
73 static const struct stw_pf_color_info
74 stw_pf_color[] = {
75 /* no-alpha */
76 { PIPE_FORMAT_X8R8G8B8_UNORM, { 8, 8, 8, 0}, {16, 8, 0, 0} },
77 { PIPE_FORMAT_B8G8R8X8_UNORM, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
78 { PIPE_FORMAT_R5G6B5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 0} },
79 /* alpha */
80 { PIPE_FORMAT_A8R8G8B8_UNORM, { 8, 8, 8, 8}, {16, 8, 0, 24} },
81 { PIPE_FORMAT_B8G8R8A8_UNORM, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
82 #if 0
83 { PIPE_FORMAT_A2B10G10R10_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
84 #endif
85 { PIPE_FORMAT_A1R5G5B5_UNORM, { 5, 5, 5, 1}, {10, 5, 0, 15} },
86 { PIPE_FORMAT_A4R4G4B4_UNORM, { 4, 4, 4, 4}, {16, 4, 0, 12} }
87 };
88
89
90 static const struct stw_pf_depth_info
91 stw_pf_depth_stencil[] = {
92 /* pure depth */
93 { PIPE_FORMAT_Z32_UNORM, {32, 0} },
94 { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
95 { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
96 { PIPE_FORMAT_Z16_UNORM, {16, 0} },
97 /* pure stencil */
98 { PIPE_FORMAT_S8_UNORM, { 0, 8} },
99 /* combined depth-stencil */
100 { PIPE_FORMAT_S8Z24_UNORM, {24, 8} },
101 { PIPE_FORMAT_Z24S8_UNORM, {24, 8} }
102 };
103
104
105 static const boolean
106 stw_pf_doublebuffer[] = {
107 FALSE,
108 TRUE,
109 };
110
111
112 const unsigned
113 stw_pf_multisample[] = {
114 0,
115 4
116 };
117
118
119 static void
120 stw_pixelformat_add(
121 struct stw_device *stw_dev,
122 const struct stw_pf_color_info *color,
123 const struct stw_pf_depth_info *depth,
124 unsigned accum,
125 boolean doublebuffer,
126 unsigned samples )
127 {
128 boolean extended = FALSE;
129 struct stw_pixelformat_info *pfi;
130
131 assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
132 if(stw_dev->pixelformat_extended_count >= STW_MAX_PIXELFORMATS)
133 return;
134
135 assert(pf_layout( color->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
136 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_R ) == color->bits.red );
137 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_G ) == color->bits.green );
138 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_B ) == color->bits.blue );
139 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_A ) == color->bits.alpha );
140 assert(pf_layout( depth->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
141 assert(pf_get_component_bits( depth->format, PIPE_FORMAT_COMP_Z ) == depth->bits.depth );
142 assert(pf_get_component_bits( depth->format, PIPE_FORMAT_COMP_S ) == depth->bits.stencil );
143
144 pfi = &stw_dev->pixelformats[stw_dev->pixelformat_extended_count];
145
146 memset(pfi, 0, sizeof *pfi);
147
148 pfi->color_format = color->format;
149 pfi->depth_stencil_format = depth->format;
150
151 pfi->pfd.nSize = sizeof pfi->pfd;
152 pfi->pfd.nVersion = 1;
153
154 pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
155
156 /* TODO: also support non-native pixel formats */
157 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
158
159 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
160 pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
161
162 if (doublebuffer)
163 pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_COPY;
164
165 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
166
167 pfi->pfd.cColorBits = color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
168 pfi->pfd.cRedBits = color->bits.red;
169 pfi->pfd.cRedShift = color->shift.red;
170 pfi->pfd.cGreenBits = color->bits.green;
171 pfi->pfd.cGreenShift = color->shift.green;
172 pfi->pfd.cBlueBits = color->bits.blue;
173 pfi->pfd.cBlueShift = color->shift.blue;
174 pfi->pfd.cAlphaBits = color->bits.alpha;
175 pfi->pfd.cAlphaShift = color->shift.alpha;
176 pfi->pfd.cAccumBits = 4*accum;
177 pfi->pfd.cAccumRedBits = accum;
178 pfi->pfd.cAccumGreenBits = accum;
179 pfi->pfd.cAccumBlueBits = accum;
180 pfi->pfd.cAccumAlphaBits = accum;
181 pfi->pfd.cDepthBits = depth->bits.depth;
182 pfi->pfd.cStencilBits = depth->bits.stencil;
183 pfi->pfd.cAuxBuffers = 0;
184 pfi->pfd.iLayerType = 0;
185 pfi->pfd.bReserved = 0;
186 pfi->pfd.dwLayerMask = 0;
187 pfi->pfd.dwVisibleMask = 0;
188 pfi->pfd.dwDamageMask = 0;
189
190 if(samples) {
191 pfi->numSampleBuffers = 1;
192 pfi->numSamples = samples;
193 extended = TRUE;
194 }
195
196 ++stw_dev->pixelformat_extended_count;
197
198 if(!extended) {
199 ++stw_dev->pixelformat_count;
200 assert(stw_dev->pixelformat_count == stw_dev->pixelformat_extended_count);
201 }
202 }
203
204 void
205 stw_pixelformat_init( void )
206 {
207 struct pipe_screen *screen = stw_dev->screen;
208 unsigned i, j, k, l;
209
210 assert( !stw_dev->pixelformat_count );
211 assert( !stw_dev->pixelformat_extended_count );
212
213 for(i = 0; i < Elements(stw_pf_multisample); ++i) {
214 unsigned samples = stw_pf_multisample[i];
215
216 /* FIXME: re-enabled MSAA when we can query it */
217 if(samples)
218 continue;
219
220 for(j = 0; j < Elements(stw_pf_color); ++j) {
221 const struct stw_pf_color_info *color = &stw_pf_color[j];
222
223 if(!screen->is_format_supported(screen, color->format, PIPE_TEXTURE_2D,
224 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
225 continue;
226
227 for(k = 0; k < Elements(stw_pf_doublebuffer); ++k) {
228 unsigned doublebuffer = stw_pf_doublebuffer[k];
229
230 for(l = 0; l < Elements(stw_pf_depth_stencil); ++l) {
231 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[l];
232
233 if(!screen->is_format_supported(screen, depth->format, PIPE_TEXTURE_2D,
234 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0))
235 continue;
236
237 stw_pixelformat_add( stw_dev, color, depth, 0, doublebuffer, samples );
238 stw_pixelformat_add( stw_dev, color, depth, 16, doublebuffer, samples );
239 }
240 }
241 }
242 }
243
244 assert( stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count );
245 assert( stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS );
246 }
247
248 uint
249 stw_pixelformat_get_count( void )
250 {
251 return stw_dev->pixelformat_count;
252 }
253
254 uint
255 stw_pixelformat_get_extended_count( void )
256 {
257 return stw_dev->pixelformat_extended_count;
258 }
259
260 const struct stw_pixelformat_info *
261 stw_pixelformat_get_info( uint index )
262 {
263 assert( index < stw_dev->pixelformat_extended_count );
264
265 return &stw_dev->pixelformats[index];
266 }
267
268
269 void
270 stw_pixelformat_visual(GLvisual *visual,
271 const struct stw_pixelformat_info *pfi )
272 {
273 memset(visual, 0, sizeof *visual);
274 _mesa_initialize_visual(
275 visual,
276 (pfi->pfd.iPixelType == PFD_TYPE_RGBA) ? GL_TRUE : GL_FALSE,
277 (pfi->pfd.dwFlags & PFD_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
278 (pfi->pfd.dwFlags & PFD_STEREO) ? GL_TRUE : GL_FALSE,
279 pfi->pfd.cRedBits,
280 pfi->pfd.cGreenBits,
281 pfi->pfd.cBlueBits,
282 pfi->pfd.cAlphaBits,
283 (pfi->pfd.iPixelType == PFD_TYPE_COLORINDEX) ? pfi->pfd.cColorBits : 0,
284 pfi->pfd.cDepthBits,
285 pfi->pfd.cStencilBits,
286 pfi->pfd.cAccumRedBits,
287 pfi->pfd.cAccumGreenBits,
288 pfi->pfd.cAccumBlueBits,
289 pfi->pfd.cAccumAlphaBits,
290 pfi->numSamples );
291 }
292
293
294 LONG APIENTRY
295 DrvDescribePixelFormat(
296 HDC hdc,
297 INT iPixelFormat,
298 ULONG cjpfd,
299 PIXELFORMATDESCRIPTOR *ppfd )
300 {
301 uint count;
302 uint index;
303 const struct stw_pixelformat_info *pfi;
304
305 (void) hdc;
306
307 count = stw_pixelformat_get_extended_count();
308 index = (uint) iPixelFormat - 1;
309
310 if (ppfd == NULL)
311 return count;
312 if (index >= count || cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
313 return 0;
314
315 pfi = stw_pixelformat_get_info( index );
316
317 memcpy(ppfd, &pfi->pfd, sizeof( PIXELFORMATDESCRIPTOR ));
318
319 return count;
320 }
321
322 BOOL APIENTRY
323 DrvDescribeLayerPlane(
324 HDC hdc,
325 INT iPixelFormat,
326 INT iLayerPlane,
327 UINT nBytes,
328 LPLAYERPLANEDESCRIPTOR plpd )
329 {
330 assert(0);
331 return FALSE;
332 }
333
334 int APIENTRY
335 DrvGetLayerPaletteEntries(
336 HDC hdc,
337 INT iLayerPlane,
338 INT iStart,
339 INT cEntries,
340 COLORREF *pcr )
341 {
342 assert(0);
343 return 0;
344 }
345
346 int APIENTRY
347 DrvSetLayerPaletteEntries(
348 HDC hdc,
349 INT iLayerPlane,
350 INT iStart,
351 INT cEntries,
352 CONST COLORREF *pcr )
353 {
354 assert(0);
355 return 0;
356 }
357
358 BOOL APIENTRY
359 DrvRealizeLayerPalette(
360 HDC hdc,
361 INT iLayerPlane,
362 BOOL bRealize )
363 {
364 assert(0);
365 return FALSE;
366 }
367
368 /* Only used by the wgl code, but have it here to avoid exporting the
369 * pixelformat.h functionality.
370 */
371 int stw_pixelformat_choose( HDC hdc,
372 CONST PIXELFORMATDESCRIPTOR *ppfd )
373 {
374 uint count;
375 uint index;
376 uint bestindex;
377 uint bestdelta;
378
379 (void) hdc;
380
381 count = stw_pixelformat_get_count();
382 bestindex = count;
383 bestdelta = ~0U;
384
385 for (index = 0; index < count; index++) {
386 uint delta = 0;
387 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info( index );
388
389 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
390 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
391 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
392 continue;
393
394 /* FIXME: Take in account individual channel bits */
395 if (ppfd->cColorBits != pfi->pfd.cColorBits)
396 delta += 8;
397
398 if (ppfd->cDepthBits != pfi->pfd.cDepthBits)
399 delta += 4;
400
401 if (ppfd->cStencilBits != pfi->pfd.cStencilBits)
402 delta += 2;
403
404 if (ppfd->cAlphaBits != pfi->pfd.cAlphaBits)
405 delta++;
406
407 if (delta < bestdelta) {
408 bestindex = index;
409 bestdelta = delta;
410 if (bestdelta == 0)
411 break;
412 }
413 }
414
415 if (bestindex == count)
416 return 0;
417
418 return bestindex + 1;
419 }