android 表面,扩展,实例都创建成功
This commit is contained in:
+27
-1
@@ -1,4 +1,11 @@
|
||||
#include "AppBase.h"
|
||||
#include <assert.h>
|
||||
|
||||
|
||||
void VK_CHECK(VkResult ret)
|
||||
{
|
||||
assert(ret == VK_SUCCESS);
|
||||
}
|
||||
|
||||
std::vector<char> AppBase::readFile(const std::string& enginePath)
|
||||
{
|
||||
@@ -55,6 +62,18 @@ bool AppBase::checkValidationLayerSupport() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void checkInstanceExtensions() {
|
||||
uint32_t extensionCount = 0;
|
||||
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
|
||||
|
||||
std::vector<VkExtensionProperties> extensions(extensionCount);
|
||||
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, extensions.data());
|
||||
|
||||
logOut << "Android Available instance extensions:" << std::endl;
|
||||
for (const auto& extension : extensions) {
|
||||
logOut << "ExtensionName:" << extension.extensionName << " Version:" << extension.specVersion << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void AppBase::createInstance()
|
||||
{
|
||||
@@ -84,7 +103,14 @@ void AppBase::createInstance()
|
||||
for(int i = 0; i < glfwExtensionCount; i++) {
|
||||
extensions.push_back(glfwExtensions[i]);
|
||||
}
|
||||
#else
|
||||
checkInstanceExtensions();
|
||||
// Android: 手动添加必要的扩展
|
||||
extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
||||
extensions.push_back(VK_KHR_ANDROID_SURFACE_EXTENSION_NAME);
|
||||
|
||||
// 可选:添加这些扩展以获得更好的兼容性
|
||||
extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
|
||||
#endif
|
||||
|
||||
if (enableValidationLayers) {
|
||||
@@ -160,7 +186,7 @@ QueueFamilyIndices AppBase::findQueueFamilies(VkPhysicalDevice device, VkSurface
|
||||
indices.graphicsFamily = i;
|
||||
}
|
||||
VkBool32 presentSupport = false;
|
||||
vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);
|
||||
VK_CHECK(vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport));
|
||||
if (presentSupport) {
|
||||
indices.presentFamily = i;
|
||||
}
|
||||
|
||||
+17
-10
@@ -1,23 +1,28 @@
|
||||
#ifndef VULKAN_UTILS_H
|
||||
#define VULKAN_UTILS_H
|
||||
|
||||
#define VK_CHECK(x) \
|
||||
do \
|
||||
{ \
|
||||
VkResult err = x; \
|
||||
if (err) \
|
||||
{ \
|
||||
assert(false); \
|
||||
} \
|
||||
} while (0)
|
||||
//#define VK_CHECK(x) \
|
||||
// do \
|
||||
// { \
|
||||
// VkResult ret = x; \
|
||||
// if (ret != VK_SUCCESS) \
|
||||
// { \
|
||||
// assert(false); \
|
||||
// } \
|
||||
// } while (0)
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
#define logOut std::cout
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
#else
|
||||
#include "../app/src/main/cpp/AndroidOut.h"
|
||||
#define logOut aout
|
||||
#define VK_USE_PLATFORM_ANDROID_KHR
|
||||
#include <vulkan/vulkan.h>
|
||||
#include <vulkan/vulkan_android.h>
|
||||
#include <android/native_window.h>
|
||||
#endif
|
||||
|
||||
|
||||
@@ -29,7 +34,9 @@
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <set>
|
||||
#include <assert.h>
|
||||
|
||||
void VK_CHECK(VkResult ret);
|
||||
|
||||
|
||||
struct QueueFamilyIndices {
|
||||
std::optional<uint32_t> graphicsFamily;
|
||||
|
||||
+10
-10
@@ -44,7 +44,7 @@ void Application::createSurface() {
|
||||
}
|
||||
#else
|
||||
|
||||
VkSurfaceKHR surface{};
|
||||
//VkSurfaceKHR surface{};
|
||||
|
||||
VkAndroidSurfaceCreateInfoKHR info{VK_STRUCTURE_TYPE_ANDROID_SURFACE_CREATE_INFO_KHR};
|
||||
|
||||
@@ -73,6 +73,7 @@ void Application::initVulkan()
|
||||
createCommandPool(); // 创建命令池
|
||||
createCommandBuffer(); // 创建命令缓冲区
|
||||
createSyncObjects(); // 创建同步对象
|
||||
inited = true;
|
||||
}
|
||||
|
||||
void Application::createImageViews() {
|
||||
@@ -259,6 +260,7 @@ void Application::createSwapChain() {
|
||||
|
||||
swapChainImageFormat = surfaceFormat.format;
|
||||
swapChainExtent = extent;
|
||||
MAX_FRAMES_IN_FLIGHT = imageCount;
|
||||
}
|
||||
|
||||
void Application::createPipelineLayout() {
|
||||
@@ -447,12 +449,11 @@ void Application::createCommandBuffer()
|
||||
|
||||
void Application::createSyncObjects()
|
||||
{
|
||||
int swapChainImageCount = swapChainImages.size();
|
||||
|
||||
imageAvailableSemaphores.resize(swapChainImageCount);
|
||||
renderFinishedSemaphores.resize(swapChainImageCount);
|
||||
imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);
|
||||
imagesInFlight.resize(swapChainImageCount, VK_NULL_HANDLE);
|
||||
imagesInFlight.resize(MAX_FRAMES_IN_FLIGHT, VK_NULL_HANDLE);
|
||||
|
||||
VkSemaphoreCreateInfo semaphoreInfo{};
|
||||
semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
@@ -462,7 +463,7 @@ void Application::createSyncObjects()
|
||||
fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
|
||||
// 为每个交换链图像创建信号量
|
||||
for (size_t i = 0; i < swapChainImageCount; i++) {
|
||||
for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
|
||||
if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||
|
||||
vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to create synchronization objects!");
|
||||
@@ -516,15 +517,14 @@ void Application::recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t im
|
||||
}
|
||||
}
|
||||
|
||||
void Application::drawFrame() {
|
||||
void Application::drawFrame()
|
||||
{
|
||||
// 1. 等待前一帧完成
|
||||
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
|
||||
|
||||
// 2. 获取交换链图像 - 使用当前帧的信号量
|
||||
uint32_t imageIndex;
|
||||
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX,
|
||||
imageAvailableSemaphores[currentFrame],
|
||||
VK_NULL_HANDLE, &imageIndex);
|
||||
VkResult result = vkAcquireNextImageKHR(device, swapChain, UINT64_MAX,imageAvailableSemaphores[currentFrame],VK_NULL_HANDLE, &imageIndex);
|
||||
|
||||
if (result != VK_SUCCESS) {
|
||||
throw std::runtime_error("failed to acquire swap chain image!");
|
||||
|
||||
@@ -11,7 +11,6 @@ class Application : public AppBase
|
||||
public:
|
||||
void run();
|
||||
|
||||
protected:
|
||||
void initVulkan();
|
||||
void initWindow();
|
||||
void createSurface();
|
||||
@@ -31,6 +30,7 @@ protected:
|
||||
void recordCommandBuffer(VkCommandBuffer commandBuffer, uint32_t imageIndex);
|
||||
void createSyncObjects();
|
||||
void drawFrame();
|
||||
bool isInited() { return inited; }
|
||||
private:
|
||||
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE; // 物理设备
|
||||
VkDevice device; // 逻辑设备
|
||||
@@ -55,4 +55,5 @@ private:
|
||||
std::vector<VkFence> imagesInFlight; // 跟踪每个图像的使用状态
|
||||
int currentFrame = 0;
|
||||
int MAX_FRAMES_IN_FLIGHT = 2;
|
||||
bool inited = false;
|
||||
};
|
||||
Reference in New Issue
Block a user