Example #1
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;
        }
Example #2
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;
        }
 /// <summary>
 /// Sets an object of the material pass parameter. Cloning the <see cref="Material"/> if required.
 /// </summary>
 /// <typeparam name="T">The type of value.</typeparam>
 /// <param name="modelComponent">The <see cref="ModelComponent"/> to update material parameter on.</param>
 /// <param name="parameterAccessor">The parameter to update.</param>
 /// <param name="value">The value.</param>
 /// <param name="materialIndex">The index of the material to update. Default is 0.</param>
 /// <param name="passIndex">The index of the pass of the material to update. Default is 0.</param>
 /// <exception cref="ArgumentNullException">If <paramref name="modelComponent"/> is <see langword="null"/>.</exception>
 /// <exception cref="ArgumentOutOfRangeException">
 /// If <paramref name="materialIndex"/> is less than 0 or greater than <see cref="ModelComponent.GetMaterialCount"/> and not in <see cref="ModelComponent.Materials"/>.
 /// Or if <paramref name="passIndex"/> is less than 0 or greater than or equal to the mu,ber of passes the material has.
 /// </exception>
 public static void SetMaterialParameter <T>(this ModelComponent modelComponent, ObjectParameterAccessor <T> parameterAccessor, T value, int materialIndex = 0, int passIndex = 0)
 {
     modelComponent.GetMaterialPassParameters(materialIndex, passIndex).Set(parameterAccessor, value);
 }