Skip to content

Commit e4c503b

Browse files
committed
[Toolkit.Graphics] Simplify EffectData to allow only a single effect to be stored. Refactor EffectPool to match the new behavior.
1 parent 722309f commit e4c503b

23 files changed

+300
-309
lines changed

Source/Tests/SharpDX.Toolkit.Graphics.Tests/TestEffect.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,8 @@ public void TestCompiler()
4747

4848
var bytecode = result.EffectData;
4949

50-
// Check that we have a single effect compiled in this archive.
51-
Assert.AreEqual(bytecode.Effects.Count, 1);
52-
5350
// Check that the name of this effect is the file name
54-
Assert.AreEqual(bytecode.Effects[0].Name, "TestEffect");
51+
Assert.AreEqual(bytecode.Description.Name, "TestEffect");
5552

5653
// We have 3 shaders compiled: VS, VS2, PS
5754
Assert.AreEqual(bytecode.Shaders.Count, 3);

Source/Toolkit/SharpDX.Toolkit.Compiler/Effect/EffectCompilerInternal.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ internal class EffectCompilerInternal
5656

5757
private readonly List<string> currentExports = new List<string>();
5858
private EffectCompilerFlags compilerFlags;
59-
private EffectData.Effect effect;
6059
private EffectData effectData;
6160

61+
private EffectData.Effect effect;
62+
6263
private List<string> includeDirectoryList;
6364
public IncludeFileDelegate IncludeFileDelegate;
6465
private Include includeHandler;
@@ -138,7 +139,7 @@ public EffectCompilerResult Compile(string sourceCode, string filePath, EffectCo
138139
dependencyList.Save(dependencyFilePath);
139140

140141
// If dynamic compiling, store the parameters used to compile this effect directly in the bytecode
141-
if (allowDynamicCompiling && result.EffectData != null && result.EffectData.Effects.Count == 1)
142+
if (allowDynamicCompiling && result.EffectData != null)
142143
{
143144
var compilerArguments = new EffectData.CompilerArguments { FilePath = filePath, DependencyFilePath = dependencyFilePath, Macros = new List<EffectData.ShaderMacro>(), IncludeDirectoryList = new List<string>() };
144145
if (macrosArgs != null)
@@ -151,7 +152,7 @@ public EffectCompilerResult Compile(string sourceCode, string filePath, EffectCo
151152
compilerArguments.IncludeDirectoryList.AddRange(includeDirectoryList);
152153
}
153154

154-
result.EffectData.Effects[0].Arguments = compilerArguments;
155+
result.EffectData.Description.Arguments = compilerArguments;
155156
}
156157
}
157158

@@ -189,17 +190,14 @@ private void InternalCompile(string sourceCode, string fileName)
189190

190191
effectData = new EffectData
191192
{
192-
Effects = new List<EffectData.Effect>(),
193-
Shaders = new List<EffectData.Shader>()
193+
Shaders = new List<EffectData.Shader>(),
194+
Description = new EffectData.Effect()
195+
{
196+
Name = Path.GetFileNameWithoutExtension(fileName),
197+
Techniques = new List<EffectData.Technique>(),
198+
}
194199
};
195-
196-
effect = new EffectData.Effect()
197-
{
198-
Name = Path.GetFileNameWithoutExtension(fileName),
199-
Techniques = new List<EffectData.Technique>()
200-
};
201-
202-
effectData.Effects.Add(effect);
200+
effect = effectData.Description;
203201

204202
if (parserResult.Shader != null)
205203
{
@@ -422,7 +420,7 @@ private void HandleEffectName(Ast.Expression expression)
422420
}
423421
else
424422
{
425-
effect.Name = (string) value;
423+
effect.Name = (string)value;
426424
}
427425
}
428426

Source/Toolkit/SharpDX.Toolkit.Compiler/Effect/EffectDataCodeWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ namespace {1}
7676
";
7777

7878
var effectToGenerateText = new StringBuilder();
79-
foreach(var effect in effectData.Effects)
80-
{
81-
effectToGenerateText.AppendFormat("// Effect [{0}]\r\n", effect.Name);
82-
}
79+
effectToGenerateText.AppendFormat("// Effect [{0}]\r\n", effectData.Description.Name);
8380

8481
var buffer = new MemoryStream();
8582
effectData.Save(buffer);

Source/Toolkit/SharpDX.Toolkit.Game/EffectCompilerSystem.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,10 @@ public override void Update(GameTime gameTime)
135135
var effectData = effectToRebind.EffectData;
136136

137137
// First register the bytecode to the pool in order to reuse existing shaders
138-
effect.Pool.RegisterBytecode(effectData, true);
138+
var newEffect = effect.Pool.RegisterBytecode(effectData);
139139

