2015-11-03 04:19:41 +00:00
|
|
|
#include "../win/Win32Common.hpp"
|
2015-11-06 03:20:58 +00:00
|
|
|
#if _WIN32_WINNT_WIN10
|
|
|
|
#include <LogVisor/LogVisor.hpp>
|
|
|
|
#include "boo/graphicsdev/D3D.hpp"
|
2015-11-02 05:41:24 +00:00
|
|
|
#include "boo/IGraphicsContext.hpp"
|
|
|
|
#include <vector>
|
|
|
|
#include "d3dx12.h"
|
|
|
|
#include <d3dcompiler.h>
|
2015-11-05 00:00:29 +00:00
|
|
|
#include <comdef.h>
|
|
|
|
|
2015-11-05 04:57:48 +00:00
|
|
|
#define MAX_UNIFORM_COUNT 8
|
|
|
|
#define MAX_TEXTURE_COUNT 8
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
extern PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignaturePROC;
|
2015-11-07 01:43:12 +00:00
|
|
|
extern pD3DCompile D3DCompilePROC;
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
namespace boo
|
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
static LogVisor::LogModule Log("boo::D3D12");
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
static inline void ThrowIfFailed(HRESULT hr)
|
|
|
|
{
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
// Set a breakpoint on this line to catch Win32 API errors.
|
2015-11-05 00:00:29 +00:00
|
|
|
_com_error err(hr);
|
|
|
|
LPCTSTR errMsg = err.ErrorMessage();
|
|
|
|
Log.report(LogVisor::FatalError, errMsg);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline UINT64 NextHeapOffset(UINT64 offset, const D3D12_RESOURCE_ALLOCATION_INFO& info)
|
|
|
|
{
|
|
|
|
offset += info.SizeInBytes;
|
|
|
|
return (offset + info.Alignment - 1) & ~(info.Alignment - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct D3D12Data : IGraphicsData
|
|
|
|
{
|
|
|
|
std::vector<std::unique_ptr<class D3D12ShaderPipeline>> m_SPs;
|
|
|
|
std::vector<std::unique_ptr<struct D3D12ShaderDataBinding>> m_SBinds;
|
|
|
|
std::vector<std::unique_ptr<class D3D12GraphicsBufferS>> m_SBufs;
|
|
|
|
std::vector<std::unique_ptr<class D3D12GraphicsBufferD>> m_DBufs;
|
|
|
|
std::vector<std::unique_ptr<class D3D12TextureS>> m_STexs;
|
2015-11-27 22:14:05 +00:00
|
|
|
std::vector<std::unique_ptr<class D3D12TextureSA>> m_SATexs;
|
2015-11-02 05:41:24 +00:00
|
|
|
std::vector<std::unique_ptr<class D3D12TextureD>> m_DTexs;
|
2015-11-04 00:27:32 +00:00
|
|
|
std::vector<std::unique_ptr<class D3D12TextureR>> m_RTexs;
|
2015-11-02 05:41:24 +00:00
|
|
|
std::vector<std::unique_ptr<struct D3D12VertexFormat>> m_VFmts;
|
2015-11-07 20:03:21 +00:00
|
|
|
ComPtr<ID3D12Heap> m_bufHeap;
|
|
|
|
ComPtr<ID3D12Heap> m_texHeap;
|
2015-11-02 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const D3D12_RESOURCE_STATES USE_TABLE[] =
|
|
|
|
{
|
|
|
|
D3D12_RESOURCE_STATE_COMMON,
|
|
|
|
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER,
|
|
|
|
D3D12_RESOURCE_STATE_INDEX_BUFFER,
|
|
|
|
D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER
|
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12GraphicsBufferS : public IGraphicsBufferS
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
|
|
|
friend struct D3D12CommandQueue;
|
|
|
|
D3D12_RESOURCE_STATES m_state;
|
2015-11-05 00:00:29 +00:00
|
|
|
size_t m_sz;
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D12_RESOURCE_DESC m_gpuDesc;
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12GraphicsBufferS(BufferUse use, D3D12Context* ctx, const void* data, size_t stride, size_t count)
|
2015-11-21 02:58:56 +00:00
|
|
|
: m_state(USE_TABLE[int(use)]), m_stride(stride), m_count(count), m_sz(stride * count)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
m_gpuDesc = CD3DX12_RESOURCE_DESC::Buffer(m_sz);
|
2015-11-18 06:14:49 +00:00
|
|
|
size_t reqSz = GetRequiredIntermediateSize(ctx->m_dev.Get(), &m_gpuDesc, 0, 1);
|
|
|
|
m_gpuDesc = CD3DX12_RESOURCE_DESC::Buffer(reqSz);
|
2015-11-02 05:41:24 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
|
|
|
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D12_HEAP_FLAG_NONE, &m_gpuDesc,
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_buf));
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_SUBRESOURCE_DATA upData = {data, LONG_PTR(m_sz), LONG_PTR(m_sz)};
|
|
|
|
if (!PrepSubresources<1>(ctx->m_dev.Get(), &m_gpuDesc, m_buf.Get(), 0, 0, 1, &upData))
|
2015-11-06 03:20:58 +00:00
|
|
|
Log.report(LogVisor::FatalError, "error preparing resource for upload");
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
|
|
|
ComPtr<ID3D12Resource> m_buf;
|
|
|
|
ComPtr<ID3D12Resource> m_gpuBuf;
|
|
|
|
~D3D12GraphicsBufferS() = default;
|
|
|
|
|
|
|
|
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &m_gpuDesc, m_state,
|
2015-11-02 05:41:24 +00:00
|
|
|
nullptr, __uuidof(ID3D12Resource), &m_gpuBuf));
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
/* Stage resource upload */
|
2015-11-18 06:14:49 +00:00
|
|
|
CommandSubresourcesTransfer<1>(ctx->m_dev.Get(), ctx->m_loadlist.Get(), m_gpuBuf.Get(), m_buf.Get(), 0, 0, 1);
|
2015-11-05 00:00:29 +00:00
|
|
|
ctx->m_loadlist->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_gpuBuf.Get(),
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
return NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &m_gpuDesc));
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12GraphicsBufferD : public IGraphicsBufferD
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
|
|
|
friend struct D3D12CommandQueue;
|
|
|
|
D3D12CommandQueue* m_q;
|
|
|
|
D3D12_RESOURCE_STATES m_state;
|
2015-11-18 06:14:49 +00:00
|
|
|
size_t m_mappedSz;
|
|
|
|
void* m_mappedBuf = nullptr;
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12GraphicsBufferD(D3D12CommandQueue* q, BufferUse use, D3D12Context* ctx, size_t stride, size_t count)
|
2015-11-21 02:58:56 +00:00
|
|
|
: m_state(USE_TABLE[int(use)]), m_q(q), m_stride(stride), m_count(count)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
size_t sz = stride * count;
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(sz);
|
|
|
|
size_t reqSz = GetRequiredIntermediateSize(ctx->m_dev.Get(), &desc, 0, 1);
|
|
|
|
desc = CD3DX12_RESOURCE_DESC::Buffer(reqSz);
|
2015-11-04 00:27:32 +00:00
|
|
|
for (int i=0 ; i<2 ; ++i)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
|
|
|
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_HEAP_FLAG_NONE, &desc,
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_bufs[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
2015-11-04 00:27:32 +00:00
|
|
|
ComPtr<ID3D12Resource> m_bufs[2];
|
|
|
|
ComPtr<ID3D12Resource> m_gpuBufs[2];
|
2015-11-02 05:41:24 +00:00
|
|
|
~D3D12GraphicsBufferD() = default;
|
|
|
|
|
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
|
|
|
|
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
2015-11-04 00:27:32 +00:00
|
|
|
for (int i=0 ; i<2 ; ++i)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
D3D12_RESOURCE_DESC desc = m_bufs[i]->GetDesc();
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &desc, m_state,
|
|
|
|
nullptr, __uuidof(ID3D12Resource), &m_gpuBufs[i]));
|
|
|
|
offset = NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &desc));
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12TextureS : public ITextureS
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
2015-11-27 22:14:05 +00:00
|
|
|
TextureFormat m_fmt;
|
2015-11-05 00:00:29 +00:00
|
|
|
size_t m_sz;
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D12_RESOURCE_DESC m_gpuDesc;
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12TextureS(D3D12Context* ctx, size_t width, size_t height, size_t mips,
|
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
2015-11-27 22:14:05 +00:00
|
|
|
: m_fmt(fmt), m_sz(sz)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
m_gpuDesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, width, height, 1, mips);
|
2015-11-18 06:14:49 +00:00
|
|
|
size_t reqSz = GetRequiredIntermediateSize(ctx->m_dev.Get(), &m_gpuDesc, 0, mips);
|
2015-11-02 05:41:24 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
|
|
|
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(reqSz),
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_tex));
|
2015-11-05 00:00:29 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
2015-11-05 00:00:29 +00:00
|
|
|
D3D12_SUBRESOURCE_DATA upData[16] = {};
|
2015-11-06 03:20:58 +00:00
|
|
|
for (size_t i=0 ; i<m_gpuDesc.MipLevels && i<16 ; ++i)
|
2015-11-05 00:00:29 +00:00
|
|
|
{
|
|
|
|
upData[i].pData = dataIt;
|
|
|
|
upData[i].RowPitch = width * 4;
|
|
|
|
upData[i].SlicePitch = upData[i].RowPitch * height;
|
|
|
|
dataIt += upData[i].SlicePitch;
|
|
|
|
width /= 2;
|
|
|
|
height /= 2;
|
|
|
|
}
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
if (!PrepSubresources<16>(ctx->m_dev.Get(), &m_gpuDesc, m_tex.Get(), 0, 0, m_gpuDesc.MipLevels, upData))
|
2015-11-05 00:00:29 +00:00
|
|
|
Log.report(LogVisor::FatalError, "error preparing resource for upload");
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D12Resource> m_tex;
|
|
|
|
ComPtr<ID3D12Resource> m_gpuTex;
|
|
|
|
~D3D12TextureS() = default;
|
|
|
|
|
|
|
|
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &m_gpuDesc,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
|
nullptr, __uuidof(ID3D12Resource), &m_gpuTex));
|
|
|
|
|
|
|
|
CommandSubresourcesTransfer<16>(ctx->m_dev.Get(), ctx->m_loadlist.Get(), m_gpuTex.Get(), m_tex.Get(), 0, 0, m_gpuDesc.MipLevels);
|
2015-11-05 00:00:29 +00:00
|
|
|
ctx->m_loadlist->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_gpuTex.Get(),
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
return NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &m_gpuDesc));
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
|
|
|
|
TextureFormat format() const {return m_fmt;}
|
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12TextureSA : public ITextureSA
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
|
|
|
TextureFormat m_fmt;
|
|
|
|
size_t m_layers;
|
|
|
|
size_t m_sz;
|
|
|
|
D3D12_RESOURCE_DESC m_gpuDesc;
|
|
|
|
D3D12TextureSA(D3D12Context* ctx, size_t width, size_t height, size_t layers,
|
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
|
|
|
: m_fmt(fmt), m_layers(layers), m_sz(sz)
|
|
|
|
{
|
|
|
|
size_t pixelPitch;
|
|
|
|
DXGI_FORMAT pixelFmt;
|
|
|
|
if (fmt == TextureFormat::RGBA8)
|
|
|
|
{
|
|
|
|
pixelPitch = 4;
|
|
|
|
pixelFmt = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
}
|
|
|
|
else if (fmt == TextureFormat::I8)
|
|
|
|
{
|
|
|
|
pixelPitch = 1;
|
|
|
|
pixelFmt = DXGI_FORMAT_R8_UNORM;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_gpuDesc = CD3DX12_RESOURCE_DESC::Tex2D(pixelFmt, width, height, layers, 1);
|
|
|
|
size_t reqSz = GetRequiredIntermediateSize(ctx->m_dev.Get(), &m_gpuDesc, 0, layers);
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
|
|
|
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
|
|
|
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(reqSz),
|
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_tex));
|
|
|
|
|
|
|
|
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
|
|
|
D3D12_SUBRESOURCE_DATA upData[16] = {};
|
|
|
|
for (size_t i=0 ; i<layers && i<16 ; ++i)
|
|
|
|
{
|
|
|
|
upData[i].pData = dataIt;
|
|
|
|
upData[i].RowPitch = width * pixelPitch;
|
|
|
|
upData[i].SlicePitch = upData[i].RowPitch * height;
|
|
|
|
dataIt += upData[i].SlicePitch;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PrepSubresources<16>(ctx->m_dev.Get(), &m_gpuDesc, m_tex.Get(), 0, 0, layers, upData))
|
|
|
|
Log.report(LogVisor::FatalError, "error preparing resource for upload");
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D12Resource> m_tex;
|
|
|
|
ComPtr<ID3D12Resource> m_gpuTex;
|
|
|
|
~D3D12TextureSA() = default;
|
|
|
|
|
|
|
|
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &m_gpuDesc,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST,
|
|
|
|
nullptr, __uuidof(ID3D12Resource), &m_gpuTex));
|
|
|
|
|
|
|
|
CommandSubresourcesTransfer<16>(ctx->m_dev.Get(), ctx->m_loadlist.Get(), m_gpuTex.Get(), m_tex.Get(), 0, 0, m_gpuDesc.DepthOrArraySize);
|
|
|
|
ctx->m_loadlist->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_gpuTex.Get(),
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
|
|
|
|
|
|
|
return NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &m_gpuDesc));
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureFormat format() const {return m_fmt;}
|
|
|
|
size_t layers() const {return m_layers;}
|
2015-11-02 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12TextureD : public ITextureD
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
|
|
|
friend struct D3D12CommandQueue;
|
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
2015-11-27 22:14:05 +00:00
|
|
|
TextureFormat m_fmt;
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12CommandQueue* m_q;
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_RESOURCE_DESC m_gpuDesc;
|
|
|
|
size_t m_mappedSz;
|
|
|
|
void* m_mappedBuf = nullptr;
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12TextureD(D3D12CommandQueue* q, D3D12Context* ctx, size_t width, size_t height, TextureFormat fmt)
|
2015-11-27 22:14:05 +00:00
|
|
|
: m_fmt(fmt), m_q(q)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
m_gpuDesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, width, height);
|
|
|
|
size_t reqSz = GetRequiredIntermediateSize(ctx->m_dev.Get(), &m_gpuDesc, 0, 1);
|
2015-11-04 00:27:32 +00:00
|
|
|
for (int i=0 ; i<2 ; ++i)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
|
|
|
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
2015-11-07 20:03:21 +00:00
|
|
|
D3D12_HEAP_FLAG_NONE,
|
2015-11-18 06:14:49 +00:00
|
|
|
&CD3DX12_RESOURCE_DESC::Buffer(reqSz),
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_texs[i]));
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D12Resource> m_texs[2];
|
|
|
|
ComPtr<ID3D12Resource> m_gpuTexs[2];
|
|
|
|
~D3D12TextureD() = default;
|
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
|
|
|
for (int i=0 ; i<2 ; ++i)
|
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &m_gpuDesc,
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
2015-11-02 05:41:24 +00:00
|
|
|
nullptr, __uuidof(ID3D12Resource), &m_gpuTexs[i]));
|
2015-11-18 06:14:49 +00:00
|
|
|
offset = NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &m_gpuDesc));
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-04 00:27:32 +00:00
|
|
|
return offset;
|
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
|
|
|
|
TextureFormat format() const {return m_fmt;}
|
2015-11-04 00:27:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const float BLACK_COLOR[] = {0.0,0.0,0.0,1.0};
|
|
|
|
|
|
|
|
class D3D12TextureR : public ITextureR
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
|
|
|
friend struct D3D12CommandQueue;
|
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
|
|
|
size_t m_samples = 0;
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
void Setup(D3D12Context* ctx, size_t width, size_t height, size_t samples)
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_DESC rtvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, m_width, m_height, 1, 0, 1,
|
|
|
|
0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET);
|
2015-11-04 00:27:32 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE,
|
|
|
|
&rtvresdesc, D3D12_RESOURCE_STATE_RENDER_TARGET, &CD3DX12_CLEAR_VALUE(DXGI_FORMAT_R8G8B8A8_UNORM, BLACK_COLOR),
|
2015-11-06 03:20:58 +00:00
|
|
|
__uuidof(ID3D12Resource), &m_gpuTex));
|
2015-11-03 04:19:41 +00:00
|
|
|
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC rtvdesc = {D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1};
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDescriptorHeap(&rtvdesc, __uuidof(ID3D12DescriptorHeap), &m_rtvHeap));
|
|
|
|
|
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC dsvdesc = {D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1};
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDescriptorHeap(&dsvdesc, __uuidof(ID3D12DescriptorHeap), &m_dsvHeap));
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
if (samples > 1)
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_DESC rtvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, m_width, m_height, 1, 0, samples,
|
|
|
|
0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE,
|
|
|
|
&rtvresdesc, D3D12_RESOURCE_STATE_RENDER_TARGET, &CD3DX12_CLEAR_VALUE(DXGI_FORMAT_R8G8B8A8_UNORM, BLACK_COLOR),
|
2015-11-06 03:20:58 +00:00
|
|
|
__uuidof(ID3D12Resource), &m_gpuMsaaTex));
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
CD3DX12_RESOURCE_DESC dsvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D24_UNORM_S8_UINT, m_width, m_height, 1, 0, samples,
|
|
|
|
0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE,
|
|
|
|
&dsvresdesc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &CD3DX12_CLEAR_VALUE(DXGI_FORMAT_D24_UNORM_S8_UINT, 1.0, 0),
|
2015-11-06 03:20:58 +00:00
|
|
|
__uuidof(ID3D12Resource), &m_depthTex));
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
D3D12_RENDER_TARGET_VIEW_DESC rtvvdesc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RTV_DIMENSION_TEXTURE2D};
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->m_dev->CreateRenderTargetView(m_gpuMsaaTex.Get(), &rtvvdesc, m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
D3D12_DEPTH_STENCIL_VIEW_DESC dsvvdesc = {DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_DSV_DIMENSION_TEXTURE2D};
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->m_dev->CreateDepthStencilView(m_depthTex.Get(), &dsvvdesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-05 00:00:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
CD3DX12_RESOURCE_DESC dsvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D24_UNORM_S8_UINT, m_width, m_height, 1, 0, 1,
|
|
|
|
0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL);
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT), D3D12_HEAP_FLAG_NONE,
|
|
|
|
&dsvresdesc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &CD3DX12_CLEAR_VALUE(DXGI_FORMAT_D24_UNORM_S8_UINT, 1.0, 0),
|
|
|
|
__uuidof(ID3D12Resource), &m_depthTex));
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
D3D12_RENDER_TARGET_VIEW_DESC rtvvdesc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RTV_DIMENSION_TEXTURE2D};
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->m_dev->CreateRenderTargetView(m_gpuTex.Get(), &rtvvdesc, m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
D3D12_DEPTH_STENCIL_VIEW_DESC dsvvdesc = {DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_DSV_DIMENSION_TEXTURE2D};
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->m_dev->CreateDepthStencilView(m_depthTex.Get(), &dsvvdesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-05 00:00:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D12TextureR(D3D12Context* ctx, size_t width, size_t height, size_t samples)
|
|
|
|
: m_width(width), m_height(height), m_samples(samples)
|
|
|
|
{
|
|
|
|
if (samples == 0) m_samples = 1;
|
|
|
|
Setup(ctx, width, height, samples);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-04 00:27:32 +00:00
|
|
|
public:
|
2015-11-05 00:00:29 +00:00
|
|
|
size_t samples() const {return m_samples;}
|
2015-11-06 03:20:58 +00:00
|
|
|
ComPtr<ID3D12Resource> m_gpuTex;
|
|
|
|
ComPtr<ID3D12Resource> m_gpuMsaaTex;
|
|
|
|
ComPtr<ID3D12Resource> m_depthTex;
|
2015-11-04 00:27:32 +00:00
|
|
|
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
|
|
|
|
ComPtr<ID3D12DescriptorHeap> m_dsvHeap;
|
|
|
|
~D3D12TextureR() = default;
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
void resize(D3D12Context* ctx, size_t width, size_t height)
|
|
|
|
{
|
2015-11-05 04:57:48 +00:00
|
|
|
if (width < 1)
|
|
|
|
width = 1;
|
|
|
|
if (height < 1)
|
|
|
|
height = 1;
|
2015-11-05 00:00:29 +00:00
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
|
|
|
Setup(ctx, width, height, m_samples);
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
ID3D12Resource* getRenderColorRes() const
|
|
|
|
{if (m_samples > 1) return m_gpuMsaaTex.Get(); return m_gpuTex.Get();}
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
static const size_t SEMANTIC_SIZE_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
0,
|
2015-11-02 05:41:24 +00:00
|
|
|
12,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
2015-11-02 05:41:24 +00:00
|
|
|
12,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-02 05:41:24 +00:00
|
|
|
4,
|
|
|
|
8,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-02 05:41:24 +00:00
|
|
|
16
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* SEMANTIC_NAME_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
nullptr,
|
|
|
|
"POSITION",
|
2015-11-02 05:41:24 +00:00
|
|
|
"POSITION",
|
|
|
|
"NORMAL",
|
2015-11-27 22:14:05 +00:00
|
|
|
"NORMAL",
|
|
|
|
"COLOR",
|
2015-11-02 05:41:24 +00:00
|
|
|
"COLOR",
|
|
|
|
"UV",
|
2015-11-27 22:14:05 +00:00
|
|
|
"UV",
|
|
|
|
"WEIGHT",
|
|
|
|
"MODELVIEW"
|
2015-11-02 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const DXGI_FORMAT SEMANTIC_TYPE_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_UNKNOWN,
|
2015-11-02 05:41:24 +00:00
|
|
|
DXGI_FORMAT_R32G32B32_FLOAT,
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2015-11-02 05:41:24 +00:00
|
|
|
DXGI_FORMAT_R32G32B32_FLOAT,
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2015-11-02 05:41:24 +00:00
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM,
|
|
|
|
DXGI_FORMAT_R32G32_FLOAT,
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2015-11-02 05:41:24 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
};
|
|
|
|
|
|
|
|
struct D3D12VertexFormat : IVertexFormat
|
|
|
|
{
|
|
|
|
size_t m_elementCount;
|
|
|
|
std::unique_ptr<D3D12_INPUT_ELEMENT_DESC[]> m_elements;
|
|
|
|
D3D12VertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
2015-11-05 00:00:29 +00:00
|
|
|
: m_elementCount(elementCount),
|
|
|
|
m_elements(new D3D12_INPUT_ELEMENT_DESC[elementCount])
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
memset(m_elements.get(), 0, elementCount * sizeof(D3D12_INPUT_ELEMENT_DESC));
|
2015-11-05 00:00:29 +00:00
|
|
|
size_t offset = 0;
|
2015-11-27 22:14:05 +00:00
|
|
|
size_t instOffset = 0;
|
2015-11-02 05:41:24 +00:00
|
|
|
for (size_t i=0 ; i<elementCount ; ++i)
|
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
const VertexElementDescriptor* elemin = &elements[i];
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_INPUT_ELEMENT_DESC& elem = m_elements[i];
|
2015-11-27 22:14:05 +00:00
|
|
|
int semantic = int(elemin->semantic & boo::VertexSemantic::SemanticMask);
|
|
|
|
elem.SemanticName = SEMANTIC_NAME_TABLE[semantic];
|
2015-11-05 00:00:29 +00:00
|
|
|
elem.SemanticIndex = elemin->semanticIdx;
|
2015-11-27 22:14:05 +00:00
|
|
|
elem.Format = SEMANTIC_TYPE_TABLE[semantic];
|
|
|
|
if ((elemin->semantic & boo::VertexSemantic::Instanced) != boo::VertexSemantic::None)
|
|
|
|
{
|
|
|
|
elem.InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_INSTANCE_DATA;
|
|
|
|
elem.InstanceDataStepRate = 1;
|
|
|
|
elem.InputSlot = 1;
|
|
|
|
elem.AlignedByteOffset = instOffset;
|
|
|
|
instOffset += SEMANTIC_SIZE_TABLE[semantic];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elem.InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
|
|
|
elem.AlignedByteOffset = offset;
|
|
|
|
offset += SEMANTIC_SIZE_TABLE[semantic];
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const D3D12_BLEND BLEND_FACTOR_TABLE[] =
|
|
|
|
{
|
|
|
|
D3D12_BLEND_ZERO,
|
|
|
|
D3D12_BLEND_ONE,
|
|
|
|
D3D12_BLEND_SRC_COLOR,
|
|
|
|
D3D12_BLEND_INV_SRC_COLOR,
|
|
|
|
D3D12_BLEND_DEST_COLOR,
|
|
|
|
D3D12_BLEND_INV_DEST_COLOR,
|
|
|
|
D3D12_BLEND_SRC_ALPHA,
|
|
|
|
D3D12_BLEND_INV_SRC_ALPHA,
|
|
|
|
D3D12_BLEND_DEST_ALPHA,
|
2015-11-27 22:14:05 +00:00
|
|
|
D3D12_BLEND_INV_DEST_ALPHA,
|
|
|
|
D3D12_BLEND_SRC1_COLOR,
|
|
|
|
D3D12_BLEND_INV_SRC1_COLOR
|
2015-11-02 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class D3D12ShaderPipeline : public IShaderPipeline
|
|
|
|
{
|
|
|
|
friend class D3D12DataFactory;
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12ShaderPipeline(D3D12Context* ctx, ID3DBlob* vert, ID3DBlob* pixel, ID3DBlob* pipeline,
|
2015-11-02 05:41:24 +00:00
|
|
|
const D3D12VertexFormat* vtxFmt,
|
|
|
|
BlendFactor srcFac, BlendFactor dstFac,
|
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
|
|
|
{
|
|
|
|
D3D12_GRAPHICS_PIPELINE_STATE_DESC desc = {};
|
|
|
|
desc.pRootSignature = ctx->m_rs.Get();
|
|
|
|
desc.VS = {vert->GetBufferPointer(), vert->GetBufferSize()};
|
|
|
|
desc.PS = {pixel->GetBufferPointer(), pixel->GetBufferSize()};
|
|
|
|
desc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
|
2015-11-21 02:58:56 +00:00
|
|
|
if (dstFac != BlendFactor::Zero)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
desc.BlendState.RenderTarget[0].BlendEnable = true;
|
2015-11-21 02:58:56 +00:00
|
|
|
desc.BlendState.RenderTarget[0].SrcBlend = BLEND_FACTOR_TABLE[int(srcFac)];
|
|
|
|
desc.BlendState.RenderTarget[0].DestBlend = BLEND_FACTOR_TABLE[int(dstFac)];
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
desc.RasterizerState = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
|
|
|
|
if (!backfaceCulling)
|
|
|
|
desc.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
|
|
|
|
desc.DepthStencilState = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
|
|
|
|
if (!depthTest)
|
|
|
|
desc.DepthStencilState.DepthEnable = false;
|
|
|
|
if (!depthWrite)
|
|
|
|
desc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO;
|
|
|
|
desc.InputLayout.NumElements = vtxFmt->m_elementCount;
|
|
|
|
desc.InputLayout.pInputElementDescs = vtxFmt->m_elements.get();
|
|
|
|
desc.SampleMask = UINT_MAX;
|
|
|
|
desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
|
|
|
desc.NumRenderTargets = 1;
|
|
|
|
desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2015-11-05 00:00:29 +00:00
|
|
|
desc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
2015-11-02 05:41:24 +00:00
|
|
|
desc.SampleDesc.Count = 1;
|
2015-11-18 06:14:49 +00:00
|
|
|
if (pipeline)
|
|
|
|
{
|
|
|
|
desc.CachedPSO.pCachedBlob = pipeline->GetBufferPointer();
|
|
|
|
desc.CachedPSO.CachedBlobSizeInBytes = pipeline->GetBufferSize();
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateGraphicsPipelineState(&desc, __uuidof(ID3D12PipelineState), &m_state));
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D12PipelineState> m_state;
|
|
|
|
~D3D12ShaderPipeline() = default;
|
|
|
|
D3D12ShaderPipeline& operator=(const D3D12ShaderPipeline&) = delete;
|
|
|
|
D3D12ShaderPipeline(const D3D12ShaderPipeline&) = delete;
|
|
|
|
};
|
|
|
|
|
|
|
|
static UINT64 PlaceBufferForGPU(IGraphicsBuffer* buf, D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
return static_cast<D3D12GraphicsBufferD*>(buf)->placeForGPU(ctx, gpuHeap, offset);
|
|
|
|
else
|
|
|
|
return static_cast<D3D12GraphicsBufferS*>(buf)->placeForGPU(ctx, gpuHeap, offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static UINT64 PlaceTextureForGPU(ITexture* tex, D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
switch (tex->type())
|
|
|
|
{
|
|
|
|
case TextureType::Dynamic:
|
2015-11-02 05:41:24 +00:00
|
|
|
return static_cast<D3D12TextureD*>(tex)->placeForGPU(ctx, gpuHeap, offset);
|
2015-11-27 22:14:05 +00:00
|
|
|
case TextureType::Static:
|
2015-11-02 05:41:24 +00:00
|
|
|
return static_cast<D3D12TextureS*>(tex)->placeForGPU(ctx, gpuHeap, offset);
|
2015-11-27 22:14:05 +00:00
|
|
|
case TextureType::StaticArray:
|
|
|
|
return static_cast<D3D12TextureSA*>(tex)->placeForGPU(ctx, gpuHeap, offset);
|
|
|
|
}
|
2015-11-05 00:00:29 +00:00
|
|
|
return offset;
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ID3D12Resource* GetBufferGPUResource(const IGraphicsBuffer* buf, int idx,
|
2015-11-05 04:57:48 +00:00
|
|
|
D3D12_VERTEX_BUFFER_VIEW& descOut)
|
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferD* cbuf = static_cast<const D3D12GraphicsBufferD*>(buf);
|
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.StrideInBytes = cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBufs[idx]->GetGPUVirtualAddress();
|
|
|
|
return cbuf->m_gpuBufs[idx].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferS* cbuf = static_cast<const D3D12GraphicsBufferS*>(buf);
|
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.StrideInBytes = cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBuf->GetGPUVirtualAddress();
|
|
|
|
return cbuf->m_gpuBuf.Get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ID3D12Resource* GetBufferGPUResource(const IGraphicsBuffer* buf, int idx,
|
|
|
|
D3D12_INDEX_BUFFER_VIEW& descOut)
|
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferD* cbuf = static_cast<const D3D12GraphicsBufferD*>(buf);
|
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBufs[idx]->GetGPUVirtualAddress();
|
|
|
|
descOut.Format = DXGI_FORMAT_R32_UINT;
|
|
|
|
return cbuf->m_gpuBufs[idx].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferS* cbuf = static_cast<const D3D12GraphicsBufferS*>(buf);
|
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBuf->GetGPUVirtualAddress();
|
|
|
|
descOut.Format = DXGI_FORMAT_R32_UINT;
|
|
|
|
return cbuf->m_gpuBuf.Get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ID3D12Resource* GetBufferGPUResource(const IGraphicsBuffer* buf, int idx,
|
|
|
|
D3D12_CONSTANT_BUFFER_VIEW_DESC& descOut)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
if (buf->dynamic())
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferD* cbuf = static_cast<const D3D12GraphicsBufferD*>(buf);
|
2015-11-05 04:57:48 +00:00
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBufs[idx]->GetGPUVirtualAddress();
|
2015-11-02 05:41:24 +00:00
|
|
|
return cbuf->m_gpuBufs[idx].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const D3D12GraphicsBufferS* cbuf = static_cast<const D3D12GraphicsBufferS*>(buf);
|
2015-11-05 04:57:48 +00:00
|
|
|
descOut.SizeInBytes = cbuf->m_count * cbuf->m_stride;
|
|
|
|
descOut.BufferLocation = cbuf->m_gpuBuf->GetGPUVirtualAddress();
|
2015-11-02 05:41:24 +00:00
|
|
|
return cbuf->m_gpuBuf.Get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
static const struct RGBATex2DViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
RGBATex2DViewDesc()
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
|
|
|
Texture2D = {UINT(0), UINT(-1), UINT(0), 0.0f};
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
} RGBATex2DViewDesc;
|
|
|
|
|
|
|
|
static const struct GreyTex2DViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
|
|
|
{
|
|
|
|
GreyTex2DViewDesc()
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
Format = DXGI_FORMAT_R8_UNORM;
|
|
|
|
ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
|
|
|
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
|
|
|
Texture2D = {UINT(0), UINT(-1), UINT(0), 0.0f};
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
} GreyTex2DViewDesc;
|
2015-11-02 05:41:24 +00:00
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
static const struct RGBATex2DArrayViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
RGBATex2DArrayViewDesc()
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2015-11-27 22:14:05 +00:00
|
|
|
ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
|
2015-11-02 05:41:24 +00:00
|
|
|
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
2015-11-27 22:14:05 +00:00
|
|
|
Texture2DArray = {UINT(0), UINT(-1), 0, 0, UINT(0), 0.0f};
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
} RGBATex2DArrayViewDesc;
|
|
|
|
|
|
|
|
static const struct GreyTex2DArrayViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
|
|
|
{
|
|
|
|
GreyTex2DArrayViewDesc()
|
|
|
|
{
|
|
|
|
Format = DXGI_FORMAT_R8_UNORM;
|
|
|
|
ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2DARRAY;
|
|
|
|
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
|
|
|
Texture2DArray = {UINT(0), UINT(-1), 0, 0, UINT(0), 0.0f};
|
|
|
|
}
|
|
|
|
} GreyTex2DArrayViewDesc;
|
|
|
|
|
|
|
|
static ID3D12Resource* GetTextureGPUResource(const ITexture* tex, int idx,
|
|
|
|
D3D12_SHADER_RESOURCE_VIEW_DESC& descOut)
|
|
|
|
{
|
|
|
|
switch (tex->type())
|
|
|
|
{
|
|
|
|
case TextureType::Dynamic:
|
|
|
|
{
|
|
|
|
const D3D12TextureD* ctex = static_cast<const D3D12TextureD*>(tex);
|
|
|
|
switch (ctex->format())
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
descOut = RGBATex2DViewDesc;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
descOut = GreyTex2DViewDesc;
|
|
|
|
break;
|
|
|
|
default:break;
|
|
|
|
}
|
|
|
|
return ctex->m_gpuTexs[idx].Get();
|
|
|
|
}
|
|
|
|
case TextureType::Static:
|
|
|
|
{
|
|
|
|
const D3D12TextureS* ctex = static_cast<const D3D12TextureS*>(tex);
|
|
|
|
switch (ctex->format())
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
descOut = RGBATex2DViewDesc;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
descOut = GreyTex2DViewDesc;
|
|
|
|
break;
|
|
|
|
default:break;
|
|
|
|
}
|
|
|
|
return ctex->m_gpuTex.Get();
|
|
|
|
}
|
|
|
|
case TextureType::StaticArray:
|
|
|
|
{
|
|
|
|
const D3D12TextureSA* ctex = static_cast<const D3D12TextureSA*>(tex);
|
|
|
|
switch (ctex->format())
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
descOut = RGBATex2DArrayViewDesc;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
descOut = GreyTex2DArrayViewDesc;
|
|
|
|
break;
|
|
|
|
default:break;
|
|
|
|
}
|
|
|
|
descOut.Texture2DArray.ArraySize = ctex->layers();
|
|
|
|
return ctex->m_gpuTex.Get();
|
|
|
|
}
|
|
|
|
case TextureType::Render:
|
|
|
|
{
|
|
|
|
const D3D12TextureR* ctex = static_cast<const D3D12TextureR*>(tex);
|
|
|
|
descOut = RGBATex2DViewDesc;
|
|
|
|
return ctex->m_gpuTex.Get();
|
|
|
|
}
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
struct D3D12ShaderDataBinding : IShaderDataBinding
|
|
|
|
{
|
|
|
|
D3D12ShaderPipeline* m_pipeline;
|
|
|
|
ComPtr<ID3D12Heap> m_gpuHeap;
|
2015-11-04 00:27:32 +00:00
|
|
|
ComPtr<ID3D12DescriptorHeap> m_descHeap[2];
|
2015-11-02 05:41:24 +00:00
|
|
|
IGraphicsBuffer* m_vbuf;
|
2015-11-27 22:14:05 +00:00
|
|
|
IGraphicsBuffer* m_instVbuf;
|
2015-11-02 05:41:24 +00:00
|
|
|
IGraphicsBuffer* m_ibuf;
|
|
|
|
size_t m_ubufCount;
|
|
|
|
std::unique_ptr<IGraphicsBuffer*[]> m_ubufs;
|
|
|
|
size_t m_texCount;
|
|
|
|
std::unique_ptr<ITexture*[]> m_texs;
|
2015-11-27 22:14:05 +00:00
|
|
|
D3D12_VERTEX_BUFFER_VIEW m_vboView[2][2] = {{},{}};
|
2015-11-05 00:00:29 +00:00
|
|
|
D3D12_INDEX_BUFFER_VIEW m_iboView[2];
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12ShaderDataBinding(D3D12Context* ctx,
|
|
|
|
IShaderPipeline* pipeline,
|
2015-11-27 22:14:05 +00:00
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbuf, IGraphicsBuffer* ibuf,
|
2015-11-02 05:41:24 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs,
|
|
|
|
size_t texCount, ITexture** texs)
|
|
|
|
: m_pipeline(static_cast<D3D12ShaderPipeline*>(pipeline)),
|
|
|
|
m_vbuf(vbuf),
|
2015-11-27 22:14:05 +00:00
|
|
|
m_instVbuf(instVbuf),
|
2015-11-02 05:41:24 +00:00
|
|
|
m_ibuf(ibuf),
|
|
|
|
m_ubufCount(ubufCount),
|
|
|
|
m_ubufs(new IGraphicsBuffer*[ubufCount]),
|
|
|
|
m_texCount(texCount),
|
|
|
|
m_texs(new ITexture*[texCount])
|
|
|
|
{
|
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
|
|
|
m_ubufs[i] = ubufs[i];
|
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
|
|
|
m_texs[i] = texs[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void commit(D3D12Context* ctx)
|
|
|
|
{
|
2015-11-04 00:27:32 +00:00
|
|
|
/* Create double-buffered descriptor heaps */
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_DESCRIPTOR_HEAP_DESC desc;
|
|
|
|
desc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
|
2015-11-05 04:57:48 +00:00
|
|
|
desc.NumDescriptors = MAX_UNIFORM_COUNT + MAX_TEXTURE_COUNT;
|
2015-11-02 05:41:24 +00:00
|
|
|
desc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE;
|
|
|
|
desc.NodeMask = 0;
|
|
|
|
|
|
|
|
UINT incSz = ctx->m_dev->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
|
2015-11-04 00:27:32 +00:00
|
|
|
for (int b=0 ; b<2 ; ++b)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDescriptorHeap(&desc, _uuidof(ID3D12DescriptorHeap), &m_descHeap[b]));
|
|
|
|
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_descHeap[b]->GetCPUDescriptorHandleForHeapStart());
|
2015-11-05 04:57:48 +00:00
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
if (m_vbuf)
|
|
|
|
GetBufferGPUResource(m_vbuf, b, m_vboView[b][0]);
|
|
|
|
if (m_instVbuf)
|
|
|
|
GetBufferGPUResource(m_instVbuf, b, m_vboView[b][1]);
|
2015-11-02 05:41:24 +00:00
|
|
|
if (m_ibuf)
|
2015-11-05 04:57:48 +00:00
|
|
|
GetBufferGPUResource(m_ibuf, b, m_iboView[b]);
|
|
|
|
for (size_t i=0 ; i<MAX_UNIFORM_COUNT ; ++i)
|
2015-11-05 00:00:29 +00:00
|
|
|
{
|
2015-11-05 04:57:48 +00:00
|
|
|
if (i<m_ubufCount)
|
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_CONSTANT_BUFFER_VIEW_DESC viewDesc;
|
2015-11-05 04:57:48 +00:00
|
|
|
GetBufferGPUResource(m_ubufs[i], b, viewDesc);
|
2015-11-18 06:14:49 +00:00
|
|
|
viewDesc.SizeInBytes = (viewDesc.SizeInBytes + 255) & ~255;
|
2015-11-05 04:57:48 +00:00
|
|
|
ctx->m_dev->CreateConstantBufferView(&viewDesc, handle);
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
handle.Offset(1, incSz);
|
|
|
|
}
|
2015-11-05 04:57:48 +00:00
|
|
|
for (size_t i=0 ; i<MAX_TEXTURE_COUNT ; ++i)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-05 04:57:48 +00:00
|
|
|
if (i<m_texCount)
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc;
|
|
|
|
ID3D12Resource* res = GetTextureGPUResource(m_texs[i], b, srvDesc);
|
|
|
|
ctx->m_dev->CreateShaderResourceView(res, &srvDesc, handle);
|
2015-11-05 04:57:48 +00:00
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
handle.Offset(1, incSz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 00:00:29 +00:00
|
|
|
|
|
|
|
void bind(ID3D12GraphicsCommandList* list, int b)
|
|
|
|
{
|
|
|
|
ID3D12DescriptorHeap* heap[] = {m_descHeap[b].Get()};
|
|
|
|
list->SetDescriptorHeaps(1, heap);
|
2015-11-05 04:57:48 +00:00
|
|
|
list->SetGraphicsRootDescriptorTable(0, m_descHeap[b]->GetGPUDescriptorHandleForHeapStart());
|
2015-11-05 00:00:29 +00:00
|
|
|
list->SetPipelineState(m_pipeline->m_state.Get());
|
2015-11-27 22:14:05 +00:00
|
|
|
list->IASetVertexBuffers(0, 2, m_vboView[b]);
|
2015-11-05 00:00:29 +00:00
|
|
|
if (m_ibuf)
|
|
|
|
list->IASetIndexBuffer(&m_iboView[b]);
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
};
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
static ID3D12GraphicsCommandList* WaitForLoadList(D3D12Context* ctx)
|
|
|
|
{
|
|
|
|
/* Wait for previous transaction to complete (if in progress) */
|
|
|
|
if (ctx->m_loadfence->GetCompletedValue() < ctx->m_loadfenceval)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_loadfence->SetEventOnCompletion(ctx->m_loadfenceval, ctx->m_loadfencehandle));
|
|
|
|
WaitForSingleObject(ctx->m_loadfencehandle, INFINITE);
|
|
|
|
|
|
|
|
/* Reset allocator and list */
|
|
|
|
ThrowIfFailed(ctx->m_loadqalloc->Reset());
|
|
|
|
ThrowIfFailed(ctx->m_loadlist->Reset(ctx->m_loadqalloc.Get(), nullptr));
|
|
|
|
}
|
|
|
|
return ctx->m_loadlist.Get();
|
|
|
|
}
|
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
struct D3D12CommandQueue : IGraphicsCommandQueue
|
|
|
|
{
|
2015-11-21 02:58:56 +00:00
|
|
|
Platform platform() const {return IGraphicsDataFactory::Platform::D3D12;}
|
2015-11-18 06:14:49 +00:00
|
|
|
const SystemChar* platformName() const {return _S("D3D12");}
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12Context* m_ctx;
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12Context::Window* m_windowCtx;
|
2015-11-02 05:41:24 +00:00
|
|
|
IGraphicsContext* m_parent;
|
|
|
|
ComPtr<ID3D12GraphicsCommandList> m_cmdList;
|
2015-11-05 00:00:29 +00:00
|
|
|
ComPtr<ID3D12Fence> m_fence;
|
2015-11-02 05:41:24 +00:00
|
|
|
|
|
|
|
size_t m_fillBuf = 0;
|
|
|
|
size_t m_drawBuf = 0;
|
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
void resetCommandList()
|
|
|
|
{
|
|
|
|
ThrowIfFailed(m_ctx->m_qalloc[m_fillBuf]->Reset());
|
|
|
|
ThrowIfFailed(m_cmdList->Reset(m_ctx->m_qalloc[m_fillBuf].Get(), nullptr));
|
|
|
|
m_cmdList->SetGraphicsRootSignature(m_ctx->m_rs.Get());
|
|
|
|
}
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
D3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent,
|
|
|
|
ID3D12CommandQueue** cmdQueueOut)
|
2015-11-04 00:27:32 +00:00
|
|
|
: m_ctx(ctx), m_windowCtx(windowCtx), m_parent(parent)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
|
|
|
__uuidof(ID3D12CommandAllocator),
|
2015-11-04 00:27:32 +00:00
|
|
|
&ctx->m_qalloc[0]));
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
|
|
|
__uuidof(ID3D12CommandAllocator),
|
|
|
|
&ctx->m_qalloc[1]));
|
2015-11-02 05:41:24 +00:00
|
|
|
D3D12_COMMAND_QUEUE_DESC desc =
|
|
|
|
{
|
|
|
|
D3D12_COMMAND_LIST_TYPE_DIRECT,
|
|
|
|
D3D12_COMMAND_QUEUE_PRIORITY_HIGH,
|
|
|
|
D3D12_COMMAND_QUEUE_FLAG_NONE
|
|
|
|
};
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &ctx->m_q));
|
2015-11-05 00:00:29 +00:00
|
|
|
*cmdQueueOut = ctx->m_q.Get();
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &m_fence));
|
2015-11-04 00:27:32 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, ctx->m_qalloc[0].Get(),
|
2015-11-02 05:41:24 +00:00
|
|
|
nullptr, __uuidof(ID3D12GraphicsCommandList), &m_cmdList));
|
|
|
|
m_cmdList->SetGraphicsRootSignature(m_ctx->m_rs.Get());
|
|
|
|
}
|
|
|
|
|
2015-11-03 04:19:41 +00:00
|
|
|
void setShaderDataBinding(IShaderDataBinding* binding)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
D3D12ShaderDataBinding* cbind = static_cast<D3D12ShaderDataBinding*>(binding);
|
2015-11-05 00:00:29 +00:00
|
|
|
cbind->bind(m_cmdList.Get(), m_fillBuf);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
D3D12TextureR* m_boundTarget = nullptr;
|
2015-11-04 00:27:32 +00:00
|
|
|
void setRenderTarget(ITextureR* target)
|
2015-11-03 04:19:41 +00:00
|
|
|
{
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12TextureR* ctarget = static_cast<D3D12TextureR*>(target);
|
2015-11-03 04:19:41 +00:00
|
|
|
|
|
|
|
if (m_boundTarget)
|
2015-11-06 03:20:58 +00:00
|
|
|
m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_boundTarget->getRenderColorRes(),
|
2015-11-03 04:19:41 +00:00
|
|
|
D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
|
|
|
|
|
|
|
m_cmdList->OMSetRenderTargets(1, &ctarget->m_rtvHeap->GetCPUDescriptorHandleForHeapStart(),
|
|
|
|
false, &ctarget->m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(ctarget->getRenderColorRes(),
|
2015-11-03 04:19:41 +00:00
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET));
|
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
m_boundTarget = ctarget;
|
2015-11-03 04:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setViewport(const SWindowRect& rect)
|
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12_VIEWPORT vp = {FLOAT(rect.location[0]), FLOAT(rect.location[1]),
|
|
|
|
FLOAT(rect.size[0]), FLOAT(rect.size[1]), 0.0f, 1.0f};
|
2015-11-04 00:27:32 +00:00
|
|
|
m_cmdList->RSSetViewports(1, &vp);
|
2015-11-05 00:00:29 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
void setScissor(const SWindowRect& rect)
|
|
|
|
{
|
|
|
|
D3D12_RECT d3drect = {rect.location[0], rect.location[1], rect.size[0], rect.size[1]};
|
|
|
|
m_cmdList->RSSetScissorRects(1, &d3drect);
|
|
|
|
}
|
|
|
|
|
|
|
|
int pendingDynamicSlot() {return m_fillBuf;}
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
void flushBufferUpdates() {}
|
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
std::unordered_map<D3D12TextureR*, std::pair<size_t, size_t>> m_texResizes;
|
2015-11-05 00:00:29 +00:00
|
|
|
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
|
|
|
|
{
|
|
|
|
D3D12TextureR* ctex = static_cast<D3D12TextureR*>(tex);
|
2015-11-07 01:43:12 +00:00
|
|
|
m_texResizes[ctex] = std::make_pair(width, height);
|
2015-11-03 04:19:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float m_clearColor[4] = {0.0,0.0,0.0,1.0};
|
2015-11-02 05:41:24 +00:00
|
|
|
void setClearColor(const float rgba[4])
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
m_clearColor[0] = rgba[0];
|
|
|
|
m_clearColor[1] = rgba[1];
|
|
|
|
m_clearColor[2] = rgba[2];
|
|
|
|
m_clearColor[3] = rgba[3];
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
void clearTarget(bool render=true, bool depth=true)
|
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
if (!m_boundTarget)
|
|
|
|
return;
|
2015-11-02 05:41:24 +00:00
|
|
|
if (render)
|
2015-11-03 04:19:41 +00:00
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_boundTarget->m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->ClearRenderTargetView(handle, m_clearColor, 0, nullptr);
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
if (depth)
|
2015-11-03 04:19:41 +00:00
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_boundTarget->m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, 1.0, 0, 0, nullptr);
|
|
|
|
}
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setDrawPrimitive(Primitive prim)
|
|
|
|
{
|
2015-11-21 02:58:56 +00:00
|
|
|
if (prim == Primitive::Triangles)
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
|
2015-11-21 02:58:56 +00:00
|
|
|
else if (prim == Primitive::TriStrips)
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
void draw(size_t start, size_t count)
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->DrawInstanced(count, 1, start, 0);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
void drawIndexed(size_t start, size_t count)
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->DrawIndexedInstanced(count, 1, start, 0, 0);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->DrawInstanced(count, instCount, start, 0);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-03 04:19:41 +00:00
|
|
|
|
2015-11-02 05:41:24 +00:00
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
m_cmdList->DrawIndexedInstanced(count, instCount, start, 0, 0);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
bool m_doPresent = false;
|
|
|
|
void resolveDisplay(ITextureR* source)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12TextureR* csource = static_cast<D3D12TextureR*>(source);
|
|
|
|
|
2015-11-05 04:57:48 +00:00
|
|
|
if (m_windowCtx->m_needsResize)
|
|
|
|
{
|
|
|
|
UINT nodeMasks[] = {0,0};
|
|
|
|
IUnknown* const queues[] = {m_ctx->m_q.Get(), m_ctx->m_q.Get()};
|
|
|
|
m_windowCtx->m_swapChain->ResizeBuffers1(2, m_windowCtx->width, m_windowCtx->height,
|
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH, nodeMasks, queues);
|
|
|
|
m_windowCtx->m_backBuf = m_windowCtx->m_swapChain->GetCurrentBackBufferIndex();
|
|
|
|
m_windowCtx->m_needsResize = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComPtr<ID3D12Resource> dest;
|
|
|
|
ThrowIfFailed(m_windowCtx->m_swapChain->GetBuffer(m_windowCtx->m_backBuf, __uuidof(ID3D12Resource), &dest));
|
2015-11-04 00:27:32 +00:00
|
|
|
|
|
|
|
if (csource->m_samples > 1)
|
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
ID3D12Resource* src = csource->m_gpuMsaaTex.Get();
|
2015-11-05 00:00:29 +00:00
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_BARRIER msaaSetup[] =
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
|
|
|
D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
|
2015-11-05 04:57:48 +00:00
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(dest.Get(),
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RESOLVE_DEST)
|
|
|
|
};
|
|
|
|
m_cmdList->ResourceBarrier(2, msaaSetup);
|
|
|
|
|
2015-11-05 04:57:48 +00:00
|
|
|
m_cmdList->ResolveSubresource(dest.Get(), 0, src, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
|
2015-11-04 00:27:32 +00:00
|
|
|
|
|
|
|
D3D12_RESOURCE_BARRIER msaaTeardown[] =
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
|
|
|
D3D12_RESOURCE_STATE_RESOLVE_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET),
|
2015-11-05 04:57:48 +00:00
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(dest.Get(),
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_RESOLVE_DEST, D3D12_RESOURCE_STATE_PRESENT)
|
|
|
|
};
|
|
|
|
m_cmdList->ResourceBarrier(2, msaaTeardown);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
ID3D12Resource* src = csource->m_gpuTex.Get();
|
2015-11-05 00:00:29 +00:00
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_BARRIER copySetup[] =
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
|
|
|
D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE),
|
2015-11-05 04:57:48 +00:00
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(dest.Get(),
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_COPY_DEST)
|
|
|
|
};
|
|
|
|
m_cmdList->ResourceBarrier(2, copySetup);
|
|
|
|
|
2015-11-05 04:57:48 +00:00
|
|
|
m_cmdList->CopyResource(dest.Get(), src);
|
2015-11-04 00:27:32 +00:00
|
|
|
|
|
|
|
D3D12_RESOURCE_BARRIER copyTeardown[] =
|
|
|
|
{
|
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET),
|
2015-11-05 04:57:48 +00:00
|
|
|
CD3DX12_RESOURCE_BARRIER::Transition(dest.Get(),
|
2015-11-04 00:27:32 +00:00
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PRESENT)
|
|
|
|
};
|
|
|
|
m_cmdList->ResourceBarrier(2, copyTeardown);
|
|
|
|
}
|
|
|
|
m_doPresent = true;
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
UINT64 m_submittedFenceVal = 0;
|
2015-11-02 05:41:24 +00:00
|
|
|
void execute()
|
|
|
|
{
|
2015-11-04 00:27:32 +00:00
|
|
|
/* Check on fence */
|
2015-11-05 00:00:29 +00:00
|
|
|
if (m_fence->GetCompletedValue() < m_submittedFenceVal)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
/* Abandon this list (renderer too slow) */
|
2015-11-06 03:20:58 +00:00
|
|
|
m_cmdList->Close();
|
2015-11-04 00:27:32 +00:00
|
|
|
resetCommandList();
|
2015-11-05 00:00:29 +00:00
|
|
|
m_doPresent = false;
|
2015-11-04 00:27:32 +00:00
|
|
|
return;
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-07 01:43:12 +00:00
|
|
|
|
|
|
|
/* Perform texture resizes */
|
|
|
|
if (m_texResizes.size())
|
|
|
|
{
|
|
|
|
for (const auto& resize : m_texResizes)
|
|
|
|
resize.first->resize(m_ctx, resize.second.first, resize.second.second);
|
|
|
|
m_texResizes.clear();
|
|
|
|
m_cmdList->Close();
|
|
|
|
resetCommandList();
|
|
|
|
m_doPresent = false;
|
|
|
|
return;
|
|
|
|
}
|
2015-11-04 00:27:32 +00:00
|
|
|
|
|
|
|
m_drawBuf = m_fillBuf;
|
|
|
|
++m_fillBuf;
|
|
|
|
if (m_fillBuf == 2)
|
|
|
|
m_fillBuf = 0;
|
|
|
|
|
|
|
|
m_cmdList->Close();
|
|
|
|
ID3D12CommandList* cl[] = {m_cmdList.Get()};
|
|
|
|
m_ctx->m_q->ExecuteCommandLists(1, cl);
|
|
|
|
|
|
|
|
if (m_doPresent)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(m_windowCtx->m_swapChain->Present(1, 0));
|
|
|
|
m_windowCtx->m_backBuf = m_windowCtx->m_swapChain->GetCurrentBackBufferIndex();
|
2015-11-05 00:00:29 +00:00
|
|
|
m_doPresent = false;
|
2015-11-04 00:27:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
++m_submittedFenceVal;
|
2015-11-05 00:00:29 +00:00
|
|
|
ThrowIfFailed(m_ctx->m_q->Signal(m_fence.Get(), m_submittedFenceVal));
|
2015-11-04 00:27:32 +00:00
|
|
|
|
|
|
|
resetCommandList();
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void D3D12GraphicsBufferD::load(const void* data, size_t sz)
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
ID3D12Resource* res = m_bufs[m_q->m_fillBuf].Get();
|
2015-11-18 06:14:49 +00:00
|
|
|
ID3D12Resource* gpuRes = m_gpuBufs[m_q->m_fillBuf].Get();
|
|
|
|
D3D12_SUBRESOURCE_DATA d = {data, LONG_PTR(sz), LONG_PTR(sz)};
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
m_state, D3D12_RESOURCE_STATE_COPY_DEST));
|
|
|
|
if (!UpdateSubresources<1>(m_q->m_cmdList.Get(), gpuRes, res, 0, 0, 1, &d))
|
|
|
|
Log.report(LogVisor::FatalError, "unable to update dynamic buffer data");
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, m_state));
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
void* D3D12GraphicsBufferD::map(size_t sz)
|
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
if (m_mappedBuf)
|
|
|
|
free(m_mappedBuf);
|
|
|
|
m_mappedSz = sz;
|
|
|
|
m_mappedBuf = malloc(sz);
|
|
|
|
return m_mappedBuf;
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
void D3D12GraphicsBufferD::unmap()
|
|
|
|
{
|
2015-11-03 04:19:41 +00:00
|
|
|
ID3D12Resource* res = m_bufs[m_q->m_fillBuf].Get();
|
2015-11-18 06:14:49 +00:00
|
|
|
ID3D12Resource* gpuRes = m_gpuBufs[m_q->m_fillBuf].Get();
|
|
|
|
D3D12_SUBRESOURCE_DATA data = {m_mappedBuf, LONG_PTR(m_mappedSz), LONG_PTR(m_mappedSz)};
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
m_state, D3D12_RESOURCE_STATE_COPY_DEST));
|
|
|
|
if (!UpdateSubresources<1>(m_q->m_cmdList.Get(), gpuRes, res, 0, 0, 1, &data))
|
|
|
|
Log.report(LogVisor::FatalError, "unable to update dynamic buffer data");
|
|
|
|
free(m_mappedBuf);
|
|
|
|
m_mappedBuf = nullptr;
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, m_state));
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 00:27:32 +00:00
|
|
|
void D3D12TextureD::load(const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
ID3D12Resource* res = m_texs[m_q->m_fillBuf].Get();
|
2015-11-18 06:14:49 +00:00
|
|
|
ID3D12Resource* gpuRes = m_gpuTexs[m_q->m_fillBuf].Get();
|
|
|
|
D3D12_SUBRESOURCE_DATA d = {data, LONG_PTR(m_width * 4), LONG_PTR(sz)};
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_DEST));
|
|
|
|
if (!UpdateSubresources<1>(m_q->m_cmdList.Get(), gpuRes, res, 0, 0, 1, &d))
|
|
|
|
Log.report(LogVisor::FatalError, "unable to update dynamic texture data");
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
2015-11-04 00:27:32 +00:00
|
|
|
}
|
|
|
|
void* D3D12TextureD::map(size_t sz)
|
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
if (m_mappedBuf)
|
|
|
|
free(m_mappedBuf);
|
|
|
|
m_mappedSz = sz;
|
|
|
|
m_mappedBuf = malloc(sz);
|
|
|
|
return m_mappedBuf;
|
2015-11-04 00:27:32 +00:00
|
|
|
}
|
|
|
|
void D3D12TextureD::unmap()
|
|
|
|
{
|
|
|
|
ID3D12Resource* res = m_texs[m_q->m_fillBuf].Get();
|
2015-11-18 06:14:49 +00:00
|
|
|
ID3D12Resource* gpuRes = m_gpuTexs[m_q->m_fillBuf].Get();
|
|
|
|
D3D12_SUBRESOURCE_DATA data = {m_mappedBuf, LONG_PTR(m_width * 4), LONG_PTR(m_mappedSz)};
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_DEST));
|
|
|
|
if (!UpdateSubresources<1>(m_q->m_cmdList.Get(), gpuRes, res, 0, 0, 1, &data))
|
|
|
|
Log.report(LogVisor::FatalError, "unable to update dynamic buffer data");
|
|
|
|
free(m_mappedBuf);
|
|
|
|
m_mappedBuf = nullptr;
|
|
|
|
m_q->m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(gpuRes,
|
|
|
|
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
2015-11-04 00:27:32 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
class D3D12DataFactory : public ID3DDataFactory
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsContext* m_parent;
|
|
|
|
IGraphicsData* m_deferredData = nullptr;
|
|
|
|
struct D3D12Context* m_ctx;
|
|
|
|
std::unordered_set<IGraphicsData*> m_committedData;
|
|
|
|
public:
|
|
|
|
D3D12DataFactory(IGraphicsContext* parent, D3D12Context* ctx)
|
|
|
|
: m_parent(parent), m_deferredData(new struct D3D12Data()), m_ctx(ctx)
|
|
|
|
{
|
|
|
|
CD3DX12_DESCRIPTOR_RANGE ranges[] =
|
|
|
|
{
|
|
|
|
{D3D12_DESCRIPTOR_RANGE_TYPE_CBV, MAX_UNIFORM_COUNT, 0},
|
|
|
|
{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, MAX_TEXTURE_COUNT, 0}
|
|
|
|
};
|
|
|
|
CD3DX12_ROOT_PARAMETER rootParms[1];
|
|
|
|
rootParms[0].InitAsDescriptorTable(2, ranges);
|
|
|
|
|
|
|
|
ComPtr<ID3DBlob> rsOutBlob;
|
|
|
|
ComPtr<ID3DBlob> rsErrorBlob;
|
|
|
|
ThrowIfFailed(D3D12SerializeRootSignaturePROC(
|
|
|
|
&CD3DX12_ROOT_SIGNATURE_DESC(1, rootParms, 1, &CD3DX12_STATIC_SAMPLER_DESC(0),
|
|
|
|
D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT),
|
|
|
|
D3D_ROOT_SIGNATURE_VERSION_1, &rsOutBlob, &rsErrorBlob));
|
|
|
|
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateRootSignature(0, rsOutBlob->GetBufferPointer(),
|
|
|
|
rsOutBlob->GetBufferSize(), __uuidof(ID3D12RootSignature), &ctx->m_rs));
|
|
|
|
}
|
|
|
|
~D3D12DataFactory() = default;
|
2015-11-02 05:41:24 +00:00
|
|
|
|
2015-11-21 02:58:56 +00:00
|
|
|
Platform platform() const {return Platform::D3D12;}
|
2015-11-18 06:14:49 +00:00
|
|
|
const SystemChar* platformName() const {return _S("D3D12");}
|
2015-11-04 00:27:32 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsBufferS* newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
|
|
|
|
{
|
|
|
|
D3D12GraphicsBufferS* retval = new D3D12GraphicsBufferS(use, m_ctx, data, stride, count);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_SBufs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
IGraphicsBufferS* newStaticBuffer(BufferUse use, std::unique_ptr<uint8_t[]>&& data, size_t stride, size_t count)
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]> d = std::move(data);
|
|
|
|
D3D12GraphicsBufferS* retval = new D3D12GraphicsBufferS(use, m_ctx, d.get(), stride, count);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_SBufs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsBufferD* newDynamicBuffer(BufferUse use, size_t stride, size_t count)
|
|
|
|
{
|
|
|
|
D3D12CommandQueue* q = static_cast<D3D12CommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
D3D12GraphicsBufferD* retval = new D3D12GraphicsBufferD(q, use, m_ctx, stride, count);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_DBufs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
ITextureS* newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
D3D12TextureS* retval = new D3D12TextureS(m_ctx, width, height, mips, fmt, data, sz);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_STexs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
ITextureS* newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
std::unique_ptr<uint8_t[]>&& data, size_t sz)
|
|
|
|
{
|
|
|
|
std::unique_ptr<uint8_t[]> d = std::move(data);
|
|
|
|
D3D12TextureS* retval = new D3D12TextureS(m_ctx, width, height, mips, fmt, d.get(), sz);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_STexs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
ITextureSA* newStaticArrayTexture(size_t width, size_t height, size_t layers, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
|
|
|
{
|
|
|
|
D3D12TextureSA* retval = new D3D12TextureSA(m_ctx, width, height, layers, fmt, data, sz);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_SATexs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
ITextureD* newDynamicTexture(size_t width, size_t height, TextureFormat fmt)
|
|
|
|
{
|
|
|
|
D3D12CommandQueue* q = static_cast<D3D12CommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
D3D12TextureD* retval = new D3D12TextureD(q, m_ctx, width, height, fmt);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_DTexs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
ITextureR* newRenderTexture(size_t width, size_t height, size_t samples)
|
|
|
|
{
|
|
|
|
D3D12CommandQueue* q = static_cast<D3D12CommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
D3D12TextureR* retval = new D3D12TextureR(m_ctx, width, height, samples);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_RTexs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
IVertexFormat* newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
|
|
|
{
|
|
|
|
D3D12VertexFormat* retval = new struct D3D12VertexFormat(elementCount, elements);
|
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_VFmts.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if _DEBUG
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_DEBUG | D3DCOMPILE_OPTIMIZATION_LEVEL0
|
|
|
|
#else
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_OPTIMIZATION_LEVEL3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
IShaderPipeline* newShaderPipeline
|
|
|
|
(const char* vertSource, const char* fragSource,
|
2015-11-18 06:14:49 +00:00
|
|
|
ComPtr<ID3DBlob>& vertBlobOut, ComPtr<ID3DBlob>& fragBlobOut,
|
|
|
|
ComPtr<ID3DBlob>& pipelineBlob, IVertexFormat* vtxFmt,
|
|
|
|
BlendFactor srcFac, BlendFactor dstFac,
|
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> errBlob;
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
if (!vertBlobOut)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
if (FAILED(D3DCompilePROC(vertSource, strlen(vertSource), "HECL Vert Source", nullptr, nullptr, "main",
|
|
|
|
"vs_5_0", BOO_D3DCOMPILE_FLAG, 0, &vertBlobOut, &errBlob)))
|
|
|
|
{
|
|
|
|
Log.report(LogVisor::FatalError, "error compiling vert shader: %s", errBlob->GetBufferPointer());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
if (!fragBlobOut)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2015-11-18 06:14:49 +00:00
|
|
|
if (FAILED(D3DCompilePROC(fragSource, strlen(fragSource), "HECL Pixel Source", nullptr, nullptr, "main",
|
|
|
|
"ps_5_0", BOO_D3DCOMPILE_FLAG, 0, &fragBlobOut, &errBlob)))
|
|
|
|
{
|
|
|
|
Log.report(LogVisor::FatalError, "error compiling pixel shader: %s", errBlob->GetBufferPointer());
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D12ShaderPipeline* retval = new D3D12ShaderPipeline(m_ctx, vertBlobOut.Get(), fragBlobOut.Get(), pipelineBlob.Get(),
|
|
|
|
static_cast<const D3D12VertexFormat*>(vtxFmt),
|
|
|
|
srcFac, dstFac, depthTest, depthWrite, backfaceCulling);
|
|
|
|
if (!pipelineBlob)
|
|
|
|
retval->m_state->GetCachedBlob(&pipelineBlob);
|
2015-11-06 03:20:58 +00:00
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_SPs.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
IShaderDataBinding* newShaderDataBinding(IShaderPipeline* pipeline,
|
|
|
|
IVertexFormat* vtxFormat,
|
2015-11-27 22:14:05 +00:00
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbuf, IGraphicsBuffer* ibuf,
|
2015-11-06 03:20:58 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs,
|
|
|
|
size_t texCount, ITexture** texs)
|
|
|
|
{
|
|
|
|
D3D12ShaderDataBinding* retval =
|
2015-11-27 22:14:05 +00:00
|
|
|
new D3D12ShaderDataBinding(m_ctx, pipeline, vbuf, instVbuf, ibuf, ubufCount, ubufs, texCount, texs);
|
2015-11-06 03:20:58 +00:00
|
|
|
static_cast<D3D12Data*>(m_deferredData)->m_SBinds.emplace_back(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
delete static_cast<D3D12Data*>(m_deferredData);
|
|
|
|
m_deferredData = new struct D3D12Data();
|
|
|
|
}
|
|
|
|
|
|
|
|
IGraphicsData* commit()
|
|
|
|
{
|
|
|
|
D3D12Data* retval = static_cast<D3D12Data*>(m_deferredData);
|
|
|
|
|
|
|
|
/* Gather resource descriptions */
|
2015-11-27 22:14:05 +00:00
|
|
|
std::vector<D3D12_RESOURCE_DESC> bufDescs;
|
|
|
|
bufDescs.reserve(retval->m_SBufs.size() + retval->m_DBufs.size() * 2);
|
|
|
|
|
|
|
|
std::vector<D3D12_RESOURCE_DESC> texDescs;
|
|
|
|
texDescs.reserve(retval->m_STexs.size() + retval->m_SATexs.size() + retval->m_DTexs.size() * 2);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12GraphicsBufferS>& buf : retval->m_SBufs)
|
2015-11-27 22:14:05 +00:00
|
|
|
bufDescs.push_back(buf->m_buf->GetDesc());
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12GraphicsBufferD>& buf : retval->m_DBufs)
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
bufDescs.push_back(buf->m_bufs[0]->GetDesc());
|
|
|
|
bufDescs.push_back(buf->m_bufs[1]->GetDesc());
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12TextureS>& tex : retval->m_STexs)
|
2015-11-27 22:14:05 +00:00
|
|
|
texDescs.push_back(tex->m_tex->GetDesc());
|
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12TextureSA>& tex : retval->m_SATexs)
|
|
|
|
texDescs.push_back(tex->m_tex->GetDesc());
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12TextureD>& tex : retval->m_DTexs)
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
texDescs.push_back(tex->m_texs[0]->GetDesc());
|
|
|
|
texDescs.push_back(tex->m_texs[1]->GetDesc());
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create heap */
|
2015-11-27 22:14:05 +00:00
|
|
|
if (bufDescs.size())
|
|
|
|
{
|
|
|
|
D3D12_RESOURCE_ALLOCATION_INFO bufAllocInfo =
|
|
|
|
m_ctx->m_dev->GetResourceAllocationInfo(0, bufDescs.size(), bufDescs.data());
|
|
|
|
ThrowIfFailed(m_ctx->m_dev->CreateHeap(&CD3DX12_HEAP_DESC(bufAllocInfo,
|
|
|
|
D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_FLAG_ALLOW_ONLY_BUFFERS),
|
|
|
|
__uuidof(ID3D12Heap), &retval->m_bufHeap));
|
|
|
|
}
|
|
|
|
if (texDescs.size())
|
|
|
|
{
|
|
|
|
D3D12_RESOURCE_ALLOCATION_INFO texAllocInfo =
|
|
|
|
m_ctx->m_dev->GetResourceAllocationInfo(0, texDescs.size(), texDescs.data());
|
|
|
|
ThrowIfFailed(m_ctx->m_dev->CreateHeap(&CD3DX12_HEAP_DESC(texAllocInfo,
|
|
|
|
D3D12_HEAP_TYPE_DEFAULT, D3D12_HEAP_FLAG_ALLOW_ONLY_NON_RT_DS_TEXTURES),
|
|
|
|
__uuidof(ID3D12Heap), &retval->m_texHeap));
|
|
|
|
}
|
2015-11-07 20:03:21 +00:00
|
|
|
ID3D12Heap* bufHeap = retval->m_bufHeap.Get();
|
|
|
|
ID3D12Heap* texHeap = retval->m_texHeap.Get();
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
/* Place resources */
|
2015-11-07 20:03:21 +00:00
|
|
|
UINT64 offsetBuf = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
for (std::unique_ptr<D3D12GraphicsBufferS>& buf : retval->m_SBufs)
|
2015-11-07 20:03:21 +00:00
|
|
|
offsetBuf = PlaceBufferForGPU(buf.get(), m_ctx, bufHeap, offsetBuf);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12GraphicsBufferD>& buf : retval->m_DBufs)
|
2015-11-07 20:03:21 +00:00
|
|
|
offsetBuf = PlaceBufferForGPU(buf.get(), m_ctx, bufHeap, offsetBuf);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-11-07 20:03:21 +00:00
|
|
|
UINT64 offsetTex = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
for (std::unique_ptr<D3D12TextureS>& tex : retval->m_STexs)
|
2015-11-07 20:03:21 +00:00
|
|
|
offsetTex = PlaceTextureForGPU(tex.get(), m_ctx, texHeap, offsetTex);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
for (std::unique_ptr<D3D12TextureSA>& tex : retval->m_SATexs)
|
|
|
|
offsetTex = PlaceTextureForGPU(tex.get(), m_ctx, texHeap, offsetTex);
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
for (std::unique_ptr<D3D12TextureD>& tex : retval->m_DTexs)
|
2015-11-07 20:03:21 +00:00
|
|
|
offsetTex = PlaceTextureForGPU(tex.get(), m_ctx, texHeap, offsetTex);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
/* Execute static uploads */
|
|
|
|
ThrowIfFailed(m_ctx->m_loadlist->Close());
|
|
|
|
ID3D12CommandList* list[] = {m_ctx->m_loadlist.Get()};
|
|
|
|
m_ctx->m_loadq->ExecuteCommandLists(1, list);
|
|
|
|
++m_ctx->m_loadfenceval;
|
|
|
|
ThrowIfFailed(m_ctx->m_loadq->Signal(m_ctx->m_loadfence.Get(), m_ctx->m_loadfenceval));
|
|
|
|
|
|
|
|
/* Commit data bindings (create descriptor heaps) */
|
|
|
|
for (std::unique_ptr<D3D12ShaderDataBinding>& bind : retval->m_SBinds)
|
|
|
|
bind->commit(m_ctx);
|
|
|
|
|
2015-11-18 06:14:49 +00:00
|
|
|
/* Block handle return until data is ready on GPU */
|
|
|
|
WaitForLoadList(m_ctx);
|
|
|
|
|
|
|
|
/* Delete static upload heaps */
|
|
|
|
for (std::unique_ptr<D3D12GraphicsBufferS>& buf : retval->m_SBufs)
|
|
|
|
buf->m_buf.Reset();
|
|
|
|
|
|
|
|
for (std::unique_ptr<D3D12TextureS>& tex : retval->m_STexs)
|
|
|
|
tex->m_tex.Reset();
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
for (std::unique_ptr<D3D12TextureSA>& tex : retval->m_SATexs)
|
|
|
|
tex->m_tex.Reset();
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
/* All set! */
|
|
|
|
m_deferredData = new struct D3D12Data();
|
|
|
|
m_committedData.insert(retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyData(IGraphicsData* d)
|
|
|
|
{
|
|
|
|
D3D12Data* data = static_cast<D3D12Data*>(d);
|
|
|
|
m_committedData.erase(data);
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyAllData()
|
|
|
|
{
|
|
|
|
for (IGraphicsData* data : m_committedData)
|
|
|
|
delete static_cast<D3D12Data*>(data);
|
|
|
|
m_committedData.clear();
|
|
|
|
}
|
|
|
|
};
|
2015-11-02 05:41:24 +00:00
|
|
|
|
2015-11-05 00:00:29 +00:00
|
|
|
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent,
|
|
|
|
ID3D12CommandQueue** cmdQueueOut)
|
2015-11-02 05:41:24 +00:00
|
|
|
{
|
2015-11-05 00:00:29 +00:00
|
|
|
return new struct D3D12CommandQueue(ctx, windowCtx, parent, cmdQueueOut);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsDataFactory* _NewD3D12DataFactory(D3D12Context* ctx, IGraphicsContext* parent)
|
|
|
|
{
|
|
|
|
return new D3D12DataFactory(parent, ctx);
|
2015-11-02 05:41:24 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _WIN32_WINNT_WIN10
|