Add support for parameters in QShaderGenerator

Change-Id: Iff8fec51ddbf9b018cb5ec3aff357125134013eb
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
This commit is contained in:
Kevin Ottens 2017-07-06 16:58:36 +02:00 committed by Sean Harmer
parent 13ecd686b9
commit 0d196b4268
2 changed files with 26 additions and 8 deletions

View File

@ -41,6 +41,22 @@
QT_BEGIN_NAMESPACE
namespace
{
QByteArray replaceParameters(const QByteArray &original, const QShaderNode &node)
{
auto result = original;
for (const auto &parameterName : node.parameterNames()) {
const auto placeholder = QByteArray(QByteArrayLiteral("$") + parameterName.toUtf8());
const auto value = node.parameter(parameterName).toString().toUtf8();
result.replace(placeholder, value);
}
return result;
}
}
QByteArray QShaderGenerator::createShaderCode() const
{
auto code = QByteArrayList();
@ -67,7 +83,7 @@ QByteArray QShaderGenerator::createShaderCode() const
for (const auto &node : graph.nodes()) {
for (const auto &snippet : node.rule(format).headerSnippets) {
code << snippet;
code << replaceParameters(snippet, node);
}
}
@ -93,7 +109,8 @@ QByteArray QShaderGenerator::createShaderCode() const
const auto variable = QByteArray(QByteArrayLiteral("v") + QByteArray::number(variableIndex));
line.replace(placeholder, variable);
}
code << QByteArrayLiteral(" ") + line;
code << QByteArrayLiteral(" ") + replaceParameters(line, node);
}
code << QByteArrayLiteral("}");

View File

@ -77,12 +77,13 @@ namespace
auto graph = QShaderGraph();
auto worldPosition = createNode({
createPort(QShaderNodePort::Output, "worldPosition")
createPort(QShaderNodePort::Output, "value")
});
worldPosition.addRule(openGLES2, QShaderNode::Rule("highp vec3 $worldPosition = worldPosition;",
QByteArrayList() << "varying highp vec3 worldPosition;"));
worldPosition.addRule(openGL3, QShaderNode::Rule("vec3 $worldPosition = worldPosition;",
QByteArrayList() << "in vec3 worldPosition;"));
worldPosition.setParameter("name", "worldPosition");
worldPosition.addRule(openGLES2, QShaderNode::Rule("highp vec3 $value = $name;",
QByteArrayList() << "varying highp vec3 $name;"));
worldPosition.addRule(openGL3, QShaderNode::Rule("vec3 $value = $name;",
QByteArrayList() << "in vec3 $name;"));
auto texture = createNode({
createPort(QShaderNodePort::Output, "texture")
@ -163,7 +164,7 @@ namespace
graph.addEdge(createEdge(texture.uuid(), "texture", sampleTexture.uuid(), "sampler"));
graph.addEdge(createEdge(texCoord.uuid(), "texCoord", sampleTexture.uuid(), "coord"));
graph.addEdge(createEdge(worldPosition.uuid(), "worldPosition", lightFunction.uuid(), "position"));
graph.addEdge(createEdge(worldPosition.uuid(), "value", lightFunction.uuid(), "position"));
graph.addEdge(createEdge(sampleTexture.uuid(), "color", lightFunction.uuid(), "baseColor"));
graph.addEdge(createEdge(lightIntensity.uuid(), "lightIntensity", lightFunction.uuid(), "lightIntensity"));