Comment on each line of code to express the intent of this line of code. Dx11Dem
ID: 3829335 • Letter: C
Question
Comment on each line of code to express the intent of this line of code.
Dx11DemoBase::Dx11DemoBase( ) : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 )
{
}
Dx11DemoBase::~Dx11DemoBase( )
{
Shutdown( );
}
bool Dx11DemoBase::Initialize( HINSTANCE hInstance, HWND hwnd )
{
hInstance_ = hInstance;
hwnd_ = hwnd;
RECT dimensions;
GetClientRect( hwnd, &dimensions );
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes = ARRAYSIZE( driverTypes );
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) );
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0;
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT result;
unsigned int driver = 0;
for( driver = 0; driver < totalDriverTypes; ++driver )
{
result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0, creationFlags,
featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
&d3dDevice_, &featureLevel_, &d3dContext_ );
if( SUCCEEDED( result ) )
{
driverType_ = driverTypes[driver];
break;
}
}
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the Direct3D device!" );
return false;
}
ID3D11Texture2D* backBufferTexture;
result = swapChain_->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTexture );
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to get the swap chain back buffer!" );
return false;
}
result = d3dDevice_->CreateRenderTargetView( backBufferTexture, 0, &backBufferTarget_ );
if( backBufferTexture )
backBufferTexture->Release( );
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the render target view!" );
return false;
}
d3dContext_->OMSetRenderTargets( 1, &backBufferTarget_, 0 );
D3D11_VIEWPORT viewport;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
d3dContext_->RSSetViewports( 1, &viewport );
return LoadContent( );
}
//bool Dx11DemoBase::CompileD3DShader( char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer )
bool Dx11DemoBase::CompileD3DShader(LPCWSTR filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* errorBuffer = 0;
HRESULT result;
//result = D3DX11CompileFromFile( filePath, 0, 0, entry, shaderModel,
// shaderFlags, 0, 0, buffer, &errorBuffer, 0 );
result = D3DCompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, buffer, &errorBuffer);
if( FAILED( result ) )
{
if( errorBuffer != 0 )
{
OutputDebugStringA( ( char* )errorBuffer->GetBufferPointer( ) );
errorBuffer->Release( );
}
return false;
}
if( errorBuffer != 0 )
errorBuffer->Release( );
return true;
}
bool Dx11DemoBase::LoadContent( )
{
// Override with demo specifics, if any...
return true;
}
void Dx11DemoBase::UnloadContent( )
{
// Override with demo specifics, if any...
}
void Dx11DemoBase::Shutdown( )
{
UnloadContent( );
if( backBufferTarget_ ) backBufferTarget_->Release( );
if( swapChain_ ) swapChain_->Release( );
if( d3dContext_ ) d3dContext_->Release( );
if( d3dDevice_ ) d3dDevice_->Release( );
backBufferTarget_ = 0;
swapChain_ = 0;
d3dContext_ = 0;
d3dDevice_ = 0;
}
Explanation / Answer
// Comment on each line of code to express the intent of this line of code.
/*
* Dx11DemoBase constructor is using default initializer list to initialize class member variables
*/
Dx11DemoBase::Dx11DemoBase( ) : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 )
{
}
/*
* Dx11DemoBase destructor is calling Shutdown method to free all resources
*/
Dx11DemoBase::~Dx11DemoBase( )
{
// calls shutdown method to clean all resources
Shutdown( );
}
/* Initialize memory and other relevant modules
* @param HINSTANCE object, HWND object
* @return bool
*/
bool Dx11DemoBase::Initialize( HINSTANCE hInstance, HWND hwnd )
{
// initialize class member variables with passed object
hInstance_ = hInstance;
hwnd_ = hwnd;
// create RECT class object
RECT dimensions;
// function call to GetClientRect method and passing reference of dimentions to get updated values
GetClientRect( hwnd, &dimensions );
// initializes width and height using dimension object
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
// initializes D3D_DRIVER_TYPE array with 4 values
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
};
// get number of types from ARRAYSIZE method
unsigned int totalDriverTypes = ARRAYSIZE( driverTypes );
// initializes D3D_FEATURE_LEVEL array with 3 values
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
// get totalFeatureLevels from ARRAYSIZE method
unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );
// create DXGI_SWAP_CHAIN_DESC object
DXGI_SWAP_CHAIN_DESC swapChainDesc;
// initialize swapChainDesc with 0, like memset
ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) );
// assign values to swapChainDesc member variables
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0;
// below code is for debug purpose
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
// create HRESULT object
HRESULT result;
unsigned int driver = 0;
// iterate for driver types and create device for all one by one
for( driver = 0; driver < totalDriverTypes; ++driver )
{
result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0, creationFlags,
featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
&d3dDevice_, &featureLevel_, &d3dContext_ );
// break in case all are created successfully
if( SUCCEEDED( result ) )
{
driverType_ = driverTypes[driver];
break;
}
}
// return false in case of failure
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the Direct3D device!" );
return false;
}
// create ID3D11Texture2D type pointer
ID3D11Texture2D* backBufferTexture;
result = swapChain_->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTexture );
// check for failure and return with false in case of failure
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to get the swap chain back buffer!" );
return false;
}
result = d3dDevice_->CreateRenderTargetView( backBufferTexture, 0, &backBufferTarget_ );
// if backBufferTexture exist, Release the memory as it is no longer needed
if( backBufferTexture )
backBufferTexture->Release( );
// check for failure and return with false in case of failure
if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the render target view!" );
return false;
}
d3dContext_->OMSetRenderTargets( 1, &backBufferTarget_, 0 );
// create D3D11_VIEWPORT type object
D3D11_VIEWPORT viewport;
// initialize member variables of viewport and return true after all done by calling method LoadContent
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
d3dContext_->RSSetViewports( 1, &viewport );
return LoadContent( );
}
//bool Dx11DemoBase::CompileD3DShader( char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer )
/* Shader method implementation memory and other relevant modules
* @param filepath, entry character pointer, shaderModel character pointer, double pointer buffer
* @return bool
*/
bool Dx11DemoBase::CompileD3DShader(LPCWSTR filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3DCOMPILE_DEBUG;
#endif
ID3DBlob* errorBuffer = 0;
HRESULT result;
//result = D3DX11CompileFromFile( filePath, 0, 0, entry, shaderModel,
// shaderFlags, 0, 0, buffer, &errorBuffer, 0 );
result = D3DCompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, buffer, &errorBuffer);
// in case of failure, release the errorBuffer and return false value to the calling function
if( FAILED( result ) )
{
if( errorBuffer != 0 )
{
OutputDebugStringA( ( char* )errorBuffer->GetBufferPointer( ) );
errorBuffer->Release( );
}
return false;
}
// Release errorBuffer by checking if they still exist , kind of resource free and return true to calling function
if( errorBuffer != 0 )
errorBuffer->Release( );
return true;
}
/* method returns if loadcontent is done or not
* @param null
* @return bool
*/
bool Dx11DemoBase::LoadContent( )
{
// Override with demo specifics, if any...
return true;
}
/* method unload the contents
* @param null
* @return void
*/
void Dx11DemoBase::UnloadContent( )
{
// Override with demo specifics, if any...
}
/* shutdown method, releases devices related memory
* @param null
* @return void
*/
void Dx11DemoBase::Shutdown( )
{
// unload the content by calling UnloadContent method
UnloadContent( );
// Release all instances oene by one by checking if they still exist , kind of resource free//
if( backBufferTarget_ ) backBufferTarget_->Release( );
if( swapChain_ ) swapChain_->Release( );
if( d3dContext_ ) d3dContext_->Release( );
if( d3dDevice_ ) d3dDevice_->Release( );
// Assigning all pointers to 0 to take care of dangling pointers
backBufferTarget_ = 0;
swapChain_ = 0;
d3dContext_ = 0;
d3dDevice_ = 0;
}