DrawBatch() public method

Sorts the batch items and then groups batch drawing into maximal allowed batch sets that do not overflow the 16 bit array indices for vertices.
public DrawBatch ( SpriteSortMode sortMode, Effect effect ) : void
sortMode SpriteSortMode The type of depth sorting desired for the rendering.
effect Effect The custom effect to apply to the drawn geometry
return void
Example #1
0
        public void End()
        {
            _beginCalled = false;

            if (_sortMode != SpriteSortMode.Immediate)
            {
                Setup();
            }

            _batcher.DrawBatch(_sortMode);
        }
Example #2
0
        public void End()
        {
            _beginCalled = false;

            if (_sortMode != SpriteSortMode.Immediate)
            {
                Setup();
            }
#if PSM
            GraphicsDevice.BlendState = _blendState;
            _blendState.PlatformApplyState(GraphicsDevice);
#endif

            _batcher.DrawBatch(_sortMode, _effect);
        }
Example #3
0
        /// <summary>
        /// Flushes all batched text and sprites to the screen.
        /// </summary>
        /// <remarks>This command should be called after <see cref="Begin"/> and drawing commands.</remarks>
        public void End()
        {
            if (!_beginCalled)
            {
                throw new InvalidOperationException("Begin must be called before calling End.");
            }

            _beginCalled = false;

            if (_sortMode != SpriteSortMode.Immediate)
            {
                Setup();
            }

            _batcher.DrawBatch(_sortMode, _effect);
        }
Example #4
0
        public void End()
        {
            // apply the custom effect if there is one
            if (_effect != null)
            {
                _effect.Apply();

                if (graphicsDevice.Textures._textures.Count > 0)
                {
                    foreach (EffectParameter ep in _effect._textureMappings)
                    {
                        // if user didn't inform the texture index, we can't bind it
                        if (ep.UserInedx == -1)
                        {
                            continue;
                        }

                        Texture tex = graphicsDevice.Textures[ep.UserInedx];

                        // Need to support multiple passes as well
                        GL.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + ep.UserInedx));
                        GL.BindTexture(TextureTarget.Texture2D, tex._textureId);
                        GL.Uniform1(ep.UniformLocation, ep.UserInedx);
                    }
                }
            }

            // Disable Blending by default = BlendState.Opaque
            GL.Disable(EnableCap.Blend);

            // set the blend mode
            if (_blendState == BlendState.NonPremultiplied)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.AlphaBlend)
            {
                GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.Additive)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                GL.Enable(EnableCap.Blend);
            }

            // set camera
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            Viewport vp = this.graphicsDevice.Viewport;

            // Switch on the flags.
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                break;
            }

            case DisplayOrientation.PortraitUpsideDown:
            {
                GL.Rotate(180, 0, 0, 1);
                break;
            }

            default:
            {
                break;
            }
            }

            GL.Ortho(0, vp.Width, vp.Height, 0, -1, 1);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Enable(EnableCap.ScissorTest);
            }

            GL.MatrixMode(MatrixMode.Modelview);

            GL.Viewport(0, 0, vp.Width, vp.Height);
            //GL.Viewport (vp.X, vp.Y, vp.Width, vp.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GL.LoadMatrix(ref _matrix.M11);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(EnableCap.DepthTest);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(EnableCap.Texture2D);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);

            _batcher.DrawBatch(_sortMode, _samplerState);

            // Disable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Disable(EnableCap.ScissorTest);
            }

            // clear out the textures
            graphicsDevice.Textures._textures.Clear();

            // unbinds shader
            if (_effect != null)
            {
                GL.UseProgram(0);
                _effect = null;
            }
        }
