示例#1
0
void  D3D12Render::v_update()
{
	// Reuse the memory associated with command recording.
    // We can only reset when the associated command lists have finished execution on the GPU.
	ThrowIfFailed(m_pCommandAlloc->Reset());

	// A command list can be reset after it has been added to the command queue via ExecuteCommandList.
	// Reusing the command list reuses memory.
	ThrowIfFailed(m_pCommandList->Reset(m_pCommandAlloc.Get(), nullptr));

	// Indicate a state transition on the resource usage.
	m_pCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(CurrentBackBuffer(),
		D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET));

	// Set the viewport and scissor rect.  This needs to be reset whenever the command list is reset.
	m_pCommandList->RSSetViewports(1, &m_ScreenViewport);
	m_pCommandList->RSSetScissorRects(1, &m_ScissorRect);

	// Clear the back buffer and depth buffer.
	m_pCommandList->ClearRenderTargetView(CurrentBackBufferView(), Colors::LightSteelBlue, 0, nullptr);
	m_pCommandList->ClearDepthStencilView(DepthStencilView(), D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL, 1.0f, 0, 0, nullptr);

	// Specify the buffers we are going to render to.
	m_pCommandList->OMSetRenderTargets(1, &CurrentBackBufferView(), true, &DepthStencilView());

	// Indicate a state transition on the resource usage.
	m_pCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(CurrentBackBuffer(),
		D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PRESENT));

	// Done recording commands.
	ThrowIfFailed(m_pCommandList->Close());

	// Add the command list to the queue for execution.
	ID3D12CommandList* cmdsLists[] ={ m_pCommandList.Get() };
	m_pCommandQueue->ExecuteCommandLists(_countof(cmdsLists), cmdsLists);

	// swap the back and front buffers
	ThrowIfFailed(m_pSwapChain->Present(0, 0));
	m_CurrBackBuffer = (m_CurrBackBuffer + 1) % m_SwapChainBufferCount;

	// Wait until frame commands are complete.  This waiting is inefficient and is
	// done for simplicity.  Later we will show how to organize our rendering code
	// so we do not have to wait per frame.
	flush_cmdQueue();
}
	// Creates the DirectX device. This must be called before the device is used.
	bool CRenderDevice::SetupDevice(HWND hWnd)
	{
		HRESULT hr = S_OK;

		if( DeviceAndSwapChain( hr, hWnd ) == false )
		{
			return false;
		}

		if( RenderTargetView( hr ) == false )
		{
			return false;
		}

		if( DepthStencilView( hr ) == false )
		{
			return false;
		}

		// Select the back buffer and depth buffer to use for rendering now
		m_pd3dDevice->OMSetRenderTargets( 1, &m_pRenderTargetView, m_pDepthStencilView );


		// Setup the viewport - defines which part of the window we will render to, almost always the whole window
		D3D10_VIEWPORT vp;
		vp.Width  = m_ViewWidth;
		vp.Height = m_ViewHeight;
		vp.MinDepth = 0.0f;
		vp.MaxDepth = 1.0f;
		vp.TopLeftX = 0;
		vp.TopLeftY = 0;
		m_pd3dDevice->RSSetViewports( 1, &vp );

		
		return true;
	}
