D3D9RendererTexture2D::D3D9RendererTexture2D(IDirect3DDevice9 &d3dDevice, D3D9Renderer& renderer, const RendererTexture2DDesc &desc)
       :RendererTexture2D(desc)
	,m_d3dDevice(d3dDevice)
{
	m_d3dTexture     = 0;
	//managed textures can't be locked.
#ifdef	DIRECT3D9_SUPPORT_D3DUSAGE_DYNAMIC
	m_usage          = renderer.canUseManagedResources() ? 0 : D3DUSAGE_DYNAMIC;
#else
	m_usage          = 0;
#endif
	m_pool           = renderer.canUseManagedResources() ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT;
	m_format         = getD3D9TextureFormat(desc.format);
	m_d3dMinFilter   = getD3D9TextureFilter(desc.filter);
	m_d3dMagFilter   = getD3D9TextureFilter(desc.filter);
	m_d3dMipFilter   = getD3D9TextureFilter(desc.filter);
	m_d3dAddressingU = getD3D9TextureAddressing(desc.addressingU);
	m_d3dAddressingV = getD3D9TextureAddressing(desc.addressingV);
	if(desc.renderTarget)
	{
		m_usage = D3DUSAGE_RENDERTARGET; 
		m_pool  = D3DPOOL_DEFAULT;
	}
	if(isDepthStencilFormat(desc.format))
	{
		m_usage = D3DUSAGE_DEPTHSTENCIL; 
		m_pool  = D3DPOOL_DEFAULT;
	}
	onDeviceReset();
}
D3D9RendererInstanceBuffer::D3D9RendererInstanceBuffer(IDirect3DDevice9 &d3dDevice, D3D9Renderer& renderer, const RendererInstanceBufferDesc &desc) :
RendererInstanceBuffer(desc)
#if RENDERER_INSTANCING
	,m_d3dDevice(d3dDevice)
#endif
{
#if RENDERER_INSTANCING
	m_d3dVertexBuffer = 0;
#endif

	m_usage      = 0;
	m_pool       = renderer.canUseManagedResources() ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT;
	m_bufferSize = (UINT)(desc.maxInstances * m_stride);

#if RENDERER_ENABLE_DYNAMIC_VB_POOLS
	if(desc.hint==RendererInstanceBuffer::HINT_DYNAMIC )
	{
		m_usage = desc.registerInCUDA ? 0 : D3DUSAGE_DYNAMIC;
		m_pool  = D3DPOOL_DEFAULT;
	}
#endif

	onDeviceReset();

#if RENDERER_INSTANCING	
	if(m_d3dVertexBuffer)
	{
		m_maxInstances = desc.maxInstances;
	}
#else
	m_maxInstances = desc.maxInstances;
	mInstanceBuffer = malloc(m_maxInstances*m_stride); // PX_ALLOC(m_maxInstances*m_stride);
#endif
}
D3D9RendererVertexBuffer::D3D9RendererVertexBuffer(IDirect3DDevice9 &d3dDevice, D3D9Renderer& renderer, const RendererVertexBufferDesc &desc, bool deferredUnlock) :
RendererVertexBuffer(desc),
	m_d3dDevice(d3dDevice),
	m_savedData(NULL)
{
	m_d3dVertexBuffer = 0;
	m_deferredUnlock = deferredUnlock;

	m_usage      = 0;
	m_pool       = renderer.canUseManagedResources() ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT;
	m_bufferSize = (UINT)(desc.maxVertices * m_stride);

	m_bufferWritten = false;

#if RENDERER_ENABLE_DYNAMIC_VB_POOLS
	if(desc.hint==RendererVertexBuffer::HINT_DYNAMIC && !desc.canReadBack)
	{
		m_usage = desc.registerInCUDA ? 0 : D3DUSAGE_DYNAMIC;
		m_pool  = D3DPOOL_DEFAULT;
	}
#endif

	onDeviceReset();

	if(m_d3dVertexBuffer)
	{
		m_maxVertices = desc.maxVertices;
	}
}
D3D9RendererIndexBuffer::D3D9RendererIndexBuffer(IDirect3DDevice9 &d3dDevice, D3D9Renderer& renderer, const RendererIndexBufferDesc &desc) :
	RendererIndexBuffer(desc),
	m_d3dDevice(d3dDevice)
{
	m_d3dIndexBuffer = 0;

	m_usage      = 0;
	m_pool       = renderer.canUseManagedResources() ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT;
	PxU32	indexSize  = getFormatByteSize(desc.format);
	m_format     = getD3D9Format(desc.format);
	m_bufferSize = indexSize * desc.maxIndices;

#if RENDERER_ENABLE_DYNAMIC_VB_POOLS
	if(desc.hint == RendererIndexBuffer::HINT_DYNAMIC && !desc.canReadBack)
	{
		m_usage = desc.registerInCUDA ? 0 : D3DUSAGE_DYNAMIC;
		m_pool  = D3DPOOL_DEFAULT;
	}
#endif

	onDeviceReset();

	if(m_d3dIndexBuffer)
	{
		m_maxIndices = desc.maxIndices;
	}
}
D3D9RendererVertexBuffer::D3D9RendererVertexBuffer(IDirect3DDevice9 &d3dDevice, D3D9Renderer& renderer, const RendererVertexBufferDesc &desc) :
RendererVertexBuffer(desc),
	m_d3dDevice(d3dDevice),
	m_savedData(NULL)
{
	m_d3dVertexBuffer = 0;

	m_usage      = D3DUSAGE_WRITEONLY;
	m_lockFlags  = 0;
	m_pool       = renderer.canUseManagedResources() ? D3DPOOL_MANAGED : D3DPOOL_DEFAULT;
	m_bufferSize = (UINT)(desc.maxVertices * m_stride);

	m_bufferWritten = false;

#if RENDERER_ENABLE_DYNAMIC_VB_POOLS
	if(desc.hint==RendererVertexBuffer::HINT_DYNAMIC )
	{
		if (!desc.registerInCUDA)
		{
			m_usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY;
			// discard all that data if we are going to overwrite it again (saves some time waiting to copy everything back)
			m_lockFlags = D3DLOCK_DISCARD;
		}
		else
		{
			m_usage = 0;
		}

		m_pool  = D3DPOOL_DEFAULT;
	}
#endif

	onDeviceReset();

	if(m_d3dVertexBuffer)
	{
		m_maxVertices = desc.maxVertices;
	}
}