Skip to content

Commit

Permalink
Custom Main (#701)
Browse files Browse the repository at this point in the history
* Platform Context

* fix sample warning

* reset open_cl_interop

* fix android build

* review changes

* macro fixes
  • Loading branch information
tomadamatkinson authored Jul 17, 2023
1 parent 8ea55cc commit 4a18567
Show file tree
Hide file tree
Showing 37 changed files with 868 additions and 141 deletions.
4 changes: 2 additions & 2 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ else()
add_executable(${PROJECT_NAME} WIN32 ${SRC})
endif()

target_link_libraries(${PROJECT_NAME} PUBLIC apps plugins)
target_link_libraries(${PROJECT_NAME} PRIVATE vkb__core apps plugins)

# Create android project
if(ANDROID)
Expand Down Expand Up @@ -80,4 +80,4 @@ if(MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${SUFFIX} ${CMAKE_CURRENT_BINARY_DIR}/lib/${CONFIG_DIR}/${TARGET_ARCH})
set_target_properties(${PROJECT_NAME} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY_${SUFFIX} ${CMAKE_CURRENT_BINARY_DIR}/lib/${CONFIG_DIR}/${TARGET_ARCH})
endforeach()
endif()
endif()
52 changes: 26 additions & 26 deletions app/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (c) 2019-2021, Arm Limited and Contributors
/* Copyright (c) 2019-2023, Arm Limited and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -19,32 +19,34 @@
#include "platform/platform.h"
#include "plugins/plugins.h"

#if defined(VK_USE_PLATFORM_ANDROID_KHR)
#include <core/platform/entrypoint.hpp>

#if defined(PLATFORM__ANDROID)
# include "platform/android/android_platform.h"
void android_main(android_app *state)
{
vkb::AndroidPlatform platform{state};
#elif defined(VK_USE_PLATFORM_WIN32_KHR)
#elif defined(PLATFORM__WINDOWS)
# include "platform/windows/windows_platform.h"
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR lpCmdLine, INT nCmdShow)
{
vkb::WindowsPlatform platform{hInstance, hPrevInstance,
lpCmdLine, nCmdShow};
#elif defined(VK_USE_PLATFORM_DISPLAY_KHR)
#elif defined(PLATFORM__LINUX_D2D)
# include "platform/unix/unix_d2d_platform.h"
int main(int argc, char *argv[])
{
vkb::UnixD2DPlatform platform{argc, argv};
#else
#elif defined(PLATFORM__LINUX) || defined(PLATFORM__MACOS)
# include "platform/unix/unix_platform.h"
int main(int argc, char *argv[])
#else
# error "Platform not supported"
#endif

CUSTOM_MAIN(context)
{
# if defined(VK_USE_PLATFORM_METAL_EXT)
vkb::UnixPlatform platform{vkb::UnixType::Mac, argc, argv};
# elif defined(VK_USE_PLATFORM_XCB_KHR) || defined(VK_USE_PLATFORM_XLIB_KHR) || defined(VK_USE_PLATFORM_WAYLAND_KHR)
vkb::UnixPlatform platform{vkb::UnixType::Linux, argc, argv};
# endif
#if defined(PLATFORM__ANDROID)
vkb::AndroidPlatform platform{context};
#elif defined(PLATFORM__WINDOWS)
vkb::WindowsPlatform platform{context};
#elif defined(PLATFORM__LINUX_D2D)
vkb::UnixD2DPlatform platform{context};
#elif defined(PLATFORM__LINUX)
vkb::UnixPlatform platform{context, vkb::UnixType::Linux};
#elif defined(PLATFORM__MACOS)
vkb::UnixPlatform platform{context, vkb::UnixType::Mac};
#else
# error "Platform not supported"
#endif

auto code = platform.initialize(plugins::get_all());
Expand All @@ -56,7 +58,5 @@ int main(int argc, char *argv[])

platform.terminate(code);

#ifndef VK_USE_PLATFORM_ANDROID_KHR
return EXIT_SUCCESS;
#endif
}
return 0;
}
13 changes: 11 additions & 2 deletions components/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
# 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.
#

add_subdirectory(core)
add_subdirectory(core)

if(ANDROID)
add_subdirectory(android)
elseif(WIN32)
add_subdirectory(windows)
elseif(APPLE OR UNIX)
add_subdirectory(unix)
else()
message(FATAL_ERROR "Unsupported platform")
endif()
35 changes: 35 additions & 0 deletions components/android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) 2023, Thomas Atkinson
#
# SPDX-License-Identifier: Apache-2.0
#
# 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.

vkb__register_component(
NAME android_platform
HEADERS
include/android/context.hpp
SRC
src/context.cpp
src/entrypoint.cpp
LINK_LIBS
vkb__core
)

add_definitions(-DVULKAN_HPP_TYPESAFE_CONVERSION=1)

# Import game-activity static lib inside the game-activity_static prefab module.
find_package(game-activity REQUIRED CONFIG)
target_link_libraries(vkb__android_platform PUBLIC log android game-activity::game-activity_static)

# attach to core
target_link_libraries(vkb__core INTERFACE vkb__android_platform)
46 changes: 46 additions & 0 deletions components/android/include/android/context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Copyright (c) 2023, Thomas Atkinson
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#pragma once

#include <string>
#include <vector>

#include <core/platform/context.hpp>

#include <game-activity/native_app_glue/android_native_app_glue.h>

namespace vkb
{
/**
* @brief Android platform context
*
* @warning Use in extreme circumstances with code guarded by the PLATFORM__ANDROID define
*/
class AndroidPlatformContext final : public PlatformContext
{
public:
AndroidPlatformContext(android_app *app);
~AndroidPlatformContext() override = default;

android_app *app{nullptr};

static std::string android_external_storage_directory;
static std::string android_temp_directory;
static std::vector<std::string> android_arguments;
};
} // namespace vkb
69 changes: 69 additions & 0 deletions components/android/src/context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/* Copyright (c) 2023, Thomas Atkinson
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#include "android/context.hpp"

#include <jni.h>

extern "C"
{
JNIEXPORT void JNICALL
Java_com_khronos_vulkan_1samples_SampleLauncherActivity_initFilePath(JNIEnv *env, jobject thiz, jstring external_dir, jstring temp_dir)
{
const char *external_dir_cstr = env->GetStringUTFChars(external_dir, 0);
vkb::AndroidPlatformContext::android_external_storage_directory = std::string(external_dir_cstr) + "/";
env->ReleaseStringUTFChars(external_dir, external_dir_cstr);

const char *temp_dir_cstr = env->GetStringUTFChars(temp_dir, 0);
vkb::AndroidPlatformContext::android_temp_directory = std::string(temp_dir_cstr) + "/";
env->ReleaseStringUTFChars(temp_dir, temp_dir_cstr);
}

JNIEXPORT void JNICALL
Java_com_khronos_vulkan_1samples_SampleLauncherActivity_sendArgumentsToPlatform(JNIEnv *env, jobject thiz, jobjectArray arg_strings)
{
std::vector<std::string> args;

for (int i = 0; i < env->GetArrayLength(arg_strings); i++)
{
jstring arg_string = (jstring) (env->GetObjectArrayElement(arg_strings, i));

const char *arg = env->GetStringUTFChars(arg_string, 0);

args.push_back(std::string(arg));

env->ReleaseStringUTFChars(arg_string, arg);
}

vkb::AndroidPlatformContext::android_arguments = args;
}
}

namespace vkb
{
std::string AndroidPlatformContext::android_external_storage_directory = {};
std::string AndroidPlatformContext::android_temp_directory = {};
std::vector<std::string> AndroidPlatformContext::android_arguments = {};

AndroidPlatformContext::AndroidPlatformContext(android_app *app) :
PlatformContext{}, app{app}
{
_external_storage_directory = android_external_storage_directory;
_temp_directory = android_temp_directory;
_arguments = android_arguments;
}
} // namespace vkb
25 changes: 25 additions & 0 deletions components/android/src/entrypoint.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Copyright (c) 2023, Thomas Atkinson
*
* SPDX-License-Identifier: Apache-2.0
*
* 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.
*/

