static void Main(string[] args) { try { var mainGameComponent = new MainGameComponent(); var deviceResourcesOptions = new DeviceResourcesOptions(); #if DEBUG deviceResourcesOptions.Debug = true; #endif var deviceResources = new RenderTargetDeviceResources(1920, 1080, mainGameComponent.MinimalFeatureLevel, deviceResourcesOptions); var device = deviceResources.D3DDevice; var context = deviceResources.D3DContext; Console.WriteLine(deviceResources.AdapterDescription.AdapterDescription); var queryDisjoint = device.CreateQuery(new D3D11QueryDesc(D3D11QueryType.TimestampDisjoint)); var queryStart = device.CreateQuery(new D3D11QueryDesc(D3D11QueryType.Timestamp)); var queryEnd = device.CreateQuery(new D3D11QueryDesc(D3D11QueryType.Timestamp)); var queryEvent = device.CreateQuery(new D3D11QueryDesc(D3D11QueryType.Event)); context.Begin(queryDisjoint); context.End(queryStart); mainGameComponent.CreateDeviceDependentResources(deviceResources); mainGameComponent.CreateWindowSizeDependentResources(); mainGameComponent.Update(null); mainGameComponent.Render(); deviceResources.Present(); context.End(queryEvent); while (context.GetData(queryEvent)) { System.Threading.Thread.Sleep(1); } context.End(queryEnd); context.End(queryDisjoint); long timestampStart; while (context.GetData(queryStart, D3D11AsyncGetDataOptions.None, out timestampStart)) { Console.WriteLine("timestampStart sleep"); System.Threading.Thread.Sleep(1); } long timestampEnd; while (context.GetData(queryEnd, D3D11AsyncGetDataOptions.None, out timestampEnd)) { Console.WriteLine("timestampEnd sleep"); System.Threading.Thread.Sleep(1); } D3D11QueryDataTimestampDisjoint disjointData; while (context.GetData(queryDisjoint, D3D11AsyncGetDataOptions.None, out disjointData)) { Console.WriteLine("disjointData sleep"); System.Threading.Thread.Sleep(1); } Console.WriteLine("timestampStart: {0}", timestampStart); Console.WriteLine("timestampEnd: {0}", timestampEnd); if (disjointData.IsDisjoint) { Console.WriteLine("disjointData is disjoint"); } else { Console.WriteLine("frequency: {0}", disjointData.Frequency); long delta = timestampEnd - timestampStart; double time = (delta * 1000.0) / disjointData.Frequency; Console.WriteLine("delta: {0}", delta); Console.WriteLine("time: {0}", time); } } catch (Exception ex) { Console.WriteLine(ex); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(true); }
public static byte[] Generate(uint width, uint height, string optFileName, RenderFace side, RenderMethod method) { var options = new DeviceResourcesOptions { UseHighestFeatureLevel = false, PreferMultisampling = true }; var deviceResources = new RenderTargetDeviceResources(width, height, D3D11FeatureLevel.FeatureLevel100, options); var optComponent = new OptComponent(optFileName); try { optComponent.CreateDeviceDependentResources(deviceResources); optComponent.CreateWindowSizeDependentResources(); optComponent.Update(null); optComponent.WorldMatrix = XMMatrix.Translation(optComponent.OptCenter.X, -optComponent.OptCenter.Z, optComponent.OptCenter.Y) * XMMatrix.ScalingFromVector(XMVector.Replicate(0.95f / optComponent.OptSize)); switch (side) { case RenderFace.Top: default: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 2, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 0, 1)); break; case RenderFace.Front: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(0, 0, 2), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0)); break; case RenderFace.Side: optComponent.ViewMatrix = XMMatrix.LookAtRH(new XMFloat3(2, 0, 0), new XMFloat3(0, 0, 0), new XMFloat3(0, 1, 0)); break; } optComponent.ProjectionMatrix = XMMatrix.OrthographicRH(1, 1, 0, 5); var context = deviceResources.D3DContext; context.OutputMergerSetRenderTargets(new[] { deviceResources.D3DRenderTargetView }, deviceResources.D3DDepthStencilView); context.ClearRenderTargetView(deviceResources.D3DRenderTargetView, new[] { 0.0f, 0.0f, 0.0f, 0.0f }); context.ClearDepthStencilView(deviceResources.D3DDepthStencilView, D3D11ClearOptions.Depth, 1.0f, 0); optComponent.Render(); deviceResources.Present(); var buffer = deviceResources.GetBackBufferContent(); for (int i = 0; i < width * height; i++) { byte a = buffer[i * 4 + 3]; buffer[i * 4 + 3] = a == 0 ? (byte)0 : (byte)255; } switch (method) { case RenderMethod.Color: default: return(buffer); case RenderMethod.GrayScaleLight: return(ConvertToGrayScaleLight(buffer, (int)width, (int)height)); case RenderMethod.GrayScaleDark: return(ConvertToGrayScaleDark(buffer, (int)width, (int)height)); case RenderMethod.Blue: return(ConvertToBlue(buffer, (int)width, (int)height)); } } finally { optComponent.ReleaseWindowSizeDependentResources(); optComponent.ReleaseDeviceDependentResources(); deviceResources.Release(); } }