Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
DirectX.cpp
1
2#include "stdafx.h"
3#include "DirectX.hpp"
4#include <alicorn/std/vector.hpp>
5#include <d3dcompiler.h>
6#pragma comment(lib, "d3dcompiler.lib" )
7
8using Shader_t = ::covellite::api::renderer::DirectX::Shader;
9
10/*static*/ auto Shader_t::Compile(const BinaryData_t & _Header,
11 const BinaryData_t & _Body, LPCSTR _pEntryPoint, LPCSTR _pTarget) -> ComPtr_t<ID3DBlob>
12{
13 using namespace ::alicorn::extension::std;
14 using ::alicorn::extension::cpp::IS_RELEASE_CONFIGURATION;
15
16 const auto FullShaderBody = _Header + _Body;
17
18 constexpr DWORD ShaderFlags = (IS_RELEASE_CONFIGURATION) ? 0 :
19 D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
20
21 ::Microsoft::WRL::ComPtr<ID3DBlob> pCompiledEffect;
22 ::Microsoft::WRL::ComPtr<ID3DBlob> pError;
23 auto Result = D3DCompile(FullShaderBody.data(), FullShaderBody.size(),
24 (::std::string("[Covellite::Api]: ") + _pEntryPoint).c_str(),
25 NULL, NULL, _pEntryPoint, _pTarget, ShaderFlags, 0,
26 &pCompiledEffect, &pError);
27 if (FAILED(Result))
28 {
29 throw STD_EXCEPTION << "Failed: " << Result <<
30 " [header line: " << GetHeaderLines(_Header) << ", "
31 << (pError ? (char *)pError->GetBufferPointer() : "Unknown error")
32 << "].";
33 }
34
35 return pCompiledEffect;
36}
37
38/*static*/ ::std::string Shader_t::GetVersion(const String_t & _Kind)
39{
40 return (_Kind == uT("Pixel")) ? "ps_4_0" : "vs_4_0";
41}
42
43/*static*/ auto Shader_t::Convert(const ::std::string & _Source) -> BinaryData_t
44{
45 return BinaryData_t{ ::std::begin(_Source), ::std::end(_Source) };
46}
47
48/*static*/ ::std::size_t Shader_t::GetHeaderLines(const BinaryData_t & _Header)
49{
50 ::std::size_t Result = 0;
51
52 auto itPosition = _Header.cbegin();
53 while (true)
54 {
55 itPosition = ::std::find(itPosition, _Header.cend(), '\n');
56 if (itPosition == _Header.cend()) break;
57
58 ++itPosition;
59 Result++;
60 }
61
62 return Result;
63}