protected override void PlatformClearBuffer()
        {
            RenderPassInfo renderPassInfo = GetCurrentRenderPass();

            renderPassInfo.ClearBuffer = true;
            renderPassInfo.ClearColor  = ClearColor;
        }
 private void CreateNextRenderPass()
 {
     _currentRenderPassState = new RenderPassInfo()
     {
         Framebuffer = CurrentFramebuffer
     };
     _renderPassStates.Add(_currentRenderPassState);
 }
        private void FlushFrameDrawCommands()
        {
            _scInfo.AcquireNextImage(_device, _imageAvailableSemaphore);
            for (int i = 0; i < _renderPassStates.Count; i++)
            {
                RenderPassInfo renderPassState = _renderPassStates[i];

                VkCommandBuffer primaryCommandBuffer = GetPrimaryCommandBuffer();
                renderPassState.PrimaryCommandBuffer = primaryCommandBuffer;
                VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.New();
                beginInfo.flags = VkCommandBufferUsageFlags.OneTimeSubmit;
                vkBeginCommandBuffer(primaryCommandBuffer, ref beginInfo);
                VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo.New();
                VkFramebufferBase     fbInfo = renderPassState.Framebuffer;
                renderPassBeginInfo.framebuffer = fbInfo.VkFramebuffer;
                renderPassBeginInfo.renderPass  = renderPassState.ClearBuffer
                    ? fbInfo.RenderPassClearBuffer
                    : fbInfo.RenderPassNoClear;

                if (renderPassState.ClearBuffer)
                {
                    VkClearColorValue colorClear = new VkClearColorValue
                    {
                        float32_0 = renderPassState.ClearColor.R,
                        float32_1 = renderPassState.ClearColor.G,
                        float32_2 = renderPassState.ClearColor.B,
                        float32_3 = renderPassState.ClearColor.A
                    };
                    VkClearDepthStencilValue depthClear = new VkClearDepthStencilValue()
                    {
                        depth = 1f, stencil = 0
                    };
                    StackList <VkClearValue, Size512Bytes> clearValues = new StackList <VkClearValue, Size512Bytes>();
                    if (fbInfo.ColorTexture != null)
                    {
                        clearValues.Add(new VkClearValue()
                        {
                            color = colorClear
                        });
                    }
                    if (fbInfo.DepthTexture != null)
                    {
                        clearValues.Add(new VkClearValue()
                        {
                            depthStencil = depthClear
                        });
                    }

                    renderPassBeginInfo.clearValueCount = clearValues.Count;
                    renderPassBeginInfo.pClearValues    = (VkClearValue *)clearValues.Data;
                }
                renderPassBeginInfo.renderArea.extent = new VkExtent2D(fbInfo.Width, fbInfo.Height);

                vkCmdBeginRenderPass(primaryCommandBuffer, ref renderPassBeginInfo, VkSubpassContents.SecondaryCommandBuffers);
                RawList <VkCommandBuffer> secondaryCBs = renderPassState.SecondaryCommandBuffers;
                if (secondaryCBs.Count > 0)
                {
                    vkCmdExecuteCommands(primaryCommandBuffer, secondaryCBs.Count, ref secondaryCBs[0]);
                }
                vkCmdEndRenderPass(primaryCommandBuffer);
                vkEndCommandBuffer(primaryCommandBuffer);

                VkSubmitInfo         submitInfo    = VkSubmitInfo.New();
                VkSemaphore          waitSemaphore = (i == 0) ? _imageAvailableSemaphore : _renderPassSemaphores[i - 1];
                VkPipelineStageFlags waitStages    = VkPipelineStageFlags.ColorAttachmentOutput;
                submitInfo.waitSemaphoreCount = 1;
                submitInfo.pWaitSemaphores    = &waitSemaphore;
                submitInfo.pWaitDstStageMask  = &waitStages;
                VkCommandBuffer cb = primaryCommandBuffer;
                submitInfo.commandBufferCount = 1;
                submitInfo.pCommandBuffers    = &cb;
                VkSemaphore signalSemaphore = _renderPassSemaphores[i];
                submitInfo.signalSemaphoreCount = 1;
                submitInfo.pSignalSemaphores    = &signalSemaphore;
                vkQueueSubmit(_graphicsQueue, 1, ref submitInfo, VkFence.Null);
            }
        }
        private void DrawPrimitives(int indexCount, int instanceCount, int startingIndex, int startingVertex)
        {
            RenderPassInfo     renderPassState  = GetCurrentRenderPass();
            VkPipelineLayout   layout           = ShaderResourceBindingSlots.PipelineLayout;
            VkPipelineCacheKey pipelineCacheKey = new VkPipelineCacheKey();

            pipelineCacheKey.RenderPass        = renderPassState.Framebuffer.RenderPassClearBuffer;
            pipelineCacheKey.PipelineLayout    = layout;
            pipelineCacheKey.BlendState        = (VkBlendState)BlendState;
            pipelineCacheKey.Framebuffer       = renderPassState.Framebuffer;
            pipelineCacheKey.DepthStencilState = (VkDepthStencilState)DepthStencilState;
            pipelineCacheKey.RasterizerState   = (VkRasterizerState)RasterizerState;
            pipelineCacheKey.PrimitiveTopology = _primitiveTopology;
            pipelineCacheKey.ShaderSet         = ShaderSet;
            pipelineCacheKey.VertexBindings    = VertexBuffers;
            VkPipeline graphicsPipeline = _resourceCache.GetGraphicsPipeline(ref pipelineCacheKey);

            VkDescriptorSetCacheKey descriptorSetCacheKey = new VkDescriptorSetCacheKey();

            descriptorSetCacheKey.ShaderResourceBindingSlots = ShaderResourceBindingSlots;
            descriptorSetCacheKey.ConstantBuffers            = _constantBuffers;
            descriptorSetCacheKey.TextureBindings            = _textureBindings;
            descriptorSetCacheKey.SamplerStates = _samplerStates;
            VkDescriptorSet descriptorSet = _resourceCache.GetDescriptorSet(ref descriptorSetCacheKey);

            VkCommandBuffer          cb        = GetCommandBuffer();
            VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo.New();

            beginInfo.flags = VkCommandBufferUsageFlags.OneTimeSubmit | VkCommandBufferUsageFlags.RenderPassContinue;
            VkCommandBufferInheritanceInfo inheritanceInfo = VkCommandBufferInheritanceInfo.New();

            inheritanceInfo.renderPass = renderPassState.Framebuffer.RenderPassClearBuffer;
            beginInfo.pInheritanceInfo = &inheritanceInfo;
            vkBeginCommandBuffer(cb, ref beginInfo);

            vkCmdBindPipeline(cb, VkPipelineBindPoint.Graphics, graphicsPipeline);
            vkCmdBindDescriptorSets(
                cb,
                VkPipelineBindPoint.Graphics,
                layout,
                0,
                1,
                ref descriptorSet,
                0,
                IntPtr.Zero);

            int vbCount = ShaderSet.InputLayout.InputDescriptions.Length;
            StackList <VkBuffer, Size512Bytes> vbs = new StackList <VkBuffer, Size512Bytes>();

            for (int vbIndex = 0; vbIndex < vbCount; vbIndex++)
            {
                vbs.Add(((VkVertexBuffer)VertexBuffers[vbIndex]).DeviceBuffer);
            }

            StackList <VkBuffer, Size512Bytes> offsets = new StackList <VkBuffer, Size512Bytes>();

            vkCmdBindVertexBuffers(cb, 0, vbs.Count, (IntPtr)vbs.Data, (IntPtr)offsets.Data);
            vkCmdBindIndexBuffer(cb, IndexBuffer.DeviceBuffer, 0, IndexBuffer.IndexType);
            VkViewport viewport = new VkViewport()
            {
                x        = Viewport.X,
                y        = Viewport.Y,
                width    = Viewport.Width,
                height   = Viewport.Height,
                minDepth = 0,
                maxDepth = 1
            };

            vkCmdSetViewport(cb, 0, 1, ref viewport);
            vkCmdSetScissor(cb, 0, 1, ref _scissorRect);
            vkCmdDrawIndexed(cb, (uint)indexCount, (uint)instanceCount, (uint)startingIndex, startingVertex, 0);
            vkEndCommandBuffer(cb);

            renderPassState.SecondaryCommandBuffers.Add(cb);
        }