wgl: Check for PIPE_TEXTURE_USAGE_DISPLAY_TARGET support in exported color pixel...
[mesa.git] / src / gallium / state_trackers / wgl / stw_pixelformat.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 "main/mtypes.h"
29 #include "main/context.h"
30
31 #include "pipe/p_format.h"
32 #include "pipe/p_defines.h"
33 #include "pipe/p_screen.h"
34
35 #include "util/u_debug.h"
36
37 #include "stw_icd.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_X8R8G8B8_UNORM, { 8, 8, 8, 0}, {16, 8, 0, 0} },
77 { PIPE_FORMAT_B8G8R8X8_UNORM, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
78 { PIPE_FORMAT_R5G6B5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 0} },
79 /* alpha */
80 { PIPE_FORMAT_A8R8G8B8_UNORM, { 8, 8, 8, 8}, {16, 8, 0, 24} },
81 { PIPE_FORMAT_B8G8R8A8_UNORM, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
82 #if 0
83 { PIPE_FORMAT_A2B10G10R10_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
84 #endif
85 { PIPE_FORMAT_A1R5G5B5_UNORM, { 5, 5, 5, 1}, {10, 5, 0, 15} },
86 { PIPE_FORMAT_A4R4G4B4_UNORM, { 4, 4, 4, 4}, {16, 4, 0, 12} }
87 };
88
89
90 static const struct stw_pf_depth_info
91 stw_pf_depth_stencil[] = {
92 /* pure depth */
93 { PIPE_FORMAT_Z32_UNORM, {32, 0} },
94 { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
95 { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
96 { PIPE_FORMAT_Z16_UNORM, {16, 0} },
97 /* combined depth-stencil */
98 { PIPE_FORMAT_S8Z24_UNORM, {24, 8} },
99 { PIPE_FORMAT_Z24S8_UNORM, {24, 8} }
100 };
101
102
103 static const boolean
104 stw_pf_doublebuffer[] = {
105 FALSE,
106 TRUE,
107 };
108
109
110 const unsigned
111 stw_pf_multisample[] = {
112 0,
113 4
114 };
115
116
117 static void
118 stw_pixelformat_add(
119 struct stw_device *stw_dev,
120 const struct stw_pf_color_info *color,
121 const struct stw_pf_depth_info *depth,
122 unsigned accum,
123 boolean doublebuffer,
124 unsigned samples )
125 {
126 boolean extended = FALSE;
127 struct stw_pixelformat_info *pfi;
128
129 assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
130 if(stw_dev->pixelformat_extended_count >= STW_MAX_PIXELFORMATS)
131 return;
132
133 assert(pf_layout( color->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
134 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_R ) == color->bits.red );
135 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_G ) == color->bits.green );
136 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_B ) == color->bits.blue );
137 assert(pf_get_component_bits( color->format, PIPE_FORMAT_COMP_A ) == color->bits.alpha );
138 assert(pf_layout( depth->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
139 assert(pf_get_component_bits( depth->format, PIPE_FORMAT_COMP_Z ) == depth->bits.depth );
140 assert(pf_get_component_bits( depth->format, PIPE_FORMAT_COMP_S ) == depth->bits.stencil );
141
142 pfi = &stw_dev->pixelformats[stw_dev->pixelformat_extended_count];
143
144 memset(pfi, 0, sizeof *pfi);
145
146 pfi->color_format = color->format;
147 pfi->depth_stencil_format = depth->format;
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 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
156
157 /* See http://www.opengl.org/pipeline/article/vol003_7/ */
158 pfi->pfd.dwFlags |= PFD_SUPPORT_COMPOSITION;
159
160 if (doublebuffer)
161 pfi->pfd.dwFlags |= PFD_DOUBLEBUFFER | PFD_SWAP_COPY;
162
163 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
164
165 pfi->pfd.cColorBits = color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
166 pfi->pfd.cRedBits = color->bits.red;
167 pfi->pfd.cRedShift = color->shift.red;
168 pfi->pfd.cGreenBits = color->bits.green;
169 pfi->pfd.cGreenShift = color->shift.green;
170 pfi->pfd.cBlueBits = color->bits.blue;
171 pfi->pfd.cBlueShift = color->shift.blue;
172 pfi->pfd.cAlphaBits = color->bits.alpha;
173 pfi->pfd.cAlphaShift = color->shift.alpha;
174 pfi->pfd.cAccumBits = 4*accum;
175 pfi->pfd.cAccumRedBits = accum;
176 pfi->pfd.cAccumGreenBits = accum;
177 pfi->pfd.cAccumBlueBits = accum;
178 pfi->pfd.cAccumAlphaBits = accum;
179 pfi->pfd.cDepthBits = depth->bits.depth;
180 pfi->pfd.cStencilBits = depth->bits.stencil;
181 pfi->pfd.cAuxBuffers = 0;
182 pfi->pfd.iLayerType = 0;
183 pfi->pfd.bReserved = 0;
184 pfi->pfd.dwLayerMask = 0;
185 pfi->pfd.dwVisibleMask = 0;
186 pfi->pfd.dwDamageMask = 0;
187
188 if(samples) {
189 pfi->numSampleBuffers = 1;
190 pfi->numSamples = samples;
191 extended = TRUE;
192 }
193
194 ++stw_dev->pixelformat_extended_count;
195
196 if(!extended) {
197 ++stw_dev->pixelformat_count;
198 assert(stw_dev->pixelformat_count == stw_dev->pixelformat_extended_count);
199 }
200 }
201
202 void
203 stw_pixelformat_init( void )
204 {
205 struct pipe_screen *screen = stw_dev->screen;
206 unsigned i, j, k, l;
207
208 assert( !stw_dev->pixelformat_count );
209 assert( !stw_dev->pixelformat_extended_count );
210
211 for(i = 0; i < Elements(stw_pf_multisample); ++i) {
212 unsigned samples = stw_pf_multisample[i];
213
214 /* FIXME: re-enabled MSAA when we can query it */
215 if(samples)
216 continue;
217
218 for(j = 0; j < Elements(stw_pf_color); ++j) {
219 const struct stw_pf_color_info *color = &stw_pf_color[j];
220
221 if(!screen->is_format_supported(screen, color->format, PIPE_TEXTURE_2D,
222 PIPE_TEXTURE_USAGE_RENDER_TARGET |
223 PIPE_TEXTURE_USAGE_DISPLAY_TARGET, 0))
224 continue;
225
226 for(k = 0; k < Elements(stw_pf_doublebuffer); ++k) {
227 unsigned doublebuffer = stw_pf_doublebuffer[k];
228
229 for(l = 0; l < Elements(stw_pf_depth_stencil); ++l) {
230 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[l];
231
232 if(!screen->is_format_supported(screen, depth->format, PIPE_TEXTURE_2D,
233 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0))
234 continue;
235
236 stw_pixelformat_add( stw_dev, color, depth, 0, doublebuffer, samples );
237 stw_pixelformat_add( stw_dev, color, depth, 16, doublebuffer, samples );
238 }
239 }
240 }
241 }
242
243 assert( stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count );
244 assert( stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS );
245 }
246
247 uint
248 stw_pixelformat_get_count( void )
249 {
250 return stw_dev->pixelformat_count;
251 }
252
253 uint
254 stw_pixelformat_get_extended_count( void )
255 {
256 return stw_dev->pixelformat_extended_count;
257 }
258
259 const struct stw_pixelformat_info *
260 stw_pixelformat_get_info( uint index )
261 {
262 assert( index < stw_dev->pixelformat_extended_count );
263
264 return &stw_dev->pixelformats[index];
265 }
266
267
268 void
269 stw_pixelformat_visual(GLvisual *visual,
270 const struct stw_pixelformat_info *pfi )
271 {
272 memset(visual, 0, sizeof *visual);
273 _mesa_initialize_visual(
274 visual,
275 (pfi->pfd.iPixelType == PFD_TYPE_RGBA) ? GL_TRUE : GL_FALSE,
276 (pfi->pfd.dwFlags & PFD_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
277 (pfi->pfd.dwFlags & PFD_STEREO) ? GL_TRUE : GL_FALSE,
278 pfi->pfd.cRedBits,
279 pfi->pfd.cGreenBits,
280 pfi->pfd.cBlueBits,
281 pfi->pfd.cAlphaBits,
282 (pfi->pfd.iPixelType == PFD_TYPE_COLORINDEX) ? pfi->pfd.cColorBits : 0,
283 pfi->pfd.cDepthBits,
284 pfi->pfd.cStencilBits,
285 pfi->pfd.cAccumRedBits,
286 pfi->pfd.cAccumGreenBits,
287 pfi->pfd.cAccumBlueBits,
288 pfi->pfd.cAccumAlphaBits,
289 pfi->numSamples );
290 }
291
292
293 LONG APIENTRY
294 DrvDescribePixelFormat(
295 HDC hdc,
296 INT iPixelFormat,
297 ULONG cjpfd,
298 PIXELFORMATDESCRIPTOR *ppfd )
299 {
300 uint count;
301 uint index;
302 const struct stw_pixelformat_info *pfi;
303
304 (void) hdc;
305
306 count = stw_pixelformat_get_extended_count();
307 index = (uint) iPixelFormat - 1;
308
309 if (ppfd == NULL)
310 return count;
311 if (index >= count || cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
312 return 0;
313
314 pfi = stw_pixelformat_get_info( index );
315
316 memcpy(ppfd, &pfi->pfd, sizeof( PIXELFORMATDESCRIPTOR ));
317
318 return count;
319 }
320
321 BOOL APIENTRY
322 DrvDescribeLayerPlane(
323 HDC hdc,
324 INT iPixelFormat,
325 INT iLayerPlane,
326 UINT nBytes,
327 LPLAYERPLANEDESCRIPTOR plpd )
328 {
329 assert(0);
330 return FALSE;
331 }
332
333 int APIENTRY
334 DrvGetLayerPaletteEntries(
335 HDC hdc,
336 INT iLayerPlane,
337 INT iStart,
338 INT cEntries,
339 COLORREF *pcr )
340 {
341 assert(0);
342 return 0;
343 }
344
345 int APIENTRY
346 DrvSetLayerPaletteEntries(
347 HDC hdc,
348 INT iLayerPlane,
349 INT iStart,
350 INT cEntries,
351 CONST COLORREF *pcr )
352 {
353 assert(0);
354 return 0;
355 }
356
357 BOOL APIENTRY
358 DrvRealizeLayerPalette(
359 HDC hdc,
360 INT iLayerPlane,
361 BOOL bRealize )
362 {
363 assert(0);
364 return FALSE;
365 }
366
367 /* Only used by the wgl code, but have it here to avoid exporting the
368 * pixelformat.h functionality.
369 */
370 int stw_pixelformat_choose( HDC hdc,
371 CONST PIXELFORMATDESCRIPTOR *ppfd )
372 {
373 uint count;
374 uint index;
375 uint bestindex;
376 uint bestdelta;
377
378 (void) hdc;
379
380 count = stw_pixelformat_get_count();
381 bestindex = count;
382 bestdelta = ~0U;
383
384 for (index = 0; index < count; index++) {
385 uint delta = 0;
386 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info( index );
387
388 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
389 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
390 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
391 continue;
392
393 /* FIXME: Take in account individual channel bits */
394 if (ppfd->cColorBits != pfi->pfd.cColorBits)
395 delta += 8;
396
397 if (ppfd->cDepthBits != pfi->pfd.cDepthBits)
398 delta += 4;
399
400 if (ppfd->cStencilBits != pfi->pfd.cStencilBits)
401 delta += 2;
402
403 if (ppfd->cAlphaBits != pfi->pfd.cAlphaBits)
404 delta++;
405
406 if (delta < bestdelta) {
407 bestindex = index;
408 bestdelta = delta;
409 if (bestdelta == 0)
410 break;
411 }
412 }
413
414 if (bestindex == count)
415 return 0;
416
417 return bestindex + 1;
418 }