Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class CustomFunction : public ShaderNode {
int32_t build(QString &code, QStack<QString> &stack, const AbstractNodeGraph::Link &link, int32_t &depth, int32_t &type) override {
if(m_position == -1) {
if(!m_func.isEmpty()) {
static_cast<ShaderGraph *>(m_graph)->addFunction(m_funcName, m_func);
static_cast<ShaderGraph *>(m_graph)->addFragmentFunction(m_funcName, m_func);

if(link.oport->m_type != MetaType::INVALID) {
type = link.oport->m_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class ExtractPosition : public ShaderNode {
" return vec4(m[3][0], m[3][1], m[3][2], m[3][3]);\n"
"}\n";

static_cast<ShaderGraph *>(m_graph)->addFunction("ExtractPosition", func);
static_cast<ShaderGraph *>(m_graph)->addFragmentFunction("ExtractPosition", func);

QStringList arguments = getArguments(code, stack, depth, type);

Expand Down
37 changes: 27 additions & 10 deletions modules/editor/shadertools/converter/shadergraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ ShaderGraph::ShaderGraph() :
Split::registerClassFactory(Engine::resourceSystem());
Swizzle::registerClassFactory(Engine::resourceSystem());

CustomFunction::registerClassFactory(Engine::resourceSystem());

// Common
NodeGroup::registerClassFactory(Engine::resourceSystem());

Expand Down Expand Up @@ -307,7 +309,7 @@ GraphNode *ShaderGraph::nodeCreate(const TString &type, int &index) {
return node;
} else { // Self exposed function
if(!type.isEmpty()) {
CustomFunction *function = new CustomFunction();
CustomFunction *function = Engine::objectCreate<CustomFunction>(type);
function->exposeFunction(m_exposedFunctions[type]);
function->setGraph(this);

Expand Down Expand Up @@ -404,12 +406,19 @@ bool ShaderGraph::buildGraph(GraphNode *node) {
setPragma("uniforms", layout);

// Functions
TString functions;
for(const auto &it : m_functions) {
functions += it.second + '\n';
TString vertexFunctions;
for(const auto &it : m_vertexFunctions) {
vertexFunctions += it.second + '\n';
}

setPragma("vertexFunctions", vertexFunctions);

TString fragmentFunctions;
for(const auto &it : m_fragmentFunctions) {
fragmentFunctions += it.second + '\n';
}

setPragma("functions", functions);
setPragma("fragmentFunctions", fragmentFunctions);

return true;
}
Expand Down Expand Up @@ -558,10 +567,17 @@ void ShaderGraph::addUniform(const TString &name, uint8_t type, const Variant &v
m_uniforms.push_back({name, type, 1, value});
}

void ShaderGraph::addFunction(const TString &name, TString &code) {
auto it = m_functions.find(name);
if(it == m_functions.end()) {
m_functions[name] = code;
void ShaderGraph::addVertexFunction(const TString &name, TString &code) {
auto it = m_vertexFunctions.find(name);
if(it == m_vertexFunctions.end()) {
m_vertexFunctions[name] = code;
}
}

void ShaderGraph::addFragmentFunction(const TString &name, TString &code) {
auto it = m_fragmentFunctions.find(name);
if(it == m_fragmentFunctions.end()) {
m_fragmentFunctions[name] = code;
}
}

Expand Down Expand Up @@ -673,7 +689,8 @@ TString ShaderGraph::buildFrom(GraphNode *node, Stage stage) {
void ShaderGraph::cleanup() {
m_textures.clear();
m_uniforms.clear();
m_functions.clear();
m_vertexFunctions.clear();
m_fragmentFunctions.clear();
m_pragmas.clear();
}

Expand Down
6 changes: 4 additions & 2 deletions modules/editor/shadertools/converter/shadergraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class ShaderGraph : public AbstractNodeGraph {

void addUniform(const TString &name, uint8_t type, const Variant &value);

void addFunction(const TString &name, TString &code);
void addVertexFunction(const TString &name, TString &code);
void addFragmentFunction(const TString &name, TString &code);

StringList nodeList() const override;

Expand Down Expand Up @@ -116,7 +117,8 @@ class ShaderGraph : public AbstractNodeGraph {

std::list<std::pair<TString, int32_t>> m_textures;

std::map<TString, TString> m_functions;
std::map<TString, TString> m_vertexFunctions;
std::map<TString, TString> m_fragmentFunctions;

std::map<TString, TString> m_exposedFunctions;

Expand Down
2 changes: 1 addition & 1 deletion modules/editor/shadertools/shaders/Billboard.vert
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ layout(location = 7) flat out vec4 _objectId;
layout(location = 8) flat out int _instanceOffset;
layout(location = 9) out mat4 _modelView;

#pragma functions
#pragma vertexFunctions

void main(void) {
#pragma offset
Expand Down
2 changes: 1 addition & 1 deletion modules/editor/shadertools/shaders/Shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ layout(location = 0) out vec4 gbuffer0;

#include "Functions.h"

#pragma functions
#pragma fragmentFunctions

void main(void) {
#pragma instance
Expand Down
2 changes: 1 addition & 1 deletion modules/editor/shadertools/shaders/Skinned.vert
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ layout(location = 7) flat out vec4 _objectId;
layout(location = 8) flat out int _instanceOffset;
layout(location = 9) out mat4 _modelView;

#pragma functions
#pragma vertexFunctions

void main(void) {
#pragma skinOffset
Expand Down
2 changes: 1 addition & 1 deletion modules/editor/shadertools/shaders/Static.vert
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ layout(location = 7) flat out vec4 _objectId;
layout(location = 8) flat out int _instanceOffset;
layout(location = 9) out mat4 _modelView;

#pragma functions
#pragma vertexFunctions

void main(void) {
#pragma offset
Expand Down
Loading