Rise
The Vieneo Province
Loading...
Searching...
No Matches
CloudTemporal11.h
Go to the documentation of this file.
1#pragma once
2#include "Program11.h"
3#include "ShaderBytecode11.h"
4#include <DirectXMath.h>
5#include <filesystem>
6#include <cmath>
7#include <algorithm>
8
9namespace Rendering
10{
11 // Used by the opt-in orbital cloud pass. This owns only cloud history; it never
12 // resolves or rewrites the scene's geometry depth, stencil, or MSAA.
14 {
15 friend class CloudPass11;
16 public:
17 struct Frame
18 {
19 DirectX::XMFLOAT4X4 view{},projection{},inverseView{};
20 DirectX::XMFLOAT3 eye{},noiseOffset{},sun{};
21 DirectX::XMFLOAT4 light{},ambient{},nightVision{},layer{},airglow{},horizon{},atmosphere{};
22 const void* previousWeather=nullptr;
23 const void* currentWeather=nullptr;
24 DirectX::XMFLOAT4 eclipse{};
26 float weatherBlend=0;
27 uint64_t milliseconds=0;
28 bool transientLight=false;
29 };
30 static bool Compatible(const Frame& previous,const Frame& current)
31 {
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;
35 return true;
36 };
38 || !close(&previous.eye.x,&current.eye.x,3,2.f)
39 // Eclipse edges move much faster than ordinary day/night
40 // shading when the sun direction is overridden.
41 || !close(&previous.sun.x,&current.sun.x,3,current.eclipse.w>0 ? .0001f : .02f)
42 || !close(&previous.eclipse.x,&current.eclipse.x,3,2.f)
43 || !close(&previous.eclipse.w,&current.eclipse.w,1,.001f)
44 || !close(&previous.eclipseSunRadius,&current.eclipseSunRadius,1,.000001f)
45 || !close(&previous.light.x,&current.light.x,4,.05f)
46 || !close(&previous.ambient.x,&current.ambient.x,4,.05f)
47 || !close(&previous.nightVision.x,&current.nightVision.x,4,.001f)
48 || !close(&previous.airglow.x,&current.airglow.x,4,.05f)
49 || !close(&previous.horizon.x,&current.horizon.x,4,.05f)
50 || !close(&previous.atmosphere.x,&current.atmosphere.x,4,.05f)
51 || !close(&previous.layer.x,&current.layer.x,4,.001f)
52 || !close(&previous.projection._11,&current.projection._11,16,.001f)
55 || !std::isfinite(current.weatherBlend) || std::abs(current.weatherBlend-previous.weatherBlend)>.02f)return false;
56 // Reproject ordinary pans and orbital motion. Reset abrupt camera
57 // switches (including scaled view transforms used by zoom).
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])
60 || std::abs(current.view.m[r][c]-previous.view.m[r][c])>.25f)return false;
61 for(unsigned i=0;i<3;++i)
62 {
63 const float delta=std::remainder((&current.noiseOffset.x)[i]-(&previous.noiseOffset.x)[i],6400.f);
64 if(!std::isfinite(delta) || std::abs(delta)>.1f)return false;
65 }
66 return true;
67 }
68 HRESULT Initialize(ID3D11Device* native,const std::filesystem::path& shaders)
69 {
70 Reset();if(!native)return E_INVALIDARG;device=native;
71 const auto vs=ReadShaderBytecode11(shaders/"CloudVolume-Fullscreen11-1.cso");
72 const auto ps=ReadShaderBytecode11(shaders/"CloudTemporal-resolve-1.cso");
73 if(vs.empty() || ps.empty())return E_FAIL;
74 HRESULT hr=resolve.Create(device.Get(),vs.data(),vs.size(),ps.data(),ps.size());
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;
81 values.Sampler("linearSampler",linear.Get());values.Sampler("pointSampler",point.Get());
82 return S_OK;
83 }
84 void ResetHistory() {valid=false;phase=0;written=false;}
85 void ReleaseTargets() {for(auto& target:color)target={};for(auto& target:distance)target={};width=height=0;ResetHistory();}
86 void Reset() {ReleaseTargets();resolve.Reset();values=Parameters11{};linear.Reset();point.Reset();device.Reset();}
87 HRESULT BeginFrame(const Frame& frame,unsigned fullWidth,unsigned fullHeight,unsigned sampleWidth,unsigned sampleHeight)
88 {
89 written=false;
90 if(!device || fullWidth<2 || fullHeight<2 || !sampleWidth || !sampleHeight)return E_INVALIDARG;
91 auto validation=frame;validation.transientLight=false;
92 if(!Compatible(validation,validation) || std::abs(frame.projection._11)<1e-10f || std::abs(frame.projection._22)<1e-10f)
93 {ResetHistory();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]))
96 {ResetHistory();return E_INVALIDARG;}
97 const unsigned w=fullWidth/2,h=fullHeight/2;
98 if(width!=w || height!=h)
99 {
101 Target newColor[2],newDistance[2];HRESULT hr;
102 for(unsigned i=0;i<2;++i)
103 {
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;
106 }
107 for(unsigned i=0;i<2;++i){color[i]=std::move(newColor[i]);distance[i]=std::move(newDistance[i]);}
108 width=w;height=h;
109 }
110 if(valid && !Compatible(previous,frame))ResetHistory();
111 pending=frame;
112 jitter={((phase&1) ? .25f : -.25f)/sampleWidth,((phase&2) ? .25f : -.25f)/sampleHeight,0,0};
113 return S_OK;
114 }
115 DirectX::XMFLOAT4 Jitter() const {return jitter;}
116 bool HasHistory() const {return valid;}
117 ID3D11ShaderResourceView* Image() const {return written ? color[last].srv.Get() : nullptr;}
118 ID3D11ShaderResourceView* Distance() const {return written ? distance[last].srv.Get() : nullptr;}
119 HRESULT Resolve(ID3D11DeviceContext* context,ID3D11ShaderResourceView* current,ID3D11ShaderResourceView* currentDistance)
120 {
121 if(!context || !current || !currentDistance || !width || !height)return E_INVALIDARG;
122 const unsigned index=phase&1,old=1-index;
123 if(!valid)
124 {
125 const float clear[4]{};context->ClearRenderTargetView(color[old].rtv.Get(),clear);
126 context->ClearRenderTargetView(distance[old].rtv.Get(),clear);
127 }
128 DirectX::XMFLOAT4 delta{};
129 if(valid)for(unsigned i=0;i<3;++i)
130 (&delta.x)[i]=(&pending.eye.x)[i]-(&previous.eye.x)[i]
131 +std::remainder((&pending.noiseOffset.x)[i]-(&previous.noiseOffset.x)[i],6400.f);
132 values.Set("inverseView",pending.inverseView);values.Set("previousView",valid ? previous.view : pending.view);
133 values.Set("previousProjection",valid ? previous.projection : pending.projection);
134 values.Set("eyeDelta",delta);values.Set("jitter",jitter);values.Set("historyValid",valid ? 1u : 0u);
135 values.Set("screen",DirectX::XMFLOAT4(1/pending.projection._11,1/pending.projection._22,float(width),float(height)));
136 values.Texture("currentImage",current);values.Texture("currentDistance",currentDistance);
137 values.Texture("previousImage",color[old].srv.Get());values.Texture("previousDistance",distance[old].srv.Get());
138 Unbind(context);ID3D11RenderTargetView* targets[]={color[index].rtv.Get(),distance[index].rtv.Get()};
139 context->OMSetRenderTargets(2,targets,nullptr);
140 const D3D11_VIEWPORT viewport{0,0,float(width),float(height),0,1};context->RSSetViewports(1,&viewport);
141 const HRESULT hr=resolve.Bind(context,values);
142 if(SUCCEEDED(hr))context->Draw(3,0);
143 Unbind(context);values.ClearTextures();
144 if(FAILED(hr)){ResetHistory();return hr;}
145 previous=pending;valid=written=true;last=index;++phase;return S_OK;
146 }
147 private:
148 struct Target
149 {
150 Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;
151 Microsoft::WRL::ComPtr<ID3D11RenderTargetView> rtv;
152 Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> srv;
153 };
154 HRESULT Create(Target& target,unsigned w,unsigned h,DXGI_FORMAT format)
155 {
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);
161 return hr;
162 }
163 static void Unbind(ID3D11DeviceContext* context)
164 {
165 ID3D11ShaderResourceView* empty[16]{};context->PSSetShaderResources(0,16,empty);
166 context->OMSetRenderTargets(0,nullptr,nullptr);
167 }
168 Microsoft::WRL::ComPtr<ID3D11Device> device;
169 Microsoft::WRL::ComPtr<ID3D11SamplerState> linear,point;
172 DirectX::XMFLOAT4 jitter{};
173 unsigned width=0,height=0,phase=0,last=0;
174 bool valid=false,written=false;
175 };
176}
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 &current)
void Set(const std::string &name, const void *data, size_t bytes)
Definition Program11.cpp:45
void Texture(const std::string &name, ID3D11ShaderResourceView *value)
Definition Program11.cpp:55
void Sampler(const std::string &name, ID3D11SamplerState *value)
Definition Program11.cpp:66
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 &parameters)
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > srv
Microsoft::WRL::ComPtr< ID3D11Texture2D > texture
Microsoft::WRL::ComPtr< ID3D11RenderTargetView > rtv