コード例 #1
0
ファイル: GraphicsDevice.cs プロジェクト: zwcloud/FNA
        public void SetRenderTargets(params RenderTargetBinding[] renderTargets)
        {
            // Checking for redundant SetRenderTargets...
            if (renderTargets == null && RenderTargetCount == 0)
            {
                return;
            }
            else if (renderTargets != null && renderTargets.Length == RenderTargetCount)
            {
                bool isRedundant = true;
                for (int i = 0; i < renderTargets.Length; i += 1)
                {
                    if (renderTargets[i].RenderTarget != renderTargetBindings[i].RenderTarget ||
                        renderTargets[i].CubeMapFace != renderTargetBindings[i].CubeMapFace)
                    {
                        isRedundant = false;
                    }
                }
                if (isRedundant)
                {
                    return;
                }
            }

            if (renderTargets == null || renderTargets.Length == 0)
            {
                GLDevice.SetRenderTargets(null, null, 0, DepthFormat.None);

                // Set the viewport to the size of the backbuffer.
                Viewport = new Viewport(0, 0, PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight);

                // Set the scissor rectangle to the size of the backbuffer.
                ScissorRectangle = new Rectangle(0, 0, PresentationParameters.BackBufferWidth, PresentationParameters.BackBufferHeight);

                if (PresentationParameters.RenderTargetUsage == RenderTargetUsage.DiscardContents)
                {
                    Clear(DiscardColor);
                }

                // Generate mipmaps for previous targets, if needed
                for (int i = 0; i < RenderTargetCount; i += 1)
                {
                    if (renderTargetBindings[i].RenderTarget.LevelCount > 1)
                    {
                        GLDevice.GenerateTargetMipmaps(
                            renderTargetBindings[i].RenderTarget.texture
                            );
                    }
                }
                Array.Clear(renderTargetBindings, 0, renderTargetBindings.Length);
                RenderTargetCount = 0;
            }
            else
            {
                uint[] glTarget = new uint[renderTargets.Length];
                OpenGLDevice.GLenum[] glTargetFace = new OpenGLDevice.GLenum[renderTargets.Length];
                for (int i = 0; i < renderTargets.Length; i += 1)
                {
                    glTarget[i] = renderTargets[i].RenderTarget.texture.Handle;
                    if (renderTargets[i].RenderTarget is RenderTarget2D)
                    {
                        glTargetFace[i] = OpenGLDevice.GLenum.GL_TEXTURE_2D;
                    }
                    else
                    {
                        glTargetFace[i] = OpenGLDevice.GLenum.GL_TEXTURE_CUBE_MAP_POSITIVE_X + (int)renderTargets[i].CubeMapFace;
                    }
                }
                IRenderTarget target = renderTargets[0].RenderTarget as IRenderTarget;
                GLDevice.SetRenderTargets(
                    glTarget,
                    glTargetFace,
                    target.DepthStencilBuffer,
                    target.DepthStencilFormat
                    );

                // Generate mipmaps for previous targets, if needed
                for (int i = 0; i < RenderTargetCount; i += 1)
                {
                    if (renderTargetBindings[i].RenderTarget.LevelCount > 1)
                    {
                        // We only need to gen mipmaps if the target is no longer bound.
                        bool stillBound = false;
                        for (int j = 0; j < renderTargets.Length; j += 1)
                        {
                            if (renderTargetBindings[i].RenderTarget == renderTargets[j].RenderTarget)
                            {
                                stillBound = true;
                                break;
                            }
                        }
                        if (!stillBound)
                        {
                            GLDevice.GenerateTargetMipmaps(
                                renderTargetBindings[i].RenderTarget.texture
                                );
                        }
                    }
                }
                Array.Clear(renderTargetBindings, 0, renderTargetBindings.Length);
                Array.Copy(renderTargets, renderTargetBindings, renderTargets.Length);
                RenderTargetCount = renderTargets.Length;

                // Set the viewport to the size of the first render target.
                Viewport = new Viewport(0, 0, target.Width, target.Height);

                // Set the scissor rectangle to the size of the first render target.
                ScissorRectangle = new Rectangle(0, 0, target.Width, target.Height);

                if (target.RenderTargetUsage == RenderTargetUsage.DiscardContents)
                {
                    Clear(DiscardColor);
                }
            }
        }