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 unsigned supported_flags = 0;
250 if (stw_dev->stw_winsys && stw_dev->stw_winsys->get_pfd_flags)
251 supported_flags = stw_dev->stw_winsys->get_pfd_flags(screen);
252
253 /* Since GLUT for Windows doesn't support MSAA we have an env var
254 * to force all pixel formats to have a particular number of samples.
255 */
256 {
257 const char *samples = getenv("WGL_FORCE_MSAA");
258 if (!samples) {
259 static bool warned = false;
260 samples = getenv("SVGA_FORCE_MSAA");
261 if (samples && !warned) {
262 fprintf(stderr, "*** SVGA_FORCE_MSAA is deprecated; "
263 "use WGL_FORCE_MSAA instead ***\n");
264 warned = true;
265 }
266 }
267
268 if (samples)
269 force_samples = atoi(samples);
270 }
271
272 if (!extended) {
273 bind_flags |= PIPE_BIND_DISPLAY_TARGET;
274 }
275
276 for (ms = 0; ms < ARRAY_SIZE(stw_pf_multisample); ms++) {
277 unsigned samples = stw_pf_multisample[ms];
278
279 if (force_samples && samples != force_samples)
280 continue;
281
282 for (cfmt = 0; cfmt < num_color_formats; cfmt++) {
283 if (!screen->is_format_supported(screen, color_formats[cfmt].format,
284 PIPE_TEXTURE_2D, samples, samples,
285 bind_flags)) {
286 continue;
287 }
288
289 for (ds = 0; ds < ARRAY_SIZE(stw_pf_depth_stencil); ds++) {
290 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[ds];
291
292 if (!screen->is_format_supported(screen, depth->format,
293 PIPE_TEXTURE_2D, samples,
294 samples,
295 PIPE_BIND_DEPTH_STENCIL)) {
296 continue;
297 }
298
299 for (f = 0; f < ARRAY_SIZE(stw_pf_flag); f++) {
300 stw_pfd_flag flag = stw_pf_flag[f];
301 if ((supported_flags & flag) != flag)
302 continue;
303 for (acc = 0; acc < 2; acc++) {
304 stw_pixelformat_add(stw_dev, extended, &color_formats[cfmt],
305 depth, acc * 16,
306 (flag & stw_pfd_double_buffer) != 0,
307 (flag == stw_pfd_gdi_support) != 0, samples);
308 num_added++;
309 }
310 }
311 }
312 }
313 }
314
315 return num_added;
316 }
317
318
319 void
stw_pixelformat_init(void)320 stw_pixelformat_init(void)
321 {
322 unsigned num_formats;
323
324 assert(!stw_dev->pixelformat_count);
325
326 util_dynarray_init(&stw_dev->pixelformats, NULL);
327
328 /* normal, displayable formats */
329 num_formats = add_color_format_variants(stw_pf_color,
330 ARRAY_SIZE(stw_pf_color), false);
331 assert(num_formats > 0);
332
333 /* extended, pbuffer-only formats */
334 add_color_format_variants(stw_pf_color_extended,
335 ARRAY_SIZE(stw_pf_color_extended), true);
336
337 assert(stw_dev->pixelformat_count <=
338 util_dynarray_num_elements(&stw_dev->pixelformats,
339 struct stw_pixelformat_info));
340 }
341
342
343 uint
stw_pixelformat_get_count(HDC hdc)344 stw_pixelformat_get_count(HDC hdc)
345 {
346 if (!stw_init_screen(hdc))
347 return 0;
348
349 return stw_dev->pixelformat_count;
350 }
351
352
353 uint
stw_pixelformat_get_extended_count(HDC hdc)354 stw_pixelformat_get_extended_count(HDC hdc)
355 {
356 if (!stw_init_screen(hdc))
357 return 0;
358
359 return util_dynarray_num_elements(&stw_dev->pixelformats,
360 struct stw_pixelformat_info);
361 }
362
363
364 const struct stw_pixelformat_info *
stw_pixelformat_get_info(int iPixelFormat)365 stw_pixelformat_get_info(int iPixelFormat)
366 {
367 unsigned index;
368
369 if (iPixelFormat <= 0) {
370 return NULL;
371 }
372
373 index = iPixelFormat - 1;
374 if (index >= util_dynarray_num_elements(&stw_dev->pixelformats,
375 struct stw_pixelformat_info)) {
376 return NULL;
377 }
378
379 return util_dynarray_element(&stw_dev->pixelformats,
380 struct stw_pixelformat_info,
381 index);
382 }
383
384 /**
385 * Return the stw pixel format that most closely matches the pixel format
386 * on HDC.
387 * Used to get a pixel format when SetPixelFormat() hasn't been called before.
388 */
389 int
stw_pixelformat_guess(HDC hdc)390 stw_pixelformat_guess(HDC hdc)
391 {
392 int iPixelFormat = GetPixelFormat(hdc);
393 PIXELFORMATDESCRIPTOR pfd;
394
395 if (!iPixelFormat)
396 return 0;
397 if (!DescribePixelFormat(hdc, iPixelFormat, sizeof(pfd), &pfd))
398 return 0;
399 return stw_pixelformat_choose(hdc, &pfd);
400 }
401
402 const struct stw_pixelformat_info *
stw_pixelformat_get_info_from_hdc(HDC hdc)403 stw_pixelformat_get_info_from_hdc(HDC hdc)
404 {
405 /*
406 * GDI only knows about displayable pixel formats, so determine the pixel
407 * format from the framebuffer.
408 *
409 * This also allows to use a OpenGL DLL / ICD without installing.
410 */
411 struct stw_framebuffer *fb;
412 fb = stw_framebuffer_from_hdc(hdc);
413 if (fb) {
414 const struct stw_pixelformat_info *pfi = fb->pfi;
415 stw_framebuffer_unlock(fb);
416 return pfi;
417 }
418
419 /* Applications should call SetPixelFormat before creating a context,
420 * but not all do, and the opengl32 runtime seems to use a default
421 * pixel format in some cases, so use that.
422 */
423 int iPixelFormat = stw_pixelformat_guess(hdc);
424 if (!iPixelFormat)
425 return 0;
426
427 return stw_pixelformat_get_info( iPixelFormat );
428 }
429
430
431 LONG APIENTRY
DrvDescribePixelFormat(HDC hdc,INT iPixelFormat,ULONG cjpfd,PIXELFORMATDESCRIPTOR * ppfd)432 DrvDescribePixelFormat(HDC hdc, INT iPixelFormat, ULONG cjpfd,
433 PIXELFORMATDESCRIPTOR *ppfd)
434 {
435 uint count;
436 const struct stw_pixelformat_info *pfi;
437
438 if (!stw_dev)
439 return 0;
440
441 count = stw_pixelformat_get_count(hdc);
442
443 if (ppfd == NULL)
444 return count;
445
446 if (cjpfd != sizeof(PIXELFORMATDESCRIPTOR))
447 return 0;
448
449 pfi = stw_pixelformat_get_info(iPixelFormat);
450 if (!pfi) {
451 return 0;
452 }
453
454 memcpy(ppfd, &pfi->pfd, sizeof(PIXELFORMATDESCRIPTOR));
455
456 return count;
457 }
458
459
460 BOOL APIENTRY
DrvDescribeLayerPlane(HDC hdc,INT iPixelFormat,INT iLayerPlane,UINT nBytes,LPLAYERPLANEDESCRIPTOR plpd)461 DrvDescribeLayerPlane(HDC hdc, INT iPixelFormat, INT iLayerPlane,
462 UINT nBytes, LPLAYERPLANEDESCRIPTOR plpd)
463 {
464 assert(0);
465 return false;
466 }
467
468
469 int APIENTRY
DrvGetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,COLORREF * pcr)470 DrvGetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
471 INT cEntries, COLORREF *pcr)
472 {
473 assert(0);
474 return 0;
475 }
476
477
478 int APIENTRY
DrvSetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,CONST COLORREF * pcr)479 DrvSetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
480 INT cEntries, CONST COLORREF *pcr)
481 {
482 assert(0);
483 return 0;
484 }
485
486
487 BOOL APIENTRY
DrvRealizeLayerPalette(HDC hdc,INT iLayerPlane,BOOL bRealize)488 DrvRealizeLayerPalette(HDC hdc, INT iLayerPlane, BOOL bRealize)
489 {
490 assert(0);
491 return false;
492 }
493
494
495 /* Only used by the wgl code, but have it here to avoid exporting the
496 * pixelformat.h functionality.
497 */
498 int
stw_pixelformat_choose(HDC hdc,CONST PIXELFORMATDESCRIPTOR * ppfd)499 stw_pixelformat_choose(HDC hdc, CONST PIXELFORMATDESCRIPTOR *ppfd)
500 {
501 uint count;
502 uint index;
503 uint bestindex;
504 uint bestdelta;
505
506 count = stw_pixelformat_get_extended_count(hdc);
507 bestindex = 0;
508 bestdelta = ~0U;
509
510 for (index = 1; index <= count; index++) {
511 uint delta = 0;
512 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info(index);
513
514 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
515 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
516 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
517 continue;
518
519 /* Selection logic:
520 * - Enabling a feature (depth, stencil...) is given highest priority.
521 * - Giving as many bits as requested is given medium priority.
522 * - Giving no more bits than requested is given lowest priority.
523 */
524
525 if (ppfd->cColorBits && !pfi->pfd.cColorBits)
526 delta += 10000;
527 else if (ppfd->cColorBits > pfi->pfd.cColorBits)
528 delta += 100;
529 else if (ppfd->cColorBits < pfi->pfd.cColorBits)
530 delta++;
531
532 if (ppfd->cDepthBits && !pfi->pfd.cDepthBits)
533 delta += 10000;
534 else if (ppfd->cDepthBits > pfi->pfd.cDepthBits)
535 delta += 200;
536 else if (ppfd->cDepthBits < pfi->pfd.cDepthBits)
537 delta += 2;
538
539 if (ppfd->cStencilBits && !pfi->pfd.cStencilBits)
540 delta += 10000;
541 else if (ppfd->cStencilBits > pfi->pfd.cStencilBits)
542 delta += 400;
543 else if (ppfd->cStencilBits < pfi->pfd.cStencilBits)
544 delta++;
545
546 if (ppfd->cAlphaBits && !pfi->pfd.cAlphaBits)
547 delta += 10000;
548 else if (ppfd->cAlphaBits > pfi->pfd.cAlphaBits)
549 delta += 100;
550 else if (ppfd->cAlphaBits < pfi->pfd.cAlphaBits)
551 delta++;
552
553 if (ppfd->cRedBits && !pfi->pfd.cRedBits)
554 delta += 10000;
555 else if (ppfd->cRedBits > pfi->pfd.cRedBits)
556 delta += 100;
557 else if (ppfd->cRedBits < pfi->pfd.cRedBits)
558 delta++;
559
560 if (ppfd->cGreenBits && !pfi->pfd.cGreenBits)
561 delta += 10000;
562 else if (ppfd->cGreenBits > pfi->pfd.cGreenBits)
563 delta += 100;
564 else if (ppfd->cGreenBits < pfi->pfd.cGreenBits)
565 delta++;
566
567 if (ppfd->cBlueBits && !pfi->pfd.cBlueBits)
568 delta += 10000;
569 else if (ppfd->cBlueBits > pfi->pfd.cBlueBits)
570 delta += 100;
571 else if (ppfd->cBlueBits < pfi->pfd.cBlueBits)
572 delta++;
573
574 if (delta < bestdelta) {
575 bestindex = index;
576 bestdelta = delta;
577 if (bestdelta == 0)
578 break;
579 }
580 }
581
582 return bestindex;
583 }
584