-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathDataTypeStatic.cs
139 lines (125 loc) · 7.38 KB
/
DataTypeStatic.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
namespace UnityEditor.ShaderFoundry
{
// This class stores static data relating IInternalType and IPublicType objects.
// This is used to handle generics in order to interface with the native container.
class DataTypeStatic
{
internal readonly struct DataTypeInfo
{
public readonly DataType dataType;
public readonly Type publicType;
public readonly Type internalType;
public readonly int internalTypeSizeInBytes;
public DataTypeInfo(DataType dataType, Type internalType, Type publicType)
: this(dataType, internalType, publicType, System.Runtime.InteropServices.Marshal.SizeOf(internalType))
{
}
private DataTypeInfo(DataType dataType, Type internalType, Type publicType, int internalTypeSizeInBytes)
{
this.dataType = dataType;
this.publicType = publicType;
this.internalType = internalType;
this.internalTypeSizeInBytes = internalTypeSizeInBytes;
}
public static DataTypeInfo Invalid => new DataTypeInfo(DataType.Unknown, null, null, 0);
}
DataTypeInfo Invalid = DataTypeInfo.Invalid;
Dictionary<Type, DataTypeInfo> internalTypeMap = new Dictionary<Type, DataTypeInfo>();
Dictionary<Type, DataTypeInfo> publicTypeMap = new Dictionary<Type, DataTypeInfo>();
Dictionary<DataType, DataTypeInfo> dataTypeToInfo = new Dictionary<DataType, DataTypeInfo>();
static DataTypeStatic m_Instance = null;
static DataTypeStatic Instance
{
get
{
if (m_Instance == null)
Initialize();
return m_Instance;
}
}
static void Initialize()
{
// The type relations must be explicitly recorded.
m_Instance = new DataTypeStatic();
m_Instance.Register<ShaderAttributeParameterInternal, ShaderAttributeParameter>(DataType.ShaderAttributeParameter);
m_Instance.Register<ShaderAttributeInternal, ShaderAttribute>(DataType.ShaderAttribute);
m_Instance.Register<RenderStateDescriptorInternal, RenderStateDescriptor>(DataType.RenderStateDescriptor);
m_Instance.Register<DefineDescriptorInternal, DefineDescriptor>(DataType.DefineDescriptor);
m_Instance.Register<IncludeDescriptorInternal, IncludeDescriptor>(DataType.IncludeDescriptor);
m_Instance.Register<KeywordDescriptorInternal, KeywordDescriptor>(DataType.KeywordDescriptor);
m_Instance.Register<PragmaDescriptorInternal, PragmaDescriptor>(DataType.PragmaDescriptor);
m_Instance.Register<TagDescriptorInternal, TagDescriptor>(DataType.TagDescriptor);
m_Instance.Register<FunctionParameterInternal, FunctionParameter>(DataType.FunctionParameter);
m_Instance.Register<ShaderFunctionInternal, ShaderFunction>(DataType.ShaderFunction);
m_Instance.Register<StructFieldInternal, StructField>(DataType.StructField);
m_Instance.Register<ShaderTypeInternal, ShaderType>(DataType.ShaderType);
m_Instance.Register<BlockVariableInternal, BlockVariable>(DataType.BlockVariable);
m_Instance.Register<BlockInternal, Block>(DataType.Block);
m_Instance.Register<CustomizationPointInternal, CustomizationPoint>(DataType.CustomizationPoint);
m_Instance.Register<TemplatePassInternal, TemplatePass>(DataType.TemplatePass);
m_Instance.Register<TemplateInternal, Template>(DataType.Template);
m_Instance.Register<TemplateInstanceInternal, TemplateInstance>(DataType.TemplateInstance);
m_Instance.Register<ShaderDependencyInternal, ShaderDependency>(DataType.ShaderDependency);
m_Instance.Register<ShaderCustomEditorInternal, ShaderCustomEditor>(DataType.ShaderCustomEditor);
m_Instance.Register<PackageRequirementInternal, PackageRequirement>(DataType.PackageRequirement);
m_Instance.Register<CopyRuleInternal, CopyRule>(DataType.CopyRule);
m_Instance.Register<BlockLinkOverrideInternal, BlockLinkOverride>(DataType.LinkOverride);
m_Instance.Register<BlockLinkOverrideInternal.LinkAccessorInternal, BlockLinkOverride.LinkAccessor>(DataType.LinkAccessor);
m_Instance.Register<BlockLinkOverrideInternal.LinkElementInternal, BlockLinkOverride.LinkElement>(DataType.LinkElement);
m_Instance.Register<BlockSequenceElementInternal, BlockSequenceElement>(DataType.BlockSequenceElement);
m_Instance.Register<CustomizationPointImplementationInternal, CustomizationPointImplementation>(DataType.CustomizationPointImplementation);
m_Instance.Register<StageDescriptionInternal, StageDescription>(DataType.StageDescription);
m_Instance.Register<BlockShaderInterfaceInternal, BlockShaderInterface>(DataType.BlockShaderInterface);
m_Instance.Register<BlockShaderInternal, BlockShader>(DataType.BlockShader);
m_Instance.Register<RegisterTemplatesWithInterfaceInternal, RegisterTemplatesWithInterface>(DataType.RegisterTemplatesWithInterface);
m_Instance.Register<InterfaceRegistrationStatementInternal, InterfaceRegistrationStatement>(DataType.InterfaceRegistrationStatement);
m_Instance.Register<NamespaceInternal, Namespace>(DataType.Namespace);
}
void Register<InternalType, PublicType>(DataType dataType)
where InternalType : struct, IInternalType<InternalType>
where PublicType : struct, IPublicType<PublicType>
{
var iType = typeof(InternalType);
var pType = typeof(PublicType);
var info = new DataTypeInfo(dataType, iType, pType);
m_Instance.internalTypeMap[iType] = info;
m_Instance.publicTypeMap[pType] = info;
m_Instance.dataTypeToInfo[dataType] = info;
}
static internal DataTypeInfo GetInfoFromDataType(DataType type)
{
if (!Instance.dataTypeToInfo.TryGetValue(type, out var info))
return Instance.Invalid;
return info;
}
static internal DataTypeInfo GetInfoFromInternalType<T>() where T : struct, IInternalType<T>
{
if (!Instance.internalTypeMap.TryGetValue(typeof(T), out var info))
return Instance.Invalid;
return info;
}
static internal DataType GetDataTypeFromInternalType<T>() where T : struct, IInternalType<T>
{
if (!Instance.internalTypeMap.TryGetValue(typeof(T), out var info))
return DataType.Unknown;
return info.dataType;
}
static internal DataTypeInfo GetInfoFromPublicType<T>() where T : struct, IPublicType<T>
{
if (!Instance.publicTypeMap.TryGetValue(typeof(T), out var info))
return Instance.Invalid;
return info;
}
static internal DataType GetDataTypeFromPublicType<T>() where T : struct, IPublicType<T>
{
if (!Instance.publicTypeMap.TryGetValue(typeof(T), out var info))
return DataType.Unknown;
return info.dataType;
}
}
}