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"
41
#include "stw_winsys.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} },
80
{ PIPE_FORMAT_B8G8R8A8_UNORM, { 8, 8, 8, 8}, {16, 8, 0, 24} },
81
{ PIPE_FORMAT_A8R8G8B8_UNORM, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
82
/* shallow bit depths */
83
{ PIPE_FORMAT_B5G6R5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 0} },
85
{ PIPE_FORMAT_R10G10B10A2_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
87
{ PIPE_FORMAT_B5G5R5A1_UNORM, { 5, 5, 5, 1}, {10, 5, 0, 15} },
88
{ PIPE_FORMAT_B4G4R4A4_UNORM, { 4, 4, 4, 4}, {16, 4, 0, 12} }
91
static const struct stw_pf_color_info
92
stw_pf_color_extended[] = {
93
{ PIPE_FORMAT_R32G32B32A32_FLOAT, {32, 32, 32, 32}, {0, 32, 64, 96} }
96
static const struct stw_pf_depth_info
97
stw_pf_depth_stencil[] = {
99
{ PIPE_FORMAT_Z32_UNORM, {32, 0} },
100
{ PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
101
{ PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
102
{ PIPE_FORMAT_Z16_UNORM, {16, 0} },
103
/* combined depth-stencil */
104
{ PIPE_FORMAT_Z24_UNORM_S8_UINT, {24, 8} },
105
{ PIPE_FORMAT_S8_UINT_Z24_UNORM, {24, 8} }
110
stw_pf_doublebuffer[] = {
116
static const stw_pfd_flag
118
stw_pfd_double_buffer,
124
stw_pf_multisample[] = {
133
stw_pixelformat_add(struct stw_device *stw_dev,
135
const struct stw_pf_color_info *color,
136
const struct stw_pf_depth_info *depth,
138
boolean doublebuffer,
142
struct stw_pixelformat_info *pfi;
144
assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 0) == color->bits.red);
145
assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 1) == color->bits.green);
146
assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 2) == color->bits.blue);
147
assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 3) == color->bits.alpha);
148
assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 0) == depth->bits.depth);
149
assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 1) == depth->bits.stencil);
151
pfi = util_dynarray_grow(&stw_dev->pixelformats,
152
struct stw_pixelformat_info,
155
memset(pfi, 0, sizeof *pfi);
157
pfi->iPixelFormat = util_dynarray_num_elements(&stw_dev->pixelformats, struct stw_pixelformat_info);
158
pfi->pfd.nSize = sizeof pfi->pfd;
159
pfi->pfd.nVersion = 1;
161
pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
163
/* TODO: also support non-native pixel formats */
165
pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
168
/* See http://www.opengl.org/pipeline/article/vol003_7/ */
169
pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
172
pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE;
175
pfi->pfd.dwFlags |= PFD_SUPPORT_GDI;
177
pfi->pfd.iPixelType = PFD_TYPE_RGBA;
179
pfi->pfd.cColorBits =
180
color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
181
pfi->pfd.cRedBits = color->bits.red;
182
pfi->pfd.cRedShift = color->shift.red;
183
pfi->pfd.cGreenBits = color->bits.green;
184
pfi->pfd.cGreenShift = color->shift.green;
185
pfi->pfd.cBlueBits = color->bits.blue;
186
pfi->pfd.cBlueShift = color->shift.blue;
187
pfi->pfd.cAlphaBits = color->bits.alpha;
188
pfi->pfd.cAlphaShift = color->shift.alpha;
189
pfi->pfd.cAccumBits = 4*accum;
190
pfi->pfd.cAccumRedBits = accum;
191
pfi->pfd.cAccumGreenBits = accum;
192
pfi->pfd.cAccumBlueBits = accum;
193
pfi->pfd.cAccumAlphaBits = accum;
194
pfi->pfd.cDepthBits = depth->bits.depth;
195
pfi->pfd.cStencilBits = depth->bits.stencil;
196
pfi->pfd.cAuxBuffers = 0;
197
pfi->pfd.iLayerType = 0;
198
pfi->pfd.bReserved = 0;
199
pfi->pfd.dwLayerMask = 0;
200
pfi->pfd.dwVisibleMask = 0;
201
pfi->pfd.dwDamageMask = 0;
204
* since gallium frontend can allocate depth/stencil/accum buffers, we provide
205
* only color buffers here
207
pfi->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
209
pfi->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
211
pfi->stvis.color_format = color->format;
212
pfi->stvis.depth_stencil_format = depth->format;
214
pfi->stvis.accum_format = (accum) ?
215
PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
217
pfi->stvis.samples = samples;
219
/* WGL_ARB_render_texture */
220
if (color->bits.alpha)
221
pfi->bindToTextureRGBA = TRUE;
223
pfi->bindToTextureRGB = TRUE;
226
++stw_dev->pixelformat_count;
227
assert(stw_dev->pixelformat_count ==
228
util_dynarray_num_elements(&stw_dev->pixelformats,
229
struct stw_pixelformat_info));
235
* Add the depth/stencil/accum/ms variants for a list of color formats.
238
add_color_format_variants(const struct stw_pf_color_info *color_formats,
239
unsigned num_color_formats, boolean extended)
241
struct pipe_screen *screen = stw_dev->screen;
242
unsigned cfmt, ms, db, ds, acc, f;
243
unsigned bind_flags = PIPE_BIND_RENDER_TARGET;
244
unsigned num_added = 0;
245
int force_samples = 0;
247
unsigned supported_flags = 0;
248
if (stw_dev->stw_winsys && stw_dev->stw_winsys->get_pfd_flags)
249
supported_flags = stw_dev->stw_winsys->get_pfd_flags(screen);
251
/* Since GLUT for Windows doesn't support MSAA we have an env var
252
* to force all pixel formats to have a particular number of samples.
255
const char *samples = getenv("WGL_FORCE_MSAA");
257
static bool warned = false;
258
samples = getenv("SVGA_FORCE_MSAA");
259
if (samples && !warned) {
260
fprintf(stderr, "*** SVGA_FORCE_MSAA is deprecated; "
261
"use WGL_FORCE_MSAA instead ***\n");
267
force_samples = atoi(samples);
271
bind_flags |= PIPE_BIND_DISPLAY_TARGET;
274
for (ms = 0; ms < ARRAY_SIZE(stw_pf_multisample); ms++) {
275
unsigned samples = stw_pf_multisample[ms];
277
if (force_samples && samples != force_samples)
280
for (cfmt = 0; cfmt < num_color_formats; cfmt++) {
281
if (!screen->is_format_supported(screen, color_formats[cfmt].format,
282
PIPE_TEXTURE_2D, samples, samples,
287
for (db = 0; db < ARRAY_SIZE(stw_pf_doublebuffer); db++) {
288
unsigned doublebuffer = stw_pf_doublebuffer[db];
290
for (ds = 0; ds < ARRAY_SIZE(stw_pf_depth_stencil); ds++) {
291
const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[ds];
293
if (!screen->is_format_supported(screen, depth->format,
294
PIPE_TEXTURE_2D, samples,
296
PIPE_BIND_DEPTH_STENCIL)) {
300
for (f = 0; f < ARRAY_SIZE(stw_pf_flag); f++) {
301
stw_pfd_flag flag = stw_pf_flag[f];
302
if (!(supported_flags & flag) || (flag == stw_pfd_double_buffer && !doublebuffer))
304
for (acc = 0; acc < 2; acc++) {
305
stw_pixelformat_add(stw_dev, extended, &color_formats[cfmt],
306
depth, acc * 16, doublebuffer,
307
(flag == stw_pfd_gdi_support), samples);
321
stw_pixelformat_init(void)
323
unsigned num_formats;
325
assert(!stw_dev->pixelformat_count);
327
util_dynarray_init(&stw_dev->pixelformats, NULL);
329
/* normal, displayable formats */
330
num_formats = add_color_format_variants(stw_pf_color,
331
ARRAY_SIZE(stw_pf_color), FALSE);
332
assert(num_formats > 0);
334
/* extended, pbuffer-only formats */
335
add_color_format_variants(stw_pf_color_extended,
336
ARRAY_SIZE(stw_pf_color_extended), TRUE);
338
assert(stw_dev->pixelformat_count <=
339
util_dynarray_num_elements(&stw_dev->pixelformats,
340
struct stw_pixelformat_info));
345
stw_pixelformat_get_count(HDC hdc)
347
if (!stw_init_screen(hdc))
350
return stw_dev->pixelformat_count;
355
stw_pixelformat_get_extended_count(HDC hdc)
357
if (!stw_init_screen(hdc))
360
return util_dynarray_num_elements(&stw_dev->pixelformats,
361
struct stw_pixelformat_info);
365
const struct stw_pixelformat_info *
366
stw_pixelformat_get_info(int iPixelFormat)
370
if (iPixelFormat <= 0) {
374
index = iPixelFormat - 1;
375
if (index >= util_dynarray_num_elements(&stw_dev->pixelformats,
376
struct stw_pixelformat_info)) {
380
return util_dynarray_element(&stw_dev->pixelformats,
381
struct stw_pixelformat_info,
387
DrvDescribePixelFormat(HDC hdc, INT iPixelFormat, ULONG cjpfd,
388
PIXELFORMATDESCRIPTOR *ppfd)
391
const struct stw_pixelformat_info *pfi;
396
count = stw_pixelformat_get_count(hdc);
401
if (cjpfd != sizeof(PIXELFORMATDESCRIPTOR))
404
pfi = stw_pixelformat_get_info(iPixelFormat);
409
memcpy(ppfd, &pfi->pfd, sizeof(PIXELFORMATDESCRIPTOR));
416
DrvDescribeLayerPlane(HDC hdc, INT iPixelFormat, INT iLayerPlane,
417
UINT nBytes, LPLAYERPLANEDESCRIPTOR plpd)
425
DrvGetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
426
INT cEntries, COLORREF *pcr)
434
DrvSetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
435
INT cEntries, CONST COLORREF *pcr)
443
DrvRealizeLayerPalette(HDC hdc, INT iLayerPlane, BOOL bRealize)
450
/* Only used by the wgl code, but have it here to avoid exporting the
451
* pixelformat.h functionality.
454
stw_pixelformat_choose(HDC hdc, CONST PIXELFORMATDESCRIPTOR *ppfd)
461
count = stw_pixelformat_get_extended_count(hdc);
465
for (index = 1; index <= count; index++) {
467
const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info(index);
469
if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
470
!!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
471
!!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
475
* - Enabling a feature (depth, stencil...) is given highest priority.
476
* - Giving as many bits as requested is given medium priority.
477
* - Giving no more bits than requested is given lowest priority.
480
/* FIXME: Take in account individual channel bits */
481
if (ppfd->cColorBits && !pfi->pfd.cColorBits)
483
else if (ppfd->cColorBits > pfi->pfd.cColorBits)
485
else if (ppfd->cColorBits < pfi->pfd.cColorBits)
488
if (ppfd->cDepthBits && !pfi->pfd.cDepthBits)
490
else if (ppfd->cDepthBits > pfi->pfd.cDepthBits)
492
else if (ppfd->cDepthBits < pfi->pfd.cDepthBits)
495
if (ppfd->cStencilBits && !pfi->pfd.cStencilBits)
497
else if (ppfd->cStencilBits > pfi->pfd.cStencilBits)
499
else if (ppfd->cStencilBits < pfi->pfd.cStencilBits)
502
if (ppfd->cAlphaBits && !pfi->pfd.cAlphaBits)
504
else if (ppfd->cAlphaBits > pfi->pfd.cAlphaBits)
506
else if (ppfd->cAlphaBits < pfi->pfd.cAlphaBits)
509
if (delta < bestdelta) {