private void Initialize(BlendStateDescription?blendStateDescription = null)
        {
            commandList = factory.CreateCommandList();

            var indexBufferDescription = new BufferDescription(sizeof(ushort) * MAX_BATCH * 6, BufferUsage.IndexBuffer | BufferUsage.Dynamic);

            indexBuffer = factory.CreateBuffer(indexBufferDescription);

            var vertexBufferDescription = new BufferDescription(VERTEX_SIZE * MAX_BATCH * 4, BufferUsage.VertexBuffer | BufferUsage.Dynamic);

            vertexBuffer = factory.CreateBuffer(vertexBufferDescription);

            var worldMatrixBufferDescription = new BufferDescription(64, BufferUsage.UniformBuffer);

            worldMatrixBuffer = factory.CreateBuffer(worldMatrixBufferDescription);

            viewResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(new ResourceLayoutElementDescription("WorldView", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            graphicsResourceLayout = factory.CreateResourceLayout(new ResourceLayoutDescription(
                                                                      new ResourceLayoutElementDescription("Texture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                                                                      new ResourceLayoutElementDescription("TextureSampler", ResourceKind.Sampler, ShaderStages.Fragment)));

            var rasterizerStateDescription = new RasterizerStateDescription(FaceCullMode.None, PolygonFillMode.Solid, FrontFace.Clockwise, true, true);

            var pipelineDescription = new GraphicsPipelineDescription(
                blendStateDescription ?? BlendStateDescription.SingleAlphaBlend,
                DepthStencilStateDescription.DepthOnlyGreaterEqual,
                rasterizerStateDescription,
                PrimitiveTopology.TriangleList,
                shaderSet,
                new[] { viewResourceLayout, graphicsResourceLayout },
                framebuffer.OutputDescription);

            pipeline = factory.CreateGraphicsPipeline(pipelineDescription);
        }
        public SpriteRenderer(IGraphicsContext context, Framebuffer framebuffer, BlendStateDescription?blendStateDescription = null)
        {
            this.device      = context.Device;
            this.factory     = context.Factory;
            this.framebuffer = framebuffer;
            var shaders = factory.CreateFromSpirv(Shaders.SpritebatchDefaultVertexShader, Shaders.SpritebatchDefaultFragmentShader);

            this.shaderSet = new ShaderSetDescription(new[] { VertexPosition3ColorTexture.VertexLayout }, shaders);
            Initialize(blendStateDescription ?? BlendStateDescription.SingleAlphaBlend);
        }
예제 #3
0
        /// <summary>
        /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects and a custom effect.
        /// Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, depthStencilState.None, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp).
        /// Passing a null effect selects the default effect shader.
        /// </summary>
        /// <param name="graphicsContext">The graphics context to use.</param>
        /// <param name="effect">The effect to use for this begin/end draw session (default effect if null)</param>
        /// <param name="sessionSortMode">Sprite drawing order used for the Begin/End session.</param>
        /// <param name="sessionBlendState">Blending state used for the Begin/End session</param>
        /// <param name="sessionSamplerState">Texture sampling used for the Begin/End session</param>
        /// <param name="sessionDepthStencilState">Depth and stencil state used for the Begin/End session</param>
        /// <param name="sessionRasterizerState">Rasterization state used for the Begin/End session</param>
        /// <param name="stencilValue">The value of the stencil buffer to take as reference for the Begin/End session</param>
        /// <exception cref="System.InvalidOperationException">Only one SpriteBatch at a time can use SpriteSortMode.Immediate</exception>
        protected void Begin(GraphicsContext graphicsContext, EffectInstance effect, SpriteSortMode sessionSortMode, BlendStateDescription?sessionBlendState, SamplerState sessionSamplerState, DepthStencilStateDescription?sessionDepthStencilState, RasterizerStateDescription?sessionRasterizerState, int stencilValue)
        {
            CheckEndHasBeenCalled("begin");

            GraphicsContext = graphicsContext;

            sortMode              = sessionSortMode;
            blendState            = sessionBlendState;
            samplerState          = sessionSamplerState;
            depthStencilState     = sessionDepthStencilState;
            rasterizerState       = sessionRasterizerState;
            stencilReferenceValue = stencilValue;

            Effect = effect ?? (graphicsDevice.ColorSpace == ColorSpace.Linear ? DefaultEffectSRgb : DefaultEffect);

            // Force the effect to update
            Effect.UpdateEffect(graphicsDevice);

            textureUpdater = null;
            if (Effect.Effect.HasParameter(TexturingKeys.Texture0))
            {
                textureUpdater = Effect.Parameters.GetAccessor(TexturingKeys.Texture0);
            }
            if (Effect.Effect.HasParameter(TexturingKeys.TextureCube0))
            {
                textureUpdater = Effect.Parameters.GetAccessor(TexturingKeys.TextureCube0);
            }
            if (Effect.Effect.HasParameter(TexturingKeys.Texture3D0))
            {
                textureUpdater = Effect.Parameters.GetAccessor(TexturingKeys.Texture3D0);
            }

            samplerUpdater = null;
            if (Effect.Effect.HasParameter(TexturingKeys.Sampler))
            {
                samplerUpdater = Effect.Parameters.GetAccessor(TexturingKeys.Sampler);
            }

            // Immediate mode, then prepare for rendering here instead of End()
            if (sessionSortMode == SpriteSortMode.Immediate)
            {
                if (ResourceContext.IsInImmediateMode)
                {
                    throw new InvalidOperationException("Only one SpriteBatch at a time can use SpriteSortMode.Immediate");
                }

                PrepareForRendering();

                ResourceContext.IsInImmediateMode = true;
            }

            // Sets to true isBeginCalled
            isBeginCalled = true;
        }
예제 #4
0
파일: UIBatch.cs 프로젝트: yyzreal/xenko
        /// <summary>
        /// Begins a image batch rendering using the specified blend state, sampler, depth stencil, rasterizer state objects, and the view-projection transformation matrix.
        /// Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.None, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp).
        /// </summary>
        /// <param name="graphicsContext">The graphics context to use.</param>
        /// <param name="blendState">Blending options.</param>
        /// <param name="samplerState">Texture sampling options.</param>
        /// <param name="depthStencilState">Depth and stencil options.</param>
        /// <param name="rasterizerState">Rasterization options.</param>
        /// <param name="viewProjection">The view projection matrix used for this series of draw calls</param>
        /// <param name="stencilValue">The value of the stencil buffer to take as reference</param>
        public void Begin(GraphicsContext graphicsContext, ref Matrix viewProjection, BlendStateDescription?blendState, SamplerState samplerState, RasterizerStateDescription?rasterizerState, DepthStencilStateDescription?depthStencilState, int stencilValue)
        {
            viewProjectionMatrix = viewProjection;

            currentBlendState        = blendState;
            currentSamplerState      = samplerState;
            currentRasterizerState   = rasterizerState;
            currentDepthStencilState = depthStencilState;
            currentStencilValue      = stencilValue;

            Begin(graphicsContext, null, SpriteSortMode.BackToFront, blendState, samplerState, depthStencilState, rasterizerState, stencilValue);
        }
예제 #5
0
 /// <summary>
 /// Sets the state.
 /// </summary>
 /// <param name="blendStateDesc">The blend state desc.</param>
 public void SetState(BlendStateDescription?blendStateDesc)
 {
     if (IsNULL)
     {
         return;
     }
     if (BlendState != BlendStateProxy.Empty)
     {
         RemoveAndDispose(ref blendState);
     }
     blendState = blendStateDesc != null?
                  effectsManager.StateManager.Register(blendStateDesc.Value) : BlendStateProxy.Empty;
 }
예제 #6
0
        public void Begin(GraphicsContext graphicsContext, Matrix world, Matrix viewProjection, Color4 color, BlendStateDescription?blendState = null, SamplerState samplerState = null, DepthStencilStateDescription?depthStencilState = null, RasterizerStateDescription?rasterizerState = null)
        {
            CheckEndHasBeenCalled();

            GraphicsContext      = graphicsContext;
            ViewProjectionMatrix = viewProjection;

            BlendState        = blendState;
            SamplerState      = samplerState;
            DepthStencilState = depthStencilState;
            RasterizerState   = rasterizerState;


            textureUpdater = null;
            if (Effect.Effect.HasParameter(TexturingKeys.Texture0))
            {
                textureUpdater = Effect.Parameters.GetAccessor(TexturingKeys.Texture0);
            }

            samplerUpdater = null;
            if (Effect.Effect.HasParameter(TexturingKeys.Sampler))
            {
                samplerUpdater = Effect.Parameters.GetAccessor(TexturingKeys.Sampler);
            }

            PrepareForRendering();

            MutablePipeline.State.Output.CaptureState(GraphicsContext.CommandList);
            MutablePipeline.Update();

            GraphicsContext.CommandList.SetPipelineState(MutablePipeline.CurrentState);


            var wvp = world * ViewProjectionMatrix;

            Parameters.Set(TileMeshBaseKeys.MatrixTransform, wvp);
            Parameters.Set(TileMeshBaseKeys.Color, color);
            hasBegun = true;
        }
예제 #7
0
        public override void Draw(RenderDrawContext context, RenderView renderView, RenderViewStage renderViewStage, int startIndex, int endIndex)
        {
            base.Draw(context, renderView, renderViewStage, startIndex, endIndex);

            Matrix viewInverse;

            Matrix.Invert(ref renderView.View, out viewInverse);

            BlendStateDescription?       previousBlendState        = null;
            DepthStencilStateDescription?previousDepthStencilState = null;
            EffectInstance previousEffect = null;

            //TODO string comparison ...?
            var isPicking = renderViewStage.RenderStage.Name == "Picking";

            bool hasBegin = false;

            for (var index = startIndex; index < endIndex; index++)
            {
                var renderNodeReference = renderViewStage.SortedRenderNodes[index].RenderNode;
                var renderNode          = GetRenderNode(renderNodeReference);

                var renderSprite = (RenderSprite)renderNode.RenderObject;

                var spriteComp        = renderSprite.SpriteComponent;
                var transfoComp       = renderSprite.TransformComponent;
                var depthStencilState = renderSprite.SpriteComponent.IgnoreDepth ? DepthStencilStates.None : DepthStencilStates.Default;

                var sprite = spriteComp.CurrentSprite;
                if (sprite == null)
                {
                    continue;
                }

                // TODO: this should probably be moved to Prepare()
                // Project the position
                // TODO: This could be done in a SIMD batch, but we need to figure-out how to plugin in with RenderMesh object
                var worldPosition = new Vector4(renderSprite.TransformComponent.WorldMatrix.TranslationVector, 1.0f);

                Vector4 projectedPosition;
                Vector4.Transform(ref worldPosition, ref renderView.ViewProjection, out projectedPosition);
                var projectedZ = projectedPosition.Z / projectedPosition.W;

                // Update the sprite batch
                var blendState    = isPicking ? BlendStates.Default : sprite.IsTransparent ? (spriteComp.PremultipliedAlpha ? BlendStates.AlphaBlend : BlendStates.NonPremultiplied) : BlendStates.Opaque;
                var currentEffect = isPicking ? GetOrCreatePickingSpriteEffect() : null; // TODO remove this code when material are available
                if (previousEffect != currentEffect || blendState != previousBlendState || depthStencilState != previousDepthStencilState)
                {
                    if (hasBegin)
                    {
                        sprite3DBatch.End();
                    }
                    sprite3DBatch.Begin(context.GraphicsContext, renderView.ViewProjection, SpriteSortMode.Deferred, blendState, null, depthStencilState, RasterizerStates.CullNone, currentEffect);
                    hasBegin = true;
                }
                previousEffect            = currentEffect;
                previousBlendState        = blendState;
                previousDepthStencilState = depthStencilState;

                var sourceRegion = sprite.Region;
                var texture      = sprite.Texture;
                var color        = spriteComp.Color;
                if (isPicking) // TODO move this code corresponding to picking out of the runtime code.
                {
                    var compId = RuntimeIdHelper.ToRuntimeId(spriteComp);
                    color = new Color4(compId);
                }

                // skip the sprite if no texture is set.
                if (texture == null)
                {
                    continue;
                }

                // determine the element world matrix depending on the type of sprite
                var worldMatrix = transfoComp.WorldMatrix;
                if (spriteComp.SpriteType == SpriteType.Billboard)
                {
                    worldMatrix = viewInverse;

                    // remove scale of the camera
                    worldMatrix.Row1 /= ((Vector3)viewInverse.Row1).Length();
                    worldMatrix.Row2 /= ((Vector3)viewInverse.Row2).Length();

                    // set the scale of the object
                    worldMatrix.Row1 *= ((Vector3)transfoComp.WorldMatrix.Row1).Length();
                    worldMatrix.Row2 *= ((Vector3)transfoComp.WorldMatrix.Row2).Length();

                    // set the position
                    worldMatrix.TranslationVector = transfoComp.WorldMatrix.TranslationVector;
                }

                // calculate normalized position of the center of the sprite (takes into account the possible rotation of the image)
                var normalizedCenter = new Vector2(sprite.Center.X / sourceRegion.Width - 0.5f, 0.5f - sprite.Center.Y / sourceRegion.Height);
                if (sprite.Orientation == ImageOrientation.Rotated90)
                {
                    var oldCenterX = normalizedCenter.X;
                    normalizedCenter.X = -normalizedCenter.Y;
                    normalizedCenter.Y = oldCenterX;
                }
                // apply the offset due to the center of the sprite
                var centerOffset = Vector2.Modulate(normalizedCenter, sprite.SizeInternal);
                worldMatrix.M41 -= centerOffset.X * worldMatrix.M11 + centerOffset.Y * worldMatrix.M21;
                worldMatrix.M42 -= centerOffset.X * worldMatrix.M12 + centerOffset.Y * worldMatrix.M22;
                worldMatrix.M43 -= centerOffset.X * worldMatrix.M13 + centerOffset.Y * worldMatrix.M23;

                // draw the sprite
                sprite3DBatch.Draw(texture, ref worldMatrix, ref sourceRegion, ref sprite.SizeInternal, ref color, sprite.Orientation, SwizzleMode.None, projectedZ);
            }

            if (hasBegin)
            {
                sprite3DBatch.End();
            }
        }
예제 #8
0
 /// <summary>
 /// Begins a sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects, plus a custom effect and a 2D transformation matrix. Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.Default, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp). Passing a null effect selects the default SpriteBatch Class shader.
 /// </summary>
 /// <param name="graphicsContext">The graphics context to use.</param>
 /// <param name="viewMatrix">The view matrix to use for the batch session</param>
 /// <param name="sortMode">The sprite drawing order to use for the batch session</param>
 /// <param name="blendState">The blending state to use for the batch session</param>
 /// <param name="samplerState">The sampling state to use for the batch session</param>
 /// <param name="depthStencilState">The depth stencil state to use for the batch session</param>
 /// <param name="rasterizerState">The rasterizer state to use for the batch session</param>
 /// <param name="effect">The effect to use for the batch session</param>
 /// <param name="stencilValue">The value of the stencil buffer to take as reference for the batch session</param>
 public void Begin(GraphicsContext graphicsContext, Matrix viewMatrix, SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendStateDescription?blendState = null, SamplerState samplerState = null, DepthStencilStateDescription?depthStencilState = null, RasterizerStateDescription?rasterizerState = null, EffectInstance effect = null, int stencilValue = 0)
 {
     UpdateDefaultProjectionMatrix(graphicsContext.CommandList);
     Begin(graphicsContext, viewMatrix, defaultProjectionMatrix, sortMode, blendState, samplerState, depthStencilState, rasterizerState, effect, stencilValue);
 }
예제 #9
0
 /// <summary>
 /// Draws a quad with a texture. This Draw method is using the current effect bound to this instance.
 /// </summary>
 /// <param name="texture">The texture.</param>
 /// <param name="applyEffectStates">The flag to apply effect states.</param>
 public void Draw(GraphicsContext graphicsContext, Texture texture, BlendStateDescription?blendState = null)
 {
     Draw(graphicsContext, texture, null, Color.White, blendState);
 }
예제 #10
0
        public override void Draw(RenderDrawContext context, RenderView renderView, RenderViewStage renderViewStage, int startIndex, int endIndex)
        {
            base.Draw(context, renderView, renderViewStage, startIndex, endIndex);

            BlendStateDescription?       previousBlendState        = null;
            DepthStencilStateDescription?previousDepthStencilState = null;
            EffectInstance previousEffect = null;

            //TODO string comparison ...?
            var isPicking = RenderSystem.RenderStages[renderViewStage.Index].Name == "Picking";

            var hasBegin = false;

            for (var index = startIndex; index < endIndex; index++)
            {
                var renderNodeReference = renderViewStage.SortedRenderNodes[index].RenderNode;
                var renderNode          = GetRenderNode(renderNodeReference);

                var spriteState = (RenderSpriteStudio)renderNode.RenderObject;

                var depthStencilState = DepthStencilStates.DepthRead;

                foreach (var node in spriteState.SortedNodes)
                {
                    if (node.Sprite?.Texture == null || node.Sprite.Region.Width <= 0 || node.Sprite.Region.Height <= 0f || node.Hide != 0)
                    {
                        continue;
                    }

                    // Update the sprite batch

                    BlendStateDescription spriteBlending;
                    switch (node.BaseNode.AlphaBlending)
                    {
                    case SpriteStudioBlending.Mix:
                        spriteBlending = BlendStates.AlphaBlend;
                        break;

                    case SpriteStudioBlending.Multiplication:
                        spriteBlending = MultBlendState;
                        break;

                    case SpriteStudioBlending.Addition:
                        spriteBlending = BlendStates.Additive;
                        break;

                    case SpriteStudioBlending.Subtraction:
                        spriteBlending = SubBlendState;
                        break;

                    default:
                        throw new ArgumentOutOfRangeException();
                    }

                    // TODO: this should probably be moved to Prepare()
                    // Project the position
                    // TODO: This could be done in a SIMD batch, but we need to figure-out how to plugin in with RenderMesh object
                    var worldPosition = new Vector4(spriteState.WorldMatrix.TranslationVector, 1.0f);

                    Vector4 projectedPosition;
                    Vector4.Transform(ref worldPosition, ref renderView.ViewProjection, out projectedPosition);
                    var projectedZ = projectedPosition.Z / projectedPosition.W;

                    var blendState = isPicking ? BlendStates.Default : spriteBlending;
                    // TODO: the current impementation to determine if the sprite is selected does not work. This should be fixed later at some point
                    //var currentEffect = isPicking ? GetOrCreatePickingSpriteEffect() : ShadowObject.IsObjectSelected(spriteState.SpriteStudioComponent) ? GetOrCreateSelectedSpriteEffect() : null;
                    var currentEffect = isPicking ? GetOrCreatePickingSpriteEffect() : null;
                    // TODO remove this code when material are available
                    if (previousEffect != currentEffect || blendState != previousBlendState || depthStencilState != previousDepthStencilState)
                    {
                        if (hasBegin)
                        {
                            sprite3DBatch.End();
                        }
                        sprite3DBatch.Begin(context.GraphicsContext, renderView.ViewProjection, SpriteSortMode.Deferred, blendState, null, depthStencilState, RasterizerStates.CullNone, currentEffect);
                        hasBegin = true;
                    }

                    previousEffect            = currentEffect;
                    previousBlendState        = blendState;
                    previousDepthStencilState = depthStencilState;

                    var sourceRegion = node.Sprite.Region;
                    var texture      = node.Sprite.Texture;

                    // skip the sprite if no texture is set.
                    if (texture == null)
                    {
                        continue;
                    }

                    var color4 = Color4.White;
                    if (isPicking)
                    {
                        // TODO move this code corresponding to picking out of the runtime code.
                        color4 = new Color4(RuntimeIdHelper.ToRuntimeId(spriteState.Source));
                    }
                    else
                    {
                        if (node.BlendFactor > 0.0f)
                        {
                            switch (node.BlendType) //todo this should be done in a shader
                            {
                            case SpriteStudioBlending.Mix:
                                color4 = Color4.Lerp(color4, node.BlendColor, node.BlendFactor) * node.FinalTransparency;
                                break;

                            case SpriteStudioBlending.Multiplication:
                                color4 = Color4.Lerp(color4, node.BlendColor, node.BlendFactor) * node.FinalTransparency;
                                break;

                            case SpriteStudioBlending.Addition:
                                color4 = Color4.Lerp(color4, node.BlendColor, node.BlendFactor) * node.FinalTransparency;
                                break;

                            case SpriteStudioBlending.Subtraction:
                                color4 = Color4.Lerp(color4, node.BlendColor, node.BlendFactor) * node.FinalTransparency;
                                break;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                        else
                        {
                            color4 *= node.FinalTransparency;
                        }
                    }

                    Matrix.Multiply(ref node.ModelTransform, ref spriteState.WorldMatrix, out var worldMatrix);

                    // calculate normalized position of the center of the sprite (takes into account the possible rotation of the image)
                    var normalizedCenter = new Vector2(node.Sprite.Center.X / sourceRegion.Width - 0.5f, 0.5f - node.Sprite.Center.Y / sourceRegion.Height);
                    if (node.Sprite.Orientation == ImageOrientation.Rotated90)
                    {
                        var oldCenterX = normalizedCenter.X;
                        normalizedCenter.X = -normalizedCenter.Y;
                        normalizedCenter.Y = oldCenterX;
                    }
                    // apply the offset due to the center of the sprite
                    var size         = node.Sprite.Size;
                    var centerOffset = Vector2.Modulate(normalizedCenter, size);
                    worldMatrix.M41 -= centerOffset.X * worldMatrix.M11 + centerOffset.Y * worldMatrix.M21;
                    worldMatrix.M42 -= centerOffset.X * worldMatrix.M12 + centerOffset.Y * worldMatrix.M22;

                    // draw the sprite
                    sprite3DBatch.Draw(texture, ref worldMatrix, ref sourceRegion, ref size, ref color4, node.Sprite.Orientation, SwizzleMode.None, projectedZ);
                }
            }

            if (hasBegin)
            {
                sprite3DBatch.End();
            }
        }
예제 #11
0
 /// <summary>
 /// Draws a fullscreen texture using a <see cref="SamplerStateFactory.LinearClamp"/> sampler
 /// and the texture color multiplied by a custom color. See <see cref="Draw+a+texture"/> to learn how to use it.
 /// </summary>
 /// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
 /// <param name="color">The color.</param>
 /// <param name="applyEffectStates">The flag to apply effect states.</param>
 public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, Color4 color, BlendStateDescription?blendState = null)
 {
     graphicsContext.DrawTexture(texture, null, color, blendState);
 }
예제 #12
0
 /// <summary>
 /// Draws a fullscreen texture using the specified sampler. See <see cref="Draw+a+texture"/> to learn how to use it.
 /// </summary>
 /// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
 /// <param name="sampler">The sampler.</param>
 /// <param name="applyEffectStates">The flag to apply effect states.</param>
 public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, SamplerState sampler, BlendStateDescription?blendState = null)
 {
     graphicsContext.DrawTexture(texture, sampler, Color4.White, blendState);
 }
예제 #13
0
        public override void Draw(RenderDrawContext context, RenderView renderView, RenderViewStage renderViewStage, int startIndex, int endIndex)
        {
            //base.Draw(context, renderView, renderViewStage, startIndex, endIndex);

            for (var index = startIndex; index < endIndex; index++)
            {
                var renderNodeReference = renderViewStage.SortedRenderNodes[index].RenderNode;
                var renderNode          = GetRenderNode(renderNodeReference);
                var renderTileMap       = (RenderTileMap)renderNode.RenderObject;

                var tileMapComp   = renderTileMap.TileMapComponent;
                var transformComp = renderTileMap.TransformComponent;
                var tileMesh      = renderTileMap.TileMesh;
                var grid          = tileMapComp.Grid;

                if (grid == null || tileMesh == null)
                {
                    continue;
                }

                var color          = tileMapComp.Color;
                var world          = renderTileMap.TransformComponent.WorldMatrix;
                var viewProjection = renderView.ViewProjection;
                var cellSize       = grid.CellSize;

                BlendStateDescription?blendState   = tileMapComp.PremultipliedAlpha ? BlendStates.AlphaBlend : BlendStates.NonPremultiplied;
                SamplerState          samplerState = context.GraphicsDevice.SamplerStates.PointClamp;
                if (tileMapComp.Sampler != TileMapComponent.TileMapSampler.PointClamp)
                {
                    switch (tileMapComp.Sampler)
                    {
                    case TileMapComponent.TileMapSampler.LinearClamp:
                        samplerState = context.GraphicsDevice.SamplerStates.LinearClamp;
                        break;

                    case TileMapComponent.TileMapSampler.AnisotropicClamp:
                        samplerState = context.GraphicsDevice.SamplerStates.AnisotropicClamp;
                        break;
                    }
                }

                DepthStencilStateDescription?depthStencilState = tileMapComp.IgnoreDepth ? DepthStencilStates.None : DepthStencilStates.Default;

                grid.FindVisibleGridBlocks(ref world, ref viewProjection, visibleBlocks);
                if (visibleBlocks.Count > 0)
                {
                    tileMeshRenderer.Begin(context.GraphicsContext, world, viewProjection, color, blendState, samplerState, depthStencilState, RasterizerStates.CullNone);

                    tileMesh.GetTileMeshDraws(visibleBlocks, context.GraphicsContext, ref cellSize, visibleTileMeshDraws);

                    for (int i = 0; i < visibleTileMeshDraws.Count; i++)
                    {
                        var tileMeshDraw = visibleTileMeshDraws[i];
                        tileMeshRenderer.Draw(tileMeshDraw);
                    }
                    tileMeshRenderer.End();

                    visibleBlocks.Clear();
                    visibleTileMeshDraws.Clear();
                }
            }
        }
 public SpriteRenderer(GraphicsDevice device, ResourceFactory factory, Framebuffer framebuffer, ShaderSetDescription shaderSet, BlendStateDescription?blendStateDescription = null)
 {
     this.device      = device;
     this.factory     = factory;
     this.framebuffer = framebuffer;
     this.shaderSet   = shaderSet;
     Initialize(blendStateDescription ?? BlendStateDescription.SingleAlphaBlend);
 }
예제 #15
0
파일: UIBatch.cs 프로젝트: yyzreal/xenko
 /// <summary>
 /// Begins a image batch rendering using the specified blend state, depth stencil and a view-projection transformation matrix.
 /// Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.None).
 /// </summary>
 /// <param name="graphicsContext">The graphics context to use.</param>
 /// <param name="blendState">Blending options.</param>
 /// <param name="depthStencilState">Depth and stencil options.</param>
 /// <param name="viewProjection">The view projection matrix used for this series of draw calls</param>
 /// <param name="stencilValue">The value of the stencil buffer to take as reference</param>
 /// <param name="overrideEffect">The number of the override effect to use, if any</param>
 public void Begin(GraphicsContext graphicsContext, ref Matrix viewProjection, BlendStateDescription?blendState, DepthStencilStateDescription?depthStencilState, int stencilValue)
 {
     Begin(graphicsContext, ref viewProjection, blendState, null, null, depthStencilState, stencilValue);
 }
예제 #16
0
 /// <summary>
 /// Draws a fullscreen texture using the specified sampler
 /// and the texture color multiplied by a custom color. See <see cref="Draw+a+texture"/> to learn how to use it.
 /// </summary>
 /// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param>
 /// <param name="sampler">The sampler.</param>
 /// <param name="color">The color.</param>
 /// <param name="applyEffectStates">The flag to apply effect states.</param>
 public static void DrawTexture(this GraphicsContext graphicsContext, Texture texture, SamplerState sampler, Color4 color, BlendStateDescription?blendState = null)
 {
     graphicsContext.CommandList.GraphicsDevice.PrimitiveQuad.Draw(graphicsContext, texture, sampler, color, blendState);
 }
예제 #17
0
        /// <summary>
        /// Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the texture.
        /// </summary>
        /// <param name="texture">The texture to draw.</param>
        /// <param name="samplerState">State of the sampler. If null, default sampler is <see cref="SamplerStateFactory.LinearClamp" />.</param>
        /// <param name="color">The color.</param>
        /// <param name="applyEffectStates">The flag to apply effect states.</param>
        /// <exception cref="System.ArgumentException">Expecting a Texture;texture</exception>
        public void Draw(GraphicsContext graphicsContext, Texture texture, SamplerState samplerState, Color4 color, BlendStateDescription?blendState = null)
        {
            pipelineState.State.RootSignature  = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode = simpleEffect.Effect.Bytecode;
            pipelineState.State.BlendState     = blendState ?? BlendStates.Default;
            pipelineState.State.Output.CaptureState(graphicsContext.CommandList);
            pipelineState.Update();
            graphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Make sure that we are using our vertex shader
            simpleEffect.Parameters.Set(SpriteEffectKeys.Color, color);
            simpleEffect.Parameters.Set(TexturingKeys.Texture0, texture);
            simpleEffect.Parameters.Set(TexturingKeys.Sampler, samplerState ?? GraphicsDevice.SamplerStates.LinearClamp);
            simpleEffect.Apply(graphicsContext);

            Draw(graphicsContext.CommandList);

            // TODO ADD QUICK UNBIND FOR SRV
            //GraphicsDevice.Context.PixelShader.SetShaderResource(0, null);
        }
예제 #18
0
        /// <summary>
        /// Begins a 3D sprite batch rendering using the specified sorting mode and blend state, sampler, depth stencil, rasterizer state objects, plus a custom effect and a view-projection matrix.
        /// Passing null for any of the state objects selects the default default state objects (BlendState.AlphaBlend, DepthStencilState.Default, RasterizerState.CullCounterClockwise, SamplerState.LinearClamp).
        /// Passing a null effect selects the default SpriteBatch Class shader.
        /// </summary>
        /// <param name="graphicsContext">The graphics context to use.</param>
        /// <param name="sortMode">The sprite drawing order to use for the batch session</param>
        /// <param name="effect">The effect to use for the batch session</param>
        /// <param name="blendState">The blending state to use for the batch session</param>
        /// <param name="samplerState">The sampling state to use for the batch session</param>
        /// <param name="depthStencilState">The depth stencil state to use for the batch session</param>
        /// <param name="rasterizerState">The rasterizer state to use for the batch session</param>
        /// <param name="stencilValue">The value of the stencil buffer to take as reference for the batch session</param>
        /// <param name="viewProjection">The view-projection matrix to use for the batch session</param>
        public void Begin(GraphicsContext graphicsContext, Matrix viewProjection, SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendStateDescription?blendState = null, SamplerState samplerState = null, DepthStencilStateDescription?depthStencilState = null, RasterizerStateDescription?rasterizerState = null, EffectInstance effect = null, int stencilValue = 0)
        {
            CheckEndHasBeenCalled("begin");

            transformationMatrix = viewProjection;

            base.Begin(graphicsContext, effect, sortMode, blendState, samplerState, depthStencilState, rasterizerState, stencilValue);
        }
 public SpriteRenderer(Window window, BlendStateDescription?bsd = null) : this(window, window.Framebuffer, bsd)
 {
 }