Skip to content

Commit eb5e553

Browse files
committed
Adds debug images
1 parent 70ebd6a commit eb5e553

File tree

3 files changed

+99
-37
lines changed

3 files changed

+99
-37
lines changed

BloomEffectRenderer/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@
2626
// You can specify all the values or you can default the Build and Revision Numbers
2727
// by using the '*' as shown below:
2828
// [assembly: AssemblyVersion("1.0.*")]
29-
[assembly: AssemblyVersion("1.1.0.2")]
30-
[assembly: AssemblyFileVersion("1.1.0.2")]
29+
[assembly: AssemblyVersion("1.1.0.3")]
30+
[assembly: AssemblyFileVersion("1.1.0.3")]

BloomEffectRenderer/Renderer.cs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,22 @@ public void Initialize(GraphicsDevice gd, Point resolution)
7272
}
7373
}
7474

75+
public void LoadContent(GraphicsDevice graphicsDevice)
76+
{
77+
ExtractEffect = new Effect(graphicsDevice, EffectResource.ExtractEffect.Bytecode);
78+
GaussianBlurEffect = new Effect(graphicsDevice, EffectResource.BlurEffect.Bytecode);
79+
CombineEffect = new Effect(graphicsDevice, EffectResource.CombineEffect.Bytecode);
80+
}
81+
82+
public void UnloadContent()
83+
{
84+
BloomRenderTarget1?.Dispose();
85+
BloomRenderTarget2?.Dispose();
86+
ExtractEffect?.Dispose();
87+
GaussianBlurEffect?.Dispose();
88+
CombineEffect?.Dispose();
89+
}
90+
7591
/// <summary>
7692
/// A delegate you may use if you want to render the individual steps of this pipeline in order to debug.
7793
/// </summary>
@@ -174,6 +190,9 @@ public static void DrawFullscreenQuad(GraphicsDevice gd, SpriteBatch sb, Texture
174190
gd.SetRenderTarget(renderTarget);
175191
gd.Clear(Color.Black);
176192

193+
if (t == null)
194+
return;
195+
177196
sb.Begin(SpriteSortMode.Immediate,
178197
blendState ?? BlendState.Opaque,
179198
samplerState ?? SamplerState.PointClamp,
@@ -193,15 +212,14 @@ public static void DrawFullscreenQuad(GraphicsDevice gd, SpriteBatch sb, Texture
193212
h = renderTarget.Height;
194213
}
195214

196-
if (t != null)
197-
sb.Draw(t,
198-
new Rectangle(0, 0, w, h),
199-
new Rectangle(0, 0, t.Width, t.Height),
200-
Color.White,
201-
0f,
202-
Vector2.Zero,
203-
SpriteEffects.None,
204-
1f);
215+
sb.Draw(t,
216+
new Rectangle(0, 0, w, h),
217+
new Rectangle(0, 0, t.Width, t.Height),
218+
Color.White,
219+
0f,
220+
Vector2.Zero,
221+
SpriteEffects.None,
222+
1f);
205223
sb.End();
206224
}
207225

@@ -274,21 +292,5 @@ private static float ComputeGaussian(float n, Setting setting)
274292
var theta = setting.BlurAmount.Value;
275293
return (float) (1.0 / Math.Sqrt(2 * Math.PI * theta) * Math.Exp(-(n * n) / (2 * theta * theta)));
276294
}
277-
278-
public void LoadContent(GraphicsDevice graphicsDevice)
279-
{
280-
ExtractEffect = new Effect(graphicsDevice, EffectResource.ExtractEffect.Bytecode);
281-
GaussianBlurEffect = new Effect(graphicsDevice, EffectResource.BlurEffect.Bytecode);
282-
CombineEffect = new Effect(graphicsDevice, EffectResource.CombineEffect.Bytecode);
283-
}
284-
285-
public void UnloadContent()
286-
{
287-
BloomRenderTarget1?.Dispose();
288-
BloomRenderTarget2?.Dispose();
289-
ExtractEffect?.Dispose();
290-
GaussianBlurEffect?.Dispose();
291-
CombineEffect?.Dispose();
292-
}
293295
}
294296
}

TestGame/Game1.cs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,18 @@ public class Game1 : Game
4848

4949
private readonly GraphicsDeviceManager graphics;
5050
private SpriteBatch spriteBatch;
51+
private InputManager Input { get; } = new InputManager();
5152

5253
private SpriteFont Font { get; set; }
5354
private Texture2D Image { get; set; }
5455
private Point Resolution { get; } = new Point(1280, 720);
5556
private Renderer Renderer { get; } = new Renderer();
57+
5658
private bool IsBloom { get; set; } = true;
59+
private bool IsDebug { get; set; } = true;
5760
private int SettingIndex { get; set; }
58-
private InputManager Input { get; } = new InputManager();
61+
private RenderTarget2D DebugTarget { get; set; }
62+
private GameTime GameTime { get; set; }
5963

