Files
face_sdk/vulkan/AppBase.h
2025-10-28 22:24:24 +08:00

123 lines
3.7 KiB
C++

#ifndef VULKAN_UTILS_H
#define VULKAN_UTILS_H
//#define VK_CHECK(x) \
// do \
// { \
// VkResult ret = x; \
// if (ret != VK_SUCCESS) \
// { \
// assert(false); \
// } \
// } while (0)
//#include <volk.h>
#ifdef _WIN32
#define logOut std::cout
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#include <windows.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
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <cstdlib>
#include <vector>
#include <optional>
#include <set>
#include <thread>
#include <mutex>
#include <chrono>
#include <ctime>
#include <cmath>
#include <unordered_map>
#include <array>
void VK_CHECK(VkResult ret);
struct QueueFamilyIndices {
std::optional<uint32_t> graphicsFamily;
std::optional<uint32_t> presentFamily;
bool isComplete() {
return graphicsFamily.has_value() && presentFamily.has_value();
}
};
struct TextureLoadingVertexStructure
{
float pos[3];
float uv[2];
float normal[3];
};
class AppBase
{
public:
std::vector<char> readFile(const std::string& enginePath);
std::vector<unsigned char> readFileUnsignedChar(const std::string& enginePath, bool example);
//std::string getPath(const std::string path);
VkShaderModule createShaderModule(VkDevice& device, const std::vector<char>& code);
const std::vector<const char*> validationLayers = {"VK_LAYER_KHRONOS_validation"};
long long getCurrentTimeMillis() {
auto now = std::chrono::system_clock::now();
auto duration = now.time_since_epoch();
return std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
}
VkInstance instance;
void createInstance();
bool checkValidationLayerSupport();
void setupDebugMessenger();
bool checkSwapChainSupport(VkPhysicalDevice device, VkSurfaceKHR& surface);
bool checkDeviceExtensionSupport(VkPhysicalDevice device);
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device, VkSurfaceKHR& surface);
bool isDeviceSuitable(VkPhysicalDevice device, VkSurfaceKHR& surface);
void pickPhysicalDevice(VkPhysicalDevice& physicalDevice, VkSurfaceKHR& surface);
#ifdef _WIN32
GLFWwindow *window;
std::vector<unsigned char> readFileUnsignedCharWin32(const std::wstring path, bool example);
static std::wstring UTF8ToWideString(const std::string& utf8str)
{
if (utf8str.empty()) return L"";
int wideLen = MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(),
-1, nullptr, 0);
if (wideLen == 0) {
throw std::runtime_error("UTF-8转换失败");
}
std::vector<wchar_t> buffer(wideLen);
MultiByteToWideChar(CP_UTF8, 0, utf8str.c_str(), -1,
buffer.data(), wideLen);
return std::wstring(buffer.data());
}
#endif
bool enableValidationLayers = true;
VkDebugUtilsMessengerEXT debugMessenger;
// 计算面的法线
void calculateFaceNormal(const TextureLoadingVertexStructure& v0, const TextureLoadingVertexStructure& v1, const TextureLoadingVertexStructure& v2, float* outNormal);
// 计算所有顶点的法线
void calculateVertexNormals(std::vector<TextureLoadingVertexStructure>& obj_vertices, const std::vector<uint32_t>& obj_indices);
};
#endif // VULKAN_UTILS_H