Ejemplo n.º 1
0
            /// <summary>
            /// Creates a render target texture with the given width and height.
            /// </summary>
            /// <param name="device">Graphics device.</param>
            /// <param name="width">Width of generated texture.</param>
            /// <param name="height">Height of generated texture.</param>
            /// <param name="gfxConfig">The GFX configuration.</param>
            public static D3D11.ID3D11Texture2D CreateRenderTargetTexture(
                EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));
                gfxConfig.EnsureNotNull(nameof(gfxConfig));

                var textureDescription = new D3D11.Texture2DDescription();

                if (gfxConfig.AntialiasingEnabled &&
                    device.IsStandardAntialiasingPossible)
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Format            = DEFAULT_TEXTURE_FORMAT;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality);
                    textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }
                else
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Format            = DEFAULT_TEXTURE_FORMAT;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = new SampleDescription(1, 0);
                    textureDescription.BindFlags         = D3D11.BindFlags.ShaderResource | D3D11.BindFlags.RenderTarget;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }

                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
        /// <summary>
        /// Creates a default SwapChain for the given target control.
        /// </summary>
        /// <param name="targetControl">Target control of the swap chain.</param>
        /// <param name="device">Graphics device.</param>
        /// <param name="gfxConfig">The current graphics configuration.</param>
        internal static IDXGISwapChain1 CreateSwapChainForWinForms(WinForms.Control targetControl, EngineDevice device, GraphicsViewConfiguration gfxConfig)
        {
            targetControl.EnsureNotNull(nameof(targetControl));
            device.EnsureNotNull(nameof(device));
            gfxConfig.EnsureNotNull(nameof(gfxConfig));

            // Create the swap chain description
            var swapChainDesc = new SwapChainDescription1();

            if (gfxConfig.AntialiasingEnabled && device.IsStandardAntialiasingPossible)
            {
                swapChainDesc.BufferCount       = 2;
                swapChainDesc.SampleDescription = device.Internals.GetSampleDescription(gfxConfig.AntialiasingQuality);
            }
            else
            {
                swapChainDesc.BufferCount       = 2;
                swapChainDesc.SampleDescription = new SampleDescription(1, 0);
            }

            // Set common parameters
            swapChainDesc.Width      = targetControl.Width;
            swapChainDesc.Height     = targetControl.Height;
            swapChainDesc.Format     = GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT;
            swapChainDesc.Scaling    = Scaling.Stretch;
            swapChainDesc.SwapEffect = SwapEffect.Discard;
            swapChainDesc.Usage      = Usage.RenderTargetOutput;

            // Create and return the swap chain and the render target
            return(device.Internals.FactoryDxgi.CreateSwapChainForHwnd(
                       device.Internals.DeviceD3D11_1, targetControl.Handle,
                       swapChainDesc));
        }
Ejemplo n.º 3
0
 public void EditViewConfiguration(RenderLoop renderLoop, GraphicsViewConfiguration viewConfig)
 {
 }
