Rise
The Vieneo Province
Loading...
Searching...
No Matches
DrawContext11.cpp
Go to the documentation of this file.
1#include "DrawContext11.h"
2#include "FrameProfiler11.h"
3#include <cstring>
4#include <cmath>
5using Microsoft::WRL::ComPtr;
6namespace Rendering
7{
9 {
10 raster.FillMode=D3D11_FILL_SOLID;raster.CullMode=D3D11_CULL_BACK;raster.FrontCounterClockwise=TRUE;
11 raster.DepthClipEnable=TRUE;raster.MultisampleEnable=TRUE;
12 depth.DepthEnable=TRUE;depth.DepthWriteMask=D3D11_DEPTH_WRITE_MASK_ALL;depth.DepthFunc=D3D11_COMPARISON_LESS_EQUAL;
13 depth.StencilReadMask=depth.StencilWriteMask=255;
14 depth.FrontFace=depth.BackFace={D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_STENCIL_OP_KEEP,D3D11_COMPARISON_ALWAYS};
15 for(auto& target:blend.RenderTarget)
16 target={FALSE,D3D11_BLEND_ONE,D3D11_BLEND_ZERO,D3D11_BLEND_OP_ADD,D3D11_BLEND_ONE,D3D11_BLEND_ZERO,D3D11_BLEND_OP_ADD,D3D11_COLOR_WRITE_ENABLE_ALL};
17 }
19 {
20 cachedRaster=nullptr;cachedDepth=nullptr;cachedBlend=nullptr;
21 rasterStates.clear();depthStates.clear();blendStates.clear();color.Reset();depth.Reset();owner=nullptr;
24 status="native draw context not initialized";
25 }
26 HRESULT DrawContext11::Initialize(Device11& device,const std::filesystem::path& directory)
27 {
28 Reset();if(!device.Device() || !device.Context())return E_INVALIDARG;owner=&device;
29 HRESULT hr=fixed.Initialize(device.Device(),directory);if(FAILED(hr)){status=fixed.Status();return hr;}
30 if(FAILED(hr=materials.Initialize(device.Device(),directory))){status=materials.Status();return hr;}
31 status="native production draw context ready";if(device.SceneTarget())BindScene();return S_OK;
32 }
34 {
35 if(!owner)return;
36 const auto& desc=owner->Depth().Description();
37 const D3D11_VIEWPORT next{0,0,float(desc.Width),float(desc.Height),0,1};
39 }
41 {
42 if(owner){owner->UnbindShaderResources();owner->Context()->OMSetRenderTargets(0,nullptr,nullptr);}
43 color.Reset();depth.Reset();viewport={};
44 }
45 HRESULT DrawContext11::Target(ID3D11RenderTargetView* newColor,ID3D11DepthStencilView* newDepth,const D3D11_VIEWPORT& next)
46 {
47 if(!owner || !newColor)return E_INVALIDARG;
48 if(!std::isfinite(next.Width) || !std::isfinite(next.Height) || next.Width<1 || next.Height<1
49 || !std::isfinite(next.TopLeftX) || !std::isfinite(next.TopLeftY) || next.TopLeftX<0 || next.TopLeftY<0
50 || !std::isfinite(next.MinDepth) || !std::isfinite(next.MaxDepth)
51 || next.MinDepth<0 || next.MaxDepth>1 || next.MinDepth>next.MaxDepth)return E_INVALIDARG;
52 ComPtr<ID3D11Device> device;newColor->GetDevice(&device);if(device.Get()!=owner->Device())return E_INVALIDARG;
53 if(newDepth){device.Reset();newDepth->GetDevice(&device);if(device.Get()!=owner->Device())return E_INVALIDARG;}
54 owner->UnbindShaderResources();color=newColor;depth=newDepth;viewport=next;
55 owner->Context()->OMSetRenderTargets(1,&newColor,newDepth);owner->Context()->RSSetViewports(1,&viewport);return S_OK;
56 }
57 void DrawContext11::Scissor(const RECT& value)
58 {
59 scissor=value;if(owner)owner->Context()->RSSetScissorRects(1,&scissor);
60 }
63 {
65 const HRESULT hr=Target(saved.color.Get(),saved.depth.Get(),saved.viewport);return FAILED(hr) ? hr : ApplyPipeline();
66 }
68 {
69 if(!owner)return E_UNEXPECTED;HRESULT hr;
70 auto rasterDescription=pipeline.raster;
71 // Legacy points have no facing. The geometry shader expands them into
72 // triangles, so world culling would otherwise hide runway/approach lights.
73 // Keep the caller's descriptor intact for the following mesh draw.
74 if(fixedPoints)rasterDescription.CullMode=D3D11_CULL_NONE;
75 if(!cachedRaster || memcmp(&cachedPipeline.raster,&rasterDescription,sizeof(rasterDescription)))
76 {
77 auto raster=rasterStates.find(Bytes(rasterDescription));
78 if(raster==rasterStates.end())
79 {
80 ComPtr<ID3D11RasterizerState> created;if(FAILED(hr=owner->Device()->CreateRasterizerState(&rasterDescription,&created)))return hr;
81 raster=rasterStates.emplace(Bytes(rasterDescription),std::move(created)).first;
82 }
83 cachedRaster=raster->second.Get();cachedPipeline.raster=rasterDescription;
84 }
86 {
87 auto depthState=depthStates.find(Bytes(pipeline.depth));
88 if(depthState==depthStates.end())
89 {
90 ComPtr<ID3D11DepthStencilState> created;if(FAILED(hr=owner->Device()->CreateDepthStencilState(&pipeline.depth,&created)))return hr;
91 depthState=depthStates.emplace(Bytes(pipeline.depth),std::move(created)).first;
92 }
93 cachedDepth=depthState->second.Get();cachedPipeline.depth=pipeline.depth;
94 }
96 {
97 auto blend=blendStates.find(Bytes(pipeline.blend));
98 if(blend==blendStates.end())
99 {
100 ComPtr<ID3D11BlendState> created;if(FAILED(hr=owner->Device()->CreateBlendState(&pipeline.blend,&created)))return hr;
101 blend=blendStates.emplace(Bytes(pipeline.blend),std::move(created)).first;
102 }
103 cachedBlend=blend->second.Get();cachedPipeline.blend=pipeline.blend;
104 }
105 // Still bind every time: native passes can change the context directly.
106 auto* context=owner->Context();context->RSSetState(cachedRaster);context->RSSetScissorRects(1,&scissor);
107 context->OMSetDepthStencilState(cachedDepth,pipeline.stencilReference);
108 context->OMSetBlendState(cachedBlend,pipeline.blendFactor,pipeline.sampleMask);
109 return S_OK;
110 }
111 HRESULT DrawContext11::Draw(Geometry11& vertices,const VertexLayout11& layout,Primitive11 primitive,UINT first,UINT count,
112 Geometry11* indices,INT baseVertex,UINT streamOffset)
113 {
114 if(!owner || !color)return E_UNEXPECTED;
115 if(!count)return S_FALSE;
116 uint64_t elements=0;D3D11_PRIMITIVE_TOPOLOGY topology{};
117 switch(primitive)
118 {
119 case Primitive11::Points:elements=count;topology=D3D11_PRIMITIVE_TOPOLOGY_POINTLIST;break;
120 case Primitive11::Lines:elements=uint64_t(count)*2;topology=D3D11_PRIMITIVE_TOPOLOGY_LINELIST;break;
121 case Primitive11::LineStrip:elements=uint64_t(count)+1;topology=D3D11_PRIMITIVE_TOPOLOGY_LINESTRIP;break;
122 case Primitive11::Triangles:elements=uint64_t(count)*3;topology=D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;break;
123 case Primitive11::TriangleStrip:elements=uint64_t(count)+2;topology=D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP;break;
124 case Primitive11::TriangleFan:elements=uint64_t(count)+2;topology=D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;break;
125 default:return E_INVALIDARG;
126 }
127 if(!layout.Valid() || streamOffset>vertices.Size() || elements>UINT_MAX)return E_INVALIDARG;
128 const size_t vertexCount=(vertices.Size()-streamOffset)/layout.stride;
129 if(indices)
130 {
131 const UINT indexSize=indices->Indices32() ? 4 : 2;
132 if(uint64_t(first)+elements>indices->Size()/indexSize)return E_INVALIDARG;
133 // Validate the authored range before asking the GPU to fetch it.
134 for(size_t i=first;i<size_t(first)+elements;++i)
135 {
136 uint32_t indexValue=0;memcpy(&indexValue,indices->Bytes().data()+i*indexSize,indexSize);
137 const int64_t adjusted=int64_t(indexValue)+baseVertex;
138 if(adjusted<0 || uint64_t(adjusted)>=vertexCount)return E_INVALIDARG;
139 }
140 }
141 else if(uint64_t(first)+elements>vertexCount)return E_INVALIDARG;
142 HRESULT hr=ApplyPipeline(material<0 && primitive==Primitive11::Points);if(FAILED(hr))return hr;
143 auto* context=owner->Context();
144 if(FAILED(hr=vertices.BindVertices(context,layout,streamOffset,material<0)))return hr;
145 if(material<0)
146 {
147 fixedState.viewport={viewport.TopLeftX,viewport.TopLeftY,viewport.Width,viewport.Height};
151 hr=fixed.Bind(context,fixedState,primitive==Primitive11::Points);
152 }
153 else hr=materials.Bind(context,UINT(material),layout,viewport);
154 if(FAILED(hr))return hr;context->IASetPrimitiveTopology(topology);
155 if(primitive==Primitive11::TriangleFan)
156 {
157 if(uint64_t(count)*3*sizeof(uint32_t)>UINT_MAX)return E_INVALIDARG;
158 const UINT size=count*3*sizeof(uint32_t);
159 if(size>fanCapacity){if(FAILED(hr=fanIndices.Create(owner->Device(),size,true,true,true)))return hr;fanCapacity=size;}
160 uint32_t* output=nullptr;if(FAILED(hr=fanIndices.Lock(0,size,reinterpret_cast<void**>(&output),false,true)))return hr;
161 const auto at=[&](UINT n){uint32_t value=first+n;if(indices){value=0;const UINT stride=indices->Indices32() ? 4 : 2;
162 memcpy(&value,indices->Bytes().data()+size_t(first+n)*stride,stride);}return value;};
163 for(UINT i=0;i<count;++i){output[i*3]=at(0);output[i*3+1]=at(i+1);output[i*3+2]=at(i+2);}
164 fanIndices.Unlock();if(FAILED(hr=fanIndices.BindIndices(context)))return hr;context->DrawIndexed(count*3,0,indices ? baseVertex : 0);
165 }
166 else if(indices){if(FAILED(hr=indices->BindIndices(context)))return hr;context->DrawIndexed(UINT(elements),first,baseVertex);}
167 else context->Draw(UINT(elements),first);
169 return S_OK;
170 }
171 HRESULT DrawContext11::DrawImmediate(const void* source,UINT size,const VertexLayout11& layout,Primitive11 primitive,UINT count)
172 {
173 if(!owner || !source || !size)return E_INVALIDARG;HRESULT hr;
174 uint64_t required=count;
175 if(primitive==Primitive11::Lines)required*=2;
176 else if(primitive==Primitive11::Triangles)required*=3;
177 else if(primitive==Primitive11::LineStrip)++required;
178 else if(primitive==Primitive11::TriangleStrip || primitive==Primitive11::TriangleFan)required+=2;
179 if(!layout.Valid() || required>size/layout.stride)return E_INVALIDARG;
180 if(size>immediateCapacity){if(FAILED(hr=immediate.Create(owner->Device(),size,true)))return hr;immediateCapacity=size;}
181 void* output=nullptr;if(FAILED(hr=immediate.Lock(0,size,&output,false,true)))return hr;
182 memcpy(output,source,size);immediate.Unlock();return Draw(immediate,layout,primitive,0,count);
183 }
184}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
void UnbindShaderResources()
Definition Device11.cpp:163
ID3D11DeviceContext * Context() const
Definition Device11.h:66
ID3D11RenderTargetView * SceneTarget() const
Definition Device11.h:68
ID3D11Device * Device() const
Definition Device11.h:65
const SceneDepth11 & Depth() const
Definition Device11.h:70
Microsoft::WRL::ComPtr< ID3D11RenderTargetView > color
const RECT & Scissor() const
ID3D11DepthStencilState * cachedDepth
std::map< Key< D3D11_RASTERIZER_DESC >, Microsoft::WRL::ComPtr< ID3D11RasterizerState > > rasterStates
HRESULT ApplyPipeline(bool fixedPoints=false)
void Scissor(const RECT &value)
std::map< Key< D3D11_DEPTH_STENCIL_DESC >, Microsoft::WRL::ComPtr< ID3D11DepthStencilState > > depthStates
std::map< Key< D3D11_BLEND_DESC >, Microsoft::WRL::ComPtr< ID3D11BlendState > > blendStates
HRESULT Restore(const Saved &saved)
HRESULT DrawImmediate(const void *vertices, UINT bytes, const VertexLayout11 &layout, Primitive11 primitive, UINT primitiveCount)
ID3D11RasterizerState * cachedRaster
HRESULT Initialize(Device11 &device, const std::filesystem::path &shaderDirectory={})
ID3D11BlendState * cachedBlend
Microsoft::WRL::ComPtr< ID3D11DepthStencilView > depth
HRESULT Target(ID3D11RenderTargetView *color, ID3D11DepthStencilView *depth, const D3D11_VIEWPORT &viewport)
HRESULT Draw(Geometry11 &vertices, const VertexLayout11 &layout, Primitive11 primitive, UINT first, UINT primitiveCount, Geometry11 *indices=nullptr, INT baseVertex=0, UINT streamOffset=0)
static Key< Description > Bytes(const Description &value)
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
const std::string & Status() const
HRESULT Bind(ID3D11DeviceContext *context, const FixedState11 &state, bool pointList=false)
void GeometryDraw(UINT primitives)
static FrameProfiler11 & Get()
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
bool Indices32() const
Definition Geometry11.h:39
HRESULT BindIndices(ID3D11DeviceContext *context)
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
UINT Size() const
Definition Geometry11.h:38
HRESULT BindVertices(ID3D11DeviceContext *context, const VertexLayout11 &layout, UINT byteOffset, bool canonical)
const std::vector< uint8_t > & Bytes() const
Definition Geometry11.h:37
HRESULT Bind(ID3D11DeviceContext *context, UINT pass, const VertexLayout11 &layout, const D3D11_VIEWPORT &viewport)
const std::string & Status() const
Definition Materials11.h:16
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
const D3D11_TEXTURE2D_DESC & Description() const
Definition Device11.h:22
ID3D11DepthStencilView * WriteView() const
Definition Device11.h:18
Microsoft::WRL::ComPtr< ID3D11DepthStencilView > depth
Microsoft::WRL::ComPtr< ID3D11RenderTargetView > color
DirectX::XMFLOAT4 viewport
D3D11_DEPTH_STENCIL_DESC depth
D3D11_RASTERIZER_DESC raster
D3D11_BLEND_DESC blend
static constexpr UINT Absent
Definition Geometry11.h:13
UINT PackedColors() const
Definition Geometry11.h:19