140140
// Then re-instantiate this effect.
141-
effect.InitializeFrom(effectData.Effects[0]);
141+
effect.InitializeFrom(newEffect);
142142
}
143143

144144
base.Update(gameTime);
@@ -347,7 +347,7 @@ private void ThreadCompiler()
347347
var compilerResult = compiler.CompileFromFile(args.FilePath, args.CompilerFlags, args.Macros, args.IncludeDirectoryList, true, args.DependencyFilePath);
348348

349349
// If there are errors notify them
350-
if (compilerResult.HasErrors || compilerResult.EffectData.Effects.Count == 0)
350+
if (compilerResult.HasErrors)
351351
{
352352
OnCompilationError(new EffectCompilerEventArgs(effect, compilerResult.Logger.Messages));
353353
}

Source/Toolkit/SharpDX.Toolkit.Graphics/Effect.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,21 @@ public Effect(GraphicsDevice device, byte[] bytecode)
9090
/// <remarks>The effect bytecode must contain only a single effect and will be registered into the <see cref="GraphicsDevice.DefaultEffectPool"/>.</remarks>
9191
public Effect(GraphicsDevice device, EffectData effectData, EffectPool effectPool = null) : base(device)
9292
{
93-
if (effectData.Effects.Count != 1)
94-
throw new ArgumentException(string.Format("Expecting only one effect in the effect bytecode instead of [{0}].", Utilities.Join(",", effectData.Effects)), "effectData");
95-
9693
ConstantBuffers = new EffectConstantBufferCollection();
9794
Parameters = new EffectParameterCollection();
9895
Techniques = new EffectTechniqueCollection();
9996

10097
Pool = effectPool ?? device.DefaultEffectPool;
10198

10299
// Sets the effect name
103-
Name = effectData.Effects[0].Name;
100+
Name = effectData.Description.Name;
104101

105102
// Register the bytecode to the pool
106-
Pool.RegisterBytecode(effectData);
103+
var effect = Pool.RegisterBytecode(effectData);
107104

108105
// Initialize from effect
109-
InitializeFrom(Pool.Find(Name));
106+
InitializeFrom(effect);
107+
110108
// If everything was fine, then we can register it into the pool
111109
Pool.AddEffect(this);
112110
}
@@ -145,12 +143,6 @@ public Effect(GraphicsDevice device, EffectData effectData, EffectPool effectPoo
145143
/// <exception cref="System.InvalidOperationException">If no techniques found in this effect.</exception>
146144
public void InitializeFrom(EffectData.Effect effectDataArg)
147145
{
148-
if (effectDataArg == null)
149-
throw new ArgumentException(string.Format("Unable to find effect [{0}] from the EffectPool.", Name), "effectName");
150-
151-
if (effectDataArg.Techniques.Count == 0)
152-
throw new InvalidOperationException("No techniques found in this effect.");
153-
154146
RawEffectData = effectDataArg;
155147

156148
// Clean any previously allocated resources

Source/Toolkit/SharpDX.Toolkit.Graphics/EffectPass.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ internal void Initialize(Logger logger)
423423
var stageBlock = new StageBlock(shaderType);
424424
pipeline.Stages[i] = stageBlock;
425425

426-
stageBlock.Index = link.RuntimeIndex;
426+
stageBlock.Index = link.Index;
427427
stageBlock.ShaderStage = Effect.GraphicsDevice.ShaderStages[i];
428428

429429
InitStageBlock(stageBlock, logger);
@@ -438,13 +438,14 @@ internal void Initialize(Logger logger)
438438
private void InitStageBlock(StageBlock stageBlock, Logger logger)
439439
{
440440
// If null shader, then skip init
441-
if (stageBlock.Index < 0)
441+
var shaderIndex = stageBlock.Index;
442+
if (shaderIndex < 0)
442443
{
443444
return;
444445
}
445446

446447
string errorProfile;
447-
stageBlock.Shader = Effect.Pool.GetOrCompileShader(stageBlock.Type, stageBlock.Index, out errorProfile);
448+
stageBlock.Shader = Effect.Pool.GetOrCompileShader(stageBlock.Type, shaderIndex, out errorProfile);
448449

449450
if (stageBlock.Shader == null)
450451
{
@@ -459,7 +460,7 @@ private void InitStageBlock(StageBlock stageBlock, Logger logger)
459460
return;
460461
}
461462

462-
var shaderRaw = Effect.Pool.EffectData.Shaders[stageBlock.Index];
463+
var shaderRaw = Effect.Pool.RegisteredShaders[shaderIndex];
463464

464465
// Cache the input signature
465466
if (shaderRaw.Type == EffectShaderType.Vertex)

0 commit comments

Comments
 (0)