Пример #1
0
        internal static MySpritesBatch CreateSpritesBatch()
        {
            var batch = new MySpritesBatch();

            batch.startInstance = m_spriteInstanceList.Count;
            return(batch);
        }
Пример #2
0
        internal static void Draw()
        {
            // flush inner sprites batcher
            if (m_currentInnerBatch.texture != null)
            {
                m_currentInnerBatch.Commit();
            }
            m_currentInnerBatch = new MySpritesBatch();

            if (m_inputLayout == null)
            {
                var spritesInput = MyVertexInput.Empty()
                                   .Append(MyVertexInputComponentType.CUSTOM_HALF4_0, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                                   .Append(MyVertexInputComponentType.CUSTOM_HALF4_1, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                                   .Append(MyVertexInputComponentType.COLOR4, 0, MyVertexInputComponentFreq.PER_INSTANCE);

                m_inputLayout = MyVertexInput.CreateLayout(spritesInput.Hash, MyShaderCache.CompileFromFile("sprite.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            //

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_spritesInstanceBuffer.Buffer, MyVertexFormatSpritePositionTextureColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            context.VertexShader.Set(m_vertexShader.VertexShader);

            context.PixelShader.Set(m_pixelShader.PixelShader);
            context.PixelShader.SetSamplers(0, MyRender.StandardSamplers);

            context.OutputMerger.SetTargets(MyRender.Backbuffer.RenderTarget);
            context.OutputMerger.SetBlendState(MyRender.BlendStateGui);

            DataStream stream;

            context.MapSubresource(m_spritesInstanceBuffer.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            for (int i = 0; i < m_spriteInstanceList.Count; i++)
            {
                stream.Write(m_spriteInstanceList[i]);
            }
            context.UnmapSubresource(m_spritesInstanceBuffer.Buffer, 0);
            stream.Dispose();

            for (int b = 0; b < m_spriteBatches.Count; b++)
            {
                context.PixelShader.SetShaderResource(0, m_spriteBatches[b].texture);
                context.DrawInstanced(4, m_spriteBatches[b].instanceCount, 0, m_spriteBatches[b].startInstance);
            }

            m_spriteInstanceList.Clear();
            m_spriteBatches.Clear();
        }
        internal static void AddSingleSprite(MyTexture2D texture, MyVertexFormatSpritePositionTextureColor sprite)
        {
            if (m_currentInnerBatch.texture != texture.ShaderView)
            {
                if (m_currentInnerBatch.texture != null)
                    m_currentInnerBatch.Commit();

                m_currentInnerBatch = CreateSpritesBatch();
                m_currentInnerBatch.startInstance = m_spriteInstanceList.Count;
                m_currentInnerBatch.SetTexture(texture);
            }

            m_currentInnerBatch.AddSprite(sprite);
        }
Пример #4
0
        internal static void AddSingleSprite(MyTexture2D texture, MyVertexFormatSpritePositionTextureColor sprite)
        {
            if (m_currentInnerBatch.texture != texture.ShaderView)
            {
                if (m_currentInnerBatch.texture != null)
                {
                    m_currentInnerBatch.Commit();
                }

                m_currentInnerBatch = CreateSpritesBatch();
                m_currentInnerBatch.startInstance = m_spriteInstanceList.Count;
                m_currentInnerBatch.SetTexture(texture);
            }

            m_currentInnerBatch.AddSprite(sprite);
        }
Пример #5
0
 internal static void Commit(MySpritesBatch batch)
 {
     batch.Count = StackTop().m_instances.Count - batch.Start;
     StackTop().m_batches.Add(batch);
 }
        internal static void Draw()
        {
            // flush inner sprites batcher
            if (m_currentInnerBatch.texture != null)
                m_currentInnerBatch.Commit();
            m_currentInnerBatch = new MySpritesBatch();

            if (m_inputLayout == null)
            {
                var spritesInput = MyVertexInput.Empty()
                    .Append(MyVertexInputComponentType.CUSTOM_HALF4_0, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                    .Append(MyVertexInputComponentType.CUSTOM_HALF4_1, 0, MyVertexInputComponentFreq.PER_INSTANCE)
                    .Append(MyVertexInputComponentType.COLOR4, 0, MyVertexInputComponentFreq.PER_INSTANCE);

                m_inputLayout = MyVertexInput.CreateLayout(spritesInput.Hash, MyShaderCache.CompileFromFile("sprite.hlsl", "vs", MyShaderProfile.VS_5_0).Bytecode);
            }

            //

            var context = MyRender.Context;

            context.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleStrip;

            context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(m_spritesInstanceBuffer.Buffer, MyVertexFormatSpritePositionTextureColor.STRIDE, 0));
            context.InputAssembler.InputLayout = m_inputLayout;

            context.Rasterizer.SetViewport(0, 0, MyRender.ViewportResolution.X, MyRender.ViewportResolution.Y);

            context.VertexShader.Set(m_vertexShader.VertexShader);

            context.PixelShader.Set(m_pixelShader.PixelShader);
            context.PixelShader.SetSamplers(0, MyRender.StandardSamplers);

            context.OutputMerger.SetTargets(MyRender.Backbuffer.RenderTarget);
            context.OutputMerger.SetBlendState(MyRender.BlendStateGui);

            DataStream stream;
            context.MapSubresource(m_spritesInstanceBuffer.Buffer, MapMode.WriteDiscard, MapFlags.None, out stream);
            for (int i = 0; i < m_spriteInstanceList.Count; i++)
                stream.Write(m_spriteInstanceList[i]);
            context.UnmapSubresource(m_spritesInstanceBuffer.Buffer, 0);
            stream.Dispose();

            for (int b = 0; b < m_spriteBatches.Count; b++)
            {
                context.PixelShader.SetShaderResource(0, m_spriteBatches[b].texture);
                context.DrawInstanced(4, m_spriteBatches[b].instanceCount, 0, m_spriteBatches[b].startInstance);
            }

            m_spriteInstanceList.Clear();
            m_spriteBatches.Clear();
        }
 internal static MySpritesBatch CreateSpritesBatch()
 {
     var batch = new MySpritesBatch();
     batch.startInstance = m_spriteInstanceList.Count;
     return batch;
 }
Пример #8
0
 internal static void Commit(MySpritesBatch batch)
 {
     batch.Count = StackTop().m_instances.Count - batch.Start;
     StackTop().m_batches.Add(batch);
 }