Skip to content

Commit 73cbf2e

Browse files
april-roszkowskiEvergreen
authored andcommitted
[Port][6000.0][UUM-122106] Add "is HDR" option to texture shader properties
1 parent d7f6654 commit 73cbf2e

28 files changed

+5940
-334
lines changed

Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct UnityTexture2D
2323
SAMPLER(samplerstate);
2424
float4 texelSize;
2525
float4 scaleTranslate;
26+
float4 hdrDecode;
2627

2728
// these functions allows users to convert code using Texture2D to UnityTexture2D by simply changing the type of the variable
2829
// the existing texture macros will call these functions, which will forward the call to the texture appropriately
@@ -61,15 +62,16 @@ float4 tex2D(UnityTexture2D tex, float2 uv) { return SAMPLE_TEXT
6162
float4 tex2Dlod(UnityTexture2D tex, float4 uv0l) { return SAMPLE_TEXTURE2D_LOD(tex.tex, tex.samplerstate, uv0l.xy, uv0l.w); }
6263
float4 tex2Dbias(UnityTexture2D tex, float4 uv0b) { return SAMPLE_TEXTURE2D_BIAS(tex.tex, tex.samplerstate, uv0b.xy, uv0b.w); }
6364

64-
#define UnityBuildTexture2DStruct(n) UnityBuildTexture2DStructInternal(TEXTURE2D_ARGS(n, sampler##n), n##_TexelSize, n##_ST)
65-
#define UnityBuildTexture2DStructNoScale(n) UnityBuildTexture2DStructInternal(TEXTURE2D_ARGS(n, sampler##n), n##_TexelSize, float4(1, 1, 0, 0))
66-
UnityTexture2D UnityBuildTexture2DStructInternal(TEXTURE2D_PARAM(tex, samplerstate), float4 texelSize, float4 scaleTranslate)
65+
#define UnityBuildTexture2DStruct(n) UnityBuildTexture2DStructInternal(TEXTURE2D_ARGS(n, sampler##n), n##_TexelSize, n##_ST, float4(0, 0, 0, 0))
66+
#define UnityBuildTexture2DStructNoScale(n) UnityBuildTexture2DStructInternal(TEXTURE2D_ARGS(n, sampler##n), n##_TexelSize, float4(1, 1, 0, 0), float4(0, 0, 0, 0))
67+
UnityTexture2D UnityBuildTexture2DStructInternal(TEXTURE2D_PARAM(tex, samplerstate), float4 texelSize, float4 scaleTranslate, float4 hdrDecode)
6768
{
6869
UnityTexture2D result;
6970
result.tex = tex;
7071
result.samplerstate = samplerstate;
7172
result.texelSize = texelSize;
7273
result.scaleTranslate = scaleTranslate;
74+
result.hdrDecode = hdrDecode;
7375
return result;
7476
}
7577

@@ -78,6 +80,7 @@ struct UnityTexture2DArray
7880
{
7981
TEXTURE2D_ARRAY(tex);
8082
SAMPLER(samplerstate);
83+
float4 hdrDecode;
8184

8285
// these functions allows users to convert code using Texture2DArray to UnityTexture2DArray by simply changing the type of the variable
8386
// the existing texture macros will call these functions, which will forward the call to the texture appropriately
@@ -94,12 +97,13 @@ struct UnityTexture2DArray
9497
float4 Load(int4 pixel) { return LOAD_TEXTURE2D_ARRAY(tex, pixel.xy, pixel.z); }
9598
};
9699

97-
#define UnityBuildTexture2DArrayStruct(n) UnityBuildTexture2DArrayStructInternal(TEXTURE2D_ARRAY_ARGS(n, sampler##n))
98-
UnityTexture2DArray UnityBuildTexture2DArrayStructInternal(TEXTURE2D_ARRAY_PARAM(tex, samplerstate))
100+
#define UnityBuildTexture2DArrayStruct(n) UnityBuildTexture2DArrayStructInternal(TEXTURE2D_ARRAY_ARGS(n, sampler##n), float4(0, 0, 0, 0))
101+
UnityTexture2DArray UnityBuildTexture2DArrayStructInternal(TEXTURE2D_ARRAY_PARAM(tex, samplerstate), float4 hdrDecode)
99102
{
100103
UnityTexture2DArray result;
101104
result.tex = tex;
102105
result.samplerstate = samplerstate;
106+
result.hdrDecode = hdrDecode;
103107
return result;
104108
}
105109

@@ -108,6 +112,7 @@ struct UnityTextureCube
108112
{
109113
TEXTURECUBE(tex);
110114
SAMPLER(samplerstate);
115+
float4 hdrDecode;
111116

112117
// these functions allows users to convert code using TextureCube to UnityTextureCube by simply changing the type of the variable
113118
// the existing texture macros will call these functions, which will forward the call to the texture appropriately
@@ -128,12 +133,13 @@ struct UnityTextureCube
128133
float4 texCUBE(UnityTextureCube tex, float3 dir) { return SAMPLE_TEXTURECUBE(tex.tex, tex.samplerstate, dir); }
129134
float4 texCUBEbias(UnityTextureCube tex, float4 dirBias) { return SAMPLE_TEXTURECUBE_BIAS(tex.tex, tex.samplerstate, dirBias.xyz, dirBias.w); }
130135

131-
#define UnityBuildTextureCubeStruct(n) UnityBuildTextureCubeStructInternal(TEXTURECUBE_ARGS(n, sampler##n))
132-
UnityTextureCube UnityBuildTextureCubeStructInternal(TEXTURECUBE_PARAM(tex, samplerstate))
136+
#define UnityBuildTextureCubeStruct(n) UnityBuildTextureCubeStructInternal(TEXTURECUBE_ARGS(n, sampler##n), float4(0, 0, 0, 0))
137+
UnityTextureCube UnityBuildTextureCubeStructInternal(TEXTURECUBE_PARAM(tex, samplerstate), float4 hdrDecode)
133138
{
134139
UnityTextureCube result;
135140
result.tex = tex;
136141
result.samplerstate = samplerstate;
142+
result.hdrDecode = hdrDecode;
137143
return result;
138144
}
139145

@@ -142,11 +148,6 @@ struct UnityTexture3D
142148
{
143149
TEXTURE3D(tex);
144150
SAMPLER(samplerstate);
145-
146-
// This dummy field is unused in Unity 6.4 and earlier. Here, the field is added strictly as a dummy to work around UUM-133088
147-
// Without this additional field, UnityTexture3D and sampler3D_f have the exact same structure, which will cause DXC to consider
148-
// them as the same type. This causes to compilation errors due to the corresponding overloads of tex3D being considered ambiguous
149-
// by DXC. Adding the field resolves this. Starting from Unity 6.5, the field is actually used in code.
150151
float4 hdrDecode;
151152

152153
// these functions allows users to convert code using Texture3D to UnityTexture3D by simply changing the type of the variable
@@ -161,12 +162,13 @@ struct UnityTexture3D
161162

162163
float4 tex3D(UnityTexture3D tex, float3 uvw) { return SAMPLE_TEXTURE3D(tex.tex, tex.samplerstate, uvw); }
163164

164-
#define UnityBuildTexture3DStruct(n) UnityBuildTexture3DStructInternal(TEXTURE3D_ARGS(n, sampler##n))
165-
UnityTexture3D UnityBuildTexture3DStructInternal(TEXTURE3D_PARAM(tex, samplerstate))
165+
#define UnityBuildTexture3DStruct(n) UnityBuildTexture3DStructInternal(TEXTURE3D_ARGS(n, sampler##n), float4(0, 0, 0, 0))
166+
UnityTexture3D UnityBuildTexture3DStructInternal(TEXTURE3D_PARAM(tex, samplerstate), float4 hdrDecode)
166167
{
167168
UnityTexture3D result;
168169
result.tex = tex;
169170
result.samplerstate = samplerstate;
171+
result.hdrDecode = hdrDecode;
170172
return result;
171173
}
172174

Packages/com.unity.shadergraph/Editor/Data/Graphs/CubemapShaderProperty.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ internal CubemapShaderProperty()
2323

2424
internal string modifiableTagString => modifiable ? "" : "[NonModifiableTextureData]";
2525

26+
[SerializeField]
27+
internal bool isHDR = false;
28+
29+
internal string isHDRString => isHDR ? "[HDR]" : "";
30+
2631
internal override string GetPropertyBlockString()
2732
{
28-
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{referenceName}(\"{displayName}\", CUBE) = \"\" {{}}";
33+
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{isHDRString}{referenceName}(\"{displayName}\", CUBE) = \"\" {{}}";
2934
}
3035

3136
internal override bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.HybridPerInstance) && (decl != HLSLDeclaration.DoNotDeclare);
@@ -34,6 +39,8 @@ internal override void ForeachHLSLProperty(Action<HLSLProperty> action)
3439
{
3540
action(new HLSLProperty(HLSLType._TextureCube, referenceName, HLSLDeclaration.Global));
3641
action(new HLSLProperty(HLSLType._SamplerState, "sampler" + referenceName, HLSLDeclaration.Global));
42+
if (isHDR)
43+
action(new HLSLProperty(HLSLType._float4, referenceName + "_HDR", HLSLDeclaration.Global));
3744
}
3845

3946
internal override string GetPropertyAsArgumentString(string precisionString)
@@ -50,8 +57,11 @@ internal override string GetHLSLVariableName(bool isSubgraphProperty, Generation
5057
{
5158
if (isSubgraphProperty)
5259
return referenceName;
53-
else
54-
return $"UnityBuildTextureCubeStruct({referenceName})";
60+
61+
string nameArg = referenceName;
62+
string samplerArg = $"sampler{referenceName}";
63+
string hdrDecodeArg = isHDR ? $"{referenceName}_HDR" : "float4(0, 0, 0, 0)";
64+
return $"UnityBuildTextureCubeStructInternal({nameArg}, {samplerArg}, {hdrDecodeArg})";
5565
}
5666

5767
[SerializeField]

Packages/com.unity.shadergraph/Editor/Data/Graphs/Texture2DArrayShaderProperty.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ internal Texture2DArrayShaderProperty()
2121

2222
internal string modifiableTagString => modifiable ? "" : "[NonModifiableTextureData]";
2323

24+
[SerializeField]
25+
internal bool isHDR = false;
26+
27+
internal string isHDRString => isHDR ? "[HDR]" : "";
28+
2429
internal override string GetPropertyBlockString()
2530
{
26-
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{referenceName}(\"{displayName}\", 2DArray) = \"\" {{}}";
31+
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{isHDRString}{referenceName}(\"{displayName}\", 2DArray) = \"\" {{}}";
2732
}
2833

2934
internal override bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.HybridPerInstance) && (decl != HLSLDeclaration.DoNotDeclare);
@@ -32,6 +37,8 @@ internal override void ForeachHLSLProperty(Action<HLSLProperty> action)
3237
{
3338
action(new HLSLProperty(HLSLType._Texture2DArray, referenceName, HLSLDeclaration.Global));
3439
action(new HLSLProperty(HLSLType._SamplerState, "sampler" + referenceName, HLSLDeclaration.Global));
40+
if (isHDR)
41+
action(new HLSLProperty(HLSLType._float4, referenceName + "_HDR", HLSLDeclaration.Global));
3542
}
3643

3744
internal override string GetPropertyAsArgumentString(string precisionString)
@@ -48,8 +55,11 @@ internal override string GetHLSLVariableName(bool isSubgraphProperty, Generation
4855
{
4956
if (isSubgraphProperty)
5057
return referenceName;
51-
else
52-
return $"UnityBuildTexture2DArrayStruct({referenceName})";
58+
59+
string nameArg = referenceName;
60+
string samplerArg = $"sampler{referenceName}";
61+
string hdrDecodeArg = isHDR ? $"{referenceName}_HDR" : "float4(0, 0, 0, 0)";
62+
return $"UnityBuildTexture2DArrayStructInternal({nameArg}, {samplerArg}, {hdrDecodeArg})";
5363
}
5464

5565
[SerializeField]

Packages/com.unity.shadergraph/Editor/Data/Graphs/Texture2DShaderProperty.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,17 @@ internal Texture2DShaderProperty()
4949
[SerializeField]
5050
internal bool useTilingAndOffset = false;
5151

52+
[SerializeField]
53+
internal bool isHDR = false;
54+
5255
internal string useSTString => useTilingAndOffset ? "" : "[NoScaleOffset]";
56+
internal string isHDRString => isHDR ? "[HDR]" : "";
5357
internal string mainTextureString => isMainTexture ? "[MainTexture]" : "";
5458

5559
internal override string GetPropertyBlockString()
5660
{
5761
var normalTagString = (defaultType == DefaultType.NormalMap) ? "[Normal]" : "";
58-
return $"{hideTagString}{modifiableTagString}{normalTagString}{mainTextureString}{useSTString}{referenceName}(\"{displayName}\", 2D) = \"{ToShaderLabString(defaultType)}\" {{}}";
62+
return $"{hideTagString}{modifiableTagString}{normalTagString}{mainTextureString}{useSTString}{isHDRString}{referenceName}(\"{displayName}\", 2D) = \"{ToShaderLabString(defaultType)}\" {{}}";
5963
}
6064

6165
// Texture2D properties cannot be set via Hybrid path at the moment; disallow that choice
@@ -72,6 +76,10 @@ internal override void ForeachHLSLProperty(Action<HLSLProperty> action)
7276
{
7377
action(new HLSLProperty(HLSLType._float4, referenceName + "_ST", decl));
7478
}
79+
if (isHDR)
80+
{
81+
action(new HLSLProperty(HLSLType._float4, referenceName + "_HDR", decl));
82+
}
7583
}
7684

7785
internal override string GetPropertyAsArgumentString(string precisionString)
@@ -88,17 +96,13 @@ internal override string GetHLSLVariableName(bool isSubgraphProperty, Generation
8896
{
8997
if (isSubgraphProperty)
9098
return referenceName;
91-
else
92-
{
93-
if (useTilingAndOffset)
94-
{
95-
return $"UnityBuildTexture2DStruct({referenceName})";
96-
}
97-
else
98-
{
99-
return $"UnityBuildTexture2DStructNoScale({referenceName})";
100-
}
101-
}
99+
100+
string nameArg = referenceName;
101+
string samplerArg = $"sampler{referenceName}";
102+
string texelSizeArg = $"{referenceName}_TexelSize";
103+
string scaleTranslateArg = useTilingAndOffset ? $"{referenceName}_ST" : "float4(1, 1, 0, 0)";
104+
string hdrDecodeArg = isHDR ? $"{referenceName}_HDR" : "float4(0, 0, 0, 0)";
105+
return $"UnityBuildTexture2DStructInternal({nameArg}, {samplerArg}, {texelSizeArg}, {scaleTranslateArg}, {hdrDecodeArg})";
102106
}
103107

104108
[SerializeField]

Packages/com.unity.shadergraph/Editor/Data/Graphs/Texture3DShaderProperty.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ internal Texture3DShaderProperty()
2121

2222
internal string modifiableTagString => modifiable ? "" : "[NonModifiableTextureData]";
2323

24+
[SerializeField]
25+
internal bool isHDR = false;
26+
27+
internal string isHDRString => isHDR ? "[HDR]" : "";
28+
2429
internal override string GetPropertyBlockString()
2530
{
26-
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{referenceName}(\"{displayName}\", 3D) = \"white\" {{}}";
31+
return $"{hideTagString}{modifiableTagString}[NoScaleOffset]{isHDRString}{referenceName}(\"{displayName}\", 3D) = \"white\" {{}}";
2732
}
2833

2934
internal override bool AllowHLSLDeclaration(HLSLDeclaration decl) => (decl != HLSLDeclaration.HybridPerInstance) && (decl != HLSLDeclaration.DoNotDeclare);
@@ -32,6 +37,8 @@ internal override void ForeachHLSLProperty(Action<HLSLProperty> action)
3237
{
3338
action(new HLSLProperty(HLSLType._Texture3D, referenceName, HLSLDeclaration.Global));
3439
action(new HLSLProperty(HLSLType._SamplerState, "sampler" + referenceName, HLSLDeclaration.Global));
40+
if (isHDR)
41+
action(new HLSLProperty(HLSLType._float4, referenceName + "_HDR", HLSLDeclaration.Global));
3542
}
3643

3744
internal override string GetPropertyAsArgumentString(string precisionString)
@@ -48,8 +55,11 @@ internal override string GetHLSLVariableName(bool isSubgraphProperty, Generation
4855
{
4956
if (isSubgraphProperty)
5057
return referenceName;
51-
else
52-
return $"UnityBuildTexture3DStruct({referenceName})";
58+
59+
string nameArg = referenceName;
60+
string samplerArg = $"sampler{referenceName}";
61+
string hdrDecodeArg = isHDR ? $"{referenceName}_HDR" : "float4(0, 0, 0, 0)";
62+
return $"UnityBuildTexture3DStructInternal({referenceName}, {samplerArg}, {hdrDecodeArg})";
5363
}
5464

5565
[SerializeField]

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleCubemapNode.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,26 @@ public sealed override void UpdateNodeAfterDeserialization()
4545
// Node generations
4646
public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
4747
{
48+
string outputVectorVariableName = GetVariableNameForSlot(OutputSlotId);
49+
4850
//Sampler input slot
4951
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInputId);
5052
var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
5153

54+
// Sample
5255
var id = GetSlotValue(CubemapInputId, generationMode);
5356
string result = string.Format("$precision4 {0} = SAMPLE_TEXTURECUBE_LOD({1}.tex, {2}.samplerstate, reflect(-{3}, {4}), {5});"
54-
, GetVariableNameForSlot(OutputSlotId)
57+
, outputVectorVariableName
5558
, id
5659
, edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : id
5760
, GetSlotValue(ViewDirInputId, generationMode)
5861
, GetSlotValue(NormalInputId, generationMode)
5962
, GetSlotValue(LODInputId, generationMode));
6063

6164
sb.AppendLine(result);
65+
66+
// Decode HDR
67+
SampleTexture2DNode.AppendHDRDecodeOperation(sb, outputVectorVariableName, id);
6268
}
6369

6470
public NeededCoordinateSpace RequiresViewDirection(ShaderStageCapability stageCapability)

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleRawCubemapNode.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,25 @@ public sealed override void UpdateNodeAfterDeserialization()
4141
// Node generations
4242
public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
4343
{
44+
string outputVectorVariableName = GetVariableNameForSlot(OutputSlotId);
45+
4446
//Sampler input slot
4547
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInputId);
4648
var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
4749

50+
// Sample
4851
var id = GetSlotValue(CubemapInputId, generationMode);
4952
string result = string.Format("$precision4 {0} = SAMPLE_TEXTURECUBE_LOD({1}.tex, {2}.samplerstate, {3}, {4});"
50-
, GetVariableNameForSlot(OutputSlotId)
53+
, outputVectorVariableName
5154
, id
5255
, edgesSampler.Any() ? GetSlotValue(SamplerInputId, generationMode) : id
5356
, GetSlotValue(NormalInputId, generationMode)
5457
, GetSlotValue(LODInputId, generationMode));
5558

5659
sb.AppendLine(result);
60+
61+
// Decode HDR
62+
SampleTexture2DNode.AppendHDRDecodeOperation(sb, outputVectorVariableName, id);
5763
}
5864

5965
public NeededCoordinateSpace RequiresNormal(ShaderStageCapability stageCapability)

Packages/com.unity.shadergraph/Editor/Data/Nodes/Input/Texture/SampleTexture2DArrayNode.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,19 @@ public sealed override void UpdateNodeAfterDeserialization()
8282
// Node generations
8383
public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMode)
8484
{
85+
string outputVectorVariableName = GetVariableNameForSlot(OutputSlotRGBAId);
86+
8587
var uvName = GetSlotValue(UVInput, generationMode);
8688
var indexName = GetSlotValue(IndexInputId, generationMode);
8789

8890
//Sampler input slot
8991
var samplerSlot = FindInputSlot<MaterialSlot>(SamplerInput);
9092
var edgesSampler = owner.GetEdges(samplerSlot.slotReference);
9193

94+
// Sample
9295
var id = GetSlotValue(TextureInputId, generationMode);
9396
var result = string.Format("$precision4 {0} = {1}({2}.tex, {3}.samplerstate, {4}, {5} {6});"
94-
, GetVariableNameForSlot(OutputSlotRGBAId)
97+
, outputVectorVariableName
9598
, MipSamplingModesUtils.Get2DTextureSamplingMacro(m_MipSamplingMode, usePlatformMacros: !m_EnableGlobalMipBias, isArray: true)
9699
, id
97100
, edgesSampler.Any() ? GetSlotValue(SamplerInput, generationMode) : id
@@ -101,10 +104,14 @@ public virtual void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode gene
101104

102105
sb.AppendLine(result);
103106

104-
sb.AppendLine(string.Format("$precision {0} = {1}.r;", GetVariableNameForSlot(OutputSlotRId), GetVariableNameForSlot(OutputSlotRGBAId)));
105-
sb.AppendLine(string.Format("$precision {0} = {1}.g;", GetVariableNameForSlot(OutputSlotGId), GetVariableNameForSlot(OutputSlotRGBAId)));
106-
sb.AppendLine(string.Format("$precision {0} = {1}.b;", GetVariableNameForSlot(OutputSlotBId), GetVariableNameForSlot(OutputSlotRGBAId)));
107-
sb.AppendLine(string.Format("$precision {0} = {1}.a;", GetVariableNameForSlot(OutputSlotAId), GetVariableNameForSlot(OutputSlotRGBAId)));
107+
// Decode HDR
108+
SampleTexture2DNode.AppendHDRDecodeOperation(sb, outputVectorVariableName, id);
109+
110+
// Extract components
111+
sb.AppendLine(string.Format("$precision {0} = {1}.r;", GetVariableNameForSlot(OutputSlotRId), outputVectorVariableName));
112+
sb.AppendLine(string.Format("$precision {0} = {1}.g;", GetVariableNameForSlot(OutputSlotGId), outputVectorVariableName));
113+
sb.AppendLine(string.Format("$precision {0} = {1}.b;", GetVariableNameForSlot(OutputSlotBId), outputVectorVariableName));
114+
sb.AppendLine(string.Format("$precision {0} = {1}.a;", GetVariableNameForSlot(OutputSlotAId), outputVectorVariableName));
108115
}
109116

110117
public bool RequiresMeshUV(UVChannel channel, ShaderStageCapability stageCapability)

0 commit comments

Comments
 (0)