-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShaderBlob.cpp
172 lines (152 loc) · 4.22 KB
/
ShaderBlob.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "D3D12FrameWork/ShaderBlob.h"
#include "D3D12FrameWork/Common.h"
#include "D3D12FrameWork/ComPtr.h"
#include "D3D12FrameWork/DXCompiler.h"
#include <filesystem>
#include <tchar.h>
#include <fstream>
#include <ctype.h>
namespace D3D12FrameWork {
ShaderBlob::ShaderBlob()
{
}
ShaderBlob::~ShaderBlob() {
}
bool ShaderBlob::Init(
std::wstring_view _shaderName,
ShaderType _type,
ID3D12ShaderReflection** _ppRflc,
DX12Settings::ShaderVersion _version
) {
m_pShaderFileChecker.reset(new ShaderFileChecker(std::wstring(
_shaderName.begin(), _shaderName.end()
)));
//とりあえずdebug用
auto compArg = std::vector<ShaderCompileOption>();
compArg.emplace_back(ShaderCompileOption::SHADERCOMPILE_DEBUG);
compArg.emplace_back(ShaderCompileOption::SHADERCOMPILE_SKIP_OPTIMIZATION);
std::string up_initial;
std::string low_initial;
switch (_type)
{
case D3D12FrameWork::ShaderBlob::PIXEL:
up_initial = "P"; low_initial = "p";
break;
case D3D12FrameWork::ShaderBlob::VERTEX:
up_initial = "V"; low_initial = "v";
break;
case D3D12FrameWork::ShaderBlob::COMPUTE:
up_initial = "C"; low_initial = "c";
break;
case D3D12FrameWork::ShaderBlob::GEOMETRY:
up_initial = "G"; low_initial = "g";
break;
case D3D12FrameWork::ShaderBlob::AMPLIFY:
up_initial = "A"; low_initial = "a";
break;
case D3D12FrameWork::ShaderBlob::MESH:
up_initial = "M"; low_initial = "m";
break;
case D3D12FrameWork::ShaderBlob::NONE:
break;
default:
break;
}
std::string version;
switch (_version)
{
case D3D12FrameWork::DX12Settings::VERSION_5_0:
version = "5_0";
break;
case D3D12FrameWork::DX12Settings::VERSION_6_5:
version = "6_5";
break;
case D3D12FrameWork::DX12Settings::VERSION_6_0:
version = "6_0";
break;
default:
break;
}
IBlobObject* pTmpBlob = nullptr;
auto result = ShaderCompiler::Instance()->CompileShader(
m_pShaderFileChecker->GetShaderPath(),
m_pShaderFileChecker->GetCompiledPath(),
up_initial+"Smain",
low_initial+"s_"+version,
compArg,
&pTmpBlob,
_ppRflc,
m_pShaderFileChecker->IsPreCompiled()
);
m_pShaderBlob.reset(pTmpBlob);
return result;
}
ShaderFileChecker::ShaderFileChecker(std::wstring _shaderName)
{
m_shaderPath = std::filesystem::path(
DX12Settings::SHADER_PATH / (_shaderName + L".hlsl")
);
m_compiledPath = std::filesystem::path(
DX12Settings::SHADER_PATH / (_shaderName + L".cso")
);
}
ShaderFileChecker::~ShaderFileChecker()
{
}
bool ShaderFileChecker::IsPreCompiled()
{
if (!std::filesystem::exists(m_compiledPath)) return false;
#if defined(DEBUG) || defined(_DEBUG)
if (!std::filesystem::exists(m_shaderPath)) {
assert(false);
}
auto comDate = std::filesystem::last_write_time(m_compiledPath).time_since_epoch();
auto shdDate = std::filesystem::last_write_time(m_shaderPath).time_since_epoch();
if (comDate < shdDate) {//shaderの更新日時より前にコンパイルしている.
return false;
}
//hlsliも調べておく
auto includePaths = CollectIncludePath();
for (auto ipath : includePaths) {
auto incShader = m_shaderPath.parent_path().append(ipath);
if (!std::filesystem::exists(incShader)) {
assert(false);
}
auto incDate = std::filesystem::last_write_time(incShader).time_since_epoch();
if (comDate < incDate) {
return false;
}
}
//pdbも調べておく。
//auto pdbPath = DX12Settings::SHADER_PDB_PATH / m_shaderPath.filename();
//if (!std::filesystem::exists(pdbPath)) {
// return false;
//}
#endif // DEBUG
return true;
}
std::list<std::string>
ShaderFileChecker::CollectIncludePath() {
std::list<std::string> ret;
std::ifstream shFile(m_shaderPath);
if (!shFile.is_open()) {
assert(false);
return ret;
}
std::string line;
std::string includeMacro = "#include";
while (std::getline(shFile,line) &&
!std::isalpha(line[0]))
{
auto result = line.find(includeMacro);
if (result == std::string::npos) continue;
auto startPos = line.find_first_of('"', result);
if (startPos == std::string::npos) continue;
auto endPos = line.find_last_of('"');
if (endPos <= startPos) continue;
auto includePath = line.substr(startPos + 1, endPos - startPos - 1);
ret.emplace_back(includePath);
}
return ret;
}
}