OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkSLIRGenerator.h" | 8 #include "SkSLIRGenerator.h" |
9 | 9 |
10 #include "limits.h" | 10 #include "limits.h" |
(...skipping 1094 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1105 } else { | 1105 } else { |
1106 return ifFalse; | 1106 return ifFalse; |
1107 } | 1107 } |
1108 } | 1108 } |
1109 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio
n, | 1109 return std::unique_ptr<Expression>(new TernaryExpression(expression.fPositio
n, |
1110 std::move(test), | 1110 std::move(test), |
1111 std::move(ifTrue), | 1111 std::move(ifTrue), |
1112 std::move(ifFalse))
); | 1112 std::move(ifFalse))
); |
1113 } | 1113 } |
1114 | 1114 |
| 1115 // scales the texture coordinates by the texture size for sampling rectangle tex
tures. |
| 1116 // For vec2 coordinates, implements the transformation: |
| 1117 // texture(sampler, coord) -> texture(sampler, textureSize(sampler) * coord) |
| 1118 // For vec3 coordinates, implements the transformation: |
| 1119 // texture(sampler, coord) -> texture(sampler, vec3(textureSize(sampler), 1.
0) * coord)) |
| 1120 void IRGenerator::fixRectSampling(std::vector<std::unique_ptr<Expression>>& argu
ments) { |
| 1121 ASSERT(arguments.size() == 2); |
| 1122 ASSERT(arguments[0]->fType == *fContext.fSampler2DRect_Type); |
| 1123 ASSERT(arguments[0]->fKind == Expression::kVariableReference_Kind); |
| 1124 const Variable& sampler = ((VariableReference&) *arguments[0]).fVariable; |
| 1125 const Symbol* textureSizeSymbol = (*fSymbolTable)["textureSize"]; |
| 1126 ASSERT(textureSizeSymbol->fKind == Symbol::kFunctionDeclaration_Kind); |
| 1127 const FunctionDeclaration& textureSize = (FunctionDeclaration&) *textureSize
Symbol; |
| 1128 std::vector<std::unique_ptr<Expression>> sizeArguments; |
| 1129 sizeArguments.emplace_back(new VariableReference(Position(), sampler)); |
| 1130 std::unique_ptr<Expression> vec2Size = call(Position(), textureSize, std::mo
ve(sizeArguments)); |
| 1131 const Type& type = arguments[1]->fType; |
| 1132 std::unique_ptr<Expression> scale; |
| 1133 if (type == *fContext.fVec2_Type) { |
| 1134 scale = std::move(vec2Size); |
| 1135 } else { |
| 1136 ASSERT(type == *fContext.fVec3_Type); |
| 1137 std::vector<std::unique_ptr<Expression>> vec3Arguments; |
| 1138 vec3Arguments.push_back(std::move(vec2Size)); |
| 1139 vec3Arguments.emplace_back(new FloatLiteral(fContext, Position(), 1.0)); |
| 1140 scale.reset(new Constructor(Position(), *fContext.fVec3_Type, std::move(
vec3Arguments))); |
| 1141 } |
| 1142 arguments[1].reset(new BinaryExpression(Position(), std::move(scale), Token:
:STAR, |
| 1143 std::move(arguments[1]), type)); |
| 1144 } |
| 1145 |
1115 std::unique_ptr<Expression> IRGenerator::call(Position position, | 1146 std::unique_ptr<Expression> IRGenerator::call(Position position, |
1116 const FunctionDeclaration& functio
n, | 1147 const FunctionDeclaration& functio
n, |
1117 std::vector<std::unique_ptr<Expres
sion>> arguments) { | 1148 std::vector<std::unique_ptr<Expres
sion>> arguments) { |
1118 if (function.fParameters.size() != arguments.size()) { | 1149 if (function.fParameters.size() != arguments.size()) { |
1119 String msg = "call to '" + function.fName + "' expected " + | 1150 String msg = "call to '" + function.fName + "' expected " + |
1120 to_string((uint64_t) function.fParameters.size(
)) + | 1151 to_string((uint64_t) function.fParameters.size(
)) + |
1121 " argument"; | 1152 " argument"; |
1122 if (function.fParameters.size() != 1) { | 1153 if (function.fParameters.size() != 1) { |
1123 msg += "s"; | 1154 msg += "s"; |
1124 } | 1155 } |
(...skipping 17 matching lines...) Expand all Loading... |
1142 } | 1173 } |
1143 for (size_t i = 0; i < arguments.size(); i++) { | 1174 for (size_t i = 0; i < arguments.size(); i++) { |
1144 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); | 1175 arguments[i] = this->coerce(std::move(arguments[i]), *types[i]); |
1145 if (!arguments[i]) { | 1176 if (!arguments[i]) { |
1146 return nullptr; | 1177 return nullptr; |
1147 } | 1178 } |
1148 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifi
ers::kOut_Flag)) { | 1179 if (arguments[i] && (function.fParameters[i]->fModifiers.fFlags & Modifi
ers::kOut_Flag)) { |
1149 this->markWrittenTo(*arguments[i], true); | 1180 this->markWrittenTo(*arguments[i], true); |
1150 } | 1181 } |
1151 } | 1182 } |
| 1183 if (function.fBuiltin && function.fName == "texture" && |
| 1184 arguments[0]->fType == *fContext.fSampler2DRect_Type) { |
| 1185 this->fixRectSampling(arguments); |
| 1186 } |
1152 return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType,
function, | 1187 return std::unique_ptr<FunctionCall>(new FunctionCall(position, *returnType,
function, |
1153 std::move(arguments)))
; | 1188 std::move(arguments)))
; |
1154 } | 1189 } |
1155 | 1190 |
1156 /** | 1191 /** |
1157 * Determines the cost of coercing the arguments of a function to the required t
ypes. Returns true | 1192 * Determines the cost of coercing the arguments of a function to the required t
ypes. Returns true |
1158 * if the cost could be computed, false if the call is not valid. Cost has no pa
rticular meaning | 1193 * if the cost could be computed, false if the call is not valid. Cost has no pa
rticular meaning |
1159 * other than "lower costs are preferred". | 1194 * other than "lower costs are preferred". |
1160 */ | 1195 */ |
1161 bool IRGenerator::determineCallCost(const FunctionDeclaration& function, | 1196 bool IRGenerator::determineCallCost(const FunctionDeclaration& function, |
(...skipping 516 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1678 case Expression::kIndex_Kind: | 1713 case Expression::kIndex_Kind: |
1679 this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite); | 1714 this->markWrittenTo(*((IndexExpression&) expr).fBase, readWrite); |
1680 break; | 1715 break; |
1681 default: | 1716 default: |
1682 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio
n() + "'"); | 1717 fErrors.error(expr.fPosition, "cannot assign to '" + expr.descriptio
n() + "'"); |
1683 break; | 1718 break; |
1684 } | 1719 } |
1685 } | 1720 } |
1686 | 1721 |
1687 } | 1722 } |
OLD | NEW |