Undefine Bool after the headers that define them

X11/Xlib.h defines Bool as a macro, which clashes with Reactor's Bool
class. This X11 header is also included by vulkan.h, vk_icd.h, and
EGL/eglplatform.h

Previously we undefined Bool in various places. It's not possible to
just undefine it in Reactor.hpp because one can have headers following
the inclusion of Reactor.hpp which redefine it (note our convention is
to include platform headers after project headers).

Since X11 is only needed in select places related to the windowing
system, we can instead avoid the use of these headers in other headers,
and only include them in .cpp files where necessary. Note that
vulkan_core.h contains the whole plaform-independent Vulkan API, so it
should be used instead of vulkan.h for all non-WSI code.

Bug: b/127920555
Change-Id: Id11a1c58b87802fe9e530387831693e44accae2b
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/46368
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/include/vulkan/vk_ext_provoking_vertex.h b/include/vulkan/vk_ext_provoking_vertex.h
index c81fd6e..c6fe7b8 100644
--- a/include/vulkan/vk_ext_provoking_vertex.h
+++ b/include/vulkan/vk_ext_provoking_vertex.h
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include "vulkan.h"
+#include "vulkan_core.h"
 
 // THIS FILE SHOULD BE DELETED IF VK_EXT_provoking_vertex IS EVER ADDED TO THE VULKAN HEADERS
 #ifdef VK_EXT_provoking_vertex
diff --git a/src/Main/libX11.cpp b/src/Main/libX11.cpp
index f3723ff..7711d12 100644
--- a/src/Main/libX11.cpp
+++ b/src/Main/libX11.cpp
@@ -16,8 +16,6 @@
 
 #include "Common/SharedLibrary.hpp"
 
-#define Bool int
-
 LibX11exports::LibX11exports(void *libX11, void *libXext)
 {
 	XOpenDisplay = (Display *(*)(char*))getProcAddress(libX11, "XOpenDisplay");
diff --git a/src/Main/libX11.hpp b/src/Main/libX11.hpp
index b923d3d..0170649 100644
--- a/src/Main/libX11.hpp
+++ b/src/Main/libX11.hpp
@@ -48,8 +48,6 @@
 	int (*XShmPutImage)(Display *display, Drawable d, GC gc, XImage *image, int src_x, int src_y, int dest_x, int dest_y, unsigned int width, unsigned int height, bool send_event);
 };
 
