/** * Check if a mapping is valid by checking the types and swizzle of both of * the variables * * @param mapping the mapping * @return true if this mapping is valid */ public static boolean typesMatch(VariableMapping mapping) { String leftType = mapping.getLeftVariable().getType(); String rightType = mapping.getRightVariable().getType(); String leftSwizzling = mapping.getLeftSwizzling(); String rightSwizzling = mapping.getRightSwizzling(); //types match : no error if (leftType.equals(rightType) && leftSwizzling.length() == rightSwizzling.length()) { return true; } if (isSwizzlable(leftType) && isSwizzlable(rightType)) { if (getCardinality(leftType, leftSwizzling) == getCardinality(rightType, rightSwizzling)) { return true; } } return false; }
/** * check the types of a mapping, left type must match right type take the * swizzle into account * * @param mapping the mapping * @param statement1 the statement being read * @throws MatParseException */ protected void checkTypes(VariableMapping mapping, Statement statement1) throws MatParseException { if (!ShaderUtils.typesMatch(mapping)) { String ls = mapping.getLeftSwizzling().length() == 0 ? "" : "." + mapping.getLeftSwizzling(); String rs = mapping.getRightSwizzling().length() == 0 ? "" : "." + mapping.getRightSwizzling(); throw new MatParseException("Type mismatch, cannot convert " + mapping.getRightVariable().getType() + rs + " to " + mapping.getLeftVariable().getType() + ls, statement1); } if (!ShaderUtils.multiplicityMatch(mapping)) { String type1 = mapping.getLeftVariable().getType() + "[" + mapping.getLeftVariable().getMultiplicity() + "]"; String type2 = mapping.getRightVariable().getType() + "[" + mapping.getRightVariable().getMultiplicity() + "]"; throw new MatParseException("Type mismatch, cannot convert " + type1 + " to " + type2, statement1); } }
/** * Updates the type of the right variable of a mapping from the type of the * left variable. * * @param mapping the mapping to consider. */ protected void updateRightTypeFromLeftType(VariableMapping mapping) { String type = mapping.getLeftVariable().getType(); int card = ShaderUtils.getCardinality(type, mapping.getRightSwizzling()); if (card > 0) { if (card == 1) { type = "float"; } else { type = "vec" + card; } } mapping.getRightVariable().setType(type); }
/** * return the cardinality of a type and a swizzle example : vec4 cardinality * is 4 float cardinality is 1 vec4.xyz cardinality is 3. sampler2D * cardinality is 0 * * @param type the glsl type * @param swizzling the swizzling of a variable * @return the cardinality */ public static int getCardinality(String type, String swizzling) { int card = 0; if (isSwizzlable(type)) { if (type.equals("float")) { card = 1; if (swizzling.length() != 0) { card = 0; } } else { card = Integer.parseInt(type.replaceAll(".*vec", "")); if (swizzling.length() > 0) { card = swizzling.length(); } } } return card; }
/** * check the types of a mapping, left type must match right type take the * swizzle into account * * @param mapping the mapping * @param statement1 the statement being read * @throws MatParseException */ protected void checkTypes(VariableMapping mapping, Statement statement1) throws MatParseException { if (!ShaderUtils.typesMatch(mapping)) { String ls = mapping.getLeftSwizzling().length() == 0 ? "" : "." + mapping.getLeftSwizzling(); String rs = mapping.getRightSwizzling().length() == 0 ? "" : "." + mapping.getRightSwizzling(); throw new MatParseException("Type mismatch, cannot convert " + mapping.getRightVariable().getType() + rs + " to " + mapping.getLeftVariable().getType() + ls, statement1); } if (!ShaderUtils.multiplicityMatch(mapping)) { String type1 = mapping.getLeftVariable().getType() + "[" + mapping.getLeftVariable().getMultiplicity() + "]"; String type2 = mapping.getRightVariable().getType() + "[" + mapping.getRightVariable().getMultiplicity() + "]"; throw new MatParseException("Type mismatch, cannot convert " + type1 + " to " + type2, statement1); } }
/** * updates the type of the right variable of a mapping from the type of the * left variable * * @param mapping the mapping to consider */ protected void updateRightTypeFromLeftType(VariableMapping mapping) { String type = mapping.getLeftVariable().getType(); int card = ShaderUtils.getCardinality(type, mapping.getRightSwizzling()); if (card > 0) { if (card == 1) { type = "float"; } else { type = "vec" + card; } } mapping.getRightVariable().setType(type); }
/** * return the cardinality of a type and a swizzle example : vec4 cardinality * is 4 float cardinality is 1 vec4.xyz cardinality is 3. sampler2D * cardinality is 0 * * @param type the glsl type * @param swizzling the swizzling of a variable * @return the cardinality */ public static int getCardinality(String type, String swizzling) { int card = 0; if (isSwizzlable(type)) { if (type.equals("float")) { card = 1; if (swizzling.length() != 0) { card = 0; } } else { card = Integer.parseInt(type.replaceAll(".*vec", "")); if (swizzling.length() > 0) { card = swizzling.length(); } } } return card; }
/** * Check if a mapping is valid by checking the types and swizzle of both of * the variables * * @param mapping the mapping * @return true if this mapping is valid */ public static boolean typesMatch(VariableMapping mapping) { String leftType = mapping.getLeftVariable().getType(); String rightType = mapping.getRightVariable().getType(); String leftSwizzling = mapping.getLeftSwizzling(); String rightSwizzling = mapping.getRightSwizzling(); //types match : no error if (leftType.equals(rightType) && leftSwizzling.length() == rightSwizzling.length()) { return true; } if (isSwizzlable(leftType) && isSwizzlable(rightType)) { if (getCardinality(leftType, leftSwizzling) == getCardinality(rightType, rightSwizzling)) { return true; } } return false; }