Rise
The Vieneo Province
Loading...
Searching...
No Matches
ReentryEnvelope11.cpp
Go to the documentation of this file.
1#include "ReentryEnvelope11.h"
2#include "ShaderBytecode11.h"
4#include <cstring>
5using namespace DirectX;
6using Microsoft::WRL::ComPtr;
7namespace Rendering
8{
10 {
11 program.Reset();vertices.Reset();indices.Reset();state.Reset();raster.Reset();
12 depthState.Reset();blend.Reset();flow.Reset();flowSampler.Reset();device.Reset();
13 }
14 HRESULT ReentryEnvelope11::Initialize(ID3D11Device* native,const std::filesystem::path& directory)
15 {
16 Reset();if(!native)return E_INVALIDARG;device=native;
17 ComPtr<ID3D11Device1> device1;HRESULT hr=device.As(&device1);if(FAILED(hr))return hr;
18 const auto level=device->GetFeatureLevel();
19 if(FAILED(hr=device1->CreateDeviceContextState(0,&level,1,D3D11_SDK_VERSION,__uuidof(ID3D11Device),nullptr,&state)))return hr;
20 const auto vs=ReadShaderBytecode11(directory/"ReentryEnvelope-envelopeVertex-1.cso");
21 const auto ps=ReadShaderBytecode11(directory/"ReentryEnvelope-envelopePixel-1.cso");
22 const D3D11_INPUT_ELEMENT_DESC layout[]={
23 {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_VERTEX_DATA,0},
24 {"NORMAL",0,DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D11_INPUT_PER_VERTEX_DATA,0},
25 {"TEXCOORD",0,DXGI_FORMAT_R32G32_FLOAT,0,24,D3D11_INPUT_PER_VERTEX_DATA,0},
26 {"TEXCOORD",1,DXGI_FORMAT_R32G32B32_FLOAT,0,32,D3D11_INPUT_PER_VERTEX_DATA,0},
27 {"TEXCOORD",2,DXGI_FORMAT_R32G32B32_FLOAT,0,44,D3D11_INPUT_PER_VERTEX_DATA,0}};
28 if(FAILED(hr=program.Create(device.Get(),vs.data(),vs.size(),ps.data(),ps.size(),layout,5)))return hr;
29 // Embed the authored flow field, including filtered mips. The released
30 // client does not depend on Originals, a loose DDS, or a glass fallback.
31 std::vector<std::vector<uint8_t>> levels(1);
32 levels.front().assign(std::begin(ReentryFlowData::Pixels),std::end(ReentryFlowData::Pixels));
33 for(unsigned size=ReentryFlowData::Size;size>1;size/=2)
34 {
35 const auto& previous=levels.back();const unsigned next=size/2;
36 std::vector<uint8_t> pixels(next*next*next);
37 for(unsigned z=0;z<next;++z)for(unsigned y=0;y<next;++y)for(unsigned x=0;x<next;++x)
38 {
39 unsigned sum=0;
40 for(unsigned dz=0;dz<2;++dz)for(unsigned dy=0;dy<2;++dy)for(unsigned dx=0;dx<2;++dx)
41 sum+=previous[((z*2+dz)*size+y*2+dy)*size+x*2+dx];
42 pixels[(z*next+y)*next+x]=uint8_t((sum+4)/8);
43 }
44 levels.push_back(std::move(pixels));
45 }
46 std::vector<D3D11_SUBRESOURCE_DATA> flowData;
47 unsigned flowSize=ReentryFlowData::Size;
48 for(const auto& mip:levels){flowData.push_back({mip.data(),flowSize,flowSize*flowSize});flowSize=(std::max)(1u,flowSize/2);}
49 D3D11_TEXTURE3D_DESC td{};td.Width=td.Height=td.Depth=ReentryFlowData::Size;
50 td.MipLevels=UINT(levels.size());td.Format=DXGI_FORMAT_R8_UNORM;
51 td.Usage=D3D11_USAGE_IMMUTABLE;td.BindFlags=D3D11_BIND_SHADER_RESOURCE;
52 ComPtr<ID3D11Texture3D> texture;
53 if(FAILED(hr=device->CreateTexture3D(&td,flowData.data(),&texture)))return hr;
54 if(FAILED(hr=device->CreateShaderResourceView(texture.Get(),nullptr,&flow)))return hr;
55 D3D11_SAMPLER_DESC sd{};sd.Filter=D3D11_FILTER_MIN_MAG_MIP_LINEAR;
56 sd.AddressU=sd.AddressV=sd.AddressW=D3D11_TEXTURE_ADDRESS_WRAP;sd.MaxLOD=D3D11_FLOAT32_MAX;
57 if(FAILED(hr=device->CreateSamplerState(&sd,&flowSampler)))return hr;
58 // Retain sub-metre glow without modifying per-vertex clip depth (which
59 // corrupts interpolated depth on triangles crossing an external eye).
60 // The rasterizer still clips w/x/y; this compact effect is within the
61 // world far plane. World depth and cockpit stencil remain read-only.
62 D3D11_RASTERIZER_DESC rs{};rs.FillMode=D3D11_FILL_SOLID;rs.CullMode=D3D11_CULL_NONE;rs.DepthClipEnable=FALSE;rs.MultisampleEnable=TRUE;
63 if(FAILED(hr=device->CreateRasterizerState(&rs,&raster)))return hr;
64 D3D11_DEPTH_STENCIL_DESC ds{};ds.DepthEnable=TRUE;ds.DepthWriteMask=D3D11_DEPTH_WRITE_MASK_ZERO;ds.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;
65 ds.StencilEnable=TRUE;ds.StencilReadMask=255;ds.StencilWriteMask=0;
66 ds.FrontFace={D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_COMPARISON_GREATER};ds.BackFace=ds.FrontFace;
67 if(FAILED(hr=device->CreateDepthStencilState(&ds,&depthState)))return hr;
68 // Optically thin additive shell, order independent, preserving scene alpha.
69 D3D11_BLEND_DESC bs{};bs.RenderTarget[0]={TRUE,D3D11_BLEND_ONE,D3D11_BLEND_ONE,D3D11_BLEND_OP_ADD,
70 D3D11_BLEND_ZERO,D3D11_BLEND_ONE,D3D11_BLEND_OP_ADD,
71 D3D11_COLOR_WRITE_ENABLE_RED|D3D11_COLOR_WRITE_ENABLE_GREEN|D3D11_COLOR_WRITE_ENABLE_BLUE};
72 if(FAILED(hr=device->CreateBlendState(&bs,&blend)))return hr;
73 D3D11_BUFFER_DESC bd{};bd.Usage=D3D11_USAGE_DYNAMIC;bd.BindFlags=D3D11_BIND_VERTEX_BUFFER;bd.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE;
75 if(FAILED(hr=device->CreateBuffer(&bd,nullptr,&vertices)))return hr;
76 const auto mesh=ReentryEnvelope::Generate({{{0,0,0},1}},{0,0,1});
77 bd={};bd.Usage=D3D11_USAGE_IMMUTABLE;bd.BindFlags=D3D11_BIND_INDEX_BUFFER;bd.ByteWidth=UINT(mesh.indices.size()*sizeof(uint16_t));
78 D3D11_SUBRESOURCE_DATA data{mesh.indices.data(),0,0};
79 return device->CreateBuffer(&bd,&data,&indices);
80 }
81 HRESULT ReentryEnvelope11::Render(ID3D11DeviceContext* context,ID3D11RenderTargetView* scene,const SceneDepth11& depth,
82 const D3D11_VIEWPORT& viewport,const XMFLOAT4X4& view,const XMFLOAT4X4& projection,
83 const ReentryEnvelope::Mesh& mesh,float strength,float time,float transmission,float visibilityKm)
84 {
85 if(!std::isfinite(strength)||strength<=0||mesh.vertices.empty())return S_OK;
86 if(!Ready()||!context||!scene||!depth.ReadView()||!vertices||!indices||
88 !std::isfinite(time)||!std::isfinite(transmission)||!std::isfinite(visibilityKm))return E_INVALIDARG;
89 XMVECTOR determinant;XMFLOAT4X4 inverse;
90 XMStoreFloat4x4(&inverse,XMMatrixInverse(&determinant,XMLoadFloat4x4(&view)));
91 if(std::abs(XMVectorGetX(determinant))<1e-20f)return E_INVALIDARG;
92 D3D11_MAPPED_SUBRESOURCE mapped{};
93 HRESULT hr=context->Map(vertices.Get(),0,D3D11_MAP_WRITE_DISCARD,0,&mapped);if(FAILED(hr))return hr;
94 std::memcpy(mapped.pData,mesh.vertices.data(),mesh.vertices.size()*sizeof(ReentryEnvelope::Vertex));context->Unmap(vertices.Get(),0);
95 Parameters11 values;values.Set("envelopeView",view);values.Set("envelopeProjection",projection);
96 values.Set("envelopeCamera",XMFLOAT4(inverse._41,inverse._42,inverse._43,0));
97 values.Set("envelopeFlow",XMFLOAT4(mesh.windward.x,mesh.windward.y,mesh.windward.z,mesh.radius));
98 values.Set("envelopeMaterialFlow",XMFLOAT4(mesh.materialWindward.x,mesh.materialWindward.y,mesh.materialWindward.z,0));
99 values.Set("envelopeTuning",XMFLOAT4(ReentryEnvelope::Saturate(strength),time,ReentryEnvelope::Saturate(transmission),visibilityKm));
100 values.Texture("envelopeArtwork",flow.Get());values.Sampler("envelopeArtworkSampler",flowSampler.Get());
101 struct Restore
102 {
103 ComPtr<ID3D11DeviceContext1> context;ComPtr<ID3DDeviceContextState> previous;
104 ~Restore(){if(previous){context->OMSetRenderTargets(0,nullptr,nullptr);context->SwapDeviceContextState(previous.Get(),nullptr);}}
105 } restore;
106 if(FAILED(hr=context->QueryInterface(IID_PPV_ARGS(&restore.context))))return hr;
107 restore.context->SwapDeviceContextState(state.Get(),&restore.previous);
108 context->RSSetState(raster.Get());context->RSSetViewports(1,&viewport);
109 context->OMSetRenderTargets(1,&scene,depth.ReadView());context->OMSetDepthStencilState(depthState.Get(),3);
110 context->OMSetBlendState(blend.Get(),nullptr,UINT_MAX);
111 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
112 const UINT stride=sizeof(ReentryEnvelope::Vertex),offset=0;
113 context->IASetVertexBuffers(0,1,vertices.GetAddressOf(),&stride,&offset);context->IASetIndexBuffer(indices.Get(),DXGI_FORMAT_R16_UINT,0);
114 // A narrow emissive sheet plus a softer outer layer gives the flowing
115 // material depth without turning the envelope into an opaque surface.
116 for(const auto layer:{XMFLOAT4(-.015f,.82f,0,0),XMFLOAT4(.025f,.32f,1,0)})
117 {
118 values.Set("envelopeLayer",layer);
119 if(FAILED(hr=program.Bind(context,values)))return hr;
120 context->DrawIndexed(ReentryEnvelope::Rings*ReentryEnvelope::Segments*6,0,0);
121 }
122 return S_OK;
123 }
124}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
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)
Microsoft::WRL::ComPtr< ID3D11Device > device
Microsoft::WRL::ComPtr< ID3D11Buffer > vertices
HRESULT Render(ID3D11DeviceContext *context, ID3D11RenderTargetView *scene, const SceneDepth11 &depth, const D3D11_VIEWPORT &viewport, const DirectX::XMFLOAT4X4 &view, const DirectX::XMFLOAT4X4 &projection, const ReentryEnvelope::Mesh &mesh, float strength, float time, float transmission, float visibilityKm)
Microsoft::WRL::ComPtr< ID3D11SamplerState > flowSampler
Microsoft::WRL::ComPtr< ID3DDeviceContextState > state
Microsoft::WRL::ComPtr< ID3D11RasterizerState > raster
Microsoft::WRL::ComPtr< ID3D11DepthStencilState > depthState
Microsoft::WRL::ComPtr< ID3D11ShaderResourceView > flow
Microsoft::WRL::ComPtr< ID3D11Buffer > indices
Microsoft::WRL::ComPtr< ID3D11BlendState > blend
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &directory={})
ID3D11DepthStencilView * ReadView() const
Definition Device11.h:19
Definition DDS.h:24
constexpr unsigned Rings
constexpr unsigned Segments
Mesh Generate(const std::vector< Sphere > &spheres, Vector3 windward)
float Saturate(float x)
constexpr uint8_t Pixels[262144]
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
Model.
std::vector< Vertex > vertices