locked
I can't map the texture bytes to CPU memory.

    Question

  • // Create a descriptor for the render target buffer.
    	CD3D11_TEXTURE2D_DESC renderTargetDesc(
    		DXGI_FORMAT_R32G32B32A32_FLOAT,
    		static_cast<UINT>(m_renderTargetSize.Width),
    		static_cast<UINT>(m_renderTargetSize.Height),
    		1,
    		1,
    		D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE
    		);
    	renderTargetDesc.Usage = D3D11_USAGE_DEFAULT;
    	//renderTargetDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
    	renderTargetDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX | D3D11_RESOURCE_MISC_SHARED_NTHANDLE;
    
    	// Allocate a 2-D surface as the render target buffer.
    	DX::ThrowIfFailed(
    		m_d3dDevice->CreateTexture2D(
    		&renderTargetDesc,
    		nullptr,
    		&m_renderTarget
    		)
    		);
    	D3D11_RENDER_TARGET_VIEW_DESC renderTargetViewDesc;
    	renderTargetViewDesc.Format = renderTargetDesc.Format;
    	renderTargetViewDesc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
    	renderTargetViewDesc.Texture2D.MipSlice = 0;
    	DX::ThrowIfFailed(
    		m_d3dDevice->CreateRenderTargetView(
    		m_renderTarget.Get(),
    		nullptr,
    		&m_renderTargetView
    		)
    		);
    HRESULT hr = m_d3dContext->Map(m_renderTarget.Get(),0,D3D11_MAP_READ,0,&tmp);
    m_d3dContext->Unmap(m_renderTarget.Get(),0); // failed

    How should i set the CD3D11_TEXTURE2D_DESC  ?

    I just want to get bytes form a rendertargetview or a texture2D. what should i do?

    Monday, November 26, 2012 6:55 AM

Answers

  • Generally speaking you can't CPU access a render target, nor would you want to as it's extremely slow on most architectures.

    If you need to access the render target on the CPU, you would create a staging resource and copy it from the render target to the staging resource, then map the staging resource for CPU access. There are some additional complications here because if the render target is an MSAA surface you have to resolve it first.

    The "ScreenGrab" module in DirectXTK (and the standalone version of it in the DirectXTex package) does exactly this kind of 'capture' of a render target to generate a screenshot. As long as you aren't doing it very often, then performance impact is pretty tolerable. It is not the kind of thing, however, you would want to do every frame or even every few hundred frames.


    Monday, November 26, 2012 10:02 PM