Ejemplo n.º 1
0
        private void Update(EvaluationContext context)
        {
            var resourceManager = ResourceManager.Instance();
            var device          = resourceManager.Device;

            Size2 size = Resolution.GetValue(context);

            if (size.Width == 0 || size.Height == 0)
            {
                size = context.RequestedResolution;
            }

            if (size.Width <= 0 || size.Height <= 0 || size.Width > MaximumTexture2DSize || size.Height > MaximumTexture2DSize)
            {
                Log.Warning("Invalid texture size: " + size);
                return;
            }

            _sampleCount = Multisampling.GetValue(context).Clamp(1, 32);

            bool generateMips    = GenerateMips.GetValue(context);
            var  withDepthBuffer = WithDepthBuffer.GetValue(context);

            UpdateTextures(device, size, TextureFormat.GetValue(context), withDepthBuffer ? Format.R32_Typeless : Format.Unknown, generateMips);

            var deviceContext = device.ImmediateContext;

            // Save settings in context
            var prevRequestedResolution = context.RequestedResolution;
            var prevViewports           = deviceContext.Rasterizer.GetViewports <RawViewportF>();
            var prevTargets             = deviceContext.OutputMerger.GetRenderTargets(2, out var prevDepthStencilView);
            var prevObjectToWorld       = context.ObjectToWorld;
            var prevWorldToCamera       = context.WorldToCamera;
            var prevCameraToClipSpace   = context.CameraToClipSpace;

            deviceContext.Rasterizer.SetViewport(new SharpDX.Viewport(0, 0, size.Width, size.Height, 0.0f, 1.0f));
            deviceContext.OutputMerger.SetTargets(_multiSampledDepthBufferDsv, _multiSampledColorBufferRtv);

            // Clear
            var clear = Clear.GetValue(context);
            var c     = ClearColor.GetValue(context);

            if (clear || !_wasClearedOnce)
            {
                try
                {
                    deviceContext.ClearRenderTargetView(_multiSampledColorBufferRtv, new Color(c.X, c.Y, c.Z, c.W));
                    if (_multiSampledDepthBufferDsv != null)
                    {
                        deviceContext.ClearDepthStencilView(_multiSampledDepthBufferDsv, DepthStencilClearFlags.Depth, 1.0f, 0);
                    }

                    _wasClearedOnce = true;
                }
                catch
                {
                    Log.Error($"{Parent.Symbol.Name}: Error clearing actual render target.", SymbolChildId);
                }
            }



            // Set default values for new sub tree
            context.RequestedResolution = size;
            context.SetDefaultCamera();

            if (TextureReference.IsConnected)
            {
                var reference = TextureReference.GetValue(context);
                reference.ColorTexture = ColorTexture;
                reference.DepthTexture = DepthTexture;
            }

            // Render
            Command.GetValue(context);

            // Restore context
            context.ObjectToWorld       = prevObjectToWorld;
            context.WorldToCamera       = prevWorldToCamera;
            context.CameraToClipSpace   = prevCameraToClipSpace;
            context.RequestedResolution = prevRequestedResolution;
            deviceContext.Rasterizer.SetViewports(prevViewports);
            deviceContext.OutputMerger.SetTargets(prevDepthStencilView, prevTargets);


            if (_sampleCount > 1)
            {
                try
                {
                    device.ImmediateContext.ResolveSubresource(_multiSampledColorBuffer,
                                                               0,
                                                               _resolvedColorBuffer, 0,
                                                               _resolvedColorBuffer.Description.Format);

                    if (withDepthBuffer)
                    {
                        ResolveDepthBuffer();
                    }
                }
                catch (Exception e)
                {
                    Log.Error("resolving render target buffer failed:" + e.Message);
                }
            }

            if (generateMips)
            {
                deviceContext.GenerateMips(DownSamplingRequired ? _resolvedColorBufferSrv : _multiSampledColorBufferSrv);
            }

            // Clean up ref counts for RTVs
            for (int i = 0; i < prevTargets.Length; i++)
            {
                prevTargets[i]?.Dispose();
            }

            prevDepthStencilView?.Dispose();

            ColorBuffer.Value = ColorTexture;
            ColorBuffer.DirtyFlag.Clear();
            DepthBuffer.Value = DepthTexture;
            DepthBuffer.DirtyFlag.Clear();
        }