Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / gallium / state_trackers / wgl / stw_pixelformat.c
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/u_format.h"
33 #include "util/u_debug.h"
34 #include "util/u_memory.h"
35
36 #include "stw_icd.h"
37 #include "stw_device.h"
38 #include "stw_pixelformat.h"
39 #include "stw_tls.h"
40
41
42 struct stw_pf_color_info
43 {
44 enum pipe_format format;
45 struct {
46 unsigned char red;
47 unsigned char green;
48 unsigned char blue;
49 unsigned char alpha;
50 } bits;
51 struct {
52 unsigned char red;
53 unsigned char green;
54 unsigned char blue;
55 unsigned char alpha;
56 } shift;
57 };
58
59 struct stw_pf_depth_info
60 {
61 enum pipe_format format;
62 struct {
63 unsigned char depth;
64 unsigned char stencil;
65 } bits;
66 };
67
68
69 /* NOTE: order matters, since in otherwise equal circumstances the first
70 * format listed will get chosen */
71
72 static const struct stw_pf_color_info
73 stw_pf_color[] = {
74 /* no-alpha */
75 { PIPE_FORMAT_B8G8R8X8_UNORM, { 8, 8, 8, 0}, {16, 8, 0, 0} },
76 { PIPE_FORMAT_X8R8G8B8_UNORM, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
77 { PIPE_FORMAT_B5G6R5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 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 #if 0
82 { PIPE_FORMAT_R10G10B10A2_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
83 #endif
84 { PIPE_FORMAT_B5G5R5A1_UNORM, { 5, 5, 5, 1}, {10, 5, 0, 15} },
85 { PIPE_FORMAT_B4G4R4A4_UNORM, { 4, 4, 4, 4}, {16, 4, 0, 12} }
86 };
87
88 static const struct stw_pf_color_info
89 stw_pf_color_extended[] = {
90 { PIPE_FORMAT_R32G32B32A32_FLOAT, { 32, 32, 32, 32}, { 0, 32, 64, 96} }
91 };
92
93 static const struct stw_pf_depth_info
94 stw_pf_depth_stencil[] = {
95 /* pure depth */
96 { PIPE_FORMAT_Z32_UNORM, {32, 0} },
97 { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
98 { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
99 { PIPE_FORMAT_Z16_UNORM, {16, 0} },
100 /* combined depth-stencil */
101 { PIPE_FORMAT_Z24_UNORM_S8_UINT, {24, 8} },
102 { PIPE_FORMAT_S8_UINT_Z24_UNORM, {24, 8} }
103 };
104
105
106 static const boolean
107 stw_pf_doublebuffer[] = {
108 FALSE,
109 TRUE,
110 };
111
112
113 const unsigned
114 stw_pf_multisample[] = {
115 0,
116 4,
117 8,
118 16
119 };
120
121
122 static void
123 stw_pixelformat_add(
124 struct stw_device *stw_dev,
125 boolean extended,
126 const struct stw_pf_color_info *color,
127 const struct stw_pf_depth_info *depth,
128 unsigned accum,
129 boolean doublebuffer,
130 unsigned samples )
131 {
132 struct stw_pixelformat_info *pfi;
133
134 assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
135 if(stw_dev->pixelformat_extended_count >= STW_MAX_PIXELFORMATS)
136 return;
137
138 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 0) == color->bits.red);
139 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 1) == color->bits.green);
140 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 2) == color->bits.blue);
141 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 3) == color->bits.alpha);
142 assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 0) == depth->bits.depth);
143 assert(util_format_get_component_bits(depth->format, UTIL_FORMAT_COLORSPACE_ZS, 1) == depth->bits.stencil);
144
145 pfi = &stw_dev->pixelformats[stw_dev->pixelformat_extended_count];
146
147 memset(pfi, 0, sizeof *pfi);
148
149 pfi->pfd.nSize = sizeof pfi->pfd;
150 pfi->pfd.nVersion = 1;
151
152 pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
153
154 /* TODO: also support non-native pixel formats */
155 if (!extended) {
156 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
157 }
158
159 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
160 pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
161
162 if (doublebuffer)
163 pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_EXCHANGE;
164
165 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
166
167 pfi->pfd.cColorBits = color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
168 pfi->pfd.cRedBits = color->bits.red;
169 pfi->pfd.cRedShift = color->shift.red;
170 pfi->pfd.cGreenBits = color->bits.green;
171 pfi->pfd.cGreenShift = color->shift.green;
172 pfi->pfd.cBlueBits = color->bits.blue;
173 pfi->pfd.cBlueShift = color->shift.blue;
174 pfi->pfd.cAlphaBits = color->bits.alpha;
175 pfi->pfd.cAlphaShift = color->shift.alpha;
176 pfi->pfd.cAccumBits = 4*accum;
177 pfi->pfd.cAccumRedBits = accum;
178 pfi->pfd.cAccumGreenBits = accum;
179 pfi->pfd.cAccumBlueBits = accum;
180 pfi->pfd.cAccumAlphaBits = accum;
181 pfi->pfd.cDepthBits = depth->bits.depth;
182 pfi->pfd.cStencilBits = depth->bits.stencil;
183 pfi->pfd.cAuxBuffers = 0;
184 pfi->pfd.iLayerType = 0;
185 pfi->pfd.bReserved = 0;
186 pfi->pfd.dwLayerMask = 0;
187 pfi->pfd.dwVisibleMask = 0;
188 pfi->pfd.dwDamageMask = 0;
189
190 /*
191 * since state trackers can allocate depth/stencil/accum buffers, we provide
192 * only color buffers here
193 */
194 pfi->stvis.buffer_mask = ST_ATTACHMENT_FRONT_LEFT_MASK;
195 if (doublebuffer)
196 pfi->stvis.buffer_mask |= ST_ATTACHMENT_BACK_LEFT_MASK;
197
198 pfi->stvis.color_format = color->format;
199 pfi->stvis.depth_stencil_format = depth->format;
200
201 pfi->stvis.accum_format = (accum) ?
202 PIPE_FORMAT_R16G16B16A16_SNORM : PIPE_FORMAT_NONE;
203
204 pfi->stvis.samples = samples;
205 pfi->stvis.render_buffer = ST_ATTACHMENT_INVALID;
206
207 ++stw_dev->pixelformat_extended_count;
208
209 if(!extended) {
210 ++stw_dev->pixelformat_count;
211 assert(stw_dev->pixelformat_count == stw_dev->pixelformat_extended_count);
212 }
213 }
214
215
216 /**
217 * Add the depth/stencil/accum/ms variants for a particular color format.
218 */
219 static unsigned
220 add_color_format_variants(const struct stw_pf_color_info *color,
221 boolean extended)
222 {
223 struct pipe_screen *screen = stw_dev->screen;
224 unsigned ms, db, ds, acc;
225 unsigned bind_flags = PIPE_BIND_RENDER_TARGET;
226 unsigned num_added = 0;
227 int force_samples = 0;
228
229 /* Since GLUT for Windows doesn't support MSAA we have an env var
230 * to force all pixel formats to have a particular number of samples.
231 */
232 {
233 const char *samples= getenv("SVGA_FORCE_MSAA");
234 if (samples)
235 force_samples = atoi(samples);
236 }
237
238 if (!extended) {
239 bind_flags |= PIPE_BIND_DISPLAY_TARGET;
240 }
241
242 for (ms = 0; ms < Elements(stw_pf_multisample); ms++) {
243 unsigned samples = stw_pf_multisample[ms];
244
245 if (force_samples && samples != force_samples)
246 continue;
247
248 if (!screen->is_format_supported(screen, color->format,
249 PIPE_TEXTURE_2D, samples, bind_flags)) {
250 continue;
251 }
252
253 for (db = 0; db < Elements(stw_pf_doublebuffer); db++) {
254 unsigned doublebuffer = stw_pf_doublebuffer[db];
255
256 for (ds = 0; ds < Elements(stw_pf_depth_stencil); ds++) {
257 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[ds];
258
259 if (!screen->is_format_supported(screen, depth->format,
260 PIPE_TEXTURE_2D, samples,
261 PIPE_BIND_DEPTH_STENCIL)) {
262 continue;
263 }
264
265 for (acc = 0; acc < 2; acc++) {
266 stw_pixelformat_add(stw_dev, extended, color, depth,
267 acc * 16, doublebuffer, samples);
268 num_added++;
269 }
270 }
271 }
272 }
273
274 return num_added;
275 }
276
277
278 void
279 stw_pixelformat_init( void )
280 {
281 unsigned i;
282 unsigned num_formats = 0;
283
284 assert( !stw_dev->pixelformat_count );
285 assert( !stw_dev->pixelformat_extended_count );
286
287 /* normal, displayable formats */
288 for (i = 0; i < Elements(stw_pf_color); i++) {
289 num_formats += add_color_format_variants(&stw_pf_color[i], FALSE);
290 }
291 assert(num_formats > 0);
292
293 /* extended, pbuffer-only formats */
294 for (i = 0; i < Elements(stw_pf_color_extended); i++) {
295 add_color_format_variants(&stw_pf_color_extended[i], TRUE);
296 }
297
298 assert( stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count );
299 assert( stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS );
300 }
301
302 uint
303 stw_pixelformat_get_count( void )
304 {
305 return stw_dev->pixelformat_count;
306 }
307
308 uint
309 stw_pixelformat_get_extended_count( void )
310 {
311 return stw_dev->pixelformat_extended_count;
312 }
313
314 const struct stw_pixelformat_info *
315 stw_pixelformat_get_info( int iPixelFormat )
316 {
317 unsigned index;
318
319 if (iPixelFormat <= 0) {
320 return NULL;
321 }
322
323 index = iPixelFormat - 1;
324 if (index >= stw_dev->pixelformat_extended_count) {
325 return NULL;
326 }
327
328 return &stw_dev->pixelformats[index];
329 }
330
331
332 LONG APIENTRY
333 DrvDescribePixelFormat(
334 HDC hdc,
335 INT iPixelFormat,
336 ULONG cjpfd,
337 PIXELFORMATDESCRIPTOR *ppfd )
338 {
339 uint count;
340 const struct stw_pixelformat_info *pfi;
341
342 (void) hdc;
343
344 if (!stw_dev)
345 return 0;
346
347 count = stw_pixelformat_get_count();
348
349 if (ppfd == NULL)
350 return count;
351 if (cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
352 return 0;
353
354 pfi = stw_pixelformat_get_info( iPixelFormat );
355 if (!pfi) {
356 return 0;
357 }
358
359 memcpy(ppfd, &pfi->pfd, sizeof( PIXELFORMATDESCRIPTOR ));
360
361 return count;
362 }
363
364 BOOL APIENTRY
365 DrvDescribeLayerPlane(
366 HDC hdc,
367 INT iPixelFormat,
368 INT iLayerPlane,
369 UINT nBytes,
370 LPLAYERPLANEDESCRIPTOR plpd )
371 {
372 assert(0);
373 return FALSE;
374 }
375
376 int APIENTRY
377 DrvGetLayerPaletteEntries(
378 HDC hdc,
379 INT iLayerPlane,
380 INT iStart,
381 INT cEntries,
382 COLORREF *pcr )
383 {
384 assert(0);
385 return 0;
386 }
387
388 int APIENTRY
389 DrvSetLayerPaletteEntries(
390 HDC hdc,
391 INT iLayerPlane,
392 INT iStart,
393 INT cEntries,
394 CONST COLORREF *pcr )
395 {
396 assert(0);
397 return 0;
398 }
399
400 BOOL APIENTRY
401 DrvRealizeLayerPalette(
402 HDC hdc,
403 INT iLayerPlane,
404 BOOL bRealize )
405 {
406 assert(0);
407 return FALSE;
408 }
409
410 /* Only used by the wgl code, but have it here to avoid exporting the
411 * pixelformat.h functionality.
412 */
413 int stw_pixelformat_choose( HDC hdc,
414 CONST PIXELFORMATDESCRIPTOR *ppfd )
415 {
416 uint count;
417 uint index;
418 uint bestindex;
419 uint bestdelta;
420
421 (void) hdc;
422
423 count = stw_pixelformat_get_extended_count();
424 bestindex = 0;
425 bestdelta = ~0U;
426
427 for (index = 1; index <= count; index++) {
428 uint delta = 0;
429 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info( index );
430
431 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
432 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
433 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
434 continue;
435
436 /* FIXME: Take in account individual channel bits */
437 if (ppfd->cColorBits != pfi->pfd.cColorBits)
438 delta += 8;
439
440 if (ppfd->cDepthBits != pfi->pfd.cDepthBits)
441 delta += 4;
442
443 if (ppfd->cStencilBits != pfi->pfd.cStencilBits)
444 delta += 2;
445
446 if (ppfd->cAlphaBits != pfi->pfd.cAlphaBits)
447 delta++;
448
449 if (delta < bestdelta) {
450 bestindex = index;
451 bestdelta = delta;
452 if (bestdelta == 0)
453 break;
454 }
455 }
456
457 return bestindex;
458 }