forked from AcademySoftwareFoundation/MaterialX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVkResourceBindingContext.cpp
More file actions
180 lines (157 loc) · 6.82 KB
/
VkResourceBindingContext.cpp
File metadata and controls
180 lines (157 loc) · 6.82 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
//
// TM & (c) 2022 Lucasfilm Entertainment Company Ltd. and Lucasfilm Ltd.
// All rights reserved. See LICENSE.txt for license.
//
#include <MaterialXGenGlsl/VkResourceBindingContext.h>
MATERIALX_NAMESPACE_BEGIN
//
// VkResourceBindingContext methods
//
VkResourceBindingContext::VkResourceBindingContext(size_t uniformBindingLocation) :
_hwInitUniformBindLocation(uniformBindingLocation)
{
}
void VkResourceBindingContext::initialize()
{
// Reset bind location counter.
_hwUniformBindLocation = _hwInitUniformBindLocation;
}
void VkResourceBindingContext::emitDirectives(GenContext& context, ShaderStage& stage)
{
ShaderGenerator& generator = context.getShaderGenerator();
// Write shader stage directives for Vulkan compliance
std::string shaderStage;
if (stage.getName() == Stage::VERTEX)
{
shaderStage = "vertex";
}
else if (stage.getName() == Stage::PIXEL)
{
shaderStage = "fragment";
}
if (!shaderStage.empty())
{
generator.emitLine("#pragma shader_stage(" + shaderStage + ")", stage, false);
}
}
void VkResourceBindingContext::emitResourceBindings(GenContext& context, const VariableBlock& uniforms, ShaderStage& stage)
{
ShaderGenerator& generator = context.getShaderGenerator();
const Syntax& syntax = generator.getSyntax();
// First, emit all value uniforms in a block with single layout binding
bool hasValueUniforms = false;
for (auto uniform : uniforms.getVariableOrder())
{
if (uniform->getType() != Type::FILENAME)
{
hasValueUniforms = true;
break;
}
}
if (hasValueUniforms)
{
generator.emitLine("layout (std140, binding=" + std::to_string(_hwUniformBindLocation++) + ") " +
syntax.getUniformQualifier() + " " + uniforms.getName() + "_" + stage.getName(),
stage, false);
generator.emitScopeBegin(stage);
for (auto uniform : uniforms.getVariableOrder())
{
if (uniform->getType() != Type::FILENAME)
{
generator.emitLineBegin(stage);
generator.emitVariableDeclaration(uniform, EMPTY_STRING, context, stage, false);
generator.emitString(Syntax::SEMICOLON, stage);
generator.emitLineEnd(stage, false);
}
}
generator.emitScopeEnd(stage, true);
}
// Second, emit all sampler uniforms as separate uniforms with separate layout bindings
for (auto uniform : uniforms.getVariableOrder())
{
if (uniform->getType() == Type::FILENAME)
{
generator.emitString("layout (binding=" + std::to_string(_hwUniformBindLocation++) + ") " + syntax.getUniformQualifier() + " ", stage);
generator.emitVariableDeclaration(uniform, EMPTY_STRING, context, stage, false);
generator.emitLineEnd(stage, true);
}
}
generator.emitLineBreak(stage);
}
void VkResourceBindingContext::emitStructuredResourceBindings(GenContext& context, const VariableBlock& uniforms,
ShaderStage& stage, const std::string& structInstanceName,
const std::string& arraySuffix)
{
ShaderGenerator& generator = context.getShaderGenerator();
const Syntax& syntax = generator.getSyntax();
// Glsl structures need to be aligned. We make a best effort to base align struct members and add
// padding if required.
// https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_uniform_buffer_object.txt
const size_t baseAlignment = 16;
std::unordered_map<const TypeDesc*, size_t> alignmentMap({ { Type::FLOAT, baseAlignment / 4 },
{ Type::INTEGER, baseAlignment / 4 },
{ Type::BOOLEAN, baseAlignment / 4 },
{ Type::COLOR3, baseAlignment },
{ Type::COLOR4, baseAlignment },
{ Type::VECTOR2, baseAlignment },
{ Type::VECTOR3, baseAlignment },
{ Type::VECTOR4, baseAlignment },
{ Type::MATRIX33, baseAlignment * 4 },
{ Type::MATRIX44, baseAlignment * 4 } });
// Get struct alignment and size
// alignment, uniform member index
vector<std::pair<size_t, size_t>> memberOrder;
size_t structSize = 0;
for (size_t i = 0; i < uniforms.size(); ++i)
{
auto it = alignmentMap.find(uniforms[i]->getType());
if (it == alignmentMap.end())
{
structSize += baseAlignment;
memberOrder.push_back(std::make_pair(baseAlignment, i));
}
else
{
structSize += it->second;
memberOrder.push_back(std::make_pair(it->second, i));
}
}
// Align up and determine number of padding floats to add
const size_t numPaddingfloats =
(((structSize + (baseAlignment - 1)) & ~(baseAlignment - 1)) - structSize) / 4;
// Sort order from largest to smallest
std::sort(memberOrder.begin(), memberOrder.end(),
[](const std::pair<size_t, size_t>& a, const std::pair<size_t, size_t>& b)
{
return a.first > b.first;
});
// Emit the struct
generator.emitLine("struct " + uniforms.getName(), stage, false);
generator.emitScopeBegin(stage);
for (size_t i = 0; i < uniforms.size(); ++i)
{
size_t variableIndex = memberOrder[i].second;
generator.emitLineBegin(stage);
generator.emitVariableDeclaration(
uniforms[variableIndex], EMPTY_STRING, context, stage, false);
generator.emitString(Syntax::SEMICOLON, stage);
generator.emitLineEnd(stage, false);
}
// Emit padding
for (size_t i = 0; i < numPaddingfloats; ++i)
{
generator.emitLine("float pad" + std::to_string(i), stage, true);
}
generator.emitScopeEnd(stage, true);
// Emit binding information
generator.emitLineBreak(stage);
generator.emitLine("layout (std140, binding=" +
std::to_string(_hwUniformBindLocation++) + ") " +
syntax.getUniformQualifier() + " " + uniforms.getName() + "_" +
stage.getName(),
stage, false);
generator.emitScopeBegin(stage);
generator.emitLine(uniforms.getName() + " " + structInstanceName + arraySuffix, stage);
generator.emitScopeEnd(stage, true);
}
MATERIALX_NAMESPACE_END