2 * $Logfile: /Freespace2/code/Graphics/GrD3D.cpp $
7 * Code for our Direct3D renderer
10 * Revision 1.1 2002/05/03 03:28:09 root
14 * 42 10/13/99 3:49p Jefff
15 * fixed unnumbered XSTRs
17 * 41 9/13/99 11:25p Dave
18 * Fixed problem with mode-switching and D3D movies.
20 * 40 9/13/99 11:30a Dave
21 * Added checkboxes and functionality for disabling PXO banners as well as
22 * disabling d3d zbuffer biasing.
24 * 39 9/10/99 11:53a Dave
25 * Shutdown graphics before sound to eliminate apparent lockups when
26 * Directsound decides to be lame. Fix TOPMOST problem with D3D windows.
28 * 38 9/04/99 8:00p Dave
29 * Fixed up 1024 and 32 bit movie support.
31 * 37 8/30/99 5:01p Dave
32 * Made d3d do less state changing in the nebula. Use new chat server for
35 * 36 8/20/99 2:09p Dave
38 * 35 8/18/99 9:35a Dave
39 * Made d3d shutdown more stable.
41 * 34 8/11/99 3:30p Dave
42 * Fixed window focus problems.
44 * 33 8/04/99 5:36p Dave
45 * Make glide and D3D switch out properly.
47 * 32 8/02/99 6:25p Dave
48 * Fixed d3d screen save/popup problem.
50 * 31 7/30/99 7:01p Dave
51 * Dogfight escort gauge. Fixed up laser rendering in Glide.
53 * 30 7/29/99 10:47p Dave
54 * Standardized D3D fogging using vertex fog. Shook out Savage 4 bugs.
56 * 29 7/27/99 3:09p Dave
57 * Made g400 work. Whee.
59 * 28 7/24/99 4:19p Dave
60 * Fixed dumb code with briefing bitmaps. Made d3d zbuffer work much
61 * better. Made model code use zbuffer more intelligently.
63 * 27 7/16/99 1:49p Dave
64 * 8 bit aabitmaps. yay.
66 * 26 7/14/99 9:42a Dave
67 * Put in clear_color debug function. Put in base for 3dnow stuff / P3
70 * 25 7/13/99 1:15p Dave
71 * 32 bit support. Whee!
73 * 24 6/29/99 10:35a Dave
74 * Interface polygon bitmaps! Whee!
76 * 23 6/03/99 6:37p Dave
77 * More TNT fun. Made perspective bitmaps more flexible.
79 * 22 5/05/99 9:02p Dave
80 * Fixed D3D aabitmap rendering. Spiffed up nebula effect a bit (added
81 * rotations, tweaked values, made bitmap selection more random). Fixed
82 * D3D beam weapon clipping problem. Added D3d frame dumping.
84 * 21 2/03/99 11:44a Dave
85 * Fixed d3d transparent textures.
87 * 20 1/24/99 11:36p Dave
88 * First full rev of beam weapons. Very customizable. Removed some bogus
89 * Int3()'s in low level net code.
91 * 19 1/15/99 11:29a Neilk
92 * Fixed D3D screen/texture pixel formatting problem.
94 * 18 1/11/99 6:21p Neilk
95 * Fixed broken D3D card fog-capability check.
97 * 17 1/06/99 2:24p Dave
98 * Stubs and release build fixes.
100 * 16 12/18/98 1:13a Dave
101 * Rough 1024x768 support for Direct3D. Proper detection and usage through
104 * 15 12/09/98 7:34p Dave
105 * Cleanup up nebula effect. Tweaked many values.
107 * 14 12/08/98 7:30p Dave
108 * Fixed broken compile.
110 * 13 12/08/98 7:03p Dave
111 * Much improved D3D fogging. Also put in vertex fogging for the cheesiest
114 * 12 12/08/98 2:47p Johnson
115 * Made D3D fogging use eye-relative depth instead of z-depth. Works like
116 * Glide w-buffer now.
118 * 11 12/08/98 9:36a Dave
119 * Almost done nebula effect for D3D. Looks 85% as good as Glide.
121 * 10 12/07/98 5:51p Dave
122 * Finally got d3d fog working! Now we just need to tweak values.
124 * 9 12/06/98 6:53p Dave
126 * 8 12/06/98 3:08p Dave
127 * Fixed grx_tmapper to handle pixel fog flag. First run fog support for
130 * 7 12/06/98 2:36p Dave
131 * Drastically improved nebula fogging.
133 * 6 12/01/98 10:25a Johnson
134 * Fixed direct3d texture coord/font problems.
136 * 5 11/30/98 1:07p Dave
137 * 16 bit conversion, first run.
139 * 4 11/11/98 5:37p Dave
140 * Checkin for multiplayer testing.
142 * 3 10/09/98 2:57p Dave
143 * Starting splitting up OS stuff.
145 * 2 10/07/98 10:52a Dave
148 * 1 10/07/98 10:49a Dave
150 * 110 6/13/98 6:01p Hoffoss
151 * Externalized all new (or forgot to be added) strings to all the code.
153 * 109 6/13/98 3:18p Hoffoss
154 * NOX()ed out a bunch of strings that shouldn't be translated.
156 * 108 5/24/98 9:41p John
157 * changed allender's previous fix to actually not draw the lines on
160 * 107 5/24/98 9:16p Allender
161 * put in previously non-NDEBUG code to draw bogus cursor when Gr_cursor
162 * wasn't defined. Caused d3d to crash before playing movies
164 * 106 5/22/98 10:29p John
165 * fixed some mode switching and line offset detection bugs.
167 * 105 5/22/98 1:11p John
168 * Added code to actually detect which offset a line needs
176 #include "grd3dinternal.h"
182 #include "floating.h"
184 #include "osregistry.h"
188 #include "grinternal.h"
190 #include "alphacolors.h"
191 #include "systemvars.h"
196 LPDIRECTDRAW lpDD1 = NULL;
197 LPDIRECTDRAW2 lpDD = NULL;
198 LPDIRECT3D2 lpD3D = NULL;
199 LPDIRECT3DDEVICE2 lpD3DDevice = NULL;
200 // LPDIRECT3DDEVICE lpD3DDeviceEB = NULL;
201 LPDIRECTDRAWSURFACE lpBackBuffer = NULL;
202 LPDIRECTDRAWSURFACE lpFrontBuffer = NULL;
203 LPDIRECTDRAWSURFACE lpZBuffer = NULL;
205 LPDIRECT3DVIEWPORT2 lpViewport;
207 DDPIXELFORMAT AlphaTextureFormat;
208 int Largest_alpha = 0;
209 DDPIXELFORMAT NonAlphaTextureFormat;
210 DDPIXELFORMAT NonAlphaTextureFormat_1555;
211 DDPIXELFORMAT NonAlphaTextureFormat_565;
213 DDPIXELFORMAT ScreenFormat;
215 static RECT D3D_cursor_clip_rect;
217 D3DDEVICEDESC D3DHWDevDesc, D3DHELDevDesc;
218 LPD3DDEVICEDESC lpDevDesc = NULL;
220 DDCAPS DD_driver_caps;
223 int D3D_texture_divider = 1;
227 char Device_init_error[512] = "";
229 // -1 == no fog, bad bad bad
232 int D3D_fog_mode = -1;
234 static int In_frame = 0;
240 int D3d_rendition_uvs = 0;
247 D3D_zbias = !D3D_zbias;
250 #define MAX_D2D_DEVICES 8
251 #define MAX_D3D_DEVICES 16
253 typedef struct d3d_device {
263 d3d_device D2D_devices[MAX_D2D_DEVICES];
264 d3d_device D3D_devices[MAX_D3D_DEVICES];
266 int Num_d2d_devices = 0;
267 int Num_d3d_devices = 0;
269 d3d_device *D3D_device;
271 void mprint_guid( LPGUID lpGuid )
275 mprintf(( "None\n" ));
278 for (i=0; i<sizeof(GUID); i++ ) {
279 mprintf(( "%x ", *ptr++ ));
281 mprintf(( "\n", *ptr++ ));
286 #define PUTD3DINSTRUCTION(op, sz, cnt, ptr) do { \
287 ((LPD3DINSTRUCTION) ptr)->bOpcode = op; \
288 ((LPD3DINSTRUCTION) ptr)->bSize = sz; \
289 ((LPD3DINSTRUCTION) ptr)->wCount = cnt; \
290 ptr = (void *)(((LPD3DINSTRUCTION) ptr) + 1); } while (0)
292 #define VERTEX_DATA(loc, cnt, ptr) do { \
293 if ((ptr) != (loc)) memcpy((ptr), (loc), sizeof(D3DVERTEX) * (cnt)); \
294 ptr = (void *)(((LPD3DVERTEX) (ptr)) + (cnt)); } while (0)
296 // OP_MATRIX_MULTIPLY size: 4 (sizeof D3DINSTRUCTION)
297 #define OP_MATRIX_MULTIPLY(cnt, ptr) \
298 PUTD3DINSTRUCTION(D3DOP_MATRIXMULTIPLY, sizeof(D3DMATRIXMULTIPLY), cnt, ptr)
300 // MATRIX_MULTIPLY_DATA size: 12 (sizeof MATRIXMULTIPLY)
301 #define MATRIX_MULTIPLY_DATA(src1, src2, dest, ptr) do { \
302 ((LPD3DMATRIXMULTIPLY) ptr)->hSrcMatrix1 = src1; \
303 ((LPD3DMATRIXMULTIPLY) ptr)->hSrcMatrix2 = src2; \
304 ((LPD3DMATRIXMULTIPLY) ptr)->hDestMatrix = dest; \
305 ptr = (void *)(((LPD3DMATRIXMULTIPLY) ptr) + 1); } while (0)
307 // OP_STATE_LIGHT size: 4 (sizeof D3DINSTRUCTION)
308 #define OP_STATE_LIGHT(cnt, ptr) \
309 PUTD3DINSTRUCTION(D3DOP_STATELIGHT, sizeof(D3DSTATE), cnt, ptr)
311 // OP_STATE_TRANSFORM size: 4 (sizeof D3DINSTRUCTION)
312 #define OP_STATE_TRANSFORM(cnt, ptr) \
313 PUTD3DINSTRUCTION(D3DOP_STATETRANSFORM, sizeof(D3DSTATE), cnt, ptr)
315 // OP_STATE_RENDER size: 4 (sizeof D3DINSTRUCTION)
316 #define OP_STATE_RENDER(cnt, ptr) \
317 PUTD3DINSTRUCTION(D3DOP_STATERENDER, sizeof(D3DSTATE), cnt, ptr)
319 // STATE_DATA size: 8 (sizeof D3DSTATE)
320 #define STATE_DATA(type, arg, ptr) do { \
321 ((LPD3DSTATE) ptr)->drstRenderStateType = (D3DRENDERSTATETYPE)type; \
322 ((LPD3DSTATE) ptr)->dwArg[0] = arg; \
323 ptr = (void *)(((LPD3DSTATE) ptr) + 1); } while (0)
325 // OP_PROCESS_VERTICES size: 4 (sizeof D3DINSTRUCTION)
326 #define OP_PROCESS_VERTICES(cnt, ptr) \
327 PUTD3DINSTRUCTION(D3DOP_PROCESSVERTICES, sizeof(D3DPROCESSVERTICES), cnt, ptr)
329 // PROCESSVERTICES_DATA size: 16 (sizeof D3DPROCESSVERTICES)
330 #define PROCESSVERTICES_DATA(flgs, strt, cnt, ptr) do { \
331 ((LPD3DPROCESSVERTICES) ptr)->dwFlags = flgs; \
332 ((LPD3DPROCESSVERTICES) ptr)->wStart = strt; \
333 ((LPD3DPROCESSVERTICES) ptr)->wDest = strt; \
334 ((LPD3DPROCESSVERTICES) ptr)->dwCount = cnt; \
335 ((LPD3DPROCESSVERTICES) ptr)->dwReserved = 0; \
336 ptr = (void *)(((LPD3DPROCESSVERTICES) ptr) + 1); } while (0)
338 // OP_TRIANGLE_LIST size: 4 (sizeof D3DINSTRUCTION)
339 #define OP_TRIANGLE_LIST(cnt, ptr) \
340 PUTD3DINSTRUCTION(D3DOP_TRIANGLE, sizeof(D3DTRIANGLE), cnt, ptr)
342 #define TRIANGLE_LIST_DATA(loc, count, ptr) do { \
343 if ((ptr) != (loc)) memcpy((ptr), (loc), sizeof(D3DTRIANGLE) * (count)); \
344 ptr = (void *)(((LPD3DTRIANGLE) (ptr)) + (count)); } while (0)
346 // OP_LINE_LIST size: 4 (sizeof D3DINSTRUCTION)
347 #define OP_LINE_LIST(cnt, ptr) \
348 PUTD3DINSTRUCTION(D3DOP_LINE, sizeof(D3DLINE), cnt, ptr)
350 #define LINE_LIST_DATA(loc, count, ptr) do { \
351 if ((ptr) != (loc)) memcpy((ptr), (loc), sizeof(D3DLINE) * (count)); \
352 ptr = (void *)(((LPD3DLINE) (ptr)) + (count)); } while (0)
354 // OP_POINT_LIST size: 8 (sizeof D3DINSTRUCTION + sizeof D3DPOINT)
355 #define OP_POINT_LIST(first, cnt, ptr) do { \
356 PUTD3DINSTRUCTION(D3DOP_POINT, sizeof(D3DPOINT), 1, ptr); \
357 ((LPD3DPOINT)(ptr))->wCount = cnt; \
358 ((LPD3DPOINT)(ptr))->wFirst = first; \
359 ptr = (void*)(((LPD3DPOINT)(ptr)) + 1); } while(0)
361 // OP_SPAN_LIST size: 8 (sizeof D3DINSTRUCTION + sizeof D3DSPAN)
362 #define OP_SPAN_LIST(first, cnt, ptr) \
363 PUTD3DINSTRUCTION(D3DOP_SPAN, sizeof(D3DSPAN), 1, ptr); \
364 ((LPD3DSPAN)(ptr))->wCount = cnt; \
365 ((LPD3DSPAN)(ptr))->wFirst = first; \
366 ptr = (void*)(((LPD3DSPAN)(ptr)) + 1); } while(0)
368 // OP_BRANCH_FORWARD size: 18 (sizeof D3DINSTRUCTION + sizeof D3DBRANCH)
369 #define OP_BRANCH_FORWARD(tmask, tvalue, tnegate, toffset, ptr) \
370 PUTD3DINSTRUCTION(D3DOP_BRANCHFORWARD, sizeof(D3DBRANCH), 1, ptr); \
371 ((LPD3DBRANCH) ptr)->dwMask = tmask; \
372 ((LPD3DBRANCH) ptr)->dwValue = tvalue; \
373 ((LPD3DBRANCH) ptr)->bNegate = tnegate; \
374 ((LPD3DBRANCH) ptr)->dwOffset = toffset; \
375 ptr = (void *)(((LPD3DBRANCH) (ptr)) + 1); } while (0)
377 // OP_SET_STATUS size: 20 (sizeof D3DINSTRUCTION + sizeof D3DSTATUS)
378 #define OP_SET_STATUS(flags, status, _x1, _y1, _x2, _y2, ptr) \
379 PUTD3DINSTRUCTION(D3DOP_SETSTATUS, sizeof(D3DSTATUS), 1, ptr); \
380 ((LPD3DSTATUS)(ptr))->dwFlags = flags; \
381 ((LPD3DSTATUS)(ptr))->dwStatus = status; \
382 ((LPD3DSTATUS)(ptr))->drExtent.x1 = _x1; \
383 ((LPD3DSTATUS)(ptr))->drExtent.y1 = _y1; \
384 ((LPD3DSTATUS)(ptr))->drExtent.x2 = _x2; \
385 ((LPD3DSTATUS)(ptr))->drExtent.y2 = _y2; \
386 ptr = (void *)(((LPD3DSTATUS) (ptr)) + 1); } while (0)
389 #define OP_NOP(ptr) \
390 PUTD3DINSTRUCTION(D3DOP_TRIANGLE, sizeof(D3DTRIANGLE), 0, ptr)
392 #define OP_EXIT(ptr) \
393 PUTD3DINSTRUCTION(D3DOP_EXIT, 0, 0, ptr)
395 #define QWORD_ALIGNED(ptr) \
396 (!(0x00000007L & (ULONG)(ptr)))
398 #define D3D_MAX_VERTS 512
399 #define D3D_EXBUFFER_SIZE 65536
400 static int D3D_num_verts;
401 //static D3DTLVERTEX D3D_vertices[D3D_MAX_VERTS];
402 static D3DTLVERTEX *D3D_vertices;
403 //static ubyte D3D_exbuffer[D3D_EXBUFFER_SIZE];
404 static void *D3D_ex_ptr, *D3D_ex_end;
405 LPDIRECT3DEXECUTEBUFFER lpExBuf = NULL;
407 LPVOID lpBufStart, lpPointer, lpInsStart;
411 void gr_d3d_exb_init()
415 D3DEXECUTEBUFFERDESC debDesc;
421 Exb_size = D3D_EXBUFFER_SIZE + sizeof(D3DTLVERTEX)*D3D_MAX_VERTS;
423 // create a D3DEXECUTEBUFFERDESC
424 memset( &debDesc, 0, sizeof( debDesc ) );
425 debDesc.dwSize = sizeof( debDesc );
426 debDesc.dwFlags = D3DDEB_BUFSIZE;
427 debDesc.dwBufferSize = Exb_size;
430 ddrval = lpD3DDeviceEB->CreateExecuteBuffer( &debDesc, &lpExBuf, NULL );
431 if ( ddrval != DD_OK ) {
432 mprintf(( "GR_D3D_INIT: CreateExecuteBuffer failed. size=%d, '%s'\n", Exb_size, d3d_error_string(ddrval) ));
436 memset( &debDesc, 0, sizeof( debDesc ) );
437 debDesc.dwSize = sizeof( debDesc );
438 ddrval = lpExBuf->Lock( &debDesc );
439 if ( ddrval != DD_OK ) {
440 mprintf(( "Failed to lock the execute buffer!\n" ));
444 lpPointer = lpBufStart = lpInsStart = debDesc.lpData;
446 lpPointer = (void *)((uint)lpPointer+sizeof(D3DTLVERTEX)*D3D_MAX_VERTS);
447 lpInsStart = lpPointer;
449 OP_PROCESS_VERTICES( 1, lpPointer );
450 PROCESSVERTICES_DATA( D3DPROCESSVERTICES_COPY, 0, 1, lpPointer );
453 D3D_ex_ptr = lpPointer;
454 D3D_ex_end = (void *)((uint)lpBufStart + Exb_size - 1024);
455 D3D_vertices = (D3DTLVERTEX *)lpBufStart;
470 HRESULT set_wbuffer_planes(LPDIRECT3DDEVICE2 lpDev, float dvWNear, float dvWFar)
477 memset(&matWorld, 0, sizeof(matWorld));
478 memset(&matView, 0, sizeof(matWorld));
479 memset(&matProj, 0, sizeof(matWorld));
480 matWorld._11 = 1; matWorld._22 = 1; matWorld._33 = 1; matWorld._44 = 1;
481 matView._11 = 1; matView._22 = 1; matView._33 = 1; matView._44 = 1;
482 matProj._11 = 1; matProj._22 = 1; matProj._33 = 1; matProj._44 = 1;
484 res = lpDev->SetTransform( D3DTRANSFORMSTATE_WORLD, &matWorld );
486 res = lpDev->SetTransform( D3DTRANSFORMSTATE_VIEW, &matView );
491 matProj._44 = dvWNear; // not used
492 matProj._33 = dvWNear / (dvWFar - dvWNear) + 1;
494 res = lpDev->SetTransform( D3DTRANSFORMSTATE_PROJECTION, &matProj );
500 dc_get_arg(ARG_FLOAT);
501 float n = Dc_arg_float;
502 dc_get_arg(ARG_FLOAT);
503 float f = Dc_arg_float;
504 set_wbuffer_planes(lpD3DDevice, n, f);
507 void gr_d3d_exb_flush(int end_of_frame)
511 D3DEXECUTEBUFFERDESC debDesc;
512 D3DEXECUTEDATA d3dExData;
518 if (!lpExBuf) return;
520 OP_EXIT( D3D_ex_ptr );
522 lpPointer = lpInsStart;
523 OP_PROCESS_VERTICES( 1, lpPointer );
524 PROCESSVERTICES_DATA( D3DPROCESSVERTICES_COPY, 0, D3D_num_verts, lpPointer );
526 ddrval = lpExBuf->Unlock();
527 if (ddrval != DD_OK ) {
528 mprintf(( "Failed to unlock the execute buffer!\n" ));
532 memset(&d3dExData, 0, sizeof(D3DEXECUTEDATA));
533 d3dExData.dwSize = sizeof(D3DEXECUTEDATA);
534 d3dExData.dwVertexCount = D3D_num_verts;
535 d3dExData.dwInstructionOffset = (ULONG)((char*)lpInsStart - (char*)lpBufStart);
536 d3dExData.dwInstructionLength = (ULONG)((char*)D3D_ex_ptr - (char*)lpInsStart);
538 // if (end_of_frame==0) {
539 // mprintf(( "Flushing execute buffer in frame, %d verts, %d data size!\n", D3D_num_verts, d3dExData.dwInstructionLength ));
540 // } else if (end_of_frame==2) {
541 // mprintf(( "Flushing execute buffer in frame, because of VRAM flush!\n" ));
544 ddrval = lpExBuf->SetExecuteData(&d3dExData);
545 if (ddrval != DD_OK ) {
546 mprintf(( "Failed to SetExecuteData!\n" ));
550 ddrval = lpD3DDeviceEB->Execute( lpExBuf, lpViewport, D3DEXECUTE_UNCLIPPED );
551 if (ddrval != DD_OK ) {
552 mprintf(( "Failed to Execute! nverts=%d\n", D3D_num_verts));
553 mprintf(( "(%s)\n", d3d_error_string(ddrval) ));
558 memset( &debDesc, 0, sizeof( debDesc ) );
559 debDesc.dwSize = sizeof( debDesc );
560 ddrval = lpExBuf->Lock( &debDesc );
561 if ( ddrval != DD_OK ) {
562 mprintf(( "Failed to lock the execute buffer!\n" ));
566 lpPointer = lpBufStart = lpInsStart = debDesc.lpData;
568 lpPointer = (void *)((uint)lpPointer+sizeof(D3DTLVERTEX)*D3D_MAX_VERTS);
569 lpInsStart = lpPointer;
571 OP_PROCESS_VERTICES( 1, lpPointer );
572 PROCESSVERTICES_DATA( D3DPROCESSVERTICES_COPY, 0, 1, lpPointer );
575 D3D_ex_ptr = lpPointer;
576 D3D_ex_end = (void *)((uint)lpBufStart + Exb_size - 1024);
577 D3D_vertices = (D3DTLVERTEX *)lpBufStart;
595 HRESULT d3d_SetRenderState( D3DRENDERSTATETYPE dwRenderStateType, DWORD dwRenderState )
598 return lpD3DDevice->SetRenderState(dwRenderStateType, dwRenderState );
600 if ( (uint)D3D_ex_ptr > (uint)D3D_ex_end ) {
603 OP_STATE_RENDER(1, D3D_ex_ptr);
604 STATE_DATA(dwRenderStateType, dwRenderState, D3D_ex_ptr);
610 HRESULT d3d_DrawPrimitive( D3DPRIMITIVETYPE dptPrimitiveType, D3DVERTEXTYPE dvtVertexType, LPVOID lpvVertices, DWORD dwVertexCount, DWORD dwFlags )
613 return lpD3DDevice->DrawPrimitive(dptPrimitiveType, dvtVertexType, lpvVertices, dwVertexCount, dwFlags );
616 switch( dptPrimitiveType ) {
618 case D3DPT_TRIANGLEFAN:
619 if ( dvtVertexType == D3DVT_TLVERTEX ) {
621 D3DTLVERTEX *Verts = (D3DTLVERTEX *)lpvVertices;
623 if ( D3D_num_verts + dwVertexCount > D3D_MAX_VERTS ) gr_d3d_exb_flush(0);
624 if ( (uint)D3D_ex_ptr > (uint)D3D_ex_end ) gr_d3d_exb_flush(0);
626 D3DTLVERTEX *src_v = &D3D_vertices[D3D_num_verts];
629 for (i=0; i<(int)dwVertexCount; i++ ) {
633 // triangle data must be QWORD aligned, so we need to make sure
634 // that the OP_TRIANGLE_LIST is unaligned! Note that you MUST have
635 // the braces {} around the OP_NOP since the macro in D3DMACS.H will
636 // fail if you remove them.
638 if ( QWORD_ALIGNED( D3D_ex_ptr ) ) {
639 OP_NOP( D3D_ex_ptr );
642 OP_TRIANGLE_LIST( unsigned short(dwVertexCount-2), D3D_ex_ptr );
644 LPD3DTRIANGLE tri = ( LPD3DTRIANGLE )D3D_ex_ptr;
646 for (i=0; i<(int)dwVertexCount-2; i++ ) {
647 tri->v1 = unsigned short(D3D_num_verts+0);
648 tri->v2 = unsigned short(D3D_num_verts+i+1);
649 tri->v3 = unsigned short(D3D_num_verts+i+2);
650 tri->wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE;
654 D3D_ex_ptr = ( LPVOID )tri;
655 D3D_num_verts += (int)dwVertexCount;
660 if ( dvtVertexType == D3DVT_TLVERTEX ) {
662 D3DTLVERTEX *Verts = (D3DTLVERTEX *)lpvVertices;
664 if ( D3D_num_verts + dwVertexCount > D3D_MAX_VERTS ) gr_d3d_exb_flush(0);
665 if ( (uint)D3D_ex_ptr > (uint)D3D_ex_end ) gr_d3d_exb_flush(0);
667 D3DTLVERTEX *src_v = &D3D_vertices[D3D_num_verts];
670 for (i=0; i<(int)dwVertexCount; i++ ) {
674 // triangle data must be QWORD aligned, so we need to make sure
675 // that the OP_TRIANGLE_LIST is unaligned! Note that you MUST have
676 // the braces {} around the OP_NOP since the macro in D3DMACS.H will
677 // fail if you remove them.
679 if ( QWORD_ALIGNED( D3D_ex_ptr ) ) {
680 OP_NOP( D3D_ex_ptr );
683 ushort nlines = ushort(dwVertexCount/2);
685 OP_LINE_LIST( nlines, D3D_ex_ptr );
687 for (i=0; i<(int)nlines; i++ ) {
688 LPD3DLINE line = (LPD3DLINE )D3D_ex_ptr;
689 line->v1 = unsigned short(D3D_num_verts);
690 line->v2 = unsigned short(D3D_num_verts+1);
691 D3D_ex_ptr = (void *)(((LPD3DLINE)D3D_ex_ptr) + 1);
694 D3D_num_verts += (int)dwVertexCount;
702 volatile int D3D_running = 0;
703 volatile int D3D_activate = 0;
704 volatile int D3D_deactivate = 0;
706 void gr_d3d_activate(int active)
708 if ( !D3D_running ) {
711 mprintf(( "Direct3D activate: %d\n", active ));
713 HWND hwnd = (HWND)os_get_window();
719 ClipCursor(&D3D_cursor_clip_rect);
720 ShowWindow(hwnd,SW_RESTORE);
727 ShowWindow(hwnd,SW_MINIMIZE);
733 void d3d_start_frame()
737 if (!D3D_inited) return;
739 if ( In_frame < 0 || In_frame > 1 ) {
740 mprintf(( "Start frame error! (%d)\n", In_frame ));
743 if ( In_frame == 1 ) return;
745 // gr_d3d_clear_surface(lpBackBuffer, 0.0f, 0.0f, 0.0f);
749 ddrval = lpD3DDevice->BeginScene();
750 if (ddrval != D3D_OK ) {
751 //mprintf(( "Failed to begin scene!\n%s\n", d3d_error_string(ddrval) ));
759 void d3d_stop_frame()
763 if (!D3D_inited) return;
765 if ( In_frame < 0 || In_frame > 1 ) {
766 mprintf(( "Stop frame error! (%d)\n", In_frame ));
770 if ( In_frame == 0 ) return;
776 ddrval = lpD3DDevice->EndScene();
777 if (ddrval != D3D_OK ) {
778 //mprintf(( "Failed to end scene!\n%s\n", d3d_error_string(ddrval) ));
790 static void d3d_dump_format(DDPIXELFORMAT *pf)
794 for (r = 0, m = pf->dwRBitMask; !(m & 1); r++, m >>= 1);
795 for (r = 0; m & 1; r++, m >>= 1);
796 for (g = 0, m = pf->dwGBitMask; !(m & 1); g++, m >>= 1);
797 for (g = 0; m & 1; g++, m >>= 1);
798 for (b = 0, m = pf->dwBBitMask; !(m & 1); b++, m >>= 1);
799 for (b = 0; m & 1; b++, m >>= 1);
800 if ( pf->dwFlags & DDPF_ALPHAPIXELS ) {
801 for (a = 0, m = pf->dwRGBAlphaBitMask; !(m & 1); a++, m >>= 1);
802 for (a = 0; m & 1; a++, m >>= 1);
803 mprintf(( "ARGB, %d:%d:%d:%d\n", a, r, g, b ));
806 mprintf(( "RGB, %d:%d:%d\n", r, g, b ));
810 int D3D_found_1555_tex = 0;
811 int D3D_found_565_tex = 0;
812 static HRESULT WINAPI EnumTextureFormatsCallback(LPDDSURFACEDESC lpDDSD, LPVOID lpContext)
814 if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXEDTO8) {
815 mprintf(( "Palettized to an 8 bpp palette\n" ));
818 if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) {
819 mprintf(( "Palettized 8 bpp\n" ));
820 // TextureFormat = *lpDDSD;
821 // mprintf(( " ^-- And I'm using this one!\n" ));
822 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED4) {
823 mprintf(( "Palettized 4 bpp\n" ));
825 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED2) {
826 mprintf(( "Palettized 2 bpp\n" ));
828 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED1) {
829 mprintf(( "Palettized 1 bpp\n" ));
830 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ALPHA ) {
831 mprintf(( "Alpha something or other\n" ));
833 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_YUV ) {
834 mprintf(( "YUV?\n" ));
836 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ZBUFFER ) {
837 mprintf(( "ZBUFFER?\n" ));
839 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_COMPRESSED ) {
840 mprintf(( "Compressed?\n" ));
842 } else if (lpDDSD->ddpfPixelFormat.dwFlags & DDPF_FOURCC) {
843 mprintf(( "FourCC?\n" ));
848 for (r = 0, m = lpDDSD->ddpfPixelFormat.dwRBitMask; !(m & 1) && (r < 32); r++, m >>= 1);
849 for (r = 0; m & 1; r++, m >>= 1);
850 for (g = 0, m = lpDDSD->ddpfPixelFormat.dwGBitMask; !(m & 1) && (g < 32); g++, m >>= 1);
851 for (g = 0; m & 1; g++, m >>= 1);
852 for (b = 0, m = lpDDSD->ddpfPixelFormat.dwBBitMask; !(m & 1) && (b < 32); b++, m >>= 1);
853 for (b = 0; m & 1; b++, m >>= 1);
854 if ( lpDDSD->ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS ) {
855 for (a = 0, m = lpDDSD->ddpfPixelFormat.dwRGBAlphaBitMask; !(m & 1) && (a < 32); a++, m >>= 1);
856 for (a = 0; m & 1; a++, m >>= 1);
857 mprintf(( "ARGB, %d:%d:%d:%d\n", a, r, g, b ));
860 mprintf(( "RGB, %d:%d:%d\n", r, g, b ));
863 // if we found a nice 1555 texture format
864 if( (r == 5) && (g == 5) && (b == 5) && (a == 1)){
866 NonAlphaTextureFormat_1555 = lpDDSD->ddpfPixelFormat;
867 NonAlphaTextureFormat = lpDDSD->ddpfPixelFormat;
868 D3D_found_1555_tex = 1;
871 else if ( (r == 5) && (g == 6) && (b == 5) ){
872 NonAlphaTextureFormat_565 = lpDDSD->ddpfPixelFormat;
873 D3D_found_565_tex = 1;
875 // otherwise keep looking
876 else if(!D3D_found_1555_tex) {
877 //if ( (r==4) && (g==4) && (b==4) && (a==4) && (lpDDSD->ddpfPixelFormat.dwRGBBitCount==16) ) {
878 if ( (r>0) && (g>0) && (b>0) && (lpDDSD->ddpfPixelFormat.dwRGBBitCount==16) ) {
879 if ( r+g+b > Largest_rgb ) {
881 NonAlphaTextureFormat = lpDDSD->ddpfPixelFormat;
886 // HACK!!! Some 3dfx cards (Allender, Whiteside, Johnson) choose
887 // ARGB=8:3:3:2 as a texture format but when you go to create a surface
888 // in system RAM with this format, it fails, saying invalid surface format...
889 // So I'll just force 4:4:4:4 which seems to work on all cards I've seen.
890 if ( (a>0) && (a<8) && (lpDDSD->ddpfPixelFormat.dwRGBBitCount<=16) ) {
891 if ( a > Largest_alpha ) {
893 AlphaTextureFormat = lpDDSD->ddpfPixelFormat;
901 HRESULT WINAPI gr_d3d_enum( LPGUID lpGUID,
902 LPSTR lpDeviceDescription,
904 LPD3DDEVICEDESC lpHWDesc,
905 LPD3DDEVICEDESC lpHELDesc,
910 // mprintf(( "Found 3d device %s: %s\n", lpDeviceName, lpDeviceDescription ));
912 if ( lpHWDesc && lpHWDesc->dwFlags != 0 ) {
914 } //else if ( lpHELDesc ) {
918 d3d_device *d2d = (d3d_device *)lpContext;
919 d3d_device *d3d = (d3d_device *)&D3D_devices[Num_d3d_devices++];
922 memmove( &d3d->guid_3d, lpGUID, sizeof(GUID) );
923 d3d->pguid_3d = &d3d->guid_3d;
925 memset( &d3d->guid_3d, 0, sizeof(GUID) );
926 d3d->pguid_3d = NULL;
929 memmove( &d3d->guid_2d, &d2d->guid_2d, sizeof(GUID) );
930 if ( d2d->pguid_2d ) {
931 d3d->pguid_2d = &d3d->guid_2d;
933 d3d->pguid_2d = NULL;
936 //strcpy( d3d->name, lpDeviceName );
937 //strcat( d3d->name, " - " );
938 strcpy( d3d->name, NOX("Direct 3D - ") );
939 strcat( d3d->name, d2d->name );
940 //strcat( d3d->name, lpDeviceDescription );
943 return D3DENUMRET_OK;
946 BOOL WINAPI gr_d2d_enum( LPGUID lpGUID,
947 LPSTR lpDeviceDescription,
951 d3d_device *d2d = (d3d_device *)&D2D_devices[Num_d2d_devices++];
953 // mprintf(( "Found 2d device %s: %s\n", lpDeviceName, lpDeviceDescription ));
956 memmove( &d2d->guid_2d, lpGUID, sizeof(GUID) );
957 d2d->pguid_2d = &d2d->guid_2d;
959 memset( &d2d->guid_2d, 0, sizeof(GUID) );
960 d2d->pguid_2d = NULL;
963 strcpy( d2d->name, lpDeviceDescription );
964 // strcat( d2d->name, lpDeviceName );
966 return D3DENUMRET_OK;
969 d3d_device *d3d_poll_devices()
977 ddrval = DirectDrawEnumerate( gr_d2d_enum, NULL );
978 if ( ddrval != DD_OK ) {
979 mprintf(( "GR_D3D_INIT: DirectDrawEnumerate failed.\n" ));
983 for ( i=0; i<Num_d2d_devices; i++) {
984 d3d_device *d2d = (d3d_device *)&D2D_devices[i];
986 ddrval = DirectDrawCreate( d2d->pguid_2d, &lpDD1, NULL );
987 if ( ddrval != DD_OK ) {
988 mprintf(( "GR_D3D_INIT: DirectDrawCreate failed.\n" ));
992 ddrval = lpDD1->QueryInterface( IID_IDirect3D2, ( LPVOID *) &lpD3D );
993 if ( ddrval != DD_OK ) {
994 mprintf(( "GR_D3D_INIT: QueryInterface failed.\n" ));
998 ddrval = lpD3D->EnumDevices(gr_d3d_enum, d2d );
999 if ( ddrval != DD_OK ) {
1000 mprintf(( "WIN_DD32: D3D enum devices failed. (0x%x)\n", ddrval ));
1010 for ( i=0; i<Num_d3d_devices; i++) {
1011 mprintf(( "D3D Device %d: %s\n", i, D3D_devices[i].name ));
1012 //mprint_guid(D3D_devices[i].pguid_2d);
1015 if ( Num_d3d_devices <= 0 ) {
1016 mprintf(( "No D3D device found!\n" ));
1021 if ( Num_d3d_devices > 0 ) {
1022 //mprintf(( "More than one D3D device found!\n" ));
1024 char *name = os_config_read_string( NULL, "VideoCard", NULL );
1026 if ( name && (strlen(name)>0) ) {
1027 for ( i=0; i<Num_d3d_devices; i++) {
1028 if ( strstr( name, D3D_devices[i].name ) ) {
1029 mprintf(( "Using one that matched Direct3D registry value '%s'.\n", name ));
1030 return &D3D_devices[i];
1033 mprintf(( "WARNING!!!! Couldn't find one that matched Direct3D registry value '%s'.\n", name ));
1036 mprintf(( "But no Direct3D registry key or no match, so use the last one.\n" ));
1038 // Use the last device.
1039 return &D3D_devices[Num_d3d_devices-1];
1045 mprintf(( "Direct3D Polling failed.\n" ));
1050 LPDIRECTDRAW gr_d3d_get_dd()
1056 LPDIRECTDRAWSURFACE gr_d3d_get_dd_surface()
1058 return lpBackBuffer;
1061 void gr_d3d_clip_cursor(int active)
1064 ClipCursor(&D3D_cursor_clip_rect);
1070 // front buffer, backbuffer, d3d device, viewport
1071 int gr_d3d_create_rendering_objects(int clear)
1079 memset( &ddsd, 0, sizeof( ddsd ));
1081 ddsd.dwSize = sizeof( ddsd );
1085 ddsd.dwFlags = DDSD_CAPS;
1086 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE;
1090 ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
1091 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_3DDEVICE | DDSCAPS_COMPLEX;
1092 ddsd.dwBackBufferCount = 1;
1095 ddrval = lpDD->CreateSurface( &ddsd, &lpFrontBuffer, NULL );
1096 if ( ddrval != DD_OK ) {
1097 // mprintf(( "GR_D3D_INIT: CreateSurface (Front) failed.\n" ));
1098 strcpy(Device_init_error, "CreateSurface (Front) failed.");
1102 // create a clipper and a backbuffer in windowed mode
1104 // create a clipper for windowed mode
1105 LPDIRECTDRAWCLIPPER pcClipper;
1106 HRESULT hr = lpDD->CreateClipper(0, &pcClipper, NULL);
1111 // Associate the clipper with our window.
1112 pcClipper->SetHWnd(0, (HWND)os_get_window());
1113 lpFrontBuffer->SetClipper(pcClipper);
1114 pcClipper->Release();
1117 ddsd.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS;
1118 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_3DDEVICE;
1119 ddsd.dwWidth = gr_screen.max_w;
1120 ddsd.dwHeight = gr_screen.max_h;
1121 hr = lpDD->CreateSurface(&ddsd, &lpBackBuffer, NULL);
1123 strcpy(Device_init_error, "Windowed backbuffer create failed.");
1127 // backbuffer is already created for us in fullscreen mode
1129 ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
1130 ddrval = lpFrontBuffer->GetAttachedSurface( &ddscaps, &lpBackBuffer );
1131 if ( ddrval != DD_OK ) {
1132 // mprintf(( "GR_D3D_INIT: GetAttachedSurface (Back) failed.\n" ));
1133 strcpy(Device_init_error, "GetAttachedSurface (Back) failed.");
1139 // Create a z-buffer and attach it to the backbuffer
1143 memset( &ddsd, 0, sizeof( ddsd ) );
1144 ddsd.dwSize = sizeof(ddsd);
1145 ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_ZBUFFERBITDEPTH;
1146 ddsd.dwWidth = gr_screen.max_w;
1147 ddsd.dwHeight = gr_screen.max_h;
1148 ddsd.dwZBufferBitDepth = 16;
1150 ddsd.ddsCaps.dwCaps = DDSCAPS_ZBUFFER | DDSCAPS_VIDEOMEMORY;
1151 if (lpDD->CreateSurface(&ddsd, &lpZBuffer, NULL) != DD_OK) {
1152 // mprintf(( "GR_D3D_INIT: Create Zbuffer failed.\n" ));
1153 strcpy(Device_init_error, "Create Zbuffer failed.");
1157 if (lpBackBuffer->AddAttachedSurface(lpZBuffer) != DD_OK) {
1158 // mprintf(( "GR_D3D_INIT: Attach Zbuffer failed.\n" ));
1159 strcpy(Device_init_error, "Attach Zbuffer failed.");
1164 // blit all the buffers clear
1166 // Clear the surface
1168 ddBltFx.dwSize = sizeof(DDBLTFX);
1169 ddBltFx.dwFillColor = 0;
1170 lpFrontBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddBltFx);
1171 lpBackBuffer->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddBltFx);
1174 // Create the D3D device
1176 ddrval = lpD3D->CreateDevice( D3D_device->guid_3d, lpBackBuffer, &lpD3DDevice );
1177 if ( ddrval != DD_OK ) {
1178 // mprintf(( "GR_D3D_INIT: Create D3D Device2 failed. %s\n", d3d_error_string(ddrval) ));
1179 sprintf(Device_init_error, "Create D3D Device2 failed. %s\n", d3d_error_string(ddrval));
1183 // Create and add viewport
1184 ddrval = lpD3D->CreateViewport( &lpViewport, NULL );
1185 if ( ddrval != DD_OK ) {
1186 // mprintf(( "GR_D3D_INIT: CreateViewport failed.\n" ));
1187 strcpy(Device_init_error, "CreateViewport failed.");
1191 ddrval = lpD3DDevice->AddViewport( lpViewport );
1192 if ( ddrval != DD_OK ) {
1193 // mprintf(( "GR_D3D_INIT: AddViewport failed.\n" ));
1194 strcpy(Device_init_error, "CreateViewport failed.");
1199 // Setup the viewport for a reasonable viewing area
1200 D3DVIEWPORT viewdata;
1203 memset( &viewdata, 0, sizeof( viewdata ) );
1205 // Compensate for aspect ratio
1206 if ( gr_screen.max_w > gr_screen.max_h ){
1207 largest_side = gr_screen.max_w;
1209 largest_side = gr_screen.max_h;
1212 // this sets up W data so the fogging can work
1213 extern float z_mult;
1214 set_wbuffer_planes(lpD3DDevice, 0.0f, z_mult);
1216 viewdata.dwSize = sizeof( viewdata );
1217 viewdata.dwX = viewdata.dwY = 0;
1218 viewdata.dwWidth = gr_screen.max_w;
1219 viewdata.dwHeight = gr_screen.max_h;
1220 viewdata.dvScaleX = largest_side / 2.0F;
1221 viewdata.dvScaleY = largest_side / 2.0F;
1222 viewdata.dvMaxX = ( float ) ( viewdata.dwWidth / ( 2.0F * viewdata.dvScaleX ) );
1223 viewdata.dvMaxY = ( float ) ( viewdata.dwHeight / ( 2.0F * viewdata.dvScaleY ) );
1224 viewdata.dvMinZ = 0.0f;
1225 viewdata.dvMaxZ = 1.0f; // choose something appropriate here!
1227 ddrval = lpViewport->SetViewport( &viewdata );
1228 if ( ddrval != DD_OK ) {
1229 // mprintf(( "GR_D3D_INIT: SetViewport failed.\n" ));
1230 strcpy(Device_init_error, "SetViewport failed.");
1234 ddrval = lpD3DDevice->SetCurrentViewport(lpViewport );
1235 if ( ddrval != DD_OK ) {
1236 // mprintf(( "GR_D3D_INIT: SetCurrentViewport failed.\n" ));
1237 strcpy(Device_init_error, "SetCurrentViewport failed.");
1244 void gr_d3d_release_rendering_objects()
1247 lpViewport->Release();
1251 if ( lpD3DDevice ) {
1252 lpD3DDevice->Release();
1257 lpZBuffer->Release();
1262 lpBackBuffer->Release();
1263 lpBackBuffer = NULL;
1266 if (lpFrontBuffer) {
1267 lpFrontBuffer->Release();
1268 lpFrontBuffer = NULL;
1272 void gr_d3d_set_initial_render_state()
1274 d3d_SetRenderState(D3DRENDERSTATE_DITHERENABLE, TRUE );
1275 d3d_SetRenderState(D3DRENDERSTATE_TEXTUREMIN, D3DFILTER_LINEAR );
1276 d3d_SetRenderState(D3DRENDERSTATE_TEXTUREMAG, D3DFILTER_LINEAR );
1277 d3d_SetRenderState(D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD );
1278 d3d_SetRenderState(D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE );
1279 d3d_SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE );
1282 void gr_d3d_init_device(int screen_width, int screen_height)
1287 if ( os_config_read_uint( NULL, NOX("D3DUseExecuteBuffers"), 0 )) {
1294 d3d_device *dd = d3d_poll_devices();
1297 // Error( LOCATION, "No Direct3D devices found!\n" );
1298 strcpy(Device_init_error, "No Direct3D devices found!");
1302 // Let things catch up....
1305 hwnd = (HWND)os_get_window();
1307 // mprintf(( "gr_d3d_init_device: No window handle.\n" ));
1308 strcpy(Device_init_error, "Could not get application window handle");
1314 SetWindowPos(hwnd, HWND_TOP, 0, 0, gr_screen.max_w, gr_screen.max_h, SWP_SHOWWINDOW | SWP_NOMOVE | SWP_NOSIZE | SWP_DRAWFRAME);
1315 SetForegroundWindow(hwnd);
1316 SetActiveWindow(hwnd);
1318 D3D_cursor_clip_rect.left = 0;
1319 D3D_cursor_clip_rect.top = 0;
1320 D3D_cursor_clip_rect.right = gr_screen.max_w-1;
1321 D3D_cursor_clip_rect.bottom = gr_screen.max_h-1;
1323 // Prepare the window to go full screen
1325 mprintf(( "Window in debugging mode... mouse clicking may cause problems!\n" ));
1326 SetWindowLong( hwnd, GWL_EXSTYLE, 0 );
1327 SetWindowLong( hwnd, GWL_STYLE, WS_POPUP );
1328 ShowWindow(hwnd, SW_SHOWNORMAL );
1330 SystemParametersInfo( SPI_GETWORKAREA, 0, &work_rect, 0 );
1331 SetWindowPos( hwnd, HWND_TOPMOST, work_rect.left, work_rect.top, gr_screen.max_w, gr_screen.max_h, 0 );
1332 SetActiveWindow(hwnd);
1333 SetForegroundWindow(hwnd);
1334 D3D_cursor_clip_rect.left = work_rect.left;
1335 D3D_cursor_clip_rect.top = work_rect.top;
1336 D3D_cursor_clip_rect.right = work_rect.left + gr_screen.max_w - 1;
1337 D3D_cursor_clip_rect.bottom = work_rect.top + gr_screen.max_h - 1;
1339 SetWindowLong( hwnd, GWL_EXSTYLE, 0 );
1340 SetWindowLong( hwnd, GWL_STYLE, WS_POPUP );
1341 ShowWindow(hwnd, SW_SHOWNORMAL );
1342 // SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, GetSystemMetrics( SM_CXSCREEN ), GetSystemMetrics( SM_CYSCREEN ), 0 );
1343 SetWindowPos( hwnd, HWND_TOPMOST, 0, 0, gr_screen.max_w, gr_screen.max_h, 0 );
1344 SetActiveWindow(hwnd);
1345 SetForegroundWindow(hwnd);
1346 D3D_cursor_clip_rect.left = 0;
1347 D3D_cursor_clip_rect.top = 0;
1348 D3D_cursor_clip_rect.right = gr_screen.max_w-1;
1349 D3D_cursor_clip_rect.bottom = gr_screen.max_h-1;
1353 // active d3d device
1357 ddrval = DirectDrawCreate( dd->pguid_2d, &lpDD1, NULL );
1359 if ( ddrval != DD_OK ) {
1360 // mprintf(( "GR_D3D_INIT: DirectDrawCreate failed.\n" ));
1361 strcpy(Device_init_error, "DirectDrawCreate failed");
1365 ddrval = lpDD1->QueryInterface(IID_IDirectDraw2, (LPVOID *)&lpDD);
1366 if(ddrval != DD_OK) {
1367 // mprintf(( "GR_D3D_INIT: DirectDrawCreate2 failed.\n" ));
1368 strcpy(Device_init_error, "DirectDrawCreate2 failed");
1373 ddrval = lpDD->SetCooperativeLevel( hwnd, DDSCL_NORMAL );
1375 ddrval = lpDD->SetCooperativeLevel( hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_NOWINDOWCHANGES );
1377 if ( ddrval != DD_OK ) {
1378 // mprintf(( "GR_D3D_INIT: SetCooperativeLevel EXCLUSIVE failed.\n" ));
1379 strcpy(Device_init_error, "SetCooperativeLevel EXCLUSIVE failed.");
1383 // Go to full screen!
1386 ddrval = lpDD->SetDisplayMode( screen_width, screen_height, gr_screen.bits_per_pixel, 0, 0 );
1388 if ( ddrval != DD_OK ) {
1389 // mprintf(( "GR_D3D_INIT: SetDisplayMode failed.\n" ));
1390 strcpy(Device_init_error, "SetDisplayMode failed.");
1395 gr_d3d_clip_cursor(1);
1397 ddrval = lpDD->QueryInterface( IID_IDirect3D2, ( LPVOID *) &lpD3D );
1398 if ( ddrval != DD_OK ) {
1399 // mprintf(( "GR_D3D_INIT: QueryInterface failed.\n" ));
1400 strcpy(Device_init_error, "QueryInterface failed.");
1404 // create all surfaces here
1405 if(!gr_d3d_create_rendering_objects(1)){
1410 extern void dd_get_shift_masks( DDSURFACEDESC *ddsd );
1412 memset( &ddsd, 0, sizeof( ddsd ) );
1413 ddsd.dwSize = sizeof(ddsd);
1414 lpBackBuffer->GetSurfaceDesc(&ddsd);
1415 dd_get_shift_masks( &ddsd );
1416 ScreenFormat = ddsd.ddpfPixelFormat;
1419 // if we're in windowed mode, fill in bits per pixel and bytes per pixel here
1421 gr_screen.bits_per_pixel = ScreenFormat.dwRGBBitCount;
1422 gr_screen.bytes_per_pixel = gr_screen.bits_per_pixel / 8;
1425 // Init values so we can choose the largest of each format
1428 D3D_found_1555_tex = 0;
1429 D3D_found_565_tex = 0;
1430 ddrval = lpD3DDevice->EnumTextureFormats(EnumTextureFormatsCallback, NULL );
1431 if ( ddrval != DD_OK ) {
1432 // mprintf(( "GR_D3D_INIT: EnumTextureFormats failed.\n" ));
1433 strcpy(Device_init_error, "EnumTextureFormats failed.");
1437 // if we found our ideal texture, set the global pixel format
1438 if(D3D_found_1555_tex){
1439 Bm_pixel_format = BM_PIXEL_FORMAT_ARGB_D3D;
1442 if ( Largest_alpha == 0 ) {
1444 MessageBox( NULL, XSTR("Alpha channel textures",620), XSTR("Missing Features",621), MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
1447 if ( Largest_rgb == 0 ) {
1449 MessageBox( NULL, XSTR("16-bpp RGB textures",622), XSTR("Missing Features",621), MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
1453 // setup texture pixel formats
1455 DDPIXELFORMAT *surface_desc;
1460 // Determine the red, green and blue masks' shift and scale.
1461 surface_desc = &NonAlphaTextureFormat;
1462 for (s = 0, m = surface_desc->dwRBitMask; !(m & 1); s++, m >>= 1);
1463 Gr_t_red.mask = surface_desc->dwRBitMask;
1465 Gr_t_red.scale = 255 / (surface_desc->dwRBitMask >> s);
1466 for (s = 0, m = surface_desc->dwGBitMask; !(m & 1); s++, m >>= 1);
1467 Gr_t_green.mask = surface_desc->dwRBitMask;
1468 Gr_t_green.shift = s;
1469 Gr_t_green.scale = 255 / (surface_desc->dwGBitMask >> s);
1470 for (s = 0, m = surface_desc->dwBBitMask; !(m & 1); s++, m >>= 1);
1471 Gr_t_blue.mask = surface_desc->dwRBitMask;
1472 Gr_t_blue.shift = s;
1473 Gr_t_blue.scale = 255 / (surface_desc->dwBBitMask >> s);
1474 Gr_t_alpha.mask = surface_desc->dwRGBAlphaBitMask;
1475 if ( surface_desc->dwFlags & DDPF_ALPHAPIXELS ) {
1476 for (s = 0, m = surface_desc->dwRGBAlphaBitMask; !(m & 1); s++, m >>= 1);
1477 Gr_t_alpha.shift = s;
1478 Gr_t_alpha.scale = 255 / (surface_desc->dwRGBAlphaBitMask >> s);
1480 Gr_t_alpha.shift = 0;
1481 Gr_t_alpha.scale = 256;
1484 // Determine the red, green and blue masks' shift and scale.
1485 surface_desc = &AlphaTextureFormat;
1486 for (s = 0, m = surface_desc->dwRBitMask; !(m & 1); s++, m >>= 1);
1487 Gr_ta_red.mask = surface_desc->dwRBitMask;
1488 Gr_ta_red.shift = s;
1489 Gr_ta_red.scale = 255 / (surface_desc->dwRBitMask >> s);
1490 for (s = 0, m = surface_desc->dwGBitMask; !(m & 1); s++, m >>= 1);
1491 Gr_ta_green.mask = surface_desc->dwRBitMask;
1492 Gr_ta_green.shift = s;
1493 Gr_ta_green.scale = 255 / (surface_desc->dwGBitMask >> s);
1494 for (s = 0, m = surface_desc->dwBBitMask; !(m & 1); s++, m >>= 1);
1495 Gr_ta_blue.mask = surface_desc->dwRBitMask;
1496 Gr_ta_blue.shift = s;
1497 Gr_ta_blue.scale = 255 / (surface_desc->dwBBitMask >> s);
1498 Gr_ta_alpha.mask = surface_desc->dwRGBAlphaBitMask;
1499 if ( surface_desc->dwFlags & DDPF_ALPHAPIXELS ) {
1500 for (s = 0, m = surface_desc->dwRGBAlphaBitMask; !(m & 1); s++, m >>= 1);
1501 Gr_ta_alpha.shift = s;
1502 Gr_ta_alpha.scale = 255 / (surface_desc->dwRGBAlphaBitMask >> s);
1504 Gr_ta_alpha.shift = 0;
1505 Gr_ta_alpha.scale = 256;
1509 mprintf(( "Alpha texture format = " ));
1510 d3d_dump_format(&AlphaTextureFormat);
1512 mprintf(( "Non-alpha texture format = " ));
1513 d3d_dump_format(&NonAlphaTextureFormat);
1515 mprintf(( "Screen format = " ));
1516 d3d_dump_format(&ScreenFormat);
1520 // gr_d3d_exb_init();
1522 memset( &D3DHWDevDesc, 0, sizeof( D3DHWDevDesc ) );
1523 D3DHWDevDesc.dwSize = sizeof(D3DHELDevDesc);
1525 memset( &D3DHELDevDesc, 0, sizeof( D3DHELDevDesc ) );
1526 D3DHELDevDesc.dwSize = sizeof(D3DHELDevDesc);
1528 ddrval = lpD3DDevice->GetCaps( &D3DHWDevDesc, &D3DHELDevDesc );
1529 if ( ddrval != DD_OK ) {
1530 mprintf(( "GR_D3D_INIT: 3DDevice->GetCaps failed.\n" ));
1533 lpDevDesc = &D3DHWDevDesc;
1535 lpDD->GetCaps(&DD_driver_caps,&DD_hel_caps);
1540 char missing_features[128*1024];
1542 strcpy( missing_features, XSTR("Your video card is missing the following features required by FreeSpace:\r\n\r\n",623) );
1545 if ( !(lpDevDesc->dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGVERTEX) && !(lpDevDesc->dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)){
1546 strcat( missing_features, XSTR("Vertex fog or Table fog\r\n", 1499) );
1550 // Texture blending values
1551 if ( !(lpDevDesc->dpcTriCaps.dwTextureBlendCaps & D3DPTBLENDCAPS_MODULATE )) {
1552 strcat( missing_features, XSTR("Texture blending mode = Modulate\r\n", 624) );
1556 // Source blending values
1557 // if ( !(lpDevDesc->dpcTriCaps.dwSrcBlendCaps & D3DPBLENDCAPS_ONE) ) {
1558 // strcat( missing_features, "Source blending mode = ONE\r\n" );
1562 if ( !(lpDevDesc->dpcTriCaps.dwSrcBlendCaps & (D3DPBLENDCAPS_SRCALPHA|D3DPBLENDCAPS_BOTHSRCALPHA)) ) {
1563 strcat( missing_features, XSTR("Source blending mode = SRCALPHA or BOTHSRCALPHA\r\n", 625) );
1567 // if ( !(lpDevDesc->dpcTriCaps.dwDestBlendCaps & D3DPBLENDCAPS_ZERO ) ) {
1568 // strcat( missing_features, "Destination blending mode = ZERO\r\n" );
1572 // if ( !(lpDevDesc->dpcTriCaps.dwDestBlendCaps & D3DPBLENDCAPS_ONE ) ) {
1573 // strcat( missing_features, "Destination blending mode = ONE\r\n" );
1577 // Dest blending values
1578 if ( !(lpDevDesc->dpcTriCaps.dwDestBlendCaps & (D3DPBLENDCAPS_INVSRCALPHA|D3DPBLENDCAPS_BOTHINVSRCALPHA)) ) {
1579 strcat( missing_features, XSTR("Destination blending mode = INVSRCALPHA or BOTHINVSRCALPHA\r\n",626) );
1583 // If card is Mystique 220, turn off modulaalpha since it doesn't work...
1584 if ( Largest_alpha < 4 ) {
1585 lpDevDesc->dpcTriCaps.dwTextureBlendCaps &= (~D3DPTBLENDCAPS_MODULATEALPHA);
1590 MessageBox( NULL, missing_features, XSTR("Missing Features",621), MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
1595 // fog info - for now we'll prefer table fog over vertex fog
1597 // if the user wants to force w-fog, maybe do it
1598 if(os_config_read_uint(NULL, "ForceWFOG", 0) && (lpDevDesc->dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)){
1601 // if the card does not have vertex fog, but has table fog, let it go
1602 if(!(lpDevDesc->dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGVERTEX) && (lpDevDesc->dpcTriCaps.dwRasterCaps & D3DPRASTERCAPS_FOGTABLE)){
1608 DWORD dwFree, dwTotal;
1610 memset(&ddsCaps,0,sizeof(ddsCaps) );
1611 ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1612 HRESULT ddrval = lpDD->GetAvailableVidMem(&ddsCaps, &dwTotal, &dwFree);
1613 if ( ddrval != DD_OK ) {
1614 mprintf(( "GR_D3D_INIT: GetAvailableVidMem failed.\n" ));
1618 if ( dwFree < (1024*1024) ) {
1620 MessageBox( NULL, XSTR("At least 1 MB of available video memory required.",627), XSTR("Missing Features",621), MB_OK|MB_TASKMODAL|MB_SETFOREGROUND );
1625 // setup proper render state
1626 gr_d3d_set_initial_render_state();
1628 mprintf(( "Direct3D Initialized OK!\n" ));
1634 mprintf(( "Direct3D Initialization failed.\n" ));
1640 int Gr_d3d_mouse_saved = 0;
1641 int Gr_d3d_mouse_saved_x1 = 0;
1642 int Gr_d3d_mouse_saved_y1 = 0;
1643 int Gr_d3d_mouse_saved_x2 = 0;
1644 int Gr_d3d_mouse_saved_y2 = 0;
1645 int Gr_d3d_mouse_saved_w = 0;
1646 int Gr_d3d_mouse_saved_h = 0;
1647 #define MAX_SAVE_SIZE (32*32)
1648 ushort Gr_d3d_mouse_saved_data[MAX_SAVE_SIZE];
1650 // Clamps X between R1 and R2
1651 #define CLAMP(x,r1,r2) do { if ( (x) < (r1) ) (x) = (r1); else if ((x) > (r2)) (x) = (r2); } while(0)
1653 void gr_d3d_save_mouse_area(int x, int y, int w, int h )
1660 Gr_d3d_mouse_saved_x1 = x;
1661 Gr_d3d_mouse_saved_y1 = y;
1662 Gr_d3d_mouse_saved_x2 = x+w-1;
1663 Gr_d3d_mouse_saved_y2 = y+h-1;
1665 CLAMP(Gr_d3d_mouse_saved_x1, gr_screen.clip_left, gr_screen.clip_right );
1666 CLAMP(Gr_d3d_mouse_saved_x2, gr_screen.clip_left, gr_screen.clip_right );
1667 CLAMP(Gr_d3d_mouse_saved_y1, gr_screen.clip_top, gr_screen.clip_bottom );
1668 CLAMP(Gr_d3d_mouse_saved_y2, gr_screen.clip_top, gr_screen.clip_bottom );
1670 Gr_d3d_mouse_saved_w = Gr_d3d_mouse_saved_x2 - Gr_d3d_mouse_saved_x1 + 1;
1671 Gr_d3d_mouse_saved_h = Gr_d3d_mouse_saved_y2 - Gr_d3d_mouse_saved_y1 + 1;
1673 if ( Gr_d3d_mouse_saved_w < 1 ) return;
1674 if ( Gr_d3d_mouse_saved_h < 1 ) return;
1676 // Make sure we're not saving too much!
1677 Assert( (Gr_d3d_mouse_saved_w*Gr_d3d_mouse_saved_h) <= MAX_SAVE_SIZE );
1682 memset( &ddsd, 0, sizeof( ddsd ) );
1683 ddsd.dwSize = sizeof( ddsd );
1685 ddrval = lpBackBuffer->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
1686 if ( ddrval == DD_OK ) {
1689 int short_per_row=ddsd.lPitch/2;
1691 rptr = (ushort *)ddsd.lpSurface;
1693 ushort *sptr, *dptr;
1695 dptr = Gr_d3d_mouse_saved_data;
1697 for (int i=0; i<Gr_d3d_mouse_saved_h; i++ ) {
1698 sptr = &rptr[(Gr_d3d_mouse_saved_y1+i)*short_per_row+Gr_d3d_mouse_saved_x1];
1700 for(int j=0; j<Gr_d3d_mouse_saved_w; j++ ) {
1706 lpBackBuffer->Unlock( NULL );
1708 Gr_d3d_mouse_saved = 1;
1711 mprintf(( "Couldn't get read-only lock to backbuffer for d3d mouse save\n" ));
1725 mouse_eval_deltas();
1727 Gr_d3d_mouse_saved = 0; // assume not saved
1729 if(!Gr_bitmap_poly){
1733 if ( mouse_is_visible() ) {
1735 mouse_get_pos( &mx, &my );
1737 gr_d3d_save_mouse_area(mx,my,32,32);
1738 if ( Gr_cursor == -1 ) {
1740 gr_set_color(255,255,255);
1741 gr_line( mx, my, mx+7, my + 7 );
1742 gr_line( mx, my, mx+5, my );
1743 gr_line( mx, my, mx, my+5 );
1746 gr_set_bitmap(Gr_cursor);
1747 gr_bitmap( mx, my );
1751 // Move stop frame before drawing cursor so cursor always appears on PowerVR.
1756 // ddrval = lpFrontBuffer->Flip( NULL, 1 );
1757 // if (ddrval != DD_OK ) {
1758 // mprintf(( "Fullscreen flip failed!\n" ));
1764 RECT rect, view_rect;
1765 GetClientRect( (HWND)os_get_window(), &rect);
1766 ClientToScreen( (HWND)os_get_window(), (POINT*)&rect.left );
1767 ClientToScreen( (HWND)os_get_window(), (POINT*)&rect.right );
1770 view_rect.right = gr_screen.max_w - 1;
1771 view_rect.bottom = gr_screen.max_h - 1;
1774 lpFrontBuffer->Blt(&rect, lpBackBuffer, &view_rect, 0, NULL );
1775 while(lpFrontBuffer->GetBltStatus(DDGBS_ISBLTDONE) != DD_OK){}
1778 if ( lpFrontBuffer->IsLost() == DDERR_SURFACELOST ) {
1779 lpFrontBuffer->Restore();
1781 if ( lpBackBuffer->IsLost() == DDERR_SURFACELOST ) {
1782 lpBackBuffer->Restore();
1784 if ( lpZBuffer->IsLost() == DDERR_SURFACELOST ) {
1785 lpZBuffer->Restore();
1787 ddrval = lpFrontBuffer->Flip( NULL, DDFLIP_WAIT );
1788 if ( ddrval == DDERR_SURFACELOST ) {
1789 mprintf(( "Front surface lost... attempting to restore...\n" ));
1790 os_sleep(1000); // Wait a second
1798 } else if (ddrval != DD_OK ) {
1799 mprintf(( "Fullscreen flip failed!\n" ));
1805 int cnt = D3D_activate;
1809 gr_d3d_clip_cursor(1);
1812 cnt = D3D_deactivate;
1814 D3D_deactivate-=cnt;
1815 gr_d3d_clip_cursor(0);
1821 void gr_d3d_flip_cleanup()
1825 if (lpFrontBuffer->Flip( NULL, 1 ) != DD_OK ) {
1826 mprintf(( "Flip failed!\n" ));
1832 void gr_d3d_flip_window(uint _hdc, int x, int y, int w, int h )
1836 void gr_d3d_cleanup()
1838 if (!D3D_inited) return;
1840 d3d_tcache_cleanup();
1843 gr_d3d_release_rendering_objects();
1853 HWND hwnd = (HWND)os_get_window();
1855 ddrval = lpDD->RestoreDisplayMode();
1856 if( ddrval != DD_OK ) {
1857 mprintf(( "WIN_DD32: RestoreDisplayMode failed (0x%x)\n", ddrval ));
1860 ddrval = lpDD->SetCooperativeLevel( hwnd, DDSCL_NORMAL );
1861 if( ddrval != DD_OK ) {
1862 mprintf(( "WIN_DD32: SetCooperativeLevel W Failed (0x%x)\n", ddrval ));
1865 // restore windows clipping rectangle
1868 // SetWindowLong( hwnd, GWL_STYLE, WS_CAPTION | WS_SYSMENU );
1869 // SetWindowLong( hwnd, GWL_EXSTYLE, 0 );
1870 // SetWindowPos( hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_SHOWWINDOW|SWP_NOMOVE|SWP_NOSIZE );
1872 // gr_d3d_clip_cursor(0);
1884 void gr_d3d_fade_in(int instantaneous)
1888 void gr_d3d_fade_out(int instantaneous)
1894 void gr_d3d_force_windowed()
1896 HWND hwnd = (HWND)os_get_window();
1899 PostMessage(hwnd,WM_SYSKEYUP, 0x9, 0xa00f0001 );
1900 PostMessage(hwnd,WM_SYSKEYUP, 0x12, 0xc0380001 );
1902 gr_d3d_clip_cursor(0);
1904 // Wait a second to give things a change to settle down.
1908 static char *Gr_saved_screen = NULL;
1910 int gr_d3d_save_screen()
1914 if ( Gr_saved_screen ) {
1915 mprintf(( "Screen alread saved!\n" ));
1919 Gr_saved_screen = (char*)malloc( gr_screen.max_w * gr_screen.max_h * gr_screen.bytes_per_pixel );
1920 if (!Gr_saved_screen) {
1921 mprintf(( "Couldn't get memory for saved screen!\n" ));
1928 memset( &ddsd, 0, sizeof( ddsd ) );
1929 ddsd.dwSize = sizeof( ddsd );
1931 ddrval = lpFrontBuffer->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
1932 if ( ddrval != DD_OK ) {
1933 free(Gr_saved_screen);
1934 Gr_saved_screen = NULL;
1935 mprintf(( "Error locking surface for save_screen, %s\n", d3d_error_string(ddrval) ));
1941 uint *dptr = (uint*)ddsd.lpSurface;
1943 for (i=0; i<gr_screen.max_h; i++ ) {
1944 memcpy( &Gr_saved_screen[gr_screen.max_w * i * gr_screen.bytes_per_pixel], dptr, gr_screen.max_w * gr_screen.bytes_per_pixel );
1946 dptr = (uint *)((uint)dptr + ddsd.lPitch);
1949 ushort *dptr = (ushort *)ddsd.lpSurface;
1951 for (i=0; i<gr_screen.max_h; i++ ) {
1952 memcpy( &Gr_saved_screen[gr_screen.max_w * i * gr_screen.bytes_per_pixel], dptr, gr_screen.max_w * gr_screen.bytes_per_pixel );
1954 dptr = (ushort *)((uint)dptr + ddsd.lPitch);
1958 // Unlock the front buffer
1959 lpFrontBuffer->Unlock( NULL );
1961 if ( Gr_d3d_mouse_saved && !D3D_32bit) {
1962 ushort *sptr, *dptr;
1964 sptr = Gr_d3d_mouse_saved_data;
1966 for (int i=0; i<Gr_d3d_mouse_saved_h; i++ ) {
1967 dptr = &((ushort*)Gr_saved_screen)[(Gr_d3d_mouse_saved_y1 + i) * gr_screen.max_w + Gr_d3d_mouse_saved_x1];
1969 for(int j=0; j<Gr_d3d_mouse_saved_w; j++ ) {
1978 void gr_d3d_restore_screen(int id)
1982 if ( !Gr_saved_screen ) {
1990 memset( &ddsd, 0, sizeof( ddsd ) );
1991 ddsd.dwSize = sizeof( ddsd );
1993 ddrval = lpBackBuffer->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
1994 if ( ddrval != DD_OK ) {
1995 free(Gr_saved_screen);
1996 Gr_saved_screen = NULL;
1997 mprintf(( "Error locking surface for restore_screen, %s\n", d3d_error_string(ddrval) ));
2003 uint *dptr = (uint *)ddsd.lpSurface;
2005 for (i=0; i<gr_screen.max_h; i++ ) {
2006 memcpy( dptr, &Gr_saved_screen[gr_screen.max_w * i * gr_screen.bytes_per_pixel], gr_screen.max_w * gr_screen.bytes_per_pixel );
2008 dptr = (uint *)((uint)dptr + ddsd.lPitch);
2011 ushort *dptr = (ushort *)ddsd.lpSurface;
2013 for (i=0; i<gr_screen.max_h; i++ ) {
2014 memcpy( dptr, &Gr_saved_screen[gr_screen.max_w * i * gr_screen.bytes_per_pixel], gr_screen.max_w * gr_screen.bytes_per_pixel );
2016 dptr = (ushort *)((uint)dptr + ddsd.lPitch);
2020 // Unlock the back buffer
2021 lpBackBuffer->Unlock( NULL );
2024 void gr_d3d_free_screen(int id)
2026 if ( Gr_saved_screen ) {
2027 free( Gr_saved_screen );
2028 Gr_saved_screen = NULL;
2032 static int D3d_dump_frames = 0;
2033 static ubyte *D3d_dump_buffer = NULL;
2034 static int D3d_dump_frame_number = 0;
2035 static int D3d_dump_frame_count = 0;
2036 static int D3d_dump_frame_count_max = 0;
2037 static int D3d_dump_frame_size = 0;
2038 void gr_d3d_dump_frame_start(int first_frame, int frames_between_dumps)
2040 if ( D3d_dump_frames ) {
2041 Int3(); // We're already dumping frames. See John.
2044 D3d_dump_frames = 1;
2045 D3d_dump_frame_number = first_frame;
2046 D3d_dump_frame_count = 0;
2047 D3d_dump_frame_count_max = frames_between_dumps;
2048 D3d_dump_frame_size = gr_screen.max_w * gr_screen.max_h * 2;
2050 if ( !D3d_dump_buffer ) {
2051 int size = D3d_dump_frame_count_max * D3d_dump_frame_size;
2052 D3d_dump_buffer = (ubyte *)malloc(size);
2053 if ( !D3d_dump_buffer ) {
2054 Error(LOCATION, "Unable to malloc %d bytes for dump buffer", size );
2059 extern int tga_compress(char *out, char *in, int bytecount);
2060 void gr_d3d_flush_frame_dump()
2063 char filename[MAX_PATH_LEN], *movie_path = ".\\";
2064 ubyte outrow[1024*3*4];
2066 if ( gr_screen.max_w > 1024) {
2067 mprintf(( "Screen too wide for frame_dump\n" ));
2071 for (i = 0; i < D3d_dump_frame_count; i++) {
2073 int w = gr_screen.max_w;
2074 int h = gr_screen.max_h;
2076 sprintf(filename, NOX("%sfrm%04d.tga"), movie_path, D3d_dump_frame_number );
2077 D3d_dump_frame_number++;
2079 CFILE *f = cfopen(filename, "wb");
2081 // Write the TGA header
2082 cfwrite_ubyte( 0, f ); // IDLength;
2083 cfwrite_ubyte( 0, f ); // ColorMapType;
2084 cfwrite_ubyte( 10, f ); // ImageType; // 2 = 24bpp, uncompressed, 10=24bpp rle compressed
2085 cfwrite_ushort( 0, f ); // CMapStart;
2086 cfwrite_ushort( 0, f ); // CMapLength;
2087 cfwrite_ubyte( 0, f ); // CMapDepth;
2088 cfwrite_ushort( 0, f ); // XOffset;
2089 cfwrite_ushort( 0, f ); // YOffset;
2090 cfwrite_ushort( (ushort)w, f ); // Width;
2091 cfwrite_ushort( (ushort)h, f ); // Height;
2092 cfwrite_ubyte( 24, f ); //PixelDepth;
2093 cfwrite_ubyte( 0, f ); //ImageDesc;
2095 // Go through and write our pixels
2097 ubyte *src_ptr = D3d_dump_buffer+(i*D3d_dump_frame_size)+(j*w*2);
2099 int len = tga_compress( (char *)outrow, (char *)src_ptr, w*sizeof(short) );
2101 cfwrite(outrow,len,1,f);
2108 D3d_dump_frame_count = 0;
2111 void gr_d3d_dump_frame_stop()
2114 if ( !D3d_dump_frames ) {
2115 Int3(); // We're not dumping frames. See John.
2119 // dump any remaining frames
2120 gr_d3d_flush_frame_dump();
2122 D3d_dump_frames = 0;
2123 if ( D3d_dump_buffer ) {
2124 free(D3d_dump_buffer);
2125 D3d_dump_buffer = NULL;
2129 void gr_d3d_dump_screen_hack( ushort * dst )
2133 LPDIRECTDRAWSURFACE surf = lpBackBuffer;
2137 // don't dump in 32 bit
2143 BM_SELECT_SCREEN_FORMAT();
2144 memset( &ddsd, 0, sizeof( ddsd ) );
2145 ddsd.dwSize = sizeof( ddsd );
2147 // try and lock the buffer
2148 ddrval = surf->Lock( NULL, &ddsd, DDLOCK_WAIT | DDLOCK_READONLY, NULL );
2149 if ( ddrval != DD_OK ) {
2150 mprintf(( "Error locking surface for get_region, %s\n", d3d_error_string(ddrval) ));
2155 ubyte *rptr = (ubyte*)ddsd.lpSurface;
2157 for (int i=0; i<gr_screen.max_h; i++ ) {
2158 sptr = (ushort*)&rptr[(gr_screen.max_h-i-1)*ddsd.lPitch];
2160 for(int j=0; j<gr_screen.max_w; j++ ) {
2163 bm_get_components((ubyte*)pixel, &r, &g, &b, NULL);
2165 // bash to 565, hackity hack
2167 pixel |= ((r >> 3) << 11);
2168 pixel |= ((g >> 2) << 5);
2175 // Unlock the buffer
2176 surf->Unlock( NULL );
2179 void gr_d3d_dump_frame()
2181 // A hacked function to dump the frame buffer contents
2182 gr_d3d_dump_screen_hack( (ushort *)(D3d_dump_buffer+(D3d_dump_frame_count*D3d_dump_frame_size)) );
2184 D3d_dump_frame_count++;
2186 if ( D3d_dump_frame_count == D3d_dump_frame_count_max ) {
2187 gr_d3d_flush_frame_dump();
2196 void gr_d3d_unlock()
2200 void gr_d3d_fog_set(int fog_mode, int r, int g, int b, float fog_near, float fog_far)
2204 Assert((r >= 0) && (r < 256));
2205 Assert((g >= 0) && (g < 256));
2206 Assert((b >= 0) && (b < 256));
2209 if(fog_mode == GR_FOGMODE_NONE){
2210 // only change state if we need to
2211 if(gr_screen.current_fog_mode != fog_mode){
2212 d3d_SetRenderState(D3DRENDERSTATE_FOGENABLE, FALSE );
2214 gr_screen.current_fog_mode = fog_mode;
2216 // to prevent further state changes
2220 // maybe switch fogging on
2221 if(gr_screen.current_fog_mode != fog_mode){
2222 d3d_SetRenderState(D3DRENDERSTATE_FOGENABLE, TRUE);
2224 // if we're using table fog, enable table fogging
2225 if(D3D_fog_mode == 2){
2226 d3d_SetRenderState( D3DRENDERSTATE_FOGTABLEMODE, D3DFOG_LINEAR );
2229 gr_screen.current_fog_mode = fog_mode;
2232 // is color changing?
2233 if( (gr_screen.current_fog_color.red != r) || (gr_screen.current_fog_color.green != g) || (gr_screen.current_fog_color.blue != b) ){
2235 gr_d3d_init_color( &gr_screen.current_fog_color, r, g, b );
2237 color = RGB_MAKE(r, g, b);
2238 d3d_SetRenderState(D3DRENDERSTATE_FOGCOLOR, color);
2242 if( (fog_near >= 0.0f) && (fog_far >= 0.0f) && ((fog_near != gr_screen.fog_near) || (fog_far != gr_screen.fog_far)) ){
2243 gr_screen.fog_near = fog_near;
2244 gr_screen.fog_far = fog_far;
2246 // only generate a new fog table if we have to (wfog/table fog mode)
2247 if(D3D_fog_mode == 2){
2248 d3d_SetRenderState( D3DRENDERSTATE_FOGTABLESTART, *((DWORD *)(&fog_near)));
2249 d3d_SetRenderState( D3DRENDERSTATE_FOGTABLEEND, *((DWORD *)(&fog_far)));
2254 void gr_d3d_set_gamma(float gamma)
2257 Gr_gamma_int = int(Gr_gamma*100);
2259 // Create the Gamma lookup table
2261 for (i=0; i<256; i++ ) {
2262 int v = fl2i(pow(i2fl(i)/255.0f, 1.0f/Gr_gamma)*255.0f);
2265 } else if ( v < 0 ) {
2268 Gr_gamma_lookup[i] = v;
2271 // Flush any existing textures
2275 void d3d_get_pixel(int x, int y, ubyte *pixel)
2280 memset( &ddsd, 0, sizeof( ddsd ) );
2281 ddsd.dwSize = sizeof( ddsd );
2283 ddrval = lpFrontBuffer->Lock( NULL, &ddsd, DDLOCK_WAIT, NULL );
2284 if ( ddrval != DD_OK ) {
2285 mprintf(( "Error locking surface for uv test, %s\n", d3d_error_string(ddrval) ));
2289 ubyte *dptr = (ubyte *)((uint)ddsd.lpSurface + y*ddsd.lPitch + (x * gr_screen.bytes_per_pixel));
2290 memcpy(pixel, dptr, gr_screen.bytes_per_pixel);
2292 // Unlock the buffer
2293 lpFrontBuffer->Unlock( NULL );
2296 void gr_d3d_get_pixel(int x, int y, int *r, int *g, int *b)
2300 // resolution checking
2301 int gr_d3d_supports_res_interface(int res)
2306 int gr_d3d_supports_res_ingame(int res)
2311 void gr_d3d_set_cull(int cull)
2313 // switch culling on or off
2315 d3d_SetRenderState( D3DRENDERSTATE_CULLMODE, D3DCULL_CCW );
2317 d3d_SetRenderState( D3DRENDERSTATE_CULLMODE, D3DCULL_NONE );
2322 void gr_d3d_cross_fade(int bmap1, int bmap2, int x1, int y1, int x2, int y2, float pct)
2325 gr_set_bitmap(bmap1);
2328 gr_set_bitmap(bmap2);
2334 void gr_d3d_filter_set(int filter)
2339 void gr_d3d_set_clear_color(int r, int g, int b)
2341 gr_init_color(&gr_screen.current_clear_color, r, g, b);
2344 // JAS: Need to turn optimizations off or Alan's machine, with 3dfx direct3d hangs...
2345 #pragma optimize("",off)
2347 void d3d_detect_texture_origin_32()
2355 mprintf(( "Detecting uv type...\n" ));
2358 gr_init_alphacolor(&ac,255,255,255,255);
2360 memset( data, 0, 32*32 );
2361 data[15*32+15] = 14;
2363 test_bmp = bm_create( 8, 32, 32, data, BMP_AABITMAP );
2365 mprintf(( "Trial #1\n" ));
2366 D3d_rendition_uvs = 0;
2369 gr_set_color_fast(&ac);
2370 gr_set_bitmap( test_bmp );
2371 gr_aabitmap_ex(0, 0, 32, 32, 15, 15);
2374 d3d_get_pixel(0, 0, (ubyte*)&pix1a);
2375 d3d_get_pixel(1, 1, (ubyte*)&pix1b);
2381 mprintf(( "Trial #2\n" ));
2382 D3d_rendition_uvs = 1;
2385 gr_set_color_fast(&ac);
2386 gr_set_bitmap( test_bmp );
2387 gr_aabitmap_ex(0, 0, 32, 32, 15, 15);
2390 d3d_get_pixel(0, 0, (ubyte*)&pix2a);
2391 d3d_get_pixel(1, 1, (ubyte*)&pix2b);
2397 bm_release(test_bmp);
2399 mprintf(( "Pixel 1 = %x , %x\n", pix1a, pix1b ));
2400 mprintf(( "Pixel 2 = %x , %x\n", pix2a, pix2b ));
2402 if ( (pix1b!=0) || (pix2b!=0) ) {
2403 D3d_rendition_uvs = 1;
2405 D3d_rendition_uvs = 0;
2408 mprintf(( "Rendition uvs: %d\n", D3d_rendition_uvs ));
2411 void d3d_detect_texture_origin_16()
2416 ushort pix1a, pix2a;
2417 ushort pix1b, pix2b;
2419 mprintf(( "Detecting uv type...\n" ));
2422 gr_init_alphacolor(&ac,255,255,255,255);
2424 memset( data, 0, 32*32 );
2425 data[15*32+15] = 14;
2427 test_bmp = bm_create( 8, 32, 32, data, BMP_AABITMAP );
2429 mprintf(( "Trial #1\n" ));
2430 D3d_rendition_uvs = 0;
2433 gr_set_color_fast(&ac);
2434 gr_set_bitmap( test_bmp );
2435 gr_aabitmap_ex(0, 0, 32, 32, 15, 15);
2438 d3d_get_pixel(0, 0, (ubyte*)&pix1a);
2439 d3d_get_pixel(1, 1, (ubyte*)&pix1b);
2445 mprintf(( "Trial #2\n" ));
2446 D3d_rendition_uvs = 1;
2449 gr_set_color_fast(&ac);
2450 gr_set_bitmap( test_bmp );
2451 gr_aabitmap_ex(0, 0, 32, 32, 15, 15);
2454 d3d_get_pixel(0, 0, (ubyte*)&pix2a);
2455 d3d_get_pixel(1, 1, (ubyte*)&pix2b);
2461 bm_release(test_bmp);
2463 mprintf(( "Pixel 1 = %x , %x\n", pix1a, pix1b ));
2464 mprintf(( "Pixel 2 = %x , %x\n", pix2a, pix2b ));
2466 if ( (pix1b!=0) || (pix2b!=0) ) {
2467 D3d_rendition_uvs = 1;
2469 D3d_rendition_uvs = 0;
2472 mprintf(( "Rendition uvs: %d\n", D3d_rendition_uvs ));
2475 void gr_d3d_get_region(int front, int w, int h, ubyte *data)
2479 LPDIRECTDRAWSURFACE surf = front ? lpFrontBuffer : lpBackBuffer;
2481 memset( &ddsd, 0, sizeof( ddsd ) );
2482 ddsd.dwSize = sizeof( ddsd );
2484 // try and lock the buffer
2485 ddrval = surf->Lock( NULL, &ddsd, DDLOCK_WAIT | DDLOCK_READONLY, NULL );
2486 if ( ddrval != DD_OK ) {
2487 mprintf(( "Error locking surface for get_region, %s\n", d3d_error_string(ddrval) ));
2493 ubyte *rptr = (ubyte*)ddsd.lpSurface;
2495 for (int i=0; i<h; i++ ) {
2496 sptr = (ubyte*)&rptr[ i * ddsd.lPitch ];
2498 // don't think we need to swizzle here ...
2499 for(int j=0; j<w; j++ ) {
2500 memcpy(dptr, sptr, gr_screen.bytes_per_pixel);
2501 dptr += gr_screen.bytes_per_pixel;
2502 sptr += gr_screen.bytes_per_pixel;
2506 // Unlock the buffer
2507 surf->Unlock( NULL );
2510 extern float D3D_line_offset;
2512 void d3d_detect_line_offset_32()
2518 mprintf(( "Detecting line offset...\n" ));
2521 gr_init_alphacolor(&ac, 255,255, 255, 255);
2523 mprintf(( "Trial #1\n" ));
2524 D3D_line_offset = 0.0f;
2527 gr_set_color_fast(&ac);
2531 d3d_get_pixel(0, 0, (ubyte*)&pix1a);
2532 d3d_get_pixel(1, 1, (ubyte*)&pix1b);
2538 mprintf(( "Trial #2\n" ));
2539 D3D_line_offset = 0.5f;
2542 gr_set_color_fast(&ac);
2546 d3d_get_pixel(0, 0, (ubyte*)&pix2a);
2547 d3d_get_pixel(1, 1, (ubyte*)&pix2b);
2553 mprintf(( "Pixel 1 = %x , %x\n", pix1a, pix1b ));
2554 mprintf(( "Pixel 2 = %x , %x\n", pix2a, pix2b ));
2556 if ( (pix1a!=0) && (pix2a==0) ) {
2557 D3D_line_offset = 0.0f;
2558 } else if ( (pix1a==0) && (pix2a!=0) ) {
2559 D3D_line_offset = 0.5f;
2561 D3D_line_offset = 0.0f;
2564 mprintf(( "Line offset: %.1f\n", D3D_line_offset ));
2567 void d3d_detect_line_offset_16()
2570 ushort pix1a, pix2a;
2571 ushort pix1b, pix2b;
2573 mprintf(( "Detecting line offset...\n" ));
2576 gr_init_alphacolor(&ac, 255,255, 255, 255);
2578 mprintf(( "Trial #1\n" ));
2579 D3D_line_offset = 0.0f;
2582 gr_set_color_fast(&ac);
2586 d3d_get_pixel(0, 0, (ubyte*)&pix1a);
2587 d3d_get_pixel(1, 1, (ubyte*)&pix1b);
2593 mprintf(( "Trial #2\n" ));
2594 D3D_line_offset = 0.5f;
2597 gr_set_color_fast(&ac);
2601 d3d_get_pixel(0, 0, (ubyte*)&pix2a);
2602 d3d_get_pixel(1, 1, (ubyte*)&pix2b);
2608 mprintf(( "Pixel 1 = %x , %x\n", pix1a, pix1b ));
2609 mprintf(( "Pixel 2 = %x , %x\n", pix2a, pix2b ));
2611 if ( (pix1a!=0) && (pix2a==0) ) {
2612 D3D_line_offset = 0.0f;
2613 } else if ( (pix1a==0) && (pix2a!=0) ) {
2614 D3D_line_offset = 0.5f;
2616 D3D_line_offset = 0.0f;
2619 mprintf(( "Line offset: %.1f\n", D3D_line_offset ));
2622 #pragma optimize("",on)
2626 D3D_enabled = 1; // Tell Freespace code that we're using Direct3D.
2629 Assert( !D3D_inited );
2632 Bm_pixel_format = BM_PIXEL_FORMAT_D3D;
2635 D3D_window = Cmdline_window;
2639 gr_d3d_init_device(gr_screen.max_w, gr_screen.max_h);
2642 if(Cmdline_force_32bit){
2643 gr_screen.bits_per_pixel = 32;
2644 gr_screen.bytes_per_pixel = 4;
2645 gr_d3d_init_device(gr_screen.max_w, gr_screen.max_h);
2648 gr_screen.bits_per_pixel = 16;
2649 gr_screen.bytes_per_pixel = 2;
2650 gr_d3d_init_device(gr_screen.max_w, gr_screen.max_h);
2654 // determine 32 bit status
2655 D3D_32bit = gr_screen.bits_per_pixel == 32 ? 1 : 0;
2656 d3d_tcache_init(D3D_32bit ? 1 : 0);
2657 Gr_bitmap_poly = D3D_32bit ? 1 : 0;
2659 // did we initialize properly?
2665 if(os_config_read_uint(NULL, "DisableZbias", 0)){
2671 Gr_current_red = &Gr_red;
2672 Gr_current_blue = &Gr_blue;
2673 Gr_current_green = &Gr_green;
2674 Gr_current_alpha = &Gr_alpha;
2676 gr_screen.gf_flip = gr_d3d_flip;
2677 gr_screen.gf_flip_window = gr_d3d_flip_window;
2678 gr_screen.gf_set_clip = gr_d3d_set_clip;
2679 gr_screen.gf_reset_clip = gr_d3d_reset_clip;
2680 gr_screen.gf_set_font = grx_set_font;
2682 gr_screen.gf_get_color = gr_d3d_get_color;
2683 gr_screen.gf_init_color = gr_d3d_init_color;
2684 gr_screen.gf_set_color_fast = gr_d3d_set_color_fast;
2685 gr_screen.gf_set_color = gr_d3d_set_color;
2686 gr_screen.gf_init_color = gr_d3d_init_color;
2687 gr_screen.gf_init_alphacolor = gr_d3d_init_alphacolor;
2689 gr_screen.gf_set_bitmap = gr_d3d_set_bitmap;
2690 gr_screen.gf_create_shader = gr_d3d_create_shader;
2691 gr_screen.gf_set_shader = gr_d3d_set_shader;
2692 gr_screen.gf_clear = gr_d3d_clear;
2693 // gr_screen.gf_bitmap = gr_d3d_bitmap;
2694 // gr_screen.gf_bitmap_ex = gr_d3d_bitmap_ex;
2695 gr_screen.gf_aabitmap = gr_d3d_aabitmap;
2696 gr_screen.gf_aabitmap_ex = gr_d3d_aabitmap_ex;
2698 gr_screen.gf_rect = gr_d3d_rect;
2699 gr_screen.gf_shade = gr_d3d_shade;
2700 gr_screen.gf_string = gr_d3d_string;
2701 gr_screen.gf_circle = gr_d3d_circle;
2703 gr_screen.gf_line = gr_d3d_line;
2704 gr_screen.gf_aaline = gr_d3d_aaline;
2705 gr_screen.gf_pixel = gr_d3d_pixel;
2706 gr_screen.gf_scaler = gr_d3d_scaler;
2707 gr_screen.gf_aascaler = gr_d3d_aascaler;
2708 gr_screen.gf_tmapper = gr_d3d_tmapper;
2710 gr_screen.gf_gradient = gr_d3d_gradient;
2712 gr_screen.gf_set_palette = gr_d3d_set_palette;
2713 gr_screen.gf_print_screen = gr_d3d_print_screen;
2715 gr_screen.gf_fade_in = gr_d3d_fade_in;
2716 gr_screen.gf_fade_out = gr_d3d_fade_out;
2717 gr_screen.gf_flash = gr_d3d_flash;
2719 gr_screen.gf_zbuffer_get = gr_d3d_zbuffer_get;
2720 gr_screen.gf_zbuffer_set = gr_d3d_zbuffer_set;
2721 gr_screen.gf_zbuffer_clear = gr_d3d_zbuffer_clear;
2723 gr_screen.gf_save_screen = gr_d3d_save_screen;
2724 gr_screen.gf_restore_screen = gr_d3d_restore_screen;
2725 gr_screen.gf_free_screen = gr_d3d_free_screen;
2727 // Screen dumping stuff
2728 gr_screen.gf_dump_frame_start = gr_d3d_dump_frame_start;
2729 gr_screen.gf_dump_frame_stop = gr_d3d_dump_frame_stop;
2730 gr_screen.gf_dump_frame = gr_d3d_dump_frame;
2732 gr_screen.gf_set_gamma = gr_d3d_set_gamma;
2734 // Lock/unlock stuff
2735 gr_screen.gf_lock = gr_d3d_lock;
2736 gr_screen.gf_unlock = gr_d3d_unlock;
2739 gr_screen.gf_get_region = gr_d3d_get_region;
2742 gr_screen.gf_fog_set = gr_d3d_fog_set;
2745 gr_screen.gf_get_pixel = gr_d3d_get_pixel;
2748 gr_screen.gf_set_cull = gr_d3d_set_cull;
2751 gr_screen.gf_cross_fade = gr_d3d_cross_fade;
2754 gr_screen.gf_filter_set = gr_d3d_filter_set;
2757 gr_screen.gf_tcache_set = d3d_tcache_set;
2760 gr_screen.gf_set_clear_color = gr_d3d_set_clear_color;
2762 uint tmp = os_config_read_uint( NULL, "D3DTextureOrigin", 0xFFFF );
2764 if ( tmp != 0xFFFF ) {
2766 D3d_rendition_uvs = 1;
2768 D3d_rendition_uvs = 0;
2772 d3d_detect_texture_origin_32();
2774 d3d_detect_texture_origin_16();
2778 tmp = os_config_read_uint( NULL, "D3DLineOffset", 0xFFFF );
2780 if ( tmp != 0xFFFF ) {
2782 D3D_line_offset = 0.5f;
2784 D3D_line_offset = 0.0f;
2788 d3d_detect_line_offset_32();
2790 d3d_detect_line_offset_16();
2803 char* d3d_error_string(HRESULT error)
2808 return "No error.\0";
2809 case DDERR_ALREADYINITIALIZED:
2810 return "This object is already initialized.\0";
2811 case DDERR_BLTFASTCANTCLIP:
2812 return "Return if a clipper object is attached to the source surface passed into a BltFast call.\0";
2813 case DDERR_CANNOTATTACHSURFACE:
2814 return "This surface can not be attached to the requested surface.\0";
2815 case DDERR_CANNOTDETACHSURFACE:
2816 return "This surface can not be detached from the requested surface.\0";
2817 case DDERR_CANTCREATEDC:
2818 return "Windows can not create any more DCs.\0";
2819 case DDERR_CANTDUPLICATE:
2820 return "Can't duplicate primary & 3D surfaces, or surfaces that are implicitly created.\0";
2821 case DDERR_CLIPPERISUSINGHWND:
2822 return "An attempt was made to set a cliplist for a clipper object that is already monitoring an hwnd.\0";
2823 case DDERR_COLORKEYNOTSET:
2824 return "No src color key specified for this operation.\0";
2825 case DDERR_CURRENTLYNOTAVAIL:
2826 return "Support is currently not available.\0";
2827 case DDERR_DIRECTDRAWALREADYCREATED:
2828 return "A DirectDraw object representing this driver has already been created for this process.\0";
2829 case DDERR_EXCEPTION:
2830 return "An exception was encountered while performing the requested operation.\0";
2831 case DDERR_EXCLUSIVEMODEALREADYSET:
2832 return "An attempt was made to set the cooperative level when it was already set to exclusive.\0";
2834 return "Generic failure.\0";
2835 case DDERR_HEIGHTALIGN:
2836 return "Height of rectangle provided is not a multiple of reqd alignment.\0";
2837 case DDERR_HWNDALREADYSET:
2838 return "The CooperativeLevel HWND has already been set. It can not be reset while the process has surfaces or palettes created.\0";
2839 case DDERR_HWNDSUBCLASSED:
2840 return "HWND used by DirectDraw CooperativeLevel has been subclassed, this prevents DirectDraw from restoring state.\0";
2841 case DDERR_IMPLICITLYCREATED:
2842 return "This surface can not be restored because it is an implicitly created surface.\0";
2843 case DDERR_INCOMPATIBLEPRIMARY:
2844 return "Unable to match primary surface creation request with existing primary surface.\0";
2845 case DDERR_INVALIDCAPS:
2846 return "One or more of the caps bits passed to the callback are incorrect.\0";
2847 case DDERR_INVALIDCLIPLIST:
2848 return "DirectDraw does not support the provided cliplist.\0";
2849 case DDERR_INVALIDDIRECTDRAWGUID:
2850 return "The GUID passed to DirectDrawCreate is not a valid DirectDraw driver identifier.\0";
2851 case DDERR_INVALIDMODE:
2852 return "DirectDraw does not support the requested mode.\0";
2853 case DDERR_INVALIDOBJECT:
2854 return "DirectDraw received a pointer that was an invalid DIRECTDRAW object.\0";
2855 case DDERR_INVALIDPARAMS:
2856 return "One or more of the parameters passed to the function are incorrect.\0";
2857 case DDERR_INVALIDPIXELFORMAT:
2858 return "The pixel format was invalid as specified.\0";
2859 case DDERR_INVALIDPOSITION:
2860 return "Returned when the position of the overlay on the destination is no longer legal for that destination.\0";
2861 case DDERR_INVALIDRECT:
2862 return "Rectangle provided was invalid.\0";
2863 case DDERR_LOCKEDSURFACES:
2864 return "Operation could not be carried out because one or more surfaces are locked.\0";
2866 return "There is no 3D present.\0";
2867 case DDERR_NOALPHAHW:
2868 return "Operation could not be carried out because there is no alpha accleration hardware present or available.\0";
2870 return "No blitter hardware present.\0";
2871 case DDERR_NOCLIPLIST:
2872 return "No cliplist available.\0";
2873 case DDERR_NOCLIPPERATTACHED:
2874 return "No clipper object attached to surface object.\0";
2875 case DDERR_NOCOLORCONVHW:
2876 return "Operation could not be carried out because there is no color conversion hardware present or available.\0";
2877 case DDERR_NOCOLORKEY:
2878 return "Surface doesn't currently have a color key\0";
2879 case DDERR_NOCOLORKEYHW:
2880 return "Operation could not be carried out because there is no hardware support of the destination color key.\0";
2881 case DDERR_NOCOOPERATIVELEVELSET:
2882 return "Create function called without DirectDraw object method SetCooperativeLevel being called.\0";
2884 return "No DC was ever created for this surface.\0";
2885 case DDERR_NODDROPSHW:
2886 return "No DirectDraw ROP hardware.\0";
2887 case DDERR_NODIRECTDRAWHW:
2888 return "A hardware-only DirectDraw object creation was attempted but the driver did not support any hardware.\0";
2889 case DDERR_NOEMULATION:
2890 return "Software emulation not available.\0";
2891 case DDERR_NOEXCLUSIVEMODE:
2892 return "Operation requires the application to have exclusive mode but the application does not have exclusive mode.\0";
2893 case DDERR_NOFLIPHW:
2894 return "Flipping visible surfaces is not supported.\0";
2896 return "There is no GDI present.\0";
2898 return "Clipper notification requires an HWND or no HWND has previously been set as the CooperativeLevel HWND.\0";
2899 case DDERR_NOMIRRORHW:
2900 return "Operation could not be carried out because there is no hardware present or available.\0";
2901 case DDERR_NOOVERLAYDEST:
2902 return "Returned when GetOverlayPosition is called on an overlay that UpdateOverlay has never been called on to establish a destination.\0";
2903 case DDERR_NOOVERLAYHW:
2904 return "Operation could not be carried out because there is no overlay hardware present or available.\0";
2905 case DDERR_NOPALETTEATTACHED:
2906 return "No palette object attached to this surface.\0";
2907 case DDERR_NOPALETTEHW:
2908 return "No hardware support for 16 or 256 color palettes.\0";
2909 case DDERR_NORASTEROPHW:
2910 return "Operation could not be carried out because there is no appropriate raster op hardware present or available.\0";
2911 case DDERR_NOROTATIONHW:
2912 return "Operation could not be carried out because there is no rotation hardware present or available.\0";
2913 case DDERR_NOSTRETCHHW:
2914 return "Operation could not be carried out because there is no hardware support for stretching.\0";
2915 case DDERR_NOT4BITCOLOR:
2916 return "DirectDrawSurface is not in 4 bit color palette and the requested operation requires 4 bit color palette.\0";
2917 case DDERR_NOT4BITCOLORINDEX:
2918 return "DirectDrawSurface is not in 4 bit color index palette and the requested operation requires 4 bit color index palette.\0";
2919 case DDERR_NOT8BITCOLOR:
2920 return "DirectDrawSurface is not in 8 bit color mode and the requested operation requires 8 bit color.\0";
2921 case DDERR_NOTAOVERLAYSURFACE:
2922 return "Returned when an overlay member is called for a non-overlay surface.\0";
2923 case DDERR_NOTEXTUREHW:
2924 return "Operation could not be carried out because there is no texture mapping hardware present or available.\0";
2925 case DDERR_NOTFLIPPABLE:
2926 return "An attempt has been made to flip a surface that is not flippable.\0";
2927 case DDERR_NOTFOUND:
2928 return "Requested item was not found.\0";
2929 case DDERR_NOTLOCKED:
2930 return "Surface was not locked. An attempt to unlock a surface that was not locked at all, or by this process, has been attempted.\0";
2931 case DDERR_NOTPALETTIZED:
2932 return "The surface being used is not a palette-based surface.\0";
2933 case DDERR_NOVSYNCHW:
2934 return "Operation could not be carried out because there is no hardware support for vertical blank synchronized operations.\0";
2935 case DDERR_NOZBUFFERHW:
2936 return "Operation could not be carried out because there is no hardware support for zbuffer blitting.\0";
2937 case DDERR_NOZOVERLAYHW:
2938 return "Overlay surfaces could not be z layered based on their BltOrder because the hardware does not support z layering of overlays.\0";
2939 case DDERR_OUTOFCAPS:
2940 return "The hardware needed for the requested operation has already been allocated.\0";
2941 case DDERR_OUTOFMEMORY:
2942 return "DirectDraw does not have enough memory to perform the operation.\0";
2943 case DDERR_OUTOFVIDEOMEMORY:
2944 return "DirectDraw does not have enough memory to perform the operation.\0";
2945 case DDERR_OVERLAYCANTCLIP:
2946 return "The hardware does not support clipped overlays.\0";
2947 case DDERR_OVERLAYCOLORKEYONLYONEACTIVE:
2948 return "Can only have ony color key active at one time for overlays.\0";
2949 case DDERR_OVERLAYNOTVISIBLE:
2950 return "Returned when GetOverlayPosition is called on a hidden overlay.\0";
2951 case DDERR_PALETTEBUSY:
2952 return "Access to this palette is being refused because the palette is already locked by another thread.\0";
2953 case DDERR_PRIMARYSURFACEALREADYEXISTS:
2954 return "This process already has created a primary surface.\0";
2955 case DDERR_REGIONTOOSMALL:
2956 return "Region passed to Clipper::GetClipList is too small.\0";
2957 case DDERR_SURFACEALREADYATTACHED:
2958 return "This surface is already attached to the surface it is being attached to.\0";
2959 case DDERR_SURFACEALREADYDEPENDENT:
2960 return "This surface is already a dependency of the surface it is being made a dependency of.\0";
2961 case DDERR_SURFACEBUSY:
2962 return "Access to this surface is being refused because the surface is already locked by another thread.\0";
2963 case DDERR_SURFACEISOBSCURED:
2964 return "Access to surface refused because the surface is obscured.\0";
2965 case DDERR_SURFACELOST:
2966 return "Access to this surface is being refused because the surface memory is gone. The DirectDrawSurface object representing this surface should have Restore called on it.\0";
2967 case DDERR_SURFACENOTATTACHED:
2968 return "The requested surface is not attached.\0";
2969 case DDERR_TOOBIGHEIGHT:
2970 return "Height requested by DirectDraw is too large.\0";
2971 case DDERR_TOOBIGSIZE:
2972 return "Size requested by DirectDraw is too large, but the individual height and width are OK.\0";
2973 case DDERR_TOOBIGWIDTH:
2974 return "Width requested by DirectDraw is too large.\0";
2975 case DDERR_UNSUPPORTED:
2976 return "Action not supported.\0";
2977 case DDERR_UNSUPPORTEDFORMAT:
2978 return "FOURCC format requested is unsupported by DirectDraw.\0";
2979 case DDERR_UNSUPPORTEDMASK:
2980 return "Bitmask in the pixel format requested is unsupported by DirectDraw.\0";
2981 case DDERR_VERTICALBLANKINPROGRESS:
2982 return "Vertical blank is in progress.\0";
2983 case DDERR_WASSTILLDRAWING:
2984 return "Informs DirectDraw that the previous Blt which is transfering information to or from this Surface is incomplete.\0";
2985 case DDERR_WRONGMODE:
2986 return "This surface can not be restored because it was created in a different mode.\0";
2988 return "Rectangle provided was not horizontally aligned on required boundary.\0";
2989 case D3DERR_BADMAJORVERSION:
2990 return "D3DERR_BADMAJORVERSION\0";
2991 case D3DERR_BADMINORVERSION:
2992 return "D3DERR_BADMINORVERSION\0";
2993 case D3DERR_EXECUTE_LOCKED:
2994 return "D3DERR_EXECUTE_LOCKED\0";
2995 case D3DERR_EXECUTE_NOT_LOCKED:
2996 return "D3DERR_EXECUTE_NOT_LOCKED\0";
2997 case D3DERR_EXECUTE_CREATE_FAILED:
2998 return "D3DERR_EXECUTE_CREATE_FAILED\0";
2999 case D3DERR_EXECUTE_DESTROY_FAILED:
3000 return "D3DERR_EXECUTE_DESTROY_FAILED\0";
3001 case D3DERR_EXECUTE_LOCK_FAILED:
3002 return "D3DERR_EXECUTE_LOCK_FAILED\0";
3003 case D3DERR_EXECUTE_UNLOCK_FAILED:
3004 return "D3DERR_EXECUTE_UNLOCK_FAILED\0";
3005 case D3DERR_EXECUTE_FAILED:
3006 return "D3DERR_EXECUTE_FAILED\0";
3007 case D3DERR_EXECUTE_CLIPPED_FAILED:
3008 return "D3DERR_EXECUTE_CLIPPED_FAILED\0";
3009 case D3DERR_TEXTURE_NO_SUPPORT:
3010 return "D3DERR_TEXTURE_NO_SUPPORT\0";
3011 case D3DERR_TEXTURE_NOT_LOCKED:
3012 return "D3DERR_TEXTURE_NOT_LOCKED\0";
3013 case D3DERR_TEXTURE_LOCKED:
3014 return "D3DERR_TEXTURELOCKED\0";
3015 case D3DERR_TEXTURE_CREATE_FAILED:
3016 return "D3DERR_TEXTURE_CREATE_FAILED\0";
3017 case D3DERR_TEXTURE_DESTROY_FAILED:
3018 return "D3DERR_TEXTURE_DESTROY_FAILED\0";
3019 case D3DERR_TEXTURE_LOCK_FAILED:
3020 return "D3DERR_TEXTURE_LOCK_FAILED\0";
3021 case D3DERR_TEXTURE_UNLOCK_FAILED:
3022 return "D3DERR_TEXTURE_UNLOCK_FAILED\0";
3023 case D3DERR_TEXTURE_LOAD_FAILED:
3024 return "D3DERR_TEXTURE_LOAD_FAILED\0";
3025 case D3DERR_MATRIX_CREATE_FAILED:
3026 return "D3DERR_MATRIX_CREATE_FAILED\0";
3027 case D3DERR_MATRIX_DESTROY_FAILED:
3028 return "D3DERR_MATRIX_DESTROY_FAILED\0";
3029 case D3DERR_MATRIX_SETDATA_FAILED:
3030 return "D3DERR_MATRIX_SETDATA_FAILED\0";
3031 case D3DERR_SETVIEWPORTDATA_FAILED:
3032 return "D3DERR_SETVIEWPORTDATA_FAILED\0";
3033 case D3DERR_MATERIAL_CREATE_FAILED:
3034 return "D3DERR_MATERIAL_CREATE_FAILED\0";
3035 case D3DERR_MATERIAL_DESTROY_FAILED:
3036 return "D3DERR_MATERIAL_DESTROY_FAILED\0";
3037 case D3DERR_MATERIAL_SETDATA_FAILED:
3038 return "D3DERR_MATERIAL_SETDATA_FAILED\0";
3039 case D3DERR_LIGHT_SET_FAILED:
3040 return "D3DERR_LIGHT_SET_FAILED\0";
3042 return "Unrecognized error value.\0";