Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Drawie.Backend.Core/Shaders/Generation/BuiltInFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public partial class BuiltInFunctions

private const string Epsilon = "1e-10";

public Expression GetUnpremultiply(Expression rgbaHalf4) => Call(Unpremultiply, rgbaHalf4);

public Expression GetRgbToHsv(Expression rgba) => Call(RgbToHsv, rgba);

public Expression GetRgbToHsl(Expression rgba) => Call(RgbToHsl, rgba);
Expand Down Expand Up @@ -65,6 +67,17 @@ private void Require(IBuiltInFunction function)
usedFunctions.Add(function);
}

private static readonly BuiltInFunction<Half4> Unpremultiply = new(
"half4 color",
nameof(Unpremultiply),
"""
if (color.a <= 0.0) {
return half4(0, 0, 0, 0);
}
return half4(color.rgb / color.a, color.a);
"""
);

// Taken from here https://www.shadertoy.com/view/4dKcWK
private static readonly BuiltInFunction<Half3> HueToRgb = new(
"float hue",
Expand Down
21 changes: 15 additions & 6 deletions src/Drawie.Backend.Core/Shaders/Generation/ShaderBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,29 @@ public SurfaceSampler AddOrGetSurface(DrawingSurface surface, ColorSampleMode sa

public Half4 Sample(SurfaceSampler texName, Expression pos, bool normalizedCoordinates)
{
string resultName = $"color_{GetUniqueNameNumber()}";
Half4 result = new Half4(resultName);
_variables.Add(result);
string rawResultName = $"color_{GetUniqueNameNumber()}";
Half4 rawResult = new Half4(rawResultName);
_variables.Add(rawResult);

string unpremulResultName = $"color_{GetUniqueNameNumber()}";
Half4 unpremulResult = new Half4(unpremulResultName);
_variables.Add(unpremulResult);

if (normalizedCoordinates)
{
_bodyBuilder.AppendLine(
$"half4 {resultName} = {texName.VariableName}.eval({pos.ExpressionValue} * iResolution);");
$"half4 {rawResultName} = {texName.VariableName}.eval({pos.ExpressionValue} * iResolution);");
_bodyBuilder.AppendLine(
$"half4 {unpremulResultName} = {Functions.GetUnpremultiply(rawResult).ExpressionValue};");
}
else
{
_bodyBuilder.AppendLine($"half4 {resultName} = {texName.VariableName}.eval({pos.ExpressionValue});");
_bodyBuilder.AppendLine($"half4 {rawResultName} = {texName.VariableName}.eval({pos.ExpressionValue});");
_bodyBuilder.AppendLine(
$"half4 {unpremulResultName} = {Functions.GetUnpremultiply(rawResult).ExpressionValue};");
}

return result;
return unpremulResult;
}

public void ReturnVar(Half4 colorValue, bool premultiply)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,14 @@ internal static List<UniformDeclaration> DeclarationsFromEffect(string code, SKR
return null;
}

private static string RemoveLastOccurance(string text, string toRemove)
{
int index = text.LastIndexOf(toRemove, StringComparison.Ordinal);
return index < 0 ?
text :
text.Substring(0, index) + text.Substring(index + toRemove.Length);
}

private static bool TryDetectType(string lastString, string name, out UniformValueType? detectedType)
{
if (!lastString.Contains("uniform ", StringComparison.InvariantCultureIgnoreCase))
Expand All @@ -561,7 +569,7 @@ private static bool TryDetectType(string lastString, string name, out UniformVal
return false;
}

string nameLessBlock = lastString.Replace(name, string.Empty);
string nameLessBlock = RemoveLastOccurance(lastString, name);

if (nameLessBlock.Contains("color", StringComparison.InvariantCultureIgnoreCase))
{
Expand Down