cleanup and refactoring
This commit is contained in:
parent
2302158928
commit
76f6bf62a4
1285 changed files with 757994 additions and 8 deletions
29
raytracer/nvpro_core/nvdx12/README.md
Normal file
29
raytracer/nvpro_core/nvdx12/README.md
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
## Table of Contents
|
||||
- [base_dx12.hpp](#base_dx12hpp)
|
||||
- [context_dx12.hpp](#context_dx12hpp)
|
||||
- [error_dx12.hpp](#error_dx12hpp)
|
||||
|
||||
## base_dx12.hpp
|
||||
### class nvdx12::DeviceUtils
|
||||
|
||||
Utility class for simple creation of pipeline states, root signatures,
|
||||
and buffers.
|
||||
### function nvdx12::transitionBarrier
|
||||
|
||||
Short-hand function to create a transition barrier
|
||||
|
||||
## context_dx12.hpp
|
||||
### class nvdx12::Context
|
||||
|
||||
Container class for a basic DX12 app, consisting of a DXGI factory, a DX12
|
||||
device, and a command queue.
|
||||
### struct nvdx12::ContextCreateInfo
|
||||
|
||||
Properties for context initialization.
|
||||
|
||||
## error_dx12.hpp
|
||||
### function nvdx12::checkResult
|
||||
|
||||
> Returns true on critical error result, logs errors.
|
||||
|
||||
Use `HR_CHECK(result)` to automatically log filename/linenumber.
|
||||
227
raytracer/nvpro_core/nvdx12/base_dx12.cpp
Normal file
227
raytracer/nvpro_core/nvdx12/base_dx12.cpp
Normal file
|
|
@ -0,0 +1,227 @@
|
|||
/*
|
||||
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include "base_dx12.hpp"
|
||||
#include "error_dx12.hpp"
|
||||
#include <dxgi1_5.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
D3D12_RESOURCE_BARRIER transitionBarrier(_In_ ID3D12Resource* pResource,
|
||||
D3D12_RESOURCE_STATES stateBefore,
|
||||
D3D12_RESOURCE_STATES stateAfter,
|
||||
UINT subresource /*= D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES*/,
|
||||
D3D12_RESOURCE_BARRIER_FLAGS flags /*= D3D12_RESOURCE_BARRIER_FLAG_NONE*/)
|
||||
{
|
||||
D3D12_RESOURCE_BARRIER result;
|
||||
ZeroMemory(&result, sizeof(result));
|
||||
result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
|
||||
result.Flags = flags;
|
||||
result.Transition.pResource = pResource;
|
||||
result.Transition.StateBefore = stateBefore;
|
||||
result.Transition.StateAfter = stateAfter;
|
||||
result.Transition.Subresource = subresource;
|
||||
return result;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
ID3D12Device* DeviceUtils::createDevice(IDXGIFactory5* factory)
|
||||
{
|
||||
HRESULT hr = 0;
|
||||
IDXGIAdapter1* hardwareAdapter = nullptr;
|
||||
|
||||
// Look for an actual GPU. This sample does not support WARP (software)
|
||||
// devices.
|
||||
for(UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != factory->EnumAdapters1(adapterIndex, &hardwareAdapter); ++adapterIndex)
|
||||
{
|
||||
DXGI_ADAPTER_DESC1 desc;
|
||||
hardwareAdapter->GetDesc1(&desc);
|
||||
|
||||
if(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
|
||||
{
|
||||
// Don't select the Basic Render Driver adapter.
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check to see if the adapter supports Direct3D 12, but don't create the
|
||||
// actual device yet.
|
||||
if(SUCCEEDED(D3D12CreateDevice(hardwareAdapter, D3D_FEATURE_LEVEL_12_0, _uuidof(ID3D12Device), nullptr)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(hardwareAdapter == nullptr)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
DXGI_ADAPTER_DESC1 adapterDesc;
|
||||
hardwareAdapter->GetDesc1(&adapterDesc);
|
||||
printf("Running on DXGI Adapter %S\n", adapterDesc.Description);
|
||||
|
||||
// Create the DX12 device on the selected GPU
|
||||
hr = D3D12CreateDevice(hardwareAdapter, D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&m_device));
|
||||
if(FAILED(hr))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
hardwareAdapter->Release();
|
||||
return m_device;
|
||||
}
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC DeviceUtils::createDefaultPipelineDesc(D3D12_INPUT_ELEMENT_DESC* inputDescs,
|
||||
UINT inputCount,
|
||||
ID3D12RootSignature* rootSignature,
|
||||
void* vertexShaderPointer,
|
||||
size_t vertexShaderSize,
|
||||
void* pixelShaderPointer,
|
||||
size_t pixelShaderSize)
|
||||
{
|
||||
|
||||
// Describe and create the graphics pipeline state object (PSO).
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
|
||||
psoDesc.InputLayout = {inputDescs, inputCount};
|
||||
psoDesc.pRootSignature = rootSignature;
|
||||
|
||||
psoDesc.VS = {vertexShaderPointer, vertexShaderSize};
|
||||
psoDesc.PS = {pixelShaderPointer, pixelShaderSize};
|
||||
|
||||
psoDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
|
||||
psoDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
|
||||
psoDesc.RasterizerState.FrontCounterClockwise = TRUE;
|
||||
psoDesc.RasterizerState.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
|
||||
psoDesc.RasterizerState.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
|
||||
psoDesc.RasterizerState.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
|
||||
psoDesc.RasterizerState.DepthClipEnable = TRUE;
|
||||
psoDesc.RasterizerState.MultisampleEnable = FALSE;
|
||||
psoDesc.RasterizerState.AntialiasedLineEnable = FALSE;
|
||||
psoDesc.RasterizerState.ForcedSampleCount = 0;
|
||||
psoDesc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
|
||||
|
||||
psoDesc.BlendState.AlphaToCoverageEnable = FALSE;
|
||||
psoDesc.BlendState.IndependentBlendEnable = FALSE;
|
||||
|
||||
const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc = {
|
||||
FALSE,
|
||||
FALSE,
|
||||
D3D12_BLEND_ONE,
|
||||
D3D12_BLEND_ZERO,
|
||||
D3D12_BLEND_OP_ADD,
|
||||
D3D12_BLEND_ONE,
|
||||
D3D12_BLEND_ZERO,
|
||||
D3D12_BLEND_OP_ADD,
|
||||
D3D12_LOGIC_OP_NOOP,
|
||||
D3D12_COLOR_WRITE_ENABLE_ALL,
|
||||
};
|
||||
for(UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)
|
||||
psoDesc.BlendState.RenderTarget[i] = defaultRenderTargetBlendDesc;
|
||||
|
||||
psoDesc.DepthStencilState.DepthEnable = FALSE;
|
||||
psoDesc.DepthStencilState.StencilEnable = FALSE;
|
||||
psoDesc.SampleMask = UINT_MAX;
|
||||
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||
psoDesc.NumRenderTargets = 1;
|
||||
psoDesc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
psoDesc.SampleDesc.Count = 1;
|
||||
|
||||
return psoDesc;
|
||||
}
|
||||
|
||||
void DeviceUtils::addDepthStencilTestToPipeline(D3D12_GRAPHICS_PIPELINE_STATE_DESC& psoDesc,
|
||||
bool enableDepth /*= true*/,
|
||||
bool enableStencil /*= false*/,
|
||||
DXGI_FORMAT format /*= DXGI_FORMAT_D32_FLOAT*/)
|
||||
{
|
||||
D3D12_DEPTH_STENCIL_DESC depthStencilState;
|
||||
depthStencilState.DepthEnable = enableDepth ? TRUE : FALSE;
|
||||
depthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
|
||||
depthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
|
||||
depthStencilState.StencilEnable = enableStencil ? TRUE : FALSE;
|
||||
depthStencilState.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
|
||||
depthStencilState.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
|
||||
const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp = {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP,
|
||||
D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS};
|
||||
depthStencilState.FrontFace = defaultStencilOp;
|
||||
depthStencilState.BackFace = defaultStencilOp;
|
||||
|
||||
psoDesc.DepthStencilState = depthStencilState;
|
||||
psoDesc.DSVFormat = format;
|
||||
}
|
||||
|
||||
ID3D12Resource* DeviceUtils::createBuffer(uint64_t size, D3D12_RESOURCE_FLAGS flags, D3D12_RESOURCE_STATES initState, const D3D12_HEAP_PROPERTIES& heapProps)
|
||||
{
|
||||
D3D12_RESOURCE_DESC bufDesc = {};
|
||||
bufDesc.Alignment = 0;
|
||||
bufDesc.DepthOrArraySize = 1;
|
||||
bufDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
|
||||
bufDesc.Flags = flags;
|
||||
bufDesc.Format = DXGI_FORMAT_UNKNOWN;
|
||||
bufDesc.Height = 1;
|
||||
bufDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
|
||||
bufDesc.MipLevels = 1;
|
||||
bufDesc.SampleDesc.Count = 1;
|
||||
bufDesc.SampleDesc.Quality = 0;
|
||||
bufDesc.Width = size;
|
||||
|
||||
ID3D12Resource* pBuffer;
|
||||
HRESULT hr = 0;
|
||||
hr = m_device->CreateCommittedResource(&heapProps, D3D12_HEAP_FLAG_NONE, &bufDesc, initState, nullptr, IID_PPV_ARGS(&pBuffer));
|
||||
if(FAILED(hr))
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
return pBuffer;
|
||||
}
|
||||
|
||||
ID3D12RootSignature* DeviceUtils::createRootSignature(D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc)
|
||||
{
|
||||
HRESULT hr = 0;
|
||||
|
||||
ID3DBlob* serializedRootSignature;
|
||||
ID3DBlob* error;
|
||||
ID3D12RootSignature* rootSignature;
|
||||
hr = D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1, &serializedRootSignature, &error);
|
||||
if(FAILED(hr))
|
||||
{
|
||||
fprintf(stderr, "Could not serialize root signature: %s\n", (LPCSTR)(error->GetBufferPointer()));
|
||||
error->Release();
|
||||
return nullptr;
|
||||
}
|
||||
hr = m_device->CreateRootSignature(0, serializedRootSignature->GetBufferPointer(),
|
||||
serializedRootSignature->GetBufferSize(), IID_PPV_ARGS(&rootSignature));
|
||||
|
||||
serializedRootSignature->Release();
|
||||
if(FAILED(hr))
|
||||
{
|
||||
fprintf(stderr, "Could not create root signature\n");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return rootSignature;
|
||||
}
|
||||
|
||||
} // namespace nvdx12
|
||||
102
raytracer/nvpro_core/nvdx12/base_dx12.hpp
Normal file
102
raytracer/nvpro_core/nvdx12/base_dx12.hpp
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
/*
|
||||
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef NV_DX12_BASE_INCLUDED
|
||||
#define NV_DX12_BASE_INCLUDED
|
||||
|
||||
#include <assert.h>
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_5.h>
|
||||
#include <platform.h>
|
||||
#include <vector>
|
||||
|
||||
/// \todo Detect swap chain size
|
||||
#define D3D12_SWAP_CHAIN_SIZE 3
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
/** @DOC_START
|
||||
# class nvdx12::DeviceUtils
|
||||
Utility class for simple creation of pipeline states, root signatures,
|
||||
and buffers.
|
||||
-- @DOC_END - */
|
||||
|
||||
|
||||
/** @DOC_START
|
||||
# function nvdx12::transitionBarrier
|
||||
Short-hand function to create a transition barrier
|
||||
-- @DOC_END - */
|
||||
|
||||
|
||||
// Specifies a heap used for uploading. This heap type has CPU access optimized for uploading to the GPU.
|
||||
static const D3D12_HEAP_PROPERTIES uploadHeapProps = {D3D12_HEAP_TYPE_UPLOAD, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
|
||||
D3D12_MEMORY_POOL_UNKNOWN, 0, 0};
|
||||
|
||||
// Specifies the default heap. This heap type experiences the most bandwidth for the GPU, but cannot provide CPU access.
|
||||
static const D3D12_HEAP_PROPERTIES defaultHeapProps = {D3D12_HEAP_TYPE_DEFAULT, D3D12_CPU_PAGE_PROPERTY_UNKNOWN,
|
||||
D3D12_MEMORY_POOL_UNKNOWN, 0, 0};
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
|
||||
D3D12_RESOURCE_BARRIER transitionBarrier(_In_ ID3D12Resource* pResource,
|
||||
D3D12_RESOURCE_STATES stateBefore,
|
||||
D3D12_RESOURCE_STATES stateAfter,
|
||||
UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
|
||||
D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE);
|
||||
|
||||
struct DeviceUtils
|
||||
{
|
||||
DeviceUtils()
|
||||
: m_device(nullptr)
|
||||
{
|
||||
}
|
||||
DeviceUtils(ID3D12Device* device)
|
||||
: m_device(device)
|
||||
{
|
||||
}
|
||||
|
||||
ID3D12Device* createDevice(IDXGIFactory5* factory);
|
||||
|
||||
D3D12_GRAPHICS_PIPELINE_STATE_DESC createDefaultPipelineDesc(D3D12_INPUT_ELEMENT_DESC* inputDescs,
|
||||
UINT inputCount,
|
||||
ID3D12RootSignature* rootSignature,
|
||||
void* vertexShaderPointer,
|
||||
size_t vertexShaderSize,
|
||||
void* pixelShaderPointer,
|
||||
size_t pixelShaderSize);
|
||||
|
||||
void addDepthStencilTestToPipeline(D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc,
|
||||
bool enableDepth = true,
|
||||
bool enableStencil = false,
|
||||
DXGI_FORMAT format = DXGI_FORMAT_D32_FLOAT);
|
||||
|
||||
ID3D12Resource* createBuffer(uint64_t size, D3D12_RESOURCE_FLAGS flags, D3D12_RESOURCE_STATES initState, const D3D12_HEAP_PROPERTIES& heapProps);
|
||||
|
||||
ID3D12RootSignature* createRootSignature(D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc);
|
||||
|
||||
ID3D12Device* m_device;
|
||||
};
|
||||
|
||||
} // namespace nvdx12
|
||||
|
||||
#endif
|
||||
182
raytracer/nvpro_core/nvdx12/context_dx12.cpp
Normal file
182
raytracer/nvpro_core/nvdx12/context_dx12.cpp
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/*
|
||||
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include "context_dx12.hpp"
|
||||
#include "error_dx12.hpp"
|
||||
|
||||
#include <nvh/nvprint.hpp>
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
bool Context::init(const ContextCreateInfo& info)
|
||||
{
|
||||
UINT dxgiFactoryFlags = 0;
|
||||
#ifndef NDEBUG
|
||||
// Enable the debug layer (requires the Graphics Tools "optional feature").
|
||||
// This will allow the driver to output errors and track object leaks
|
||||
// NOTE: Enabling the debug layer after device creation will invalidate the
|
||||
// active device.
|
||||
{
|
||||
ID3D12Debug* debugController;
|
||||
if(SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
|
||||
{
|
||||
debugController->EnableDebugLayer();
|
||||
|
||||
// Enable additional debug layers.
|
||||
dxgiFactoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if(HR_CHECK(CreateDXGIFactory2(dxgiFactoryFlags, IID_PPV_ARGS(&m_factory))))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto compatibleAdapters = getCompatibleAdapters(info);
|
||||
if(compatibleAdapters.empty())
|
||||
{
|
||||
assert(!"No compatible adapter found");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create the DX12 device on the selected GPU
|
||||
const HRESULT hr =
|
||||
D3D12CreateDevice(compatibleAdapters[info.compatibleAdapterIndex], D3D_FEATURE_LEVEL_12_0, IID_PPV_ARGS(&m_device));
|
||||
|
||||
// Release all the adapters gathered by getCompatibleAdapters above
|
||||
for(IDXGIAdapter1* adapter : compatibleAdapters)
|
||||
{
|
||||
adapter->Release();
|
||||
}
|
||||
|
||||
if(HR_CHECK(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
D3D12_COMMAND_QUEUE_DESC queue_desc = {};
|
||||
queue_desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
|
||||
|
||||
if(HR_CHECK(m_device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&m_commandQueue))))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Context::deinit()
|
||||
{
|
||||
// Release all DX12 objects of the context
|
||||
m_commandQueue->Release();
|
||||
m_factory->Release();
|
||||
|
||||
#ifndef NDEBUG
|
||||
// If the debug layer is enabled, write on stdout whether there are any
|
||||
// leaked DX12 objects. Since the device is still alive, the report should
|
||||
// indicate a nonzero ID3D12Device reference count, but all other references
|
||||
// should have a Refcount: 0. The nonzero IntRef indicates the DX12-internal
|
||||
// references to the object, which the driver will release upon release of
|
||||
// the device.
|
||||
HRESULT hr;
|
||||
ID3D12DebugDevice* debugDevice = nullptr;
|
||||
hr = m_device->QueryInterface(__uuidof(ID3D12DebugDevice), (void**)(&debugDevice));
|
||||
if(SUCCEEDED(hr))
|
||||
{
|
||||
debugDevice->ReportLiveDeviceObjects(D3D12_RLDO_DETAIL | D3D12_RLDO_IGNORE_INTERNAL);
|
||||
debugDevice->Release();
|
||||
}
|
||||
#endif
|
||||
|
||||
// Release the device itself
|
||||
unsigned long deviceRefs = 0;
|
||||
deviceRefs = m_device->Release();
|
||||
assert(deviceRefs == 0 && "Some references to the device have not been released properly");
|
||||
}
|
||||
|
||||
std::vector<IDXGIAdapter1*> Context::getCompatibleAdapters(const ContextCreateInfo& info)
|
||||
{
|
||||
if(info.verboseCompatibleAdapters)
|
||||
{
|
||||
LOGI("____________________\n");
|
||||
LOGI("Compatible Adapters :\n");
|
||||
}
|
||||
|
||||
IDXGIAdapter1* adapter = nullptr;
|
||||
std::vector<IDXGIAdapter1*> compatibleAdapters;
|
||||
|
||||
// Find the adapters that represents a GPU device and support Direct3D 12.
|
||||
for(UINT adapterIndex = 0; SUCCEEDED(m_factory->EnumAdapters1(adapterIndex, &adapter)); ++adapterIndex)
|
||||
{
|
||||
DXGI_ADAPTER_DESC1 desc;
|
||||
adapter->GetDesc1(&desc);
|
||||
|
||||
// Ignore the sotware "Basic Render Driver" adapter.
|
||||
if(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
|
||||
{
|
||||
adapter->Release();
|
||||
continue;
|
||||
}
|
||||
|
||||
char description[128 * 4] = "";
|
||||
WideCharToMultiByte(CP_UTF8, 0, desc.Description, 128, description, sizeof(description), nullptr, nullptr);
|
||||
|
||||
// Check to see if the adapter supports Direct3D 12, but don't create the actual device yet.
|
||||
if(SUCCEEDED(D3D12CreateDevice(adapter, D3D_FEATURE_LEVEL_12_0, _uuidof(ID3D12Device), nullptr)))
|
||||
{
|
||||
if(info.verboseCompatibleAdapters)
|
||||
{
|
||||
LOGI("%zu: %s\n", compatibleAdapters.size(), description);
|
||||
}
|
||||
compatibleAdapters.push_back(adapter);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(info.verboseCompatibleAdapters)
|
||||
{
|
||||
LOGW("Skipping adapter %s\n", description);
|
||||
}
|
||||
adapter->Release();
|
||||
}
|
||||
}
|
||||
|
||||
if(adapter)
|
||||
{
|
||||
adapter->Release();
|
||||
}
|
||||
|
||||
if(info.verboseCompatibleAdapters)
|
||||
{
|
||||
LOGI("Compatible adapters devices found : ");
|
||||
if(!compatibleAdapters.empty())
|
||||
{
|
||||
LOGI("%zu\n", compatibleAdapters.size());
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGI("OMG... NONE !!\n");
|
||||
}
|
||||
}
|
||||
|
||||
return compatibleAdapters;
|
||||
}
|
||||
|
||||
} // namespace nvdx12
|
||||
74
raytracer/nvpro_core/nvdx12/context_dx12.hpp
Normal file
74
raytracer/nvpro_core/nvdx12/context_dx12.hpp
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2016-2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2016-2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#ifndef NV_DX12_CONTEXT_INCLUDED
|
||||
#define NV_DX12_CONTEXT_INCLUDED
|
||||
|
||||
#include <d3d12.h>
|
||||
#include <dxgi1_5.h>
|
||||
#include <vector>
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/** @DOC_START
|
||||
# class nvdx12::Context
|
||||
Container class for a basic DX12 app, consisting of a DXGI factory, a DX12
|
||||
device, and a command queue.
|
||||
-- @DOC_END */
|
||||
|
||||
|
||||
/** @DOC_START
|
||||
# struct nvdx12::ContextCreateInfo
|
||||
Properties for context initialization.
|
||||
-- @DOC_END */
|
||||
|
||||
|
||||
struct ContextCreateInfo
|
||||
{
|
||||
UINT compatibleAdapterIndex = 0;
|
||||
|
||||
// Information printed at Context::init time
|
||||
bool verboseCompatibleAdapters = false;
|
||||
};
|
||||
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
Context(Context const&) = delete;
|
||||
Context& operator=(Context const&) = delete;
|
||||
|
||||
Context() = default;
|
||||
|
||||
IDXGIFactory5* m_factory;
|
||||
ID3D12Device* m_device;
|
||||
ID3D12CommandQueue* m_commandQueue;
|
||||
|
||||
bool init(const ContextCreateInfo& info);
|
||||
void deinit();
|
||||
|
||||
std::vector<IDXGIAdapter1*> getCompatibleAdapters(const ContextCreateInfo& info);
|
||||
};
|
||||
|
||||
} // namespace nvdx12
|
||||
|
||||
#endif
|
||||
83
raytracer/nvpro_core/nvdx12/error_dx12.cpp
Normal file
83
raytracer/nvpro_core/nvdx12/error_dx12.cpp
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
#include "error_dx12.hpp"
|
||||
|
||||
#include <nvh/nvprint.hpp>
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
const char* getResultString(HRESULT hr)
|
||||
{
|
||||
const char* resultString = "unknown";
|
||||
|
||||
#define STR(a) \
|
||||
case a: \
|
||||
resultString = #a; \
|
||||
break;
|
||||
|
||||
switch(hr)
|
||||
{
|
||||
STR(S_OK);
|
||||
STR(E_FAIL);
|
||||
STR(E_INVALIDARG);
|
||||
STR(E_OUTOFMEMORY);
|
||||
STR(DXGI_ERROR_INVALID_CALL);
|
||||
STR(DXGI_ERROR_WAS_STILL_DRAWING);
|
||||
STR(D3D12_ERROR_ADAPTER_NOT_FOUND);
|
||||
STR(D3D12_ERROR_DRIVER_VERSION_MISMATCH);
|
||||
}
|
||||
#undef STR
|
||||
return resultString;
|
||||
}
|
||||
|
||||
bool checkResult(HRESULT hr, const char* message)
|
||||
{
|
||||
if(SUCCEEDED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if(message)
|
||||
{
|
||||
LOGE("HRESULT %li - %s - %s\n", hr, getResultString(hr), message);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOGE("HRESULT %li - %s\n", hr, getResultString(hr));
|
||||
}
|
||||
assert(!"Critical DX12 Error");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool checkResult(HRESULT hr, const char* file, int line)
|
||||
{
|
||||
if(SUCCEEDED(hr))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
LOGE("%s(%i): DX12 Error : %s\n", file, line, getResultString(hr));
|
||||
assert(!"Critical DX12 Error");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace nvdx12
|
||||
43
raytracer/nvpro_core/nvdx12/error_dx12.hpp
Normal file
43
raytracer/nvpro_core/nvdx12/error_dx12.hpp
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 2021 NVIDIA CORPORATION
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
/** @DOC_START
|
||||
# function nvdx12::checkResult
|
||||
> Returns true on critical error result, logs errors.
|
||||
|
||||
Use `HR_CHECK(result)` to automatically log filename/linenumber.
|
||||
@DOC_END */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <d3d12.h>
|
||||
|
||||
namespace nvdx12 {
|
||||
|
||||
bool checkResult(HRESULT hr, const char* message = nullptr);
|
||||
bool checkResult(HRESULT hr, const char* file, int line);
|
||||
|
||||
#ifndef HR_CHECK
|
||||
#define HR_CHECK(result) nvdx12::checkResult(result, __FILE__, __LINE__)
|
||||
#endif
|
||||
|
||||
} // namespace nvdx12
|
||||
Loading…
Add table
Add a link
Reference in a new issue