Question

I'm using DirectX 11 SDK and render something to the texture (and it's works). Than use D3DX11SaveTextureToFile to save the output to the PNG texture. Everything works, but I don't get transparency.

I want to have the background (space where none of the elements have been rendered) transparent. Instead of this, I get some background.

I was trying to change the float ClearColor[4] for the ClearRenderTargetView function to the{ 0.0f, 0.0f, 0.0f, 1.0f } because I thought that last one is alpha and 1.0f with give me transparency, but that haven't worked for me (the same for 0.0f).

Here is the part of my code that may be useful:

Blend states (which I believe are good, they works for rendering transparent elements on screen - of course screen of monitor always have some "background", the PNG file not):

D3D11_BLEND_DESC blendDesc;
ZeroMemory(&blendDesc, sizeof(D3D11_BLEND_DESC) );
blendDesc.AlphaToCoverageEnable = false;
blendDesc.IndependentBlendEnable = false;        
blendDesc.RenderTarget[0].BlendEnable = true;
blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;

blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; 
blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; 
blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL ;

ID3D11BlendState * blendState;

if(FAILED(device->CreateBlendState(&blendDesc, &blendState))){
        //...
}

g_pImmediateContext->OMSetBlendState(blendState,NULL,0xffffffff);

And:

// Setup the render target texture description.
textureDesc.Width = textureWidth;
textureDesc.Height = textureHeight;
textureDesc.MipLevels = 1;
textureDesc.ArraySize = 1;
textureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
textureDesc.SampleDesc.Count = 1;
textureDesc.Usage = D3D11_USAGE_DEFAULT;
textureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
textureDesc.CPUAccessFlags = 0;
textureDesc.MiscFlags = 0;
textureDesc.SampleDesc.Quality = 0;
textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;    
...
//Create the render target texture.
result = device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture);  
...
result = device->CreateShaderResourceView(renderTargetTexture, 
    &shaderResourceViewDesc, &shaderResourceView);  
...
g_pImmediateContext->OMSetRenderTargets(1, &renderTargetView, 
    g_pDepthStencilView);
float ClearColor[4] = { 0.0f, 0.0f, 0.0f, 1.0f }; //red, green, blue, alpha
g_pImmediateContext->ClearRenderTargetView( renderTargetView, ClearColor );

//clear the depth buffer to 1.0 (max depth)
g_pImmediateContext->ClearDepthStencilView( g_pDepthStencilView, 
    D3D11_CLEAR_DEPTH, 1.0f, 0 ); 
...
//render here
...
D3DX11SaveTextureToFile(g_pImmediateContext, renderTargetTexture, 
    D3DX11_IFF_BMP, CommonFunctions::s2ws(newTextureName).c_str());
Was it helpful?

Solution

If you want to save a PNG file, use D3DX11_IFF_PNG instead of D3DX11_IFF_BMP.


As PolGraphic mentioned in his comment, it might be necessary to change the blend state from D3D11_BLEND_SRC_ALPHA and D3D11_BLEND_INV_SRC_ALPHA to D3D11_BLEND_ONE which works for PNG. However, in most cases the default setting SRC_ALPHA / INV_SRC_ALPHA should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top