Files
client/tests/world_weather_tests.h

73 lines
6.6 KiB
C++

#pragma once
#include "../src/game/world_weather.h"
inline void test_world_weather(D3D_DRIVER_TYPE driver=D3D_DRIVER_TYPE_WARP) {
using namespace world_weather;
float matrix[16]{};matrix[0]=matrix[5]=1;matrix[10]=-1.001f;matrix[11]=-1;matrix[14]=-.1f;
projection found;assert(find_projection(matrix,16,1,1,found) && found.b<0);
assert(!find_projection(matrix,16,2,1,found));matrix[15]=1;assert(!find_projection(matrix,16,1,1,found));
matrix[15]=0;matrix[10]=0;matrix[14]=.1f;assert(find_projection(matrix,16,1,1,found) && found.b>0);
matrix[10]=-1.001f;matrix[14]=-.1f;
ComPtr<ID3D11Device> device;ComPtr<ID3D11DeviceContext> context;
const D3D_FEATURE_LEVEL levels[]={D3D_FEATURE_LEVEL_12_1,D3D_FEATURE_LEVEL_12_0,D3D_FEATURE_LEVEL_11_1,D3D_FEATURE_LEVEL_11_0};
assert(SUCCEEDED(D3D11CreateDevice(nullptr,driver,nullptr,0,levels,4,D3D11_SDK_VERSION,&device,nullptr,&context)));
D3D11_TEXTURE2D_DESC td{};td.Width=td.Height=256;td.MipLevels=td.ArraySize=1;td.SampleDesc.Count=1;
td.Format=DXGI_FORMAT_R8G8B8A8_UNORM;td.BindFlags=D3D11_BIND_RENDER_TARGET;
ComPtr<ID3D11Texture2D> image;ComPtr<ID3D11RenderTargetView> target;
assert(SUCCEEDED(device->CreateTexture2D(&td,nullptr,&image)));assert(SUCCEEDED(device->CreateRenderTargetView(image.Get(),nullptr,&target)));
td.Format=DXGI_FORMAT_D32_FLOAT;td.BindFlags=D3D11_BIND_DEPTH_STENCIL;
ComPtr<ID3D11Texture2D> depth_texture;ComPtr<ID3D11DepthStencilView> dsv;
assert(SUCCEEDED(device->CreateTexture2D(&td,nullptr,&depth_texture)));assert(SUCCEEDED(device->CreateDepthStencilView(depth_texture.Get(),nullptr,&dsv)));
td.Format=DXGI_FORMAT_R8G8B8A8_UNORM;td.BindFlags=0;td.Usage=D3D11_USAGE_STAGING;td.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
ComPtr<ID3D11Texture2D> staging;assert(SUCCEEDED(device->CreateTexture2D(&td,nullptr,&staging)));
ID3D11RenderTargetView* rt=target.Get();context->OMSetRenderTargets(1,&rt,dsv.Get());
D3D11_VIEWPORT viewport{0,0,256,256,0,1};context->RSSetViewports(1,&viewport);
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
D3D11_BUFFER_DESC bd{};bd.ByteWidth=64;bd.BindFlags=D3D11_BIND_CONSTANT_BUFFER;D3D11_SUBRESOURCE_DATA initial{matrix,0,0};
ComPtr<ID3D11Buffer> native_cb;assert(SUCCEEDED(device->CreateBuffer(&bd,&initial,&native_cb)));
ID3D11Buffer* cb=native_cb.Get();context->VSSetConstantBuffers(0,1,&cb);
world::frame scene;scene.valid=true;scene.fov_x=scene.fov_y=1;scene.matrix[0]=scene.matrix[5]=scene.matrix[10]=scene.matrix[15]=1;
enabled=true;expected_x=expected_y=1;
observe({matrix,sizeof(matrix),reinterpret_cast<uintptr_t>(native_cb.Get())});
assert(discover(device.Get(),context.Get(),scene,found) && found.b<0);
settings options;options.snow=1;options.radius=8;options.snow_color=IM_COL32(255,0,0,255);
auto render=[&](float clear_depth,projection p,float camera_x) {
const float black[4]{0,0,0,1};context->ClearRenderTargetView(rt,black);context->ClearDepthStencilView(dsv.Get(),D3D11_CLEAR_DEPTH,clear_depth,0);
scene.camera.x=camera_x;assert(draw(device.Get(),context.Get(),scene,rt,dsv.Get(),viewport,p,options,3.5));
D3D11_PRIMITIVE_TOPOLOGY topology;context->IAGetPrimitiveTopology(&topology);assert(topology==D3D11_PRIMITIVE_TOPOLOGY_LINELIST);
ComPtr<ID3D11RenderTargetView> restored;ComPtr<ID3D11DepthStencilView> restored_depth;context->OMGetRenderTargets(1,&restored,&restored_depth);
assert(restored.Get()==rt && restored_depth.Get()==dsv.Get());
ComPtr<ID3D11Buffer> restored_cb;context->VSGetConstantBuffers(0,1,&restored_cb);assert(restored_cb.Get()==native_cb.Get());
context->CopyResource(staging.Get(),image.Get());D3D11_MAPPED_SUBRESOURCE map{};assert(SUCCEEDED(context->Map(staging.Get(),0,D3D11_MAP_READ,0,&map)));
uint64_t sum=0,hash=0;for(int y=0;y<256;++y)for(int x=0;x<256;++x) {auto* pixel=static_cast<unsigned char*>(map.pData)+y*map.RowPitch+x*4;sum+=pixel[0];hash=hash*31+pixel[0];}
context->Unmap(staging.Get(),0);return std::make_pair(sum,hash);
};
const auto visible=render(1,{-1.001f,-.1f},0);assert(visible.first>0);
assert(render(0,{-1.001f,-.1f},0).first==0);
assert(render(1,{-1.001f,-.1f},.5f).second!=visible.second);
assert(render(0,{0,.1f},0).first>0);assert(render(1,{0,.1f},0).first==0);
options.snow=0;options.rain=1;options.rain_color=IM_COL32(255,0,0,255);assert(render(1,{-1.001f,-.1f},0).first>0);
// Read the actual camera-layout projection path without any GPU staging copy.
alignas(16) unsigned char client_data[0x800]{},renderer_data[0x500]{},camera_data[0x1100]{};
const auto old_client=world::client.load();world::client=reinterpret_cast<uintptr_t>(client_data);
const uintptr_t renderer_pointer=reinterpret_cast<uintptr_t>(renderer_data),camera_pointer=reinterpret_cast<uintptr_t>(camera_data);
memcpy(client_data+0x1c0,&renderer_pointer,sizeof(renderer_pointer));memcpy(renderer_data+0x468,&camera_pointer,sizeof(camera_pointer));
float camera_projection_data[16]{};memcpy(camera_projection_data,matrix,sizeof(matrix));camera_projection_data[14]=-.025f;
memcpy(camera_data+0xf58,camera_projection_data,sizeof(camera_projection_data));
assert(camera_projection(scene));assert(discover(device.Get(),context.Get(),scene,found) && std::abs(found.b+.025f)<.000001f);
camera_projection_data[14]=-.05f;camera_projection_data[10]=-1.002f;memcpy(camera_data+0xf58,camera_projection_data,sizeof(camera_projection_data));
assert(camera_projection(scene));assert(discover(device.Get(),context.Get(),scene,found) && std::abs(found.a+1.001f)<.00001f && std::abs(found.b+.025f)<.000001f);
world::client=old_client;
// Run both upload observers against the real adapter without staging readback.
original_update=+[](ID3D11DeviceContext* c,ID3D11Resource* r,UINT sub,const D3D11_BOX* box,const void* data,UINT row,UINT depth){c->UpdateSubresource(r,sub,box,data,row,depth);};
cached_buffer=0;enabled=true;update_detour(context.Get(),native_cb.Get(),0,nullptr,matrix,0,0);
assert(discover(device.Get(),context.Get(),scene,found));
draw_context=context.Get();draw_thread=GetCurrentThreadId();
static int boundaries=0;boundaries=0;
before_target_change=+[](ID3D11DeviceContext* c){ComPtr<ID3D11DepthStencilView> d;c->OMGetRenderTargets(0,nullptr,&d);assert(d);++boundaries;};
target_boundary(context.Get(),dsv.Get());assert(boundaries==0);
target_boundary(context.Get(),nullptr);assert(boundaries==1);
before_target_change=nullptr;
shutdown();assert(!enabled && !vs);
std::cout<<"PASS: world-space rain/snow shaders, native projection matching, standard/reverse depth occlusion, camera parallax and complete pipeline restoration (GPU)\n";
}