コード例 #1
0
ファイル: Program.cs プロジェクト: liuzhaolovezt/CSGL_Samples
        void RecordCommands()
        {
            for (int i = 0; i < swapchainImages.Count; i++)
            {
                VkCommandBuffer commandBuffer = commandBuffers[i];
                commandBuffer.Reset(VkCommandBufferResetFlags.None);
                VkCommandBufferBeginInfo beginInfo = new VkCommandBufferBeginInfo();
                beginInfo.flags = VkCommandBufferUsageFlags.SimultaneousUseBit;
                commandBuffer.Begin(beginInfo);

                //transfer to writable
                commandBuffer.PipelineBarrier(VkPipelineStageFlags.FragmentShaderBit, VkPipelineStageFlags.TransferBit, VkDependencyFlags.None,
                                              null, null,
                                              new List <VkImageMemoryBarrier> {
                    new VkImageMemoryBarrier {
                        image               = textureImage,
                        oldLayout           = VkImageLayout.ShaderReadOnlyOptimal,
                        newLayout           = VkImageLayout.TransferDstOptimal,
                        srcQueueFamilyIndex = -1,    //VK_QUEUE_FAMILY_IGNORED
                        dstQueueFamilyIndex = -1,
                        srcAccessMask       = VkAccessFlags.None,
                        dstAccessMask       = VkAccessFlags.TransferWriteBit,
                        subresourceRange    = new VkImageSubresourceRange {
                            aspectMask     = VkImageAspectFlags.ColorBit,
                            baseArrayLayer = 0,
                            layerCount     = 1,
                            baseMipLevel   = 0,
                            levelCount     = 1
                        }
                    }
                });

                commandBuffer.CopyBufferToImage(stagingBuffer, textureImage, VkImageLayout.TransferDstOptimal,
                                                new VkBufferImageCopy[] {
                    new VkBufferImageCopy {
                        imageExtent = new VkExtent3D {
                            width  = imageWidth,
                            height = imageHeight,
                            depth  = 1
                        },
                        imageSubresource = new VkImageSubresourceLayers {
                            aspectMask     = VkImageAspectFlags.ColorBit,
                            baseArrayLayer = 0,
                            layerCount     = 1,
                            mipLevel       = 0
                        }
                    }
                });

                //transfer to shader readable
                commandBuffer.PipelineBarrier(VkPipelineStageFlags.TransferBit, VkPipelineStageFlags.FragmentShaderBit, VkDependencyFlags.None,
                                              null, null,
                                              new List <VkImageMemoryBarrier> {
                    new VkImageMemoryBarrier {
                        image               = textureImage,
                        oldLayout           = VkImageLayout.TransferDstOptimal,
                        newLayout           = VkImageLayout.ShaderReadOnlyOptimal,
                        srcQueueFamilyIndex = -1,    //VK_QUEUE_FAMILY_IGNORED
                        dstQueueFamilyIndex = -1,
                        srcAccessMask       = VkAccessFlags.TransferWriteBit,
                        dstAccessMask       = VkAccessFlags.ShaderReadBit,
                        subresourceRange    = new VkImageSubresourceRange {
                            aspectMask     = VkImageAspectFlags.ColorBit,
                            baseArrayLayer = 0,
                            layerCount     = 1,
                            baseMipLevel   = 0,
                            levelCount     = 1
                        }
                    }
                });

                VkRenderPassBeginInfo renderPassBeginInfo = new VkRenderPassBeginInfo();
                renderPassBeginInfo.renderPass  = renderPass;
                renderPassBeginInfo.framebuffer = swapchainFramebuffers[i];
                renderPassBeginInfo.renderArea  = new VkRect2D {
                    extent = new VkExtent2D {
                        width  = window.FramebufferWidth,
                        height = window.FramebufferHeight
                    }
                };
                renderPassBeginInfo.clearValues = new List <VkClearValue> {
                    new VkClearValue {
                        color = new VkClearColorValue(0.125f, 0.125f, 0.125f, 0.125f)
                    }
                };

                commandBuffer.BeginRenderPass(renderPassBeginInfo, VkSubpassContents.Inline);

                commandBuffer.BindPipeline(VkPipelineBindPoint.Graphics, pipeline);
                commandBuffer.BindDescriptorSets(VkPipelineBindPoint.Graphics, pipelineLayout, 0, descriptorSet, null);
                commandBuffer.BindVertexBuffers(0, vertexBuffer, 0);
                commandBuffer.BindIndexBuffer(indexBuffer, 0, VkIndexType.UINT32);

                commandBuffer.SetViewports(0, new VkViewport {
                    width    = swapchainExtent.width,
                    height   = swapchainExtent.height,
                    minDepth = 0,
                    maxDepth = 1,
                });
                commandBuffer.SetScissor(0, new VkRect2D {
                    extent = swapchainExtent
                });
                commandBuffer.DrawIndexed(6, 1, 0, 0, 0);

                commandBuffer.EndRenderPass();
                commandBuffer.End();
            }
        }