From 0a8214175ef2453eded507c12a3c7dd05b6b05ac Mon Sep 17 00:00:00 2001 From: "Bob Brown (DIRECTX)" Date: Thu, 3 Dec 2015 15:57:25 -0800 Subject: [PATCH 1/3] VS template updates - Getting d3dx12.h up to date with latest version. - Cleaning up code related to resizing the swap chain. --- .../DirectX12App/Common/DeviceResources.cpp | 44 ++++++------ Templates/DirectX12App/Common/d3dx12.h | 68 +++++++++++++++++-- 2 files changed, 81 insertions(+), 31 deletions(-) diff --git a/Templates/DirectX12App/Common/DeviceResources.cpp b/Templates/DirectX12App/Common/DeviceResources.cpp index 6aeb2e554..08525b85a 100644 --- a/Templates/DirectX12App/Common/DeviceResources.cpp +++ b/Templates/DirectX12App/Common/DeviceResources.cpp @@ -139,6 +139,23 @@ void DX::DeviceResources::CreateDeviceResources() DX::ThrowIfFailed(m_d3dDevice->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue))); + // Create descriptor heaps for render target views and depth stencil views. + D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {}; + rtvHeapDesc.NumDescriptors = c_frameCount; + rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; + rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + DX::ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap))); + m_rtvHeap->SetName(L"Render Target View Descriptor Heap"); + + m_rtvDescriptorSize = m_d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); + + D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {}; + dsvHeapDesc.NumDescriptors = 1; + dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; + dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; + ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&m_dsvHeap))); + m_dsvHeap->SetName(L"Depth Stencil View Descriptor Heap"); + for (UINT n = 0; n < c_frameCount; n++) { DX::ThrowIfFailed( @@ -159,12 +176,12 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() // Wait until all previous GPU work is complete. WaitForGpu(); - // Clear the previous window size specific content. + // Clear the previous window size specific content and update the tracked fence values. for (UINT n = 0; n < c_frameCount; n++) { m_renderTargets[n] = nullptr; + m_fenceValues[n] = m_fenceValues[m_currentFrame]; } - m_rtvHeap = nullptr; UpdateRenderTargetSize(); @@ -266,16 +283,8 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() // Create render target views of the swap chain back buffer. { - D3D12_DESCRIPTOR_HEAP_DESC rtvHeapDesc = {}; - rtvHeapDesc.NumDescriptors = c_frameCount; - rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; - rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; - DX::ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap))); - m_rtvHeap->SetName(L"Render Target View Descriptor Heap"); - m_currentFrame = 0; CD3DX12_CPU_DESCRIPTOR_HANDLE rtvDescriptor(m_rtvHeap->GetCPUDescriptorHandleForHeapStart()); - m_rtvDescriptorSize = m_d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); for (UINT n = 0; n < c_frameCount; n++) { DX::ThrowIfFailed(m_swapChain->GetBuffer(n, IID_PPV_ARGS(&m_renderTargets[n]))); @@ -288,14 +297,8 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() } } - // Create a depth stencil view. + // Create a depth stencil and view. { - D3D12_DESCRIPTOR_HEAP_DESC dsvHeapDesc = {}; - dsvHeapDesc.NumDescriptors = 1; - dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; - dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; - ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&m_dsvHeap))); - D3D12_HEAP_PROPERTIES depthHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); D3D12_RESOURCE_DESC depthResourceDesc = CD3DX12_RESOURCE_DESC::Tex2D( DXGI_FORMAT_D32_FLOAT, @@ -329,13 +332,6 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() m_d3dDevice->CreateDepthStencilView(m_depthStencil.Get(), &dsvDesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart()); } - // All pending GPU work was already finished. Update the tracked fence values - // to the last value signaled. - for (UINT n = 0; n < c_frameCount; n++) - { - m_fenceValues[n] = m_fenceValues[m_currentFrame]; - } - // Set the 3D rendering viewport to target the entire window. m_screenViewport = { 0.0f, 0.0f, m_d3dRenderTargetSize.Width, m_d3dRenderTargetSize.Height, 0.0f, 1.0f }; } diff --git a/Templates/DirectX12App/Common/d3dx12.h b/Templates/DirectX12App/Common/d3dx12.h index 68d7350f7..dc4608aeb 100644 --- a/Templates/DirectX12App/Common/d3dx12.h +++ b/Templates/DirectX12App/Common/d3dx12.h @@ -1,4 +1,4 @@ -////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////// // // Copyright (C) Microsoft Corporation. All Rights Reserved. // @@ -386,6 +386,15 @@ struct CD3DX12_HEAP_DESC : public D3D12_HEAP_DESC bool IsCPUAccessible() const { return static_cast< const CD3DX12_HEAP_PROPERTIES* >( &Properties )->IsCPUAccessible(); } }; +inline bool operator==( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r ) +{ + return l.SizeInBytes == r.SizeInBytes && + l.Properties == r.Properties && + l.Alignment == r.Alignment && + l.Flags == r.Flags; +} +inline bool operator!=( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r ) +{ return !( l == r ); } //------------------------------------------------------------------------------------------------ struct CD3DX12_CLEAR_VALUE : public D3D12_CLEAR_VALUE @@ -433,6 +442,30 @@ struct CD3DX12_RANGE : public D3D12_RANGE operator const D3D12_RANGE&() const { return *this; } }; +//------------------------------------------------------------------------------------------------ +struct CD3DX12_SHADER_BYTECODE : public D3D12_SHADER_BYTECODE +{ + CD3DX12_SHADER_BYTECODE() + {} + explicit CD3DX12_SHADER_BYTECODE(const D3D12_SHADER_BYTECODE &o) : + D3D12_SHADER_BYTECODE(o) + {} + CD3DX12_SHADER_BYTECODE( + ID3DBlob* pShaderBlob ) + { + pShaderBytecode = pShaderBlob->GetBufferPointer(); + BytecodeLength = pShaderBlob->GetBufferSize(); + } + CD3DX12_SHADER_BYTECODE( + void* _pShaderBytecode, + SIZE_T bytecodeLength ) + { + pShaderBytecode = _pShaderBytecode; + BytecodeLength = bytecodeLength; + } + operator const D3D12_SHADER_BYTECODE&() const { return *this; } +}; + //------------------------------------------------------------------------------------------------ struct CD3DX12_TILED_RESOURCE_COORDINATE : public D3D12_TILED_RESOURCE_COORDINATE { @@ -536,7 +569,8 @@ struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES, D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE) { - CD3DX12_RESOURCE_BARRIER result = {}; + CD3DX12_RESOURCE_BARRIER result; + ZeroMemory(&result, sizeof(result)); D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; result.Flags = flags; @@ -550,7 +584,8 @@ struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER _In_ ID3D12Resource* pResourceBefore, _In_ ID3D12Resource* pResourceAfter) { - CD3DX12_RESOURCE_BARRIER result = {}; + CD3DX12_RESOURCE_BARRIER result; + ZeroMemory(&result, sizeof(result)); D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING; barrier.Aliasing.pResourceBefore = pResourceBefore; @@ -560,7 +595,8 @@ struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER static inline CD3DX12_RESOURCE_BARRIER UAV( _In_ ID3D12Resource* pResource) { - CD3DX12_RESOURCE_BARRIER result = {}; + CD3DX12_RESOURCE_BARRIER result; + ZeroMemory(&result, sizeof(result)); D3D12_RESOURCE_BARRIER &barrier = result; result.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV; barrier.UAV.pResource = pResource; @@ -710,7 +746,7 @@ struct CD3DX12_ROOT_DESCRIPTOR_TABLE : public D3D12_ROOT_DESCRIPTOR_TABLE inline void Init( UINT numDescriptorRanges, - _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges) + _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges) { Init(*this, numDescriptorRanges, _pDescriptorRanges); } @@ -1012,6 +1048,10 @@ struct CD3DX12_ROOT_SIGNATURE_DESC : public D3D12_ROOT_SIGNATURE_DESC { Init(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags); } + CD3DX12_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT) + { + Init(0, NULL, 0, NULL, D3D12_ROOT_SIGNATURE_FLAG_NONE); + } inline void Init( UINT numParameters, @@ -1037,8 +1077,6 @@ struct CD3DX12_ROOT_SIGNATURE_DESC : public D3D12_ROOT_SIGNATURE_DESC desc.pStaticSamplers = _pStaticSamplers; desc.Flags = flags; } - - CD3DX12_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT) : CD3DX12_ROOT_SIGNATURE_DESC(0,NULL,0,NULL,D3D12_ROOT_SIGNATURE_FLAG_NONE) {} }; //------------------------------------------------------------------------------------------------ @@ -1292,6 +1330,22 @@ struct CD3DX12_RESOURCE_DESC : public D3D12_RESOURCE_DESC { return D3D12CalcSubresource(MipSlice, ArraySlice, PlaneSlice, MipLevels, ArraySize()); } operator const D3D12_RESOURCE_DESC&() const { return *this; } }; +inline bool operator==( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r ) +{ + return l.Dimension == r.Dimension && + l.Alignment == r.Alignment && + l.Width == r.Width && + l.Height == r.Height && + l.DepthOrArraySize == r.DepthOrArraySize && + l.MipLevels == r.MipLevels && + l.Format == r.Format && + l.SampleDesc.Count == r.SampleDesc.Count && + l.SampleDesc.Quality == r.SampleDesc.Quality && + l.Layout == r.Layout && + l.Flags == r.Flags; +} +inline bool operator!=( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r ) +{ return !( l == r ); } //------------------------------------------------------------------------------------------------ // Row-by-row memcpy From 444883b873fa5332a5fec43b7c17decf6c32aa64 Mon Sep 17 00:00:00 2001 From: "Bob Brown (DIRECTX)" Date: Wed, 9 Dec 2015 16:58:35 -0800 Subject: [PATCH 2/3] VS UWP template cleanup - removing duplicate TODOs - adding debug output for apps that don't correctly release all resources before creating a new device (in device removed scenarios) - converting hard-coded DXGI_FORMATs into member variables on DeviceResources. - naming of D3D objects will only happen in debug builds. --- Templates/.gitignore | 1 + Templates/DirectX12App/App.cpp | 13 ++- .../DirectX12App/Common/DeviceResources.cpp | 90 +++++++++---------- .../DirectX12App/Common/DeviceResources.h | 10 ++- Templates/DirectX12App/Common/DirectXHelper.h | 12 +++ .../Content/Sample3DSceneRenderer.cpp | 16 ++-- Templates/DirectX12App/DirectX12App.vcxproj | 12 +-- Templates/DirectX12App/pch.h | 4 + 8 files changed, 88 insertions(+), 70 deletions(-) diff --git a/Templates/.gitignore b/Templates/.gitignore index 4a3d052c0..28e5671b3 100644 --- a/Templates/.gitignore +++ b/Templates/.gitignore @@ -7,5 +7,6 @@ Release/ x64/ *.ipch *.sdf +*.opendb *.opensdf *.user diff --git a/Templates/DirectX12App/App.cpp b/Templates/DirectX12App/App.cpp index eab3bd782..5b9529178 100644 --- a/Templates/DirectX12App/App.cpp +++ b/Templates/DirectX12App/App.cpp @@ -15,6 +15,8 @@ using namespace Windows::System; using namespace Windows::Foundation; using namespace Windows::Graphics::Display; +using Microsoft::WRL::ComPtr; + // The DirectX 12 Application template is documented at http://go.microsoft.com/fwlink/?LinkID=613670&clcid=0x409 // The main function is only used to initialize our IFrameworkView class. @@ -142,9 +144,7 @@ void App::OnSuspending(Platform::Object^ sender, SuspendingEventArgs^ args) create_task([this, deferral]() { - // TODO: Insert your code here. m_main->OnSuspending(); - deferral->Complete(); }); } @@ -155,7 +155,6 @@ void App::OnResuming(Platform::Object^ sender, Platform::Object^ args) // and state are persisted when resuming from suspend. Note that this event // does not occur if the app was previously terminated. - // TODO: Insert your code here. m_main->OnResuming(); } @@ -209,6 +208,14 @@ std::shared_ptr App::GetDeviceResources() m_deviceResources = nullptr; m_main->OnDeviceRemoved(); + +#if defined(_DEBUG) + ComPtr dxgiDebug; + if (SUCCEEDED(DXGIGetDebugInterface1(0, IID_PPV_ARGS(&dxgiDebug)))) + { + dxgiDebug->ReportLiveObjects(DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_FLAGS(DXGI_DEBUG_RLO_SUMMARY | DXGI_DEBUG_RLO_IGNORE_INTERNAL)); + } +#endif } if (m_deviceResources == nullptr) diff --git a/Templates/DirectX12App/Common/DeviceResources.cpp b/Templates/DirectX12App/Common/DeviceResources.cpp index 08525b85a..d19a7ef31 100644 --- a/Templates/DirectX12App/Common/DeviceResources.cpp +++ b/Templates/DirectX12App/Common/DeviceResources.cpp @@ -64,11 +64,14 @@ namespace ScreenRotation }; // Constructor for DeviceResources. -DX::DeviceResources::DeviceResources() : +DX::DeviceResources::DeviceResources(DXGI_FORMAT backBufferFormat, DXGI_FORMAT depthBufferFormat) : m_currentFrame(0), m_screenViewport(), m_rtvDescriptorSize(0), m_fenceEvent(0), + m_backBufferFormat(backBufferFormat), + m_depthBufferFormat(depthBufferFormat), + m_fenceValues{}, m_d3dRenderTargetSize(), m_outputSize(), m_logicalSize(), @@ -78,7 +81,6 @@ DX::DeviceResources::DeviceResources() : m_effectiveDpi(-1.0f), m_deviceRemoved(false) { - ZeroMemory(m_fenceValues, sizeof(m_fenceValues)); CreateDeviceIndependentResources(); CreateDeviceResources(); } @@ -105,7 +107,7 @@ void DX::DeviceResources::CreateDeviceResources() DX::ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&m_dxgiFactory))); ComPtr adapter; - GetHardwareAdapter(m_dxgiFactory.Get(), &adapter); + GetHardwareAdapter(&adapter); // Create the Direct3D 12 API device object HRESULT hr = D3D12CreateDevice( @@ -114,6 +116,7 @@ void DX::DeviceResources::CreateDeviceResources() IID_PPV_ARGS(&m_d3dDevice) // Returns the Direct3D device created. ); +#if defined(_DEBUG) if (FAILED(hr)) { // If the initialization fails, fall back to the WARP device. @@ -121,16 +124,13 @@ void DX::DeviceResources::CreateDeviceResources() // http://go.microsoft.com/fwlink/?LinkId=286690 ComPtr warpAdapter; - m_dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)); + DX::ThrowIfFailed(m_dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter))); - DX::ThrowIfFailed( - D3D12CreateDevice( - warpAdapter.Get(), - D3D_FEATURE_LEVEL_11_0, - IID_PPV_ARGS(&m_d3dDevice) - ) - ); + hr = D3D12CreateDevice(warpAdapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&m_d3dDevice)); } +#endif + + DX::ThrowIfFailed(hr); // Create the command queue. D3D12_COMMAND_QUEUE_DESC queueDesc = {}; @@ -145,7 +145,7 @@ void DX::DeviceResources::CreateDeviceResources() rtvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV; rtvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; DX::ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&rtvHeapDesc, IID_PPV_ARGS(&m_rtvHeap))); - m_rtvHeap->SetName(L"Render Target View Descriptor Heap"); + DX::SetName(m_rtvHeap.Get(), L"Render Target View Descriptor Heap"); m_rtvDescriptorSize = m_d3dDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV); @@ -154,7 +154,7 @@ void DX::DeviceResources::CreateDeviceResources() dsvHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV; dsvHeapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE; ThrowIfFailed(m_d3dDevice->CreateDescriptorHeap(&dsvHeapDesc, IID_PPV_ARGS(&m_dsvHeap))); - m_dsvHeap->SetName(L"Depth Stencil View Descriptor Heap"); + DX::SetName(m_dsvHeap.Get(), L"Depth Stencil View Descriptor Heap"); for (UINT n = 0; n < c_frameCount; n++) { @@ -194,16 +194,13 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() m_d3dRenderTargetSize.Width = swapDimensions ? m_outputSize.Height : m_outputSize.Width; m_d3dRenderTargetSize.Height = swapDimensions ? m_outputSize.Width : m_outputSize.Height; + UINT backBufferWidth = lround(m_d3dRenderTargetSize.Width); + UINT backBufferHeight = lround(m_d3dRenderTargetSize.Height); + if (m_swapChain != nullptr) { // If the swap chain already exists, resize it. - HRESULT hr = m_swapChain->ResizeBuffers( - c_frameCount, - lround(m_d3dRenderTargetSize.Width), - lround(m_d3dRenderTargetSize.Height), - DXGI_FORMAT_B8G8R8A8_UNORM, - 0 - ); + HRESULT hr = m_swapChain->ResizeBuffers(c_frameCount, backBufferWidth, backBufferHeight, m_backBufferFormat, 0); if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET) { @@ -224,15 +221,15 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() DXGI_SCALING scaling = DisplayMetrics::SupportHighResolutions ? DXGI_SCALING_NONE : DXGI_SCALING_STRETCH; DXGI_SWAP_CHAIN_DESC1 swapChainDesc = {}; - swapChainDesc.Width = lround(m_d3dRenderTargetSize.Width); // Match the size of the window. - swapChainDesc.Height = lround(m_d3dRenderTargetSize.Height); - swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // This is the most common swap chain format. + swapChainDesc.Width = backBufferWidth; // Match the size of the window. + swapChainDesc.Height = backBufferHeight; + swapChainDesc.Format = m_backBufferFormat; swapChainDesc.Stereo = false; swapChainDesc.SampleDesc.Count = 1; // Don't use multi-sampling. swapChainDesc.SampleDesc.Quality = 0; swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; - swapChainDesc.BufferCount = c_frameCount; // Use triple-buffering to minimize latency. - swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; // All Windows Universal apps must use _FLIP_ SwapEffects + swapChainDesc.BufferCount = c_frameCount; // Use triple-buffering to minimize latency. + swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; // All Windows Universal apps must use _FLIP_ SwapEffects. swapChainDesc.Flags = 0; swapChainDesc.Scaling = scaling; swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_IGNORE; @@ -283,7 +280,7 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() // Create render target views of the swap chain back buffer. { - m_currentFrame = 0; + m_currentFrame = m_swapChain->GetCurrentBackBufferIndex(); CD3DX12_CPU_DESCRIPTOR_HANDLE rtvDescriptor(m_rtvHeap->GetCPUDescriptorHandleForHeapStart()); for (UINT n = 0; n < c_frameCount; n++) { @@ -292,28 +289,21 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() rtvDescriptor.Offset(m_rtvDescriptorSize); WCHAR name[25]; - swprintf_s(name, L"Render Target %d", n); - m_renderTargets[n]->SetName(name); + if (swprintf_s(name, L"Render Target %u", n) > 0) + { + DX::SetName(m_renderTargets[n].Get(), name); + } } } // Create a depth stencil and view. { D3D12_HEAP_PROPERTIES depthHeapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT); - D3D12_RESOURCE_DESC depthResourceDesc = CD3DX12_RESOURCE_DESC::Tex2D( - DXGI_FORMAT_D32_FLOAT, - static_cast(m_d3dRenderTargetSize.Width), - static_cast(m_d3dRenderTargetSize.Height), - 1, - 0, - 1, - 0, - D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL); - - D3D12_CLEAR_VALUE depthOptimizedClearValue = {}; - depthOptimizedClearValue.Format = DXGI_FORMAT_D32_FLOAT; - depthOptimizedClearValue.DepthStencil.Depth = 1.0f; - depthOptimizedClearValue.DepthStencil.Stencil = 0; + + D3D12_RESOURCE_DESC depthResourceDesc = CD3DX12_RESOURCE_DESC::Tex2D(m_depthBufferFormat, backBufferWidth, backBufferHeight); + depthResourceDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + + CD3DX12_CLEAR_VALUE depthOptimizedClearValue(m_depthBufferFormat, 1.0f, 0); ThrowIfFailed(m_d3dDevice->CreateCommittedResource( &depthHeapProperties, @@ -324,8 +314,10 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() IID_PPV_ARGS(&m_depthStencil) )); + DX::SetName(m_depthStencil.Get(), L"Depth Buffer"); + D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {}; - dsvDesc.Format = DXGI_FORMAT_D32_FLOAT; + dsvDesc.Format = m_depthBufferFormat; dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D; dsvDesc.Flags = D3D12_DSV_FLAG_NONE; @@ -336,6 +328,7 @@ void DX::DeviceResources::CreateWindowSizeDependentResources() m_screenViewport = { 0.0f, 0.0f, m_d3dRenderTargetSize.Width, m_d3dRenderTargetSize.Height, 0.0f, 1.0f }; } +// Determine the dimensions of the render target and whether it will be scaled down. void DX::DeviceResources::UpdateRenderTargetSize() { m_effectiveDpi = m_dpi; @@ -423,11 +416,8 @@ void DX::DeviceResources::ValidateDevice() // Next, get the information for the current default adapter. - ComPtr currentFactory; - DX::ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(¤tFactory))); - ComPtr currentDefaultAdapter; - DX::ThrowIfFailed(currentFactory->EnumAdapters1(0, ¤tDefaultAdapter)); + GetHardwareAdapter(¤tDefaultAdapter); DXGI_ADAPTER_DESC currentDesc; DX::ThrowIfFailed(currentDefaultAdapter->GetDesc(¤tDesc)); @@ -487,7 +477,7 @@ void DX::DeviceResources::MoveToNextFrame() DX::ThrowIfFailed(m_commandQueue->Signal(m_fence.Get(), currentFenceValue)); // Advance the frame index. - m_currentFrame = (m_currentFrame + 1) % c_frameCount; + m_currentFrame = m_swapChain->GetCurrentBackBufferIndex(); // Check to see if the next frame is ready to start. if (m_fence->GetCompletedValue() < m_fenceValues[m_currentFrame]) @@ -557,12 +547,12 @@ DXGI_MODE_ROTATION DX::DeviceResources::ComputeDisplayRotation() // This method acquires the first available hardware adapter that supports Direct3D 12. // If no such adapter can be found, *ppAdapter will be set to nullptr. -void DX::DeviceResources::GetHardwareAdapter(IDXGIFactory4* pFactory, IDXGIAdapter1** ppAdapter) +void DX::DeviceResources::GetHardwareAdapter(IDXGIAdapter1** ppAdapter) { ComPtr adapter; *ppAdapter = nullptr; - for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex) + for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != m_dxgiFactory->EnumAdapters1(adapterIndex, &adapter); adapterIndex++) { DXGI_ADAPTER_DESC1 desc; adapter->GetDesc1(&desc); diff --git a/Templates/DirectX12App/Common/DeviceResources.h b/Templates/DirectX12App/Common/DeviceResources.h index 37618ce50..c48338663 100644 --- a/Templates/DirectX12App/Common/DeviceResources.h +++ b/Templates/DirectX12App/Common/DeviceResources.h @@ -8,7 +8,7 @@ namespace DX class DeviceResources { public: - DeviceResources(); + DeviceResources(DXGI_FORMAT backBufferFormat = DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT depthBufferFormat = DXGI_FORMAT_D32_FLOAT); void SetWindow(Windows::UI::Core::CoreWindow^ window); void SetLogicalSize(Windows::Foundation::Size logicalSize); void SetCurrentOrientation(Windows::Graphics::Display::DisplayOrientations currentOrientation); @@ -33,6 +33,8 @@ namespace DX ID3D12Resource* GetDepthStencil() const { return m_depthStencil.Get(); } ID3D12CommandQueue* GetCommandQueue() const { return m_commandQueue.Get(); } ID3D12CommandAllocator* GetCommandAllocator() const { return m_commandAllocators[m_currentFrame].Get(); } + DXGI_FORMAT GetBackBufferFormat() const { return m_backBufferFormat; } + DXGI_FORMAT GetDepthBufferFormat() const { return m_depthBufferFormat; } D3D12_VIEWPORT GetScreenViewport() const { return m_screenViewport; } DirectX::XMFLOAT4X4 GetOrientationTransform3D() const { return m_orientationTransform3D; } UINT GetCurrentFrameIndex() const { return m_currentFrame; } @@ -53,7 +55,7 @@ namespace DX void UpdateRenderTargetSize(); void MoveToNextFrame(); DXGI_MODE_ROTATION ComputeDisplayRotation(); - void GetHardwareAdapter(IDXGIFactory4* pFactory, IDXGIAdapter1** ppAdapter); + void GetHardwareAdapter(IDXGIAdapter1** ppAdapter); UINT m_currentFrame; @@ -65,10 +67,12 @@ namespace DX Microsoft::WRL::ComPtr m_depthStencil; Microsoft::WRL::ComPtr m_rtvHeap; Microsoft::WRL::ComPtr m_dsvHeap; - UINT m_rtvDescriptorSize; Microsoft::WRL::ComPtr m_commandQueue; Microsoft::WRL::ComPtr m_commandAllocators[c_frameCount]; + DXGI_FORMAT m_backBufferFormat; + DXGI_FORMAT m_depthBufferFormat; D3D12_VIEWPORT m_screenViewport; + UINT m_rtvDescriptorSize; bool m_deviceRemoved; // CPU/GPU Synchronization. diff --git a/Templates/DirectX12App/Common/DirectXHelper.h b/Templates/DirectX12App/Common/DirectXHelper.h index a4fb3c43c..41ea7f3d6 100644 --- a/Templates/DirectX12App/Common/DirectXHelper.h +++ b/Templates/DirectX12App/Common/DirectXHelper.h @@ -39,4 +39,16 @@ namespace DX static const float dipsPerInch = 96.0f; return floorf(dips * dpi / dipsPerInch + 0.5f); // Round to nearest integer. } + + // Assign a name to the object to aid with debugging. +#if defined(_DEBUG) + inline void SetName(ID3D12Object* pObject, LPCWSTR name) + { + pObject->SetName(name); + } +#else + inline void SetName(ID3D12Object*, LPCWSTR) + { + } +#endif } diff --git a/Templates/DirectX12App/Content/Sample3DSceneRenderer.cpp b/Templates/DirectX12App/Content/Sample3DSceneRenderer.cpp index cae578bae..70bf43103 100644 --- a/Templates/DirectX12App/Content/Sample3DSceneRenderer.cpp +++ b/Templates/DirectX12App/Content/Sample3DSceneRenderer.cpp @@ -96,8 +96,8 @@ void Sample3DSceneRenderer::CreateDeviceDependentResources() state.SampleMask = UINT_MAX; state.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE; state.NumRenderTargets = 1; - state.RTVFormats[0] = DXGI_FORMAT_B8G8R8A8_UNORM; - state.DSVFormat = DXGI_FORMAT_D32_FLOAT; + state.RTVFormats[0] = m_deviceResources->GetBackBufferFormat(); + state.DSVFormat = m_deviceResources->GetDepthBufferFormat(); state.SampleDesc.Count = 1; DX::ThrowIfFailed(m_deviceResources->GetD3DDevice()->CreateGraphicsPipelineState(&state, IID_PPV_ARGS(&m_pipelineState))); @@ -152,8 +152,8 @@ void Sample3DSceneRenderer::CreateDeviceDependentResources() nullptr, IID_PPV_ARGS(&vertexBufferUpload))); - m_vertexBuffer->SetName(L"Vertex Buffer Resource"); - vertexBufferUpload->SetName(L"Vertex Buffer Upload Resource"); + DX::SetName(m_vertexBuffer.Get(), L"Vertex Buffer Resource"); + DX::SetName(vertexBufferUpload.Get(), L"Vertex Buffer Upload Resource"); // Upload the vertex buffer to the GPU. { @@ -216,8 +216,8 @@ void Sample3DSceneRenderer::CreateDeviceDependentResources() nullptr, IID_PPV_ARGS(&indexBufferUpload))); - m_indexBuffer->SetName(L"Index Buffer Resource"); - indexBufferUpload->SetName(L"Index Buffer Upload Resource"); + DX::SetName(m_indexBuffer.Get(), L"Index Buffer Resource"); + DX::SetName(indexBufferUpload.Get(), L"Index Buffer Upload Resource"); // Upload the index buffer to the GPU. { @@ -242,7 +242,7 @@ void Sample3DSceneRenderer::CreateDeviceDependentResources() heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; DX::ThrowIfFailed(d3dDevice->CreateDescriptorHeap(&heapDesc, IID_PPV_ARGS(&m_cbvHeap))); - m_cbvHeap->SetName(L"Constant Buffer View Descriptor Heap"); + DX::SetName(m_cbvHeap.Get(), L"Constant Buffer View Descriptor Heap"); } CD3DX12_RESOURCE_DESC constantBufferDesc = CD3DX12_RESOURCE_DESC::Buffer(DX::c_frameCount * c_alignedConstantBufferSize); @@ -254,7 +254,7 @@ void Sample3DSceneRenderer::CreateDeviceDependentResources() nullptr, IID_PPV_ARGS(&m_constantBuffer))); - m_constantBuffer->SetName(L"Constant Buffer"); + DX::SetName(m_constantBuffer.Get(), L"Constant Buffer"); // Create constant buffer views to access the upload buffer. D3D12_GPU_VIRTUAL_ADDRESS cbvGpuAddress = m_constantBuffer->GetGPUVirtualAddress(); diff --git a/Templates/DirectX12App/DirectX12App.vcxproj b/Templates/DirectX12App/DirectX12App.vcxproj index defa1231e..5880b5165 100644 --- a/Templates/DirectX12App/DirectX12App.vcxproj +++ b/Templates/DirectX12App/DirectX12App.vcxproj @@ -108,7 +108,7 @@ - mincore.lib;d3d12.lib;dxgi.lib;windowscodecs.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\arm; $(VCInstallDir)\lib\arm @@ -122,7 +122,7 @@ - mincore.lib;d3d12.lib;dxgi.lib;windowscodecs.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\arm; $(VCInstallDir)\lib\arm @@ -136,7 +136,7 @@ - mincore.lib;d3d12.lib;dxgi.lib;windowscodecs.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store; $(VCInstallDir)\lib @@ -150,7 +150,7 @@ - mincore.lib;d3d12.lib;dxgi.lib;windowscodecs.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store; $(VCInstallDir)\lib @@ -164,7 +164,7 @@ - d3d12.lib;dxgi.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\amd64; $(VCInstallDir)\lib\amd64 @@ -178,7 +178,7 @@ - mincore.lib;d3d12.lib;dxgi.lib;windowscodecs.lib;%(AdditionalDependencies) + mincore.lib;d3d12.lib;dxgi.lib;dxguid.lib;%(AdditionalDependencies) %(AdditionalLibraryDirectories); $(VCInstallDir)\lib\store\amd64; $(VCInstallDir)\lib\amd64 diff --git a/Templates/DirectX12App/pch.h b/Templates/DirectX12App/pch.h index d1f6375fa..d6fd54139 100644 --- a/Templates/DirectX12App/pch.h +++ b/Templates/DirectX12App/pch.h @@ -12,3 +12,7 @@ #include #include #include + +#if defined(_DEBUG) +#include +#endif From 7d74eeec36dc30e4e783878a5d0798d5781ecd8c Mon Sep 17 00:00:00 2001 From: "Bob Brown (DIRECTX)" Date: Tue, 5 Jan 2016 12:31:59 -0800 Subject: [PATCH 3/3] Workaround for VS 2015 Update 1 compiler bug (Issue #68) Profile and Release configs were unable to build after VS 2015 update 1 due to a bug in the compiler. Changing the assembler output flags will work around the problem until a fix is available. --- MiniEngine/Core/Core_VS14.vcxproj | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MiniEngine/Core/Core_VS14.vcxproj b/MiniEngine/Core/Core_VS14.vcxproj index 157634cd4..eca1d1ea6 100644 --- a/MiniEngine/Core/Core_VS14.vcxproj +++ b/MiniEngine/Core/Core_VS14.vcxproj @@ -70,6 +70,12 @@ true + + AssemblyAndMachineCode + + + AssemblyAndMachineCode +