X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=platform%2Fwin32%2FGenaDrive%2FDirect.cpp;h=b8112d3be414b76865f02bd7b68438120b6bc2c5;hb=eacee137eed2f00111e03544f6b1e43d20a7403b;hp=b6f4285bed6d26f6e54e52da229b560e7d2ba48b;hpb=4b2b67ebc7fc6205c6589243b9c9a0a172a97ebb;p=picodrive.git diff --git a/platform/win32/GenaDrive/Direct.cpp b/platform/win32/GenaDrive/Direct.cpp index b6f4285..b8112d3 100644 --- a/platform/win32/GenaDrive/Direct.cpp +++ b/platform/win32/GenaDrive/Direct.cpp @@ -1,6 +1,7 @@ - #include "app.h" +#ifdef USE_D3D +// d3d static IDirect3D8 *Direct3D=NULL; IDirect3DDevice8 *Device=NULL; IDirect3DSurface8 *DirectBack=NULL; // Back Buffer @@ -16,12 +17,163 @@ struct CustomVertex #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1) static CustomVertex VertexList[4]; +#endif + +// ddraw +#include + +LPDIRECTDRAW7 m_pDD = NULL; +LPDIRECTDRAWSURFACE7 m_pddsFrontBuffer = NULL; +LPDIRECTDRAWSURFACE7 m_pddsBackBuffer = NULL; + +// quick and dirty stuff.. +static void DirectExitDDraw() +{ + RELEASE(m_pddsBackBuffer); + RELEASE(m_pddsFrontBuffer); + RELEASE(m_pDD); +} + +static int DirectDrawInit() +{ + HRESULT ret; + LPDIRECTDRAWCLIPPER pcClipper = NULL; + DDSURFACEDESC2 ddsd; + + ret = DirectDrawCreateEx(NULL, (VOID**)&m_pDD, IID_IDirectDraw7, NULL); + if (ret) { LOGFAIL(); return 1; } + + // Set cooperative level + ret = m_pDD->SetCooperativeLevel( FrameWnd, DDSCL_NORMAL ); + if (ret) { LOGFAIL(); goto fail; } + + // Create the primary surface + ZeroMemory( &ddsd, sizeof( ddsd ) ); + ddsd.dwSize = sizeof( ddsd ); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + + ret = m_pDD->CreateSurface( &ddsd, &m_pddsFrontBuffer, NULL ); + if (ret) { LOGFAIL(); goto fail; } + + // Create the backbuffer surface + ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; + ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY; + ddsd.dwWidth = EmuWidth; + ddsd.dwHeight = EmuHeight; + + ret = m_pDD->CreateSurface( &ddsd, &m_pddsBackBuffer, NULL ); + if (ret) { LOGFAIL(); goto fail; } + + // clipper + ret = m_pDD->CreateClipper( 0, &pcClipper, NULL ); + if (ret) { LOGFAIL(); goto fail; } + + ret = pcClipper->SetHWnd( 0, FrameWnd ); + if (ret) { LOGFAIL(); goto fail; } + + ret = m_pddsFrontBuffer->SetClipper( pcClipper ); + if (ret) { LOGFAIL(); goto fail; } + + RELEASE(pcClipper); + return 0; + +fail: + RELEASE(pcClipper); + DirectExitDDraw(); + return 1; +} + +static int DirectScreenDDraw() +{ + DDSURFACEDESC2 sd; + unsigned short *ps=EmuScreen; + int ret, x, y; + + memset(&sd, 0, sizeof(sd)); + sd.dwSize = sizeof(sd); + ret = m_pddsBackBuffer->Lock(NULL, &sd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT|DDLOCK_WRITEONLY, NULL); + if (ret) { LOGFAIL(); return 1; } + + //dprintf2("w: %i h: %i pi: %i pf: %i\n", sd.dwWidth, sd.dwHeight, sd.lPitch, sd.ddpfPixelFormat.dwRGBBitCount); + + if (sd.ddpfPixelFormat.dwRGBBitCount == 32) + { + int *dst = (int *)sd.lpSurface; + for (y = 0; y < EmuHeight; y++) + { + for (x = 0; x < EmuWidth; x++) + { + int s = *ps++; + dst[x] = ((s&0xf800)<<8) | ((s&0x07e0)<<5) | ((s&0x001f)<<3); + } + dst = (int *)((char *)dst + sd.lPitch); + } + } + else if (sd.ddpfPixelFormat.dwRGBBitCount == 24) /* wine uses this for me */ + { + void *dst = sd.lpSurface; + for (y = 0; y < EmuHeight; y++) + { + unsigned char *dst1 = (unsigned char *) dst; + for (x = 0; x < EmuWidth; x++, dst1 += 3) + { + int s = *ps++; + dst1[2] = (s&0xf800)>>8; dst1[1] = (s&0x07e0)>>3; dst1[0] = s<<3; // BGR + } + dst = (void *)((char *)dst + sd.lPitch); + } + } + else if (sd.ddpfPixelFormat.dwRGBBitCount == 16) + { + unsigned short *dst = (unsigned short *)sd.lpSurface; + for (y = 0; y < EmuHeight; y++) + { + memcpy(dst, ps, EmuWidth*2); + ps += EmuWidth; + dst = (unsigned short *)((char *)dst + sd.lPitch); + } + } + else + { + LOGFAIL(); + } + + ret = m_pddsBackBuffer->Unlock(NULL); + if (ret) { LOGFAIL(); return 1; } + return 0; +} + +static int DirectClearDDraw(unsigned int colour) +{ + int ret; + DDBLTFX ddbltfx; + ZeroMemory( &ddbltfx, sizeof(ddbltfx) ); + ddbltfx.dwSize = sizeof(ddbltfx); + ddbltfx.dwFillColor = colour; + + if (m_pddsBackBuffer != NULL) + ret = m_pddsBackBuffer->Blt( NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx ); + if (ret) { LOGFAIL(); return 1; } + return 0; +} + +static int DirectPresentDDraw() +{ + int ret = m_pddsFrontBuffer->Blt(&FrameRectMy, m_pddsBackBuffer, &EmuScreenRect, DDBLT_WAIT, NULL); + if (ret) { LOGFAIL(); return 1; } + return 0; +} + + +/* D3D */ int DirectInit() { - D3DPRESENT_PARAMETERS d3dpp; +#if USE_D3D + D3DPRESENT_PARAMETERS d3dpp; D3DDISPLAYMODE mode; - int i=0,ret=0; + int i,u,ret=0; memset(&d3dpp,0,sizeof(d3dpp)); memset(&mode,0,sizeof(mode)); @@ -33,6 +185,7 @@ int DirectInit() d3dpp.BackBufferHeight=MainHeight; d3dpp.BackBufferCount =1; d3dpp.SwapEffect=D3DSWAPEFFECT_DISCARD; + d3dpp.MultiSampleType =D3DMULTISAMPLE_NONE; #ifdef _XBOX d3dpp.BackBufferFormat=D3DFMT_X8R8G8B8; @@ -41,39 +194,50 @@ int DirectInit() Direct3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&mode); d3dpp.BackBufferFormat=mode.Format; d3dpp.Windowed=1; + d3dpp.hDeviceWindow=FrameWnd; #endif // Try to create a device with hardware vertex processing: - for (i=0;i<4;i++) + for (i=0;i<3;i++) { int behave=D3DCREATE_HARDWARE_VERTEXPROCESSING; -#ifdef _XBOX - if (i==1) - { - // If 60Hz didn't work, try PAL 50Hz instead: - d3dpp.FullScreen_RefreshRateInHz=0; - d3dpp.BackBufferHeight=MainHeight=576; - } -#endif - // Try software vertex processing: - if (i==2) behave=D3DCREATE_MIXED_VERTEXPROCESSING; - if (i==3) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING; + if (i==1) behave=D3DCREATE_MIXED_VERTEXPROCESSING; + if (i==2) behave=D3DCREATE_SOFTWARE_VERTEXPROCESSING; - Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd,behave,&d3dpp,&Device); + Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,FrameWnd, + behave|D3DCREATE_MULTITHREADED,&d3dpp,&Device); if (Device) break; } - if (Device==NULL) return 1; + if (Device==NULL) + { +#if 0 + // try ref + Direct3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_REF,FrameWnd, + D3DCREATE_SOFTWARE_VERTEXPROCESSING|D3DCREATE_MULTITHREADED,&d3dpp,&Device); + if (Device==NULL) goto fail0; + HMODULE test = LoadLibrary("d3d8d.dll"); + if (test != NULL) FreeLibrary(test); + else { + error("Sorry, but this program requires Direct3D with hardware acceleration.\n\n" + "You can try using Direct3D software emulation, but you have to install " + "DirectX SDK for it to work\n(it seems to be missing now)."); + goto fail1; + } +#else + goto fail1; +#endif + } Device->GetBackBuffer(0,D3DBACKBUFFER_TYPE_MONO,&DirectBack); - if (DirectBack==NULL) return 1; + if (DirectBack==NULL) goto fail1; Device->CreateVertexBuffer(sizeof(VertexList),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&VertexBuffer); - if (VertexBuffer==NULL) return 1; + if (VertexBuffer==NULL) goto fail2; - ret=TexScreenInit(); if (ret) return 1; + ret=TexScreenInit(); if (ret) goto fail3; //FontInit(); @@ -82,21 +246,63 @@ int DirectInit() // Set up texture modes: Device->SetTextureStageState(0,D3DTSS_ADDRESSU,D3DTADDRESS_CLAMP); Device->SetTextureStageState(0,D3DTSS_ADDRESSV,D3DTADDRESS_CLAMP); + return 0; + +fail3: + RELEASE(VertexBuffer) +fail2: + RELEASE(DirectBack) +fail1: + RELEASE(Device) +fail0: + RELEASE(Direct3D) + + // error("Failed to use Direct3D, trying DirectDraw.."); +#endif + // try DirectDraw + return DirectDrawInit(); } void DirectExit() { - //FontExit(); - //TexScreenExit(); +#ifdef USE_D3D + TexScreenExit(); + // d3d RELEASE(VertexBuffer) RELEASE(DirectBack) RELEASE(Device) RELEASE(Direct3D) +#endif + DirectExitDDraw(); +} + +int DirectClear(unsigned int colour) +{ +#ifdef USE_D3D + if (Device != NULL) { + Device->Clear(0,NULL,D3DCLEAR_TARGET,colour,1.0f,0); + return 0; + } +#endif + + return DirectClearDDraw(colour); } +int DirectPresent() +{ +#ifdef USE_D3D + if (Device != NULL) { + Device->Present(NULL,NULL,NULL,NULL); + return 0; + } +#endif + + return DirectPresentDDraw(); +} +#ifdef USE_D3D static int MakeVertexList() { struct CustomVertex *vert=NULL,*pv=NULL; @@ -138,18 +344,6 @@ static int MakeVertexList() return 0; } -int DirectClear(unsigned int colour) -{ - Device->Clear(0,NULL,D3DCLEAR_TARGET,colour,1.0f,0); - return 0; -} - -int DirectPresent() -{ - Device->Present(NULL,NULL,NULL,NULL); - return 0; -} - static int SetupMatrices() { D3DXVECTOR3 eye ( 0.0f, 0.0f, 0.0f ); @@ -159,7 +353,7 @@ static int SetupMatrices() float nudgex=0.0f,nudgey=0.0f; memset(&mat,0,sizeof(mat)); - + mat.m[0][0]=mat.m[1][1]=mat.m[2][2]=mat.m[3][3]=1.0f; Device->SetTransform(D3DTS_WORLD,&mat); @@ -184,12 +378,17 @@ static int SetupMatrices() int DirectScreen() { unsigned char *lock=NULL; + int ret; + + if (Device == NULL) + return DirectScreenDDraw(); // Copy the screen to the screen texture: #ifdef _XBOX TexScreenSwizzle(); #else - TexScreenLinear(); + ret=TexScreenLinear(); + if (ret) dprintf2("TexScreenLinear failed\n"); #endif SetupMatrices(); @@ -197,14 +396,24 @@ int DirectScreen() MakeVertexList(); // Copy vertices in: - VertexBuffer->Lock(0,sizeof(VertexList),&lock,0); if (lock==NULL) return 1; + VertexBuffer->Lock(0,sizeof(VertexList),&lock,0); + if (lock==NULL) { dprintf2("VertexBuffer->Lock failed\n"); return 1; } memcpy(lock,VertexList,sizeof(VertexList)); VertexBuffer->Unlock(); + Device->BeginScene(); Device->SetTexture(0,TexScreen); Device->SetStreamSource(0,VertexBuffer,sizeof(CustomVertex)); Device->SetVertexShader(D3DFVF_CUSTOMVERTEX); Device->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2); - + Device->EndScene(); + return 0; } +#else +int DirectScreen() +{ + return DirectScreenDDraw(); +} +#endif +