6064
public Game1()
6165
{
@@ -82,6 +86,14 @@ protected override void Initialize()
8286
{
8387
base.Initialize();
8488
Renderer.Initialize(graphics.GraphicsDevice, Resolution);
89+
DebugTarget = new RenderTarget2D(graphicsDevice: graphics.GraphicsDevice,
90+
width: Resolution.X,
91+
height: Resolution.Y,
92+
mipMap: false,
93+
preferredFormat: SurfaceFormat.Color,
94+
preferredDepthFormat: DepthFormat.None,
95+
preferredMultiSampleCount: 1,
96+
usage: RenderTargetUsage.PreserveContents);
8597
}
8698

8799
/// <summary>
@@ -125,6 +137,9 @@ private void HandleInput()
125137
{
126138
if (Input.Key.Is.Press(Keys.Space))
127139
IsBloom = !IsBloom;
140+
if (Input.Key.Is.Press(Keys.Tab))
141+
IsDebug = !IsDebug;
142+
128143
if (Input.Key.Is.Press(Keys.OemPlus) || Input.Key.Is.Press(Keys.Add))
129144
{
130145
SettingIndex++;
@@ -150,7 +165,7 @@ private void HandleInput()
150165

151166
private void HandleFloatInput(Keys down, Keys up, float step, Fader f, ref bool isModified, bool repeat = false)
152167
{
153-
f.Value = HandleFloatInput(down, up, step, (float)f.Value, ref isModified, repeat);
168+
f.Value = HandleFloatInput(down, up, step, (float) f.Value, ref isModified, repeat);
154169
}
155170

156171
private float HandleFloatInput(Keys down, Keys up, float step, float value, ref bool isModified,
@@ -176,8 +191,14 @@ private float HandleFloatInput(Keys down, Keys up, float step, float value, ref
176191
/// <param name="gameTime">Provides a snapshot of timing values.</param>
177192
protected override void Draw(GameTime gameTime)
178193
{
194+
GraphicsDevice.SetRenderTarget(DebugTarget);
195+
GraphicsDevice.Clear(Color.TransparentBlack);
196+
GraphicsDevice.SetRenderTarget(null);
179197
GraphicsDevice.Clear(Color.CornflowerBlue);
198+
// Persist gameTime to pass it to the debug-renderer for font-lerping.
199+
GameTime = gameTime;
180200
DrawImage();
201+
DrawDebugTarget();
181202
DrawText(gameTime);
182203
base.Draw(gameTime);
183204
}
@@ -191,7 +212,8 @@ private void DrawImage()
191212
"image",
192213
Image,
193214
null,
194-
Setting.PRESET_SETTING[SettingIndex]);
215+
Setting.PRESET_SETTING[SettingIndex],
216+
DebugDel);
195217
}
196218
else
197219
{
@@ -201,17 +223,53 @@ private void DrawImage()
201223
}
202224
}
203225

204-
private void DrawText(GameTime gameTime)
226+
private void DebugDel(string name, RenderTarget2D t, RenderPhase phase)
227+
{
228+
if (t != null)
229+
{
230+
// The constantly switching to and from the debug-rendertarget only works if it's set to 'preserveContents' which is something
231+
// I wouldn't recommend doing in a real game-environment.
232+
GraphicsDevice.SetRenderTarget(DebugTarget);
233+
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
234+
float f = 7;
235+
spriteBatch.Draw(t,
236+
new Rectangle(((int) phase + 1) * Resolution.X / (int) f,
237+
5 * (Resolution.Y / (int) f),
238+
(int) (Resolution.X / f),
239+
(int) (Resolution.Y / f)),
240+
Color.White);
241+
spriteBatch.DrawString(Font,
242+
phase + ":",
243+
new Vector2(((int) phase + 1f) * Resolution.X / (int) f, 5 * (Resolution.Y / (int) f) - 10),
244+
GetLerpColor(GameTime));
245+
spriteBatch.End();
246+
}
247+
}
248+
249+
private void DrawDebugTarget()
250+
{
251+
if (IsDebug)
252+
{
253+
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
254+
spriteBatch.Draw(DebugTarget, new Rectangle(0, 0, Resolution.X, Resolution.Y), Color.White);
255+
spriteBatch.End();
256+
}
257+
}
258+
259+
private Color GetLerpColor(GameTime gameTime)
205260
{
206261
var t = .5f + .5f * (float) Math.Sin(5 * gameTime.TotalGameTime.TotalSeconds);
207-
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
208-
Color c = Color.Lerp(Color.White, Color.Gray, t);
262+
return Color.Lerp(Color.White, Color.Gray, t);
263+
}
209264

210-
spriteBatch.DrawString(Font, BuildText(), new Vector2(10, 10), c);
265+
private void DrawText(GameTime gameTime)
266+
{
267+
Color c = GetLerpColor(gameTime);
211268

269+
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
270+
spriteBatch.DrawString(Font, BuildText(), new Vector2(10, 10), c);
212271
Vector2 s = Font.MeasureString(IMAGE_COPYRIGHT);
213272
spriteBatch.DrawString(Font, IMAGE_COPYRIGHT, new Vector2(30f, Resolution.Y - s.Y - 30f), c);
214-
215273
spriteBatch.End();
216274
}
217275

@@ -220,8 +278,10 @@ private string BuildText()
220278
Setting s = Setting.PRESET_SETTING[SettingIndex];
221279
StringBuilder sb = new StringBuilder();
222280
string bloom = IsBloom ? "ON" : "OFF";
223-
sb.Append($"Blur Effect: {bloom} (space)\n\n");
224-
sb.Append($"Setting: [{SettingIndex+1}/{Setting.PRESET_SETTING.Length}] {s.Name} >(+), <(-)\n");
281+
string debug = IsDebug ? "ON" : "OFF";
282+
sb.Append($"Blur Effect: {bloom} (SPACE)\n");
283+
sb.Append($"Debug View: {debug} (TAB)\n\n");
284+
sb.Append($"Setting: [{SettingIndex + 1}/{Setting.PRESET_SETTING.Length}] {s.Name} >(+), <(-)\n");
225285
sb.Append($" BloomThreshold : {s.BloomThreshold.Value:0.###} >(q), <(w)\n");
226286
sb.Append($" BlurAmount : {s.BlurAmount.Value:0.###} >(a), <(s)\n");
227287
sb.Append($" BloomIntensity : {s.BloomIntensity.Value:0.###} >(y), <(x)\n");

0 commit comments

Comments
 (0)