Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EffectProgram.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Collections.Generic;
5 using SiliconStudio.Paradox.Shaders;
6 
7 namespace SiliconStudio.Paradox.Graphics
8 {
9  internal partial class EffectProgram : GraphicsResourceBase
10  {
11  public static EffectProgram New(GraphicsDevice graphicsDevice, EffectBytecode bytecode)
12  {
13  var effectProgramLibrary = graphicsDevice.GetOrCreateSharedData(GraphicsDeviceSharedDataType.PerDevice, typeof(EffectProgram), () => new EffectProgramLibrary());
14  return effectProgramLibrary.GetOrCreateShader(graphicsDevice, bytecode);
15  }
16 
17  private class EffectProgramLibrary : IDisposable
18  {
19  private readonly Dictionary<EffectBytecode, EffectProgram> ShaderLibrary = new Dictionary<EffectBytecode, EffectProgram>();
20 
21  public EffectProgram GetOrCreateShader(GraphicsDevice graphicsDevice, EffectBytecode bytecode)
22  {
23  lock (ShaderLibrary)
24  {
25  EffectProgram effectProgram;
26  if (!ShaderLibrary.TryGetValue(bytecode, out effectProgram))
27  {
28  effectProgram = new EffectProgram(graphicsDevice, bytecode);
29  ShaderLibrary.Add(bytecode, effectProgram);
30  }
31  return effectProgram;
32  }
33  }
34 
35  public void Dispose()
36  {
37  lock (ShaderLibrary)
38  {
39  foreach (var effectProgram in ShaderLibrary)
40  {
41  effectProgram.Value.Dispose();
42  }
43  ShaderLibrary.Clear();
44  }
45  }
46  }
47  }
48 }
Contains a compiled shader with bytecode for each stage.