1 | #pragma once |
2 | |
3 | #ifdef _WIN64 |
4 | #define VK_USE_PLATFORM_WIN32_KHR 1 |
5 | #include <aclapi.h> |
6 | #include <dxgi1_2.h> |
7 | #include <windows.h> |
8 | #include <VersionHelpers.h> |
9 | #endif |
10 | |
11 | #include "taichi/rhi/vulkan/vulkan_common.h" |
12 | |
13 | #include <functional> |
14 | #include <optional> |
15 | #include <string> |
16 | #include <vector> |
17 | |
18 | namespace taichi::lang { |
19 | namespace vulkan { |
20 | |
21 | class VulkanEnvSettings { |
22 | public: |
23 | // This version number is used to create a vkInstance, it should be |
24 | // the highest API version that is designed to use. |
25 | // Reference: |
26 | // https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VkApplicationInfo.html |
27 | static constexpr uint32_t k_api_version() { |
28 | return VK_API_VERSION_1_3; |
29 | } |
30 | }; |
31 | |
32 | #ifdef _WIN64 |
33 | |
34 | // this class is needed for Vulkan-Cuda interop on Windows |
35 | // the implementation is taken from |
36 | // https://github.com/NVIDIA/cuda-samples/tree/master/Samples/vulkanImageCUDA |
37 | class WindowsSecurityAttributes { |
38 | protected: |
39 | SECURITY_ATTRIBUTES security_attributes; |
40 | PSECURITY_DESCRIPTOR security_descriptor; |
41 | |
42 | public: |
43 | WindowsSecurityAttributes(); |
44 | SECURITY_ATTRIBUTES *operator&(); |
45 | ~WindowsSecurityAttributes(); |
46 | }; |
47 | |
48 | inline WindowsSecurityAttributes::WindowsSecurityAttributes() { |
49 | security_descriptor = (PSECURITY_DESCRIPTOR)calloc( |
50 | 1, SECURITY_DESCRIPTOR_MIN_LENGTH + 2 * sizeof(void **)); |
51 | |
52 | PSID *sid = |
53 | (PSID *)((PBYTE)security_descriptor + SECURITY_DESCRIPTOR_MIN_LENGTH); |
54 | PACL *acl = (PACL *)((PBYTE)sid + sizeof(PSID *)); |
55 | |
56 | InitializeSecurityDescriptor(security_descriptor, |
57 | SECURITY_DESCRIPTOR_REVISION); |
58 | |
59 | SID_IDENTIFIER_AUTHORITY sid_identifier_auth = SECURITY_WORLD_SID_AUTHORITY; |
60 | AllocateAndInitializeSid(&sid_identifier_auth, 1, SECURITY_WORLD_RID, 0, 0, 0, |
61 | 0, 0, 0, 0, sid); |
62 | |
63 | EXPLICIT_ACCESS explicit_access; |
64 | ZeroMemory(&explicit_access, sizeof(EXPLICIT_ACCESS)); |
65 | explicit_access.grfAccessPermissions = |
66 | STANDARD_RIGHTS_ALL | SPECIFIC_RIGHTS_ALL; |
67 | explicit_access.grfAccessMode = SET_ACCESS; |
68 | explicit_access.grfInheritance = INHERIT_ONLY; |
69 | explicit_access.Trustee.TrusteeForm = TRUSTEE_IS_SID; |
70 | explicit_access.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; |
71 | explicit_access.Trustee.ptstrName = (LPTSTR)*sid; |
72 | |
73 | SetEntriesInAcl(1, &explicit_access, NULL, acl); |
74 | |
75 | SetSecurityDescriptorDacl(security_descriptor, TRUE, *acl, FALSE); |
76 | |
77 | security_attributes.nLength = sizeof(security_attributes); |
78 | security_attributes.lpSecurityDescriptor = security_descriptor; |
79 | security_attributes.bInheritHandle = TRUE; |
80 | } |
81 | |
82 | inline SECURITY_ATTRIBUTES *WindowsSecurityAttributes::operator&() { |
83 | return &security_attributes; |
84 | } |
85 | |
86 | inline WindowsSecurityAttributes::~WindowsSecurityAttributes() { |
87 | PSID *sid = |
88 | (PSID *)((PBYTE)security_descriptor + SECURITY_DESCRIPTOR_MIN_LENGTH); |
89 | PACL *acl = (PACL *)((PBYTE)sid + sizeof(PSID *)); |
90 | |
91 | if (*sid) { |
92 | FreeSid(*sid); |
93 | } |
94 | if (*acl) { |
95 | LocalFree(*acl); |
96 | } |
97 | free(security_descriptor); |
98 | } |
99 | |
100 | #endif //_WIN64 |
101 | |
102 | } // namespace vulkan |
103 | } // namespace taichi::lang |
104 | |