void Application::CreateRootSignature()
{	
	D3D12_DESCRIPTOR_RANGE ranges[1];
	ranges[0].RangeType = D3D12_DESCRIPTOR_RANGE_TYPE_SRV;
	ranges[0].NumDescriptors = 1;
	ranges[0].BaseShaderRegister = 0;
	ranges[0].RegisterSpace = 0;
	ranges[0].OffsetInDescriptorsFromTableStart = D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND;
	//D3DX: ranges[0].Init(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0);


	D3D12_ROOT_PARAMETER rootParameters[2];
	rootParameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE; // Would also be possible to create a shader resource view right away, since we have only one texture. But for practicing we use a table.
	rootParameters[0].DescriptorTable.NumDescriptorRanges = 1;
	rootParameters[0].DescriptorTable.pDescriptorRanges = ranges;
	rootParameters[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;

	rootParameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
	rootParameters[1].Constants.ShaderRegister = 0;
	rootParameters[1].Constants.RegisterSpace = 0;
	rootParameters[1].Constants.Num32BitValues = 1;
	rootParameters[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;


	D3D12_STATIC_SAMPLER_DESC sampler = {};
	sampler.Filter = D3D12_FILTER_MIN_MAG_LINEAR_MIP_POINT;
	sampler.AddressU = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
	sampler.AddressV = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
	sampler.AddressW = D3D12_TEXTURE_ADDRESS_MODE_BORDER;
	sampler.MipLODBias = 0;
	sampler.MaxAnisotropy = 0;
	sampler.ComparisonFunc = D3D12_COMPARISON_FUNC_NEVER;
	sampler.BorderColor = D3D12_STATIC_BORDER_COLOR_TRANSPARENT_BLACK;
	sampler.MinLOD = 0.0f;
	sampler.MaxLOD = D3D12_FLOAT32_MAX;
	sampler.ShaderRegister = 0;
	sampler.RegisterSpace = 0;
	sampler.ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;

	D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc;
	rootSignatureDesc.NumParameters = 2;
	rootSignatureDesc.pParameters = rootParameters;
	rootSignatureDesc.NumStaticSamplers = 1;
	rootSignatureDesc.pStaticSamplers = &sampler;
	rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;


	ComPtr<ID3DBlob> signature;
	ComPtr<ID3DBlob> error;
	if (FAILED(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &signature, &error)))
	{
		OutputDXError(error.Get());
		CRITICAL_ERROR("Failed to serialize root signature.");
	}
	if (FAILED(device->GetD3D12Device()->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&rootSignature))))
		CRITICAL_ERROR("Failed to createroot signature");
}
Exemplo n.º 2
0
bool Screen3D::Create(HWND hwnd, DisplayModeInfo& m, float viewdistance)
{

	if( NULL == ( D3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
	    return false;


	D3DDISPLAYMODE d3ddm;
	if( FAILED( D3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
		return false;
	
	hWnd = hwnd;

	
	//Get the client rectangle dimensions  of the window.
	RECT WindowRect;
	GetClientRect(hwnd, &WindowRect);

	if( m.Width == -1 || m.Height == -1)
	{
		DisplayMode.Width  = WindowRect.right - WindowRect.left;
		DisplayMode.Height = WindowRect.bottom - WindowRect.top;
	}
	else
	{
		DisplayMode.Width  = m.Width;
		DisplayMode.Height = m.Height;
	}

	OutputMessage( "Attempting Resolution: %dx%d\n", DisplayMode.Width, DisplayMode.Height);

    // Set up the structure used to create the D3DDevice. 
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
	if ( DisplayMode.Fullscreen )
	{
		d3dpp.Windowed   = FALSE; 
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; //D3DSWAPEFFECT_COPY ;
		d3dpp.PresentationInterval =  D3DPRESENT_INTERVAL_IMMEDIATE;//D3DPRESENT_INTERVAL_ONE  ;
		d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT ;
	}
	else
	{
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
		d3dpp.Windowed = TRUE; 
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	}
    
	d3dpp.BackBufferWidth  = DisplayMode.Width ;
	d3dpp.BackBufferHeight = DisplayMode.Height;
    d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

	
	OutputMessage( "BackBuffer: %d\n", d3ddm.Format );

	//Get the depth/stencil caps

	//check for 32bit depth buffer
	if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D32) ))
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D32;
	}

	//second choice is 24bit buffer with 8 bit stencil
	else if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D24S8) ))
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
	}

	//third  choice is 24bit buffer with 4 bit stencil
	else if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D24X4S4) ))
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D24X4S4;
	}

	//fourth choice is 24bit buffer with 8 bits wasted
	else if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D24X8) ))
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D24X8;
	}

	//fifth choice is 16bit buffer
	else if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D16) ) )
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	}

	//last choice is 15bit depth buffer with 1 bit stencil
	else if(  SUCCEEDED ( D3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, 
		D3DDEVTYPE_HAL, d3ddm.Format, D3DUSAGE_DEPTHSTENCIL, 
		D3DRTYPE_SURFACE, D3DFMT_D15S1) ) )
	{
		d3dpp.AutoDepthStencilFormat = D3DFMT_D15S1;
	}

	OutputMessage( "DepthStencil: %d\n", d3dpp.AutoDepthStencilFormat );
	

    // Create the D3DDevice
	HRESULT hr;
	DWORD VertexFlags=0;
	if(true)
		VertexFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
	else
		VertexFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;


	// Set default settings
	//UINT AdapterToUse=D3DADAPTER_DEFAULT; //D3D->GetAdapterCount()-1
	//D3DDEVTYPE DeviceType=D3DDEVTYPE_HAL; //D3DDEVTYPE_REF
