diff --git a/app/src/main/assets/shaders/simple_shader.frag b/app/src/main/assets/shaders/simple_shader.frag new file mode 100644 index 0000000..fcffbbf --- /dev/null +++ b/app/src/main/assets/shaders/simple_shader.frag @@ -0,0 +1,7 @@ +#version 450 +layout(location = 0) in vec3 fragColor; +layout(location = 0) out vec4 outColor; + +void main() { + outColor = vec4(fragColor, 1.0); +} \ No newline at end of file diff --git a/app/src/main/assets/shaders/simple_shader.frag.spv b/app/src/main/assets/shaders/simple_shader.frag.spv new file mode 100644 index 0000000..da37f7e Binary files /dev/null and b/app/src/main/assets/shaders/simple_shader.frag.spv differ diff --git a/app/src/main/assets/shaders/simple_shader.vert b/app/src/main/assets/shaders/simple_shader.vert new file mode 100644 index 0000000..f66e990 --- /dev/null +++ b/app/src/main/assets/shaders/simple_shader.vert @@ -0,0 +1,19 @@ +#version 450 +layout(location = 0) out vec3 fragColor; + +vec2 positions[3] = vec2[]( + vec2(0.0, -0.5), + vec2(0.5, 0.5), + vec2(-0.5, 0.5) +); + +vec3 colors[3] = vec3[]( + vec3(1.0, 0.0, 0.0), + vec3(0.0, 1.0, 0.0), + vec3(0.0, 0.0, 1.0) +); + +void main() { + gl_Position = vec4(positions[gl_VertexIndex], 0.0, 1.0); + fragColor = colors[gl_VertexIndex]; +} \ No newline at end of file diff --git a/app/src/main/assets/shaders/simple_shader.vert.spv b/app/src/main/assets/shaders/simple_shader.vert.spv new file mode 100644 index 0000000..cf7123f Binary files /dev/null and b/app/src/main/assets/shaders/simple_shader.vert.spv differ diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp index 4fab6a7..9559761 100644 --- a/app/src/main/cpp/main.cpp +++ b/app/src/main/cpp/main.cpp @@ -4,6 +4,7 @@ #include #include "AndroidOut.h" #include "Application.h" +#include #include #include @@ -12,6 +13,7 @@ extern "C" { android_app* g_android_app = nullptr; Application* g_Application = nullptr; +AAssetManager* g_assetManager = nullptr; void handle_cmd(android_app *pApp, int32_t cmd) { switch (cmd) { @@ -30,6 +32,7 @@ void handle_cmd(android_app *pApp, int32_t cmd) { void android_main(struct android_app *pApp) { aout << "Welcome to android_main" << std::endl; g_android_app = pApp; + g_assetManager = pApp->activity->assetManager; Application application; g_Application = &application; pApp->onAppCmd = handle_cmd; diff --git a/vulkan/AppBase.cpp b/vulkan/AppBase.cpp index 1aa2a6d..3f22d2e 100644 --- a/vulkan/AppBase.cpp +++ b/vulkan/AppBase.cpp @@ -1,14 +1,23 @@ #include "AppBase.h" #include +#ifdef _WIN32 +#else +#include +extern AAssetManager* g_assetManager; +#endif // _WIN32 + void VK_CHECK(VkResult ret) { assert(ret == VK_SUCCESS); } -std::vector AppBase::readFile(const std::string& enginePath) +std::vector AppBase::readFile(const std::string& path) { + std::vector buffer; +#ifdef _WIN32 + std::string enginePath = "app/src/main/assets/" + path; std::ifstream file{ enginePath, std::ios::ate | std::ios::binary }; if (!file.is_open()) @@ -17,13 +26,26 @@ std::vector AppBase::readFile(const std::string& enginePath) } size_t fileSize = static_cast(file.tellg()); - std::vector buffer(fileSize); + buffer.resize(fileSize); file.seekg(0); file.read(buffer.data(), fileSize); file.close(); +#else + AAsset* asset = AAssetManager_open(g_assetManager, path.c_str(), AASSET_MODE_BUFFER); + if (!asset) { + logOut << "Failed to load file: " << path.c_str() << std::endl; + return buffer; + } + + size_t length = AAsset_getLength(asset); + buffer.resize(length); + AAsset_read(asset, buffer.data(), length); + AAsset_close(asset); +#endif // _WIN32 return buffer; } + VkShaderModule AppBase::createShaderModule(VkDevice& device, const std::vector& code) { VkShaderModuleCreateInfo createInfo{}; diff --git a/vulkan/Application.cpp b/vulkan/Application.cpp index 11c61a9..bb8e964 100644 --- a/vulkan/Application.cpp +++ b/vulkan/Application.cpp @@ -181,47 +181,22 @@ void Application::createLogicalDevice() { vkGetDeviceQueue(device, indices.presentFamily.value(), 0, &presentQueue); } -inline VkExtent2D choose_extent( - VkExtent2D request_extent, - const VkExtent2D &min_image_extent, - const VkExtent2D &max_image_extent, - const VkExtent2D ¤t_extent) -{ - if (current_extent.width == 0xFFFFFFFF) - { - return request_extent; - } - - if (request_extent.width < 1 || request_extent.height < 1) - { - logOut << "(Swapchain) Image extent ("<< request_extent.width << ", " << request_extent.height << ") not supported. Selecting ("<< current_extent.width << ", " << current_extent.height << ")." << std::endl; - return current_extent; - } - - request_extent.width = std::max(request_extent.width, min_image_extent.width); - request_extent.width = std::min(request_extent.width, max_image_extent.width); - - request_extent.height = std::max(request_extent.height, min_image_extent.height); - request_extent.height = std::min(request_extent.height, max_image_extent.height); - - return request_extent; -} void Application::createSwapChain() { // 选择交换链格式、颜色空间和分辨率 - VkSurfaceFormatKHR surfaceFormat = { VK_FORMAT_B8G8R8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; + VkSurfaceFormatKHR surfaceFormat = { VK_FORMAT_R8G8B8A8_SRGB, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; - VkExtent2D extent = { 1, 1 }; - VkSurfaceCapabilitiesKHR surface_capabilities{}; vkGetPhysicalDeviceSurfaceCapabilitiesKHR(this->physicalDevice, this->surface, &surface_capabilities); - extent = choose_extent(extent, surface_capabilities.minImageExtent, surface_capabilities.maxImageExtent, surface_capabilities.currentExtent); + VkExtent2D extent = surface_capabilities.currentExtent; + + uint32_t imageCount = 2; // 创建交换链 VkSwapchainCreateInfoKHR createInfo{}; createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; createInfo.surface = surface; - createInfo.minImageCount = 2; // 双缓冲 + createInfo.minImageCount = imageCount; // 双缓冲 createInfo.imageFormat = surfaceFormat.format; createInfo.imageColorSpace = surfaceFormat.colorSpace; createInfo.imageExtent = extent; @@ -252,8 +227,7 @@ void Application::createSwapChain() { throw std::runtime_error("failed to create swap chain!"); } - // 获取交换链图像 - uint32_t imageCount; + vkGetSwapchainImagesKHR(device, swapChain, &imageCount, nullptr); swapChainImages.resize(imageCount); vkGetSwapchainImagesKHR(device, swapChain, &imageCount, swapChainImages.data());