From 260713ec34aea1ade639f543676798017b2527fb Mon Sep 17 00:00:00 2001 From: Spoike Date: Wed, 4 Nov 2009 21:44:48 +0000 Subject: [PATCH] Still messy. git-svn-id: https://svn.code.sf.net/p/fteqw/code/branches/wip@3402 fc73d0e0-1445-4013-8a0c-d673dee63da5 --- engine/d3d/d3d_backend.c | 159 +++++ engine/d3d/d3d_image.c | 286 +++++++++ engine/d3d/vid_d3d.c | 1258 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 1703 insertions(+) create mode 100644 engine/d3d/d3d_backend.c create mode 100644 engine/d3d/d3d_image.c create mode 100644 engine/d3d/vid_d3d.c diff --git a/engine/d3d/d3d_backend.c b/engine/d3d/d3d_backend.c new file mode 100644 index 00000000..d8008150 --- /dev/null +++ b/engine/d3d/d3d_backend.c @@ -0,0 +1,159 @@ +#include "quakedef.h" +#include "shader.h" + +#include +LPDIRECT3DDEVICE9 pD3DDev9; + +extern int be_maxpasses; + +void BE_Init(void) +{ + be_maxpasses = 1; +} + +static D3DBE_ApplyShaderBits(unsigned int bits) +{ + if (bits & SBITS_BLEND_BITS) + { + int src; + int dst; + + switch(bits & SBITS_SRCBLEND_BITS) + { + case SBITS_SRCBLEND_ZERO: dst = D3DBLEND_ZERO; break; + case SBITS_SRCBLEND_ONE: dst = D3DBLEND_ONE; break; + case SBITS_SRCBLEND_DST_COLOR: src = D3DBLEND_DESTCOLOR; break; + case SBITS_SRCBLEND_ONE_MINUS_DST_COLOR: src = D3DBLEND_INVDESTCOLOR; break; + case SBITS_SRCBLEND_SRC_ALPHA: src = D3DBLEND_SRCALPHA; break; + case SBITS_SRCBLEND_ONE_MINUS_SRC_ALPHA: src = D3DBLEND_INVSRCALPHA; break; + case SBITS_SRCBLEND_DST_ALPHA: src = D3DBLEND_DESTALPHA; break; + case SBITS_SRCBLEND_ONE_MINUS_DST_ALPHA: src = D3DBLEND_INVDESTALPHA; break; + case SBITS_SRCBLEND_ALPHA_SATURATE: src = D3DBLEND_SRCALPHASAT; break; + } + switch(bits & SBITS_DSTBLEND_BITS) + { + case SBITS_DSTBLEND_ZERO: dst = D3DBLEND_ZERO; break; + case SBITS_DSTBLEND_ONE: dst = D3DBLEND_ONE; break; + case SBITS_DSTBLEND_SRC_ALPHA: dst = D3DBLEND_SRCALPHA; break; + case SBITS_DSTBLEND_ONE_MINUS_SRC_ALPHA: dst = D3DBLEND_INVSRCALPHA; break; + case SBITS_DSTBLEND_DST_ALPHA: dst = D3DBLEND_DESTALPHA; break; + case SBITS_DSTBLEND_ONE_MINUS_DST_ALPHA: dst = D3DBLEND_INVDESTALPHA; break; + case SBITS_DSTBLEND_SRC_COLOR: dst = D3DBLEND_SRCCOLOR; break; + case SBITS_DSTBLEND_ONE_MINUS_SRC_COLOR: dst = D3DBLEND_INVSRCCOLOR; break; + } + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHABLENDENABLE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_SRCBLEND, src); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_DESTBLEND, dst); + } + else + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHABLENDENABLE, FALSE); + + switch(bits & SBITS_ATEST_BITS) + { + case SBITS_ATEST_NONE: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHATESTENABLE, FALSE); +// IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAREF, 0); +// IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAFUNC, 0); + break; + case SBITS_ATEST_GT0: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHATESTENABLE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAREF, 0); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAFUNC, D3DCMP_GREATER); + break; + case SBITS_ATEST_LT128: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHATESTENABLE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAREF, 128); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAFUNC, D3DCMP_LESS); + break; + case SBITS_ATEST_GE128: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHATESTENABLE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAREF, 128); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); + break; + } + + if(bits & SBITS_MISC_DEPTHWRITE) + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZWRITEENABLE, TRUE); + else + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZWRITEENABLE, FALSE); + + if(bits & SBITS_MISC_NODEPTHTEST) + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZENABLE, FALSE); + else + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZENABLE, TRUE); + + switch(bits & (SBITS_MISC_DEPTHEQUALONLY|SBITS_MISC_DEPTHCLOSERONLY)) + { + default: + case 0: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZFUNC, D3DCMP_LESSEQUAL); + break; + case SBITS_MISC_DEPTHEQUALONLY: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZFUNC, D3DCMP_EQUAL); + break; + case SBITS_MISC_DEPTHCLOSERONLY: + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZFUNC, D3DCMP_LESS); + break; + } +} + +unsigned int workingbuffer[65536]; +void BE_DrawMeshChain(shader_t *shader, mesh_t *meshchain, vbo_t *vbo, texnums_t *texnums) +{ + float *xyz; + unsigned int *colour; + float *st[1]; + unsigned int fmt = 0; + unsigned int stride; + int i; + + if (!shader) + return; + if (!texnums) + return; + + stride = 0; +memset(workingbuffer, 0, sizeof(workingbuffer)); + xyz = (float*)(workingbuffer+stride); + stride += 3; + fmt |= D3DFVF_XYZ; + + colour = (unsigned int*)(workingbuffer+stride); + stride += 1; + fmt |= D3DFVF_DIFFUSE; + + st[0] = (float*)(workingbuffer+stride); + stride += 2; + fmt |= D3DFVF_TEX1; + + D3DBE_ApplyShaderBits(shader->passes[0].shaderbits); + + IDirect3DDevice9_SetFVF(pD3DDev9, fmt); + + switch(shader->passes[0].texgen) + { + case T_GEN_DIFFUSE: + IDirect3DDevice9_SetTexture (pD3DDev9, 0, texnums->base.ptr); + break; + case T_GEN_SINGLEMAP: + IDirect3DDevice9_SetTexture (pD3DDev9, 0, shader->passes[0].anim_frames[0].ptr); + break; + } + + /*I'm guessing I ought to create a temp vertex buffer for this*/ + for (i = 0; i < meshchain->numvertexes; i++) + { + VectorCopy(meshchain->xyz_array[i], (xyz+stride*i)); + *(colour+stride*i) = 0xffffffff;//*(unsigned int*)meshchain->colors4b_array[i]; + Vector2Copy(meshchain->st_array[i], (st[0]+stride*i)); + } + IDirect3DDevice9_DrawIndexedPrimitiveUP(pD3DDev9, D3DPT_TRIANGLELIST, 0, meshchain->numvertexes, meshchain->numindexes/3, meshchain->indexes, D3DFMT_QINDEX, workingbuffer, stride*4); +} + +void BE_SelectMode(backendmode_t mode, unsigned int flags) +{ +} + +void BE_ClearVBO(vbo_t *vbo) +{ +} \ No newline at end of file diff --git a/engine/d3d/d3d_image.c b/engine/d3d/d3d_image.c new file mode 100644 index 00000000..5b837436 --- /dev/null +++ b/engine/d3d/d3d_image.c @@ -0,0 +1,286 @@ +#include "quakedef.h" + +#include +LPDIRECT3DDEVICE9 pD3DDev9; + + + +extern cvar_t gl_picmip; +extern cvar_t gl_picmip2d; + +texid_t D3D_AllocNewTexture(int width, int height) +{ + return r_nulltex; +} + + + + +static void D3D9_RoundDimensions(int *scaled_width, int *scaled_height, qboolean mipmap) +{ +// if (gl_config.arb_texture_non_power_of_two) //NPOT is a simple extension that relaxes errors. +// { +// TRACE(("dbg: GL_RoundDimensions: GL_ARB_texture_non_power_of_two\n")); +// } +// else + { + int width = *scaled_width; + int height = *scaled_height; + for (*scaled_width = 1 ; *scaled_width < width ; *scaled_width<<=1) + ; + for (*scaled_height = 1 ; *scaled_height < height ; *scaled_height<<=1) + ; + } + + if (mipmap) + { + TRACE(("dbg: GL_RoundDimensions: %i\n", gl_picmip.ival)); + *scaled_width >>= gl_picmip.ival; + *scaled_height >>= gl_picmip.ival; + } + else + { + *scaled_width >>= gl_picmip2d.ival; + *scaled_height >>= gl_picmip2d.ival; + } + + TRACE(("dbg: GL_RoundDimensions: %i\n", gl_max_size.ival)); + if (gl_max_size.ival) + { + if (*scaled_width > gl_max_size.ival) + *scaled_width = gl_max_size.ival; + if (*scaled_height > gl_max_size.ival) + *scaled_height = gl_max_size.ival; + } + + if (*scaled_width < 1) + *scaled_width = 1; + if (*scaled_height < 1) + *scaled_height = 1; +} + +static void Upload_Texture_32(LPDIRECT3DTEXTURE9 tex, unsigned int *data, int width, int height) +{ + int x, y; + unsigned int *dest; + unsigned char swapbuf[4]; + unsigned char swapbuf2[4]; + D3DLOCKED_RECT lock; + + D3DSURFACE_DESC desc; + IDirect3DTexture9_GetLevelDesc(tex, 0, &desc); + + IDirect3DTexture9_LockRect(tex, 0, &lock, NULL, DDLOCK_NOSYSLOCK|D3DLOCK_READONLY); + + if (width == desc.Width && height == desc.Height) + { +// if (desc.lPitch == twidth*4) +// { +// memcpy(desc.lpSurface, data, width*height*4); +// } +// else + { + for (y = 0; y < height; y++) + { + dest = (unsigned int *)((char *)lock.pBits + lock.Pitch*y); + for (x = 0; x < width; x++) + { + *(unsigned int*)swapbuf2 = *(unsigned int*)swapbuf = data[x]; + swapbuf[0] = swapbuf2[2]; + swapbuf[2] = swapbuf2[0]; + dest[x] = *(unsigned int*)swapbuf; + } + data += width; + } + } + } + else + { + int x, y; + int iny; + unsigned int *row, *inrow; + + for (y = 0; y < desc.Height; y++) + { + row = (unsigned int*)((char *)lock.pBits + lock.Pitch*y); + iny = (y * height) / desc.Height; + inrow = data + width*iny; + for (x = 0; x < desc.Width; x++) + { + *(unsigned int*)swapbuf2 = *(unsigned int*)swapbuf = inrow[(x * width)/desc.Width]; + swapbuf[0] = swapbuf2[2]; + swapbuf[2] = swapbuf2[0]; + row[x] = *(unsigned int*)swapbuf; + } + } + + + + //mimic opengl and draw it white +// memset(desc.lpSurface, 255, twidth*theight*4); + } + + IDirect3DTexture9_UnlockRect(tex, 0); +} + +//create a basic shader from a 32bit image +static LPDIRECT3DBASETEXTURE9 D3D9_LoadTexture_32(char *name, unsigned int *data, int width, int height, int flags) +{ + int nwidth, nheight; + + LPDIRECT3DTEXTURE9 newsurf; +/* + if (!(flags & TF_MANDATORY)) + { + Con_Printf("Texture upload missing flags\n"); + return NULL; + } +*/ + + nwidth = width; + nheight = height; + D3D9_RoundDimensions(&nwidth, &nheight, !(flags & IF_NOMIPMAP)); + + IDirect3DDevice9_CreateTexture(pD3DDev9, nwidth, nheight, 0, ((flags & IF_NOMIPMAP)?0:D3DUSAGE_AUTOGENMIPMAP), D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &newsurf, NULL); + + if (!newsurf) + return NULL; + + Upload_Texture_32(newsurf, data, width, height); + + if (!(flags & IF_NOMIPMAP)) + IDirect3DBaseTexture9_GenerateMipSubLevels(newsurf); + + return (LPDIRECT3DBASETEXTURE9)newsurf; +} + +static LPDIRECT3DBASETEXTURE9 D3D9_LoadTexture_8(char *name, unsigned char *data, int width, int height, int flags, enum uploadfmt fmt) +{ + static unsigned trans[1024*1024]; + int i, s; + qboolean noalpha; + int p; + + if (width*height > 1024*1024) + Sys_Error("GL_Upload8: image too big (%i*%i)", width, height); + + s = width*height; + // if there are no transparent pixels, make it a 3 component + // texture even if it was specified as otherwise + if ((fmt!=TF_SOLID8) && !(flags & IF_NOALPHA)) + { + noalpha = true; + for (i=0 ; i>4]] & 0x00ffffff; + trans[i] |= ( int )ColorPercent[p&15] << 24; + //trans[i] = 0x7fff0000; + } + break; +*/ + } + } + else + { + for (i=(s&~3)-4 ; i>=0 ; i-=4) + { + trans[i] = d_8to24rgbtable[data[i]]; + trans[i+1] = d_8to24rgbtable[data[i+1]]; + trans[i+2] = d_8to24rgbtable[data[i+2]]; + trans[i+3] = d_8to24rgbtable[data[i+3]]; + } + for (i=s&~3 ; i 0) + { + Key_Event(K_MWHEELUP, 0, true); + Key_Event(K_MWHEELUP, 0, false); + } + else + { + Key_Event(K_MWHEELDOWN, 0, true); + Key_Event(K_MWHEELDOWN, 0, false); + } + } + break; + + case WM_INPUT: + // raw input handling + IN_RawInput_MouseRead((HANDLE)lParam); + break; + + case WM_SIZE: + if (!vid_initializing) + { + GetWindowRect(mainwindow, &window_rect); + // force width/height to be updated +// glwidth = window_rect.right - window_rect.left; +// glheight = window_rect.bottom - window_rect.top; +// Cvar_ForceCallback(&vid_conautoscale); +// Cvar_ForceCallback(&vid_conwidth); + } + break; + + case WM_CLOSE: + if (!vid_initializing) + if (MessageBox (mainwindow, "Are you sure you want to quit?", "Confirm Exit", + MB_YESNO | MB_SETFOREGROUND | MB_ICONQUESTION) == IDYES) + { + Sys_Quit (); + } + + break; + + case WM_ACTIVATE: + fActive = LOWORD(wParam); + fMinimized = (BOOL) HIWORD(wParam); + if (!D3D9AppActivate(!(fActive == WA_INACTIVE), fMinimized)) + break;//so, urm, tell me microsoft, what changed? + if (modestate == MS_FULLDIB) + ShowWindow(mainwindow, SW_SHOWNORMAL); + + // fix the leftover Alt from any Alt-Tab or the like that switched us away +// ClearAllStates (); + + break; + + case WM_DESTROY: + { +// if (dibwindow) +// DestroyWindow (dibwindow); + } + break; + + case MM_MCINOTIFY: + lRet = CDAudio_MessageHandler (hWnd, uMsg, wParam, lParam); + break; + + + case WM_MWHOOK: + if (!vid_initializing) + MW_Hook_Message (lParam); + break; + + default: + /* pass all unhandled messages to DefWindowProc */ + lRet = DefWindowProc (hWnd, uMsg, wParam, lParam); + break; + } + + /* return 1 if handled message, 0 if not */ + return lRet; +} + +static D3DPRESENT_PARAMETERS d3dpp; +static void resetD3D9(void) +{ + IDirect3DDevice9_Reset(pD3DDev9, &d3dpp); + + + /*clear the screen to black as soon as we start up, so there's no lingering framebuffer state*/ + IDirect3DDevice9_BeginScene(pD3DDev9); + IDirect3DDevice9_Clear(pD3DDev9, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); + IDirect3DDevice9_EndScene(pD3DDev9); + IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL); + + + + + + + + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_DITHERENABLE, FALSE); + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_SPECULARENABLE, FALSE); + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_LIGHTING, FALSE); +} + +#if (WINVER < 0x500) && !defined(__GNUC__) +typedef struct tagMONITORINFO +{ + DWORD cbSize; + RECT rcMonitor; + RECT rcWork; + DWORD dwFlags; +} MONITORINFO, *LPMONITORINFO; +#endif + +static void initD3D9(HWND hWnd, rendererstate_t *info) +{ + int i; + int numadaptors; + int err; + D3DADAPTER_IDENTIFIER9 inf; + extern cvar_t _vid_wait_override; + + static HMODULE d3d9dll; + LPDIRECT3D9 (WINAPI *pDirect3DCreate9) (int version); + + if (!d3d9dll) + d3d9dll = LoadLibrary("d3d9.dll"); + if (!d3d9dll) + { + Con_Printf("Direct3d 9 does not appear to be installed\n"); + return; + } + pDirect3DCreate9 = (void*)GetProcAddress(d3d9dll, "Direct3DCreate9"); + if (!pDirect3DCreate9) + { + Con_Printf("Direct3d 9 does not appear to be installed properly\n"); + return; + } + + pD3D = pDirect3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface + if (!pD3D) + return; + + numadaptors = IDirect3D9_GetAdapterCount(pD3D); + for (i = 0; i < numadaptors; i++) + { //try each adaptor in turn until we get one that actually works + memset(&d3dpp, 0, sizeof(d3dpp)); // clear out the struct for use + d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames + d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D + d3dpp.BackBufferWidth = info->width; + d3dpp.BackBufferHeight = info->height; + d3dpp.MultiSampleType = info->multisample; + d3dpp.BackBufferCount = 3; + d3dpp.FullScreen_RefreshRateInHz = info->fullscreen?info->rate:0; //don't pass a rate if not fullscreen, d3d doesn't like it. + d3dpp.Windowed = !info->fullscreen; + + d3dpp.EnableAutoDepthStencil = true; + d3dpp.AutoDepthStencilFormat = D3DFMT_D16; + d3dpp.BackBufferFormat = info->fullscreen?D3DFMT_X8R8G8B8:D3DFMT_UNKNOWN; + + if (!*_vid_wait_override.string) + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT; + else + { + if (_vid_wait_override.value == 1) + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE; + else if (_vid_wait_override.value == 2) + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_TWO; + else if (_vid_wait_override.value == 3) + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_THREE; + else if (_vid_wait_override.value == 4) + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_FOUR; + else + d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; + } + + memset(&inf, 0, sizeof(inf)); + err = IDirect3D9_GetAdapterIdentifier(pD3D, i, 0, &inf); + + // create a device class using this information and information from the d3dpp stuct + IDirect3D9_CreateDevice(pD3D, + i, + D3DDEVTYPE_HAL, + hWnd, + D3DCREATE_SOFTWARE_VERTEXPROCESSING, + &d3dpp, + &pD3DDev9); + + if (pD3DDev9) + { + HMONITOR hm; + MONITORINFO mi; + char *s; + for (s = inf.Description + strlen(inf.Description)-1; s >= inf.Description && *s <= ' '; s--) + *s = 0; + Con_Printf("D3D9: Using device %s\n", inf.Description); + + if (d3dpp.Windowed) //fullscreen we get positioned automagically. + { //windowed, we get positioned at 0,0... which is often going to be on the wrong screen + //the user can figure it out from here + static HANDLE huser32; + BOOL (WINAPI *pGetMonitorInfoA)(HMONITOR hMonitor, LPMONITORINFO lpmi); + if (!huser32) + huser32 = LoadLibrary("user32.dll"); + if (!huser32) + return; + pGetMonitorInfoA = (void*)GetProcAddress(huser32, "GetMonitorInfoA"); + if (!pGetMonitorInfoA) + return; + + hm = IDirect3D9_GetAdapterMonitor(pD3D, i); + memset(&mi, 0, sizeof(mi)); + mi.cbSize = sizeof(mi); + pGetMonitorInfoA(hm, &mi); + MoveWindow(d3dpp.hDeviceWindow, mi.rcWork.left, mi.rcWork.top, d3dpp.BackBufferWidth, d3dpp.BackBufferHeight, false); + } + return; //successful + } + } + + + Con_Printf("IDirect3D9_CreateDevice failed\n"); + + + return; +} + +static qboolean D3D9_VID_Init(rendererstate_t *info, unsigned char *palette) +{ + DWORD width = info->width; + DWORD height = info->height; + DWORD bpp = info->bpp; + DWORD zbpp = 16; + DWORD flags = 0; + MSG msg; + + extern cvar_t vid_conwidth; + extern cvar_t vid_conheight; + + //DDGAMMARAMP gammaramp; + //int i; + + char *CLASSNAME = "FTED3D9QUAKE"; + WNDCLASS wc = { + 0, + &D3D9_WindowProc, + 0, + 0, + NULL, + NULL, + NULL, + NULL, + NULL, + CLASSNAME + }; + + vid_initializing = true; + + RegisterClass(&wc); + + if (info->fullscreen) + mainwindow = CreateWindow(CLASSNAME, "Direct3D", 0, 0, 0, width, height, NULL, NULL, NULL, NULL); + else + mainwindow = CreateWindow(CLASSNAME, "Direct3D", WS_OVERLAPPEDWINDOW, 0, 0, width, height, NULL, NULL, NULL, NULL); +/* + width = vid_conwidth.value; + height = vid_conheight.value; +*/ + // Try as specified. + + initD3D9(mainwindow, info); + if (!pD3DDev9) + return false; + + + + while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + ShowWindow(mainwindow, SW_NORMAL); + //IDirect3DDevice9_Clear(pD3DDev9, 0, NULL, D3DCLEAR_TARGET, 0xffffffff, 1, 0); + //IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL); + + IDirect3DDevice9_Clear(pD3DDev9, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); + IDirect3DDevice9_BeginScene(pD3DDev9); + IDirect3DDevice9_EndScene(pD3DDev9); + IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL); + + + +// pD3DX->lpVtbl->GetBufferSize((void*)pD3DX, &width, &height); + vid.pixelwidth = width; + vid.pixelheight = height; + vid.recalc_refdef = true; + + vid.width = vid.conwidth = width; + vid.height = vid.conheight = height; + +// pDD->lpVtbl->QueryInterface ((void*)pDD, &IID_IDirectDrawGammaControl, (void**)&pGammaControl); +/* if (pGammaControl) + { + for (i = 0; i < 256; i++) + gammaramp.red[i] = i*2; + pGammaControl->lpVtbl->SetGammaRamp(pGammaControl, 0, &gammaramp); + } + else*/ + Con_Printf("Couldn't get gamma controls\n"); + + vid_initializing = false; + + +resetD3D9(); +/* + + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAFUNC, D3DCMP_GREATER ); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHAREF, 0.666*256 ); + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHATESTENABLE, TRUE ); + + + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_DITHERENABLE, FALSE); + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_SPECULARENABLE, FALSE); + //IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRENDERSTATE_TEXTUREPERSPECTIVE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_LIGHTING, FALSE); + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZWRITEENABLE, TRUE); + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ZFUNC, D3DCMP_LESSEQUAL); + +*/ + GetWindowRect(mainwindow, &window_rect); + + + D3D9_VID_GenPaletteTables(palette); + + { + extern qboolean mouseactive; + mouseactive = false; + } + + { + extern cvar_t v_contrast; + void GLV_Gamma_Callback(struct cvar_s *var, char *oldvalue); + Cvar_Hook(&v_gamma, GLV_Gamma_Callback); + Cvar_Hook(&v_contrast, GLV_Gamma_Callback); + + Cvar_ForceCallback(&v_gamma); + } + + return true; +} + +/*a new model has been loaded*/ +static void (D3D9_R_NewMap) (void) +{ + int i; + r_worldentity.model = cl.worldmodel; + R_AnimateLight(); +// D3D9_BuildLightmaps(); + + /*wipe any lingering particles*/ + P_ClearParticles(); + + for (i=0 ; inumtextures ; i++) + { + if (!cl.worldmodel->textures[i]) + continue; + cl.worldmodel->textures[i]->texturechain = NULL; + } +} + +extern mleaf_t *r_viewleaf, *r_oldviewleaf; +extern mleaf_t *r_viewleaf2, *r_oldviewleaf2; +static void (D3D9_R_PreNewMap) (void) +{ + r_viewleaf = NULL; + r_oldviewleaf = NULL; + r_viewleaf2 = NULL; + r_oldviewleaf2 = NULL; +} +static int (D3D9_R_LightPoint) (vec3_t point) +{ + return 0; +} + +static void (D3D9_R_PushDlights) (void) +{ +} +static void (D3D9_R_AddStain) (vec3_t org, float red, float green, float blue, float radius) +{ +} +static void (D3D9_R_LessenStains) (void) +{ +} + +static void (D3D9_VID_DeInit) (void) +{ + /*final shutdown, kill the video stuff*/ + if (pD3DDev9) + { + IDirect3DDevice9_Release(pD3DDev9); + pD3DDev9 = NULL; + } + if (pD3D) + { + IDirect3D9_Release(pD3D); + pD3D = NULL; + } + if (mainwindow) + { + DestroyWindow(mainwindow); + mainwindow = NULL; + } +} +static void (D3D9_VID_LockBuffer) (void) +{ +} +static void (D3D9_VID_UnlockBuffer) (void) +{ +} +static void (D3D9_D_BeginDirectRect) (int x, int y, qbyte *pbitmap, int width, int height) +{ +} +static void (D3D9_D_EndDirectRect) (int x, int y, int width, int height) +{ +} +static void (D3D9_VID_ForceLockState) (int lk) +{ +} +static int (D3D9_VID_ForceUnlockedAndReturnState) (void) +{ + return 0; +} + +static void (D3D9_VID_SetPalette) (unsigned char *palette) +{ + D3D9_VID_GenPaletteTables(palette); +} +static void (D3D9_VID_ShiftPalette) (unsigned char *palette) +{ + D3D9_VID_GenPaletteTables(palette); +} +static char *(D3D9_VID_GetRGBInfo) (int prepad, int *truevidwidth, int *truevidheight) +{ + return NULL; +} +static void (D3D9_VID_SetWindowCaption) (char *msg) +{ + SetWindowText(mainwindow, msg); +} + +void Matrix4_OrthographicD3D(float *proj, float xmin, float xmax, float ymax, float ymin, + float znear, float zfar); +void d3dx_ortho(float *m); + +static void D3D9_Set2D (void) +{ + float m[16]; + D3DVIEWPORT9 vport; +// IDirect3DDevice9_EndScene(pD3DDev9); + + Matrix4_OrthographicD3D(m, 0, vid.width, 0, vid.height, -100, 100); + d3dx_ortho(m); + IDirect3DDevice9_SetTransform(pD3DDev9, D3DTS_PROJECTION, (D3DMATRIX*)m); + + Matrix4_Identity(m); + IDirect3DDevice9_SetTransform(pD3DDev9, D3DTS_WORLD, (D3DMATRIX*)m); + + Matrix4_Identity(m); + IDirect3DDevice9_SetTransform(pD3DDev9, D3DTS_VIEW, (D3DMATRIX*)m); + + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_CULLMODE, D3DCULL_CCW); + + IDirect3DDevice9_SetSamplerState(pD3DDev9, 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); + IDirect3DDevice9_SetSamplerState(pD3DDev9, 1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); + + IDirect3DDevice9_SetSamplerState(pD3DDev9, 0, D3DSAMP_MIPFILTER, D3DTEXF_NONE); + IDirect3DDevice9_SetSamplerState(pD3DDev9, 1, D3DSAMP_MIPFILTER, D3DTEXF_NONE); + + IDirect3DDevice9_SetSamplerState(pD3DDev9, 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); + IDirect3DDevice9_SetSamplerState(pD3DDev9, 1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); + + vport.X = 0; + vport.Y = 0; + D3D9_GetBufferSize(&vport.Width, &vport.Height); + vport.MinZ = 0; + vport.MaxZ = 1; + IDirect3DDevice9_SetViewport(pD3DDev9, &vport); +} + +static void D3D9_GetBufferSize(int *width, int *height) +{ + *width = vid.pixelwidth; + *height = vid.pixelheight;//vid.height; +// IDirect3DDevice9_GetBufferSize((void*)pD3DX, width, height); +} + +static int d3d9error(int i) +{ + if (FAILED(i))// != D3D_OK) + Con_Printf("D3D error: %x %i\n", i); + return i; +} + +typedef struct { + float x, y, z; + unsigned int colour; +} d3d9bsvert_t; +static void D3D9_BrightenScreen (void) +{ + d3d9bsvert_t d3d9bsvert[4]; + index_t d3d9quadindexes[6] = { + 0, 1, 2, + 0, 2, 3 + }; + + extern cvar_t gl_contrast; + float f; + unsigned int colour; + + RSpeedMark(); + + if (gl_contrast.value <= 1.0) + return; + + f = gl_contrast.value; + f = min (f, 3); + + IDirect3DDevice9_SetTexture (pD3DDev9, 0, NULL); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHABLENDENABLE, TRUE); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_SRCBLEND, D3DBLEND_DESTCOLOR); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_DESTBLEND, D3DBLEND_ONE); + + while (f > 1) + { + if (f >= 2) + colour = 0xffffffff; + else + { + colour = (f-1)*255; + colour = (colour * 0x010101) | 0xff000000; + } + + + d3d9bsvert[0].x = 0; + d3d9bsvert[0].y = 0; + d3d9bsvert[0].z = 0; + d3d9bsvert[0].colour = colour; + + d3d9bsvert[1].x = vid.width; + d3d9bsvert[1].y = 0; + d3d9bsvert[1].z = 0; + d3d9bsvert[1].colour = colour; + + d3d9bsvert[2].x = vid.width; + d3d9bsvert[2].y = vid.height; + d3d9bsvert[2].z = 0; + d3d9bsvert[2].colour = colour; + + d3d9bsvert[3].x = 0; + d3d9bsvert[3].y = vid.height; + d3d9bsvert[3].z = 0; + d3d9bsvert[3].colour = colour; + + IDirect3DDevice9_SetFVF(pD3DDev9, D3DFVF_XYZ|D3DFVF_DIFFUSE); + IDirect3DDevice9_DrawIndexedPrimitiveUP(pD3DDev9, D3DPT_TRIANGLELIST, 0, 4, 2, d3d9quadindexes, D3DFMT_QINDEX, d3d9bsvert, sizeof(d3d9bsvert[0])); + + + f *= 0.5; + } + + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); + IDirect3DDevice9_SetRenderState(pD3DDev9, D3DRS_ALPHABLENDENABLE, FALSE); + + RSpeedEnd(RSPEED_PALETTEFLASHES); +} + +static void (D3D9_SCR_UpdateScreen) (void) +{ + extern int keydown[]; + extern cvar_t vid_conheight; + int uimenu; +#ifdef TEXTEDITOR + extern qboolean editormodal; +#endif + qboolean nohud; + RSpeedMark(); + + switch (IDirect3DDevice9_TestCooperativeLevel(pD3DDev9)) + { + case D3DERR_DEVICELOST: + //the user has task switched away from us or something + return; + case D3DERR_DEVICENOTRESET: + resetD3D9(); + if (FAILED(IDirect3DDevice9_TestCooperativeLevel(pD3DDev9))) + Sys_Error("D3D9 Device lost. Additionally restoration failed.\n"); + // D3DSucks(); + // scr_disabled_for_loading = false; + + VID_ShiftPalette (NULL); + break; + default: + break; + } + + + if (keydown['k']) + { + d3d9error(IDirect3DDevice9_Clear(pD3DDev9, 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(rand()&255, rand()&255, rand()&255), 1.0f, 0)); + d3d9error(IDirect3DDevice9_BeginScene(pD3DDev9)); + d3d9error(IDirect3DDevice9_EndScene(pD3DDev9)); + d3d9error(IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL)); + + VID_ShiftPalette (NULL); + } + + vid.numpages = 2;// + gl_triplebuffer.value; + + if (scr_disabled_for_loading) + { + extern float scr_disabled_time; + if (Sys_DoubleTime() - scr_disabled_time > 60 || key_dest != key_game) + { + scr_disabled_for_loading = false; + } + else + { + IDirect3DDevice9_BeginScene(pD3DDev9); + scr_drawloading = true; + SCR_DrawLoading (); + scr_drawloading = false; + IDirect3DDevice9_EndScene(pD3DDev9); + IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL); + RSpeedEnd(RSPEED_TOTALREFRESH); + return; + } + } + + if (!scr_initialized || !con_initialized) + { + RSpeedEnd(RSPEED_TOTALREFRESH); + return; // not initialized yet + } + +#pragma message("Fixme: remove the code from here...") + { + extern cvar_t vid_conwidth, vid_conheight, vid_conautoscale; + if (vid_conautoscale.value) + { + vid.conwidth = vid.pixelwidth*vid_conautoscale.value; + vid.conheight = vid.pixelheight*vid_conautoscale.value; + } + else + { + vid.conwidth = vid_conwidth.value; + vid.conheight = vid_conheight.value; + } + + if (!vid.conwidth) + vid.conwidth = vid.pixelwidth; + if (!vid.conheight) + vid.conheight = vid.pixelheight; + + if (vid.width != vid.conwidth || vid.height != vid.conheight) + vid.recalc_refdef = true; + vid.width = vid.conwidth; + vid.height = vid.conheight; + } + + +#ifdef VM_UI + uimenu = UI_MenuState(); +#else + uimenu = 0; +#endif + + d3d9error(IDirect3DDevice9_BeginScene(pD3DDev9)); + D3D9_Set2D (); +/* +#ifdef TEXTEDITOR + if (editormodal) + { + Editor_Draw(); + GLV_UpdatePalette (false, host_frametime); +#if defined(_WIN32) && defined(GLQUAKE) + Media_RecordFrame(); +#endif + GLR_BrightenScreen(); + + if (key_dest == key_console) + Con_DrawConsole(vid_conheight.value/2, false); + GL_EndRendering (); + GL_DoSwap(); + RSpeedEnd(RSPEED_TOTALREFRESH); + return; + } +#endif +*/ + if (Media_ShowFilm()) + { + M_Draw(0); +// GLV_UpdatePalette (false, host_frametime); +#if defined(_WIN32) + Media_RecordFrame(); +#endif +// GLR_BrightenScreen(); + IDirect3DDevice9_EndScene(pD3DDev9); + IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL); + +// pD3DDev->lpVtbl->BeginScene(pD3DDev); + + RSpeedEnd(RSPEED_TOTALREFRESH); + return; + } + + // + // determine size of refresh window + // + if (vid.recalc_refdef) + SCR_CalcRefdef (); + +// +// do 3D refresh drawing, and then update the screen +// + SCR_SetUpToDrawConsole (); + + nohud = false; + +#ifdef VM_CG + if (CG_Refresh()) + nohud = true; + else +#endif +#ifdef CSQC_DAT + if (cls.state == ca_active && CSQC_DrawView()) + nohud = true; + else +#endif + if (r_worldentity.model && uimenu != 1) + { + V_RenderView (); +// Q1BSP_TestClipDecal(); + } + + + D3D9_Set2D (); + + D3D9_BrightenScreen(); + + if (!nohud) + SCR_TileClear (); + + SCR_DrawTwoDimensional(uimenu, nohud); + + GLV_UpdatePalette (false, host_frametime); +#if defined(_WIN32) && defined(GLQUAKE) + Media_RecordFrame(); +#endif + + RSpeedEnd(RSPEED_TOTALREFRESH); + RSpeedShow(); +#pragma message("Fixme: ... to here") + + + d3d9error(IDirect3DDevice9_EndScene(pD3DDev9)); + d3d9error(IDirect3DDevice9_Present(pD3DDev9, NULL, NULL, NULL, NULL)); + + window_center_x = (window_rect.left + window_rect.right)/2; + window_center_y = (window_rect.top + window_rect.bottom)/2; + + + IN_UpdateGrabs(modestate != MS_WINDOWED, ActiveApp); + + VID_ShiftPalette (NULL); +} + + + + + +static void (D3D9_Draw_BeginDisc) (void) +{ +} +static void (D3D9_Draw_EndDisc) (void) +{ +} + + +static void (D3D9_Draw_Init) (void) +{ + R2D_Init(); +} +static void (D3D9_Draw_ReInit) (void) +{ +} +static void (D3D9_Draw_Crosshair) (void) +{ +} +static void (D3D9_Draw_TransPicTranslate) (int x, int y, int w, int h, qbyte *pic, qbyte *translation) +{ +} +static void (D3D9_Draw_Fill) (int x, int y, int w, int h, unsigned int c) +{ +} +static void (D3D9_Draw_FillRGB) (int x, int y, int w, int h, float r, float g, float b) +{ +} +static void (D3D9_Draw_FadeScreen) (void) +{ +} +static void (D3D9_Draw_BeginDisc) (void); +static void (D3D9_Draw_EndDisc) (void); + +static void (D3D9_R_Init) (void) +{ +} +static void (D3D9_R_DeInit) (void) +{ +} +static void (D3D9_R_ReInit) (void) +{ +} +static void (D3D9_R_RenderView) (void) +{ + d3d9error(IDirect3DDevice9_Clear(pD3DDev9, 0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1, 0)); +} + +void (D3D9_R_NewMap) (void); +void (D3D9_R_PreNewMap) (void); +int (D3D9_R_LightPoint) (vec3_t point); + +void (D3D9_R_PushDlights) (void); +void (D3D9_R_AddStain) (vec3_t org, float red, float green, float blue, float radius); +void (D3D9_R_LessenStains) (void); + +qboolean (D3D9_VID_Init) (rendererstate_t *info, unsigned char *palette); +void (D3D9_VID_DeInit) (void); +void (D3D9_VID_LockBuffer) (void); +void (D3D9_VID_UnlockBuffer) (void); +void (D3D9_D_BeginDirectRect) (int x, int y, qbyte *pbitmap, int width, int height); +void (D3D9_D_EndDirectRect) (int x, int y, int width, int height); +void (D3D9_VID_ForceLockState) (int lk); +int (D3D9_VID_ForceUnlockedAndReturnState) (void); +void (D3D9_VID_SetPalette) (unsigned char *palette); +void (D3D9_VID_ShiftPalette) (unsigned char *palette); +char *(D3D9_VID_GetRGBInfo) (int prepad, int *truevidwidth, int *truevidheight); +void (D3D9_VID_SetWindowCaption) (char *msg); + +void (D3D9_SCR_UpdateScreen) (void); + + + + + +rendererinfo_t d3drendererinfo = +{ + "Direct3D9 Native", + { + "D3D", + "Direct3d", + "DirectX", + "DX" + }, + QR_DIRECT3D, + + R_SafePicFromWad, + R_SafeCachePic, + D3D9_Draw_Init, + D3D9_Draw_ReInit, + D3D9_Draw_Crosshair, + R_ScalePic, + R_SubPic, + D3D9_Draw_TransPicTranslate, + R_ConsoleBackground, + R_EditorBackground, + R_TileClear, + D3D9_Draw_Fill, + D3D9_Draw_FillRGB, + D3D9_Draw_FadeScreen, + D3D9_Draw_BeginDisc, + D3D9_Draw_EndDisc, + + R_Image, + R_ImageColours, + + R2D_Init, + D3D9_R_DeInit, + D3D9_R_ReInit, + D3D9_R_RenderView, + + D3D9_R_NewMap, + D3D9_R_PreNewMap, + D3D9_R_LightPoint, + + D3D9_R_PushDlights, + D3D9_R_AddStain, + D3D9_R_LessenStains, + + NULL, + NULL, + NULL, + + RMod_Init, + RMod_ClearAll, + RMod_ForName, + RMod_FindName, + RMod_Extradata, + RMod_TouchModel, + + RMod_NowLoadExternal, + RMod_Think, + Mod_GetTag, + Mod_TagNumForName, + Mod_SkinNumForName, + Mod_FrameNumForName, + Mod_FrameDuration, + + + D3D9_VID_Init, + D3D9_VID_DeInit, + D3D9_VID_LockBuffer, + D3D9_VID_UnlockBuffer, + D3D9_D_BeginDirectRect, + D3D9_D_EndDirectRect, + D3D9_VID_ForceLockState, + D3D9_VID_ForceUnlockedAndReturnState, + D3D9_VID_SetPalette, + D3D9_VID_ShiftPalette, + D3D9_VID_GetRGBInfo, + D3D9_VID_SetWindowCaption, + + D3D9_SCR_UpdateScreen, + + "no more" +}; + +#endif