예제 #1
0
 public virtual void DrawSceneForReflection(DeviceContextHolder holder, ICamera camera)
 {
     Scene.Draw(holder, camera, SpecialRenderMode.Reflection);
 }
예제 #2
0
 public virtual void DrawSceneForShadows(DeviceContextHolder holder, ICamera camera)
 {
     Scene.Draw(holder, camera, SpecialRenderMode.Shadow);
 }
예제 #3
0
        public virtual void Shot(double multiplier, double downscale, Stream outputStream, bool lossless)
        {
            var resolutionMultiplier = ResolutionMultiplier;
            var format = lossless ? ImageFileFormat.Png : ImageFileFormat.Jpg;

            ResolutionMultiplier = multiplier;

            if (Equals(downscale, 1d) && !UseMsaa)
            {
                // Simplest case: existing buffer will do just great, so let’s use it
                Draw();
                Texture2D.ToStream(DeviceContext, _renderBuffer, format, outputStream);
            }
            else
            {
                // More complicated situation: we need to temporary replace existing _renderBuffer
                // with a custom one

                // Preparing temporary replacement for _renderBuffer…
                if (_shotRenderBuffer == null)
                {
                    // Let’s keep all those buffers in memory between shots to make series shooting faster
                    _shotRenderBuffer = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                }

                _shotRenderBuffer.Resize(DeviceContextHolder, Width, Height, SampleDescription);

                // If we won’t call Resize() here, it will be called in Draw(), and then swap chain will
                // be recreated in a wrong time
                if (_resized)
                {
                    Resize();
                    _resized = false;
                }

                // Destroying current swap chain if needed
                var swapChainMode = _swapChain != null;
                if (swapChainMode)
                {
                    DisposeHelper.Dispose(ref _swapChain);
                }

                // Replacing _renderBuffer…
                var renderView = _renderView;
                _renderView = _shotRenderBuffer.TargetView;

                // Calculating output width and height and, if needed, preparing downscaled buffer…
                var outputWidth  = (Width * downscale).RoundToInt();
                var outputHeight = (Height * downscale).RoundToInt();

                if (!Equals(downscale, 1d))
                {
                    if (_shotDownsampleTexture == null)
                    {
                        _shotDownsampleTexture = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                    }

                    _shotDownsampleTexture.Resize(DeviceContextHolder, outputWidth, outputHeight, null);
                }

                // Ready to draw!
                Draw();

                // For MSAA, we need to copy the result into a texture without MSAA enabled to save it later
                TargetResourceTexture result;
                if (UseMsaa)
                {
                    if (_shotMsaaTemporaryTexture == null)
                    {
                        _shotMsaaTemporaryTexture = TargetResourceTexture.Create(Format.R8G8B8A8_UNorm);
                    }

                    _shotMsaaTemporaryTexture.Resize(DeviceContextHolder, Width, Height, null);

                    DeviceContextHolder.GetHelper <CopyHelper>()
                    .Draw(DeviceContextHolder, _shotRenderBuffer.View, _shotMsaaTemporaryTexture.TargetView);
                    result = _shotMsaaTemporaryTexture;
                }
                else
                {
                    result = _shotRenderBuffer;
                }

                if (Equals(downscale, 1d))
                {
                    Texture2D.ToStream(DeviceContext, result.Texture, format, outputStream);
                }
                else
                {
                    DeviceContextHolder.GetHelper <DownsampleHelper>()
                    .Draw(DeviceContextHolder, result, _shotDownsampleTexture, false);
                    Texture2D.ToStream(DeviceContext, _shotDownsampleTexture.Texture, format, outputStream);
                }

                // Restoring old stuff
                _renderView = renderView;

                if (swapChainMode)
                {
                    RecreateSwapChain();
                }
            }

            ResolutionMultiplier = resolutionMultiplier;
        }