Example #5
0
        public void End()
        {
            // Disable Blending by default = BlendState.Opaque
            GL.Disable(All.Blend);

            // set the blend mode
            if (_blendState == BlendState.NonPremultiplied)
            {
                GL.BlendFunc(All.SrcAlpha, All.OneMinusSrcAlpha);
                GL.Enable(All.Blend);
            }

            if (_blendState == BlendState.AlphaBlend)
            {
                GL.BlendFunc(All.One, All.OneMinusSrcAlpha);
                GL.Enable(All.Blend);
            }

            if (_blendState == BlendState.Additive)
            {
                GL.BlendFunc(All.SrcAlpha, All.One);
                GL.Enable(All.Blend);
            }

            // set camera
            GL.MatrixMode(All.Projection);
            GL.LoadIdentity();

            // Switch on the flags.
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.PortraitUpsideDown:
            {
                GL.Rotate(180, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }

            default:
            {
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }
            }

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Enable(All.ScissorTest);
            }


            GL.MatrixMode(All.Modelview);

            GL.Viewport(0, 0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GL.LoadMatrix(ref _matrix.M11);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(All.DepthTest);
            GL.TexEnv(All.TextureEnv, All.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(All.Texture2D);
            GL.EnableClientState(All.VertexArray);
            GL.EnableClientState(All.ColorArray);
            GL.EnableClientState(All.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(All.CullFace);
            GL.FrontFace(All.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);

            _batcher.DrawBatch(_sortMode);

            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Disable(All.ScissorTest);
            }
        }
Example #6
0
        public void End()
        {
            // apply the custom effect if there is one
            if (_effect != null)
            {
                if (graphicsDevice.Textures._textures.Count > 0)
                {
                    foreach (var texture in graphicsDevice.Textures._textures)
                    {
                        int index = texture.Key;
                        if (index > 0)
                        {  // zero is the active texture
                            if (index < _effect._textureMappings.Count)
                            {
                                var tex       = _effect._textureMappings[index];
                                int texOffset = (int)TextureUnit.Texture0;
                                var tex2      = texture.Value;
                                if (tex != null)
                                {
                                    // Need to support multiple passes as well
                                    GL.UseProgram(_effect.CurrentTechnique.Passes[0].shaderProgram);
                                    GL.ActiveTexture((TextureUnit)texOffset + index);
                                    GL.BindTexture(TextureTarget.Texture2D, tex2._textureId);
                                    GL.Uniform1(tex.internalIndex, tex2._textureId);
                                    GL.UseProgram(0);
                                }
                            }
                        }
                    }
                }
                _effect.Apply();
            }

            // Disable Blending by default = BlendState.Opaque
            GL.Disable(EnableCap.Blend);

            // set the blend mode
            if (_blendState == BlendState.NonPremultiplied)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.AlphaBlend)
            {
                GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                GL.Enable(EnableCap.Blend);
            }

            if (_blendState == BlendState.Additive)
            {
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                GL.Enable(EnableCap.Blend);
            }

            // set camera
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            // Switch on the flags.
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Height, this.graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.PortraitUpsideDown:
            {
                GL.Rotate(180, 0, 0, 1);
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }

            default:
            {
                GL.Ortho(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }
            }

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Enable(EnableCap.ScissorTest);
            }

            GL.MatrixMode(MatrixMode.Modelview);

            GL.Viewport(0, 0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GL.LoadMatrix(ref _matrix.M11);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(EnableCap.DepthTest);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(EnableCap.Texture2D);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);
            //GL.Color4(1.0f, 1.0f, 1.0f, 0.5f);

            _batcher.DrawBatch(_sortMode, _samplerState);
            //GL.UseProgram(0);
            // clear out the textures
            graphicsDevice.Textures._textures.Clear();

            spriteEffect.CurrentTechnique.Passes[0].Apply();

            if (this.graphicsDevice.RasterizerState.ScissorTestEnable)
            {
                GL.Disable(EnableCap.ScissorTest);
            }
        }
