|
@@ -1,5 +1,6 @@
|
|
|
using System.Collections.Generic;
|
|
|
using System.Text;
|
|
|
+using PixiEditor.DrawingApi.Core.ColorsImpl;
|
|
|
using PixiEditor.DrawingApi.Core.Shaders.Generation.Expressions;
|
|
|
using PixiEditor.Numerics;
|
|
|
|
|
@@ -10,9 +11,9 @@ public class ShaderBuilder
|
|
|
public Uniforms Uniforms { get; } = new Uniforms();
|
|
|
|
|
|
private StringBuilder _bodyBuilder = new StringBuilder();
|
|
|
-
|
|
|
+
|
|
|
private List<ShaderExpressionVariable> _variables = new List<ShaderExpressionVariable>();
|
|
|
-
|
|
|
+
|
|
|
private Dictionary<Texture, TextureSampler> _samplers = new Dictionary<Texture, TextureSampler>();
|
|
|
|
|
|
public Shader BuildShader()
|
|
@@ -47,22 +48,22 @@ public class ShaderBuilder
|
|
|
{
|
|
|
return sampler;
|
|
|
}
|
|
|
-
|
|
|
- string name = $"texture_{Uniforms.Count}";
|
|
|
+
|
|
|
+ string name = $"texture_{GetUniqueNameNumber()}";
|
|
|
using var snapshot = texture.DrawingSurface.Snapshot();
|
|
|
Uniforms[name] = new Uniform(name, snapshot.ToShader());
|
|
|
var newSampler = new TextureSampler(name);
|
|
|
_samplers[texture] = newSampler;
|
|
|
-
|
|
|
+
|
|
|
return newSampler;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public Half4 Sample(TextureSampler texName, Float2 pos)
|
|
|
{
|
|
|
- string resultName = $"color_{_variables.Count}";
|
|
|
+ string resultName = $"color_{GetUniqueNameNumber()}";
|
|
|
Half4 result = new Half4(resultName);
|
|
|
_variables.Add(result);
|
|
|
- _bodyBuilder.AppendLine($"half4 {resultName} = sample({texName.UniformName}, {pos.UniformName});");
|
|
|
+ _bodyBuilder.AppendLine($"half4 {resultName} = sample({texName.UniformName}, {pos.UniformName});");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -70,56 +71,89 @@ public class ShaderBuilder
|
|
|
{
|
|
|
_bodyBuilder.AppendLine($"return {colorValue.UniformName};");
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void ReturnConst(Half4 colorValue)
|
|
|
{
|
|
|
_bodyBuilder.AppendLine($"return {colorValue.ConstantValueString};");
|
|
|
}
|
|
|
|
|
|
+ public void AddUniform(string uniformName, Color color)
|
|
|
+ {
|
|
|
+ Uniforms[uniformName] = new Uniform(uniformName, color);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void AddUniform(string coords, VecD constCoordsConstantValue)
|
|
|
+ {
|
|
|
+ Uniforms[coords] = new Uniform(coords, constCoordsConstantValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void AddUniform(string uniformName, float floatValue)
|
|
|
+ {
|
|
|
+ Uniforms[uniformName] = new Uniform(uniformName, floatValue);
|
|
|
+ }
|
|
|
+
|
|
|
public void Set<T>(T contextPosition, T coordinateValue) where T : ShaderExpressionVariable
|
|
|
{
|
|
|
+ if (contextPosition.UniformName == coordinateValue.UniformName)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
_bodyBuilder.AppendLine($"{contextPosition.UniformName} = {coordinateValue.UniformName};");
|
|
|
}
|
|
|
|
|
|
public void SetConstant<T>(T contextPosition, T constantValueVar) where T : ShaderExpressionVariable
|
|
|
{
|
|
|
- _bodyBuilder.AppendLine($"{contextPosition.UniformName} = {constantValueVar.ConstantValueString};");
|
|
|
+ _bodyBuilder.AppendLine($"{contextPosition.UniformName} = {constantValueVar.ConstantValueString};");
|
|
|
}
|
|
|
|
|
|
public Float2 ConstructFloat2(Expression x, Expression y)
|
|
|
{
|
|
|
- string name = $"vec2_{_variables.Count}";
|
|
|
+ string name = $"vec2_{GetUniqueNameNumber()}";
|
|
|
Float2 result = new Float2(name);
|
|
|
_variables.Add(result);
|
|
|
|
|
|
- string xExpression = x.ExpressionValue;
|
|
|
+ string xExpression = x.ExpressionValue;
|
|
|
string yExpression = y.ExpressionValue;
|
|
|
-
|
|
|
+
|
|
|
_bodyBuilder.AppendLine($"float2 {name} = float2({xExpression}, {yExpression});");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
public Float1 ConstructFloat1(Expression assignment)
|
|
|
{
|
|
|
- string name = $"float_{_variables.Count}";
|
|
|
+ string name = $"float_{GetUniqueNameNumber()}";
|
|
|
Float1 result = new Float1(name);
|
|
|
_variables.Add(result);
|
|
|
-
|
|
|
+
|
|
|
_bodyBuilder.AppendLine($"float {name} = {assignment.ExpressionValue};");
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ public Int2 ConstructInt2(Expression first, Expression second)
|
|
|
+ {
|
|
|
+ string name = $"int2_{GetUniqueNameNumber()}";
|
|
|
+ Int2 result = new Int2(name);
|
|
|
+ _variables.Add(result);
|
|
|
+
|
|
|
+ string firstExpression = first.ExpressionValue;
|
|
|
+ string secondExpression = second.ExpressionValue;
|
|
|
+
|
|
|
+ _bodyBuilder.AppendLine($"int2 {name} = int2({firstExpression}, {secondExpression});");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
public Half4 ConstructHalf4(Expression r, Expression g, Expression b, Expression a)
|
|
|
{
|
|
|
- string name = $"color_{_variables.Count}";
|
|
|
+ string name = $"color_{GetUniqueNameNumber()}";
|
|
|
Half4 result = new Half4(name);
|
|
|
_variables.Add(result);
|
|
|
-
|
|
|
+
|
|
|
string rExpression = r.ExpressionValue;
|
|
|
string gExpression = g.ExpressionValue;
|
|
|
string bExpression = b.ExpressionValue;
|
|
|
string aExpression = a.ExpressionValue;
|
|
|
-
|
|
|
+
|
|
|
_bodyBuilder.AppendLine($"half4 {name} = half4({rExpression}, {gExpression}, {bExpression}, {aExpression});");
|
|
|
return result;
|
|
|
}
|
|
@@ -129,10 +163,15 @@ public class ShaderBuilder
|
|
|
_bodyBuilder.Clear();
|
|
|
_variables.Clear();
|
|
|
_samplers.Clear();
|
|
|
-
|
|
|
+
|
|
|
foreach (var uniform in Uniforms)
|
|
|
{
|
|
|
uniform.Value.Dispose();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public string GetUniqueNameNumber()
|
|
|
+ {
|
|
|
+ return (_variables.Count + Uniforms.Count + 1).ToString();
|
|
|
+ }
|
|
|
}
|