private void Vkctrl_VulkanClosing(object sender, SharpVulkanWpf.VulkanEventArgs args) { // 本クラスで作ったVulkanの各リソースを破棄する. var dev = args.Device; VulkanAPI.vkDestroyImage(dev, m_image); m_image = null; VulkanAPI.vkDestroyImageView(dev, m_imageView); m_imageView = null; VulkanAPI.vkFreeMemory(dev, m_imageMemory); m_imageMemory = null; VulkanAPI.vkDestroySampler(dev, m_imageSampler); m_imageSampler = null; VulkanAPI.vkDestroyPipeline(dev, m_graphicsPipeline); m_graphicsPipeline = null; VulkanAPI.vkDestroyPipelineLayout(dev, m_pipelineLayout); m_pipelineLayout = null; m_shaderStages.Select(x => x.module).ToList().ForEach(x => VulkanAPI.vkDestroyShaderModule(dev, x)); VulkanAPI.vkDestroyBuffer(dev, m_vertexBuffer); m_vertexBuffer = null; VulkanAPI.vkFreeMemory(dev, m_vertexBufferMemory); m_vertexBufferMemory = null; VulkanAPI.vkDestroyBuffer(dev, m_uniformBuffer); m_uniformBuffer = null; VulkanAPI.vkFreeMemory(dev, m_uniformBufferMemory); m_uniformBufferMemory = null; VulkanAPI.vkFreeDescriptorSets(dev, m_descriptorPool, new[] { m_descriptorSet }); VulkanAPI.vkDestroyDescriptorSetLayout(dev, m_descriptorSetLayout); m_descriptorSetLayout = null; VulkanAPI.vkDestroyDescriptorPool(dev, m_descriptorPool); m_descriptorPool = null; VulkanAPI.vkFreeCommandBuffers(dev, m_commandPool, m_commandBuffers); VulkanAPI.vkDestroyCommandPool(dev, m_commandPool); m_commandBuffers = null; m_commandPool = null; }
private void Vkctrl_VulkanInitialized(object sender, SharpVulkanWpf.VulkanEventArgs args) { var device = args.Device; var commandPoolCreateInfo = new VkCommandPoolCreateInfo() { queueFamilyIndex = args.GraphicsQueueIndex, flags = VkCommandPoolCreateFlags.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, }; VulkanAPI.vkCreateCommandPool(device, ref commandPoolCreateInfo, out m_commandPool); var allocateInfo = new VkCommandBufferAllocateInfo() { commandBufferCount = 1, commandPool = m_commandPool, }; VulkanAPI.vkAllocateCommandBuffers(device, ref allocateInfo, out m_commandBuffers); // 頂点入力情報の構築. m_vertexInputState = CreateVertexInputState(); // プリミティブの情報 トライアングルリストでデータが生成されている. m_inputAssemblyState = new VkPipelineInputAssemblyStateCreateInfo() { topology = VkPrimitiveTopology.VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST }; // シェーダーステージ情報の構築. m_shaderStages = new VkPipelineShaderStageCreateInfo[2] { SampleHelpers.CreateShader(device, "resource/simpleVS.spv", VkShaderStageFlagBits.VK_SHADER_STAGE_VERTEX_BIT), SampleHelpers.CreateShader(device, "resource/simpleFS.spv", VkShaderStageFlagBits.VK_SHADER_STAGE_FRAGMENT_BIT), }; // ラスタライザーステートの構築. m_rasterizationState = new VkPipelineRasterizationStateCreateInfo(); // デプスステンシルステートの構築. m_depthStencilState = new VkPipelineDepthStencilStateCreateInfo(); // カラーブレンドステートの構築. m_colorBlendState = new VkPipelineColorBlendStateCreateInfo(); var colorBlendAttachment = new VkPipelineColorBlendAttachmentState(); m_colorBlendState.attachments = new[] { colorBlendAttachment }; // マルチサンプルステートの構築. m_multisampleState = new VkPipelineMultisampleStateCreateInfo(); // パイプラインレイアウトの構築. m_pipelineLayout = CreatePipelineLayout(device); // ビューポートステートの構築. m_viewportState = CreateViewportState(); // グラフィックスパイプラインを構築. m_graphicsPipeline = CreateGraphicsPipeline(device, vkctrl.GetControlRenderPass()); // 頂点バッファの作成. CreateVertexBuffer(device, args.PhysicalDevice); }
private void Vkctrl_VulkanClosing(object sender, SharpVulkanWpf.VulkanEventArgs args) { // 本クラスで作ったVulkanの各リソースを破棄する. var dev = args.Device; VulkanAPI.vkFreeCommandBuffers(dev, m_commandPool, m_commandBuffers); VulkanAPI.vkDestroyCommandPool(dev, m_commandPool); m_commandBuffers = null; m_commandPool = null; }
private void Vkctrl_VulkanRendering(object sender, SharpVulkanWpf.VulkanEventArgs args) { var device = args.Device; var image = vkctrl.AcquireNextImage(); var command = m_commandBuffers[0]; VulkanAPI.vkBeginCommandBuffer(command); VkClearColorValue clearColor = new VkClearColorValue(); clearColor.valF32.R = 0.125f; clearColor.valF32.G = 0.25f; clearColor.valF32.B = (float)(0.5f * Math.Sin(m_frameCount * 0.1) + 0.5); clearColor.valF32.A = 1.0f; VkImageSubresourceRange range = new VkImageSubresourceRange() { baseMipLevel = 0, levelCount = 1, baseArrayLayer = 0, layerCount = 1, aspectMask = VkImageAspectFlags.VK_IMAGE_ASPECT_COLOR_BIT }; setImageMemoryBarrier(command, VkAccessFlags.VK_ACCESS_MEMORY_READ_BIT, VkAccessFlags.VK_ACCESS_TRANSFER_WRITE_BIT, VkImageLayout.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, VkImageLayout.VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, image, VkImageAspectFlags.VK_IMAGE_ASPECT_COLOR_BIT); VulkanAPI.vkCmdClearColorImage(command, image, VkImageLayout.VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, ref clearColor, new VkImageSubresourceRange[] { range }); // Present のためにレイアウト変更 setImageMemoryBarrier(command, VkAccessFlags.VK_ACCESS_TRANSFER_WRITE_BIT, VkAccessFlags.VK_ACCESS_MEMORY_READ_BIT, VkImageLayout.VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VkImageLayout.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR, image, VkImageAspectFlags.VK_IMAGE_ASPECT_COLOR_BIT); VulkanAPI.vkEndCommandBuffer(command); var submitInfo = new VkSubmitInfo() { commandBuffers = new[] { command } }; VulkanAPI.vkQueueSubmit(args.GraphicsQueue, new VkSubmitInfo[] { submitInfo }, null); vkctrl.SwapBuffers(); m_frameCount++; }
private void Vkctrl_VulkanRendering(object sender, SharpVulkanWpf.VulkanEventArgs args) { var device = args.Device; var image = vkctrl.AcquireNextImage(); var framebuffer = vkctrl.AcquireNextFramebuffer(); var command = m_commandBuffers[0]; VulkanAPI.vkBeginCommandBuffer(command); VkClearColorValue clearColor = new VkClearColorValue(); clearColor.valF32.R = 0.125f; clearColor.valF32.G = 0.25f; clearColor.valF32.B = (float)(0.5f * Math.Sin(m_frameCount * 0.1) + 0.5); clearColor.valF32.A = 1.0f; var renderArea = new VkRect2D() { offset = new VkOffset2D(), extent = vkctrl.GetCurrentExtent(), }; var renderPassBeginInfo = new VkRenderPassBeginInfo() { framebuffer = framebuffer, renderArea = renderArea, renderPass = vkctrl.GetControlRenderPass(), pClearValues = new[] { new VkClearValue() { color = clearColor } } }; VulkanAPI.vkCmdBeginRenderPass(command, ref renderPassBeginInfo, VkSubpassContents.VK_SUBPASS_CONTENTS_INLINE); VulkanAPI.vkCmdBindPipeline(command, VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline); VulkanAPI.vkCmdBindVertexBuffers(command, 0, 1, new[] { m_vertexBuffer }, new[] { (VkDeviceSize)0 }); VulkanAPI.vkCmdDraw(command, 3, 1, 0, 0); VulkanAPI.vkCmdEndRenderPass(command); VulkanAPI.vkEndCommandBuffer(command); var submitInfo = new VkSubmitInfo() { commandBuffers = new[] { command } }; VulkanAPI.vkQueueSubmit(args.GraphicsQueue, new VkSubmitInfo[] { submitInfo }, null); vkctrl.SwapBuffers(); m_frameCount++; }
private void Vkctrl_VulkanClosing(object sender, SharpVulkanWpf.VulkanEventArgs args) { // 本クラスで作ったVulkanの各リソースを破棄する. var dev = args.Device; VulkanAPI.vkDestroyPipeline(dev, m_graphicsPipeline); m_graphicsPipeline = null; m_shaderStages.Select(x => x.module).ToList().ForEach(x => VulkanAPI.vkDestroyShaderModule(dev, x)); m_resourceManager.Destroy(dev); VulkanAPI.vkFreeCommandBuffers(dev, m_commandPool, m_commandBuffers); VulkanAPI.vkDestroyCommandPool(dev, m_commandPool); m_commandBuffers = null; m_commandPool = null; }
private void Vkctrl_VulkanInitialized(object sender, SharpVulkanWpf.VulkanEventArgs args) { var device = args.Device; var commandPoolCreateInfo = new VkCommandPoolCreateInfo() { queueFamilyIndex = args.GraphicsQueueIndex, flags = VkCommandPoolCreateFlags.VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, }; VulkanAPI.vkCreateCommandPool(device, ref commandPoolCreateInfo, out m_commandPool); var allocateInfo = new VkCommandBufferAllocateInfo() { commandBufferCount = 1, commandPool = m_commandPool, }; VulkanAPI.vkAllocateCommandBuffers(device, ref allocateInfo, out m_commandBuffers); }
private void Vkctrl_VulkanRendering(object sender, SharpVulkanWpf.VulkanEventArgs args) { var device = args.Device; var image = vkctrl.AcquireNextImage(); var framebuffer = vkctrl.AcquireNextFramebuffer(); var command = m_commandBuffers[0]; VulkanAPI.vkBeginCommandBuffer(command); VkClearColorValue clearColor = new VkClearColorValue(); clearColor.valF32.R = 0.125f; clearColor.valF32.G = 0.25f; clearColor.valF32.B = 0.4f; clearColor.valF32.A = 1.0f; VkClearDepthStencilValue clearDepth = new VkClearDepthStencilValue() { depth = 1.0f, stencil = 0, }; var currentExtent = vkctrl.GetCurrentExtent(); // 定数バッファの更新. var sceneTrs = new Transform(); var aspect = (float)currentExtent.width / (float)currentExtent.height; var proj = mat4.Perspective((float)Math.PI / 3.0f, aspect, 1.0f, 100.0f); sceneTrs.World = mat4.RotateY(m_frameCount * 0.05f) * mat4.RotateX(m_frameCount * 0.01f); sceneTrs.View = mat4.LookAt(new vec3(0, 0, 6), new vec3(0, 0, 0), new vec3(0, 1, 0)); sceneTrs.Proj = proj; MappedMemoryStream mapped = null; VulkanAPI.vkMapMemory(device, m_uniformBufferMemory, 0, VkDeviceSize.VK_WHOLE_SIZE, 0, out mapped); mapped.Write(sceneTrs); VulkanAPI.vkUnmapMemory(device, m_uniformBufferMemory); var renderArea = new VkRect2D() { offset = new VkOffset2D(), extent = vkctrl.GetCurrentExtent(), }; var renderPassBeginInfo = new VkRenderPassBeginInfo() { framebuffer = framebuffer, renderArea = renderArea, renderPass = vkctrl.GetControlRenderPass(), pClearValues = new[] { new VkClearValue() { color = clearColor }, new VkClearValue() { depthStencil = clearDepth }, } }; VulkanAPI.vkCmdBeginRenderPass(command, ref renderPassBeginInfo, VkSubpassContents.VK_SUBPASS_CONTENTS_INLINE); VulkanAPI.vkCmdBindPipeline(command, VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, m_graphicsPipeline); VulkanAPI.vkCmdBindVertexBuffers(command, 0, 1, new[] { m_vertexBuffer }, new[] { (VkDeviceSize)0 }); VulkanAPI.vkCmdBindIndexBuffer(command, m_indexBuffer, 0, VkIndexType.VK_INDEX_TYPE_UINT16); VulkanAPI.vkCmdBindDescriptorSets(command, VkPipelineBindPoint.VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, new[] { m_descriptorSet }, null); VulkanAPI.vkCmdDrawIndexed(command, 36, 1, 0, 0, 0); VulkanAPI.vkCmdEndRenderPass(command); VulkanAPI.vkEndCommandBuffer(command); var submitInfo = new VkSubmitInfo() { commandBuffers = new[] { command } }; VulkanAPI.vkQueueSubmit(args.GraphicsQueue, submitInfo, null); vkctrl.SwapBuffers(); m_frameCount++; }