Path: blob/21.2-virgl/src/virtio/vulkan/vn_pipeline.c
4560 views
/*1* Copyright 2019 Google LLC2* SPDX-License-Identifier: MIT3*4* based in part on anv and radv which are:5* Copyright © 2015 Intel Corporation6* Copyright © 2016 Red Hat.7* Copyright © 2016 Bas Nieuwenhuizen8*/910#include "vn_pipeline.h"1112#include "venus-protocol/vn_protocol_driver_pipeline.h"13#include "venus-protocol/vn_protocol_driver_pipeline_cache.h"14#include "venus-protocol/vn_protocol_driver_pipeline_layout.h"15#include "venus-protocol/vn_protocol_driver_shader_module.h"1617#include "vn_device.h"1819/* shader module commands */2021VkResult22vn_CreateShaderModule(VkDevice device,23const VkShaderModuleCreateInfo *pCreateInfo,24const VkAllocationCallbacks *pAllocator,25VkShaderModule *pShaderModule)26{27struct vn_device *dev = vn_device_from_handle(device);28const VkAllocationCallbacks *alloc =29pAllocator ? pAllocator : &dev->base.base.alloc;3031struct vn_shader_module *mod =32vk_zalloc(alloc, sizeof(*mod), VN_DEFAULT_ALIGN,33VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);34if (!mod)35return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);3637vn_object_base_init(&mod->base, VK_OBJECT_TYPE_SHADER_MODULE, &dev->base);3839VkShaderModule mod_handle = vn_shader_module_to_handle(mod);40vn_async_vkCreateShaderModule(dev->instance, device, pCreateInfo, NULL,41&mod_handle);4243*pShaderModule = mod_handle;4445return VK_SUCCESS;46}4748void49vn_DestroyShaderModule(VkDevice device,50VkShaderModule shaderModule,51const VkAllocationCallbacks *pAllocator)52{53struct vn_device *dev = vn_device_from_handle(device);54struct vn_shader_module *mod = vn_shader_module_from_handle(shaderModule);55const VkAllocationCallbacks *alloc =56pAllocator ? pAllocator : &dev->base.base.alloc;5758if (!mod)59return;6061vn_async_vkDestroyShaderModule(dev->instance, device, shaderModule, NULL);6263vn_object_base_fini(&mod->base);64vk_free(alloc, mod);65}6667/* pipeline layout commands */6869VkResult70vn_CreatePipelineLayout(VkDevice device,71const VkPipelineLayoutCreateInfo *pCreateInfo,72const VkAllocationCallbacks *pAllocator,73VkPipelineLayout *pPipelineLayout)74{75struct vn_device *dev = vn_device_from_handle(device);76const VkAllocationCallbacks *alloc =77pAllocator ? pAllocator : &dev->base.base.alloc;7879struct vn_pipeline_layout *layout =80vk_zalloc(alloc, sizeof(*layout), VN_DEFAULT_ALIGN,81VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);82if (!layout)83return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);8485vn_object_base_init(&layout->base, VK_OBJECT_TYPE_PIPELINE_LAYOUT,86&dev->base);8788VkPipelineLayout layout_handle = vn_pipeline_layout_to_handle(layout);89vn_async_vkCreatePipelineLayout(dev->instance, device, pCreateInfo, NULL,90&layout_handle);9192*pPipelineLayout = layout_handle;9394return VK_SUCCESS;95}9697void98vn_DestroyPipelineLayout(VkDevice device,99VkPipelineLayout pipelineLayout,100const VkAllocationCallbacks *pAllocator)101{102struct vn_device *dev = vn_device_from_handle(device);103struct vn_pipeline_layout *layout =104vn_pipeline_layout_from_handle(pipelineLayout);105const VkAllocationCallbacks *alloc =106pAllocator ? pAllocator : &dev->base.base.alloc;107108if (!layout)109return;110111vn_async_vkDestroyPipelineLayout(dev->instance, device, pipelineLayout,112NULL);113114vn_object_base_fini(&layout->base);115vk_free(alloc, layout);116}117118/* pipeline cache commands */119120VkResult121vn_CreatePipelineCache(VkDevice device,122const VkPipelineCacheCreateInfo *pCreateInfo,123const VkAllocationCallbacks *pAllocator,124VkPipelineCache *pPipelineCache)125{126struct vn_device *dev = vn_device_from_handle(device);127const VkAllocationCallbacks *alloc =128pAllocator ? pAllocator : &dev->base.base.alloc;129130struct vn_pipeline_cache *cache =131vk_zalloc(alloc, sizeof(*cache), VN_DEFAULT_ALIGN,132VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);133if (!cache)134return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);135136vn_object_base_init(&cache->base, VK_OBJECT_TYPE_PIPELINE_CACHE,137&dev->base);138139VkPipelineCacheCreateInfo local_create_info;140if (pCreateInfo->initialDataSize) {141local_create_info = *pCreateInfo;142local_create_info.pInitialData +=143sizeof(struct vk_pipeline_cache_header);144pCreateInfo = &local_create_info;145}146147VkPipelineCache cache_handle = vn_pipeline_cache_to_handle(cache);148vn_async_vkCreatePipelineCache(dev->instance, device, pCreateInfo, NULL,149&cache_handle);150151*pPipelineCache = cache_handle;152153return VK_SUCCESS;154}155156void157vn_DestroyPipelineCache(VkDevice device,158VkPipelineCache pipelineCache,159const VkAllocationCallbacks *pAllocator)160{161struct vn_device *dev = vn_device_from_handle(device);162struct vn_pipeline_cache *cache =163vn_pipeline_cache_from_handle(pipelineCache);164const VkAllocationCallbacks *alloc =165pAllocator ? pAllocator : &dev->base.base.alloc;166167if (!cache)168return;169170vn_async_vkDestroyPipelineCache(dev->instance, device, pipelineCache,171NULL);172173vn_object_base_fini(&cache->base);174vk_free(alloc, cache);175}176177VkResult178vn_GetPipelineCacheData(VkDevice device,179VkPipelineCache pipelineCache,180size_t *pDataSize,181void *pData)182{183struct vn_device *dev = vn_device_from_handle(device);184struct vn_physical_device *physical_dev = dev->physical_device;185186struct vk_pipeline_cache_header *header = pData;187VkResult result;188if (!pData) {189result = vn_call_vkGetPipelineCacheData(dev->instance, device,190pipelineCache, pDataSize, NULL);191if (result != VK_SUCCESS)192return vn_error(dev->instance, result);193194*pDataSize += sizeof(*header);195return VK_SUCCESS;196}197198if (*pDataSize <= sizeof(*header)) {199*pDataSize = 0;200return VK_INCOMPLETE;201}202203const VkPhysicalDeviceProperties *props =204&physical_dev->properties.properties;205header->header_size = sizeof(*header);206header->header_version = VK_PIPELINE_CACHE_HEADER_VERSION_ONE;207header->vendor_id = props->vendorID;208header->device_id = props->deviceID;209memcpy(header->uuid, props->pipelineCacheUUID, VK_UUID_SIZE);210211*pDataSize -= header->header_size;212result =213vn_call_vkGetPipelineCacheData(dev->instance, device, pipelineCache,214pDataSize, pData + header->header_size);215if (result < VK_SUCCESS)216return vn_error(dev->instance, result);217218*pDataSize += header->header_size;219220return result;221}222223VkResult224vn_MergePipelineCaches(VkDevice device,225VkPipelineCache dstCache,226uint32_t srcCacheCount,227const VkPipelineCache *pSrcCaches)228{229struct vn_device *dev = vn_device_from_handle(device);230231vn_async_vkMergePipelineCaches(dev->instance, device, dstCache,232srcCacheCount, pSrcCaches);233234return VK_SUCCESS;235}236237/* pipeline commands */238239VkResult240vn_CreateGraphicsPipelines(VkDevice device,241VkPipelineCache pipelineCache,242uint32_t createInfoCount,243const VkGraphicsPipelineCreateInfo *pCreateInfos,244const VkAllocationCallbacks *pAllocator,245VkPipeline *pPipelines)246{247struct vn_device *dev = vn_device_from_handle(device);248const VkAllocationCallbacks *alloc =249pAllocator ? pAllocator : &dev->base.base.alloc;250251for (uint32_t i = 0; i < createInfoCount; i++) {252struct vn_pipeline *pipeline =253vk_zalloc(alloc, sizeof(*pipeline), VN_DEFAULT_ALIGN,254VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);255if (!pipeline) {256for (uint32_t j = 0; j < i; j++)257vk_free(alloc, vn_pipeline_from_handle(pPipelines[j]));258memset(pPipelines, 0, sizeof(*pPipelines) * createInfoCount);259return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);260}261262vn_object_base_init(&pipeline->base, VK_OBJECT_TYPE_PIPELINE,263&dev->base);264265VkPipeline pipeline_handle = vn_pipeline_to_handle(pipeline);266pPipelines[i] = pipeline_handle;267}268269vn_async_vkCreateGraphicsPipelines(dev->instance, device, pipelineCache,270createInfoCount, pCreateInfos, NULL,271pPipelines);272273return VK_SUCCESS;274}275276VkResult277vn_CreateComputePipelines(VkDevice device,278VkPipelineCache pipelineCache,279uint32_t createInfoCount,280const VkComputePipelineCreateInfo *pCreateInfos,281const VkAllocationCallbacks *pAllocator,282VkPipeline *pPipelines)283{284struct vn_device *dev = vn_device_from_handle(device);285const VkAllocationCallbacks *alloc =286pAllocator ? pAllocator : &dev->base.base.alloc;287288for (uint32_t i = 0; i < createInfoCount; i++) {289struct vn_pipeline *pipeline =290vk_zalloc(alloc, sizeof(*pipeline), VN_DEFAULT_ALIGN,291VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);292if (!pipeline) {293for (uint32_t j = 0; j < i; j++)294vk_free(alloc, vn_pipeline_from_handle(pPipelines[j]));295memset(pPipelines, 0, sizeof(*pPipelines) * createInfoCount);296return vn_error(dev->instance, VK_ERROR_OUT_OF_HOST_MEMORY);297}298299vn_object_base_init(&pipeline->base, VK_OBJECT_TYPE_PIPELINE,300&dev->base);301302VkPipeline pipeline_handle = vn_pipeline_to_handle(pipeline);303pPipelines[i] = pipeline_handle;304}305306vn_async_vkCreateComputePipelines(dev->instance, device, pipelineCache,307createInfoCount, pCreateInfos, NULL,308pPipelines);309310return VK_SUCCESS;311}312313void314vn_DestroyPipeline(VkDevice device,315VkPipeline _pipeline,316const VkAllocationCallbacks *pAllocator)317{318struct vn_device *dev = vn_device_from_handle(device);319struct vn_pipeline *pipeline = vn_pipeline_from_handle(_pipeline);320const VkAllocationCallbacks *alloc =321pAllocator ? pAllocator : &dev->base.base.alloc;322323if (!pipeline)324return;325326vn_async_vkDestroyPipeline(dev->instance, device, _pipeline, NULL);327328vn_object_base_fini(&pipeline->base);329vk_free(alloc, pipeline);330}331332333