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 "pipe/p_format.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 "gldrv.h"
38 #include "stw_device.h"
39 #include "stw_pixelformat.h"
40 #include "stw_tls.h"
41
42
43 struct stw_pf_color_info
44 {
45 enum pipe_format format;
46 struct {
47 unsigned char red;
48 unsigned char green;
49 unsigned char blue;
50 unsigned char alpha;
51 } bits;
52 struct {
53 unsigned char red;
54 unsigned char green;
55 unsigned char blue;
56 unsigned char alpha;
57 } shift;
58 };
59
60 struct stw_pf_depth_info
61 {
62 enum pipe_format format;
63 struct {
64 unsigned char depth;
65 unsigned char stencil;
66 } bits;
67 };
68
69
70 /* NOTE: order matters, since in otherwise equal circumstances the first
71 * format listed will get chosen */
72
73 static const struct stw_pf_color_info
74 stw_pf_color[] = {
75 /* no-alpha */
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} },
78 /* alpha */
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} },
83 #if 0
84 { PIPE_FORMAT_R10G10B10A2_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
85 #endif
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 };
89
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} }
93 };
94
95 static const struct stw_pf_depth_info
96 stw_pf_depth_stencil[] = {
97 /* pure depth */
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} }
105 };
106
107
108 static const boolean
109 stw_pf_doublebuffer[] = {
110 FALSE,
111 TRUE,
112 };
113
114
115 const unsigned
116 stw_pf_multisample[] = {
117 0,
118 4,
119 8,
120 16
121 };
122
123
124 static void
stw_pixelformat_add(struct stw_device * stw_dev,boolean extended,const struct stw_pf_color_info * color,const struct stw_pf_depth_info * depth,unsigned accum,boolean doublebuffer,unsigned samples)125 stw_pixelformat_add(struct stw_device *stw_dev,
126 boolean extended,
127 const struct stw_pf_color_info *color,
128 const struct stw_pf_depth_info *depth,
129 unsigned accum,
130 boolean doublebuffer,
131 unsigned samples)
132 {
133 struct stw_pixelformat_info *pfi;
134
135 assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
136 if (stw_dev->pixelformat_extended_count >= STW_MAX_PIXELFORMATS)
137 return;
138
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);
145
146 pfi = &stw_dev->pixelformats[stw_dev->pixelformat_extended_count];
147
148 memset(pfi, 0, sizeof *pfi);
149
150 pfi->pfd.nSize = sizeof pfi->pfd;
151 pfi->pfd.nVersion = 1;
152
153 pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
154
155 /* TODO: also support non-native pixel formats */
156 if (!extended) {
157 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
158 }
159
160 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
161 pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
162
163 if (doublebuffer)
164 pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE;
165
166 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
167
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;
191
192 /*
193 * since gallium frontend can allocate depth/stencil/accum buffers, we provide
194 * only color buffers here
195 */
196 pfi->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
197 if (doublebuffer)
198 pfi->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
199
200 pfi->stvis.color_format = color->format;
201 pfi->stvis.depth_stencil_format = depth->format;
202
203 pfi->stvis.accum_format = (accum) ?
204 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
205
206 pfi->stvis.samples = samples;
207 pfi->stvis.render_buffer = ST_ATTACHMENT_INVALID;
208
209 /* WGL_ARB_render_texture */
210 if (color->bits.alpha)
211 pfi->bindToTextureRGBA = TRUE;
212
213 pfi->bindToTextureRGB = TRUE;
214
215 ++stw_dev->pixelformat_extended_count;
216
217 if (!extended) {
218 ++stw_dev->pixelformat_count;
219 assert(stw_dev->pixelformat_count == stw_dev->pixelformat_extended_count);
220 }
221 }
222
223
224 /**
225 * Add the depth/stencil/accum/ms variants for a list of color formats.
226 */
227 static unsigned
add_color_format_variants(const struct stw_pf_color_info * color_formats,unsigned num_color_formats,boolean extended)228 add_color_format_variants(const struct stw_pf_color_info *color_formats,
229 unsigned num_color_formats, boolean extended)
230 {
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;
236
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.
239 */
240 {
241 const char *samples= getenv("SVGA_FORCE_MSAA");
242 if (samples)
243 force_samples = atoi(samples);
244 }
245
246 if (!extended) {
247 bind_flags |= PIPE_BIND_DISPLAY_TARGET;
248 }
249
250 for (ms = 0; ms < ARRAY_SIZE(stw_pf_multisample); ms++) {
251 unsigned samples = stw_pf_multisample[ms];
252
253 if (force_samples && samples != force_samples)
254 continue;
255
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,
259 bind_flags)) {
260 continue;
261 }
262
263 for (db = 0; db < ARRAY_SIZE(stw_pf_doublebuffer); db++) {
264 unsigned doublebuffer = stw_pf_doublebuffer[db];
265
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];
268
269 if (!screen->is_format_supported(screen, depth->format,
270 PIPE_TEXTURE_2D, samples,
271 samples,
272 PIPE_BIND_DEPTH_STENCIL)) {
273 continue;
274 }
275
276 for (acc = 0; acc < 2; acc++) {
277 stw_pixelformat_add(stw_dev, extended, &color_formats[cfmt],
278 depth,
279 acc * 16, doublebuffer, samples);
280 num_added++;
281 }
282 }
283 }
284 }
285 }
286
287 return num_added;
288 }
289
290
291 void
stw_pixelformat_init(void)292 stw_pixelformat_init(void)
293 {
294 unsigned num_formats;
295
296 assert(!stw_dev->pixelformat_count);
297 assert(!stw_dev->pixelformat_extended_count);
298
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);
303
304 /* extended, pbuffer-only formats */
305 add_color_format_variants(stw_pf_color_extended,
306 ARRAY_SIZE(stw_pf_color_extended), TRUE);
307
308 assert(stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count);
309 assert(stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS);
310 }
311
312
313 uint
stw_pixelformat_get_count(void)314 stw_pixelformat_get_count(void)
315 {
316 if (!stw_init_screen())
317 return 0;
318
319 return stw_dev->pixelformat_count;
320 }
321
322
323 uint
stw_pixelformat_get_extended_count(void)324 stw_pixelformat_get_extended_count(void)
325 {
326 if (!stw_init_screen())
327 return 0;
328
329 return stw_dev->pixelformat_extended_count;
330 }
331
332
333 const struct stw_pixelformat_info *
stw_pixelformat_get_info(int iPixelFormat)334 stw_pixelformat_get_info(int iPixelFormat)
335 {
336 unsigned index;
337
338 if (iPixelFormat <= 0) {
339 return NULL;
340 }
341
342 index = iPixelFormat - 1;
343 if (index >= stw_dev->pixelformat_extended_count) {
344 return NULL;
345 }
346
347 return &stw_dev->pixelformats[index];
348 }
349
350
351 LONG APIENTRY
DrvDescribePixelFormat(HDC hdc,INT iPixelFormat,ULONG cjpfd,PIXELFORMATDESCRIPTOR * ppfd)352 DrvDescribePixelFormat(HDC hdc, INT iPixelFormat, ULONG cjpfd,
353 PIXELFORMATDESCRIPTOR *ppfd)
354 {
355 uint count;
356 const struct stw_pixelformat_info *pfi;
357
358 (void) hdc;
359
360 if (!stw_dev)
361 return 0;
362
363 count = stw_pixelformat_get_count();
364
365 if (ppfd == NULL)
366 return count;
367
368 if (cjpfd != sizeof(PIXELFORMATDESCRIPTOR))
369 return 0;
370
371 pfi = stw_pixelformat_get_info(iPixelFormat);
372 if (!pfi) {
373 return 0;
374 }
375
376 memcpy(ppfd, &pfi->pfd, sizeof(PIXELFORMATDESCRIPTOR));
377
378 return count;
379 }
380
381
382 BOOL APIENTRY
DrvDescribeLayerPlane(HDC hdc,INT iPixelFormat,INT iLayerPlane,UINT nBytes,LPLAYERPLANEDESCRIPTOR plpd)383 DrvDescribeLayerPlane(HDC hdc, INT iPixelFormat, INT iLayerPlane,
384 UINT nBytes, LPLAYERPLANEDESCRIPTOR plpd)
385 {
386 assert(0);
387 return FALSE;
388 }
389
390
391 int APIENTRY
DrvGetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,COLORREF * pcr)392 DrvGetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
393 INT cEntries, COLORREF *pcr)
394 {
395 assert(0);
396 return 0;
397 }
398
399
400 int APIENTRY
DrvSetLayerPaletteEntries(HDC hdc,INT iLayerPlane,INT iStart,INT cEntries,CONST COLORREF * pcr)401 DrvSetLayerPaletteEntries(HDC hdc, INT iLayerPlane, INT iStart,
402 INT cEntries, CONST COLORREF *pcr)
403 {
404 assert(0);
405 return 0;
406 }
407
408
409 BOOL APIENTRY
DrvRealizeLayerPalette(HDC hdc,INT iLayerPlane,BOOL bRealize)410 DrvRealizeLayerPalette(HDC hdc, INT iLayerPlane, BOOL bRealize)
411 {
412 assert(0);
413 return FALSE;
414 }
415
416
417 /* Only used by the wgl code, but have it here to avoid exporting the
418 * pixelformat.h functionality.
419 */
420 int
stw_pixelformat_choose(HDC hdc,CONST PIXELFORMATDESCRIPTOR * ppfd)421 stw_pixelformat_choose(HDC hdc, CONST PIXELFORMATDESCRIPTOR *ppfd)
422 {
423 uint count;
424 uint index;
425 uint bestindex;
426 uint bestdelta;
427
428 (void) hdc;
429
430 count = stw_pixelformat_get_extended_count();
431 bestindex = 0;
432 bestdelta = ~0U;
433
434 for (index = 1; index <= count; index++) {
435 uint delta = 0;
436 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info(index);
437
438 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
439 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
440 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
441 continue;
442
443 /* Selection logic:
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.
447 */
448
449 /* FIXME: Take in account individual channel bits */
450 if (ppfd->cColorBits && !pfi->pfd.cColorBits)
451 delta += 10000;
452 else if (ppfd->cColorBits > pfi->pfd.cColorBits)
453 delta += 100;
454 else if (ppfd->cColorBits < pfi->pfd.cColorBits)
455 delta++;
456
457 if (ppfd->cDepthBits && !pfi->pfd.cDepthBits)
458 delta += 10000;
459 else if (ppfd->cDepthBits > pfi->pfd.cDepthBits)
460 delta += 200;
461 else if (ppfd->cDepthBits < pfi->pfd.cDepthBits)
462 delta += 2;
463
464 if (ppfd->cStencilBits && !pfi->pfd.cStencilBits)
465 delta += 10000;
466 else if (ppfd->cStencilBits > pfi->pfd.cStencilBits)
467 delta += 400;
468 else if (ppfd->cStencilBits < pfi->pfd.cStencilBits)
469 delta++;
470
471 if (ppfd->cAlphaBits && !pfi->pfd.cAlphaBits)
472 delta += 10000;
473 else if (ppfd->cAlphaBits > pfi->pfd.cAlphaBits)
474 delta += 100;
475 else if (ppfd->cAlphaBits < pfi->pfd.cAlphaBits)
476 delta++;
477
478 if (delta < bestdelta) {
479 bestindex = index;
480 bestdelta = delta;
481 if (bestdelta == 0)
482 break;
483 }
484 }
485
486 return bestindex;
487 }
488