Rise
The Vieneo Province
Loading...
Searching...
No Matches
RunwayLights11.cpp
Go to the documentation of this file.
1#include "RunwayLights11.h"
2#include "ShaderBytecode11.h"
3#include "FrameProfiler11.h"
4#include <algorithm>
5#include <cstring>
6
7namespace Rendering
8{
10 {
12 }
13 HRESULT RunwayLights11::Initialize(ID3D11Device* native)
14 {
15 Reset();
16 const auto vs=ReadShaderBytecode11(L"RunwayLights-Vertex-1.cso");
17 const auto ps=ReadShaderBytecode11(L"RunwayLights-Pixel-1.cso");
18 if(vs.empty() || ps.empty())return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
19 const D3D11_INPUT_ELEMENT_DESC layout[]={
20 {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT,0,0,D3D11_INPUT_PER_INSTANCE_DATA,1},
21 {"PSIZE",0,DXGI_FORMAT_R32_FLOAT,0,12,D3D11_INPUT_PER_INSTANCE_DATA,1},
22 {"INTENSITY",0,DXGI_FORMAT_R32_FLOAT,0,16,D3D11_INPUT_PER_INSTANCE_DATA,1}};
23 static_assert(sizeof(RunwayLightInstance11)==20,"runway instance layout");
24 HRESULT hr=program.Create(native,vs.data(),vs.size(),ps.data(),ps.size(),layout,UINT(std::size(layout)));
25 if(FAILED(hr))return hr;
26 D3D11_SAMPLER_DESC desc{};
27 desc.Filter=D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
28 desc.AddressU=desc.AddressV=desc.AddressW=D3D11_TEXTURE_ADDRESS_CLAMP;
29 desc.ComparisonFunc=D3D11_COMPARISON_NEVER;desc.MaxLOD=0;
30 if(FAILED(hr=native->CreateSamplerState(&desc,&sampler)))return hr;
31 parameters.Sampler("coreSampler",sampler.Get());device=native;return S_OK;
32 }
33
34 HRESULT RunwayLights11::Draw(DrawContext11& draws,ID3D11ShaderResourceView* texture,
35 const std::vector<RunwayLightInstance11>& lights)
36 {
37 if(lights.empty())return S_OK;
38 if(!draws.Owner() || !texture || lights.size()>UINT_MAX/sizeof(RunwayLightInstance11))return E_INVALIDARG;
39 auto* native=draws.Owner()->Device();auto* context=draws.Owner()->Context();
40 HRESULT hr;
41 if(device.Get()!=native && FAILED(hr=Initialize(native)))return hr;
42 const UINT count=UINT(lights.size());
43 if(count>capacity)
44 {
45 instances.Reset();capacity=0;
46 const UINT next=(std::max)(count,256u);
47 D3D11_BUFFER_DESC desc{};desc.ByteWidth=next*sizeof(RunwayLightInstance11);
48 desc.Usage=D3D11_USAGE_DYNAMIC;desc.BindFlags=D3D11_BIND_VERTEX_BUFFER;
49 desc.CPUAccessFlags=D3D11_CPU_ACCESS_WRITE;
50 if(FAILED(hr=native->CreateBuffer(&desc,nullptr,&instances)))return hr;
51 capacity=next;
52 }
53 D3D11_MAPPED_SUBRESOURCE map{};
54 if(FAILED(hr=context->Map(instances.Get(),0,D3D11_MAP_WRITE_DISCARD,0,&map)))return hr;
55 memcpy(map.pData,lights.data(),count*sizeof(RunwayLightInstance11));context->Unmap(instances.Get(),0);
57 const auto& fixed=draws.Fixed();const auto& viewport=draws.Viewport();
58 parameters.Set("lightView",fixed.view);parameters.Set("lightProjection",fixed.projection);
59 parameters.Set("lightViewport",DirectX::XMFLOAT4(viewport.Width,viewport.Height,fixed.legacyPixelCenters ? 1.f : 0.f,0));
60 parameters.Set("lightClipPlane",fixed.clipEnabled ? fixed.clipPlane : DirectX::XMFLOAT4(0,0,0,1));
61 parameters.Texture("groundLightTexture",texture);
62 if(FAILED(hr=program.Bind(context,parameters)))return hr;
63 context->GSSetShader(nullptr,nullptr,0);
64 const auto saved=draws.Pipeline();auto& pipeline=draws.Pipeline();
65 pipeline.raster.CullMode=D3D11_CULL_NONE;
66 pipeline.depth.DepthEnable=TRUE;pipeline.depth.DepthWriteMask=D3D11_DEPTH_WRITE_MASK_ZERO;
67 pipeline.depth.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;
68 pipeline.blend.AlphaToCoverageEnable=FALSE;
69 auto& blend=pipeline.blend.RenderTarget[0];
70 blend.BlendEnable=TRUE;blend.SrcBlend=blend.DestBlend=D3D11_BLEND_ONE;blend.BlendOp=D3D11_BLEND_OP_ADD;
71 blend.SrcBlendAlpha=D3D11_BLEND_ZERO;blend.DestBlendAlpha=D3D11_BLEND_ONE;blend.BlendOpAlpha=D3D11_BLEND_OP_ADD;
72 blend.RenderTargetWriteMask=D3D11_COLOR_WRITE_ENABLE_RED|D3D11_COLOR_WRITE_ENABLE_GREEN|D3D11_COLOR_WRITE_ENABLE_BLUE;
73 hr=draws.ApplyPipeline();
74 if(SUCCEEDED(hr))
75 {
76 auto* buffer=instances.Get();const UINT stride=sizeof(RunwayLightInstance11),offset=0;
77 context->IASetVertexBuffers(0,1,&buffer,&stride,&offset);
78 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
79 context->DrawInstanced(4,count,0,0);
81 }
82 pipeline=saved;const HRESULT restored=draws.ApplyPipeline();
83 return FAILED(hr) ? hr : restored;
84 }
85}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
ID3D11DeviceContext * Context() const
Definition Device11.h:66
ID3D11Device * Device() const
Definition Device11.h:65
const D3D11_VIEWPORT & Viewport() const
HRESULT ApplyPipeline(bool fixedPoints=false)
FixedState11 & Fixed()
Device11 * Owner() const
void GeometryDraw(UINT primitives)
static FrameProfiler11 & Get()
void BufferUpload(UINT64 bytes)
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)
HRESULT Initialize(ID3D11Device *native)
Microsoft::WRL::ComPtr< ID3D11Device > device
Microsoft::WRL::ComPtr< ID3D11SamplerState > sampler
HRESULT Draw(DrawContext11 &draws, ID3D11ShaderResourceView *groundLightTexture, const std::vector< RunwayLightInstance11 > &lights)
Microsoft::WRL::ComPtr< ID3D11Buffer > instances
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
D3D11_RASTERIZER_DESC raster