示例#1
0
        /// <summary>
        /// Adds the draw data back, and increments the position
        /// </summary>
        /// <param name="renderData"></param>
        public void AddRenderData(SpriteRenderData renderData)
        {
            m_drawDataPosition++;

            if (m_drawDataPosition >= m_maxSpriteInstances)
            {
                Flush();
            }
        }
示例#2
0
        /// <summary>
        /// Creates a new instance of the SpriteRenderer
        /// </summary>
        /// <param name="device">The device to use</param>
        /// <param name="maxSpriteInstances">The maximum sprite instances that can be cached before a flush happens</param>
        public SpriteRenderer(Device device, int maxSpriteInstances = 10000)
        {
            /* Initialize our arrays to hold our sprite data */
            m_spriteRenderData = new SpriteRenderData[maxSpriteInstances];
            m_spriteDrawData = new SpriteDrawData[maxSpriteInstances];

            /* Initialize all the items in the array */
            for (int i = 0; i < maxSpriteInstances; i++)
            {
                m_spriteRenderData[i] = new SpriteRenderData();
            }

            m_maxSpriteInstances = maxSpriteInstances;
            m_device = device;

            /* Create our default blend states using our helper */
            m_blendStates = SpriteRendererBlendStateHelper.InitializeDefaultBlendStates(m_device);

            /* Create our vertex shader */
            m_vertexShaderInstanced10 = new VertexShader10(device,
                                                           SHADER_RESOURCE_NAME, Assembly.GetExecutingAssembly(),
                                                           "SpriteInstancedVS", 
                                                           ShaderVersion.Vs_4_0);

            /* Create our pixel shader */
            m_pixelShader10 = new PixelShader10(device,
                                                SHADER_RESOURCE_NAME, Assembly.GetExecutingAssembly(),
                                                "SpritePS", 
                                                ShaderVersion.Ps_4_0,ShaderFlags.Debug);
            
            /* Create a new sprite quad that holds our GPU buffers */
            m_spriteQuad = new SpriteQuad(device, maxSpriteInstances);

            m_spriteQuadShaderBinding = new GeometryInputShaderBinding(m_spriteQuad, 
                                                                       m_vertexShaderInstanced10, 
                                                                       m_pixelShader10);

            var rastDesc = new RasterizerStateDescription();
            rastDesc.IsAntialiasedLineEnabled = false;
            rastDesc.CullMode = CullMode.None;
            rastDesc.DepthBias = 0;
            rastDesc.DepthBiasClamp = 1.0f;
            rastDesc.IsDepthClipEnabled = false;
            rastDesc.FillMode = FillMode.Solid;
            rastDesc.IsFrontCounterclockwise = false;
            rastDesc.IsMultisampleEnabled = false;
            rastDesc.IsScissorEnabled = false;
            rastDesc.SlopeScaledDepthBias = 0;
            m_rasterizerState = RasterizerState.FromDescription(m_device, rastDesc);
            
            var dsDesc = new DepthStencilStateDescription();
            dsDesc.IsDepthEnabled = false;
            dsDesc.DepthWriteMask = DepthWriteMask.All;
            dsDesc.DepthComparison = Comparison.Less;
            dsDesc.IsStencilEnabled = false;
            dsDesc.StencilReadMask = 0xff;
            dsDesc.StencilWriteMask = 0xff;
            dsDesc.FrontFace = new DepthStencilOperationDescription{ DepthFailOperation = StencilOperation.Keep, FailOperation = StencilOperation.Replace, Comparison = Comparison.Always };
            dsDesc.BackFace = dsDesc.FrontFace;
            m_dsState = DepthStencilState.FromDescription(m_device, dsDesc);

            var sampDesc = new SamplerDescription();
            sampDesc.AddressU = TextureAddressMode.Wrap;
            sampDesc.AddressV = TextureAddressMode.Wrap;
            sampDesc.AddressW = TextureAddressMode.Wrap;
            sampDesc.BorderColor = new Color4(0, 0, 0, 0).InternalColor4;
            sampDesc.ComparisonFunction = Comparison.Never;
            sampDesc.Filter = Filter.MinMagMipLinear;
            sampDesc.MaximumAnisotropy = 1;
            sampDesc.MaximumLod = float.MaxValue;
            sampDesc.MinimumLod = 0;
            sampDesc.MipLodBias = 0;
            m_linearSamplerState = SamplerState.FromDescription(m_device, sampDesc);

            sampDesc.Filter = Filter.MinMagMipPoint;
            m_pointSamplerState = SamplerState.FromDescription(m_device, sampDesc);
        }