OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLSPIRVCodeGenerator.h" | 8 #include "SkSLSPIRVCodeGenerator.h" |
9 | 9 |
10 #include "string.h" | 10 #include "string.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 134 } |
135 | 135 |
136 void SPIRVCodeGenerator::writeWord(int32_t word, std::ostream& out) { | 136 void SPIRVCodeGenerator::writeWord(int32_t word, std::ostream& out) { |
137 #if SPIRV_DEBUG | 137 #if SPIRV_DEBUG |
138 out << "(" << word << ") "; | 138 out << "(" << word << ") "; |
139 #else | 139 #else |
140 out.write((const char*) &word, sizeof(word)); | 140 out.write((const char*) &word, sizeof(word)); |
141 #endif | 141 #endif |
142 } | 142 } |
143 | 143 |
144 static bool is_float(const Type& type) { | 144 static bool is_float(const Context& context, const Type& type) { |
145 if (type.kind() == Type::kVector_Kind) { | 145 if (type.kind() == Type::kVector_Kind) { |
146 return is_float(*type.componentType()); | 146 return is_float(context, type.componentType()); |
147 } | 147 } |
148 return type == *kFloat_Type || type == *kDouble_Type; | 148 return type == *context.fFloat_Type || type == *context.fDouble_Type; |
149 } | 149 } |
150 | 150 |
151 static bool is_signed(const Type& type) { | 151 static bool is_signed(const Context& context, const Type& type) { |
152 if (type.kind() == Type::kVector_Kind) { | 152 if (type.kind() == Type::kVector_Kind) { |
153 return is_signed(*type.componentType()); | 153 return is_signed(context, type.componentType()); |
154 } | 154 } |
155 return type == *kInt_Type; | 155 return type == *context.fInt_Type; |
156 } | 156 } |
157 | 157 |
158 static bool is_unsigned(const Type& type) { | 158 static bool is_unsigned(const Context& context, const Type& type) { |
159 if (type.kind() == Type::kVector_Kind) { | 159 if (type.kind() == Type::kVector_Kind) { |
160 return is_unsigned(*type.componentType()); | 160 return is_unsigned(context, type.componentType()); |
161 } | 161 } |
162 return type == *kUInt_Type; | 162 return type == *context.fUInt_Type; |
163 } | 163 } |
164 | 164 |
165 static bool is_bool(const Type& type) { | 165 static bool is_bool(const Context& context, const Type& type) { |
166 if (type.kind() == Type::kVector_Kind) { | 166 if (type.kind() == Type::kVector_Kind) { |
167 return is_bool(*type.componentType()); | 167 return is_bool(context, type.componentType()); |
168 } | 168 } |
169 return type == *kBool_Type; | 169 return type == *context.fBool_Type; |
170 } | 170 } |
171 | 171 |
172 static bool is_out(std::shared_ptr<Variable> var) { | 172 static bool is_out(const Variable& var) { |
173 return (var->fModifiers.fFlags & Modifiers::kOut_Flag) != 0; | 173 return (var.fModifiers.fFlags & Modifiers::kOut_Flag) != 0; |
174 } | 174 } |
175 | 175 |
176 #if SPIRV_DEBUG | 176 #if SPIRV_DEBUG |
177 static std::string opcode_text(SpvOp_ opCode) { | 177 static std::string opcode_text(SpvOp_ opCode) { |
178 switch (opCode) { | 178 switch (opCode) { |
179 case SpvOpNop: | 179 case SpvOpNop: |
180 return "Nop"; | 180 return "Nop"; |
181 case SpvOpUndef: | 181 case SpvOpUndef: |
182 return "Undef"; | 182 return "Undef"; |
183 case SpvOpSourceContinued: | 183 case SpvOpSourceContinued: |
(...skipping 782 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
966 SpvId SPIRVCodeGenerator::nextId() { | 966 SpvId SPIRVCodeGenerator::nextId() { |
967 return fIdCount++; | 967 return fIdCount++; |
968 } | 968 } |
969 | 969 |
970 void SPIRVCodeGenerator::writeStruct(const Type& type, SpvId resultId) { | 970 void SPIRVCodeGenerator::writeStruct(const Type& type, SpvId resultId) { |
971 this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer
); | 971 this->writeInstruction(SpvOpName, resultId, type.name().c_str(), fNameBuffer
); |
972 // go ahead and write all of the field types, so we don't inadvertently writ
e them while we're | 972 // go ahead and write all of the field types, so we don't inadvertently writ
e them while we're |
973 // in the middle of writing the struct instruction | 973 // in the middle of writing the struct instruction |
974 std::vector<SpvId> types; | 974 std::vector<SpvId> types; |
975 for (const auto& f : type.fields()) { | 975 for (const auto& f : type.fields()) { |
976 types.push_back(this->getType(*f.fType)); | 976 types.push_back(this->getType(f.fType)); |
977 } | 977 } |
978 this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuff
er); | 978 this->writeOpCode(SpvOpTypeStruct, 2 + (int32_t) types.size(), fConstantBuff
er); |
979 this->writeWord(resultId, fConstantBuffer); | 979 this->writeWord(resultId, fConstantBuffer); |
980 for (SpvId id : types) { | 980 for (SpvId id : types) { |
981 this->writeWord(id, fConstantBuffer); | 981 this->writeWord(id, fConstantBuffer); |
982 } | 982 } |
983 size_t offset = 0; | 983 size_t offset = 0; |
984 for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) { | 984 for (int32_t i = 0; i < (int32_t) type.fields().size(); i++) { |
985 size_t size = type.fields()[i].fType->size(); | 985 size_t size = type.fields()[i].fType.size(); |
986 size_t alignment = type.fields()[i].fType->alignment(); | 986 size_t alignment = type.fields()[i].fType.alignment(); |
987 size_t mod = offset % alignment; | 987 size_t mod = offset % alignment; |
988 if (mod != 0) { | 988 if (mod != 0) { |
989 offset += alignment - mod; | 989 offset += alignment - mod; |
990 } | 990 } |
991 this->writeInstruction(SpvOpMemberName, resultId, i, type.fields()[i].fN
ame.c_str(), | 991 this->writeInstruction(SpvOpMemberName, resultId, i, type.fields()[i].fN
ame.c_str(), |
992 fNameBuffer); | 992 fNameBuffer); |
993 this->writeLayout(type.fields()[i].fModifiers.fLayout, resultId, i); | 993 this->writeLayout(type.fields()[i].fModifiers.fLayout, resultId, i); |
994 if (type.fields()[i].fModifiers.fLayout.fBuiltin < 0) { | 994 if (type.fields()[i].fModifiers.fLayout.fBuiltin < 0) { |
995 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, Spv
DecorationOffset, | 995 this->writeInstruction(SpvOpMemberDecorate, resultId, (SpvId) i, Spv
DecorationOffset, |
996 (SpvId) offset, fDecorationBuffer); | 996 (SpvId) offset, fDecorationBuffer); |
997 } | 997 } |
998 if (type.fields()[i].fType->kind() == Type::kMatrix_Kind) { | 998 if (type.fields()[i].fType.kind() == Type::kMatrix_Kind) { |
999 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onColMajor, | 999 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onColMajor, |
1000 fDecorationBuffer); | 1000 fDecorationBuffer); |
1001 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onMatrixStride, | 1001 this->writeInstruction(SpvOpMemberDecorate, resultId, i, SpvDecorati
onMatrixStride, |
1002 (SpvId) type.fields()[i].fType->stride(), fDe
corationBuffer); | 1002 (SpvId) type.fields()[i].fType.stride(), fDec
orationBuffer); |
1003 } | 1003 } |
1004 offset += size; | 1004 offset += size; |
1005 Type::Kind kind = type.fields()[i].fType->kind(); | 1005 Type::Kind kind = type.fields()[i].fType.kind(); |
1006 if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset
% alignment != 0) { | 1006 if ((kind == Type::kArray_Kind || kind == Type::kStruct_Kind) && offset
% alignment != 0) { |
1007 offset += alignment - offset % alignment; | 1007 offset += alignment - offset % alignment; |
1008 } | 1008 } |
1009 ASSERT(offset % alignment == 0); | 1009 ASSERT(offset % alignment == 0); |
1010 } | 1010 } |
1011 } | 1011 } |
1012 | 1012 |
1013 SpvId SPIRVCodeGenerator::getType(const Type& type) { | 1013 SpvId SPIRVCodeGenerator::getType(const Type& type) { |
1014 auto entry = fTypeMap.find(type.name()); | 1014 auto entry = fTypeMap.find(type.name()); |
1015 if (entry == fTypeMap.end()) { | 1015 if (entry == fTypeMap.end()) { |
1016 SpvId result = this->nextId(); | 1016 SpvId result = this->nextId(); |
1017 switch (type.kind()) { | 1017 switch (type.kind()) { |
1018 case Type::kScalar_Kind: | 1018 case Type::kScalar_Kind: |
1019 if (type == *kBool_Type) { | 1019 if (type == *fContext.fBool_Type) { |
1020 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffe
r); | 1020 this->writeInstruction(SpvOpTypeBool, result, fConstantBuffe
r); |
1021 } else if (type == *kInt_Type) { | 1021 } else if (type == *fContext.fInt_Type) { |
1022 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstan
tBuffer); | 1022 this->writeInstruction(SpvOpTypeInt, result, 32, 1, fConstan
tBuffer); |
1023 } else if (type == *kUInt_Type) { | 1023 } else if (type == *fContext.fUInt_Type) { |
1024 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstan
tBuffer); | 1024 this->writeInstruction(SpvOpTypeInt, result, 32, 0, fConstan
tBuffer); |
1025 } else if (type == *kFloat_Type) { | 1025 } else if (type == *fContext.fFloat_Type) { |
1026 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstant
Buffer); | 1026 this->writeInstruction(SpvOpTypeFloat, result, 32, fConstant
Buffer); |
1027 } else if (type == *kDouble_Type) { | 1027 } else if (type == *fContext.fDouble_Type) { |
1028 this->writeInstruction(SpvOpTypeFloat, result, 64, fConstant
Buffer); | 1028 this->writeInstruction(SpvOpTypeFloat, result, 64, fConstant
Buffer); |
1029 } else { | 1029 } else { |
1030 ASSERT(false); | 1030 ASSERT(false); |
1031 } | 1031 } |
1032 break; | 1032 break; |
1033 case Type::kVector_Kind: | 1033 case Type::kVector_Kind: |
1034 this->writeInstruction(SpvOpTypeVector, result, | 1034 this->writeInstruction(SpvOpTypeVector, result, |
1035 this->getType(*type.componentType()), | 1035 this->getType(type.componentType()), |
1036 type.columns(), fConstantBuffer); | 1036 type.columns(), fConstantBuffer); |
1037 break; | 1037 break; |
1038 case Type::kMatrix_Kind: | 1038 case Type::kMatrix_Kind: |
1039 this->writeInstruction(SpvOpTypeMatrix, result, this->getType(*i
ndex_type(type)), | 1039 this->writeInstruction(SpvOpTypeMatrix, result, |
| 1040 this->getType(index_type(fContext, type))
, |
1040 type.columns(), fConstantBuffer); | 1041 type.columns(), fConstantBuffer); |
1041 break; | 1042 break; |
1042 case Type::kStruct_Kind: | 1043 case Type::kStruct_Kind: |
1043 this->writeStruct(type, result); | 1044 this->writeStruct(type, result); |
1044 break; | 1045 break; |
1045 case Type::kArray_Kind: { | 1046 case Type::kArray_Kind: { |
1046 if (type.columns() > 0) { | 1047 if (type.columns() > 0) { |
1047 IntLiteral count(Position(), type.columns()); | 1048 IntLiteral count(fContext, Position(), type.columns()); |
1048 this->writeInstruction(SpvOpTypeArray, result, | 1049 this->writeInstruction(SpvOpTypeArray, result, |
1049 this->getType(*type.componentType()),
| 1050 this->getType(type.componentType()), |
1050 this->writeIntLiteral(count), fConsta
ntBuffer); | 1051 this->writeIntLiteral(count), fConsta
ntBuffer); |
1051 this->writeInstruction(SpvOpDecorate, result, SpvDecorationA
rrayStride, | 1052 this->writeInstruction(SpvOpDecorate, result, SpvDecorationA
rrayStride, |
1052 (int32_t) type.stride(), fDecorationB
uffer); | 1053 (int32_t) type.stride(), fDecorationB
uffer); |
1053 } else { | 1054 } else { |
1054 ABORT("runtime-sized arrays are not yet supported"); | 1055 ABORT("runtime-sized arrays are not yet supported"); |
1055 this->writeInstruction(SpvOpTypeRuntimeArray, result, | 1056 this->writeInstruction(SpvOpTypeRuntimeArray, result, |
1056 this->getType(*type.componentType()),
fConstantBuffer); | 1057 this->getType(type.componentType()),
fConstantBuffer); |
1057 } | 1058 } |
1058 break; | 1059 break; |
1059 } | 1060 } |
1060 case Type::kSampler_Kind: { | 1061 case Type::kSampler_Kind: { |
1061 SpvId image = this->nextId(); | 1062 SpvId image = this->nextId(); |
1062 this->writeInstruction(SpvOpTypeImage, image, this->getType(*kFl
oat_Type), | 1063 this->writeInstruction(SpvOpTypeImage, image, this->getType(*fCo
ntext.fFloat_Type), |
1063 type.dimensions(), type.isDepth(), type.i
sArrayed(), | 1064 type.dimensions(), type.isDepth(), type.i
sArrayed(), |
1064 type.isMultisampled(), type.isSampled(), | 1065 type.isMultisampled(), type.isSampled(), |
1065 SpvImageFormatUnknown, fConstantBuffer); | 1066 SpvImageFormatUnknown, fConstantBuffer); |
1066 this->writeInstruction(SpvOpTypeSampledImage, result, image, fCo
nstantBuffer); | 1067 this->writeInstruction(SpvOpTypeSampledImage, result, image, fCo
nstantBuffer); |
1067 break; | 1068 break; |
1068 } | 1069 } |
1069 default: | 1070 default: |
1070 if (type == *kVoid_Type) { | 1071 if (type == *fContext.fVoid_Type) { |
1071 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffe
r); | 1072 this->writeInstruction(SpvOpTypeVoid, result, fConstantBuffe
r); |
1072 } else { | 1073 } else { |
1073 ABORT("invalid type: %s", type.description().c_str()); | 1074 ABORT("invalid type: %s", type.description().c_str()); |
1074 } | 1075 } |
1075 } | 1076 } |
1076 fTypeMap[type.name()] = result; | 1077 fTypeMap[type.name()] = result; |
1077 return result; | 1078 return result; |
1078 } | 1079 } |
1079 return entry->second; | 1080 return entry->second; |
1080 } | 1081 } |
1081 | 1082 |
1082 SpvId SPIRVCodeGenerator::getFunctionType(std::shared_ptr<FunctionDeclaration> f
unction) { | 1083 SpvId SPIRVCodeGenerator::getFunctionType(const FunctionDeclaration& function) { |
1083 std::string key = function->fReturnType->description() + "("; | 1084 std::string key = function.fReturnType.description() + "("; |
1084 std::string separator = ""; | 1085 std::string separator = ""; |
1085 for (size_t i = 0; i < function->fParameters.size(); i++) { | 1086 for (size_t i = 0; i < function.fParameters.size(); i++) { |
1086 key += separator; | 1087 key += separator; |
1087 separator = ", "; | 1088 separator = ", "; |
1088 key += function->fParameters[i]->fType->description(); | 1089 key += function.fParameters[i]->fType.description(); |
1089 } | 1090 } |
1090 key += ")"; | 1091 key += ")"; |
1091 auto entry = fTypeMap.find(key); | 1092 auto entry = fTypeMap.find(key); |
1092 if (entry == fTypeMap.end()) { | 1093 if (entry == fTypeMap.end()) { |
1093 SpvId result = this->nextId(); | 1094 SpvId result = this->nextId(); |
1094 int32_t length = 3 + (int32_t) function->fParameters.size(); | 1095 int32_t length = 3 + (int32_t) function.fParameters.size(); |
1095 SpvId returnType = this->getType(*function->fReturnType); | 1096 SpvId returnType = this->getType(function.fReturnType); |
1096 std::vector<SpvId> parameterTypes; | 1097 std::vector<SpvId> parameterTypes; |
1097 for (size_t i = 0; i < function->fParameters.size(); i++) { | 1098 for (size_t i = 0; i < function.fParameters.size(); i++) { |
1098 // glslang seems to treat all function arguments as pointers whether
they need to be or | 1099 // glslang seems to treat all function arguments as pointers whether
they need to be or |
1099 // not. I was initially puzzled by this until I ran bizarre failure
s with certain | 1100 // not. I was initially puzzled by this until I ran bizarre failure
s with certain |
1100 // patterns of function calls and control constructs, as exemplified
by this minimal | 1101 // patterns of function calls and control constructs, as exemplified
by this minimal |
1101 // failure case: | 1102 // failure case: |
1102 // | 1103 // |
1103 // void sphere(float x) { | 1104 // void sphere(float x) { |
1104 // } | 1105 // } |
1105 // | 1106 // |
1106 // void map() { | 1107 // void map() { |
1107 // sphere(1.0); | 1108 // sphere(1.0); |
1108 // } | 1109 // } |
1109 // | 1110 // |
1110 // void main() { | 1111 // void main() { |
1111 // for (int i = 0; i < 1; i++) { | 1112 // for (int i = 0; i < 1; i++) { |
1112 // map(); | 1113 // map(); |
1113 // } | 1114 // } |
1114 // } | 1115 // } |
1115 // | 1116 // |
1116 // As of this writing, compiling this in the "obvious" way (with sph
ere taking a float) | 1117 // As of this writing, compiling this in the "obvious" way (with sph
ere taking a float) |
1117 // crashes. Making it take a float* and storing the argument in a te
mporary variable, | 1118 // crashes. Making it take a float* and storing the argument in a te
mporary variable, |
1118 // as glslang does, fixes it. It's entirely possible I simply missed
whichever part of | 1119 // as glslang does, fixes it. It's entirely possible I simply missed
whichever part of |
1119 // the spec makes this make sense. | 1120 // the spec makes this make sense. |
1120 // if (is_out(function->fParameters[i])) { | 1121 // if (is_out(function->fParameters[i])) { |
1121 parameterTypes.push_back(this->getPointerType(function->fParamet
ers[i]->fType, | 1122 parameterTypes.push_back(this->getPointerType(function.fParamete
rs[i]->fType, |
1122 SpvStorageClassFun
ction)); | 1123 SpvStorageClassFun
ction)); |
1123 // } else { | 1124 // } else { |
1124 // parameterTypes.push_back(this->getType(*function->fParameters[
i]->fType)); | 1125 // parameterTypes.push_back(this->getType(function.fParameters[i]
->fType)); |
1125 // } | 1126 // } |
1126 } | 1127 } |
1127 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); | 1128 this->writeOpCode(SpvOpTypeFunction, length, fConstantBuffer); |
1128 this->writeWord(result, fConstantBuffer); | 1129 this->writeWord(result, fConstantBuffer); |
1129 this->writeWord(returnType, fConstantBuffer); | 1130 this->writeWord(returnType, fConstantBuffer); |
1130 for (SpvId id : parameterTypes) { | 1131 for (SpvId id : parameterTypes) { |
1131 this->writeWord(id, fConstantBuffer); | 1132 this->writeWord(id, fConstantBuffer); |
1132 } | 1133 } |
1133 fTypeMap[key] = result; | 1134 fTypeMap[key] = result; |
1134 return result; | 1135 return result; |
1135 } | 1136 } |
1136 return entry->second; | 1137 return entry->second; |
1137 } | 1138 } |
1138 | 1139 |
1139 SpvId SPIRVCodeGenerator::getPointerType(std::shared_ptr<Type> type, | 1140 SpvId SPIRVCodeGenerator::getPointerType(const Type& type, |
1140 SpvStorageClass_ storageClass) { | 1141 SpvStorageClass_ storageClass) { |
1141 std::string key = type->description() + "*" + to_string(storageClass); | 1142 std::string key = type.description() + "*" + to_string(storageClass); |
1142 auto entry = fTypeMap.find(key); | 1143 auto entry = fTypeMap.find(key); |
1143 if (entry == fTypeMap.end()) { | 1144 if (entry == fTypeMap.end()) { |
1144 SpvId result = this->nextId(); | 1145 SpvId result = this->nextId(); |
1145 this->writeInstruction(SpvOpTypePointer, result, storageClass, | 1146 this->writeInstruction(SpvOpTypePointer, result, storageClass, |
1146 this->getType(*type), fConstantBuffer); | 1147 this->getType(type), fConstantBuffer); |
1147 fTypeMap[key] = result; | 1148 fTypeMap[key] = result; |
1148 return result; | 1149 return result; |
1149 } | 1150 } |
1150 return entry->second; | 1151 return entry->second; |
1151 } | 1152 } |
1152 | 1153 |
1153 SpvId SPIRVCodeGenerator::writeExpression(Expression& expr, std::ostream& out) { | 1154 SpvId SPIRVCodeGenerator::writeExpression(Expression& expr, std::ostream& out) { |
1154 switch (expr.fKind) { | 1155 switch (expr.fKind) { |
1155 case Expression::kBinary_Kind: | 1156 case Expression::kBinary_Kind: |
1156 return this->writeBinaryExpression((BinaryExpression&) expr, out); | 1157 return this->writeBinaryExpression((BinaryExpression&) expr, out); |
(...skipping 21 matching lines...) Expand all Loading... |
1178 return this->writeTernaryExpression((TernaryExpression&) expr, out); | 1179 return this->writeTernaryExpression((TernaryExpression&) expr, out); |
1179 case Expression::kIndex_Kind: | 1180 case Expression::kIndex_Kind: |
1180 return this->writeIndexExpression((IndexExpression&) expr, out); | 1181 return this->writeIndexExpression((IndexExpression&) expr, out); |
1181 default: | 1182 default: |
1182 ABORT("unsupported expression: %s", expr.description().c_str()); | 1183 ABORT("unsupported expression: %s", expr.description().c_str()); |
1183 } | 1184 } |
1184 return -1; | 1185 return -1; |
1185 } | 1186 } |
1186 | 1187 |
1187 SpvId SPIRVCodeGenerator::writeIntrinsicCall(FunctionCall& c, std::ostream& out)
{ | 1188 SpvId SPIRVCodeGenerator::writeIntrinsicCall(FunctionCall& c, std::ostream& out)
{ |
1188 auto intrinsic = fIntrinsicMap.find(c.fFunction->fName); | 1189 auto intrinsic = fIntrinsicMap.find(c.fFunction.fName); |
1189 ASSERT(intrinsic != fIntrinsicMap.end()); | 1190 ASSERT(intrinsic != fIntrinsicMap.end()); |
1190 std::shared_ptr<Type> type = c.fArguments[0]->fType; | 1191 const Type& type = c.fArguments[0]->fType; |
1191 int32_t intrinsicId; | 1192 int32_t intrinsicId; |
1192 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(*ty
pe)) { | 1193 if (std::get<0>(intrinsic->second) == kSpecial_IntrinsicKind || is_float(fCo
ntext, type)) { |
1193 intrinsicId = std::get<1>(intrinsic->second); | 1194 intrinsicId = std::get<1>(intrinsic->second); |
1194 } else if (is_signed(*type)) { | 1195 } else if (is_signed(fContext, type)) { |
1195 intrinsicId = std::get<2>(intrinsic->second); | 1196 intrinsicId = std::get<2>(intrinsic->second); |
1196 } else if (is_unsigned(*type)) { | 1197 } else if (is_unsigned(fContext, type)) { |
1197 intrinsicId = std::get<3>(intrinsic->second); | 1198 intrinsicId = std::get<3>(intrinsic->second); |
1198 } else if (is_bool(*type)) { | 1199 } else if (is_bool(fContext, type)) { |
1199 intrinsicId = std::get<4>(intrinsic->second); | 1200 intrinsicId = std::get<4>(intrinsic->second); |
1200 } else { | 1201 } else { |
1201 ABORT("invalid call %s, cannot operate on '%s'", c.description().c_str()
, | 1202 ABORT("invalid call %s, cannot operate on '%s'", c.description().c_str()
, |
1202 type->description().c_str()); | 1203 type.description().c_str()); |
1203 } | 1204 } |
1204 switch (std::get<0>(intrinsic->second)) { | 1205 switch (std::get<0>(intrinsic->second)) { |
1205 case kGLSL_STD_450_IntrinsicKind: { | 1206 case kGLSL_STD_450_IntrinsicKind: { |
1206 SpvId result = this->nextId(); | 1207 SpvId result = this->nextId(); |
1207 std::vector<SpvId> arguments; | 1208 std::vector<SpvId> arguments; |
1208 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1209 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1209 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); | 1210 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); |
1210 } | 1211 } |
1211 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out)
; | 1212 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out)
; |
1212 this->writeWord(this->getType(*c.fType), out); | 1213 this->writeWord(this->getType(c.fType), out); |
1213 this->writeWord(result, out); | 1214 this->writeWord(result, out); |
1214 this->writeWord(fGLSLExtendedInstructions, out); | 1215 this->writeWord(fGLSLExtendedInstructions, out); |
1215 this->writeWord(intrinsicId, out); | 1216 this->writeWord(intrinsicId, out); |
1216 for (SpvId id : arguments) { | 1217 for (SpvId id : arguments) { |
1217 this->writeWord(id, out); | 1218 this->writeWord(id, out); |
1218 } | 1219 } |
1219 return result; | 1220 return result; |
1220 } | 1221 } |
1221 case kSPIRV_IntrinsicKind: { | 1222 case kSPIRV_IntrinsicKind: { |
1222 SpvId result = this->nextId(); | 1223 SpvId result = this->nextId(); |
1223 std::vector<SpvId> arguments; | 1224 std::vector<SpvId> arguments; |
1224 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1225 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1225 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); | 1226 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); |
1226 } | 1227 } |
1227 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size
(), out); | 1228 this->writeOpCode((SpvOp_) intrinsicId, 3 + (int32_t) arguments.size
(), out); |
1228 this->writeWord(this->getType(*c.fType), out); | 1229 this->writeWord(this->getType(c.fType), out); |
1229 this->writeWord(result, out); | 1230 this->writeWord(result, out); |
1230 for (SpvId id : arguments) { | 1231 for (SpvId id : arguments) { |
1231 this->writeWord(id, out); | 1232 this->writeWord(id, out); |
1232 } | 1233 } |
1233 return result; | 1234 return result; |
1234 } | 1235 } |
1235 case kSpecial_IntrinsicKind: | 1236 case kSpecial_IntrinsicKind: |
1236 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId
, out); | 1237 return this->writeSpecialIntrinsic(c, (SpecialIntrinsic) intrinsicId
, out); |
1237 default: | 1238 default: |
1238 ABORT("unsupported intrinsic kind"); | 1239 ABORT("unsupported intrinsic kind"); |
1239 } | 1240 } |
1240 } | 1241 } |
1241 | 1242 |
1242 SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(FunctionCall& c, SpecialIntrinsi
c kind, | 1243 SpvId SPIRVCodeGenerator::writeSpecialIntrinsic(FunctionCall& c, SpecialIntrinsi
c kind, |
1243 std::ostream& out) { | 1244 std::ostream& out) { |
1244 SpvId result = this->nextId(); | 1245 SpvId result = this->nextId(); |
1245 switch (kind) { | 1246 switch (kind) { |
1246 case kAtan_SpecialIntrinsic: { | 1247 case kAtan_SpecialIntrinsic: { |
1247 std::vector<SpvId> arguments; | 1248 std::vector<SpvId> arguments; |
1248 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1249 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1249 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); | 1250 arguments.push_back(this->writeExpression(*c.fArguments[i], out)
); |
1250 } | 1251 } |
1251 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out)
; | 1252 this->writeOpCode(SpvOpExtInst, 5 + (int32_t) arguments.size(), out)
; |
1252 this->writeWord(this->getType(*c.fType), out); | 1253 this->writeWord(this->getType(c.fType), out); |
1253 this->writeWord(result, out); | 1254 this->writeWord(result, out); |
1254 this->writeWord(fGLSLExtendedInstructions, out); | 1255 this->writeWord(fGLSLExtendedInstructions, out); |
1255 this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450
Atan, out); | 1256 this->writeWord(arguments.size() == 2 ? GLSLstd450Atan2 : GLSLstd450
Atan, out); |
1256 for (SpvId id : arguments) { | 1257 for (SpvId id : arguments) { |
1257 this->writeWord(id, out); | 1258 this->writeWord(id, out); |
1258 } | 1259 } |
1259 return result; | 1260 return result; |
1260 } | 1261 } |
1261 case kTexture_SpecialIntrinsic: { | 1262 case kTexture_SpecialIntrinsic: { |
1262 SpvId type = this->getType(*c.fType); | 1263 SpvId type = this->getType(c.fType); |
1263 SpvId sampler = this->writeExpression(*c.fArguments[0], out); | 1264 SpvId sampler = this->writeExpression(*c.fArguments[0], out); |
1264 SpvId uv = this->writeExpression(*c.fArguments[1], out); | 1265 SpvId uv = this->writeExpression(*c.fArguments[1], out); |
1265 if (c.fArguments.size() == 3) { | 1266 if (c.fArguments.size() == 3) { |
1266 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, | 1267 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, |
1267 SpvImageOperandsBiasMask, | 1268 SpvImageOperandsBiasMask, |
1268 this->writeExpression(*c.fArguments[2], o
ut), | 1269 this->writeExpression(*c.fArguments[2], o
ut), |
1269 out); | 1270 out); |
1270 } else { | 1271 } else { |
1271 ASSERT(c.fArguments.size() == 2); | 1272 ASSERT(c.fArguments.size() == 2); |
1272 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, out); | 1273 this->writeInstruction(SpvOpImageSampleImplicitLod, type, result
, sampler, uv, out); |
1273 } | 1274 } |
1274 break; | 1275 break; |
1275 } | 1276 } |
1276 case kTextureProj_SpecialIntrinsic: { | 1277 case kTextureProj_SpecialIntrinsic: { |
1277 SpvId type = this->getType(*c.fType); | 1278 SpvId type = this->getType(c.fType); |
1278 SpvId sampler = this->writeExpression(*c.fArguments[0], out); | 1279 SpvId sampler = this->writeExpression(*c.fArguments[0], out); |
1279 SpvId uv = this->writeExpression(*c.fArguments[1], out); | 1280 SpvId uv = this->writeExpression(*c.fArguments[1], out); |
1280 if (c.fArguments.size() == 3) { | 1281 if (c.fArguments.size() == 3) { |
1281 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, | 1282 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
1282 SpvImageOperandsBiasMask, | 1283 SpvImageOperandsBiasMask, |
1283 this->writeExpression(*c.fArguments[2], o
ut), | 1284 this->writeExpression(*c.fArguments[2], o
ut), |
1284 out); | 1285 out); |
1285 } else { | 1286 } else { |
1286 ASSERT(c.fArguments.size() == 2); | 1287 ASSERT(c.fArguments.size() == 2); |
1287 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, | 1288 this->writeInstruction(SpvOpImageSampleProjImplicitLod, type, re
sult, sampler, uv, |
1288 out); | 1289 out); |
1289 } | 1290 } |
1290 break; | 1291 break; |
1291 } | 1292 } |
1292 case kTexture2D_SpecialIntrinsic: { | 1293 case kTexture2D_SpecialIntrinsic: { |
1293 SpvId img = this->writeExpression(*c.fArguments[0], out); | 1294 SpvId img = this->writeExpression(*c.fArguments[0], out); |
1294 SpvId coords = this->writeExpression(*c.fArguments[1], out); | 1295 SpvId coords = this->writeExpression(*c.fArguments[1], out); |
1295 this->writeInstruction(SpvOpImageSampleImplicitLod, | 1296 this->writeInstruction(SpvOpImageSampleImplicitLod, |
1296 this->getType(*c.fType), | 1297 this->getType(c.fType), |
1297 result, | 1298 result, |
1298 img, | 1299 img, |
1299 coords, | 1300 coords, |
1300 out); | 1301 out); |
1301 break; | 1302 break; |
1302 } | 1303 } |
1303 } | 1304 } |
1304 return result; | 1305 return result; |
1305 } | 1306 } |
1306 | 1307 |
1307 SpvId SPIRVCodeGenerator::writeFunctionCall(FunctionCall& c, std::ostream& out)
{ | 1308 SpvId SPIRVCodeGenerator::writeFunctionCall(FunctionCall& c, std::ostream& out)
{ |
1308 const auto& entry = fFunctionMap.find(c.fFunction); | 1309 const auto& entry = fFunctionMap.find(&c.fFunction); |
1309 if (entry == fFunctionMap.end()) { | 1310 if (entry == fFunctionMap.end()) { |
1310 return this->writeIntrinsicCall(c, out); | 1311 return this->writeIntrinsicCall(c, out); |
1311 } | 1312 } |
1312 // stores (variable, type, lvalue) pairs to extract and save after the funct
ion call is complete | 1313 // stores (variable, type, lvalue) pairs to extract and save after the funct
ion call is complete |
1313 std::vector<std::tuple<SpvId, SpvId, std::unique_ptr<LValue>>> lvalues; | 1314 std::vector<std::tuple<SpvId, SpvId, std::unique_ptr<LValue>>> lvalues; |
1314 std::vector<SpvId> arguments; | 1315 std::vector<SpvId> arguments; |
1315 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1316 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1316 // id of temporary variable that we will use to hold this argument, or 0
if it is being | 1317 // id of temporary variable that we will use to hold this argument, or 0
if it is being |
1317 // passed directly | 1318 // passed directly |
1318 SpvId tmpVar; | 1319 SpvId tmpVar; |
1319 // if we need a temporary var to store this argument, this is the value
to store in the var | 1320 // if we need a temporary var to store this argument, this is the value
to store in the var |
1320 SpvId tmpValueId; | 1321 SpvId tmpValueId; |
1321 if (is_out(c.fFunction->fParameters[i])) { | 1322 if (is_out(*c.fFunction.fParameters[i])) { |
1322 std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out); | 1323 std::unique_ptr<LValue> lv = this->getLValue(*c.fArguments[i], out); |
1323 SpvId ptr = lv->getPointer(); | 1324 SpvId ptr = lv->getPointer(); |
1324 if (ptr) { | 1325 if (ptr) { |
1325 arguments.push_back(ptr); | 1326 arguments.push_back(ptr); |
1326 continue; | 1327 continue; |
1327 } else { | 1328 } else { |
1328 // lvalue cannot simply be read and written via a pointer (e.g.
a swizzle). Need to | 1329 // lvalue cannot simply be read and written via a pointer (e.g.
a swizzle). Need to |
1329 // copy it into a temp, call the function, read the value out of
the temp, and then | 1330 // copy it into a temp, call the function, read the value out of
the temp, and then |
1330 // update the lvalue. | 1331 // update the lvalue. |
1331 tmpValueId = lv->load(out); | 1332 tmpValueId = lv->load(out); |
1332 tmpVar = this->nextId(); | 1333 tmpVar = this->nextId(); |
1333 lvalues.push_back(std::make_tuple(tmpVar, this->getType(*c.fArgu
ments[i]->fType), | 1334 lvalues.push_back(std::make_tuple(tmpVar, this->getType(c.fArgum
ents[i]->fType), |
1334 std::move(lv))); | 1335 std::move(lv))); |
1335 } | 1336 } |
1336 } else { | 1337 } else { |
1337 // see getFunctionType for an explanation of why we're always using
pointer parameters | 1338 // see getFunctionType for an explanation of why we're always using
pointer parameters |
1338 tmpValueId = this->writeExpression(*c.fArguments[i], out); | 1339 tmpValueId = this->writeExpression(*c.fArguments[i], out); |
1339 tmpVar = this->nextId(); | 1340 tmpVar = this->nextId(); |
1340 } | 1341 } |
1341 this->writeInstruction(SpvOpVariable, | 1342 this->writeInstruction(SpvOpVariable, |
1342 this->getPointerType(c.fArguments[i]->fType, | 1343 this->getPointerType(c.fArguments[i]->fType, |
1343 SpvStorageClassFunction), | 1344 SpvStorageClassFunction), |
1344 tmpVar, | 1345 tmpVar, |
1345 SpvStorageClassFunction, | 1346 SpvStorageClassFunction, |
1346 out); | 1347 fVariableBuffer); |
1347 this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out); | 1348 this->writeInstruction(SpvOpStore, tmpVar, tmpValueId, out); |
1348 arguments.push_back(tmpVar); | 1349 arguments.push_back(tmpVar); |
1349 } | 1350 } |
1350 SpvId result = this->nextId(); | 1351 SpvId result = this->nextId(); |
1351 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out)
; | 1352 this->writeOpCode(SpvOpFunctionCall, 4 + (int32_t) c.fArguments.size(), out)
; |
1352 this->writeWord(this->getType(*c.fType), out); | 1353 this->writeWord(this->getType(c.fType), out); |
1353 this->writeWord(result, out); | 1354 this->writeWord(result, out); |
1354 this->writeWord(entry->second, out); | 1355 this->writeWord(entry->second, out); |
1355 for (SpvId id : arguments) { | 1356 for (SpvId id : arguments) { |
1356 this->writeWord(id, out); | 1357 this->writeWord(id, out); |
1357 } | 1358 } |
1358 // now that the call is complete, we may need to update some lvalues with th
e new values of out | 1359 // now that the call is complete, we may need to update some lvalues with th
e new values of out |
1359 // arguments | 1360 // arguments |
1360 for (const auto& tuple : lvalues) { | 1361 for (const auto& tuple : lvalues) { |
1361 SpvId load = this->nextId(); | 1362 SpvId load = this->nextId(); |
1362 this->writeInstruction(SpvOpLoad, std::get<1>(tuple), load, std::get<0>(
tuple), out); | 1363 this->writeInstruction(SpvOpLoad, std::get<1>(tuple), load, std::get<0>(
tuple), out); |
1363 std::get<2>(tuple)->store(load, out); | 1364 std::get<2>(tuple)->store(load, out); |
1364 } | 1365 } |
1365 return result; | 1366 return result; |
1366 } | 1367 } |
1367 | 1368 |
1368 SpvId SPIRVCodeGenerator::writeConstantVector(Constructor& c) { | 1369 SpvId SPIRVCodeGenerator::writeConstantVector(Constructor& c) { |
1369 ASSERT(c.fType->kind() == Type::kVector_Kind && c.isConstant()); | 1370 ASSERT(c.fType.kind() == Type::kVector_Kind && c.isConstant()); |
1370 SpvId result = this->nextId(); | 1371 SpvId result = this->nextId(); |
1371 std::vector<SpvId> arguments; | 1372 std::vector<SpvId> arguments; |
1372 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1373 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1373 arguments.push_back(this->writeExpression(*c.fArguments[i], fConstantBuf
fer)); | 1374 arguments.push_back(this->writeExpression(*c.fArguments[i], fConstantBuf
fer)); |
1374 } | 1375 } |
1375 SpvId type = this->getType(*c.fType); | 1376 SpvId type = this->getType(c.fType); |
1376 if (c.fArguments.size() == 1) { | 1377 if (c.fArguments.size() == 1) { |
1377 // with a single argument, a vector will have all of its entries equal t
o the argument | 1378 // with a single argument, a vector will have all of its entries equal t
o the argument |
1378 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType->columns(), fConst
antBuffer); | 1379 this->writeOpCode(SpvOpConstantComposite, 3 + c.fType.columns(), fConsta
ntBuffer); |
1379 this->writeWord(type, fConstantBuffer); | 1380 this->writeWord(type, fConstantBuffer); |
1380 this->writeWord(result, fConstantBuffer); | 1381 this->writeWord(result, fConstantBuffer); |
1381 for (int i = 0; i < c.fType->columns(); i++) { | 1382 for (int i = 0; i < c.fType.columns(); i++) { |
1382 this->writeWord(arguments[0], fConstantBuffer); | 1383 this->writeWord(arguments[0], fConstantBuffer); |
1383 } | 1384 } |
1384 } else { | 1385 } else { |
1385 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fArguments.siz
e(), | 1386 this->writeOpCode(SpvOpConstantComposite, 3 + (int32_t) c.fArguments.siz
e(), |
1386 fConstantBuffer); | 1387 fConstantBuffer); |
1387 this->writeWord(type, fConstantBuffer); | 1388 this->writeWord(type, fConstantBuffer); |
1388 this->writeWord(result, fConstantBuffer); | 1389 this->writeWord(result, fConstantBuffer); |
1389 for (SpvId id : arguments) { | 1390 for (SpvId id : arguments) { |
1390 this->writeWord(id, fConstantBuffer); | 1391 this->writeWord(id, fConstantBuffer); |
1391 } | 1392 } |
1392 } | 1393 } |
1393 return result; | 1394 return result; |
1394 } | 1395 } |
1395 | 1396 |
1396 SpvId SPIRVCodeGenerator::writeFloatConstructor(Constructor& c, std::ostream& ou
t) { | 1397 SpvId SPIRVCodeGenerator::writeFloatConstructor(Constructor& c, std::ostream& ou
t) { |
1397 ASSERT(c.fType == kFloat_Type); | 1398 ASSERT(c.fType == *fContext.fFloat_Type); |
1398 ASSERT(c.fArguments.size() == 1); | 1399 ASSERT(c.fArguments.size() == 1); |
1399 ASSERT(c.fArguments[0]->fType->isNumber()); | 1400 ASSERT(c.fArguments[0]->fType.isNumber()); |
1400 SpvId result = this->nextId(); | 1401 SpvId result = this->nextId(); |
1401 SpvId parameter = this->writeExpression(*c.fArguments[0], out); | 1402 SpvId parameter = this->writeExpression(*c.fArguments[0], out); |
1402 if (c.fArguments[0]->fType == kInt_Type) { | 1403 if (c.fArguments[0]->fType == *fContext.fInt_Type) { |
1403 this->writeInstruction(SpvOpConvertSToF, this->getType(*c.fType), result
, parameter, | 1404 this->writeInstruction(SpvOpConvertSToF, this->getType(c.fType), result,
parameter, |
1404 out); | 1405 out); |
1405 } else if (c.fArguments[0]->fType == kUInt_Type) { | 1406 } else if (c.fArguments[0]->fType == *fContext.fUInt_Type) { |
1406 this->writeInstruction(SpvOpConvertUToF, this->getType(*c.fType), result
, parameter, | 1407 this->writeInstruction(SpvOpConvertUToF, this->getType(c.fType), result,
parameter, |
1407 out); | 1408 out); |
1408 } else if (c.fArguments[0]->fType == kFloat_Type) { | 1409 } else if (c.fArguments[0]->fType == *fContext.fFloat_Type) { |
1409 return parameter; | 1410 return parameter; |
1410 } | 1411 } |
1411 return result; | 1412 return result; |
1412 } | 1413 } |
1413 | 1414 |
1414 SpvId SPIRVCodeGenerator::writeIntConstructor(Constructor& c, std::ostream& out)
{ | 1415 SpvId SPIRVCodeGenerator::writeIntConstructor(Constructor& c, std::ostream& out)
{ |
1415 ASSERT(c.fType == kInt_Type); | 1416 ASSERT(c.fType == *fContext.fInt_Type); |
1416 ASSERT(c.fArguments.size() == 1); | 1417 ASSERT(c.fArguments.size() == 1); |
1417 ASSERT(c.fArguments[0]->fType->isNumber()); | 1418 ASSERT(c.fArguments[0]->fType.isNumber()); |
1418 SpvId result = this->nextId(); | 1419 SpvId result = this->nextId(); |
1419 SpvId parameter = this->writeExpression(*c.fArguments[0], out); | 1420 SpvId parameter = this->writeExpression(*c.fArguments[0], out); |
1420 if (c.fArguments[0]->fType == kFloat_Type) { | 1421 if (c.fArguments[0]->fType == *fContext.fFloat_Type) { |
1421 this->writeInstruction(SpvOpConvertFToS, this->getType(*c.fType), result
, parameter, | 1422 this->writeInstruction(SpvOpConvertFToS, this->getType(c.fType), result,
parameter, |
1422 out); | 1423 out); |
1423 } else if (c.fArguments[0]->fType == kUInt_Type) { | 1424 } else if (c.fArguments[0]->fType == *fContext.fUInt_Type) { |
1424 this->writeInstruction(SpvOpSatConvertUToS, this->getType(*c.fType), res
ult, parameter, | 1425 this->writeInstruction(SpvOpSatConvertUToS, this->getType(c.fType), resu
lt, parameter, |
1425 out); | 1426 out); |
1426 } else if (c.fArguments[0]->fType == kInt_Type) { | 1427 } else if (c.fArguments[0]->fType == *fContext.fInt_Type) { |
1427 return parameter; | 1428 return parameter; |
1428 } | 1429 } |
1429 return result; | 1430 return result; |
1430 } | 1431 } |
1431 | 1432 |
1432 SpvId SPIRVCodeGenerator::writeMatrixConstructor(Constructor& c, std::ostream& o
ut) { | 1433 SpvId SPIRVCodeGenerator::writeMatrixConstructor(Constructor& c, std::ostream& o
ut) { |
1433 ASSERT(c.fType->kind() == Type::kMatrix_Kind); | 1434 ASSERT(c.fType.kind() == Type::kMatrix_Kind); |
1434 // go ahead and write the arguments so we don't try to write new instruction
s in the middle of | 1435 // go ahead and write the arguments so we don't try to write new instruction
s in the middle of |
1435 // an instruction | 1436 // an instruction |
1436 std::vector<SpvId> arguments; | 1437 std::vector<SpvId> arguments; |
1437 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1438 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1438 arguments.push_back(this->writeExpression(*c.fArguments[i], out)); | 1439 arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
1439 } | 1440 } |
1440 SpvId result = this->nextId(); | 1441 SpvId result = this->nextId(); |
1441 int rows = c.fType->rows(); | 1442 int rows = c.fType.rows(); |
1442 int columns = c.fType->columns(); | 1443 int columns = c.fType.columns(); |
1443 // FIXME this won't work to create a matrix from another matrix | 1444 // FIXME this won't work to create a matrix from another matrix |
1444 if (arguments.size() == 1) { | 1445 if (arguments.size() == 1) { |
1445 // with a single argument, a matrix will have all of its diagonal entrie
s equal to the | 1446 // with a single argument, a matrix will have all of its diagonal entrie
s equal to the |
1446 // argument and its other values equal to zero | 1447 // argument and its other values equal to zero |
1447 // FIXME this won't work for int matrices | 1448 // FIXME this won't work for int matrices |
1448 FloatLiteral zero(Position(), 0); | 1449 FloatLiteral zero(fContext, Position(), 0); |
1449 SpvId zeroId = this->writeFloatLiteral(zero); | 1450 SpvId zeroId = this->writeFloatLiteral(zero); |
1450 std::vector<SpvId> columnIds; | 1451 std::vector<SpvId> columnIds; |
1451 for (int column = 0; column < columns; column++) { | 1452 for (int column = 0; column < columns; column++) { |
1452 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows(), | 1453 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.rows(), |
1453 out); | 1454 out); |
1454 this->writeWord(this->getType(*c.fType->componentType()->toCompound(
rows, 1)), out); | 1455 this->writeWord(this->getType(c.fType.componentType().toCompound(fCo
ntext, rows, 1)), |
| 1456 out); |
1455 SpvId columnId = this->nextId(); | 1457 SpvId columnId = this->nextId(); |
1456 this->writeWord(columnId, out); | 1458 this->writeWord(columnId, out); |
1457 columnIds.push_back(columnId); | 1459 columnIds.push_back(columnId); |
1458 for (int row = 0; row < c.fType->columns(); row++) { | 1460 for (int row = 0; row < c.fType.columns(); row++) { |
1459 this->writeWord(row == column ? arguments[0] : zeroId, out); | 1461 this->writeWord(row == column ? arguments[0] : zeroId, out); |
1460 } | 1462 } |
1461 } | 1463 } |
1462 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, | 1464 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, |
1463 out); | 1465 out); |
1464 this->writeWord(this->getType(*c.fType), out); | 1466 this->writeWord(this->getType(c.fType), out); |
1465 this->writeWord(result, out); | 1467 this->writeWord(result, out); |
1466 for (SpvId id : columnIds) { | 1468 for (SpvId id : columnIds) { |
1467 this->writeWord(id, out); | 1469 this->writeWord(id, out); |
1468 } | 1470 } |
1469 } else { | 1471 } else { |
1470 std::vector<SpvId> columnIds; | 1472 std::vector<SpvId> columnIds; |
1471 int currentCount = 0; | 1473 int currentCount = 0; |
1472 for (size_t i = 0; i < arguments.size(); i++) { | 1474 for (size_t i = 0; i < arguments.size(); i++) { |
1473 if (c.fArguments[i]->fType->kind() == Type::kVector_Kind) { | 1475 if (c.fArguments[i]->fType.kind() == Type::kVector_Kind) { |
1474 ASSERT(currentCount == 0); | 1476 ASSERT(currentCount == 0); |
1475 columnIds.push_back(arguments[i]); | 1477 columnIds.push_back(arguments[i]); |
1476 currentCount = 0; | 1478 currentCount = 0; |
1477 } else { | 1479 } else { |
1478 ASSERT(c.fArguments[i]->fType->kind() == Type::kScalar_Kind); | 1480 ASSERT(c.fArguments[i]->fType.kind() == Type::kScalar_Kind); |
1479 if (currentCount == 0) { | 1481 if (currentCount == 0) { |
1480 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->rows
(), out); | 1482 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.rows(
), out); |
1481 this->writeWord(this->getType(*c.fType->componentType()->toC
ompound(rows, 1)), | 1483 this->writeWord(this->getType(c.fType.componentType().toComp
ound(fContext, rows, |
| 1484
1)), |
1482 out); | 1485 out); |
1483 SpvId id = this->nextId(); | 1486 SpvId id = this->nextId(); |
1484 this->writeWord(id, out); | 1487 this->writeWord(id, out); |
1485 columnIds.push_back(id); | 1488 columnIds.push_back(id); |
1486 } | 1489 } |
1487 this->writeWord(arguments[i], out); | 1490 this->writeWord(arguments[i], out); |
1488 currentCount = (currentCount + 1) % rows; | 1491 currentCount = (currentCount + 1) % rows; |
1489 } | 1492 } |
1490 } | 1493 } |
1491 ASSERT(columnIds.size() == (size_t) columns); | 1494 ASSERT(columnIds.size() == (size_t) columns); |
1492 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); | 1495 this->writeOpCode(SpvOpCompositeConstruct, 3 + columns, out); |
1493 this->writeWord(this->getType(*c.fType), out); | 1496 this->writeWord(this->getType(c.fType), out); |
1494 this->writeWord(result, out); | 1497 this->writeWord(result, out); |
1495 for (SpvId id : columnIds) { | 1498 for (SpvId id : columnIds) { |
1496 this->writeWord(id, out); | 1499 this->writeWord(id, out); |
1497 } | 1500 } |
1498 } | 1501 } |
1499 return result; | 1502 return result; |
1500 } | 1503 } |
1501 | 1504 |
1502 SpvId SPIRVCodeGenerator::writeVectorConstructor(Constructor& c, std::ostream& o
ut) { | 1505 SpvId SPIRVCodeGenerator::writeVectorConstructor(Constructor& c, std::ostream& o
ut) { |
1503 ASSERT(c.fType->kind() == Type::kVector_Kind); | 1506 ASSERT(c.fType.kind() == Type::kVector_Kind); |
1504 if (c.isConstant()) { | 1507 if (c.isConstant()) { |
1505 return this->writeConstantVector(c); | 1508 return this->writeConstantVector(c); |
1506 } | 1509 } |
1507 // go ahead and write the arguments so we don't try to write new instruction
s in the middle of | 1510 // go ahead and write the arguments so we don't try to write new instruction
s in the middle of |
1508 // an instruction | 1511 // an instruction |
1509 std::vector<SpvId> arguments; | 1512 std::vector<SpvId> arguments; |
1510 for (size_t i = 0; i < c.fArguments.size(); i++) { | 1513 for (size_t i = 0; i < c.fArguments.size(); i++) { |
1511 arguments.push_back(this->writeExpression(*c.fArguments[i], out)); | 1514 arguments.push_back(this->writeExpression(*c.fArguments[i], out)); |
1512 } | 1515 } |
1513 SpvId result = this->nextId(); | 1516 SpvId result = this->nextId(); |
1514 if (arguments.size() == 1 && c.fArguments[0]->fType->kind() == Type::kScalar
_Kind) { | 1517 if (arguments.size() == 1 && c.fArguments[0]->fType.kind() == Type::kScalar_
Kind) { |
1515 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType->columns(), out); | 1518 this->writeOpCode(SpvOpCompositeConstruct, 3 + c.fType.columns(), out); |
1516 this->writeWord(this->getType(*c.fType), out); | 1519 this->writeWord(this->getType(c.fType), out); |
1517 this->writeWord(result, out); | 1520 this->writeWord(result, out); |
1518 for (int i = 0; i < c.fType->columns(); i++) { | 1521 for (int i = 0; i < c.fType.columns(); i++) { |
1519 this->writeWord(arguments[0], out); | 1522 this->writeWord(arguments[0], out); |
1520 } | 1523 } |
1521 } else { | 1524 } else { |
1522 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fArguments.si
ze(), out); | 1525 this->writeOpCode(SpvOpCompositeConstruct, 3 + (int32_t) c.fArguments.si
ze(), out); |
1523 this->writeWord(this->getType(*c.fType), out); | 1526 this->writeWord(this->getType(c.fType), out); |
1524 this->writeWord(result, out); | 1527 this->writeWord(result, out); |
1525 for (SpvId id : arguments) { | 1528 for (SpvId id : arguments) { |
1526 this->writeWord(id, out); | 1529 this->writeWord(id, out); |
1527 } | 1530 } |
1528 } | 1531 } |
1529 return result; | 1532 return result; |
1530 } | 1533 } |
1531 | 1534 |
1532 SpvId SPIRVCodeGenerator::writeConstructor(Constructor& c, std::ostream& out) { | 1535 SpvId SPIRVCodeGenerator::writeConstructor(Constructor& c, std::ostream& out) { |
1533 if (c.fType == kFloat_Type) { | 1536 if (c.fType == *fContext.fFloat_Type) { |
1534 return this->writeFloatConstructor(c, out); | 1537 return this->writeFloatConstructor(c, out); |
1535 } else if (c.fType == kInt_Type) { | 1538 } else if (c.fType == *fContext.fInt_Type) { |
1536 return this->writeIntConstructor(c, out); | 1539 return this->writeIntConstructor(c, out); |
1537 } | 1540 } |
1538 switch (c.fType->kind()) { | 1541 switch (c.fType.kind()) { |
1539 case Type::kVector_Kind: | 1542 case Type::kVector_Kind: |
1540 return this->writeVectorConstructor(c, out); | 1543 return this->writeVectorConstructor(c, out); |
1541 case Type::kMatrix_Kind: | 1544 case Type::kMatrix_Kind: |
1542 return this->writeMatrixConstructor(c, out); | 1545 return this->writeMatrixConstructor(c, out); |
1543 default: | 1546 default: |
1544 ABORT("unsupported constructor: %s", c.description().c_str()); | 1547 ABORT("unsupported constructor: %s", c.description().c_str()); |
1545 } | 1548 } |
1546 } | 1549 } |
1547 | 1550 |
1548 SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { | 1551 SpvStorageClass_ get_storage_class(const Modifiers& modifiers) { |
1549 if (modifiers.fFlags & Modifiers::kIn_Flag) { | 1552 if (modifiers.fFlags & Modifiers::kIn_Flag) { |
1550 return SpvStorageClassInput; | 1553 return SpvStorageClassInput; |
1551 } else if (modifiers.fFlags & Modifiers::kOut_Flag) { | 1554 } else if (modifiers.fFlags & Modifiers::kOut_Flag) { |
1552 return SpvStorageClassOutput; | 1555 return SpvStorageClassOutput; |
1553 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) { | 1556 } else if (modifiers.fFlags & Modifiers::kUniform_Flag) { |
1554 return SpvStorageClassUniform; | 1557 return SpvStorageClassUniform; |
1555 } else { | 1558 } else { |
1556 return SpvStorageClassFunction; | 1559 return SpvStorageClassFunction; |
1557 } | 1560 } |
1558 } | 1561 } |
1559 | 1562 |
1560 SpvStorageClass_ get_storage_class(Expression& expr) { | 1563 SpvStorageClass_ get_storage_class(Expression& expr) { |
1561 switch (expr.fKind) { | 1564 switch (expr.fKind) { |
1562 case Expression::kVariableReference_Kind: | 1565 case Expression::kVariableReference_Kind: |
1563 return get_storage_class(((VariableReference&) expr).fVariable->fMod
ifiers); | 1566 return get_storage_class(((VariableReference&) expr).fVariable.fModi
fiers); |
1564 case Expression::kFieldAccess_Kind: | 1567 case Expression::kFieldAccess_Kind: |
1565 return get_storage_class(*((FieldAccess&) expr).fBase); | 1568 return get_storage_class(*((FieldAccess&) expr).fBase); |
1566 case Expression::kIndex_Kind: | 1569 case Expression::kIndex_Kind: |
1567 return get_storage_class(*((IndexExpression&) expr).fBase); | 1570 return get_storage_class(*((IndexExpression&) expr).fBase); |
1568 default: | 1571 default: |
1569 return SpvStorageClassFunction; | 1572 return SpvStorageClassFunction; |
1570 } | 1573 } |
1571 } | 1574 } |
1572 | 1575 |
1573 std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(Expression& expr, std::ost
ream& out) { | 1576 std::vector<SpvId> SPIRVCodeGenerator::getAccessChain(Expression& expr, std::ost
ream& out) { |
1574 std::vector<SpvId> chain; | 1577 std::vector<SpvId> chain; |
1575 switch (expr.fKind) { | 1578 switch (expr.fKind) { |
1576 case Expression::kIndex_Kind: { | 1579 case Expression::kIndex_Kind: { |
1577 IndexExpression& indexExpr = (IndexExpression&) expr; | 1580 IndexExpression& indexExpr = (IndexExpression&) expr; |
1578 chain = this->getAccessChain(*indexExpr.fBase, out); | 1581 chain = this->getAccessChain(*indexExpr.fBase, out); |
1579 chain.push_back(this->writeExpression(*indexExpr.fIndex, out)); | 1582 chain.push_back(this->writeExpression(*indexExpr.fIndex, out)); |
1580 break; | 1583 break; |
1581 } | 1584 } |
1582 case Expression::kFieldAccess_Kind: { | 1585 case Expression::kFieldAccess_Kind: { |
1583 FieldAccess& fieldExpr = (FieldAccess&) expr; | 1586 FieldAccess& fieldExpr = (FieldAccess&) expr; |
1584 chain = this->getAccessChain(*fieldExpr.fBase, out); | 1587 chain = this->getAccessChain(*fieldExpr.fBase, out); |
1585 IntLiteral index(Position(), fieldExpr.fFieldIndex); | 1588 IntLiteral index(fContext, Position(), fieldExpr.fFieldIndex); |
1586 chain.push_back(this->writeIntLiteral(index)); | 1589 chain.push_back(this->writeIntLiteral(index)); |
1587 break; | 1590 break; |
1588 } | 1591 } |
1589 default: | 1592 default: |
1590 chain.push_back(this->getLValue(expr, out)->getPointer()); | 1593 chain.push_back(this->getLValue(expr, out)->getPointer()); |
1591 } | 1594 } |
1592 return chain; | 1595 return chain; |
1593 } | 1596 } |
1594 | 1597 |
1595 class PointerLValue : public SPIRVCodeGenerator::LValue { | 1598 class PointerLValue : public SPIRVCodeGenerator::LValue { |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1691 const SpvId fVecPointer; | 1694 const SpvId fVecPointer; |
1692 const std::vector<int>& fComponents; | 1695 const std::vector<int>& fComponents; |
1693 const Type& fBaseType; | 1696 const Type& fBaseType; |
1694 const Type& fSwizzleType; | 1697 const Type& fSwizzleType; |
1695 }; | 1698 }; |
1696 | 1699 |
1697 std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(Expres
sion& expr, | 1700 std::unique_ptr<SPIRVCodeGenerator::LValue> SPIRVCodeGenerator::getLValue(Expres
sion& expr, |
1698 std::o
stream& out) { | 1701 std::o
stream& out) { |
1699 switch (expr.fKind) { | 1702 switch (expr.fKind) { |
1700 case Expression::kVariableReference_Kind: { | 1703 case Expression::kVariableReference_Kind: { |
1701 std::shared_ptr<Variable> var = ((VariableReference&) expr).fVariabl
e; | 1704 const Variable& var = ((VariableReference&) expr).fVariable; |
1702 auto entry = fVariableMap.find(var); | 1705 auto entry = fVariableMap.find(&var); |
1703 ASSERT(entry != fVariableMap.end()); | 1706 ASSERT(entry != fVariableMap.end()); |
1704 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( | 1707 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( |
1705 *this, | 1708 *this, |
1706 entry->se
cond, | 1709 entry->se
cond, |
1707 this->get
Type(*expr.fType))); | 1710 this->get
Type(expr.fType))); |
1708 } | 1711 } |
1709 case Expression::kIndex_Kind: // fall through | 1712 case Expression::kIndex_Kind: // fall through |
1710 case Expression::kFieldAccess_Kind: { | 1713 case Expression::kFieldAccess_Kind: { |
1711 std::vector<SpvId> chain = this->getAccessChain(expr, out); | 1714 std::vector<SpvId> chain = this->getAccessChain(expr, out); |
1712 SpvId member = this->nextId(); | 1715 SpvId member = this->nextId(); |
1713 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out)
; | 1716 this->writeOpCode(SpvOpAccessChain, (SpvId) (3 + chain.size()), out)
; |
1714 this->writeWord(this->getPointerType(expr.fType, get_storage_class(e
xpr)), out); | 1717 this->writeWord(this->getPointerType(expr.fType, get_storage_class(e
xpr)), out); |
1715 this->writeWord(member, out); | 1718 this->writeWord(member, out); |
1716 for (SpvId idx : chain) { | 1719 for (SpvId idx : chain) { |
1717 this->writeWord(idx, out); | 1720 this->writeWord(idx, out); |
1718 } | 1721 } |
1719 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( | 1722 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( |
1720 *this, | 1723 *this, |
1721 member, | 1724 member, |
1722 this->get
Type(*expr.fType))); | 1725 this->get
Type(expr.fType))); |
1723 } | 1726 } |
1724 | 1727 |
1725 case Expression::kSwizzle_Kind: { | 1728 case Expression::kSwizzle_Kind: { |
1726 Swizzle& swizzle = (Swizzle&) expr; | 1729 Swizzle& swizzle = (Swizzle&) expr; |
1727 size_t count = swizzle.fComponents.size(); | 1730 size_t count = swizzle.fComponents.size(); |
1728 SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer(); | 1731 SpvId base = this->getLValue(*swizzle.fBase, out)->getPointer(); |
1729 ASSERT(base); | 1732 ASSERT(base); |
1730 if (count == 1) { | 1733 if (count == 1) { |
1731 IntLiteral index(Position(), swizzle.fComponents[0]); | 1734 IntLiteral index(fContext, Position(), swizzle.fComponents[0]); |
1732 SpvId member = this->nextId(); | 1735 SpvId member = this->nextId(); |
1733 this->writeInstruction(SpvOpAccessChain, | 1736 this->writeInstruction(SpvOpAccessChain, |
1734 this->getPointerType(swizzle.fType, | 1737 this->getPointerType(swizzle.fType, |
1735 get_storage_class(*s
wizzle.fBase)), | 1738 get_storage_class(*s
wizzle.fBase)), |
1736 member, | 1739 member, |
1737 base, | 1740 base, |
1738 this->writeIntLiteral(index), | 1741 this->writeIntLiteral(index), |
1739 out); | 1742 out); |
1740 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLV
alue( | 1743 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLV
alue( |
1741 *this, | 1744 *this, |
1742 member, | 1745 member, |
1743 this->get
Type(*expr.fType))); | 1746 this->get
Type(expr.fType))); |
1744 } else { | 1747 } else { |
1745 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLV
alue( | 1748 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new SwizzleLV
alue( |
1746 *t
his, | 1749 *t
his, |
1747 ba
se, | 1750 ba
se, |
1748 sw
izzle.fComponents, | 1751 sw
izzle.fComponents, |
1749 *s
wizzle.fBase->fType, | 1752 sw
izzle.fBase->fType, |
1750 *e
xpr.fType)); | 1753 ex
pr.fType)); |
1751 } | 1754 } |
1752 } | 1755 } |
1753 | 1756 |
1754 default: | 1757 default: |
1755 // expr isn't actually an lvalue, create a dummy variable for it. Th
is case happens due | 1758 // expr isn't actually an lvalue, create a dummy variable for it. Th
is case happens due |
1756 // to the need to store values in temporary variables during functio
n calls (see | 1759 // to the need to store values in temporary variables during functio
n calls (see |
1757 // comments in getFunctionType); erroneous uses of rvalues as lvalue
s should have been | 1760 // comments in getFunctionType); erroneous uses of rvalues as lvalue
s should have been |
1758 // caught by IRGenerator | 1761 // caught by IRGenerator |
1759 SpvId result = this->nextId(); | 1762 SpvId result = this->nextId(); |
1760 SpvId type = this->getPointerType(expr.fType, SpvStorageClassFunctio
n); | 1763 SpvId type = this->getPointerType(expr.fType, SpvStorageClassFunctio
n); |
1761 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassF
unction, out); | 1764 this->writeInstruction(SpvOpVariable, type, result, SpvStorageClassF
unction, |
| 1765 fVariableBuffer); |
1762 this->writeInstruction(SpvOpStore, result, this->writeExpression(exp
r, out), out); | 1766 this->writeInstruction(SpvOpStore, result, this->writeExpression(exp
r, out), out); |
1763 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( | 1767 return std::unique_ptr<SPIRVCodeGenerator::LValue>(new PointerLValue
( |
1764 *this, | 1768 *this, |
1765 result, | 1769 result, |
1766 this->get
Type(*expr.fType))); | 1770 this->get
Type(expr.fType))); |
1767 } | 1771 } |
1768 } | 1772 } |
1769 | 1773 |
1770 SpvId SPIRVCodeGenerator::writeVariableReference(VariableReference& ref, std::os
tream& out) { | 1774 SpvId SPIRVCodeGenerator::writeVariableReference(VariableReference& ref, std::os
tream& out) { |
1771 auto entry = fVariableMap.find(ref.fVariable); | 1775 auto entry = fVariableMap.find(&ref.fVariable); |
1772 ASSERT(entry != fVariableMap.end()); | 1776 ASSERT(entry != fVariableMap.end()); |
1773 SpvId var = entry->second; | 1777 SpvId var = entry->second; |
1774 SpvId result = this->nextId(); | 1778 SpvId result = this->nextId(); |
1775 this->writeInstruction(SpvOpLoad, this->getType(*ref.fVariable->fType), resu
lt, var, out); | 1779 this->writeInstruction(SpvOpLoad, this->getType(ref.fVariable.fType), result
, var, out); |
1776 return result; | 1780 return result; |
1777 } | 1781 } |
1778 | 1782 |
1779 SpvId SPIRVCodeGenerator::writeIndexExpression(IndexExpression& expr, std::ostre
am& out) { | 1783 SpvId SPIRVCodeGenerator::writeIndexExpression(IndexExpression& expr, std::ostre
am& out) { |
1780 return getLValue(expr, out)->load(out); | 1784 return getLValue(expr, out)->load(out); |
1781 } | 1785 } |
1782 | 1786 |
1783 SpvId SPIRVCodeGenerator::writeFieldAccess(FieldAccess& f, std::ostream& out) { | 1787 SpvId SPIRVCodeGenerator::writeFieldAccess(FieldAccess& f, std::ostream& out) { |
1784 return getLValue(f, out)->load(out); | 1788 return getLValue(f, out)->load(out); |
1785 } | 1789 } |
1786 | 1790 |
1787 SpvId SPIRVCodeGenerator::writeSwizzle(Swizzle& swizzle, std::ostream& out) { | 1791 SpvId SPIRVCodeGenerator::writeSwizzle(Swizzle& swizzle, std::ostream& out) { |
1788 SpvId base = this->writeExpression(*swizzle.fBase, out); | 1792 SpvId base = this->writeExpression(*swizzle.fBase, out); |
1789 SpvId result = this->nextId(); | 1793 SpvId result = this->nextId(); |
1790 size_t count = swizzle.fComponents.size(); | 1794 size_t count = swizzle.fComponents.size(); |
1791 if (count == 1) { | 1795 if (count == 1) { |
1792 this->writeInstruction(SpvOpCompositeExtract, this->getType(*swizzle.fTy
pe), result, base, | 1796 this->writeInstruction(SpvOpCompositeExtract, this->getType(swizzle.fTyp
e), result, base, |
1793 swizzle.fComponents[0], out); | 1797 swizzle.fComponents[0], out); |
1794 } else { | 1798 } else { |
1795 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); | 1799 this->writeOpCode(SpvOpVectorShuffle, 5 + (int32_t) count, out); |
1796 this->writeWord(this->getType(*swizzle.fType), out); | 1800 this->writeWord(this->getType(swizzle.fType), out); |
1797 this->writeWord(result, out); | 1801 this->writeWord(result, out); |
1798 this->writeWord(base, out); | 1802 this->writeWord(base, out); |
1799 this->writeWord(base, out); | 1803 this->writeWord(base, out); |
1800 for (int component : swizzle.fComponents) { | 1804 for (int component : swizzle.fComponents) { |
1801 this->writeWord(component, out); | 1805 this->writeWord(component, out); |
1802 } | 1806 } |
1803 } | 1807 } |
1804 return result; | 1808 return result; |
1805 } | 1809 } |
1806 | 1810 |
1807 SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType, | 1811 SpvId SPIRVCodeGenerator::writeBinaryOperation(const Type& resultType, |
1808 const Type& operandType, SpvId lh
s, | 1812 const Type& operandType, SpvId lh
s, |
1809 SpvId rhs, SpvOp_ ifFloat, SpvOp_
ifInt, | 1813 SpvId rhs, SpvOp_ ifFloat, SpvOp_
ifInt, |
1810 SpvOp_ ifUInt, SpvOp_ ifBool, std
::ostream& out) { | 1814 SpvOp_ ifUInt, SpvOp_ ifBool, std
::ostream& out) { |
1811 SpvId result = this->nextId(); | 1815 SpvId result = this->nextId(); |
1812 if (is_float(operandType)) { | 1816 if (is_float(fContext, operandType)) { |
1813 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs,
rhs, out); | 1817 this->writeInstruction(ifFloat, this->getType(resultType), result, lhs,
rhs, out); |
1814 } else if (is_signed(operandType)) { | 1818 } else if (is_signed(fContext, operandType)) { |
1815 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rh
s, out); | 1819 this->writeInstruction(ifInt, this->getType(resultType), result, lhs, rh
s, out); |
1816 } else if (is_unsigned(operandType)) { | 1820 } else if (is_unsigned(fContext, operandType)) { |
1817 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, r
hs, out); | 1821 this->writeInstruction(ifUInt, this->getType(resultType), result, lhs, r
hs, out); |
1818 } else if (operandType == *kBool_Type) { | 1822 } else if (operandType == *fContext.fBool_Type) { |
1819 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, r
hs, out); | 1823 this->writeInstruction(ifBool, this->getType(resultType), result, lhs, r
hs, out); |
1820 } else { | 1824 } else { |
1821 ABORT("invalid operandType: %s", operandType.description().c_str()); | 1825 ABORT("invalid operandType: %s", operandType.description().c_str()); |
1822 } | 1826 } |
1823 return result; | 1827 return result; |
1824 } | 1828 } |
1825 | 1829 |
1826 bool is_assignment(Token::Kind op) { | 1830 bool is_assignment(Token::Kind op) { |
1827 switch (op) { | 1831 switch (op) { |
1828 case Token::EQ: // fall through | 1832 case Token::EQ: // fall through |
(...skipping 26 matching lines...) Expand all Loading... |
1855 } | 1859 } |
1856 case Token::LOGICALAND: | 1860 case Token::LOGICALAND: |
1857 return this->writeLogicalAnd(b, out); | 1861 return this->writeLogicalAnd(b, out); |
1858 case Token::LOGICALOR: | 1862 case Token::LOGICALOR: |
1859 return this->writeLogicalOr(b, out); | 1863 return this->writeLogicalOr(b, out); |
1860 default: | 1864 default: |
1861 break; | 1865 break; |
1862 } | 1866 } |
1863 | 1867 |
1864 // "normal" operators | 1868 // "normal" operators |
1865 const Type& resultType = *b.fType; | 1869 const Type& resultType = b.fType; |
1866 std::unique_ptr<LValue> lvalue; | 1870 std::unique_ptr<LValue> lvalue; |
1867 SpvId lhs; | 1871 SpvId lhs; |
1868 if (is_assignment(b.fOperator)) { | 1872 if (is_assignment(b.fOperator)) { |
1869 lvalue = this->getLValue(*b.fLeft, out); | 1873 lvalue = this->getLValue(*b.fLeft, out); |
1870 lhs = lvalue->load(out); | 1874 lhs = lvalue->load(out); |
1871 } else { | 1875 } else { |
1872 lvalue = nullptr; | 1876 lvalue = nullptr; |
1873 lhs = this->writeExpression(*b.fLeft, out); | 1877 lhs = this->writeExpression(*b.fLeft, out); |
1874 } | 1878 } |
1875 SpvId rhs = this->writeExpression(*b.fRight, out); | 1879 SpvId rhs = this->writeExpression(*b.fRight, out); |
1876 // component type we are operating on: float, int, uint | 1880 // component type we are operating on: float, int, uint |
1877 const Type* operandType; | 1881 const Type* operandType; |
1878 // IR allows mismatched types in expressions (e.g. vec2 * float), but they n
eed special handling | 1882 // IR allows mismatched types in expressions (e.g. vec2 * float), but they n
eed special handling |
1879 // in SPIR-V | 1883 // in SPIR-V |
1880 if (b.fLeft->fType != b.fRight->fType) { | 1884 if (b.fLeft->fType != b.fRight->fType) { |
1881 if (b.fLeft->fType->kind() == Type::kVector_Kind && | 1885 if (b.fLeft->fType.kind() == Type::kVector_Kind && |
1882 b.fRight->fType->isNumber()) { | 1886 b.fRight->fType.isNumber()) { |
1883 // promote number to vector | 1887 // promote number to vector |
1884 SpvId vec = this->nextId(); | 1888 SpvId vec = this->nextId(); |
1885 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); | 1889 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType.columns(), ou
t); |
1886 this->writeWord(this->getType(resultType), out); | 1890 this->writeWord(this->getType(resultType), out); |
1887 this->writeWord(vec, out); | 1891 this->writeWord(vec, out); |
1888 for (int i = 0; i < resultType.columns(); i++) { | 1892 for (int i = 0; i < resultType.columns(); i++) { |
1889 this->writeWord(rhs, out); | 1893 this->writeWord(rhs, out); |
1890 } | 1894 } |
1891 rhs = vec; | 1895 rhs = vec; |
1892 operandType = b.fRight->fType.get(); | 1896 operandType = &b.fRight->fType; |
1893 } else if (b.fRight->fType->kind() == Type::kVector_Kind && | 1897 } else if (b.fRight->fType.kind() == Type::kVector_Kind && |
1894 b.fLeft->fType->isNumber()) { | 1898 b.fLeft->fType.isNumber()) { |
1895 // promote number to vector | 1899 // promote number to vector |
1896 SpvId vec = this->nextId(); | 1900 SpvId vec = this->nextId(); |
1897 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType->columns(), o
ut); | 1901 this->writeOpCode(SpvOpCompositeConstruct, 3 + b.fType.columns(), ou
t); |
1898 this->writeWord(this->getType(resultType), out); | 1902 this->writeWord(this->getType(resultType), out); |
1899 this->writeWord(vec, out); | 1903 this->writeWord(vec, out); |
1900 for (int i = 0; i < resultType.columns(); i++) { | 1904 for (int i = 0; i < resultType.columns(); i++) { |
1901 this->writeWord(lhs, out); | 1905 this->writeWord(lhs, out); |
1902 } | 1906 } |
1903 lhs = vec; | 1907 lhs = vec; |
1904 ASSERT(!lvalue); | 1908 ASSERT(!lvalue); |
1905 operandType = b.fLeft->fType.get(); | 1909 operandType = &b.fLeft->fType; |
1906 } else if (b.fLeft->fType->kind() == Type::kMatrix_Kind) { | 1910 } else if (b.fLeft->fType.kind() == Type::kMatrix_Kind) { |
1907 SpvOp_ op; | 1911 SpvOp_ op; |
1908 if (b.fRight->fType->kind() == Type::kMatrix_Kind) { | 1912 if (b.fRight->fType.kind() == Type::kMatrix_Kind) { |
1909 op = SpvOpMatrixTimesMatrix; | 1913 op = SpvOpMatrixTimesMatrix; |
1910 } else if (b.fRight->fType->kind() == Type::kVector_Kind) { | 1914 } else if (b.fRight->fType.kind() == Type::kVector_Kind) { |
1911 op = SpvOpMatrixTimesVector; | 1915 op = SpvOpMatrixTimesVector; |
1912 } else { | 1916 } else { |
1913 ASSERT(b.fRight->fType->kind() == Type::kScalar_Kind); | 1917 ASSERT(b.fRight->fType.kind() == Type::kScalar_Kind); |
1914 op = SpvOpMatrixTimesScalar; | 1918 op = SpvOpMatrixTimesScalar; |
1915 } | 1919 } |
1916 SpvId result = this->nextId(); | 1920 SpvId result = this->nextId(); |
1917 this->writeInstruction(op, this->getType(*b.fType), result, lhs, rhs
, out); | 1921 this->writeInstruction(op, this->getType(b.fType), result, lhs, rhs,
out); |
1918 if (b.fOperator == Token::STAREQ) { | 1922 if (b.fOperator == Token::STAREQ) { |
1919 lvalue->store(result, out); | 1923 lvalue->store(result, out); |
1920 } else { | 1924 } else { |
1921 ASSERT(b.fOperator == Token::STAR); | 1925 ASSERT(b.fOperator == Token::STAR); |
1922 } | 1926 } |
1923 return result; | 1927 return result; |
1924 } else if (b.fRight->fType->kind() == Type::kMatrix_Kind) { | 1928 } else if (b.fRight->fType.kind() == Type::kMatrix_Kind) { |
1925 SpvId result = this->nextId(); | 1929 SpvId result = this->nextId(); |
1926 if (b.fLeft->fType->kind() == Type::kVector_Kind) { | 1930 if (b.fLeft->fType.kind() == Type::kVector_Kind) { |
1927 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(*b.
fType), result, | 1931 this->writeInstruction(SpvOpVectorTimesMatrix, this->getType(b.f
Type), result, |
1928 lhs, rhs, out); | 1932 lhs, rhs, out); |
1929 } else { | 1933 } else { |
1930 ASSERT(b.fLeft->fType->kind() == Type::kScalar_Kind); | 1934 ASSERT(b.fLeft->fType.kind() == Type::kScalar_Kind); |
1931 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(*b.
fType), result, rhs, | 1935 this->writeInstruction(SpvOpMatrixTimesScalar, this->getType(b.f
Type), result, rhs, |
1932 lhs, out); | 1936 lhs, out); |
1933 } | 1937 } |
1934 if (b.fOperator == Token::STAREQ) { | 1938 if (b.fOperator == Token::STAREQ) { |
1935 lvalue->store(result, out); | 1939 lvalue->store(result, out); |
1936 } else { | 1940 } else { |
1937 ASSERT(b.fOperator == Token::STAR); | 1941 ASSERT(b.fOperator == Token::STAR); |
1938 } | 1942 } |
1939 return result; | 1943 return result; |
1940 } else { | 1944 } else { |
1941 ABORT("unsupported binary expression: %s", b.description().c_str()); | 1945 ABORT("unsupported binary expression: %s", b.description().c_str()); |
1942 } | 1946 } |
1943 } else { | 1947 } else { |
1944 operandType = b.fLeft->fType.get(); | 1948 operandType = &b.fLeft->fType; |
1945 ASSERT(*operandType == *b.fRight->fType); | 1949 ASSERT(*operandType == b.fRight->fType); |
1946 } | 1950 } |
1947 switch (b.fOperator) { | 1951 switch (b.fOperator) { |
1948 case Token::EQEQ: | 1952 case Token::EQEQ: |
1949 ASSERT(resultType == *kBool_Type); | 1953 ASSERT(resultType == *fContext.fBool_Type); |
1950 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdEqual, | 1954 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdEqual, |
1951 SpvOpIEqual, SpvOpIEqual, SpvOpLog
icalEqual, out); | 1955 SpvOpIEqual, SpvOpIEqual, SpvOpLog
icalEqual, out); |
1952 case Token::NEQ: | 1956 case Token::NEQ: |
1953 ASSERT(resultType == *kBool_Type); | 1957 ASSERT(resultType == *fContext.fBool_Type); |
1954 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdNotEqual, | 1958 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdNotEqual, |
1955 SpvOpINotEqual, SpvOpINotEqual, Sp
vOpLogicalNotEqual, | 1959 SpvOpINotEqual, SpvOpINotEqual, Sp
vOpLogicalNotEqual, |
1956 out); | 1960 out); |
1957 case Token::GT: | 1961 case Token::GT: |
1958 ASSERT(resultType == *kBool_Type); | 1962 ASSERT(resultType == *fContext.fBool_Type); |
1959 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, | 1963 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, |
1960 SpvOpFOrdGreaterThan, SpvOpSGreate
rThan, | 1964 SpvOpFOrdGreaterThan, SpvOpSGreate
rThan, |
1961 SpvOpUGreaterThan, SpvOpUndef, out
); | 1965 SpvOpUGreaterThan, SpvOpUndef, out
); |
1962 case Token::LT: | 1966 case Token::LT: |
1963 ASSERT(resultType == *kBool_Type); | 1967 ASSERT(resultType == *fContext.fBool_Type); |
1964 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdLessThan, | 1968 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFOrdLessThan, |
1965 SpvOpSLessThan, SpvOpULessThan, Sp
vOpUndef, out); | 1969 SpvOpSLessThan, SpvOpULessThan, Sp
vOpUndef, out); |
1966 case Token::GTEQ: | 1970 case Token::GTEQ: |
1967 ASSERT(resultType == *kBool_Type); | 1971 ASSERT(resultType == *fContext.fBool_Type); |
1968 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, | 1972 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, |
1969 SpvOpFOrdGreaterThanEqual, SpvOpSG
reaterThanEqual, | 1973 SpvOpFOrdGreaterThanEqual, SpvOpSG
reaterThanEqual, |
1970 SpvOpUGreaterThanEqual, SpvOpUndef
, out); | 1974 SpvOpUGreaterThanEqual, SpvOpUndef
, out); |
1971 case Token::LTEQ: | 1975 case Token::LTEQ: |
1972 ASSERT(resultType == *kBool_Type); | 1976 ASSERT(resultType == *fContext.fBool_Type); |
1973 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, | 1977 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, |
1974 SpvOpFOrdLessThanEqual, SpvOpSLess
ThanEqual, | 1978 SpvOpFOrdLessThanEqual, SpvOpSLess
ThanEqual, |
1975 SpvOpULessThanEqual, SpvOpUndef, o
ut); | 1979 SpvOpULessThanEqual, SpvOpUndef, o
ut); |
1976 case Token::PLUS: | 1980 case Token::PLUS: |
1977 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFAdd, | 1981 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFAdd, |
1978 SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
out); | 1982 SpvOpIAdd, SpvOpIAdd, SpvOpUndef,
out); |
1979 case Token::MINUS: | 1983 case Token::MINUS: |
1980 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFSub, | 1984 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFSub, |
1981 SpvOpISub, SpvOpISub, SpvOpUndef,
out); | 1985 SpvOpISub, SpvOpISub, SpvOpUndef,
out); |
1982 case Token::STAR: | 1986 case Token::STAR: |
1983 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && | 1987 if (b.fLeft->fType.kind() == Type::kMatrix_Kind && |
1984 b.fRight->fType->kind() == Type::kMatrix_Kind) { | 1988 b.fRight->fType.kind() == Type::kMatrix_Kind) { |
1985 // matrix multiply | 1989 // matrix multiply |
1986 SpvId result = this->nextId(); | 1990 SpvId result = this->nextId(); |
1987 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, | 1991 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
1988 lhs, rhs, out); | 1992 lhs, rhs, out); |
1989 return result; | 1993 return result; |
1990 } | 1994 } |
1991 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFMul, | 1995 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFMul, |
1992 SpvOpIMul, SpvOpIMul, SpvOpUndef,
out); | 1996 SpvOpIMul, SpvOpIMul, SpvOpUndef,
out); |
1993 case Token::SLASH: | 1997 case Token::SLASH: |
1994 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFDiv, | 1998 return this->writeBinaryOperation(resultType, *operandType, lhs, rhs
, SpvOpFDiv, |
1995 SpvOpSDiv, SpvOpUDiv, SpvOpUndef,
out); | 1999 SpvOpSDiv, SpvOpUDiv, SpvOpUndef,
out); |
1996 case Token::PLUSEQ: { | 2000 case Token::PLUSEQ: { |
1997 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFAdd, | 2001 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFAdd, |
1998 SpvOpIAdd, SpvOpIAdd, SpvO
pUndef, out); | 2002 SpvOpIAdd, SpvOpIAdd, SpvO
pUndef, out); |
1999 ASSERT(lvalue); | 2003 ASSERT(lvalue); |
2000 lvalue->store(result, out); | 2004 lvalue->store(result, out); |
2001 return result; | 2005 return result; |
2002 } | 2006 } |
2003 case Token::MINUSEQ: { | 2007 case Token::MINUSEQ: { |
2004 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFSub, | 2008 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFSub, |
2005 SpvOpISub, SpvOpISub, SpvO
pUndef, out); | 2009 SpvOpISub, SpvOpISub, SpvO
pUndef, out); |
2006 ASSERT(lvalue); | 2010 ASSERT(lvalue); |
2007 lvalue->store(result, out); | 2011 lvalue->store(result, out); |
2008 return result; | 2012 return result; |
2009 } | 2013 } |
2010 case Token::STAREQ: { | 2014 case Token::STAREQ: { |
2011 if (b.fLeft->fType->kind() == Type::kMatrix_Kind && | 2015 if (b.fLeft->fType.kind() == Type::kMatrix_Kind && |
2012 b.fRight->fType->kind() == Type::kMatrix_Kind) { | 2016 b.fRight->fType.kind() == Type::kMatrix_Kind) { |
2013 // matrix multiply | 2017 // matrix multiply |
2014 SpvId result = this->nextId(); | 2018 SpvId result = this->nextId(); |
2015 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, | 2019 this->writeInstruction(SpvOpMatrixTimesMatrix, this->getType(res
ultType), result, |
2016 lhs, rhs, out); | 2020 lhs, rhs, out); |
2017 ASSERT(lvalue); | 2021 ASSERT(lvalue); |
2018 lvalue->store(result, out); | 2022 lvalue->store(result, out); |
2019 return result; | 2023 return result; |
2020 } | 2024 } |
2021 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFMul, | 2025 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFMul, |
2022 SpvOpIMul, SpvOpIMul, SpvO
pUndef, out); | 2026 SpvOpIMul, SpvOpIMul, SpvO
pUndef, out); |
2023 ASSERT(lvalue); | 2027 ASSERT(lvalue); |
2024 lvalue->store(result, out); | 2028 lvalue->store(result, out); |
2025 return result; | 2029 return result; |
2026 } | 2030 } |
2027 case Token::SLASHEQ: { | 2031 case Token::SLASHEQ: { |
2028 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFDiv, | 2032 SpvId result = this->writeBinaryOperation(resultType, *operandType,
lhs, rhs, SpvOpFDiv, |
2029 SpvOpSDiv, SpvOpUDiv, SpvO
pUndef, out); | 2033 SpvOpSDiv, SpvOpUDiv, SpvO
pUndef, out); |
2030 ASSERT(lvalue); | 2034 ASSERT(lvalue); |
2031 lvalue->store(result, out); | 2035 lvalue->store(result, out); |
2032 return result; | 2036 return result; |
2033 } | 2037 } |
2034 default: | 2038 default: |
2035 // FIXME: missing support for some operators (bitwise, &&=, ||=, shi
ft...) | 2039 // FIXME: missing support for some operators (bitwise, &&=, ||=, shi
ft...) |
2036 ABORT("unsupported binary expression: %s", b.description().c_str()); | 2040 ABORT("unsupported binary expression: %s", b.description().c_str()); |
2037 } | 2041 } |
2038 } | 2042 } |
2039 | 2043 |
2040 SpvId SPIRVCodeGenerator::writeLogicalAnd(BinaryExpression& a, std::ostream& out
) { | 2044 SpvId SPIRVCodeGenerator::writeLogicalAnd(BinaryExpression& a, std::ostream& out
) { |
2041 ASSERT(a.fOperator == Token::LOGICALAND); | 2045 ASSERT(a.fOperator == Token::LOGICALAND); |
2042 BoolLiteral falseLiteral(Position(), false); | 2046 BoolLiteral falseLiteral(fContext, Position(), false); |
2043 SpvId falseConstant = this->writeBoolLiteral(falseLiteral); | 2047 SpvId falseConstant = this->writeBoolLiteral(falseLiteral); |
2044 SpvId lhs = this->writeExpression(*a.fLeft, out); | 2048 SpvId lhs = this->writeExpression(*a.fLeft, out); |
2045 SpvId rhsLabel = this->nextId(); | 2049 SpvId rhsLabel = this->nextId(); |
2046 SpvId end = this->nextId(); | 2050 SpvId end = this->nextId(); |
2047 SpvId lhsBlock = fCurrentBlock; | 2051 SpvId lhsBlock = fCurrentBlock; |
2048 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); | 2052 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
2049 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); | 2053 this->writeInstruction(SpvOpBranchConditional, lhs, rhsLabel, end, out); |
2050 this->writeLabel(rhsLabel, out); | 2054 this->writeLabel(rhsLabel, out); |
2051 SpvId rhs = this->writeExpression(*a.fRight, out); | 2055 SpvId rhs = this->writeExpression(*a.fRight, out); |
2052 SpvId rhsBlock = fCurrentBlock; | 2056 SpvId rhsBlock = fCurrentBlock; |
2053 this->writeInstruction(SpvOpBranch, end, out); | 2057 this->writeInstruction(SpvOpBranch, end, out); |
2054 this->writeLabel(end, out); | 2058 this->writeLabel(end, out); |
2055 SpvId result = this->nextId(); | 2059 SpvId result = this->nextId(); |
2056 this->writeInstruction(SpvOpPhi, this->getType(*kBool_Type), result, falseCo
nstant, lhsBlock, | 2060 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result
, falseConstant, |
2057 rhs, rhsBlock, out); | 2061 lhsBlock, rhs, rhsBlock, out); |
2058 return result; | 2062 return result; |
2059 } | 2063 } |
2060 | 2064 |
2061 SpvId SPIRVCodeGenerator::writeLogicalOr(BinaryExpression& o, std::ostream& out)
{ | 2065 SpvId SPIRVCodeGenerator::writeLogicalOr(BinaryExpression& o, std::ostream& out)
{ |
2062 ASSERT(o.fOperator == Token::LOGICALOR); | 2066 ASSERT(o.fOperator == Token::LOGICALOR); |
2063 BoolLiteral trueLiteral(Position(), true); | 2067 BoolLiteral trueLiteral(fContext, Position(), true); |
2064 SpvId trueConstant = this->writeBoolLiteral(trueLiteral); | 2068 SpvId trueConstant = this->writeBoolLiteral(trueLiteral); |
2065 SpvId lhs = this->writeExpression(*o.fLeft, out); | 2069 SpvId lhs = this->writeExpression(*o.fLeft, out); |
2066 SpvId rhsLabel = this->nextId(); | 2070 SpvId rhsLabel = this->nextId(); |
2067 SpvId end = this->nextId(); | 2071 SpvId end = this->nextId(); |
2068 SpvId lhsBlock = fCurrentBlock; | 2072 SpvId lhsBlock = fCurrentBlock; |
2069 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); | 2073 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
2070 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); | 2074 this->writeInstruction(SpvOpBranchConditional, lhs, end, rhsLabel, out); |
2071 this->writeLabel(rhsLabel, out); | 2075 this->writeLabel(rhsLabel, out); |
2072 SpvId rhs = this->writeExpression(*o.fRight, out); | 2076 SpvId rhs = this->writeExpression(*o.fRight, out); |
2073 SpvId rhsBlock = fCurrentBlock; | 2077 SpvId rhsBlock = fCurrentBlock; |
2074 this->writeInstruction(SpvOpBranch, end, out); | 2078 this->writeInstruction(SpvOpBranch, end, out); |
2075 this->writeLabel(end, out); | 2079 this->writeLabel(end, out); |
2076 SpvId result = this->nextId(); | 2080 SpvId result = this->nextId(); |
2077 this->writeInstruction(SpvOpPhi, this->getType(*kBool_Type), result, trueCon
stant, lhsBlock, | 2081 this->writeInstruction(SpvOpPhi, this->getType(*fContext.fBool_Type), result
, trueConstant, |
2078 rhs, rhsBlock, out); | 2082 lhsBlock, rhs, rhsBlock, out); |
2079 return result; | 2083 return result; |
2080 } | 2084 } |
2081 | 2085 |
2082 SpvId SPIRVCodeGenerator::writeTernaryExpression(TernaryExpression& t, std::ostr
eam& out) { | 2086 SpvId SPIRVCodeGenerator::writeTernaryExpression(TernaryExpression& t, std::ostr
eam& out) { |
2083 SpvId test = this->writeExpression(*t.fTest, out); | 2087 SpvId test = this->writeExpression(*t.fTest, out); |
2084 if (t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) { | 2088 if (t.fIfTrue->isConstant() && t.fIfFalse->isConstant()) { |
2085 // both true and false are constants, can just use OpSelect | 2089 // both true and false are constants, can just use OpSelect |
2086 SpvId result = this->nextId(); | 2090 SpvId result = this->nextId(); |
2087 SpvId trueId = this->writeExpression(*t.fIfTrue, out); | 2091 SpvId trueId = this->writeExpression(*t.fIfTrue, out); |
2088 SpvId falseId = this->writeExpression(*t.fIfFalse, out); | 2092 SpvId falseId = this->writeExpression(*t.fIfFalse, out); |
2089 this->writeInstruction(SpvOpSelect, this->getType(*t.fType), result, tes
t, trueId, falseId, | 2093 this->writeInstruction(SpvOpSelect, this->getType(t.fType), result, test
, trueId, falseId, |
2090 out); | 2094 out); |
2091 return result; | 2095 return result; |
2092 } | 2096 } |
2093 // was originally using OpPhi to choose the result, but for some reason that
is crashing on | 2097 // was originally using OpPhi to choose the result, but for some reason that
is crashing on |
2094 // Adreno. Switched to storing the result in a temp variable as glslang does
. | 2098 // Adreno. Switched to storing the result in a temp variable as glslang does
. |
2095 SpvId var = this->nextId(); | 2099 SpvId var = this->nextId(); |
2096 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStora
geClassFunction), | 2100 this->writeInstruction(SpvOpVariable, this->getPointerType(t.fType, SpvStora
geClassFunction), |
2097 var, SpvStorageClassFunction, out); | 2101 var, SpvStorageClassFunction, fVariableBuffer); |
2098 SpvId trueLabel = this->nextId(); | 2102 SpvId trueLabel = this->nextId(); |
2099 SpvId falseLabel = this->nextId(); | 2103 SpvId falseLabel = this->nextId(); |
2100 SpvId end = this->nextId(); | 2104 SpvId end = this->nextId(); |
2101 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); | 2105 this->writeInstruction(SpvOpSelectionMerge, end, SpvSelectionControlMaskNone
, out); |
2102 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel,
out); | 2106 this->writeInstruction(SpvOpBranchConditional, test, trueLabel, falseLabel,
out); |
2103 this->writeLabel(trueLabel, out); | 2107 this->writeLabel(trueLabel, out); |
2104 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, ou
t), out); | 2108 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfTrue, ou
t), out); |
2105 this->writeInstruction(SpvOpBranch, end, out); | 2109 this->writeInstruction(SpvOpBranch, end, out); |
2106 this->writeLabel(falseLabel, out); | 2110 this->writeLabel(falseLabel, out); |
2107 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, o
ut), out); | 2111 this->writeInstruction(SpvOpStore, var, this->writeExpression(*t.fIfFalse, o
ut), out); |
2108 this->writeInstruction(SpvOpBranch, end, out); | 2112 this->writeInstruction(SpvOpBranch, end, out); |
2109 this->writeLabel(end, out); | 2113 this->writeLabel(end, out); |
2110 SpvId result = this->nextId(); | 2114 SpvId result = this->nextId(); |
2111 this->writeInstruction(SpvOpLoad, this->getType(*t.fType), result, var, out)
; | 2115 this->writeInstruction(SpvOpLoad, this->getType(t.fType), result, var, out); |
2112 return result; | 2116 return result; |
2113 } | 2117 } |
2114 | 2118 |
2115 Expression* literal_1(const Type& type) { | 2119 std::unique_ptr<Expression> create_literal_1(const Context& context, const Type&
type) { |
2116 static IntLiteral int1(Position(), 1); | 2120 if (type == *context.fInt_Type) { |
2117 static FloatLiteral float1(Position(), 1.0); | 2121 return std::unique_ptr<Expression>(new IntLiteral(context, Position(), 1
)); |
2118 if (type == *kInt_Type) { | |
2119 return &int1; | |
2120 } | 2122 } |
2121 else if (type == *kFloat_Type) { | 2123 else if (type == *context.fFloat_Type) { |
2122 return &float1; | 2124 return std::unique_ptr<Expression>(new FloatLiteral(context, Position(),
1.0)); |
2123 } else { | 2125 } else { |
2124 ABORT("math is unsupported on type '%s'") | 2126 ABORT("math is unsupported on type '%s'") |
2125 } | 2127 } |
2126 } | 2128 } |
2127 | 2129 |
2128 SpvId SPIRVCodeGenerator::writePrefixExpression(PrefixExpression& p, std::ostrea
m& out) { | 2130 SpvId SPIRVCodeGenerator::writePrefixExpression(PrefixExpression& p, std::ostrea
m& out) { |
2129 if (p.fOperator == Token::MINUS) { | 2131 if (p.fOperator == Token::MINUS) { |
2130 SpvId result = this->nextId(); | 2132 SpvId result = this->nextId(); |
2131 SpvId typeId = this->getType(*p.fType); | 2133 SpvId typeId = this->getType(p.fType); |
2132 SpvId expr = this->writeExpression(*p.fOperand, out); | 2134 SpvId expr = this->writeExpression(*p.fOperand, out); |
2133 if (is_float(*p.fType)) { | 2135 if (is_float(fContext, p.fType)) { |
2134 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); | 2136 this->writeInstruction(SpvOpFNegate, typeId, result, expr, out); |
2135 } else if (is_signed(*p.fType)) { | 2137 } else if (is_signed(fContext, p.fType)) { |
2136 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); | 2138 this->writeInstruction(SpvOpSNegate, typeId, result, expr, out); |
2137 } else { | 2139 } else { |
2138 ABORT("unsupported prefix expression %s", p.description().c_str()); | 2140 ABORT("unsupported prefix expression %s", p.description().c_str()); |
2139 }; | 2141 }; |
2140 return result; | 2142 return result; |
2141 } | 2143 } |
2142 switch (p.fOperator) { | 2144 switch (p.fOperator) { |
2143 case Token::PLUS: | 2145 case Token::PLUS: |
2144 return this->writeExpression(*p.fOperand, out); | 2146 return this->writeExpression(*p.fOperand, out); |
2145 case Token::PLUSPLUS: { | 2147 case Token::PLUSPLUS: { |
2146 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); | 2148 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
2147 SpvId one = this->writeExpression(*literal_1(*p.fType), out); | 2149 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fTyp
e), out); |
2148 SpvId result = this->writeBinaryOperation(*p.fType, *p.fType, lv->lo
ad(out), one, | 2150 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load
(out), one, |
2149 SpvOpFAdd, SpvOpIAdd, SpvO
pIAdd, SpvOpUndef, | 2151 SpvOpFAdd, SpvOpIAdd, SpvO
pIAdd, SpvOpUndef, |
2150 out); | 2152 out); |
2151 lv->store(result, out); | 2153 lv->store(result, out); |
2152 return result; | 2154 return result; |
2153 } | 2155 } |
2154 case Token::MINUSMINUS: { | 2156 case Token::MINUSMINUS: { |
2155 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); | 2157 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
2156 SpvId one = this->writeExpression(*literal_1(*p.fType), out); | 2158 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fTyp
e), out); |
2157 SpvId result = this->writeBinaryOperation(*p.fType, *p.fType, lv->lo
ad(out), one, | 2159 SpvId result = this->writeBinaryOperation(p.fType, p.fType, lv->load
(out), one, |
2158 SpvOpFSub, SpvOpISub, SpvO
pISub, SpvOpUndef, | 2160 SpvOpFSub, SpvOpISub, SpvO
pISub, SpvOpUndef, |
2159 out); | 2161 out); |
2160 lv->store(result, out); | 2162 lv->store(result, out); |
2161 return result; | 2163 return result; |
2162 } | 2164 } |
2163 case Token::NOT: { | 2165 case Token::NOT: { |
2164 ASSERT(p.fOperand->fType == kBool_Type); | 2166 ASSERT(p.fOperand->fType == *fContext.fBool_Type); |
2165 SpvId result = this->nextId(); | 2167 SpvId result = this->nextId(); |
2166 this->writeInstruction(SpvOpLogicalNot, this->getType(*p.fOperand->f
Type), result, | 2168 this->writeInstruction(SpvOpLogicalNot, this->getType(p.fOperand->fT
ype), result, |
2167 this->writeExpression(*p.fOperand, out), out)
; | 2169 this->writeExpression(*p.fOperand, out), out)
; |
2168 return result; | 2170 return result; |
2169 } | 2171 } |
2170 default: | 2172 default: |
2171 ABORT("unsupported prefix expression: %s", p.description().c_str()); | 2173 ABORT("unsupported prefix expression: %s", p.description().c_str()); |
2172 } | 2174 } |
2173 } | 2175 } |
2174 | 2176 |
2175 SpvId SPIRVCodeGenerator::writePostfixExpression(PostfixExpression& p, std::ostr
eam& out) { | 2177 SpvId SPIRVCodeGenerator::writePostfixExpression(PostfixExpression& p, std::ostr
eam& out) { |
2176 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); | 2178 std::unique_ptr<LValue> lv = this->getLValue(*p.fOperand, out); |
2177 SpvId result = lv->load(out); | 2179 SpvId result = lv->load(out); |
2178 SpvId one = this->writeExpression(*literal_1(*p.fType), out); | 2180 SpvId one = this->writeExpression(*create_literal_1(fContext, p.fType), out)
; |
2179 switch (p.fOperator) { | 2181 switch (p.fOperator) { |
2180 case Token::PLUSPLUS: { | 2182 case Token::PLUSPLUS: { |
2181 SpvId temp = this->writeBinaryOperation(*p.fType, *p.fType, result,
one, SpvOpFAdd, | 2183 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFAdd, |
2182 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); | 2184 SpvOpIAdd, SpvOpIAdd, SpvOpU
ndef, out); |
2183 lv->store(temp, out); | 2185 lv->store(temp, out); |
2184 return result; | 2186 return result; |
2185 } | 2187 } |
2186 case Token::MINUSMINUS: { | 2188 case Token::MINUSMINUS: { |
2187 SpvId temp = this->writeBinaryOperation(*p.fType, *p.fType, result,
one, SpvOpFSub, | 2189 SpvId temp = this->writeBinaryOperation(p.fType, p.fType, result, on
e, SpvOpFSub, |
2188 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); | 2190 SpvOpISub, SpvOpISub, SpvOpU
ndef, out); |
2189 lv->store(temp, out); | 2191 lv->store(temp, out); |
2190 return result; | 2192 return result; |
2191 } | 2193 } |
2192 default: | 2194 default: |
2193 ABORT("unsupported postfix expression %s", p.description().c_str()); | 2195 ABORT("unsupported postfix expression %s", p.description().c_str()); |
2194 } | 2196 } |
2195 } | 2197 } |
2196 | 2198 |
2197 SpvId SPIRVCodeGenerator::writeBoolLiteral(BoolLiteral& b) { | 2199 SpvId SPIRVCodeGenerator::writeBoolLiteral(BoolLiteral& b) { |
2198 if (b.fValue) { | 2200 if (b.fValue) { |
2199 if (fBoolTrue == 0) { | 2201 if (fBoolTrue == 0) { |
2200 fBoolTrue = this->nextId(); | 2202 fBoolTrue = this->nextId(); |
2201 this->writeInstruction(SpvOpConstantTrue, this->getType(*b.fType), f
BoolTrue, | 2203 this->writeInstruction(SpvOpConstantTrue, this->getType(b.fType), fB
oolTrue, |
2202 fConstantBuffer); | 2204 fConstantBuffer); |
2203 } | 2205 } |
2204 return fBoolTrue; | 2206 return fBoolTrue; |
2205 } else { | 2207 } else { |
2206 if (fBoolFalse == 0) { | 2208 if (fBoolFalse == 0) { |
2207 fBoolFalse = this->nextId(); | 2209 fBoolFalse = this->nextId(); |
2208 this->writeInstruction(SpvOpConstantFalse, this->getType(*b.fType),
fBoolFalse, | 2210 this->writeInstruction(SpvOpConstantFalse, this->getType(b.fType), f
BoolFalse, |
2209 fConstantBuffer); | 2211 fConstantBuffer); |
2210 } | 2212 } |
2211 return fBoolFalse; | 2213 return fBoolFalse; |
2212 } | 2214 } |
2213 } | 2215 } |
2214 | 2216 |
2215 SpvId SPIRVCodeGenerator::writeIntLiteral(IntLiteral& i) { | 2217 SpvId SPIRVCodeGenerator::writeIntLiteral(IntLiteral& i) { |
2216 if (i.fType == kInt_Type) { | 2218 if (i.fType == *fContext.fInt_Type) { |
2217 auto entry = fIntConstants.find(i.fValue); | 2219 auto entry = fIntConstants.find(i.fValue); |
2218 if (entry == fIntConstants.end()) { | 2220 if (entry == fIntConstants.end()) { |
2219 SpvId result = this->nextId(); | 2221 SpvId result = this->nextId(); |
2220 this->writeInstruction(SpvOpConstant, this->getType(*i.fType), resul
t, (SpvId) i.fValue, | 2222 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
2221 fConstantBuffer); | 2223 fConstantBuffer); |
2222 fIntConstants[i.fValue] = result; | 2224 fIntConstants[i.fValue] = result; |
2223 return result; | 2225 return result; |
2224 } | 2226 } |
2225 return entry->second; | 2227 return entry->second; |
2226 } else { | 2228 } else { |
2227 ASSERT(i.fType == kUInt_Type); | 2229 ASSERT(i.fType == *fContext.fUInt_Type); |
2228 auto entry = fUIntConstants.find(i.fValue); | 2230 auto entry = fUIntConstants.find(i.fValue); |
2229 if (entry == fUIntConstants.end()) { | 2231 if (entry == fUIntConstants.end()) { |
2230 SpvId result = this->nextId(); | 2232 SpvId result = this->nextId(); |
2231 this->writeInstruction(SpvOpConstant, this->getType(*i.fType), resul
t, (SpvId) i.fValue, | 2233 this->writeInstruction(SpvOpConstant, this->getType(i.fType), result
, (SpvId) i.fValue, |
2232 fConstantBuffer); | 2234 fConstantBuffer); |
2233 fUIntConstants[i.fValue] = result; | 2235 fUIntConstants[i.fValue] = result; |
2234 return result; | 2236 return result; |
2235 } | 2237 } |
2236 return entry->second; | 2238 return entry->second; |
2237 } | 2239 } |
2238 } | 2240 } |
2239 | 2241 |
2240 SpvId SPIRVCodeGenerator::writeFloatLiteral(FloatLiteral& f) { | 2242 SpvId SPIRVCodeGenerator::writeFloatLiteral(FloatLiteral& f) { |
2241 if (f.fType == kFloat_Type) { | 2243 if (f.fType == *fContext.fFloat_Type) { |
2242 float value = (float) f.fValue; | 2244 float value = (float) f.fValue; |
2243 auto entry = fFloatConstants.find(value); | 2245 auto entry = fFloatConstants.find(value); |
2244 if (entry == fFloatConstants.end()) { | 2246 if (entry == fFloatConstants.end()) { |
2245 SpvId result = this->nextId(); | 2247 SpvId result = this->nextId(); |
2246 uint32_t bits; | 2248 uint32_t bits; |
2247 ASSERT(sizeof(bits) == sizeof(value)); | 2249 ASSERT(sizeof(bits) == sizeof(value)); |
2248 memcpy(&bits, &value, sizeof(bits)); | 2250 memcpy(&bits, &value, sizeof(bits)); |
2249 this->writeInstruction(SpvOpConstant, this->getType(*f.fType), resul
t, bits, | 2251 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, bits, |
2250 fConstantBuffer); | 2252 fConstantBuffer); |
2251 fFloatConstants[value] = result; | 2253 fFloatConstants[value] = result; |
2252 return result; | 2254 return result; |
2253 } | 2255 } |
2254 return entry->second; | 2256 return entry->second; |
2255 } else { | 2257 } else { |
2256 ASSERT(f.fType == kDouble_Type); | 2258 ASSERT(f.fType == *fContext.fDouble_Type); |
2257 auto entry = fDoubleConstants.find(f.fValue); | 2259 auto entry = fDoubleConstants.find(f.fValue); |
2258 if (entry == fDoubleConstants.end()) { | 2260 if (entry == fDoubleConstants.end()) { |
2259 SpvId result = this->nextId(); | 2261 SpvId result = this->nextId(); |
2260 uint64_t bits; | 2262 uint64_t bits; |
2261 ASSERT(sizeof(bits) == sizeof(f.fValue)); | 2263 ASSERT(sizeof(bits) == sizeof(f.fValue)); |
2262 memcpy(&bits, &f.fValue, sizeof(bits)); | 2264 memcpy(&bits, &f.fValue, sizeof(bits)); |
2263 this->writeInstruction(SpvOpConstant, this->getType(*f.fType), resul
t, | 2265 this->writeInstruction(SpvOpConstant, this->getType(f.fType), result
, |
2264 bits & 0xffffffff, bits >> 32, fConstantBuffe
r); | 2266 bits & 0xffffffff, bits >> 32, fConstantBuffe
r); |
2265 fDoubleConstants[f.fValue] = result; | 2267 fDoubleConstants[f.fValue] = result; |
2266 return result; | 2268 return result; |
2267 } | 2269 } |
2268 return entry->second; | 2270 return entry->second; |
2269 } | 2271 } |
2270 } | 2272 } |
2271 | 2273 |
2272 SpvId SPIRVCodeGenerator::writeFunctionStart(std::shared_ptr<FunctionDeclaration
> f, | 2274 SpvId SPIRVCodeGenerator::writeFunctionStart(const FunctionDeclaration& f, std::
ostream& out) { |
2273 std::ostream& out) { | 2275 SpvId result = fFunctionMap[&f]; |
2274 SpvId result = fFunctionMap[f]; | 2276 this->writeInstruction(SpvOpFunction, this->getType(f.fReturnType), result, |
2275 this->writeInstruction(SpvOpFunction, this->getType(*f->fReturnType), result
, | |
2276 SpvFunctionControlMaskNone, this->getFunctionType(f),
out); | 2277 SpvFunctionControlMaskNone, this->getFunctionType(f),
out); |
2277 this->writeInstruction(SpvOpName, result, f->fName.c_str(), fNameBuffer); | 2278 this->writeInstruction(SpvOpName, result, f.fName.c_str(), fNameBuffer); |
2278 for (size_t i = 0; i < f->fParameters.size(); i++) { | 2279 for (size_t i = 0; i < f.fParameters.size(); i++) { |
2279 SpvId id = this->nextId(); | 2280 SpvId id = this->nextId(); |
2280 fVariableMap[f->fParameters[i]] = id; | 2281 fVariableMap[f.fParameters[i]] = id; |
2281 SpvId type; | 2282 SpvId type; |
2282 type = this->getPointerType(f->fParameters[i]->fType, SpvStorageClassFun
ction); | 2283 type = this->getPointerType(f.fParameters[i]->fType, SpvStorageClassFunc
tion); |
2283 this->writeInstruction(SpvOpFunctionParameter, type, id, out); | 2284 this->writeInstruction(SpvOpFunctionParameter, type, id, out); |
2284 } | 2285 } |
2285 return result; | 2286 return result; |
2286 } | 2287 } |
2287 | 2288 |
2288 SpvId SPIRVCodeGenerator::writeFunction(FunctionDefinition& f, std::ostream& out
) { | 2289 SpvId SPIRVCodeGenerator::writeFunction(const FunctionDefinition& f, std::ostrea
m& out) { |
2289 SpvId result = this->writeFunctionStart(f.fDeclaration, out); | 2290 SpvId result = this->writeFunctionStart(f.fDeclaration, out); |
2290 this->writeLabel(this->nextId(), out); | 2291 this->writeLabel(this->nextId(), out); |
2291 if (f.fDeclaration->fName == "main") { | 2292 if (f.fDeclaration.fName == "main") { |
2292 out << fGlobalInitializersBuffer.str(); | 2293 out << fGlobalInitializersBuffer.str(); |
2293 } | 2294 } |
2294 std::stringstream bodyBuffer; | 2295 std::stringstream bodyBuffer; |
2295 this->writeBlock(*f.fBody, bodyBuffer); | 2296 this->writeBlock(*f.fBody, bodyBuffer); |
2296 out << fVariableBuffer.str(); | 2297 out << fVariableBuffer.str(); |
2297 fVariableBuffer.str(""); | 2298 fVariableBuffer.str(""); |
2298 out << bodyBuffer.str(); | 2299 out << bodyBuffer.str(); |
2299 if (fCurrentBlock) { | 2300 if (fCurrentBlock) { |
2300 this->writeInstruction(SpvOpReturn, out); | 2301 this->writeInstruction(SpvOpReturn, out); |
2301 } | 2302 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2343 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nDescriptorSet, | 2344 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nDescriptorSet, |
2344 layout.fSet, fDecorationBuffer); | 2345 layout.fSet, fDecorationBuffer); |
2345 } | 2346 } |
2346 if (layout.fBuiltin >= 0) { | 2347 if (layout.fBuiltin >= 0) { |
2347 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBuiltIn, | 2348 this->writeInstruction(SpvOpMemberDecorate, target, member, SpvDecoratio
nBuiltIn, |
2348 layout.fBuiltin, fDecorationBuffer); | 2349 layout.fBuiltin, fDecorationBuffer); |
2349 } | 2350 } |
2350 } | 2351 } |
2351 | 2352 |
2352 SpvId SPIRVCodeGenerator::writeInterfaceBlock(InterfaceBlock& intf) { | 2353 SpvId SPIRVCodeGenerator::writeInterfaceBlock(InterfaceBlock& intf) { |
2353 SpvId type = this->getType(*intf.fVariable->fType); | 2354 SpvId type = this->getType(intf.fVariable.fType); |
2354 SpvId result = this->nextId(); | 2355 SpvId result = this->nextId(); |
2355 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); | 2356 this->writeInstruction(SpvOpDecorate, type, SpvDecorationBlock, fDecorationB
uffer); |
2356 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable->fModifiers
); | 2357 SpvStorageClass_ storageClass = get_storage_class(intf.fVariable.fModifiers)
; |
2357 SpvId ptrType = this->nextId(); | 2358 SpvId ptrType = this->nextId(); |
2358 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); | 2359 this->writeInstruction(SpvOpTypePointer, ptrType, storageClass, type, fConst
antBuffer); |
2359 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); | 2360 this->writeInstruction(SpvOpVariable, ptrType, result, storageClass, fConsta
ntBuffer); |
2360 this->writeLayout(intf.fVariable->fModifiers.fLayout, result); | 2361 this->writeLayout(intf.fVariable.fModifiers.fLayout, result); |
2361 fVariableMap[intf.fVariable] = result; | 2362 fVariableMap[&intf.fVariable] = result; |
2362 return result; | 2363 return result; |
2363 } | 2364 } |
2364 | 2365 |
2365 void SPIRVCodeGenerator::writeGlobalVars(VarDeclaration& decl, std::ostream& out
) { | 2366 void SPIRVCodeGenerator::writeGlobalVars(VarDeclaration& decl, std::ostream& out
) { |
2366 for (size_t i = 0; i < decl.fVars.size(); i++) { | 2367 for (size_t i = 0; i < decl.fVars.size(); i++) { |
2367 if (!decl.fVars[i]->fIsReadFrom && !decl.fVars[i]->fIsWrittenTo) { | 2368 if (!decl.fVars[i]->fIsReadFrom && !decl.fVars[i]->fIsWrittenTo && |
| 2369 !(decl.fVars[i]->fModifiers.fFlags & (Modifiers::kIn_Flag | |
| 2370 Modifiers::kOut_Flag | |
| 2371 Modifiers::kUniform_Flag))
) { |
| 2372 // variable is dead and not an input / output var (the Vulkan debug
layers complain if |
| 2373 // we elide an interface var, even if it's dead) |
2368 continue; | 2374 continue; |
2369 } | 2375 } |
2370 SpvStorageClass_ storageClass; | 2376 SpvStorageClass_ storageClass; |
2371 if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kIn_Flag) { | 2377 if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kIn_Flag) { |
2372 storageClass = SpvStorageClassInput; | 2378 storageClass = SpvStorageClassInput; |
2373 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { | 2379 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kOut_Flag) { |
2374 storageClass = SpvStorageClassOutput; | 2380 storageClass = SpvStorageClassOutput; |
2375 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kUniform_Flag)
{ | 2381 } else if (decl.fVars[i]->fModifiers.fFlags & Modifiers::kUniform_Flag)
{ |
2376 if (decl.fVars[i]->fType->kind() == Type::kSampler_Kind) { | 2382 if (decl.fVars[i]->fType.kind() == Type::kSampler_Kind) { |
2377 storageClass = SpvStorageClassUniformConstant; | 2383 storageClass = SpvStorageClassUniformConstant; |
2378 } else { | 2384 } else { |
2379 storageClass = SpvStorageClassUniform; | 2385 storageClass = SpvStorageClassUniform; |
2380 } | 2386 } |
2381 } else { | 2387 } else { |
2382 storageClass = SpvStorageClassPrivate; | 2388 storageClass = SpvStorageClassPrivate; |
2383 } | 2389 } |
2384 SpvId id = this->nextId(); | 2390 SpvId id = this->nextId(); |
2385 fVariableMap[decl.fVars[i]] = id; | 2391 fVariableMap[decl.fVars[i]] = id; |
2386 SpvId type = this->getPointerType(decl.fVars[i]->fType, storageClass); | 2392 SpvId type = this->getPointerType(decl.fVars[i]->fType, storageClass); |
2387 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); | 2393 this->writeInstruction(SpvOpVariable, type, id, storageClass, fConstantB
uffer); |
2388 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); | 2394 this->writeInstruction(SpvOpName, id, decl.fVars[i]->fName.c_str(), fNam
eBuffer); |
2389 if (decl.fVars[i]->fType->kind() == Type::kMatrix_Kind) { | 2395 if (decl.fVars[i]->fType.kind() == Type::kMatrix_Kind) { |
2390 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionColMajor, | 2396 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionColMajor, |
2391 fDecorationBuffer); | 2397 fDecorationBuffer); |
2392 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionMatrixStride, | 2398 this->writeInstruction(SpvOpMemberDecorate, id, (SpvId) i, SpvDecora
tionMatrixStride, |
2393 (SpvId) decl.fVars[i]->fType->stride(), fDeco
rationBuffer); | 2399 (SpvId) decl.fVars[i]->fType.stride(), fDecor
ationBuffer); |
2394 } | 2400 } |
2395 if (decl.fValues[i]) { | 2401 if (decl.fValues[i]) { |
2396 ASSERT(!fCurrentBlock); | 2402 ASSERT(!fCurrentBlock); |
2397 fCurrentBlock = -1; | 2403 fCurrentBlock = -1; |
2398 SpvId value = this->writeExpression(*decl.fValues[i], fGlobalInitial
izersBuffer); | 2404 SpvId value = this->writeExpression(*decl.fValues[i], fGlobalInitial
izersBuffer); |
2399 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); | 2405 this->writeInstruction(SpvOpStore, id, value, fGlobalInitializersBuf
fer); |
2400 fCurrentBlock = 0; | 2406 fCurrentBlock = 0; |
2401 } | 2407 } |
2402 this->writeLayout(decl.fVars[i]->fModifiers.fLayout, id); | 2408 this->writeLayout(decl.fVars[i]->fModifiers.fLayout, id); |
2403 } | 2409 } |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2531 } | 2537 } |
2532 | 2538 |
2533 void SPIRVCodeGenerator::writeInstructions(Program& program, std::ostream& out)
{ | 2539 void SPIRVCodeGenerator::writeInstructions(Program& program, std::ostream& out)
{ |
2534 fGLSLExtendedInstructions = this->nextId(); | 2540 fGLSLExtendedInstructions = this->nextId(); |
2535 std::stringstream body; | 2541 std::stringstream body; |
2536 std::vector<SpvId> interfaceVars; | 2542 std::vector<SpvId> interfaceVars; |
2537 // assign IDs to functions | 2543 // assign IDs to functions |
2538 for (size_t i = 0; i < program.fElements.size(); i++) { | 2544 for (size_t i = 0; i < program.fElements.size(); i++) { |
2539 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { | 2545 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
2540 FunctionDefinition& f = (FunctionDefinition&) *program.fElements[i]; | 2546 FunctionDefinition& f = (FunctionDefinition&) *program.fElements[i]; |
2541 fFunctionMap[f.fDeclaration] = this->nextId(); | 2547 fFunctionMap[&f.fDeclaration] = this->nextId(); |
2542 } | 2548 } |
2543 } | 2549 } |
2544 for (size_t i = 0; i < program.fElements.size(); i++) { | 2550 for (size_t i = 0; i < program.fElements.size(); i++) { |
2545 if (program.fElements[i]->fKind == ProgramElement::kInterfaceBlock_Kind)
{ | 2551 if (program.fElements[i]->fKind == ProgramElement::kInterfaceBlock_Kind)
{ |
2546 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; | 2552 InterfaceBlock& intf = (InterfaceBlock&) *program.fElements[i]; |
2547 SpvId id = this->writeInterfaceBlock(intf); | 2553 SpvId id = this->writeInterfaceBlock(intf); |
2548 if ((intf.fVariable->fModifiers.fFlags & Modifiers::kIn_Flag) || | 2554 if ((intf.fVariable.fModifiers.fFlags & Modifiers::kIn_Flag) || |
2549 (intf.fVariable->fModifiers.fFlags & Modifiers::kOut_Flag)) { | 2555 (intf.fVariable.fModifiers.fFlags & Modifiers::kOut_Flag)) { |
2550 interfaceVars.push_back(id); | 2556 interfaceVars.push_back(id); |
2551 } | 2557 } |
2552 } | 2558 } |
2553 } | 2559 } |
2554 for (size_t i = 0; i < program.fElements.size(); i++) { | 2560 for (size_t i = 0; i < program.fElements.size(); i++) { |
2555 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { | 2561 if (program.fElements[i]->fKind == ProgramElement::kVar_Kind) { |
2556 this->writeGlobalVars(((VarDeclaration&) *program.fElements[i]), bod
y); | 2562 this->writeGlobalVars(((VarDeclaration&) *program.fElements[i]), bod
y); |
2557 } | 2563 } |
2558 } | 2564 } |
2559 for (size_t i = 0; i < program.fElements.size(); i++) { | 2565 for (size_t i = 0; i < program.fElements.size(); i++) { |
2560 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { | 2566 if (program.fElements[i]->fKind == ProgramElement::kFunction_Kind) { |
2561 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); | 2567 this->writeFunction(((FunctionDefinition&) *program.fElements[i]), b
ody); |
2562 } | 2568 } |
2563 } | 2569 } |
2564 std::shared_ptr<FunctionDeclaration> main = nullptr; | 2570 const FunctionDeclaration* main = nullptr; |
2565 for (auto entry : fFunctionMap) { | 2571 for (auto entry : fFunctionMap) { |
2566 if (entry.first->fName == "main") { | 2572 if (entry.first->fName == "main") { |
2567 main = entry.first; | 2573 main = entry.first; |
2568 } | 2574 } |
2569 } | 2575 } |
2570 ASSERT(main); | 2576 ASSERT(main); |
2571 for (auto entry : fVariableMap) { | 2577 for (auto entry : fVariableMap) { |
2572 std::shared_ptr<Variable> var = entry.first; | 2578 const Variable* var = entry.first; |
2573 if (var->fStorage == Variable::kGlobal_Storage && | 2579 if (var->fStorage == Variable::kGlobal_Storage && |
2574 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) || | 2580 ((var->fModifiers.fFlags & Modifiers::kIn_Flag) || |
2575 (var->fModifiers.fFlags & Modifiers::kOut_Flag))) { | 2581 (var->fModifiers.fFlags & Modifiers::kOut_Flag))) { |
2576 interfaceVars.push_back(entry.second); | 2582 interfaceVars.push_back(entry.second); |
2577 } | 2583 } |
2578 } | 2584 } |
2579 this->writeCapabilities(out); | 2585 this->writeCapabilities(out); |
2580 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.
std.450", out); | 2586 this->writeInstruction(SpvOpExtInstImport, fGLSLExtendedInstructions, "GLSL.
std.450", out); |
2581 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemor
yModelGLSL450, out); | 2587 this->writeInstruction(SpvOpMemoryModel, SpvAddressingModelLogical, SpvMemor
yModelGLSL450, out); |
2582 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (strlen(main->fName.c_str())
+ 4) / 4) + | 2588 this->writeOpCode(SpvOpEntryPoint, (SpvId) (3 + (strlen(main->fName.c_str())
+ 4) / 4) + |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2620 this->writeWord(SpvVersion, out); | 2626 this->writeWord(SpvVersion, out); |
2621 this->writeWord(SKSL_MAGIC, out); | 2627 this->writeWord(SKSL_MAGIC, out); |
2622 std::stringstream buffer; | 2628 std::stringstream buffer; |
2623 this->writeInstructions(program, buffer); | 2629 this->writeInstructions(program, buffer); |
2624 this->writeWord(fIdCount, out); | 2630 this->writeWord(fIdCount, out); |
2625 this->writeWord(0, out); // reserved, always zero | 2631 this->writeWord(0, out); // reserved, always zero |
2626 out << buffer.str(); | 2632 out << buffer.str(); |
2627 } | 2633 } |
2628 | 2634 |
2629 } | 2635 } |
OLD | NEW |