internal void Clear()
        {
            m_deviceContext.ClearState();

            m_inputLayout = null;
            m_primitiveTopology = PrimitiveTopology.Undefined;
            m_indexBufferRef = null;
            m_indexBufferFormat = 0;
            m_indexBufferOffset = 0;
            for (int i = 0; i < m_vertexBuffers.Length; i++)
                m_vertexBuffers[i] = null;
            for (int i = 0; i < m_vertexBuffersStrides.Length; i++)
                m_vertexBuffersStrides[i] = 0;

            m_blendState = null;
            m_stencilRef = 0;
            m_depthStencilState = null;
            m_rtvsCount = 0;
            for (int i = 0; i < m_rtvs.Length; i++)
                m_rtvs[i] = null;
            m_dsv = null;

            m_rasterizerState = null;
            m_scissorLeftTop = new Vector2I(-1, -1);
            m_scissorRightBottom = new Vector2I(-1, -1);
            m_viewport = default(RawViewportF);

            m_targetBuffer = null;
            m_targetOffsets = 0;

            m_statistics.ClearStates++;
        }
        internal void SetIndexBuffer(IIndexBuffer ib, MyIndexBufferFormat format, int offset)
        {
            if (ib == m_indexBufferRef &&
                format == m_indexBufferFormat &&
                offset == m_indexBufferOffset)
            {
                return;
            }

            m_indexBufferRef    = ib;
            m_indexBufferFormat = format;
            m_indexBufferOffset = offset;
            m_deviceContext.InputAssembler.SetIndexBuffer(ib.Buffer, (Format)format, offset);
            m_statistics.SetIndexBuffers++;
        }
        internal void Clear()
        {
            if (m_deviceContext != null)
            {
                m_deviceContext.ClearState();
            }

            m_inputLayout       = null;
            m_primitiveTopology = PrimitiveTopology.Undefined;
            m_indexBufferRef    = null;
            m_indexBufferFormat = 0;
            m_indexBufferOffset = 0;
            for (int i = 0; i < m_vertexBuffers.Length; i++)
            {
                m_vertexBuffers[i] = null;
            }
            for (int i = 0; i < m_vertexBuffersStrides.Length; i++)
            {
                m_vertexBuffersStrides[i] = 0;
            }
            for (int i = 0; i < m_vertexBuffersByteOffset.Length; i++)
            {
                m_vertexBuffersByteOffset[i] = 0;
            }

            m_blendState        = null;
            m_stencilRef        = 0;
            m_depthStencilState = null;
            m_rtvsCount         = 0;
            for (int i = 0; i < m_rtvs.Length; i++)
            {
                m_rtvs[i] = null;
            }
            m_dsv = null;

            m_rasterizerState    = null;
            m_scissorLeftTop     = new Vector2I(-1, -1);
            m_scissorRightBottom = new Vector2I(-1, -1);
            m_viewport           = default(RawViewportF);

            m_targetBuffer  = null;
            m_targetOffsets = 0;

            if (m_statistics != null)
            {
                m_statistics.ClearStates++;
            }
        }
        internal void SetIndexBuffer(IIndexBuffer ib, MyIndexBufferFormat format, int offset)
        {
            if (ib == m_indexBufferRef
                && format == m_indexBufferFormat
                && offset == m_indexBufferOffset)
                return;

            m_indexBufferRef = ib;
            m_indexBufferFormat = format;
            m_indexBufferOffset = offset;
            m_deviceContext.InputAssembler.SetIndexBuffer(ib.Buffer, (Format)format, offset);
            m_statistics.SetIndexBuffers++;
        }
        public IIndexBuffer CreateIndexBuffer(string name, int elements, IntPtr?initData = null, MyIndexBufferFormat format = MyIndexBufferFormat.UShort, ResourceUsage usage = ResourceUsage.Default)
        {
            Debug.Assert(usage != ResourceUsage.Staging, "Conspicuous ResourceUsage setup: should an index buffer be a staging resource?");

            int stride;

            switch (format)
            {
            case MyIndexBufferFormat.UShort:
                stride = 2;
                break;

            case MyIndexBufferFormat.UInt:
                stride = 4;
                break;

            default:
                throw new NotImplementedException("Unsupported index buffer format.");
            }

            BufferDescription description = new BufferDescription(elements * stride,
                                                                  usage,
                                                                  BindFlags.IndexBuffer,
                                                                  usage == ResourceUsage.Dynamic ? CpuAccessFlags.Write : CpuAccessFlags.None,
                                                                  ResourceOptionFlags.None,
                                                                  stride);

            return(CreateInternal <MyIndexBuffer>(name, ref description, initData, b => b.Format = format));
        }