1 /**************************************************************************
3 * Copyright 2008 VMware, Inc.
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 VMWARE 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 "pipe/p_format.h"
29 #include "pipe/p_defines.h"
30 #include "pipe/p_screen.h"
32 #include "util/u_format.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
37 #include "stw_device.h"
38 #include "stw_pixelformat.h"
42 struct stw_pf_color_info
44 enum pipe_format format
;
59 struct stw_pf_depth_info
61 enum pipe_format format
;
64 unsigned char stencil
;
69 /* NOTE: order matters, since in otherwise equal circumstances the first
70 * format listed will get chosen */
72 static const struct stw_pf_color_info
75 { PIPE_FORMAT_B8G8R8X8_UNORM
, { 8, 8, 8, 0}, {16, 8, 0, 0} },
76 { PIPE_FORMAT_X8R8G8B8_UNORM
, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
77 { PIPE_FORMAT_B5G6R5_UNORM
, { 5, 6, 5, 0}, {11, 5, 0, 0} },
79 { PIPE_FORMAT_B8G8R8A8_UNORM
, { 8, 8, 8, 8}, {16, 8, 0, 24} },
80 { PIPE_FORMAT_A8R8G8B8_UNORM
, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
82 { PIPE_FORMAT_R10G10B10A2_UNORM
, {10, 10, 10, 2}, { 0, 10, 20, 30} },
84 { PIPE_FORMAT_B5G5R5A1_UNORM
, { 5, 5, 5, 1}, {10, 5, 0, 15} },
85 { PIPE_FORMAT_B4G4R4A4_UNORM
, { 4, 4, 4, 4}, {16, 4, 0, 12} }
88 static const struct stw_pf_color_info
89 stw_pf_color_extended
[] = {
90 { PIPE_FORMAT_R32G32B32A32_FLOAT
, { 32, 32, 32, 32}, { 0, 32, 64, 96} }
93 static const struct stw_pf_depth_info
94 stw_pf_depth_stencil
[] = {
96 { PIPE_FORMAT_Z32_UNORM
, {32, 0} },
97 { PIPE_FORMAT_X8Z24_UNORM
, {24, 0} },
98 { PIPE_FORMAT_Z24X8_UNORM
, {24, 0} },
99 { PIPE_FORMAT_Z16_UNORM
, {16, 0} },
100 /* combined depth-stencil */
101 { PIPE_FORMAT_Z24_UNORM_S8_UINT
, {24, 8} },
102 { PIPE_FORMAT_S8_UINT_Z24_UNORM
, {24, 8} }
107 stw_pf_doublebuffer
[] = {
114 stw_pf_multisample
[] = {
122 struct stw_device
*stw_dev
,
124 const struct stw_pf_color_info
*color
,
125 const struct stw_pf_depth_info
*depth
,
127 boolean doublebuffer
,
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
->pfd
.nSize
= sizeof pfi
->pfd
;
148 pfi
->pfd
.nVersion
= 1;
150 pfi
->pfd
.dwFlags
= PFD_SUPPORT_OPENGL
;
152 /* TODO: also support non-native pixel formats */
154 pfi
->pfd
.dwFlags
|= PFD_DRAW_TO_WINDOW
;
157 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
158 pfi
->pfd
.dwFlags
|= PFD_SUPPORT_COMPOSITION
;
161 pfi
->pfd
.dwFlags
|= PFD_DOUBLEBUFFER
| PFD_SWAP_COPY
;
163 pfi
->pfd
.iPixelType
= PFD_TYPE_RGBA
;
165 pfi
->pfd
.cColorBits
= color
->bits
.red
+ color
->bits
.green
+ color
->bits
.blue
+ color
->bits
.alpha
;
166 pfi
->pfd
.cRedBits
= color
->bits
.red
;
167 pfi
->pfd
.cRedShift
= color
->shift
.red
;
168 pfi
->pfd
.cGreenBits
= color
->bits
.green
;
169 pfi
->pfd
.cGreenShift
= color
->shift
.green
;
170 pfi
->pfd
.cBlueBits
= color
->bits
.blue
;
171 pfi
->pfd
.cBlueShift
= color
->shift
.blue
;
172 pfi
->pfd
.cAlphaBits
= color
->bits
.alpha
;
173 pfi
->pfd
.cAlphaShift
= color
->shift
.alpha
;
174 pfi
->pfd
.cAccumBits
= 4*accum
;
175 pfi
->pfd
.cAccumRedBits
= accum
;
176 pfi
->pfd
.cAccumGreenBits
= accum
;
177 pfi
->pfd
.cAccumBlueBits
= accum
;
178 pfi
->pfd
.cAccumAlphaBits
= accum
;
179 pfi
->pfd
.cDepthBits
= depth
->bits
.depth
;
180 pfi
->pfd
.cStencilBits
= depth
->bits
.stencil
;
181 pfi
->pfd
.cAuxBuffers
= 0;
182 pfi
->pfd
.iLayerType
= 0;
183 pfi
->pfd
.bReserved
= 0;
184 pfi
->pfd
.dwLayerMask
= 0;
185 pfi
->pfd
.dwVisibleMask
= 0;
186 pfi
->pfd
.dwDamageMask
= 0;
189 * since state trackers can allocate depth/stencil/accum buffers, we provide
190 * only color buffers here
192 pfi
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
194 pfi
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
196 pfi
->stvis
.color_format
= color
->format
;
197 pfi
->stvis
.depth_stencil_format
= depth
->format
;
199 pfi
->stvis
.accum_format
= (accum
) ?
200 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
202 pfi
->stvis
.samples
= samples
;
203 pfi
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
205 ++stw_dev
->pixelformat_extended_count
;
208 ++stw_dev
->pixelformat_count
;
209 assert(stw_dev
->pixelformat_count
== stw_dev
->pixelformat_extended_count
);
215 * Add the depth/stencil/accum/ms variants for a particular color format.
218 add_color_format_variants(const struct stw_pf_color_info
*color
,
221 struct pipe_screen
*screen
= stw_dev
->screen
;
222 unsigned ms
, db
, ds
, acc
;
223 unsigned bind_flags
= PIPE_BIND_RENDER_TARGET
;
226 bind_flags
|= PIPE_BIND_DISPLAY_TARGET
;
229 if (!screen
->is_format_supported(screen
, color
->format
,
230 PIPE_TEXTURE_2D
, 0, bind_flags
)) {
234 for (ms
= 0; ms
< Elements(stw_pf_multisample
); ms
++) {
235 unsigned samples
= stw_pf_multisample
[ms
];
237 /* FIXME: re-enabled MSAA when we can query it */
241 for (db
= 0; db
< Elements(stw_pf_doublebuffer
); db
++) {
242 unsigned doublebuffer
= stw_pf_doublebuffer
[db
];
244 for (ds
= 0; ds
< Elements(stw_pf_depth_stencil
); ds
++) {
245 const struct stw_pf_depth_info
*depth
= &stw_pf_depth_stencil
[ds
];
247 if (!screen
->is_format_supported(screen
, depth
->format
,
249 PIPE_BIND_DEPTH_STENCIL
)) {
253 for (acc
= 0; acc
< 2; acc
++) {
254 stw_pixelformat_add(stw_dev
, extended
, color
, depth
,
255 acc
* 16, doublebuffer
, samples
);
264 stw_pixelformat_init( void )
268 assert( !stw_dev
->pixelformat_count
);
269 assert( !stw_dev
->pixelformat_extended_count
);
271 /* normal, displayable formats */
272 for (i
= 0; i
< Elements(stw_pf_color
); i
++) {
273 add_color_format_variants(&stw_pf_color
[i
], FALSE
);
276 /* extended, pbuffer-only formats */
277 for (i
= 0; i
< Elements(stw_pf_color_extended
); i
++) {
278 add_color_format_variants(&stw_pf_color_extended
[i
], TRUE
);
281 assert( stw_dev
->pixelformat_count
<= stw_dev
->pixelformat_extended_count
);
282 assert( stw_dev
->pixelformat_extended_count
<= STW_MAX_PIXELFORMATS
);
286 stw_pixelformat_get_count( void )
288 return stw_dev
->pixelformat_count
;
292 stw_pixelformat_get_extended_count( void )
294 return stw_dev
->pixelformat_extended_count
;
297 const struct stw_pixelformat_info
*
298 stw_pixelformat_get_info( int iPixelFormat
)
302 if (iPixelFormat
<= 0) {
306 index
= iPixelFormat
- 1;
307 if (index
>= stw_dev
->pixelformat_extended_count
) {
311 return &stw_dev
->pixelformats
[index
];
316 DrvDescribePixelFormat(
320 PIXELFORMATDESCRIPTOR
*ppfd
)
323 const struct stw_pixelformat_info
*pfi
;
330 count
= stw_pixelformat_get_count();
334 if (cjpfd
!= sizeof( PIXELFORMATDESCRIPTOR
))
337 pfi
= stw_pixelformat_get_info( iPixelFormat
);
342 memcpy(ppfd
, &pfi
->pfd
, sizeof( PIXELFORMATDESCRIPTOR
));
348 DrvDescribeLayerPlane(
353 LPLAYERPLANEDESCRIPTOR plpd
)
360 DrvGetLayerPaletteEntries(
372 DrvSetLayerPaletteEntries(
377 CONST COLORREF
*pcr
)
384 DrvRealizeLayerPalette(
393 /* Only used by the wgl code, but have it here to avoid exporting the
394 * pixelformat.h functionality.
396 int stw_pixelformat_choose( HDC hdc
,
397 CONST PIXELFORMATDESCRIPTOR
*ppfd
)
406 count
= stw_pixelformat_get_extended_count();
410 for (index
= 1; index
<= count
; index
++) {
412 const struct stw_pixelformat_info
*pfi
= stw_pixelformat_get_info( index
);
414 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
415 !!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
416 !!(pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
))
419 /* FIXME: Take in account individual channel bits */
420 if (ppfd
->cColorBits
!= pfi
->pfd
.cColorBits
)
423 if (ppfd
->cDepthBits
!= pfi
->pfd
.cDepthBits
)
426 if (ppfd
->cStencilBits
!= pfi
->pfd
.cStencilBits
)
429 if (ppfd
->cAlphaBits
!= pfi
->pfd
.cAlphaBits
)
432 if (delta
< bestdelta
) {