Skip to content

Commit 80a03dc

Browse files
svc-reach-platform-supportEvergreen
authored and
Evergreen
committed
[Port] [6000.0][UUM-87636] Fix sprite instancing with 2d shaders
1 parent 53297a2 commit 80a03dc

35 files changed

+5287
-18
lines changed

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteLitPass.hlsl

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ half4 _RendererColor;
2525
PackedVaryings vert(Attributes input)
2626
{
2727
Varyings output = (Varyings)0;
28+
UNITY_SETUP_INSTANCE_ID(input);
29+
30+
SetUpSpriteInstanceProperties();
2831
input.positionOS = UnityFlipSprite(input.positionOS, unity_SpriteProps.xy);
2932
output = BuildVaryings(input);
3033
output.color *= _RendererColor * unity_SpriteColor; // vertex color has to applied here

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteNormalPass.hlsl

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ PackedVaryings vert(Attributes input)
55
Varyings output = (Varyings)0;
66
input.positionOS = UnityFlipSprite(input.positionOS, unity_SpriteProps.xy);
77
output = BuildVaryings(input);
8+
output.color *= unity_SpriteColor;
89
output.normalWS = -GetViewForwardDir();
910
PackedVaryings packedOutput = PackVaryings(output);
1011
return packedOutput;

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Includes/SpriteUnlitPass.hlsl

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ half4 _RendererColor;
77
PackedVaryings vert(Attributes input)
88
{
99
Varyings output = (Varyings)0;
10+
UNITY_SETUP_INSTANCE_ID(input);
11+
12+
SetUpSpriteInstanceProperties();
1013
input.positionOS = UnityFlipSprite(input.positionOS, unity_SpriteProps.xy);
1114
output = BuildVaryings(input);
1215
output.color *= _RendererColor * unity_SpriteColor; // vertex color has to applied here

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteCustomLitSubTarget.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ public override void Setup(ref TargetSetupContext context)
2121
{
2222
base.Setup(ref context);
2323
context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
24-
#if HAS_VFX_GRAPH
24+
2525
var universalRPType = typeof(UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset);
26-
if (TargetsVFX() && !context.HasCustomEditorForRenderPipeline(universalRPType))
27-
{
28-
context.AddCustomEditorForRenderPipeline(typeof(VFXGenericShaderGraphMaterialGUI).FullName, universalRPType);
29-
}
26+
var gui = typeof(ShaderGraphSpriteGUI);
27+
#if HAS_VFX_GRAPH
28+
if (TargetsVFX())
29+
gui = typeof(VFXGenericShaderGraphMaterialGUI);
3030
#endif
31+
context.AddCustomEditorForRenderPipeline(gui.FullName, universalRPType);
3132
context.AddSubShader(PostProcessSubShader(SubShaders.SpriteLit(target)));
3233
}
3334

@@ -244,6 +245,7 @@ static class SpriteLitRequiredFields
244245

245246
public static FieldCollection Normal = new FieldCollection()
246247
{
248+
StructFields.Varyings.color,
247249
StructFields.Varyings.normalWS,
248250
StructFields.Varyings.tangentWS,
249251
};

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteLitSubTarget.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ public override void Setup(ref TargetSetupContext context)
2323
{
2424
base.Setup(ref context);
2525
context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
26-
#if HAS_VFX_GRAPH
26+
2727
var universalRPType = typeof(UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset);
28-
if (TargetsVFX() && !context.HasCustomEditorForRenderPipeline(universalRPType))
29-
{
30-
context.AddCustomEditorForRenderPipeline(typeof(VFXGenericShaderGraphMaterialGUI).FullName, universalRPType);
31-
}
28+
var gui = typeof(ShaderGraphSpriteGUI);
29+
#if HAS_VFX_GRAPH
30+
if (TargetsVFX())
31+
gui = typeof(VFXGenericShaderGraphMaterialGUI);
3232
#endif
33+
context.AddCustomEditorForRenderPipeline(gui.FullName, universalRPType);
3334
context.AddSubShader(PostProcessSubShader(SubShaders.SpriteLit(target)));
3435
}
3536

@@ -266,6 +267,7 @@ static class SpriteLitRequiredFields
266267

267268
public static FieldCollection Normal = new FieldCollection()
268269
{
270+
StructFields.Varyings.color,
269271
StructFields.Varyings.normalWS,
270272
StructFields.Varyings.tangentWS,
271273
};

Packages/com.unity.render-pipelines.universal/Editor/2D/ShaderGraph/Targets/UniversalSpriteUnlitSubTarget.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ public override void Setup(ref TargetSetupContext context)
2323
{
2424
base.Setup(ref context);
2525
context.AddAssetDependency(kSourceCodeGuid, AssetCollection.Flags.SourceDependency);
26-
#if HAS_VFX_GRAPH
26+
2727
var universalRPType = typeof(UnityEngine.Rendering.Universal.UniversalRenderPipelineAsset);
28-
if (TargetsVFX() && !context.HasCustomEditorForRenderPipeline(universalRPType))
29-
{
30-
context.AddCustomEditorForRenderPipeline(typeof(VFXGenericShaderGraphMaterialGUI).FullName, universalRPType);
31-
}
28+
var gui = typeof(ShaderGraphSpriteGUI);
29+
#if HAS_VFX_GRAPH
30+
if (TargetsVFX())
31+
gui = typeof(VFXGenericShaderGraphMaterialGUI);
3232
#endif
33+
context.AddCustomEditorForRenderPipeline(gui.FullName, universalRPType);
3334
context.AddSubShader(PostProcessSubShader(SubShaders.SpriteUnlit(target)));
3435
}
3536

Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
area: Material
1+
area: Materials
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using UnityEngine;
2+
3+
namespace UnityEditor
4+
{
5+
// Used for ShaderGraph Sprite shaders
6+
class ShaderGraphSpriteGUI : BaseShaderGUI
7+
{
8+
protected override uint materialFilter => uint.MaxValue & ~(uint)Expandable.SurfaceOptions;
9+
10+
MaterialProperty[] properties;
11+
12+
// collect properties from the material properties
13+
public override void FindProperties(MaterialProperty[] properties)
14+
{
15+
// save off the list of all properties for shadergraph
16+
this.properties = properties;
17+
18+
var material = materialEditor?.target as Material;
19+
if (material == null)
20+
return;
21+
22+
base.FindProperties(properties);
23+
}
24+
25+
public override void DrawSurfaceInputs(Material material)
26+
{
27+
DrawShaderGraphProperties(material, properties);
28+
}
29+
}
30+
}

Packages/com.unity.render-pipelines.universal/Editor/ShaderGUI/ShadergraphSpriteGUI.cs.meta

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Targets/UniversalTarget.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1650,6 +1650,7 @@ static class CorePragmas
16501650
{
16511651
{ Pragma.Target(ShaderModel.Target20) },
16521652
{ Pragma.ExcludeRenderers(new[] { Platform.D3D9 }) },
1653+
{ Pragma.MultiCompileInstancing },
16531654
{ Pragma.Vertex("vert") },
16541655
{ Pragma.Fragment("frag") },
16551656
};

Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/Core2D.hlsl

+19
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,23 @@ float3 UnitySkinSprite( in float3 positionOS, in float4 blendIndices, in float4
3737
return positionOS;
3838
}
3939

40+
#ifdef UNITY_INSTANCING_ENABLED
41+
UNITY_INSTANCING_BUFFER_START(PerDrawSprite)
42+
// SpriteRenderer.Color while Non-Batched/Instanced.
43+
UNITY_DEFINE_INSTANCED_PROP(float4, unity_SpriteRendererColorArray)
44+
// this could be smaller but that's how bit each entry is regardless of type
45+
UNITY_DEFINE_INSTANCED_PROP(float2, unity_SpriteFlipArray)
46+
UNITY_INSTANCING_BUFFER_END(PerDrawSprite)
47+
48+
#define unity_SpriteColor UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteRendererColorArray)
49+
#define unity_SpriteFlip UNITY_ACCESS_INSTANCED_PROP(PerDrawSprite, unity_SpriteFlipArray)
50+
#endif // instancing
51+
52+
void SetUpSpriteInstanceProperties()
53+
{
54+
#ifdef UNITY_INSTANCING_ENABLED
55+
unity_SpriteProps.xy = unity_SpriteFlip;
56+
#endif
57+
}
58+
4059
#endif

Packages/com.unity.render-pipelines.universal/Shaders/2D/Sprite-Lit-Default.shader

+10-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
3636

3737
#include_with_pragmas "Packages/com.unity.render-pipelines.universal/Shaders/2D/Include/ShapeLightShared.hlsl"
3838

39+
// GPU Instancing
40+
#pragma multi_compile_instancing
3941
#pragma multi_compile _ DEBUG_DISPLAY SKINNED_SPRITE
4042

4143
struct Attributes
@@ -97,6 +99,7 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
9799
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
98100
UNITY_SKINNED_VERTEX_COMPUTE(v);
99101

102+
SetUpSpriteInstanceProperties();
100103
v.positionOS = UnityFlipSprite(v.positionOS, unity_SpriteProps.xy);
101104
o.positionCS = TransformObjectToHClip(v.positionOS);
102105
#if defined(DEBUG_DISPLAY)
@@ -141,6 +144,8 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
141144
#pragma vertex NormalsRenderingVertex
142145
#pragma fragment NormalsRenderingFragment
143146

147+
// GPU Instancing
148+
#pragma multi_compile_instancing
144149
#pragma multi_compile _ SKINNED_SPRITE
145150

146151
struct Attributes
@@ -181,10 +186,11 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
181186
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
182187
UNITY_SKINNED_VERTEX_COMPUTE(attributes);
183188

189+
SetUpSpriteInstanceProperties();
184190
attributes.positionOS = UnityFlipSprite(attributes.positionOS, unity_SpriteProps.xy);
185191
o.positionCS = TransformObjectToHClip(attributes.positionOS);
186192
o.uv = attributes.uv;
187-
o.color = attributes.color;
193+
o.color = attributes.color * _Color * unity_SpriteColor;
188194
o.normalWS = -GetViewForwardDir();
189195
o.tangentWS = TransformObjectToWorldDir(attributes.tangent.xyz);
190196
o.bitangentWS = cross(o.normalWS, o.tangentWS) * attributes.tangent.w;
@@ -218,6 +224,8 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
218224
#pragma vertex UnlitVertex
219225
#pragma fragment UnlitFragment
220226

227+
// GPU Instancing
228+
#pragma multi_compile_instancing
221229
#pragma multi_compile _ DEBUG_DISPLAY SKINNED_SPRITE
222230

223231
struct Attributes
@@ -256,6 +264,7 @@ Shader "Universal Render Pipeline/2D/Sprite-Lit-Default"
256264
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
257265
UNITY_SKINNED_VERTEX_COMPUTE(attributes);
258266

267+
SetUpSpriteInstanceProperties();
259268
attributes.positionOS = UnityFlipSprite( attributes.positionOS, unity_SpriteProps.xy);
260269
o.positionCS = TransformObjectToHClip(attributes.positionOS);
261270
#if defined(DEBUG_DISPLAY)

Packages/com.unity.render-pipelines.universal/Shaders/2D/Sprite-Unlit-Default.shader

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Shader "Universal Render Pipeline/2D/Sprite-Unlit-Default"
1616
{
1717
Tags {"Queue" = "Transparent" "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
1818

19-
Blend SrcAlpha OneMinusSrcAlpha
19+
Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
2020
Cull Off
2121
ZWrite Off
2222

@@ -37,6 +37,8 @@ Shader "Universal Render Pipeline/2D/Sprite-Unlit-Default"
3737
#pragma vertex UnlitVertex
3838
#pragma fragment UnlitFragment
3939

40+
// GPU Instancing
41+
#pragma multi_compile_instancing
4042
#pragma multi_compile _ DEBUG_DISPLAY SKINNED_SPRITE
4143

4244
struct Attributes
@@ -75,6 +77,7 @@ Shader "Universal Render Pipeline/2D/Sprite-Unlit-Default"
7577
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
7678
UNITY_SKINNED_VERTEX_COMPUTE(v);
7779

80+
SetUpSpriteInstanceProperties();
7881
v.positionOS = UnityFlipSprite(v.positionOS, unity_SpriteProps.xy);
7982
o.positionCS = TransformObjectToHClip(v.positionOS);
8083
#if defined(DEBUG_DISPLAY)
@@ -126,6 +129,8 @@ Shader "Universal Render Pipeline/2D/Sprite-Unlit-Default"
126129
#pragma vertex UnlitVertex
127130
#pragma fragment UnlitFragment
128131

132+
// GPU Instancing
133+
#pragma multi_compile_instancing
129134
#pragma multi_compile _ SKINNED_SPRITE
130135
#pragma multi_compile_fragment _ DEBUG_DISPLAY
131136

@@ -165,6 +170,7 @@ Shader "Universal Render Pipeline/2D/Sprite-Unlit-Default"
165170
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
166171
UNITY_SKINNED_VERTEX_COMPUTE(attributes);
167172

173+
SetUpSpriteInstanceProperties();
168174
attributes.positionOS = UnityFlipSprite(attributes.positionOS, unity_SpriteProps.xy);
169175
o.positionCS = TransformObjectToHClip(attributes.positionOS);
170176
#if defined(DEBUG_DISPLAY)

Tests/SRPTests/Projects/UniversalGraphicsTest_2D/Assets/Scenes/083_GPU_Instancing.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)