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

Add ability to specify post-process material output location #3205

Merged
merged 2 commits into from
Oct 21, 2020
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
19 changes: 15 additions & 4 deletions filament/src/materials/colorGrading/colorGradingAsSubpass.mat
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ material {
name : colorBuffer,
}
],
outputs : [
{
name : color,
target : color,
type : float4,
location : 0
},
{
name : tonemappedOutput,
target : color,
type : float4,
location : 1
}
],
variables : [
vertex
],
Expand All @@ -51,9 +65,6 @@ vertex {

fragment {

// TODO: specify an output for this at location 1.
layout(location = 1) out vec4 tonemappedOutput;

#include "../../../../shaders/src/dithering.fs"
#include "../../../../shaders/src/vignette.fs"

Expand Down Expand Up @@ -108,8 +119,8 @@ fragment {
#else
postProcess.color = dithered;
#endif
postProcess.tonemappedOutput = postProcess.color;
}
tonemappedOutput = postProcess.color;
}

}
8 changes: 5 additions & 3 deletions libs/filamat/include/filamat/MaterialBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {

//! Add a new fragment shader output variable. Only valid for materials in the POST_PROCESS domain.
MaterialBuilder& output(VariableQualifier qualifier, OutputTarget target,
OutputType type, const char* name) noexcept;
OutputType type, const char* name, int location = -1) noexcept;

MaterialBuilder& enableFramebufferFetch() noexcept;

Expand Down Expand Up @@ -531,13 +531,15 @@ class UTILS_PUBLIC MaterialBuilder : public MaterialBuilderBase {
struct Output {
Output() noexcept = default;
Output(const char* outputName, VariableQualifier qualifier, OutputTarget target,
OutputType type)
: name(outputName), qualifier(qualifier), target(target), type(type) { }
OutputType type, int location) noexcept
: name(outputName), qualifier(qualifier), target(target), type(type),
location(location) { }

utils::CString name;
VariableQualifier qualifier;
OutputTarget target;
OutputType type;
int location;
};

static constexpr size_t MATERIAL_PROPERTIES_COUNT = filament::MATERIAL_PROPERTIES_COUNT;
Expand Down
13 changes: 11 additions & 2 deletions libs/filamat/src/MaterialBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,15 +761,24 @@ bool MaterialBuilder::generateShaders(const std::vector<Variant>& variants, Chun
}

MaterialBuilder& MaterialBuilder::output(VariableQualifier qualifier, OutputTarget target,
OutputType type, const char* name) noexcept {
OutputType type, const char* name, int location) noexcept {

ASSERT_PRECONDITION(target != OutputTarget::DEPTH || type == OutputType::FLOAT,
"Depth outputs must be of type FLOAT.");
ASSERT_PRECONDITION(target != OutputTarget::DEPTH || qualifier == VariableQualifier::OUT,
"Depth outputs must use OUT qualifier.");

ASSERT_PRECONDITION(location >= -1,
"Output location must be >= 0 (or use -1 for default location).");

// A location value of -1 signals using the default location. We'll simply take the previous
// output's location and add 1.
if (location == -1) {
location = mOutputs.empty() ? 0 : mOutputs.back().location + 1;
}

// Unconditionally add this output, then we'll check if we've maxed on on any particular target.
mOutputs.emplace_back(name, qualifier, target, type);
auto& output = mOutputs.emplace_back(name, qualifier, target, type, location);

uint8_t colorOutputCount = 0;
uint8_t depthOutputCount = 0;
Expand Down
3 changes: 1 addition & 2 deletions libs/filamat/src/shaders/ShaderGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,10 +542,9 @@ std::string ShaderGenerator::createPostProcessFragmentProgram(
cg.generatePostProcessGetters(fs, ShaderType::FRAGMENT);

// Generate post-process outputs.
size_t outputIndex = 0;
for (const auto& output : mOutputs) {
if (output.target == MaterialBuilder::OutputTarget::COLOR) {
cg.generateOutput(fs, ShaderType::FRAGMENT, output.name, outputIndex++,
cg.generateOutput(fs, ShaderType::FRAGMENT, output.name, output.location,
output.qualifier, output.type);
}
if (output.target == MaterialBuilder::OutputTarget::DEPTH) {
Expand Down
15 changes: 14 additions & 1 deletion tools/matc/src/matc/ParametersProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ static bool processOutput(MaterialBuilder& builder, const JsonishObject& jsonObj
}
}

const JsonishValue* locationValue = jsonObject.getValue("location");
if (locationValue) {
if (locationValue->getType() != JsonishValue::NUMBER) {
std::cerr << "outputs: location must be a NUMBER." << std::endl;
return false;
}
}

const char* name = nameValue->toJsonString()->getString().c_str();

OutputTarget target = OutputTarget::COLOR;
Expand All @@ -449,7 +457,12 @@ static bool processOutput(MaterialBuilder& builder, const JsonishObject& jsonObj
qualifier = Enums::toEnum<OutputQualifier>(qualifierValue->toJsonString()->getString());
}

builder.output(qualifier, target, type, name);
int location = -1;
if (locationValue) {
location = static_cast<int>(locationValue->toJsonNumber()->getFloat());
}

builder.output(qualifier, target, type, name, location);

return true;
}
Expand Down