Esempio n. 1
0
        protected override void CreateDeviceCompatibleTexture(int width, int height, IDisposable texture10, out IDisposable texture11, out IDisposable srv11)
        {
            var texture  = (global::SharpDX.Direct3D11.Texture2D)texture10;
            var device11 = ((SpriteRenderer)Sprite).Device;

            lock (device11)
            {
                var dxgiResource = texture.QueryInterface <global::SharpDX.DXGI.Resource>();

                global::SharpDX.Direct3D11.Texture2D tex11;
                if (PixCompatible)
                {
                    tex11 = new global::SharpDX.Direct3D11.Texture2D(device11, new global::SharpDX.Direct3D11.Texture2DDescription()
                    {
                        ArraySize         = 1,
                        BindFlags         = global::SharpDX.Direct3D11.BindFlags.ShaderResource | global::SharpDX.Direct3D11.BindFlags.RenderTarget,
                        CpuAccessFlags    = global::SharpDX.Direct3D11.CpuAccessFlags.None,
                        Format            = Format.R8G8B8A8_UNorm,
                        Height            = height,
                        Width             = width,
                        MipLevels         = 1,
                        OptionFlags       = global::SharpDX.Direct3D11.ResourceOptionFlags.Shared,
                        SampleDescription = new SampleDescription(1, 0),
                        Usage             = global::SharpDX.Direct3D11.ResourceUsage.Default
                    });
                }
                else
                {
                    tex11 = device11.OpenSharedResource <global::SharpDX.Direct3D11.Texture2D>(dxgiResource.SharedHandle);
                }
                srv11     = new global::SharpDX.Direct3D11.ShaderResourceView(device11, tex11);
                texture11 = tex11;
                dxgiResource.Dispose();
            }
        }
            /// <summary>
            /// Called when [render].
            /// </summary>
            /// <param name="context">The context.</param>
            /// <param name="deviceContext">The device context.</param>
            public override void Render(RenderContext context, DeviceContextProxy deviceContext)
            {
                var succ = duplicationResource.Initialize();

                if (!succ)
                {
                    RaiseInvalidateRender();
                    return;
                }
                context.RenderHost.RenderConfiguration = config;

                if (duplicationResource.GetFrame(Output, out var data, ref pointer, out var isTimeOut, out var accessLost))
                {
                    if (data.FrameInfo.TotalMetadataBufferSize > 0)
                    {
                        frameProcessor.ProcessFrame(ref data, deviceContext);
                    }
                    invalidRender = true;
                }

                if (frameProcessor.SharedTexture != null && !accessLost)
                {
                    if (clearTarget)
                    {
                        clearTarget = false;
                        context.RenderHost.ClearRenderTarget(deviceContext, true, false);
                    }
                    var cursorValid = false;
                    if (pointer.Visible)
                    {
                        if (frameProcessor.ProcessCursor(ref pointer, deviceContext, out var rect))
                        {
                            GetCursorVertexBound((int)context.ActualWidth, (int)context.ActualHeight, frameProcessor.TextureWidth, frameProcessor.TextureHeight, ref rect);
                            invalidRender = true;
                            cursorValid   = true;
                        }
                    }
                    if (invalidRender)
                    {
                        OnUpdatePerModelStruct(context);
                        modelCB.Upload(deviceContext, ref modelStruct);
                        DefaultShaderPass.BindShader(deviceContext);
                        DefaultShaderPass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState | StateType.RasterState);
                        DefaultShaderPass.PixelShader.BindSampler(deviceContext, samplerBindSlot, textureSampler);
                        var left = (int)(context.ActualWidth * Math.Abs(modelStruct.TopLeft.X + 1) / 2);
                        var top  = (int)(context.ActualHeight * Math.Abs(modelStruct.TopLeft.Y - 1) / 2);
                        deviceContext.SetScissorRectangle(left, top, (int)context.ActualWidth - left, (int)context.ActualHeight - top);
                        using (var textureView = new global::SharpDX.Direct3D11.ShaderResourceView(deviceContext, frameProcessor.SharedTexture))
                        {
                            deviceContext.SetShaderResource(PixelShader.Type, textureBindSlot, textureView);
                            deviceContext.Draw(4, 0);
                        }
                        if (ShowMouseCursor && cursorValid)
                        {
                            DrawCursor(ref pointer, deviceContext);
                        }
                        invalidRender = false;
                    }
                }
                if (isTimeOut)
                {
                }
                else if (accessLost)
                {
                    throw new SharpDXException(ResultCode.AccessLost);
                }
                else
                {
                    duplicationResource.ReleaseFrame();
                }
                RaiseInvalidateRender();
            }
