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_debug.h"
38 #include "stw_device.h"
39 #include "stw_pixelformat.h"
43 struct stw_pf_color_info
45 enum pipe_format format
;
60 struct stw_pf_depth_info
62 enum pipe_format format
;
65 unsigned char stencil
;
70 /* NOTE: order matters, since in otherwise equal circumstances the first
71 * format listed will get chosen */
73 static const struct stw_pf_color_info
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} },
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} },
83 { PIPE_FORMAT_A2B10G10R10_UNORM
, {10, 10, 10, 2}, { 0, 10, 20, 30} },
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} }
90 static const struct stw_pf_depth_info
91 stw_pf_depth_stencil
[] = {
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} },
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} }
106 stw_pf_doublebuffer
[] = {
113 stw_pf_multisample
[] = {
121 struct stw_device
*stw_dev
,
122 const struct stw_pf_color_info
*color
,
123 const struct stw_pf_depth_info
*depth
,
125 boolean doublebuffer
,
128 boolean extended
= FALSE
;
129 struct stw_pixelformat_info
*pfi
;
131 assert(stw_dev
->pixelformat_extended_count
< STW_MAX_PIXELFORMATS
);
132 if(stw_dev
->pixelformat_extended_count
>= STW_MAX_PIXELFORMATS
)
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
);
144 pfi
= &stw_dev
->pixelformats
[stw_dev
->pixelformat_extended_count
];
146 memset(pfi
, 0, sizeof *pfi
);
148 pfi
->color_format
= color
->format
;
149 pfi
->depth_stencil_format
= depth
->format
;
151 pfi
->pfd
.nSize
= sizeof pfi
->pfd
;
152 pfi
->pfd
.nVersion
= 1;
154 pfi
->pfd
.dwFlags
= PFD_SUPPORT_OPENGL
;
156 /* TODO: also support non-native pixel formats */
157 pfi
->pfd
.dwFlags
|= PFD_DRAW_TO_WINDOW
;
159 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
160 pfi
->pfd
.dwFlags
|= PFD_SUPPORT_COMPOSITION
;
163 pfi
->pfd
.dwFlags
|= PFD_DOUBLEBUFFER
| PFD_SWAP_COPY
;
165 pfi
->pfd
.iPixelType
= PFD_TYPE_RGBA
;
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;
191 pfi
->numSampleBuffers
= 1;
192 pfi
->numSamples
= samples
;
196 ++stw_dev
->pixelformat_extended_count
;
199 ++stw_dev
->pixelformat_count
;
200 assert(stw_dev
->pixelformat_count
== stw_dev
->pixelformat_extended_count
);
205 stw_pixelformat_init( void )
207 struct pipe_screen
*screen
= stw_dev
->screen
;
210 assert( !stw_dev
->pixelformat_count
);
211 assert( !stw_dev
->pixelformat_extended_count
);
213 for(i
= 0; i
< Elements(stw_pf_multisample
); ++i
) {
214 unsigned samples
= stw_pf_multisample
[i
];
216 /* FIXME: re-enabled MSAA when we can query it */
220 for(j
= 0; j
< Elements(stw_pf_color
); ++j
) {
221 const struct stw_pf_color_info
*color
= &stw_pf_color
[j
];
223 if(!screen
->is_format_supported(screen
, color
->format
, PIPE_TEXTURE_2D
,
224 PIPE_TEXTURE_USAGE_RENDER_TARGET
, 0))
227 for(k
= 0; k
< Elements(stw_pf_doublebuffer
); ++k
) {
228 unsigned doublebuffer
= stw_pf_doublebuffer
[k
];
230 for(l
= 0; l
< Elements(stw_pf_depth_stencil
); ++l
) {
231 const struct stw_pf_depth_info
*depth
= &stw_pf_depth_stencil
[l
];
233 if(!screen
->is_format_supported(screen
, depth
->format
, PIPE_TEXTURE_2D
,
234 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
, 0))
237 stw_pixelformat_add( stw_dev
, color
, depth
, 0, doublebuffer
, samples
);
238 stw_pixelformat_add( stw_dev
, color
, depth
, 16, doublebuffer
, samples
);
244 assert( stw_dev
->pixelformat_count
<= stw_dev
->pixelformat_extended_count
);
245 assert( stw_dev
->pixelformat_extended_count
<= STW_MAX_PIXELFORMATS
);
249 stw_pixelformat_get_count( void )
251 return stw_dev
->pixelformat_count
;
255 stw_pixelformat_get_extended_count( void )
257 return stw_dev
->pixelformat_extended_count
;
260 const struct stw_pixelformat_info
*
261 stw_pixelformat_get_info( uint index
)
263 assert( index
< stw_dev
->pixelformat_extended_count
);
265 return &stw_dev
->pixelformats
[index
];
270 stw_pixelformat_visual(GLvisual
*visual
,
271 const struct stw_pixelformat_info
*pfi
)
273 memset(visual
, 0, sizeof *visual
);
274 _mesa_initialize_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
,
283 (pfi
->pfd
.iPixelType
== PFD_TYPE_COLORINDEX
) ? pfi
->pfd
.cColorBits
: 0,
285 pfi
->pfd
.cStencilBits
,
286 pfi
->pfd
.cAccumRedBits
,
287 pfi
->pfd
.cAccumGreenBits
,
288 pfi
->pfd
.cAccumBlueBits
,
289 pfi
->pfd
.cAccumAlphaBits
,
295 DrvDescribePixelFormat(
299 PIXELFORMATDESCRIPTOR
*ppfd
)
303 const struct stw_pixelformat_info
*pfi
;
307 count
= stw_pixelformat_get_extended_count();
308 index
= (uint
) iPixelFormat
- 1;
312 if (index
>= count
|| cjpfd
!= sizeof( PIXELFORMATDESCRIPTOR
))
315 pfi
= stw_pixelformat_get_info( index
);
317 memcpy(ppfd
, &pfi
->pfd
, sizeof( PIXELFORMATDESCRIPTOR
));
323 DrvDescribeLayerPlane(
328 LPLAYERPLANEDESCRIPTOR plpd
)
335 DrvGetLayerPaletteEntries(
347 DrvSetLayerPaletteEntries(
352 CONST COLORREF
*pcr
)
359 DrvRealizeLayerPalette(
368 /* Only used by the wgl code, but have it here to avoid exporting the
369 * pixelformat.h functionality.
371 int stw_pixelformat_choose( HDC hdc
,
372 CONST PIXELFORMATDESCRIPTOR
*ppfd
)
381 count
= stw_pixelformat_get_count();
385 for (index
= 0; index
< count
; index
++) {
387 const struct stw_pixelformat_info
*pfi
= stw_pixelformat_get_info( index
);
389 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
390 !!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
391 !!(pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
))
394 /* FIXME: Take in account individual channel bits */
395 if (ppfd
->cColorBits
!= pfi
->pfd
.cColorBits
)
398 if (ppfd
->cDepthBits
!= pfi
->pfd
.cDepthBits
)
401 if (ppfd
->cStencilBits
!= pfi
->pfd
.cStencilBits
)
404 if (ppfd
->cAlphaBits
!= pfi
->pfd
.cAlphaBits
)
407 if (delta
< bestdelta
) {
415 if (bestindex
== count
)
418 return bestindex
+ 1;