xref: /aosp_15_r20/external/swiftshader/src/Vulkan/VkConfig.hpp (revision 03ce13f70fcc45d86ee91b7ee4cab1936a95046e)
1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef VK_CONFIG_HPP_
16 #define VK_CONFIG_HPP_
17 
18 #include "Version.hpp"
19 #include "Device/Config.hpp"
20 #include "Vulkan/VulkanPlatform.hpp"
21 #include "spirv-tools/libspirv.h"
22 
23 #ifndef SWIFTSHADER_LEGACY_PRECISION
24 #	define SWIFTSHADER_LEGACY_PRECISION false
25 #endif
26 
27 #ifndef SWIFTSHADER_DEVICE_MEMORY_ALLOCATION_ALIGNMENT
28 #	define SWIFTSHADER_DEVICE_MEMORY_ALLOCATION_ALIGNMENT 256
29 #endif
30 
31 namespace vk {
32 
33 // Note: Constant array initialization requires a string literal.
34 //       constexpr char* or char[] does not work for that purpose.
35 #define SWIFTSHADER_DEVICE_NAME "SwiftShader Device"  // Max length: VK_MAX_PHYSICAL_DEVICE_NAME_SIZE
36 #define SWIFTSHADER_UUID "SwiftShaderUUID"            // Max length: VK_UUID_SIZE (16)
37 
38 constexpr spv_target_env SPIRV_VERSION = SPV_ENV_VULKAN_1_3;
39 
40 constexpr uint32_t API_VERSION = VK_API_VERSION_1_3;
41 constexpr uint32_t DRIVER_VERSION = VK_MAKE_VERSION(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
42 constexpr uint32_t VENDOR_ID = 0x1AE0;  // Google, Inc.: https://pcisig.com/google-inc-1
43 constexpr uint32_t DEVICE_ID = 0xC0DE;  // SwiftShader (placeholder)
44 
45 // "Allocations returned by vkAllocateMemory are guaranteed to meet any alignment requirement of the implementation."
46 constexpr VkDeviceSize DEVICE_MEMORY_ALLOCATION_ALIGNMENT = SWIFTSHADER_DEVICE_MEMORY_ALLOCATION_ALIGNMENT;
47 
48 constexpr VkDeviceSize MIN_MEMORY_MAP_ALIGNMENT = 64;
49 static_assert(DEVICE_MEMORY_ALLOCATION_ALIGNMENT >= MIN_MEMORY_MAP_ALIGNMENT);
50 
51 constexpr VkDeviceSize MIN_IMPORTED_HOST_POINTER_ALIGNMENT = 4096;
52 static_assert(MIN_IMPORTED_HOST_POINTER_ALIGNMENT >= DEVICE_MEMORY_ALLOCATION_ALIGNMENT);
53 
54 // Vulkan 1.2 requires buffer offset alignment to be at most 256.
55 constexpr VkDeviceSize MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT = 256;
56 constexpr VkDeviceSize MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT = 256;
57 constexpr VkDeviceSize MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT = 256;
58 static_assert(DEVICE_MEMORY_ALLOCATION_ALIGNMENT >= MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT);
59 static_assert(DEVICE_MEMORY_ALLOCATION_ALIGNMENT >= MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
60 static_assert(DEVICE_MEMORY_ALLOCATION_ALIGNMENT >= MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT);
61 
62 // Alignment of all other Vulkan resources.
63 constexpr VkDeviceSize MEMORY_REQUIREMENTS_OFFSET_ALIGNMENT = 16;  // 16 bytes for 128-bit vector types.
64 static_assert(DEVICE_MEMORY_ALLOCATION_ALIGNMENT >= MEMORY_REQUIREMENTS_OFFSET_ALIGNMENT);
65 
66 constexpr VkDeviceSize HOST_MEMORY_ALLOCATION_ALIGNMENT = 16;  // 16 bytes for 128-bit vector types.
67 
68 constexpr uint32_t MEMORY_TYPE_GENERIC_BIT = 0x1;  // Generic system memory.
69 
70 constexpr uint32_t MAX_IMAGE_LEVELS_1D = 15;
71 constexpr uint32_t MAX_IMAGE_LEVELS_2D = 15;
72 constexpr uint32_t MAX_IMAGE_LEVELS_3D = 12;
73 constexpr uint32_t MAX_IMAGE_LEVELS_CUBE = 15;
74 constexpr uint32_t MAX_IMAGE_ARRAY_LAYERS = 2048;
75 constexpr float MAX_SAMPLER_LOD_BIAS = 15.0;
76 
77 static_assert(MAX_IMAGE_LEVELS_1D <= sw::MIPMAP_LEVELS);
78 static_assert(MAX_IMAGE_LEVELS_2D <= sw::MIPMAP_LEVELS);
79 static_assert(MAX_IMAGE_LEVELS_3D <= sw::MIPMAP_LEVELS);
80 static_assert(MAX_IMAGE_LEVELS_CUBE <= sw::MIPMAP_LEVELS);
81 
82 constexpr uint32_t MAX_BOUND_DESCRIPTOR_SETS = 4;
83 constexpr uint32_t MAX_VERTEX_INPUT_BINDINGS = 16;
84 constexpr uint32_t MAX_PUSH_CONSTANT_SIZE = 128;
85 constexpr uint32_t MAX_UPDATE_AFTER_BIND_DESCRIPTORS = 500000;
86 
87 constexpr uint32_t MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC = 8;
88 constexpr uint32_t MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC = 4;
89 constexpr uint32_t MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC =
90     MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC +
91     MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC;
92 
93 constexpr uint32_t MAX_COMPUTE_WORKGROUP_INVOCATIONS = 256;
94 
95 constexpr size_t MAX_INLINE_UNIFORM_BLOCK_SIZE = 256;
96 
97 constexpr float MAX_POINT_SIZE = 1023.0;
98 
99 constexpr int MAX_SAMPLER_ALLOCATION_COUNT = 4000;
100 
101 constexpr int SUBPIXEL_PRECISION_BITS = SWIFTSHADER_LEGACY_PRECISION ? 4 : 8;
102 constexpr float SUBPIXEL_PRECISION_FACTOR = static_cast<float>(1 << SUBPIXEL_PRECISION_BITS);
103 constexpr int SUBPIXEL_PRECISION_MASK = 0xFFFFFFFF >> (32 - SUBPIXEL_PRECISION_BITS);
104 
105 constexpr int MAX_VIEWPORTS = 16;
106 
107 // TODO: The heap size should be configured based on available RAM.
108 constexpr VkDeviceSize PHYSICAL_DEVICE_HEAP_SIZE = 0x80000000ull;   // 0x80000000 = 2 GiB
109 constexpr VkDeviceSize MAX_MEMORY_ALLOCATION_SIZE = 0x40000000ull;  // 0x40000000 = 1 GiB
110 
111 // Memory offset calculations in 32-bit SIMD elements limit us to addressing at most 4 GiB.
112 // Signed arithmetic further restricts it to 2 GiB.
113 static_assert(MAX_MEMORY_ALLOCATION_SIZE <= 0x80000000ull, "maxMemoryAllocationSize must not exceed 2 GiB");
114 
115 }  // namespace vk
116 
117 #if defined(__linux__) && !defined(__ANDROID__)
118 #	define SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD 1
119 #	define SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD 1
120 #elif defined(__ANDROID__)
121 #	define SWIFTSHADER_EXTERNAL_SEMAPHORE_OPAQUE_FD 1
122 #endif
123 #if defined(__APPLE__)
124 #	define SWIFTSHADER_EXTERNAL_MEMORY_OPAQUE_FD 1
125 #endif
126 
127 #endif  // VK_CONFIG_HPP_
128