2015-11-06 03:20:58 +00:00
|
|
|
#include "../win/Win32Common.hpp"
|
2016-03-04 23:02:18 +00:00
|
|
|
#include "logvisor/logvisor.hpp"
|
2015-11-06 03:20:58 +00:00
|
|
|
#include "boo/graphicsdev/D3D.hpp"
|
|
|
|
#include "boo/IGraphicsContext.hpp"
|
2017-01-20 03:52:40 +00:00
|
|
|
#include "Common.hpp"
|
2015-11-06 03:20:58 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <thread>
|
|
|
|
#include <mutex>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <d3dcompiler.h>
|
|
|
|
#include <comdef.h>
|
2015-12-02 22:04:24 +00:00
|
|
|
#include <algorithm>
|
2015-12-09 05:23:15 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <forward_list>
|
2017-03-05 22:59:58 +00:00
|
|
|
#include "xxhash.h"
|
2015-12-02 22:04:24 +00:00
|
|
|
|
|
|
|
#undef min
|
|
|
|
#undef max
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
extern pD3DCompile D3DCompilePROC;
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
namespace boo
|
|
|
|
{
|
2016-03-04 23:02:18 +00:00
|
|
|
static logvisor::Module Log("boo::D3D11");
|
2017-03-05 22:59:58 +00:00
|
|
|
class D3D11DataFactory;
|
|
|
|
|
|
|
|
struct D3D11ShareableShader : IShareableShader<D3D11DataFactory, D3D11ShareableShader>
|
|
|
|
{
|
|
|
|
ComPtr<ID3D11DeviceChild> m_shader;
|
|
|
|
ComPtr<ID3DBlob> m_vtxBlob;
|
|
|
|
D3D11ShareableShader(D3D11DataFactory& fac, uint64_t srcKey, uint64_t binKey,
|
|
|
|
ComPtr<ID3D11DeviceChild>&& s, ComPtr<ID3DBlob>&& vb)
|
|
|
|
: IShareableShader(fac, srcKey, binKey), m_shader(std::move(s)), m_vtxBlob(std::move(vb)) {}
|
|
|
|
D3D11ShareableShader(D3D11DataFactory& fac, uint64_t srcKey, uint64_t binKey,
|
|
|
|
ComPtr<ID3D11DeviceChild>&& s)
|
|
|
|
: IShareableShader(fac, srcKey, binKey), m_shader(std::move(s)) {}
|
|
|
|
};
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
static inline void ThrowIfFailed(HRESULT hr)
|
|
|
|
{
|
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
|
|
|
// Set a breakpoint on this line to catch Win32 API errors.
|
|
|
|
_com_error err(hr);
|
|
|
|
LPCTSTR errMsg = err.ErrorMessage();
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, errMsg);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
struct D3D11Data : IGraphicsDataPriv<D3D11Data>
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
std::vector<std::unique_ptr<class D3D11ShaderPipeline>> m_SPs;
|
|
|
|
std::vector<std::unique_ptr<struct D3D11ShaderDataBinding>> m_SBinds;
|
|
|
|
std::vector<std::unique_ptr<class D3D11GraphicsBufferS>> m_SBufs;
|
|
|
|
std::vector<std::unique_ptr<class D3D11GraphicsBufferD>> m_DBufs;
|
|
|
|
std::vector<std::unique_ptr<class D3D11TextureS>> m_STexs;
|
|
|
|
std::vector<std::unique_ptr<class D3D11TextureSA>> m_SATexs;
|
|
|
|
std::vector<std::unique_ptr<class D3D11TextureD>> m_DTexs;
|
|
|
|
std::vector<std::unique_ptr<class D3D11TextureR>> m_RTexs;
|
|
|
|
std::vector<std::unique_ptr<struct D3D11VertexFormat>> m_VFmts;
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
|
|
|
|
2016-12-11 06:18:41 +00:00
|
|
|
class D3D11GraphicsBufferD;
|
2016-12-11 01:50:26 +00:00
|
|
|
struct D3D11Pool : IGraphicsBufferPool
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
std::unordered_map<D3D11GraphicsBufferD*, std::unique_ptr<D3D11GraphicsBufferD>> m_DBufs;
|
2016-12-11 01:50:26 +00:00
|
|
|
};
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
static const D3D11_BIND_FLAG USE_TABLE[] =
|
|
|
|
{
|
|
|
|
D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
D3D11_BIND_VERTEX_BUFFER,
|
|
|
|
D3D11_BIND_INDEX_BUFFER,
|
|
|
|
D3D11_BIND_CONSTANT_BUFFER
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11GraphicsBufferS : public IGraphicsBufferS
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
|
|
|
friend struct D3D11CommandQueue;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
size_t m_sz;
|
|
|
|
D3D11GraphicsBufferS(BufferUse use, D3D11Context* ctx, const void* data, size_t stride, size_t count)
|
|
|
|
: m_stride(stride), m_count(count), m_sz(stride * count)
|
|
|
|
{
|
|
|
|
D3D11_SUBRESOURCE_DATA iData = {data};
|
2015-11-21 02:58:56 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateBuffer(&CD3D11_BUFFER_DESC(m_sz, USE_TABLE[int(use)], D3D11_USAGE_IMMUTABLE), &iData, &m_buf));
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
|
|
|
ComPtr<ID3D11Buffer> m_buf;
|
|
|
|
~D3D11GraphicsBufferS() = default;
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11GraphicsBufferD : public IGraphicsBufferD
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
|
|
|
friend struct D3D11CommandQueue;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11CommandQueue* m_q;
|
2015-12-02 22:04:24 +00:00
|
|
|
std::unique_ptr<uint8_t[]> m_cpuBuf;
|
|
|
|
size_t m_cpuSz;
|
|
|
|
int m_validSlots = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11GraphicsBufferD(D3D11CommandQueue* q, BufferUse use, D3D11Context* ctx, size_t stride, size_t count)
|
|
|
|
: m_q(q), m_stride(stride), m_count(count)
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
m_cpuSz = stride * count;
|
|
|
|
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
|
2015-11-06 03:20:58 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
2015-12-02 22:04:24 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateBuffer(&CD3D11_BUFFER_DESC(m_cpuSz, USE_TABLE[int(use)],
|
2015-11-18 06:14:49 +00:00
|
|
|
D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE), nullptr, &m_bufs[i]));
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2015-12-09 05:52:51 +00:00
|
|
|
void update(ID3D11DeviceContext* ctx, int b);
|
2015-11-06 03:20:58 +00:00
|
|
|
public:
|
|
|
|
size_t m_stride;
|
|
|
|
size_t m_count;
|
|
|
|
ComPtr<ID3D11Buffer> m_bufs[3];
|
|
|
|
~D3D11GraphicsBufferD() = default;
|
|
|
|
|
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
2016-12-11 23:29:08 +00:00
|
|
|
};
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11TextureS : public ITextureS
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
|
|
|
size_t m_sz;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11TextureS(D3D11Context* ctx, size_t width, size_t height, size_t mips,
|
2016-01-19 20:30:51 +00:00
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
|
|
|
: m_sz(sz)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-02-17 03:34:21 +00:00
|
|
|
DXGI_FORMAT pfmt;
|
|
|
|
int pxPitchNum = 1;
|
|
|
|
int pxPitchDenom = 1;
|
|
|
|
bool compressed = false;
|
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
pfmt = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
pxPitchNum = 4;
|
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
pfmt = DXGI_FORMAT_R8_UNORM;
|
|
|
|
break;
|
|
|
|
case TextureFormat::DXT1:
|
|
|
|
pfmt = DXGI_FORMAT_BC1_UNORM;
|
|
|
|
compressed = true;
|
|
|
|
pxPitchNum = 1;
|
|
|
|
pxPitchDenom = 2;
|
|
|
|
break;
|
|
|
|
default:
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
2016-02-17 03:34:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CD3D11_TEXTURE2D_DESC desc(pfmt, width, height, 1, mips,
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_IMMUTABLE);
|
|
|
|
|
|
|
|
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
|
|
|
D3D11_SUBRESOURCE_DATA upData[16] = {};
|
|
|
|
for (size_t i=0 ; i<mips && i<16 ; ++i)
|
|
|
|
{
|
|
|
|
upData[i].pSysMem = dataIt;
|
2016-02-17 03:34:21 +00:00
|
|
|
upData[i].SysMemPitch = width * pxPitchNum / pxPitchDenom;
|
2015-11-06 03:20:58 +00:00
|
|
|
upData[i].SysMemSlicePitch = upData[i].SysMemPitch * height;
|
|
|
|
dataIt += upData[i].SysMemSlicePitch;
|
2017-01-29 07:26:42 +00:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&desc, upData, &m_tex));
|
2016-01-19 20:30:51 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateShaderResourceView(m_tex.Get(),
|
2016-02-17 03:34:21 +00:00
|
|
|
&CD3D11_SHADER_RESOURCE_VIEW_DESC(m_tex.Get(), D3D_SRV_DIMENSION_TEXTURE2D, pfmt, 0, mips), &m_srv));
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D11Texture2D> m_tex;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> m_srv;
|
|
|
|
~D3D11TextureS() = default;
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11TextureSA : public ITextureSA
|
2015-11-27 22:14:05 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
size_t m_sz;
|
|
|
|
D3D11TextureSA(D3D11Context* ctx, size_t width, size_t height, size_t layers,
|
2017-01-29 07:26:42 +00:00
|
|
|
size_t mips, TextureFormat fmt, const void* data, size_t sz)
|
2015-11-27 22:14:05 +00:00
|
|
|
: 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;
|
|
|
|
}
|
|
|
|
|
2017-01-29 07:26:42 +00:00
|
|
|
CD3D11_TEXTURE2D_DESC desc(pixelFmt, width, height, layers, mips,
|
2015-11-27 22:14:05 +00:00
|
|
|
D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_IMMUTABLE);
|
|
|
|
|
|
|
|
const uint8_t* dataIt = static_cast<const uint8_t*>(data);
|
2017-01-29 07:26:42 +00:00
|
|
|
std::unique_ptr<D3D11_SUBRESOURCE_DATA[]> upData(new D3D11_SUBRESOURCE_DATA[layers * mips]);
|
|
|
|
D3D11_SUBRESOURCE_DATA* outIt = upData.get();
|
|
|
|
for (size_t i=0 ; i<mips ; ++i)
|
2015-12-08 23:57:16 +00:00
|
|
|
{
|
2017-01-29 07:26:42 +00:00
|
|
|
for (size_t j=0 ; j<layers ; ++j)
|
2015-12-08 23:57:16 +00:00
|
|
|
{
|
2017-01-29 07:26:42 +00:00
|
|
|
outIt->pSysMem = dataIt;
|
|
|
|
outIt->SysMemPitch = width * pixelPitch;
|
|
|
|
outIt->SysMemSlicePitch = outIt->SysMemPitch * height;
|
|
|
|
dataIt += outIt->SysMemSlicePitch;
|
|
|
|
++outIt;
|
2015-12-08 23:57:16 +00:00
|
|
|
}
|
2017-01-29 07:26:42 +00:00
|
|
|
if (width > 1)
|
|
|
|
width /= 2;
|
|
|
|
if (height > 1)
|
|
|
|
height /= 2;
|
2015-11-27 22:14:05 +00:00
|
|
|
}
|
2017-01-29 07:26:42 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&desc, upData.get(), &m_tex));
|
2015-11-27 22:14:05 +00:00
|
|
|
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateShaderResourceView(m_tex.Get(),
|
|
|
|
&CD3D11_SHADER_RESOURCE_VIEW_DESC(m_tex.Get(), D3D_SRV_DIMENSION_TEXTURE2DARRAY, pixelFmt), &m_srv));
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D11Texture2D> m_tex;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> m_srv;
|
|
|
|
~D3D11TextureSA() = default;
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11TextureD : public ITextureD
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
|
|
|
friend struct D3D11CommandQueue;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
|
|
|
D3D11CommandQueue* m_q;
|
2015-12-02 22:04:24 +00:00
|
|
|
std::unique_ptr<uint8_t[]> m_cpuBuf;
|
|
|
|
size_t m_cpuSz;
|
2017-01-22 09:04:37 +00:00
|
|
|
size_t m_pxPitch;
|
2015-12-02 22:04:24 +00:00
|
|
|
int m_validSlots = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11TextureD(D3D11CommandQueue* q, D3D11Context* ctx, size_t width, size_t height, TextureFormat fmt)
|
2017-01-22 09:04:37 +00:00
|
|
|
: m_width(width), m_height(height), m_q(q)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
DXGI_FORMAT pixelFmt;
|
|
|
|
switch (fmt)
|
|
|
|
{
|
|
|
|
case TextureFormat::RGBA8:
|
|
|
|
pixelFmt = DXGI_FORMAT_R8G8B8A8_UNORM;
|
2017-01-22 09:04:37 +00:00
|
|
|
m_pxPitch = 4;
|
2015-12-02 22:04:24 +00:00
|
|
|
break;
|
|
|
|
case TextureFormat::I8:
|
|
|
|
pixelFmt = DXGI_FORMAT_R8_UNORM;
|
2017-01-22 09:04:37 +00:00
|
|
|
m_pxPitch = 1;
|
2015-12-02 22:04:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2016-03-04 23:02:18 +00:00
|
|
|
Log.report(logvisor::Fatal, "unsupported tex format");
|
2015-12-02 22:04:24 +00:00
|
|
|
}
|
|
|
|
|
2017-01-22 09:04:37 +00:00
|
|
|
m_cpuSz = width * height * m_pxPitch;
|
2015-12-02 22:04:24 +00:00
|
|
|
m_cpuBuf.reset(new uint8_t[m_cpuSz]);
|
|
|
|
|
|
|
|
CD3D11_TEXTURE2D_DESC desc(pixelFmt, width, height, 1, 1,
|
2016-03-08 20:28:44 +00:00
|
|
|
D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
|
2015-11-06 03:20:58 +00:00
|
|
|
for (int i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&desc, nullptr, &m_texs[i]));
|
2016-01-19 20:30:51 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateShaderResourceView(m_texs[i].Get(),
|
2015-12-02 22:04:24 +00:00
|
|
|
&CD3D11_SHADER_RESOURCE_VIEW_DESC(m_texs[i].Get(), D3D_SRV_DIMENSION_TEXTURE2D, pixelFmt), &m_srvs[i]));
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-09 05:52:51 +00:00
|
|
|
void update(ID3D11DeviceContext* ctx, int b);
|
2015-11-06 03:20:58 +00:00
|
|
|
public:
|
|
|
|
ComPtr<ID3D11Texture2D> m_texs[3];
|
|
|
|
ComPtr<ID3D11ShaderResourceView> m_srvs[3];
|
|
|
|
~D3D11TextureD() = default;
|
|
|
|
|
|
|
|
void load(const void* data, size_t sz);
|
|
|
|
void* map(size_t sz);
|
|
|
|
void unmap();
|
|
|
|
};
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
class D3D11TextureR : public ITextureR
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
|
|
|
friend struct D3D11CommandQueue;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
size_t m_width = 0;
|
|
|
|
size_t m_height = 0;
|
|
|
|
size_t m_samples = 0;
|
2016-02-26 00:11:09 +00:00
|
|
|
bool m_enableShaderColorBind;
|
|
|
|
bool m_enableShaderDepthBind;
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
void Setup(D3D11Context* ctx, size_t width, size_t height, size_t samples,
|
|
|
|
bool enableShaderColorBind, bool enableShaderDepthBind)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-01-19 20:30:51 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, width, height,
|
2016-02-26 00:11:09 +00:00
|
|
|
1, 1, D3D11_BIND_RENDER_TARGET, D3D11_USAGE_DEFAULT, 0, samples), nullptr, &m_colorTex));
|
2016-01-19 20:30:51 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_D24_UNORM_S8_UINT, width, height,
|
2015-11-06 03:20:58 +00:00
|
|
|
1, 1, D3D11_BIND_DEPTH_STENCIL, D3D11_USAGE_DEFAULT, 0, samples), nullptr, &m_depthTex));
|
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
D3D11_RTV_DIMENSION rtvDim;
|
|
|
|
D3D11_DSV_DIMENSION dsvDim;
|
|
|
|
D3D11_SRV_DIMENSION srvDim;
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
if (samples > 1)
|
|
|
|
{
|
2016-02-26 00:11:09 +00:00
|
|
|
rtvDim = D3D11_RTV_DIMENSION_TEXTURE2DMS;
|
|
|
|
dsvDim = D3D11_DSV_DIMENSION_TEXTURE2DMS;
|
|
|
|
srvDim = D3D11_SRV_DIMENSION_TEXTURE2DMS;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-26 00:11:09 +00:00
|
|
|
rtvDim = D3D11_RTV_DIMENSION_TEXTURE2D;
|
|
|
|
dsvDim = D3D11_DSV_DIMENSION_TEXTURE2D;
|
|
|
|
srvDim = D3D11_SRV_DIMENSION_TEXTURE2D;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateRenderTargetView(m_colorTex.Get(),
|
|
|
|
&CD3D11_RENDER_TARGET_VIEW_DESC(m_colorTex.Get(), rtvDim), &m_rtv));
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDepthStencilView(m_depthTex.Get(),
|
|
|
|
&CD3D11_DEPTH_STENCIL_VIEW_DESC(m_depthTex.Get(), dsvDim), &m_dsv));
|
|
|
|
|
|
|
|
if (enableShaderColorBind)
|
|
|
|
{
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R8G8B8A8_UNORM, width, height,
|
|
|
|
1, 1, D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DEFAULT, 0, samples), nullptr, &m_colorBindTex));
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateShaderResourceView(m_colorBindTex.Get(),
|
|
|
|
&CD3D11_SHADER_RESOURCE_VIEW_DESC(m_colorBindTex.Get(), srvDim), &m_colorSrv));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableShaderDepthBind)
|
|
|
|
{
|
2016-02-26 05:35:12 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateTexture2D(&CD3D11_TEXTURE2D_DESC(DXGI_FORMAT_R24G8_TYPELESS, width, height,
|
2016-02-26 00:11:09 +00:00
|
|
|
1, 1, D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_DEFAULT, 0, samples), nullptr, &m_depthBindTex));
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateShaderResourceView(m_depthBindTex.Get(),
|
2016-02-26 05:35:12 +00:00
|
|
|
&CD3D11_SHADER_RESOURCE_VIEW_DESC(m_depthBindTex.Get(), srvDim, DXGI_FORMAT_R24G8_TYPELESS), &m_depthSrv));
|
2016-02-26 00:11:09 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
D3D11TextureR(D3D11Context* ctx, size_t width, size_t height, size_t samples,
|
|
|
|
bool enableShaderColorBind, bool enableShaderDepthBind)
|
|
|
|
: m_width(width), m_height(height), m_samples(samples),
|
|
|
|
m_enableShaderColorBind(enableShaderColorBind), m_enableShaderDepthBind(enableShaderDepthBind)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
if (samples == 0) m_samples = 1;
|
2016-02-26 00:11:09 +00:00
|
|
|
Setup(ctx, width, height, samples, enableShaderColorBind, enableShaderDepthBind);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
size_t samples() const {return m_samples;}
|
2016-02-26 00:11:09 +00:00
|
|
|
ComPtr<ID3D11Texture2D> m_colorTex;
|
2015-11-06 03:20:58 +00:00
|
|
|
ComPtr<ID3D11RenderTargetView> m_rtv;
|
2016-02-26 00:11:09 +00:00
|
|
|
|
|
|
|
ComPtr<ID3D11Texture2D> m_depthTex;
|
2015-11-06 03:20:58 +00:00
|
|
|
ComPtr<ID3D11DepthStencilView> m_dsv;
|
2016-02-26 00:11:09 +00:00
|
|
|
|
|
|
|
ComPtr<ID3D11Texture2D> m_colorBindTex;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> m_colorSrv;
|
|
|
|
|
|
|
|
ComPtr<ID3D11Texture2D> m_depthBindTex;
|
|
|
|
ComPtr<ID3D11ShaderResourceView> m_depthSrv;
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
~D3D11TextureR() = default;
|
|
|
|
|
|
|
|
void resize(D3D11Context* ctx, size_t width, size_t height)
|
|
|
|
{
|
|
|
|
if (width < 1)
|
|
|
|
width = 1;
|
|
|
|
if (height < 1)
|
|
|
|
height = 1;
|
|
|
|
m_width = width;
|
|
|
|
m_height = height;
|
2016-02-26 00:11:09 +00:00
|
|
|
Setup(ctx, width, height, m_samples, m_enableShaderColorBind, m_enableShaderDepthBind);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const size_t SEMANTIC_SIZE_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
0,
|
2015-11-06 03:20:58 +00:00
|
|
|
12,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
2015-11-06 03:20:58 +00:00
|
|
|
12,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-06 03:20:58 +00:00
|
|
|
4,
|
|
|
|
8,
|
2015-11-27 22:14:05 +00:00
|
|
|
16,
|
|
|
|
16,
|
2015-11-06 03:20:58 +00:00
|
|
|
16
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* SEMANTIC_NAME_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
nullptr,
|
|
|
|
"POSITION",
|
2015-11-06 03:20:58 +00:00
|
|
|
"POSITION",
|
|
|
|
"NORMAL",
|
2015-11-27 22:14:05 +00:00
|
|
|
"NORMAL",
|
|
|
|
"COLOR",
|
2015-11-06 03:20:58 +00:00
|
|
|
"COLOR",
|
|
|
|
"UV",
|
2015-11-27 22:14:05 +00:00
|
|
|
"UV",
|
|
|
|
"WEIGHT",
|
|
|
|
"MODELVIEW"
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const DXGI_FORMAT SEMANTIC_TYPE_TABLE[] =
|
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_UNKNOWN,
|
2015-11-06 03:20:58 +00:00
|
|
|
DXGI_FORMAT_R32G32B32_FLOAT,
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2015-11-06 03:20:58 +00:00
|
|
|
DXGI_FORMAT_R32G32B32_FLOAT,
|
2015-11-27 22:14:05 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT,
|
2015-11-06 03:20:58 +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-06 03:20:58 +00:00
|
|
|
DXGI_FORMAT_R32G32B32A32_FLOAT
|
|
|
|
};
|
|
|
|
|
|
|
|
struct D3D11VertexFormat : IVertexFormat
|
|
|
|
{
|
|
|
|
size_t m_elementCount;
|
|
|
|
std::unique_ptr<D3D11_INPUT_ELEMENT_DESC[]> m_elements;
|
2016-12-11 01:50:26 +00:00
|
|
|
size_t m_stride = 0;
|
|
|
|
size_t m_instStride = 0;
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11VertexFormat(size_t elementCount, const VertexElementDescriptor* elements)
|
|
|
|
: m_elementCount(elementCount),
|
|
|
|
m_elements(new D3D11_INPUT_ELEMENT_DESC[elementCount])
|
|
|
|
{
|
|
|
|
memset(m_elements.get(), 0, elementCount * sizeof(D3D11_INPUT_ELEMENT_DESC));
|
|
|
|
for (size_t i=0 ; i<elementCount ; ++i)
|
|
|
|
{
|
|
|
|
const VertexElementDescriptor* elemin = &elements[i];
|
|
|
|
D3D11_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-06 03:20:58 +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 = D3D11_INPUT_PER_INSTANCE_DATA;
|
|
|
|
elem.InstanceDataStepRate = 1;
|
|
|
|
elem.InputSlot = 1;
|
2016-12-11 01:50:26 +00:00
|
|
|
elem.AlignedByteOffset = m_instStride;
|
|
|
|
m_instStride += SEMANTIC_SIZE_TABLE[semantic];
|
2015-11-27 22:14:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
elem.InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;
|
2016-12-11 01:50:26 +00:00
|
|
|
elem.AlignedByteOffset = m_stride;
|
|
|
|
m_stride += SEMANTIC_SIZE_TABLE[semantic];
|
2015-11-27 22:14:05 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-24 08:34:03 +00:00
|
|
|
static const D3D11_PRIMITIVE_TOPOLOGY PRIMITIVE_TABLE[] =
|
|
|
|
{
|
|
|
|
D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST,
|
|
|
|
D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP
|
|
|
|
};
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
static const D3D11_BLEND BLEND_FACTOR_TABLE[] =
|
|
|
|
{
|
|
|
|
D3D11_BLEND_ZERO,
|
|
|
|
D3D11_BLEND_ONE,
|
|
|
|
D3D11_BLEND_SRC_COLOR,
|
|
|
|
D3D11_BLEND_INV_SRC_COLOR,
|
|
|
|
D3D11_BLEND_DEST_COLOR,
|
|
|
|
D3D11_BLEND_INV_DEST_COLOR,
|
|
|
|
D3D11_BLEND_SRC_ALPHA,
|
|
|
|
D3D11_BLEND_INV_SRC_ALPHA,
|
|
|
|
D3D11_BLEND_DEST_ALPHA,
|
2015-11-27 22:14:05 +00:00
|
|
|
D3D11_BLEND_INV_DEST_ALPHA,
|
|
|
|
D3D11_BLEND_SRC1_COLOR,
|
|
|
|
D3D11_BLEND_INV_SRC1_COLOR
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class D3D11ShaderPipeline : public IShaderPipeline
|
|
|
|
{
|
|
|
|
friend class D3D11DataFactory;
|
2016-12-11 01:50:26 +00:00
|
|
|
friend struct D3D11ShaderDataBinding;
|
2017-01-21 00:19:18 +00:00
|
|
|
const D3D11VertexFormat* m_vtxFmt;
|
2017-03-05 22:59:58 +00:00
|
|
|
D3D11ShareableShader::Token m_vert;
|
|
|
|
D3D11ShareableShader::Token m_pixel;
|
2016-12-11 01:50:26 +00:00
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
D3D11ShaderPipeline(D3D11Context* ctx,
|
|
|
|
D3D11ShareableShader::Token&& vert,
|
|
|
|
D3D11ShareableShader::Token&& pixel,
|
2015-11-06 03:20:58 +00:00
|
|
|
const D3D11VertexFormat* vtxFmt,
|
2016-03-24 08:34:03 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
|
2015-11-06 03:20:58 +00:00
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
2017-03-05 22:59:58 +00:00
|
|
|
: m_vtxFmt(vtxFmt), m_vert(std::move(vert)), m_pixel(std::move(pixel)),
|
|
|
|
m_topology(PRIMITIVE_TABLE[int(prim)])
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2017-03-05 22:59:58 +00:00
|
|
|
m_vert.get().m_shader.As<ID3D11VertexShader>(&m_vShader);
|
|
|
|
m_pixel.get().m_shader.As<ID3D11PixelShader>(&m_pShader);
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-01-19 20:30:51 +00:00
|
|
|
CD3D11_RASTERIZER_DESC rasDesc(D3D11_FILL_SOLID, backfaceCulling ? D3D11_CULL_BACK : D3D11_CULL_NONE, true,
|
|
|
|
D3D11_DEFAULT_DEPTH_BIAS, D3D11_DEFAULT_DEPTH_BIAS_CLAMP, D3D11_DEFAULT_SLOPE_SCALED_DEPTH_BIAS,
|
2015-12-31 06:34:26 +00:00
|
|
|
true, true, false, false);
|
2015-11-06 03:20:58 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateRasterizerState(&rasDesc, &m_rasState));
|
|
|
|
|
|
|
|
CD3D11_DEPTH_STENCIL_DESC dsDesc(D3D11_DEFAULT);
|
|
|
|
dsDesc.DepthEnable = depthTest;
|
|
|
|
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK(depthWrite);
|
2016-08-01 04:32:27 +00:00
|
|
|
dsDesc.DepthFunc = D3D11_COMPARISON_GREATER_EQUAL;
|
2015-11-06 03:20:58 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDepthStencilState(&dsDesc, &m_dsState));
|
|
|
|
|
|
|
|
CD3D11_BLEND_DESC blDesc(D3D11_DEFAULT);
|
2015-11-21 02:58:56 +00:00
|
|
|
blDesc.RenderTarget[0].BlendEnable = (dstFac != BlendFactor::Zero);
|
|
|
|
blDesc.RenderTarget[0].SrcBlend = BLEND_FACTOR_TABLE[int(srcFac)];
|
|
|
|
blDesc.RenderTarget[0].DestBlend = BLEND_FACTOR_TABLE[int(dstFac)];
|
2015-11-06 03:20:58 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateBlendState(&blDesc, &m_blState));
|
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
const auto& vertBuf = m_vert.get().m_vtxBlob;
|
2016-01-19 20:30:51 +00:00
|
|
|
ThrowIfFailed(ctx->m_dev->CreateInputLayout(vtxFmt->m_elements.get(), vtxFmt->m_elementCount,
|
2017-03-05 22:59:58 +00:00
|
|
|
vertBuf->GetBufferPointer(), vertBuf->GetBufferSize(), &m_inLayout));
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
public:
|
|
|
|
ComPtr<ID3D11VertexShader> m_vShader;
|
|
|
|
ComPtr<ID3D11PixelShader> m_pShader;
|
|
|
|
ComPtr<ID3D11RasterizerState> m_rasState;
|
|
|
|
ComPtr<ID3D11DepthStencilState> m_dsState;
|
|
|
|
ComPtr<ID3D11BlendState> m_blState;
|
|
|
|
ComPtr<ID3D11InputLayout> m_inLayout;
|
2016-03-24 08:34:03 +00:00
|
|
|
D3D11_PRIMITIVE_TOPOLOGY m_topology;
|
2015-11-06 03:20:58 +00:00
|
|
|
~D3D11ShaderPipeline() = default;
|
|
|
|
D3D11ShaderPipeline& operator=(const D3D11ShaderPipeline&) = delete;
|
|
|
|
D3D11ShaderPipeline(const D3D11ShaderPipeline&) = delete;
|
|
|
|
|
|
|
|
void bind(ID3D11DeviceContext* ctx)
|
|
|
|
{
|
|
|
|
ctx->VSSetShader(m_vShader.Get(), nullptr, 0);
|
|
|
|
ctx->PSSetShader(m_pShader.Get(), nullptr, 0);
|
|
|
|
ctx->RSSetState(m_rasState.Get());
|
|
|
|
ctx->OMSetDepthStencilState(m_dsState.Get(), 0);
|
|
|
|
ctx->OMSetBlendState(m_blState.Get(), nullptr, 0xffffffff);
|
|
|
|
ctx->IASetInputLayout(m_inLayout.Get());
|
2016-03-24 08:34:03 +00:00
|
|
|
ctx->IASetPrimitiveTopology(m_topology);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
struct D3D11ShaderDataBinding : IShaderDataBindingPriv<D3D11Data>
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11ShaderPipeline* m_pipeline;
|
|
|
|
IGraphicsBuffer* m_vbuf;
|
|
|
|
IGraphicsBuffer* m_instVbuf;
|
|
|
|
IGraphicsBuffer* m_ibuf;
|
|
|
|
std::vector<IGraphicsBuffer*> m_ubufs;
|
2016-04-04 19:33:42 +00:00
|
|
|
std::unique_ptr<UINT[]> m_ubufFirstConsts;
|
|
|
|
std::unique_ptr<UINT[]> m_ubufNumConsts;
|
|
|
|
std::unique_ptr<bool[]> m_pubufs;
|
2017-01-21 00:19:18 +00:00
|
|
|
std::vector<ITexture*> m_texs;
|
2016-12-11 01:50:26 +00:00
|
|
|
UINT m_baseOffsets[2];
|
2015-12-18 04:55:06 +00:00
|
|
|
|
2017-01-20 03:52:40 +00:00
|
|
|
D3D11ShaderDataBinding(D3D11Data* d,
|
|
|
|
D3D11Context* ctx,
|
2015-11-06 03:20:58 +00:00
|
|
|
IShaderPipeline* pipeline,
|
2015-11-27 22:14:05 +00:00
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbuf, IGraphicsBuffer* ibuf,
|
2016-04-04 19:33:42 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs, const PipelineStage* ubufStages,
|
2016-03-30 20:42:36 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2016-12-11 01:50:26 +00:00
|
|
|
size_t texCount, ITexture** texs, size_t baseVert, size_t baseInst)
|
2017-01-20 03:52:40 +00:00
|
|
|
: IShaderDataBindingPriv(d),
|
2017-01-21 00:19:18 +00:00
|
|
|
m_pipeline(static_cast<D3D11ShaderPipeline*>(pipeline)),
|
|
|
|
m_vbuf(vbuf),
|
|
|
|
m_instVbuf(instVbuf),
|
|
|
|
m_ibuf(ibuf)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
m_ubufs.reserve(ubufCount);
|
|
|
|
m_texs.reserve(texCount);
|
|
|
|
|
2016-12-11 01:50:26 +00:00
|
|
|
m_baseOffsets[0] = UINT(baseVert * m_pipeline->m_vtxFmt->m_stride);
|
|
|
|
m_baseOffsets[1] = UINT(baseInst * m_pipeline->m_vtxFmt->m_instStride);
|
|
|
|
|
2016-04-04 19:33:42 +00:00
|
|
|
if (ubufStages)
|
|
|
|
{
|
|
|
|
m_pubufs.reset(new bool[ubufCount]);
|
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
|
|
|
m_pubufs[i] = ubufStages[i] == PipelineStage::Fragment;
|
|
|
|
}
|
2016-03-30 20:42:36 +00:00
|
|
|
if (ubufOffs && ubufSizes)
|
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
m_ubufFirstConsts.reset(new UINT[ubufCount]);
|
|
|
|
m_ubufNumConsts.reset(new UINT[ubufCount]);
|
2016-03-30 20:42:36 +00:00
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (ubufOffs[i] % 256)
|
|
|
|
Log.report(logvisor::Fatal, "non-256-byte-aligned uniform-offset %d provided to newShaderDataBinding", int(i));
|
|
|
|
#endif
|
2016-04-04 19:33:42 +00:00
|
|
|
m_ubufFirstConsts[i] = ubufOffs[i] / 16;
|
|
|
|
m_ubufNumConsts[i] = ((ubufSizes[i] + 255) & ~255) / 16;
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
for (size_t i=0 ; i<ubufCount ; ++i)
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (!ubufs[i])
|
|
|
|
Log.report(logvisor::Fatal, "null uniform-buffer %d provided to newShaderDataBinding", int(i));
|
|
|
|
#endif
|
2017-01-21 00:19:18 +00:00
|
|
|
m_ubufs.push_back(ubufs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
for (size_t i=0 ; i<texCount ; ++i)
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
m_texs.push_back(texs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
void bind(ID3D11DeviceContext1* ctx, int b)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
m_pipeline->bind(ctx);
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
ID3D11Buffer* bufs[2] = {};
|
|
|
|
UINT strides[2] = {};
|
|
|
|
|
|
|
|
if (m_vbuf)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
if (m_vbuf->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_vbuf);
|
2015-11-27 22:14:05 +00:00
|
|
|
bufs[0] = cbuf->m_bufs[b].Get();
|
|
|
|
strides[0] = UINT(cbuf->m_stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_vbuf);
|
2015-11-27 22:14:05 +00:00
|
|
|
bufs[0] = cbuf->m_buf.Get();
|
|
|
|
strides[0] = UINT(cbuf->m_stride);
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
|
|
|
|
if (m_instVbuf)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2015-11-27 22:14:05 +00:00
|
|
|
if (m_instVbuf->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_instVbuf);
|
2015-11-27 22:14:05 +00:00
|
|
|
bufs[1] = cbuf->m_bufs[b].Get();
|
|
|
|
strides[1] = UINT(cbuf->m_stride);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_instVbuf);
|
2015-11-27 22:14:05 +00:00
|
|
|
bufs[1] = cbuf->m_buf.Get();
|
|
|
|
strides[1] = UINT(cbuf->m_stride);
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 01:50:26 +00:00
|
|
|
ctx->IASetVertexBuffers(0, 2, bufs, strides, m_baseOffsets);
|
2015-11-27 22:14:05 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
if (m_ibuf)
|
|
|
|
{
|
|
|
|
if (m_ibuf->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_ibuf);
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->IASetIndexBuffer(cbuf->m_bufs[b].Get(), DXGI_FORMAT_R32_UINT, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_ibuf);
|
2015-11-06 03:20:58 +00:00
|
|
|
ctx->IASetIndexBuffer(cbuf->m_buf.Get(), DXGI_FORMAT_R32_UINT, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 23:29:08 +00:00
|
|
|
if (m_ubufs.size())
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
if (m_ubufFirstConsts)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
ID3D11Buffer* constBufs[8] = {};
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->VSSetConstantBuffers(0, m_ubufs.size(), constBufs);
|
|
|
|
for (int i=0 ; i<8 && i<m_ubufs.size() ; ++i)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
if (m_pubufs && m_pubufs[i])
|
|
|
|
continue;
|
2016-03-30 20:42:36 +00:00
|
|
|
if (m_ubufs[i]->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_ubufs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
constBufs[i] = cbuf->m_bufs[b].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_ubufs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
constBufs[i] = cbuf->m_buf.Get();
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->VSSetConstantBuffers1(0, m_ubufs.size(), constBufs, m_ubufFirstConsts.get(), m_ubufNumConsts.get());
|
2016-04-04 19:33:42 +00:00
|
|
|
|
|
|
|
if (m_pubufs)
|
|
|
|
{
|
|
|
|
ID3D11Buffer* constBufs[8] = {};
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->PSSetConstantBuffers(0, m_ubufs.size(), constBufs);
|
|
|
|
for (int i=0 ; i<8 && i<m_ubufs.size() ; ++i)
|
2016-04-04 19:33:42 +00:00
|
|
|
{
|
|
|
|
if (!m_pubufs[i])
|
|
|
|
continue;
|
|
|
|
if (m_ubufs[i]->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_ubufs[i]);
|
2016-04-04 19:33:42 +00:00
|
|
|
constBufs[i] = cbuf->m_bufs[b].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_ubufs[i]);
|
2016-04-04 19:33:42 +00:00
|
|
|
constBufs[i] = cbuf->m_buf.Get();
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->PSSetConstantBuffers1(0, m_ubufs.size(), constBufs, m_ubufFirstConsts.get(), m_ubufNumConsts.get());
|
2016-04-04 19:33:42 +00:00
|
|
|
}
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
ID3D11Buffer* constBufs[8] = {};
|
2016-12-11 23:29:08 +00:00
|
|
|
for (int i=0 ; i<8 && i<m_ubufs.size() ; ++i)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-04-04 19:33:42 +00:00
|
|
|
if (m_pubufs && m_pubufs[i])
|
|
|
|
continue;
|
2016-03-30 20:42:36 +00:00
|
|
|
if (m_ubufs[i]->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_ubufs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
constBufs[i] = cbuf->m_bufs[b].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_ubufs[i]);
|
2016-03-30 20:42:36 +00:00
|
|
|
constBufs[i] = cbuf->m_buf.Get();
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->VSSetConstantBuffers(0, m_ubufs.size(), constBufs);
|
2016-04-04 19:33:42 +00:00
|
|
|
|
|
|
|
if (m_pubufs)
|
|
|
|
{
|
|
|
|
ID3D11Buffer* constBufs[8] = {};
|
2016-12-11 23:29:08 +00:00
|
|
|
for (int i=0 ; i<8 && i<m_ubufs.size() ; ++i)
|
2016-04-04 19:33:42 +00:00
|
|
|
{
|
|
|
|
if (!m_pubufs[i])
|
|
|
|
continue;
|
|
|
|
if (m_ubufs[i]->dynamic())
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferD* cbuf = static_cast<D3D11GraphicsBufferD*>(m_ubufs[i]);
|
2016-04-04 19:33:42 +00:00
|
|
|
constBufs[i] = cbuf->m_bufs[b].Get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11GraphicsBufferS* cbuf = static_cast<D3D11GraphicsBufferS*>(m_ubufs[i]);
|
2016-04-04 19:33:42 +00:00
|
|
|
constBufs[i] = cbuf->m_buf.Get();
|
|
|
|
}
|
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->PSSetConstantBuffers(0, m_ubufs.size(), constBufs);
|
2016-04-04 19:33:42 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 23:29:08 +00:00
|
|
|
if (m_texs.size())
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-08-01 04:32:27 +00:00
|
|
|
ID3D11ShaderResourceView* srvs[8] = {};
|
2016-12-11 23:29:08 +00:00
|
|
|
for (int i=0 ; i<8 && i<m_texs.size() ; ++i)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-08-01 04:32:27 +00:00
|
|
|
if (m_texs[i])
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-08-01 04:32:27 +00:00
|
|
|
switch (m_texs[i]->type())
|
|
|
|
{
|
|
|
|
case TextureType::Dynamic:
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureD* ctex = static_cast<D3D11TextureD*>(m_texs[i]);
|
2016-08-01 04:32:27 +00:00
|
|
|
srvs[i] = ctex->m_srvs[b].Get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TextureType::Static:
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureS* ctex = static_cast<D3D11TextureS*>(m_texs[i]);
|
2016-08-01 04:32:27 +00:00
|
|
|
srvs[i] = ctex->m_srv.Get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TextureType::StaticArray:
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureSA* ctex = static_cast<D3D11TextureSA*>(m_texs[i]);
|
2016-08-01 04:32:27 +00:00
|
|
|
srvs[i] = ctex->m_srv.Get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TextureType::Render:
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureR* ctex = static_cast<D3D11TextureR*>(m_texs[i]);
|
2016-08-01 04:32:27 +00:00
|
|
|
srvs[i] = ctex->m_colorSrv.Get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
ctx->PSSetShaderResources(0, m_texs.size(), srvs);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct D3D11CommandQueue : IGraphicsCommandQueue
|
|
|
|
{
|
2015-11-21 02:58:56 +00:00
|
|
|
Platform platform() const {return IGraphicsDataFactory::Platform::D3D11;}
|
2015-11-18 06:14:49 +00:00
|
|
|
const SystemChar* platformName() const {return _S("D3D11");}
|
2015-11-06 03:20:58 +00:00
|
|
|
D3D11Context* m_ctx;
|
|
|
|
D3D11Context::Window* m_windowCtx;
|
|
|
|
IGraphicsContext* m_parent;
|
|
|
|
ComPtr<ID3D11DeviceContext1> m_deferredCtx;
|
|
|
|
|
|
|
|
size_t m_fillBuf = 0;
|
|
|
|
size_t m_completeBuf = 0;
|
|
|
|
size_t m_drawBuf = 0;
|
|
|
|
bool m_running = true;
|
|
|
|
|
|
|
|
std::mutex m_mt;
|
|
|
|
std::condition_variable m_cv;
|
|
|
|
std::mutex m_initmt;
|
|
|
|
std::condition_variable m_initcv;
|
|
|
|
std::unique_lock<std::mutex> m_initlk;
|
|
|
|
std::thread m_thr;
|
|
|
|
|
2016-12-11 23:29:08 +00:00
|
|
|
struct CommandList
|
|
|
|
{
|
|
|
|
ComPtr<ID3D11CommandList> list;
|
2017-01-21 00:19:18 +00:00
|
|
|
std::vector<IShaderDataBindingPriv<D3D11Data>::Token> resTokens;
|
|
|
|
D3D11TextureR* workDoPresent = nullptr;
|
2016-12-11 23:29:08 +00:00
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
list.Reset();
|
2017-01-20 03:52:40 +00:00
|
|
|
resTokens.clear();
|
2017-01-21 00:19:18 +00:00
|
|
|
workDoPresent = nullptr;
|
2016-12-11 23:29:08 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
CommandList m_cmdLists[3];
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-12-11 01:50:26 +00:00
|
|
|
std::recursive_mutex m_dynamicLock;
|
2015-12-09 05:52:51 +00:00
|
|
|
void ProcessDynamicLoads(ID3D11DeviceContext* ctx);
|
2015-11-06 03:20:58 +00:00
|
|
|
static void RenderingWorker(D3D11CommandQueue* self)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(self->m_initmt);
|
|
|
|
}
|
|
|
|
self->m_initcv.notify_one();
|
|
|
|
while (self->m_running)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(self->m_mt);
|
|
|
|
self->m_cv.wait(lk);
|
|
|
|
if (!self->m_running)
|
|
|
|
break;
|
|
|
|
self->m_drawBuf = self->m_completeBuf;
|
2016-12-11 23:29:08 +00:00
|
|
|
auto& CmdList = self->m_cmdLists[self->m_drawBuf];
|
2015-11-07 01:43:12 +00:00
|
|
|
|
2015-12-09 05:52:51 +00:00
|
|
|
self->ProcessDynamicLoads(self->m_ctx->m_devCtx.Get());
|
2015-12-01 00:33:14 +00:00
|
|
|
|
2015-11-07 01:43:12 +00:00
|
|
|
if (self->m_texResizes.size())
|
|
|
|
{
|
|
|
|
for (const auto& resize : self->m_texResizes)
|
|
|
|
resize.first->resize(self->m_ctx, resize.second.first, resize.second.second);
|
|
|
|
self->m_texResizes.clear();
|
2016-12-11 23:29:08 +00:00
|
|
|
CmdList.reset();
|
2015-11-07 01:43:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->m_windowCtx->m_needsFSTransition)
|
|
|
|
{
|
|
|
|
if (self->m_windowCtx->m_fs)
|
|
|
|
{
|
|
|
|
self->m_windowCtx->m_swapChain->SetFullscreenState(true, nullptr);
|
|
|
|
self->m_windowCtx->m_swapChain->ResizeTarget(&self->m_windowCtx->m_fsdesc);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
self->m_windowCtx->m_swapChain->SetFullscreenState(false, nullptr);
|
|
|
|
|
|
|
|
self->m_windowCtx->m_needsFSTransition = false;
|
2016-12-11 23:29:08 +00:00
|
|
|
CmdList.reset();
|
2015-11-07 01:43:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->m_windowCtx->m_needsResize)
|
|
|
|
{
|
2015-12-01 00:33:14 +00:00
|
|
|
self->m_windowCtx->m_swapChain->ResizeBuffers(2,
|
|
|
|
self->m_windowCtx->width, self->m_windowCtx->height,
|
2015-11-07 01:43:12 +00:00
|
|
|
DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH);
|
|
|
|
self->m_windowCtx->m_needsResize = false;
|
2016-12-11 23:29:08 +00:00
|
|
|
CmdList.reset();
|
2015-11-07 01:43:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2015-11-07 01:43:12 +00:00
|
|
|
|
2016-12-11 23:29:08 +00:00
|
|
|
auto& CmdList = self->m_cmdLists[self->m_drawBuf];
|
|
|
|
ID3D11CommandList* list = CmdList.list.Get();
|
2015-11-06 03:20:58 +00:00
|
|
|
self->m_ctx->m_devCtx->ExecuteCommandList(list, false);
|
2015-11-07 01:43:12 +00:00
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureR* csource = CmdList.workDoPresent;
|
2015-11-07 01:43:12 +00:00
|
|
|
if (csource)
|
|
|
|
{
|
|
|
|
ComPtr<ID3D11Texture2D> dest;
|
|
|
|
ThrowIfFailed(self->m_windowCtx->m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), &dest));
|
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
ID3D11Texture2D* src = csource->m_colorTex.Get();
|
2015-11-07 01:43:12 +00:00
|
|
|
if (csource->m_samples > 1)
|
|
|
|
self->m_ctx->m_devCtx->ResolveSubresource(dest.Get(), 0, src, 0, DXGI_FORMAT_R8G8B8A8_UNORM);
|
|
|
|
else
|
|
|
|
self->m_ctx->m_devCtx->CopyResource(dest.Get(), src);
|
2016-01-19 20:30:51 +00:00
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
self->m_windowCtx->m_swapChain->Present(1, 0);
|
2015-11-07 01:43:12 +00:00
|
|
|
}
|
2016-12-11 23:29:08 +00:00
|
|
|
|
|
|
|
CmdList.reset();
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
D3D11CommandQueue(D3D11Context* ctx, D3D11Context::Window* windowCtx, IGraphicsContext* parent)
|
|
|
|
: m_ctx(ctx), m_windowCtx(windowCtx), m_parent(parent),
|
|
|
|
m_initlk(m_initmt),
|
|
|
|
m_thr(RenderingWorker, this)
|
|
|
|
{
|
|
|
|
m_initcv.wait(m_initlk);
|
|
|
|
m_initlk.unlock();
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateDeferredContext1(0, &m_deferredCtx));
|
|
|
|
}
|
|
|
|
|
2015-12-05 01:03:44 +00:00
|
|
|
void stopRenderer()
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
|
|
|
m_running = false;
|
|
|
|
m_cv.notify_one();
|
|
|
|
m_thr.join();
|
|
|
|
}
|
|
|
|
|
2015-12-05 01:03:44 +00:00
|
|
|
~D3D11CommandQueue()
|
|
|
|
{
|
|
|
|
if (m_running) stopRenderer();
|
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
void setShaderDataBinding(IShaderDataBinding* binding)
|
|
|
|
{
|
|
|
|
D3D11ShaderDataBinding* cbind = static_cast<D3D11ShaderDataBinding*>(binding);
|
|
|
|
cbind->bind(m_deferredCtx.Get(), m_fillBuf);
|
2017-01-20 03:52:40 +00:00
|
|
|
m_cmdLists[m_fillBuf].resTokens.push_back(cbind->lock());
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
ID3D11SamplerState* samp[] = {m_ctx->m_ss.Get()};
|
|
|
|
m_deferredCtx->PSSetSamplers(0, 1, samp);
|
|
|
|
}
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureR* m_boundTarget;
|
2015-11-06 03:20:58 +00:00
|
|
|
void setRenderTarget(ITextureR* target)
|
|
|
|
{
|
|
|
|
D3D11TextureR* ctarget = static_cast<D3D11TextureR*>(target);
|
|
|
|
ID3D11RenderTargetView* view[] = {ctarget->m_rtv.Get()};
|
|
|
|
m_deferredCtx->OMSetRenderTargets(1, view, ctarget->m_dsv.Get());
|
2017-01-21 00:19:18 +00:00
|
|
|
m_boundTarget = ctarget;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 02:25:55 +00:00
|
|
|
void setViewport(const SWindowRect& rect, float znear, float zfar)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
if (m_boundTarget)
|
|
|
|
{
|
|
|
|
int boundHeight = m_boundTarget->m_height;
|
|
|
|
D3D11_VIEWPORT vp = {FLOAT(rect.location[0]), FLOAT(boundHeight - rect.location[1] - rect.size[1]),
|
|
|
|
FLOAT(rect.size[0]), FLOAT(rect.size[1]), znear, zfar};
|
|
|
|
m_deferredCtx->RSSetViewports(1, &vp);
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-27 22:14:05 +00:00
|
|
|
void setScissor(const SWindowRect& rect)
|
|
|
|
{
|
2015-12-31 06:34:26 +00:00
|
|
|
if (m_boundTarget)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
int boundHeight = m_boundTarget->m_height;
|
|
|
|
D3D11_RECT d3drect = {LONG(rect.location[0]), LONG(boundHeight - rect.location[1] - rect.size[1]),
|
|
|
|
LONG(rect.location[0] + rect.size[0]), LONG(boundHeight - rect.location[1])};
|
2015-12-31 06:34:26 +00:00
|
|
|
m_deferredCtx->RSSetScissorRects(1, &d3drect);
|
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
std::unordered_map<D3D11TextureR*, std::pair<size_t, size_t>> m_texResizes;
|
2015-11-06 03:20:58 +00:00
|
|
|
void resizeRenderTexture(ITextureR* tex, size_t width, size_t height)
|
|
|
|
{
|
|
|
|
D3D11TextureR* ctex = static_cast<D3D11TextureR*>(tex);
|
2015-11-07 01:43:12 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
2017-01-21 00:19:18 +00:00
|
|
|
m_texResizes[ctex] = std::make_pair(width, height);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-21 01:06:08 +00:00
|
|
|
void schedulePostFrameHandler(std::function<void(void)>&& func)
|
|
|
|
{
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
float m_clearColor[4] = {0.0,0.0,0.0,1.0};
|
|
|
|
void setClearColor(const float rgba[4])
|
|
|
|
{
|
|
|
|
m_clearColor[0] = rgba[0];
|
|
|
|
m_clearColor[1] = rgba[1];
|
|
|
|
m_clearColor[2] = rgba[2];
|
|
|
|
m_clearColor[3] = rgba[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
void clearTarget(bool render=true, bool depth=true)
|
|
|
|
{
|
|
|
|
if (!m_boundTarget)
|
|
|
|
return;
|
|
|
|
if (render)
|
|
|
|
m_deferredCtx->ClearRenderTargetView(m_boundTarget->m_rtv.Get(), m_clearColor);
|
|
|
|
if (depth)
|
2016-04-03 05:20:21 +00:00
|
|
|
m_deferredCtx->ClearDepthStencilView(m_boundTarget->m_dsv.Get(), D3D11_CLEAR_DEPTH, 0.0f, 0);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw(size_t start, size_t count)
|
|
|
|
{
|
2015-11-07 01:43:12 +00:00
|
|
|
m_deferredCtx->Draw(count, start);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawIndexed(size_t start, size_t count)
|
|
|
|
{
|
2015-11-07 01:43:12 +00:00
|
|
|
m_deferredCtx->DrawIndexed(count, start, 0);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void drawInstances(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
|
|
|
m_deferredCtx->DrawInstanced(count, instCount, start, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void drawInstancesIndexed(size_t start, size_t count, size_t instCount)
|
|
|
|
{
|
|
|
|
m_deferredCtx->DrawIndexedInstanced(count, instCount, start, 0, 0);
|
|
|
|
}
|
|
|
|
|
2016-02-26 00:11:09 +00:00
|
|
|
void resolveBindTexture(ITextureR* texture, const SWindowRect& rect, bool tlOrigin, bool color, bool depth)
|
|
|
|
{
|
|
|
|
const D3D11TextureR* tex = static_cast<const D3D11TextureR*>(texture);
|
|
|
|
if (color && tex->m_enableShaderColorBind)
|
|
|
|
{
|
|
|
|
if (tex->m_samples > 1)
|
|
|
|
{
|
|
|
|
m_deferredCtx->CopyResource(tex->m_colorBindTex.Get(), tex->m_colorTex.Get());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-02-27 02:02:54 +00:00
|
|
|
SWindowRect intersectRect = rect.intersect(SWindowRect(0, 0, tex->m_width, tex->m_height));
|
|
|
|
int y = tlOrigin ? intersectRect.location[1] : (tex->m_height - intersectRect.size[1] - intersectRect.location[1]);
|
|
|
|
D3D11_BOX box = {UINT(intersectRect.location[0]), UINT(y), 0,
|
|
|
|
UINT(intersectRect.location[0] + intersectRect.size[0]), UINT(y + intersectRect.size[1]), 1};
|
2016-02-26 20:16:21 +00:00
|
|
|
m_deferredCtx->CopySubresourceRegion1(tex->m_colorBindTex.Get(), 0, box.left, box.top, 0,
|
2016-02-26 00:11:09 +00:00
|
|
|
tex->m_colorTex.Get(), 0, &box, D3D11_COPY_DISCARD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (depth && tex->m_enableShaderDepthBind)
|
|
|
|
{
|
|
|
|
m_deferredCtx->CopyResource(tex->m_depthBindTex.Get(), tex->m_depthTex.Get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-21 00:19:18 +00:00
|
|
|
D3D11TextureR* m_doPresent;
|
2015-11-06 03:20:58 +00:00
|
|
|
void resolveDisplay(ITextureR* source)
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
m_doPresent = static_cast<D3D11TextureR*>(source);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-02 22:04:24 +00:00
|
|
|
void execute();
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
|
|
|
|
2015-12-09 05:52:51 +00:00
|
|
|
void D3D11GraphicsBufferD::update(ID3D11DeviceContext* ctx, int b)
|
2015-12-02 22:04:24 +00:00
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validSlots) == 0)
|
|
|
|
{
|
|
|
|
ID3D11Buffer* res = m_bufs[b].Get();
|
|
|
|
D3D11_MAPPED_SUBRESOURCE d;
|
2016-12-11 06:18:41 +00:00
|
|
|
if (SUCCEEDED(ctx->Map(res, 0, D3D11_MAP_WRITE_DISCARD, 0, &d)))
|
|
|
|
{
|
|
|
|
memcpy(d.pData, m_cpuBuf.get(), m_cpuSz);
|
|
|
|
ctx->Unmap(res, 0);
|
|
|
|
}
|
2015-12-02 22:04:24 +00:00
|
|
|
m_validSlots |= slot;
|
|
|
|
}
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
void D3D11GraphicsBufferD::load(const void* data, size_t sz)
|
|
|
|
{
|
2016-12-11 01:50:26 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> lk(m_q->m_dynamicLock);
|
2015-12-02 22:04:24 +00:00
|
|
|
size_t bufSz = std::min(sz, m_cpuSz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validSlots = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
void* D3D11GraphicsBufferD::map(size_t sz)
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
if (sz > m_cpuSz)
|
|
|
|
return nullptr;
|
2015-12-09 21:02:04 +00:00
|
|
|
m_q->m_dynamicLock.lock();
|
2015-12-02 22:04:24 +00:00
|
|
|
return m_cpuBuf.get();
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
void D3D11GraphicsBufferD::unmap()
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
m_validSlots = 0;
|
2015-12-09 21:02:04 +00:00
|
|
|
m_q->m_dynamicLock.unlock();
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-09 05:52:51 +00:00
|
|
|
void D3D11TextureD::update(ID3D11DeviceContext* ctx, int b)
|
2015-12-02 22:04:24 +00:00
|
|
|
{
|
|
|
|
int slot = 1 << b;
|
|
|
|
if ((slot & m_validSlots) == 0)
|
|
|
|
{
|
|
|
|
ID3D11Texture2D* res = m_texs[b].Get();
|
|
|
|
D3D11_MAPPED_SUBRESOURCE d;
|
2015-12-09 05:52:51 +00:00
|
|
|
ctx->Map(res, 0, D3D11_MAP_WRITE_DISCARD, 0, &d);
|
2017-01-22 09:04:37 +00:00
|
|
|
size_t rowSz = m_pxPitch * m_width;
|
|
|
|
for (size_t i=0 ; i<m_cpuSz ; i+=rowSz, reinterpret_cast<uint8_t*&>(d.pData)+=d.RowPitch)
|
|
|
|
memmove(d.pData, m_cpuBuf.get() + i, rowSz);
|
2015-12-09 05:52:51 +00:00
|
|
|
ctx->Unmap(res, 0);
|
2015-12-02 22:04:24 +00:00
|
|
|
m_validSlots |= slot;
|
|
|
|
}
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
void D3D11TextureD::load(const void* data, size_t sz)
|
|
|
|
{
|
2016-12-11 01:50:26 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> lk(m_q->m_dynamicLock);
|
2015-12-02 22:04:24 +00:00
|
|
|
size_t bufSz = std::min(sz, m_cpuSz);
|
|
|
|
memcpy(m_cpuBuf.get(), data, bufSz);
|
|
|
|
m_validSlots = 0;
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
void* D3D11TextureD::map(size_t sz)
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
if (sz > m_cpuSz)
|
|
|
|
return nullptr;
|
2015-12-09 21:02:04 +00:00
|
|
|
m_q->m_dynamicLock.lock();
|
2015-12-02 22:04:24 +00:00
|
|
|
return m_cpuBuf.get();
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
void D3D11TextureD::unmap()
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
m_validSlots = 0;
|
2015-12-09 21:02:04 +00:00
|
|
|
m_q->m_dynamicLock.unlock();
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class D3D11DataFactory : public ID3DDataFactory
|
|
|
|
{
|
2015-12-02 22:04:24 +00:00
|
|
|
friend struct D3D11CommandQueue;
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsContext* m_parent;
|
2015-12-14 19:19:29 +00:00
|
|
|
static thread_local D3D11Data* m_deferredData;
|
2015-11-06 03:20:58 +00:00
|
|
|
struct D3D11Context* m_ctx;
|
2015-12-02 22:04:24 +00:00
|
|
|
std::unordered_set<D3D11Data*> m_committedData;
|
2016-12-11 01:50:26 +00:00
|
|
|
std::unordered_set<D3D11Pool*> m_committedPools;
|
2015-12-14 19:19:29 +00:00
|
|
|
std::mutex m_committedMutex;
|
2017-03-05 22:59:58 +00:00
|
|
|
std::unordered_map<uint64_t, std::unique_ptr<D3D11ShareableShader>> m_sharedShaders;
|
|
|
|
std::unordered_map<uint64_t, uint64_t> m_sourceToBinary;
|
2016-02-24 20:52:31 +00:00
|
|
|
uint32_t m_sampleCount;
|
2015-12-05 01:03:44 +00:00
|
|
|
|
|
|
|
void destroyData(IGraphicsData* d)
|
|
|
|
{
|
2015-12-14 19:19:29 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2015-12-05 01:03:44 +00:00
|
|
|
D3D11Data* data = static_cast<D3D11Data*>(d);
|
|
|
|
m_committedData.erase(data);
|
2017-01-20 03:52:40 +00:00
|
|
|
data->decrement();
|
2015-12-05 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroyAllData()
|
|
|
|
{
|
2015-12-14 19:19:29 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2017-01-20 03:52:40 +00:00
|
|
|
for (D3D11Data* data : m_committedData)
|
|
|
|
data->decrement();
|
2016-12-11 01:50:26 +00:00
|
|
|
for (IGraphicsBufferPool* pool : m_committedPools)
|
2016-12-11 23:29:08 +00:00
|
|
|
delete static_cast<D3D11Pool*>(pool);
|
2015-12-05 01:03:44 +00:00
|
|
|
m_committedData.clear();
|
2016-12-11 01:50:26 +00:00
|
|
|
m_committedPools.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void destroyPool(IGraphicsBufferPool* p)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
D3D11Pool* pool = static_cast<D3D11Pool*>(p);
|
|
|
|
m_committedPools.erase(pool);
|
2016-12-11 23:29:08 +00:00
|
|
|
delete pool;
|
2015-12-09 05:23:15 +00:00
|
|
|
}
|
|
|
|
|
2016-12-11 01:50:26 +00:00
|
|
|
IGraphicsBufferD* newPoolBuffer(IGraphicsBufferPool* p, BufferUse use,
|
|
|
|
size_t stride, size_t count)
|
|
|
|
{
|
|
|
|
D3D11CommandQueue* q = static_cast<D3D11CommandQueue*>(m_parent->getCommandQueue());
|
|
|
|
D3D11Pool* pool = static_cast<D3D11Pool*>(p);
|
|
|
|
D3D11GraphicsBufferD* retval = new D3D11GraphicsBufferD(q, use, m_ctx, stride, count);
|
2017-01-21 00:19:18 +00:00
|
|
|
pool->m_DBufs.emplace(std::make_pair(retval, retval));
|
2016-12-11 01:50:26 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-12-11 06:18:41 +00:00
|
|
|
void deletePoolBuffer(IGraphicsBufferPool* p, IGraphicsBufferD* buf)
|
|
|
|
{
|
|
|
|
D3D11Pool* pool = static_cast<D3D11Pool*>(p);
|
2016-12-11 23:29:08 +00:00
|
|
|
pool->m_DBufs.erase(static_cast<D3D11GraphicsBufferD*>(buf));
|
2016-12-11 06:18:41 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
public:
|
2016-02-24 20:52:31 +00:00
|
|
|
D3D11DataFactory(IGraphicsContext* parent, D3D11Context* ctx, uint32_t sampleCount)
|
|
|
|
: m_parent(parent), m_ctx(ctx), m_sampleCount(sampleCount)
|
2015-11-06 03:20:58 +00:00
|
|
|
{}
|
2015-12-05 01:03:44 +00:00
|
|
|
~D3D11DataFactory() {destroyAllData();}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-11-21 02:58:56 +00:00
|
|
|
Platform platform() const {return Platform::D3D11;}
|
2015-11-18 06:14:49 +00:00
|
|
|
const SystemChar* platformName() const {return _S("D3D11");}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
class Context : public ID3DDataFactory::Context
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-03-30 20:42:36 +00:00
|
|
|
friend class D3D11DataFactory;
|
|
|
|
D3D11DataFactory& m_parent;
|
|
|
|
Context(D3D11DataFactory& parent) : m_parent(parent) {}
|
|
|
|
public:
|
|
|
|
Platform platform() const {return Platform::D3D11;}
|
|
|
|
const SystemChar* platformName() const {return _S("D3D11");}
|
|
|
|
|
|
|
|
IGraphicsBufferS* newStaticBuffer(BufferUse use, const void* data, size_t stride, size_t count)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11GraphicsBufferS* retval = new D3D11GraphicsBufferS(use, m_parent.m_ctx, data, stride, count);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_SBufs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
IGraphicsBufferD* newDynamicBuffer(BufferUse use, size_t stride, size_t count)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11CommandQueue* q = static_cast<D3D11CommandQueue*>(m_parent.m_parent->getCommandQueue());
|
|
|
|
D3D11GraphicsBufferD* retval = new D3D11GraphicsBufferD(q, use, m_parent.m_ctx, stride, count);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_DBufs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2016-02-17 03:34:21 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
ITextureS* newStaticTexture(size_t width, size_t height, size_t mips, TextureFormat fmt,
|
|
|
|
const void* data, size_t sz)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11TextureS* retval = new D3D11TextureS(m_parent.m_ctx, width, height, mips, fmt, data, sz);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_STexs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-07 01:43:12 +00:00
|
|
|
|
2017-01-29 07:26:42 +00:00
|
|
|
ITextureSA* newStaticArrayTexture(size_t width, size_t height, size_t layers, size_t mips,
|
|
|
|
TextureFormat fmt, const void* data, size_t sz)
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2017-01-29 07:26:42 +00:00
|
|
|
D3D11TextureSA* retval = new D3D11TextureSA(m_parent.m_ctx, width, height, layers, mips, fmt, data, sz);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_SATexs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-27 22:14:05 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
ITextureD* newDynamicTexture(size_t width, size_t height, TextureFormat fmt)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11CommandQueue* q = static_cast<D3D11CommandQueue*>(m_parent.m_parent->getCommandQueue());
|
|
|
|
D3D11TextureD* retval = new D3D11TextureD(q, m_parent.m_ctx, width, height, fmt);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_DTexs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
ITextureR* newRenderTexture(size_t width, size_t height,
|
|
|
|
bool enableShaderColorBind, bool enableShaderDepthBind)
|
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11TextureR* retval = new D3D11TextureR(m_parent.m_ctx, width, height, m_parent.m_sampleCount,
|
|
|
|
enableShaderColorBind, enableShaderDepthBind);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_RTexs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-12-11 01:50:26 +00:00
|
|
|
IVertexFormat* newVertexFormat(size_t elementCount, const VertexElementDescriptor* elements,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11CommandQueue* q = static_cast<D3D11CommandQueue*>(m_parent.m_parent->getCommandQueue());
|
|
|
|
D3D11VertexFormat* retval = new struct D3D11VertexFormat(elementCount, elements);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_VFmts.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
|
|
|
#if _DEBUG
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_DEBUG | D3DCOMPILE_OPTIMIZATION_LEVEL0
|
|
|
|
#else
|
|
|
|
#define BOO_D3DCOMPILE_FLAG D3DCOMPILE_OPTIMIZATION_LEVEL3
|
|
|
|
#endif
|
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
static uint64_t CompileVert(ComPtr<ID3DBlob>& vertBlobOut, const char* vertSource, uint64_t srcKey,
|
|
|
|
D3D11DataFactory& factory)
|
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> errBlob;
|
|
|
|
if (FAILED(D3DCompilePROC(vertSource, strlen(vertSource), "HECL Vert Source", nullptr, nullptr, "main",
|
|
|
|
"vs_5_0", BOO_D3DCOMPILE_FLAG, 0, &vertBlobOut, &errBlob)))
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Fatal, "error compiling vert shader: %s", errBlob->GetBufferPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
XXH64_state_t hashState;
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, vertBlobOut->GetBufferPointer(), vertBlobOut->GetBufferSize());
|
|
|
|
uint64_t binKey = XXH64_digest(&hashState);
|
|
|
|
factory.m_sourceToBinary[srcKey] = binKey;
|
|
|
|
return binKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t CompileFrag(ComPtr<ID3DBlob>& fragBlobOut, const char* fragSource, uint64_t srcKey,
|
|
|
|
D3D11DataFactory& factory)
|
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> errBlob;
|
|
|
|
if (FAILED(D3DCompilePROC(fragSource, strlen(fragSource), "HECL Pixel Source", nullptr, nullptr, "main",
|
|
|
|
"ps_5_0", BOO_D3DCOMPILE_FLAG, 0, &fragBlobOut, &errBlob)))
|
|
|
|
{
|
|
|
|
Log.report(logvisor::Fatal, "error compiling pixel shader: %s", errBlob->GetBufferPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
XXH64_state_t hashState;
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
XXH64_update(&hashState, fragBlobOut->GetBufferPointer(), fragBlobOut->GetBufferSize());
|
|
|
|
uint64_t binKey = XXH64_digest(&hashState);
|
|
|
|
factory.m_sourceToBinary[srcKey] = binKey;
|
|
|
|
return binKey;
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
IShaderPipeline* newShaderPipeline
|
|
|
|
(const char* vertSource, const char* fragSource,
|
2017-03-05 22:59:58 +00:00
|
|
|
ComPtr<ID3DBlob>* vertBlobOut, ComPtr<ID3DBlob>* fragBlobOut,
|
|
|
|
ComPtr<ID3DBlob>* pipelineBlob, IVertexFormat* vtxFmt,
|
2016-03-30 20:42:36 +00:00
|
|
|
BlendFactor srcFac, BlendFactor dstFac, Primitive prim,
|
|
|
|
bool depthTest, bool depthWrite, bool backfaceCulling)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2017-03-05 22:59:58 +00:00
|
|
|
XXH64_state_t hashState;
|
|
|
|
uint64_t srcHashes[2] = {};
|
|
|
|
uint64_t binHashes[2] = {};
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
if (vertSource)
|
|
|
|
{
|
|
|
|
XXH64_update(&hashState, vertSource, strlen(vertSource));
|
|
|
|
srcHashes[0] = XXH64_digest(&hashState);
|
|
|
|
auto binSearch = m_parent.m_sourceToBinary.find(srcHashes[0]);
|
|
|
|
if (binSearch != m_parent.m_sourceToBinary.cend())
|
|
|
|
binHashes[0] = binSearch->second;
|
|
|
|
}
|
|
|
|
else if (vertBlobOut && *vertBlobOut)
|
|
|
|
{
|
|
|
|
XXH64_update(&hashState, (*vertBlobOut)->GetBufferPointer(), (*vertBlobOut)->GetBufferSize());
|
|
|
|
binHashes[0] = XXH64_digest(&hashState);
|
|
|
|
}
|
|
|
|
XXH64_reset(&hashState, 0);
|
|
|
|
if (fragSource)
|
|
|
|
{
|
|
|
|
XXH64_update(&hashState, fragSource, strlen(fragSource));
|
|
|
|
srcHashes[1] = XXH64_digest(&hashState);
|
|
|
|
auto binSearch = m_parent.m_sourceToBinary.find(srcHashes[1]);
|
|
|
|
if (binSearch != m_parent.m_sourceToBinary.cend())
|
|
|
|
binHashes[1] = binSearch->second;
|
|
|
|
}
|
|
|
|
else if (fragBlobOut && *fragBlobOut)
|
|
|
|
{
|
|
|
|
XXH64_update(&hashState, (*fragBlobOut)->GetBufferPointer(), (*fragBlobOut)->GetBufferSize());
|
|
|
|
binHashes[1] = XXH64_digest(&hashState);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vertBlobOut && !*vertBlobOut)
|
|
|
|
binHashes[0] = CompileVert(*vertBlobOut, vertSource, srcHashes[0], m_parent);
|
2016-03-30 20:42:36 +00:00
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
if (fragBlobOut && !*fragBlobOut)
|
|
|
|
binHashes[1] = CompileFrag(*fragBlobOut, fragSource, srcHashes[1], m_parent);
|
|
|
|
|
|
|
|
|
|
|
|
struct D3D11Context* ctx = m_parent.m_ctx;
|
|
|
|
D3D11ShareableShader::Token vertShader;
|
|
|
|
D3D11ShareableShader::Token fragShader;
|
|
|
|
auto vertFind = binHashes[0] ? m_parent.m_sharedShaders.find(binHashes[0]) :
|
|
|
|
m_parent.m_sharedShaders.end();
|
|
|
|
if (vertFind != m_parent.m_sharedShaders.end())
|
2015-11-18 06:14:49 +00:00
|
|
|
{
|
2017-03-05 22:59:58 +00:00
|
|
|
vertShader = vertFind->second->lock();
|
2015-11-18 06:14:49 +00:00
|
|
|
}
|
2017-03-05 22:59:58 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> vertBlob;
|
|
|
|
if (vertBlobOut)
|
|
|
|
vertBlob = *vertBlobOut;
|
|
|
|
else
|
|
|
|
binHashes[0] = CompileVert(vertBlob, vertSource, srcHashes[0], m_parent);
|
|
|
|
|
|
|
|
ComPtr<ID3D11VertexShader> vShader;
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreateVertexShader(vertBlob->GetBufferPointer(),
|
|
|
|
vertBlob->GetBufferSize(), nullptr, &vShader));
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2017-03-05 22:59:58 +00:00
|
|
|
auto it =
|
|
|
|
m_parent.m_sharedShaders.emplace(std::make_pair(binHashes[0],
|
|
|
|
std::make_unique<D3D11ShareableShader>(m_parent, srcHashes[0], binHashes[0],
|
|
|
|
std::move(vShader), std::move(vertBlob)))).first;
|
|
|
|
vertShader = it->second->lock();
|
|
|
|
}
|
|
|
|
auto fragFind = binHashes[1] ? m_parent.m_sharedShaders.find(binHashes[1]) :
|
|
|
|
m_parent.m_sharedShaders.end();
|
|
|
|
if (fragFind != m_parent.m_sharedShaders.end())
|
2015-11-18 06:14:49 +00:00
|
|
|
{
|
2017-03-05 22:59:58 +00:00
|
|
|
fragShader = fragFind->second->lock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ComPtr<ID3DBlob> fragBlob;
|
|
|
|
ComPtr<ID3DBlob>* useFragBlob;
|
|
|
|
if (fragBlobOut)
|
|
|
|
{
|
|
|
|
useFragBlob = fragBlobOut;
|
|
|
|
}
|
|
|
|
else
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
2017-03-05 22:59:58 +00:00
|
|
|
useFragBlob = &fragBlob;
|
|
|
|
binHashes[1] = CompileFrag(fragBlob, fragSource, srcHashes[1], m_parent);
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
2017-03-05 22:59:58 +00:00
|
|
|
|
|
|
|
ComPtr<ID3D11PixelShader> pShader;
|
|
|
|
ThrowIfFailed(ctx->m_dev->CreatePixelShader((*useFragBlob)->GetBufferPointer(),
|
|
|
|
(*useFragBlob)->GetBufferSize(), nullptr, &pShader));
|
|
|
|
|
|
|
|
auto it =
|
|
|
|
m_parent.m_sharedShaders.emplace(std::make_pair(binHashes[1],
|
|
|
|
std::make_unique<D3D11ShareableShader>(m_parent, srcHashes[1], binHashes[1],
|
|
|
|
std::move(pShader)))).first;
|
|
|
|
fragShader = it->second->lock();
|
2015-11-18 06:14:49 +00:00
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2017-03-05 22:59:58 +00:00
|
|
|
D3D11ShaderPipeline* retval = new D3D11ShaderPipeline(ctx,
|
|
|
|
std::move(vertShader), std::move(fragShader),
|
2016-03-30 20:42:36 +00:00
|
|
|
static_cast<const D3D11VertexFormat*>(vtxFmt),
|
|
|
|
srcFac, dstFac, prim, depthTest, depthWrite, backfaceCulling);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_SPs.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
IShaderDataBinding* newShaderDataBinding(IShaderPipeline* pipeline,
|
|
|
|
IVertexFormat* vtxFormat,
|
|
|
|
IGraphicsBuffer* vbuf, IGraphicsBuffer* instVbo, IGraphicsBuffer* ibuf,
|
2016-04-04 19:33:42 +00:00
|
|
|
size_t ubufCount, IGraphicsBuffer** ubufs, const PipelineStage* ubufStages,
|
2016-03-30 20:42:36 +00:00
|
|
|
const size_t* ubufOffs, const size_t* ubufSizes,
|
2016-12-11 01:50:26 +00:00
|
|
|
size_t texCount, ITexture** texs,
|
|
|
|
size_t baseVert, size_t baseInst)
|
2016-03-30 20:42:36 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
D3D11Data* d = static_cast<D3D11Data*>(m_deferredData);
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11ShaderDataBinding* retval =
|
2017-01-20 03:52:40 +00:00
|
|
|
new D3D11ShaderDataBinding(d, m_parent.m_ctx, pipeline, vbuf, instVbo, ibuf,
|
2016-12-11 01:50:26 +00:00
|
|
|
ubufCount, ubufs, ubufStages, ubufOffs, ubufSizes, texCount, texs,
|
|
|
|
baseVert, baseInst);
|
2016-12-11 23:29:08 +00:00
|
|
|
d->m_SBinds.emplace_back(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
};
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
GraphicsDataToken commitTransaction(const FactoryCommitFunc& trans)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-03-30 20:42:36 +00:00
|
|
|
if (m_deferredData)
|
|
|
|
Log.report(logvisor::Fatal, "nested commitTransaction usage detected");
|
|
|
|
m_deferredData = new D3D11Data();
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2016-03-30 20:42:36 +00:00
|
|
|
D3D11DataFactory::Context ctx(*this);
|
|
|
|
if (!trans(ctx))
|
|
|
|
{
|
|
|
|
delete m_deferredData;
|
|
|
|
m_deferredData = nullptr;
|
2015-12-18 04:55:06 +00:00
|
|
|
return GraphicsDataToken(this, nullptr);
|
2016-03-30 20:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
2015-12-02 22:04:24 +00:00
|
|
|
D3D11Data* retval = m_deferredData;
|
2015-12-14 19:19:29 +00:00
|
|
|
m_deferredData = nullptr;
|
2015-11-06 03:20:58 +00:00
|
|
|
m_committedData.insert(retval);
|
2016-03-30 20:42:36 +00:00
|
|
|
lk.unlock();
|
2015-12-18 04:55:06 +00:00
|
|
|
return GraphicsDataToken(this, retval);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
2016-12-11 01:50:26 +00:00
|
|
|
|
|
|
|
GraphicsBufferPoolToken newBufferPool()
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lk(m_committedMutex);
|
|
|
|
D3D11Pool* retval = new D3D11Pool;
|
|
|
|
m_committedPools.insert(retval);
|
|
|
|
return GraphicsBufferPoolToken(this, retval);
|
|
|
|
}
|
2017-03-05 22:59:58 +00:00
|
|
|
|
|
|
|
void _unregisterShareableShader(uint64_t srcKey, uint64_t binKey)
|
|
|
|
{
|
|
|
|
if (srcKey)
|
|
|
|
m_sourceToBinary.erase(srcKey);
|
|
|
|
m_sharedShaders.erase(binKey);
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
};
|
|
|
|
|
2015-12-14 19:19:29 +00:00
|
|
|
thread_local D3D11Data* D3D11DataFactory::m_deferredData;
|
|
|
|
|
2015-12-02 22:04:24 +00:00
|
|
|
void D3D11CommandQueue::execute()
|
2016-01-19 20:30:51 +00:00
|
|
|
{
|
2016-12-11 23:29:08 +00:00
|
|
|
/* Finish command list */
|
|
|
|
auto& CmdList = m_cmdLists[m_fillBuf];
|
|
|
|
ThrowIfFailed(m_deferredCtx->FinishCommandList(false, &CmdList.list));
|
2017-01-21 00:19:18 +00:00
|
|
|
CmdList.workDoPresent = m_doPresent;
|
|
|
|
m_doPresent = nullptr;
|
2015-12-08 23:57:16 +00:00
|
|
|
|
|
|
|
/* Wait for worker thread to become ready */
|
2015-12-02 22:04:24 +00:00
|
|
|
std::unique_lock<std::mutex> lk(m_mt);
|
2015-12-08 23:57:16 +00:00
|
|
|
|
|
|
|
/* Ready for next frame */
|
2015-12-02 22:04:24 +00:00
|
|
|
m_completeBuf = m_fillBuf;
|
|
|
|
for (size_t i=0 ; i<3 ; ++i)
|
|
|
|
{
|
|
|
|
if (i == m_completeBuf || i == m_drawBuf)
|
|
|
|
continue;
|
|
|
|
m_fillBuf = i;
|
|
|
|
break;
|
|
|
|
}
|
2015-12-08 23:57:16 +00:00
|
|
|
|
|
|
|
/* Return control to worker thread */
|
2015-12-02 22:04:24 +00:00
|
|
|
lk.unlock();
|
|
|
|
m_cv.notify_one();
|
|
|
|
}
|
2015-11-06 03:20:58 +00:00
|
|
|
|
2015-12-09 05:52:51 +00:00
|
|
|
void D3D11CommandQueue::ProcessDynamicLoads(ID3D11DeviceContext* ctx)
|
|
|
|
{
|
|
|
|
D3D11DataFactory* gfxF = static_cast<D3D11DataFactory*>(m_parent->getDataFactory());
|
2016-12-11 01:50:26 +00:00
|
|
|
std::unique_lock<std::recursive_mutex> lk(m_dynamicLock);
|
2015-12-14 19:19:29 +00:00
|
|
|
std::unique_lock<std::mutex> datalk(gfxF->m_committedMutex);
|
2015-12-09 21:02:04 +00:00
|
|
|
|
2015-12-09 05:52:51 +00:00
|
|
|
for (D3D11Data* d : gfxF->m_committedData)
|
|
|
|
{
|
2017-01-21 00:19:18 +00:00
|
|
|
for (std::unique_ptr<D3D11GraphicsBufferD>& b : d->m_DBufs)
|
2016-01-02 21:16:50 +00:00
|
|
|
b->update(ctx, m_drawBuf);
|
2017-01-21 00:19:18 +00:00
|
|
|
for (std::unique_ptr<D3D11TextureD>& t : d->m_DTexs)
|
2016-01-02 21:16:50 +00:00
|
|
|
t->update(ctx, m_drawBuf);
|
2015-12-09 05:52:51 +00:00
|
|
|
}
|
2016-12-11 01:50:26 +00:00
|
|
|
for (D3D11Pool* p : gfxF->m_committedPools)
|
|
|
|
{
|
2016-12-11 06:18:41 +00:00
|
|
|
for (auto& b : p->m_DBufs)
|
2016-12-11 23:29:08 +00:00
|
|
|
b.second->update(ctx, m_drawBuf);
|
2016-12-11 01:50:26 +00:00
|
|
|
}
|
2015-12-09 05:52:51 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 03:20:58 +00:00
|
|
|
IGraphicsCommandQueue* _NewD3D11CommandQueue(D3D11Context* ctx, D3D11Context::Window* windowCtx, IGraphicsContext* parent)
|
|
|
|
{
|
|
|
|
return new D3D11CommandQueue(ctx, windowCtx, parent);
|
|
|
|
}
|
|
|
|
|
2016-02-24 20:52:31 +00:00
|
|
|
IGraphicsDataFactory* _NewD3D11DataFactory(D3D11Context* ctx, IGraphicsContext* parent, uint32_t sampleCount)
|
2015-11-06 03:20:58 +00:00
|
|
|
{
|
2016-02-24 20:52:31 +00:00
|
|
|
return new D3D11DataFactory(parent, ctx, sampleCount);
|
2015-11-06 03:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|