Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/Example1/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(Example1)
cmake_minimum_required(VERSION 2.8)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR APPLE)
add_definitions(-std=c++11)
endif()

Expand Down
2 changes: 1 addition & 1 deletion Examples/Example2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(Example2)
cmake_minimum_required(VERSION 2.8)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR APPLE)
add_definitions(-std=c++11)
endif()

Expand Down
3 changes: 3 additions & 0 deletions Examples/Example2/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@

#include "linmath.h"
#include "cube.h"
#ifndef VK_DYNAMIC_STATE_RANGE_SIZE
#define VK_DYNAMIC_STATE_RANGE_SIZE 9
#endif

#define DEMO_TEXTURE_COUNT 1
#define APP_SHORT_NAME "cube"
Expand Down
4 changes: 3 additions & 1 deletion Examples/Example2/linmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include <math.h>

# define M_PI 3.14159265358979323846 /* pi */
#ifndef __APPLE__
#define M_PI 3.14159265358979323846 /* pi */
#endif

// Converts degrees to radians.
#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
Expand Down
2 changes: 1 addition & 1 deletion Examples/Example3/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
project(Example3)
cmake_minimum_required(VERSION 2.8)

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
if(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR APPLE)
add_definitions(-std=c++11)
endif()

Expand Down
4 changes: 4 additions & 0 deletions Examples/Teapots/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ endif()
if(ANDROID)
add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR)
endif()
if(APPLE)
add_definitions(-DVK_USE_PLATFORM_METAL_EXT)
add_definitions(-D-DWIN32_LEAN_AND_MEAN)
endif()
#-------------------------------------------------------------------------------------------------
#---------------------------------------Find glslangValidator-------------------------------------
if(NOT EXISTS ${GLSLANG_VALIDATOR})
Expand Down
11 changes: 10 additions & 1 deletion WSIWindow/CInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ CInstance::CInstance(const bool enable_validation, const char* app_name, const c
extensions.Pick(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);
#elif VK_USE_PLATFORM_MIR_KHR
extensions.Pick(VK_KHR_MIR_SURFACE_EXTENSION_NAME);
#elif VK_USE_PLATFORM_METAL_EXT
extensions.Pick("VK_KHR_portability_enumeration");
//extensions.Pick("VK_KHR_portability_subset");
extensions.Pick("VK_EXT_metal_surface");
#endif
} else LOGE("Failed to load VK_KHR_Surface");

