void DevState::createCleanState() { if (dwMyThread != 0) { ods("D3D9: CreateCleanState from other thread."); } Stash<DWORD> stashThread(&dwMyThread, GetCurrentThreadId()); if (pSB) pSB->Release(); pSB = NULL; IDirect3DStateBlock9* pStateBlock = NULL; dev->CreateStateBlock(D3DSBT_ALL, &pStateBlock); if (! pStateBlock) return; pStateBlock->Capture(); dev->CreateStateBlock(D3DSBT_ALL, &pSB); if (! pSB) { pStateBlock->Release(); return; } D3DVIEWPORT9 vp; dev->GetViewport(&vp); dev->SetVertexShader(NULL); dev->SetPixelShader(NULL); dev->SetFVF(D3DFVF_TLVERTEX); dev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); dev->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // 0x16 dev->SetRenderState(D3DRS_WRAP0, FALSE); // 0x80 dev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); dev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ONE); dev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); dev->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); dev->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATER); dev->SetRenderState(D3DRS_ZENABLE, FALSE); dev->SetRenderState(D3DRS_ZWRITEENABLE, FALSE); dev->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); dev->SetRenderState(D3DRS_COLORVERTEX, FALSE); dev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE); dev->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); dev->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE); dev->SetRenderState(D3DRS_LIGHTING, FALSE); pSB->Capture(); pStateBlock->Apply(); pStateBlock->Release(); }
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); }
void CubeMesh::DrawRay(const D3DXVECTOR2& screenPos, const D3DXMATRIX& matWorld, const D3DXMATRIX& matView, const D3DXMATRIX& matProj) { HRESULT hr = S_FALSE; GraphicsDevice* pGDevice = GraphicsDevice::getInstance(); IDirect3DDevice9* pDevice = pGDevice->m_pD3DDevice; Ray ray = CalcPickingRay((int)screenPos.x, (int)screenPos.y, pGDevice->mCubeViewport, matView, matProj ); PCVertex rayLine[] = { {D3DXVECTOR3(0.0f,0.0f,0.0f), D3DCOLOR_ARGB(255,255,0,0)}, {ray.Origin + 1000*ray.Direction, D3DCOLOR_ARGB(255,255,0,0)}, }; PCVertex intersectPoint[] = { {p, D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(0.5f,0.0f,0.0f), D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(0.0f,0.5f,0.0f), D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(0.0f,0.0f,0.5f), D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(-0.5f,0.0f,0.0f), D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(0.0f,-0.5f,0.0f), D3DCOLOR_ARGB(255,0,0,255)}, {p+D3DXVECTOR3(0.0f,0.0f,-0.5f), D3DCOLOR_ARGB(255,0,0,255)}, }; pGDevice->SetViewport(pGDevice->mCubeViewport); pDevice->SetVertexShader(NULL); pDevice->SetPixelShader(NULL); V(pDevice->SetTransform(D3DTS_WORLD, &matWorld)); V(pDevice->SetTransform(D3DTS_VIEW, &matView)); V(pDevice->SetTransform(D3DTS_PROJECTION, &matProj)); V(pDevice->SetRenderState(D3DRS_LIGHTING, FALSE)); V(pDevice->SetRenderState(D3DRS_ZENABLE, FALSE)); V(pDevice->SetFVF(D3DFVF_XYZ | D3DFVF_DIFFUSE)); V(pDevice->DrawPrimitiveUP(D3DPT_LINELIST, 1, rayLine, sizeof(PCVertex))); V(pDevice->DrawPrimitiveUP(D3DPT_POINTLIST, 7, intersectPoint, sizeof(PCVertex))); V(pDevice->SetFVF(NULL)); V(pDevice->SetRenderState(D3DRS_ZENABLE, TRUE)); V(pDevice->SetRenderState(D3DRS_LIGHTING, TRUE)); pGDevice->ResetViewport(); }
void TexCube::Render() { if(NULL == m_pWnd || NULL == m_pVB || NULL == m_pIB) { return; } IDirect3DDevice9* pDev = m_pWnd->GetD3DDevice(); pDev->SetStreamSource(0, m_pVB, 0, sizeof(SRNormalTextureVertex)); pDev->SetFVF(SRNormalTextureVertex::FVF); pDev->SetIndices(m_pIB); pDev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 24, 0, 12); }
fResult f2dGraphics2DImpl::Begin() { fResult tRet = f2dGraphicsImpl::Begin(); if(tRet != FCYERR_OK) return tRet; if(!(m_pIB && m_pVB)) return FCYERR_INTERNALERR; m_pParent->SubmitVD(NULL); setColorBlendType(m_ColorBlendType); IDirect3DDevice9* pDev = (IDirect3DDevice9*)m_pParent->GetHandle(); pDev->SetFVF(FVF); pDev->SetStreamSource(0, m_pVB, 0, sizeof(f2dGraphics2DVertex)); pDev->SetIndices(m_pIB); if(FAILED(pDev->BeginScene())) { m_bInRender = false; return FCYERR_INTERNALERR; } else { // 准备缓冲区 m_pVB->Lock(0, m_VBMaxCount*sizeof(f2dGraphics2DVertex), (void**)&m_pVBData, D3DLOCK_DISCARD); m_pIB->Lock(0, m_IBMaxCount*sizeof(fuShort), (void**)&m_pIBData, D3DLOCK_DISCARD); m_VBUsedCount = 0; m_IBUsedCount = 0; m_VBAlloced = 0; m_IBAlloced = 0; return FCYERR_OK; } }
// 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; }
int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int){ Application app("Okienko"); // Inicjalizacja Direct3D IDirect3D9* d3d = Direct3DCreate9(D3D_SDK_VERSION); // Parametry urzadzenia D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = true; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferCount = 1; d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; d3dpp.BackBufferHeight = app.get_height(); d3dpp.BackBufferWidth = app.get_width(); d3dpp.EnableAutoDepthStencil = true; d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; // Tworzenie urzadzenia IDirect3DDevice9* dev; d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, app.window_handle(), D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &dev); app.init_font(dev, "Courier New"); dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); // Brak obcinania scian dev->SetRenderState(D3DRS_LIGHTING, false); // Brak swiatla // Inicjalizacja kamery // D3DXVECTOR3 eye(0, 5, -10); D3DXVECTOR3 target(0, 0, 0); D3DXVECTOR3 up(0, 1, 0); D3DXMATRIXA16 view; D3DXMATRIX mProjection; D3DXMatrixPerspectiveFovLH(&mProjection, D3DX_PI * 0.5f, app.get_width()/(float)app.get_height(), 1, 50); dev->SetTransform(D3DTS_PROJECTION, &mProjection); // Model pudelka Vertex box[] = { // X Y Z Color { 1, 1, 1, D3DCOLOR_XRGB(255, 255, 255)}, {-1, 1, 1, D3DCOLOR_XRGB(0, 255, 255)}, {-1, 1, -1, D3DCOLOR_XRGB(0, 255, 0)}, { 1, 1, -1, D3DCOLOR_XRGB(255, 255, 0)}, { 1, -1, 1, D3DCOLOR_XRGB(255, 0, 255)}, {-1, -1, 1, D3DCOLOR_XRGB(0, 0, 255)}, {-1, -1, -1, D3DCOLOR_XRGB(0, 0, 0)}, { 1, -1, -1, D3DCOLOR_XRGB(255, 0, 0)}, box[0], box[4], box[1], box[5], box[2], box[6], box[3], box[7], box[0], box[4] }; int box_size = (16 + 2) * sizeof(Vertex); // Tworzenie bufora wierzcholkow IDirect3DVertexBuffer9* box_buffer; dev->CreateVertexBuffer(box_size, 0, Vertex_Format, D3DPOOL_MANAGED, &box_buffer, NULL); VOID* pVoid; box_buffer->Lock(0, box_size, (void**)&pVoid, 0); memcpy(pVoid, box, box_size); box_buffer->Unlock(); dev->SetFVF(Vertex_Format); dev->SetStreamSource(0, box_buffer, 0, sizeof(Vertex)); float radius = 3; while(app.running()){ float alfa = app.get_alfa(); float beta = app.get_beta(); // Aktualizujemy kamere D3DXVECTOR3 eye( radius * cos(alfa) * sin(beta), radius * cos(beta), radius * sin(alfa) * sin(beta) ); D3DXMatrixLookAtLH(&view, &eye, &target, &up); dev->SetTransform(D3DTS_VIEW, &view); // Rysujemy pudeleczko dev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0); dev->BeginScene(); for(int i = 0; i < 2; i++) dev->DrawPrimitive(D3DPT_TRIANGLEFAN, i*4, 2); dev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 8, 8); app.print(10, 10, "Mysz X = %d", app.get_mouse_x()); app.print(10, 24, "Mysz Y = %d", app.get_mouse_y()); dev->EndScene(); dev->Present(NULL, NULL, NULL, NULL); } // Zwalniamy zasoby box_buffer->Release(); dev->Release(); d3d->Release(); return 0; }
bool CSprite::Render(const SFloatPoint & _pos) const { HRESULT hr; CGraphicsManager *pGraphicsManager = CGraphicsManager::GetInstance(); IDirect3DDevice9 *pDevice = pGraphicsManager->GetDevice(); D3DXMATRIX matWorld, matTranslation; hr = pDevice->GetTransform(D3DTS_WORLD, &matWorld); if(FAILED(hr)) { LogErrorHr("Failed to get world transform", hr); return false; } D3DXMatrixTranslation(&matTranslation, _pos.m_fX, _pos.m_fY, 0.0f); hr = pDevice->MultiplyTransform(D3DTS_WORLD, &matTranslation); if(FAILED(hr)) { LogErrorHr("Failed to multiply world transform", hr); return false; } hr = pDevice->SetStreamSource(0, m_pVertexBuffer, 0, sizeof(SFaceVertex)); if(FAILED(hr)) { LogErrorHr("Failed to set stream source", hr); return false; } hr = pDevice->SetFVF(SFaceVertex::_fvf); if(FAILED(hr)) { LogErrorHr("Failed to set FVF", hr); return false; } hr = pDevice->SetTexture(0, m_pTexture); if(FAILED(hr)) { LogErrorHr("Failed to set texture", hr); return false; } hr = pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2); if(FAILED(hr)) { LogErrorHr("Failed to draw primitive", hr); return false; } hr = pDevice->SetTexture(0, NULL); if(FAILED(hr)) { LogErrorHr("Failed to reset texture", hr); return false; } hr = pDevice->SetTransform(D3DTS_WORLD, &matWorld); if(FAILED(hr)) { LogErrorHr("Failed to restore world transform", hr); return false; } return true; }