4#include <DirectXMath.h>
32 const auto close=[](
const float* a,
const float* b,
unsigned n,
float limit) {
33 for(
unsigned i=0;i<n;++i)
34 if(!std::isfinite(a[i]) || !std::isfinite(b[i]) || std::abs(a[i]-b[i])>limit)
return false;
58 for(
unsigned r=0;r<3;++r)
for(
unsigned c=0;c<3;++c)
59 if(!std::isfinite(current.
view.m[r][c]) || !std::isfinite(
previous.
view.m[r][c])
61 for(
unsigned i=0;i<3;++i)
64 if(!std::isfinite(delta) || std::abs(delta)>.1f)
return false;
70 Reset();
if(!native)
return E_INVALIDARG;
device=native;
73 if(vs.empty() || ps.empty())
return E_FAIL;
75 if(FAILED(hr))
return hr;
76 D3D11_SAMPLER_DESC sd{};sd.AddressU=sd.AddressV=sd.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
77 sd.MaxLOD=D3D11_FLOAT32_MAX;sd.Filter=D3D11_FILTER_MIN_MAG_MIP_LINEAR;
78 if(FAILED(hr=
device->CreateSamplerState(&sd,&
linear)))
return hr;
79 sd.Filter=D3D11_FILTER_MIN_MAG_MIP_POINT;
80 if(FAILED(hr=
device->CreateSamplerState(&sd,&
point)))
return hr;
87 HRESULT BeginFrame(
const Frame& frame,
unsigned fullWidth,
unsigned fullHeight,
unsigned sampleWidth,
unsigned sampleHeight)
90 if(!
device || fullWidth<2 || fullHeight<2 || !sampleWidth || !sampleHeight)
return E_INVALIDARG;
94 for(
unsigned r=0;r<4;++r)
for(
unsigned c=0;c<4;++c)
95 if(!std::isfinite(frame.
view.m[r][c]) || !std::isfinite(frame.
inverseView.m[r][c]))
97 const unsigned w=fullWidth/2,h=fullHeight/2;
102 for(
unsigned i=0;i<2;++i)
104 if(FAILED(hr=
Create(newColor[i],w,h,DXGI_FORMAT_R16G16B16A16_FLOAT)))
return hr;
105 if(FAILED(hr=
Create(newDistance[i],w,h,DXGI_FORMAT_R32G32_FLOAT)))
return hr;
107 for(
unsigned i=0;i<2;++i){
color[i]=std::move(newColor[i]);
distance[i]=std::move(newDistance[i]);}
112 jitter={((
phase&1) ? .25f : -.25f)/sampleWidth,((
phase&2) ? .25f : -.25f)/sampleHeight,0,0};
119 HRESULT Resolve(ID3D11DeviceContext* context,ID3D11ShaderResourceView* current,ID3D11ShaderResourceView* currentDistance)
121 if(!context || !current || !currentDistance || !
width || !
height)
return E_INVALIDARG;
122 const unsigned index=
phase&1,old=1-index;
125 const float clear[4]{};context->ClearRenderTargetView(
color[old].rtv.Get(),clear);
126 context->ClearRenderTargetView(
distance[old].rtv.Get(),clear);
128 DirectX::XMFLOAT4 delta{};
129 if(
valid)
for(
unsigned i=0;i<3;++i)
139 context->OMSetRenderTargets(2,targets,
nullptr);
140 const D3D11_VIEWPORT viewport{0,0,float(
width),float(
height),0,1};context->RSSetViewports(1,&viewport);
142 if(SUCCEEDED(hr))context->Draw(3,0);
150 Microsoft::WRL::ComPtr<ID3D11Texture2D>
texture;
151 Microsoft::WRL::ComPtr<ID3D11RenderTargetView>
rtv;
152 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView>
srv;
156 D3D11_TEXTURE2D_DESC td{};td.Width=w;td.Height=h;td.ArraySize=td.MipLevels=1;
157 td.SampleDesc={1,0};td.Format=format;td.BindFlags=D3D11_BIND_RENDER_TARGET|D3D11_BIND_SHADER_RESOURCE;
158 HRESULT hr=
device->CreateTexture2D(&td,
nullptr,&target.texture);
159 if(SUCCEEDED(hr))hr=
device->CreateRenderTargetView(target.texture.Get(),
nullptr,&target.rtv);
160 if(SUCCEEDED(hr))hr=
device->CreateShaderResourceView(target.texture.Get(),
nullptr,&target.srv);
163 static void Unbind(ID3D11DeviceContext* context)
165 ID3D11ShaderResourceView* empty[16]{};context->PSSetShaderResources(0,16,empty);
166 context->OMSetRenderTargets(0,
nullptr,
nullptr);
168 Microsoft::WRL::ComPtr<ID3D11Device>
device;
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
HRESULT Create(Target &target, unsigned w, unsigned h, DXGI_FORMAT format)
HRESULT Initialize(ID3D11Device *native, const std::filesystem::path &shaders)
Microsoft::WRL::ComPtr< ID3D11SamplerState > linear
ID3D11ShaderResourceView * Image() const
Microsoft::WRL::ComPtr< ID3D11SamplerState > point
ID3D11ShaderResourceView * Distance() const
HRESULT BeginFrame(const Frame &frame, unsigned fullWidth, unsigned fullHeight, unsigned sampleWidth, unsigned sampleHeight)
static void Unbind(ID3D11DeviceContext *context)
Microsoft::WRL::ComPtr< ID3D11Device > device
DirectX::XMFLOAT4 Jitter() const
HRESULT Resolve(ID3D11DeviceContext *context, ID3D11ShaderResourceView *current, ID3D11ShaderResourceView *currentDistance)
static bool Compatible(const Frame &previous, const Frame ¤t)
void Set(const std::string &name, const void *data, size_t bytes)
void Texture(const std::string &name, ID3D11ShaderResourceView *value)
void Sampler(const std::string &name, ID3D11SamplerState *value)
HRESULT Create(ID3D11Device *device, const void *vertexCode, size_t vertexSize, const void *pixelCode, size_t pixelSize, const D3D11_INPUT_ELEMENT_DESC *elements=nullptr, UINT elementCount=0, UINT bgraColorMask=0)
HRESULT Bind(ID3D11DeviceContext *context, const Parameters11 ¶meters)
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
DirectX::XMFLOAT4 eclipse
const void * previousWeather
DirectX::XMFLOAT4X4 projection
DirectX::XMFLOAT3 noiseOffset
DirectX::XMFLOAT4 ambient
DirectX::XMFLOAT4 horizon
DirectX::XMFLOAT4 airglow
const void * currentWeather
DirectX::XMFLOAT4 atmosphere
DirectX::XMFLOAT4X4 inverseView
DirectX::XMFLOAT4 nightVision
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > srv
Microsoft::WRL::ComPtr< ID3D11Texture2D > texture
Microsoft::WRL::ComPtr< ID3D11RenderTargetView > rtv