예제 #1
0
        public void Execute(VkCommandBuffer buffer)
        {
            VkImageMemoryBarrier imageMemoryBarrier = VkImageMemoryBarrier.New();

            imageMemoryBarrier.srcAccessMask       = VkAccessFlags.None;
            imageMemoryBarrier.dstAccessMask       = VkAccessFlags.ColorAttachmentRead | VkAccessFlags.ColorAttachmentWrite;
            imageMemoryBarrier.oldLayout           = VkImageLayout.Undefined;
            imageMemoryBarrier.newLayout           = VkImageLayout.ColorAttachmentOptimal;
            imageMemoryBarrier.srcQueueFamilyIndex = VulkanNative.QueueFamilyIgnored;
            imageMemoryBarrier.dstQueueFamilyIndex = VulkanNative.QueueFamilyIgnored;
            imageMemoryBarrier.image            = swapchainImage;
            imageMemoryBarrier.subresourceRange = new VkImageSubresourceRange()
            {
                baseMipLevel   = 0,
                levelCount     = 1,
                baseArrayLayer = 0,
                layerCount     = 1,
                aspectMask     = VkImageAspectFlags.Color
            };

            vkCmdPipelineBarrier(buffer, VkPipelineStageFlags.AllGraphics, VkPipelineStageFlags.AllGraphics, VkDependencyFlags.ByRegion,
                                 0, null, 0, null, 1, &imageMemoryBarrier);

            VkClearColorValue clearColorValue = new VkClearColorValue {
                float32_0 = 3f / 255f, float32_1 = 2f / 255, float32_2 = 12f / 255, float32_3 = 1
            };
            VkClearDepthStencilValue clearDepthStencilValue = new VkClearDepthStencilValue(1, 0);

            VkClearValue[] clearValues = new VkClearValue[2];
            clearValues[0].color        = clearColorValue;
            clearValues[1].depthStencil = clearDepthStencilValue;

            VkRenderPassBeginInfo passBeginInfo = VkRenderPassBeginInfo.New();

            passBeginInfo.renderPass               = renderPass;
            passBeginInfo.framebuffer              = swapchainFramebuffer;
            passBeginInfo.renderArea.extent.width  = (uint)Program.width;
            passBeginInfo.renderArea.extent.height = (uint)Program.height;
            passBeginInfo.clearValueCount          = (uint)clearValues.Length;

            fixed(VkClearValue *ptr = clearValues)
            passBeginInfo.pClearValues = ptr;

            vkCmdBeginRenderPass(buffer, &passBeginInfo, VkSubpassContents.Inline);

            VkViewport viewport = new VkViewport();

            viewport.x      = 0;
            viewport.y      = (float)Program.height;
            viewport.width  = (float)Program.width;
            viewport.height = -(float)Program.height;

            VkRect2D scissor = new VkRect2D();

            scissor.offset.x      = 0;
            scissor.offset.y      = 0;
            scissor.extent.width  = (uint)Program.width;
            scissor.extent.height = (uint)Program.height;

            vkCmdSetViewport(buffer, 0, 1, &viewport);
            vkCmdSetScissor(buffer, 0, 1, &scissor);

            vkCmdBindPipeline(buffer, VkPipelineBindPoint.Graphics, pipeline);
            if (mesh.IsReady)
            {
                mesh.Use();
                VkBuffer[] databuffers = new VkBuffer[] { mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer, mesh.Mesh.vertices.Buffer };
                ulong[]    offsets     = new ulong[] { 0, 3 * 4, 6 * 4, 6 * 4 + 2 * 4 };

                fixed(VkBuffer *bptr = databuffers)
                fixed(ulong *optr = offsets)
                vkCmdBindVertexBuffers(buffer, 0, 4, bptr, optr);

                vkCmdBindIndexBuffer(buffer, mesh.Mesh.indices.Buffer, 0, VkIndexType.Uint32);

                uniformdata.Write(0, camera.View.ToFloatArray());
                uniformdata.Write(16, camera.Projection.ToFloatArray());

                UpdateUniformData(uniformdata, swapchainImageIndex);

                VkDescriptorSet  sets   = descriptorSets[swapchainImageIndex];
                VkPipelineLayout layout = pipelineLayout;
                vkCmdBindDescriptorSets(buffer, VkPipelineBindPoint.Graphics, layout, 0, 1, &sets, 0, null);

                vkCmdDrawIndexed(buffer, (uint)mesh.Mesh.indices.Length, 1, 0, 0, 0);
                mesh.StopUse();
            }

            vkCmdEndRenderPass(buffer);
        }