mirror of https://github.com/AxioDL/boo.git
Merge branch 'master' of https://github.com/AxioDL/libBoo
This commit is contained in:
commit
1c3c09d05a
|
@ -34,9 +34,9 @@ if(WIN32)
|
|||
include/boo/graphicsdev/D3D11.hpp
|
||||
include/boo/graphicsdev/D3D12.hpp)
|
||||
|
||||
list(APPEND _BOO_SYS_DEFINES -DUNICODE=1)
|
||||
list(APPEND _BOO_SYS_DEFINES -DUNICODE -D_UNICODE)
|
||||
|
||||
list(APPEND _BOO_SYS_LIBS Winusb)
|
||||
list(APPEND _BOO_SYS_LIBS Winusb opengl32 Setupapi D3DCompiler)
|
||||
elseif(APPLE)
|
||||
list(APPEND PLAT_SRCS
|
||||
lib/mac/ApplicationCocoa.mm
|
||||
|
|
2
LogVisor
2
LogVisor
|
@ -1 +1 @@
|
|||
Subproject commit 189e047977b138b711259ad84d94471f5d006ffb
|
||||
Subproject commit 4c2442df2d800fc25339d3d301d7d3691da7bafb
|
|
@ -5,6 +5,8 @@
|
|||
#include <sdkddkver.h>
|
||||
#if _WIN32_WINNT_WIN7
|
||||
|
||||
#if 0
|
||||
|
||||
#include "IGraphicsDataFactory.hpp"
|
||||
|
||||
namespace boo
|
||||
|
@ -50,7 +52,7 @@ public:
|
|||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif // _WIN32_WINNT_WIN7
|
||||
#endif // _WIN32
|
||||
#endif // GDEV_D3D11_HPP
|
||||
|
|
|
@ -115,6 +115,9 @@ struct VertexElementDescriptor
|
|||
const IGraphicsBuffer* indexBuffer = nullptr;
|
||||
VertexSemantic semantic;
|
||||
int semanticIdx = 0;
|
||||
VertexElementDescriptor() = default;
|
||||
VertexElementDescriptor(const IGraphicsBuffer* v, const IGraphicsBuffer* i, VertexSemantic s, int idx=0)
|
||||
: vertBuffer(v), indexBuffer(i), semantic(s), semanticIdx(idx) {}
|
||||
};
|
||||
|
||||
/** Opaque token for referencing a complete graphics pipeline state necessary
|
||||
|
|
|
@ -81,6 +81,8 @@
|
|||
#define __glew_h__
|
||||
#define __GLEW_H__
|
||||
|
||||
#define GLEW_STATIC 1
|
||||
|
||||
#if defined(__gl_h_) || defined(__GL_H__) || defined(_GL_H) || defined(__X_GL_H)
|
||||
#error gl.h included before glew.h
|
||||
#endif
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
#include "boo/graphicsdev/D3D12.hpp"
|
||||
#include "../win/Win32Common.hpp"
|
||||
#include "boo/graphicsdev/D3D12.hpp"
|
||||
#include "boo/IGraphicsContext.hpp"
|
||||
#include <vector>
|
||||
#include <thread>
|
||||
|
@ -10,17 +10,22 @@
|
|||
|
||||
#include "d3dx12.h"
|
||||
#include <d3dcompiler.h>
|
||||
#include <comdef.h>
|
||||
|
||||
extern PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignaturePROC;
|
||||
|
||||
namespace boo
|
||||
{
|
||||
static LogVisor::LogModule Log("boo::GL");
|
||||
static LogVisor::LogModule Log("boo::D3D12");
|
||||
|
||||
static inline void ThrowIfFailed(HRESULT hr)
|
||||
{
|
||||
if (FAILED(hr))
|
||||
{
|
||||
// Set a breakpoint on this line to catch Win32 API errors.
|
||||
Log.report(LogVisor::FatalError, "General D3D12 err");
|
||||
_com_error err(hr);
|
||||
LPCTSTR errMsg = err.ErrorMessage();
|
||||
Log.report(LogVisor::FatalError, errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -56,18 +61,15 @@ class D3D12GraphicsBufferS : public IGraphicsBufferS
|
|||
friend class D3D12DataFactory;
|
||||
friend struct D3D12CommandQueue;
|
||||
D3D12_RESOURCE_STATES m_state;
|
||||
const void* m_data;
|
||||
size_t m_sz;
|
||||
D3D12GraphicsBufferS(BufferUse use, D3D12Context* ctx, const void* data, size_t stride, size_t count)
|
||||
: m_state(USE_TABLE[use]), m_stride(stride), m_count(count)
|
||||
: m_state(USE_TABLE[use]), m_stride(stride), m_count(count), m_data(data), m_sz(stride * count)
|
||||
{
|
||||
size_t sz = stride * count;
|
||||
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
||||
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
||||
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(sz),
|
||||
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(m_sz),
|
||||
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_buf));
|
||||
void* m_d3dBuf;
|
||||
m_buf->Map(0, nullptr, &m_d3dBuf);
|
||||
memcpy(m_d3dBuf, data, sz);
|
||||
m_buf->Unmap(0, nullptr);
|
||||
}
|
||||
public:
|
||||
size_t m_stride;
|
||||
|
@ -81,6 +83,14 @@ public:
|
|||
D3D12_RESOURCE_DESC desc = m_buf->GetDesc();
|
||||
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &desc, m_state,
|
||||
nullptr, __uuidof(ID3D12Resource), &m_gpuBuf));
|
||||
|
||||
/* Stage resource upload */
|
||||
D3D12_SUBRESOURCE_DATA upData = {m_data, m_sz, m_sz};
|
||||
if (!UpdateSubresources<16>(ctx->m_loadlist.Get(), m_gpuBuf.Get(), m_buf.Get(), 0, 0, 1, &upData))
|
||||
Log.report(LogVisor::FatalError, "error preparing resource for upload");
|
||||
ctx->m_loadlist->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_gpuBuf.Get(),
|
||||
D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER));
|
||||
|
||||
return NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &desc));
|
||||
}
|
||||
};
|
||||
|
@ -138,28 +148,18 @@ D3D12DataFactory::newStaticBuffer(BufferUse use, const void* data, size_t stride
|
|||
class D3D12TextureS : public ITextureS
|
||||
{
|
||||
friend class D3D12DataFactory;
|
||||
const void* m_data;
|
||||
size_t m_sz;
|
||||
D3D12_RESOURCE_DESC m_desc;
|
||||
D3D12TextureS(D3D12Context* ctx, size_t width, size_t height, size_t mips,
|
||||
TextureFormat fmt, const void* data, size_t sz)
|
||||
: m_data(data), m_sz(sz)
|
||||
{
|
||||
m_desc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, width, height, 1, mips);
|
||||
ThrowIfFailed(ctx->m_dev->CreateCommittedResource(
|
||||
&CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD),
|
||||
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, width, height, 1, mips),
|
||||
D3D12_HEAP_FLAG_NONE, &CD3DX12_RESOURCE_DESC::Buffer(sz),
|
||||
D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, __uuidof(ID3D12Resource), &m_tex));
|
||||
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
||||
if (fmt == TextureFormatRGBA8)
|
||||
{
|
||||
for (size_t i=0 ; i<mips ; ++i)
|
||||
{
|
||||
void* data;
|
||||
m_tex->Map(i, nullptr, &data);
|
||||
size_t thisSz = width * height * 4;
|
||||
memcpy(data, dataIt, thisSz);
|
||||
m_tex->Unmap(i, nullptr);
|
||||
dataIt += thisSz;
|
||||
width /= 2;
|
||||
height /= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
public:
|
||||
ComPtr<ID3D12Resource> m_tex;
|
||||
|
@ -168,11 +168,32 @@ public:
|
|||
|
||||
UINT64 placeForGPU(D3D12Context* ctx, ID3D12Heap* gpuHeap, UINT64 offset)
|
||||
{
|
||||
D3D12_RESOURCE_DESC desc = m_tex->GetDesc();
|
||||
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &desc,
|
||||
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE,
|
||||
ThrowIfFailed(ctx->m_dev->CreatePlacedResource(gpuHeap, offset, &m_desc,
|
||||
D3D12_RESOURCE_STATE_COPY_DEST,
|
||||
nullptr, __uuidof(ID3D12Resource), &m_gpuTex));
|
||||
return NextHeapOffset(offset, ctx->m_dev->GetResourceAllocationInfo(0, 1, &desc));
|
||||
|
||||
int width = m_desc.Width;
|
||||
int height = m_desc.Height;
|
||||
|
||||
/* Stage resource upload */
|
||||
const uint8_t* dataIt = static_cast<const uint8_t*>(m_data);
|
||||
D3D12_SUBRESOURCE_DATA upData[16] = {};
|
||||
for (size_t i=0 ; i<m_desc.MipLevels && i<16 ; ++i)
|
||||
{
|
||||
upData[i].pData = dataIt;
|
||||
upData[i].RowPitch = width * 4;
|
||||
upData[i].SlicePitch = upData[i].RowPitch * height;
|
||||
dataIt += upData[i].SlicePitch;
|
||||
width /= 2;
|
||||
height /= 2;
|
||||
}
|
||||
|
||||
if (!UpdateSubresources<16>(ctx->m_loadlist.Get(), m_gpuTex.Get(), m_tex.Get(), 0, 0, m_desc.MipLevels, upData))
|
||||
Log.report(LogVisor::FatalError, "error preparing resource for upload");
|
||||
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_desc));
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -226,38 +247,77 @@ class D3D12TextureR : public ITextureR
|
|||
size_t m_width = 0;
|
||||
size_t m_height = 0;
|
||||
size_t m_samples = 0;
|
||||
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;
|
||||
|
||||
CD3DX12_RESOURCE_DESC rtvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_R8G8B8A8_UNORM, m_width, m_height, 1, 0, m_samples,
|
||||
0, D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
|
||||
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);
|
||||
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),
|
||||
__uuidof(ID3D12Resource), &m_gpuTexs[0]));
|
||||
|
||||
CD3DX12_RESOURCE_DESC dsvresdesc = CD3DX12_RESOURCE_DESC::Tex2D(DXGI_FORMAT_D24_UNORM_S8_UINT, m_width, m_height, 1, 0, m_samples,
|
||||
0, D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_DEFAULT_MSAA_RESOURCE_PLACEMENT_ALIGNMENT);
|
||||
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_gpuTexs[1]));
|
||||
|
||||
D3D12_DESCRIPTOR_HEAP_DESC rtvdesc = {D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1};
|
||||
ThrowIfFailed(ctx->m_dev->CreateDescriptorHeap(&rtvdesc, __uuidof(ID3D12DescriptorHeap), &m_rtvHeap));
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtvvdesc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RTV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateRenderTargetView(m_gpuTexs[0].Get(), &rtvvdesc, m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
D3D12_DESCRIPTOR_HEAP_DESC dsvdesc = {D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1};
|
||||
ThrowIfFailed(ctx->m_dev->CreateDescriptorHeap(&dsvdesc, __uuidof(ID3D12DescriptorHeap), &m_dsvHeap));
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC dsvvdesc = {DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_DSV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateDepthStencilView(m_gpuTexs[1].Get(), &dsvvdesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
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),
|
||||
__uuidof(ID3D12Resource), &m_gpuMsaaTexs[0]));
|
||||
|
||||
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),
|
||||
__uuidof(ID3D12Resource), &m_gpuMsaaTexs[1]));
|
||||
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtvvdesc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RTV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateRenderTargetView(m_gpuMsaaTexs[0].Get(), &rtvvdesc, m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC dsvvdesc = {DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_DSV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateDepthStencilView(m_gpuMsaaTexs[1].Get(), &dsvvdesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
}
|
||||
else
|
||||
{
|
||||
D3D12_RENDER_TARGET_VIEW_DESC rtvvdesc = {DXGI_FORMAT_R8G8B8A8_UNORM, D3D12_RTV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateRenderTargetView(m_gpuTexs[0].Get(), &rtvvdesc, m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
|
||||
D3D12_DEPTH_STENCIL_VIEW_DESC dsvvdesc = {DXGI_FORMAT_D24_UNORM_S8_UINT, D3D12_DSV_DIMENSION_TEXTURE2D};
|
||||
ctx->m_dev->CreateDepthStencilView(m_gpuTexs[1].Get(), &dsvvdesc, m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
public:
|
||||
size_t samples() const {return m_samples;}
|
||||
ComPtr<ID3D12Resource> m_gpuTexs[2];
|
||||
ComPtr<ID3D12Resource> m_gpuMsaaTexs[2];
|
||||
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
|
||||
ComPtr<ID3D12DescriptorHeap> m_dsvHeap;
|
||||
~D3D12TextureR() = default;
|
||||
|
||||
void resize(D3D12Context* ctx, size_t width, size_t height)
|
||||
{
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
Setup(ctx, width, height, m_samples);
|
||||
}
|
||||
};
|
||||
|
||||
ITextureS*
|
||||
|
@ -301,18 +361,21 @@ struct D3D12VertexFormat : IVertexFormat
|
|||
size_t m_elementCount;
|
||||
std::unique_ptr<D3D12_INPUT_ELEMENT_DESC[]> m_elements;
|
||||
D3D12VertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
||||
: m_elementCount(elementCount),
|
||||
m_elements(new D3D12_INPUT_ELEMENT_DESC[elementCount])
|
||||
: m_elementCount(elementCount),
|
||||
m_elements(new D3D12_INPUT_ELEMENT_DESC[elementCount])
|
||||
{
|
||||
memset(m_elements.get(), 0, elementCount * sizeof(D3D12_INPUT_ELEMENT_DESC));
|
||||
size_t offset = 0;
|
||||
for (size_t i=0 ; i<elementCount ; ++i)
|
||||
{
|
||||
const VertexElementDescriptor* elemin = &elements[i];
|
||||
D3D12_INPUT_ELEMENT_DESC& elem = m_elements[i];
|
||||
elem.SemanticName = SEMANTIC_NAME_TABLE[elements->semantic];
|
||||
elem.SemanticIndex = elements->semanticIdx;
|
||||
elem.Format = SEMANTIC_TYPE_TABLE[elements->semantic];
|
||||
elem.AlignedByteOffset = D3D12_APPEND_ALIGNED_ELEMENT;
|
||||
elem.SemanticName = SEMANTIC_NAME_TABLE[elemin->semantic];
|
||||
elem.SemanticIndex = elemin->semanticIdx;
|
||||
elem.Format = SEMANTIC_TYPE_TABLE[elemin->semantic];
|
||||
elem.AlignedByteOffset = offset;
|
||||
elem.InputSlotClass = D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA;
|
||||
offset += SEMANTIC_SIZE_TABLE[elemin->semantic];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -364,6 +427,7 @@ class D3D12ShaderPipeline : public IShaderPipeline
|
|||
desc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
|
||||
desc.NumRenderTargets = 1;
|
||||
desc.RTVFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
desc.DSVFormat = DXGI_FORMAT_D24_UNORM_S8_UINT;
|
||||
desc.SampleDesc.Count = 1;
|
||||
ThrowIfFailed(ctx->m_dev->CreateGraphicsPipelineState(&desc, __uuidof(ID3D12PipelineState), &m_state));
|
||||
}
|
||||
|
@ -417,6 +481,7 @@ static UINT64 PlaceTextureForGPU(ITexture* tex, D3D12Context* ctx, ID3D12Heap* g
|
|||
return static_cast<D3D12TextureD*>(tex)->placeForGPU(ctx, gpuHeap, offset);
|
||||
else if (tex->type() == ITexture::TextureStatic)
|
||||
return static_cast<D3D12TextureS*>(tex)->placeForGPU(ctx, gpuHeap, offset);
|
||||
return offset;
|
||||
}
|
||||
|
||||
static ID3D12Resource* GetBufferGPUResource(const IGraphicsBuffer* buf, int idx,
|
||||
|
@ -455,6 +520,7 @@ static ID3D12Resource* GetTextureGPUResource(const ITexture* tex, int idx)
|
|||
const D3D12TextureS* ctex = static_cast<const D3D12TextureS*>(tex);
|
||||
return ctex->m_gpuTex.Get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static const struct DefaultTex2DViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
||||
|
@ -464,7 +530,7 @@ static const struct DefaultTex2DViewDesc : D3D12_SHADER_RESOURCE_VIEW_DESC
|
|||
Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
||||
ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
|
||||
Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
|
||||
Texture2D = {0, -1, 0, 0.0f};
|
||||
Texture2D = {UINT(0), UINT(-1), UINT(0), 0.0f};
|
||||
}
|
||||
} Tex2DViewDesc;
|
||||
|
||||
|
@ -479,6 +545,8 @@ struct D3D12ShaderDataBinding : IShaderDataBinding
|
|||
std::unique_ptr<IGraphicsBuffer*[]> m_ubufs;
|
||||
size_t m_texCount;
|
||||
std::unique_ptr<ITexture*[]> m_texs;
|
||||
D3D12_VERTEX_BUFFER_VIEW m_vboView[2];
|
||||
D3D12_INDEX_BUFFER_VIEW m_iboView[2];
|
||||
D3D12ShaderDataBinding(D3D12Context* ctx,
|
||||
IShaderPipeline* pipeline,
|
||||
IGraphicsBuffer* vbuf, IGraphicsBuffer* ibuf,
|
||||
|
@ -514,10 +582,20 @@ struct D3D12ShaderDataBinding : IShaderDataBinding
|
|||
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_descHeap[b]->GetCPUDescriptorHandleForHeapStart());
|
||||
D3D12_SHADER_RESOURCE_VIEW_DESC viewDesc;
|
||||
|
||||
ctx->m_dev->CreateShaderResourceView(GetBufferGPUResource(m_vbuf, b, viewDesc), &viewDesc, handle);
|
||||
ID3D12Resource* res = GetBufferGPUResource(m_vbuf, b, viewDesc);
|
||||
m_vboView[b].BufferLocation = res->GetGPUVirtualAddress();
|
||||
m_vboView[b].StrideInBytes = viewDesc.Buffer.StructureByteStride;
|
||||
m_vboView[b].SizeInBytes = viewDesc.Buffer.NumElements * m_vboView[b].StrideInBytes;
|
||||
ctx->m_dev->CreateShaderResourceView(res, &viewDesc, handle);
|
||||
handle.Offset(1, incSz);
|
||||
if (m_ibuf)
|
||||
ctx->m_dev->CreateShaderResourceView(GetBufferGPUResource(m_ibuf, b, viewDesc), &viewDesc, handle);
|
||||
{
|
||||
res = GetBufferGPUResource(m_ibuf, b, viewDesc);
|
||||
m_iboView[b].BufferLocation = res->GetGPUVirtualAddress();
|
||||
m_iboView[b].SizeInBytes = viewDesc.Buffer.NumElements * viewDesc.Buffer.StructureByteStride;
|
||||
m_iboView[b].Format = DXGI_FORMAT_R32_UINT;
|
||||
ctx->m_dev->CreateShaderResourceView(res, &viewDesc, handle);
|
||||
}
|
||||
handle.Offset(1, incSz);
|
||||
for (size_t i=0 ; i<m_ubufCount ; ++i)
|
||||
{
|
||||
|
@ -531,6 +609,16 @@ struct D3D12ShaderDataBinding : IShaderDataBinding
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bind(ID3D12GraphicsCommandList* list, int b)
|
||||
{
|
||||
ID3D12DescriptorHeap* heap[] = {m_descHeap[b].Get()};
|
||||
list->SetDescriptorHeaps(1, heap);
|
||||
list->SetPipelineState(m_pipeline->m_state.Get());
|
||||
list->IASetVertexBuffers(0, 1, &m_vboView[b]);
|
||||
if (m_ibuf)
|
||||
list->IASetIndexBuffer(&m_iboView[b]);
|
||||
}
|
||||
};
|
||||
|
||||
IShaderDataBinding*
|
||||
|
@ -557,8 +645,9 @@ D3D12DataFactory::D3D12DataFactory(IGraphicsContext* parent, D3D12Context* ctx)
|
|||
|
||||
ComPtr<ID3DBlob> rsOutBlob;
|
||||
ComPtr<ID3DBlob> rsErrorBlob;
|
||||
ThrowIfFailed(D3D12SerializeRootSignature(
|
||||
&CD3DX12_ROOT_SIGNATURE_DESC(2, rootParms, 1, &CD3DX12_STATIC_SAMPLER_DESC(0)),
|
||||
ThrowIfFailed(D3D12SerializeRootSignaturePROC(
|
||||
&CD3DX12_ROOT_SIGNATURE_DESC(2, 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(),
|
||||
|
@ -571,6 +660,21 @@ void D3D12DataFactory::reset()
|
|||
m_deferredData = new struct D3D12Data();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
IGraphicsData* D3D12DataFactory::commit()
|
||||
{
|
||||
D3D12Data* retval = static_cast<D3D12Data*>(m_deferredData);
|
||||
|
@ -607,6 +711,9 @@ IGraphicsData* D3D12DataFactory::commit()
|
|||
D3D12_HEAP_TYPE_DEFAULT), __uuidof(ID3D12Heap), &retval->m_gpuHeap));
|
||||
ID3D12Heap* gpuHeap = retval->m_gpuHeap.Get();
|
||||
|
||||
/* Wait for previous transaction to complete */
|
||||
WaitForLoadList(m_ctx);
|
||||
|
||||
/* Place resources */
|
||||
UINT64 offset = 0;
|
||||
for (std::unique_ptr<D3D12GraphicsBufferS>& buf : retval->m_SBufs)
|
||||
|
@ -621,6 +728,13 @@ IGraphicsData* D3D12DataFactory::commit()
|
|||
for (std::unique_ptr<D3D12TextureD>& tex : retval->m_DTexs)
|
||||
offset = PlaceTextureForGPU(tex.get(), m_ctx, gpuHeap, offset);
|
||||
|
||||
/* 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);
|
||||
|
@ -653,6 +767,7 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
D3D12Context::Window* m_windowCtx;
|
||||
IGraphicsContext* m_parent;
|
||||
ComPtr<ID3D12GraphicsCommandList> m_cmdList;
|
||||
ComPtr<ID3D12Fence> m_fence;
|
||||
|
||||
size_t m_fillBuf = 0;
|
||||
size_t m_drawBuf = 0;
|
||||
|
@ -664,7 +779,8 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
m_cmdList->SetGraphicsRootSignature(m_ctx->m_rs.Get());
|
||||
}
|
||||
|
||||
D3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent)
|
||||
D3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent,
|
||||
ID3D12CommandQueue** cmdQueueOut)
|
||||
: m_ctx(ctx), m_windowCtx(windowCtx), m_parent(parent)
|
||||
{
|
||||
ThrowIfFailed(ctx->m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
|
@ -680,8 +796,8 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
D3D12_COMMAND_QUEUE_FLAG_NONE
|
||||
};
|
||||
ThrowIfFailed(ctx->m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &ctx->m_q));
|
||||
ThrowIfFailed(ctx->m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE,
|
||||
__uuidof(ID3D12Fence), &ctx->m_frameFence));
|
||||
*cmdQueueOut = ctx->m_q.Get();
|
||||
ThrowIfFailed(ctx->m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &m_fence));
|
||||
ThrowIfFailed(ctx->m_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, ctx->m_qalloc[0].Get(),
|
||||
nullptr, __uuidof(ID3D12GraphicsCommandList), &m_cmdList));
|
||||
m_cmdList->SetGraphicsRootSignature(m_ctx->m_rs.Get());
|
||||
|
@ -690,18 +806,16 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
void setShaderDataBinding(IShaderDataBinding* binding)
|
||||
{
|
||||
D3D12ShaderDataBinding* cbind = static_cast<D3D12ShaderDataBinding*>(binding);
|
||||
ID3D12DescriptorHeap* descHeap = cbind->m_descHeap[m_fillBuf].Get();
|
||||
m_cmdList->SetDescriptorHeaps(1, &descHeap);
|
||||
m_cmdList->SetPipelineState(cbind->m_pipeline->m_state.Get());
|
||||
cbind->bind(m_cmdList.Get(), m_fillBuf);
|
||||
}
|
||||
|
||||
ID3D12Resource* m_boundTarget = nullptr;
|
||||
D3D12TextureR* m_boundTarget = nullptr;
|
||||
void setRenderTarget(ITextureR* target)
|
||||
{
|
||||
D3D12TextureR* ctarget = static_cast<D3D12TextureR*>(target);
|
||||
|
||||
if (m_boundTarget)
|
||||
m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_boundTarget,
|
||||
m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(m_boundTarget->m_gpuTexs[0].Get(),
|
||||
D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE));
|
||||
|
||||
m_cmdList->OMSetRenderTargets(1, &ctarget->m_rtvHeap->GetCPUDescriptorHandleForHeapStart(),
|
||||
|
@ -710,13 +824,21 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
m_cmdList->ResourceBarrier(1, &CD3DX12_RESOURCE_BARRIER::Transition(ctarget->m_gpuTexs[0].Get(),
|
||||
D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_RENDER_TARGET));
|
||||
|
||||
m_boundTarget = ctarget->m_gpuTexs[0].Get();
|
||||
m_boundTarget = ctarget;
|
||||
}
|
||||
|
||||
void setViewport(const SWindowRect& rect)
|
||||
{
|
||||
D3D12_VIEWPORT vp = {rect.location[0], rect.location[1], rect.size[0], rect.size[1], 0.0, 1.0};
|
||||
m_cmdList->RSSetViewports(1, &vp);
|
||||
D3D12_RECT r = {rect.location[0], rect.location[1], rect.size[0], rect.size[1]};
|
||||
m_cmdList->RSSetScissorRects(1, &r);
|
||||
}
|
||||
|
||||
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
|
||||
{
|
||||
D3D12TextureR* ctex = static_cast<D3D12TextureR*>(tex);
|
||||
ctex->resize(m_ctx, width, height);
|
||||
}
|
||||
|
||||
float m_clearColor[4] = {0.0,0.0,0.0,1.0};
|
||||
|
@ -730,14 +852,16 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
|
||||
void clearTarget(bool render=true, bool depth=true)
|
||||
{
|
||||
if (!m_boundTarget)
|
||||
return;
|
||||
if (render)
|
||||
{
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE handle;
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_boundTarget->m_rtvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
m_cmdList->ClearRenderTargetView(handle, m_clearColor, 0, nullptr);
|
||||
}
|
||||
if (depth)
|
||||
{
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE handle;
|
||||
CD3DX12_CPU_DESCRIPTOR_HANDLE handle(m_boundTarget->m_dsvHeap->GetCPUDescriptorHandleForHeapStart());
|
||||
m_cmdList->ClearDepthStencilView(handle, D3D12_CLEAR_FLAG_DEPTH, 1.0, 0, 0, nullptr);
|
||||
}
|
||||
}
|
||||
|
@ -775,11 +899,12 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
{
|
||||
D3D12TextureR* csource = static_cast<D3D12TextureR*>(source);
|
||||
|
||||
ID3D12Resource* src = csource->m_gpuTexs[0].Get();
|
||||
ID3D12Resource* dest = m_windowCtx->m_fb[m_windowCtx->m_backBuf].Get();
|
||||
|
||||
if (csource->m_samples > 1)
|
||||
{
|
||||
ID3D12Resource* src = csource->m_gpuMsaaTexs[0].Get();
|
||||
|
||||
D3D12_RESOURCE_BARRIER msaaSetup[] =
|
||||
{
|
||||
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
||||
|
@ -802,6 +927,8 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
}
|
||||
else
|
||||
{
|
||||
ID3D12Resource* src = csource->m_gpuTexs[0].Get();
|
||||
|
||||
D3D12_RESOURCE_BARRIER copySetup[] =
|
||||
{
|
||||
CD3DX12_RESOURCE_BARRIER::Transition(src,
|
||||
|
@ -829,9 +956,11 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
void execute()
|
||||
{
|
||||
/* Check on fence */
|
||||
if (m_ctx->m_frameFence->GetCompletedValue() < m_submittedFenceVal)
|
||||
if (m_fence->GetCompletedValue() < m_submittedFenceVal)
|
||||
{
|
||||
/* Abandon this list (renderer too slow) */
|
||||
resetCommandList();
|
||||
m_doPresent = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -848,10 +977,11 @@ struct D3D12CommandQueue : IGraphicsCommandQueue
|
|||
{
|
||||
ThrowIfFailed(m_windowCtx->m_swapChain->Present(1, 0));
|
||||
m_windowCtx->m_backBuf = m_windowCtx->m_swapChain->GetCurrentBackBufferIndex();
|
||||
m_doPresent = false;
|
||||
}
|
||||
|
||||
++m_submittedFenceVal;
|
||||
ThrowIfFailed(m_ctx->m_q->Signal(m_ctx->m_frameFence.Get(), m_submittedFenceVal));
|
||||
ThrowIfFailed(m_ctx->m_q->Signal(m_fence.Get(), m_submittedFenceVal));
|
||||
|
||||
resetCommandList();
|
||||
}
|
||||
|
@ -935,9 +1065,10 @@ IVertexFormat* D3D12DataFactory::newVertexFormat
|
|||
return retval;
|
||||
}
|
||||
|
||||
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent)
|
||||
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent,
|
||||
ID3D12CommandQueue** cmdQueueOut)
|
||||
{
|
||||
return new struct D3D12CommandQueue(ctx, windowCtx, parent);
|
||||
return new struct D3D12CommandQueue(ctx, windowCtx, parent, cmdQueueOut);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
#include <Usbiodef.h>
|
||||
|
||||
#if _DEBUG
|
||||
#define DXGI_CREATE_FLAGS DXGI_CREATE_FACTORY_DEBUG
|
||||
#define D3D11_CREATE_DEVICE_FLAGS D3D11_CREATE_DEVICE_DEBUG
|
||||
#else
|
||||
#define DXGI_CREATE_FLAGS 0
|
||||
#define D3D11_CREATE_DEVICE_FLAGS 0
|
||||
#endif
|
||||
|
||||
|
@ -16,6 +18,9 @@
|
|||
#include "boo/inputdev/DeviceFinder.hpp"
|
||||
#include <LogVisor/LogVisor.hpp>
|
||||
|
||||
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
|
||||
PFN_D3D12_SERIALIZE_ROOT_SIGNATURE D3D12SerializeRootSignaturePROC = nullptr;
|
||||
|
||||
namespace boo
|
||||
{
|
||||
static LogVisor::LogModule Log("ApplicationWin32");
|
||||
|
@ -68,22 +73,59 @@ public:
|
|||
HMODULE d3d12lib = LoadLibraryW(L"D3D12.dll");
|
||||
if (d3d12lib)
|
||||
{
|
||||
#if _DEBUG
|
||||
{
|
||||
PFN_D3D12_GET_DEBUG_INTERFACE MyD3D12GetDebugInterface =
|
||||
(PFN_D3D12_GET_DEBUG_INTERFACE)GetProcAddress(d3d12lib, "D3D12GetDebugInterface");
|
||||
ComPtr<ID3D12Debug> debugController;
|
||||
if (SUCCEEDED(MyD3D12GetDebugInterface(IID_PPV_ARGS(&debugController))))
|
||||
{
|
||||
debugController->EnableDebugLayer();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
D3D12SerializeRootSignaturePROC =
|
||||
(PFN_D3D12_SERIALIZE_ROOT_SIGNATURE)GetProcAddress(d3d12lib, "D3D12SerializeRootSignature");
|
||||
|
||||
/* Create device */
|
||||
PFN_D3D12_CREATE_DEVICE MyD3D12CreateDevice = (PFN_D3D12_CREATE_DEVICE)GetProcAddress(d3d12lib, "D3D12CreateDevice");
|
||||
if (!MyD3D12CreateDevice)
|
||||
Log.report(LogVisor::FatalError, "unable to find D3D12CreateDevice in D3D12.dll");
|
||||
|
||||
/* Create device */
|
||||
if (FAILED(MyD3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), &m_d3dCtx.m_ctx12.m_dev)))
|
||||
HRESULT hr = MyD3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), &m_d3dCtx.m_ctx12.m_dev);
|
||||
if (FAILED(hr))
|
||||
Log.report(LogVisor::FatalError, "unable to create D3D12 device");
|
||||
|
||||
/* Obtain DXGI Factory */
|
||||
ComPtr<IDXGIDevice2> device;
|
||||
ComPtr<IDXGIAdapter> adapter;
|
||||
m_d3dCtx.m_ctx12.m_dev.As<IDXGIDevice2>(&device);
|
||||
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
|
||||
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_dxFactory);
|
||||
|
||||
hr = MyCreateDXGIFactory2(DXGI_CREATE_FLAGS, __uuidof(IDXGIFactory4), &m_d3dCtx.m_ctx12.m_dxFactory);
|
||||
if (FAILED(hr))
|
||||
Log.report(LogVisor::FatalError, "unable to create DXGI factory");
|
||||
|
||||
/* Establish loader objects */
|
||||
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
__uuidof(ID3D12CommandAllocator), &m_d3dCtx.m_ctx12.m_loadqalloc)))
|
||||
Log.report(LogVisor::FatalError, "unable to create loader allocator");
|
||||
|
||||
D3D12_COMMAND_QUEUE_DESC desc =
|
||||
{
|
||||
D3D12_COMMAND_LIST_TYPE_DIRECT,
|
||||
D3D12_COMMAND_QUEUE_PRIORITY_NORMAL,
|
||||
D3D12_COMMAND_QUEUE_FLAG_NONE
|
||||
};
|
||||
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandQueue(&desc, __uuidof(ID3D12CommandQueue), &m_d3dCtx.m_ctx12.m_loadq)))
|
||||
Log.report(LogVisor::FatalError, "unable to create loader queue");
|
||||
|
||||
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence), &m_d3dCtx.m_ctx12.m_loadfence)))
|
||||
Log.report(LogVisor::FatalError, "unable to create loader fence");
|
||||
|
||||
m_d3dCtx.m_ctx12.m_loadfencehandle = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
||||
|
||||
if (FAILED(m_d3dCtx.m_ctx12.m_dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_d3dCtx.m_ctx12.m_loadqalloc.Get(),
|
||||
nullptr, __uuidof(ID3D12GraphicsCommandList), &m_d3dCtx.m_ctx12.m_loadlist)))
|
||||
Log.report(LogVisor::FatalError, "unable to create loader list");
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
@ -110,7 +152,7 @@ public:
|
|||
ComPtr<IDXGIAdapter> adapter;
|
||||
m_d3dCtx.m_ctx11.m_dev.As<IDXGIDevice2>(&device);
|
||||
device->GetParent(__uuidof(IDXGIAdapter), &adapter);
|
||||
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_dxFactory);
|
||||
adapter->GetParent(__uuidof(IDXGIFactory2), &m_d3dCtx.m_ctx11.m_dxFactory);
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -154,8 +196,11 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
DWORD m_mainThreadId = 0;
|
||||
int run()
|
||||
{
|
||||
m_mainThreadId = GetCurrentThreadId();
|
||||
|
||||
/* Spawn client thread */
|
||||
int clientReturn = 0;
|
||||
std::thread clientThread([&]()
|
||||
|
@ -165,6 +210,16 @@ public:
|
|||
MSG msg = {0};
|
||||
while (GetMessage(&msg, NULL, 0, 0))
|
||||
{
|
||||
if (msg.message == WM_USER)
|
||||
{
|
||||
/* New-window message (coalesced onto main thread) */
|
||||
std::unique_lock<std::mutex> lk(m_nwmt);
|
||||
const SystemString* title = reinterpret_cast<const SystemString*>(msg.wParam);
|
||||
m_mwret = newWindow(*title);
|
||||
lk.unlock();
|
||||
m_nwcv.notify_one();
|
||||
continue;
|
||||
}
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
@ -194,8 +249,20 @@ public:
|
|||
return m_args;
|
||||
}
|
||||
|
||||
std::mutex m_nwmt;
|
||||
std::condition_variable m_nwcv;
|
||||
IWindow* m_mwret = nullptr;
|
||||
IWindow* newWindow(const SystemString& title)
|
||||
{
|
||||
if (GetCurrentThreadId() != m_mainThreadId)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(m_nwmt);
|
||||
if (!PostThreadMessage(m_mainThreadId, WM_USER, WPARAM(&title), 0))
|
||||
Log.report(LogVisor::FatalError, "PostThreadMessage error");
|
||||
m_nwcv.wait(lk);
|
||||
return m_mwret;
|
||||
}
|
||||
|
||||
IWindow* window = _WindowWin32New(title, m_d3dCtx);
|
||||
HWND hwnd = HWND(window->getPlatformHandle());
|
||||
m_allWindows[hwnd] = window;
|
||||
|
@ -217,6 +284,24 @@ int ApplicationRun(IApplication::EPlatformType platform,
|
|||
if (platform != IApplication::PLAT_WIN32 &&
|
||||
platform != IApplication::PLAT_AUTO)
|
||||
return 1;
|
||||
|
||||
/* One class for *all* boo windows */
|
||||
WNDCLASS wndClass =
|
||||
{
|
||||
0,
|
||||
WindowProc,
|
||||
0,
|
||||
0,
|
||||
GetModuleHandle(nullptr),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
L"BooWindow"
|
||||
};
|
||||
|
||||
RegisterClassW(&wndClass);
|
||||
|
||||
APP = new ApplicationWin32(cb, uniqueName, friendlyName, pname, args, singleInstance);
|
||||
return APP->run();
|
||||
}
|
||||
|
@ -242,36 +327,3 @@ static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM l
|
|||
return static_cast<boo::ApplicationWin32*>(boo::APP)->winHwndHandler(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
int wmain(int argc, wchar_t** argv);
|
||||
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
|
||||
{
|
||||
#if _DEBUG
|
||||
/* Debug console */
|
||||
AllocConsole();
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
#endif
|
||||
|
||||
/* One class for *all* boo windows */
|
||||
WNDCLASS wndClass =
|
||||
{
|
||||
0,
|
||||
WindowProc,
|
||||
0,
|
||||
0,
|
||||
hInstance,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
L"BooWindow"
|
||||
};
|
||||
|
||||
RegisterClassW(&wndClass);
|
||||
|
||||
int argc = 0;
|
||||
LPWSTR* argv = CommandLineToArgvW(lpCmdLine, &argc);
|
||||
|
||||
/* Call into the 'proper' entry point */
|
||||
return wmain(argc, argv);
|
||||
|
||||
}
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <windows.h>
|
||||
#include <unordered_map>
|
||||
|
||||
#include "boo/System.hpp"
|
||||
|
||||
namespace boo {class IWindow;}
|
||||
|
||||
#if _WIN32_WINNT_WIN10
|
||||
|
@ -19,12 +21,16 @@ namespace boo {class IWindow;}
|
|||
|
||||
struct D3D12Context
|
||||
{
|
||||
ComPtr<IDXGIFactory4> m_dxFactory;
|
||||
ComPtr<ID3D12Device> m_dev;
|
||||
ComPtr<ID3D12CommandAllocator> m_qalloc[2];
|
||||
ComPtr<ID3D12CommandQueue> m_q;
|
||||
ComPtr<ID3D12CommandAllocator> m_loadqalloc;
|
||||
ComPtr<ID3D12CommandQueue> m_loadq;
|
||||
ComPtr<ID3D12Fence> m_frameFence;
|
||||
ComPtr<ID3D12Fence> m_loadfence;
|
||||
UINT64 m_loadfenceval = 0;
|
||||
HANDLE m_loadfencehandle;
|
||||
ComPtr<ID3D12GraphicsCommandList> m_loadlist;
|
||||
ComPtr<ID3D12RootSignature> m_rs;
|
||||
struct Window
|
||||
{
|
||||
|
@ -45,6 +51,7 @@ struct D3D12Context
|
|||
|
||||
struct D3D11Context
|
||||
{
|
||||
ComPtr<IDXGIFactory2> m_dxFactory;
|
||||
ComPtr<ID3D11Device1> m_dev;
|
||||
ComPtr<ID3D11DeviceContext1> m_devCtx;
|
||||
struct Window
|
||||
|
@ -55,15 +62,12 @@ struct D3D11Context
|
|||
std::unordered_map<boo::IWindow*, Window> m_windows;
|
||||
};
|
||||
|
||||
#include "boo/System.hpp"
|
||||
|
||||
struct D3DAppContext
|
||||
{
|
||||
D3D11Context m_ctx11;
|
||||
#if _WIN32_WINNT_WIN10
|
||||
D3D12Context m_ctx12;
|
||||
#endif
|
||||
ComPtr<IDXGIFactory2> m_dxFactory;
|
||||
};
|
||||
|
||||
struct HWNDEvent
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
namespace boo
|
||||
{
|
||||
static LogVisor::LogModule Log("WindowWin32");
|
||||
class WindowWin32;
|
||||
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent);
|
||||
IGraphicsCommandQueue* _NewD3D12CommandQueue(D3D12Context* ctx, D3D12Context::Window* windowCtx, IGraphicsContext* parent,
|
||||
ID3D12CommandQueue** cmdQueueOut);
|
||||
IGraphicsCommandQueue* _NewD3D11CommandQueue(D3D11Context* ctx, D3D11Context::Window* windowCtx, IGraphicsContext* parent);
|
||||
|
||||
struct GraphicsContextWin32 : IGraphicsContext
|
||||
|
@ -19,7 +19,7 @@ struct GraphicsContextWin32 : IGraphicsContext
|
|||
|
||||
EGraphicsAPI m_api;
|
||||
EPixelFormat m_pf;
|
||||
WindowWin32* m_parentWindow;
|
||||
IWindow* m_parentWindow;
|
||||
D3DAppContext& m_d3dCtx;
|
||||
|
||||
ComPtr<IDXGISwapChain1> m_swapChain;
|
||||
|
@ -31,7 +31,7 @@ struct GraphicsContextWin32 : IGraphicsContext
|
|||
public:
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
GraphicsContextWin32(EGraphicsAPI api, WindowWin32* parentWindow, D3DAppContext& d3dCtx)
|
||||
GraphicsContextWin32(EGraphicsAPI api, IWindow* parentWindow, HWND hwnd, D3DAppContext& d3dCtx)
|
||||
: m_api(api),
|
||||
m_pf(PF_RGBA8),
|
||||
m_parentWindow(parentWindow),
|
||||
|
@ -46,30 +46,22 @@ public:
|
|||
scDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
||||
scDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
|
||||
|
||||
#if _WIN32_WINNT_WIN10
|
||||
IUnknown* dev;
|
||||
if (d3dCtx.m_ctx12.m_dev)
|
||||
{
|
||||
scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
|
||||
dev = static_cast<IUnknown*>(d3dCtx.m_ctx12.m_dev.Get());
|
||||
}
|
||||
else
|
||||
dev = static_cast<IUnknown*>(d3dCtx.m_ctx11.m_dev.Get());
|
||||
#else
|
||||
IUnknown* dev = static_cast<IUnknown*>(d3dCtx.m_ctx11.m_dev.Get());
|
||||
#endif
|
||||
if (FAILED(d3dCtx.m_dxFactory->CreateSwapChainForHwnd(dev,
|
||||
parentWindow->m_hwnd, &scDesc, nullptr, nullptr, &m_swapChain)))
|
||||
Log.report(LogVisor::FatalError, "unable to create swap chain");
|
||||
|
||||
if (FAILED(m_swapChain->GetContainingOutput(&m_output)))
|
||||
Log.report(LogVisor::FatalError, "unable to get DXGI output");
|
||||
|
||||
#if _WIN32_WINNT_WIN10
|
||||
if (d3dCtx.m_ctx12.m_dev)
|
||||
{
|
||||
auto insIt = d3dCtx.m_ctx12.m_windows.emplace(std::make_pair(parentWindow, D3D12Context::Window()));
|
||||
D3D12Context::Window& w = insIt.first->second;
|
||||
|
||||
ID3D12CommandQueue* cmdQueue;
|
||||
m_dataFactory = new D3D12DataFactory(this, &d3dCtx.m_ctx12);
|
||||
m_commandQueue = _NewD3D12CommandQueue(&d3dCtx.m_ctx12, &w, this, &cmdQueue);
|
||||
|
||||
scDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
|
||||
HRESULT hr = d3dCtx.m_ctx12.m_dxFactory->CreateSwapChainForHwnd(cmdQueue,
|
||||
hwnd, &scDesc, nullptr, nullptr, &m_swapChain);
|
||||
if (FAILED(hr))
|
||||
Log.report(LogVisor::FatalError, "unable to create swap chain");
|
||||
|
||||
m_swapChain.As<IDXGISwapChain3>(&w.m_swapChain);
|
||||
m_swapChain->GetBuffer(0, __uuidof(ID3D12Resource), &w.m_fb[0]);
|
||||
m_swapChain->GetBuffer(1, __uuidof(ID3D12Resource), &w.m_fb[1]);
|
||||
|
@ -77,12 +69,15 @@ public:
|
|||
D3D12_RESOURCE_DESC resDesc = w.m_fb[0]->GetDesc();
|
||||
w.width = resDesc.Width;
|
||||
w.height = resDesc.Height;
|
||||
m_dataFactory = new D3D12DataFactory(this, &d3dCtx.m_ctx12);
|
||||
m_commandQueue = _NewD3D12CommandQueue(&d3dCtx.m_ctx12, &w, this);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
#if 0
|
||||
if (FAILED(d3dCtx.m_ctx11.m_dxFactory->CreateSwapChainForHwnd(d3dCtx.m_ctx11.m_dev.Get(),
|
||||
hwnd, &scDesc, nullptr, nullptr, &m_swapChain)))
|
||||
Log.report(LogVisor::FatalError, "unable to create swap chain");
|
||||
|
||||
auto insIt = d3dCtx.m_ctx11.m_windows.emplace(std::make_pair(parentWindow, D3D11Context::Window()));
|
||||
D3D11Context::Window& w = insIt.first->second;
|
||||
ComPtr<ID3D11Texture2D> fbRes;
|
||||
|
@ -93,7 +88,11 @@ public:
|
|||
w.height = resDesc.Height;
|
||||
m_dataFactory = new D3D11DataFactory(this, &d3dCtx.m_ctx11);
|
||||
m_commandQueue = _NewD3D11CommandQueue(&d3dCtx.m_ctx11, &insIt.first->second, this);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (FAILED(m_swapChain->GetContainingOutput(&m_output)))
|
||||
Log.report(LogVisor::FatalError, "unable to get DXGI output");
|
||||
}
|
||||
|
||||
~GraphicsContextWin32()
|
||||
|
@ -141,18 +140,18 @@ public:
|
|||
|
||||
IGraphicsCommandQueue* getCommandQueue()
|
||||
{
|
||||
|
||||
return m_commandQueue;
|
||||
}
|
||||
|
||||
IGraphicsDataFactory* getDataFactory()
|
||||
{
|
||||
|
||||
return m_dataFactory;
|
||||
}
|
||||
|
||||
/* Creates a new context on current thread!! Call from client loading thread */
|
||||
IGraphicsDataFactory* getLoadContextDataFactory()
|
||||
{
|
||||
|
||||
return m_dataFactory;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -235,7 +234,7 @@ public:
|
|||
m_hwnd = CreateWindowW(L"BooWindow", title.c_str(), WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
NULL, NULL, NULL, NULL);
|
||||
m_gfxCtx.reset(new GraphicsContextWin32(IGraphicsContext::API_D3D11, this, d3dCtx));
|
||||
m_gfxCtx.reset(new GraphicsContextWin32(IGraphicsContext::API_D3D11, this, m_hwnd, d3dCtx));
|
||||
}
|
||||
|
||||
~WindowWin32()
|
||||
|
|
133
test/main.cpp
133
test/main.cpp
|
@ -7,6 +7,15 @@
|
|||
#include <condition_variable>
|
||||
#include <LogVisor/LogVisor.hpp>
|
||||
|
||||
#if _WIN32_WINNT_WIN10
|
||||
#include <boo/graphicsdev/D3D12.hpp>
|
||||
#include <boo/graphicsdev/D3D11.hpp>
|
||||
#elif _WIN32_WINNT_WIN7
|
||||
#include <boo/graphicsdev/D3D11.hpp>
|
||||
#else
|
||||
#error unsupported windows version
|
||||
#endif
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
|
@ -203,8 +212,7 @@ struct TestApplicationCallback : IApplicationCallback
|
|||
{
|
||||
std::unique_lock<std::mutex> lk(self->m_initmt);
|
||||
|
||||
GLDataFactory* factory =
|
||||
dynamic_cast<GLDataFactory*>(self->mainWindow->getLoadContextDataFactory());
|
||||
IGraphicsDataFactory* factory = self->mainWindow->getLoadContextDataFactory();
|
||||
|
||||
/* Create render target */
|
||||
int x, y, w, h;
|
||||
|
@ -250,32 +258,70 @@ struct TestApplicationCallback : IApplicationCallback
|
|||
factory->newStaticTexture(256, 256, 1, TextureFormatRGBA8, tex, 256*256*4);
|
||||
|
||||
/* Make shader pipeline */
|
||||
static const char* VS =
|
||||
"#version 330\n"
|
||||
"layout(location=0) in vec3 in_pos;\n"
|
||||
"layout(location=1) in vec2 in_uv;\n"
|
||||
"out vec2 out_uv;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(in_pos, 1.0);\n"
|
||||
" out_uv = in_uv;\n"
|
||||
"}\n";
|
||||
IShaderPipeline* pipeline = nullptr;
|
||||
if (factory->platform() == IGraphicsDataFactory::PlatformOGL)
|
||||
{
|
||||
GLDataFactory* glF = dynamic_cast<GLDataFactory*>(factory);
|
||||
|
||||
static const char* FS =
|
||||
"#version 330\n"
|
||||
"precision highp float;\n"
|
||||
"uniform sampler2D smplr;\n"
|
||||
"layout(location=0) out vec4 out_frag;\n"
|
||||
"in vec2 out_uv;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" out_frag = texture(smplr, out_uv);\n"
|
||||
"}\n";
|
||||
static const char* VS =
|
||||
"#version 330\n"
|
||||
"layout(location=0) in vec3 in_pos;\n"
|
||||
"layout(location=1) in vec2 in_uv;\n"
|
||||
"out vec2 out_uv;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_Position = vec4(in_pos, 1.0);\n"
|
||||
" out_uv = in_uv;\n"
|
||||
"}\n";
|
||||
|
||||
static const char* FS =
|
||||
"#version 330\n"
|
||||
"precision highp float;\n"
|
||||
"uniform sampler2D smplr;\n"
|
||||
"layout(location=0) out vec4 out_frag;\n"
|
||||
"in vec2 out_uv;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" out_frag = texture(smplr, out_uv);\n"
|
||||
"}\n";
|
||||
|
||||
static const char* TexNames[] = {"smplr"};
|
||||
|
||||
pipeline = glF->newShaderPipeline(VS, FS, 1, TexNames, BlendFactorOne, BlendFactorZero, true, true, false);
|
||||
}
|
||||
#if _WIN32_WINNT_WIN10
|
||||
else if (factory->platform() == IGraphicsDataFactory::PlatformD3D12)
|
||||
{
|
||||
D3D12DataFactory* d3dF = dynamic_cast<D3D12DataFactory*>(factory);
|
||||
|
||||
static const char* VS =
|
||||
"struct VertData {float3 in_pos : POSITION; float2 in_uv : UV;};\n"
|
||||
"struct VertToFrag {float4 out_pos : SV_Position; float2 out_uv : UV;};\n"
|
||||
"VertToFrag main(in VertData v)\n"
|
||||
"{\n"
|
||||
" VertToFrag retval;\n"
|
||||
" retval.out_pos = float4(v.in_pos, 1.0);\n"
|
||||
" retval.out_uv = v.in_uv;\n"
|
||||
" return retval;\n"
|
||||
"}\n";
|
||||
|
||||
static const char* PS =
|
||||
"SamplerState samp : register(s0);\n"
|
||||
"Texture2D tex : register(t0);\n"
|
||||
"struct VertToFrag {float4 out_pos : SV_Position; float2 out_uv : UV;};\n"
|
||||
"float4 main(in VertToFrag d) : SV_Target0\n"
|
||||
"{\n"
|
||||
" return tex.Sample(samp, d.out_uv);\n"
|
||||
"}\n";
|
||||
|
||||
ComPtr<ID3DBlob> vsCompile;
|
||||
ComPtr<ID3DBlob> psCompile;
|
||||
pipeline = d3dF->newShaderPipeline(VS, PS, vsCompile, psCompile, vfmt,
|
||||
BlendFactorOne, BlendFactorZero, true, true, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
static const char* TexNames[] = {"smplr"};
|
||||
|
||||
IShaderPipeline* pipeline =
|
||||
factory->newShaderPipeline(VS, FS, 1, TexNames, BlendFactorOne, BlendFactorZero, true, true, false);
|
||||
|
||||
/* Make shader data binding */
|
||||
self->m_binding =
|
||||
|
@ -320,17 +366,23 @@ struct TestApplicationCallback : IApplicationCallback
|
|||
while (running)
|
||||
{
|
||||
mainWindow->waitForRetrace();
|
||||
gfxQ->setRenderTarget(m_renderTarget);
|
||||
gfxQ->setViewport(windowCallback.m_lastRect);
|
||||
|
||||
if (windowCallback.m_rectDirty)
|
||||
{
|
||||
gfxQ->resizeRenderTexture(m_renderTarget, windowCallback.m_lastRect.size[0], windowCallback.m_lastRect.size[1]);
|
||||
windowCallback.m_rectDirty = false;
|
||||
}
|
||||
|
||||
gfxQ->setRenderTarget(m_renderTarget);
|
||||
SWindowRect r = windowCallback.m_lastRect;
|
||||
r.location[0] = 0;
|
||||
r.location[1] = 0;
|
||||
gfxQ->setViewport(r);
|
||||
float rgba[] = {sinf(frameIdx / 60.0), cosf(frameIdx / 60.0), 0.0, 1.0};
|
||||
gfxQ->setClearColor(rgba);
|
||||
gfxQ->clearTarget();
|
||||
gfxQ->setDrawPrimitive(PrimitiveTriStrips);
|
||||
|
||||
gfxQ->setShaderDataBinding(m_binding);
|
||||
gfxQ->draw(0, 4);
|
||||
gfxQ->resolveDisplay(m_renderTarget);
|
||||
|
@ -385,3 +437,30 @@ int main(int argc, const char** argv)
|
|||
return ret;
|
||||
}
|
||||
|
||||
#if _WIN32
|
||||
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int)
|
||||
{
|
||||
int argc = 0;
|
||||
const boo::SystemChar** argv = (const wchar_t**)(CommandLineToArgvW(lpCmdLine, &argc));
|
||||
|
||||
LogVisor::CreateWin32Console();
|
||||
LogVisor::RegisterConsoleLogger();
|
||||
boo::TestApplicationCallback appCb;
|
||||
int ret = ApplicationRun(boo::IApplication::PLAT_AUTO,
|
||||
appCb, _S("rwk"), _S("RWK"), argc, argv);
|
||||
printf("IM DYING!!\n");
|
||||
return ret;
|
||||
|
||||
}
|
||||
#else
|
||||
int main(int argc, const char** argv)
|
||||
{
|
||||
LogVisor::RegisterConsoleLogger();
|
||||
boo::TestApplicationCallback appCb;
|
||||
int ret = ApplicationRun(boo::IApplication::PLAT_AUTO,
|
||||
appCb, _S("rwk"), _S("RWK"), argc, argv);
|
||||
printf("IM DYING!!\n");
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
|
Loading…
Reference in New Issue