Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ClosureContext params storage #2195

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 0 additions & 38 deletions source/MaterialXGenShader/GenContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,44 +109,6 @@ void GenContext::getOutputSuffix(const ShaderOutput* output, string& suffix) con
}
}

ScopedSetClosureParams::ScopedSetClosureParams(const ClosureContext::ClosureParams* params, const ShaderNode* node, ClosureContext* cct) :
_cct(cct),
_node(node),
_oldParams(nullptr)
{
if (_cct)
{
_oldParams = _cct->getClosureParams(_node);
_cct->setClosureParams(_node, params);
}
}

ScopedSetClosureParams::ScopedSetClosureParams(const ShaderNode* fromNode, const ShaderNode* toNode, ClosureContext* cct) :
_cct(cct),
_node(toNode),
_oldParams(nullptr)
{
// The class must be safe for the cases where a context is not set
// so make sure to check for nullptr here.
if (_cct)
{
const ClosureContext::ClosureParams* newParams = _cct->getClosureParams(fromNode);
if (newParams)
{
_oldParams = _cct->getClosureParams(_node);
_cct->setClosureParams(_node, newParams);
}
}
}

ScopedSetClosureParams::~ScopedSetClosureParams()
{
if (_cct)
{
_cct->setClosureParams(_node, _oldParams);
}
}

ScopedSetVariableName::ScopedSetVariableName(const string& name, ShaderPort* port) :
_port(port),
_oldName(port->getVariable())
Expand Down
43 changes: 1 addition & 42 deletions source/MaterialXGenShader/GenContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,56 +294,15 @@ class MX_GENSHADER_API ClosureContext
}
[[deprecated]] const string& getSuffix(const TypeDesc* nodeType) const { return getSuffix(*nodeType); }

/// Set extra parameters to use for evaluating a closure.
void setClosureParams(const ShaderNode* closure, const ClosureParams* params)
{
if (params)
{
_params[closure] = params;
}
else
{
_params.erase(closure);
}
}

/// Return extra parameters to use for evaluating a closure. Or return
/// nullptr if no parameters have been set for the given closure.
const ClosureParams* getClosureParams(const ShaderNode* closure) const
{
auto it = _params.find(closure);
return it != _params.end() ? it->second : nullptr;
}

protected:
const int _type;
std::unordered_map<TypeDesc, Arguments, TypeDesc::Hasher> _arguments;
std::unordered_map<TypeDesc, string, TypeDesc::Hasher> _suffix;
std::unordered_map<const ShaderNode*, const ClosureParams*> _params;
// std::unordered_map<const ShaderNode*, const ClosureParams*> _params;

static const Arguments EMPTY_ARGUMENTS;
};

/// A RAII class for setting extra parameters for closure evaluation,
/// stored in the closure context.
class MX_GENSHADER_API ScopedSetClosureParams
{
public:
/// Constructor for setting explicit parameters for a closure node.
ScopedSetClosureParams(const ClosureContext::ClosureParams* params, const ShaderNode* node, ClosureContext* cct);

/// Constructor for setting parameters from one closure node to another.
ScopedSetClosureParams(const ShaderNode* fromNode, const ShaderNode* toNode, ClosureContext* cct);

/// Destructor restoring the closure parameter state.
~ScopedSetClosureParams();

private:
ClosureContext* _cct;
const ShaderNode* _node;
const ClosureContext::ClosureParams* _oldParams;
};

/// A RAII class for overriding port variable names.
class MX_GENSHADER_API ScopedSetVariableName
{
Expand Down
11 changes: 0 additions & 11 deletions source/MaterialXGenShader/Nodes/ClosureCompoundNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,17 +169,6 @@ void ClosureCompoundNode::emitFunctionCall(const ShaderNode& node, GenContext& c
const ShaderGraphOutputSocket* outputSocket = _rootGraph->getOutputSocket();
const TypeDesc closureType = outputSocket->getType();

// Check if extra parameters has been added for this node.
const ClosureContext::ClosureParams* params = cct->getClosureParams(&node);
if (closureType == Type::BSDF && params)
{
// Assign the parameters to the BSDF.
for (auto it : *params)
{
shadergen.emitLine(outputSocket->getVariable() + "." + it.first + " = " + shadergen.getUpstreamResult(it.second, context), stage);
}
}

// Emit function name.
shadergen.emitString(_functionName + cct->getSuffix(closureType) + "(", stage);

Expand Down
10 changes: 0 additions & 10 deletions source/MaterialXGenShader/Nodes/ClosureSourceCodeNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,7 @@ void ClosureSourceCodeNode::emitFunctionCall(const ShaderNode& node, GenContext&
ClosureContext* cct = context.getClosureContext();
if (cct)
{
// Check if extra parameters has been added for this node.
const TypeDesc closureType = output->getType();
const ClosureContext::ClosureParams* params = cct->getClosureParams(&node);
if (closureType == Type::BSDF && params)
{
// Assign the parameters to the BSDF.
for (auto it : *params)
{
shadergen.emitLine(output->getVariable() + "." + it.first + " = " + shadergen.getUpstreamResult(it.second, context), stage);
}
}

// Emit function name.
shadergen.emitLineBegin(stage);
Expand Down