1010import git .artdeell .ArtVK ;
1111import it .unimi .dsi .fastutil .ints .Int2IntMap ;
1212import it .unimi .dsi .fastutil .ints .Int2IntMap .Entry ;
13- import it . unimi . dsi . fastutil . objects . ReferenceArrayList ;
13+
1414import java .nio .IntBuffer ;
15- import java .util .Collection ;
16- import java .util .HashSet ;
17- import java .util .List ;
1815import java .util .Locale ;
1916import java .util .Set ;
2017import net .fabricmc .api .EnvType ;
2522import org .lwjgl .glfw .GLFW ;
2623import org .lwjgl .glfw .GLFWVulkan ;
2724import org .lwjgl .system .MemoryStack ;
28- import org .lwjgl .util .vma .Vma ;
29- import org .lwjgl .util .vma .VmaAllocatorCreateInfo ;
30- import org .lwjgl .util .vma .VmaVulkanFunctions ;
3125import org .lwjgl .vulkan .*;
3226import org .lwjgl .vulkan .VkDeviceQueueCreateInfo .Buffer ;
3327
@@ -70,41 +64,18 @@ public void handleWindowCreationErrors(final GLFWErrorCapture.@Nullable Error er
7064 if (!GLFWVulkan .glfwVulkanSupported ()) {
7165 throw new BackendCreationException ("Vulkan is not supported" , BackendCreationException .Reason .GLFW_ERROR );
7266 }
73-
74- Set <String > deviceExtensions = new HashSet <>(REQUIRED_DEVICE_EXTENSIONS );
7567 Vk11Instance instance = null ;
7668 Vk11PhysicalDevice physicalDevice = null ;
7769 VkDevice device = null ;
7870 IntVMA vma = null ;
7971
8072 try {
8173 boolean renderdocAttached = "1" .equals (System .getenv ("ENABLE_VULKAN_RENDERDOC_CAPTURE" ));
82- boolean validation = "true" .equalsIgnoreCase (System .getProperty ("dogshitvk .validation" , "false" ));
74+ boolean validation = "true" .equalsIgnoreCase (System .getProperty ("artvk .validation" , "false" ));
8375 boolean useDebugLabels = debugOptions .useLabels () || renderdocAttached ;
8476 instance = new Vk11Instance (debugOptions .logLevel (), useDebugLabels , validation );
8577 physicalDevice = findPhysicalDevice (instance );
86-
87- if (physicalDevice .hasDeviceExtension ("VK_KHR_portability_subset" )) {
88- deviceExtensions .add ("VK_KHR_portability_subset" );
89- }
90-
91- if (useDebugLabels ) {
92- if (physicalDevice .hasDeviceExtension ("VK_AMD_buffer_marker" )) {
93- deviceExtensions .add ("VK_AMD_buffer_marker" );
94- } else if (physicalDevice .hasDeviceExtension ("VK_NV_device_diagnostic_checkpoints" )) {
95- deviceExtensions .add ("VK_NV_device_diagnostic_checkpoints" );
96- }
97- }
98-
99- if (physicalDevice .hasDeviceExtension ("VK_EXT_multi_draw" )) {
100- deviceExtensions .add ("VK_EXT_multi_draw" );
101- }
102-
103- if (physicalDevice .hasDeviceExtension ("VK_EXT_vertex_attribute_divisor" )) {
104- deviceExtensions .add ("VK_EXT_vertex_attribute_divisor" );
105- }
106-
107- device = createVkDevice (deviceExtensions , physicalDevice );
78+ device = createVkDevice (physicalDevice );
10879 vma = new IntVMA (device );
10980 } catch (BackendCreationException e ) {
11081 if (vma != null ) vma .close ();
@@ -119,7 +90,7 @@ public void handleWindowCreationErrors(final GLFWErrorCapture.@Nullable Error er
11990 }
12091
12192 return new GpuDevice (
122- new Vk11Device (defaultShaderSource , instance , physicalDevice , deviceExtensions , device , vma ), criticalShaderLoader
93+ new Vk11Device (defaultShaderSource , instance , physicalDevice , device , vma ), criticalShaderLoader
12394 );
12495 }
12596
@@ -156,7 +127,7 @@ private static Vk11PhysicalDevice findPhysicalDevice(final Vk11Instance instance
156127 firstDevice = currentDevice ;
157128 }
158129
159- if (deviceMeetsFeatureQueryRequirements ( currentDevice ) && isDeviceSuitable (currentDevice )) {
130+ if (isDeviceSuitable (currentDevice )) {
160131 if (selectedDevice == null ) {
161132 selectedDevice = currentDevice ;
162133 } else if (isDeviceDiscrete (currentDevice ) && !isDeviceDiscrete (selectedDevice )) {
@@ -174,32 +145,19 @@ private static Vk11PhysicalDevice findPhysicalDevice(final Vk11Instance instance
174145 }
175146
176147 if (selectedDevice == null ) {
177- throwForMissingRequirements (firstDevice );
178- assert false ;
148+ throw new BackendCreationException ("No compatible devices found" , BackendCreationException .Reason .VULKAN_NO_DEVICE );
179149 }
180150
181- return new Vk11PhysicalDevice (selectedDevice );
182- }
183-
184- private static boolean deviceMeetsFeatureQueryRequirements (final VkPhysicalDevice vkPhysicalDevice ) {
185- try (MemoryStack stack = MemoryStack .stackPush ()) {
186- VkPhysicalDeviceProperties properties = VkPhysicalDeviceProperties .calloc (stack );
187- VK10 .vkGetPhysicalDeviceProperties (vkPhysicalDevice , properties );
188- return properties .apiVersion () >= VK11 .VK_API_VERSION_1_1 ;
189- }
151+ return new Vk11PhysicalDevice (selectedDevice , instance .propertiesMode );
190152 }
191153
192154 private static boolean isDeviceSuitable (final VkPhysicalDevice vkPhysicalDevice ) throws BackendCreationException {
193155 try (
194- Vk11PhysicalDevice physicalDevice = new Vk11PhysicalDevice (vkPhysicalDevice );
156+ Vk11PhysicalDevice physicalDevice = new Vk11PhysicalDevice (vkPhysicalDevice , Vk11PhysicalDevice . PROPERTIES_VK11 );
195157 ) {
196- String deviceName = physicalDevice .deviceName ();
158+ String deviceName = physicalDevice .properties (). deviceName ();
197159 Set <String > missingExtensions = physicalDevice .getMissingExtensions (REQUIRED_DEVICE_EXTENSIONS );
198160 boolean isSuitableDevice = true ;
199- if (physicalDevice .vkPhysicalDeviceProperties ().apiVersion () < VK11 .VK_API_VERSION_1_1 ) {
200- ArtVK .LOGGER .warn ("Device [{}] does not support Vulkan 1.1" , deviceName );
201- isSuitableDevice = false ;
202- }
203161
204162 if (physicalDevice .graphicsQueueFamilyAndIndex () == null ) {
205163 ArtVK .LOGGER .warn ("Device [{}] does not have a graphics queue" , deviceName );
@@ -235,61 +193,8 @@ private static String getDeviceName(final VkPhysicalDevice vkPhysicalDevice) {
235193 }
236194 }
237195
238- private static void throwForMissingRequirements (final VkPhysicalDevice vkPhysicalDevice ) throws BackendCreationException {
239- List <String > missingCapabilities = new ReferenceArrayList <>();
240- BackendCreationException .Reason mostProminentReason = BackendCreationException .Reason .OTHER ;
241- if (!deviceMeetsFeatureQueryRequirements (vkPhysicalDevice )) {
242- throw new BackendCreationException ("Device missing capabilities" , BackendCreationException .Reason .VULKAN_DEVICE_VERSION_TOO_LOW , List .of ("VULKAN_CORE_1_1" ));
243- }
244-
245- try (
246- Vk11PhysicalDevice physicalDevice = new Vk11PhysicalDevice (vkPhysicalDevice );
247- MemoryStack stack = MemoryStack .stackPush ();
248- ) {
249- VkPhysicalDeviceFeatures2 deviceFeatures = VkPhysicalDeviceFeatures2 .calloc (stack ).sType$Default ();
250- VK11 .vkGetPhysicalDeviceFeatures2 (vkPhysicalDevice , deviceFeatures );
251-
252- Set <String > missingExtensions = physicalDevice .getMissingExtensions (REQUIRED_DEVICE_EXTENSIONS );
253- if (!missingExtensions .isEmpty ()) {
254- mostProminentReason = BackendCreationException .Reason .VULKAN_MISSING_EXTENSION ;
255- missingCapabilities .addAll (missingExtensions );
256- }
257-
258- if (physicalDevice .graphicsQueueFamilyAndIndex () == null ) {
259- mostProminentReason = BackendCreationException .Reason .VULKAN_NO_GRAPHICS_QUEUE ;
260- missingCapabilities .add ("COMBINED_GRAPHICS_COMPUTE_PRESENT_QUEUE" );
261- }
262-
263- if (physicalDevice .vkPhysicalDeviceProperties ().apiVersion () < VK11 .VK_API_VERSION_1_1 ) {
264- mostProminentReason = BackendCreationException .Reason .VULKAN_DEVICE_VERSION_TOO_LOW ;
265- missingCapabilities .add ("VULKAN_CORE_1_1" );
266- }
267- }
268-
269- throw new BackendCreationException ("Device missing capabilities" , mostProminentReason , missingCapabilities );
270- }
271-
272- private static VkDevice createVkDevice (
273- final Collection <String > deviceExtensions , final Vk11PhysicalDevice physicalDevice
274- ) throws BackendCreationException {
196+ private static VkDevice createVkDevice (final Vk11PhysicalDevice physicalDevice ) throws BackendCreationException {
275197 try (MemoryStack stack = MemoryStack .stackPush ()) {
276- VkPhysicalDeviceFeatures2 availableFeatures = physicalDevice .vkPhysicalDeviceFeatures ();
277-
278- VkPhysicalDeviceFeatures2 deviceFeatures = VkPhysicalDeviceFeatures2 .calloc (stack ).sType$Default ();
279- // Enable required VK10 features
280- deviceFeatures .features ().multiDrawIndirect (availableFeatures .features ().multiDrawIndirect ());
281- deviceFeatures .features ().fillModeNonSolid (availableFeatures .features ().fillModeNonSolid ());
282- deviceFeatures .features ().samplerAnisotropy (availableFeatures .features ().samplerAnisotropy ());
283-
284- // Enable VK10 shaderDrawParameters via pNext chain
285- VkPhysicalDeviceVulkan11Features vk11Features = VkPhysicalDeviceVulkan11Features .calloc (stack ).sType$Default ();
286- vk11Features .shaderDrawParameters (true );
287- deviceFeatures .pNext (vk11Features .address ());
288-
289- // Enable VK10 vertexAttributeDivisor via pNext chain (EXT extension)
290- VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT vertexDivisorFeatures = VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT .calloc (stack ).sType$Default ();
291- vertexDivisorFeatures .vertexAttributeInstanceRateDivisor (true );
292- vk11Features .pNext (vertexDivisorFeatures .address ());
293198
294199 Int2IntMap queuesToCreate = physicalDevice .queueFamilyCreateInfoMap ();
295200 Buffer queueCreationInfo = VkDeviceQueueCreateInfo .calloc (queuesToCreate .size (), stack );
@@ -302,17 +207,12 @@ private static VkDevice createVkDevice(
302207 }
303208
304209 queueCreationInfo .position (0 );
305- PointerBuffer enabledExtensionsBuffer = stack .callocPointer (deviceExtensions .size ());
306210
307- for (String name : deviceExtensions ) {
308- enabledExtensionsBuffer .put (stack .UTF8 (name ));
309- }
310-
311- enabledExtensionsBuffer .flip ();
312211 VkDeviceCreateInfo deviceCreateInfo = VkDeviceCreateInfo .calloc (stack ).sType$Default ();
313- deviceCreateInfo .pNext (deviceFeatures .address ());
314212 deviceCreateInfo .pQueueCreateInfos (queueCreationInfo );
315- deviceCreateInfo .ppEnabledExtensionNames (enabledExtensionsBuffer );
213+
214+ physicalDevice .features ().addFeaturesAndExtensions (stack , physicalDevice , deviceCreateInfo );
215+
316216 PointerBuffer pointer = stack .callocPointer (1 );
317217 Vk11Utils .throwIfFailure (
318218 VK10 .vkCreateDevice (physicalDevice .vkPhysicalDevice (), deviceCreateInfo , null , pointer ),
0 commit comments