git.s-ol.nu ~forks/DiligentCore / c6e198f
Enable VulkanLogicalDevice with external devices s-ol a month ago
4 changed file(s) with 109 addition(s) and 46 deletion(s). Raw diff Collapse all Expand all
9494 const ExtensionFeatures& EnabledExtFeatures,
9595 const VkAllocationCallbacks* vkAllocator);
9696
97 static std::shared_ptr<VulkanLogicalDevice> Create(const VulkanPhysicalDevice& PhysicalDevice,
98 const VkDevice& Device,
99 VkPipelineStageFlags EnabledShaderStages,
100 const VkAllocationCallbacks* vkAllocator);
97 static std::shared_ptr<VulkanLogicalDevice> Create(const VulkanPhysicalDevice& PhysicalDevice,
98 const VkDevice& Device,
99 const VkPhysicalDeviceFeatures& EnabledFeatures,
100 const VkAllocationCallbacks* vkAllocator);
101101
102102 // clang-format off
103103 VulkanLogicalDevice (const VulkanLogicalDevice&) = delete;
235235 const VkDeviceCreateInfo& DeviceCI,
236236 const ExtensionFeatures& EnabledExtFeatures,
237237 const VkAllocationCallbacks* vkAllocator);
238 VulkanLogicalDevice(const VulkanPhysicalDevice& PhysicalDevice,
239 const VkDevice& Device,
240 VkPipelineStageFlags EnabledShaderStages,
241 const VkAllocationCallbacks* vkAllocator);
238 VulkanLogicalDevice(const VulkanPhysicalDevice& PhysicalDevice,
239 const VkDevice& Device,
240 const VkPhysicalDeviceFeatures& EnabledFeatures,
241 const VkAllocationCallbacks* vkAllocator);
242242
243243 template <typename VkObjectType,
244244 VulkanHandleTypeId VkTypeId,
8888 IDeviceContext** ppContexts) PURE;
8989
9090 VIRTUAL void METHOD(AttachToVulkanDevice)(THIS_
91 const VkInstance REF Instance,
92 const VkPhysicalDevice REF PhysicalDevice,
93 const VkDevice REF LogicalDevice,
94 const VkQueue REF Queue,
95 uint32_t QueueFamilyIndex,
96 const EngineVkCreateInfo REF EngineCI,
97 IRenderDevice** ppDevice,
98 IDeviceContext** ppContexts) PURE;
91 const VkInstance REF Instance,
92 const VkPhysicalDevice REF PhysicalDevice,
93 const VkPhysicalDeviceFeatures REF DeviceFeatures,
94 const VkDevice REF LogicalDevice,
95 const VkQueue REF Queue,
96 uint32_t QueueFamilyIndex,
97 const EngineVkCreateInfo REF EngineCI,
98 IRenderDevice** ppDevice,
99 IDeviceContext** ppContexts) PURE;
99100
100101 /// Creates a swap chain for Vulkan-based engine implementation
101102
6767
6868 virtual void DILIGENT_CALL_TYPE AttachToVulkanDevice(const VkInstance& Instance,
6969 const VkPhysicalDevice& PhysicalDevice,
70 const VkPhysicalDeviceFeatures& DeviceFeatures,
7071 const VkDevice& LogicalDevice,
7172 const VkQueue& Queue,
7273 uint32_t QueueFamilyIndex,
300301 RayQueryFeats.rayQuery != VK_FALSE, RayTracing2, "Inline ray tracing is");
301302 // clang-format on
302303 #undef FeatureSupport
304 #undef ENABLE_FEATURE
303305
304306
305307 // To enable some device extensions you must enable instance extension VK_KHR_get_physical_device_properties2
542544 }
543545 }
544546
545 void EngineFactoryVkImpl::AttachToVulkanDevice(const VkInstance& Instance,
546 const VkPhysicalDevice& PhysicalDevice,
547 const VkDevice& LogicalDevice,
548 const VkQueue& Queue,
549 uint32_t QueueFamilyIndex,
550 const EngineVkCreateInfo& EngineCI,
551 IRenderDevice** ppDevice,
552 IDeviceContext** ppContexts)
553 {
547 void EngineFactoryVkImpl::AttachToVulkanDevice(const VkInstance& VkInstance,
548 const VkPhysicalDevice& VkPhysicalDevice,
549 const VkPhysicalDeviceFeatures& PhysicalDeviceFeatures,
550 const VkDevice& VkLogicalDevice,
551 const VkQueue& Queue,
552 uint32_t QueueFamilyIndex,
553 const EngineVkCreateInfo& _EngineCI,
554 IRenderDevice** ppDevice,
555 IDeviceContext** ppContexts)
556 {
557 EngineVkCreateInfo EngineCI = _EngineCI;
558
554559 auto& RawMemAllocator = GetRawAllocator();
555560
556 auto VulkanInstance = VulkanUtilities::VulkanInstance::Create(Instance, nullptr);
557 auto VulkanPhysicalDevice = VulkanUtilities::VulkanPhysicalDevice::Create(PhysicalDevice, *VulkanInstance);
558
559 VkPipelineStageFlags PipelineStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT | VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT;
560 auto VulkanLogicalDevice = VulkanUtilities::VulkanLogicalDevice::Create(*VulkanPhysicalDevice, LogicalDevice, PipelineStages, nullptr);
561 auto Instance = VulkanUtilities::VulkanInstance::Create(VkInstance, reinterpret_cast<VkAllocationCallbacks*>(EngineCI.pVkAllocator));
562 auto vkAllocator = Instance->GetVkAllocator();
563
564 auto PhysicalDevice = VulkanUtilities::VulkanPhysicalDevice::Create(VkPhysicalDevice, *Instance);
565 auto LogicalDevice = VulkanUtilities::VulkanLogicalDevice::Create(*PhysicalDevice, VkLogicalDevice, PhysicalDeviceFeatures, vkAllocator);
566
567 auto GetFeatureState = [](DEVICE_FEATURE_STATE RequestedState, bool IsFeatureSupported, const char* FeatureName)
568 {
569 switch (RequestedState)
570 {
571 case DEVICE_FEATURE_STATE_DISABLED:
572 return DEVICE_FEATURE_STATE_DISABLED;
573
574 case DEVICE_FEATURE_STATE_ENABLED:
575 {
576 if (IsFeatureSupported)
577 return DEVICE_FEATURE_STATE_ENABLED;
578 else
579 LOG_ERROR_AND_THROW(FeatureName, " not supported by this device");
580 }
581
582 case DEVICE_FEATURE_STATE_OPTIONAL:
583 return IsFeatureSupported ? DEVICE_FEATURE_STATE_ENABLED : DEVICE_FEATURE_STATE_DISABLED;
584
585 default:
586 UNEXPECTED("Unexpected feature state");
587 return DEVICE_FEATURE_STATE_DISABLED;
588 }
589 };
590
591 #define ENABLE_FEATURE(vkFeature, State, FeatureName) \
592 do \
593 { \
594 State = \
595 GetFeatureState(State, PhysicalDeviceFeatures.vkFeature != VK_FALSE, FeatureName); \
596 } while (false)
597 auto ImageCubeArrayFeature = DEVICE_FEATURE_STATE_OPTIONAL;
598 auto SamplerAnisotropyFeature = DEVICE_FEATURE_STATE_OPTIONAL;
599 // clang-format off
600 ENABLE_FEATURE(geometryShader, EngineCI.Features.GeometryShaders, "Geometry shaders are");
601 ENABLE_FEATURE(tessellationShader, EngineCI.Features.Tessellation, "Tessellation is");
602 ENABLE_FEATURE(pipelineStatisticsQuery, EngineCI.Features.PipelineStatisticsQueries, "Pipeline statistics queries are");
603 ENABLE_FEATURE(occlusionQueryPrecise, EngineCI.Features.OcclusionQueries, "Occlusion queries are");
604 ENABLE_FEATURE(imageCubeArray, ImageCubeArrayFeature, "Image cube arrays are");
605 ENABLE_FEATURE(fillModeNonSolid, EngineCI.Features.WireframeFill, "Wireframe fill is");
606 ENABLE_FEATURE(samplerAnisotropy, SamplerAnisotropyFeature, "Anisotropic texture filtering is");
607 ENABLE_FEATURE(depthBiasClamp, EngineCI.Features.DepthBiasClamp, "Depth bias clamp is");
608 ENABLE_FEATURE(depthClamp, EngineCI.Features.DepthClamp, "Depth clamp is");
609 ENABLE_FEATURE(independentBlend, EngineCI.Features.IndependentBlend, "Independent blend is");
610 ENABLE_FEATURE(dualSrcBlend, EngineCI.Features.DualSourceBlend, "Dual-source blend is");
611 ENABLE_FEATURE(multiViewport, EngineCI.Features.MultiViewport, "Multiviewport is");
612 ENABLE_FEATURE(textureCompressionBC, EngineCI.Features.TextureCompressionBC, "BC texture compression is");
613 ENABLE_FEATURE(vertexPipelineStoresAndAtomics, EngineCI.Features.VertexPipelineUAVWritesAndAtomics, "Vertex pipeline UAV writes and atomics are");
614 ENABLE_FEATURE(fragmentStoresAndAtomics, EngineCI.Features.PixelUAVWritesAndAtomics, "Pixel UAV writes and atomics are");
615 ENABLE_FEATURE(shaderStorageImageExtendedFormats, EngineCI.Features.TextureUAVExtendedFormats, "Texture UAV extended formats are");
616 // clang-format on
617 #undef ENABLE_FEATURE
618
561619 RefCntAutoPtr<CommandQueueVkImpl> pCmdQueueVk{
562 NEW_RC_OBJ(RawMemAllocator, "CommandQueueVk instance", CommandQueueVkImpl)(VulkanLogicalDevice, Queue, QueueFamilyIndex)};
620 NEW_RC_OBJ(RawMemAllocator, "CommandQueueVk instance", CommandQueueVkImpl)(LogicalDevice, Queue, QueueFamilyIndex)};
563621 std::array<ICommandQueueVk*, 1> CommandQueues = {{pCmdQueueVk}};
564622
565623 OnRenderDeviceCreated = [&](RenderDeviceVkImpl* pRenderDeviceVk)
574632 };
575633
576634 AttachToVulkanDevice(
577 VulkanInstance,
578 std::move(VulkanPhysicalDevice),
579 VulkanLogicalDevice,
635 Instance,
636 std::move(PhysicalDevice),
637 LogicalDevice,
580638 CommandQueues.size(), CommandQueues.data(),
581639 EngineCI,
582640 ppDevice,
4242 return std::shared_ptr<VulkanLogicalDevice>{LogicalDevice};
4343 }
4444
45 std::shared_ptr<VulkanLogicalDevice> VulkanLogicalDevice::Create(const VulkanPhysicalDevice& PhysicalDevice,
46 const VkDevice& Device,
47 VkPipelineStageFlags EnabledShaderStages,
48 const VkAllocationCallbacks* vkAllocator)
49 {
50 auto* LogicalDevice = new VulkanLogicalDevice{PhysicalDevice, Device, EnabledShaderStages, vkAllocator};
45 std::shared_ptr<VulkanLogicalDevice> VulkanLogicalDevice::Create(const VulkanPhysicalDevice& PhysicalDevice,
46 const VkDevice& Device,
47 const VkPhysicalDeviceFeatures& EnabledFeatures,
48 const VkAllocationCallbacks* vkAllocator)
49 {
50 auto* LogicalDevice = new VulkanLogicalDevice{PhysicalDevice, Device, EnabledFeatures, vkAllocator};
5151 return std::shared_ptr<VulkanLogicalDevice>{LogicalDevice};
5252 }
5353
8484 m_EnabledShaderStages |= VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR;
8585 }
8686
87 VulkanLogicalDevice::VulkanLogicalDevice(const VulkanPhysicalDevice& PhysicalDevice,
88 const VkDevice& Device,
89 VkPipelineStageFlags EnabledShaderStages,
90 const VkAllocationCallbacks* vkAllocator) :
87 VulkanLogicalDevice::VulkanLogicalDevice(const VulkanPhysicalDevice& PhysicalDevice,
88 const VkDevice& Device,
89 const VkPhysicalDeviceFeatures& EnabledFeatures,
90 const VkAllocationCallbacks* vkAllocator) :
9191 m_VkDevice{Device},
9292 m_VkAllocator{vkAllocator},
93 m_EnabledShaderStages{EnabledShaderStages},
94 m_EnabledFeatures{},
93 m_EnabledFeatures{EnabledFeatures},
9594 m_EnabledExtFeatures{}
9695 {
9796 #if DILIGENT_USE_VOLK
9998 // https://github.com/zeux/volk#optimizing-device-calls
10099 volkLoadDevice(m_VkDevice);
101100 #endif
101 m_EnabledShaderStages = VK_PIPELINE_STAGE_VERTEX_SHADER_BIT | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT;
102 if (EnabledFeatures.geometryShader)
103 m_EnabledShaderStages |= VK_PIPELINE_STAGE_GEOMETRY_SHADER_BIT;
104 if (EnabledFeatures.tessellationShader)
105 m_EnabledShaderStages |= VK_PIPELINE_STAGE_TESSELLATION_CONTROL_SHADER_BIT | VK_PIPELINE_STAGE_TESSELLATION_EVALUATION_SHADER_BIT;
102106 }
103107
104108