c13d89cd711c77ed1b180fdf83d0b12698d4ca86
[mesa.git] / src / mesa / drivers / d3d / D3DUTILS.CPP
1 /*===========================================================================*/
2 /* */
3 /* Mesa-3.0 DirectX 6 Driver */
4 /* */
5 /* By Leigh McRae */
6 /* */
7 /* http://www.altsoftware.com/ */
8 /* */
9 /* Copyright (c) 1999-1998 alt.software inc. All Rights Reserved */
10 /*===========================================================================*/
11 #include "D3DHAL.h"
12 /*===========================================================================*/
13 /* Local only functions. */
14 /*===========================================================================*/
15 static int CountTrailingZeros( DWORD dwMask );
16 /*===========================================================================*/
17 /* This function is used to get the pointer to the surface and the pitch for*/
18 /* the scanline rendering functions. */
19 /*===========================================================================*/
20 /* RETURN: */
21 /*===========================================================================*/
22 extern "C" DDSURFACEDESC2 *LockHAL( PMESAD3DSHARED pShared, BOOL bBack )
23 {
24 PMESAD3DHAL pHAL = (PMESAD3DHAL)pShared;
25 static DDSURFACEDESC2 ddsd2;
26 HRESULT rc;
27
28 DPF(( DBG_FUNC, "LockHAL();" ));
29
30 /* Set the request structure up first. */
31 memset( &ddsd2, 0, sizeof(DDSURFACEDESC2) );
32 ddsd2.dwSize = sizeof(DDSURFACEDESC2);
33
34 /* Make sure we have enough info. */
35 if ( pHAL )
36 {
37 rc = pHAL->lpDDSRender->Lock( NULL, &ddsd2, DDLOCK_WAIT, NULL );
38 if ( FAILED(rc) )
39 {
40 RIP( pHAL, "Lock (RENDER) ->", ErrorStringD3D(rc) );
41 }
42 }
43
44 return &ddsd2;
45 }
46 /*===========================================================================*/
47 /* This is just a simple wrapper. I probably don't need to do any error */
48 /* checking as the Lock must have worked inorder to get here... */
49 /*===========================================================================*/
50 /* RETURN: */
51 /*===========================================================================*/
52 extern "C" void UnlockHAL( PMESAD3DSHARED pShared, BOOL bBack )
53 {
54 PMESAD3DHAL pHAL = (PMESAD3DHAL)pShared;
55 HRESULT rc;
56
57 DPF(( DBG_FUNC, "UnlockHAL();" ));
58
59 /* Make sure we have enough info. */
60 if ( pHAL )
61 {
62 rc = pHAL->lpDDSRender->Unlock( NULL );
63 if ( FAILED(rc) )
64 {
65 RIP( pHAL, "Unlock (RENDER) ->", ErrorStringD3D(rc) );
66 }
67 }
68 }
69 /*===========================================================================*/
70 /* This function will track the main/Primary window that will be used as the*/
71 /* target for the Blt in SwapBuffers. As a side effect the call will check */
72 /* to see if the primary surface is the same size and position as the screen.*/
73 /* If they are the same size we will call it fullscreen... */
74 /*===========================================================================*/
75 /* RETURN: */
76 /*===========================================================================*/
77 extern "C" void UpdateScreenPosHAL( PMESAD3DSHARED pShared )
78 {
79 PMESAD3DHAL pHAL = (PMESAD3DHAL)pShared;
80 POINT pt;
81 DWORD dwWidth, dwHeight;
82
83 DPF(( DBG_FUNC, "UpdateScreenPosHAL();" ));
84
85 /* Make sure we have enough info. */
86 if ( pHAL != NULL )
87 {
88 /* Update the windows screen position. */
89 GetClientRect( pShared->hwnd, &pShared->rectW );
90 pt.x = pt.y = 0;
91 ClientToScreen( pShared->hwnd, &pt );
92 OffsetRect( &pShared->rectW, pt.x, pt.y);
93
94 /* Compare the primary to the screen. */
95 dwWidth = GetSystemMetrics( SM_CXSCREEN );
96 dwHeight = GetSystemMetrics( SM_CYSCREEN );
97 if ( (pShared->rectW.left > 0) || (pShared->rectW.top > 0) ||
98 (pShared->rectW.right > dwWidth) || (pShared->rectW.bottom > dwHeight) )
99 pShared->bWindow = TRUE;
100 else
101 pShared->bWindow = FALSE;
102 }
103 }
104 /*===========================================================================*/
105 /* This function will fill in the pixel info structure defined in D3Dshared.*/
106 /* Basicly it will take a DirectDraw pixelformat structure and make scaling */
107 /* values that will convert from 8bit channels to whatever the supplied ddpf */
108 /* uses. Also we will generate shift values that will be used to get move */
109 /* each component of the pixel into place. */
110 /* I have now added a special case for a 1bit alpha channel. If I find a 1b*/
111 /* alpha then I will set the scale to -1.0 which should be unique. Later I */
112 /* can check the alpha scale value too see if its -1.0 and thus handle it. I*/
113 /* was finding that the case was not working tom my advantage so this is my */
114 /* HACK for the day. As a TODO I should work on this... */
115 /*===========================================================================*/
116 /* RETURN: */
117 /*===========================================================================*/
118 void Solve8BitChannelPixelFormat( DDPIXELFORMAT *pddpf, PPIXELINFO pPixel )
119 {
120 DPF(( DBG_FUNC, "Solve8BitChannelPixelFromat();" ));
121
122 memset( pPixel, 0, sizeof(PPIXELINFO) );
123
124 /* Check too see if the color space is valid in the PF. */
125 if ( pddpf->dwFlags & DDPF_RGB )
126 {
127 /* Solve the red stuff. */
128 pPixel->dwRMask = pddpf->dwRBitMask;
129 pPixel->rShift = CountTrailingZeros( pPixel->dwRMask );
130 pPixel->rScale = (float)0.00392156 * (float)(pPixel->dwRMask >> pPixel->rShift);
131
132 /* Solve the green thingy's. */
133 pPixel->dwGMask = pddpf->dwGBitMask;
134 pPixel->gShift = CountTrailingZeros( pPixel->dwGMask );
135 pPixel->gScale = (float)0.00392156 * (float)(pPixel->dwGMask >> pPixel->gShift);
136
137 /* Solve the blues. */
138 pPixel->dwBMask = pddpf->dwBBitMask;
139 pPixel->bShift = CountTrailingZeros( pddpf->dwBBitMask );
140 pPixel->bScale = (float)0.00392156 * (float)(pddpf->dwBBitMask >> pPixel->bShift);
141 }
142
143 /* Do the alpha channel if there is one. */
144 if ( pddpf->dwFlags & DDPF_ALPHAPIXELS )
145 {
146 pPixel->dwAMask = pddpf->dwRGBAlphaBitMask;
147 pPixel->aShift = CountTrailingZeros( pPixel->dwAMask );
148
149 /* Special case a 1bit alpha. */
150 if ( (pPixel->dwAMask >> pPixel->aShift) == 1 )
151 pPixel->aScale = -1.0;
152 else
153 pPixel->aScale = (float)0.00392156 * (float)(pPixel->dwAMask >> pPixel->aShift);
154 }
155
156 /* Get the size of the pixel in bytes. Should work as dwRGBBitCount is in a union. */
157 pPixel->cb = pddpf->dwRGBBitCount / 8;
158 }
159 /*===========================================================================*/
160 /* See RETURN :) */
161 /*===========================================================================*/
162 /* RETURN: number of contiguous zeros starting from the right. */
163 /*===========================================================================*/
164 static int CountTrailingZeros( DWORD dwMask )
165 {
166 DWORD Mask;
167
168 if ( dwMask == 0 )
169 return 32;
170
171 /* Can't take credit for this one! */
172 Mask = dwMask & -(int)dwMask;
173 return ((Mask & 0xFFFF0000)!=0) << 4
174 | ((Mask & 0xFF00FF00)!=0) << 3
175 | ((Mask & 0xF0F0F0F0)!=0) << 2
176 | ((Mask & 0xCCCCCCCC)!=0) << 1
177 | ((Mask & 0xAAAAAAAA)!=0);
178 }
179 /*===========================================================================*/
180 /* This function will convert the DDraw error code to its macro string. The*/
181 /* returned pointer is static so you need not worry about memory managemnet */
182 /* but the error message gets written over from call to call... */
183 /*===========================================================================*/
184 /* RETURN: pointer to the single static buffer that hold the error message. */
185 /*===========================================================================*/
186 char *ErrorStringD3D( HRESULT hr )
187 {
188 static char errorString[128];
189
190 switch( hr )
191 {
192 case DDERR_ALREADYINITIALIZED:
193 strcpy( errorString, "DDERR_ALREADYINITIALIZED" );
194 break;
195
196 case DDERR_CANNOTATTACHSURFACE:
197 strcpy( errorString, "DDERR_CANNOTATTACHSURFACE" );
198 break;
199
200 case DDERR_CANNOTDETACHSURFACE:
201 strcpy( errorString, "DDERR_CANNOTDETACHSURFACE" );
202 break;
203
204 case DDERR_CURRENTLYNOTAVAIL:
205 strcpy( errorString, "DDERR_CURRENTLYNOTAVAIL" );
206 break;
207
208 case DDERR_EXCEPTION:
209 strcpy( errorString, "DDERR_EXCEPTION" );
210 break;
211
212 case DDERR_GENERIC:
213 strcpy( errorString, "DDERR_GENERIC" );
214 break;
215
216 case DDERR_HEIGHTALIGN:
217 strcpy( errorString, "DDERR_HEIGHTALIGN" );
218 break;
219
220 case DDERR_INCOMPATIBLEPRIMARY:
221 strcpy( errorString, "DDERR_INCOMPATIBLEPRIMARY" );
222 break;
223
224 case DDERR_INVALIDCAPS:
225 strcpy( errorString, "DDERR_INVALIDCAPS" );
226 break;
227
228 case DDERR_INVALIDCLIPLIST:
229 strcpy( errorString, "DDERR_INVALIDCLIPLIST" );
230 break;
231
232 case DDERR_INVALIDMODE:
233 strcpy( errorString, "DDERR_INVALIDMODE" );
234 break;
235
236 case DDERR_INVALIDOBJECT:
237 strcpy( errorString, "DDERR_INVALIDOBJECT" );
238 break;
239
240 case DDERR_INVALIDPARAMS:
241 strcpy( errorString, "DDERR_INVALIDPARAMS" );
242 break;
243
244 case DDERR_INVALIDPIXELFORMAT:
245 strcpy( errorString, "DDERR_INVALIDPIXELFORMAT" );
246 break;
247
248 case DDERR_INVALIDRECT:
249 strcpy( errorString, "DDERR_INVALIDRECT" );
250 break;
251
252 case DDERR_LOCKEDSURFACES:
253 strcpy( errorString, "DDERR_LOCKEDSURFACES" );
254 break;
255
256 case DDERR_NO3D:
257 strcpy( errorString, "DDERR_NO3D" );
258 break;
259
260 case DDERR_NOALPHAHW:
261 strcpy( errorString, "DDERR_NOALPHAHW" );
262 break;
263
264 case DDERR_NOCLIPLIST:
265 strcpy( errorString, "DDERR_NOCLIPLIST" );
266 break;
267
268 case DDERR_NOCOLORCONVHW:
269 strcpy( errorString, "DDERR_NOCOLORCONVHW" );
270 break;
271
272 case DDERR_NOCOOPERATIVELEVELSET:
273 strcpy( errorString, "DDERR_NOCOOPERATIVELEVELSET" );
274 break;
275
276 case DDERR_NOCOLORKEY:
277 strcpy( errorString, "DDERR_NOCOLORKEY" );
278 break;
279
280 case DDERR_NOCOLORKEYHW:
281 strcpy( errorString, "DDERR_NOCOLORKEYHW" );
282 break;
283
284 case DDERR_NODIRECTDRAWSUPPORT:
285 strcpy( errorString, "DDERR_NODIRECTDRAWSUPPORT" );
286 break;
287
288 case DDERR_NOEXCLUSIVEMODE:
289 strcpy( errorString, "DDERR_NOEXCLUSIVEMODE" );
290 break;
291
292 case DDERR_NOFLIPHW:
293 strcpy( errorString, "DDERR_NOFLIPHW" );
294 break;
295
296 case DDERR_NOGDI:
297 strcpy( errorString, "DDERR_NOGDI" );
298 break;
299
300 case DDERR_NOMIRRORHW:
301 strcpy( errorString, "DDERR_NOMIRRORHW" );
302 break;
303
304 case DDERR_NOTFOUND:
305 strcpy( errorString, "DDERR_NOTFOUND" );
306 break;
307
308 case DDERR_NOOVERLAYHW:
309 strcpy( errorString, "DDERR_NOOVERLAYHW" );
310 break;
311
312 case DDERR_OVERLAPPINGRECTS:
313 strcpy( errorString, "DDERR_OVERLAPPINGRECTS" );
314 break;
315
316 case DDERR_NORASTEROPHW:
317 strcpy( errorString, "DDERR_NORASTEROPHW" );
318 break;
319
320 case DDERR_NOROTATIONHW:
321 strcpy( errorString, "DDERR_NOROTATIONHW" );
322 break;
323
324 case DDERR_NOSTRETCHHW:
325 strcpy( errorString, "DDERR_NOSTRETCHHW" );
326 break;
327
328 case DDERR_NOT4BITCOLOR:
329 strcpy( errorString, "DDERR_NOT4BITCOLOR" );
330 break;
331
332 case DDERR_NOT4BITCOLORINDEX:
333 strcpy( errorString, "DDERR_NOT4BITCOLORINDEX" );
334 break;
335
336 case DDERR_NOT8BITCOLOR:
337 strcpy( errorString, "DDERR_NOT8BITCOLOR" );
338 break;
339
340 case DDERR_NOTEXTUREHW:
341 strcpy( errorString, "DDERR_NOTEXTUREHW" );
342 break;
343
344 case DDERR_NOVSYNCHW:
345 strcpy( errorString, "DDERR_NOVSYNCHW" );
346 break;
347
348 case DDERR_NOZBUFFERHW:
349 strcpy( errorString, "DDERR_NOZBUFFERHW" );
350 break;
351
352 case DDERR_NOZOVERLAYHW:
353 strcpy( errorString, "DDERR_NOZOVERLAYHW" );
354 break;
355
356 case DDERR_OUTOFCAPS:
357 strcpy( errorString, "DDERR_OUTOFCAPS" );
358 break;
359
360 case DDERR_OUTOFMEMORY:
361 strcpy( errorString, "DDERR_OUTOFMEMORY" );
362 break;
363
364 case DDERR_OUTOFVIDEOMEMORY:
365 strcpy( errorString, "DDERR_OUTOFVIDEOMEMORY" );
366 break;
367
368 case DDERR_OVERLAYCANTCLIP:
369 strcpy( errorString, "DDERR_OVERLAYCANTCLIP" );
370 break;
371
372 case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
373 strcpy( errorString, "DDERR_OVERLAYCOLORKEYONLYONEACTIVE" );
374 break;
375
376 case DDERR_PALETTEBUSY:
377 strcpy( errorString, "DDERR_PALETTEBUSY" );
378 break;
379
380 case DDERR_COLORKEYNOTSET:
381 strcpy( errorString, "DDERR_COLORKEYNOTSET" );
382 break;
383
384 case DDERR_SURFACEALREADYATTACHED:
385 strcpy( errorString, "DDERR_SURFACEALREADYATTACHED" );
386 break;
387
388 case DDERR_SURFACEALREADYDEPENDENT:
389 strcpy( errorString, "DDERR_SURFACEALREADYDEPENDENT" );
390 break;
391
392 case DDERR_SURFACEBUSY:
393 strcpy( errorString, "DDERR_SURFACEBUSY" );
394 break;
395
396 case DDERR_CANTLOCKSURFACE:
397 strcpy( errorString, "DDERR_CANTLOCKSURFACE" );
398 break;
399
400 case DDERR_SURFACEISOBSCURED:
401 strcpy( errorString, "DDERR_SURFACEISOBSCURED" );
402 break;
403
404 case DDERR_SURFACELOST:
405 strcpy( errorString, "DDERR_SURFACELOST" );
406 break;
407
408 case DDERR_SURFACENOTATTACHED:
409 strcpy( errorString, "DDERR_SURFACENOTATTACHED" );
410 break;
411
412 case DDERR_TOOBIGHEIGHT:
413 strcpy( errorString, "DDERR_TOOBIGHEIGHT" );
414 break;
415
416 case DDERR_TOOBIGSIZE:
417 strcpy( errorString, "DDERR_TOOBIGSIZE" );
418 break;
419
420 case DDERR_TOOBIGWIDTH:
421 strcpy( errorString, "DDERR_TOOBIGWIDTH" );
422 break;
423
424 case DDERR_UNSUPPORTED:
425 strcpy( errorString, "DDERR_UNSUPPORTED" );
426 break;
427
428 case DDERR_UNSUPPORTEDFORMAT:
429 strcpy( errorString, "DDERR_UNSUPPORTEDFORMAT" );
430 break;
431
432 case DDERR_UNSUPPORTEDMASK:
433 strcpy( errorString, "DDERR_UNSUPPORTEDMASK" );
434 break;
435
436 case DDERR_INVALIDSTREAM:
437 strcpy( errorString, "DDERR_INVALIDSTREAM" );
438 break;
439
440 case DDERR_VERTICALBLANKINPROGRESS:
441 strcpy( errorString, "DDERR_VERTICALBLANKINPROGRESS" );
442 break;
443
444 case DDERR_WASSTILLDRAWING:
445 strcpy( errorString, "DDERR_WASSTILLDRAWING" );
446 break;
447
448 case DDERR_XALIGN:
449 strcpy( errorString, "DDERR_XALIGN" );
450 break;
451
452 case DDERR_INVALIDDIRECTDRAWGUID:
453 strcpy( errorString, "DDERR_INVALIDDIRECTDRAWGUID" );
454 break;
455
456 case DDERR_DIRECTDRAWALREADYCREATED:
457 strcpy( errorString, "DDERR_DIRECTDRAWALREADYCREATED" );
458 break;
459
460 case DDERR_NODIRECTDRAWHW:
461 strcpy( errorString, "DDERR_NODIRECTDRAWHW" );
462 break;
463
464 case DDERR_PRIMARYSURFACEALREADYEXISTS:
465 strcpy( errorString, "DDERR_PRIMARYSURFACEALREADYEXISTS" );
466 break;
467
468 case DDERR_NOEMULATION:
469 strcpy( errorString, "DDERR_NOEMULATION" );
470 break;
471
472 case DDERR_REGIONTOOSMALL:
473 strcpy( errorString, "DDERR_REGIONTOOSMALL" );
474 break;
475
476 case DDERR_CLIPPERISUSINGHWND:
477 strcpy( errorString, "DDERR_CLIPPERISUSINGHWND" );
478 break;
479
480 case DDERR_NOCLIPPERATTACHED:
481 strcpy( errorString, "DDERR_NOCLIPPERATTACHED" );
482 break;
483
484 case DDERR_NOHWND:
485 strcpy( errorString, "DDERR_NOHWND" );
486 break;
487
488 case DDERR_HWNDSUBCLASSED:
489 strcpy( errorString, "DDERR_HWNDSUBCLASSED" );
490 break;
491
492 case DDERR_HWNDALREADYSET:
493 strcpy( errorString, "DDERR_HWNDALREADYSET" );
494 break;
495
496 case DDERR_NOPALETTEATTACHED:
497 strcpy( errorString, "DDERR_NOPALETTEATTACHED" );
498 break;
499
500 case DDERR_NOPALETTEHW:
501 strcpy( errorString, "DDERR_NOPALETTEHW" );
502 break;
503
504 case DDERR_BLTFASTCANTCLIP:
505 strcpy( errorString, "DDERR_BLTFASTCANTCLIP" );
506 break;
507
508 case DDERR_NOBLTHW:
509 strcpy( errorString, "DDERR_NOBLTHW" );
510 break;
511
512 case DDERR_NODDROPSHW:
513 strcpy( errorString, "DDERR_NODDROPSHW" );
514 break;
515
516 case DDERR_OVERLAYNOTVISIBLE:
517 strcpy( errorString, "DDERR_OVERLAYNOTVISIBLE" );
518 break;
519
520 case DDERR_NOOVERLAYDEST:
521 strcpy( errorString, "DDERR_NOOVERLAYDEST" );
522 break;
523
524 case DDERR_INVALIDPOSITION:
525 strcpy( errorString, "DDERR_INVALIDPOSITION" );
526 break;
527
528 case DDERR_NOTAOVERLAYSURFACE:
529 strcpy( errorString, "DDERR_NOTAOVERLAYSURFACE" );
530 break;
531
532 case DDERR_EXCLUSIVEMODEALREADYSET:
533 strcpy( errorString, "DDERR_EXCLUSIVEMODEALREADYSET" );
534 break;
535
536 case DDERR_NOTFLIPPABLE:
537 strcpy( errorString, "DDERR_NOTFLIPPABLE" );
538 break;
539
540 case DDERR_CANTDUPLICATE:
541 strcpy( errorString, "DDERR_CANTDUPLICATE" );
542 break;
543
544 case DDERR_NOTLOCKED:
545 strcpy( errorString, "DDERR_NOTLOCKED" );
546 break;
547
548 case DDERR_CANTCREATEDC:
549 strcpy( errorString, "DDERR_CANTCREATEDC" );
550 break;
551
552 case DDERR_NODC:
553 strcpy( errorString, "DDERR_NODC" );
554 break;
555
556 case DDERR_WRONGMODE:
557 strcpy( errorString, "DDERR_WRONGMODE" );
558 break;
559
560 case DDERR_IMPLICITLYCREATED:
561 strcpy( errorString, "DDERR_IMPLICITLYCREATED" );
562 break;
563
564 case DDERR_NOTPALETTIZED:
565 strcpy( errorString, "DDERR_NOTPALETTIZED" );
566 break;
567
568 case DDERR_UNSUPPORTEDMODE:
569 strcpy( errorString, "DDERR_UNSUPPORTEDMODE" );
570 break;
571
572 case DDERR_NOMIPMAPHW:
573 strcpy( errorString, "DDERR_NOMIPMAPHW" );
574 break;
575
576 case DDERR_INVALIDSURFACETYPE:
577 strcpy( errorString, "DDERR_INVALIDSURFACETYPE" );
578 break;
579
580 case DDERR_NOOPTIMIZEHW:
581 strcpy( errorString, "DDERR_NOOPTIMIZEHW" );
582 break;
583
584 case DDERR_NOTLOADED:
585 strcpy( errorString, "DDERR_NOTLOADED" );
586 break;
587
588 case DDERR_NOFOCUSWINDOW:
589 strcpy( errorString, "DDERR_NOFOCUSWINDOW" );
590 break;
591
592 case DDERR_DCALREADYCREATED:
593 strcpy( errorString, "DDERR_DCALREADYCREATED" );
594 break;
595
596 case DDERR_NONONLOCALVIDMEM:
597 strcpy( errorString, "DDERR_NONONLOCALVIDMEM" );
598 break;
599
600 case DDERR_CANTPAGELOCK:
601 strcpy( errorString, "DDERR_CANTPAGELOCK" );
602 break;
603
604 case DDERR_CANTPAGEUNLOCK:
605 strcpy( errorString, "DDERR_CANTPAGEUNLOCK" );
606 break;
607
608 case DDERR_NOTPAGELOCKED:
609 strcpy( errorString, "DDERR_NOTPAGELOCKED" );
610 break;
611
612 case DDERR_MOREDATA:
613 strcpy( errorString, "DDERR_MOREDATA" );
614 break;
615
616 case DDERR_EXPIRED:
617 strcpy( errorString, "DDERR_EXPIRED" );
618 break;
619
620 case DDERR_VIDEONOTACTIVE:
621 strcpy( errorString, "DDERR_VIDEONOTACTIVE" );
622 break;
623
624 case DDERR_DEVICEDOESNTOWNSURFACE:
625 strcpy( errorString, "DDERR_DEVICEDOESNTOWNSURFACE" );
626 break;
627
628 case DDERR_NOTINITIALIZED:
629 strcpy( errorString, "DDERR_NOTINITIALIZED" );
630 break;
631
632 default:
633 strcpy( errorString, "<unknown error code>" );
634 break;
635 }
636
637 return &errorString[0];
638 }