1+ // ***************************************************************************  
2+ // This is free and unencumbered software released into the public domain. 
3+ //  
4+ // Anyone is free to copy, modify, publish, use, compile, sell, or 
5+ // distribute this software, either in source code form or as a compiled 
6+ // binary, for any purpose, commercial or non-commercial, and by any 
7+ // means. 
8+ //  
9+ // In jurisdictions that recognize copyright laws, the author or authors 
10+ // of this software dedicate any and all copyright interest in the 
11+ // software to the public domain. We make this dedication for the benefit 
12+ // of the public at large and to the detriment of our heirs and 
13+ // successors. We intend this dedication to be an overt act of 
14+ // relinquishment in perpetuity of all present and future rights to this 
15+ // software under copyright law. 
16+ //  
17+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
18+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
19+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
20+ // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 
21+ // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
22+ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 
23+ // OTHER DEALINGS IN THE SOFTWARE. 
24+ //  
25+ // For more information, please refer to <http://unlicense.org> 
26+ // *************************************************************************** 
27+ 
28+ using  System ; 
29+ using  System . Diagnostics ; 
30+ using  System . IO ; 
31+ using  System . Reflection ; 
32+ using  Microsoft . Xna . Framework ; 
33+ using  Microsoft . Xna . Framework . Graphics ; 
34+ 
35+ namespace  BloomEffectRenderer . Effects 
36+ { 
37+  /// <summary> 
38+  /// Reperesents the bytecode of an <see cref="Effect" /> that is encapsulated inside a compiled assembly. 
39+  /// </summary> 
40+  /// <remarks> 
41+  /// <para> 
42+  /// Files that are encapsulated inside a compiled assembly are commonly known as Manifiest or embedded resources. 
43+  /// Since embedded resources are added to the assembly at compiled time, they can not be accidentally deleted or 
44+  /// misplaced. However, if the file needs to be changed, the assembly will need to be re-compiled with the changed 
45+  /// file. 
46+  /// </para> 
47+  /// <para> 
48+  /// To add an embedded resource file to an assembly, first add it to the project and then change the Build Action 
49+  /// in the Properties of the file to <code>Embedded Resource</code>. The next time the project is compiled, the 
50+  /// compiler will add the file to the assembly as an embedded resource. The compiler adds namespace(s) to the 
51+  /// embedded resource so it matches with the path of where the file was added to the project. 
52+  /// </para> 
53+  /// </remarks> 
54+  public  class  EffectResource 
55+  { 
56+  private  static EffectResource  extractEffect ; 
57+  private  static EffectResource  blurEffect ; 
58+  private  static EffectResource  combineEffect ; 
59+  private  static string  shaderExtension ; 
60+ 
61+  public  static EffectResource  ExtractEffect 
62+  => 
63+  extractEffect  ?? 
64+  ( extractEffect  = 
65+  new  EffectResource ( $ "BloomEffectRenderer.Effects.Resources.BloomExtract.{ shaderExtension } .mgfxo") ) ; 
66+  public  static EffectResource  BlurEffect 
67+  => 
68+  blurEffect  ?? 
69+  ( blurEffect  = 
70+  new  EffectResource ( $ "BloomEffectRenderer.Effects.Resources.GaussianBlur.{ shaderExtension } .mgfxo") ) ; 
71+  public  static EffectResource  CombineEffect 
72+  => 
73+  combineEffect  ?? 
74+  ( combineEffect  = 
75+  new  EffectResource ( $ "BloomEffectRenderer.Effects.Resources.BloomCombine.{ shaderExtension } .mgfxo") ) ; 
76+ 
77+  static EffectResource ( ) 
78+  { 
79+  DetermineShaderExtension ( ) ; 
80+  } 
81+ 
82+  private  static void  DetermineShaderExtension ( ) 
83+  { 
84+  // Use reflection to figure out if Shader.Profile is OpenGL (0) or DirectX (1). 
85+  // May need to be changed / fixed for future shader profiles. 
86+ 
87+  var  assembly  =  typeof ( Game ) . GetTypeInfo ( ) . Assembly ; 
88+  Debug . Assert ( assembly  !=  null ) ; 
89+ 
90+  var  shaderType  =  assembly . GetType ( "Microsoft.Xna.Framework.Graphics.Shader" ) ; 
91+  Debug . Assert ( shaderType  !=  null ) ; 
92+  var  shaderTypeInfo  =  shaderType . GetTypeInfo ( ) ; 
93+  Debug . Assert ( shaderTypeInfo  !=  null ) ; 
94+ 
95+  // https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/Shader/Shader.cs#L47 
96+  var  profileProperty  =  shaderTypeInfo . GetDeclaredProperty ( "Profile" ) ; 
97+  var  value  =  ( int )  profileProperty . GetValue ( null ) ; 
98+ 
99+  switch  ( value ) 
100+  { 
101+  case  0 : 
102+  // OpenGL 
103+  shaderExtension  =  "ogl" ; 
104+  break ; 
105+  case  1 : 
106+  // DirectX 
107+  shaderExtension  =  "dx11" ; 
108+  break ; 
109+  default : 
110+  throw  new  InvalidOperationException ( "Unknown shader profile." ) ; 
111+  } 
112+  } 
113+ 
114+  public  readonly  string  ResourceName ; 
115+  private  volatile  byte [ ]  bytecode ; 
116+  private  readonly  Assembly  assembly ; 
117+ 
118+  public  byte [ ]  Bytecode 
119+  { 
120+  get 
121+  { 
122+  if  ( bytecode  !=  null ) 
123+  return  bytecode ; 
124+ 
125+  lock  ( this ) 
126+  { 
127+  if  ( bytecode  !=  null ) 
128+  return  bytecode ; 
129+ 
130+  var  stream  =  assembly . GetManifestResourceStream ( ResourceName ) ; 
131+  using  ( var  memoryStream  =  new  MemoryStream ( ) ) 
132+  { 
133+  stream . CopyTo ( memoryStream ) ; 
134+  bytecode  =  memoryStream . ToArray ( ) ; 
135+  } 
136+  } 
137+ 
138+  return  bytecode ; 
139+  } 
140+  } 
141+ 
142+  /// <summary> 
143+  /// Initializes a new instance of the <see cref="EffectResource" /> class. 
144+  /// </summary> 
145+  /// <param name="resourceName">The name of the embedded resource. This must include the namespace(s).</param> 
146+  /// <param name="assembly">The assembly which the embedded resource is apart of.</param> 
147+  public  EffectResource ( string  resourceName ,  Assembly  assembly  =  null ) 
148+  { 
149+  ResourceName  =  resourceName ; 
150+  this . assembly  =  assembly  ??  typeof ( EffectResource ) . GetTypeInfo ( ) . Assembly ; 
151+  } 
152+  } 
153+ } 
0 commit comments