示例#3
0
pgn::DepthStencilView* RenderingSystem::createDepthStencilView(pgn::Texture* tex)
{
	return debug_new DepthStencilView((Texture*)tex);
}
示例#4
0
pgn::DepthStencilView* RenderingSystem::createDepthStencilView(pgn::RenderBuffer* rb)
{
	return debug_new DepthStencilView((RenderBuffer*)rb);
}
示例#5
0
void D3D12Render::init_size()
{
	assert(m_pD3D12Device);
	assert(m_pSwapChain);
    assert(m_pCommandAlloc);

	// Flush before changing any resources.
	flush_cmdQueue();

    ThrowIfFailed(m_pCommandList->Reset(m_pCommandAlloc.Get(), nullptr));

	// Release the previous resources we will be recreating.
	for (int i = 0; i < m_SwapChainBufferCount; ++i) {
		m_pSwapChainBuffer[i].Reset();
	}
    m_pDepthStencilBuffer.Reset();
	
	// Resize the swap chain.
    ThrowIfFailed(m_pSwapChain->ResizeBuffers(
		m_SwapChainBufferCount, 
		mClientWidth, mClientHeight, 
		m_BackBufferFormat, 
		DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH));

	m_CurrBackBuffer = 0;
 
	CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHeapHandle(m_pRendetTargetViewHeap->GetCPUDescriptorHandleForHeapStart());
	for (UINT i = 0; i < m_SwapChainBufferCount; i++) {

		ThrowIfFailed(m_pSwapChain->GetBuffer(i, IID_PPV_ARGS(&m_pSwapChainBuffer[i])));
		m_pD3D12Device->CreateRenderTargetView(m_pSwapChainBuffer[i].Get(), nullptr, rtvHeapHandle);
		rtvHeapHandle.Offset(1, m_RtvDescriptorSize);
	}

    // Create the depth/stencil buffer and view.
    D3D12_RESOURCE_DESC depthStencilDesc;
    depthStencilDesc.Dimension          = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
    depthStencilDesc.Alignment          = 0;
    depthStencilDesc.Width              = mClientWidth;
    depthStencilDesc.Height             = mClientHeight;
    depthStencilDesc.DepthOrArraySize   = 1;
    depthStencilDesc.MipLevels          = 1;
    depthStencilDesc.Format             = m_DepthStencilFormat;
    depthStencilDesc.SampleDesc.Count   = m_4xMsaaState ? 4 : 1;
    depthStencilDesc.SampleDesc.Quality = m_4xMsaaState ? (m_4xMsaaQuality - 1) : 0;
    depthStencilDesc.Layout             = D3D12_TEXTURE_LAYOUT_UNKNOWN;
    depthStencilDesc.Flags              = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;

    D3D12_CLEAR_VALUE optClear;
    optClear.Format = m_DepthStencilFormat;
    optClear.DepthStencil.Depth = 1.0f;
    optClear.DepthStencil.Stencil = 0;
    ThrowIfFailed(m_pD3D12Device->CreateCommittedResource(
        &CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT),
		D3D12_HEAP_FLAG_NONE,
        &depthStencilDesc,
		D3D12_RESOURCE_STATE_COMMON,
        &optClear,
        IID_PPV_ARGS(m_pDepthStencilBuffer.GetAddressOf())));

    // Create descriptor to mip level 0 of entire resource using the format of the resource.
    m_pD3D12Device->CreateDepthStencilView(m_pDepthStencilBuffer.Get(), nullptr, DepthStencilView());

    // Transition the resource from its initial state to be used as a depth buffer.
	m_pCommandList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_pDepthStencilBuffer.Get(),
		                            D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_DEPTH_WRITE));
	
    // Execute the resize commands.
    ThrowIfFailed(m_pCommandList->Close());
    ID3D12CommandList* cmdsLists[] = { m_pCommandList.Get() };
    m_pCommandQueue->ExecuteCommandLists(_countof(cmdsLists), cmdsLists);

	// Wait until resize is complete.
	flush_cmdQueue();

	// Update the viewport transform to cover the client area.
	m_ScreenViewport.TopLeftX = 0;
	m_ScreenViewport.TopLeftY = 0;
	m_ScreenViewport.Width    = static_cast<float>(mClientWidth);
	m_ScreenViewport.Height   = static_cast<float>(mClientHeight);
	m_ScreenViewport.MinDepth = 0.0f;
	m_ScreenViewport.MaxDepth = 1.0f;

    m_ScissorRect = { 0, 0, mClientWidth, mClientHeight };
}