Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: src/sksl/SkSLSPIRVCodeGenerator.cpp

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

Powered by Google App Engine
This is Rietveld 408576698