67 for(
unsigned i=0;i<4;++i)
for(
unsigned pass=0;pass<2;++pass)
69 if(!
HasShaderBytecode11(directory/(std::string(
"FogBeams-")+(pass ?
"FogEdge-" :
"FogPixel-")+std::to_string(1u<<i)+
".cso")))
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
71 if(!
HasShaderBytecode11(directory/
"FogBeams-FogVertex-1.cso"))
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
72 for(
unsigned i=0;i<4;++i)
for(
const char* entry:{
"FogLow",
"FogResolvePixel",
"FogResolveEdge"})
73 if(!
HasShaderBytecode11(directory/(std::string(
"FogBeams-")+entry+
"-"+std::to_string(1u<<i)+
".cso")))
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
76 for(
unsigned i=0;i<4;++i)
for(
unsigned pass=0;pass<2;++pass)
78 if(!
HasShaderBytecode11(directory/(std::string(
"FogOrganic-")+(pass ?
"FogEdge-" :
"FogPixel-")+std::to_string(1u<<i)+
".cso")))
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
81 for(
unsigned i=0;i<4;++i)
82 if(!
HasShaderBytecode11(directory/(
"FogOrganic-FogLow-"+std::to_string(1u<<i)+
".cso")))
return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
84 D3D11_RASTERIZER_DESC rs{};rs.FillMode=D3D11_FILL_SOLID;rs.CullMode=D3D11_CULL_NONE;
85 rs.DepthClipEnable=rs.MultisampleEnable=rs.ScissorEnable=TRUE;
86 if(FAILED(hr=
device->CreateRasterizerState(&rs,&
raster)))
return hr;
87 D3D11_DEPTH_STENCIL_DESC ds{};ds.DepthFunc=D3D11_COMPARISON_ALWAYS;
88 ds.StencilEnable=TRUE;ds.StencilReadMask=255;
89 ds.FrontFace={D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_COMPARISON_GREATER};ds.BackFace=ds.FrontFace;
90 if(FAILED(hr=
device->CreateDepthStencilState(&ds,&
stencil)))
return hr;
91 D3D11_BLEND_DESC bs{};bs.RenderTarget[0]={TRUE,D3D11_BLEND_ONE,D3D11_BLEND_ONE,D3D11_BLEND_OP_ADD,
92 D3D11_BLEND_ZERO,D3D11_BLEND_ONE,D3D11_BLEND_OP_ADD,D3D11_COLOR_WRITE_ENABLE_RED|D3D11_COLOR_WRITE_ENABLE_GREEN|D3D11_COLOR_WRITE_ENABLE_BLUE};
93 if(FAILED(hr=
device->CreateBlendState(&bs,&
blend)))
return hr;
94 bs.RenderTarget[0].SrcBlendAlpha=bs.RenderTarget[0].DestBlendAlpha=D3D11_BLEND_ONE;
95 bs.RenderTarget[0].BlendOpAlpha=D3D11_BLEND_OP_MAX;bs.RenderTarget[0].RenderTargetWriteMask=D3D11_COLOR_WRITE_ENABLE_ALL;
97 D3D11_SAMPLER_DESC sampler{};sampler.Filter=D3D11_FILTER_MIN_MAG_MIP_LINEAR;
98 sampler.AddressU=sampler.AddressV=sampler.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;sampler.MaxLOD=D3D11_FLOAT32_MAX;
99 sampler.ComparisonFunc=D3D11_COMPARISON_NEVER;sampler.MaxAnisotropy=1;
101 ComPtr<ID3D11Device1> device1;
if(FAILED(hr=
device.As(&device1)))
return hr;
102 const auto level=
device->GetFeatureLevel();
103 return device1->CreateDeviceContextState(0,&level,1,D3D11_SDK_VERSION,__uuidof(ID3D11Device),
nullptr,&
state);
158 const D3D11_VIEWPORT& viewport,
const Matrix& view,
const Matrix& projection,
159 const std::vector<Light>& lights,
const Parameters11* clouds)
161 lastDraws=0;
if(lights.empty())
return S_OK;
162 if(!
Ready() || !context || !scene || !depth.
ReadView())
return E_UNEXPECTED;
163 if(viewport.Width<1 || viewport.Height<1 || viewport.TopLeftX<0 || viewport.TopLeftY<0
164 || viewport.TopLeftX+viewport.Width>depth.
Description().Width || viewport.TopLeftY+viewport.Height>depth.
Description().Height
165 || !std::isfinite(projection._11) || !std::isfinite(projection._22) || std::abs(projection._11)<1e-8f || std::abs(projection._22)<1e-8f)
return E_INVALIDARG;
166 unsigned variant=0;
while(variant<4 && (1u<<variant)!=depth.
Description().SampleDesc.Count)++variant;
167 if(variant==4)
return E_INVALIDARG;
171 (std::max)(4.f,(std::max)(viewport.Width/384.f,viewport.Height/216.f));
172 D3D11_VIEWPORT lowViewport{0,0,std::ceil(viewport.Width/divisor),std::ceil(viewport.Height/divisor),0,1};
175 if(!
resolutionDivisor){lowViewport.Width=(std::min)(384.f,lowViewport.Width);lowViewport.Height=(std::min)(216.f,lowViewport.Height);}
176 if(reduced){
const HRESULT hr=
PrepareTargets(
unsigned(lowViewport.Width),
unsigned(lowViewport.Height));
if(FAILED(hr))
return hr;}
178 if(!clouds)
for(
const char* name:{
"noiseSampler",
"weatherPreviousSampler",
"weatherCurrentSampler",
"headlightDepthSampler",
"localBaseSampler"})
180 Matrix inverse;XMVECTOR determinant;
const auto v=XMLoadFloat4x4(&view),p=XMLoadFloat4x4(&projection);
181 XMStoreFloat4x4(&inverse,XMMatrixInverse(&determinant,v));
182 if(!std::isfinite(XMVectorGetX(determinant)) || std::abs(XMVectorGetX(determinant))<1e-8f)
return E_INVALIDARG;
183 values.
Set(
"fogView",view);values.
Set(
"fogInverseView",inverse);values.
Set(
"fogProjection",projection);
184 values.
Set(
"fogViewport",XMFLOAT4(viewport.TopLeftX,viewport.TopLeftY,viewport.Width,viewport.Height));
185 values.
Set(
"fogBufferSize",XMFLOAT4(lowViewport.Width,lowViewport.Height,0,0));
186 values.
Set(
"fogClouds",
int(clouds!=
nullptr));values.
Set(
"riseSceneStencilAvailable",UINT(depth.
StencilView()!=
nullptr));
188 Restore restore;
HRESULT hr=context->QueryInterface(IID_PPV_ARGS(&restore.context));
if(FAILED(hr))
return hr;
189 restore.context->SwapDeviceContextState(
state.Get(),&restore.previous);
190 Unbind(context);context->OMSetRenderTargets(1,&scene,depth.
ReadView());context->RSSetViewports(1,&viewport);
191 context->RSSetState(
raster.Get());context->OMSetDepthStencilState(
stencil.Get(),3);context->OMSetBlendState(
blend.Get(),
nullptr,UINT_MAX);
192 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
193 context->GSSetShader(
nullptr,
nullptr,0);context->HSSetShader(
nullptr,
nullptr,0);context->DSSetShader(
nullptr,
nullptr,0);
197 const float empty[4]{0,0,0,-1};context->ClearRenderTargetView(
scatteringAll.Get(),empty);
198 context->OMSetDepthStencilState(
nullptr,0);context->OMSetBlendState(
lowBlend.Get(),
nullptr,UINT_MAX);
199 auto* target=
scatteringAll.Get();context->OMSetRenderTargets(1,&target,
nullptr);
200 context->RSSetViewports(1,&lowViewport);
202 std::array<XMFLOAT4,MaxLights> positions{},directions{},ups{},angles{},profiles{};
204 D3D11_RECT resolveBounds{LONG(viewport.TopLeftX+viewport.Width),LONG(viewport.TopLeftY+viewport.Height),LONG(viewport.TopLeftX),LONG(viewport.TopLeftY)};
205 for(
size_t index=0;index<(std::min)(lights.size(),
size_t(
MaxLights));++index)
207 auto light=lights[index];
208 if(!(light.extinctionPerKm>0) || !(light.color.w>0) || !std::isfinite(light.extinctionPerKm) || !(light.profile.rangeKm>0))
continue;
209 const float length=XMVectorGetX(XMVector3Length(XMLoadFloat3(&light.direction)));
210 if(!std::isfinite(length) || length<1e-8f)
return E_INVALIDARG;
211 XMStoreFloat3(&light.direction,XMVectorScale(XMLoadFloat3(&light.direction),1/length));
213 D3D11_RECT rectangle;
if(!Bounds(light,v,p,viewport,rectangle))
continue;
216 resolveBounds.left=(std::min)(resolveBounds.left,rectangle.left);resolveBounds.top=(std::min)(resolveBounds.top,rectangle.top);
217 resolveBounds.right=(std::max)(resolveBounds.right,rectangle.right);resolveBounds.bottom=(std::max)(resolveBounds.bottom,rectangle.bottom);
218 positions[count]={light.position.x,light.position.y,light.position.z,light.profile.rangeKm};
219 directions[count]={light.direction.x,light.direction.y,light.direction.z,0};ups[count]={light.up.x,light.up.y,light.up.z,0};
220 angles[count]=light.profile.Angles<XMFLOAT4>();profiles[count]=light.profile.Parameters<XMFLOAT4>(light.extinctionPerKm);
221 const float sx=lowViewport.Width/viewport.Width,sy=lowViewport.Height/viewport.Height;
222 rectangle={LONG(std::floor((rectangle.left-viewport.TopLeftX)*sx)),LONG(std::floor((rectangle.top-viewport.TopLeftY)*sy)),
223 LONG(std::ceil((rectangle.right-viewport.TopLeftX)*sx)),LONG(std::ceil((rectangle.bottom-viewport.TopLeftY)*sy))};
226 context->RSSetScissorRects(1,&rectangle);
227 values.
Set(
"fogLampPosition",XMFLOAT4(light.position.x,light.position.y,light.position.z,light.profile.rangeKm));
228 values.
Set(
"fogLampDirection",XMFLOAT4(light.direction.x,light.direction.y,light.direction.z,0));
229 values.
Set(
"fogLampUp",XMFLOAT4(light.up.x,light.up.y,light.up.z,0));values.
Set(
"fogLampColor",light.color);
230 values.
Set(
"fogLampAngles",light.profile.Angles<XMFLOAT4>());
231 values.
Set(
"fogLampParameters",light.profile.Parameters<XMFLOAT4>(light.extinctionPerKm));
232 values.
Set(
"fogCloudIndex",light.cloudIndex);
243 context->OMSetRenderTargets(1,&scene,depth.
ReadView());context->RSSetViewports(1,&viewport);
244 context->RSSetScissorRects(1,&resolveBounds);context->OMSetDepthStencilState(
stencil.Get(),3);context->OMSetBlendState(
blend.Get(),
nullptr,UINT_MAX);
245 values.
Set(
"fogResolveCount",count);values.
Set(
"fogResolvePositions",positions);values.
Set(
"fogResolveDirections",directions);
246 values.
Set(
"fogResolveUps",ups);values.
Set(
"fogResolveAngles",angles);values.
Set(
"fogResolveProfiles",profiles);
249 if(variant){
if(FAILED(hr=
resolveEdge[variant].Bind(context,values)))
return hr;context->Draw(3,0);++
lastDraws;}