public void Bind(D3D9.Device dev, D3D9.Volume volume, D3D9.BaseTexture mipTex)
        {
            //Entering critical section
            LockDeviceAccess();

            var bufferResources = GetBufferResources(dev);
            var isNewBuffer     = false;

            if (bufferResources == null)
            {
                bufferResources = new BufferResources();
                this.mapDeviceToBufferResources.Add(dev, bufferResources);
                isNewBuffer = true;
            }

            bufferResources.MipTex = mipTex;
            bufferResources.Volume = volume;

            var desc = volume.Description;

            width  = desc.Width;
            height = desc.Height;
            depth  = desc.Depth;
            format = D3D9Helper.ConvertEnum(desc.Format);
            // Default
            rowPitch    = Width;
            slicePitch  = Height * Width;
            sizeInBytes = PixelUtil.GetMemorySize(Width, Height, Depth, Format);

            if (isNewBuffer && this.ownerTexture.IsManuallyLoaded)
            {
                foreach (var it in this.mapDeviceToBufferResources)
                {
                    if (it.Value != bufferResources && it.Value.Volume != null && it.Key.TestCooperativeLevel().Success&&
                        dev.TestCooperativeLevel().Success)
                    {
                        var fullBufferBox = new BasicBox(0, 0, 0, Width, Height, Depth);
                        var dstBox        = new PixelBox(fullBufferBox, Format);

                        var data = new byte[sizeInBytes];
                        using (var d = BufferBase.Wrap(data))
                        {
                            dstBox.Data = d;
                            BlitToMemory(fullBufferBox, dstBox, it.Value, it.Key);
                            BlitFromMemory(dstBox, fullBufferBox, bufferResources);
                            Array.Clear(data, 0, sizeInBytes);
                        }
                        break;
                    }
                }
            }

            //Leaving critical section
            UnlockDeviceAccess();
        }
Beispiel #2
0
        private void Render()
        {
            elaspedTime += _timer.GetElaspedTime();
            if (elaspedTime < REFRESH)
            {
                Thread.Sleep(1);
                return;
            }

            lock (emote)
            {
                emote.Update((float)REFRESH * 1000);

                //_device.Clear(ClearFlags.Target, Color.Transparent, 1.0f, 0);
                //_device.Clear(ClearFlags.Target, new SharpDX.Mathematics.Interop.RawColorBGRA(0, 0, 0, 0), 1.0f, 0);
                //_device.Clear(ClearFlags.Target, new SharpDX.Mathematics.Interop.RawColorBGRA(0, 255, 0, 255), 1.0f, 0);
                _device.Clear(ClearFlags.Target, new SharpDX.Mathematics.Interop.RawColorBGRA(0, 0, 0, 255), 1.0f, 0);

                _device.BeginScene();
                //device.UpdateSurface(device.GetBackBuffer(0,0),new Surface(new IntPtr(e.D3DSurface)));
                emote.Draw();
                _device.EndScene();
                try
                {
                    _device.Present();
                }
                catch (SharpDXException exception)
                {
                    if (exception.ResultCode == SharpDX.Direct3D9.ResultCode.DeviceLost)
                    {
                        Console.WriteLine("Device Lost Detected");
                        emote.OnDeviceLost();
                        Result r;
                        while ((r = _device.TestCooperativeLevel()) == SharpDX.Direct3D9.ResultCode.DeviceLost)
                        {
                            Thread.Sleep(5);
                        }
                        r = _device.TestCooperativeLevel();
                        if (r == SharpDX.Direct3D9.ResultCode.DeviceNotReset)
                        {
                            emote.D3DReset();
                            emote.OnDeviceReset();
                            //e.D3DInitRenderState();
                        }
                        else
                        {
                            Console.WriteLine(r);
                        }
                        //r = _device.TestCooperativeLevel();
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            //if (EnableTransparentWindow)
            //{
            //    emote.D3DAfterPresentSetTransparentWindow();
            //}

            elaspedTime = 0;
        }
        private void HandleCaptureRequest(Device device)
        {
            try
            {
                bool tmp;
                if (this.queryIssued && this.query.GetData(out tmp, false))
                {
                    this.queryIssued = false;
                    var lockedRect = this.surface.LockRectangle(LockFlags.ReadOnly);
                    this.surfaceDataPointer = lockedRect.DataPointer;
                    this.surfaceLocked = true;

                    this.copyEvent.Set();
                }

                using (var backbuffer = device.GetBackBuffer(0, 0))
                {
                    device.StretchRectangle(backbuffer, this.renderTarget, TextureFilter.None);
                }

                if (this.surfaceLocked)
                {
                    lock (this.renderTargetLock)
                    {
                        if (this.surfaceLocked)
                        {
                            this.surface.UnlockRectangle();
                            this.surfaceLocked = false;
                        }
                    }
                }

                try
                {
                    var cooplevel = device.TestCooperativeLevel();
                    if (cooplevel.Code == ResultCode.Success.Code)
                    {
                        device.GetRenderTargetData(this.renderTarget, this.surface);
                        this.query.Issue(Issue.End);
                        this.queryIssued = true;
                    }
                    else
                    {
                        this.DebugMessage(string.Format("DirectX Error: TestCooperativeLevel = {0}", cooplevel.Code));
                    }
                }
                catch (Exception ex)
                {
                    this.DebugMessage(ex.ToString());
                }
            }
            catch (Exception e)
            {
                this.DebugMessage(e.ToString());
            }
        }
Beispiel #4
0
        public void display(VideoFrame videoFrame, Color backColor, RenderMode mode)
        {
            lock (renderLock)
            {
                if (device == null)
                {
                    return;
                }

                SharpDX.Result deviceStatus = device.TestCooperativeLevel();

                if (deviceStatus.Success == true)
                {
                    try
                    {
                        SharpDX.ColorBGRA backColorDX = new SharpDX.ColorBGRA(backColor.R,
                                                                              backColor.G, backColor.B, backColor.A);

                        device.Clear(D3D.ClearFlags.Target, backColorDX, 1.0f, 0);

                        if (mode == RenderMode.CLEAR_SCREEN)
                        {
                            device.Present();
                            return;
                        }

                        device.BeginScene();

                        SharpDX.Rectangle videoSourceRect = new SharpDX.Rectangle();

                        if (mode == RenderMode.NORMAL)
                        {
                            videoSourceRect = new SharpDX.Rectangle(0, 0, videoFrame.Width, videoFrame.Height);

                            SharpDX.DataRectangle stream = offscreen.LockRectangle(LockFlags.None);

                            videoFrame.copyFrameDataToSurface(stream.DataPointer, stream.Pitch);

                            offscreen.UnlockRectangle();
                        }
                        else if (mode == RenderMode.PAUSED)
                        {
                            videoSourceRect = new SharpDX.Rectangle(0, 0, offscreen.Description.Width, offscreen.Description.Height);
                        }

                        videoDestRect = getVideoDestRect(backBuffer);

                        device.StretchRectangle(offscreen, videoSourceRect,
                                                backBuffer, videoDestRect, D3D.TextureFilter.Linear);

                        drawText();

                        device.EndScene();
                        device.Present();

                        RenderMode = mode;
                    }
                    catch (SharpDX.SharpDXException)
                    {
                        //log.Info("lost direct3d device", e);
                        deviceStatus = device.TestCooperativeLevel();
                    }
                }

                if (deviceStatus.Code == D3D.ResultCode.DeviceLost.Result)
                {
                    //Can't Reset yet, wait for a bit
                }
                else if (deviceStatus.Code == D3D.ResultCode.DeviceNotReset.Result)
                {
                    resetDevice();
                }
            }
        }