Android 编译通过,但肯定运行不了
This commit is contained in:
Generated
+6
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
@@ -8,10 +8,16 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
|
|
||||||
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
|
if(CMAKE_SYSTEM_NAME STREQUAL "Android")
|
||||||
message(STATUS "Building for Android")
|
message(STATUS "Building for Android")
|
||||||
|
|
||||||
|
include_directories(
|
||||||
|
|
||||||
|
)
|
||||||
|
|
||||||
add_library(sample SHARED
|
add_library(sample SHARED
|
||||||
app/src/main/cpp/main.cpp
|
app/src/main/cpp/main.cpp
|
||||||
app/src/main/cpp/AndroidOut.cpp
|
app/src/main/cpp/AndroidOut.cpp
|
||||||
|
vulkan/Application.h
|
||||||
|
vulkan/Application.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
find_package(game-activity REQUIRED CONFIG)
|
find_package(game-activity REQUIRED CONFIG)
|
||||||
@@ -20,11 +26,14 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Android")
|
|||||||
set(CMAKE_SHARED_LINKER_FLAGS
|
set(CMAKE_SHARED_LINKER_FLAGS
|
||||||
"${CMAKE_SHARED_LINKER_FLAGS} -u Java_com_google_androidgamesdk_GameActivity_initializeNativeCode")
|
"${CMAKE_SHARED_LINKER_FLAGS} -u Java_com_google_androidgamesdk_GameActivity_initializeNativeCode")
|
||||||
|
|
||||||
|
find_package(Vulkan REQUIRED)
|
||||||
|
|
||||||
target_link_libraries(sample
|
target_link_libraries(sample
|
||||||
game-activity::game-activity_static
|
game-activity::game-activity_static
|
||||||
|
Vulkan::Vulkan
|
||||||
android
|
android
|
||||||
log)
|
log)
|
||||||
|
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+17
-3
@@ -10,7 +10,7 @@ void Application::createInstance()
|
|||||||
|
|
||||||
VkApplicationInfo appInfo{};
|
VkApplicationInfo appInfo{};
|
||||||
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
|
||||||
appInfo.pApplicationName = "Hello Triangle";
|
appInfo.pApplicationName = "Sample";
|
||||||
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||||
appInfo.pEngineName = "No Engine";
|
appInfo.pEngineName = "No Engine";
|
||||||
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
||||||
@@ -20,6 +20,7 @@ void Application::createInstance()
|
|||||||
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
|
||||||
createInfo.pApplicationInfo = &appInfo;
|
createInfo.pApplicationInfo = &appInfo;
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
// 获取 GLFW 所需的扩展
|
// 获取 GLFW 所需的扩展
|
||||||
uint32_t glfwExtensionCount = 0;
|
uint32_t glfwExtensionCount = 0;
|
||||||
const char** glfwExtensions;
|
const char** glfwExtensions;
|
||||||
@@ -33,6 +34,7 @@ void Application::createInstance()
|
|||||||
|
|
||||||
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
createInfo.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
|
||||||
createInfo.ppEnabledExtensionNames = extensions.data();
|
createInfo.ppEnabledExtensionNames = extensions.data();
|
||||||
|
#endif
|
||||||
|
|
||||||
if (enableValidationLayers) {
|
if (enableValidationLayers) {
|
||||||
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
createInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
|
||||||
@@ -49,12 +51,16 @@ void Application::createInstance()
|
|||||||
|
|
||||||
void Application::initWindow()
|
void Application::initWindow()
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
glfwInit();
|
glfwInit();
|
||||||
|
|
||||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||||
|
|
||||||
window = glfwCreateWindow(kWidth, kHeight, "Vulkan", nullptr, nullptr);
|
window = glfwCreateWindow(kWidth, kHeight, "Vulkan", nullptr, nullptr);
|
||||||
|
#else
|
||||||
|
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::run()
|
void Application::run()
|
||||||
@@ -66,9 +72,11 @@ void Application::run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Application::createSurface() {
|
void Application::createSurface() {
|
||||||
|
#ifdef _WIN32
|
||||||
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
|
if (glfwCreateWindowSurface(instance, window, nullptr, &surface) != VK_SUCCESS) {
|
||||||
throw std::runtime_error("failed to create window surface!");
|
throw std::runtime_error("failed to create window surface!");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
||||||
@@ -202,11 +210,15 @@ void Application::createCommandBuffer() {
|
|||||||
|
|
||||||
void Application::mainLoop()
|
void Application::mainLoop()
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
while (!glfwWindowShouldClose(window))
|
while (!glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
drawFrame(); // 在这里调用
|
drawFrame(); // 在这里调用
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
|
||||||
|
#endif
|
||||||
vkDeviceWaitIdle(device); // 等待设备空闲
|
vkDeviceWaitIdle(device); // 等待设备空闲
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,8 +429,8 @@ void Application::createPipelineLayout() {
|
|||||||
|
|
||||||
void Application::createGraphicsPipeline() {
|
void Application::createGraphicsPipeline() {
|
||||||
// 顶点着色器和片段着色器
|
// 顶点着色器和片段着色器
|
||||||
auto vertShaderCode = readFile("D:/Graphics/vulkan_demo/shaders/simple_shader.vert.spv");
|
auto vertShaderCode = readFile("shaders/simple_shader.vert.spv");
|
||||||
auto fragShaderCode = readFile("D:/Graphics/vulkan_demo/shaders/simple_shader.frag.spv");
|
auto fragShaderCode = readFile("shaders/simple_shader.frag.spv");
|
||||||
|
|
||||||
// 创建着色器模块
|
// 创建着色器模块
|
||||||
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
VkShaderModule vertShaderModule = createShaderModule(vertShaderCode);
|
||||||
@@ -727,9 +739,11 @@ void Application::cleanup() {
|
|||||||
// 销毁 Vulkan 实例
|
// 销毁 Vulkan 实例
|
||||||
vkDestroyInstance(instance, nullptr);
|
vkDestroyInstance(instance, nullptr);
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
// 销毁 GLFW 窗口
|
// 销毁 GLFW 窗口
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
|
|
||||||
// 终止 GLFW
|
// 终止 GLFW
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
+11
-4
@@ -1,8 +1,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#define GLFW_INCLUDE_VULKAN
|
#ifdef _WIN32
|
||||||
#include <GLFW/glfw3.h>
|
#define GLFW_INCLUDE_VULKAN
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
#else
|
||||||
|
#include <vulkan/vulkan.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -61,7 +66,9 @@ public:
|
|||||||
void run();
|
void run();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
#ifdef _WIN32
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
|
#endif
|
||||||
VkInstance instance;
|
VkInstance instance;
|
||||||
|
|
||||||
void initWindow();
|
void initWindow();
|
||||||
@@ -113,6 +120,6 @@ private:
|
|||||||
const std::vector<const char*> validationLayers = {
|
const std::vector<const char*> validationLayers = {
|
||||||
"VK_LAYER_KHRONOS_validation"
|
"VK_LAYER_KHRONOS_validation"
|
||||||
};
|
};
|
||||||
const int kWidth = 480;
|
const uint32_t kWidth = 480;
|
||||||
const int kHeight = 480;
|
const uint32_t kHeight = 480;
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user