Files
XCEngine/engine/include/XCEngine/RHI/D3D12/D3D12Common.h

156 lines
5.7 KiB
C++

#pragma once
#include <d3d12.h>
#include <dxgi1_4.h>
namespace XCEngine {
namespace RHI {
inline UINT GetDescriptorHandleIncrementSize(ID3D12Device* device, D3D12_DESCRIPTOR_HEAP_TYPE heapType) {
return device->GetDescriptorHandleIncrementSize(heapType);
}
inline UINT GetRTVDescriptorSize(ID3D12Device* device) {
return device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_RTV);
}
inline UINT GetDSVDescriptorSize(ID3D12Device* device) {
return device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV);
}
inline UINT GetCBV_SRV_UAVDescriptorSize(ID3D12Device* device) {
return device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}
inline UINT GetSamplerDescriptorSize(ID3D12Device* device) {
return device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER);
}
inline D3D12_RESOURCE_BARRIER CreateTransitionBarrier(
ID3D12Resource* resource,
D3D12_RESOURCE_STATES stateBefore,
D3D12_RESOURCE_STATES stateAfter,
UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES)
{
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = resource;
barrier.Transition.StateBefore = stateBefore;
barrier.Transition.StateAfter = stateAfter;
barrier.Transition.Subresource = subresource;
return barrier;
}
inline D3D12_RESOURCE_BARRIER CreateUAVBarrier(ID3D12Resource* resource = nullptr) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.UAV.pResource = resource;
return barrier;
}
inline D3D12_RESOURCE_BARRIER CreateAliasingBarrier(ID3D12Resource* beforeResource = nullptr, ID3D12Resource* afterResource = nullptr) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Aliasing.pResourceBefore = beforeResource;
barrier.Aliasing.pResourceAfter = afterResource;
return barrier;
}
inline bool CheckFormatSupport(ID3D12Device* device, DXGI_FORMAT format, D3D12_FORMAT_SUPPORT1 support1, D3D12_FORMAT_SUPPORT2 support2 = D3D12_FORMAT_SUPPORT2_NONE) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT formatSupport = {};
formatSupport.Format = format;
formatSupport.Support1 = support1;
formatSupport.Support2 = support2;
HRESULT hr = device->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &formatSupport, sizeof(formatSupport));
return SUCCEEDED(hr);
}
inline bool IsRenderTargetFormatSupported(ID3D12Device* device, DXGI_FORMAT format) {
return CheckFormatSupport(device, format, D3D12_FORMAT_SUPPORT1_RENDER_TARGET);
}
inline bool IsDepthStencilFormatSupported(ID3D12Device* device, DXGI_FORMAT format) {
return CheckFormatSupport(device, format, D3D12_FORMAT_SUPPORT1_DEPTH_STENCIL);
}
inline bool IsShaderResourceFormatSupported(ID3D12Device* device, DXGI_FORMAT format) {
return CheckFormatSupport(device, format, D3D12_FORMAT_SUPPORT1_SHADER_LOAD);
}
inline bool IsTextureFormatSupported(ID3D12Device* device, DXGI_FORMAT format) {
return CheckFormatSupport(device, format, D3D12_FORMAT_SUPPORT1_TEXTURE2D);
}
inline D3D12_CLEAR_VALUE CreateRenderTargetClearValue(DXGI_FORMAT format, float r = 0.0f, float g = 0.0f, float b = 0.0f, float a = 1.0f) {
D3D12_CLEAR_VALUE clearValue = {};
clearValue.Format = format;
clearValue.Color[0] = r;
clearValue.Color[1] = g;
clearValue.Color[2] = b;
clearValue.Color[3] = a;
return clearValue;
}
inline D3D12_CLEAR_VALUE CreateDepthStencilClearValue(DXGI_FORMAT format, float depth = 1.0f, uint8_t stencil = 0) {
D3D12_CLEAR_VALUE clearValue = {};
clearValue.Format = format;
clearValue.DepthStencil.Depth = depth;
clearValue.DepthStencil.Stencil = stencil;
return clearValue;
}
inline D3D12_VIEWPORT CreateViewport(float width, float height, float topLeftX = 0.0f, float topLeftY = 0.0f, float minDepth = 0.0f, float maxDepth = 1.0f) {
D3D12_VIEWPORT viewport = {};
viewport.TopLeftX = topLeftX;
viewport.TopLeftY = topLeftY;
viewport.Width = width;
viewport.Height = height;
viewport.MinDepth = minDepth;
viewport.MaxDepth = maxDepth;
return viewport;
}
inline D3D12_RECT CreateScissorRect(int left, int top, int right, int bottom) {
D3D12_RECT rect = {};
rect.left = left;
rect.top = top;
rect.right = right;
rect.bottom = bottom;
return rect;
}
inline D3D12_VERTEX_BUFFER_VIEW CreateVertexBufferView(D3D12_GPU_VIRTUAL_ADDRESS bufferLocation, UINT sizeInBytes, UINT strideInBytes) {
D3D12_VERTEX_BUFFER_VIEW view = {};
view.BufferLocation = bufferLocation;
view.SizeInBytes = sizeInBytes;
view.StrideInBytes = strideInBytes;
return view;
}
inline D3D12_INDEX_BUFFER_VIEW CreateIndexBufferView(D3D12_GPU_VIRTUAL_ADDRESS bufferLocation, UINT sizeInBytes, DXGI_FORMAT format) {
D3D12_INDEX_BUFFER_VIEW view = {};
view.BufferLocation = bufferLocation;
view.SizeInBytes = sizeInBytes;
view.Format = format;
return view;
}
inline D3D12_CPU_DESCRIPTOR_HANDLE GetCPUDescriptorHandle(D3D12_CPU_DESCRIPTOR_HANDLE baseHandle, UINT offsetInDescriptors, UINT descriptorSize) {
D3D12_CPU_DESCRIPTOR_HANDLE handle = baseHandle;
handle.ptr += offsetInDescriptors * descriptorSize;
return handle;
}
inline D3D12_GPU_DESCRIPTOR_HANDLE GetGPUDescriptorHandle(D3D12_GPU_DESCRIPTOR_HANDLE baseHandle, UINT offsetInDescriptors, UINT descriptorSize) {
D3D12_GPU_DESCRIPTOR_HANDLE handle = baseHandle;
handle.ptr += offsetInDescriptors * descriptorSize;
return handle;
}
} // namespace RHI
} // namespace XCEngine