Refactor config constants
- Replace enums with constexpr variables, and use the type that matches
the variables they are assigned to.
- Some unused constants have been removed.
- A bug-prone signed comparison for an array index has been fixed.
- Vulkan limits are statically asserted against internal limits.
Bug: b/25718218
Change-Id: I9e3f71ed54fb62bcccf5775bcab95ce15ae685cc
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/57668
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Sean Risser <srisser@google.com>
diff --git a/src/Device/Config.hpp b/src/Device/Config.hpp
index 0fd38ed..780fa66 100644
--- a/src/Device/Config.hpp
+++ b/src/Device/Config.hpp
@@ -17,23 +17,17 @@
namespace sw {
-enum
-{
- OUTLINE_RESOLUTION = 8192, // Maximum vertical resolution of the render target
- MIPMAP_LEVELS = 14,
- MAX_UNIFORM_BLOCK_SIZE = 16384,
- MAX_CLIP_DISTANCES = 8,
- MAX_CULL_DISTANCES = 8,
- MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS = 64,
- MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS = 64,
- MIN_TEXEL_OFFSET = -8,
- MAX_TEXEL_OFFSET = 7,
- MAX_TEXTURE_LOD = MIPMAP_LEVELS - 2, // Trilinear accesses lod+1
- MAX_COLOR_BUFFERS = 8,
- MAX_INTERFACE_COMPONENTS = 32 * 4, // Must be multiple of 4 for 16-byte alignment.
- MAX_FRAMEBUFFER_DIM = OUTLINE_RESOLUTION,
- MAX_VIEWPORT_DIM = MAX_FRAMEBUFFER_DIM,
-};
+constexpr int OUTLINE_RESOLUTION = 8192; // Maximum vertical resolution of the render target
+constexpr int MIPMAP_LEVELS = 14;
+constexpr int MAX_CLIP_DISTANCES = 8;
+constexpr int MAX_CULL_DISTANCES = 8;
+constexpr int MIN_TEXEL_OFFSET = -8;
+constexpr int MAX_TEXEL_OFFSET = 7;
+constexpr int MAX_TEXTURE_LOD = MIPMAP_LEVELS - 2; // Trilinear accesses lod+1
+constexpr int MAX_COLOR_BUFFERS = 8;
+constexpr int MAX_INTERFACE_COMPONENTS = 32 * 4; // Must be multiple of 4 for 16-byte alignment.
+constexpr int MAX_FRAMEBUFFER_DIM = OUTLINE_RESOLUTION;
+constexpr int MAX_VIEWPORT_DIM = MAX_FRAMEBUFFER_DIM;
} // namespace sw
diff --git a/src/Pipeline/SpirvShaderMemory.cpp b/src/Pipeline/SpirvShaderMemory.cpp
index 2fb1e67..5167e51 100644
--- a/src/Pipeline/SpirvShaderMemory.cpp
+++ b/src/Pipeline/SpirvShaderMemory.cpp
@@ -186,7 +186,7 @@
// Note: the module may contain descriptor set references that are not suitable for this implementation -- using a set index higher than the number
// of descriptor set binding points we support. As long as the selected entrypoint doesn't actually touch the out of range binding points, this
// is valid. In this case make the value nullptr to make it easier to diagnose an attempt to dereference it.
- if(d.DescriptorSet < vk::MAX_BOUND_DESCRIPTOR_SETS)
+ if(static_cast<uint32_t>(d.DescriptorSet) < vk::MAX_BOUND_DESCRIPTOR_SETS)
{
state->createPointer(resultId, SIMD::Pointer(routine->descriptorSets[d.DescriptorSet], size));
}
@@ -385,7 +385,7 @@
case Object::Kind::DescriptorSet:
{
const auto &d = descriptorDecorations.at(id);
- ASSERT(d.DescriptorSet >= 0 && d.DescriptorSet < vk::MAX_BOUND_DESCRIPTOR_SETS);
+ ASSERT(d.DescriptorSet >= 0 && static_cast<uint32_t>(d.DescriptorSet) < vk::MAX_BOUND_DESCRIPTOR_SETS);
ASSERT(d.Binding >= 0);
ASSERT(routine->pipelineLayout->getDescriptorCount(d.DescriptorSet, d.Binding) != 0); // "If descriptorCount is zero this binding entry is reserved and the resource must not be accessed from any stage via this binding within any pipeline using the set layout."
diff --git a/src/Vulkan/VkConfig.hpp b/src/Vulkan/VkConfig.hpp
index 43dae96..fd461fe 100644
--- a/src/Vulkan/VkConfig.hpp
+++ b/src/Vulkan/VkConfig.hpp
@@ -16,7 +16,7 @@
#define VK_CONFIG_HPP_
#include "Version.hpp"
-
+#include "Device/Config.hpp"
#include "Vulkan/VulkanPlatform.hpp"
#include "spirv-tools/libspirv.h"
@@ -27,58 +27,46 @@
#define SWIFTSHADER_DEVICE_NAME "SwiftShader Device" // Max length: VK_MAX_PHYSICAL_DEVICE_NAME_SIZE
#define SWIFTSHADER_UUID "SwiftShaderUUID" // Max length: VK_UUID_SIZE (16)
-const spv_target_env SPIRV_VERSION = SPV_ENV_VULKAN_1_2;
+constexpr spv_target_env SPIRV_VERSION = SPV_ENV_VULKAN_1_2;
-enum
-{
- API_VERSION = VK_API_VERSION_1_2,
- DRIVER_VERSION = VK_MAKE_VERSION(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION),
- VENDOR_ID = 0x1AE0, // Google, Inc.: https://pcisig.com/google-inc-1
- DEVICE_ID = 0xC0DE, // SwiftShader (placeholder)
-};
+constexpr uint32_t API_VERSION = VK_API_VERSION_1_2;
+constexpr uint32_t DRIVER_VERSION = VK_MAKE_VERSION(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
+constexpr uint32_t VENDOR_ID = 0x1AE0; // Google, Inc.: https://pcisig.com/google-inc-1
+constexpr uint32_t DEVICE_ID = 0xC0DE; // SwiftShader (placeholder)
-enum
-{
- // Alignment of all Vulkan objects, pools, device memory, images, buffers, descriptors.
- REQUIRED_MEMORY_ALIGNMENT = 16, // 16 bytes for 128-bit vector types.
+// Alignment of all Vulkan objects, pools, device memory, images, buffers, descriptors.
+constexpr VkDeviceSize REQUIRED_MEMORY_ALIGNMENT = 16; // 16 bytes for 128-bit vector types.
- MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT = 256,
- MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT = 256,
- MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT = 256,
+// Vulkan 1.2 requires buffer offset alignment to be at most 256.
+constexpr VkDeviceSize MIN_TEXEL_BUFFER_OFFSET_ALIGNMENT = 256;
+constexpr VkDeviceSize MIN_UNIFORM_BUFFER_OFFSET_ALIGNMENT = 256;
+constexpr VkDeviceSize MIN_STORAGE_BUFFER_OFFSET_ALIGNMENT = 256;
- MEMORY_TYPE_GENERIC_BIT = 0x1, // Generic system memory.
-};
+constexpr uint32_t MEMORY_TYPE_GENERIC_BIT = 0x1; // Generic system memory.
-enum
-{
- MAX_IMAGE_LEVELS_1D = 14,
- MAX_IMAGE_LEVELS_2D = 14,
- MAX_IMAGE_LEVELS_3D = 12,
- MAX_IMAGE_LEVELS_CUBE = 14,
- MAX_IMAGE_ARRAY_LAYERS = 2048,
- MAX_SAMPLER_LOD_BIAS = 15,
-};
+constexpr uint32_t MAX_IMAGE_LEVELS_1D = 14;
+constexpr uint32_t MAX_IMAGE_LEVELS_2D = 14;
+constexpr uint32_t MAX_IMAGE_LEVELS_3D = 12;
+constexpr uint32_t MAX_IMAGE_LEVELS_CUBE = 14;
+constexpr uint32_t MAX_IMAGE_ARRAY_LAYERS = 2048;
+constexpr float MAX_SAMPLER_LOD_BIAS = 15.0;
-enum
-{
- MAX_BOUND_DESCRIPTOR_SETS = 4,
- MAX_VERTEX_INPUT_BINDINGS = 16,
- MAX_PUSH_CONSTANT_SIZE = 128,
-};
+static_assert(MAX_IMAGE_LEVELS_1D <= sw::MIPMAP_LEVELS);
+static_assert(MAX_IMAGE_LEVELS_2D <= sw::MIPMAP_LEVELS);
+static_assert(MAX_IMAGE_LEVELS_3D <= sw::MIPMAP_LEVELS);
+static_assert(MAX_IMAGE_LEVELS_CUBE <= sw::MIPMAP_LEVELS);
-enum
-{
- MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC = 8,
- MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC = 4,
- MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC =
- MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC +
- MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC,
-};
+constexpr uint32_t MAX_BOUND_DESCRIPTOR_SETS = 4;
+constexpr uint32_t MAX_VERTEX_INPUT_BINDINGS = 16;
+constexpr uint32_t MAX_PUSH_CONSTANT_SIZE = 128;
-enum
-{
- MAX_POINT_SIZE = 1023,
-};
+constexpr uint32_t MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC = 8;
+constexpr uint32_t MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC = 4;
+constexpr uint32_t MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC =
+ MAX_DESCRIPTOR_SET_UNIFORM_BUFFERS_DYNAMIC +
+ MAX_DESCRIPTOR_SET_STORAGE_BUFFERS_DYNAMIC;
+
+constexpr float MAX_POINT_SIZE = 1023.0;
constexpr int MAX_SAMPLER_ALLOCATION_COUNT = 4000;