D3D9RenderInstanceBuffer::D3D9RenderInstanceBuffer(IDirect3DDevice9 &d3dDevice, const RenderInstanceBufferDesc &desc) :
	RenderInstanceBuffer(desc)
#if RENDERER_INSTANCING
	,m_d3dDevice(d3dDevice)
#endif
{
#if RENDERER_INSTANCING
	m_d3dVertexBuffer = 0;
#endif
	
	m_usage      = 0;
	m_pool       = D3DPOOL_MANAGED;
	m_bufferSize = (UINT)(desc.maxInstances * m_stride);
	
#if RENDERER_ENABLE_DYNAMIC_VB_POOLS
	if(desc.hint==RenderInstanceBuffer::HINT_DYNAMIC)
	{
		m_usage = D3DUSAGE_DYNAMIC;
		m_pool  = D3DPOOL_DEFAULT;
	}
#endif
	
	onDeviceReset();

#if RENDERER_INSTANCING	
	if(m_d3dVertexBuffer)
	{
		m_maxInstances = desc.maxInstances;
	}
#else
	m_maxInstances = desc.maxInstances;
	mInstanceBuffer = PX_ALLOC(m_maxInstances*m_stride);
#endif
}
	bool D3D9WPFRenderer::checkResize( bool isDeviceLost )
	{
		bool isDeviceReset = false;
		//resize the system if the desired width or height is more than we can support.
		if ( mDesiredWidth > m_displayWidth
			|| mDesiredHeight > m_displayHeight
			|| isDeviceLost )
		{
			m_displayWidth = PxMax( mDesiredWidth, m_displayWidth );
			m_displayHeight = PxMax( mDesiredHeight, m_displayHeight );
			if ( isDeviceLost )
			{
				physx::PxU64 res = m_d3dDevice->TestCooperativeLevel();
				if(res == D3D_OK || res == D3DERR_DEVICENOTRESET)	//if device is lost, device has to be ready for reset
				{
					isDeviceReset = true;
					onDeviceLost();
					onDeviceReset();
				}
			}
			else
			{
				releaseSurface();
				releaseDepthStencilSurface();
				allocateSurface();
				buildDepthStencilSurface();
				// set out initial states...
				m_d3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);
				m_d3dDevice->SetRenderState(D3DRS_LIGHTING, 0);
				m_d3dDevice->SetRenderState(D3DRS_ZENABLE,  1);
			}
		}
		//else just mess with the viewport so we only render pixel-by-pixel
		D3DVIEWPORT9 viewport = {0};
		m_d3dDevice->GetViewport(&viewport);
		if ( viewport.Width != mDesiredWidth
			|| viewport.Height != mDesiredHeight )
		{
			viewport.X = 0;
			viewport.Y = 0;
			viewport.Width  = (DWORD)mDesiredWidth;
			viewport.Height = (DWORD)mDesiredHeight;
			viewport.MinZ =  0.0f;
			viewport.MaxZ =  1.0f;
			m_d3dDevice->SetViewport(&viewport);
		}
		return isDeviceReset;
	}