2018-07-18 09:40:26 +00:00
|
|
|
// Copyright 2017 The Dawn Authors
|
2017-11-17 22:52:04 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-01-15 20:56:35 +00:00
|
|
|
#ifndef COMMON_VULKANPLATFORM_H_
|
|
|
|
#define COMMON_VULKANPLATFORM_H_
|
|
|
|
|
2018-07-18 11:52:46 +00:00
|
|
|
#if !defined(DAWN_ENABLE_BACKEND_VULKAN)
|
2018-01-15 20:56:35 +00:00
|
|
|
# error "vulkan_platform.h included without the Vulkan backend enabled"
|
|
|
|
#endif
|
2017-11-17 22:52:04 +00:00
|
|
|
|
2018-07-25 11:44:28 +00:00
|
|
|
#include "common/Platform.h"
|
|
|
|
|
2017-12-05 14:41:18 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
// vulkan.h defines non-dispatchable handles to opaque pointers on 64bit architectures and uint64_t
|
|
|
|
// on 32bit architectures. This causes a problem in 32bit where the handles cannot be used to
|
|
|
|
// distinguish between overloads of the same function.
|
|
|
|
// Change the definition of non-dispatchable handles to be opaque structures containing a uint64_t
|
|
|
|
// and overload the comparison operators between themselves and VK_NULL_HANDLE (which will be
|
|
|
|
// redefined to be nullptr). This keeps the type-safety of having the handles be different types
|
|
|
|
// (like vulkan.h on 64 bit) but makes sure the types are different on 32 bit architectures.
|
|
|
|
|
|
|
|
// Simple handle types that supports "nullptr_t" as a 0 value.
|
2018-07-25 11:44:28 +00:00
|
|
|
template <typename Tag, typename HandleType>
|
2017-12-05 14:41:18 +00:00
|
|
|
class VkNonDispatchableHandle {
|
|
|
|
public:
|
|
|
|
// Default constructor and assigning of VK_NULL_HANDLE
|
|
|
|
VkNonDispatchableHandle() = default;
|
|
|
|
VkNonDispatchableHandle(std::nullptr_t) : mHandle(0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use default copy constructor/assignment
|
2018-07-25 11:44:28 +00:00
|
|
|
VkNonDispatchableHandle(const VkNonDispatchableHandle<Tag, HandleType>& other) = default;
|
|
|
|
VkNonDispatchableHandle& operator=(const VkNonDispatchableHandle<Tag, HandleType>&) = default;
|
2017-12-05 14:41:18 +00:00
|
|
|
|
|
|
|
// Comparisons between handles
|
2018-07-25 11:44:28 +00:00
|
|
|
bool operator==(VkNonDispatchableHandle<Tag, HandleType> other) {
|
2017-12-05 14:41:18 +00:00
|
|
|
return mHandle == other.mHandle;
|
|
|
|
}
|
2018-07-25 11:44:28 +00:00
|
|
|
bool operator!=(VkNonDispatchableHandle<Tag, HandleType> other) {
|
2017-12-05 14:41:18 +00:00
|
|
|
return mHandle != other.mHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comparisons between handles and VK_NULL_HANDLE
|
|
|
|
bool operator==(std::nullptr_t) {
|
|
|
|
return mHandle == 0;
|
|
|
|
}
|
|
|
|
bool operator!=(std::nullptr_t) {
|
|
|
|
return mHandle != 0;
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:44:28 +00:00
|
|
|
// The regular Vulkan handle type depends on the pointer width but is always 64 bits wide.
|
|
|
|
// - On 64bit it is an opaque pointer type, probably to help with type safety
|
|
|
|
// - On 32bit it is a uint64_t because pointers aren't wide enough (and non dispatchable
|
|
|
|
// handles can be optimized to not be pointer but contain GPU virtual addresses or the
|
|
|
|
// data in a packed form).
|
|
|
|
// Because of this we need two types of conversions from our handle type: to uint64_t and to
|
|
|
|
// the "native" Vulkan type that may not be an uint64_t
|
|
|
|
|
|
|
|
static VkNonDispatchableHandle<Tag, HandleType> CreateFromU64(uint64_t handle) {
|
2018-01-16 16:15:19 +00:00
|
|
|
return {handle};
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:44:28 +00:00
|
|
|
uint64_t GetU64() const {
|
2018-01-26 21:45:46 +00:00
|
|
|
return mHandle;
|
|
|
|
}
|
|
|
|
|
2018-07-25 11:44:28 +00:00
|
|
|
#if DAWN_PLATFORM_64_BIT
|
|
|
|
static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
|
|
|
|
return CreateFromU64(static_cast<uint64_t>(reinterpret_cast<intptr_t>(handle)));
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleType GetHandle() const {
|
|
|
|
return mHandle;
|
|
|
|
}
|
|
|
|
#elif DAWN_PLATFORM_32_BIT
|
|
|
|
static VkNonDispatchableHandle<Tag, HandleType> CreateFromHandle(HandleType handle) {
|
|
|
|
return {handle};
|
|
|
|
}
|
|
|
|
|
|
|
|
HandleType GetHandle() const {
|
|
|
|
return mHandle;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# error "Unsupported platform"
|
|
|
|
#endif
|
|
|
|
|
2017-12-05 14:41:18 +00:00
|
|
|
private:
|
2018-01-16 16:15:19 +00:00
|
|
|
VkNonDispatchableHandle(uint64_t handle) : mHandle(handle) {
|
|
|
|
}
|
|
|
|
|
2017-12-05 14:41:18 +00:00
|
|
|
uint64_t mHandle = 0;
|
|
|
|
};
|
|
|
|
|
2018-07-25 11:44:28 +00:00
|
|
|
#if DAWN_PLATFORM_64_BIT
|
|
|
|
# define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) \
|
|
|
|
using object##Native = struct object##_T*;
|
|
|
|
#elif DAWN_PLATFORM_32_BIT
|
|
|
|
# define DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) using object##Native = uint64_t;
|
|
|
|
#else
|
|
|
|
# error "Unsupported platform"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) \
|
|
|
|
struct VkTag##object; \
|
|
|
|
DAWN_DEFINE_NATIVE_NON_DISPATCHABLE_HANDLE(object) \
|
|
|
|
using object = VkNonDispatchableHandle<VkTag##object, object##Native>; \
|
|
|
|
static_assert(sizeof(object) == sizeof(uint64_t), ""); \
|
|
|
|
static_assert(alignof(object) == alignof(uint64_t), ""); \
|
|
|
|
static_assert(sizeof(object) == sizeof(object##Native), ""); \
|
|
|
|
static_assert(alignof(object) == alignof(object##Native), "");
|
2017-12-05 14:41:18 +00:00
|
|
|
|
|
|
|
# include <vulkan/vulkan.h>
|
|
|
|
|
|
|
|
// VK_NULL_HANDLE is defined to 0 but we don't want our handle type to compare to arbitrary
|
|
|
|
// integers. Redefine VK_NULL_HANDLE to nullptr that has its own type.
|
|
|
|
# undef VK_NULL_HANDLE
|
|
|
|
# define VK_NULL_HANDLE nullptr
|
2017-11-17 22:52:04 +00:00
|
|
|
|
2018-07-17 09:27:58 +00:00
|
|
|
// Remove windows.h macros after vulkan_platform's include of windows.h
|
2018-07-18 11:37:54 +00:00
|
|
|
#if defined(DAWN_PLATFORM_WINDOWS)
|
2018-07-17 09:27:58 +00:00
|
|
|
# include "common/windows_with_undefs.h"
|
|
|
|
#endif
|
|
|
|
|
2018-01-15 20:56:35 +00:00
|
|
|
#endif // COMMON_VULKANPLATFORM_H_
|