Ejemplo n.º 1
0
        public void TestDefaultMessage()
        {
            // This will get the default message for Result.Ok code
            var descriptor = ResultDescriptor.Find(Result.Ok);

            Assert.True(!descriptor.Description.Contains("Unknown"));
        }
Ejemplo n.º 2
0
        public void TestDXGI()
        {
            // Force to load DXGI assembly
            var factory = new Factory1();

            factory.Dispose();
            // Look for DXGI descriptor SharpDX.DXGI.ResultCode.DeviceRemoved
            var descriptor = ResultDescriptor.Find(0x887A0005);

            Assert.AreEqual(descriptor.NativeApiCode, "DXGI_ERROR_DEVICE_REMOVED");
        }
Ejemplo n.º 3
0
            /// <summary>
            /// Presents this instance.
            /// </summary>
            /// <returns></returns>
            public override bool Present()
            {
                var res = swapChain.Present(VSyncInterval, PresentFlags.None, presentParams);

                if (res.Success)
                {
                    return(true);
                }
                else
                {
                    var desc = ResultDescriptor.Find(res);
                    if (desc == global::SharpDX.DXGI.ResultCode.DeviceRemoved || desc == global::SharpDX.DXGI.ResultCode.DeviceReset || desc == global::SharpDX.DXGI.ResultCode.DeviceHung)
                    {
                        RaiseOnDeviceLost();
                    }
                    else
                    {
                        swapChain.Present(VSyncInterval, PresentFlags.Restart, presentParams);
                    }
                    return(false);
                }
            }
Ejemplo n.º 4
0
 /// <summary>
 /// Updates the and render.
 /// </summary>
 public void UpdateAndRender()
 {
     if (CanRender())
     {
         IsBusy = true;
         var t0 = TimeSpan.FromSeconds((double)Stopwatch.GetTimestamp() / Stopwatch.Frequency);
         RenderStatistics.FPSStatistics.Push((t0 - lastRenderTime).TotalMilliseconds);
         RenderStatistics.Camera = viewport.CameraCore;
         lastRenderTime          = t0;
         UpdateRequested         = false;
         ++updateCounter;
         renderContext.AutoUpdateOctree      = RenderConfiguration.AutoUpdateOctree;
         renderContext.EnableBoundingFrustum = EnableRenderFrustum;
         if (RenderConfiguration.UpdatePerFrameData)
         {
             viewport.Update(t0);
             renderContext.TimeStamp           = t0;
             renderContext.Camera              = viewport.CameraCore;
             renderContext.WorldMatrix         = viewport.WorldMatrix;
             renderContext.OITWeightPower      = RenderConfiguration.OITWeightPower;
             renderContext.OITWeightDepthSlope = RenderConfiguration.OITWeightDepthSlope;
             renderContext.OITWeightMode       = RenderConfiguration.OITWeightMode;
         }
         PreRender();
         UpdateSceneGraphRequested = false;
         try
         {
             if (renderBuffer.BeginDraw())
             {
                 OnRender(t0);
                 renderBuffer.EndDraw();
             }
             if (RenderConfiguration.RenderD2D && D2DTarget.D2DTarget != null)
             {
                 OnRender2D(t0);
             }
             renderBuffer.Present();
         }
         catch (SharpDXException ex)
         {
             var desc = ResultDescriptor.Find(ex.ResultCode);
             if (desc == global::SharpDX.DXGI.ResultCode.DeviceRemoved || desc == global::SharpDX.DXGI.ResultCode.DeviceReset ||
                 desc == global::SharpDX.DXGI.ResultCode.DeviceHung || desc == global::SharpDX.Direct2D1.ResultCode.RecreateTarget ||
                 desc == global::SharpDX.DXGI.ResultCode.AccessLost)
             {
                 Log(LogLevel.Warning, $"Device Lost, code = {desc.Code}");
                 RenderBuffer_OnDeviceLost(RenderBuffer, EventArgs.Empty);
             }
             else
             {
                 Log(LogLevel.Error, ex);
                 EndD3D();
                 ExceptionOccurred?.Invoke(this, new RelayExceptionEventArgs(ex));
             }
         }
         catch (Exception ex)
         {
             Log(LogLevel.Error, ex);
             EndD3D();
             ExceptionOccurred?.Invoke(this, new RelayExceptionEventArgs(ex));
         }
         finally
         {
             PostRender();
             IsBusy = false;
         }
         lastRenderingDuration = TimeSpan.FromSeconds((double)Stopwatch.GetTimestamp() / Stopwatch.Frequency) - t0;
         RenderStatistics.LatencyStatistics.Push(lastRenderingDuration.TotalMilliseconds);
         OnRendered?.Invoke(this, EventArgs.Empty);
     }
 }
Ejemplo n.º 5
0
 public SharpGenException(string message, Exception?innerException = null)
     : this(ResultDescriptor.Find(innerException?.HResult ?? Result.Fail), message, innerException)
 {
 }
Ejemplo n.º 6
0
 public SharpGenException(string message, Exception?innerException = null, params object[]?args)
     : this(ResultDescriptor.Find(innerException?.HResult ?? Result.Fail), message, innerException, args)
 {
 }
Ejemplo n.º 7
0
 public SharpGenException(Result result, string?message = null, Exception?innerException = null, params object[]?args)
     : this(ResultDescriptor.Find(result), message, innerException, args)
 {
 }