Example #7
0
        public void End()
        {
            // set the blend mode
            switch (_blendMode)
            {
            case SpriteBlendMode.PreMultiplied:
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.One, BlendingFactorDest.OneMinusSrcAlpha);
                break;

            case SpriteBlendMode.AlphaBlend:
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
                break;

            case SpriteBlendMode.Additive:
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One);
                break;

            case SpriteBlendMode.None:
                GL.Disable(EnableCap.Blend);
                break;
            }

            // set camera
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            // Switch on the flags.
            switch (_graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            {
                GL.Rotate(-90, 0, 0, 1);
                GL.Ortho(0, _graphicsDevice.Viewport.Height, _graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            case DisplayOrientation.LandscapeRight:
            {
                GL.Rotate(90, 0, 0, 1);
                GL.Ortho(0, _graphicsDevice.Viewport.Height, _graphicsDevice.Viewport.Width, 0, -1, 1);
                break;
            }

            default:
            {
                GL.Ortho(0, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height, 0, -1, 1);
                break;
            }
            }

            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref _matrix.M11);
            GL.Viewport(0, 0, _graphicsDevice.Viewport.Width, _graphicsDevice.Viewport.Height);

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GL.Disable(EnableCap.DepthTest);
            GL.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)All.BlendSrc);
            GL.Enable(EnableCap.Texture2D);
            GL.EnableClientState(ArrayCap.VertexArray);
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.TextureCoordArray);

            // Enable Culling for better performance
            GL.Enable(EnableCap.CullFace);
            GL.FrontFace(FrontFaceDirection.Cw);
            GL.Color4(1.0f, 1.0f, 1.0f, 1.0f);

            _batcher.DrawBatch(_sortMode);
        }
Example #8
0
        public void End()
        {
            // set up camera
            switch (this.graphicsDevice.PresentationParameters.DisplayOrientation)
            {
            case DisplayOrientation.LandscapeLeft:
            case DisplayOrientation.LandscapeRight:
            case DisplayOrientation.PortraitUpsideDown:
                throw new NotImplementedException();

            default:
                if (this.graphicsDevice.RenderTarget != null)
                {
                    Matrix4.CreateOrthographicOffCenter(0, this.graphicsDevice.RenderTarget.Width, this.graphicsDevice.RenderTarget.Height, 0, -1, 1, out GLStateManager.Projection);
                    break;
                }
                else
                {
                    Matrix4.CreateOrthographicOffCenter(0, this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, -1, 1, out GLStateManager.Projection);
                    break;
                }
            }

            GL.Viewport(this.graphicsDevice.Viewport.X, this.graphicsDevice.Viewport.Y,
                        this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height);

            // Enable Scissor Tests if necessary
            if (this.graphicsDevice.RenderState.ScissorTestEnable)
            {
                GL.Enable(EnableCap.ScissorTest);
                GL.Scissor(this.graphicsDevice.ScissorRectangle.X, this.graphicsDevice.ScissorRectangle.Y, this.graphicsDevice.ScissorRectangle.Width, this.graphicsDevice.ScissorRectangle.Height);
            }

            GLStateManager.ModelView = _matrix.ToMatrix4();

            // Initialize OpenGL states (ideally move this to initialize somewhere else)
            GLStateManager.DepthTest(false);
            GLStateManager.Textures2D(true);

            // Enable Culling for better performance
            GLStateManager.Cull(FrontFaceDirection.Cw);

            switch (_sortMode)
            {
            case SpriteSortMode.Immediate:
                break;

            default:
                this.graphicsDevice.RenderState.SourceBlend      = _blendState.ColorSourceBlend;
                this.graphicsDevice.RenderState.DestinationBlend = _blendState.ColorDestinationBlend;
                break;
            }

            GLStateManager.BlendFunc((BlendingFactorSrc)this.graphicsDevice.RenderState.SourceBlend, (BlendingFactorDest)this.graphicsDevice.RenderState.DestinationBlend);

            _batcher.DrawBatch(_sortMode);

            // GG EDIT always disable scissor test after drawing a batch
            if (this.graphicsDevice.RenderState.ScissorTestEnable)
            {
                GL.Disable(EnableCap.ScissorTest);
            }
        }