Skip to content

Commit 8038ea9

Browse files
committed
Adds effects as emedded resources
1 parent b8d2830 commit 8038ea9

20 files changed

+384
-21
lines changed

BloomEffectRenderer.sln.DotSettings

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2626
OTHER DEALINGS IN THE SOFTWARE.
2727

2828
For more information, please refer to <http://unlicense.org>
29-
***************************************************************************</s:String></wpf:ResourceDictionary>
29+
***************************************************************************</s:String>
30+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
31+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
32+
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

BloomEffectRenderer/BloomEffectRenderer.csproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,17 @@
3535
</PropertyGroup>
3636
<ItemGroup>
3737
<!-- A reference to the entire .NET Framework is automatically included -->
38+
<EmbeddedResource Include="Effects\Resources\BloomCombine.dx11.mgfxo" />
39+
<EmbeddedResource Include="Effects\Resources\BloomCombine.ogl.mgfxo" />
40+
<EmbeddedResource Include="Effects\Resources\BloomExtract.dx11.mgfxo" />
41+
<EmbeddedResource Include="Effects\Resources\BloomExtract.ogl.mgfxo" />
42+
<EmbeddedResource Include="Effects\Resources\GaussianBlur.dx11.mgfxo" />
43+
<EmbeddedResource Include="Effects\Resources\GaussianBlur.ogl.mgfxo" />
44+
<None Include="Effects\Resources\RebuildEffects.bat" />
3845
<None Include="packages.config" />
3946
</ItemGroup>
4047
<ItemGroup>
48+
<Compile Include="Effects\EffectResource.cs" />
4149
<Compile Include="Renderer.cs" />
4250
<Compile Include="Settings.cs" />
4351
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -56,6 +64,11 @@
5664
<Private>True</Private>
5765
</Reference>
5866
</ItemGroup>
67+
<ItemGroup>
68+
<Content Include="Effects\Resources\BloomCombine.fx" />
69+
<Content Include="Effects\Resources\BloomExtract.fx" />
70+
<Content Include="Effects\Resources\GaussianBlur.fx" />
71+
</ItemGroup>
5972
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
6073
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6174
Other similar extension points exist, see Microsoft.Common.targets.

BloomEffectRenderer/BloomEffectRenderer.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
If you pass 'null' as the target RenderTarget then it will draw directly to the backbuffer.
1818
</description>
1919
<summary>
20-
This is an extension repository for MonoGame. It uses your local MG installation and provides an easy way to render a bloom-post-process-effect from and to any RenderTarget you like.
20+
This is a PCL library for MonoGame. It uses your local MG installation and provides an easy way to render a bloom-post-process-effect from and to any RenderTarget you like.
2121
</summary>
2222
<releaseNotes></releaseNotes>
2323
<copyright>Copyright 2018</copyright>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}
1.36 KB
Binary file not shown.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Texture2D Texture;
2+
SamplerState TextureSampler
3+
{
4+
Texture = <Texture>;
5+
MinFilter = Linear;
6+
MagFilter = Linear;
7+
MipFilter = Linear;
8+
AddressU = Wrap;
9+
AddressV = Wrap;
10+
};
11+
12+
Texture2D BaseTexture;
13+
SamplerState BaseTextureSampler
14+
{
15+
Texture = <BaseTexture>;
16+
MinFilter = Linear;
17+
MagFilter = Linear;
18+
MipFilter = Linear;
19+
AddressU = Clamp;
20+
AddressV = Clamp;
21+
};
22+
23+
float BloomIntensity;
24+
float BaseIntensity;
25+
26+
float BloomSaturation;
27+
float BaseSaturation;
28+
29+
30+
float4 AdjustSaturation(float4 color, float saturation)
31+
{
32+
float grey = dot(color, float3(0.3, 0.59, 0.11));
33+
34+
return lerp(grey, color, saturation);
35+
}
36+
37+
38+
float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
39+
{
40+
float4 bloom = Texture.Sample(TextureSampler, texCoord);
41+
float4 base = BaseTexture.Sample(BaseTextureSampler, texCoord);
42+
43+
bloom = AdjustSaturation(bloom, BloomSaturation) * BloomIntensity;
44+
base = AdjustSaturation(base, BaseSaturation) * BaseIntensity;
45+
46+
base *= (1 - saturate(bloom));
47+
48+
return base + bloom;
49+
}
50+
51+
52+
technique BloomCombine
53+
{
54+
pass Pass1
55+
{
56+
#if SM4
57+
PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
58+
#elif SM3
59+
PixelShader = compile ps_3_0 PixelShaderFunction();
60+
#else
61+
PixelShader = compile ps_2_0 PixelShaderFunction();
62+
#endif
63+
}
64+
}
1.19 KB
Binary file not shown.
790 Bytes
Binary file not shown.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Texture2D Texture;
2+
SamplerState TextureSampler
3+
{
4+
Texture = <Texture>;
5+
MinFilter = Linear;
6+
MagFilter = Linear;
7+
MipFilter = Linear;
8+
AddressU = Wrap;
9+
AddressV = Wrap;
10+
};
11+
12+
float BloomThreshold;
13+
14+
float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color1 : COLOR0, float2 texCoord : TEXCOORD0) : SV_TARGET0
15+
{
16+
float4 c = Texture.Sample(TextureSampler, texCoord);
17+
18+
return saturate((c - BloomThreshold) / (1 - BloomThreshold));
19+
}
20+
21+
22+
technique BloomExtract
23+
{
24+
pass Pass1
25+
{
26+
#if SM4
27+
PixelShader = compile ps_4_0_level_9_1 PixelShaderFunction();
28+
#elif SM3
29+
PixelShader = compile ps_3_0 PixelShaderFunction();
30+
#else
31+
PixelShader = compile ps_2_0 PixelShaderFunction();
32+
#endif
33+
}
34+
}
714 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)