Add initial support for shader binding sets (WIP)

This commit is contained in:
Jérôme Leclercq
2021-06-14 22:35:05 +02:00
parent 815a7b0c62
commit f22b501e25
53 changed files with 885 additions and 511 deletions

View File

@@ -75,7 +75,7 @@ m_type(ShaderType::NotSet)
m_type = ShaderType::Fragment;
AddInput("UV", PrimitiveType::Float2, InputRole::TexCoord, 0, 0);
AddOutput("RenderTarget0", PrimitiveType::Float4, 0);
AddTexture("Potato", TextureType::Sampler2D, 1);
AddTexture("Potato", TextureType::Sampler2D, 0, 1);
UpdateTexturePreview(0, QImage(R"(C:\Users\Lynix\Pictures\potatavril.png)"));
@@ -106,12 +106,13 @@ ShaderGraph::~ShaderGraph()
m_flowScene.reset();
}
std::size_t ShaderGraph::AddBuffer(std::string name, BufferType bufferType, std::size_t structIndex, std::size_t bindingIndex)
std::size_t ShaderGraph::AddBuffer(std::string name, BufferType bufferType, std::size_t structIndex, std::size_t setIndex, std::size_t bindingIndex)
{
std::size_t index = m_buffers.size();
auto& bufferEntry = m_buffers.emplace_back();
bufferEntry.bindingIndex = bindingIndex;
bufferEntry.name = std::move(name);
bufferEntry.setIndex = setIndex;
bufferEntry.structIndex = structIndex;
bufferEntry.type = bufferType;
@@ -171,11 +172,12 @@ std::size_t ShaderGraph::AddStruct(std::string name, std::vector<StructMemberEnt
return index;
}
std::size_t ShaderGraph::AddTexture(std::string name, TextureType type, std::size_t bindingIndex)
std::size_t ShaderGraph::AddTexture(std::string name, TextureType type, std::size_t setIndex, std::size_t bindingIndex)
{
std::size_t index = m_textures.size();
auto& textureEntry = m_textures.emplace_back();
textureEntry.bindingIndex = bindingIndex;
textureEntry.setIndex = setIndex;
textureEntry.name = std::move(name);
textureEntry.type = type;
@@ -229,6 +231,7 @@ void ShaderGraph::Load(const QJsonObject& data)
BufferEntry& buffer = m_buffers.emplace_back();
buffer.bindingIndex = static_cast<std::size_t>(bufferDoc["bindingIndex"].toInt(0));
buffer.name = bufferDoc["name"].toString().toStdString();
buffer.setIndex = static_cast<std::size_t>(bufferDoc["setIndex"].toInt(0));
buffer.structIndex = bufferDoc["structIndex"].toInt();
buffer.type = DecodeEnum<BufferType>(bufferDoc["type"].toString().toStdString()).value();
}
@@ -308,6 +311,7 @@ void ShaderGraph::Load(const QJsonObject& data)
TextureEntry& texture = m_textures.emplace_back();
texture.bindingIndex = static_cast<std::size_t>(textureDoc["bindingIndex"].toInt(0));
texture.name = textureDoc["name"].toString().toStdString();
texture.setIndex = static_cast<std::size_t>(textureDoc["setIndex"].toInt(0));
texture.type = DecodeEnum<TextureType>(textureDoc["type"].toString().toStdString()).value();
}
@@ -332,6 +336,7 @@ QJsonObject ShaderGraph::Save()
QJsonObject bufferDoc;
bufferDoc["bindingIndex"] = int(buffer.bindingIndex);
bufferDoc["name"] = QString::fromStdString(buffer.name);
bufferDoc["setIndex"] = int(buffer.setIndex);
bufferDoc["structIndex"] = int(buffer.structIndex);
bufferDoc["type"] = QString(EnumToString(buffer.type));
@@ -422,6 +427,7 @@ QJsonObject ShaderGraph::Save()
QJsonObject textureDoc;
textureDoc["bindingIndex"] = int(texture.bindingIndex);
textureDoc["name"] = QString::fromStdString(texture.name);
textureDoc["setIndex"] = int(texture.setIndex);
textureDoc["type"] = QString(EnumToString(texture.type));
textureArray.append(textureDoc);
@@ -488,6 +494,7 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst() const
auto& extVar = external->externalVars.emplace_back();
extVar.bindingIndex = buffer.bindingIndex;
extVar.bindingSet = buffer.setIndex;
extVar.name = buffer.name;
extVar.type = Nz::ShaderAst::UniformType{ Nz::ShaderAst::IdentifierType{ structInfo.name } };
}
@@ -496,6 +503,7 @@ Nz::ShaderAst::StatementPtr ShaderGraph::ToAst() const
{
auto& extVar = external->externalVars.emplace_back();
extVar.bindingIndex = texture.bindingIndex;
extVar.bindingSet = texture.setIndex;
extVar.name = texture.name;
extVar.type = ToShaderExpressionType(texture.type);
}
@@ -569,12 +577,13 @@ Nz::ShaderAst::ExpressionType ShaderGraph::ToShaderExpressionType(const std::var
}, type);
};
void ShaderGraph::UpdateBuffer(std::size_t bufferIndex, std::string name, BufferType bufferType, std::size_t structIndex, std::size_t bindingIndex)
void ShaderGraph::UpdateBuffer(std::size_t bufferIndex, std::string name, BufferType bufferType, std::size_t structIndex, std::size_t setIndex, std::size_t bindingIndex)
{
assert(bufferIndex < m_buffers.size());
auto& bufferEntry = m_buffers[bufferIndex];
bufferEntry.bindingIndex = bindingIndex;
bufferEntry.name = std::move(name);
bufferEntry.setIndex = setIndex;
bufferEntry.structIndex = structIndex;
bufferEntry.type = bufferType;
@@ -624,12 +633,13 @@ void ShaderGraph::UpdateStruct(std::size_t structIndex, std::string name, std::v
OnStructUpdate(this, structIndex);
}
void ShaderGraph::UpdateTexture(std::size_t textureIndex, std::string name, TextureType type, std::size_t bindingIndex)
void ShaderGraph::UpdateTexture(std::size_t textureIndex, std::string name, TextureType type, std::size_t setIndex, std::size_t bindingIndex)
{
assert(textureIndex < m_textures.size());
auto& textureEntry = m_textures[textureIndex];
textureEntry.bindingIndex = bindingIndex;
textureEntry.name = std::move(name);
textureEntry.setIndex = setIndex;
textureEntry.type = type;
OnTextureUpdate(this, textureIndex);

View File

@@ -29,12 +29,12 @@ class ShaderGraph
ShaderGraph();
~ShaderGraph();
std::size_t AddBuffer(std::string name, BufferType bufferType, std::size_t structIndex, std::size_t bindingIndex);
std::size_t AddBuffer(std::string name, BufferType bufferType, std::size_t structIndex, std::size_t setIndex, std::size_t bindingIndex);
std::size_t AddCondition(std::string name);
std::size_t AddInput(std::string name, PrimitiveType type, InputRole role, std::size_t roleIndex, std::size_t locationIndex);
std::size_t AddOutput(std::string name, PrimitiveType type, std::size_t locationIndex);
std::size_t AddStruct(std::string name, std::vector<StructMemberEntry> members);
std::size_t AddTexture(std::string name, TextureType type, std::size_t bindingIndex);
std::size_t AddTexture(std::string name, TextureType type, std::size_t setIndex, std::size_t bindingIndex);
void Clear();
@@ -70,18 +70,19 @@ class ShaderGraph
Nz::ShaderAst::StatementPtr ToAst() const;
Nz::ShaderAst::ExpressionType ToShaderExpressionType(const std::variant<PrimitiveType, std::size_t>& type) const;
void UpdateBuffer(std::size_t bufferIndex, std::string name, BufferType bufferType, std::size_t structIndex, std::size_t bindingIndex);
void UpdateBuffer(std::size_t bufferIndex, std::string name, BufferType bufferType, std::size_t structIndex, std::size_t setIndex, std::size_t bindingIndex);
void UpdateCondition(std::size_t conditionIndex, std::string condition);
void UpdateInput(std::size_t inputIndex, std::string name, PrimitiveType type, InputRole role, std::size_t roleIndex, std::size_t locationIndex);
void UpdateOutput(std::size_t outputIndex, std::string name, PrimitiveType type, std::size_t locationIndex);
void UpdateStruct(std::size_t structIndex, std::string name, std::vector<StructMemberEntry> members);
void UpdateTexture(std::size_t textureIndex, std::string name, TextureType type, std::size_t bindingIndex);
void UpdateTexture(std::size_t textureIndex, std::string name, TextureType type, std::size_t setIndex, std::size_t bindingIndex);
void UpdateTexturePreview(std::size_t texture, QImage preview);
void UpdateType(ShaderType type);
struct BufferEntry
{
std::size_t bindingIndex;
std::size_t setIndex;
std::size_t structIndex;
std::string name;
BufferType type;
@@ -124,6 +125,7 @@ class ShaderGraph
struct TextureEntry
{
std::size_t bindingIndex;
std::size_t setIndex;
std::string name;
TextureType type;
QImage preview;

View File

@@ -26,11 +26,13 @@ m_shaderGraph(shaderGraph)
m_structList->addItem(QString::fromStdString(structEntry.name));
m_bindingIndex = new QSpinBox;
m_setIndex = new QSpinBox;
QFormLayout* formLayout = new QFormLayout;
formLayout->addRow(tr("Name"), m_outputName);
formLayout->addRow(tr("Type"), m_typeList);
formLayout->addRow(tr("Struct"), m_structList);
formLayout->addRow(tr("Set index"), m_setIndex);
formLayout->addRow(tr("Binding index"), m_bindingIndex);
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
@@ -48,6 +50,7 @@ BufferEditDialog::BufferEditDialog(const ShaderGraph& shaderGraph, const BufferI
BufferEditDialog(shaderGraph, parent)
{
m_bindingIndex->setValue(int(buffer.bindingIndex));
m_setIndex->setValue(int(buffer.setIndex));
m_outputName->setText(QString::fromStdString(buffer.name));
m_structList->setCurrentIndex(buffer.structIndex);
m_typeList->setCurrentIndex(int(buffer.type));
@@ -58,6 +61,7 @@ BufferInfo BufferEditDialog::GetBufferInfo() const
BufferInfo bufferInfo;
bufferInfo.bindingIndex = static_cast<std::size_t>(m_bindingIndex->value());
bufferInfo.name = m_outputName->text().toStdString();
bufferInfo.setIndex = static_cast<std::size_t>(m_setIndex->value());
bufferInfo.structIndex = m_structList->currentIndex();
bufferInfo.type = static_cast<BufferType>(m_typeList->currentIndex());

View File

@@ -14,6 +14,7 @@ class ShaderGraph;
struct BufferInfo
{
std::size_t bindingIndex;
std::size_t setIndex;
std::size_t structIndex;
std::string name;
BufferType type;
@@ -36,6 +37,7 @@ class BufferEditDialog : public QDialog
QComboBox* m_structList;
QLineEdit* m_outputName;
QSpinBox* m_bindingIndex;
QSpinBox* m_setIndex;
};
#include <ShaderNode/Widgets/BufferEditDialog.inl>

View File

@@ -38,7 +38,7 @@ void BufferEditor::OnAddBuffer()
connect(dialog, &QDialog::accepted, [this, dialog]
{
BufferInfo bufferInfo = dialog->GetBufferInfo();
m_shaderGraph.AddBuffer(std::move(bufferInfo.name), bufferInfo.type, bufferInfo.structIndex, bufferInfo.bindingIndex);
m_shaderGraph.AddBuffer(std::move(bufferInfo.name), bufferInfo.type, bufferInfo.structIndex, bufferInfo.setIndex, bufferInfo.bindingIndex);
});
dialog->open();
@@ -49,7 +49,9 @@ void BufferEditor::OnEditBuffer(int inputIndex)
const auto& buffer = m_shaderGraph.GetBuffer(inputIndex);
BufferInfo info;
info.bindingIndex = buffer.bindingIndex;
info.name = buffer.name;
info.setIndex = buffer.setIndex;
info.structIndex = buffer.structIndex;
info.type = buffer.type;
@@ -58,7 +60,7 @@ void BufferEditor::OnEditBuffer(int inputIndex)
connect(dialog, &QDialog::accepted, [this, dialog, inputIndex]
{
BufferInfo bufferInfo = dialog->GetBufferInfo();
m_shaderGraph.UpdateBuffer(inputIndex, std::move(bufferInfo.name), bufferInfo.type, bufferInfo.structIndex, bufferInfo.bindingIndex);
m_shaderGraph.UpdateBuffer(inputIndex, std::move(bufferInfo.name), bufferInfo.type, bufferInfo.structIndex, bufferInfo.setIndex, bufferInfo.bindingIndex);
});
dialog->open();

View File

@@ -81,6 +81,13 @@ void CodeOutputWidget::Refresh()
{
case OutputLanguage::GLSL:
{
Nz::GlslWriter::BindingMapping bindingMapping;
for (const auto& buffer : m_shaderGraph.GetBuffers())
bindingMapping.emplace(Nz::UInt64(buffer.setIndex) << 32 | Nz::UInt64(buffer.bindingIndex), bindingMapping.size());
for (const auto& texture : m_shaderGraph.GetTextures())
bindingMapping.emplace(Nz::UInt64(texture.setIndex) << 32 | Nz::UInt64(texture.bindingIndex), bindingMapping.size());
Nz::GlslWriter writer;
output = writer.Generate(ShaderGraph::ToShaderStageType(m_shaderGraph.GetType()), *shaderAst, bindingMapping, states);
break;

View File

@@ -20,10 +20,12 @@ QDialog(parent)
m_typeList->addItem(EnumToString(static_cast<TextureType>(i)));
m_bindingIndex = new QSpinBox;
m_setIndex = new QSpinBox;
QFormLayout* formLayout = new QFormLayout;
formLayout->addRow(tr("Name"), m_textureName);
formLayout->addRow(tr("Type"), m_typeList);
formLayout->addRow(tr("Set index"), m_setIndex);
formLayout->addRow(tr("Binding index"), m_bindingIndex);
QDialogButtonBox* buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
@@ -41,6 +43,7 @@ TextureEditDialog::TextureEditDialog(const TextureInfo& texture, QWidget* parent
TextureEditDialog(parent)
{
m_bindingIndex->setValue(int(texture.bindingIndex));
m_setIndex->setValue(int(texture.setIndex));
m_textureName->setText(QString::fromStdString(texture.name));
m_typeList->setCurrentText(EnumToString(texture.type));
}
@@ -50,6 +53,7 @@ TextureInfo TextureEditDialog::GetTextureInfo() const
TextureInfo inputInfo;
inputInfo.bindingIndex = static_cast<std::size_t>(m_bindingIndex->value());
inputInfo.name = m_textureName->text().toStdString();
inputInfo.setIndex = static_cast<std::size_t>(m_setIndex->value());
inputInfo.type = static_cast<TextureType>(m_typeList->currentIndex());
return inputInfo;

View File

@@ -13,6 +13,7 @@ class QSpinBox;
struct TextureInfo
{
std::size_t bindingIndex;
std::size_t setIndex;
std::string name;
TextureType type;
};
@@ -32,6 +33,7 @@ class TextureEditDialog : public QDialog
QComboBox* m_typeList;
QLineEdit* m_textureName;
QSpinBox* m_bindingIndex;
QSpinBox* m_setIndex;
};
#include <ShaderNode/Widgets/TextureEditDialog.inl>

View File

@@ -47,7 +47,7 @@ void TextureEditor::OnAddTexture()
connect(dialog, &QDialog::accepted, [this, dialog]
{
TextureInfo outputInfo = dialog->GetTextureInfo();
m_shaderGraph.AddTexture(std::move(outputInfo.name), outputInfo.type, outputInfo.bindingIndex);
m_shaderGraph.AddTexture(std::move(outputInfo.name), outputInfo.type, outputInfo.setIndex, outputInfo.bindingIndex);
});
dialog->open();
@@ -67,7 +67,7 @@ void TextureEditor::OnEditTexture(int inputIndex)
connect(dialog, &QDialog::accepted, [this, dialog, inputIndex]
{
TextureInfo textureInfo = dialog->GetTextureInfo();
m_shaderGraph.UpdateTexture(inputIndex, std::move(textureInfo.name), textureInfo.type, textureInfo.bindingIndex);
m_shaderGraph.UpdateTexture(inputIndex, std::move(textureInfo.name), textureInfo.type, textureInfo.setIndex, textureInfo.bindingIndex);
});
dialog->open();