Skip to content

Commit

Permalink
Added VirtualTexture property type (no list yet), got everything comp…
Browse files Browse the repository at this point in the history
…iling again.
  • Loading branch information
Chris Tchou committed Apr 22, 2020
1 parent 61481e2 commit ee59f50
Show file tree
Hide file tree
Showing 17 changed files with 301 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ public ConditionalField[] GetConditionalFields(PassDescriptor pass)
// Ideally we do this another way but HDLit needs this for conditional pragmas
var shaderProperties = new PropertyCollector();
owner.CollectShaderProperties(shaderProperties, GenerationMode.ForReals);
bool hasDotsProperties = shaderProperties.GetDotsInstancingPropertiesCount(GenerationMode.ForReals) > 0;
bool hasDotsProperties = shaderProperties.DotsInstancingProperties(GenerationMode.ForReals).Any();

return new ConditionalField[]
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Rendering;
Expand Down Expand Up @@ -630,6 +630,7 @@ static class CoreIncludes
{
// CorePregraph
const string kCommon = "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl";
const string kTextureStack = "Packages/com.unity.render-pipelines.core/ShaderLibrary/TextureStack.hlsl";
const string kShaderVariables = "Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/ShaderVariables.hlsl";
const string kFragInputs = "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/FragInputs.hlsl";
const string kShaderPass = "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/ShaderPass.cs.hlsl";
Expand Down Expand Up @@ -697,6 +698,7 @@ static class CoreIncludes
public static IncludeCollection CorePregraph = new IncludeCollection
{
{ kCommon, IncludeLocation.Pregraph },
{ kTextureStack, IncludeLocation.Pregraph }, // TODO: put this on a conditional
{ kShaderVariables, IncludeLocation.Pregraph },
{ kFragInputs, IncludeLocation.Pregraph },
{ kShaderPass, IncludeLocation.Pregraph },
Expand Down
20 changes: 20 additions & 0 deletions com.unity.shadergraph/Editor/Data/Graphs/AbstractShaderProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@ internal void ValidateConcretePrecision(ConcretePrecision graphPrecision)
m_ConcretePrecision = (precision == Precision.Inherit) ? graphPrecision : precision.ToConcrete();
}

// the simple interface for simple properties
internal abstract bool isBatchable { get; }

// the more complex interface for complex properties (defaulted for simple properties)
internal virtual bool hasBatchableProperties { get { return isBatchable; } }
internal virtual bool hasNonBatchableProperties { get { return !isBatchable; } }

[SerializeField]
bool m_Hidden = false;

Expand All @@ -55,12 +60,27 @@ internal virtual string GetPropertyBlockString()
return string.Empty;
}

// the simple interface for simple properties
internal virtual string GetPropertyDeclarationString(string delimiter = ";")
{
SlotValueType type = ConcreteSlotValueType.Vector4.ToSlotValueType();
return $"{concreteShaderValueType.ToShaderString(concretePrecision.ToShaderString())} {referenceName}{delimiter}";
}

// the more complex interface for complex properties (defaulted for simple properties)
internal virtual void AppendBatchablePropertyDeclarations(ShaderStringBuilder builder, string delimiter = ";")
{
if (isBatchable)
builder.AppendLine(GetPropertyDeclarationString(delimiter));
}

// the more complex interface for complex properties (defaulted for simple properties)
internal virtual void AppendNonBatchablePropertyDeclarations(ShaderStringBuilder builder, string delimiter = ";")
{
if (!isBatchable)
builder.AppendLine(GetPropertyDeclarationString(delimiter));
}