Ejemplo n.º 4
0
            /// <summary>
            /// Creates a depth buffer texture with given width and height.
            /// </summary>
            /// <param name="device">Graphics device.</param>
            /// <param name="width">Width of generated texture.</param>
            /// <param name="height">Height of generated texture.</param>
            /// <param name="gfxConfig">Current graphics configuration.</param>
            public static D3D11.ID3D11Texture2D CreateDepthBufferTexture(EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig)
            {
                device.EnsureNotNull(nameof(device));
                width.EnsurePositiveOrZero(nameof(width));
                height.EnsurePositiveOrZero(nameof(height));
                gfxConfig.EnsureNotNull(nameof(gfxConfig));

                var textureDescription = new D3D11.Texture2DDescription();

                if (gfxConfig.AntialiasingEnabled &&
                    device.IsStandardAntialiasingPossible)
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = device.GetSampleDescription(gfxConfig.AntialiasingQuality);
                    textureDescription.BindFlags         = D3D11.BindFlags.DepthStencil;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }
                else
                {
                    textureDescription.Width             = width;
                    textureDescription.Height            = height;
                    textureDescription.MipLevels         = 1;
                    textureDescription.ArraySize         = 1;
                    textureDescription.Usage             = D3D11.ResourceUsage.Default;
                    textureDescription.SampleDescription = new SampleDescription(1, 0);
                    textureDescription.BindFlags         = D3D11.BindFlags.DepthStencil;
                    textureDescription.CpuAccessFlags    = D3D11.CpuAccessFlags.None;
                    textureDescription.OptionFlags       = D3D11.ResourceOptionFlags.None;
                }

                // Set buffer format
                textureDescription.Format = Format.D32_Float_S8X24_UInt;

                // Create the texture finally
                return(device.DeviceD3D11_1.CreateTexture2D(textureDescription));
            }
        /// <summary>
        /// Creates the SwapChain object that is used on WinRT platforms.
        /// </summary>
        /// <param name="device">The device on which to create the SwapChain.</param>
        /// <param name="width">Width of the screen in pixels.</param>
        /// <param name="height">Height of the screen in pixels.</param>
        /// <param name="gfxConfig">Current graphics configuration.</param>
        internal static IDXGISwapChain1 CreateSwapChainForComposition(EngineDevice device, int width, int height, GraphicsViewConfiguration gfxConfig)
        {
            device.EnsureNotNull(nameof(device));
            width.EnsurePositiveOrZero(nameof(width));
            height.EnsurePositiveOrZero(nameof(height));
            gfxConfig.EnsureNotNull(nameof(gfxConfig));

            var desc = new SwapChainDescription1()
            {
                Width             = width,
                Height            = height,
                Format            = GraphicsHelper.Internals.DEFAULT_TEXTURE_FORMAT,
                Stereo            = false,
                SampleDescription = new SampleDescription(1, 0),
                Usage             = Usage.Backbuffer | Usage.RenderTargetOutput,
                BufferCount       = 2,
                Scaling           = Scaling.Stretch,
                SwapEffect        = SwapEffect.FlipSequential,
                AlphaMode         = gfxConfig.AlphaEnabledSwapChain ? AlphaMode.Premultiplied : AlphaMode.Ignore
            };

            return(device.Internals.FactoryDxgi.CreateSwapChainForComposition(device.Internals.DeviceD3D11_1, desc));
        }
        /// <summary>
        /// Applies the given size.
        /// </summary>
        /// <param name="renderState">The render state used for creating all resources.</param>
        public void ApplySize(RenderState renderState)
        {
            ViewInformation           viewInfo   = renderState.ViewInformation;
            GraphicsViewConfiguration viewConfig = viewInfo.ViewConfiguration;

            // Get current view size and antialiasing settings
            Size2 currentViewSize            = viewInfo.CurrentViewSize;
            bool  currentAntialiasingEnabled = viewConfig.AntialiasingEnabled;
            AntialiasingQualityLevel currentAntialiasingQuality = viewConfig.AntialiasingQuality;

            if ((m_width != currentViewSize.Width) ||
                (m_heigth != currentViewSize.Height) ||
                (m_antialiasingEnabled != currentAntialiasingEnabled) ||
                (m_antialiasingQuality != currentAntialiasingQuality))
            {
                // Dispose color-buffer resources
                GraphicsHelper.SafeDispose(ref m_colorBuffer);
                GraphicsHelper.SafeDispose(ref m_colorBufferRenderTargetView);
                GraphicsHelper.SafeDispose(ref m_colorBufferShaderResourceView);
                if (m_shaderResourceCreated)
                {
                    GraphicsHelper.SafeDispose(ref m_colorBufferShaderResource);
                }

                // Dispose depth-buffer resources
                GraphicsHelper.SafeDispose(ref m_depthBufferView);
                GraphicsHelper.SafeDispose(ref m_depthBuffer);

                // Dispose object-id buffer
                GraphicsHelper.SafeDispose(ref m_objectIDBufferRenderTargetView);
                GraphicsHelper.SafeDispose(ref m_objectIDBuffer);

                // Dispose normal-depth resources
                GraphicsHelper.SafeDispose(ref m_normalDepthBuffer);
                GraphicsHelper.SafeDispose(ref m_normalDepthBufferRenderTargetView);
                GraphicsHelper.SafeDispose(ref m_normalDepthBufferShaderResourceView);
                if (m_shaderResourceCreated)
                {
                    GraphicsHelper.SafeDispose(ref m_normalDepthBufferShaderResource);
                }

                // Create color-buffer resources
                if (m_creationMode.HasFlag(RenderTargetCreationMode.Color))
                {
                    m_colorBuffer = GraphicsHelper.CreateRenderTargetTexture(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, renderState.ViewInformation.ViewConfiguration);
                    m_colorBufferShaderResource = m_colorBuffer;
                    if (renderState.ViewInformation.ViewConfiguration.AntialiasingEnabled)
                    {
                        m_colorBufferShaderResource = GraphicsHelper.CreateTexture(renderState.Device, currentViewSize.Width, currentViewSize.Height);
                        m_shaderResourceCreated     = true;
                    }
                    else
                    {
                        m_shaderResourceCreated = false;
                    }
                    m_colorBufferRenderTargetView   = new D3D11.RenderTargetView(renderState.Device.DeviceD3D11_1, m_colorBuffer);
                    m_colorBufferShaderResourceView = new D3D11.ShaderResourceView(renderState.Device.DeviceD3D11_1, m_colorBufferShaderResource);
                }

                // Create depth-buffer resources
                if (m_creationMode.HasFlag(RenderTargetCreationMode.Depth))
                {
                    m_depthBuffer = GraphicsHelper.CreateDepthBufferTexture(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, renderState.ViewInformation.ViewConfiguration);
                    m_depthBufferView = GraphicsHelper.CreateDepthBufferView(renderState.Device, m_depthBuffer);
                }

                // Create object-id resources
                if (m_creationMode.HasFlag(RenderTargetCreationMode.ObjectID))
                {
                    m_objectIDBuffer = GraphicsHelper.CreateRenderTargetTextureObjectIDs(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, renderState.ViewInformation.ViewConfiguration);
                    m_objectIDBufferRenderTargetView = new D3D11.RenderTargetView(renderState.Device.DeviceD3D11_1, m_objectIDBuffer);
                }

                // Create normal-depth buffer resources
                if (m_creationMode.HasFlag(RenderTargetCreationMode.NormalDepth))
                {
                    m_normalDepthBuffer = GraphicsHelper.CreateRenderTargetTextureNormalDepth(
                        renderState.Device, currentViewSize.Width, currentViewSize.Height, renderState.ViewInformation.ViewConfiguration);
                    m_normalDepthBufferShaderResource = m_normalDepthBuffer;
                    if (m_shaderResourceCreated)
                    {
                        m_normalDepthBufferShaderResource = GraphicsHelper.CreateTexture(
                            renderState.Device, currentViewSize.Width, currentViewSize.Height, GraphicsHelper.DEFAULT_TEXTURE_FORMAT_NORMAL_DEPTH);
                    }
                    m_normalDepthBufferRenderTargetView   = new D3D11.RenderTargetView(renderState.Device.DeviceD3D11_1, m_normalDepthBuffer);
                    m_normalDepthBufferShaderResourceView = new D3D11.ShaderResourceView(renderState.Device.DeviceD3D11_1, m_normalDepthBufferShaderResource);
                }

                // Remember values
                m_width  = currentViewSize.Width;
                m_heigth = currentViewSize.Height;
                m_antialiasingEnabled = currentAntialiasingEnabled;
                m_antialiasingQuality = currentAntialiasingQuality;
                m_viewportF           = renderState.Viewport;
            }
        }
Ejemplo n.º 7
0
 public void EditViewConfiguration(RenderLoop renderLoop, GraphicsViewConfiguration viewConfig)
 {
     _manipulateViewConfig?.Invoke(renderLoop, viewConfig);
 }