1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include "util/format/u_formats.h"
29 #include "pipe/p_defines.h"
30 #include "pipe/p_screen.h"
31
32 #include "util/format/u_format.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
35
36 #include <GL/gl.h>
37 #include "stw_gdishim.h"
38 #include "gldrv.h"
39 #include "stw_device.h"
40 #include "stw_framebuffer.h"
41 #include "stw_pixelformat.h"
42 #include "stw_tls.h"
43 #include "stw_winsys.h"
44
45
46 struct stw_pf_color_info
47 {
48 enum pipe_format format;
49 struct {
50 unsigned char red;
51 unsigned char green;
52 unsigned char blue;
53 unsigned char alpha;
54 } bits;
55 struct {
56 unsigned char red;
57 unsigned char green;
58 unsigned char blue;
59 unsigned char alpha;
60 } shift;
61 };
62
63 struct stw_pf_depth_info
64 {
65 enum pipe_format format;
66 struct {
67 unsigned char depth;
68 unsigned char stencil;
69 } bits;
70 };
71
72
73 /* NOTE: order matters, since in otherwise equal circumstances the first
74 * format listed will get chosen */
75
76 static const struct stw_pf_color_info
77 stw_pf_color[] = {
78 /* no-alpha */
79 { PIPE_FORMAT_B8G8R8X8_UNORM, { 8, 8, 8, 0}, {16, 8, 0, 0} },
80 { PIPE_FORMAT_X8R8G8B8_UNORM, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
81 /* alpha */
82 { PIPE_FORMAT_B8G8R8A8_UNORM, { 8, 8, 8, 8}, {16, 8, 0, 24} },
83 { PIPE_FORMAT_A8R8G8B8_UNORM, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
84 /* shallow bit depths */
85 { PIPE_FORMAT_B5G6R5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 0} },
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} },
88 /* HDR bit depths */
89 { PIPE_FORMAT_R16G16B16A16_FLOAT, {16, 16, 16, 16}, { 0, 16, 32, 48 }},
90 { PIPE_FORMAT_R10G10B10A2_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
91 };
92
93 static const struct stw_pf_color_info
94 stw_pf_color_extended[] = {
95 { PIPE_FORMAT_R32G32B32A32_FLOAT, {32, 32, 32, 32}, {0, 32, 64, 96} }
96 };
97
98 static const struct stw_pf_depth_info
99 stw_pf_depth_stencil[] = {
100 /* pure depth */
101 { PIPE_FORMAT_Z32_UNORM, {32, 0} },
102 { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
103 { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
104 { PIPE_FORMAT_Z16_UNORM, {16, 0} },
105 /* combined depth-stencil */
106 { PIPE_FORMAT_Z24_UNORM_S8_UINT, {24, 8} },
107 { PIPE_FORMAT_S8_UINT_Z24_UNORM, {24, 8} }
108 };
109
110 static const stw_pfd_flag
111 stw_pf_flag[] = {
112 0,
113 stw_pfd_double_buffer,
114 stw_pfd_gdi_support,
115 stw_pfd_double_buffer | stw_pfd_gdi_support,
116 };
117
118
119 const unsigned
120 stw_pf_multisample[] = {
121 0,
122 4,
123 8,
124 16
125 };
126
127
128 static void
stw_pixelformat_add(struct stw_device * stw_dev,bool extended,const struct stw_pf_color_info * color,const struct stw_pf_depth_info * depth,unsigned accum,bool doublebuffer,bool gdi,unsigned samples)129 stw_pixelformat_add(struct stw_device *stw_dev,
130 bool extended,
131 const struct stw_pf_color_info *color,
132 const struct stw_pf_depth_info *depth,
133 unsigned accum,
134 bool doublebuffer,
135 bool gdi,
136 unsigned samples)
137 {
138 struct stw_pixelformat_info *pfi;
139
140 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 0) == color->bits.red);
141 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 1) == color->bits.green);
142 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 2) == color->bits.blue);
143 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 3) == color->bits.alpha);
144 assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 0) == depth->bits.depth);
145 assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 1) == depth->bits.stencil);
146
147 pfi = util_dynarray_grow(&stw_dev->pixelformats,
148 struct stw_pixelformat_info,
149 1);
150
151 memset(pfi, 0, sizeof *pfi);
152
153 pfi->iPixelFormat = util_dynarray_num_elements(&stw_dev->pixelformats, struct stw_pixelformat_info);
154 pfi->pfd.nSize = sizeof pfi->pfd;
155 pfi->pfd.nVersion = 1;
156
157 pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
158
159 /* TODO: also support non-native pixel formats */
160 if (!extended) {
161 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
162 }
163
164 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
165 pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
166
167 if (doublebuffer)
168 pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE;
169
170 if (gdi)
171 pfi->pfd.dwFlags |= PFD_SUPPORT_GDI;
172
173 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
174
175 pfi->pfd.cColorBits =
176 color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
177 pfi->pfd.cRedBits = color->bits.red;
178 pfi->pfd.cRedShift = color->shift.red;
179 pfi->pfd.cGreenBits = color->bits.green;
180 pfi->pfd.cGreenShift = color->shift.green;
181 pfi->pfd.cBlueBits = color->bits.blue;
182 pfi->pfd.cBlueShift = color->shift.blue;
183 pfi->pfd.cAlphaBits = color->bits.alpha;
184 pfi->pfd.cAlphaShift = color->shift.alpha;
185 pfi->pfd.cAccumBits = 4*accum;
186 pfi->pfd.cAccumRedBits = accum;
187 pfi->pfd.cAccumGreenBits = accum;
188 pfi->pfd.cAccumBlueBits = accum;
189 pfi->pfd.cAccumAlphaBits = accum;
190 pfi->pfd.cDepthBits = depth->bits.depth;
191 pfi->pfd.cStencilBits = depth->bits.stencil;
192 pfi->pfd.cAuxBuffers = 0;
193 pfi->pfd.iLayerType = 0;
194 pfi->pfd.bReserved = 0;
195 pfi->pfd.dwLayerMask = 0;
196 pfi->pfd.dwVisibleMask = 0;
197 pfi->pfd.dwDamageMask = 0;
198
199 /*
200 * since gallium frontend can allocate depth/stencil/accum buffers, we provide
201 * only color buffers here in the non-zink case, however in the zink case
202 * kopper requires that we allocate depth/stencil through the winsys
203 */
204 pfi->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
205 if (doublebuffer)
206 pfi->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
207
208 pfi->stvis.color_format = color->format;
209 pfi->stvis.depth_stencil_format = depth->format;
210
211 #ifdef GALLIUM_ZINK
212 if (stw_dev->zink && (depth->bits.depth > 0 || depth->bits.stencil > 0))
213 pfi->stvis.buffer_mask |= ST_ATTACHMENT_DEPTH_STENCIL_MASK;
214 #endif
215
216 pfi->stvis.accum_format = (accum) ?
217 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
218
219 pfi->stvis.samples = samples;
220
221 /* WGL_ARB_render_texture */
222 if (color->bits.alpha)
223 pfi->bindToTextureRGBA = true;
224
225 pfi->bindToTextureRGB = true;
226
227 if (!extended) {
228 ++stw_dev->pixelformat_count;
229 assert(stw_dev->pixelformat_count ==
230 util_dynarray_num_elements(&stw_dev->pixelformats,
231 struct stw_pixelformat_info));
232 }
233 }
234
235
236 /**
237 * Add the depth/stencil/accum/ms variants for a list of color formats.
238 */
239 static unsigned
add_color_format_variants(const struct stw_pf_color_info * color_formats,unsigned num_color_formats,bool extended)240 add_color_format_variants(const struct stw_pf_color_info *color_formats,
241 unsigned num_color_formats, bool extended)
242 {
243 struct pipe_screen *screen = stw_dev->screen;
244 unsigned cfmt, ms, ds, acc, f;
245 unsigned bind_flags = PIPE_BIND_RENDER_TARGET;
246 unsigned num_added = 0;
247 int force_samples = 0;
248
249 /* Since GLUT for Windows doesn't support MSAA we have an env var
250 * to force all pixel formats to have a particular number of samples.
251 */
252 {
253 const char *samples = getenv("WGL_FORCE_MSAA");
254 if (!samples) {
255 static bool warned = false;
256 samples = getenv("SVGA_FORCE_MSAA");
257 if (samples && !warned) {
258 fprintf(stderr, "*** SVGA_FORCE_MSAA is deprecated; "
259 "use WGL_FORCE_MSAA instead ***\n");
260 warned = true;
261 }
262 }
263
264 if (samples)
265 force_samples = atoi(samples);
266 }
267
268 if (!extended) {
269 bind_flags |= PIPE_BIND_DISPLAY_TARGET;
270 }
271
272 for (ms = 0; ms < ARRAY_SIZE(stw_pf_multisample); ms++) {
273 unsigned samples = stw_pf_multisample[ms];
274
275 if (force_samples && samples != force_samples)
276 continue;
277
278 for (cfmt = 0; cfmt < num_color_formats; cfmt++) {
279 if (!screen->is_format_supported(screen, color_formats[cfmt].format,
280 PIPE_TEXTURE_2D, samples, samples,
281 bind_flags)) {
282 continue;
283 }
284
285 for (ds = 0; ds < ARRAY_SIZE(stw_pf_depth_stencil); ds++) {
286 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[ds];
287
288 if (!screen->is_format_supported(screen, depth->format,
289 PIPE_TEXTURE_2D, samples,
290 samples,
291 PIPE_BIND_DEPTH_STENCIL)) {
292 continue;
293 }
294
295 for (f = 0; f < ARRAY_SIZE(stw_pf_flag); f++) {
296 stw_pfd_flag flag = stw_pf_flag[f];
297 for (acc = 0; acc < 2; acc++) {
298 stw_pixelformat_add(stw_dev, extended, &color_formats[cfmt],
299 depth, acc * 16,
300 (flag & stw_pfd_double_buffer) != 0,
301 (flag & stw_pfd_gdi_support) != 0, samples);
302 num_added++;
303 }
304 }
305 }
306 }
307 }
308
309 return num_added;
310 }
311
312
313 void
stw_pixelformat_init(void)314 stw_pixelformat_init(void)
315 {
316 unsigned num_formats;
317
318 assert(!stw_dev->pixelformat_count);
319
320 util_dynarray_init(&stw_dev->pixelformats, NULL);
321
322 /* normal, displayable formats */
323 num_formats = add_color_format_variants(stw_pf_color,
324 ARRAY_SIZE(stw_pf_color), false);
325 assert(num_formats > 0);
326
327 /* extended, pbuffer-only formats */
328 add_color_format_variants(stw_pf_color_extended,
329 ARRAY_SIZE(stw_pf_color_extended), true);
330
331 assert(stw_dev->pixelformat_count <=
332 util_dynarray_num_elements(&stw_dev->pixelformats,
333 struct stw_pixelformat_info));
334 }
335
336
337 uint
stw_pixelformat_get_count(HDC hdc)338 stw_pixelformat_get_count(HDC hdc)
339 {
340 if (!stw_init_screen(hdc))
341 return 0;
342
343 return stw_dev->pixelformat_count;
344 }
345
346
347 uint
stw_pixelformat_get_extended_count(HDC hdc)348 stw_pixelformat_get_extended_count(HDC hdc)
349 {
350 if (!stw_init_screen(hdc))
351 return 0;
352
353 return util_dynarray_num_elements(&stw_dev->pixelformats,
354 struct stw_pixelformat_info);
355 }
356
357
358 const struct stw_pixelformat_info *
stw_pixelformat_get_info(int iPixelFormat)359 stw_pixelformat_get_info(int iPixelFormat)
360 {
361 unsigned index;
362
363 if (iPixelFormat <= 0) {
364 return NULL;
365 }
366
367 index = iPixelFormat - 1;
368 if (index >= util_dynarray_num_elements(&stw_dev->pixelformats,
369 struct stw_pixelformat_info)) {
370 return NULL;
371 }
372
373 return util_dynarray_element(&stw_dev->pixelformats,
374 struct stw_pixelformat_info,
375 index);
376 }
377
378 /**
379 * Return the stw pixel format that most closely matches the pixel format
380 * on HDC.
381 * Used to get a pixel format when SetPixelFormat() hasn't been called before.
382 */
383 int
stw_pixelformat_guess(HDC hdc)384 stw_pixelformat_guess(HDC hdc)
385 {
386 int iPixelFormat = GetPixelFormat(hdc);
387 PIXELFORMATDESCRIPTOR pfd;
388
389 if (!iPixelFormat)
390 return 0;
391 if (!DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd))
392 return 0;
393 return stw_pixelformat_choose(hdc, &pfd);
394 }
395
396 const struct stw_pixelformat_info *
stw_pixelformat_get_info_from_hdc(HDC hdc)397 stw_pixelformat_get_info_from_hdc(HDC hdc)
398 {
399 /*
400 * GDI only knows about displayable pixel formats, so determine the pixel
401 * format from the framebuffer.
402 *
403 * This also allows to use a OpenGL DLL / ICD without installing.
404 */
405 struct stw_framebuffer *fb;
406 fb = stw_framebuffer_from_hdc(hdc);
407 if (fb) {
408 const struct stw_pixelformat_info *pfi = fb->pfi;
409 stw_framebuffer_unlock(fb);
410 return pfi;
411 }
412
413 /* Applications should call SetPixelFormat before creating a context,
414 * but not all do, and the opengl32 runtime seems to use a default
415 * pixel format in some cases, so use that.
416 */
417 int iPixelFormat = stw_pixelformat_guess(hdc);
418 if (!iPixelFormat)
419 return 0;
420
421 return stw_pixelformat_get_info( iPixelFormat );
422 }
423
424
425 LONG APIENTRY
DrvDescribePixelFormat(HDC hdc,INT iPixelFormat,ULONG cjpfd,PIXELFORMATDESCRIPTOR * ppfd)426 DrvDescribePixelFormat(HDC hdc, INT iPixelFormat, ULONG cjpfd,
427 PIXELFORMATDESCRIPTOR *ppfd)
428 {
429 uint count;
430 const struct stw_pixelformat_info *pfi;
431
432 if (!stw_dev)
433 return 0;
434
435 count = stw_pixelformat_get_count(hdc);
436
437 if (ppfd == NULL)
438 return count;
439
440 if (cjpfd != sizeof(PIXELFORMATDESCRIPTOR))
441 return 0;
442
443 pfi = stw_pixelformat_get_info(iPixelFormat);
444 if (!pfi) {
445 return 0;
446 }
447
448 memcpy(ppfd, &pfi->pfd, sizeof(PIXELFORMATDESCRIPTOR));
449
450 return count;
451 }
452
453
454 BOOL APIENTRY
DrvDescribeLayerPlane(HDC hdc,INT iPixelFormat,INT iLayerPlane,UINT nBytes,LPLAYERPLANEDESCRIPTOR plpd)455 DrvDescribeLayerPlane(HDC hdc, INT iPixelFormat, INT iLayerPlane,
456 UINT nBytes, LPLAYERPLANEDESCRIPTOR plpd)
457 {
458 assert(0);
459 return false;
460 }
461
462
463 int APIENTRY
DrvGetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,COLORREF * pcr)464 DrvGetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
465 INT cEntries, COLORREF *pcr)
466 {
467 assert(0);
468 return 0;
469 }
470
471
472 int APIENTRY
DrvSetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,CONST COLORREF * pcr)473 DrvSetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
474 INT cEntries, CONST COLORREF *pcr)
475 {
476 assert(0);
477 return 0;
478 }
479
480
481 BOOL APIENTRY
DrvRealizeLayerPalette(HDC hdc,INT iLayerPlane,BOOL bRealize)482 DrvRealizeLayerPalette(HDC hdc, INT iLayerPlane, BOOL bRealize)
483 {
484 assert(0);
485 return false;
486 }
487
488
489 /* Only used by the wgl code, but have it here to avoid exporting the
490 * pixelformat.h functionality.
491 */
492 int
stw_pixelformat_choose(HDC hdc,CONST PIXELFORMATDESCRIPTOR * ppfd)493 stw_pixelformat_choose(HDC hdc, CONST PIXELFORMATDESCRIPTOR *ppfd)
494 {
495 uint count;
496 uint index;
497 uint bestindex;
498 uint bestdelta;
499
500 count = stw_pixelformat_get_extended_count(hdc);
501 bestindex = 0;
502 bestdelta = ~0U;
503
504 for (index = 1; index <= count; index++) {
505 uint delta = 0;
506 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info(index);
507
508 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
509 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
510 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
511 continue;
512
513 /* Selection logic:
514 * - Enabling a feature (depth, stencil...) is given highest priority.
515 * - Giving as many bits as requested is given medium priority.
516 * - Giving no more bits than requested is given lowest priority.
517 */
518
519 if (ppfd->cColorBits && !pfi->pfd.cColorBits)
520 delta += 10000;
521 else if (ppfd->cColorBits > pfi->pfd.cColorBits)
522 delta += 100;
523 else if (ppfd->cColorBits < pfi->pfd.cColorBits)
524 delta++;
525
526 if (ppfd->cDepthBits && !pfi->pfd.cDepthBits)
527 delta += 10000;
528 else if (ppfd->cDepthBits > pfi->pfd.cDepthBits)
529 delta += 200;
530 else if (ppfd->cDepthBits < pfi->pfd.cDepthBits)
531 delta += 2;
532
533 if (ppfd->cStencilBits && !pfi->pfd.cStencilBits)
534 delta += 10000;
535 else if (ppfd->cStencilBits > pfi->pfd.cStencilBits)
536 delta += 400;
537 else if (ppfd->cStencilBits < pfi->pfd.cStencilBits)
538 delta++;
539
540 if (ppfd->cAlphaBits && !pfi->pfd.cAlphaBits)
541 delta += 10000;
542 else if (ppfd->cAlphaBits > pfi->pfd.cAlphaBits)
543 delta += 100;
544 else if (ppfd->cAlphaBits < pfi->pfd.cAlphaBits)
545 delta++;
546
547 if (ppfd->cRedBits && !pfi->pfd.cRedBits)
548 delta += 10000;
549 else if (ppfd->cRedBits > pfi->pfd.cRedBits)
550 delta += 100;
551 else if (ppfd->cRedBits < pfi->pfd.cRedBits)
552 delta++;
553
554 if (ppfd->cGreenBits && !pfi->pfd.cGreenBits)
555 delta += 10000;
556 else if (ppfd->cGreenBits > pfi->pfd.cGreenBits)
557 delta += 100;
558 else if (ppfd->cGreenBits < pfi->pfd.cGreenBits)
559 delta++;
560
561 if (ppfd->cBlueBits && !pfi->pfd.cBlueBits)
562 delta += 10000;
563 else if (ppfd->cBlueBits > pfi->pfd.cBlueBits)
564 delta += 100;
565 else if (ppfd->cBlueBits < pfi->pfd.cBlueBits)
566 delta++;
567
568 if (delta < bestdelta) {
569 bestindex = index;
570 bestdelta = delta;
571 if (bestdelta == 0)
572 break;
573 }
574 }
575
576 return bestindex;
577 }
578