// Initialize D3D
bool D3DRenderer::Initialize()
{
HRESULT hr;
D3D_DRIVER_TYPE m_pDriverType;
D3D_FEATURE_LEVEL m_pFeatureLevel;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE( driverTypes );
// Create the D3D device
D3D_FEATURE_LEVEL featureLevel[]=
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
int num= ARRAYSIZE(featureLevel);
// Create device, context and swapchain
DXGI_SWAP_CHAIN_DESC sdDesc;
ZeroMemory(&sdDesc, sizeof(sdDesc));
sdDesc.BufferDesc.ScanlineOrdering=DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
sdDesc.BufferDesc.Scaling=DXGI_MODE_SCALING_UNSPECIFIED;
sdDesc.BufferDesc.RefreshRate.Numerator = 60;
sdDesc.BufferDesc.RefreshRate.Denominator = 1;
sdDesc.BufferDesc.Width = m_pWindow->GetWidth();
sdDesc.BufferDesc.Height = m_pWindow->GetHeight();
sdDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sdDesc.BufferCount = 1;
sdDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sdDesc.SampleDesc.Count = 1;
sdDesc.SampleDesc.Quality = 0;
sdDesc.OutputWindow = m_pWindow->GetHwnd();
sdDesc.Windowed = TRUE;
for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
{
m_pDriverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDeviceAndSwapChain( NULL, m_pDriverType, NULL, NULL, featureLevel, num,
D3D11_SDK_VERSION, &sdDesc, &m_pSwapChain, &m_pDevice, &m_pFeatureLevel, &m_pDeviceContext );
if( SUCCEEDED( hr ) )
break;
}
if(FAILED(hr))
return FALSE;
// Make and set D3D11 render target objects
hr = m_pSwapChain->GetBuffer(0, __uuidof(*m_pRTTexture2D), reinterpret_cast<void**>(&m_pRTTexture2D));
CHECK_FAILURE(hr, "Could not create render target texture.");
hr = m_pDevice->CreateRenderTargetView(m_pRTTexture2D, NULL, &m_pRTView);
CHECK_FAILURE(hr, "Could not create render target view.");
// Make and set depth stencil
D3D11_TEXTURE2D_DESC dsTexDesc;
ZeroMemory(&dsTexDesc, sizeof(dsTexDesc));
dsTexDesc.Width = m_pWindow->GetWidth();
dsTexDesc.Height = m_pWindow->GetHeight();
dsTexDesc.MipLevels = 1;
dsTexDesc.ArraySize = 1;
dsTexDesc.Format = DXGI_FORMAT_D32_FLOAT;
dsTexDesc.SampleDesc.Count = 1;
dsTexDesc.SampleDesc.Quality = 0;
dsTexDesc.Usage = D3D11_USAGE_DEFAULT;
dsTexDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
dsTexDesc.CPUAccessFlags = 0;
dsTexDesc.MiscFlags = 0;
hr = m_pDevice->CreateTexture2D(&dsTexDesc, NULL, &m_pDSTexture2D);
CHECK_FAILURE(hr, "Could not create depth stencil texture.");
hr = m_pDevice->CreateDepthStencilView(m_pDSTexture2D, NULL, &m_pDSView);
CHECK_FAILURE(hr, "Could not create depth stencil view.");
m_pDeviceContext->OMSetRenderTargets(1, &m_pRTView, m_pDSView);
// Set depth stencil state
D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
depthStencilDesc.DepthEnable = TRUE;
depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D11_COMPARISON_ALWAYS;
depthStencilDesc.StencilEnable = FALSE;
hr = m_pDevice->CreateDepthStencilState(&depthStencilDesc, &m_pDSState);
CHECK_FAILURE(hr, "Could not create depth stencil state.");
m_pDeviceContext->OMSetDepthStencilState(m_pDSState,0);
// Setup the viewport
D3D11_VIEWPORT vp;
ZeroMemory(&vp, sizeof(vp));
vp.Width = m_pWindow->GetWidth();
vp.Height =m_pWindow->GetHeight();
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
m_pDeviceContext->RSSetViewports( 1, &vp );
// Compile the vertex shader
ID3DBlob* pVSBlob = NULL;
hr = CompileShaderFromFile( L"Main.fx", "VS", "vs_4_0", &pVSBlob );
CHECK_FAILURE(hr, "Could not Compile Vertex Shader");
// Create the vertex shader
hr = m_pDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &m_pVertexShader );
CHECK_FAILURE(hr, "Could not Create Vertex Shader");
// Define the input layout
D3D11_INPUT_ELEMENT_DESC layout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
};
UINT numElements = sizeof( layout ) / sizeof( layout[0] );
// Create the input layout
hr = m_pDevice->CreateInputLayout( layout, numElements, pVSBlob->GetBufferPointer(),
pVSBlob->GetBufferSize(), &m_pVertexLayout );
pVSBlob->Release();
CHECK_FAILURE(hr, "Could not create ");
// Set the input layout
m_pDeviceContext->IASetInputLayout( m_pVertexLayout );
// Compile the pixel shader
ID3DBlob* pPSBlob = NULL;
hr = CompileShaderFromFile( L"Main.fx", "PS", "ps_4_0", &pPSBlob );
CHECK_FAILURE(hr, "Could not Compile Pixel Shader");
// Create the pixel shader
hr = m_pDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &m_pPixelShader);
pPSBlob->Release();
CHECK_FAILURE(hr, "Could not create Pixel Shader");
// Create vertex buffer
SimpleVertex vertices[] =
{
{ XMFLOAT3( -1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f,-1.0f, 1.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f,-1.0f, 1.0f ), XMFLOAT4( 1.0f, 0.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f,-1.0f, 1.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( -1.0f, -1.0f, 1.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
{ XMFLOAT3( 1.0f, 1.0f, 0.0f ), XMFLOAT4( 1.0f, 1.0f, 0.0f, 1.0f ) },
{ XMFLOAT3( 1.0f,-1.0f, 1.0f ), XMFLOAT4( 1.0f, 1.0f, 1.0f, 1.0f ) },
};
D3D11_BUFFER_DESC bd;
ZeroMemory(&bd, sizeof(bd));
bd.Usage = D3D11_USAGE_DEFAULT;
bd.ByteWidth = sizeof( SimpleVertex ) *12;
bd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = vertices;
hr = m_pDevice->CreateBuffer( &bd, &InitData, &m_pVertexBuffer );
CHECK_FAILURE(hr, "Could not create Vertex Buffer.");
// Set vertex buffer
UINT stride = sizeof( SimpleVertex );
UINT offset = 0;
m_pDeviceContext->IASetVertexBuffers( 0, 1, &m_pVertexBuffer, &stride, &offset );
// Set primitive topology
m_pDeviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
D3D11_RASTERIZER_DESC RSdesc;
ZeroMemory(&RSdesc, sizeof(RSdesc));
RSdesc.CullMode=D3D11_CULL_NONE;
RSdesc.MultisampleEnable=FALSE;
RSdesc.AntialiasedLineEnable=FALSE;
RSdesc.DepthClipEnable=TRUE;
RSdesc.FillMode=D3D11_FILL_SOLID;
RSdesc.FrontCounterClockwise=FALSE;
RSdesc.ScissorEnable=FALSE;
m_pDevice->CreateRasterizerState(&RSdesc, &m_pRasterStateNoCulling);
m_pDeviceContext->RSSetState(m_pRasterStateNoCulling);
//Create TExture for Staging resource
D3D11_TEXTURE2D_DESC descStaging;
ZeroMemory(&descStaging, sizeof(descStaging));
descStaging.Width = m_pWindow->GetWidth();;
descStaging.Height= m_pWindow->GetHeight();;
descStaging.MipLevels = 1;
descStaging.ArraySize = 1;
descStaging.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
descStaging.SampleDesc.Count = 1;
descStaging.SampleDesc.Quality = 0;
descStaging.Usage = D3D11_USAGE_STAGING;
descStaging.BindFlags = 0;
descStaging.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
descStaging.MiscFlags = 0;
hr=m_pDevice->CreateTexture2D(&descStaging,NULL,&m_sRTTexture2D);
CHECK_FAILURE(hr, "Could not create texture for staging resource.");
return true;
}
//--------------------------------------------------------------------------------------
// Helper for compiling shaders with D3DX11
//--------------------------------------------------------------------------------------
HRESULT D3DRenderer::CompileShaderFromFile( WCHAR* szFileName, LPCSTR szEntryPoint, LPCSTR szShaderModel, ID3DBlob** ppBlobOut )
{
HRESULT hr = S_OK;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* pErrorBlob;
hr = D3DCompileFromFile(szFileName, NULL, NULL, szEntryPoint, szShaderModel,
dwShaderFlags, 0, ppBlobOut, &pErrorBlob);
if( FAILED(hr) )
{
if( pErrorBlob != NULL )
OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
if( pErrorBlob ) pErrorBlob->Release();
return hr;
}
if( pErrorBlob ) pErrorBlob->Release();
return S_OK;
}
Run(TestCase* pTest)
{
strcpy_s(m_lastError, "");
ClearRenderTargetView();
ClearDepthStencilView();
// Render before NVAPI test enabled
m_pDeviceContext->VSSetShader( m_pVertexShader, NULL, 0 );
m_pDeviceContext->PSSetShader( m_pPixelShader, NULL, 0 );
m_pDeviceContext->Draw( 6, 0 );
}