Esempio n. 3
0
        /// <summary>
        /// Called when [render].
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="deviceContext">The device context.</param>
        protected override void OnRender(IRenderContext context, DeviceContextProxy deviceContext)
        {
            bool succ = duplicationResource.Initialize();

            if (!succ)
            {
                InvalidateRenderer();
                return;
            }
            context.RenderHost.RenderConfiguration = config;
            FrameData data;

            bool isTimeOut;
            bool accessLost;


            if (duplicationResource.GetFrame(Output, out data, ref pointer, out isTimeOut, out accessLost))
            {
                if (data.FrameInfo.TotalMetadataBufferSize > 0)
                {
                    frameProcessor.ProcessFrame(ref data, deviceContext);
                }
                invalidRender = true;
            }

            if (frameProcessor.SharedTexture != null && !accessLost)
            {
                if (clearTarget)
                {
                    clearTarget = false;
                    context.RenderHost.ClearRenderTarget(deviceContext, true, false);
                }
                bool cursorValid = false;
                if (pointer.Visible)
                {
                    Vector4 rect;
                    if (frameProcessor.ProcessCursor(ref pointer, deviceContext, out rect))
                    {
                        GetCursorVertexBound((int)context.ActualWidth, (int)context.ActualHeight, frameProcessor.TextureWidth, frameProcessor.TextureHeight, ref rect);
                        invalidRender = true;
                        cursorValid   = true;
                    }
                }
                if (invalidRender)
                {
                    ModelConstBuffer.UploadDataToBuffer(deviceContext, ref modelStruct);
                    DefaultShaderPass.BindShader(deviceContext);
                    DefaultShaderPass.BindStates(deviceContext, StateType.BlendState | StateType.DepthStencilState | StateType.RasterState);
                    deviceContext.DeviceContext.InputAssembler.PrimitiveTopology = global::SharpDX.Direct3D.PrimitiveTopology.TriangleStrip;
                    DefaultShaderPass.GetShader(ShaderStage.Pixel).BindSampler(deviceContext, samplerBindSlot, textureSampler);
                    int left = (int)(context.ActualWidth * Math.Abs(modelStruct.TopLeft.X + 1) / 2);
                    int top  = (int)(context.ActualHeight * Math.Abs(modelStruct.TopLeft.Y - 1) / 2);
                    deviceContext.DeviceContext.Rasterizer.SetScissorRectangle(left, top, (int)context.ActualWidth - left, (int)context.ActualHeight - top);
                    using (var textureView = new global::SharpDX.Direct3D11.ShaderResourceView(deviceContext.DeviceContext.Device, frameProcessor.SharedTexture))
                    {
                        DefaultShaderPass.GetShader(ShaderStage.Pixel).BindTexture(deviceContext, textureBindSlot, textureView);
                        deviceContext.DeviceContext.Draw(4, 0);
                    }
                    if (ShowMouseCursor && cursorValid)
                    {
                        DrawCursor(ref pointer, deviceContext);
                    }
                    invalidRender = false;
                }
            }
            if (isTimeOut)
            {
            }
            else if (accessLost)
            {
                throw new SharpDXException(ResultCode.AccessLost);
            }
            else
            {
                duplicationResource.ReleaseFrame();
            }
            InvalidateRenderer();
        }