locked
The debug setting app color error

    Question

  • My app has some image with alpha shadow.
    In the debug setting the shadow color is different form the origin color,
    but in the release setting the shadow color is the origin color.

    I think may be there is some variable not initialize.
    So, in the release setting the color is right because the debugger not fill the data to the memory,
    and in the debug setting the color is wrong because the debugger fill some data like 0xcdcdcdcd to the memory.

    but, I don't know which variable I forget to initialize.

    Can some one help me guess which variable I forget to initialize.

    【the origin image】

    left【the release setting】 right【the debug setting】

    【the code】
    the main function is 'void CEngineDeviceImpl::Render(IGuiFrame* pGuiFrame)'

    class CEngineDeviceImpl : public IEngineDevice
    {
    	bool												m_canvasCreated;
    	D3D11_VIEWPORT										m_canvasViewport;
    	Microsoft::WRL::ComPtr<ID3D11Texture2D>				m_canvasTex;
    	Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>	m_canvasSRV;
    	Microsoft::WRL::ComPtr<ID3D11RenderTargetView>		m_canvasRT;
    };
    
    void CEngineDeviceImpl::CreateCanvas(float canvasWidth, float canvasHeight)
    {
    	// Set the 3D rendering viewport to target the entire window.
    	m_canvasViewport = CD3D11_VIEWPORT(
    		0.0f,
    		0.0f,
    		canvasWidth,
    		canvasHeight
    		);
    
    	CD3D11_TEXTURE2D_DESC sceneDesc(DXGI_FORMAT_B8G8R8A8_UNORM, int(canvasWidth), int(canvasHeight),
    		1, 1, D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE);
    	DX::ThrowIfFailed(m_d3dDevice->CreateTexture2D(&sceneDesc, nullptr,
    		m_canvasTex.GetAddressOf()));
    	DX::ThrowIfFailed(m_d3dDevice->CreateRenderTargetView(m_canvasTex.Get(), nullptr,
    		m_canvasRT.ReleaseAndGetAddressOf()));
    	DX::ThrowIfFailed(m_d3dDevice->CreateShaderResourceView(m_canvasTex.Get(), nullptr,
    		m_canvasSRV.ReleaseAndGetAddressOf()));
    
    	m_canvasCreated = true;
    }
    
    void CEngineDeviceImpl::Render(IGuiFrame* pGuiFrame)
    {
    	ASSERT(pGuiFrame);
    
     	SetRenderTargetToCanvas();
    	SpritesBegin();
    	pGuiFrame->Render();
    	SpritesEnd();
    
    	SetRenderTargetToBackbuffer();
    	SpritesBegin();
    	DrawCanvasImageToBackbuffer();
    	SpritesEnd();
    
    	ID3D11ShaderResourceView* null[] = { nullptr, nullptr };
    	m_d3dContext->PSSetShaderResources(0, 2, null);
    }
    
    void CEngineDeviceImpl::SetRenderTargetToCanvas()
    {	
    	m_d3dContext->RSSetViewports(1, &m_canvasViewport);
    	m_d3dContext->OMSetRenderTargets(1, m_canvasRT.GetAddressOf(), nullptr);
    	m_d3dContext->ClearRenderTargetView(m_canvasRT.Get(), Colors::Orange);
    }
    
    void CEngineDeviceImpl::SetRenderTargetToBackbuffer()
    {
    	// Reset the viewport to target the whole screen.
    	auto viewport = m_deviceResources->GetScreenViewport();
    	m_d3dContext->RSSetViewports(1, &viewport);
    
    	// Reset render targets to the screen.
    	ID3D11RenderTargetView *const targets[1] = { m_deviceResources->GetBackBufferRenderTargetView() };
    	m_d3dContext->OMSetRenderTargets(1, targets, nullptr);
    }
    
    void CEngineDeviceImpl::SpritesBegin()
    {
    	// Get the 2D orientation transform.
    	D2D1::Matrix3x2F _2DTransform = m_deviceResources->GetOrientationTransform2D();
    
    	// Convert the Matrix3X2F tranform to XMMATRIX.
    	DirectX::XMMATRIX transformMaxtrix =
    	{
    		_2DTransform._11, _2DTransform._12, 0, 0,
    		_2DTransform._21, _2DTransform._22, 0, 0,
    		0, 0, 1, 0,
    		_2DTransform._31, _2DTransform._32, 0, 1
    	};
    
    	// To make sure the sprite is drawn in the correct position when the device orientation changes, 
    	// we pass the 2D orientation transform to the Begin function.
    	m_sprites->Begin(SpriteSortMode_Deferred, m_states->NonPremultiplied(),
    		nullptr, nullptr, nullptr, nullptr, transformMaxtrix);
    }
    
    void CEngineDeviceImpl::SpritesEnd()
    {
    	m_sprites->End();
    }


    • Edited by lihuipeng49 Thursday, February 12, 2015 12:10 PM
    Thursday, February 12, 2015 12:07 PM

Answers

  • Thank you!

    I know the reason.

    the wrong:

    	m_sprites->Begin(SpriteSortMode_Deferred, m_states->NonPremultiplied(),
    		nullptr, nullptr, nullptr, nullptr, transformMaxtrix);

    the right:

    	m_sprites->Begin(SpriteSortMode_Deferred, m_states->Opaque(),
    		nullptr, nullptr, nullptr, nullptr, transformMaxtrix);
    

    the follow code is the key:

    #ifdef _DEBUG
    	auto color = DirectX::Colors::CornflowerBlue;
    #else
    	auto color = DirectX::Colors::Black;
    #endif
    
    	// Clear the back buffer and depth stencil view.
    	context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), color);
    	context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    

    Because the debug and the release use different color to fill the RT,
    and the SpriteBatch Begin with NonPremultiplied,
    so the color mixed different in debug and release.

    • Marked as answer by lihuipeng49 Friday, February 13, 2015 5:33 PM
    Friday, February 13, 2015 5:32 PM

All replies

  • try building a small sample here, we cannot guess.
    Friday, February 13, 2015 4:44 PM
  • Thank you!

    I know the reason.

    the wrong:

    	m_sprites->Begin(SpriteSortMode_Deferred, m_states->NonPremultiplied(),
    		nullptr, nullptr, nullptr, nullptr, transformMaxtrix);

    the right:

    	m_sprites->Begin(SpriteSortMode_Deferred, m_states->Opaque(),
    		nullptr, nullptr, nullptr, nullptr, transformMaxtrix);
    

    the follow code is the key:

    #ifdef _DEBUG
    	auto color = DirectX::Colors::CornflowerBlue;
    #else
    	auto color = DirectX::Colors::Black;
    #endif
    
    	// Clear the back buffer and depth stencil view.
    	context->ClearRenderTargetView(m_deviceResources->GetBackBufferRenderTargetView(), color);
    	context->ClearDepthStencilView(m_deviceResources->GetDepthStencilView(), D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
    

    Because the debug and the release use different color to fill the RT,
    and the SpriteBatch Begin with NonPremultiplied,
    so the color mixed different in debug and release.

    • Marked as answer by lihuipeng49 Friday, February 13, 2015 5:33 PM
    Friday, February 13, 2015 5:32 PM