Ejemplo n.º 8
0
 public SharpGenException(Result result, string?message = null, Exception?innerException = null)
     : this(ResultDescriptor.Find(result), message, innerException)
 {
 }
Ejemplo n.º 9
0
 public SharpGenException(Exception innerException, string?message = null)
     : this(ResultDescriptor.Find(innerException.HResult), message, innerException)
 {
 }
Ejemplo n.º 10
0
 public SharpGenException() : this(ResultDescriptor.Find(Result.Fail), GenericSharpGenExceptionMessage)
 {
 }
Ejemplo n.º 11
0
        private static void CaptureMain(Int32 adapterIndex, Int32 displayIndex)
        {
            Resource screenResource = null;

            try {
                using (Factory1 factory1 = new Factory1()) {
                    using (Adapter1 adapter1 = factory1.GetAdapter1(adapterIndex)) {
                        using (Device device = new Device(adapter1)) {
                            using (Output output = adapter1.GetOutput(displayIndex)) {
                                using (Output1 output1 = output.QueryInterface <Output1>()) {
                                    Int32                width  = output1.Description.DesktopBounds.Right - output1.Description.DesktopBounds.Left;
                                    Int32                height = output1.Description.DesktopBounds.Bottom - output1.Description.DesktopBounds.Top;
                                    Rectangle            bounds = new Rectangle(Point.Empty, new Size(width, height));
                                    Texture2DDescription texture2DDescription = new Texture2DDescription {
                                        CpuAccessFlags    = CpuAccessFlags.Read,
                                        BindFlags         = BindFlags.None,
                                        Format            = Format.B8G8R8A8_UNorm,
                                        Width             = width,
                                        Height            = height,
                                        OptionFlags       = ResourceOptionFlags.None,
                                        MipLevels         = 1,
                                        ArraySize         = 1,
                                        SampleDescription = { Count = 1, Quality = 0 },
                                        Usage             = ResourceUsage.Staging
                                    };
                                    using (Texture2D texture2D = new Texture2D(device, texture2DDescription)) {
                                        using (OutputDuplication outputDuplication = output1.DuplicateOutput(device)) {
                                            status = Status.Active;
                                            Int32 frameNumber = 0;
                                            do
                                            {
                                                try {
                                                    Result result = outputDuplication.TryAcquireNextFrame(100, out _, out screenResource);
                                                    if (result.Success)
                                                    {
                                                        frameNumber += 1;
                                                        using (Texture2D screenTexture2D = screenResource.QueryInterface <Texture2D>()) {
                                                            device.ImmediateContext.CopyResource(screenTexture2D, texture2D);
                                                            DataBox    dataBox           = device.ImmediateContext.MapSubresource(texture2D, 0, MapMode.Read, MapFlags.None);
                                                            Bitmap     bitmap            = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                                                            BitmapData bitmapData        = bitmap.LockBits(bounds, ImageLockMode.WriteOnly, bitmap.PixelFormat);
                                                            IntPtr     dataBoxPointer    = dataBox.DataPointer;
                                                            IntPtr     bitmapDataPointer = bitmapData.Scan0;
                                                            for (Int32 y = 0; y < height; y++)
                                                            {
                                                                Utilities.CopyMemory(bitmapDataPointer, dataBoxPointer, width * 4);
                                                                dataBoxPointer    = IntPtr.Add(dataBoxPointer, dataBox.RowPitch);
                                                                bitmapDataPointer = IntPtr.Add(bitmapDataPointer, bitmapData.Stride);
                                                            }
                                                            bitmap.UnlockBits(bitmapData);
                                                            device.ImmediateContext.UnmapSubresource(texture2D, 0);
                                                            while (SkipFrames && bitmapQueue.Count > 1)
                                                            {
                                                                bitmapQueue.TryDequeue(out Bitmap dequeuedBitmap);
                                                                dequeuedBitmap.Dispose();
                                                            }
                                                            if (frameNumber > 1 || SkipFirstFrame == false)
                                                            {
                                                                bitmapQueue.Enqueue(bitmap);
                                                                waitHandle.Set();
                                                            }
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (ResultDescriptor.Find(result).ApiCode != Result.WaitTimeout.ApiCode)
                                                        {
                                                            result.CheckError();
                                                        }
                                                    }
                                                } finally {
                                                    screenResource?.Dispose();
                                                    try {
                                                        outputDuplication.ReleaseFrame();
                                                    } catch { }
                                                }
                                            } while (status == Status.Active);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } catch (Exception exception) {
                globalException = exception;
                status          = Status.Stops;
            } finally {
                callbackThread.Join();
                Exception exception = globalException;
                while (bitmapQueue.Count > 0)
                {
                    bitmapQueue.TryDequeue(out Bitmap dequeuedBitmap);
                    dequeuedBitmap.Dispose();
                }
                globalException = null;
                waitHandle      = null;
                bitmapQueue     = null;
                captureThread   = null;
                callbackThread  = null;
                status          = Status.Inactive;
                if (OnCaptureStop != null)
                {
                    OnCaptureStop(null, new OnCaptureStopEventArgs(exception != null ? new Exception(exception.Message, exception) : null));
                }
                else
                {
                    if (exception != null)
                    {
                        ExceptionDispatchInfo.Capture(exception).Throw();
                    }
                }
            }
        }