using System; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; using Amphora; using Blacklight.Core; using SharpDX.Direct3D9; using UnitTestSharp; using Prism; namespace Blacklight.SharpDX9.UnitTesting { public class SharpDX9ScreenEffectTests : TestFixture { // Creating a device compiles the primitive shader's pipeline, which alone can pass the default budget. public const int DeviceBudget = 5000; public static byte[] Resource(Assembly assembly, string name) { using (Stream stream = assembly.GetManifestResourceStream(name)) { var returnMe = new MemoryStream(); stream.CopyTo(returnMe); return returnMe.ToArray(); } } public static ScreenEffect Create(SharpDX9WindowHandle handle, string name, bool readsScene = false) { byte[] compiled = Resource(typeof(SharpDX9ScreenEffectTests).Assembly, name + ".fxo"); return handle.CreateScreenEffect(new ByteView(compiled), readsScene); } public static void WithWindow(Action test) { using (var window = new Form()) { var handle = (SharpDX9WindowHandle)new Module().CreateDrawableSurfaceOnWindow(window.Handle); try { handle.BackgroundColor = new Color(0f, 0f, 0f, 1f); test(handle); } finally { handle.Dispose(); } } } public static void Frame(SharpDX9WindowHandle handle, params ScreenEffect[] effects) { handle.FrameReset(); for (int i = 0; i < effects.Length; ++i) { handle.DrawScreenEffect(effects[i]); } handle.SubmitDrawCalls(); } // The scene target's channels are 16 bit floats, red first. public static float[] PixelAt(SharpDX9WindowHandle handle, int x, int y) { Surface target = handle.SceneTarget.Surface; SurfaceDescription description = target.Description; using (Surface readable = Surface.CreateOffscreenPlain(handle.Backend.GraphicsDevice, description.Width, description.Height, description.Format, Pool.SystemMemory)) { handle.Backend.GraphicsDevice.GetRenderTargetData(target, readable); SharpDX.DataRectangle rectangle = readable.LockRectangle(LockFlags.ReadOnly); try { var halves = new short[4]; Marshal.Copy(rectangle.DataPointer + y * rectangle.Pitch + x * 8, halves, 0, halves.Length); var returnMe = new float[4]; for (int i = 0; i < returnMe.Length; ++i) { returnMe[i] = new SharpDX.Half { RawValue = unchecked((ushort)halves[i]) }; } return returnMe; } finally { readable.UnlockRectangle(); } } } public class SubmitDrawCallsTests : TestFixture { [CpuTimeout(DeviceBudget)] public void PositionsArePixelCentersFromTheTopLeft() { WithWindow(handle => { ScreenEffect effect = Create(handle, "ProbeEffect"); effect.SetParameter("Fill", 0f, 0f, 0f, 1f); effect.SetParameter("ShowPosition", 1f); Frame(handle, effect); CheckEqual(new[] { 3.5f, 5.5f, 0f, 1f }, PixelAt(handle, 3, 5)); }); } [CpuTimeout(DeviceBudget)] public void CompositesAPremultipliedColorOverWhatWasDrawnBefore() { WithWindow(handle => { ScreenEffect below = Create(handle, "ProbeEffect"); below.SetParameter("Fill", 1f, 0f, 0f, 1f); ScreenEffect above = Create(handle, "ProbeEffect"); above.SetParameter("Fill", 0f, 0.25f, 0f, 0.5f); Frame(handle, below, above); CheckEqual(new[] { 0.5f, 0.25f, 0f, 1f }, PixelAt(handle, 0, 0)); }); } [CpuTimeout(DeviceBudget)] public void ReadsTheSceneAtThePositionAskedFor() { WithWindow(handle => { ScreenEffect positions = Create(handle, "ProbeEffect"); positions.SetParameter("Fill", 0f, 0f, 0f, 1f); positions.SetParameter("ShowPosition", 1f); ScreenEffect shift = Create(handle, "ShiftEffect", readsScene: true); shift.SetParameter("Offset", 2f, 1f); Frame(handle, positions, shift); CheckEqual(new[] { 5.5f, 6.5f, 0f, 1f }, PixelAt(handle, 3, 5)); }); } [CpuTimeout(DeviceBudget)] public void UsesTheLatestValueOfAParameter() { WithWindow(handle => { ScreenEffect effect = Create(handle, "ProbeEffect"); effect.SetParameter("Fill", 1f, 0f, 0f, 1f); Frame(handle, effect); effect.SetParameter("Fill", 0f, 1f, 0f, 1f); Frame(handle, effect); CheckEqual(new[] { 0f, 1f, 0f, 1f }, PixelAt(handle, 0, 0)); }); } [CpuTimeout(DeviceBudget)] public void AnEffectDisposedAfterItWasQueuedThrows() { WithWindow(handle => { ScreenEffect effect = Create(handle, "ProbeEffect"); handle.FrameReset(); handle.DrawScreenEffect(effect); effect.Dispose(); CheckThrow(typeof(ObjectDisposedException)); handle.SubmitDrawCalls(); }); } [CpuTimeout(DeviceBudget)] public void AfterAResize_AnEffectKeepsItsParametersAndStillReadsTheScene() { WithWindow(handle => { ScreenEffect positions = Create(handle, "ProbeEffect"); positions.SetParameter("Fill", 0f, 0f, 0f, 1f); positions.SetParameter("ShowPosition", 1f); ScreenEffect shift = Create(handle, "ShiftEffect", readsScene: true); shift.SetParameter("Offset", 2f, 1f); Frame(handle, positions, shift); handle.Resize(64, 32); Frame(handle, positions, shift); CheckEqual(new[] { 5.5f, 6.5f, 0f, 1f }, PixelAt(handle, 3, 5)); }); } } public class SetParameterTests : TestFixture { [CpuTimeout(DeviceBudget)] public void ANameTheShaderLacksThrows() { WithWindow(handle => { ScreenEffect effect = Create(handle, "ProbeEffect"); CheckThrow(typeof(ArgumentException)); effect.SetParameter("Missing", 1f); }); } [CpuTimeout(DeviceBudget)] public void AParameterThatIsNotAFloat4Throws() { WithWindow(handle => { ScreenEffect effect = Create(handle, "ProbeEffect"); CheckThrow(typeof(ArgumentException)); effect.SetParameter("NotAFloat4", 1f); }); } } public class CtorTests : TestFixture { [CpuTimeout(DeviceBudget)] public void AShaderThatIsNotAScreenEffectThrows() { WithWindow(handle => { byte[] primitives = Resource( typeof(SharpDX9WindowHandle).Assembly, "Blacklight.SharpDX9.Shaders.Main.fxo"); CheckThrow(typeof(ArgumentException)); handle.CreateScreenEffect(new ByteView(primitives)); }); } } } }