void MythRenderD3D9::Init2DState(void) { IDirect3DDevice9* dev = AcquireDevice(); if (!dev) return; dev->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); dev->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); dev->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); dev->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); dev->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); dev->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); dev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(255,255,255)); dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); dev->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); dev->SetRenderState(D3DRS_LIGHTING, FALSE); dev->SetRenderState(D3DRS_DITHERENABLE, TRUE); dev->SetRenderState(D3DRS_STENCILENABLE, FALSE); dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); dev->SetVertexShader(NULL); SetTextureVertices(dev, false); MultiTexturing(dev, false); EnableBlending(dev, false); ReleaseDevice(); }
void Blit::setCommonBlitState() { IDirect3DDevice9 *device = getDevice(); device->SetDepthStencilSurface(NULL); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); for (int i = 0; i < MAX_VERTEX_ATTRIBS+1; i++) { device->SetStreamSourceFreq(i, 1); } RECT scissorRect = {0}; // Scissoring is disabled for flipping, but we need this to capture and restore the old rectangle device->SetScissorRect(&scissorRect); }
void Blit::setCommonBlitState() { IDirect3DDevice9 *device = getDevice(); device->SetDepthStencilSurface(NULL); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_SRGBTEXTURE, FALSE); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); for (int i = 0; i < MAX_VERTEX_ATTRIBS+1; i++) { device->SetStreamSourceFreq(i, 1); } }
void CState::Restore() { // viewport m_D3DDev->SetViewport(&m_Vp); // render states m_D3DDev->SetRenderState(D3DRS_ZENABLE, m_Z); m_D3DDev->SetRenderState(D3DRS_CULLMODE, m_CullMode); m_D3DDev->SetRenderState(D3DRS_ALPHATESTENABLE, m_AlphaTest); m_D3DDev->SetRenderState(D3DRS_ALPHABLENDENABLE, m_AlphaBlend); m_D3DDev->SetRenderState(D3DRS_BLENDOP, m_BlendOp); m_D3DDev->SetRenderState(D3DRS_SRCBLEND, m_SrcBlend); m_D3DDev->SetRenderState(D3DRS_DESTBLEND, m_DstBlend); m_D3DDev->SetRenderState(D3DRS_CLIPPLANEENABLE, m_ClipPlane); m_D3DDev->SetRenderState(D3DRS_FILLMODE, m_FillMode); m_D3DDev->SetRenderState(D3DRS_LASTPIXEL, m_LastPixel); m_D3DDev->SetRenderState(D3DRS_FOGENABLE, m_Fog); m_D3DDev->SetRenderState(D3DRS_STENCILENABLE, m_Stencil); m_D3DDev->SetRenderState(D3DRS_COLORWRITEENABLE, m_ColorWrite); m_D3DDev->SetRenderState(D3DRS_SCISSORTESTENABLE, m_Scissor); if( m_Caps.PrimitiveMiscCaps & D3DPMISCCAPS_SEPARATEALPHABLEND ) m_D3DDev->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, m_SeparateAlphaBlend); //if( m_Caps.LineCaps & D3DLINECAPS_ANTIALIAS ) m_D3DDev->SetRenderState(D3DRS_ANTIALIASEDLINEENABLE, m_AntiAliasedLine); // primitive m_D3DDev->SetFVF(m_FVF); m_D3DDev->SetStreamSource(0, m_StreamData, m_StreamOffset, m_StreamStride); if( m_StreamData ) m_StreamData->Release(); m_D3DDev->SetVertexShader(m_VertexShader); if( m_VertexShader ) m_VertexShader->Release(); // texture m_D3DDev->SetTexture(0, m_Tex); if( m_Tex ) m_Tex->Release(); m_D3DDev->SetPixelShader(m_PixelShader); if( m_PixelShader ) m_PixelShader->Release(); // texture stage states m_D3DDev->SetTextureStageState(0, D3DTSS_COLOROP, m_ColorOp); m_D3DDev->SetTextureStageState(0, D3DTSS_COLORARG1, m_ColorArg1); m_D3DDev->SetTextureStageState(0, D3DTSS_COLORARG2, m_ColorArg2); m_D3DDev->SetTextureStageState(0, D3DTSS_ALPHAOP, m_AlphaOp); m_D3DDev->SetTextureStageState(0, D3DTSS_ALPHAARG1, m_AlphaArg1); m_D3DDev->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, m_TexCoordIndex); m_D3DDev->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, m_TexTransfFlags); m_D3DDev->SetSamplerState(0, D3DSAMP_ADDRESSU, m_AddressU); m_D3DDev->SetSamplerState(0, D3DSAMP_ADDRESSV, m_AddressV); m_D3DDev->SetSamplerState(0, D3DSAMP_MAGFILTER, m_MagFilter); m_D3DDev->SetSamplerState(0, D3DSAMP_MINFILTER, m_MinFilter); m_D3DDev->SetSamplerState(0, D3DSAMP_MIPFILTER, m_MipFilter); }
bool Blit::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = copySurfaceToTexture(source, getSurfaceRect(source)); if (!texture) { return false; } IDirect3DDevice9 *device = getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setVertexShader(SHADER_VS_STANDARD); setPixelShader(SHADER_PS_PASSTHROUGH); setCommonBlitState(); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); setViewport(getSurfaceRect(dest), 0, 0); render(); texture->Release(); restoreState(); return true; }
gl::Error Blit9::boxFilter(IDirect3DSurface9 *source, IDirect3DSurface9 *dest) { IDirect3DTexture9 *texture = NULL; gl::Error error = copySurfaceToTexture(source, getSurfaceRect(source), &texture); if (error.isError()) { return error; } IDirect3DDevice9 *device = mRenderer->getDevice(); saveState(); device->SetTexture(0, texture); device->SetRenderTarget(0, dest); setVertexShader(SHADER_VS_STANDARD); setPixelShader(SHADER_PS_PASSTHROUGH); setCommonBlitState(); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); setViewport(getSurfaceRect(dest), 0, 0); render(); SafeRelease(texture); restoreState(); return gl::Error(GL_NO_ERROR); }
void sge::d3d9::devicefuncs::set_sampler_state( IDirect3DDevice9 &_device, sge::renderer::texture::stage const _stage, D3DSAMPLERSTATETYPE const _type, DWORD const _value ) { if( _device.SetSamplerState( _stage.get(), _type, _value ) != D3D_OK ) throw sge::renderer::exception( FCPPT_TEXT("SetSamplerState() failed!") ); }
int WINAPI WinMain(HINSTANCE hist,HINSTANCE phist,LPSTR cmd,int show) { srand((unsigned int)time(0)); WinInfo info; if (!initWindow(800,600,false,"my game",hist,&info)) { return -1; } DxParam param = {info.isFullScreen,info.width,info.height,info.hwnd}; DxFw df; if (!df.initDx(param)) { MessageBox(0,"f**k","",MB_OK); } if (!df.initInput(info.hwnd,info.hist,false)) { MessageBox(0,"input failed","",MB_OK); } registAllDefaultAffectos(); df.getRenderer()->setAsPerspectiveProjection(PI_OVER_2,800.0f / 600.0f,1.0f,1000.0f); SceneNodeContainer c; SceneNode* n = c.createNode("test"); XModel x; x.mXmodel = df.getResourceManager()->loadXModel(DEFAULT_GROUP_NAME,"ship.x"); n->attach(&x); MSG msg; D3DLIGHT9 light; ::ZeroMemory(&light, sizeof(light)); light.Type = D3DLIGHT_DIRECTIONAL; light.Ambient = D3DXCOLOR(0.8f, 0.8f, 0.8f, 1.0f); light.Diffuse = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f); light.Specular = D3DXCOLOR(0.2f, 0.2f, 0.2f, 1.0f); light.Direction = D3DXVECTOR3(1.0f, -1.0f, 0.0f); IDirect3DDevice9* device = df.getDevice(); device->SetSamplerState(0,D3DSAMP_MAGFILTER,D3DTEXF_LINEAR); device->SetSamplerState(0,D3DSAMP_MINFILTER,D3DTEXF_LINEAR); device->SetSamplerState(0,D3DSAMP_MIPFILTER,D3DTEXF_POINT); df.getRenderer()->setLight(0,&light); Node camera; camera.setPosition(0.0f,100.0f,0.0f); camera.lookAt(Vector3(0.0f,0.0f,0.0f)); n->yaw(PI); //Device->SetRenderState(D3DRS_NORMALIZENORMALS, true); //Device->SetRenderState(D3DRS_SPECULARENABLE, true); GUISystem* guisys = GUISystem::getSingletonPtr(); guisys->initOnce(df); //Route r(new LinearPathRel(Vector3(10.0f,0.0f,10.0f),0.5),true); //r.addPath(new LinearPathRel(Vector3(-10.0f,0.0f,10.0f),0.5)); //TranslateToAction action(n,Vector3(10.0f,0.0f,0.0f),1.0,Node::TS_PARENT); //guisys->load("media/gui/dx.xml"); //GUIButton* btn = (GUIButton*) guisys->getLayout(0)->getControlById(0); //btn->setClickListener(f**k); //GUIImage* img = layout->createImage(0,0,800,600,2); //img->setImage("menu/mainMenu.jpg"); GUILayout* layout = guisys->createLayout(0); DxFont* font = guisys->createFont("",15,false,15,0); GUILabel* label = layout->createLabel(0,0,800,600,0,0); label->setColor(0xffff0000); guisys->changeCurrentLayout(0); loggingInit(label); DxParticleSystem ps; loadParticleSystem(&ps,&df,"media/particle/particl_1.lua"); //ps.init(&df,5012,"media/tex/smoke000.tga"); /* DxParticleEmitter* em = new DxParticleEmitter; DxColorValue e = {0.7f,0.7f,0.8f,0.7f}; DxColorValue b = {0.7f,0.7f,0.8f,0.7f}; em->colorBegine = b; em->colorEnd = e; em->angle = 0.0f; em->up = Vector3::UNIT_Y; em->direction = Vector3::UNIT_Z; em->duration = 0.0f; em->emitRate = 1000.0f; em->maxPosition = Vector3(1.0f,10.0f,2.0f); em->minPosition = Vector3(-1.0f,0.0f,-1.0f); em->minVelocity = 50.0f; em->maxVelocity = 80.0f; em->maxTimeLL = 0.5f; em->minTimeLL = 0.1f; em->size = 1.5f; em->repeatDelay = 2.5f; em->currentTime = em->duration; em->repeatTimeRemain = em->repeatDelay; ps.setEmitter(em); ps.addAffector(new LinearForceAffector(0.0f,0.0f,-100.0f,LinearForceAffector::FT_ADD)); ps.addAffector(new ColorFaderAffector(0.0f,2.0f,-2.0f,-3.0f)); */ n->attach(&ps); //logToScreen("p","f**k"); /* SceneNode* nps = c.createNode("testps"); nps->attach(&ps); nps->yaw(PI); nps->translate(0.0f,0.0f,-11.0f); */ while (true) { DxRenderer* renderer = df.getRenderer(); renderer->clear(true,true,true,0xff000000); Matrix4x4 v; camera.generateParentToLocalMatrix(&v); renderer->setViewMatrix(v); renderer->render(&c); guisys->render(); //n->yaw(getTimeSinceLastFrame() * 1.0f); float timeDelta = getTimeSinceLastFrame(); Vector3 walk = Vector3::ZERO; float speed = 60.0f; float hspeed = speed; float vspeed = speed * 0.7f; IInputSystem* isys = df.getInputSystem(); if (isys->keyDown(DIK_W)) { walk.z += vspeed * timeDelta; } if (isys->keyDown(DIK_A)) { walk.x -= hspeed * timeDelta; } if (isys->keyDown(DIK_D)) { walk.x += hspeed * timeDelta; } if (isys->keyDown(DIK_S)) { walk.z -= vspeed * timeDelta; } if (isys->keyDown(DIK_F5)) { df.saveScreenshot(); } //Vector3 ani; //r.calcPosition(timeDelta,&ani); //n->translate(ani,Node::TS_PARENT); //if (action.act(timeDelta)) //{ //} n->translate(walk,Node::TS_PARENT); isys->capture(); Point p = isys->getMouseClientPosition(); guisys->processGUI(p.x,p.y,isys->mouseButtonDown(0)); ps.update(timeDelta); //logToScreen("particle num:","%d",ps.size()); renderer->present(); messagePump(&msg); if (msg.message == WM_QUIT) { break; } } guisys->release(); df.release(); UnregisterClass(info.className,info.hist); return 0; }
// parameters should be validated/clamped by caller EGLint SwapChain9::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) { if (!mSwapChain) { return EGL_SUCCESS; } IDirect3DDevice9 *device = mRenderer->getDevice(); // Disable all pipeline operations device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetPixelShader(NULL); device->SetVertexShader(NULL); device->SetRenderTarget(0, mBackBuffer); device->SetDepthStencilSurface(NULL); device->SetTexture(0, mOffscreenTexture); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); for (UINT streamIndex = 0; streamIndex < gl::MAX_VERTEX_ATTRIBS; streamIndex++) { device->SetStreamSourceFreq(streamIndex, 1); } D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f}; device->SetViewport(&viewport); float x1 = x - 0.5f; float y1 = (mHeight - y - height) - 0.5f; float x2 = (x + width) - 0.5f; float y2 = (mHeight - y) - 0.5f; float u1 = x / float(mWidth); float v1 = y / float(mHeight); float u2 = (x + width) / float(mWidth); float v2 = (y + height) / float(mHeight); float quad[4][6] = {{x1, y1, 0.0f, 1.0f, u1, v2}, {x2, y1, 0.0f, 1.0f, u2, v2}, {x2, y2, 0.0f, 1.0f, u2, v1}, {x1, y2, 0.0f, 1.0f, u1, v1}}; // x, y, z, rhw, u, v mRenderer->startScene(); device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float)); mRenderer->endScene(); device->SetTexture(0, NULL); RECT rect = { x, mHeight - y - height, x + width, mHeight - y }; HRESULT result = mSwapChain->Present(&rect, &rect, NULL, NULL, 0); mRenderer->markAllStateDirty(); if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR) { return EGL_BAD_ALLOC; } // On Windows 8 systems, IDirect3DSwapChain9::Present sometimes returns 0x88760873 when the windows is // in the process of entering/exiting fullscreen. This code doesn't seem to have any documentation. The // device appears to be ok after emitting this error so simply return a failure to swap. if (result == 0x88760873) { return EGL_BAD_NATIVE_WINDOW; } // http://crbug.com/313210 // If our swap failed, trigger a device lost event. Resetting will work around an AMD-specific // device removed bug with lost contexts when reinstalling drivers. if (FAILED(result)) { mRenderer->notifyDeviceLost(); return EGL_CONTEXT_LOST; } return EGL_SUCCESS; }
// parameters should be validated/clamped by caller EGLint SwapChain9::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) { if (!mSwapChain) { return EGL_SUCCESS; } IDirect3DDevice9 *device = mRenderer->getDevice(); // Disable all pipeline operations device->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE); device->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); device->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); device->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); device->SetRenderState(D3DRS_STENCILENABLE, FALSE); device->SetRenderState(D3DRS_CLIPPLANEENABLE, 0); device->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_ALPHA | D3DCOLORWRITEENABLE_BLUE | D3DCOLORWRITEENABLE_GREEN | D3DCOLORWRITEENABLE_RED); device->SetRenderState(D3DRS_SRGBWRITEENABLE, FALSE); device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE); device->SetPixelShader(NULL); device->SetVertexShader(NULL); device->SetRenderTarget(0, mBackBuffer); device->SetDepthStencilSurface(NULL); device->SetTexture(0, mOffscreenTexture); device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE); device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT); device->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP); device->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP); device->SetFVF(D3DFVF_XYZRHW | D3DFVF_TEX1); D3DVIEWPORT9 viewport = {0, 0, mWidth, mHeight, 0.0f, 1.0f}; device->SetViewport(&viewport); float x1 = x - 0.5f; float y1 = (mHeight - y - height) - 0.5f; float x2 = (x + width) - 0.5f; float y2 = (mHeight - y) - 0.5f; float u1 = x / float(mWidth); float v1 = y / float(mHeight); float u2 = (x + width) / float(mWidth); float v2 = (y + height) / float(mHeight); float quad[4][6] = {{x1, y1, 0.0f, 1.0f, u1, v2}, {x2, y1, 0.0f, 1.0f, u2, v2}, {x2, y2, 0.0f, 1.0f, u2, v1}, {x1, y2, 0.0f, 1.0f, u1, v1}}; // x, y, z, rhw, u, v mRenderer->startScene(); device->DrawPrimitiveUP(D3DPT_TRIANGLEFAN, 2, quad, 6 * sizeof(float)); mRenderer->endScene(); device->SetTexture(0, NULL); RECT rect = { x, mHeight - y - height, x + width, mHeight - y }; HRESULT result = mSwapChain->Present(&rect, &rect, NULL, NULL, 0); mRenderer->markAllStateDirty(); if (d3d9::isDeviceLostError(result)) { return EGL_CONTEXT_LOST; } if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY || result == D3DERR_DRIVERINTERNALERROR) { return EGL_BAD_ALLOC; } ASSERT(SUCCEEDED(result)); return EGL_SUCCESS; }
HRESULT _stdcall MGEProxyD3D::CreateDevice(UINT a, D3DDEVTYPE b, HWND c, DWORD d, D3DPRESENT_PARAMETERS8 *e, IDirect3DDevice8 **f) { LOG::logline(">> D3D Proxy CreateDevice"); if(e->Windowed) { HWND hMainWnd = GetParent(c); int wx = std::max(0, (GetSystemMetrics(SM_CXSCREEN) - (int)e->BackBufferWidth) / 2); int wy = std::max(0, (GetSystemMetrics(SM_CYSCREEN) - (int)e->BackBufferHeight) / 2); if(Configuration.Borderless) { // Remove non-client window parts and move window flush to screen edge / centre if smaller than display SetWindowLong(hMainWnd, GWL_STYLE, WS_VISIBLE); SetWindowPos(hMainWnd, NULL, wx, wy, e->BackBufferWidth, e->BackBufferHeight, SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOZORDER); } else { // Move window to top, with client area centred on one axis RECT rect = { wx, wy, e->BackBufferWidth, e->BackBufferHeight }; AdjustWindowRect(&rect, GetWindowLong(hMainWnd, GWL_STYLE), FALSE); SetWindowPos(hMainWnd, NULL, rect.left, 0, 0, 0, SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOCOPYBITS|SWP_NOZORDER); } } // Map 16x antialiasing to equivalent CSAA D3DMULTISAMPLE_TYPE msaaSamples = (Configuration.AALevel == 16) ? D3DMULTISAMPLE_8_SAMPLES : (D3DMULTISAMPLE_TYPE)Configuration.AALevel; DWORD msaaQuality = (Configuration.AALevel == 16) ? 2 : 0; // Override device parameters // Note that Morrowind will look at the modified parameters if(e->Flags & D3DPRESENTFLAG_LOCKABLE_BACKBUFFER) e->Flags ^= D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; e->MultiSampleType = msaaSamples; e->AutoDepthStencilFormat = (D3DFORMAT)Configuration.ZBufFormat; e->FullScreen_RefreshRateInHz = (!e->Windowed) ? Configuration.RefreshRate : 0; e->FullScreen_PresentationInterval = (Configuration.VWait == 255) ? D3DPRESENT_INTERVAL_IMMEDIATE : Configuration.VWait; // Convert presentation parameters to DX9 D3DPRESENT_PARAMETERS9 pp; pp.BackBufferWidth = e->BackBufferWidth; pp.BackBufferHeight = e->BackBufferHeight; pp.BackBufferFormat = e->BackBufferFormat; pp.BackBufferCount = e->BackBufferCount; pp.MultiSampleType = e->MultiSampleType; pp.MultiSampleQuality = msaaQuality; pp.SwapEffect = e->SwapEffect; pp.hDeviceWindow = e->hDeviceWindow; pp.Windowed = e->Windowed; pp.Flags = e->Flags; pp.EnableAutoDepthStencil = e->EnableAutoDepthStencil; pp.AutoDepthStencilFormat = e->AutoDepthStencilFormat; pp.FullScreen_RefreshRateInHz = e->FullScreen_RefreshRateInHz; pp.PresentationInterval = e->FullScreen_PresentationInterval; // Create device in the same manner as the proxy IDirect3DDevice9 *realDevice = NULL; HRESULT hr = realD3D->CreateDevice(a, b, c, d, &pp, &realDevice); if(hr != D3D_OK) { LOG::logline("!! D3D Proxy CreateDevice failure"); return hr; } *f = factoryProxyDevice(realDevice); // Set up default render states Configuration.ScaleFilter = (Configuration.AnisoLevel > 0) ? D3DTEXF_ANISOTROPIC : D3DTEXF_LINEAR; Configuration.MipFilter = D3DTEXF_LINEAR; for(int i = 0; i != 4; ++i) { realDevice->SetSamplerState(i, D3DSAMP_MAXANISOTROPY, Configuration.AnisoLevel); realDevice->SetSamplerState(i, D3DSAMP_MINFILTER, Configuration.ScaleFilter); realDevice->SetSamplerState(i, D3DSAMP_MIPMAPLODBIAS, *(DWORD *)&Configuration.LODBias); } // Set variables dependent on configuration DWORD FogPixelMode, FogVertexMode, RangedFog; if(Configuration.FogMode == 2) { FogVertexMode = D3DFOG_LINEAR; FogPixelMode = D3DFOG_NONE; RangedFog = 1; } else if(Configuration.FogMode == 1) { FogVertexMode = D3DFOG_LINEAR; FogPixelMode = D3DFOG_NONE; RangedFog = 0; } else { FogVertexMode = D3DFOG_NONE; FogPixelMode = D3DFOG_LINEAR; RangedFog = 0; } realDevice->SetRenderState(D3DRS_FOGVERTEXMODE, FogVertexMode); realDevice->SetRenderState(D3DRS_FOGTABLEMODE, FogPixelMode); realDevice->SetRenderState(D3DRS_RANGEFOGENABLE, RangedFog); realDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, (Configuration.AALevel > 0)); LOG::logline("<< D3D Proxy CreateDevice"); return D3D_OK; }
HRESULT APPLICATION::Init(HINSTANCE hInstance, int width, int height, bool windowed) { debug.Print("Application initiated"); //Create Window Class WNDCLASS wc; memset(&wc, 0, sizeof(WNDCLASS)); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)::DefWindowProc; wc.hInstance = hInstance; wc.lpszClassName = "D3DWND"; //Register Class and Create new Window RegisterClass(&wc); m_mainWindow = CreateWindow("D3DWND", "Example 5.6: Frustum Culling", WS_EX_TOPMOST, 0, 0, width, height, 0, 0, hInstance, 0); SetCursor(NULL); ShowWindow(m_mainWindow, SW_SHOW); UpdateWindow(m_mainWindow); //Create IDirect3D9 Interface IDirect3D9* d3d9 = Direct3DCreate9(D3D_SDK_VERSION); if(d3d9 == NULL) { debug.Print("Direct3DCreate9() - FAILED"); return E_FAIL; } //Check that the Device supports what we need from it D3DCAPS9 caps; d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps); //Hardware Vertex Processing or not? int vp = 0; if(caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; else vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; //Check vertex & pixelshader versions if(caps.VertexShaderVersion < D3DVS_VERSION(2, 0) || caps.PixelShaderVersion < D3DPS_VERSION(2, 0)) { debug.Print("Warning - Your graphic card does not support vertex and pixelshaders version 2.0"); } //Set D3DPRESENT_PARAMETERS D3DPRESENT_PARAMETERS d3dpp; d3dpp.BackBufferWidth = width; d3dpp.BackBufferHeight = height; d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; d3dpp.BackBufferCount = 1; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.MultiSampleQuality = 0; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = m_mainWindow; d3dpp.Windowed = windowed; d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; d3dpp.Flags = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; //Create the IDirect3DDevice9 if(FAILED(d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_mainWindow, vp, &d3dpp, &m_pDevice))) { debug.Print("Failed to create IDirect3DDevice9"); return E_FAIL; } //Release IDirect3D9 interface d3d9->Release(); D3DXCreateFont(m_pDevice, 18, 0, 0, 1, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial", &m_pFont); //Create m_light ::ZeroMemory(&m_light, sizeof(m_light)); m_light.Type = D3DLIGHT_DIRECTIONAL; m_light.Ambient = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); m_light.Diffuse = D3DXCOLOR(0.9f, 0.9f, 0.9f, 1.0f); m_light.Specular = D3DXCOLOR(0.5f, 0.5f, 0.5f, 1.0f); m_light.Direction = D3DXVECTOR3(0.0f, -1.0f, 0.0f); m_pDevice->SetLight(0, &m_light); m_pDevice->LightEnable(0, true); //Set sampler state for(int i=0;i<4;i++) { m_pDevice->SetSamplerState(i, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(i, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); m_pDevice->SetSamplerState(i, D3DSAMP_MIPFILTER, D3DTEXF_POINT); } //Init camera m_camera.Init(m_pDevice); //Load objects LoadObjectResources(m_pDevice); //Create 2D Line D3DXCreateLine(m_pDevice, &m_pLine); //Create city m_city.Init(INTPOINT(25, 25)); m_camera.m_focus = m_city.GetCenter(); //Init mouse m_mouse.InitMouse(m_pDevice, m_mainWindow); return S_OK; }
bool CScreen::SetDefaultGraphicsSettings3D() { HRESULT hr; D3DXMATRIX identityTransform, projTransform; D3DXMatrixIdentity(&identityTransform); D3DXMatrixPerspectiveFovLH(&projTransform, D3DX_PI / 4, 1.0f, 1.0f, 100.0f); D3DXVECTOR3 vEyePt(-10.0f, -10.0f, 10.0f); D3DXVECTOR3 vLookatPt(0.0f, 0.0f, 0.0f); D3DXVECTOR3 vUpVec(0.0f, 0.0f, 1.0f); D3DXMATRIXA16 matView; D3DXMatrixLookAtLH(&matView, &vEyePt, &vLookatPt, &vUpVec); D3DCOLORVALUE lightColor = {1.0f, 1.0f, 1.0f, 1.0f}; D3DXVECTOR3 lightDir(-1.0f, 0.7f, -0.2f); D3DLIGHT9 light; setupDirectionalLight(light, lightColor, lightDir); D3DMATERIAL9 mtrl; ZeroMemory( &mtrl, sizeof( D3DMATERIAL9 ) ); mtrl.Diffuse = lightColor; mtrl.Ambient = lightColor; mtrl.Ambient.g = 0.0f; CGraphicsManager *pGraphicsManager = CGraphicsManager::GetInstance(); IDirect3DDevice9 *pDevice = pGraphicsManager->GetDevice(); CCamera * pCamera = CCamera::GetInstance(); hr = pDevice->SetTransform(D3DTS_WORLD, &identityTransform); if(FAILED(hr)) { LogErrorHr("Failed to set world transform", hr); return false; } D3DXMATRIX camViewMat = pCamera->GetViewMatrix(); hr = pDevice->SetTransform(D3DTS_VIEW, &camViewMat); if(FAILED(hr)) { LogErrorHr("Failed to set view transform", hr); return false; } D3DXMATRIX camProjMat = pCamera->GetProjectionMatrix(); hr = pDevice->SetTransform(D3DTS_PROJECTION, &camProjMat); if(FAILED(hr)) { LogErrorHr("Failed to set projection transform", hr); return false; } hr = pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); if(FAILED(hr)) { LogErrorHr("Failed to set cull mode render state", hr); return false; } hr = pDevice->SetLight(0, &light); if(FAILED(hr)) { LogErrorHr("Failed to set light 0", hr); return false; } hr = pDevice->SetMaterial(&mtrl); if(FAILED(hr)) { LogErrorHr("Failed to set material", hr); return false; } hr = pDevice->LightEnable(0, TRUE); if(FAILED(hr)) { LogErrorHr("Failed to enable light", hr); return false; } hr = pDevice->SetRenderState(D3DRS_AMBIENT, 0x00202020); if(FAILED(hr)) { LogErrorHr("Failed to set ambient color", hr); return false; } hr = pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE); if(FAILED(hr)) { LogErrorHr("Failed to enable z", hr); return false; } hr = pDevice->SetRenderState(D3DRS_ZWRITEENABLE , TRUE); if(FAILED(hr)) { LogErrorHr("Failed to set z write enabled", hr); return false; } hr = pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR); if(FAILED(hr)) { LogErrorHr("Failed to set sampler state - MINFILTER", hr); return false; } hr = pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR); if(FAILED(hr)) { LogErrorHr("Failed to set sampler state - MAGFILTER", hr); return false; } hr = pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR); if(FAILED(hr)) { LogErrorHr("Failed to set sampler state - MIPFILTER", hr); return false; } return true; }