Rise
The Vieneo Province
Loading...
Searching...
No Matches
Materials11.cpp
Go to the documentation of this file.
1#include "Materials11.h"
2#include "ShaderBytecode11.h"
4#include <cstring>
5#include <cmath>
6
7namespace Rendering
8{
10 {
11 programs.clear();values=Parameters11{};samplers.clear();device.Reset();
12 status="native material programs not initialized";
13 }
14 HRESULT Materials11::Initialize(ID3D11Device* native,const std::filesystem::path& shaderDirectory)
15 {
16 Reset();if(!native)return E_INVALIDARG;device=native;directory=shaderDirectory;
17 for(const auto& entry:CustomUISamplers)
18 {
19 Microsoft::WRL::ComPtr<ID3D11SamplerState> sampler;
20 const HRESULT hr=device->CreateSamplerState(&entry.description,&sampler);
21 if(FAILED(hr))return hr;values.Sampler(entry.name,sampler.Get());samplers.push_back(std::move(sampler));
22 }
23 status="native material programs ready";return S_OK;
24 }
25 HRESULT Materials11::Bind(ID3D11DeviceContext* context,UINT pass,const VertexLayout11& layout,const D3D11_VIEWPORT& viewport)
26 {
27 if(!device || !context)return E_UNEXPECTED;
28 Microsoft::WRL::ComPtr<ID3D11Device> contextDevice;context->GetDevice(&contextDevice);
29 if(contextDevice.Get()!=device.Get())return E_INVALIDARG;
30 if(!layout.Valid() || !std::isfinite(viewport.Width) || !std::isfinite(viewport.Height) || viewport.Width<1 || viewport.Height<1)return E_INVALIDARG;
31 const Key key{pass,layout};auto found=programs.find(key);
32 if(found==programs.end())
33 {
34 const ShaderPass11* selected=nullptr;
35 for(const auto& candidate:CustomUIPasses)if(!strcmp(candidate.technique,"TShader") && candidate.index==pass){selected=&candidate;break;}
36 if(!selected)return E_INVALIDARG;
37 auto program=std::make_unique<Program11>();
38 const auto vertex=ReadShaderBytecode11(directory/(std::string("CustomUI-")+selected->vertex+"-1.cso"));
39 const auto pixel=ReadShaderBytecode11(directory/(std::string("CustomUI-")+selected->pixel+"-1.cso"));
40 const auto elements=layout.Elements();
41 const HRESULT hr=program->Create(device.Get(),vertex.data(),vertex.size(),pixel.data(),pixel.size(),
42 elements.data(),UINT(elements.size()),layout.PackedColors());
43 if(FAILED(hr)){status=program->Status();return hr;}
44 found=programs.emplace(key,std::move(program)).first;
45 }
46 values.Set("_risePixelCenterOffset",DirectX::XMFLOAT2(1/viewport.Width,-1/viewport.Height));
47 context->GSSetShader(nullptr,nullptr,0);context->HSSetShader(nullptr,nullptr,0);context->DSSetShader(nullptr,nullptr,0);
48 const HRESULT hr=found->second->Bind(context,values);if(FAILED(hr))status=found->second->Status();return hr;
49 }
50}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
Microsoft::WRL::ComPtr< ID3D11Device > device
Definition Materials11.h:24
HRESULT Bind(ID3D11DeviceContext *context, UINT pass, const VertexLayout11 &layout, const D3D11_VIEWPORT &viewport)
std::map< Key, std::unique_ptr< Program11 > > programs
Definition Materials11.h:27
std::vector< Microsoft::WRL::ComPtr< ID3D11SamplerState > > samplers
Definition Materials11.h:28
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
std::filesystem::path directory
Definition Materials11.h:25
void Set(const std::string &name, const void *data, size_t bytes)
Definition Program11.cpp:45
void Sampler(const std::string &name, ID3D11SamplerState *value)
Definition Program11.cpp:66
const ShaderSampler11 CustomUISamplers[]
const ShaderPass11 CustomUIPasses[]
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
UINT PackedColors() const
Definition Geometry11.h:19
std::vector< D3D11_INPUT_ELEMENT_DESC > Elements() const