blob: c00ef536e4533e2286a6c7d1270aaa5461076af8 [file] [log] [blame]
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "src/sksl/codegen/SkSLSPIRVCodeGenerator.h"
#include "src/sksl/GLSL.std.450.h"
#include "src/sksl/SkSLCompiler.h"
#include "src/sksl/SkSLOperators.h"
#include "src/sksl/ir/SkSLBlock.h"
#include "src/sksl/ir/SkSLExpressionStatement.h"
#include "src/sksl/ir/SkSLExtension.h"
#include "src/sksl/ir/SkSLIndexExpression.h"
#include "src/sksl/ir/SkSLVariableReference.h"
#ifdef SK_VULKAN
#include "src/gpu/vk/GrVkCaps.h"
#endif
#define kLast_Capability SpvCapabilityMultiViewport
namespace SkSL {
static const int32_t SKSL_MAGIC = 0x0; // FIXME: we should probably register a magic number
void SPIRVCodeGenerator::setupIntrinsics() {
#define ALL_GLSL(x) std::make_tuple(kGLSL_STD_450_IntrinsicKind, GLSLstd450 ## x, GLSLstd450 ## x, \
GLSLstd450 ## x, GLSLstd450 ## x)
#define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) std::make_tuple(kGLSL_STD_450_IntrinsicKind, \
GLSLstd450 ## ifFloat, \
GLSLstd450 ## ifInt, \
GLSLstd450 ## ifUInt, \
SpvOpUndef)
#define ALL_SPIRV(x) std::make_tuple(kSPIRV_IntrinsicKind, SpvOp ## x, SpvOp ## x, SpvOp ## x, \
SpvOp ## x)
#define SPECIAL(x) std::make_tuple(kSpecial_IntrinsicKind, k ## x ## _SpecialIntrinsic, \
k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \
k ## x ## _SpecialIntrinsic)
fIntrinsicMap[String("round")] = ALL_GLSL(Round);
fIntrinsicMap[String("roundEven")] = ALL_GLSL(RoundEven);
fIntrinsicMap[String("trunc")] = ALL_GLSL(Trunc);
fIntrinsicMap[String("abs")] = BY_TYPE_GLSL(FAbs, SAbs, SAbs);
fIntrinsicMap[String("sign")] = BY_TYPE_GLSL(FSign, SSign, SSign);
fIntrinsicMap[String("floor")] = ALL_GLSL(Floor);
fIntrinsicMap[String("ceil")] = ALL_GLSL(Ceil);
fIntrinsicMap[String("fract")] = ALL_GLSL(Fract);
fIntrinsicMap[String("radians")] = ALL_GLSL(Radians);
fIntrinsicMap[String("degrees")] = ALL_GLSL(Degrees);
fIntrinsicMap[String("sin")] = ALL_GLSL(Sin);
fIntrinsicMap[String("cos")] = ALL_GLSL(Cos);
fIntrinsicMap[String("tan")] = ALL_GLSL(Tan);
fIntrinsicMap[String("asin")] = ALL_GLSL(Asin);
fIntrinsicMap[String("acos")] = ALL_GLSL(Acos);
fIntrinsicMap[String("atan")] = SPECIAL(Atan);
fIntrinsicMap[String("sinh")] = ALL_GLSL(Sinh);
fIntrinsicMap[String("cosh")] = ALL_GLSL(Cosh);
fIntrinsicMap[String("tanh")] = ALL_GLSL(Tanh);
fIntrinsicMap[String("asinh")] = ALL_GLSL(Asinh);
fIntrinsicMap[String("acosh")] = ALL_GLSL(Acosh);
fIntrinsicMap[String("atanh")] = ALL_GLSL(Atanh);
fIntrinsicMap[String("pow")] = ALL_GLSL(Pow);
fIntrinsicMap[String("exp")] = ALL_GLSL(Exp);
fIntrinsicMap[String("log")] = ALL_GLSL(Log);
fIntrinsicMap[String("exp2")] = ALL_GLSL(Exp2);
fIntrinsicMap[String("log2")] = ALL_GLSL(Log2);
fIntrinsicMap[String("sqrt")] = ALL_GLSL(Sqrt);
fIntrinsicMap[String("inverse")] = ALL_GLSL(MatrixInverse);
fIntrinsicMap[String("outerProduct")] = ALL_SPIRV(OuterProduct);
fIntrinsicMap[String("transpose")] = ALL_SPIRV(Transpose);
fIntrinsicMap[String("isinf")] = ALL_SPIRV(IsInf);
fIntrinsicMap[String("isnan")] = ALL_SPIRV(IsNan);
fIntrinsicMap[String("inversesqrt")] = ALL_GLSL(InverseSqrt);
fIntrinsicMap[String("determinant")] = ALL_GLSL(Determinant);
fIntrinsicMap[String("matrixCompMult")] = SPECIAL(MatrixCompMult);
fIntrinsicMap[String("matrixInverse")] = ALL_GLSL(MatrixInverse);
fIntrinsicMap[String("mod")] = SPECIAL(Mod);
fIntrinsicMap[String("modf")] = ALL_GLSL(Modf);
fIntrinsicMap[String("min")] = SPECIAL(Min);
fIntrinsicMap[String("max")] = SPECIAL(Max);
fIntrinsicMap[String("clamp")] = SPECIAL(Clamp);
fIntrinsicMap[String("saturate")] = SPECIAL(Saturate);
fIntrinsicMap[String("dot")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDot,
SpvOpUndef, SpvOpUndef, SpvOpUndef);
fIntrinsicMap[String("mix")] = SPECIAL(Mix);
fIntrinsicMap[String("step")] = SPECIAL(Step);
fIntrinsicMap[String("smoothstep")] = SPECIAL(SmoothStep);
fIntrinsicMap[String("fma")] = ALL_GLSL(Fma);
fIntrinsicMap[String("frexp")] = ALL_GLSL(Frexp);
fIntrinsicMap[String("ldexp")] = ALL_GLSL(Ldexp);
#define PACK(type) fIntrinsicMap[String("pack" #type)] = ALL_GLSL(Pack ## type); \
fIntrinsicMap[String("unpack" #type)] = ALL_GLSL(Unpack ## type)
PACK(Snorm4x8);
PACK(Unorm4x8);
PACK(Snorm2x16);
PACK(Unorm2x16);
PACK(Half2x16);
PACK(Double2x32);
fIntrinsicMap[String("length")] = ALL_GLSL(Length);
fIntrinsicMap[String("distance")] = ALL_GLSL(Distance);
fIntrinsicMap[String("cross")] = ALL_GLSL(Cross);
fIntrinsicMap[String("normalize")] = ALL_GLSL(Normalize);
fIntrinsicMap[String("faceforward")] = ALL_GLSL(FaceForward);
fIntrinsicMap[String("reflect")] = ALL_GLSL(Reflect);
fIntrinsicMap[String("refract")] = ALL_GLSL(Refract);
fIntrinsicMap[String("bitCount")] = ALL_SPIRV(BitCount);
fIntrinsicMap[String("findLSB")] = ALL_GLSL(FindILsb);
fIntrinsicMap[String("findMSB")] = BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb);
fIntrinsicMap[String("dFdx")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpDPdx,
SpvOpUndef, SpvOpUndef, SpvOpUndef);
fIntrinsicMap[String("dFdy")] = SPECIAL(DFdy);
fIntrinsicMap[String("fwidth")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpFwidth,
SpvOpUndef, SpvOpUndef, SpvOpUndef);
fIntrinsicMap[String("makeSampler2D")] = SPECIAL(SampledImage);
fIntrinsicMap[String("sample")] = SPECIAL(Texture);
fIntrinsicMap[String("subpassLoad")] = SPECIAL(SubpassLoad);
fIntrinsicMap[String("floatBitsToInt")] = ALL_SPIRV(Bitcast);
fIntrinsicMap[String("floatBitsToUint")] = ALL_SPIRV(Bitcast);
fIntrinsicMap[String("intBitsToFloat")] = ALL_SPIRV(Bitcast);
fIntrinsicMap[String("uintBitsToFloat")] = ALL_SPIRV(Bitcast);
fIntrinsicMap[String("any")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
SpvOpUndef, SpvOpUndef, SpvOpAny);
fIntrinsicMap[String("all")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
SpvOpUndef, SpvOpUndef, SpvOpAll);
fIntrinsicMap[String("not")] = std::make_tuple(kSPIRV_IntrinsicKind, SpvOpUndef,
SpvOpUndef, SpvOpUndef,
SpvOpLogicalNot);
fIntrinsicMap[String("equal")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdEqual, SpvOpIEqual,
SpvOpIEqual, SpvOpLogicalEqual);
fIntrinsicMap[String("notEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdNotEqual, SpvOpINotEqual,
SpvOpINotEqual,
SpvOpLogicalNotEqual);
fIntrinsicMap[String("lessThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdLessThan, SpvOpSLessThan,
SpvOpULessThan, SpvOpUndef);
fIntrinsicMap[String("lessThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdLessThanEqual,
SpvOpSLessThanEqual,
SpvOpULessThanEqual,
SpvOpUndef);
fIntrinsicMap[String("greaterThan")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdGreaterThan,
SpvOpSGreaterThan,
SpvOpUGreaterThan,
SpvOpUndef);
fIntrinsicMap[String("greaterThanEqual")] = std::make_tuple(kSPIRV_IntrinsicKind,
SpvOpFOrdGreaterThanEqual,
SpvOpSGreaterThanEqual,
SpvOpUGreaterThanEqual,
SpvOpUndef);
fIntrinsicMap[String("EmitVertex")] = ALL_SPIRV(EmitVertex);
fIntrinsicMap[String("EndPrimitive")] = ALL_SPIRV(EndPrimitive);
// interpolateAt* not yet supported...
}
void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) {
out.write((const char*) &word, sizeof(word));
}
static bool is_float(const Context& context, const Type& type) {
return (type.isScalar() || type.isVector() || type.isMatrix()) &&
type.componentType().isFloat();
}
static bool is_signed(const Context& context, const Type& type) {
return type.isEnum() ||
((type.isScalar() || type.isVector()) && type.componentType().isSigned());
}
static bool is_unsigned(const Context& context, const Type& type) {
return (type.isScalar() || type.isVector()) && type.componentType().isUnsigned();
}
static bool is_bool(const Context& context, const Type& type) {
return (type.isScalar() || type.isVector()) && type.componentType().isBoolean();
}
static bool is_out(const Variable& var) {
return (var.modifiers().fFlags & Modifiers::kOut_Flag) != 0;
}
void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) {
SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer);
SkASSERT(opCode != SpvOpUndef);
switch (opCode) {
case SpvOpReturn: // fall through
case SpvOpReturnValue: // fall through
case SpvOpKill: // fall through
case SpvOpSwitch: // fall through
case SpvOpBranch: // fall through
case SpvOpBranchConditional:
SkASSERT(fCurrentBlock);
fCurrentBlock = 0;
break;
case SpvOpConstant: // fall through
case SpvOpConstantTrue: // fall through
case SpvOpConstantFalse: // fall through
case SpvOpConstantComposite: // fall through
case SpvOpTypeVoid: // fall through
case SpvOpTypeInt: // fall through
case SpvOpTypeFloat: // fall through
case SpvOpTypeBool: // fall through
case SpvOpTypeVector: // fall through
case SpvOpTypeMatrix: // fall through
case SpvOpTypeArray: // fall through
case SpvOpTypePointer: // fall through
case SpvOpTypeFunction: // fall through
case SpvOpTypeRuntimeArray: // fall through
case SpvOpTypeStruct: // fall through
case SpvOpTypeImage: // fall through
case SpvOpTypeSampledImage: // fall through
case SpvOpTypeSampler: // fall through
case SpvOpVariable: // fall through
case SpvOpFunction: // fall through
case SpvOpFunctionParameter: // fall through
case SpvOpFunctionEnd: // fall through
case SpvOpExecutionMode: // fall through
case SpvOpMemoryModel: // fall through
case SpvOpCapability: // fall through
case SpvOpExtInstImport: // fall through
case SpvOpEntryPoint: // fall through
case SpvOpSource: // fall through
case SpvOpSourceExtension: // fall through
case SpvOpName: // fall through
case SpvOpMemberName: // fall through
case SpvOpDecorate: // fall through
case SpvOpMemberDecorate:
break;
default:
// We may find ourselves with dead code--instructions that don't have an associated
// block. This should be a rare event, but if it happens, synthesize a label; this is
// necessary to satisfy the validator.
if (fCurrentBlock == 0) {
this->writeLabel(this->nextId(nullptr), out);
}
break;
}
this->writeWord((length << 16) | opCode, out);
}
void SPIRVCodeGenerator::writeLabel(SpvId label, OutputStream& out) {
SkASSERT(!fCurrentBlock);
fCurrentBlock = label;
this->writeInstruction(SpvOpLabel, label, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, OutputStream& out) {
this->writeOpCode(opCode, 1, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, OutputStream& out) {
this->writeOpCode(opCode, 2, out);
this->writeWord(word1, out);
}
void SPIRVCodeGenerator::writeString(const char* string, size_t length, OutputStream& out) {
out.write(string, length);
switch (length % 4) {
case 1:
out.write8(0);
[[fallthrough]];
case 2:
out.write8(0);
[[fallthrough]];
case 3:
out.write8(0);
break;
default:
this->writeWord(0, out);
}
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, StringFragment string, OutputStream& out) {
this->writeOpCode(opCode, 1 + (string.fLength + 4) / 4, out);
this->writeString(string.fChars, string.fLength, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, StringFragment string,
OutputStream& out) {
this->writeOpCode(opCode, 2 + (string.fLength + 4) / 4, out);
this->writeWord(word1, out);
this->writeString(string.fChars, string.fLength, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
StringFragment string, OutputStream& out) {
this->writeOpCode(opCode, 3 + (string.fLength + 4) / 4, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeString(string.fChars, string.fLength, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
OutputStream& out) {
this->writeOpCode(opCode, 3, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, OutputStream& out) {
this->writeOpCode(opCode, 4, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, int32_t word4, OutputStream& out) {
this->writeOpCode(opCode, 5, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
this->writeWord(word4, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, int32_t word4, int32_t word5,
OutputStream& out) {
this->writeOpCode(opCode, 6, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
this->writeWord(word4, out);
this->writeWord(word5, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, int32_t word4, int32_t word5,
int32_t word6, OutputStream& out) {
this->writeOpCode(opCode, 7, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
this->writeWord(word4, out);
this->writeWord(word5, out);
this->writeWord(word6, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, int32_t word4, int32_t word5,
int32_t word6, int32_t word7, OutputStream& out) {
this->writeOpCode(opCode, 8, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
this->writeWord(word4, out);
this->writeWord(word5, out);
this->writeWord(word6, out);
this->writeWord(word7, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
int32_t word3, int32_t word4, int32_t word5,
int32_t word6, int32_t word7, int32_t word8,
OutputStream& out) {
this->writeOpCode(opCode, 9, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeWord(word3, out);
this->writeWord(word4, out);
this->writeWord(word5, out);
this->writeWord(word6, out);
this->writeWord(word7, out);
this->writeWord(word8, out);
}
void SPIRVCodeGenerator::writeCapabilities(OutputStream& out) {
for (uint64_t i = 0, bit = 1; i <= kLast_Capability; i++, bit <<= 1) {
if (fCapabilities & bit) {
this->writeInstruction(SpvOpCapability, (SpvId) i, out);
}
}
if (fProgram.fConfig->fKind == ProgramKind::kGeometry) {
this->writeInstruction(SpvOpCapability, SpvCapabilityGeometry, out);
}
else {
this->writeInstruction(SpvOpCapability, SpvCapabilityShader, out);
}
}
SpvId SPIRVCodeGenerator::nextId(const Type* type) {
return this->nextId(type && type->hasPrecision() && !type->highPrecision()
? Precision::kRelaxed
: Precision::kDefault);
}
SpvId SPIRVCodeGenerator::nextId(Precision precision) {
if (precision == Precision::kRelaxed && !fProgram.fConfig->fSettings.fForceHighPrecision) {
this->writeInstruction(SpvOpDecorate, fIdCount, SpvDecorationRelaxedPrecision,
fDecorationBuffer);
}
return fIdCount++;
}
void SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout,
SpvId resultId) {
this->writeInstruction(SpvOpName, resultId, String(type.name()).c_str(), fNameBuffer);
// go ahead and write all of the field types, so we don't inadvertently write them while we're
// in the middle of writing the struct instruction
std::vector<SpvId> types;
for (const auto& f : type.fields()) {
types.push_back(this->getType(*f.fType, memoryLayout));
}
this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuffer);
this->writeWord(resultId, fConstantBuffer);
for (SpvId id : types) {
this->writeWord(id, fConstantBuffer);
}
size_t offset = 0;
for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) {
const Type::Field& field = type.fields()[i];
if (!MemoryLayout::LayoutIsSupported(*field.fType)) {
fErrors.error(type.fOffset, "type '" + field.fType->name() + "' is not permitted here");
return;
}
size_t size = memoryLayout.size(*field.fType);
size_t alignment = memoryLayout.alignment(*field.fType);
const Layout& fieldLayout = field.fModifiers.fLayout;
if (fieldLayout.fOffset >= 0) {
if (fieldLayout.fOffset < (int) offset) {
fErrors.error(type.fOffset,
"offset of field '" + field.fName + "' must be at "
"least " + to_string((int) offset));
}
if (fieldLayout.fOffset % alignment) {
fErrors.error(type.fOffset,
"offset of field '" + field.fName + "' must be a multiple"
" of " + to_string((int) alignment));
}
offset = fieldLayout.fOffset;
} else {
size_t mod = offset % alignment;
if (mod) {
offset += alignment - mod;
}
}
this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer);
this->writeLayout(fieldLayout, resultId, i);
if (field.fModifiers.fLayout.fBuiltin < 0) {
this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, SpvDecorationOffset,
(SpvId) offset, fDecorationBuffer);
}
if (field.fType->isMatrix()) {
this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationColMajor,
fDecorationBuffer);
this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorationMatrixStride,
(SpvId) memoryLayout.stride(*field.fType),
fDecorationBuffer);
}
if (!field.fType->highPrecision()) {
this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i,
SpvDecorationRelaxedPrecision, fDecorationBuffer);
}
offset += size;
if ((field.fType->isArray() || field.fType->isStruct()) && offset % alignment != 0) {
offset += alignment - offset % alignment;
}
}
}
const Type& SPIRVCodeGenerator::getActualType(const Type& type) {
if (type.isFloat()) {
return *fContext.fTypes.fFloat;
}
if (type.isSigned() || type.isEnum()) {
return *fContext.fTypes.fInt;
}
if (type.isUnsigned()) {
return *fContext.fTypes.fUInt;
}
if (type.isMatrix() || type.isVector()) {
if (type.componentType() == *fContext.fTypes.fHalf) {
return fContext.fTypes.fFloat->toCompound(fContext, type.columns(), type.rows());
}
if (type.componentType() == *fContext.fTypes.fShort ||
type.componentType() == *fContext.fTypes.fByte) {
return fContext.fTypes.fInt->toCompound(fContext, type.columns(), type.rows());
}
if (type.componentType() == *fContext.fTypes.fUShort ||
type.componentType() == *fContext.fTypes.fUByte) {
return fContext.fTypes.fUInt->toCompound(fContext, type.columns(), type.rows());
}
}
return type;
}
SpvId SPIRVCodeGenerator::getType(const Type& type) {
return this->getType(type, fDefaultLayout);
}
SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) {
const Type& type = this->getActualType(rawType);
String key = type.name();
if (type.isStruct() || type.isArray()) {
key += to_string((int)layout.fStd);
#ifdef SK_DEBUG
SkASSERT(layout.fStd == MemoryLayout::Standard::k140_Standard ||
layout.fStd == MemoryLayout::Standard::k430_Standard);
MemoryLayout::Standard otherStd = layout.fStd == MemoryLayout::Standard::k140_Standard
? MemoryLayout::Standard::k430_Standard
: MemoryLayout::Standard::k140_Standard;
String otherKey = type.name() + to_string((int)otherStd);
SkASSERT(fTypeMap.find(otherKey) == fTypeMap.end());
#endif
}
auto entry = fTypeMap.find(key);
if (entry == fTypeMap.end()) {
SpvId result = this->nextId(nullptr);
switch (type.typeKind()) {
case Type::TypeKind::kScalar:
if (type.isBoolean()) {
this->writeInstruction(SpvOpTypeBool, result, fConstantBuffer);
} else if (type == *fContext.fTypes.fInt || type == *fContext.fTypes.fShort ||
type == *fContext.fTypes.fIntLiteral) {
this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer);
} else if (type == *fContext.fTypes.fUInt || type == *fContext.fTypes.fUShort) {
this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstantBuffer);
} else if (type == *fContext.fTypes.fFloat || type == *fContext.fTypes.fHalf ||
type == *fContext.fTypes.fFloatLiteral) {
this->writeInstruction(SpvOpTypeFloat, result, 32, fConstantBuffer);
} else {
SkASSERT(false);
}
break;
case Type::TypeKind::kEnum:
this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstantBuffer);
break;
case Type::TypeKind::kVector:
this->writeInstruction(SpvOpTypeVector, result,
this->getType(type.componentType(), layout),
type.columns(), fConstantBuffer);
break;
case Type::TypeKind::kMatrix:
this->writeInstruction(
SpvOpTypeMatrix,
result,
this->getType(IndexExpression::IndexType(fContext, type), layout),
type.columns(),
fConstantBuffer);
break;
case Type::TypeKind::kStruct:
this->writeStruct(type, layout, result);
break;
case Type::TypeKind::kArray: {
if (!MemoryLayout::LayoutIsSupported(type)) {
fErrors.error(type.fOffset, "type '" + type.name() + "' is not permitted here");
return this->nextId(nullptr);
}
if (type.columns() > 0) {
SpvId typeId = this->getType(type.componentType(), layout);
IntLiteral countLiteral(/*offset=*/-1, type.columns(),
fContext.fTypes.fInt.get());
SpvId countId = this->writeIntLiteral(countLiteral);
this->writeInstruction(SpvOpTypeArray, result, typeId, countId,
fConstantBuffer);
this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
(int32_t) layout.stride(type),
fDecorationBuffer);
} else {
// We shouldn't have any runtime-sized arrays right now
fErrors.error(type.fOffset, "runtime-sized arrays are not supported in SPIR-V");
this->writeInstruction(SpvOpTypeRuntimeArray, result,
this->getType(type.componentType(), layout),
fConstantBuffer);
this->writeInstruction(SpvOpDecorate, result, SpvDecorationArrayStride,
(int32_t) layout.stride(type),
fDecorationBuffer);
}
break;
}
case Type::TypeKind::kSampler: {
SpvId image = result;
if (SpvDimSubpassData != type.dimensions()) {
image = this->getType(type.textureType(), layout);
}
if (SpvDimBuffer == type.dimensions()) {
fCapabilities |= (((uint64_t) 1) << SpvCapabilitySampledBuffer);
}
if (SpvDimSubpassData != type.dimensions()) {
this->writeInstruction(SpvOpTypeSampledImage, result, image, fConstantBuffer);
}
break;
}
case Type::TypeKind::kSeparateSampler: {
this->writeInstruction(SpvOpTypeSampler, result, fConstantBuffer);
break;
}
case Type::TypeKind::kTexture: {
this->writeInstruction(SpvOpTypeImage, result,
this->getType(*fContext.fTypes.fFloat, layout),
type.dimensions(), type.isDepth(), type.isArrayedTexture(),
type.isMultisampled(), type.isSampled() ? 1 : 2,
SpvImageFormatUnknown, fConstantBuffer);
fImageTypeMap[key] = result;
break;
}
default:
if (type.isVoid()) {
this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffer);
} else {
SkDEBUGFAILF("invalid type: %s", type.description().c_str());
}
}
fTypeMap[key] = result;
return result;
}
return entry->second;
}
SpvId SPIRVCodeGenerator::getImageType(const Type& type) {
SkASSERT(type.typeKind() == Type::TypeKind::kSampler);
this->getType(type);
String key = type.name() + to_string((int) fDefaultLayout.fStd);
SkASSERT(fImageTypeMap.find(key) != fImageTypeMap.end());
return fImageTypeMap[key];
}
SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
String key = to_string(this->getType(function.returnType())) + "(";
String separator;
const std::vector<const Variable*>& parameters = function.parameters();
for (size_t i = 0; i < parameters.size(); i++) {
key += separator;
separator = ", ";
key += to_string(this->getType(parameters[i]->type()));
}
key += ")";
auto entry = fTypeMap.find(key);
if (entry == fTypeMap.end()) {
SpvId result = this->nextId(nullptr);
int32_t length = 3 + (int32_t) parameters.size();
SpvId returnType = this->getType(function.returnType());
std::vector<SpvId> parameterTypes;
for (size_t i = 0; i < parameters.size(); i++) {
// glslang seems to treat all function arguments as pointers whether they need to be or
// not. I was initially puzzled by this until I ran bizarre failures with certain
// patterns of function calls and control constructs, as exemplified by this minimal
// failure case:
//
// void sphere(float x) {
// }
//
// void map() {
// sphere(1.0);
// }
//
// void main() {
// for (int i = 0; i < 1; i++) {
// map();
// }
// }
//
// As of this writing, compiling this in the "obvious" way (with sphere taking a float)
// crashes. Making it take a float* and storing the argument in a temporary variable,
// as glslang does, fixes it. It's entirely possible I simply missed whichever part of
// the spec makes this make sense.
// if (is_out(function->fParameters[i])) {
parameterTypes.push_back(this->getPointerType(parameters[i]->type(),
SpvStorageClassFunction));
// } else {
// parameterTypes.push_back(this->getType(function.fParameters[i]->fType));
// }
}
this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer);
this->writeWord(result, fConstantBuffer);
this->writeWord(returnType, fConstantBuffer);
for (SpvId id : parameterTypes) {
this->writeWord(id, fConstantBuffer);
}
fTypeMap[key] = result;
return result;
}
return entry->second;
}
SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
return this->getPointerType(type, fDefaultLayout, storageClass);
}
SpvId SPIRVCodeGenerator::getPointerType(const Type& rawType, const MemoryLayout& layout,
SpvStorageClass_ storageClass) {
const Type& type = this->getActualType(rawType);
String key = type.displayName() + "*" + to_string(layout.fStd) + to_string(storageClass);
auto entry = fTypeMap.find(key);
if (entry == fTypeMap.end()) {
SpvId result = this->nextId(nullptr);
this->writeInstruction(SpvOpTypePointer, result, storageClass,
this->getType(type), fConstantBuffer);
fTypeMap[key] = result;
return result;
}
return entry->second;
}
SpvId SPIRVCodeGenerator::writeExpression(const Expression& expr, OutputStream& out) {
switch (expr.kind()) {
case Expression::Kind::kBinary:
return this->writeBinaryExpression(expr.as<BinaryExpression>(), out);
case Expression::Kind::kBoolLiteral:
return this->writeBoolLiteral(expr.as<BoolLiteral>());
case Expression::Kind::kConstructorArray:
case Expression::Kind::kConstructorStruct:
return this->writeCompositeConstructor(expr.asAnyConstructor(), out);
case Expression::Kind::kConstructorDiagonalMatrix:
return this->writeConstructorDiagonalMatrix(expr.as<ConstructorDiagonalMatrix>(), out);
case Expression::Kind::kConstructorMatrixResize:
return this->writeConstructorMatrixResize(expr.as<ConstructorMatrixResize>(), out);
case Expression::Kind::kConstructorScalarCast:
return this->writeConstructorScalarCast(expr.as<ConstructorScalarCast>(), out);
case Expression::Kind::kConstructorSplat:
return this->writeConstructorSplat(expr.as<ConstructorSplat>(), out);
case Expression::Kind::kConstructorCompound:
return this->writeConstructorCompound(expr.as<ConstructorCompound>(), out);
case Expression::Kind::kConstructorCompoundCast:
return this->writeConstructorCompoundCast(expr.as<ConstructorCompoundCast>(), out);
case Expression::Kind::kIntLiteral:
return this->writeIntLiteral(expr.as<IntLiteral>());
case Expression::Kind::kFieldAccess:
return this->writeFieldAccess(expr.as<FieldAccess>(), out);
case Expression::Kind::kFloatLiteral:
return this->writeFloatLiteral(expr.as<FloatLiteral>());
case Expression::Kind::kFunctionCall:
return this->writeFunctionCall(expr.as<FunctionCall>(), out);
case Expression::Kind::kPrefix:
return this->writePrefixExpression(expr.as<PrefixExpression>(), out);
case Expression::Kind::kPostfix:
return this->writePostfixExpression(expr.as<PostfixExpression>(), out);
case Expression::Kind::kSwizzle:
return this->writeSwizzle(expr.as<Swizzle>(), out);
case Expression::Kind::kVariableReference:
return this->writeVariableReference(expr.as<VariableReference>(), out);
case Expression::Kind::kTernary:
return this->writeTernaryExpression(expr.as<TernaryExpression>(), out);
case Expression::Kind::kIndex:
return this->writeIndexExpression(expr.as<IndexExpression>(), out);
default:
SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str());
break;
}
return -1;
}
SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) {
const FunctionDeclaration& function = c.function();
auto intrinsic = fIntrinsicMap.find(function.name());
if (intrinsic == fIntrinsicMap.end()) {
fErrors.error(c.fOffset, "unsupported intrinsic '" + function.description() + "'");
return -1;
}
int32_t intrinsicId;
const ExpressionArray& arguments = c.arguments();
if (arguments.size() > 0) {
const Type& type = arguments[0]->type();
if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fContext, type)) {
intrinsicId = std::get<1>(intrinsic->second);
} else if (is_signed(fContext, type)) {
intrinsicId = std::get<2>(intrinsic->second);
} else if (is_unsigned(fContext, type)) {
intrinsicId = std::get<3>(intrinsic->second);
} else if (is_bool(fContext, type)) {
intrinsicId = std::get<4>(intrinsic->second);
} else {
intrinsicId = std::get<1>(intrinsic->second);
}
} else {
intrinsicId = std::get<1>(intrinsic->second);
}
switch (std::get<0>(intrinsic->second)) {
case kGLSL_STD_450_IntrinsicKind: {
SpvId result = this->nextId(&c.type());
std::vector<SpvId> argumentIds;
for (size_t i = 0; i < arguments.size(); i++) {
if (function.parameters()[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
// TODO(skia:11052): swizzled lvalues won't work with getPointer()
argumentIds.push_back(this->getLValue(*arguments[i], out)->getPointer());
} else {
argumentIds.push_back(this->writeExpression(*arguments[i], out));
}
}
this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out);
this->writeWord(this->getType(c.type()), out);
this->writeWord(result, out);
this->writeWord(fGLSLExtendedInstructions, out);
this->writeWord(intrinsicId, out);
for (SpvId id : argumentIds) {
this->writeWord(id, out);
}
return result;
}
case kSPIRV_IntrinsicKind: {
// GLSL supports dot(float, float), but SPIR-V does not. Convert it to FMul
if (intrinsicId == SpvOpDot && arguments[0]->type().isScalar()) {
intrinsicId = SpvOpFMul;
}
SpvId result = this->nextId(&c.type());
std::vector<SpvId> argumentIds;
for (size_t i = 0; i < arguments.size(); i++) {
if (function.parameters()[i]->modifiers().fFlags & Modifiers::kOut_Flag) {
// TODO(skia:11052): swizzled lvalues won't work with getPointer()
argumentIds.push_back(this->getLValue(*arguments[i], out)->getPointer());
} else {
argumentIds.push_back(this->writeExpression(*arguments[i], out));
}
}
if (!c.type().isVoid()) {
this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size(), out);
this->writeWord(this->getType(c.type()), out);
this->writeWord(result, out);
} else {
this->writeOpCode((SpvOp_) intrinsicId, 1 + (int32_t) arguments.size(), out);
}
for (SpvId id : argumentIds) {
this->writeWord(id, out);
}
return result;
}
case kSpecial_IntrinsicKind:
return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out);
default:
fErrors.error(c.fOffset, "unsupported intrinsic '" + function.description() + "'");
return -1;
}
}
std::vector<SpvId> SPIRVCodeGenerator::vectorize(const ExpressionArray& args, OutputStream& out) {
int vectorSize = 0;
for (const auto& a : args) {
if (a->type().isVector()) {
if (vectorSize) {
SkASSERT(a->type().columns() == vectorSize);
}
else {
vectorSize = a->type().columns();
}
}
}
std::vector<SpvId> result;
result.reserve(args.size());
for (const auto& arg : args) {
const Type& argType = arg->type();
SpvId raw = this->writeExpression(*arg, out);
if (vectorSize && argType.isScalar()) {
SpvId vector = this->nextId(&arg->type());
this->writeOpCode(SpvOpCompositeConstruct, 3 + vectorSize, out);
this->writeWord(this->getType(argType.toCompound(fContext, vectorSize, 1)), out);
this->writeWord(vector, out);
for (int i = 0; i < vectorSize; i++) {
this->writeWord(raw, out);
}
result.push_back(vector);
} else {
result.push_back(raw);
}
}
return result;
}
void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
SpvId signedInst, SpvId unsignedInst,
const std::vector<SpvId>& args,
OutputStream& out) {
this->writeOpCode(SpvOpExtInst, 5 + args.size(), out);
this->writeWord(this->getType(type), out);
this->writeWord(id, out);
this->writeWord(fGLSLExtendedInstructions, out);
if (is_float(fContext, type)) {
this->writeWord(floatInst, out);
} else if (is_signed(fContext, type)) {
this->writeWord(signedInst, out);
} else if (is_unsigned(fContext, type)) {
this->writeWord(unsignedInst, out);
} else {
SkASSERT(false);
}
for (SpvId a : args) {
this->writeWord(a, out);
}
}
SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(const FunctionCall& c, SpecialIntrinsic kind,
OutputStream& out) {
const ExpressionArray& arguments = c.arguments();
const Type& callType = c.type();
SpvId result = this->nextId(nullptr);
switch (kind) {
case kAtan_SpecialIntrinsic: {
std::vector<SpvId> argumentIds;
for (const std::unique_ptr<Expression>& arg : arguments) {
argumentIds.push_back(this->writeExpression(*arg, out));
}
this->writeOpCode(SpvOpExtInst, 5 + (int32_t) argumentIds.size(), out);
this->writeWord(this->getType(callType), out);
this->writeWord(result, out);
this->writeWord(fGLSLExtendedInstructions, out);
this->writeWord(argumentIds.size() == 2 ? GLSLstd450Atan2 : GLSLstd450Atan, out);
for (SpvId id : argumentIds) {
this->writeWord(id, out);
}
break;
}
case kSampledImage_SpecialIntrinsic: {
SkASSERT(arguments.size() == 2);
SpvId img = this->writeExpression(*arguments[0], out);
SpvId sampler = this->writeExpression(*arguments[1], out);
this->writeInstruction(SpvOpSampledImage,
this->getType(callType),
result,
img,
sampler,
out);
break;
}
case kSubpassLoad_SpecialIntrinsic: {
SpvId img = this->writeExpression(*arguments[0], out);
ExpressionArray args;
args.reserve_back(2);
args.push_back(IntLiteral::Make(fContext, /*offset=*/-1, /*value=*/0));
args.push_back(IntLiteral::Make(fContext, /*offset=*/-1, /*value=*/0));
ConstructorCompound ctor(/*offset=*/-1, *fContext.fTypes.fInt2, std::move(args));
SpvId coords = this->writeConstantVector(ctor);
if (arguments.size() == 1) {
this->writeInstruction(SpvOpImageRead,
this->getType(callType),
result,
img,
coords,
out);
} else {
SkASSERT(arguments.size() == 2);
SpvId sample = this->writeExpression(*arguments[1], out);
this->writeInstruction(SpvOpImageRead,
this->getType(callType),
result,
img,
coords,
SpvImageOperandsSampleMask,
sample,
out);
}
break;
}
case kTexture_SpecialIntrinsic: {
SpvOp_ op = SpvOpImageSampleImplicitLod;
const Type& arg1Type = arguments[1]->type();
switch (arguments[0]->type().dimensions()) {
case SpvDim1D:
if (arg1Type == *fContext.fTypes.fFloat2) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type == *fContext.fTypes.fFloat);
}
break;
case SpvDim2D:
if (arg1Type == *fContext.fTypes.fFloat3) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type == *fContext.fTypes.fFloat2);
}
break;
case SpvDim3D:
if (arg1Type == *fContext.fTypes.fFloat4) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type == *fContext.fTypes.fFloat3);
}
break;
case SpvDimCube: // fall through
case SpvDimRect: // fall through
case SpvDimBuffer: // fall through
case SpvDimSubpassData:
break;
}
SpvId type = this->getType(callType);
SpvId sampler = this->writeExpression(*arguments[0], out);
SpvId uv = this->writeExpression(*arguments[1], out);
if (arguments.size() == 3) {
this->writeInstruction(op, type, result, sampler, uv,
SpvImageOperandsBiasMask,
this->writeExpression(*arguments[2], out),
out);
} else {
SkASSERT(arguments.size() == 2);
if (fProgram.fConfig->fSettings.fSharpenTextures) {
FloatLiteral lodBias(/*offset=*/-1, /*value=*/-0.5,
fContext.fTypes.fFloat.get());
this->writeInstruction(op, type, result, sampler, uv,
SpvImageOperandsBiasMask,
this->writeFloatLiteral(lodBias),
out);
} else {
this->writeInstruction(op, type, result, sampler, uv,
out);
}
}
break;
}
case kMod_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
const Type& operandType = arguments[0]->type();
SpvOp_ op;
if (is_float(fContext, operandType)) {
op = SpvOpFMod;
} else if (is_signed(fContext, operandType)) {
op = SpvOpSMod;
} else if (is_unsigned(fContext, operandType)) {
op = SpvOpUMod;
} else {
SkASSERT(false);
return 0;
}
this->writeOpCode(op, 5, out);
this->writeWord(this->getType(operandType), out);
this->writeWord(result, out);
this->writeWord(args[0], out);
this->writeWord(args[1], out);
break;
}
case kDFdy_SpecialIntrinsic: {
SpvId fn = this->writeExpression(*arguments[0], out);
this->writeOpCode(SpvOpDPdy, 4, out);
this->writeWord(this->getType(callType), out);
this->writeWord(result, out);
this->writeWord(fn, out);
if (fProgram.fConfig->fSettings.fFlipY) {
// Flipping Y also negates the Y derivatives.
SpvId flipped = this->nextId(&callType);
this->writeInstruction(SpvOpFNegate, this->getType(callType), flipped, result,
out);
result = flipped;
}
break;
}
case kClamp_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
GLSLstd450UClamp, args, out);
break;
}
case kMax_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMax, GLSLstd450SMax,
GLSLstd450UMax, args, out);
break;
}
case kMin_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMin, GLSLstd450SMin,
GLSLstd450UMin, args, out);
break;
}
case kMix_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMix, SpvOpUndef,
SpvOpUndef, args, out);
break;
}
case kSaturate_SpecialIntrinsic: {
SkASSERT(arguments.size() == 1);
ExpressionArray finalArgs;
finalArgs.reserve_back(3);
finalArgs.push_back(arguments[0]->clone());
finalArgs.push_back(FloatLiteral::Make(fContext, /*offset=*/-1, /*value=*/0));
finalArgs.push_back(FloatLiteral::Make(fContext, /*offset=*/-1, /*value=*/1));
std::vector<SpvId> spvArgs = this->vectorize(finalArgs, out);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
GLSLstd450UClamp, spvArgs, out);
break;
}
case kSmoothStep_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450SmoothStep, SpvOpUndef,
SpvOpUndef, args, out);
break;
}
case kStep_SpecialIntrinsic: {
std::vector<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450Step, SpvOpUndef,
SpvOpUndef, args, out);
break;
}
case kMatrixCompMult_SpecialIntrinsic: {
SkASSERT(arguments.size() == 2);
SpvId lhs = this->writeExpression(*arguments[0], out);
SpvId rhs = this->writeExpression(*arguments[1], out);
result = this->writeComponentwiseMatrixBinary(callType, lhs, rhs, SpvOpFMul, SpvOpUndef,
out);
break;
}
}
return result;
}
namespace {
struct TempVar {
SpvId spvId;
const Type* type;
std::unique_ptr<SPIRVCodeGenerator::LValue> lvalue;
};
}
SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
const FunctionDeclaration& function = c.function();
if (function.isBuiltin() && !function.definition()) {
return this->writeIntrinsicCall(c, out);
}
const ExpressionArray& arguments = c.arguments();
const auto& entry = fFunctionMap.find(&function);
if (entry == fFunctionMap.end()) {
fErrors.error(c.fOffset, "function '" + function.description() + "' is not defined");
return -1;
}
// Temp variables are used to write back out-parameters after the function call is complete.
std::vector<TempVar> tempVars;
std::vector<SpvId> argumentIds;
for (size_t i = 0; i < arguments.size(); i++) {
// id of temporary variable that we will use to hold this argument, or 0 if it is being
// passed directly
SpvId tmpVar;
// if we need a temporary var to store this argument, this is the value to store in the var
SpvId tmpValueId;
if (is_out(*function.parameters()[i])) {
std::unique_ptr<LValue> lv = this->getLValue(*arguments[i], out);
SpvId ptr = lv->getPointer();
if (ptr != (SpvId) -1 && lv->isMemoryObjectPointer()) {
argumentIds.push_back(ptr);
continue;
} else {
// lvalue cannot simply be read and written via a pointer (e.g. a swizzle). Need to
// copy it into a temp, call the function, read the value out of the temp, and then
// update the lvalue.
tmpValueId = lv->load(out);
tmpVar = this->nextId(nullptr);
tempVars.push_back(TempVar{tmpVar, &arguments[i]->type(), std::move(lv)});
}
} else {
// See getFunctionType for an explanation of why we're always using pointer parameters.
tmpValueId = this->writeExpression(*arguments[i], out);
tmpVar = this->nextId(nullptr);
}
this->writeInstruction(SpvOpVariable,
this->getPointerType(arguments[i]->type(), SpvStorageClassFunction),
tmpVar,
SpvStorageClassFunction,
fVariableBuffer);
this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out);
argumentIds.push_back(tmpVar);
}
SpvId result = this->nextId(nullptr);
this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) arguments.size(), out);
this->writeWord(this->getType(c.type()), out);
this->writeWord(result, out);
this->writeWord(entry->second, out);
for (SpvId id : argumentIds) {
this->writeWord(id, out);
}
// Now that the call is complete, we copy temp out-variables back to their real lvalues.
for (const TempVar& tempVar : tempVars) {
SpvId load = this->nextId(tempVar.type);
this->writeInstruction(SpvOpLoad, getType(*tempVar.type), load, tempVar.spvId, out);
tempVar.lvalue->store(load, out);
}
return result;
}
SpvId SPIRVCodeGenerator::writeConstantVector(const AnyConstructor& c) {
const Type& type = c.type();
SkASSERT(type.isVector() && c.isCompileTimeConstant());
// Get each of the constructor components as SPIR-V constants.
SPIRVVectorConstant key{this->getType(type),
/*fValueId=*/{SpvId(-1), SpvId(-1), SpvId(-1), SpvId(-1)}};
for (int n = 0; n < type.columns(); n++) {
const Expression* expr = c.getConstantSubexpression(n);
if (!expr) {
SkDEBUGFAILF("writeConstantVector: %s not actually constant", c.description().c_str());
return (SpvId)-1;
}
key.fValueId[n] = this->writeExpression(*expr, fConstantBuffer);
}
// Check to see if we've already synthesized this vector constant.
auto [iter, newlyCreated] = fVectorConstants.insert({key, (SpvId)-1});
if (newlyCreated) {
// Emit an OpConstantComposite instruction for this constant.
SpvId result = this->nextId(&type);
this->writeOpCode(SpvOpConstantComposite, 3 + type.columns(), fConstantBuffer);
this->writeWord(key.fTypeId, fConstantBuffer);
this->writeWord(result, fConstantBuffer);
for (int i = 0; i < type.columns(); i++) {
this->writeWord(key.fValueId[i], fConstantBuffer);
}
iter->second = result;
}
return iter->second;
}
SpvId SPIRVCodeGenerator::castScalarToType(SpvId inputExprId,
const Type& inputType,
const Type& outputType,
OutputStream& out) {
if (outputType.isFloat()) {
return this->castScalarToFloat(inputExprId, inputType, outputType, out);
}
if (outputType.isSigned()) {
return this->castScalarToSignedInt(inputExprId, inputType, outputType, out);
}
if (outputType.isUnsigned()) {
return this->castScalarToUnsignedInt(inputExprId, inputType, outputType, out);
}
if (outputType.isBoolean()) {
return this->castScalarToBoolean(inputExprId, inputType, outputType, out);
}
fErrors.error(-1, "unsupported cast: " + inputType.description() +
" to " + outputType.description());
return inputExprId;
}
SpvId SPIRVCodeGenerator::writeFloatConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.argumentSpan().size() == 1);
SkASSERT(c.type().isFloat());
const Expression& ctorExpr = *c.argumentSpan().front();
SpvId expressionId = this->writeExpression(ctorExpr, out);
return this->castScalarToFloat(expressionId, ctorExpr.type(), c.type(), out);
}
SpvId SPIRVCodeGenerator::castScalarToFloat(SpvId inputId, const Type& inputType,
const Type& outputType, OutputStream& out) {
// Casting a float to float is a no-op.
if (inputType.isFloat()) {
return inputId;
}
// Given the input type, generate the appropriate instruction to cast to float.
SpvId result = this->nextId(&outputType);
if (inputType.isBoolean()) {
// Use OpSelect to convert the boolean argument to a literal 1.0 or 0.0.
FloatLiteral one(/*offset=*/-1, /*value=*/1, fContext.fTypes.fFloat.get());
SpvId oneID = this->writeFloatLiteral(one);
FloatLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fFloat.get());
SpvId zeroID = this->writeFloatLiteral(zero);
this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
inputId, oneID, zeroID, out);
} else if (inputType.isSigned()) {
this->writeInstruction(SpvOpConvertSToF, this->getType(outputType), result, inputId, out);
} else if (inputType.isUnsigned()) {
this->writeInstruction(SpvOpConvertUToF, this->getType(outputType), result, inputId, out);
} else {
SkDEBUGFAILF("unsupported type for float typecast: %s", inputType.description().c_str());
return (SpvId)-1;
}
return result;
}
SpvId SPIRVCodeGenerator::writeIntConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.argumentSpan().size() == 1);
SkASSERT(c.type().isSigned());
const Expression& ctorExpr = *c.argumentSpan().front();
SpvId expressionId = this->writeExpression(ctorExpr, out);
return this->castScalarToSignedInt(expressionId, ctorExpr.type(), c.type(), out);
}
SpvId SPIRVCodeGenerator::castScalarToSignedInt(SpvId inputId, const Type& inputType,
const Type& outputType, OutputStream& out) {
// Casting a signed int to signed int is a no-op.
if (inputType.isSigned()) {
return inputId;
}
// Given the input type, generate the appropriate instruction to cast to signed int.
SpvId result = this->nextId(&outputType);
if (inputType.isBoolean()) {
// Use OpSelect to convert the boolean argument to a literal 1 or 0.
IntLiteral one(/*offset=*/-1, /*value=*/1, fContext.fTypes.fInt.get());
SpvId oneID = this->writeIntLiteral(one);
IntLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fInt.get());
SpvId zeroID = this->writeIntLiteral(zero);
this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
inputId, oneID, zeroID, out);
} else if (inputType.isFloat()) {
this->writeInstruction(SpvOpConvertFToS, this->getType(outputType), result, inputId, out);
} else if (inputType.isUnsigned()) {
this->writeInstruction(SpvOpBitcast, this->getType(outputType), result, inputId, out);
} else {
SkDEBUGFAILF("unsupported type for signed int typecast: %s",
inputType.description().c_str());
return (SpvId)-1;
}
return result;
}
SpvId SPIRVCodeGenerator::writeUIntConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.argumentSpan().size() == 1);
SkASSERT(c.type().isUnsigned());
const Expression& ctorExpr = *c.argumentSpan().front();
SpvId expressionId = this->writeExpression(ctorExpr, out);
return this->castScalarToUnsignedInt(expressionId, ctorExpr.type(), c.type(), out);
}
SpvId SPIRVCodeGenerator::castScalarToUnsignedInt(SpvId inputId, const Type& inputType,
const Type& outputType, OutputStream& out) {
// Casting an unsigned int to unsigned int is a no-op.
if (inputType.isUnsigned()) {
return inputId;
}
// Given the input type, generate the appropriate instruction to cast to unsigned int.
SpvId result = this->nextId(&outputType);
if (inputType.isBoolean()) {
// Use OpSelect to convert the boolean argument to a literal 1u or 0u.
IntLiteral one(/*offset=*/-1, /*value=*/1, fContext.fTypes.fUInt.get());
SpvId oneID = this->writeIntLiteral(one);
IntLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fUInt.get());
SpvId zeroID = this->writeIntLiteral(zero);
this->writeInstruction(SpvOpSelect, this->getType(outputType), result,
inputId, oneID, zeroID, out);
} else if (inputType.isFloat()) {
this->writeInstruction(SpvOpConvertFToU, this->getType(outputType), result, inputId, out);
} else if (inputType.isSigned()) {
this->writeInstruction(SpvOpBitcast, this->getType(outputType), result, inputId, out);
} else {
SkDEBUGFAILF("unsupported type for unsigned int typecast: %s",
inputType.description().c_str());
return (SpvId)-1;
}
return result;
}
SpvId SPIRVCodeGenerator::writeBooleanConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.argumentSpan().size() == 1);
SkASSERT(c.type().isBoolean());
const Expression& ctorExpr = *c.argumentSpan().front();
SpvId expressionId = this->writeExpression(ctorExpr, out);
return this->castScalarToBoolean(expressionId, ctorExpr.type(), c.type(), out);
}
SpvId SPIRVCodeGenerator::castScalarToBoolean(SpvId inputId, const Type& inputType,
const Type& outputType, OutputStream& out) {
// Casting a bool to bool is a no-op.
if (inputType.isBoolean()) {
return inputId;
}
// Given the input type, generate the appropriate instruction to cast to bool.
SpvId result = this->nextId(nullptr);
if (inputType.isSigned()) {
// Synthesize a boolean result by comparing the input against a signed zero literal.
IntLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fInt.get());
SpvId zeroID = this->writeIntLiteral(zero);
this->writeInstruction(SpvOpINotEqual, this->getType(outputType), result,
inputId, zeroID, out);
} else if (inputType.isUnsigned()) {
// Synthesize a boolean result by comparing the input against an unsigned zero literal.
IntLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fUInt.get());
SpvId zeroID = this->writeIntLiteral(zero);
this->writeInstruction(SpvOpINotEqual, this->getType(outputType), result,
inputId, zeroID, out);
} else if (inputType.isFloat()) {
// Synthesize a boolean result by comparing the input against a floating-point zero literal.
FloatLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fFloat.get());
SpvId zeroID = this->writeFloatLiteral(zero);
this->writeInstruction(SpvOpFUnordNotEqual, this->getType(outputType), result,
inputId, zeroID, out);
} else {
SkDEBUGFAILF("unsupported type for boolean typecast: %s", inputType.description().c_str());
return (SpvId)-1;
}
return result;
}
void SPIRVCodeGenerator::writeUniformScaleMatrix(SpvId id, SpvId diagonal, const Type& type,
OutputStream& out) {
FloatLiteral zero(/*offset=*/-1, /*value=*/0, fContext.fTypes.fFloat.get());
SpvId zeroId = this->writeFloatLiteral(zero);
std::vector<SpvId> columnIds;
columnIds.reserve(type.columns());
for (int column = 0; column < type.columns(); column++) {
this->writeOpCode(SpvOpCompositeConstruct, 3 + type.rows(),
out);
this->writeWord(this->getType(type.componentType().toCompound(
fContext, /*columns=*/type.rows(), /*rows=*/1)),
out);
SpvId columnId = this->nextId(&type);
this->writeWord(columnId, out);
columnIds.push_back(columnId);
for (int row = 0; row < type.rows(); row++) {
this->writeWord(row == column ? diagonal : zeroId, out);
}
}
this->writeOpCode(SpvOpCompositeConstruct, 3 + type.columns(),
out);
this->writeWord(this->getType(type), out);
this->writeWord(id, out);
for (SpvId columnId : columnIds) {
this->writeWord(columnId, out);
}
}
SpvId SPIRVCodeGenerator::writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType,
OutputStream& out) {
SkASSERT(srcType.isMatrix());
SkASSERT(dstType.isMatrix());
SkASSERT(srcType.componentType() == dstType.componentType());
SpvId id = this->nextId(&dstType);
SpvId srcColumnType = this->getType(srcType.componentType().toCompound(fContext,
srcType.rows(),
1));
SpvId dstColumnType = this->getType(dstType.componentType().toCompound(fContext,
dstType.rows(),
1));
SkASSERT(dstType.componentType().isFloat());
FloatLiteral zero(/*offset=*/-1, /*value=*/0.0, &dstType.componentType());
const SpvId zeroId = this->writeFloatLiteral(zero);
FloatLiteral one(/*offset=*/-1, /*value=*/1.0, &dstType.componentType());
const SpvId oneId = this->writeFloatLiteral(one);
SpvId columns[4];
for (int i = 0; i < dstType.columns(); i++) {
if (i < srcType.columns()) {
// we're still inside the src matrix, copy the column
SpvId srcColumn = this->nextId(&dstType);
this->writeInstruction(SpvOpCompositeExtract, srcColumnType, srcColumn, src, i, out);
SpvId dstColumn;
if (srcType.rows() == dstType.rows()) {
// columns are equal size, don't need to do anything
dstColumn = srcColumn;
}
else if (dstType.rows() > srcType.rows()) {
// dst column is bigger, need to zero-pad it
dstColumn = this->nextId(&dstType);
int delta = dstType.rows() - srcType.rows();
this->writeOpCode(SpvOpCompositeConstruct, 4 + delta, out);
this->writeWord(dstColumnType, out);
this->writeWord(dstColumn, out);
this->writeWord(srcColumn, out);
for (int j = srcType.rows(); j < dstType.rows(); ++j) {
this->writeWord((i == j) ? oneId : zeroId, out);
}
}
else {
// dst column is smaller, need to swizzle the src column
dstColumn = this->nextId(&dstType);
this->writeOpCode(SpvOpVectorShuffle, 5 + dstType.rows(), out);
this->writeWord(dstColumnType, out);
this->writeWord(dstColumn, out);
this->writeWord(srcColumn, out);
this->writeWord(srcColumn, out);
for (int j = 0; j < dstType.rows(); j++) {
this->writeWord(j, out);
}
}
columns[i] = dstColumn;
} else {
// we're past the end of the src matrix, need to synthesize an identity-matrix column
SpvId identityColumn = this->nextId(&dstType);
this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.rows(), out);
this->writeWord(dstColumnType, out);
this->writeWord(identityColumn, out);
for (int j = 0; j < dstType.rows(); ++j) {
this->writeWord((i == j) ? oneId : zeroId, out);
}
columns[i] = identityColumn;
}
}
this->writeOpCode(SpvOpCompositeConstruct, 3 + dstType.columns(), out);
this->writeWord(this->getType(dstType), out);
this->writeWord(id, out);
for (int i = 0; i < dstType.columns(); i++) {
this->writeWord(columns[i], out);
}
return id;
}
void SPIRVCodeGenerator::addColumnEntry(SpvId columnType, Precision precision,
std::vector<SpvId>* currentColumn,
std::vector<SpvId>* columnIds,
int* currentCount, int rows, SpvId entry,
OutputStream& out) {
SkASSERT(*currentCount < rows);
++(*currentCount);
currentColumn->push_back(entry);
if (*currentCount == rows) {
*currentCount = 0;
this->writeOpCode(SpvOpCompositeConstruct, 3 + currentColumn->size(), out);
this->writeWord(columnType, out);
SpvId columnId = this->nextId(precision);
this->writeWord(columnId, out);
columnIds->push_back(columnId);
for (SpvId id : *currentColumn) {
this->writeWord(id, out);
}
currentColumn->clear();
}
}
SpvId SPIRVCodeGenerator::writeMatrixConstructor(const ConstructorCompound& c, OutputStream& out) {
const Type& type = c.type();
SkASSERT(type.isMatrix());
SkASSERT(!c.arguments().empty());
const Type& arg0Type = c.arguments()[0]->type();
// go ahead and write the arguments so we don't try to write new instructions in the middle of
// an instruction
std::vector<SpvId> arguments;
arguments.reserve(c.arguments().size());
for (const std::unique_ptr<Expression>& arg : c.arguments()) {
arguments.push_back(this->writeExpression(*arg, out));
}
SpvId result = this->nextId(&type);
int rows = type.rows();
int columns = type.columns();
if (arguments.size() == 1 && arg0Type.isVector()) {
// Special-case handling of float4 -> mat2x2.
SkASSERT(type.rows() == 2 && type.columns() == 2);
SkASSERT(arg0Type.columns() == 4);
SpvId componentType = this->getType(type.componentType());
SpvId v[4];
for (int i = 0; i < 4; ++i) {
v[i] = this->nextId(&type);
this->writeInstruction(SpvOpCompositeExtract, componentType, v[i], arguments[0], i,
out);
}
SpvId columnType = this->getType(type.componentType().toCompound(fContext, 2, 1));
SpvId column1 = this->nextId(&type);
this->writeInstruction(SpvOpCompositeConstruct, columnType, column1, v[0], v[1], out);
SpvId column2 = this->nextId(&type);
this->writeInstruction(SpvOpCompositeConstruct, columnType, column2, v[2], v[3], out);
this->writeInstruction(SpvOpCompositeConstruct, this->getType(type), result, column1,
column2, out);
} else {
SpvId columnType = this->getType(type.componentType().toCompound(fContext, rows, 1));
std::vector<SpvId> columnIds;
// ids of vectors and scalars we have written to the current column so far
std::vector<SpvId> currentColumn;
// the total number of scalars represented by currentColumn's entries
int currentCount = 0;
Precision precision = type.highPrecision() ? Precision::kDefault : Precision::kRelaxed;
for (size_t i = 0; i < arguments.size(); i++) {
const Type& argType = c.arguments()[i]->type();
if (currentCount == 0 && argType.isVector() &&
argType.columns() == type.rows()) {
// this is a complete column by itself
columnIds.push_back(arguments[i]);
} else {
if (argType.columns() == 1) {
this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
&currentCount, rows, arguments[i], out);
} else {
SpvId componentType = this->getType(argType.componentType());
for (int j = 0; j < argType.columns(); ++j) {
SpvId swizzle = this->nextId(&argType);
this->writeInstruction(SpvOpCompositeExtract, componentType, swizzle,
arguments[i], j, out);
this->addColumnEntry(columnType, precision, &currentColumn, &columnIds,
&currentCount, rows, swizzle, out);
}
}
}
}
SkASSERT(columnIds.size() == (size_t) columns);
this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out);
this->writeWord(this->getType(type), out);
this->writeWord(result, out);
for (SpvId id : columnIds) {
this->writeWord(id, out);
}
}
return result;
}
SpvId SPIRVCodeGenerator::writeConstructorCompound(const ConstructorCompound& c,
OutputStream& out) {
return c.type().isMatrix() ? this->writeMatrixConstructor(c, out)
: this->writeVectorConstructor(c, out);
}
SpvId SPIRVCodeGenerator::writeVectorConstructor(const ConstructorCompound& c, OutputStream& out) {
const Type& type = c.type();
const Type& componentType = type.componentType();
SkASSERT(type.isVector());
if (c.isCompileTimeConstant()) {
return this->writeConstantVector(c);
}
std::vector<SpvId> arguments;
for (size_t i = 0; i < c.arguments().size(); i++) {
const Type& argType = c.arguments()[i]->type();
SkASSERT(componentType == argType.componentType());
if (argType.isVector()) {
// There's a bug in the Intel Vulkan driver where OpCompositeConstruct doesn't handle
// vector arguments at all, so we always extract each vector component and pass them
// into OpCompositeConstruct individually.
SpvId vec = this->writeExpression(*c.arguments()[i], out);
for (int j = 0; j < argType.columns(); j++) {
SpvId componentId = this->nextId(&componentType);
this->writeInstruction(SpvOpCompositeExtract, this->getType(componentType),
componentId, vec, j, out);
arguments.push_back(componentId);
}
} else {
arguments.push_back(this->writeExpression(*c.arguments()[i], out));
}
}
return this->writeComposite(arguments, type, out);
}
SpvId SPIRVCodeGenerator::writeComposite(const std::vector<SpvId>& arguments,
const Type& type,
OutputStream& out) {
SkASSERT(arguments.size() == (type.isStruct() ? type.fields().size() : (size_t)type.columns()));
SpvId result = this->nextId(&type);
this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) arguments.size(), out);
this->writeWord(this->getType(type), out);
this->writeWord(result, out);
for (SpvId id : arguments) {
this->writeWord(id, out);
}
return result;
}
SpvId SPIRVCodeGenerator::writeConstructorSplat(const ConstructorSplat& c, OutputStream& out) {
// Use writeConstantVector to deduplicate constant splats.
if (c.isCompileTimeConstant()) {
return this->writeConstantVector(c);
}
// Write the splat argument.
SpvId argument = this->writeExpression(*c.argument(), out);
// Generate a OpCompositeConstruct which repeats the argument N times.
std::vector<SpvId> arguments(/*count*/ c.type().columns(), /*value*/ argument);
return this->writeComposite(arguments, c.type(), out);
}
SpvId SPIRVCodeGenerator::writeCompositeConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.type().isArray() || c.type().isStruct());
auto ctorArgs = c.argumentSpan();
std::vector<SpvId> arguments;
arguments.reserve(ctorArgs.size());
for (const std::unique_ptr<Expression>& arg : ctorArgs) {
arguments.push_back(this->writeExpression(*arg, out));
}
return this->writeComposite(arguments, c.type(), out);
}
SpvId SPIRVCodeGenerator::writeConstructorScalarCast(const ConstructorScalarCast& c,
OutputStream& out) {
const Type& type = c.type();
if (this->getActualType(type) == this->getActualType(c.argument()->type())) {
return this->writeExpression(*c.argument(), out);
}
const Expression& ctorExpr = *c.argument();
SpvId expressionId = this->writeExpression(ctorExpr, out);
return this->castScalarToType(expressionId, ctorExpr.type(), type, out);
}
SpvId SPIRVCodeGenerator::writeConstructorCompoundCast(const ConstructorCompoundCast& c,
OutputStream& out) {
const Type& ctorType = c.type();
const Type& argType = c.argument()->type();
SkASSERT(ctorType.isVector() || ctorType.isMatrix());
// Write the composite that we are casting. If the actual type matches, we are done.
SpvId compositeId = this->writeExpression(*c.argument(), out);
if (this->getActualType(ctorType) == this->getActualType(argType)) {
return compositeId;
}
// writeMatrixCopy can cast matrices to a different type.
if (ctorType.isMatrix()) {
return this->writeMatrixCopy(compositeId, argType, ctorType, out);
}
// SPIR-V doesn't support vector(vector-of-different-type) directly, so we need to extract the
// components and convert each one manually.
const Type& srcType = argType.componentType();
const Type& dstType = ctorType.componentType();
std::vector<SpvId> arguments;
arguments.reserve(argType.columns());
for (int index = 0; index < argType.columns(); ++index) {
SpvId componentId = this->nextId(&srcType);
this->writeInstruction(SpvOpCompositeExtract, this->getType(srcType), componentId,
compositeId, index, out);
arguments.push_back(this->castScalarToType(componentId, srcType, dstType, out));
}
return this->writeComposite(arguments, ctorType, out);
}
SpvId SPIRVCodeGenerator::writeConstructorDiagonalMatrix(const ConstructorDiagonalMatrix& c,
OutputStream& out) {
const Type& type = c.type();
SkASSERT(type.isMatrix());
SkASSERT(c.argument()->type().isScalar());
// Write out the scalar argument.
SpvId argument = this->writeExpression(*c.argument(), out);
// Build the diagonal matrix.
SpvId result = this->nextId(&type);
this->writeUniformScaleMatrix(result, argument, type, out);
return result;
}
SpvId SPIRVCodeGenerator::writeConstructorMatrixResize(const ConstructorMatrixResize& c,
OutputStream& out) {
// Write the input matrix.
SpvId argument = this->writeExpression(*c.argument(), out);
// Use matrix-copy to resize the input matrix to its new size.
return this->writeMatrixCopy(argument, c.argument()->type(), c.type(), out);
}
static SpvStorageClass_ get_storage_class(const Variable& var,
SpvStorageClass_ fallbackStorageClass) {
const Modifiers& modifiers = var.modifiers();
if (modifiers.fFlags & Modifiers::kIn_Flag) {
SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
return SpvStorageClassInput;
}
if (modifiers.fFlags & Modifiers::kOut_Flag) {
SkASSERT(!(modifiers.fLayout.fFlags & Layout::kPushConstant_Flag));
return SpvStorageClassOutput;
}
if (modifiers.fFlags & Modifiers::kUniform_Flag) {
if (modifiers.fLayout.fFlags & Layout::kPushConstant_Flag) {
return SpvStorageClassPushConstant;
}
if (var.type().typeKind() == Type::TypeKind::kSampler ||
var.type().typeKind() == Type::TypeKind::kSeparateSampler ||
var.type().typeKind() == Type::TypeKind::kTexture) {
return SpvStorageClassUniformConstant;
}
return SpvStorageClassUniform;
}
return fallbackStorageClass;
}
static SpvStorageClass_ get_storage_class(const Expression& expr) {
switch (expr.kind()) {
case Expression::Kind::kVariableReference: {
const Variable& var = *expr.as<VariableReference>().variable();
if (var.storage() != Variable::Storage::kGlobal) {
return SpvStorageClassFunction;
}
return get_storage_class(var, SpvStorageClassPrivate);
}
case Expression::Kind::kFieldAccess:
return get_storage_class(*expr.as<FieldAccess>().base());
case Expression::Kind::kIndex:
return get_storage_class(*expr.as<IndexExpression>().base());
default:
return SpvStorageClassFunction;
}
}
std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
std::vector<SpvId> chain;
switch (expr.kind()) {
case Expression::Kind::kIndex: {
const IndexExpression& indexExpr = expr.as<IndexExpression>();
chain = this->getAccessChain(*indexExpr.base(), out);
chain.push_back(this->writeExpression(*indexExpr.index(), out));
break;
}
case Expression::Kind::kFieldAccess: {
const FieldAccess& fieldExpr = expr.as<FieldAccess>();
chain = this->getAccessChain(*fieldExpr.base(), out);
IntLiteral index(/*offset=*/-1, fieldExpr.fieldIndex(), fContext.fTypes.fInt.get());
chain.push_back(this->writeIntLiteral(index));
break;
}
default: {
SpvId id = this->getLValue(expr, out)->getPointer();
SkASSERT(id != (SpvId) -1);
chain.push_back(id);
}
}
return chain;
}
class PointerLValue : public SPIRVCodeGenerator::LValue {
public:
PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, bool isMemoryObject, SpvId type,
SPIRVCodeGenerator::Precision precision)
: fGen(gen)
, fPointer(pointer)
, fIsMemoryObject(isMemoryObject)
, fType(type)
, fPrecision(precision) {}
SpvId getPointer() override {
return fPointer;
}
bool isMemoryObjectPointer() const override {
return fIsMemoryObject;
}
SpvId load(OutputStream& out) override {
SpvId result = fGen.nextId(fPrecision);
fGen.writeInstruction(SpvOpLoad, fType, result, fPointer, out);
return result;
}
void store(SpvId value, OutputStream& out) override {
fGen.writeInstruction(SpvOpStore, fPointer, value, out);
}
private:
SPIRVCodeGenerator& fGen;
const SpvId fPointer;
const bool fIsMemoryObject;
const SpvId fType;
const SPIRVCodeGenerator::Precision fPrecision;
};
class SwizzleLValue : public SPIRVCodeGenerator::LValue {
public:
SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const ComponentArray& components,
const Type& baseType, const Type& swizzleType)
: fGen(gen)
, fVecPointer(vecPointer)
, fComponents(components)
, fBaseType(&baseType)
, fSwizzleType(&swizzleType) {}
bool applySwizzle(const ComponentArray& components, const Type& newType) override {
ComponentArray updatedSwizzle;
for (int8_t component : components) {
if (component < 0 || component >= fComponents.count()) {
SkDEBUGFAILF("swizzle accessed nonexistent component %d", (int)component);
return false;
}
updatedSwizzle.push_back(fComponents[component]);
}
fComponents = updatedSwizzle;
fSwizzleType = &newType;
return true;
}
SpvId load(OutputStream& out) override {
SpvId base = fGen.nextId(fBaseType);
fGen.writeInstruction(SpvOpLoad, fGen.getType(*fBaseType), base, fVecPointer, out);
SpvId result = fGen.nextId(fBaseType);
fGen.writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) fComponents.size(), out);
fGen.writeWord(fGen.getType(*fSwizzleType), out);
fGen.writeWord(result, out);
fGen.writeWord(base, out);
fGen.writeWord(base, out);
for (int component : fComponents) {
fGen.writeWord(component, out);
}
return result;
}
void store(SpvId value, OutputStream& out) override {
// use OpVectorShuffle to mix and match the vector components. We effectively create
// a virtual vector out of the concatenation of the left and right vectors, and then
// select components from this virtual vector to make the result vector. For
// instance, given:
// float3L = ...;
// float3R = ...;
// L.xz = R.xy;
// we end up with the virtual vector (L.x, L.y, L.z, R.x, R.y, R.z). Then we want
// our result vector to look like (R.x, L.y, R.y), so we need to select indices
// (3, 1, 4).
SpvId base = fGen.nextId(fBaseType);
fGen.writeInstruction(SpvOpLoad, fGen.getType(*fBaseType), base, fVecPointer, out);
SpvId shuffle = fGen.nextId(fBaseType);
fGen.writeOpCode(SpvOpVectorShuffle, 5 + fBaseType->columns(), out);
fGen.writeWord(fGen.getType(*fBaseType), out);
fGen.writeWord(shuffle, out);
fGen.writeWord(base, out);
fGen.writeWord(value, out);
for (int i = 0; i < fBaseType->columns(); i++) {
// current offset into the virtual vector, defaults to pulling the unmodified
// value from the left side
int offset = i;
// check to see if we are writing this component
for (size_t j = 0; j < fComponents.size(); j++) {
if (fComponents[j] == i) {
// we're writing to this component, so adjust the offset to pull from
// the correct component of the right side instead of preserving the
// value from the left
offset = (int) (j + fBaseType->columns());
break;
}
}
fGen.writeWord(offset, out);
}
fGen.writeInstruction(SpvOpStore, fVecPointer, shuffle, out);
}
private:
SPIRVCodeGenerator& fGen;
const SpvId fVecPointer;
ComponentArray fComponents;
const Type* fBaseType;
const Type* fSwizzleType;
};
int SPIRVCodeGenerator::findUniformFieldIndex(const Variable& var) const {
auto iter = fTopLevelUniformMap.find(&var);
return (iter != fTopLevelUniformMap.end()) ? iter->second : -1;
}
std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(const Expression& expr,
OutputStream& out) {
const Type& type = expr.type();
Precision precision = type.highPrecision() ? Precision::kDefault : Precision::kRelaxed;
switch (expr.kind()) {
case Expression::Kind::kVariableReference: {
const Variable& var = *expr.as<VariableReference>().variable();
int uniformIdx = this->findUniformFieldIndex(var);
if (uniformIdx >= 0) {
IntLiteral uniformIdxLiteral{/*offset=*/-1, uniformIdx, fContext.fTypes.fInt.get()};
SpvId memberId = this->nextId(nullptr);
SpvId typeId = this->getPointerType(type, SpvStorageClassUniform);
SpvId uniformIdxId = this->writeIntLiteral(uniformIdxLiteral);
this->writeInstruction(SpvOpAccessChain, typeId, memberId, fUniformBufferId,
uniformIdxId, out);
return std::make_unique<PointerLValue>(*this, memberId,
/*isMemoryObjectPointer=*/true,
this->getType(type), precision);
}
SpvId typeId;
if (var.modifiers().fLayout.fBuiltin == SK_IN_BUILTIN) {
typeId = this->getType(*Type::MakeArrayType("sk_in", var.type().componentType(),
fSkInCount));
} else {
typeId = this->getType(type, this->memoryLayoutForVariable(var));
}
auto entry = fVariableMap.find(&var);
SkASSERT(entry != fVariableMap.end());
return std::make_unique<PointerLValue>(*this, entry->second,
/*isMemoryObjectPointer=*/true,
typeId, precision);
}
case Expression::Kind::kIndex: // fall through
case Expression::Kind::kFieldAccess: {
std::vector<SpvId> chain = this->getAccessChain(expr, out);
SpvId member = this->nextId(nullptr);
this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
this->writeWord(this->getPointerType(type, get_storage_class(expr)), out);
this->writeWord(member, out);
for (SpvId idx : chain) {
this->writeWord(idx, out);
}
return std::make_unique<PointerLValue>(*this, member, /*isMemoryObjectPointer=*/false,
this->getType(type), precision);
}
case Expression::Kind::kSwizzle: {
const Swizzle& swizzle = expr.as<Swizzle>();
std::unique_ptr<LValue> lvalue = this->getLValue(*swizzle.base(), out);
if (lvalue->applySwizzle(swizzle.components(), type)) {
return lvalue;
}
SpvId base = lvalue->getPointer();
if (base == (SpvId) -1) {
fErrors.error(swizzle.fOffset, "unable to retrieve lvalue from swizzle");
}
if (swizzle.components().size() == 1) {
SpvId member = this->nextId(nullptr);
SpvId typeId = this->getPointerType(type, get_storage_class(*swizzle.base()));
IntLiteral index(/*offset=*/-1, swizzle.components()[0],
fContext.fTypes.fInt.get());
SpvId indexId = this->writeIntLiteral(index);
this->writeInstruction(SpvOpAccessChain, typeId, member, base, indexId, out);
return std::make_unique<PointerLValue>(*this,
member,
/*isMemoryObjectPointer=*/false,
this->getType(type),
precision);
} else {
return std::make_unique<SwizzleLValue>(*this, base, swizzle.components(),
swizzle.base()->type(), type);
}
}
default: {
// expr isn't actually an lvalue, create a dummy variable for it. This case happens due
// to the need to store values in temporary variables during function calls (see
// comments in getFunctionType); erroneous uses of rvalues as lvalues should have been
// caught by IRGenerator
SpvId result = this->nextId(nullptr);
SpvId pointerType = this->getPointerType(type, SpvStorageClassFunction);
this->writeInstruction(SpvOpVariable, pointerType, result, SpvStorageClassFunction,
fVariableBuffer);
this->writeInstruction(SpvOpStore, result, this->writeExpression(expr, out), out);
return std::make_unique<PointerLValue>(*this, result, /*isMemoryObjectPointer=*/true,
this->getType(type), precision);
}
}
}
SpvId SPIRVCodeGenerator::writeVariableReference(const VariableReference& ref, OutputStream& out) {
SpvId result = this->getLValue(ref, out)->load(out);
// Handle the "flipY" setting when reading sk_FragCoord.
const Variable* variable = ref.variable();
if (variable->modifiers().fLayout.fBuiltin == SK_FRAGCOORD_BUILTIN &&
fProgram.fConfig->fSettings.fFlipY) {
// The x component never changes, so just grab it
SpvId xId = this->nextId(Precision::kDefault);
this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fTypes.fFloat), xId,
result, 0, out);
// Calculate the y component which may need to be flipped
SpvId rawYId = this->nextId(nullptr);
this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fTypes.fFloat),
rawYId, result, 1, out);
SpvId flippedYId = 0;
if (fProgram.fConfig->fSettings.fFlipY) {
// need to remap to a top-left coordinate system
if (fRTHeightStructId == (SpvId)-1) {
// height variable hasn't been written yet
SkASSERT(fRTHeightFieldIndex == (SpvId)-1);
std::vector<Type::Field> fields;
if (fProgram.fConfig->fSettings.fRTHeightOffset < 0) {
fErrors.error(ref.fOffset, "RTHeightOffset is negative");
}
fields.emplace_back(
Modifiers(Layout(/*flags=*/0, /*location=*/-1,
fProgram.fConfig->fSettings.fRTHeightOffset,
/*binding=*/-1, /*index=*/-1, /*set=*/-1, /*builtin=*/-1,
/*inputAttachmentIndex=*/-1,
Layout::kUnspecified_Primitive, /*maxVertices=*/1,
/*invocations=*/-1, /*marker=*/"", /*when=*/"",
Layout::CType::kDefault),
/*flags=*/0),
SKSL_RTHEIGHT_NAME, fContext.fTypes.fFloat.get());
StringFragment name("sksl_synthetic_uniforms");
std::unique_ptr<Type> intfStruct = Type::MakeStructType(/*offset=*/-1, name,
fields);
int binding = fProgram.fConfig->fSettings.fRTHeightBinding;
if (binding == -1) {
fErrors.error(ref.fOffset, "layout(binding=...) is required in SPIR-V");
}
int set = fProgram.fConfig->fSettings.fRTHeightSet;
if (set == -1) {
fErrors.error(ref.fOffset, "layout(set=...) is required in SPIR-V");
}
bool usePushConstants = fProgram.fConfig->fSettings.fUsePushConstants;
int flags = usePushConstants ? Layout::Flag::kPushConstant_Flag : 0;
Modifiers modifiers(
Layout(flags, /*location=*/-1, /*offset=*/-1, binding, /*index=*/-1,
set, /*builtin=*/-1, /*inputAttachmentIndex=*/-1,
Layout::kUnspecified_Primitive,
/*maxVertices=*/-1, /*invocations=*/-1, /*marker=*/"", /*when=*/"",
Layout::CType::kDefault),
Modifiers::kUniform_Flag);
const Variable* intfVar = fSynthetics.takeOwnershipOfSymbol(
std::make_unique<Variable>(/*offset=*/-1,
fProgram.fModifiers->addToPool(modifiers),
name,
intfStruct.get(),
/*builtin=*/false,
Variable::Storage::kGlobal));
InterfaceBlock intf(/*offset=*/-1, intfVar, name,
/*instanceName=*/"", /*arraySize=*/0,
std::make_shared<SymbolTable>(&fErrors, /*builtin=*/false));
fRTHeightStructId = this->writeInterfaceBlock(intf, false);
fRTHeightFieldIndex = 0;
fRTHeightStorageClass = usePushConstants ? SpvStorageClassPushConstant
: SpvStorageClassUniform;
}
SkASSERT(fRTHeightFieldIndex != (SpvId)-1);
IntLiteral fieldIndex(/*offset=*/-1, fRTHeightFieldIndex, fContext.fTypes.fInt.get());
SpvId fieldIndexId = this->writeIntLiteral(fieldIndex);
SpvId heightPtr = this->nextId(nullptr);
this->writeOpCode(SpvOpAccessChain, 5, out);
this->writeWord(this->getPointerType(*fContext.fTypes.fFloat, fRTHeightStorageClass),
out);
this->writeWord(heightPtr, out);
this->writeWord(fRTHeightStructId, out);
this->writeWord(fieldIndexId, out);
SpvId heightRead = this->nextId(nullptr);
this->writeInstruction(SpvOpLoad, this->getType(*fContext.fTypes.fFloat), heightRead,
heightPtr, out);
flippedYId = this->nextId(nullptr);
this->writeInstruction(SpvOpFSub, this->getType(*fContext.fTypes.fFloat), flippedYId,
heightRead, rawYId, out);
}
// The z component will always be zero so we just get an id to the 0 literal
FloatLiteral zero(/*offset=*/-1, /*value=*/0.0, fContext.fTypes.fFloat.get());
SpvId zeroId = writeFloatLiteral(zero);
// Calculate the w component
SpvId rawWId = this->nextId(nullptr);
this->writeInstruction(SpvOpCompositeExtract, this->getType(*fContext.fTypes.fFloat),
rawWId, result, 3, out);
// Fill in the new fragcoord with the components from above
SpvId adjusted = this->nextId(nullptr);
this->writeOpCode(SpvOpCompositeConstruct, 7, out);
this->writeWord(this->getType(*fContext.fTypes.fFloat4), out);
this->writeWord(adjusted, out);
this->writeWord(xId, out);
if (fProgram.fConfig->fSettings.fFlipY) {
this->writeWord(flippedYId, out);
} else {
this->writeWord(rawYId, out);
}
this->writeWord(zeroId, out);
this->writeWord(rawWId, out);
return adjusted;
}
// Handle the "flipY" setting when reading sk_Clockwise.
if (variable->modifiers().fLayout.fBuiltin == SK_CLOCKWISE_BUILTIN &&