internal virtual string GetPropertyAsArgumentString()
{
return GetPropertyDeclarationString(string.Empty);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace UnityEditor.ShaderGraph.Internal
{
[Serializable]
public sealed class SerializableVirtualTexture : ISerializationCallbackReceiver
{
[SerializeField]
public List<string> layerNames = new List<string>();

[SerializeField]
public List<SerializableTexture> layerTextures = new List<SerializableTexture>();

[SerializeField]
public bool procedural;

public void OnBeforeSerialize()
{
}

public void OnAfterDeserialize()
{
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.ShaderGraph.Drawing.Controls;
using UnityEngine;
using UnityEditor.Graphing;
using UnityEditor.ShaderGraph.Internal;

namespace UnityEditor.ShaderGraph
{
[Serializable]
class VirtualTextureShaderProperty : AbstractShaderProperty<SerializableVirtualTexture>
{
public VirtualTextureShaderProperty()
{
displayName = "VirtualTexture";
value = new SerializableVirtualTexture();

// add at least one layer
value.layerNames.Add("Layer0");
value.layerTextures.Add(new SerializableTexture());
}

public override PropertyType propertyType => PropertyType.VirtualTexture;

// isBatchable should never be called of we override hasBatchable / hasNonBatchableProperties
internal override bool isBatchable
{
get { throw new NotImplementedException(); }
}

internal override bool hasBatchableProperties => true;
internal override bool hasNonBatchableProperties => true;

internal override bool isExposable => true; // the textures are exposable at least..
internal override bool isRenamable => true;

// this is used for properties exposed to the Material in the shaderlab Properties{} block
internal override string GetPropertyBlockString()
{
// something along the lines of:
// [TextureStack.MyStack(0)] [NoScaleOffset] Layer0("Layer0", 2D) = "white" {}
string result = "";
for (int layer= 0; layer < value.layerNames.Count; layer++)
{
string layerName = value.layerNames[layer];
result += $"[TextureStack.{referenceName}({layer})] [NoScaleOffset] {layerName}(\"{layerName}\", 2D) = \"white\" {{}}{Environment.NewLine}";
}
return result;
}

internal override void AppendBatchablePropertyDeclarations(ShaderStringBuilder builder, string delimiter = ";")
{
int numLayers = value.layerNames.Count;
if (numLayers > 0)
{
builder.Append("DECLARE_STACK_CB(");
builder.Append(referenceName);
builder.Append(")");
builder.AppendLine(delimiter);
}
}

internal override void AppendNonBatchablePropertyDeclarations(ShaderStringBuilder builder, string delimiter = ";")
{
int numLayers = value.layerNames.Count;
if (numLayers > 0)
{
builder.Append("DECLARE_STACK");
builder.Append((numLayers <= 1) ? "" : numLayers.ToString());
builder.Append("(");
builder.Append(referenceName);
builder.Append(",");
for (int i = 0; i < value.layerNames.Count; i++)
{
if (i != 0) builder.Append(",");
builder.Append(value.layerNames[i]);
}
builder.Append(")");
builder.AppendLine(delimiter); // TODO: don't like delimiter, pretty sure it's not necessary if we invert the defaults on GEtPropertyDeclaration / GetPropertyArgument string
}
}

internal override string GetPropertyDeclarationString(string delimiter = ";")
{
throw new NotImplementedException();
}

// argument string used to pass this property to a subgraph
internal override string GetPropertyAsArgumentString()
{
// throw new NotImplementedException();
return "VirtualTexturePropertyArgumentStringGoesHere " + referenceName;
}

// if a blackboard property is deleted, all node instances of it are replaced with this:
internal override AbstractMaterialNode ToConcreteNode()
{
// TODO:
return null; // new GradientNode { };
}

internal override PreviewProperty GetPreviewMaterialProperty()
{
return new PreviewProperty(propertyType)
{
name = referenceName
};
}

internal override ShaderInput Copy()
{
return new VirtualTextureShaderProperty
{
displayName = displayName,
hidden = hidden,
value = value,
precision = precision
};
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions com.unity.shadergraph/Editor/Data/Nodes/Input/PropertyNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ void AddOutputSlot(AbstractShaderProperty property)
AddSlot(new GradientMaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
break;
case ConcreteSlotValueType.VirtualTexture:
// TODO: implement VT slot type
AddSlot(new Texture2DMaterialSlot(OutputSlotId, property.displayName, "Out", SlotType.Output));
RemoveSlotsNameNotMatching(new[] { OutputSlotId });
// AddSlot(new VirtualTextureSlot(OutputSlotId, property.displayName, "Out", SlotType.Output));
// RemoveSlotsNameNotMatching(new[] { OutputSlotId });
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
3 changes: 2 additions & 1 deletion com.unity.shadergraph/Editor/Data/Nodes/SlotValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ enum ConcreteSlotValueType
Vector3,
Vector2,
Vector1,
Boolean
Boolean,
VirtualTexture
}

static class SlotValueHelper
Expand Down
48 changes: 0 additions & 48 deletions com.unity.shadergraph/Editor/Data/Util/IncludeRegistry.cs

This file was deleted.

2 changes: 2 additions & 0 deletions com.unity.shadergraph/Editor/Data/Util/PropertyUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public static ConcreteSlotValueType ToConcreteShaderValueType(this PropertyType
return ConcreteSlotValueType.Boolean;
case PropertyType.Color:
return ConcreteSlotValueType.Vector4;
case PropertyType.VirtualTexture:
return ConcreteSlotValueType.VirtualTexture;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ public override void BuildCustomFields(ShaderInput input)
case GradientShaderProperty gradientProperty:
BuildGradientPropertyField(gradientProperty);
break;
case VirtualTextureShaderProperty virtualTextureProperty:
BuildVirtualTexturePropertyField(virtualTextureProperty);
break;
default:
throw new ArgumentOutOfRangeException();
}
Expand Down Expand Up @@ -331,6 +334,19 @@ void BuildTexture2DPropertyField(Texture2DShaderProperty property)
AddRow("Mode", defaultModeField, !graph.isSubGraph && property.generatePropertyBlock);
}

void BuildVirtualTexturePropertyField(VirtualTextureShaderProperty property)
{
Toggle proceduralToggle = new Toggle { value = property.value.procedural };
proceduralToggle.OnToggleChanged(evt =>
{
graph.owner.RegisterCompleteObjectUndo("Change VT Procedural Toggle");
property.value.procedural = evt.newValue;
DirtyNodes(ModificationScope.Graph);
});
AddRow("Procedural", proceduralToggle);
// TODO: add layer names and texture assignments view here (could we re-use the TextureShaderProperty custom builder?)
}

void BuildTexture2DArrayPropertyField(Texture2DArrayShaderProperty property)
{
var field = new ObjectField { value = property.value.textureArray, objectType = typeof(Texture2DArray) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ void AddPropertyItems(GenericMenu gm)
gm.AddItem(new GUIContent($"Texture2D Array"), false, () => AddInputRow(new Texture2DArrayShaderProperty(), true));
gm.AddItem(new GUIContent($"Texture3D"), false, () => AddInputRow(new Texture3DShaderProperty(), true));
gm.AddItem(new GUIContent($"Cubemap"), false, () => AddInputRow(new CubemapShaderProperty(), true));
gm.AddItem(new GUIContent($"Virtual Texture"), false, () => AddInputRow(new VirtualTextureShaderProperty(), true));
gm.AddItem(new GUIContent($"Boolean"), false, () => AddInputRow(new BooleanShaderProperty(), true));
gm.AddItem(new GUIContent($"Matrix2x2"), false, () => AddInputRow(new Matrix2ShaderProperty(), true));
gm.AddItem(new GUIContent($"Matrix3x3"), false, () => AddInputRow(new Matrix3ShaderProperty(), true));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace UnityEditor.ShaderGraph.Internal
namespace UnityEditor.ShaderGraph.Internal
{
[GenerationAPI]
public enum PropertyType
Expand All @@ -17,6 +17,7 @@ public enum PropertyType
Matrix2,
Matrix3,
Matrix4,
SamplerState
SamplerState,
VirtualTexture
}
}
Loading

0 comments on commit ee59f50

Please sign in to comment.