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/format/u_format.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.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_B8G8R8X8_UNORM
, { 8, 8, 8, 0}, {16, 8, 0, 0} },
77 { PIPE_FORMAT_X8R8G8B8_UNORM
, { 8, 8, 8, 0}, { 8, 16, 24, 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} },
81 /* shallow bit depths */
82 { PIPE_FORMAT_B5G6R5_UNORM
, { 5, 6, 5, 0}, {11, 5, 0, 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} }
90 static const struct stw_pf_color_info
91 stw_pf_color_extended
[] = {
92 { PIPE_FORMAT_R32G32B32A32_FLOAT
, {32, 32, 32, 32}, {0, 32, 64, 96} }
95 static const struct stw_pf_depth_info
96 stw_pf_depth_stencil
[] = {
98 { PIPE_FORMAT_Z32_UNORM
, {32, 0} },
99 { PIPE_FORMAT_X8Z24_UNORM
, {24, 0} },
100 { PIPE_FORMAT_Z24X8_UNORM
, {24, 0} },
101 { PIPE_FORMAT_Z16_UNORM
, {16, 0} },
102 /* combined depth-stencil */
103 { PIPE_FORMAT_Z24_UNORM_S8_UINT
, {24, 8} },
104 { PIPE_FORMAT_S8_UINT_Z24_UNORM
, {24, 8} }
109 stw_pf_doublebuffer
[] = {
116 stw_pf_multisample
[] = {
125 stw_pixelformat_add(struct stw_device
*stw_dev
,
127 const struct stw_pf_color_info
*color
,
128 const struct stw_pf_depth_info
*depth
,
130 boolean doublebuffer
,
133 struct stw_pixelformat_info
*pfi
;
135 assert(stw_dev
->pixelformat_extended_count
< STW_MAX_PIXELFORMATS
);
136 if (stw_dev
->pixelformat_extended_count
>= STW_MAX_PIXELFORMATS
)
139 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 0) == color
->bits
.red
);
140 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 1) == color
->bits
.green
);
141 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 2) == color
->bits
.blue
);
142 assert(util_format_get_component_bits(color
->format
, UTIL_FORMAT_COLORSPACE_RGB
, 3) == color
->bits
.alpha
);
143 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 0) == depth
->bits
.depth
);
144 assert(util_format_get_component_bits(depth
->format
, UTIL_FORMAT_COLORSPACE_ZS
, 1) == depth
->bits
.stencil
);
146 pfi
= &stw_dev
->pixelformats
[stw_dev
->pixelformat_extended_count
];
148 memset(pfi
, 0, sizeof *pfi
);
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 */
157 pfi
->pfd
.dwFlags
|= PFD_DRAW_TO_WINDOW
;
160 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
161 pfi
->pfd
.dwFlags
|= PFD_SUPPORT_COMPOSITION
;
164 pfi
->pfd
.dwFlags
|= PFD_DOUBLEBUFFER
| PFD_SWAP_EXCHANGE
;
166 pfi
->pfd
.iPixelType
= PFD_TYPE_RGBA
;
168 pfi
->pfd
.cColorBits
=
169 color
->bits
.red
+ color
->bits
.green
+ color
->bits
.blue
+ color
->bits
.alpha
;
170 pfi
->pfd
.cRedBits
= color
->bits
.red
;
171 pfi
->pfd
.cRedShift
= color
->shift
.red
;
172 pfi
->pfd
.cGreenBits
= color
->bits
.green
;
173 pfi
->pfd
.cGreenShift
= color
->shift
.green
;
174 pfi
->pfd
.cBlueBits
= color
->bits
.blue
;
175 pfi
->pfd
.cBlueShift
= color
->shift
.blue
;
176 pfi
->pfd
.cAlphaBits
= color
->bits
.alpha
;
177 pfi
->pfd
.cAlphaShift
= color
->shift
.alpha
;
178 pfi
->pfd
.cAccumBits
= 4*accum
;
179 pfi
->pfd
.cAccumRedBits
= accum
;
180 pfi
->pfd
.cAccumGreenBits
= accum
;
181 pfi
->pfd
.cAccumBlueBits
= accum
;
182 pfi
->pfd
.cAccumAlphaBits
= accum
;
183 pfi
->pfd
.cDepthBits
= depth
->bits
.depth
;
184 pfi
->pfd
.cStencilBits
= depth
->bits
.stencil
;
185 pfi
->pfd
.cAuxBuffers
= 0;
186 pfi
->pfd
.iLayerType
= 0;
187 pfi
->pfd
.bReserved
= 0;
188 pfi
->pfd
.dwLayerMask
= 0;
189 pfi
->pfd
.dwVisibleMask
= 0;
190 pfi
->pfd
.dwDamageMask
= 0;
193 * since state trackers can allocate depth/stencil/accum buffers, we provide
194 * only color buffers here
196 pfi
->stvis
.buffer_mask
= ST_ATTACHMENT_FRONT_LEFT_MASK
;
198 pfi
->stvis
.buffer_mask
|= ST_ATTACHMENT_BACK_LEFT_MASK
;
200 pfi
->stvis
.color_format
= color
->format
;
201 pfi
->stvis
.depth_stencil_format
= depth
->format
;
203 pfi
->stvis
.accum_format
= (accum
) ?
204 PIPE_FORMAT_R16G16B16A16_SNORM
: PIPE_FORMAT_NONE
;
206 pfi
->stvis
.samples
= samples
;
207 pfi
->stvis
.render_buffer
= ST_ATTACHMENT_INVALID
;
209 /* WGL_ARB_render_texture */
210 if (color
->bits
.alpha
)
211 pfi
->bindToTextureRGBA
= TRUE
;
213 pfi
->bindToTextureRGB
= TRUE
;
215 ++stw_dev
->pixelformat_extended_count
;
218 ++stw_dev
->pixelformat_count
;
219 assert(stw_dev
->pixelformat_count
== stw_dev
->pixelformat_extended_count
);
225 * Add the depth/stencil/accum/ms variants for a list of color formats.
228 add_color_format_variants(const struct stw_pf_color_info
*color_formats
,
229 unsigned num_color_formats
, boolean extended
)
231 struct pipe_screen
*screen
= stw_dev
->screen
;
232 unsigned cfmt
, ms
, db
, ds
, acc
;
233 unsigned bind_flags
= PIPE_BIND_RENDER_TARGET
;
234 unsigned num_added
= 0;
235 int force_samples
= 0;
237 /* Since GLUT for Windows doesn't support MSAA we have an env var
238 * to force all pixel formats to have a particular number of samples.
241 const char *samples
= getenv("SVGA_FORCE_MSAA");
243 force_samples
= atoi(samples
);
247 bind_flags
|= PIPE_BIND_DISPLAY_TARGET
;
250 for (ms
= 0; ms
< ARRAY_SIZE(stw_pf_multisample
); ms
++) {
251 unsigned samples
= stw_pf_multisample
[ms
];
253 if (force_samples
&& samples
!= force_samples
)
256 for (cfmt
= 0; cfmt
< num_color_formats
; cfmt
++) {
257 if (!screen
->is_format_supported(screen
, color_formats
[cfmt
].format
,
258 PIPE_TEXTURE_2D
, samples
, samples
,
263 for (db
= 0; db
< ARRAY_SIZE(stw_pf_doublebuffer
); db
++) {
264 unsigned doublebuffer
= stw_pf_doublebuffer
[db
];
266 for (ds
= 0; ds
< ARRAY_SIZE(stw_pf_depth_stencil
); ds
++) {
267 const struct stw_pf_depth_info
*depth
= &stw_pf_depth_stencil
[ds
];
269 if (!screen
->is_format_supported(screen
, depth
->format
,
270 PIPE_TEXTURE_2D
, samples
,
272 PIPE_BIND_DEPTH_STENCIL
)) {
276 for (acc
= 0; acc
< 2; acc
++) {
277 stw_pixelformat_add(stw_dev
, extended
, &color_formats
[cfmt
],
279 acc
* 16, doublebuffer
, samples
);
292 stw_pixelformat_init(void)
294 unsigned num_formats
;
296 assert(!stw_dev
->pixelformat_count
);
297 assert(!stw_dev
->pixelformat_extended_count
);
299 /* normal, displayable formats */
300 num_formats
= add_color_format_variants(stw_pf_color
,
301 ARRAY_SIZE(stw_pf_color
), FALSE
);
302 assert(num_formats
> 0);
304 /* extended, pbuffer-only formats */
305 add_color_format_variants(stw_pf_color_extended
,
306 ARRAY_SIZE(stw_pf_color_extended
), TRUE
);
308 assert(stw_dev
->pixelformat_count
<= stw_dev
->pixelformat_extended_count
);
309 assert(stw_dev
->pixelformat_extended_count
<= STW_MAX_PIXELFORMATS
);
314 stw_pixelformat_get_count(void)
316 if (!stw_init_screen())
319 return stw_dev
->pixelformat_count
;
324 stw_pixelformat_get_extended_count(void)
326 if (!stw_init_screen())
329 return stw_dev
->pixelformat_extended_count
;
333 const struct stw_pixelformat_info
*
334 stw_pixelformat_get_info(int iPixelFormat
)
338 if (iPixelFormat
<= 0) {
342 index
= iPixelFormat
- 1;
343 if (index
>= stw_dev
->pixelformat_extended_count
) {
347 return &stw_dev
->pixelformats
[index
];
352 DrvDescribePixelFormat(HDC hdc
, INT iPixelFormat
, ULONG cjpfd
,
353 PIXELFORMATDESCRIPTOR
*ppfd
)
356 const struct stw_pixelformat_info
*pfi
;
363 count
= stw_pixelformat_get_count();
368 if (cjpfd
!= sizeof(PIXELFORMATDESCRIPTOR
))
371 pfi
= stw_pixelformat_get_info(iPixelFormat
);
376 memcpy(ppfd
, &pfi
->pfd
, sizeof(PIXELFORMATDESCRIPTOR
));
383 DrvDescribeLayerPlane(HDC hdc
, INT iPixelFormat
, INT iLayerPlane
,
384 UINT nBytes
, LPLAYERPLANEDESCRIPTOR plpd
)
392 DrvGetLayerPaletteEntries(HDC hdc
, INT iLayerPlane
, INT iStart
,
393 INT cEntries
, COLORREF
*pcr
)
401 DrvSetLayerPaletteEntries(HDC hdc
, INT iLayerPlane
, INT iStart
,
402 INT cEntries
, CONST COLORREF
*pcr
)
410 DrvRealizeLayerPalette(HDC hdc
, INT iLayerPlane
, BOOL bRealize
)
417 /* Only used by the wgl code, but have it here to avoid exporting the
418 * pixelformat.h functionality.
421 stw_pixelformat_choose(HDC hdc
, CONST PIXELFORMATDESCRIPTOR
*ppfd
)
430 count
= stw_pixelformat_get_extended_count();
434 for (index
= 1; index
<= count
; index
++) {
436 const struct stw_pixelformat_info
*pfi
= stw_pixelformat_get_info(index
);
438 if (!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER_DONTCARE
) &&
439 !!(ppfd
->dwFlags
& PFD_DOUBLEBUFFER
) !=
440 !!(pfi
->pfd
.dwFlags
& PFD_DOUBLEBUFFER
))
444 * - Enabling a feature (depth, stencil...) is given highest priority.
445 * - Giving as many bits as requested is given medium priority.
446 * - Giving no more bits than requested is given lowest priority.
449 /* FIXME: Take in account individual channel bits */
450 if (ppfd
->cColorBits
&& !pfi
->pfd
.cColorBits
)
452 else if (ppfd
->cColorBits
> pfi
->pfd
.cColorBits
)
454 else if (ppfd
->cColorBits
< pfi
->pfd
.cColorBits
)
457 if (ppfd
->cDepthBits
&& !pfi
->pfd
.cDepthBits
)
459 else if (ppfd
->cDepthBits
> pfi
->pfd
.cDepthBits
)
461 else if (ppfd
->cDepthBits
< pfi
->pfd
.cDepthBits
)
464 if (ppfd
->cStencilBits
&& !pfi
->pfd
.cStencilBits
)
466 else if (ppfd
->cStencilBits
> pfi
->pfd
.cStencilBits
)
468 else if (ppfd
->cStencilBits
< pfi
->pfd
.cStencilBits
)
471 if (ppfd
->cAlphaBits
&& !pfi
->pfd
.cAlphaBits
)
473 else if (ppfd
->cAlphaBits
> pfi
->pfd
.cAlphaBits
)
475 else if (ppfd
->cAlphaBits
< pfi
->pfd
.cAlphaBits
)
478 if (delta
< bestdelta
) {