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