diff --git a/samples/api/texture_loading/texture_loading.cpp b/samples/api/texture_loading/texture_loading.cpp index cb50fa7..f799542 100644 --- a/samples/api/texture_loading/texture_loading.cpp +++ b/samples/api/texture_loading/texture_loading.cpp @@ -1368,7 +1368,7 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) last_update_time = getCurrentTimeMillis(); - myFloatValue = 1.3f; + myFloatValue = 1.f; // --- 加载前景纹理 (示例) --- int width = 640; @@ -1376,30 +1376,39 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) int rowStride = width*4; auto testImage = generateSimpleTestImage(width, height, 80); size_t dataSize = testImage.size(); - processWithVulkan(testImage.data(), width, height, rowStride, dataSize, texture_point); + processWithVulkan(testImage.data(), width, height, rowStride, dataSize, texture_point, false); + + + //width = 640; + //height = 480; + //rowStride = width * 4; + //auto arrowImage = generateSimpleTestImage(width, height, 80); + //dataSize = arrowImage.size(); + //processWithVulkan(arrowImage.data(), width, height, rowStride, dataSize, cam_text); + + + +#if WIN32 + const char* filename = "assets/out.png"; + std::vector image; + unsigned w, h; + unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8); + processWithVulkan(image.data(), w, h, w * 4, image.size(), cam_text, true); +#else + const char* filename = "/sdcard/Android/data/com.khronos.vulkan_samples/files/assets/out.png"; + std::vector image; + unsigned w, h; + unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8); + processWithVulkan(image.data(), w, h, w*4, image.size(), cam_text, true); +#endif -//#ifdef _WIN32 - width = 640; - height = 480; - rowStride = width * 4; - auto arrowImage = generateSimpleTestImage(width, height, 80); - dataSize = arrowImage.size(); - processWithVulkan(arrowImage.data(), width, height, rowStride, dataSize, cam_text); -//#else -// -// const char* filename = "face.png"; -// std::vector image; -// unsigned w, h; -// unsigned error = lodepng::decode(image, w, h, filename, LCT_RGBA, 8); -// processWithVulkan(image.data(), w, h, w*4, image.size(), cam_text); -//#endif width = 256; height = 256; rowStride = width * 4; auto lineImage = generateSimpleTestImage(width, height, 80); dataSize = lineImage.size(); - processWithVulkan(lineImage.data(), width, height, rowStride, dataSize, texture_point_line); + processWithVulkan(lineImage.data(), width, height, rowStride, dataSize, texture_point_line, false); #ifdef _WIN32 const char* filename_test = "assets/DemoHeadBaseColor.png"; const char* filename_demo1 = "assets/demo1.png"; @@ -1418,27 +1427,27 @@ bool TextureLoading::prepare(const vkb::ApplicationOptions& options) std::vector image_test; unsigned w_t, h_t; unsigned error_t = lodepng::decode(image_test, w_t, h_t, filename_test, LCT_RGBA, 8); - processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), texture); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), texture, true); image_test.clear(); w_t, h_t; error_t = lodepng::decode(image_test, w_t, h_t, filename_demo1, LCT_RGBA, 8); - processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo1); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo1, true); image_test.clear(); w_t, h_t; error_t = lodepng::decode(image_test, w_t, h_t, filename_demo2, LCT_RGBA, 8); - processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo2); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo2, true); image_test.clear(); w_t, h_t; error_t = lodepng::decode(image_test, w_t, h_t, filename_demo3, LCT_RGBA, 8); - processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo3); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo3, true); image_test.clear(); w_t, h_t; error_t = lodepng::decode(image_test, w_t, h_t, filename_demo4, LCT_RGBA, 8); - processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo4); + processWithVulkan(image_test.data(), w_t, h_t, w_t * 4, image_test.size(), tex_demo4, true); //load_texture(); @@ -1718,7 +1727,7 @@ TextureLoading* TextureLoading::this_instance = nullptr; void TextureLoadProcessWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize) { TextureLoading::Texture& cam_tex = TextureLoading::Get()->cam_text; - TextureLoading::Get()->processWithVulkan(data, width, height, rowStride, dataSize, cam_tex); + TextureLoading::Get()->processWithVulkan(data, width, height, rowStride, dataSize, cam_tex, false); } @@ -1736,6 +1745,11 @@ void TextureLoading::extendPoint(float* pos, int start_id, int end_id, float rat void TextureLoading::update_face_vertex_buffer(float* pos, int pointCount) { + if (!prepared) + { + return; + } + std::lock_guard lock(mtx_point); last_update_time = getCurrentTimeMillis(); @@ -1765,14 +1779,14 @@ void TextureLoading::update_face_vertex_buffer(float* pos, int pointCount) vertex_buffer->update(obj_vertices.data(), vertex_buffer_size); } -void TextureLoading::processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture) +void TextureLoading::processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture, bool srgb) { std::unique_lock lock(mtx); VkDevice& device = get_device().get_handle(); const VkPhysicalDevice& physicalDevice = get_device().get_gpu().get_handle(); if (out_texture.image == VK_NULL_HANDLE) { - createTexture(device, physicalDevice, width, height, out_texture); + createTexture(device, physicalDevice, width, height, out_texture, srgb); } @@ -1784,7 +1798,7 @@ void TextureLoading::processWithVulkan(uint8_t* data, int width, int height, int } // --- 以下函数保持不变 --- -void TextureLoading::createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture) +void TextureLoading::createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture, bool srgb) { texture.width = width; texture.height = height; @@ -1793,7 +1807,14 @@ void TextureLoading::createTexture(VkDevice device, VkPhysicalDevice physicalDev VkImageCreateInfo imageInfo = {}; imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;// VK_FORMAT_R8G8B8A8_SRGB; //VK_FORMAT_R8G8B8A8_UNORM; + if (srgb) + { + imageInfo.format = VK_FORMAT_R8G8B8A8_SRGB; + } + else + { + imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;// VK_FORMAT_R8G8B8A8_SRGB; //VK_FORMAT_R8G8B8A8_UNORM; + } imageInfo.extent.width = width; imageInfo.extent.height = height; imageInfo.extent.depth = 1; @@ -1830,7 +1851,14 @@ void TextureLoading::createTexture(VkDevice device, VkPhysicalDevice physicalDev viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; viewInfo.image = texture.image; viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; - viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;// VK_FORMAT_R8G8B8A8_SRGB; // VK_FORMAT_R8G8B8A8_UNORM; + if (srgb) + { + viewInfo.format = VK_FORMAT_R8G8B8A8_SRGB; + } + else + { + viewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;// VK_FORMAT_R8G8B8A8_SRGB; // VK_FORMAT_R8G8B8A8_UNORM; + } viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; viewInfo.subresourceRange.baseMipLevel = 0; viewInfo.subresourceRange.levelCount = 1; @@ -2098,6 +2126,11 @@ void TextureLoading::transitionImageLayout(VkCommandBuffer commandBuffer, VkImag void TextureLoading::update_point_vertex_buffer(float* pos, int pointCount) { + if (!prepared) + { + return; + } + std::lock_guard lock(mtx_point); if (pointCount <= 0) { point_count = 0; diff --git a/samples/api/texture_loading/texture_loading.h b/samples/api/texture_loading/texture_loading.h index a1d0221..ab9bb4c 100644 --- a/samples/api/texture_loading/texture_loading.h +++ b/samples/api/texture_loading/texture_loading.h @@ -183,8 +183,8 @@ public: virtual void view_changed() override; virtual void on_update_ui_overlay(vkb::Drawer& drawer) override; - void processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture); - void createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture); + void processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture, bool srgb); + void createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture, bool srgb); void updateTexture(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, VkQueue queue, uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& texture); uint32_t findMemoryType(VkPhysicalDevice physicalDevice, uint32_t typeFilter, VkMemoryPropertyFlags properties); VkCommandBuffer beginSingleTimeCommands(VkDevice device, VkCommandPool commandPool);