Exemplo n.º 1
0
        protected unsafe virtual void PrepareForRendering()
        {
            // Use LinearClamp for sampler state
            var localSamplerState = samplerState ?? graphicsDevice.SamplerStates.LinearClamp;

            // Sets the sampler state of the effect
            if (samplerUpdater.HasValue)
            {
                Parameters.Set(samplerUpdater.Value, localSamplerState);
            }

            Effect.UpdateEffect(graphicsDevice);

            // Setup states (Blend, DepthStencil, Rasterizer)
            mutablePipeline.State.SetDefaults();
            mutablePipeline.State.RootSignature     = Effect.RootSignature;
            mutablePipeline.State.EffectBytecode    = Effect.Effect.Bytecode;
            mutablePipeline.State.BlendState        = blendState ?? BlendStates.AlphaBlend;
            mutablePipeline.State.DepthStencilState = depthStencilState ?? DepthStencilStates.Default;
            mutablePipeline.State.RasterizerState   = rasterizerState ?? RasterizerStates.CullBack;
            mutablePipeline.State.InputElements     = ResourceContext.InputElements;
            mutablePipeline.State.PrimitiveType     = PrimitiveType.TriangleList;
            mutablePipeline.State.Output.CaptureState(GraphicsContext.CommandList);
            mutablePipeline.Update();

            // Bind pipeline
            if (mutablePipeline.State.DepthStencilState.StencilEnable)
            {
                GraphicsContext.CommandList.SetStencilReference(stencilReferenceValue);
            }
            GraphicsContext.CommandList.SetPipelineState(mutablePipeline.CurrentState);

            // Bind VB/IB
            if (ResourceContext.VertexBuffer != null)
            {
                GraphicsContext.CommandList.SetVertexBuffer(0, ResourceContext.VertexBuffer, 0, vertexStructSize);
            }
            if (ResourceContext.IndexBuffer != null)
            {
                GraphicsContext.CommandList.SetIndexBuffer(ResourceContext.IndexBuffer, 0, indexStructSize == sizeof(int));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Begins text rendering (swaps and maps the vertex buffer to write to).
        /// </summary>
        /// <param name="graphicsContext">The current GraphicsContext.</param>
        public void End([NotNull] GraphicsContext graphicsContext)
        {
            if (graphicsContext == null)
            {
                throw new ArgumentNullException(nameof(graphicsContext));
            }

            // Reallocate buffers if max number of characters is exceeded
            if (charsToRenderCount > maxCharacterCount)
            {
                maxCharacterCount = (int)(1.5f * charsToRenderCount);
                Initialize(graphicsContext, maxCharacterCount);
            }

            // Set the rendering parameters
            simpleEffect.Parameters.Set(TexturingKeys.Texture0, DebugSpriteFont);
            simpleEffect.Parameters.Set(SpriteEffectKeys.Color, TextColor);

            // Swap vertex buffer
            activeVertexBufferIndex = ++activeVertexBufferIndex >= VertexBufferCount ? 0 : activeVertexBufferIndex;

            // Map the vertex buffer to write to
            mappedVertexBuffer        = graphicsContext.CommandList.MapSubresource(vertexBuffers[activeVertexBufferIndex], 0, MapMode.WriteDiscard);
            mappedVertexBufferPointer = mappedVertexBuffer.DataBox.DataPointer;

            unsafe
            {
                // Clear buffer first (because of the buffer mapping mode used)
                Utilities.ClearMemory(mappedVertexBufferPointer, 0x0, VertexBufferLength * sizeof(VertexPositionNormalTexture));

                charsToRenderCount = 0;

                //Draw the strings
                var constantInfos = new RectangleF(GlyphWidth, GlyphHeight, DebugSpriteWidth, DebugSpriteHeight);
                foreach (var textInfo in stringsToDraw)
                {
                    var textLength        = textInfo.Text.Length;
                    var textLengthPointer = new IntPtr(&textLength);

                    Native.NativeInvoke.xnGraphicsFastTextRendererGenerateVertices(constantInfos, textInfo.RenderingInfo, textInfo.Text, out textLengthPointer, out mappedVertexBufferPointer);

                    charsToRenderCount += *(int *)textLengthPointer.ToPointer();
                }
            }

            // Unmap the vertex buffer
            graphicsContext.CommandList.UnmapSubresource(mappedVertexBuffer);
            mappedVertexBufferPointer = IntPtr.Zero;

            // Update pipeline state
            pipelineState.State.SetDefaults();
            pipelineState.State.RootSignature     = simpleEffect.RootSignature;
            pipelineState.State.EffectBytecode    = simpleEffect.Effect.Bytecode;
            pipelineState.State.DepthStencilState = DepthStencilStates.None;
            pipelineState.State.BlendState        = BlendStates.AlphaBlend;
            pipelineState.State.Output.CaptureState(graphicsContext.CommandList);
            pipelineState.State.InputElements = inputElementDescriptions[activeVertexBufferIndex];
            pipelineState.Update();

            graphicsContext.CommandList.SetPipelineState(pipelineState.CurrentState);

            // Update effect
            simpleEffect.UpdateEffect(graphicsContext.CommandList.GraphicsDevice);
            simpleEffect.Apply(graphicsContext);

            // Bind and draw
            graphicsContext.CommandList.SetVertexBuffer(0, vertexBuffersBinding[activeVertexBufferIndex].Buffer, vertexBuffersBinding[activeVertexBufferIndex].Offset, vertexBuffersBinding[activeVertexBufferIndex].Stride);
            graphicsContext.CommandList.SetIndexBuffer(indexBufferBinding.Buffer, 0, indexBufferBinding.Is32Bit);

            graphicsContext.CommandList.DrawIndexed(charsToRenderCount * 6);
        }