Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "DeviceSelection.h"
21 :
22 : #include "ps/CLogger.h"
23 : #include "renderer/backend/vulkan/Utilities.h"
24 : #include "scriptinterface/Object.h"
25 : #include "scriptinterface/Request.h"
26 :
27 : #include <algorithm>
28 : #include <iterator>
29 : #include <js/RootingAPI.h>
30 : #include <js/Value.h>
31 : #include <limits>
32 : #include <span>
33 : #include <string>
34 : #include <type_traits>
35 : #include <vector>
36 :
37 : namespace Renderer
38 : {
39 :
40 : namespace Backend
41 : {
42 :
43 : namespace Vulkan
44 : {
45 :
46 : namespace
47 : {
48 :
49 0 : std::vector<std::string> GetPhysicalDeviceExtensions(VkPhysicalDevice device)
50 : {
51 0 : uint32_t extensionCount = 0;
52 0 : ENSURE_VK_SUCCESS(vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, nullptr));
53 0 : std::vector<VkExtensionProperties> extensions(extensionCount);
54 0 : ENSURE_VK_SUCCESS(vkEnumerateDeviceExtensionProperties(device, nullptr, &extensionCount, extensions.data()));
55 :
56 0 : std::vector<std::string> availableExtensions;
57 0 : availableExtensions.reserve(extensions.size());
58 0 : for (const VkExtensionProperties& extension : extensions)
59 0 : availableExtensions.emplace_back(extension.extensionName);
60 0 : std::sort(availableExtensions.begin(), availableExtensions.end());
61 0 : return availableExtensions;
62 : }
63 :
64 0 : uint32_t GetDeviceTypeScore(const VkPhysicalDeviceType deviceType)
65 : {
66 0 : uint32_t score = 0;
67 : // We prefer discrete GPU over integrated, and integrated over others.
68 0 : switch (deviceType)
69 : {
70 : case VK_PHYSICAL_DEVICE_TYPE_OTHER:
71 : score = 1;
72 : break;
73 : case VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU:
74 : score = 4;
75 : break;
76 : case VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU:
77 : score = 5;
78 : break;
79 : case VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU:
80 : score = 3;
81 : break;
82 : case VK_PHYSICAL_DEVICE_TYPE_CPU:
83 : score = 2;
84 : break;
85 : default:
86 : break;
87 : }
88 0 : return score;
89 : }
90 :
91 : VkDeviceSize GetDeviceTotalMemory(
92 : const VkPhysicalDeviceMemoryProperties& memoryProperties)
93 : {
94 : VkDeviceSize totalMemory = 0;
95 0 : for (uint32_t heapIndex = 0; heapIndex < memoryProperties.memoryHeapCount; ++heapIndex)
96 0 : if (memoryProperties.memoryHeaps[heapIndex].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
97 0 : totalMemory += memoryProperties.memoryHeaps[heapIndex].size;
98 0 : return totalMemory;
99 : }
100 :
101 0 : VkDeviceSize GetHostTotalMemory(
102 : const VkPhysicalDeviceMemoryProperties& memoryProperties)
103 : {
104 : VkDeviceSize totalMemory = 0;
105 0 : for (uint32_t heapIndex = 0; heapIndex < memoryProperties.memoryHeapCount; ++heapIndex)
106 0 : if ((memoryProperties.memoryHeaps[heapIndex].flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) == 0)
107 0 : totalMemory += memoryProperties.memoryHeaps[heapIndex].size;
108 0 : return totalMemory;
109 : }
110 :
111 : // We don't support some types in JS, so wrap them to have in the report.
112 : template<typename T, typename Tag = void>
113 : struct ReportFormatHelper
114 : {
115 : std::string operator()(const T&) const { return "unknown"; }
116 : };
117 :
118 : template<typename T>
119 : struct ReportFormatHelper<T, typename std::enable_if_t<std::is_floating_point_v<T>>>
120 : {
121 0 : float operator()(const T& value) const { return static_cast<float>(value); }
122 : };
123 :
124 : template<typename T>
125 : struct ReportFormatHelper<T, typename std::enable_if_t<std::is_integral_v<T>>>
126 : {
127 : static constexpr bool IsSigned = std::is_signed_v<T>;
128 : using ResultType = std::conditional_t<IsSigned, int32_t, uint32_t>;
129 0 : uint32_t operator()(const T& value) const
130 : {
131 0 : if (value > std::numeric_limits<ResultType>::max())
132 : return std::numeric_limits<ResultType>::max();
133 : if constexpr (IsSigned)
134 : {
135 : if (value < std::numeric_limits<ResultType>::min())
136 : return std::numeric_limits<ResultType>::min();
137 : }
138 0 : return static_cast<ResultType>(value);
139 : }
140 : };
141 :
142 : template<typename T>
143 : struct ReportFormatHelper<T, typename std::enable_if_t<std::is_enum_v<T>>>
144 : {
145 : using HelperType = ReportFormatHelper<std::underlying_type_t<T>>;
146 : using ResultType = std::invoke_result_t<HelperType, std::underlying_type_t<T>>;
147 : ResultType operator()(const T& value) const
148 : {
149 : HelperType helper{};
150 : return helper(value);
151 : }
152 : };
153 :
154 : template<typename T>
155 : struct ReportFormatHelper<T, typename std::enable_if_t<std::is_array_v<T>>>
156 : {
157 : using HelperType = ReportFormatHelper<std::remove_extent_t<T>>;
158 : using ElementType = std::invoke_result_t<HelperType, std::remove_extent_t<T>>;
159 0 : std::vector<ElementType> operator()(const T& value) const
160 : {
161 0 : std::vector<ElementType> arr;
162 0 : arr.reserve(std::size(value));
163 : HelperType helper{};
164 0 : for (const auto& element : value)
165 0 : arr.emplace_back(helper(element));
166 0 : return arr;
167 0 : }
168 : };
169 :
170 0 : SAvailablePhysicalDevice MakeAvailablePhysicalDevice(
171 : const uint32_t physicalDeviceIndex, VkPhysicalDevice physicalDevice,
172 : VkSurfaceKHR surface, const std::vector<const char*>& requiredDeviceExtensions)
173 : {
174 0 : SAvailablePhysicalDevice availablePhysicalDevice{};
175 :
176 0 : availablePhysicalDevice.index = physicalDeviceIndex;
177 0 : availablePhysicalDevice.device = physicalDevice;
178 0 : availablePhysicalDevice.hasOutputToSurfaceSupport = false;
179 0 : availablePhysicalDevice.extensions = GetPhysicalDeviceExtensions(availablePhysicalDevice.device);
180 0 : auto hasExtension = [&extensions = availablePhysicalDevice.extensions](const char* name) -> bool
181 : {
182 0 : return std::find(extensions.begin(), extensions.end(), name) != extensions.end();
183 0 : };
184 :
185 0 : availablePhysicalDevice.hasRequiredExtensions =
186 0 : std::all_of(requiredDeviceExtensions.begin(), requiredDeviceExtensions.end(), hasExtension);
187 :
188 0 : vkGetPhysicalDeviceMemoryProperties(
189 : availablePhysicalDevice.device, &availablePhysicalDevice.memoryProperties);
190 0 : availablePhysicalDevice.deviceTotalMemory =
191 0 : GetDeviceTotalMemory(availablePhysicalDevice.memoryProperties);
192 0 : availablePhysicalDevice.hostTotalMemory =
193 0 : GetHostTotalMemory(availablePhysicalDevice.memoryProperties);
194 :
195 0 : if (hasExtension(VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME))
196 : {
197 0 : VkPhysicalDeviceDescriptorIndexingPropertiesEXT descriptorIndexingProperties{};
198 0 : descriptorIndexingProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_PROPERTIES_EXT;
199 :
200 0 : VkPhysicalDeviceProperties2 devicesProperties2{};
201 0 : devicesProperties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
202 0 : devicesProperties2.pNext = &descriptorIndexingProperties;
203 0 : vkGetPhysicalDeviceProperties2(availablePhysicalDevice.device, &devicesProperties2);
204 0 : availablePhysicalDevice.properties = devicesProperties2.properties;
205 0 : availablePhysicalDevice.descriptorIndexingProperties = descriptorIndexingProperties;
206 :
207 0 : VkPhysicalDeviceDescriptorIndexingFeaturesEXT descriptorIndexingFeatures{};
208 0 : descriptorIndexingFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DESCRIPTOR_INDEXING_FEATURES_EXT;
209 :
210 0 : VkPhysicalDeviceFeatures2 deviceFeatures2{};
211 0 : deviceFeatures2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
212 0 : deviceFeatures2.pNext = &descriptorIndexingFeatures;
213 0 : vkGetPhysicalDeviceFeatures2(availablePhysicalDevice.device, &deviceFeatures2);
214 0 : availablePhysicalDevice.features = deviceFeatures2.features;
215 0 : availablePhysicalDevice.descriptorIndexingFeatures = descriptorIndexingFeatures;
216 : }
217 : else
218 : {
219 0 : vkGetPhysicalDeviceProperties(availablePhysicalDevice.device, &availablePhysicalDevice.properties);
220 0 : vkGetPhysicalDeviceFeatures(availablePhysicalDevice.device, &availablePhysicalDevice.features);
221 : }
222 :
223 0 : uint32_t queueFamilyCount = 0;
224 0 : vkGetPhysicalDeviceQueueFamilyProperties(availablePhysicalDevice.device, &queueFamilyCount, nullptr);
225 0 : availablePhysicalDevice.queueFamilies.resize(queueFamilyCount);
226 0 : vkGetPhysicalDeviceQueueFamilyProperties(
227 : availablePhysicalDevice.device, &queueFamilyCount, availablePhysicalDevice.queueFamilies.data());
228 :
229 0 : availablePhysicalDevice.graphicsQueueFamilyIndex = availablePhysicalDevice.queueFamilies.size();
230 0 : availablePhysicalDevice.presentQueueFamilyIndex = availablePhysicalDevice.queueFamilies.size();
231 0 : if (surface != VK_NULL_HANDLE)
232 : {
233 0 : for (size_t familyIdx = 0; familyIdx < availablePhysicalDevice.queueFamilies.size(); ++familyIdx)
234 : {
235 0 : const VkQueueFamilyProperties& queueFamily = availablePhysicalDevice.queueFamilies[familyIdx];
236 0 : VkBool32 hasOutputToSurfaceSupport = false;
237 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfaceSupportKHR(
238 : availablePhysicalDevice.device, familyIdx, surface, &hasOutputToSurfaceSupport));
239 0 : if ((queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) && hasOutputToSurfaceSupport)
240 : {
241 0 : availablePhysicalDevice.hasOutputToSurfaceSupport = hasOutputToSurfaceSupport;
242 0 : availablePhysicalDevice.graphicsQueueFamilyIndex = familyIdx;
243 0 : availablePhysicalDevice.presentQueueFamilyIndex = familyIdx;
244 0 : break;
245 : }
246 : }
247 : }
248 :
249 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(
250 : availablePhysicalDevice.device, surface, &availablePhysicalDevice.surfaceCapabilities));
251 :
252 0 : uint32_t surfaceFormatCount = 0;
253 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfaceFormatsKHR(
254 : availablePhysicalDevice.device, surface, &surfaceFormatCount, nullptr));
255 0 : if (surfaceFormatCount > 0)
256 : {
257 0 : availablePhysicalDevice.surfaceFormats.resize(surfaceFormatCount);
258 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfaceFormatsKHR(
259 : availablePhysicalDevice.device, surface, &surfaceFormatCount, availablePhysicalDevice.surfaceFormats.data()));
260 : }
261 :
262 0 : uint32_t presentModeCount = 0;
263 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfacePresentModesKHR(
264 : availablePhysicalDevice.device, surface, &presentModeCount, nullptr));
265 0 : if (presentModeCount > 0)
266 : {
267 0 : availablePhysicalDevice.presentModes.resize(presentModeCount);
268 0 : ENSURE_VK_SUCCESS(vkGetPhysicalDeviceSurfacePresentModesKHR(
269 : availablePhysicalDevice.device, surface, &presentModeCount, availablePhysicalDevice.presentModes.data()));
270 : }
271 :
272 0 : return availablePhysicalDevice;
273 0 : }
274 :
275 : } // anonymous namespace
276 :
277 0 : std::vector<SAvailablePhysicalDevice> GetAvailablePhysicalDevices(
278 : VkInstance instance, VkSurfaceKHR surface,
279 : const std::vector<const char*>& requiredDeviceExtensions)
280 : {
281 0 : uint32_t physicalDeviceCount = 0;
282 0 : ENSURE_VK_SUCCESS(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, nullptr));
283 0 : if (physicalDeviceCount == 0)
284 0 : return {};
285 :
286 0 : std::vector<SAvailablePhysicalDevice> availablePhysicalDevices;
287 0 : availablePhysicalDevices.reserve(physicalDeviceCount);
288 :
289 0 : std::vector<VkPhysicalDevice> physicalDevices(physicalDeviceCount);
290 0 : ENSURE_VK_SUCCESS(vkEnumeratePhysicalDevices(instance, &physicalDeviceCount, physicalDevices.data()));
291 0 : for (uint32_t physicalDeviceIndex = 0; physicalDeviceIndex < physicalDeviceCount; ++physicalDeviceIndex)
292 : {
293 0 : availablePhysicalDevices.emplace_back(MakeAvailablePhysicalDevice(
294 0 : physicalDeviceIndex, physicalDevices[physicalDeviceIndex], surface, requiredDeviceExtensions));
295 : }
296 :
297 0 : return availablePhysicalDevices;
298 : }
299 :
300 0 : bool IsPhysicalDeviceUnsupported(const SAvailablePhysicalDevice& device)
301 : {
302 0 : if (!device.hasRequiredExtensions)
303 : return true;
304 : // We can't draw something without graphics queue. And currently we don't
305 : // support separate queues for graphics and present.
306 0 : if (device.graphicsQueueFamilyIndex != device.presentQueueFamilyIndex)
307 : return true;
308 0 : if (device.graphicsQueueFamilyIndex == device.queueFamilies.size())
309 : return true;
310 0 : if (!device.hasOutputToSurfaceSupport)
311 : return true;
312 0 : if (device.properties.limits.maxBoundDescriptorSets < 4)
313 : return true;
314 : // It's guaranteed to have sRGB but we don't support it yet.
315 0 : return std::none_of(device.surfaceFormats.begin(), device.surfaceFormats.end(), IsSurfaceFormatSupported);
316 : }
317 :
318 0 : bool ComparePhysicalDevices(
319 : const SAvailablePhysicalDevice& device1,
320 : const SAvailablePhysicalDevice& device2)
321 : {
322 0 : const uint32_t deviceTypeScore1 = GetDeviceTypeScore(device1.properties.deviceType);
323 0 : const uint32_t deviceTypeScore2 = GetDeviceTypeScore(device2.properties.deviceType);
324 0 : if (deviceTypeScore1 != deviceTypeScore2)
325 0 : return deviceTypeScore1 > deviceTypeScore2;
326 0 : return device1.index < device2.index;
327 : }
328 :
329 0 : bool IsSurfaceFormatSupported(
330 : const VkSurfaceFormatKHR& surfaceFormat)
331 : {
332 0 : return
333 0 : surfaceFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR &&
334 0 : (surfaceFormat.format == VK_FORMAT_R8G8B8A8_UNORM ||
335 0 : surfaceFormat.format == VK_FORMAT_B8G8R8A8_UNORM);
336 : }
337 :
338 0 : void ReportAvailablePhysicalDevice(const SAvailablePhysicalDevice& device,
339 : const Script::Request& rq, JS::HandleValue settings)
340 : {
341 0 : Script::SetProperty(rq, settings, "name", device.properties.deviceName);
342 0 : Script::SetProperty(rq, settings, "version",
343 0 : std::to_string(VK_API_VERSION_VARIANT(device.properties.apiVersion)) +
344 0 : "." + std::to_string(VK_API_VERSION_MAJOR(device.properties.apiVersion)) +
345 0 : "." + std::to_string(VK_API_VERSION_MINOR(device.properties.apiVersion)) +
346 0 : "." + std::to_string(VK_API_VERSION_PATCH(device.properties.apiVersion)));
347 0 : Script::SetProperty(rq, settings, "apiVersion", device.properties.apiVersion);
348 0 : Script::SetProperty(rq, settings, "driverVersion", device.properties.driverVersion);
349 0 : Script::SetProperty(rq, settings, "vendorID", device.properties.vendorID);
350 0 : Script::SetProperty(rq, settings, "deviceID", device.properties.deviceID);
351 0 : Script::SetProperty(rq, settings, "deviceType", static_cast<int32_t>(device.properties.deviceType));
352 0 : Script::SetProperty(rq, settings, "index", device.index);
353 :
354 0 : JS::RootedValue memory(rq.cx);
355 0 : Script::CreateObject(rq, &memory);
356 :
357 0 : JS::RootedValueVector memoryTypes{rq.cx};
358 0 : if (!memoryTypes.reserve(device.memoryProperties.memoryTypeCount))
359 0 : throw std::runtime_error{"Reserve failed"};
360 0 : for (const VkMemoryType& type : std::span{device.memoryProperties.memoryTypes,
361 0 : device.memoryProperties.memoryTypeCount})
362 : {
363 0 : JS::RootedValue memoryType(rq.cx);
364 0 : Script::CreateObject(rq, &memoryType);
365 0 : Script::SetProperty(rq, memoryType, "propertyFlags", static_cast<uint32_t>(type.propertyFlags));
366 0 : Script::SetProperty(rq, memoryType, "heapIndex", type.heapIndex);
367 0 : if (!memoryTypes.append(memoryType))
368 0 : throw std::runtime_error{"Append failed"};
369 0 : }
370 0 : JS::RootedValueVector memoryHeaps{rq.cx};
371 0 : if (!memoryHeaps.reserve(device.memoryProperties.memoryHeapCount))
372 0 : throw std::runtime_error{"Reserve failed"};
373 0 : for (const VkMemoryHeap& heap : std::span{device.memoryProperties.memoryHeaps,
374 0 : device.memoryProperties.memoryHeapCount})
375 : {
376 0 : JS::RootedValue memoryHeap(rq.cx);
377 0 : Script::CreateObject(rq, &memoryHeap);
378 : // We can't serialize uint64_t in JS, so put data in KiB.
379 0 : Script::SetProperty(rq, memoryHeap, "size", static_cast<uint32_t>(heap.size / 1024));
380 0 : Script::SetProperty(rq, memoryHeap, "flags", static_cast<uint32_t>(heap.flags));
381 0 : if (!memoryHeaps.append(memoryHeap))
382 0 : throw std::runtime_error{"Append failed"};
383 0 : }
384 :
385 0 : Script::SetProperty(rq, memory, "types",
386 0 : JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, memoryTypes))});
387 0 : Script::SetProperty(rq, memory, "heaps",
388 0 : JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, memoryHeaps))});
389 0 : Script::SetProperty(rq, settings, "memory", memory);
390 :
391 0 : JS::RootedValue constants(rq.cx);
392 0 : Script::CreateObject(rq, &constants);
393 :
394 0 : JS::RootedValue limitsConstants(rq.cx);
395 0 : Script::CreateObject(rq, &limitsConstants);
396 : #define REPORT_LIMITS_CONSTANT(NAME) \
397 : do \
398 : { \
399 : const ReportFormatHelper<decltype(device.properties.limits.NAME)> helper{}; \
400 : Script::SetProperty(rq, limitsConstants, #NAME, helper(device.properties.limits.NAME)); \
401 : } while (0)
402 0 : REPORT_LIMITS_CONSTANT(maxImageDimension1D);
403 0 : REPORT_LIMITS_CONSTANT(maxImageDimension2D);
404 0 : REPORT_LIMITS_CONSTANT(maxImageDimension3D);
405 0 : REPORT_LIMITS_CONSTANT(maxImageDimensionCube);
406 0 : REPORT_LIMITS_CONSTANT(maxImageArrayLayers);
407 0 : REPORT_LIMITS_CONSTANT(maxUniformBufferRange);
408 0 : REPORT_LIMITS_CONSTANT(maxStorageBufferRange);
409 0 : REPORT_LIMITS_CONSTANT(maxPushConstantsSize);
410 0 : REPORT_LIMITS_CONSTANT(maxMemoryAllocationCount);
411 0 : REPORT_LIMITS_CONSTANT(maxSamplerAllocationCount);
412 0 : REPORT_LIMITS_CONSTANT(bufferImageGranularity);
413 0 : REPORT_LIMITS_CONSTANT(maxBoundDescriptorSets);
414 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorSamplers);
415 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorUniformBuffers);
416 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorStorageBuffers);
417 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorSampledImages);
418 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorStorageImages);
419 0 : REPORT_LIMITS_CONSTANT(maxPerStageDescriptorInputAttachments);
420 0 : REPORT_LIMITS_CONSTANT(maxPerStageResources);
421 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetSamplers);
422 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetUniformBuffers);
423 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetUniformBuffersDynamic);
424 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetStorageBuffers);
425 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetStorageBuffersDynamic);
426 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetSampledImages);
427 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetStorageImages);
428 0 : REPORT_LIMITS_CONSTANT(maxDescriptorSetInputAttachments);
429 0 : REPORT_LIMITS_CONSTANT(maxVertexInputAttributes);
430 0 : REPORT_LIMITS_CONSTANT(maxVertexInputBindings);
431 0 : REPORT_LIMITS_CONSTANT(maxVertexInputAttributeOffset);
432 0 : REPORT_LIMITS_CONSTANT(maxVertexInputBindingStride);
433 0 : REPORT_LIMITS_CONSTANT(maxComputeSharedMemorySize);
434 0 : REPORT_LIMITS_CONSTANT(maxComputeWorkGroupCount);
435 0 : REPORT_LIMITS_CONSTANT(maxComputeWorkGroupInvocations);
436 0 : REPORT_LIMITS_CONSTANT(maxComputeWorkGroupSize);
437 0 : REPORT_LIMITS_CONSTANT(maxDrawIndexedIndexValue);
438 0 : REPORT_LIMITS_CONSTANT(maxSamplerLodBias);
439 0 : REPORT_LIMITS_CONSTANT(maxSamplerAnisotropy);
440 0 : REPORT_LIMITS_CONSTANT(minMemoryMapAlignment);
441 0 : REPORT_LIMITS_CONSTANT(minTexelBufferOffsetAlignment);
442 0 : REPORT_LIMITS_CONSTANT(minUniformBufferOffsetAlignment);
443 0 : REPORT_LIMITS_CONSTANT(minStorageBufferOffsetAlignment);
444 0 : REPORT_LIMITS_CONSTANT(maxFramebufferWidth);
445 0 : REPORT_LIMITS_CONSTANT(maxFramebufferHeight);
446 0 : REPORT_LIMITS_CONSTANT(maxFramebufferLayers);
447 0 : REPORT_LIMITS_CONSTANT(framebufferColorSampleCounts);
448 0 : REPORT_LIMITS_CONSTANT(framebufferDepthSampleCounts);
449 0 : REPORT_LIMITS_CONSTANT(framebufferStencilSampleCounts);
450 0 : REPORT_LIMITS_CONSTANT(framebufferNoAttachmentsSampleCounts);
451 0 : REPORT_LIMITS_CONSTANT(maxColorAttachments);
452 0 : REPORT_LIMITS_CONSTANT(sampledImageColorSampleCounts);
453 0 : REPORT_LIMITS_CONSTANT(sampledImageDepthSampleCounts);
454 0 : REPORT_LIMITS_CONSTANT(sampledImageStencilSampleCounts);
455 0 : REPORT_LIMITS_CONSTANT(storageImageSampleCounts);
456 0 : REPORT_LIMITS_CONSTANT(timestampComputeAndGraphics);
457 0 : REPORT_LIMITS_CONSTANT(timestampPeriod);
458 0 : REPORT_LIMITS_CONSTANT(optimalBufferCopyOffsetAlignment);
459 0 : REPORT_LIMITS_CONSTANT(optimalBufferCopyRowPitchAlignment);
460 : #undef REPORT_LIMITS_CONSTANT
461 0 : Script::SetProperty(rq, constants, "limits", limitsConstants);
462 :
463 0 : JS::RootedValue descriptorIndexingConstants(rq.cx);
464 0 : Script::CreateObject(rq, &descriptorIndexingConstants);
465 : #define REPORT_DESCRIPTOR_INDEXING_CONSTANT(NAME) \
466 : do \
467 : { \
468 : const ReportFormatHelper<decltype(device.descriptorIndexingProperties.NAME)> helper{}; \
469 : Script::SetProperty(rq, descriptorIndexingConstants, #NAME, helper(device.descriptorIndexingProperties.NAME)); \
470 : } while (0)
471 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxUpdateAfterBindDescriptorsInAllPools);
472 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(shaderSampledImageArrayNonUniformIndexingNative);
473 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxPerStageDescriptorUpdateAfterBindSamplers);
474 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxPerStageDescriptorUpdateAfterBindSampledImages);
475 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxPerStageDescriptorUpdateAfterBindUniformBuffers);
476 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxPerStageUpdateAfterBindResources);
477 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxDescriptorSetUpdateAfterBindSamplers);
478 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxDescriptorSetUpdateAfterBindSampledImages);
479 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxDescriptorSetUpdateAfterBindUniformBuffers);
480 0 : REPORT_DESCRIPTOR_INDEXING_CONSTANT(maxDescriptorSetUpdateAfterBindUniformBuffersDynamic);
481 : #undef REPORT_DESCRIPTOR_INDEXING_CONSTANT
482 0 : Script::SetProperty(rq, constants, "descriptor_indexing", descriptorIndexingConstants);
483 :
484 0 : Script::SetProperty(rq, settings, "constants", constants);
485 :
486 0 : JS::RootedValue features(rq.cx);
487 0 : Script::CreateObject(rq, &features);
488 : #define REPORT_FEATURE(NAME) \
489 : Script::SetProperty(rq, features, #NAME, static_cast<bool>(device.features.NAME));
490 0 : REPORT_FEATURE(robustBufferAccess);
491 0 : REPORT_FEATURE(fullDrawIndexUint32);
492 0 : REPORT_FEATURE(imageCubeArray);
493 0 : REPORT_FEATURE(geometryShader);
494 0 : REPORT_FEATURE(tessellationShader);
495 0 : REPORT_FEATURE(logicOp);
496 0 : REPORT_FEATURE(multiDrawIndirect);
497 0 : REPORT_FEATURE(depthClamp);
498 0 : REPORT_FEATURE(depthBiasClamp);
499 0 : REPORT_FEATURE(fillModeNonSolid);
500 0 : REPORT_FEATURE(samplerAnisotropy);
501 0 : REPORT_FEATURE(textureCompressionETC2);
502 0 : REPORT_FEATURE(textureCompressionASTC_LDR);
503 0 : REPORT_FEATURE(textureCompressionBC);
504 0 : REPORT_FEATURE(pipelineStatisticsQuery);
505 0 : REPORT_FEATURE(shaderUniformBufferArrayDynamicIndexing);
506 0 : REPORT_FEATURE(shaderSampledImageArrayDynamicIndexing);
507 : #undef REPORT_FEATURE
508 :
509 : #define REPORT_DESCRIPTOR_INDEXING_FEATURE(NAME) \
510 : Script::SetProperty(rq, features, #NAME, static_cast<bool>(device.descriptorIndexingFeatures.NAME));
511 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(shaderSampledImageArrayNonUniformIndexing);
512 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingUniformBufferUpdateAfterBind);
513 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingSampledImageUpdateAfterBind);
514 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingPartiallyBound);
515 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingUpdateUnusedWhilePending);
516 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingPartiallyBound);
517 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(descriptorBindingVariableDescriptorCount);
518 0 : REPORT_DESCRIPTOR_INDEXING_FEATURE(runtimeDescriptorArray);
519 : #undef REPORT_DESCRIPTOR_INDEXING_FEATURE
520 :
521 0 : Script::SetProperty(rq, settings, "features", features);
522 :
523 0 : JS::RootedValueVector presentModes{rq.cx};
524 0 : if (!presentModes.reserve(device.presentModes.size()))
525 0 : throw std::runtime_error{"Reserve failed"};
526 0 : for (const VkPresentModeKHR& mode : device.presentModes)
527 : {
528 0 : if (!presentModes.append(JS::NumberValue(static_cast<uint32_t>(mode))))
529 0 : throw std::runtime_error{"Append failed"};
530 : }
531 0 : Script::SetProperty(rq, settings, "present_modes",
532 0 : JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, presentModes))});
533 :
534 0 : JS::RootedValueVector surfaceFormats{rq.cx};
535 0 : if (!surfaceFormats.reserve(device.surfaceFormats.size()))
536 0 : throw std::runtime_error{"Reserve failed"};
537 0 : for (const VkSurfaceFormatKHR& format : device.surfaceFormats)
538 : {
539 0 : JS::RootedValue surfaceFormat(rq.cx);
540 0 : Script::CreateObject(rq, &surfaceFormat);
541 0 : Script::SetProperty(
542 0 : rq, surfaceFormat, "format", static_cast<uint32_t>(format.format));
543 0 : Script::SetProperty(
544 0 : rq, surfaceFormat, "color_space", static_cast<uint32_t>(format.colorSpace));
545 0 : if (!surfaceFormats.append(surfaceFormat))
546 0 : throw std::runtime_error{"Append failed"};
547 0 : }
548 0 : Script::SetProperty(rq, settings, "surface_formats",
549 0 : JS::RootedValue{rq.cx, JS::ObjectValue(*JS::NewArrayObject(rq.cx, surfaceFormats))});
550 :
551 0 : JS::RootedValue surfaceCapabilities(rq.cx);
552 0 : Script::CreateObject(rq, &surfaceCapabilities);
553 : #define REPORT_SURFACE_CAPABILITIES_CONSTANT(NAME) \
554 : do \
555 : { \
556 : const ReportFormatHelper<decltype(device.surfaceCapabilities.NAME)> helper{}; \
557 : Script::SetProperty(rq, surfaceCapabilities, #NAME, helper(device.surfaceCapabilities.NAME)); \
558 : } while (0)
559 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(minImageCount);
560 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(maxImageCount);
561 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(maxImageArrayLayers);
562 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(supportedTransforms);
563 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(supportedCompositeAlpha);
564 0 : REPORT_SURFACE_CAPABILITIES_CONSTANT(supportedUsageFlags);
565 : #undef REPORT_SURFACE_CAPABILITIES_CONSTANT
566 0 : Script::SetProperty(rq, settings, "surface_capabilities", surfaceCapabilities);
567 0 : }
568 :
569 : } // namespace Vulkan
570 :
571 : } // namespace Backend
572 :
573 : } // namespace Renderer
|