From 423721365886ff771b282d7199f353060d8b47c0 Mon Sep 17 00:00:00 2001 From: xiangsilian Date: Sat, 18 Oct 2025 21:13:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=9C=A8Android=E4=B8=8A?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E4=B8=89=E8=A7=92=E5=BD=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/assets/shaders/simple_shader.frag | 7 ++++ .../assets/shaders/simple_shader.frag.spv | Bin 0 -> 572 bytes .../main/assets/shaders/simple_shader.vert | 19 +++++++++ .../assets/shaders/simple_shader.vert.spv | Bin 0 -> 1504 bytes app/src/main/cpp/main.cpp | 3 ++ vulkan/AppBase.cpp | 26 +++++++++++- vulkan/Application.cpp | 38 +++--------------- 7 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 app/src/main/assets/shaders/simple_shader.frag create mode 100644 app/src/main/assets/shaders/simple_shader.frag.spv create mode 100644 app/src/main/assets/shaders/simple_shader.vert create mode 100644 app/src/main/assets/shaders/simple_shader.vert.spv 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 0000000000000000000000000000000000000000..da37f7ede672fe162b5322c32f5938a7836409a7 GIT binary patch literal 572 zcmYk2PfNo<6vUrx)7IAhv!FMrcod2U6+zU4NG^dYet=MtD1q3NHWj@2+5A*q1n0LV z(uK*}H#=`OEEHA71Yeo8+BI6MPEqd}+{a8uKdhAl0+aGA( z6gLqLrRPob_)qk0tMXUiuge|}IP@J=^z`Vvs`?d60nUc?u0MFnDF)EG3?WD_tXd~G(}N+zk@O66FYiu^P-@u*JZ3F?aDVc^abjOR}^*Rec7t&^?*uDZdsTY;mcUNHNK*l`ZeJhd1~N% zVfN;G@ULpjeZowB?r%b zYcjq?9UA(*E6?8CvAYBAl()eh+><8{x30*ScQxcN?q)-tnSB{^fj4Dv-1U~cFPo6D z4|rR~qW-=-Ie6;9J2glCpr4#8b{zD+OS{Tg%D)Ps4SXvn{ir=I+g-U4rsnOSK5 zit*&%2=S)4TVn2pb9*f{y*-f8*E^}{>5&XQJR17r{{#NBH0-%sb8=&DujZKfR8`FJ yM240e_srd~53@d%ah{wReSy)tzMd;~eKG5SjNakVF!!Mho_fv({7 #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());