SkSL now respects layout(key) on all variables

Change-Id: I33332967bba0f16a73633f13ffa851e38eba100d
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/224737
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ethan Nicholas <ethannicholas@google.com>
This commit is contained in:
Ethan Nicholas 2019-07-01 13:32:07 -04:00 committed by Skia Commit-Bot
parent 41f937f042
commit cab767f219
2 changed files with 85 additions and 57 deletions

View File

@ -128,7 +128,14 @@ GrGLSLFragmentProcessor* GrRectBlurEffect::onCreateGLSLInstance() const {
return new GrGLSLRectBlurEffect();
}
void GrRectBlurEffect::onGetGLSLProcessorKey(const GrShaderCaps& caps,
GrProcessorKeyBuilder* b) const {}
GrProcessorKeyBuilder* b) const {
bool highPrecision = ((((abs(rect.left()) > 16000.0 || abs(rect.top()) > 16000.0) ||
abs(rect.right()) > 16000.0) ||
abs(rect.bottom()) > 16000.0) ||
abs(rect.right() - rect.left()) > 16000.0) ||
abs(rect.bottom() - rect.top()) > 16000.0;
b->add32((int32_t)highPrecision);
}
bool GrRectBlurEffect::onIsEqual(const GrFragmentProcessor& other) const {
const GrRectBlurEffect& that = other.cast<GrRectBlurEffect>();
(void)that;

View File

@ -1101,64 +1101,85 @@ void CPPCodeGenerator::writeGetKey() {
this->writef("void %s::onGetGLSLProcessorKey(const GrShaderCaps& caps, "
"GrProcessorKeyBuilder* b) const {\n",
fFullName.c_str());
for (const auto& param : fSectionAndParameterHelper.getParameters()) {
String nameString(param->fName);
const char* name = nameString.c_str();
if (param->fModifiers.fLayout.fKey != Layout::kNo_Key &&
(param->fModifiers.fFlags & Modifiers::kUniform_Flag)) {
fErrors.error(param->fOffset,
"layout(key) may not be specified on uniforms");
}
switch (param->fModifiers.fLayout.fKey) {
case Layout::kKey_Key:
if (param->fModifiers.fLayout.fWhen.fLength) {
this->writef("if (%s) {", String(param->fModifiers.fLayout.fWhen).c_str());
for (const auto& p : fProgram) {
if (ProgramElement::kVar_Kind == p.fKind) {
const VarDeclarations& decls = (const VarDeclarations&) p;
for (const auto& raw : decls.fVars) {
const VarDeclaration& decl = (VarDeclaration&) *raw;
const Variable& var = *decl.fVar;
String nameString(var.fName);
const char* name = nameString.c_str();
if (var.fModifiers.fLayout.fKey != Layout::kNo_Key &&
(var.fModifiers.fFlags & Modifiers::kUniform_Flag)) {
fErrors.error(var.fOffset,
"layout(key) may not be specified on uniforms");
}
if (param->fType == *fContext.fFloat4x4_Type) {
ABORT("no automatic key handling for float4x4\n");
} else if (param->fType == *fContext.fFloat2_Type) {
this->writef(" b->add32(%s.fX);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.fY);\n",
HCodeGenerator::FieldName(name).c_str());
} else if (param->fType == *fContext.fFloat4_Type) {
this->writef(" b->add32(%s.x());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.y());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.width());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.height());\n",
HCodeGenerator::FieldName(name).c_str());
} else if (param->fType == *fContext.fHalf4_Type) {
this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
HCodeGenerator::FieldName(name).c_str());
this->write(" b->add32(((uint32_t)red << 16) | green);\n");
this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
} else {
this->writef(" b->add32((int32_t) %s);\n",
HCodeGenerator::FieldName(name).c_str());
switch (var.fModifiers.fLayout.fKey) {
case Layout::kKey_Key:
if (is_private(var)) {
this->writef("%s %s =",
HCodeGenerator::FieldType(fContext, var.fType,
var.fModifiers.fLayout).c_str(),
String(var.fName).c_str());
if (decl.fValue) {
fCPPMode = true;
this->writeExpression(*decl.fValue, kAssignment_Precedence);
fCPPMode = false;
} else {
this->writef("%s", default_value(var).c_str());
}
this->write(";\n");
}
if (var.fModifiers.fLayout.fWhen.fLength) {
this->writef("if (%s) {", String(var.fModifiers.fLayout.fWhen).c_str());
}
if (var.fType == *fContext.fFloat4x4_Type) {
ABORT("no automatic key handling for float4x4\n");
} else if (var.fType == *fContext.fFloat2_Type) {
this->writef(" b->add32(%s.fX);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.fY);\n",
HCodeGenerator::FieldName(name).c_str());
} else if (var.fType == *fContext.fFloat4_Type) {
this->writef(" b->add32(%s.x());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.y());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.width());\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" b->add32(%s.height());\n",
HCodeGenerator::FieldName(name).c_str());
} else if (var.fType == *fContext.fHalf4_Type) {
this->writef(" uint16_t red = SkFloatToHalf(%s.fR);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t green = SkFloatToHalf(%s.fG);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t blue = SkFloatToHalf(%s.fB);\n",
HCodeGenerator::FieldName(name).c_str());
this->writef(" uint16_t alpha = SkFloatToHalf(%s.fA);\n",
HCodeGenerator::FieldName(name).c_str());
this->write(" b->add32(((uint32_t)red << 16) | green);\n");
this->write(" b->add32(((uint32_t)blue << 16) | alpha);\n");
} else {
this->writef(" b->add32((int32_t) %s);\n",
HCodeGenerator::FieldName(name).c_str());
}
if (var.fModifiers.fLayout.fWhen.fLength) {
this->write("}");
}
break;
case Layout::kIdentity_Key:
if (var.fType.kind() != Type::kMatrix_Kind) {
fErrors.error(var.fOffset,
"layout(key=identity) requires matrix type");
}
this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
HCodeGenerator::FieldName(name).c_str());
break;
case Layout::kNo_Key:
break;
}
if (param->fModifiers.fLayout.fWhen.fLength) {
this->write("}");
}
break;
case Layout::kIdentity_Key:
if (param->fType.kind() != Type::kMatrix_Kind) {
fErrors.error(param->fOffset,
"layout(key=identity) requires matrix type");
}
this->writef(" b->add32(%s.isIdentity() ? 1 : 0);\n",
HCodeGenerator::FieldName(name).c_str());
break;
case Layout::kNo_Key:
break;
}
}
}
this->write("}\n");