-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshaders_clspv.cpp
More file actions
280 lines (241 loc) · 10.3 KB
/
shaders_clspv.cpp
File metadata and controls
280 lines (241 loc) · 10.3 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "kslicer.h"
#include "template_rendering.h"
#include <iostream>
#ifndef _WIN32
#include <sys/types.h>
#endif
std::string kslicer::FunctionRewriter::RewriteStdVectorTypeStr(const std::string& a_str) const
{
const bool isConst = (a_str.find("const ") != std::string::npos);
std::string typeStr = a_str;
ReplaceFirst(typeStr, "struct LiteMath::", "");
ReplaceFirst(typeStr, "LiteMath::", "");
ReplaceFirst(typeStr, "struct glm::", "");
ReplaceFirst(typeStr, "glm::", "");
ReplaceFirst(typeStr, "const ", "");
ReplaceFirst(typeStr, m_codeInfo->mainClassName + "::", "");
ReplaceFirst(typeStr, "struct float4x4", "float4x4"); // small inconvinience in math library
if(isConst)
typeStr = std::string("const ") + typeStr;
return typeStr;
}
bool kslicer::IsVectorContructorNeedsReplacement(const std::string& a_typeName)
{
static std::unordered_set<std::string> m_ctorReplacement;
static bool first = true;
if(first)
{
m_ctorReplacement.insert("float2");
m_ctorReplacement.insert("float3");
m_ctorReplacement.insert("float4");
m_ctorReplacement.insert("float2x2");
m_ctorReplacement.insert("float3x3");
m_ctorReplacement.insert("float4x4");
m_ctorReplacement.insert("int2");
m_ctorReplacement.insert("int3");
m_ctorReplacement.insert("int4");
m_ctorReplacement.insert("uint2");
m_ctorReplacement.insert("uint3");
m_ctorReplacement.insert("uint4");
m_ctorReplacement.insert("complex");
first = false;
}
return m_ctorReplacement.find(a_typeName) != m_ctorReplacement.end();
}
kslicer::ClspvCompiler::ClspvCompiler(bool a_useCPP, const std::string& a_prefix) : m_useCpp(a_useCPP), m_suffix(a_prefix)
{
}
std::string kslicer::ClspvCompiler::BuildCommand(const std::string& a_inputFile) const
{
if(m_useCpp)
return std::string("../clspv ") + ShaderSingleFile() + " -o " + ShaderSingleFile() + ".spv -pod-ubo -cl-std=CLC++ -inline-entry-points";
else
return std::string("../clspv ") + ShaderSingleFile() + " -o " + ShaderSingleFile() + ".spv -pod-pushconstant";
}
void kslicer::ClspvCompiler::GenerateShaders(nlohmann::json& a_kernelsJson, const MainClassInfo* a_codeInfo, const kslicer::TextGenSettings& a_settings)
{
const auto& mainClassFileName = a_codeInfo->mainClassFileName;
const auto& ignoreFolders = a_codeInfo->ignoreFolders;
std::filesystem::path folderPath = mainClassFileName.parent_path();
std::filesystem::path incUBOPath = folderPath / "include";
std::filesystem::create_directory(incUBOPath);
const std::string templatePath = "templates/generated.cl";
const std::filesystem::path outFileName = mainClassFileName.parent_path() / "z_generated.cl";
kslicer::ApplyJsonToTemplate(templatePath, outFileName, a_kernelsJson);
std::ofstream buildSH(mainClassFileName.parent_path() / "z_build.sh");
#if not __WIN32__
buildSH << "#!/bin/sh" << std::endl;
#endif
std::string build = this->BuildCommand();
buildSH << build.c_str() << " ";
for(auto folder : ignoreFolders) {
if(folder.string().find("TINYSTL") != std::string::npos)
continue;
buildSH << "-I" << folder.c_str() << " ";
}
buildSH << std::endl;
buildSH.close();
}
std::string kslicer::ClspvCompiler::LocalIdExpr(uint32_t a_kernelDim, uint32_t a_wgSize[3]) const
{
if(a_kernelDim == 1)
return "get_local_id(0)";
else if(a_kernelDim == 2)
{
std::stringstream strOut;
strOut << "get_local_id(0) + " << a_wgSize[0] << "*get_local_id(1)";
return strOut.str();
}
else if(a_kernelDim == 3)
{
std::stringstream strOut;
strOut << "get_local_id(0) + " << a_wgSize[0] << "*get_local_id(1) + " << a_wgSize[0]*a_wgSize[1] << "*get_local_id(2)";
return strOut.str();
}
else
{
std::cout << " [ClspvCompiler::LocalIdExpr]: Error, bad kernelDim = " << a_kernelDim << std::endl;
return "get_local_id(0)";
}
}
void kslicer::ClspvCompiler::GetThreadSizeNames(std::string a_strs[3]) const
{
a_strs[0] = "kgen_iNumElementsX";
a_strs[1] = "kgen_iNumElementsY";
a_strs[2] = "kgen_iNumElementsZ";
}
std::string kslicer::ClspvCompiler::GetSubgroupOpCode(const kslicer::KernelInfo::ReductionAccess& a_access) const
{
return "unknownCLSubgroupOperation";
}
std::string kslicer::ClspvCompiler::GetAtomicImplCode(const kslicer::KernelInfo::ReductionAccess& a_access) const
{
std::string res = "";
switch(a_access.type)
{
case KernelInfo::REDUCTION_TYPE::ADD_ONE:
case KernelInfo::REDUCTION_TYPE::ADD:
res = "atomic_add";
break;
case KernelInfo::REDUCTION_TYPE::SUB:
case KernelInfo::REDUCTION_TYPE::SUB_ONE:
res = "atomic_sub";
break;
case KernelInfo::REDUCTION_TYPE::FUNC:
{
if(a_access.funcName == "min" || a_access.funcName == "std::min") res = "atomic_min";
if(a_access.funcName == "max" || a_access.funcName == "std::max") res = "atomic_max";
}
break;
default:
break;
};
auto lastSymb = a_access.dataType[a_access.dataType.size()-1];
auto firstSimb = a_access.dataType[0]; // 'u' or 'i'
if(isdigit(lastSymb))
{
res.push_back(lastSymb);
if(firstSimb == 'u')
res.push_back(firstSimb);
}
return res;
}
std::string kslicer::ClspvCompiler::ReplaceCallFromStdNamespace(const std::string& a_call, const std::string& a_typeName) const
{
std::string call = a_call;
if(a_typeName == "float" || a_typeName == "const float" || a_typeName == "float2" || a_typeName == "const float2" ||
a_typeName == "float3" || a_typeName == "const float3" || a_typeName == "float4" || a_typeName == "const float4")
{
ReplaceFirst(call, "std::min", "fmin");
ReplaceFirst(call, "std::max", "fmax");
ReplaceFirst(call, "std::abs", "fabs");
if(call == "min")
ReplaceFirst(call, "min", "fmin");
if(call == "max")
ReplaceFirst(call, "max", "fmax");
if(call == "abs")
ReplaceFirst(call, "abs", "fabs");
}
ReplaceFirst(call, "std::", "");
return call;
}
std::string kslicer::ClspvCompiler::PrintHeaderDecl(const DeclInClass& a_decl, const clang::CompilerInstance& a_compiler, std::shared_ptr<kslicer::FunctionRewriter> a_pRewriter)
{
std::string typeInCL = a_decl.type;
ReplaceFirst(typeInCL, "const", "__constant static");
std::string result = "";
switch(a_decl.kind)
{
case kslicer::DECL_IN_CLASS::DECL_STRUCT:
result = kslicer::GetRangeSourceCode(a_decl.srcRange, a_compiler) + ";";
break;
case kslicer::DECL_IN_CLASS::DECL_CONSTANT:
result = typeInCL + " " + a_decl.name + " = " + kslicer::GetRangeSourceCode(a_decl.srcRange, a_compiler) + ";";
break;
case kslicer::DECL_IN_CLASS::DECL_TYPEDEF:
result = "typedef " + typeInCL + " " + a_decl.name + ";";
break;
default:
break;
};
ReplaceFirst(result, "LiteMath::", "");
ReplaceFirst(result, "std::", "");
return result;
}
std::string kslicer::ClspvCompiler::RewritePushBack(const std::string& memberNameA, const std::string& memberNameB, const std::string& newElemValue) const
{
return std::string("{ uint offset = atomic_inc(&") + UBOAccess(memberNameB) + "); " + memberNameA + "[offset] = " + newElemValue + ";}";
}
std::shared_ptr<kslicer::FunctionRewriter> kslicer::ClspvCompiler::MakeFuncRewriter(clang::Rewriter &R, const clang::CompilerInstance& a_compiler, kslicer::MainClassInfo* a_codeInfo, kslicer::ShittyFunction a_shit)
{
return std::make_shared<kslicer::FunctionRewriter>(R, a_compiler, a_codeInfo);
}
std::shared_ptr<kslicer::KernelRewriter> kslicer::ClspvCompiler::MakeKernRewriter(clang::Rewriter &R, const clang::CompilerInstance& a_compiler, MainClassInfo* a_codeInfo,
kslicer::KernelInfo& a_kernel, const std::string& fakeOffs)
{
return std::make_shared<kslicer::KernelRewriter>(R, a_compiler, a_codeInfo, a_kernel, fakeOffs);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kslicer::KernelRewriter::KernelRewriter(clang::Rewriter &R, const clang::CompilerInstance& a_compiler, kslicer::MainClassInfo* a_codeInfo, kslicer::KernelInfo& a_kernel, const std::string& a_fakeOffsetExpr) :
m_rewriter(R), m_compiler(a_compiler), m_codeInfo(a_codeInfo), m_mainClassName(a_codeInfo->mainClassName),
m_args(a_kernel.args), m_fakeOffsetExp(a_fakeOffsetExpr), m_kernelIsBoolTyped(a_kernel.isBoolTyped),
m_currKernel(a_kernel)
{
m_pRewrittenNodes = std::make_shared< std::unordered_set<uint64_t> >();
const auto& a_variables = a_codeInfo->dataMembers;
m_variables.reserve(a_variables.size());
for(const auto& var : a_variables)
m_variables[var.name] = var;
auto tidArgs = a_codeInfo->GetKernelTIDArgs(a_kernel);
for(const auto& arg : tidArgs)
m_threadIdArgs.push_back(arg.name);
m_explicitIdISPC = a_kernel.explicitIdISPC;
if(tidArgs.size() > 0)
m_threadIdExplicitIndexISPC = tidArgs[tidArgs.size()-1].name;
}
bool kslicer::KernelRewriter::WasNotRewrittenYet(const clang::Stmt* expr)
{
if(expr == nullptr)
return true;
if(clang::isa<clang::NullStmt>(expr))
return true;
auto exprHash = kslicer::GetHashOfSourceRange(expr->getSourceRange());
return (m_pRewrittenNodes->find(exprHash) == m_pRewrittenNodes->end());
}
void kslicer::KernelRewriter::MarkRewritten(const clang::Stmt* expr)
{
kslicer::MarkRewrittenRecursive(expr, *m_pRewrittenNodes);
}
void kslicer::DisplayVisitedNodes(const std::unordered_set<uint64_t>& a_nodes)
{
std::vector<uint64_t> allNodes;
allNodes.reserve(a_nodes.size());
for(auto p : a_nodes)
allNodes.push_back(p);
std::sort(allNodes.begin(), allNodes.end());
for(size_t i=0; i<allNodes.size(); i++)
std::cout << i << "\t" << allNodes[i] << std::endl;
}