149 const auto same = [](
const Vector& a,
const Vector& b) {
152 const auto connected = [&](
const Pane& a,
const Pane& b) {
153 for (
size_t i=0;i<a.vertices.size();i+=3)
for (
int e=0;e<3;++e)
154 for (
size_t j=0;j<b.vertices.size();j+=3)
for (
int f=0;f<3;++f)
156 const auto a0=Position(a.vertices[i+e]), a1=Position(a.vertices[i+(e+1)%3]);
157 const auto b0=Position(b.vertices[j+f]), b1=Position(b.vertices[j+(f+1)%3]);
158 if ((same(a0,b0) && same(a1,b1)) || (same(a0,b1) && same(a1,b0)))
return true;
162 for (
size_t i=0;i<
panes.size();++i)
for (
size_t j=i+1;j<
panes.size();)
166 const Pane* parts[]={&a,&b};
167 for (
const auto* p : parts)
for (
size_t t=0;t<p->vertices.size();t+=3)
169 const auto e1=(Position(p->vertices[t+1])-Position(p->vertices[t]))*1000.f;
170 const auto e2=(Position(p->vertices[t+2])-Position(p->vertices[t]))*1000.f;
174 bool shallow =
Dot(a.normal,b.normal) > .866f;
175 for (
const auto* p : parts)
for (
size_t t=0;t<p->vertices.size();t+=3)
177 const auto e1=(Position(p->vertices[t+1])-Position(p->vertices[t]))*1000.f;
178 const auto e2=(Position(p->vertices[t+2])-Position(p->vertices[t]))*1000.f;
180 shallow &=
Dot(n,normal) > .866f;
182 if (a.component!=b.component || !shallow || !connected(a,b)) { ++j;
continue; }
183 a.vertices.insert(a.vertices.end(),b.vertices.begin(),b.vertices.end());
184 a.normal=normal; a.u-=normal*
Dot(a.u,normal);
189 for (
auto& pane :
panes)
191 Point lo{1.e10f,1.e10f}, hi{-1.e10f,-1.e10f};
192 for (
const auto& vertex : pane.vertices)
194 const auto p = Project(Position(vertex),pane.origin,pane.u,pane.v);
195 lo.x = (std::min)(lo.x,p.x); lo.y = (std::min)(lo.y,p.y);
196 hi.x = (std::max)(hi.x,p.x); hi.y = (std::max)(hi.y,p.y);
198 pane.origin += (pane.u*lo.x + pane.v*lo.y)*.001f;
199 pane.fractures={};pane.uploadedCondition=-1;
202 pane.uploadedRevision = uint64_t(-1);
203 pane.geometry.Reset();
205 for(
size_t t=0;t<pane.vertices.size();t+=3)
207 const auto origin=Position(pane.vertices[t]);
208 const auto e1=(Position(pane.vertices[t+1])-origin)*1000.f;
209 const auto e2=(Position(pane.vertices[t+2])-origin)*1000.f;
211 pane.facets.push_back({origin,normal});
226 const Vector& relativeWind,
const std::vector<Matrix>& transforms,
float airDensity,
bool wiping,
float rainAir)
229 const float airSpeed =
Length(relativeWind);
230 for (
auto& pane :
panes)
232 if (
size_t(pane.component) >= transforms.size())
continue;
234 if (!
Inverse(inverse,transforms[pane.component]))
continue;
238 const Point wind{
Dot(localWind,pane.u),
Dot(localWind,pane.v)};
246 const auto center=
TransformPoint(pane.origin+(pane.u*pane.water.Width()
247 +pane.v*pane.water.Height())*.0005f,transforms[pane.component]);
248 if (center.z>0 && std::abs(normal.z)>.25f && std::abs(normal.z)>std::abs(normal.x))
251 const Point tangent{
Dot(up,pane.u),
Dot(up,pane.v)};
252 const float length=std::hypot(tangent.x,tangent.y);
255 jet.
direction={tangent.x/length,tangent.y/length};
256 jet.
speed=75.f*(std::clamp)(rainAir,0.f,1.f);
260 for (
const auto& vertex:pane.vertices)
262 const auto p=Project(Position(vertex),pane.origin,pane.u,pane.v);
265 jet.
top=(std::max)(jet.
top,height);
272 pane.water.Update(elapsed,rain,force,wind,airDensity,airSpeed,jet);
273 if (!wiping || !pane.water.Wet())
continue;
274 std::vector<WindshieldRain::Surface::BladeSweep> sweeps;
278 for (
const auto& wiper :
wipers)
280 if (
size_t(wiper.component) >= transforms.size())
continue;
284 const auto newMatrix = transforms[wiper.component]*inverse;
286 if (std::memcmp(&oldMatrix,&newMatrix,
sizeof(oldMatrix)) == 0)
continue;
287 for (
size_t i = 0; i+2 < wiper.vertices.size(); i += 3)
290 bool nearGlass =
true;
291 for (
int j = 0; j < 3; ++j)
293 const auto vertex = Position(wiper.vertices[i+j]);
299 for (
const auto& facet:pane.facets)
301 const auto delta=b-facet.origin;
302 distance=(std::min)(distance,std::abs(
Dot(delta,facet.normal)));
304 nearGlass &= distance < .00008f;
305 sweep.
previous[j] = Project(a,pane.origin,pane.u,pane.v);
306 sweep.
current[j] = Project(b,pane.origin,pane.u,pane.v);
308 if (!nearGlass)
continue;
309 sweeps.push_back(sweep);
312 pane.water.SweepBlades(sweeps,elapsed);
320 ComPtr<ID3D11Device1> device1;
HRESULT hr=
device.As(&device1);
if(FAILED(hr))
return hr;
321 const auto level=
device->GetFeatureLevel();
322 if(FAILED(hr=device1->CreateDeviceContextState(0,&level,1,D3D11_SDK_VERSION,__uuidof(ID3D11Device),
nullptr,&
isolated)))
return hr;
323 const D3D11_INPUT_ELEMENT_DESC position{
"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0};
324 const auto create=[&](
Program11& program,
const char* vsName,
const char* psName) {
327 return program.
Create(
device.Get(),vs.data(),vs.size(),ps.data(),ps.size(),&position,1);
337 if(FAILED(hr=
damage.
Create(
device.Get(),adVs.data(),adVs.size(),damagePs.data(),damagePs.size(),&position,1)))
343 if(strncmp(entry.name,
"Rain",4))
continue;
344 ComPtr<ID3D11SamplerState> sampler;
345 if(FAILED(hr=
device->CreateSamplerState(&entry.description,&sampler)))
return hr;
348 D3D11_DEPTH_STENCIL_DESC ds{};ds.DepthEnable=TRUE;ds.DepthWriteMask=D3D11_DEPTH_WRITE_MASK_ZERO;ds.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;
349 ds.FrontFace=ds.BackFace={D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_COMPARISON_ALWAYS};
351 ds.DepthEnable=FALSE;
if(FAILED(hr=
device->CreateDepthStencilState(&ds,&
noDepth)))
return hr;
352 ds.DepthEnable=TRUE;ds.StencilEnable=TRUE;ds.StencilReadMask=255;ds.StencilWriteMask=0;
353 ds.DepthFunc=D3D11_COMPARISON_ALWAYS;ds.FrontFace.StencilFunc=ds.BackFace.StencilFunc=D3D11_COMPARISON_GREATER;
355 ds.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;ds.FrontFace.StencilFunc=ds.BackFace.StencilFunc=D3D11_COMPARISON_EQUAL;
357 D3D11_BLEND_DESC bs{};
358 bs.RenderTarget[0]={FALSE,D3D11_BLEND_SRC_ALPHA,D3D11_BLEND_INV_SRC_ALPHA,D3D11_BLEND_OP_ADD,
359 D3D11_BLEND_SRC_ALPHA,D3D11_BLEND_INV_SRC_ALPHA,D3D11_BLEND_OP_ADD,D3D11_COLOR_WRITE_ENABLE_ALL};
360 if(FAILED(hr=
device->CreateBlendState(&bs,&
opaque)))
return hr;
361 bs.RenderTarget[0].BlendEnable=TRUE;
if(FAILED(hr=
device->CreateBlendState(&bs,&
alpha)))
return hr;
362 bs.IndependentBlendEnable=TRUE;
364 bs.RenderTarget[1]={TRUE,D3D11_BLEND_ONE,D3D11_BLEND_ONE,D3D11_BLEND_OP_MAX,
365 D3D11_BLEND_ONE,D3D11_BLEND_ONE,D3D11_BLEND_OP_MAX,D3D11_COLOR_WRITE_ENABLE_RED};
367 status=
"native windshield rain ready";
return S_OK;
440 const D3D11_VIEWPORT& viewport,
const std::vector<Matrix>& world,
const Matrix& viewProjection,
441 float opacity,
DepthLayout layout,
float cockpitLight,ID3D11RenderTargetView* adExclusion)
444 opacity=(std::clamp)(opacity,0.f,1.f);
446 const bool wet=opacity>0 &&
Wet();
447 if(
simulator || (!damaged && !wet))
return S_FALSE;
448 if(!
device || !
isolated || !caller || !target || caller->GetType()!=D3D11_DEVICE_CONTEXT_IMMEDIATE)
return E_UNEXPECTED;
449 if(!std::isfinite(viewport.Width) || !std::isfinite(viewport.Height) || viewport.Width<1 || viewport.Height<1
450 || !std::isfinite(viewport.MinDepth) || !std::isfinite(viewport.MaxDepth)
451 || viewport.MinDepth<0 || viewport.MaxDepth>1 || viewport.MinDepth>viewport.MaxDepth
452 || !std::isfinite(opacity) || !std::isfinite(cockpitLight))
return E_INVALIDARG;
453 ComPtr<ID3D11Device> callerDevice;caller->GetDevice(&callerDevice);
if(callerDevice!=
device)
return E_INVALIDARG;
454 std::vector<bool> visible(
panes.size(),
false);
bool visibleWater=
false;
455 for(
size_t i=0;i<
panes.size();++i)
457 const auto& pane=
panes[i];
if(
size_t(pane.component)>=world.size())
continue;
458 const auto wvp=world[pane.component]*viewProjection;
unsigned outside=63;
459 for(
const auto& vertex:pane.vertices)
461 const auto clip=
TransformClip(Position(vertex),wvp);
unsigned mask=0;
462 if(clip.x < -clip.w)mask|=1;
if(clip.x > clip.w)mask|=2;
463 if(clip.y < -clip.w)mask|=4;
if(clip.y > clip.w)mask|=8;
464 if(clip.z<0)mask|=16;
if(clip.z>clip.w)mask|=32;
467 visible[i]=outside==0;visibleWater|=visible[i] && (damaged || (wet && pane.water.Wet()));
469 if(!visibleWater)
return S_FALSE;
470 ComPtr<ID3D11Resource> resource;target->GetResource(&resource);ComPtr<ID3D11Texture2D> color;
471 HRESULT hr=resource.As(&color);
if(FAILED(hr))
return hr;
472 ComPtr<ID3D11Device> targetDevice;color->GetDevice(&targetDevice);
if(targetDevice!=
device)
return E_INVALIDARG;
473 D3D11_TEXTURE2D_DESC desc{};color->GetDesc(&desc);D3D11_RENDER_TARGET_VIEW_DESC view{};target->GetDesc(&view);
474 if(desc.ArraySize!=1 || (view.ViewDimension!=D3D11_RTV_DIMENSION_TEXTURE2D && view.ViewDimension!=D3D11_RTV_DIMENSION_TEXTURE2DMS)
475 || (view.ViewDimension==D3D11_RTV_DIMENSION_TEXTURE2D && view.Texture2D.MipSlice!=0)
476 || !std::isfinite(viewport.TopLeftX) || !std::isfinite(viewport.TopLeftY) || viewport.TopLeftX<0 || viewport.TopLeftY<0
477 || viewport.TopLeftX+viewport.Width>desc.Width || viewport.TopLeftY+viewport.Height>desc.Height)
return E_INVALIDARG;
478 desc.Format=view.Format;
481 ComPtr<ID3D11Resource> adResource;adExclusion->GetResource(&adResource);
482 ComPtr<ID3D11Texture2D> adTexture;
if(FAILED(hr=adResource.As(&adTexture)))
return hr;
483 ComPtr<ID3D11Device> adDevice;adTexture->GetDevice(&adDevice);
484 D3D11_TEXTURE2D_DESC adDesc{};adTexture->GetDesc(&adDesc);
485 D3D11_RENDER_TARGET_VIEW_DESC adView{};adExclusion->GetDesc(&adView);
486 if(adDevice!=
device || adTexture==color || adDesc.Width!=desc.Width || adDesc.Height!=desc.Height
487 || adDesc.SampleDesc.Count!=desc.SampleDesc.Count || adDesc.SampleDesc.Quality!=desc.SampleDesc.Quality
488 || adDesc.ArraySize!=1 || adView.Format!=DXGI_FORMAT_R8_UNORM || adView.ViewDimension!=view.ViewDimension
489 || (adView.ViewDimension==D3D11_RTV_DIMENSION_TEXTURE2D && adView.Texture2D.MipSlice!=0))
return E_INVALIDARG;
491 ComPtr<ID3D11DepthStencilView> readOnly;
494 D3D11_DEPTH_STENCIL_VIEW_DESC depthView{};depth->GetDesc(&depthView);
495 ComPtr<ID3D11Resource> depthResource;depth->GetResource(&depthResource);ComPtr<ID3D11Texture2D> depthTexture;
496 if(FAILED(hr=depthResource.As(&depthTexture)))
return hr;
497 ComPtr<ID3D11Device> depthDevice;depthTexture->GetDevice(&depthDevice);
if(depthDevice!=
device)
return E_INVALIDARG;
498 D3D11_TEXTURE2D_DESC depthDesc{};depthTexture->GetDesc(&depthDesc);
499 if(depthDesc.Width!=desc.Width || depthDesc.Height!=desc.Height || depthDesc.SampleDesc.Count!=desc.SampleDesc.Count
500 || depthDesc.SampleDesc.Quality!=desc.SampleDesc.Quality || depthDesc.ArraySize!=1
501 || (depthView.ViewDimension!=D3D11_DSV_DIMENSION_TEXTURE2D && depthView.ViewDimension!=D3D11_DSV_DIMENSION_TEXTURE2DMS)
502 || (depthView.ViewDimension==D3D11_DSV_DIMENSION_TEXTURE2D && depthView.Texture2D.MipSlice!=0))
return E_INVALIDARG;
503 const bool stencil=depthView.Format==DXGI_FORMAT_D24_UNORM_S8_UINT || depthView.Format==DXGI_FORMAT_D32_FLOAT_S8X24_UINT;
505 depthView.Flags|=D3D11_DSV_READ_ONLY_DEPTH|(stencil ? D3D11_DSV_READ_ONLY_STENCIL : 0);
506 if(FAILED(hr=
device->CreateDepthStencilView(depthTexture.Get(),&depthView,&readOnly)))
return hr;
509 ComPtr<ID3D11RasterizerState> raster;caller->RSGetState(&raster);
510 D3D11_RECT scissors[D3D11_VIEWPORT_AND_SCISSORRECT_OBJECT_COUNT_PER_PIPELINE]{};UINT scissorCount=UINT(std::size(scissors));
511 caller->RSGetScissorRects(&scissorCount,scissors);
512 ComPtr<ID3D11DepthStencilState> inheritedDepth;UINT reference=0;caller->OMGetDepthStencilState(&inheritedDepth,&reference);
515 D3D11_DEPTH_STENCIL_DESC inherited{};inheritedDepth->GetDesc(&inherited);
516 inherited.DepthEnable=TRUE;inherited.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;inherited.DepthWriteMask=D3D11_DEPTH_WRITE_MASK_ZERO;
517 inherited.StencilWriteMask=0;
518 inherited.FrontFace.StencilFailOp=inherited.FrontFace.StencilDepthFailOp=inherited.FrontFace.StencilPassOp=D3D11_STENCIL_OP_KEEP;
519 inherited.BackFace.StencilFailOp=inherited.BackFace.StencilDepthFailOp=inherited.BackFace.StencilPassOp=D3D11_STENCIL_OP_KEEP;
520 ComPtr<ID3D11DepthStencilState> next;
if(FAILED(hr=
device->CreateDepthStencilState(&inherited,&next)))
return hr;
521 inheritedDepth=std::move(next);
525 ComPtr<ID3D11DeviceContext1> context;
526 ComPtr<ID3DDeviceContextState> previous;
530 if(FAILED(hr=caller->QueryInterface(IID_PPV_ARGS(&restore.context))))
return hr;
531 auto* context=restore.context.Get();context->SwapDeviceContextState(
isolated.Get(),&restore.previous);
532 context->RSSetState(raster.Get());context->RSSetViewports(1,&viewport);
533 context->RSSetScissorRects(scissorCount,scissors);
534 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
537 if(desc.SampleDesc.Count>1)context->ResolveSubresource(
scene.
texture.Get(),0,color.Get(),0,desc.Format);
538 else context->CopySubresourceRegion(
scene.
texture.Get(),0,0,0,0,color.Get(),0,
nullptr);
539 parameters.
Set(
"_risePixelCenterOffset",DirectX::XMFLOAT2(1/viewport.Width,-1/viewport.Height));
543 if(pane.
vertices.size()>UINT_MAX/
sizeof(
Vertex))
return E_INVALIDARG;
544 D3D11_BUFFER_DESC buffer{};buffer.ByteWidth=UINT(pane.
vertices.size()*
sizeof(
Vertex));buffer.Usage=D3D11_USAGE_IMMUTABLE;buffer.BindFlags=D3D11_BIND_VERTEX_BUFFER;
545 const D3D11_SUBRESOURCE_DATA initial{pane.
vertices.data(),0,0};
546 const HRESULT created=
device->CreateBuffer(&buffer,&initial,&pane.
geometry);
if(FAILED(created))
return created;
548 auto* buffer=pane.
geometry.Get();
const UINT stride=
sizeof(
Vertex),offset=0;
549 context->IASetVertexBuffers(0,1,&buffer,&stride,&offset);
550 const HRESULT bound=program.Bind(context,
parameters);
if(FAILED(bound))
return bound;
553 context->OMSetDepthStencilState(
worldDepth.Get(),3);context->Draw(UINT(pane.
vertices.size()),0);
554 context->OMSetDepthStencilState(
cockpitDepth.Get(),3);context->Draw(UINT(pane.
vertices.size()),0);
558 context->OMSetDepthStencilState(!depth ?
noDepth.Get() : inheritedDepth ? inheritedDepth.Get() :
unifiedDepth.Get(),reference);
559 context->Draw(UINT(pane.
vertices.size()),0);
564 const float clear[4]{};context->ClearRenderTargetView(mask,clear);context->OMSetBlendState(
opaque.Get(),
nullptr,0xffffffff);
565 for(
size_t i=0;i<
panes.size();++i)
if(visible[i])
570 context->OMSetRenderTargets(0,
nullptr,
nullptr);
572 ID3D11RenderTargetView* targets[]={target,adExclusion};
573 context->OMSetRenderTargets(adExclusion ? 2 : 1,targets,readOnly.Get());
578 for(
size_t i=0;i<
panes.size();++i)
580 auto& pane=
panes[i];
if(!wet || !visible[i] || !pane.
water.
Wet())
continue;
581 if(FAILED(hr=
Upload(context,pane)))
return hr;
596 ID3D11ShaderResourceView* empty[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT]{};
597 context->PSSetShaderResources(0,UINT(std::size(empty)),empty);
598 context->OMSetRenderTargets(0,
nullptr,
nullptr);
599 if(desc.SampleDesc.Count>1)context->ResolveSubresource(
scene.
texture.Get(),0,color.Get(),0,desc.Format);
600 else context->CopySubresourceRegion(
scene.
texture.Get(),0,0,0,0,color.Get(),0,
nullptr);
601 context->OMSetRenderTargets(adExclusion ? 2 : 1,targets,readOnly.Get());
603 for(
size_t i=0;i<
panes.size();++i)
605 auto& pane=
panes[i];
if(!visible[i])
continue;
611 D3D11_MAPPED_SUBRESOURCE map{};
612 if(FAILED(hr=context->Map(pane.
fractures.
texture.Get(),0,D3D11_MAP_WRITE_DISCARD,0,&map)))
return hr;
613 for(UINT y=0;y<size;++y)memcpy(static_cast<char*>(map.pData)+size_t(y)*map.RowPitch,
614 field.data()+size_t(y)*size,size*
sizeof(field[0]));