-
-
Notifications
You must be signed in to change notification settings - Fork 911
/
ShaderGeneratorContext.cs
202 lines (162 loc) · 7.34 KB
/
ShaderGeneratorContext.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using Stride.Core;
using Stride.Core.Diagnostics;
using Stride.Core.Mathematics;
using Stride.Core.Serialization.Contents;
using Stride.Graphics;
using Stride.Graphics.Data;
using Stride.Rendering.Materials.ComputeColors;
namespace Stride.Rendering.Materials
{
/// <summary>
/// Base class for generating shader class source with associated parameters.
/// </summary>
public class ShaderGeneratorContext : ComponentBase
{
// TODO: Document this class
private readonly Dictionary<ParameterKey, int> parameterKeyIndices;
private readonly Dictionary<SamplerStateDescription, ObjectParameterKey<SamplerState>> declaredSamplerStates;
private readonly Dictionary<Color4, Texture> singleColorTextures = new Dictionary<Color4, Texture>();
private MaterialOverrides currentOverrides;
private Stack<MaterialOverrides> overridesStack = new Stack<MaterialOverrides>();
public delegate object FindAssetDelegate(object asset);
public delegate string GetAssetFriendlyNameDelegate(object asset);
public FindAssetDelegate FindAsset { get; set; }
public GetAssetFriendlyNameDelegate GetAssetFriendlyName { get; set; }
public LoggerResult Log { get; set; }
private GraphicsDevice graphicsDevice;
/// <summary>
/// Gets or sets the asset manager.
/// </summary>
/// <value>
/// The asset manager.
/// </value>
public ContentManager Content { get; set; }
public ShaderGeneratorContext(GraphicsDevice graphicsDevice = null)
{
this.graphicsDevice = graphicsDevice;
Parameters = new ParameterCollection();
parameterKeyIndices = new Dictionary<ParameterKey, int>();
declaredSamplerStates = new Dictionary<SamplerStateDescription, ObjectParameterKey<SamplerState>>();
currentOverrides = new MaterialOverrides();
}
public ParameterCollection Parameters { get; set; }
public MaterialOverrides CurrentOverrides
{
get
{
return currentOverrides;
}
}
public ColorSpace ColorSpace { get; set; }
public bool IsNotPixelStage { get; set; }
/// <summary>
/// Gets or sets a value indicating whether materials will be optimized (textures blended together, generate optimized shader permutations, etc...).
/// </summary>
/// <value>
/// <c>true</c> if [materials are optimized]; otherwise, <c>false</c>.
/// </value>
public bool OptimizeMaterials { get; set; }
public ParameterKey GetParameterKey(ParameterKey key)
{
if (key == null) throw new ArgumentNullException("key");
var baseKey = key;
int parameterKeyIndex;
parameterKeyIndices.TryGetValue(baseKey, out parameterKeyIndex);
key = parameterKeyIndex == 0 ? baseKey : baseKey.ComposeWith("i" + parameterKeyIndex.ToString(CultureInfo.InvariantCulture));
parameterKeyIndex++;
parameterKeyIndices[baseKey] = parameterKeyIndex;
return key;
}
public unsafe Texture GenerateTextureFromColor(Color color)
{
if (Content == null)
{
Log.Error("Trying to generate a texture without an ContentManager");
return null;
}
// Already generated?
Texture texture;
if (singleColorTextures.TryGetValue(color, out texture))
return texture;
// Generate 1x1 texture of given color
var image = Image.New2D(1, 1, 1, PixelFormat.R8G8B8A8_UNorm);
*((Color*)image.PixelBuffer[0].DataPointer) = color;
texture = image.ToSerializableVersion();
// Save texture
Content.Save(string.Format("__material_internal__/color_texture_{0:X2}{1:X2}{2:X2}{3:X2}", color.R, color.G, color.B, color.A), texture);
singleColorTextures.Add(color, texture);
return texture;
}
public ObjectParameterKey<Texture> GetTextureKey(Texture texture, ObjectParameterKey<Texture> key, Color? defaultTextureValue = null)
{
var textureKey = (ObjectParameterKey<Texture>)GetParameterKey(key);
if (texture != null)
{
Parameters.Set(textureKey, texture);
}
else if (defaultTextureValue != null && Content != null)
{
texture = GenerateTextureFromColor(defaultTextureValue.Value);
Parameters.Set(textureKey, texture);
}
return textureKey;
}
public ObjectParameterKey<SamplerState> GetSamplerKey(SamplerStateDescription samplerStateDesc, GraphicsDevice graphicsDevice)
{
ObjectParameterKey<SamplerState> key;
if (!declaredSamplerStates.TryGetValue(samplerStateDesc, out key))
{
key = MaterialKeys.Sampler.ComposeWith("i" + declaredSamplerStates.Count.ToString(CultureInfo.InvariantCulture));
declaredSamplerStates.Add(samplerStateDesc, key);
}
var samplerState = graphicsDevice != null ? SamplerState.New(graphicsDevice, samplerStateDesc) : SamplerState.NewFake(samplerStateDesc);
Parameters.Set(key, samplerState);
return key;
}
public ObjectParameterKey<Texture> GetTextureKey(ComputeTextureBase computeTexture, MaterialComputeColorKeys baseKeys)
{
var keyResolved = (ObjectParameterKey<Texture>)(computeTexture.Key ?? baseKeys.TextureBaseKey ?? MaterialKeys.GenericTexture);
return GetTextureKey(computeTexture.Texture, keyResolved, baseKeys.DefaultTextureValue);
}
public ObjectParameterKey<SamplerState> GetSamplerKey(ComputeColorParameterSampler sampler)
{
if (sampler == null) throw new ArgumentNullException("sampler");
var samplerStateDesc = new SamplerStateDescription(sampler.Filtering, sampler.AddressModeU)
{
AddressV = sampler.AddressModeV,
AddressW = TextureAddressMode.Wrap,
};
return GetSamplerKey(samplerStateDesc, graphicsDevice);
}
protected void ResetParameterKeys()
{
parameterKeyIndices.Clear();
declaredSamplerStates.Clear();
}
public void PushOverrides(MaterialOverrides overrides)
{
if (overrides == null) throw new ArgumentNullException("overrides");
overridesStack.Push(overrides);
UpdateOverrides();
}
public void PopOverrides()
{
overridesStack.Pop();
UpdateOverrides();
}
private void UpdateOverrides()
{
// Update overrides by squashing them using multiplication
currentOverrides = new MaterialOverrides();
foreach (var current in overridesStack)
{
currentOverrides *= current;
}
}
}
}