Move pf_get_component_bits() to u_format auxiliary module.
[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_format.h"
36 #include "util/u_debug.h"
37
38 #include "stw_icd.h"
39 #include "stw_device.h"
40 #include "stw_pixelformat.h"
41 #include "stw_tls.h"
42
43
44 struct stw_pf_color_info
45 {
46 enum pipe_format format;
47 struct {
48 unsigned char red;
49 unsigned char green;
50 unsigned char blue;
51 unsigned char alpha;
52 } bits;
53 struct {
54 unsigned char red;
55 unsigned char green;
56 unsigned char blue;
57 unsigned char alpha;
58 } shift;
59 };
60
61 struct stw_pf_depth_info
62 {
63 enum pipe_format format;
64 struct {
65 unsigned char depth;
66 unsigned char stencil;
67 } bits;
68 };
69
70
71 /* NOTE: order matters, since in otherwise equal circumstances the first
72 * format listed will get chosen */
73
74 static const struct stw_pf_color_info
75 stw_pf_color[] = {
76 /* no-alpha */
77 { PIPE_FORMAT_X8R8G8B8_UNORM, { 8, 8, 8, 0}, {16, 8, 0, 0} },
78 { PIPE_FORMAT_B8G8R8X8_UNORM, { 8, 8, 8, 0}, { 8, 16, 24, 0} },
79 { PIPE_FORMAT_R5G6B5_UNORM, { 5, 6, 5, 0}, {11, 5, 0, 0} },
80 /* alpha */
81 { PIPE_FORMAT_A8R8G8B8_UNORM, { 8, 8, 8, 8}, {16, 8, 0, 24} },
82 { PIPE_FORMAT_B8G8R8A8_UNORM, { 8, 8, 8, 8}, { 8, 16, 24, 0} },
83 #if 0
84 { PIPE_FORMAT_A2B10G10R10_UNORM, {10, 10, 10, 2}, { 0, 10, 20, 30} },
85 #endif
86 { PIPE_FORMAT_A1R5G5B5_UNORM, { 5, 5, 5, 1}, {10, 5, 0, 15} },
87 { PIPE_FORMAT_A4R4G4B4_UNORM, { 4, 4, 4, 4}, {16, 4, 0, 12} }
88 };
89
90
91 static const struct stw_pf_depth_info
92 stw_pf_depth_stencil[] = {
93 /* pure depth */
94 { PIPE_FORMAT_Z32_UNORM, {32, 0} },
95 { PIPE_FORMAT_Z24X8_UNORM, {24, 0} },
96 { PIPE_FORMAT_X8Z24_UNORM, {24, 0} },
97 { PIPE_FORMAT_Z16_UNORM, {16, 0} },
98 /* pure stencil */
99 { PIPE_FORMAT_S8_UNORM, { 0, 8} },
100 /* combined depth-stencil */
101 { PIPE_FORMAT_S8Z24_UNORM, {24, 8} },
102 { PIPE_FORMAT_Z24S8_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 };
118
119
120 static void
121 stw_pixelformat_add(
122 struct stw_device *stw_dev,
123 const struct stw_pf_color_info *color,
124 const struct stw_pf_depth_info *depth,
125 unsigned accum,
126 boolean doublebuffer,
127 unsigned samples )
128 {
129 boolean extended = FALSE;
130 struct stw_pixelformat_info *pfi;
131
132 assert(stw_dev->pixelformat_extended_count < STW_MAX_PIXELFORMATS);
133 if(stw_dev->pixelformat_extended_count >= STW_MAX_PIXELFORMATS)
134 return;
135
136 assert(pf_layout( color->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
137 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 0) == color->bits.red);
138 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 1) == color->bits.green);
139 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 2) == color->bits.blue);
140 assert(util_format_get_component_bits(color->format, UTIL_FORMAT_COLORSPACE_RGB, 3) == color->bits.alpha);
141 assert(pf_layout( depth->format ) == PIPE_FORMAT_LAYOUT_RGBAZS );
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->color_format = color->format;
150 pfi->depth_stencil_format = depth->format;
151
152 pfi->pfd.nSize = sizeof pfi->pfd;
153 pfi->pfd.nVersion = 1;
154
155 pfi->pfd.dwFlags = PFD_SUPPORT_OPENGL;
156
157 /* TODO: also support non-native pixel formats */
158 pfi->pfd.dwFlags |= PFD_DRAW_TO_WINDOW;
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_COPY;
165
166 pfi->pfd.iPixelType = PFD_TYPE_RGBA;
167
168 pfi->pfd.cColorBits = color->bits.red + color->bits.green + color->bits.blue + color->bits.alpha;
169 pfi->pfd.cRedBits = color->bits.red;
170 pfi->pfd.cRedShift = color->shift.red;
171 pfi->pfd.cGreenBits = color->bits.green;
172 pfi->pfd.cGreenShift = color->shift.green;
173 pfi->pfd.cBlueBits = color->bits.blue;
174 pfi->pfd.cBlueShift = color->shift.blue;
175 pfi->pfd.cAlphaBits = color->bits.alpha;
176 pfi->pfd.cAlphaShift = color->shift.alpha;
177 pfi->pfd.cAccumBits = 4*accum;
178 pfi->pfd.cAccumRedBits = accum;
179 pfi->pfd.cAccumGreenBits = accum;
180 pfi->pfd.cAccumBlueBits = accum;
181 pfi->pfd.cAccumAlphaBits = accum;
182 pfi->pfd.cDepthBits = depth->bits.depth;
183 pfi->pfd.cStencilBits = depth->bits.stencil;
184 pfi->pfd.cAuxBuffers = 0;
185 pfi->pfd.iLayerType = 0;
186 pfi->pfd.bReserved = 0;
187 pfi->pfd.dwLayerMask = 0;
188 pfi->pfd.dwVisibleMask = 0;
189 pfi->pfd.dwDamageMask = 0;
190
191 if(samples) {
192 pfi->numSampleBuffers = 1;
193 pfi->numSamples = samples;
194 extended = TRUE;
195 }
196
197 ++stw_dev->pixelformat_extended_count;
198
199 if(!extended) {
200 ++stw_dev->pixelformat_count;
201 assert(stw_dev->pixelformat_count == stw_dev->pixelformat_extended_count);
202 }
203 }
204
205 void
206 stw_pixelformat_init( void )
207 {
208 struct pipe_screen *screen = stw_dev->screen;
209 unsigned i, j, k, l;
210
211 assert( !stw_dev->pixelformat_count );
212 assert( !stw_dev->pixelformat_extended_count );
213
214 for(i = 0; i < Elements(stw_pf_multisample); ++i) {
215 unsigned samples = stw_pf_multisample[i];
216
217 /* FIXME: re-enabled MSAA when we can query it */
218 if(samples)
219 continue;
220
221 for(j = 0; j < Elements(stw_pf_color); ++j) {
222 const struct stw_pf_color_info *color = &stw_pf_color[j];
223
224 if(!screen->is_format_supported(screen, color->format, PIPE_TEXTURE_2D,
225 PIPE_TEXTURE_USAGE_RENDER_TARGET, 0))
226 continue;
227
228 for(k = 0; k < Elements(stw_pf_doublebuffer); ++k) {
229 unsigned doublebuffer = stw_pf_doublebuffer[k];
230
231 for(l = 0; l < Elements(stw_pf_depth_stencil); ++l) {
232 const struct stw_pf_depth_info *depth = &stw_pf_depth_stencil[l];
233
234 if(!screen->is_format_supported(screen, depth->format, PIPE_TEXTURE_2D,
235 PIPE_TEXTURE_USAGE_DEPTH_STENCIL, 0))
236 continue;
237
238 stw_pixelformat_add( stw_dev, color, depth, 0, doublebuffer, samples );
239 stw_pixelformat_add( stw_dev, color, depth, 16, doublebuffer, samples );
240 }
241 }
242 }
243 }
244
245 assert( stw_dev->pixelformat_count <= stw_dev->pixelformat_extended_count );
246 assert( stw_dev->pixelformat_extended_count <= STW_MAX_PIXELFORMATS );
247 }
248
249 uint
250 stw_pixelformat_get_count( void )
251 {
252 return stw_dev->pixelformat_count;
253 }
254
255 uint
256 stw_pixelformat_get_extended_count( void )
257 {
258 return stw_dev->pixelformat_extended_count;
259 }
260
261 const struct stw_pixelformat_info *
262 stw_pixelformat_get_info( uint index )
263 {
264 assert( index < stw_dev->pixelformat_extended_count );
265
266 return &stw_dev->pixelformats[index];
267 }
268
269
270 void
271 stw_pixelformat_visual(GLvisual *visual,
272 const struct stw_pixelformat_info *pfi )
273 {
274 memset(visual, 0, sizeof *visual);
275 _mesa_initialize_visual(
276 visual,
277 (pfi->pfd.iPixelType == PFD_TYPE_RGBA) ? GL_TRUE : GL_FALSE,
278 (pfi->pfd.dwFlags & PFD_DOUBLEBUFFER) ? GL_TRUE : GL_FALSE,
279 (pfi->pfd.dwFlags & PFD_STEREO) ? GL_TRUE : GL_FALSE,
280 pfi->pfd.cRedBits,
281 pfi->pfd.cGreenBits,
282 pfi->pfd.cBlueBits,
283 pfi->pfd.cAlphaBits,
284 (pfi->pfd.iPixelType == PFD_TYPE_COLORINDEX) ? pfi->pfd.cColorBits : 0,
285 pfi->pfd.cDepthBits,
286 pfi->pfd.cStencilBits,
287 pfi->pfd.cAccumRedBits,
288 pfi->pfd.cAccumGreenBits,
289 pfi->pfd.cAccumBlueBits,
290 pfi->pfd.cAccumAlphaBits,
291 pfi->numSamples );
292 }
293
294
295 LONG APIENTRY
296 DrvDescribePixelFormat(
297 HDC hdc,
298 INT iPixelFormat,
299 ULONG cjpfd,
300 PIXELFORMATDESCRIPTOR *ppfd )
301 {
302 uint count;
303 uint index;
304 const struct stw_pixelformat_info *pfi;
305
306 (void) hdc;
307
308 count = stw_pixelformat_get_extended_count();
309 index = (uint) iPixelFormat - 1;
310
311 if (ppfd == NULL)
312 return count;
313 if (index >= count || cjpfd != sizeof( PIXELFORMATDESCRIPTOR ))
314 return 0;
315
316 pfi = stw_pixelformat_get_info( index );
317
318 memcpy(ppfd, &pfi->pfd, sizeof( PIXELFORMATDESCRIPTOR ));
319
320 return count;
321 }
322
323 BOOL APIENTRY
324 DrvDescribeLayerPlane(
325 HDC hdc,
326 INT iPixelFormat,
327 INT iLayerPlane,
328 UINT nBytes,
329 LPLAYERPLANEDESCRIPTOR plpd )
330 {
331 assert(0);
332 return FALSE;
333 }
334
335 int APIENTRY
336 DrvGetLayerPaletteEntries(
337 HDC hdc,
338 INT iLayerPlane,
339 INT iStart,
340 INT cEntries,
341 COLORREF *pcr )
342 {
343 assert(0);
344 return 0;
345 }
346
347 int APIENTRY
348 DrvSetLayerPaletteEntries(
349 HDC hdc,
350 INT iLayerPlane,
351 INT iStart,
352 INT cEntries,
353 CONST COLORREF *pcr )
354 {
355 assert(0);
356 return 0;
357 }
358
359 BOOL APIENTRY
360 DrvRealizeLayerPalette(
361 HDC hdc,
362 INT iLayerPlane,
363 BOOL bRealize )
364 {
365 assert(0);
366 return FALSE;
367 }
368
369 /* Only used by the wgl code, but have it here to avoid exporting the
370 * pixelformat.h functionality.
371 */
372 int stw_pixelformat_choose( HDC hdc,
373 CONST PIXELFORMATDESCRIPTOR *ppfd )
374 {
375 uint count;
376 uint index;
377 uint bestindex;
378 uint bestdelta;
379
380 (void) hdc;
381
382 count = stw_pixelformat_get_count();
383 bestindex = count;
384 bestdelta = ~0U;
385
386 for (index = 0; index < count; index++) {
387 uint delta = 0;
388 const struct stw_pixelformat_info *pfi = stw_pixelformat_get_info( index );
389
390 if (!(ppfd->dwFlags & PFD_DOUBLEBUFFER_DONTCARE) &&
391 !!(ppfd->dwFlags & PFD_DOUBLEBUFFER) !=
392 !!(pfi->pfd.dwFlags & PFD_DOUBLEBUFFER))
393 continue;
394
395 /* FIXME: Take in account individual channel bits */
396 if (ppfd->cColorBits != pfi->pfd.cColorBits)
397 delta += 8;
398
399 if (ppfd->cDepthBits != pfi->pfd.cDepthBits)
400 delta += 4;
401
402 if (ppfd->cStencilBits != pfi->pfd.cStencilBits)
403 delta += 2;
404
405 if (ppfd->cAlphaBits != pfi->pfd.cAlphaBits)
406 delta++;
407
408 if (delta < bestdelta) {
409 bestindex = index;
410 bestdelta = delta;
411 if (bestdelta == 0)
412 break;
413 }
414 }
415
416 if (bestindex == count)
417 return 0;
418
419 return bestindex + 1;
420 }