Expand All @@ -176,11 +180,16 @@ void CInstance::Create(const CLayers& layers, const CExtensions& extensions, con
app_info.engineVersion = 1;
app_info.apiVersion = VK_API_VERSION_1_0;

VkInstanceCreateFlags inst_flags = 0;
#ifdef VK_USE_PLATFORM_METAL_EXT
inst_flags = VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
#endif

// initialize the VkInstanceCreateInfo structure
VkInstanceCreateInfo inst_info = {};
inst_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
inst_info.pNext = NULL;
inst_info.flags = 0;
inst_info.flags = inst_flags;
inst_info.pApplicationInfo = &app_info;
inst_info.enabledExtensionCount = extensions.PickCount();
inst_info.ppEnabledExtensionNames = extensions.PickList();
Expand Down
32 changes: 26 additions & 6 deletions WSIWindow/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ cmake_minimum_required(VERSION 2.8)
set(LIBRARY_NAME WSIWindow)

file(GLOB SRC_LIST "*.h" "*.cpp") # List all source files from current directory
file(GLOB OBJC_SRC_LIST "*.mm")
if(APPLE)
list(APPEND SRC_LIST ${OBJC_SRC_LIST})
endif()
add_library (${LIBRARY_NAME} STATIC ${SRC_LIST}) # and add them to the WSIWindow library.
target_include_directories (${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake") #for find_package

#=========================VULKAN_WRAPPER==========================
if (USE_VULKAN_WRAPPER)
add_subdirectory(VulkanWrapper)
target_link_libraries(${LIBRARY_NAME} VULKAN_WRAPPER)
endif()
#=================================================================
#============================VALIDATION===========================
# Validation may be enabled in both debug and release builds.
Expand Down Expand Up @@ -94,6 +92,28 @@ endif()
#if(ANDROID)
# add_definitions(-DVK_USE_PLATFORM_ANDROID_KHR)
#endif()
#=================================================================
#==============================MACOS==============================
if(APPLE)
add_definitions(-std=c++11)
add_definitions(-DVK_USE_PLATFORM_METAL_EXT)

find_library(QUARTZCORE_LIB QuartzCore)

target_link_libraries(${LIBRARY_NAME}
"-framework Cocoa"
${QUARTZCORE_LIB}
)

set (VULKAN_LOADER_NAME "vulkan")
endif()

#=========================VULKAN_WRAPPER==========================
if (USE_VULKAN_WRAPPER)
add_subdirectory(VulkanWrapper)
target_link_libraries(${LIBRARY_NAME} VULKAN_WRAPPER)
endif()

#=================================================================
#=======================Find Vulkan Loader========================
if(TARGET ${VULKAN_LOADER_NAME}) # Check if "vulkan" loader target exists
Expand Down
3 changes: 3 additions & 0 deletions WSIWindow/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
#include <xkbcommon/xkbcommon.h>
#define cTICK "\u2713"
#define PAUSE
#elif __APPLE__
#define cTICK "\u2713"
#define PAUSE
#endif

enum eColor { eRESET, eRED, eGREEN, eYELLOW, eBLUE, eMAGENTA, eCYAN, eWHITE, // normal colors
Expand Down
4 changes: 2 additions & 2 deletions WSIWindow/VulkanWrapper/vulkan/vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -6639,7 +6639,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateIOSSurfaceMVK(
#endif
#endif /* VK_USE_PLATFORM_IOS_MVK */

#ifdef VK_USE_PLATFORM_MACOS_MVK
#ifdef VK_USE_PLATFORM_METAL_EXT
#define VK_MVK_macos_surface 1
#define VK_MVK_MACOS_SURFACE_SPEC_VERSION 2
#define VK_MVK_MACOS_SURFACE_EXTENSION_NAME "VK_MVK_macos_surface"
Expand All @@ -6663,7 +6663,7 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateMacOSSurfaceMVK(
const VkAllocationCallbacks* pAllocator,
VkSurfaceKHR* pSurface);
#endif
#endif /* VK_USE_PLATFORM_MACOS_MVK */
#endif /* VK_USE_PLATFORM_METAL_EXT */

#define VK_EXT_external_memory_dma_buf 1
#define VK_EXT_EXTERNAL_MEMORY_DMA_BUF_SPEC_VERSION 1
Expand Down
10 changes: 10 additions & 0 deletions WSIWindow/VulkanWrapper/vulkan_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ extern "C" {
int InitVulkan(void) {
#ifdef _WIN32
HMODULE libvulkan = LoadLibrary("vulkan-1.dll");
#elif __APPLE__
void* libvulkan = dlopen("libvulkan.dylib", RTLD_NOW | RTLD_LOCAL);
#else
void* libvulkan = dlopen("libvulkan.so", RTLD_NOW | RTLD_LOCAL);
#endif
Expand Down Expand Up @@ -259,6 +261,10 @@ int InitVulkan(void) {
vkGetPhysicalDeviceWin32PresentationSupportKHR = reinterpret_cast<PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR>(
dlsym(libvulkan, "vkGetPhysicalDeviceWin32PresentationSupportKHR"));
#endif

#ifdef VK_USE_PLATFORM_METAL_EXT
vkCreateMetalSurfaceEXT = reinterpret_cast<PFN_vkCreateMetalSurfaceEXT>(dlsym(libvulkan, "vkCreateMetalSurfaceEXT"));
#endif
vkCreateDebugReportCallbackEXT = reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>(dlsym(libvulkan, "vkCreateDebugReportCallbackEXT"));
vkDestroyDebugReportCallbackEXT = reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>(dlsym(libvulkan, "vkDestroyDebugReportCallbackEXT"));
vkDebugReportMessageEXT = reinterpret_cast<PFN_vkDebugReportMessageEXT>(dlsym(libvulkan, "vkDebugReportMessageEXT"));
Expand Down Expand Up @@ -450,6 +456,10 @@ PFN_vkCreateAndroidSurfaceKHR vkCreateAndroidSurfaceKHR;
PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR vkGetPhysicalDeviceWin32PresentationSupportKHR;
#endif

#ifdef VK_USE_PLATFORM_METAL_EXT
PFN_vkCreateMetalSurfaceEXT vkCreateMetalSurfaceEXT;
#endif
PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT;
PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallbackEXT;
PFN_vkDebugReportMessageEXT vkDebugReportMessageEXT;
Expand Down
8 changes: 8 additions & 0 deletions WSIWindow/VulkanWrapper/vulkan_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ extern "C" {
#define VK_NO_PROTOTYPES 1
#include <vulkan/vulkan.h>

#ifdef VK_USE_PLATFORM_METAL_EXT
#include <vulkan/vulkan_metal.h>
#endif

/* Initialize the Vulkan function pointer variables declared in this header.
* Returns 0 if vulkan is not available, non-zero if it is available.
*/
Expand Down Expand Up @@ -229,6 +233,10 @@ extern PFN_vkCreateWin32SurfaceKHR vkCreateWin32SurfaceKHR;
extern PFN_vkGetPhysicalDeviceWin32PresentationSupportKHR vkGetPhysicalDeviceWin32PresentationSupportKHR;
#endif

#ifdef VK_USE_PLATFORM_METAL_EXT
extern PFN_vkCreateMetalSurfaceEXT vkCreateMetalSurfaceEXT;
#endif

// VK_EXT_debug_report
extern PFN_vkCreateDebugReportCallbackEXT vkCreateDebugReportCallbackEXT;
extern PFN_vkDestroyDebugReportCallbackEXT vkDestroyDebugReportCallbackEXT;
Expand Down
4 changes: 4 additions & 0 deletions WSIWindow/WSIWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "window_android.h"
#include "window_win32.h"
#include "window_xcb.h"
#include "window_cocoa.h"
//==============================================================

WSIWindow::WSIWindow(const char* title, const uint width, const uint height) {
Expand All @@ -36,6 +37,9 @@ WSIWindow::WSIWindow(const char* title, const uint width, const uint height) {
#elif VK_USE_PLATFORM_ANDROID_KHR
LOGI("PLATFORM: ANDROID\n");
pimpl = new Window_android(title, width, height);
#elif VK_USE_PLATFORM_METAL_EXT
LOGI("PLATFORM: COCOA\n");
pimpl = new Window_cocoa(title, width, height);
#endif
// TODO:
// #ifdef VK_USE_PLATFORM_XLIB_KHR
Expand Down
84 changes: 84 additions & 0 deletions WSIWindow/window_cocoa.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
*--------------------------------------------------------------------------
* Copyright (c) 2016 Valve Corporation
* Copyright (c) 2016 LunarG, Inc.
* Copyright (c) 2016-2017 Rene Lindsay
*
* 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.
*
* Author: Rene Lindsay <rjklindsay@gmail.com>
*
*--------------------------------------------------------------------------
*/

//==========================Cocoa===============================
#ifdef VK_USE_PLATFORM_METAL_EXT

#ifndef WINDOW_COCOA
#define WINDOW_COCOA
//-------------------------------------------------
#include "WindowImpl.h"

#ifdef __OBJC__
#import <Cocoa/Cocoa.h>
#else
typedef void* id;
#endif

//#pragma warning(disable:4996)
// clang-format off
// Convert native Cocoa keyboard scancode to cross-platform USB HID code.
const unsigned char COCOA_TO_HID[256] = {
//TODO
};
// clang-format on
//=============================Cocoa============================
class Window_cocoa : public WindowImpl {
id appDelegate;
id window;
id windowDelegate;
id view;
id layer;

void SetTitle(const char* title);
void SetWinPos (uint x, uint y);
void SetWinSize(uint w, uint h);
void CreateSurface(VkInstance instance);

public:
Window_cocoa(const char* title, uint width, uint height);
virtual ~Window_cocoa();
EventType GetEvent(bool wait_for_event = false);
bool CanPresent(VkPhysicalDevice phy, uint32_t queue_family); // check if this window can present this queue type

inline id getNativeAppDelegate() {
return appDelegate;
}

inline id getNativeWindow() {
return window;
}

inline id getNativeWindowDelegate() {
return windowDelegate;
}

inline id getNativeView() {
return view;
}
};
//==============================================================
#endif

#endif // VK_USE_PLATFORM_COCOA_KHR
//==============================================================
Loading