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_B8G8R8X8_UNORM
, { 8, 8, 8, 0}, {16, 8, 0, 0} },
78 { PIPE_FORMAT_X8R8G8B8_UNORM
, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
79 { PIPE_FORMAT_B5G6R5_UNORM
, { 5, 6, 5, 0}, {11, 5, 0, 0} },
81 { PIPE_FORMAT_B8G8R8A8_UNORM
, { 8, 8, 8, 8}, {16, 8, 0, 24} },
82 { PIPE_FORMAT_A8R8G8B8_UNORM
, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
84 { PIPE_FORMAT_R10G10B10A2_UNORM
, {10, 10, 10, 2}, { 0, 10, 20, 30} },
86 { PIPE_FORMAT_B5G5R5A1_UNORM
, { 5, 5, 5, 1}, {10, 5, 0, 15} },
87 { PIPE_FORMAT_B4G4R4A4_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_X8Z24_UNORM
, {24, 0} },
96 { PIPE_FORMAT_Z24X8_UNORM
, {24, 0} },
97 { PIPE_FORMAT_Z16_UNORM
, {16, 0} },
98 /* combined depth-stencil */
99 { PIPE_FORMAT_Z24_UNORM_S8_USCALED
, {24, 8} },
100 { PIPE_FORMAT_S8_USCALED_Z24_UNORM
, {24, 8} }
105 stw_pf_doublebuffer
[] = {
112 stw_pf_multisample
[] = {
120 struct stw_device
*stw_dev
,
121 const struct stw_pf_color_info
*color
,
122 const struct stw_pf_depth_info
*depth
,
124 boolean doublebuffer
,
127 boolean extended
= FALSE
;
128 struct stw_pixelformat_info
*pfi
;
130 assert(stw_dev
->pixelformat_extended_count
< STW_MAX_PIXELFORMATS
);
131 if(stw_dev
->pixelformat_extended_count
>= STW_MAX_PIXELFORMATS
)
134 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) == color
->bits
.red
);
135 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 1) == color
->bits
.green
);
136 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 2) == color
->bits
.blue
);
137 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) == color
->bits
.alpha
);
138 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) == depth
->bits
.depth
);
139 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) == depth
->bits
.stencil
);
141 pfi
= &stw_dev
->pixelformats
[stw_dev
->pixelformat_extended_count
];
143 memset(pfi
, 0, sizeof *pfi
);
145 pfi
->pfd
.nSize
= sizeof pfi
->pfd
;
146 pfi
->pfd
.nVersion
= 1;
148 pfi
->pfd
.dwFlags
= PFD_SUPPORT_OPENGL
;
150 /* TODO: also support non-native pixel formats */
151 pfi
->pfd
.dwFlags
|= PFD_DRAW_TO_WINDOW
;
153 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
154 pfi
->pfd
.dwFlags
|= PFD_SUPPORT_COMPOSITION
;
157 pfi
->pfd
.dwFlags
|= PFD_DOUBLEBUFFER
| PFD_SWAP_COPY
;
159 pfi
->pfd
.iPixelType
= PFD_TYPE_RGBA
;
161 pfi
->pfd
.cColorBits
= color
->bits
.red
+ color
->bits
.green
+ color
->bits
.blue
+ color
->bits
.alpha
;
162 pfi
->pfd
.cRedBits
= color
->bits
.red
;
163 pfi
->pfd
.cRedShift
= color
->shift
.red
;
164 pfi
->pfd
.cGreenBits
= color
->bits
.green
;
165 pfi
->pfd
.cGreenShift
= color
->shift
.green
;
166 pfi
->pfd
.cBlueBits
= color
->bits
.blue
;
167 pfi
->pfd
.cBlueShift
= color
->shift
.blue
;
168 pfi
->pfd
.cAlphaBits
= color
->bits
.alpha
;
169 pfi
->pfd
.cAlphaShift
= color
->shift
.alpha
;
170 pfi
->pfd
.cAccumBits
= 4*accum
;
171 pfi
->pfd
.cAccumRedBits
= accum
;
172 pfi
->pfd
.cAccumGreenBits
= accum
;
173 pfi
->pfd
.cAccumBlueBits
= accum
;
174 pfi
->pfd
.cAccumAlphaBits
= accum
;
175 pfi
->pfd
.cDepthBits
= depth
->bits
.depth
;
176 pfi
->pfd
.cStencilBits
= depth
->bits
.stencil
;
177 pfi
->pfd
.cAuxBuffers
= 0;
178 pfi
->pfd
.iLayerType
= 0;
179 pfi
->pfd
.bReserved
= 0;
180 pfi
->pfd
.dwLayerMask
= 0;
181 pfi
->pfd
.dwVisibleMask
= 0;
182 pfi
->pfd
.dwDamageMask
= 0;
185 * since state trackers can allocate depth/stencil/accum buffers, we provide
186 * only color buffers here
188 pfi
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
190 pfi
->stvis
.buffer_mask
= ST_ATTACHMENT_BACK_LEFT_MASK
;
192 pfi
->stvis
.color_format
= color
->format
;
193 pfi
->stvis
.depth_stencil_format
= depth
->format
;
195 pfi
->stvis
.accum_format
= (accum
) ?
196 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
198 pfi
->stvis
.samples
= samples
;
199 pfi
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
201 ++stw_dev
->pixelformat_extended_count
;
204 ++stw_dev
->pixelformat_count
;
205 assert(stw_dev
->pixelformat_count
== stw_dev
->pixelformat_extended_count
);
210 stw_pixelformat_init( void )
212 struct pipe_screen
*screen
= stw_dev
->screen
;
215 assert( !stw_dev
->pixelformat_count
);
216 assert( !stw_dev
->pixelformat_extended_count
);
218 for(i
= 0; i
< Elements(stw_pf_multisample
); ++i
) {
219 unsigned samples
= stw_pf_multisample
[i
];
221 /* FIXME: re-enabled MSAA when we can query it */
225 for(j
= 0; j
< Elements(stw_pf_color
); ++j
) {
226 const struct stw_pf_color_info
*color
= &stw_pf_color
[j
];
228 if(!screen
->is_format_supported(screen
, color
->format
, PIPE_TEXTURE_2D
,
229 0, PIPE_BIND_RENDER_TARGET
|
230 PIPE_BIND_DISPLAY_TARGET
, 0))
233 for(k
= 0; k
< Elements(stw_pf_doublebuffer
); ++k
) {
234 unsigned doublebuffer
= stw_pf_doublebuffer
[k
];
236 for(l
= 0; l
< Elements(stw_pf_depth_stencil
); ++l
) {
237 const struct stw_pf_depth_info
*depth
= &stw_pf_depth_stencil
[l
];
239 if(!screen
->is_format_supported(screen
, depth
->format
, PIPE_TEXTURE_2D
,
240 0, PIPE_BIND_DEPTH_STENCIL
, 0))
243 stw_pixelformat_add( stw_dev
, color
, depth
, 0, doublebuffer
, samples
);
244 stw_pixelformat_add( stw_dev
, color
, depth
, 16, doublebuffer
, samples
);
250 assert( stw_dev
->pixelformat_count
<= stw_dev
->pixelformat_extended_count
);
251 assert( stw_dev
->pixelformat_extended_count
<= STW_MAX_PIXELFORMATS
);
255 stw_pixelformat_get_count( void )
257 return stw_dev
->pixelformat_count
;
261 stw_pixelformat_get_extended_count( void )
263 return stw_dev
->pixelformat_extended_count
;
266 const struct stw_pixelformat_info
*
267 stw_pixelformat_get_info( uint index
)
269 assert( index
< stw_dev
->pixelformat_extended_count
);
271 return &stw_dev
->pixelformats
[index
];
276 DrvDescribePixelFormat(
280 PIXELFORMATDESCRIPTOR
*ppfd
)
284 const struct stw_pixelformat_info
*pfi
;
291 count
= stw_pixelformat_get_extended_count();
292 index
= (uint
) iPixelFormat
- 1;
296 if (index
>= count
|| cjpfd
!= sizeof( PIXELFORMATDESCRIPTOR
))
299 pfi
= stw_pixelformat_get_info( index
);
301 memcpy(ppfd
, &pfi
->pfd
, sizeof( PIXELFORMATDESCRIPTOR
));
307 DrvDescribeLayerPlane(
312 LPLAYERPLANEDESCRIPTOR plpd
)
319 DrvGetLayerPaletteEntries(
331 DrvSetLayerPaletteEntries(
336 CONST COLORREF
*pcr
)
343 DrvRealizeLayerPalette(
352 /* Only used by the wgl code, but have it here to avoid exporting the
353 * pixelformat.h functionality.
355 int stw_pixelformat_choose( HDC hdc
,
356 CONST PIXELFORMATDESCRIPTOR
*ppfd
)
365 count
= stw_pixelformat_get_count();
369 for (index
= 0; index
< count
; index
++) {
371 const struct stw_pixelformat_info
*pfi
= stw_pixelformat_get_info( index
);
373 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
374 !!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
375 !!(pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
))
378 /* FIXME: Take in account individual channel bits */
379 if (ppfd
->cColorBits
!= pfi
->pfd
.cColorBits
)
382 if (ppfd
->cDepthBits
!= pfi
->pfd
.cDepthBits
)
385 if (ppfd
->cStencilBits
!= pfi
->pfd
.cStencilBits
)
388 if (ppfd
->cAlphaBits
!= pfi
->pfd
.cAlphaBits
)
391 if (delta
< bestdelta
) {
399 if (bestindex
== count
)
402 return bestindex
+ 1;