1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "main/mtypes.h"
29 #include "main/context.h"
31 #include "pipe/p_format.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_screen.h"
35 #include "util/u_format.h"
36 #include "util/u_debug.h"
39 #include "stw_device.h"
40 #include "stw_pixelformat.h"
44 struct stw_pf_color_info
46 enum pipe_format format
;
61 struct stw_pf_depth_info
63 enum pipe_format format
;
66 unsigned char stencil
;
71 /* NOTE: order matters, since in otherwise equal circumstances the first
72 * format listed will get chosen */
74 static const struct stw_pf_color_info
77 { PIPE_FORMAT_X8R8G8B8_UNORM
, { 8, 8, 8, 0}, {16, 8, 0, 0} },
78 { PIPE_FORMAT_B8G8R8X8_UNORM
, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
79 { PIPE_FORMAT_R5G6B5_UNORM
, { 5, 6, 5, 0}, {11, 5, 0, 0} },
81 { PIPE_FORMAT_A8R8G8B8_UNORM
, { 8, 8, 8, 8}, {16, 8, 0, 24} },
82 { PIPE_FORMAT_B8G8R8A8_UNORM
, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
84 { PIPE_FORMAT_A2B10G10R10_UNORM
, {10, 10, 10, 2}, { 0, 10, 20, 30} },
86 { PIPE_FORMAT_A1R5G5B5_UNORM
, { 5, 5, 5, 1}, {10, 5, 0, 15} },
87 { PIPE_FORMAT_A4R4G4B4_UNORM
, { 4, 4, 4, 4}, {16, 4, 0, 12} }
91 static const struct stw_pf_depth_info
92 stw_pf_depth_stencil
[] = {
94 { PIPE_FORMAT_Z32_UNORM
, {32, 0} },
95 { PIPE_FORMAT_Z24X8_UNORM
, {24, 0} },
96 { PIPE_FORMAT_X8Z24_UNORM
, {24, 0} },
97 { PIPE_FORMAT_Z16_UNORM
, {16, 0} },
99 { PIPE_FORMAT_S8_UNORM
, { 0, 8} },
100 /* combined depth-stencil */
101 { PIPE_FORMAT_S8Z24_UNORM
, {24, 8} },
102 { PIPE_FORMAT_Z24S8_UNORM
, {24, 8} }
107 stw_pf_doublebuffer
[] = {
114 stw_pf_multisample
[] = {
122 struct stw_device
*stw_dev
,
123 const struct stw_pf_color_info
*color
,
124 const struct stw_pf_depth_info
*depth
,
126 boolean doublebuffer
,
129 boolean extended
= FALSE
;
130 struct stw_pixelformat_info
*pfi
;
132 assert(stw_dev
->pixelformat_extended_count
< STW_MAX_PIXELFORMATS
);
133 if(stw_dev
->pixelformat_extended_count
>= STW_MAX_PIXELFORMATS
)
136 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) == color
->bits
.red
);
137 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 1) == color
->bits
.green
);
138 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 2) == color
->bits
.blue
);
139 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) == color
->bits
.alpha
);
140 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) == depth
->bits
.depth
);
141 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) == depth
->bits
.stencil
);
143 pfi
= &stw_dev
->pixelformats
[stw_dev
->pixelformat_extended_count
];
145 memset(pfi
, 0, sizeof *pfi
);
147 pfi
->color_format
= color
->format
;
148 pfi
->depth_stencil_format
= depth
->format
;
150 pfi
->pfd
.nSize
= sizeof pfi
->pfd
;
151 pfi
->pfd
.nVersion
= 1;
153 pfi
->pfd
.dwFlags
= PFD_SUPPORT_OPENGL
;
155 /* TODO: also support non-native pixel formats */
156 pfi
->pfd
.dwFlags
|= PFD_DRAW_TO_WINDOW
;
158 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
159 pfi
->pfd
.dwFlags
|= PFD_SUPPORT_COMPOSITION
;
162 pfi
->pfd
.dwFlags
|= PFD_DOUBLEBUFFER
| PFD_SWAP_COPY
;
164 pfi
->pfd
.iPixelType
= PFD_TYPE_RGBA
;
166 pfi
->pfd
.cColorBits
= color
->bits
.red
+ color
->bits
.green
+ color
->bits
.blue
+ color
->bits
.alpha
;
167 pfi
->pfd
.cRedBits
= color
->bits
.red
;
168 pfi
->pfd
.cRedShift
= color
->shift
.red
;
169 pfi
->pfd
.cGreenBits
= color
->bits
.green
;
170 pfi
->pfd
.cGreenShift
= color
->shift
.green
;
171 pfi
->pfd
.cBlueBits
= color
->bits
.blue
;
172 pfi
->pfd
.cBlueShift
= color
->shift
.blue
;
173 pfi
->pfd
.cAlphaBits
= color
->bits
.alpha
;
174 pfi
->pfd
.cAlphaShift
= color
->shift
.alpha
;
175 pfi
->pfd
.cAccumBits
= 4*accum
;
176 pfi
->pfd
.cAccumRedBits
= accum
;
177 pfi
->pfd
.cAccumGreenBits
= accum
;
178 pfi
->pfd
.cAccumBlueBits
= accum
;
179 pfi
->pfd
.cAccumAlphaBits
= accum
;
180 pfi
->pfd
.cDepthBits
= depth
->bits
.depth
;
181 pfi
->pfd
.cStencilBits
= depth
->bits
.stencil
;
182 pfi
->pfd
.cAuxBuffers
= 0;
183 pfi
->pfd
.iLayerType
= 0;
184 pfi
->pfd
.bReserved
= 0;
185 pfi
->pfd
.dwLayerMask
= 0;
186 pfi
->pfd
.dwVisibleMask
= 0;
187 pfi
->pfd
.dwDamageMask
= 0;
190 pfi
->numSampleBuffers
= 1;
191 pfi
->numSamples
= samples
;
195 ++stw_dev
->pixelformat_extended_count
;
198 ++stw_dev
->pixelformat_count
;
199 assert(stw_dev
->pixelformat_count
== stw_dev
->pixelformat_extended_count
);
204 stw_pixelformat_init( void )
206 struct pipe_screen
*screen
= stw_dev
->screen
;
209 assert( !stw_dev
->pixelformat_count
);
210 assert( !stw_dev
->pixelformat_extended_count
);
212 for(i
= 0; i
< Elements(stw_pf_multisample
); ++i
) {
213 unsigned samples
= stw_pf_multisample
[i
];
215 /* FIXME: re-enabled MSAA when we can query it */
219 for(j
= 0; j
< Elements(stw_pf_color
); ++j
) {
220 const struct stw_pf_color_info
*color
= &stw_pf_color
[j
];
222 if(!screen
->is_format_supported(screen
, color
->format
, PIPE_TEXTURE_2D
,
223 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0))
226 for(k
= 0; k
< Elements(stw_pf_doublebuffer
); ++k
) {
227 unsigned doublebuffer
= stw_pf_doublebuffer
[k
];
229 for(l
= 0; l
< Elements(stw_pf_depth_stencil
); ++l
) {
230 const struct stw_pf_depth_info
*depth
= &stw_pf_depth_stencil
[l
];
232 if(!screen
->is_format_supported(screen
, depth
->format
, PIPE_TEXTURE_2D
,
233 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
236 stw_pixelformat_add( stw_dev
, color
, depth
, 0, doublebuffer
, samples
);
237 stw_pixelformat_add( stw_dev
, color
, depth
, 16, doublebuffer
, samples
);
243 assert( stw_dev
->pixelformat_count
<= stw_dev
->pixelformat_extended_count
);
244 assert( stw_dev
->pixelformat_extended_count
<= STW_MAX_PIXELFORMATS
);
248 stw_pixelformat_get_count( void )
250 return stw_dev
->pixelformat_count
;
254 stw_pixelformat_get_extended_count( void )
256 return stw_dev
->pixelformat_extended_count
;
259 const struct stw_pixelformat_info
*
260 stw_pixelformat_get_info( uint index
)
262 assert( index
< stw_dev
->pixelformat_extended_count
);
264 return &stw_dev
->pixelformats
[index
];
269 stw_pixelformat_visual(GLvisual
*visual
,
270 const struct stw_pixelformat_info
*pfi
)
272 memset(visual
, 0, sizeof *visual
);
273 _mesa_initialize_visual(
275 (pfi
->pfd
.iPixelType
== PFD_TYPE_RGBA
) ? GL_TRUE
: GL_FALSE
,
276 (pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
) ? GL_TRUE
: GL_FALSE
,
277 (pfi
->pfd
.dwFlags
& PFD_STEREO
) ? GL_TRUE
: GL_FALSE
,
282 (pfi
->pfd
.iPixelType
== PFD_TYPE_COLORINDEX
) ? pfi
->pfd
.cColorBits
: 0,
284 pfi
->pfd
.cStencilBits
,
285 pfi
->pfd
.cAccumRedBits
,
286 pfi
->pfd
.cAccumGreenBits
,
287 pfi
->pfd
.cAccumBlueBits
,
288 pfi
->pfd
.cAccumAlphaBits
,
294 DrvDescribePixelFormat(
298 PIXELFORMATDESCRIPTOR
*ppfd
)
302 const struct stw_pixelformat_info
*pfi
;
306 count
= stw_pixelformat_get_extended_count();
307 index
= (uint
) iPixelFormat
- 1;
311 if (index
>= count
|| cjpfd
!= sizeof( PIXELFORMATDESCRIPTOR
))
314 pfi
= stw_pixelformat_get_info( index
);
316 memcpy(ppfd
, &pfi
->pfd
, sizeof( PIXELFORMATDESCRIPTOR
));
322 DrvDescribeLayerPlane(
327 LPLAYERPLANEDESCRIPTOR plpd
)
334 DrvGetLayerPaletteEntries(
346 DrvSetLayerPaletteEntries(
351 CONST COLORREF
*pcr
)
358 DrvRealizeLayerPalette(
367 /* Only used by the wgl code, but have it here to avoid exporting the
368 * pixelformat.h functionality.
370 int stw_pixelformat_choose( HDC hdc
,
371 CONST PIXELFORMATDESCRIPTOR
*ppfd
)
380 count
= stw_pixelformat_get_count();
384 for (index
= 0; index
< count
; index
++) {
386 const struct stw_pixelformat_info
*pfi
= stw_pixelformat_get_info( index
);
388 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
389 !!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
390 !!(pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
))
393 /* FIXME: Take in account individual channel bits */
394 if (ppfd
->cColorBits
!= pfi
->pfd
.cColorBits
)
397 if (ppfd
->cDepthBits
!= pfi
->pfd
.cDepthBits
)
400 if (ppfd
->cStencilBits
!= pfi
->pfd
.cStencilBits
)
403 if (ppfd
->cAlphaBits
!= pfi
->pfd
.cAlphaBits
)
406 if (delta
< bestdelta
) {
414 if (bestindex
== count
)
417 return bestindex
+ 1;