-#undef Bool // b/127920555
-
 class LibX11
 {
 public:
diff --git a/src/OpenGL/common/Surface.hpp b/src/OpenGL/common/Surface.hpp
index c9e1973..a65305c 100644
--- a/src/OpenGL/common/Surface.hpp
+++ b/src/OpenGL/common/Surface.hpp
@@ -21,6 +21,7 @@
 #include "Renderer/Surface.hpp"
 
 #include <EGL/egl.h>
+#undef Bool
 
 namespace egl
 {
diff --git a/src/OpenGL/libEGL/Context.hpp b/src/OpenGL/libEGL/Context.hpp
index 438dfff..c9fe4cf 100644
--- a/src/OpenGL/libEGL/Context.hpp
+++ b/src/OpenGL/libEGL/Context.hpp
@@ -19,6 +19,8 @@
 #include "Renderer/Surface.hpp"
 
 #include <EGL/egl.h>
+#undef Bool
+
 #include <GLES/gl.h>
 
 namespace gl { class Surface; }
diff --git a/src/Pipeline/SamplerCore.hpp b/src/Pipeline/SamplerCore.hpp
index 1fee321..c7330ab 100644
--- a/src/Pipeline/SamplerCore.hpp
+++ b/src/Pipeline/SamplerCore.hpp
@@ -20,10 +20,6 @@
 #include "Reactor/Print.hpp"
 #include "Reactor/Reactor.hpp"
 
-#ifdef None
-#	undef None  // b/127920555
-#endif
-
 namespace sw {
 
 using namespace rr;
diff --git a/src/Reactor/Reactor.hpp b/src/Reactor/Reactor.hpp
index e8d053b..8d070fc 100644
--- a/src/Reactor/Reactor.hpp
+++ b/src/Reactor/Reactor.hpp
@@ -26,8 +26,6 @@
 #include <tuple>
 #include <unordered_set>
 
-#undef Bool  // b/127920555
-
 #ifdef ENABLE_RR_DEBUG_INFO
 // Functions used for generating JIT debug info.
 // See docs/ReactorDebugInfo.md for more information.
diff --git a/src/Reactor/Traits.hpp b/src/Reactor/Traits.hpp
index 3e02abb1..522ed6a 100644
--- a/src/Reactor/Traits.hpp
+++ b/src/Reactor/Traits.hpp
@@ -18,10 +18,6 @@
 #include <stdint.h>
 #include <type_traits>
 
-#ifdef Bool
-#	undef Bool  // b/127920555
-#endif
-
 namespace rr {
 
 // Forward declarations
diff --git a/src/Vulkan/VkDescriptorSet.hpp b/src/Vulkan/VkDescriptorSet.hpp
index 3ef058b..a667e82 100644
--- a/src/Vulkan/VkDescriptorSet.hpp
+++ b/src/Vulkan/VkDescriptorSet.hpp
@@ -15,9 +15,7 @@
 #ifndef VK_DESCRIPTOR_SET_HPP_
 #define VK_DESCRIPTOR_SET_HPP_
 
-// Intentionally not including VkObject.hpp here due to b/127920555
-#include "VkConfig.hpp"
-
+#include "VkObject.hpp"
 #include "marl/mutex.h"
 
 #include <array>
@@ -35,19 +33,9 @@
 	marl::mutex mutex;
 };
 
-class alignas(16) DescriptorSet
+class alignas(16) DescriptorSet : public Object<DescriptorSet, VkDescriptorSet>
 {
 public:
-	static inline DescriptorSet *Cast(VkDescriptorSet object)
-	{
-		return static_cast<DescriptorSet *>(static_cast<void *>(object));
-	}
-
-	operator VkDescriptorSet()
-	{
-		return { static_cast<uint64_t>(reinterpret_cast<uintptr_t>(this)) };
-	}
-
 	using Array = std::array<DescriptorSet *, vk::MAX_BOUND_DESCRIPTOR_SETS>;
 	using Bindings = std::array<uint8_t *, vk::MAX_BOUND_DESCRIPTOR_SETS>;
 	using DynamicOffsets = std::array<uint32_t, vk::MAX_DESCRIPTOR_SET_COMBINED_BUFFERS_DYNAMIC>;
diff --git a/src/Vulkan/VkObject.hpp b/src/Vulkan/VkObject.hpp
index 24b3200..dece90d 100644
--- a/src/Vulkan/VkObject.hpp
+++ b/src/Vulkan/VkObject.hpp
@@ -20,8 +20,10 @@
 
 #include "System/Debug.hpp"
 
-#include "Vulkan/VulkanPlatform.hpp"
 #include <vulkan/vk_icd.h>
+#undef None
+#undef Bool
+
 #include <new>
 
 namespace vk {
diff --git a/src/Vulkan/VkQueue.hpp b/src/Vulkan/VkQueue.hpp
index 022a73d..af80df8 100644
--- a/src/Vulkan/VkQueue.hpp
+++ b/src/Vulkan/VkQueue.hpp
@@ -17,11 +17,10 @@
 
 #include "VkObject.hpp"
 #include "Device/Renderer.hpp"
-#include <vulkan/vk_icd.h>
-#include <thread>
-
 #include "System/Synchronization.hpp"
 
+#include <thread>
+
 namespace marl {
 class Scheduler;
 }
diff --git a/src/Vulkan/VkStringify.cpp b/src/Vulkan/VkStringify.cpp
index eedd1bb..0d71f25 100644
--- a/src/Vulkan/VkStringify.cpp
+++ b/src/Vulkan/VkStringify.cpp
@@ -16,8 +16,8 @@
 
 #include "System/Debug.hpp"
 
-#include "vulkan/vk_ext_provoking_vertex.h"
-#include "vulkan/vk_google_filtering_precision.h"
+#include <vulkan/vk_ext_provoking_vertex.h>
+#include <vulkan/vk_google_filtering_precision.h>
 
 #include <iostream>
 #include <map>
diff --git a/src/Vulkan/VkStringify.hpp b/src/Vulkan/VkStringify.hpp
index 4da04df..f46ec92 100644
--- a/src/Vulkan/VkStringify.hpp
+++ b/src/Vulkan/VkStringify.hpp
@@ -17,7 +17,7 @@
 #ifndef VK_STRINGIFY_HPP_
 #define VK_STRINGIFY_HPP_
 
-#include <vulkan/vulkan.h>
+#include <vulkan/vulkan_core.h>
 #include <string>
 
 namespace vk {
diff --git a/src/Vulkan/VulkanPlatform.hpp b/src/Vulkan/VulkanPlatform.hpp
index c1ac233..e03104c 100644
--- a/src/Vulkan/VulkanPlatform.hpp
+++ b/src/Vulkan/VulkanPlatform.hpp
@@ -27,8 +27,8 @@
 	{
 		static_assert(sizeof(VkNonDispatchableHandle) == sizeof(uint64_t), "Size is not 64 bits!");
 
-		// VkNonDispatchabbleHandle must be POD to ensure it gets passed by value the same way as a uint64_t,
-		// which is the upstream header's handle type when compiled for 32b architectures. On 64b architectures,
+		// VkNonDispatchableHandle must be POD to ensure it gets passed by value the same way as a uint64_t,
+		// which is the upstream header's handle type when compiled for 32-bit architectures. On 64-bit architectures,
 		// the upstream header's handle type is a pointer type.
 		static_assert(std::is_trivial<VkNonDispatchableHandle<T>>::value, "VkNonDispatchableHandle<T> is not trivial!");
 		static_assert(std::is_standard_layout<VkNonDispatchableHandle<T>>::value, "VkNonDispatchableHandle<T> is not standard layout!");
@@ -51,12 +51,7 @@
 
 #include <vulkan/vk_ext_provoking_vertex.h>
 #include <vulkan/vk_google_filtering_precision.h>
-#include <vulkan/vulkan.h>
-
-#ifdef Bool
-#	undef Bool  // b/127920555
-#	undef None
-#endif
+#include <vulkan/vulkan_core.h>
 
 namespace vk {
 // Here we define constants that used to be in the Vulkan headers, but are not part of the spec.
diff --git a/src/WSI/MetalSurface.hpp b/src/WSI/MetalSurface.hpp
index 47ad600..e6dfe24 100644
--- a/src/WSI/MetalSurface.hpp
+++ b/src/WSI/MetalSurface.hpp
@@ -17,11 +17,12 @@
 
 #include "VkSurfaceKHR.hpp"
 #include "Vulkan/VkObject.hpp"
+
 #ifdef VK_USE_PLATFORM_MACOS_MVK
-#	include "vulkan/vulkan_macos.h"
+#	include <vulkan/vulkan_macos.h>
 #endif
 #ifdef VK_USE_PLATFORM_METAL_EXT
-#	include "vulkan/vulkan_metal.h"
+#	include <vulkan/vulkan_metal.h>
 #endif
 
 namespace vk {
diff --git a/src/WSI/Win32SurfaceKHR.hpp b/src/WSI/Win32SurfaceKHR.hpp
index 0c27465..8c5c714 100644
--- a/src/WSI/Win32SurfaceKHR.hpp
+++ b/src/WSI/Win32SurfaceKHR.hpp
@@ -22,8 +22,8 @@
 #if !defined(WIN32_LEAN_AND_MEAN)
 #	define WIN32_LEAN_AND_MEAN
 #endif  // !defined(WIN32_LEAN_AND_MEAN)
-#include "vulkan/vulkan_win32.h"
 #include <Windows.h>
+#include <vulkan/vulkan_win32.h>
 
 #include <map>
 
diff --git a/src/WSI/XcbSurfaceKHR.hpp b/src/WSI/XcbSurfaceKHR.hpp
index cc468fa..b0396d9 100644
--- a/src/WSI/XcbSurfaceKHR.hpp
+++ b/src/WSI/XcbSurfaceKHR.hpp
@@ -17,7 +17,8 @@
 
 #include "VkSurfaceKHR.hpp"
 #include "Vulkan/VkObject.hpp"
-#include "vulkan/vulkan_xcb.h"
+
+#include <vulkan/vulkan_xcb.h>
 #include <xcb/xcb.h>
 
 #include <unordered_map>
diff --git a/src/WSI/XlibSurfaceKHR.hpp b/src/WSI/XlibSurfaceKHR.hpp
index 448c979..f9ee870 100644
--- a/src/WSI/XlibSurfaceKHR.hpp
+++ b/src/WSI/XlibSurfaceKHR.hpp
@@ -18,7 +18,8 @@
 #include "VkSurfaceKHR.hpp"
 #include "libX11.hpp"
 #include "Vulkan/VkObject.hpp"
-#include "vulkan/vulkan_xlib.h"
+
+#include <vulkan/vulkan_xlib.h>
 
 #include <unordered_map>
 
diff --git a/src/WSI/libX11.cpp b/src/WSI/libX11.cpp
index 68bd2b5..977dc4e 100644
--- a/src/WSI/libX11.cpp
+++ b/src/WSI/libX11.cpp
@@ -16,8 +16,6 @@
 
 #include "System/SharedLibrary.hpp"
 
-#define Bool int
-
 namespace {
 
 template<typename FPTR>
diff --git a/src/WSI/libX11.hpp b/src/WSI/libX11.hpp
index a51c591..65fe593 100644
--- a/src/WSI/libX11.hpp
+++ b/src/WSI/libX11.hpp
@@ -49,8 +49,6 @@
 	int (*XShmPutImage)(Display *display, Drawable d, GC gc, XImage *image, int src_x, int src_y, int dest_x, int dest_y, unsigned int width, unsigned int height, bool send_event);
 };
 
-#undef Bool  // b/127920555
-
 class LibX11
 {
 public:
diff --git a/tests/SystemBenchmarks/LRUCacheBenchmarks.cpp b/tests/SystemBenchmarks/LRUCacheBenchmarks.cpp
index ebf7f63..543cd0b 100644
--- a/tests/SystemBenchmarks/LRUCacheBenchmarks.cpp
+++ b/tests/SystemBenchmarks/LRUCacheBenchmarks.cpp
@@ -160,7 +160,7 @@
 		ComplexKey key;
 		for(size_t w = 0; w < key.words.size(); w++)
 		{
-			key.words[w] = (1U << w);
+			key.words[w] = (1ull << w);
 		}
 		cache.add(key, i);
 	}
@@ -172,7 +172,7 @@
 		ComplexKey key;
 		for(size_t w = 0; w < key.words.size(); w++)
 		{
-			key.words[w] = i & (1U << w);
+			key.words[w] = i & (1ull << w);
 		}
 		cache.lookup(key);
 	}
@@ -190,7 +190,7 @@
 		ComplexKey key;
 		for(size_t w = 0; w < key.words.size(); w++)
 		{
-			key.words[w] = 8 + (1U << w);
+			key.words[w] = 8 + (1ull << w);
 		}
 		cache.add(key, i);
 	}
@@ -202,7 +202,7 @@
 		ComplexKey key;
 		for(size_t w = 0; w < key.words.size(); w++)
 		{
-			key.words[w] = i & (1U << w);
+			key.words[w] = i & (1ull << w);
 		}
 		cache.lookup(key);
 	}