#include <core/platform/entrypoint.hpp>

#include "android/context.hpp"

std::unique_ptr<vkb::PlatformContext> create_platform_context(android_app *app)
{
return std::make_unique<vkb::AndroidPlatformContext>(app);
}
39 changes: 38 additions & 1 deletion components/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,41 @@ vkb__register_tests(
tests/strings.test.cpp
LINK_LIBS
vkb__core
)
)

if(ANDROID)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_ANDROID_KHR PLATFORM__ANDROID)
elseif(WIN32)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_WIN32_KHR PLATFORM__WINDOWS)
elseif(APPLE)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_METAL_EXT PLATFORM__MACOS)
elseif(UNIX)
target_compile_definitions(vkb__core PUBLIC PLATFORM__LINUX)

# Choose WSI based on VKB_WSI_SELECTION
if (VKB_WSI_SELECTION STREQUAL XCB OR VKB_WSI_SELECTION STREQUAL XLIB OR VKB_WSI_SELECTION STREQUAL WAYLAND)
find_package(PkgConfig REQUIRED)
endif()
if (VKB_WSI_SELECTION STREQUAL XCB)
pkg_check_modules(XCB xcb REQUIRED)
if (XCB_FOUND)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_XCB_KHR)
endif()
elseif (VKB_WSI_SELECTION STREQUAL XLIB)
pkg_check_modules(X11 x11 REQUIRED)
if (X11_FOUND)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_XLIB_KHR)
endif()
elseif (VKB_WSI_SELECTION STREQUAL WAYLAND)
pkg_check_modules(WAYLAND wayland-client REQUIRED)
if (WAYLAND_FOUND)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_WAYLAND_KHR)
endif()
elseif (VKB_WSI_SELECTION STREQUAL D2D)
set(DIRECT_TO_DISPLAY TRUE)
set(DIRECT_TO_DISPLAY TRUE PARENT_SCOPE)
target_compile_definitions(vkb__core PUBLIC VK_USE_PLATFORM_DISPLAY_KHR PLATFORM__LINUX_D2D)
else()
message(FATAL_ERROR "Unknown WSI")
endif()
endif()
30 changes: 29 additions & 1 deletion components/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,35 @@

Core is a collection of pure interfaces or small utilities which are used across the project. The core component is the only component which does not follow the component pattern in its entirety. The only major difference between core and other components is the header prefix used is `core/<sub_dir>` instead of `components/core/<sub_dir>`.

## Interfaces
## Platform

A Platform is the name we have given to the physical hardware and operating system that the project is executing on. We support multiple platforms which can be identified by the following defines

- `PLATFORM__ANDROID`
- `PLATFORM__WINDOWS`
- `PLATFORM__LINUX_D2D`
- `PLATFORM__LINUX`
- `PLATFORM__MACOS`

Using these platforms should be as transparent as possible to a sample. Components on the other hand may add platform specific code paths if required.

An application can create a cross platform entrypoint by using the `CUSTOM_MAIN(context_name)` macro

```cpp

#include <core/platform/entrypoint.hpp>

CUSTOM_MAIN(context)
{
context.arguments();
context.external_storage_directory();
context.temp_directory();

// Components using platform specific contexts
FileSystem fs = FileSystem::from_context(context);
}

```
## Utilities
Expand Down
Loading

0 comments on commit 4a18567

Please sign in to comment.