1
2
3void TestCloudLower(Device11& gpu,CloudRenderer11&
renderer,
bool organic=
false)
4{
5 const char probeSource[]=R"(
6#include "CloudVolume.hlsl"
7uint probeFault;
8float4 CheckLowerMarch(float4 pixel:SV_Position):SV_Target
9{
10 uint index=uint(pixel.y)*160+uint(pixel.x);
11 float vertical=1-2*(index+.5)/19200;
12 float longitude=index*2.39996323;
13 float horizontal=sqrt(max(0,1-vertical*vertical));
14 float3 radial=float3(cos(longitude)*horizontal,vertical,sin(longitude)*horizontal);
15 if(index%4==0)radial=normalize(float3((pixel.x/160-.5)*.065,1,(pixel.y/120-.5)*.065));
16 float h=cloudFloorBase()-.4+1.4*pixel.y/120;
17 if(uint(pixel.x)%17==0)h=lerp(layer.y-.5,layer.z+.8,pixel.y/120);
18 if(uint(pixel.x)%5==0)h=cloudFloorBase()+.5+(float(int(index%3)-1))*.0003;
19 if(uint(pixel.x)%7==0)h=layer.y+(float(int(index%3)-1))*.0003;
20 if(uint(pixel.x)%11==0)h=layer.z-.301+(float(int(index%3)-1))*.0003;
21 if(uint(pixel.x)%13==0)h=cloudFloorBase()+.799+(float(int(index%3)-1))*.0003;
22 if(uint(pixel.x)%19==0)h=layer.z-.5+(float(int(index%3)-1))*.0003;
23 if(uint(pixel.x)%23==0)h=layer.z-.501+(float(int(index%3)-1))*.0003;
24 float3 p=radial*(layer.x+h);
25 float altitude=length(p)-layer.x;
26 float footprint=exp2(float(index%12)-11);
27 float visibility=1-smoothstep(-.104528,.052336,dot(normalize(p),-lightDirection.xyz));
28 float4 a=cloudSample(p,footprint);
29 float4 b=RiseMarchCloudSample(p,footprint,altitude,visibility);
30 float escapeA=lerp(1,.04,smoothstep(0,1,saturate((altitude-layer.y)/max(1,altitude+a.y-layer.y))));
31 float escapeB=lerp(1,.04,smoothstep(0,1,saturate((altitude-layer.y)/max(1,altitude+b.y-layer.y))));
32 precise float3 emissionA=airglowColor.rgb*visibility*(.15+.85*exp(-.125*(altitude-9)*(altitude-9)))*escapeA;
33 precise float3 emissionB=airglowColor.rgb*visibility*(.15+.85*exp(-.125*(altitude-9)*(altitude-9)))*escapeB;
34 bool fast=b.y==0 && a.y!=0;
35 return float4(abs(a.x-b.x)+abs(RiseLegacyCloudDensity(p,footprint)-RiseLegacyCloudDensityReference(p,footprint))+(probeFault!=0 && index==0 ? 1 : 0),abs(a.w-b.w),abs(a.z-b.z),
36 any(emissionA!=emissionB) ? -1 : (fast ? 3 : 2));
37}
38)";
39 const D3D_SHADER_MACRO macros[]={{"RISE_SCENE_SAMPLES","1"},
40 {organic ? "RISE_CLOUD_ORGANIC_FLIGHT" : nullptr,"1"},{nullptr,nullptr}};
41 ComPtr<ID3DBlob> code,errors;
42 const char*
path=
"Rendering/Shaders/LowerMarchProbe.hlsl";
43 const auto hr=D3DCompile(probeSource,strlen(probeSource),path,macros,D3D_COMPILE_STANDARD_FILE_INCLUDE,"CheckLowerMarch","ps_5_0",
44 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_WARNINGS_ARE_ERRORS|D3DCOMPILE_PACK_MATRIX_ROW_MAJOR|D3DCOMPILE_OPTIMIZATION_LEVEL3,
45 0,&code,&errors);
46 if(errors)std::cerr<<static_cast<const char*>(errors->GetBufferPointer());HR(hr,"compile lower march probe");
47 auto vertex=Compile("Fullscreen","vs_5_0",1);Program11 program;
48 HR(program.Create(gpu.Device(),vertex->GetBufferPointer(),vertex->GetBufferSize(),code->GetBufferPointer(),code->GetBufferSize()),"lower march program");
49 Pipeline pipeline(gpu.Device(),1);Scene scene(gpu.Device(),1,Width,Height,DXGI_FORMAT_R32G32B32A32_FLOAT);
50 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};
51 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&view,XMMatrixIdentity());
52 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,384));
53 auto* context=gpu.Context();unsigned long long points=0,fast=0,fallback=0,mismatches=0;float largest=0;
57 {
58 auto map=std::make_shared<CloudWeather::Map>();const unsigned w=64,h=32;
59 map->mips.push_back({w,h,std::vector<uint8_t>(w*h)});
60 for(unsigned y=0;y<h;++y)for(unsigned x=0;x<w;++x)
62 :
weather==5 ? uint8_t(((x/3+y/3)%2) ? 255 : 64) : uint8_t((x*17+y*23)%256);
64 for(
unsigned i=0;i<100 &&
renderer.WeatherFrame().current!=map;++i)HR(
renderer.PrepareResources(context),
"probe weather upload");
65 Check(
renderer.WeatherFrame().current==map,
"probe weather complete");
68 auto local=std::make_shared<CloudBaseWeather::Grid>();local->cube=0;local->u=local->v=1019;
69 for(unsigned y=0;y<64;++y)for(unsigned x=0;x<64;++x)
70 local->cells[y*64+x]={.05f+.02f*x,float(y)/63,(x>25 && x<35) ? .25f : 1.f,0};
71 local->Bounds();cloud.
localBase={local,1};
72 scene.Clear(context);
73 HR(
renderer.Render(context,scene.target.Get(),scene.depth,viewport,view,projection,{0,cloud.radius+.3f,0},cloud,
74 {0,-1,0},{1,1,1,1},{.3f,.4f,.5f,1}),"probe resource setup");
75 for(unsigned layerCase=0;layerCase<6;++layerCase)for(unsigned lightCase=0;lightCase<4;++lightCase)for(unsigned offset=0;offset<3;++offset)
76 {
77 const float base=layerCase==0 ? 0 : layerCase==1 ? .05f : layerCase==2 ? 2.668316f : layerCase==3 ? 9.8f : .45f;
78 const float top=layerCase==3 ? 10.f : layerCase==4 ? .6f : 11.132220f;
80
81 if(
weather>=3)values.Set(
"eye",XMFLOAT4(0,cloud.
radius+base+.799f+(
float(offset)-1)*.001f,0,0));
82 if(
weather==5)values.Set(
"eye",XMFLOAT4(0,cloud.
radius+(offset==0 ? 6.369629f : offset==1 ? 10.f : 15.f),0,0));
83 values.Set(
"noiseOffset",XMFLOAT4(offset==0 ? 0 : offset==1 ? 3199.99f : -3199.99f,
float(
weather)*37.1f,-
float(
weather)*113.3f,0));
84 values.Set("airglowColor",lightCase==0 ? XMFLOAT4(0,0,0,0) : XMFLOAT4(.05f,.08f,.11f,1));
85 values.Set("lightDirection",lightCase<2 ? XMFLOAT4(0,-1,0,0) : lightCase==2 ? XMFLOAT4(0,1,0,0) : XMFLOAT4(1,0,0,0));
86 if(layerCase!=5)values.Set("localBaseInfo",XMFLOAT4(0,0,0,0));
87 values.Set("probeFault",0u);
88 const auto draw=[&]()
89 {
90 context->ClearState();scene.Clear(context);
auto*
target=scene.target.Get();
91 context->OMSetRenderTargets(1,&target,nullptr);context->RSSetViewports(1,&viewport);context->RSSetState(pipeline.raster.Get());
92 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
93 const auto bound=program.Bind(context,values);
94 if(FAILED(bound))std::cerr<<program.Status()<<"\n";
95 HR(bound,"bind lower march probe");context->Draw(3,0);context->OMSetRenderTargets(0,nullptr,nullptr);
96 return pipeline.Pixels(gpu,scene,1);
97 };
98 if(
weather==0 && layerCase==0 && lightCase==0 && offset==0)
99 {
100 values.Set("probeFault",1u);const auto control=draw();
101 Check(control[0].color[0]>=1,"lower march probe detects deliberately injected error");values.Set("probeFault",0u);
102 }
103 for(const auto& sample:draw())
104 {
105 const auto& c=sample.color;
106 Check(std::isfinite(c[0]) && std::isfinite(c[1]) && std::isfinite(c[2]),"finite lower march difference");
107 Check(c[3]==-1 || c[3]==2 || c[3]==3,"lower march probe executed");
108 if(c[0]!=0 || c[1]!=0 || c[2]!=0 || c[3]<0)
109 {
110 if(mismatches<8)std::cout<<
"Mismatch weather="<<
weather<<
" layer="<<layerCase<<
" light="<<lightCase<<
" offset="<<offset
111 <<" density="<<c[0]<<" extinction="<<c[1]<<" weather="<<c[2]<<" emission="<<c[3]<<"\n";
112 ++mismatches;
113 }
114 largest=(std::max)(largest,(std::max)(c[0],(std::max)(c[1],c[2])));
115 fast+=c[3]==3;fallback+=c[3]==2;++points;
116 }
117 }
118 }
119 std::cout<<"Lower march probe organic="<<organic<<": "<<points<<" points, "<<fast<<" fast, "<<fallback
120 <<" fallback, "<<mismatches<<" mismatches, maximum error "<<largest<<"\n";
121 Check(fast>10000 && fallback>10000,"exercise lower shortcut and fallback");Check(mismatches==0,"exact lower march field and emission");
122 renderer.SetRainGrid(
nullptr);DebugMessages(gpu);
123}
CloudWeather::Frame weather
CloudBaseWeather::Frame localBase