using System; using System.IO; using Azimuth; using UnitTestSharp; using Blacklight.Core; using Blacklight.Core.Cameras; using Blacklight.Core.Drawables; namespace Blacklight.GPUBridge.UnitTests { // A fully static retained-mode scene (nothing added/removed/moved between frames), redrawn every frame through // the real WindowHandle -> ViewportBatcher -> BatchPool -> PrimitiveBatch pipeline. Only the GPU device itself // is swapped out (TestBatch/RealBatchFactory), so every allocation source a real backend would hit is present. public class EndToEndAllocationTests : TestFixture { private class RealBatchFactory : IBatchFactory { public IPrimitiveBatch CreateBatch() => new PrimitiveBatchTests.TestBatch(); public void BeginSubmit() { } public void Reset() { } public void ReleaseResources() { } } private class TestWindowHandle : WindowHandle { private readonly ViewportBatcher _batcher; public TestWindowHandle(int width, int height) : base(width, height) { _batcher = new ViewportBatcher(new RealBatchFactory(), this); } public override void EnqueueEntity(in Entity entity) => _batcher.QueueEntity(entity); public override void SubmitDrawCalls() => _batcher.SubmitDrawCalls(); public override void ClearToColor(Color color) { } public override Texture CreateTexture(Stream data, bool generateMipmaps) => throw new NotImplementedException(); public override void StartCameraBatch(Camera camera) => _batcher.StartCameraBatch(camera); public override void EndCameraBatch() => _batcher.EndCameraBatch(); public override void FrameReset() => _batcher.FrameReset(); public override void PrepareToBeginDrawingACel() { } } public class StaticSceneTests : TestFixture { private const int RectangleCount = 1000; private TestWindowHandle _windowHandle; private Scene _scene; private Camera _camera; public override void TestSetup() { _windowHandle = new TestWindowHandle(800, 600); _camera = new MicroscopeCamera(new Viewport(800, 600)); var collage = new Collage(); for (int i = 0; i < RectangleCount; ++i) { collage.AddChild(new Rectangle(), AffineMatrix.Identity); } var cel = new Cel(); cel.AddEntity(new Entity(collage, AffineMatrix.Identity)); _scene = new Scene(); _scene.Cels.Add(cel); } [AllocationTest] public void OneThousandStaticRectangles_PerFrameAllocation() { _windowHandle.DrawScene(_scene, _camera); CheckNoAllocations(); } } } }