105 lines
3.1 KiB
C++
105 lines
3.1 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>
|
|
#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> readFileEx(const std::string& enginePath);
|
|
//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"};
|
|
protected:
|
|
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;
|
|
#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
|