Device::Device( VkPhysicalDevice physicalDevice, UniquePointer<VkDevice> device, Allocator& allocator ) : _allocator( allocator, "Vulkan Device Context Allocator" ), _physicalDevice( eastl::move( physicalDevice ) ), _presentQueue( VK_NULL_HANDLE ), _sparseBindQueue( VK_NULL_HANDLE ), _resourceCopyQueue( VK_NULL_HANDLE ), _device( eastl::move( device ) ), _pipelineCache( nullptr ), _presentFence( eastl::move( *CreateFence( VkFenceCreateFlagBits::VK_FENCE_CREATE_SIGNALED_BIT ) ) ), _copyFence( eastl::move( *CreateFence( VkFenceCreateFlagBits::VK_FENCE_CREATE_SIGNALED_BIT ) ) ), _copySemaphore( eastl::move( *CreateSemaphore( 0 ) ) ), _copyCommandPool( eastl::move( *CreateCommandPool( VkCommandPoolCreateFlagBits::VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, 0u ) ) ), _specializationConstants( { allocator, "Vulkan Device Context Specialization Constant Bucket Allocator" } ) { }
bool CommandBufferManager::Initialize() { if (!CreateCommandPool()) return false; if (!CreateCommandBuffers()) return false; if (m_use_threaded_submission && !CreateSubmitThread()) return false; return true; }
bool Tutorial03::CreateCommandBuffers() { if( !CreateCommandPool( GetGraphicsQueue().FamilyIndex, &Vulkan.GraphicsCommandPool ) ) { std::cout << "Could not create command pool!" << std::endl; return false; } uint32_t image_count = static_cast<uint32_t>(GetSwapChain().Images.size()); Vulkan.GraphicsCommandBuffers.resize( image_count, VK_NULL_HANDLE ); if( !AllocateCommandBuffers( Vulkan.GraphicsCommandPool, image_count, &Vulkan.GraphicsCommandBuffers[0] ) ) { std::cout << "Could not allocate command buffers!" << std::endl; return false; } return true; }
void VulkanWindowContext::createBuffers(VkFormat format) { GrVkFormatToPixelConfig(format, &fPixelConfig); fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, nullptr); SkASSERT(fImageCount); fImages = new VkImage[fImageCount]; fGetSwapchainImagesKHR(fBackendContext->fDevice, fSwapchain, &fImageCount, fImages); // set up initial image layouts and create surfaces fImageLayouts = new VkImageLayout[fImageCount]; fRenderTargets = new sk_sp<GrRenderTarget>[fImageCount]; fSurfaces = new sk_sp<SkSurface>[fImageCount]; for (uint32_t i = 0; i < fImageCount; ++i) { fImageLayouts[i] = VK_IMAGE_LAYOUT_UNDEFINED; GrBackendRenderTargetDesc desc; GrVkImageInfo info; info.fImage = fImages[i]; info.fAlloc = VK_NULL_HANDLE; info.fImageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL; info.fImageTiling = VK_IMAGE_TILING_OPTIMAL; info.fFormat = format; info.fLevelCount = 1; desc.fWidth = fWidth; desc.fHeight = fHeight; desc.fConfig = fPixelConfig; desc.fOrigin = kTopLeft_GrSurfaceOrigin; desc.fSampleCnt = 0; desc.fStencilBits = 0; desc.fRenderTargetHandle = (GrBackendObject) &info; fRenderTargets[i].reset(fContext->textureProvider()->wrapBackendRenderTarget(desc)); fSurfaces[i] = this->createRenderSurface(fRenderTargets[i], 24); } // create the command pool for the command buffers if (VK_NULL_HANDLE == fCommandPool) { VkCommandPoolCreateInfo commandPoolInfo; memset(&commandPoolInfo, 0, sizeof(VkCommandPoolCreateInfo)); commandPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; // this needs to be on the render queue commandPoolInfo.queueFamilyIndex = fBackendContext->fGraphicsQueueIndex; commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT; GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, CreateCommandPool(fBackendContext->fDevice, &commandPoolInfo, nullptr, &fCommandPool)); } // set up the backbuffers VkSemaphoreCreateInfo semaphoreInfo; memset(&semaphoreInfo, 0, sizeof(VkSemaphoreCreateInfo)); semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; semaphoreInfo.pNext = nullptr; semaphoreInfo.flags = 0; VkCommandBufferAllocateInfo commandBuffersInfo; memset(&commandBuffersInfo, 0, sizeof(VkCommandBufferAllocateInfo)); commandBuffersInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; commandBuffersInfo.pNext = nullptr; commandBuffersInfo.commandPool = fCommandPool; commandBuffersInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; commandBuffersInfo.commandBufferCount = 2; VkFenceCreateInfo fenceInfo; memset(&fenceInfo, 0, sizeof(VkFenceCreateInfo)); fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; fenceInfo.pNext = nullptr; fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; // we create one additional backbuffer structure here, because we want to // give the command buffers they contain a chance to finish before we cycle back fBackbuffers = new BackbufferInfo[fImageCount + 1]; for (uint32_t i = 0; i < fImageCount + 1; ++i) { fBackbuffers[i].fImageIndex = -1; GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo, nullptr, &fBackbuffers[i].fAcquireSemaphore)); GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, CreateSemaphore(fBackendContext->fDevice, &semaphoreInfo, nullptr, &fBackbuffers[i].fRenderSemaphore)); GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, AllocateCommandBuffers(fBackendContext->fDevice, &commandBuffersInfo, fBackbuffers[i].fTransitionCmdBuffers)); GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr, &fBackbuffers[i].fUsageFences[0])); GR_VK_CALL_ERRCHECK(fBackendContext->fInterface, CreateFence(fBackendContext->fDevice, &fenceInfo, nullptr, &fBackbuffers[i].fUsageFences[1])); } fCurrentBackbufferIndex = fImageCount; }
// TODO: Resizing bool VkContext::Init(Config config) { if (!glfwInit()) { std::cout << "Unable to initialize GLFW" << std::endl; return false; } if (!CreateInstance(config.windowTitle)) { std::cout << "Unable to create Vulkan instance" << std::endl; return false; } else { std::cout << "Created Vulkan instance" << std::endl; } if (!GetPhysicalDevice()) { std::cout << "Unable to get Vulkan physical device" << std::endl; return false; } else { std::cout << "Acquired Vulkan physical device" << std::endl; } if (!CreateWindowSurface(config.windowWidth, config.windowHeight, config.windowTitle)) { std::cout << "Unable to create window surfcace for Vulkan" << std::endl; return false; } else { std::cout << "Created Vulkan window surface" << std::endl; } if (!GetQueueFamilies()) { std::cout << "Unable to get Vulkan queue families" << std::endl; return false; } else { std::cout << "Acquired queue families" << std::endl; } if (!CreateDevice()) { std::cout << "Unable to create Vulkan device" << std::endl; return false; } else { std::cout << "Created Vulkan device" << std::endl; } #ifdef VOXL_DEBUG if (!SetupValidation()) { std::cout << "Unable to setup Vulkan validation" << std::endl; return false; } else { std::cout << "Setup Vulkan validation" << std::endl; } #endif if (!CreateSemaphores()) { std::cout << "Unable to create Vulkan semaphores" << std::endl; return false; } else { std::cout << "Created Vulkan semaphores" << std::endl; } if (!CreateSwapchain(config.windowWidth, config.windowHeight)) { std::cout << "Unable to create Vulkan swapchain" << std::endl; return false; } else { std::cout << "Created Vulkan swapchain" << std::endl; } if (!CreateCommandPool()) { std::cout << "Unable to create command pool" << std::endl; return false; } else { std::cout << "Created Vulkan command pool" << std::endl; } if (!CreateCommandBuffers()) { std::cout << "Unable to create command buffers" << std::endl; return false; } else { std::cout << "Created Vulkan command buffers" << std::endl; } std::cout << "Successfully initialized Vulkan" << std::endl; return true; }