blob: cc769c364847b78f28d78c4fbaa7addf117e4d57 [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 "include/core/SkSpan.h"
#include "include/core/SkTypes.h"
#include "include/private/SkOpts_spi.h"
#include "include/private/SkSLIRNode.h"
#include "include/private/SkSLProgramElement.h"
#include "include/private/SkSLStatement.h"
#include "include/private/SkSLSymbol.h"
#include "include/private/base/SkTArray.h"
#include "include/sksl/DSLCore.h"
#include "include/sksl/DSLExpression.h"
#include "include/sksl/DSLType.h"
#include "include/sksl/DSLVar.h"
#include "include/sksl/SkSLErrorReporter.h"
#include "include/sksl/SkSLOperator.h"
#include "include/sksl/SkSLPosition.h"
#include "src/sksl/GLSL.std.450.h"
#include "src/sksl/SkSLAnalysis.h"
#include "src/sksl/SkSLBuiltinTypes.h"
#include "src/sksl/SkSLCompiler.h"
#include "src/sksl/SkSLConstantFolder.h"
#include "src/sksl/SkSLContext.h"
#include "src/sksl/SkSLIntrinsicList.h"
#include "src/sksl/SkSLModifiersPool.h"
#include "src/sksl/SkSLOutputStream.h"
#include "src/sksl/SkSLPool.h"
#include "src/sksl/SkSLProgramSettings.h"
#include "src/sksl/SkSLThreadContext.h"
#include "src/sksl/SkSLUtil.h"
#include "src/sksl/analysis/SkSLProgramUsage.h"
#include "src/sksl/ir/SkSLBinaryExpression.h"
#include "src/sksl/ir/SkSLBlock.h"
#include "src/sksl/ir/SkSLConstructor.h"
#include "src/sksl/ir/SkSLConstructorArrayCast.h"
#include "src/sksl/ir/SkSLConstructorCompound.h"
#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
#include "src/sksl/ir/SkSLConstructorScalarCast.h"
#include "src/sksl/ir/SkSLConstructorSplat.h"
#include "src/sksl/ir/SkSLDoStatement.h"
#include "src/sksl/ir/SkSLExpression.h"
#include "src/sksl/ir/SkSLExpressionStatement.h"
#include "src/sksl/ir/SkSLExtension.h"
#include "src/sksl/ir/SkSLField.h"
#include "src/sksl/ir/SkSLFieldAccess.h"
#include "src/sksl/ir/SkSLForStatement.h"
#include "src/sksl/ir/SkSLFunctionCall.h"
#include "src/sksl/ir/SkSLFunctionDeclaration.h"
#include "src/sksl/ir/SkSLFunctionDefinition.h"
#include "src/sksl/ir/SkSLIfStatement.h"
#include "src/sksl/ir/SkSLIndexExpression.h"
#include "src/sksl/ir/SkSLInterfaceBlock.h"
#include "src/sksl/ir/SkSLLiteral.h"
#include "src/sksl/ir/SkSLPostfixExpression.h"
#include "src/sksl/ir/SkSLPrefixExpression.h"
#include "src/sksl/ir/SkSLProgram.h"
#include "src/sksl/ir/SkSLReturnStatement.h"
#include "src/sksl/ir/SkSLSetting.h"
#include "src/sksl/ir/SkSLSwitchCase.h"
#include "src/sksl/ir/SkSLSwitchStatement.h"
#include "src/sksl/ir/SkSLSwizzle.h"
#include "src/sksl/ir/SkSLTernaryExpression.h"
#include "src/sksl/ir/SkSLVarDeclarations.h"
#include "src/sksl/ir/SkSLVariableReference.h"
#include <cmath>
#include <cstring>
#include <set>
#include <string>
#include <utility>
#define kLast_Capability SpvCapabilityMultiViewport
constexpr int DEVICE_FRAGCOORDS_BUILTIN = -1000;
constexpr int DEVICE_CLOCKWISE_BUILTIN = -1001;
namespace SkSL {
// Equality and hash operators for Instructions.
bool SPIRVCodeGenerator::Instruction::operator==(const SPIRVCodeGenerator::Instruction& that) const {
return fOp == that.fOp &&
fResultKind == that.fResultKind &&
fWords == that.fWords;
}
struct SPIRVCodeGenerator::Instruction::Hash {
uint32_t operator()(const SPIRVCodeGenerator::Instruction& key) const {
uint32_t hash = key.fResultKind;
hash = SkOpts::hash_fn(&key.fOp, sizeof(key.fOp), hash);
hash = SkOpts::hash_fn(key.fWords.data(), key.fWords.size() * sizeof(int32_t), hash);
return hash;
}
};
// This class is used to pass values and result placeholder slots to writeInstruction.
struct SPIRVCodeGenerator::Word {
enum Kind {
kNone, // intended for use as a sentinel, not part of any Instruction
kSpvId,
kNumber,
kDefaultPrecisionResult,
kRelaxedPrecisionResult,
kUniqueResult,
kKeyedResult,
};
Word(SpvId id) : fValue(id), fKind(Kind::kSpvId) {}
Word(int32_t val, Kind kind) : fValue(val), fKind(kind) {}
static Word Number(int32_t val) {
return Word{val, Kind::kNumber};
}
static Word Result(const Type& type) {
return (type.hasPrecision() && !type.highPrecision()) ? RelaxedResult() : Result();
}
static Word RelaxedResult() {
return Word{(int32_t)NA, kRelaxedPrecisionResult};
}
static Word UniqueResult() {
return Word{(int32_t)NA, kUniqueResult};
}
static Word Result() {
return Word{(int32_t)NA, kDefaultPrecisionResult};
}
// Unlike a Result (where the result ID is always deduplicated to its first instruction) or a
// UniqueResult (which always produces a new instruction), a KeyedResult allows an instruction
// to be deduplicated among those that share the same `key`.
static Word KeyedResult(int32_t key) { return Word{key, Kind::kKeyedResult}; }
bool isResult() const { return fKind >= Kind::kDefaultPrecisionResult; }
int32_t fValue;
Kind fKind;
};
// Skia's magic number is 31 and goes in the top 16 bits. We can use the lower bits to version the
// sksl generator if we want.
// https://github.com/KhronosGroup/SPIRV-Headers/blob/master/include/spirv/spir-v.xml#L84
static const int32_t SKSL_MAGIC = 0x001F0000;
SPIRVCodeGenerator::Intrinsic SPIRVCodeGenerator::getIntrinsic(IntrinsicKind ik) const {
#define ALL_GLSL(x) Intrinsic{kGLSL_STD_450_IntrinsicOpcodeKind, GLSLstd450 ## x, \
GLSLstd450 ## x, GLSLstd450 ## x, GLSLstd450 ## x}
#define BY_TYPE_GLSL(ifFloat, ifInt, ifUInt) Intrinsic{kGLSL_STD_450_IntrinsicOpcodeKind, \
GLSLstd450 ## ifFloat, \
GLSLstd450 ## ifInt, \
GLSLstd450 ## ifUInt, \
SpvOpUndef}
#define ALL_SPIRV(x) Intrinsic{kSPIRV_IntrinsicOpcodeKind, \
SpvOp ## x, SpvOp ## x, SpvOp ## x, SpvOp ## x}
#define BOOL_SPIRV(x) Intrinsic{kSPIRV_IntrinsicOpcodeKind, \
SpvOpUndef, SpvOpUndef, SpvOpUndef, SpvOp ## x}
#define FLOAT_SPIRV(x) Intrinsic{kSPIRV_IntrinsicOpcodeKind, \
SpvOp ## x, SpvOpUndef, SpvOpUndef, SpvOpUndef}
#define SPECIAL(x) Intrinsic{kSpecial_IntrinsicOpcodeKind, k ## x ## _SpecialIntrinsic, \
k ## x ## _SpecialIntrinsic, k ## x ## _SpecialIntrinsic, \
k ## x ## _SpecialIntrinsic}
switch (ik) {
case k_round_IntrinsicKind: return ALL_GLSL(Round);
case k_roundEven_IntrinsicKind: return ALL_GLSL(RoundEven);
case k_trunc_IntrinsicKind: return ALL_GLSL(Trunc);
case k_abs_IntrinsicKind: return BY_TYPE_GLSL(FAbs, SAbs, SAbs);
case k_sign_IntrinsicKind: return BY_TYPE_GLSL(FSign, SSign, SSign);
case k_floor_IntrinsicKind: return ALL_GLSL(Floor);
case k_ceil_IntrinsicKind: return ALL_GLSL(Ceil);
case k_fract_IntrinsicKind: return ALL_GLSL(Fract);
case k_radians_IntrinsicKind: return ALL_GLSL(Radians);
case k_degrees_IntrinsicKind: return ALL_GLSL(Degrees);
case k_sin_IntrinsicKind: return ALL_GLSL(Sin);
case k_cos_IntrinsicKind: return ALL_GLSL(Cos);
case k_tan_IntrinsicKind: return ALL_GLSL(Tan);
case k_asin_IntrinsicKind: return ALL_GLSL(Asin);
case k_acos_IntrinsicKind: return ALL_GLSL(Acos);
case k_atan_IntrinsicKind: return SPECIAL(Atan);
case k_sinh_IntrinsicKind: return ALL_GLSL(Sinh);
case k_cosh_IntrinsicKind: return ALL_GLSL(Cosh);
case k_tanh_IntrinsicKind: return ALL_GLSL(Tanh);
case k_asinh_IntrinsicKind: return ALL_GLSL(Asinh);
case k_acosh_IntrinsicKind: return ALL_GLSL(Acosh);
case k_atanh_IntrinsicKind: return ALL_GLSL(Atanh);
case k_pow_IntrinsicKind: return ALL_GLSL(Pow);
case k_exp_IntrinsicKind: return ALL_GLSL(Exp);
case k_log_IntrinsicKind: return ALL_GLSL(Log);
case k_exp2_IntrinsicKind: return ALL_GLSL(Exp2);
case k_log2_IntrinsicKind: return ALL_GLSL(Log2);
case k_sqrt_IntrinsicKind: return ALL_GLSL(Sqrt);
case k_inverse_IntrinsicKind: return ALL_GLSL(MatrixInverse);
case k_outerProduct_IntrinsicKind: return ALL_SPIRV(OuterProduct);
case k_transpose_IntrinsicKind: return ALL_SPIRV(Transpose);
case k_isinf_IntrinsicKind: return ALL_SPIRV(IsInf);
case k_isnan_IntrinsicKind: return ALL_SPIRV(IsNan);
case k_inversesqrt_IntrinsicKind: return ALL_GLSL(InverseSqrt);
case k_determinant_IntrinsicKind: return ALL_GLSL(Determinant);
case k_matrixCompMult_IntrinsicKind: return SPECIAL(MatrixCompMult);
case k_matrixInverse_IntrinsicKind: return ALL_GLSL(MatrixInverse);
case k_mod_IntrinsicKind: return SPECIAL(Mod);
case k_modf_IntrinsicKind: return ALL_GLSL(Modf);
case k_min_IntrinsicKind: return SPECIAL(Min);
case k_max_IntrinsicKind: return SPECIAL(Max);
case k_clamp_IntrinsicKind: return SPECIAL(Clamp);
case k_saturate_IntrinsicKind: return SPECIAL(Saturate);
case k_dot_IntrinsicKind: return FLOAT_SPIRV(Dot);
case k_mix_IntrinsicKind: return SPECIAL(Mix);
case k_step_IntrinsicKind: return SPECIAL(Step);
case k_smoothstep_IntrinsicKind: return SPECIAL(SmoothStep);
case k_fma_IntrinsicKind: return ALL_GLSL(Fma);
case k_frexp_IntrinsicKind: return ALL_GLSL(Frexp);
case k_ldexp_IntrinsicKind: return ALL_GLSL(Ldexp);
#define PACK(type) case k_pack##type##_IntrinsicKind: return ALL_GLSL(Pack##type); \
case k_unpack##type##_IntrinsicKind: return ALL_GLSL(Unpack##type)
PACK(Snorm4x8);
PACK(Unorm4x8);
PACK(Snorm2x16);
PACK(Unorm2x16);
PACK(Half2x16);
PACK(Double2x32);
#undef PACK
case k_length_IntrinsicKind: return ALL_GLSL(Length);
case k_distance_IntrinsicKind: return ALL_GLSL(Distance);
case k_cross_IntrinsicKind: return ALL_GLSL(Cross);
case k_normalize_IntrinsicKind: return ALL_GLSL(Normalize);
case k_faceforward_IntrinsicKind: return ALL_GLSL(FaceForward);
case k_reflect_IntrinsicKind: return ALL_GLSL(Reflect);
case k_refract_IntrinsicKind: return ALL_GLSL(Refract);
case k_bitCount_IntrinsicKind: return ALL_SPIRV(BitCount);
case k_findLSB_IntrinsicKind: return ALL_GLSL(FindILsb);
case k_findMSB_IntrinsicKind: return BY_TYPE_GLSL(FindSMsb, FindSMsb, FindUMsb);
case k_dFdx_IntrinsicKind: return FLOAT_SPIRV(DPdx);
case k_dFdy_IntrinsicKind: return SPECIAL(DFdy);
case k_fwidth_IntrinsicKind: return FLOAT_SPIRV(Fwidth);
case k_makeSampler2D_IntrinsicKind: return SPECIAL(SampledImage);
case k_sample_IntrinsicKind: return SPECIAL(Texture);
case k_sampleGrad_IntrinsicKind: return SPECIAL(TextureGrad);
case k_sampleLod_IntrinsicKind: return SPECIAL(TextureLod);
case k_subpassLoad_IntrinsicKind: return SPECIAL(SubpassLoad);
case k_floatBitsToInt_IntrinsicKind: return ALL_SPIRV(Bitcast);
case k_floatBitsToUint_IntrinsicKind: return ALL_SPIRV(Bitcast);
case k_intBitsToFloat_IntrinsicKind: return ALL_SPIRV(Bitcast);
case k_uintBitsToFloat_IntrinsicKind: return ALL_SPIRV(Bitcast);
case k_any_IntrinsicKind: return BOOL_SPIRV(Any);
case k_all_IntrinsicKind: return BOOL_SPIRV(All);
case k_not_IntrinsicKind: return BOOL_SPIRV(LogicalNot);
case k_equal_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFOrdEqual,
SpvOpIEqual,
SpvOpIEqual,
SpvOpLogicalEqual};
case k_notEqual_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFUnordNotEqual,
SpvOpINotEqual,
SpvOpINotEqual,
SpvOpLogicalNotEqual};
case k_lessThan_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFOrdLessThan,
SpvOpSLessThan,
SpvOpULessThan,
SpvOpUndef};
case k_lessThanEqual_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFOrdLessThanEqual,
SpvOpSLessThanEqual,
SpvOpULessThanEqual,
SpvOpUndef};
case k_greaterThan_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFOrdGreaterThan,
SpvOpSGreaterThan,
SpvOpUGreaterThan,
SpvOpUndef};
case k_greaterThanEqual_IntrinsicKind:
return Intrinsic{kSPIRV_IntrinsicOpcodeKind,
SpvOpFOrdGreaterThanEqual,
SpvOpSGreaterThanEqual,
SpvOpUGreaterThanEqual,
SpvOpUndef};
default:
return Intrinsic{kInvalid_IntrinsicOpcodeKind, 0, 0, 0, 0};
}
}
void SPIRVCodeGenerator::writeWord(int32_t word, OutputStream& out) {
out.write((const char*) &word, sizeof(word));
}
static bool is_float(const Type& type) {
return (type.isScalar() || type.isVector() || type.isMatrix()) &&
type.componentType().isFloat();
}
static bool is_signed(const Type& type) {
return (type.isScalar() || type.isVector()) && type.componentType().isSigned();
}
static bool is_unsigned(const Type& type) {
return (type.isScalar() || type.isVector()) && type.componentType().isUnsigned();
}
static bool is_bool(const Type& type) {
return (type.isScalar() || type.isVector()) && type.componentType().isBoolean();
}
template <typename T>
static T pick_by_type(const Type& type, T ifFloat, T ifInt, T ifUInt, T ifBool) {
if (is_float(type)) {
return ifFloat;
}
if (is_signed(type)) {
return ifInt;
}
if (is_unsigned(type)) {
return ifUInt;
}
if (is_bool(type)) {
return ifBool;
}
SkDEBUGFAIL("unrecognized type");
return ifFloat;
}
static bool is_out(const Modifiers& m) {
return (m.fFlags & Modifiers::kOut_Flag) != 0;
}
static bool is_in(const Modifiers& m) {
switch (m.fFlags & (Modifiers::kOut_Flag | Modifiers::kIn_Flag)) {
case Modifiers::kOut_Flag: // out
return false;
case 0: // implicit in
case Modifiers::kIn_Flag: // explicit in
case Modifiers::kOut_Flag | Modifiers::kIn_Flag: // inout
return true;
default: SkUNREACHABLE;
}
}
static bool is_control_flow_op(SpvOp_ op) {
switch (op) {
case SpvOpReturn:
case SpvOpReturnValue:
case SpvOpKill:
case SpvOpSwitch:
case SpvOpBranch:
case SpvOpBranchConditional:
return true;
default:
return false;
}
}
static bool is_globally_reachable_op(SpvOp_ op) {
switch (op) {
case SpvOpConstant:
case SpvOpConstantTrue:
case SpvOpConstantFalse:
case SpvOpConstantComposite:
case SpvOpTypeVoid:
case SpvOpTypeInt:
case SpvOpTypeFloat:
case SpvOpTypeBool:
case SpvOpTypeVector:
case SpvOpTypeMatrix:
case SpvOpTypeArray:
case SpvOpTypePointer:
case SpvOpTypeFunction:
case SpvOpTypeRuntimeArray:
case SpvOpTypeStruct:
case SpvOpTypeImage:
case SpvOpTypeSampledImage:
case SpvOpTypeSampler:
case SpvOpVariable:
case SpvOpFunction:
case SpvOpFunctionParameter:
case SpvOpFunctionEnd:
case SpvOpExecutionMode:
case SpvOpMemoryModel:
case SpvOpCapability:
case SpvOpExtInstImport:
case SpvOpEntryPoint:
case SpvOpSource:
case SpvOpSourceExtension:
case SpvOpName:
case SpvOpMemberName:
case SpvOpDecorate:
case SpvOpMemberDecorate:
return true;
default:
return false;
}
}
void SPIRVCodeGenerator::writeOpCode(SpvOp_ opCode, int length, OutputStream& out) {
SkASSERT(opCode != SpvOpLoad || &out != &fConstantBuffer);
SkASSERT(opCode != SpvOpUndef);
bool foundDeadCode = false;
if (is_control_flow_op(opCode)) {
// This instruction causes us to leave the current block.
foundDeadCode = (fCurrentBlock == 0);
fCurrentBlock = 0;
} else if (!is_globally_reachable_op(opCode)) {
foundDeadCode = (fCurrentBlock == 0);
}
if (foundDeadCode) {
// We just encountered dead code--an instruction that don't have an associated block.
// Synthesize a label if this happens; this is necessary to satisfy the validator.
this->writeLabel(this->nextId(nullptr), kBranchlessBlock, out);
}
this->writeWord((length << 16) | opCode, out);
}
void SPIRVCodeGenerator::writeLabel(SpvId label, StraightLineLabelType, OutputStream& out) {
// The straight-line label type is not important; in any case, no caches are invalidated.
SkASSERT(!fCurrentBlock);
fCurrentBlock = label;
this->writeInstruction(SpvOpLabel, label, out);
}
void SPIRVCodeGenerator::writeLabel(SpvId label, BranchingLabelType type,
ConditionalOpCounts ops, OutputStream& out) {
switch (type) {
case kBranchIsBelow:
case kBranchesOnBothSides:
// With a backward or bidirectional branch, we haven't seen the code between the label
// and the branch yet, so any stored value is potentially suspect. Without scanning
// ahead to check, the only safe option is to ditch the store cache entirely.
fStoreCache.reset();
[[fallthrough]];
case kBranchIsAbove:
// With a forward branch, we can rely on stores that we had cached at the start of the
// statement/expression, if they haven't been touched yet. Anything newer than that is
// pruned.
this->pruneConditionalOps(ops);
break;
}
// Emit the label.
this->writeLabel(label, kBranchlessBlock, 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(std::string_view s, OutputStream& out) {
out.write(s.data(), s.length());
switch (s.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);
break;
}
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, std::string_view string,
OutputStream& out) {
this->writeOpCode(opCode, 1 + (string.length() + 4) / 4, out);
this->writeString(string, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, std::string_view string,
OutputStream& out) {
this->writeOpCode(opCode, 2 + (string.length() + 4) / 4, out);
this->writeWord(word1, out);
this->writeString(string, out);
}
void SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode, int32_t word1, int32_t word2,
std::string_view string, OutputStream& out) {
this->writeOpCode(opCode, 3 + (string.length() + 4) / 4, out);
this->writeWord(word1, out);
this->writeWord(word2, out);
this->writeString(string, 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);
}
SPIRVCodeGenerator::Instruction SPIRVCodeGenerator::BuildInstructionKey(
SpvOp_ opCode, const SkTArray<Word>& words) {
// Assemble a cache key for this instruction.
Instruction key;
key.fOp = opCode;
key.fWords.resize(words.size());
key.fResultKind = Word::Kind::kNone;
for (int index = 0; index < words.size(); ++index) {
const Word& word = words[index];
key.fWords[index] = word.fValue;
if (word.isResult()) {
SkASSERT(key.fResultKind == Word::Kind::kNone);
key.fResultKind = word.fKind;
}
}
return key;
}
SpvId SPIRVCodeGenerator::writeInstruction(SpvOp_ opCode,
const SkTArray<Word>& words,
OutputStream& out) {
// writeOpLoad and writeOpStore have dedicated code.
SkASSERT(opCode != SpvOpLoad);
SkASSERT(opCode != SpvOpStore);
// If this instruction exists in our op cache, return the cached SpvId.
Instruction key = BuildInstructionKey(opCode, words);
if (SpvId* cachedOp = fOpCache.find(key)) {
return *cachedOp;
}
SpvId result = NA;
Precision precision = Precision::kDefault;
switch (key.fResultKind) {
case Word::Kind::kUniqueResult:
// The instruction returns a SpvId, but we do not want deduplication.
result = this->nextId(Precision::kDefault);
fSpvIdCache.set(result, key);
break;
case Word::Kind::kNone:
// The instruction doesn't return a SpvId, but we can still cache and deduplicate it.
fOpCache.set(key, result);
break;
case Word::Kind::kRelaxedPrecisionResult:
precision = Precision::kRelaxed;
[[fallthrough]];
case Word::Kind::kKeyedResult:
[[fallthrough]];
case Word::Kind::kDefaultPrecisionResult:
// Consume a new SpvId.
result = this->nextId(precision);
fOpCache.set(key, result);
fSpvIdCache.set(result, key);
// Globally-reachable ops are not subject to the whims of flow control.
if (!is_globally_reachable_op(opCode)) {
fReachableOps.push_back(result);
}
break;
default:
SkDEBUGFAIL("unexpected result kind");
break;
}
// Write the requested instruction.
this->writeOpCode(opCode, words.size() + 1, out);
for (const Word& word : words) {
if (word.isResult()) {
SkASSERT(result != NA);
this->writeWord(result, out);
} else {
this->writeWord(word.fValue, out);
}
}
// Return the result.
return result;
}
SpvId SPIRVCodeGenerator::writeOpLoad(SpvId type,
Precision precision,
SpvId pointer,
OutputStream& out) {
// Look for this pointer in our load-cache.
if (SpvId* cachedOp = fStoreCache.find(pointer)) {
return *cachedOp;
}
// Write the requested OpLoad instruction.
SpvId result = this->nextId(precision);
this->writeInstruction(SpvOpLoad, type, result, pointer, out);
return result;
}
void SPIRVCodeGenerator::writeOpStore(SpvStorageClass_ storageClass,
SpvId pointer,
SpvId value,
OutputStream& out) {
// Write the uncached SpvOpStore directly.
this->writeInstruction(SpvOpStore, pointer, value, out);
if (storageClass == SpvStorageClassFunction) {
// Insert a pointer-to-SpvId mapping into the load cache. A writeOpLoad to this pointer will
// return the cached value as-is.
fStoreCache.set(pointer, value);
fStoreOps.push_back(pointer);
}
}
SpvId SPIRVCodeGenerator::writeOpConstantTrue(const Type& type) {
return this->writeInstruction(SpvOpConstantTrue,
Words{this->getType(type), Word::Result()},
fConstantBuffer);
}
SpvId SPIRVCodeGenerator::writeOpConstantFalse(const Type& type) {
return this->writeInstruction(SpvOpConstantFalse,
Words{this->getType(type), Word::Result()},
fConstantBuffer);
}
SpvId SPIRVCodeGenerator::writeOpConstant(const Type& type, int32_t valueBits) {
return this->writeInstruction(
SpvOpConstant,
Words{this->getType(type), Word::Result(), Word::Number(valueBits)},
fConstantBuffer);
}
SpvId SPIRVCodeGenerator::writeOpConstantComposite(const Type& type,
const SkTArray<SpvId>& values) {
SkASSERT(values.size() == (type.isStruct() ? (int)type.fields().size() : type.columns()));
Words words;
words.push_back(this->getType(type));
words.push_back(Word::Result());
for (SpvId value : values) {
words.push_back(value);
}
return this->writeInstruction(SpvOpConstantComposite, words, fConstantBuffer);
}
bool SPIRVCodeGenerator::toConstants(SpvId value, SkTArray<SpvId>* constants) {
Instruction* instr = fSpvIdCache.find(value);
if (!instr) {
return false;
}
switch (instr->fOp) {
case SpvOpConstant:
case SpvOpConstantTrue:
case SpvOpConstantFalse:
constants->push_back(value);
return true;
case SpvOpConstantComposite: // OpConstantComposite ResultType ResultID Constituents...
// Start at word 2 to skip past ResultType and ResultID.
for (int i = 2; i < instr->fWords.size(); ++i) {
if (!this->toConstants(instr->fWords[i], constants)) {
return false;
}
}
return true;
default:
return false;
}
}
bool SPIRVCodeGenerator::toConstants(SkSpan<const SpvId> values, SkTArray<SpvId>* constants) {
for (SpvId value : values) {
if (!this->toConstants(value, constants)) {
return false;
}
}
return true;
}
SpvId SPIRVCodeGenerator::writeOpCompositeConstruct(const Type& type,
const SkTArray<SpvId>& values,
OutputStream& out) {
// If this is a vector composed entirely of literals, write a constant-composite instead.
if (type.isVector()) {
SkSTArray<4, SpvId> constants;
if (this->toConstants(SkSpan(values), &constants)) {
// Create a vector from literals.
return this->writeOpConstantComposite(type, constants);
}
}
// If this is a matrix composed entirely of literals, constant-composite them instead.
if (type.isMatrix()) {
SkSTArray<16, SpvId> constants;
if (this->toConstants(SkSpan(values), &constants)) {
// Create each matrix column.
SkASSERT(type.isMatrix());
const Type& vecType = type.componentType().toCompound(fContext,
/*columns=*/type.rows(),
/*rows=*/1);
SkSTArray<4, SpvId> columnIDs;
for (int index=0; index < type.columns(); ++index) {
SkSTArray<4, SpvId> columnConstants(&constants[index * type.rows()],
type.rows());
columnIDs.push_back(this->writeOpConstantComposite(vecType, columnConstants));
}
// Compose the matrix from its columns.
return this->writeOpConstantComposite(type, columnIDs);
}
}
Words words;
words.push_back(this->getType(type));
words.push_back(Word::Result(type));
for (SpvId value : values) {
words.push_back(value);
}
return this->writeInstruction(SpvOpCompositeConstruct, words, out);
}
SPIRVCodeGenerator::Instruction* SPIRVCodeGenerator::resultTypeForInstruction(
const Instruction& instr) {
// This list should contain every op that we cache that has a result and result-type.
// (If one is missing, we will not find some optimization opportunities.)
// Generally, the result type of an op is in the 0th word, but I'm not sure if this is
// universally true, so it's configurable on a per-op basis.
int resultTypeWord;
switch (instr.fOp) {
case SpvOpConstant:
case SpvOpConstantTrue:
case SpvOpConstantFalse:
case SpvOpConstantComposite:
case SpvOpCompositeConstruct:
case SpvOpCompositeExtract:
case SpvOpLoad:
resultTypeWord = 0;
break;
default:
return nullptr;
}
Instruction* typeInstr = fSpvIdCache.find(instr.fWords[resultTypeWord]);
SkASSERT(typeInstr);
return typeInstr;
}
int SPIRVCodeGenerator::numComponentsForVecInstruction(const Instruction& instr) {
// If an instruction is in the op cache, its type should be as well.
Instruction* typeInstr = this->resultTypeForInstruction(instr);
SkASSERT(typeInstr);
SkASSERT(typeInstr->fOp == SpvOpTypeVector || typeInstr->fOp == SpvOpTypeFloat ||
typeInstr->fOp == SpvOpTypeInt || typeInstr->fOp == SpvOpTypeBool);
// For vectors, extract their column count. Scalars have one component by definition.
// SpvOpTypeVector ResultID ComponentType NumComponents
return (typeInstr->fOp == SpvOpTypeVector) ? typeInstr->fWords[2]
: 1;
}
SpvId SPIRVCodeGenerator::toComponent(SpvId id, int component) {
Instruction* instr = fSpvIdCache.find(id);
if (!instr) {
return NA;
}
if (instr->fOp == SpvOpConstantComposite) {
// SpvOpConstantComposite ResultType ResultID [components...]
// Add 2 to the component index to skip past ResultType and ResultID.
return instr->fWords[2 + component];
}
if (instr->fOp == SpvOpCompositeConstruct) {
// SpvOpCompositeConstruct ResultType ResultID [components...]
// Vectors have special rules; check to see if we are composing a vector.
Instruction* composedType = fSpvIdCache.find(instr->fWords[0]);
SkASSERT(composedType);
// When composing a non-vector, each instruction word maps 1:1 to the component index.
// We can just extract out the associated component directly.
if (composedType->fOp != SpvOpTypeVector) {
return instr->fWords[2 + component];
}
// When composing a vector, components can be either scalars or vectors.
// This means we need to check the op type on each component. (+2 to skip ResultType/Result)
for (int index = 2; index < instr->fWords.size(); ++index) {
int32_t currentWord = instr->fWords[index];
// Retrieve the sub-instruction pointed to by OpCompositeConstruct.
Instruction* subinstr = fSpvIdCache.find(currentWord);
if (!subinstr) {
return NA;
}
// If this subinstruction contains the component we're looking for...
int numComponents = this->numComponentsForVecInstruction(*subinstr);
if (component < numComponents) {
if (numComponents == 1) {
// ... it's a scalar. Return it.
SkASSERT(component == 0);
return currentWord;
} else {
// ... it's a vector. Recurse into it.
return this->toComponent(currentWord, component);
}
}
// This sub-instruction doesn't contain our component. Keep walking forward.
component -= numComponents;
}
SkDEBUGFAIL("component index goes past the end of this composite value");
return NA;
}
return NA;
}
SpvId SPIRVCodeGenerator::writeOpCompositeExtract(const Type& type,
SpvId base,
int component,
OutputStream& out) {
// If the base op is a composite, we can extract from it directly.
SpvId result = this->toComponent(base, component);
if (result != NA) {
return result;
}
return this->writeInstruction(
SpvOpCompositeExtract,
{this->getType(type), Word::Result(type), base, Word::Number(component)},
out);
}
SpvId SPIRVCodeGenerator::writeOpCompositeExtract(const Type& type,
SpvId base,
int componentA,
int componentB,
OutputStream& out) {
// If the base op is a composite, we can extract from it directly.
SpvId result = this->toComponent(base, componentA);
if (result != NA) {
return this->writeOpCompositeExtract(type, result, componentB, out);
}
return this->writeInstruction(SpvOpCompositeExtract,
{this->getType(type),
Word::Result(type),
base,
Word::Number(componentA),
Word::Number(componentB)},
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);
}
}
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++;
}
SpvId SPIRVCodeGenerator::writeStruct(const Type& type, const MemoryLayout& memoryLayout) {
// If we've already written out this struct, return its existing SpvId.
if (SpvId* cachedStructId = fStructMap.find(&type)) {
return *cachedStructId;
}
// Write all of the field types first, so we don't inadvertently write them while we're in the
// middle of writing the struct instruction.
Words words;
words.push_back(Word::UniqueResult());
for (const auto& f : type.fields()) {
words.push_back(this->getType(*f.fType, memoryLayout));
}
SpvId resultId = this->writeInstruction(SpvOpTypeStruct, words, fConstantBuffer);
this->writeInstruction(SpvOpName, resultId, type.name(), fNameBuffer);
fStructMap.set(&type, resultId);
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.isSupported(*field.fType)) {
fContext.fErrors->error(type.fPosition, "type '" + field.fType->displayName() +
"' is not permitted here");
return resultId;
}
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) {
fContext.fErrors->error(field.fPosition, "offset of field '" +
std::string(field.fName) + "' must be at least " + std::to_string(offset));
}
if (fieldLayout.fOffset % alignment) {
fContext.fErrors->error(field.fPosition,
"offset of field '" + std::string(field.fName) +
"' must be a multiple of " + std::to_string(alignment));
}
offset = fieldLayout.fOffset;
} else {
size_t mod = offset % alignment;
if (mod) {
offset += alignment - mod;
}
}
this->writeInstruction(SpvOpMemberName, resultId, i, field.fName, fNameBuffer);
this->writeFieldLayout(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;
}
}
return resultId;
}
SpvId SPIRVCodeGenerator::getType(const Type& type) {
return this->getType(type, fDefaultLayout);
}
SpvId SPIRVCodeGenerator::getType(const Type& rawType, const MemoryLayout& layout) {
const Type* type = &rawType;
switch (type->typeKind()) {
case Type::TypeKind::kVoid: {
return this->writeInstruction(SpvOpTypeVoid, Words{Word::Result()}, fConstantBuffer);
}
case Type::TypeKind::kScalar:
case Type::TypeKind::kLiteral: {
if (type->isBoolean()) {
return this->writeInstruction(SpvOpTypeBool, {Word::Result()}, fConstantBuffer);
}
if (type->isSigned()) {
return this->writeInstruction(
SpvOpTypeInt,
Words{Word::Result(), Word::Number(32), Word::Number(1)},
fConstantBuffer);
}
if (type->isUnsigned()) {
return this->writeInstruction(
SpvOpTypeInt,
Words{Word::Result(), Word::Number(32), Word::Number(0)},
fConstantBuffer);
}
if (type->isFloat()) {
return this->writeInstruction(
SpvOpTypeFloat,
Words{Word::Result(), Word::Number(32)},
fConstantBuffer);
}
SkDEBUGFAILF("unrecognized scalar type '%s'", type->description().c_str());
return (SpvId)-1;
}
case Type::TypeKind::kVector: {
SpvId scalarTypeId = this->getType(type->componentType(), layout);
return this->writeInstruction(
SpvOpTypeVector,
Words{Word::Result(), scalarTypeId, Word::Number(type->columns())},
fConstantBuffer);
}
case Type::TypeKind::kMatrix: {
SpvId vectorTypeId = this->getType(IndexExpression::IndexType(fContext, *type), layout);
return this->writeInstruction(
SpvOpTypeMatrix,
Words{Word::Result(), vectorTypeId, Word::Number(type->columns())},
fConstantBuffer);
}
case Type::TypeKind::kArray: {
if (!layout.isSupported(*type)) {
fContext.fErrors->error(type->fPosition, "type '" + type->displayName() +
"' is not permitted here");
return NA;
}
if (type->columns() == 0) {
// We do not support runtime-sized arrays.
fContext.fErrors->error(type->fPosition, "runtime-sized arrays are not supported");
return NA;
}
size_t stride = layout.stride(*type);
SpvId typeId = this->getType(type->componentType(), layout);
SpvId countId = this->writeLiteral(type->columns(), *fContext.fTypes.fInt);
SpvId result = this->writeInstruction(SpvOpTypeArray,
Words{Word::KeyedResult(stride), typeId, countId},
fConstantBuffer);
this->writeInstruction(SpvOpDecorate,
{result, SpvDecorationArrayStride, Word::Number(stride)},
fDecorationBuffer);
return result;
}
case Type::TypeKind::kStruct: {
return this->writeStruct(*type, layout);
}
case Type::TypeKind::kSeparateSampler: {
return this->writeInstruction(SpvOpTypeSampler, Words{Word::Result()}, fConstantBuffer);
}
case Type::TypeKind::kSampler: {
// Subpass inputs should use the Texture type, not a Sampler.
SkASSERT(type->dimensions() != SpvDimSubpassData);
if (SpvDimBuffer == type->dimensions()) {
fCapabilities |= 1ULL << SpvCapabilitySampledBuffer;
}
SpvId imageTypeId = this->getType(type->textureType(), layout);
return this->writeInstruction(SpvOpTypeSampledImage,
Words{Word::Result(), imageTypeId},
fConstantBuffer);
}
case Type::TypeKind::kTexture: {
SpvId floatTypeId = this->getType(*fContext.fTypes.fFloat, layout);
int sampled = (type->textureAccess() == Type::TextureAccess::kSample) ? 1 : 2;
return this->writeInstruction(SpvOpTypeImage,
Words{Word::Result(),
floatTypeId,
Word::Number(type->dimensions()),
Word::Number(type->isDepth()),
Word::Number(type->isArrayedTexture()),
Word::Number(type->isMultisampled()),
Word::Number(sampled),
SpvImageFormatUnknown},
fConstantBuffer);
}
default: {
SkDEBUGFAILF("invalid type: %s", type->description().c_str());
return NA;
}
}
}
SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) {
Words words;
words.push_back(Word::Result());
words.push_back(this->getType(function.returnType()));
for (const Variable* parameter : function.parameters()) {
if (parameter->type().typeKind() == Type::TypeKind::kSampler &&
fProgram.fConfig->fSettings.fSPIRVDawnCompatMode) {
words.push_back(this->getFunctionParameterType(parameter->type().textureType()));
words.push_back(this->getFunctionParameterType(*fContext.fTypes.fSampler));
} else {
words.push_back(this->getFunctionParameterType(parameter->type()));
}
}
return this->writeInstruction(SpvOpTypeFunction, words, fConstantBuffer);
}
SpvId SPIRVCodeGenerator::getFunctionParameterType(const Type& parameterType) {
// glslang treats 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.
//
// The consensus among shader compiler authors seems to be that GPU driver generally don't
// handle value-based parameters consistently. It is highly likely that they fit their
// implementations to conform to glslang. We take care to do so ourselves.
//
// Our implementation first stores every parameter value into a function storage-class pointer
// before calling a function. The exception is for opaque handle types (samplers and textures)
// which must be stored in a pointer with UniformConstant storage-class. This prevents
// unnecessary temporaries (becuase opaque handles are always rooted in a pointer variable),
// matches glslang's behavior, and translates into WGSL more easily when targeting Dawn.
SpvStorageClass_ storageClass;
if (parameterType.typeKind() == Type::TypeKind::kSampler ||
parameterType.typeKind() == Type::TypeKind::kSeparateSampler ||
parameterType.typeKind() == Type::TypeKind::kTexture) {
storageClass = SpvStorageClassUniformConstant;
} else {
storageClass = SpvStorageClassFunction;
}
return this->getPointerType(parameterType, storageClass);
}
SpvId SPIRVCodeGenerator::getPointerType(const Type& type, SpvStorageClass_ storageClass) {
return this->getPointerType(
type, this->memoryLayoutForStorageClass(storageClass), storageClass);
}
SpvId SPIRVCodeGenerator::getPointerType(const Type& type, const MemoryLayout& layout,
SpvStorageClass_ storageClass) {
return this->writeInstruction(
SpvOpTypePointer,
Words{Word::Result(), Word::Number(storageClass), this->getType(type, layout)},
fConstantBuffer);
}
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::kConstructorArrayCast:
return this->writeExpression(*expr.as<ConstructorArrayCast>().argument(), out);
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::kFieldAccess:
return this->writeFieldAccess(expr.as<FieldAccess>(), out);
case Expression::Kind::kFunctionCall:
return this->writeFunctionCall(expr.as<FunctionCall>(), out);
case Expression::Kind::kLiteral:
return this->writeLiteral(expr.as<Literal>());
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);
case Expression::Kind::kSetting:
return this->writeExpression(*expr.as<Setting>().toLiteral(fContext), out);
default:
SkDEBUGFAILF("unsupported expression: %s", expr.description().c_str());
break;
}
return NA;
}
SpvId SPIRVCodeGenerator::writeIntrinsicCall(const FunctionCall& c, OutputStream& out) {
const FunctionDeclaration& function = c.function();
Intrinsic intrinsic = this->getIntrinsic(function.intrinsicKind());
if (intrinsic.opKind == kInvalid_IntrinsicOpcodeKind) {
fContext.fErrors->error(c.fPosition, "unsupported intrinsic '" + function.description() +
"'");
return NA;
}
const ExpressionArray& arguments = c.arguments();
int32_t intrinsicId = intrinsic.floatOp;
if (arguments.size() > 0) {
const Type& type = arguments[0]->type();
if (intrinsic.opKind == kSpecial_IntrinsicOpcodeKind) {
// Keep the default float op.
} else {
intrinsicId = pick_by_type(type, intrinsic.floatOp, intrinsic.signedOp,
intrinsic.unsignedOp, intrinsic.boolOp);
}
}
switch (intrinsic.opKind) {
case kGLSL_STD_450_IntrinsicOpcodeKind: {
SpvId result = this->nextId(&c.type());
SkTArray<SpvId> argumentIds;
std::vector<TempVar> tempVars;
argumentIds.reserve_back(arguments.size());
for (int i = 0; i < arguments.size(); i++) {
argumentIds.push_back(this->writeFunctionCallArgument(c, i, &tempVars, 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);
}
this->copyBackTempVars(tempVars, out);
return result;
}
case kSPIRV_IntrinsicOpcodeKind: {
// 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());
SkTArray<SpvId> argumentIds;
std::vector<TempVar> tempVars;
argumentIds.reserve_back(arguments.size());
for (int i = 0; i < arguments.size(); i++) {
argumentIds.push_back(this->writeFunctionCallArgument(c, i, &tempVars, 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);
}
this->copyBackTempVars(tempVars, out);
return result;
}
case kSpecial_IntrinsicOpcodeKind:
return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId, out);
default:
fContext.fErrors->error(c.fPosition, "unsupported intrinsic '" +
function.description() + "'");
return NA;
}
}
SpvId SPIRVCodeGenerator::vectorize(const Expression& arg, int vectorSize, OutputStream& out) {
SkASSERT(vectorSize >= 1 && vectorSize <= 4);
const Type& argType = arg.type();
if (argType.isScalar() && vectorSize > 1) {
ConstructorSplat splat{arg.fPosition,
argType.toCompound(fContext, vectorSize, /*rows=*/1),
arg.clone()};
return this->writeConstructorSplat(splat, out);
}
SkASSERT(vectorSize == argType.columns());
return this->writeExpression(arg, out);
}
SkTArray<SpvId> SPIRVCodeGenerator::vectorize(const ExpressionArray& args, OutputStream& out) {
int vectorSize = 1;
for (const auto& a : args) {
if (a->type().isVector()) {
if (vectorSize > 1) {
SkASSERT(a->type().columns() == vectorSize);
} else {
vectorSize = a->type().columns();
}
}
}
SkTArray<SpvId> result;
result.reserve_back(args.size());
for (const auto& arg : args) {
result.push_back(this->vectorize(*arg, vectorSize, out));
}
return result;
}
void SPIRVCodeGenerator::writeGLSLExtendedInstruction(const Type& type, SpvId id, SpvId floatInst,
SpvId signedInst, SpvId unsignedInst,
const SkTArray<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);
this->writeWord(pick_by_type(type, floatInst, signedInst, unsignedInst, NA), out);
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: {
SkSTArray<2, 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(Literal::MakeInt(fContext, Position(), /*value=*/0));
args.push_back(Literal::MakeInt(fContext, Position(), /*value=*/0));
ConstructorCompound ctor(Position(), *fContext.fTypes.fInt2, std::move(args));
SpvId coords = this->writeExpression(ctor, out);
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.matches(*fContext.fTypes.fFloat2)) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat));
}
break;
case SpvDim2D:
if (arg1Type.matches(*fContext.fTypes.fFloat3)) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat2));
}
break;
case SpvDim3D:
if (arg1Type.matches(*fContext.fTypes.fFloat4)) {
op = SpvOpImageSampleProjImplicitLod;
} else {
SkASSERT(arg1Type.matches(*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) {
SpvId lodBias = this->writeLiteral(kSharpenTexturesBias,
*fContext.fTypes.fFloat);
this->writeInstruction(op, type, result, sampler, uv,
SpvImageOperandsBiasMask, lodBias, out);
} else {
this->writeInstruction(op, type, result, sampler, uv,
out);
}
}
break;
}
case kTextureGrad_SpecialIntrinsic: {
SpvOp_ op = SpvOpImageSampleExplicitLod;
SkASSERT(arguments.size() == 4);
SkASSERT(arguments[0]->type().dimensions() == SpvDim2D);
SkASSERT(arguments[1]->type().matches(*fContext.fTypes.fFloat2));
SkASSERT(arguments[2]->type().matches(*fContext.fTypes.fFloat2));
SkASSERT(arguments[3]->type().matches(*fContext.fTypes.fFloat2));
SpvId type = this->getType(callType);
SpvId sampler = this->writeExpression(*arguments[0], out);
SpvId uv = this->writeExpression(*arguments[1], out);
SpvId dPdx = this->writeExpression(*arguments[2], out);
SpvId dPdy = this->writeExpression(*arguments[3], out);
this->writeInstruction(op, type, result, sampler, uv, SpvImageOperandsGradMask,
dPdx, dPdy, out);
break;
}
case kTextureLod_SpecialIntrinsic: {
SpvOp_ op = SpvOpImageSampleExplicitLod;
SkASSERT(arguments.size() == 3);
SkASSERT(arguments[0]->type().dimensions() == SpvDim2D);
SkASSERT(arguments[2]->type().matches(*fContext.fTypes.fFloat));
const Type& arg1Type = arguments[1]->type();
if (arg1Type.matches(*fContext.fTypes.fFloat3)) {
op = SpvOpImageSampleProjExplicitLod;
} else {
SkASSERT(arg1Type.matches(*fContext.fTypes.fFloat2));
}
SpvId type = this->getType(callType);
SpvId sampler = this->writeExpression(*arguments[0], out);
SpvId uv = this->writeExpression(*arguments[1], out);
this->writeInstruction(op, type, result, sampler, uv,
SpvImageOperandsLodMask,
this->writeExpression(*arguments[2], out),
out);
break;
}
case kMod_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
const Type& operandType = arguments[0]->type();
SpvOp_ op = pick_by_type(operandType, SpvOpFMod, SpvOpSMod, SpvOpUMod, SpvOpUndef);
SkASSERT(op != SpvOpUndef);
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.fForceNoRTFlip) {
this->addRTFlipUniform(c.fPosition);
using namespace dsl;
DSLExpression rtFlip(
ThreadContext::Compiler().convertIdentifier(Position(), SKSL_RTFLIP_NAME));
SpvId rtFlipY = this->vectorize(*rtFlip.y().release(), callType.columns(), out);
SpvId flipped = this->nextId(&callType);
this->writeInstruction(
SpvOpFMul, this->getType(callType), flipped, result, rtFlipY, out);
result = flipped;
}
break;
}
case kClamp_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
GLSLstd450UClamp, args, out);
break;
}
case kMax_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMax, GLSLstd450SMax,
GLSLstd450UMax, args, out);
break;
}
case kMin_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 2);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FMin, GLSLstd450SMin,
GLSLstd450UMin, args, out);
break;
}
case kMix_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
if (arguments[2]->type().componentType().isBoolean()) {
// Use OpSelect to implement Boolean mix().
SpvId falseId = this->writeExpression(*arguments[0], out);
SpvId trueId = this->writeExpression(*arguments[1], out);
SpvId conditionId = this->writeExpression(*arguments[2], out);
this->writeInstruction(SpvOpSelect, this->getType(arguments[0]->type()), result,
conditionId, trueId, falseId, out);
} else {
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(Literal::MakeFloat(fContext, Position(), /*value=*/0));
finalArgs.push_back(Literal::MakeFloat(fContext, Position(), /*value=*/1));
SkTArray<SpvId> spvArgs = this->vectorize(finalArgs, out);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450FClamp, GLSLstd450SClamp,
GLSLstd450UClamp, spvArgs, out);
break;
}
case kSmoothStep_SpecialIntrinsic: {
SkTArray<SpvId> args = this->vectorize(arguments, out);
SkASSERT(args.size() == 3);
this->writeGLSLExtendedInstruction(callType, result, GLSLstd450SmoothStep, SpvOpUndef,
SpvOpUndef, args, out);
break;
}
case kStep_SpecialIntrinsic: {
SkTArray<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, out);
break;
}
}
return result;
}
SpvId SPIRVCodeGenerator::writeFunctionCallArgument(const FunctionCall& call,
int argIndex,
std::vector<TempVar>* tempVars,
OutputStream& out,
SpvId* outSynthesizedSamplerId) {
const FunctionDeclaration& funcDecl = call.function();
const Expression& arg = *call.arguments()[argIndex];
const Modifiers& paramModifiers = funcDecl.parameters()[argIndex]->modifiers();
// 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 = NA;
if (is_out(paramModifiers)) {
std::unique_ptr<LValue> lv = this->getLValue(arg, out);
// We handle out params with a temp var that we copy back to the original variable at the
// end of the call. GLSL guarantees that the original variable will be unchanged until the
// end of the call, and also that out params are written back to their original variables in
// a specific order (left-to-right), so it's unsafe to pass a pointer to the original value.
if (is_in(paramModifiers)) {
tmpValueId = lv->load(out);
}
tmpVar = this->nextId(&arg.type());
tempVars->push_back(TempVar{tmpVar, &arg.type(), std::move(lv)});
} else if (funcDecl.isIntrinsic()) {
// Unlike user function calls, non-out intrinsic arguments don't need pointer parameters.
return this->writeExpression(arg, out);
} else if (arg.is<VariableReference>() &&
(arg.type().typeKind() == Type::TypeKind::kSampler ||
arg.type().typeKind() == Type::TypeKind::kSeparateSampler ||
arg.type().typeKind() == Type::TypeKind::kTexture)) {
// Opaque handle (sampler/texture) arguments are always declared as pointers but never
// stored in intermediates when calling user-defined functions.
//
// The case for intrinsics (which take opaque arguments by value) is handled above just like
// regular pointers.
//
// See getFunctionParameterType for further explanation.
const Variable* var = arg.as<VariableReference>().variable();
// In Dawn-mode the texture and sampler arguments are forwarded to the helper function.
if (const auto* p = fSynthesizedSamplerMap.find(var)) {
SkASSERT(fProgram.fConfig->fSettings.fSPIRVDawnCompatMode);
SkASSERT(arg.type().typeKind() == Type::TypeKind::kSampler);
SkASSERT(outSynthesizedSamplerId);
SpvId* img = fVariableMap.find((*p)->fTexture.get());
SpvId* sampler = fVariableMap.find((*p)->fSampler.get());
SkASSERT(img);
SkASSERT(sampler);
*outSynthesizedSamplerId = *sampler;
return *img;
}
SpvId* entry = fVariableMap.find(var);
SkASSERTF(entry, "%s", arg.description().c_str());
return *entry;
} else {
// We always use pointer parameters when calling user functions.
// See getFunctionParameterType for further explanation.
tmpValueId = this->writeExpression(arg, out);
tmpVar = this->nextId(nullptr);
}
this->writeInstruction(SpvOpVariable,
this->getPointerType(arg.type(), SpvStorageClassFunction),
tmpVar,
SpvStorageClassFunction,
fVariableBuffer);
if (tmpValueId != NA) {
this->writeOpStore(SpvStorageClassFunction, tmpVar, tmpValueId, out);
}
return tmpVar;
}
void SPIRVCodeGenerator::copyBackTempVars(const std::vector<TempVar>& tempVars, OutputStream& out) {
for (const TempVar& tempVar : tempVars) {
SpvId load = this->nextId(tempVar.type);
this->writeInstruction(SpvOpLoad, this->getType(*tempVar.type), load, tempVar.spvId, out);
tempVar.lvalue->store(load, out);
}
}
SpvId SPIRVCodeGenerator::writeFunctionCall(const FunctionCall& c, OutputStream& out) {
const FunctionDeclaration& function = c.function();
if (function.isIntrinsic() && !function.definition()) {
return this->writeIntrinsicCall(c, out);
}
const ExpressionArray& arguments = c.arguments();
SpvId* entry = fFunctionMap.find(&function);
if (!entry) {
fContext.fErrors->error(c.fPosition, "function '" + function.description() +
"' is not defined");
return NA;
}
// Temp variables are used to write back out-parameters after the function call is complete.
std::vector<TempVar> tempVars;
SkTArray<SpvId> argumentIds;
argumentIds.reserve_back(arguments.size());
for (int i = 0; i < arguments.size(); i++) {
SpvId samplerId = NA;
argumentIds.push_back(this->writeFunctionCallArgument(c, i, &tempVars, out, &samplerId));
if (samplerId != NA) {
argumentIds.push_back(samplerId);
}
}
SpvId result = this->nextId(nullptr);
this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t)argumentIds.size(), out);
this->writeWord(this->getType(c.type()), out);
this->writeWord(result, out);
this->writeWord(*entry, 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.
this->copyBackTempVars(tempVars, out);
return result;
}
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);
}
fContext.fErrors->error(Position(), "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.
const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fFloat);
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
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 NA;
}
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.
const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fInt);
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fInt);
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 NA;
}
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.
const SpvId oneID = this->writeLiteral(1.0, *fContext.fTypes.fUInt);
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fUInt);
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 NA;
}
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.
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fInt);
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.
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fUInt);
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.
const SpvId zeroID = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
this->writeInstruction(SpvOpFUnordNotEqual, this->getType(outputType), result,
inputId, zeroID, out);
} else {
SkDEBUGFAILF("unsupported type for boolean typecast: %s", inputType.description().c_str());
return NA;
}
return result;
}
SpvId SPIRVCodeGenerator::writeMatrixCopy(SpvId src, const Type& srcType, const Type& dstType,
OutputStream& out) {
SkASSERT(srcType.isMatrix());
SkASSERT(dstType.isMatrix());
SkASSERT(srcType.componentType().matches(dstType.componentType()));
const Type& srcColumnType = srcType.componentType().toCompound(fContext, srcType.rows(), 1);
const Type& dstColumnType = dstType.componentType().toCompound(fContext, dstType.rows(), 1);
SkASSERT(dstType.componentType().isFloat());
SpvId dstColumnTypeId = this->getType(dstColumnType);
const SpvId zeroId = this->writeLiteral(0.0, dstType.componentType());
const SpvId oneId = this->writeLiteral(1.0, dstType.componentType());
SkSTArray<4, SpvId> columns;
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->writeOpCompositeExtract(srcColumnType, 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
SkSTArray<4, SpvId> values;
values.push_back(srcColumn);
for (int j = srcType.rows(); j < dstType.rows(); ++j) {
values.push_back((i == j) ? oneId : zeroId);
}
dstColumn = this->writeOpCompositeConstruct(dstColumnType, values, 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(dstColumnTypeId, 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.push_back(dstColumn);
} else {
// we're past the end of the src matrix, need to synthesize an identity-matrix column
SkSTArray<4, SpvId> values;
for (int j = 0; j < dstType.rows(); ++j) {
values.push_back((i == j) ? oneId : zeroId);
}
columns.push_back(this->writeOpCompositeConstruct(dstColumnType, values, out));
}
}
return this->writeOpCompositeConstruct(dstType, columns, out);
}
void SPIRVCodeGenerator::addColumnEntry(const Type& columnType,
SkTArray<SpvId>* currentColumn,
SkTArray<SpvId>* columnIds,
int rows,
SpvId entry,
OutputStream& out) {
SkASSERT(currentColumn->size() < rows);
currentColumn->push_back(entry);
if (currentColumn->size() == rows) {
// Synthesize this column into a vector.
SpvId columnId = this->writeOpCompositeConstruct(columnType, *currentColumn, out);
columnIds->push_back(columnId);
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
SkSTArray<16, SpvId> arguments;
for (const std::unique_ptr<Expression>& arg : c.arguments()) {
arguments.push_back(this->writeExpression(*arg, out));
}
if (arguments.size() == 1 && arg0Type.isVector()) {
// Special-case handling of float4 -> mat2x2.
SkASSERT(type.rows() == 2 && type.columns() == 2);
SkASSERT(arg0Type.columns() == 4);
SpvId v[4];
for (int i = 0; i < 4; ++i) {
v[i] = this->writeOpCompositeExtract(type.componentType(), arguments[0], i, out);
}
const Type& vecType = type.componentType().toCompound(fContext, /*columns=*/2, /*rows=*/1);
SpvId v0v1 = this->writeOpCompositeConstruct(vecType, {v[0], v[1]}, out);
SpvId v2v3 = this->writeOpCompositeConstruct(vecType, {v[2], v[3]}, out);
return this->writeOpCompositeConstruct(type, {v0v1, v2v3}, out);
}
int rows = type.rows();
const Type& columnType = type.componentType().toCompound(fContext,
/*columns=*/rows, /*rows=*/1);
// SpvIds of completed columns of the matrix.
SkSTArray<4, SpvId> columnIds;
// SpvIds of scalars we have written to the current column so far.
SkSTArray<4, SpvId> currentColumn;
for (int i = 0; i < arguments.size(); i++) {
const Type& argType = c.arguments()[i]->type();
if (currentColumn.empty() && argType.isVector() && argType.columns() == rows) {
// This vector is a complete matrix column by itself and can be used as-is.
columnIds.push_back(arguments[i]);
} else if (argType.columns() == 1) {
// This argument is a lone scalar and can be added to the current column as-is.
this->addColumnEntry(columnType, &currentColumn, &columnIds, rows, arguments[i], out);
} else {
// This argument needs to be decomposed into its constituent scalars.
for (int j = 0; j < argType.columns(); ++j) {
SpvId swizzle = this->writeOpCompositeExtract(argType.componentType(),
arguments[i], j, out);
this->addColumnEntry(columnType, &currentColumn, &columnIds, rows, swizzle, out);
}
}
}
SkASSERT(columnIds.size() == type.columns());
return this->writeOpCompositeConstruct(type, columnIds, out);
}
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());
SkSTArray<4, SpvId> arguments;
for (int i = 0; i < c.arguments().size(); i++) {
const Type& argType = c.arguments()[i]->type();
SkASSERT(componentType.numberKind() == argType.componentType().numberKind());
SpvId arg = this->writeExpression(*c.arguments()[i], out);
if (argType.isMatrix()) {
// CompositeConstruct cannot take a 2x2 matrix as an input, so we need to extract out
// each scalar separately.
SkASSERT(argType.rows() == 2);
SkASSERT(argType.columns() == 2);
for (int j = 0; j < 4; ++j) {
arguments.push_back(this->writeOpCompositeExtract(componentType, arg,
j / 2, j % 2, out));
}
} else 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.
for (int j = 0; j < argType.columns(); j++) {
arguments.push_back(this->writeOpCompositeExtract(componentType, arg, j, out));
}
} else {
arguments.push_back(arg);
}
}
return this->writeOpCompositeConstruct(type, arguments, out);
}
SpvId SPIRVCodeGenerator::writeConstructorSplat(const ConstructorSplat& c, OutputStream& out) {
// Write the splat argument.
SpvId argument = this->writeExpression(*c.argument(), out);
// Generate a OpCompositeConstruct which repeats the argument N times.
SkSTArray<4, SpvId> values;
values.push_back_n(/*n=*/c.type().columns(), /*t=*/argument);
return this->writeOpCompositeConstruct(c.type(), values, out);
}
SpvId SPIRVCodeGenerator::writeCompositeConstructor(const AnyConstructor& c, OutputStream& out) {
SkASSERT(c.type().isArray() || c.type().isStruct());
auto ctorArgs = c.argumentSpan();
SkSTArray<4, SpvId> arguments;
for (const std::unique_ptr<Expression>& arg : ctorArgs) {
arguments.push_back(this->writeExpression(*arg, out));
}
return this->writeOpCompositeConstruct(c.type(), arguments, out);
}
SpvId SPIRVCodeGenerator::writeConstructorScalarCast(const ConstructorScalarCast& c,
OutputStream& out) {
const Type& type = c.type();
if (type.componentType().numberKind() == c.argument()->type().componentType().numberKind()) {
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 (ctorType.componentType().numberKind() == argType.componentType().numberKind()) {
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();
SkSTArray<4, SpvId> arguments;
for (int index = 0; index < argType.columns(); ++index) {
SpvId componentId = this->writeOpCompositeExtract(srcType, compositeId, index, out);
arguments.push_back(this->castScalarToType(componentId, srcType, dstType, out));
}
return this->writeOpCompositeConstruct(ctorType, arguments, 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 diagonal = this->writeExpression(*c.argument(), out);
// Build the diagonal matrix.
SpvId zeroId = this->writeLiteral(0.0, *fContext.fTypes.fFloat);
const Type& vecType = type.componentType().toCompound(fContext,
/*columns=*/type.rows(),
/*rows=*/1);
SkSTArray<4, SpvId> columnIds;
SkSTArray<4, SpvId> arguments;
arguments.resize(type.rows());
for (int column = 0; column < type.columns(); column++) {
for (int row = 0; row < type.rows(); row++) {
arguments[row] = (row == column) ? diagonal : zeroId;
}
columnIds.push_back(this->writeOpCompositeConstruct(vecType, arguments, out));
}
return this->writeOpCompositeConstruct(type, columnIds, out);
}
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_for_global_variable(
const Variable& var, SpvStorageClass_ fallbackStorageClass) {
SkASSERT(var.storage() == Variable::Storage::kGlobal);
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_for_global_variable(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;
}
}
SkTArray<SpvId> SPIRVCodeGenerator::getAccessChain(const Expression& expr, OutputStream& out) {
switch (expr.kind()) {
case Expression::Kind::kIndex: {
const IndexExpression& indexExpr = expr.as<IndexExpression>();
SkTArray<SpvId> chain = this->getAccessChain(*indexExpr.base(), out);
chain.push_back(this->writeExpression(*indexExpr.index(), out));
return chain;
}
case Expression::Kind::kFieldAccess: {
const FieldAccess& fieldExpr = expr.as<FieldAccess>();
SkTArray<SpvId> chain = this->getAccessChain(*fieldExpr.base(), out);
chain.push_back(this->writeLiteral(fieldExpr.fieldIndex(), *fContext.fTypes.fInt));
return chain;
}
default: {
SpvId id = this->getLValue(expr, out)->getPointer();
SkASSERT(id != NA);
return SkTArray<SpvId>{id};
}
}
SkUNREACHABLE;
}
class PointerLValue : public SPIRVCodeGenerator::LValue {
public:
PointerLValue(SPIRVCodeGenerator& gen, SpvId pointer, bool isMemoryObject, SpvId type,
SPIRVCodeGenerator::Precision precision, SpvStorageClass_ storageClass)
: fGen(gen)
, fPointer(pointer)
, fIsMemoryObject(isMemoryObject)
, fType(type)
, fPrecision(precision)
, fStorageClass(storageClass) {}
SpvId getPointer() override {
return fPointer;
}
bool isMemoryObjectPointer() const override {
return fIsMemoryObject;
}
SpvId load(OutputStream& out) override {
return fGen.writeOpLoad(fType, fPrecision, fPointer, out);
}
void store(SpvId value, OutputStream& out) override {
if (!fIsMemoryObject) {
// We are going to write into an access chain; this could represent one component of a
// vector, or one element of an array. This has the potential to invalidate other,
// *unknown* elements of our store cache. (e.g. if the store cache holds `%50 = myVec4`,
// and we store `%60 = myVec4.z`, this invalidates the cached value for %50.) To avoid
// relying on stale data, reset the store cache entirely when this happens.
fGen.fStoreCache.reset();
}
fGen.writeOpStore(fStorageClass, fPointer, value, out);
}
private:
SPIRVCodeGenerator& fGen;
const SpvId fPointer;
const bool fIsMemoryObject;
const SpvId fType;
const SPIRVCodeGenerator::Precision fPrecision;
const SpvStorageClass_ fStorageClass;
};
class SwizzleLValue : public SPIRVCodeGenerator::LValue {
public:
SwizzleLValue(SPIRVCodeGenerator& gen, SpvId vecPointer, const ComponentArray& components,
const Type& baseType, const Type& swizzleType, SpvStorageClass_ storageClass)
: fGen(gen)
, fVecPointer(vecPointer)
, fComponents(components)
, fBaseType(&baseType)
, fSwizzleType(&swizzleType)
, fStorageClass(storageClass) {}
bool applySwizzle(const ComponentArray& components, const Type& newType) override {
ComponentArray updatedSwizzle;
for (int8_t component : components) {
if (component < 0 || component >= fComponents.size()) {
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 (int 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.writeOpStore(fStorageClass, fVecPointer, shuffle, out);
}
private:
SPIRVCodeGenerator& fGen;
const SpvId fVecPointer;
ComponentArray fComponents;
const Type* fBaseType;
const Type* fSwizzleType;
const SpvStorageClass_ fStorageClass;
};
int SPIRVCodeGenerator::findUniformFieldIndex(const Variable& var) const {
int* fieldIndex = fTopLevelUniformMap.find(&var);
return fieldIndex ? *fieldIndex : -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) {
SpvId memberId = this->nextId(nullptr);
SpvId typeId = this->getPointerType(type, SpvStorageClassUniform);
SpvId uniformIdxId = this->writeLiteral((double)uniformIdx, *fContext.fTypes.fInt);
this->writeInstruction(SpvOpAccessChain, typeId, memberId, fUniformBufferId,
uniformIdxId, out);
return std::make_unique<PointerLValue>(
*this,
memberId,
/*isMemoryObjectPointer=*/true,
this->getType(type, this->memoryLayoutForVariable(var)),
precision,
SpvStorageClassUniform);
}
SpvId typeId = this->getType(type, this->memoryLayoutForVariable(var));
SpvId* entry = fVariableMap.find(&var);
SkASSERTF(entry, "%s", expr.description().c_str());
return std::make_unique<PointerLValue>(*this, *entry,
/*isMemoryObjectPointer=*/true,
typeId, precision, get_storage_class(expr));
}
case Expression::Kind::kIndex: // fall through
case Expression::Kind::kFieldAccess: {
SkTArray<SpvId> chain = this->getAccessChain(expr, out);
SpvId member = this->nextId(nullptr);
SpvStorageClass_ storageClass = get_storage_class(expr);
this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out);
this->writeWord(this->getPointerType(type, storageClass), 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, this->memoryLayoutForStorageClass(storageClass)),
precision,
storageClass);
}
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 == NA) {
fContext.fErrors->error(swizzle.fPosition,
"unable to retrieve lvalue from swizzle");
}
SpvStorageClass_ storageClass = get_storage_class(*swizzle.base());
if (swizzle.components().size() == 1) {
SpvId member = this->nextId(nullptr);
SpvId typeId = this->getPointerType(type, storageClass);
SpvId indexId = this->writeLiteral(swizzle.components()[0], *fContext.fTypes.fInt);
this->writeInstruction(SpvOpAccessChain, typeId, member, base, indexId, out);
return std::make_unique<PointerLValue>(*this, member,
/*isMemoryObjectPointer=*/false,
this->getType(type),