-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPipelineContext.cpp
More file actions
229 lines (202 loc) · 7.26 KB
/
Copy pathPipelineContext.cpp
File metadata and controls
229 lines (202 loc) · 7.26 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
#include "PipelineContext.h"
#include "Mesh.h"
#include "Engine.h"
#include "CoreLib/LibIO.h"
#include "ShaderCompiler.h"
#include "EngineLimits.h"
#include "Renderer.h"
using namespace CoreLib;
using namespace CoreLib::IO;
namespace GameEngine
{
VertexFormat PipelineContext::LoadVertexFormat(MeshVertexFormat vertFormat)
{
VertexFormat rs;
auto vertTypeId = vertFormat.GetTypeId();
if (vertexFormats.TryGetValue(vertTypeId, rs))
return rs;
VertexFormat vertexFormat;
int location = 0;
const int UNNORMALIZED = 0;
const int NORMALIZED = 1;
// Always starts with vec3 pos
rs.Attributes.Add(VertexAttributeDesc(DataType::Float3, UNNORMALIZED, 0, location));
location++;
for (int i = 0; i < vertFormat.GetUVChannelCount(); i++)
{
rs.Attributes.Add(VertexAttributeDesc(DataType::Half2, UNNORMALIZED, vertFormat.GetUVOffset(i), location));
location++;
}
if (vertFormat.HasTangent())
{
rs.Attributes.Add(VertexAttributeDesc(DataType::UInt, UNNORMALIZED, vertFormat.GetTangentFrameOffset(), location));
location++;
}
for (int i = 0; i < vertFormat.GetColorChannelCount(); i++)
{
rs.Attributes.Add(VertexAttributeDesc(DataType::Byte4, NORMALIZED, vertFormat.GetColorOffset(i), location));
location++;
}
if (vertFormat.HasSkinning())
{
rs.Attributes.Add(VertexAttributeDesc(DataType::UInt, UNNORMALIZED, vertFormat.GetBoneIdsOffset(), location));
location++;
rs.Attributes.Add(VertexAttributeDesc(DataType::UInt, UNNORMALIZED, vertFormat.GetBoneWeightsOffset(), location));
location++;
}
vertexFormats[vertTypeId] = rs;
return rs;
}
PipelineClass * PipelineContext::GetPipelineInternal(MeshVertexFormat * vertFormat, int vtxId)
{
shaderKeyChanged = false;
lastVtxId = vtxId;
shaderKeyBuilder.Clear();
shaderKeyBuilder.Append(spShaderGetId(shader));
shaderKeyBuilder.FlipLeadingByte(vtxId);
for (int i = 0; i < modulePtr; i++)
shaderKeyBuilder.Append(modules[i]->ModuleId);
/*
if (shaderKeyBuilder.Key == lastKey)
{
return lastPipeline;
}*/
if (auto pipeline = pipelineObjects.TryGetValue(shaderKeyBuilder.Key))
{
//lastKey = shaderKeyBuilder.Key;
lastPipeline = pipeline->Ptr();
return pipeline->Ptr();
}
//lastKey = shaderKeyBuilder.Key;
lastPipeline = CreatePipeline(vertFormat);
return lastPipeline;
}
PipelineClass * PipelineContext::CreatePipeline(MeshVertexFormat * vertFormat)
{
RefPtr<PipelineBuilder> pipelineBuilder = hwRenderer->CreatePipelineBuilder();
pipelineBuilder->FixedFunctionStates = fixedFunctionStates;
// Set vertex layout
pipelineBuilder->SetVertexLayout(LoadVertexFormat(*vertFormat));
// Compile shaders
Array<SpireModule*, 32> spireModules;
for (int i = 0; i < modulePtr; i++)
spireModules.Add(modules[i]->specializedModule);
spireModules.Add(vertFormat->GetSpireModule(spireEnv));
SpireDiagnosticSink * sink = spCreateDiagnosticSink(spireContext);
auto compileRs = spEnvCompileShader(spireEnv, shader, spireModules.Buffer(), spireModules.Count(), "", sink);
int count = spGetDiagnosticCount(sink);
for (int i = 0; i < count; i++)
{
SpireDiagnostic diag;
spGetDiagnosticByIndex(sink, i, &diag);
Print("%S(%d): %S\n", String(diag.FileName).ToWString(), diag.Line, String(diag.Message).ToWString());
}
if (spDiagnosticSinkHasAnyErrors(sink))
{
spDestroyDiagnosticSink(sink);
spDestroyCompilationResult(compileRs);
return nullptr;
}
ShaderCompilationResult rs;
GetShaderCompilationResult(rs, compileRs, sink);
spDestroyDiagnosticSink(sink);
spDestroyCompilationResult(compileRs);
RefPtr<PipelineClass> pipelineClass = new PipelineClass();
static int pipelineClassId = 0;
pipelineClassId++;
pipelineClass->Id = pipelineClassId;
for (auto& compiledShader : rs.Shaders)
{
Shader* shaderObj = nullptr;
if (compiledShader.Key == "vs")
{
shaderObj = hwRenderer->CreateShader(ShaderType::VertexShader, compiledShader.Value.Buffer(), compiledShader.Value.Count());
}
else if (compiledShader.Key == "fs")
{
shaderObj = hwRenderer->CreateShader(ShaderType::FragmentShader, compiledShader.Value.Buffer(), compiledShader.Value.Count());
}
else if (compiledShader.Key == "tcs")
{
shaderObj = hwRenderer->CreateShader(ShaderType::HullShader, compiledShader.Value.Buffer(), compiledShader.Value.Count());
}
else if (compiledShader.Key == "tes")
{
shaderObj = hwRenderer->CreateShader(ShaderType::DomainShader, compiledShader.Value.Buffer(), compiledShader.Value.Count());
}
pipelineClass->shaders.Add(shaderObj);
}
//String keyStr = key;
//pipelineBuilder->SetDebugName(keyStr);
pipelineBuilder->SetShaders(From(pipelineClass->shaders).Select([](const RefPtr<Shader>& s) {return s.Ptr(); }).ToList().GetArrayView());
List<RefPtr<DescriptorSetLayout>> descSetLayouts;
for (auto & descSet : rs.BindingLayouts)
{
if (descSet.Value.BindingPoint == -1)
continue;
if (descSet.Key == "NoAnimation")
{
for (auto & desc : descSet.Value.Descriptors)
desc.Stages = (StageFlags)(StageFlags::sfVertex | StageFlags::sfFragment);
}
auto layout = hwRenderer->CreateDescriptorSetLayout(descSet.Value.Descriptors.GetArrayView());
if (descSet.Value.BindingPoint >= descSetLayouts.Count())
descSetLayouts.SetSize(descSet.Value.BindingPoint + 1);
descSetLayouts[descSet.Value.BindingPoint] = layout;
}
pipelineBuilder->SetBindingLayout(From(descSetLayouts).Select([](auto x) {return x.Ptr(); }).ToList().GetArrayView());
pipelineClass->pipeline = pipelineBuilder->ToPipeline(renderTargetLayout);
pipelineObjects[shaderKeyBuilder.Key] = pipelineClass;
return pipelineClass.Ptr();
}
void ModuleInstance::SetUniformData(void * data, int length)
{
#ifdef _DEBUG
if (length > BufferLength)
throw HardwareRendererException("insufficient uniform buffer.");
#endif
if (length && UniformMemory)
{
currentDescriptor++;
currentDescriptor = currentDescriptor % DynamicBufferLengthMultiplier;
int alternateBufferOffset = currentDescriptor * BufferLength;
UniformMemory->SetDataAsync(BufferOffset + alternateBufferOffset, data, length);
//currentDescriptor = frameId;
}
bool keyChanged = false;
if (SpecializeParamOffsets.Count())
{
if (currentSpecializationKey.Count() != SpecializeParamOffsets.Count())
{
currentSpecializationKey.SetSize(SpecializeParamOffsets.Count());
keyChanged = true;
}
for (int i = 0; i < SpecializeParamOffsets.Count(); i++)
{
int param = *(int*)((char*)data + SpecializeParamOffsets[i]);
if (currentSpecializationKey[i] != param)
{
keyChanged = true;
currentSpecializationKey[i] = param;
}
}
}
if (keyChanged)
{
specializedModule = spSpecializeModule(spireContext, module, currentSpecializationKey.Buffer(), currentSpecializationKey.Count(), nullptr);
ModuleId = spGetModuleUID(specializedModule);
}
}
//IMPL_POOL_ALLOCATOR(ModuleInstance, MaxModuleInstances)
ModuleInstance::~ModuleInstance()
{
if (UniformMemory)
UniformMemory->Free((char*)UniformMemory->BufferPtr() + BufferOffset, BufferLength * DynamicBufferLengthMultiplier);
}
void ModuleInstance::SetDescriptorSetLayout(HardwareRenderer * hw, DescriptorSetLayout * layout)
{
descriptors.Clear();
for (int i = 0; i < descriptors.GetCapacity(); i++)
descriptors.Add(hw->CreateDescriptorSet(layout));
}
}