save code

This commit is contained in:
xsl
2025-12-20 22:34:23 +08:00
parent b6ac86a134
commit 7b07e7fbc1
15 changed files with 265 additions and 194 deletions
+50 -44
View File
@@ -2,7 +2,7 @@
#include "lodepng.h"
#ifdef _WIN32
#include <codecvt>
#else
#include <game-activity/native_app_glue/android_native_app_glue.h>
#include <game-activity/GameActivity.h>
@@ -658,13 +658,13 @@ void Application::loadTexture(std::string path, Texture& tex, bool srgb, VkComma
std::vector<unsigned char> image;
unsigned w, h;
unsigned error = lodepng::decode(image, w, h, data, LCT_RGBA, 8);
processWithVulkan(image.data(), w, h, w * 4, image.size(), tex, srgb, pool);
processWithVulkan(image.data(), w, h, w * 4, image.size(), tex, srgb, pool, path);
//return tex;
}
void Application::loadTexture(std::vector<unsigned char>& image_data, size_t image_size, int w, int h, Texture& tex, bool srgb, VkCommandPool pool)
void Application::loadTexture(std::vector<unsigned char>& image_data, size_t image_size, int w, int h, Texture& tex, bool srgb, VkCommandPool pool, std::string path)
{
processWithVulkan(image_data.data(), w, h, w * 4, image_size, tex, srgb, pool);
processWithVulkan(image_data.data(), w, h, w * 4, image_size, tex, srgb, pool, path);
//return tex;
}
@@ -675,7 +675,9 @@ void Application::loadTextureExample(std::wstring path, Texture& tex, bool srgb,
std::vector<unsigned char> image;
unsigned w, h;
unsigned error = lodepng::decode(image, w, h, data, LCT_RGBA, 8);
processWithVulkan(image.data(), w, h, w * 4, image.size(), tex, srgb, pool);
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::string str = converter.to_bytes(path);
processWithVulkan(image.data(), w, h, w * 4, image.size(), tex, srgb, pool, str);
//return tex;
}
@@ -684,7 +686,7 @@ void Application::loadTextureExample(std::wstring path, Texture& tex, bool srgb,
void Application::createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture, bool srgb)
void Application::createTexture(VkDevice device, VkPhysicalDevice physicalDevice, int width, int height, Texture& texture, bool srgb, std::string tex_path, size_t dataSize)
{
texture.width = width;
texture.height = height;
@@ -731,6 +733,8 @@ void Application::createTexture(VkDevice device, VkPhysicalDevice physicalDevice
throw std::runtime_error("Failed to allocate image memory!");
}
std::cout << "vkAllocateMemory device_memory " << tex_path << ": " << texture.device_memory << std::endl;
vkBindImageMemory(device, texture.image, texture.device_memory, 0);
VkImageViewCreateInfo viewInfo = {};
@@ -779,21 +783,56 @@ void Application::createTexture(VkDevice device, VkPhysicalDevice physicalDevice
throw std::runtime_error("Failed to create texture sampler!");
}
if (texture.stagingBuffer == nullptr)
{
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = dataSize;
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, nullptr, &texture.stagingBuffer) != VK_SUCCESS)
{
throw std::runtime_error("Failed to create staging buffer!");
}
}
if (texture.stagingBufferMemory == nullptr)
{
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, texture.stagingBuffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(physicalDevice,
memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (vkAllocateMemory(device, &allocInfo, nullptr, &texture.stagingBufferMemory) != VK_SUCCESS)
{
throw std::runtime_error("Failed to allocate staging buffer memory!");
}
std::cout << "vkAllocateMemory stagingBufferMemory " << tex_path << ": " << texture.stagingBufferMemory << std::endl;
vkBindBufferMemory(device, texture.stagingBuffer, texture.stagingBufferMemory, 0);
}
texture.image_layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
texture.device = device;
texture.texture_path = tex_path;
}
void Application::processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& out_texture, bool srgb, VkCommandPool pool)
void Application::processWithVulkan(uint8_t* data, int width, int height, int rowStride, size_t dataSize, Texture& texture, bool srgb, VkCommandPool pool, std::string tex_path)
{
std::unique_lock<std::mutex> lock(mtx);
if (out_texture.image == VK_NULL_HANDLE)
if (texture.image == VK_NULL_HANDLE)
{
createTexture(device, physicalDevice, width, height, out_texture, srgb);
createTexture(device, physicalDevice, width, height, texture, srgb, tex_path, dataSize);
}
updateTexture(device, physicalDevice, pool, graphicsQueue, data, width, height,
rowStride, dataSize, out_texture);
rowStride, dataSize, texture);
}
@@ -842,39 +881,6 @@ void Application::updateTexture(VkDevice device, VkPhysicalDevice physicalDevice
//VkBuffer stagingBuffer;
//VkDeviceMemory stagingBufferMemory;
if (texture.stagingBuffer == nullptr)
{
VkBufferCreateInfo bufferInfo = {};
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferInfo.size = dataSize;
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
if (vkCreateBuffer(device, &bufferInfo, nullptr, &texture.stagingBuffer) != VK_SUCCESS)
{
throw std::runtime_error("Failed to create staging buffer!");
}
}
if (texture.stagingBufferMemory == nullptr)
{
VkMemoryRequirements memRequirements;
vkGetBufferMemoryRequirements(device, texture.stagingBuffer, &memRequirements);
VkMemoryAllocateInfo allocInfo = {};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = findMemoryType(physicalDevice,
memRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
if (vkAllocateMemory(device, &allocInfo, nullptr, &texture.stagingBufferMemory) != VK_SUCCESS)
{
throw std::runtime_error("Failed to allocate staging buffer memory!");
}
vkBindBufferMemory(device, texture.stagingBuffer, texture.stagingBufferMemory, 0);
}
void* mappedData;
vkMapMemory(device, texture.stagingBufferMemory, 0, dataSize, 0, &mappedData);