#ifdef NVPERFHUD
	UINT AdapterToUse=D3D->GetAdapterCount()-1;
	D3DDEVTYPE DeviceType=D3DDEVTYPE_REF;
#else
	UINT AdapterToUse=D3DADAPTER_DEFAULT; //D3D->GetAdapterCount()-1
	D3DDEVTYPE DeviceType=D3DDEVTYPE_HAL; //D3DDEVTYPE_REF
#endif

	// Look for 'NVIDIA NVPerfHUD' adapter
	// If it is present, override default settings
	for (UINT Adapter=0;Adapter<D3D->GetAdapterCount();Adapter++)
	{
		D3DADAPTER_IDENTIFIER9 Identifier;
		HRESULT Res;
		Res = D3D->GetAdapterIdentifier(Adapter,0,&Identifier);
		if (strcmp(Identifier.Description,"NVIDIA NVPerfHUD") == 0)
		{
			AdapterToUse=Adapter;
			DeviceType=D3DDEVTYPE_REF;
			break;
		}
	}


    if( FAILED( hr = D3D->CreateDevice(
								AdapterToUse,//D3DADAPTER_DEFAULT,
								DeviceType,//D3DDEVTYPE_HAL, 
								hwnd,
								VertexFlags ,
								&d3dpp, 
								&D3DDevice 
								)
		) )
    {
		OutputDXError( hr, "D3D Device Init Failed ");
		return false;
    }
	else
	{

		//We have a mixed device, so figure out the level of T&L support
		D3DCAPS9 tlcaps;
		D3DDevice->GetDeviceCaps(&tlcaps);       // initialize m_pd3dDevice before using

		//check the shader version
		if(	D3DSHADER_VERSION_MAJOR( tlcaps.VertexShaderVersion ) < 1 ) 
			TLSupport = S3D_STANDARD;

		//Major version is 1, so check minor versions
		else if(D3DSHADER_VERSION_MINOR( tlcaps.VertexShaderVersion ) < 1 ) 
			TLSupport = S3D_VS_1_0; //Vertex shader 1.0
		else
			TLSupport = S3D_VS_1_1; //Vertex shader 1.1 or above
	}


	//set up the projection matrix
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, RADIAN(60), (float)DisplayMode.Width/(float)DisplayMode.Height, 1.0f, viewdistance );
    D3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );

	//figure out the field of view
	CalculateFOV();


    // initial render state
    D3DDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    D3DDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
    D3DDevice->SetRenderState( D3DRS_ZENABLE, TRUE  );

	D3DDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
    D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    D3DDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_MODULATE );
	D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
	D3DDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );

	SetLinearTextureFiltering();
    	
	//init the texture manager
	TM.Init(this);
	TM.RestoreTextures();

	InitialAvailableMem = D3DDevice->GetAvailableTextureMem();

	//Init the font manager
	FM.Init( *this );
	//return OK
	return true;

}