* ktx纹理解析初步

This commit is contained in:
2026-04-24 14:35:19 +08:00
parent e7c9817d56
commit 484a102ae2
2 changed files with 235 additions and 51 deletions
+233 -50
View File
@@ -3,6 +3,9 @@
#ifndef DL_DISABLE_RENDER
#include <set>
#define DDSKTX_IMPLEMENT
#include <dds-ktx.h>
#include "system/dl_system.h"
#include "system/dl_android.h"
@@ -2073,6 +2076,18 @@ void VK::_create_uniform_bone()
}
}
inline VkFormat GetVulkanFormat(ddsktx_format fmt)
{
switch (fmt)
{
case DDSKTX_FORMAT_RG16F:
return VK_FORMAT_R16G16_SFLOAT;
case DDSKTX_FORMAT_RG11B10F:
return VK_FORMAT_B10G11R11_UFLOAT_PACK32;
default:
return VK_FORMAT_UNDEFINED;
}
}
void VK::_create_skybox()
{
@@ -2169,6 +2184,133 @@ void VK::_create_skybox()
return;
}
}
// brdf贴图
{
Buffer buf = File::GetBuffer("dl/brdf.ktx");
ddsktx_texture_info tc{};
GLuint tex = 0;
if (!ddsktx_parse(&tc, buf.data(), (int)buf.size(), nullptr))
{
log_err0("解析bdrf贴图失败!");
return;
}
if (tc.num_mips != 1
|| tc.num_layers != 1
|| tc.depth != 1
|| tc.format != DDSKTX_FORMAT_RG16F)
{
log_err0("不合法的bdrf贴图!");
return;
}
VkImage image;
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = tc.width;
imageInfo.extent.height = tc.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 1;
imageInfo.format = GetVulkanFormat(tc.format);
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageInfo.flags = 0;
if (vkCreateImage(_device, &imageInfo, nullptr, &image) != VK_SUCCESS)
{
log_err0("创建图像失败!");
return;
}
VkMemoryRequirements memRequirements;
vkGetImageMemoryRequirements(_device, image, &memRequirements);
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkDeviceMemory imageMemory; //todo
if (vkAllocateMemory(_device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS)
{
log_err0("分配图像内存失败!");
return;
}
vkBindImageMemory(_device, image, imageMemory, 0);
//{
// //Create GPU texture from tc data
// glGenTextures(1, &tex);
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(img->gl_target, tex);
// for (int mip = 0; mip < tc->num_mips; mip++)
// {
// ddsktx_sub_data sub_data;
// ddsktx_get_sub(&tc, &sub_data, buf.data(), (int)buf.size(), 0, 0, mip);
// // Fill/Set texture sub resource data (mips in this case)
// if (ddsktx_format_compressed(tc.format))
// glCompressedTexImage2D(..);
// else
// glTexImage2D(..);
// }
//}
VkImageViewCreateInfo viewInfo{};
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
viewInfo.format = GetVulkanFormat(tc.format);
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 1;
VkImageView imageView;
if (vkCreateImageView(_device, &viewInfo, nullptr, &imageView) != VK_SUCCESS)
{
log_err0("创建图像视图失败!");
return;
}
_skybox._brdfImage = image;
_skybox._brdfImageView = imageView;
}
// 更新描述符
{
// brdf (正常管线)
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = _skybox._brdfImageView;
imageInfo.sampler = _skybox._brdfSampler;
for (size_t i = 0; i < _descriptorSets.size(); ++i)
{
std::array<VkWriteDescriptorSet, 1> descriptorWrites{};
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = _descriptorSets[i];
descriptorWrites[0].dstBinding = 4;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pImageInfo = &imageInfo;
vkUpdateDescriptorSets(_device,
(uint32_t)descriptorWrites.size(), descriptorWrites.data(),
0, nullptr);
}
}
}
void VK::_create_descriptor_pool()
@@ -3882,24 +4024,44 @@ VkSampleCountFlagBits VK::GetMaxUsableSampleCount()
return VK_SAMPLE_COUNT_1_BIT;
}
void VK::LoadSkybox(std::string_view path)
{
// todo image
Skybox::Instance sky;
// env
/*{
{
Buffer buf = File::GetBuffer(path + "skybox.ktx");
ddsktx_texture_info tc{};
GLuint tex = 0;
if (!ddsktx_parse(&tc, buf.data(), (int)buf.size(), nullptr))
{
log_err0("解析env贴图失败!");
return;
}
if (tc.num_mips != 1
|| tc.num_layers != 1
|| tc.depth != 1
|| !(tc.flags & DDSKTX_TEXTURE_FLAG_CUBEMAP)
|| tc.format != DDSKTX_FORMAT_RG11B10F)
{
log_err0("不合法的env贴图!");
return;
}
VkImage image;
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = w;
imageInfo.extent.height = h;
imageInfo.extent.width = tc.width;
imageInfo.extent.height = tc.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 6;
imageInfo.format = format;
imageInfo.format = GetVulkanFormat(tc.format);
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
@@ -3917,8 +4079,10 @@ void VK::LoadSkybox(std::string_view path)
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits, properties);
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkDeviceMemory imageMemory;
if (vkAllocateMemory(_device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS)
{
log_err0("分配图像内存失败!");
@@ -3931,7 +4095,7 @@ void VK::LoadSkybox(std::string_view path)
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
viewInfo.format = format;
viewInfo.format = GetVulkanFormat(tc.format);
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = 1;
@@ -3942,26 +4106,45 @@ void VK::LoadSkybox(std::string_view path)
if (vkCreateImageView(_device, &viewInfo, nullptr, &imageView) != VK_SUCCESS)
{
log_err0("创建图像视图失败!");
return 0;
return;
}
sky._envImage = image;
sky._envImageView = imageView;
}
ibl
// ibl
{
Buffer buf = File::GetBuffer(path + "ibl.ktx");
ddsktx_texture_info tc{};
GLuint tex = 0;
if (!ddsktx_parse(&tc, buf.data(), (int)buf.size(), nullptr))
{
log_err0("解析ibl贴图失败!");
return;
}
if (tc.num_mips <= 1
|| tc.num_layers != 1
|| tc.depth != 1
|| !(tc.flags & DDSKTX_TEXTURE_FLAG_CUBEMAP)
|| tc.format != DDSKTX_FORMAT_RG11B10F)
{
log_err0("不合法的ibl贴图!");
return;
}
VkImage image;
VkImageCreateInfo imageInfo{};
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageInfo.imageType = VK_IMAGE_TYPE_2D;
imageInfo.extent.width = w;
imageInfo.extent.height = h;
imageInfo.extent.width = tc.width;
imageInfo.extent.height = tc.height;
imageInfo.extent.depth = 1;
imageInfo.mipLevels = 1;
imageInfo.arrayLayers = 6;
imageInfo.format = format;
imageInfo.format = GetVulkanFormat(tc.format);
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageInfo.initialLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
@@ -3979,8 +4162,10 @@ void VK::LoadSkybox(std::string_view path)
VkMemoryAllocateInfo allocInfo{};
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
allocInfo.allocationSize = memRequirements.size;
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits, properties);
allocInfo.memoryTypeIndex = FindMemoryType(memRequirements.memoryTypeBits,
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
VkDeviceMemory imageMemory;
if (vkAllocateMemory(_device, &allocInfo, nullptr, &imageMemory) != VK_SUCCESS)
{
log_err0("分配图像内存失败!");
@@ -3993,10 +4178,10 @@ void VK::LoadSkybox(std::string_view path)
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
viewInfo.image = image;
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_CUBE;
viewInfo.format = format;
viewInfo.format = GetVulkanFormat(tc.format);
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
viewInfo.subresourceRange.baseMipLevel = 0;
viewInfo.subresourceRange.levelCount = xx;
viewInfo.subresourceRange.levelCount = tc.num_mips;
viewInfo.subresourceRange.baseArrayLayer = 0;
viewInfo.subresourceRange.layerCount = 6;
@@ -4004,49 +4189,47 @@ void VK::LoadSkybox(std::string_view path)
if (vkCreateImageView(_device, &viewInfo, nullptr, &imageView) != VK_SUCCESS)
{
log_err0("创建图像视图失败!");
return 0;
return;
}
sky._iblImage = image;
sky._iblImageView = imageView;
}*/
}
// 更新描述符
//{
// // env (skybox管线)
// VkDescriptorImageInfo imageInfo{};
// imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// imageInfo.imageView = sky._iblImageView;
// imageInfo.sampler = _skybox._iblSampler;
// // ibl (正常管线)
// VkDescriptorImageInfo imageInfo{};
// imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
// imageInfo.imageView = sky._iblImageView;
// imageInfo.sampler = _skybox._iblSampler;
{
// ibl (正常管线)
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = sky._iblImageView;
imageInfo.sampler = _skybox._iblSampler;
// std::array<VkWriteDescriptorSet, 2> descriptorWrites{};
// descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
// descriptorWrites[0].dstSet = _2d._descriptorSets[index];
// descriptorWrites[0].dstBinding = 0;
// descriptorWrites[0].dstArrayElement = 0;
// descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
// descriptorWrites[0].descriptorCount = 1;
// descriptorWrites[0].pBufferInfo = &bufferInfo;
for (size_t i = 0; i < _descriptorSets.size(); ++i)
{
std::array<VkWriteDescriptorSet, 1> descriptorWrites{};
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = _descriptorSets[i];
descriptorWrites[0].dstBinding = 3;
descriptorWrites[0].dstArrayElement = 0;
descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[0].descriptorCount = 1;
descriptorWrites[0].pImageInfo = &imageInfo;
// descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
// descriptorWrites[1].dstSet = _2d._descriptorSets[index];
// descriptorWrites[1].dstBinding = 1;
// descriptorWrites[1].dstArrayElement = 0;
// descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
// descriptorWrites[1].descriptorCount = 1;
// descriptorWrites[1].pImageInfo = &imageInfo;
vkUpdateDescriptorSets(_device,
(uint32_t)descriptorWrites.size(), descriptorWrites.data(),
0, nullptr);
}
}
{
// env (skybox管线)
VkDescriptorImageInfo imageInfo{};
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageInfo.imageView = sky._iblImageView;
imageInfo.sampler = _skybox._iblSampler;
// vkUpdateDescriptorSets(_device,
// (uint32_t)descriptorWrites.size(), descriptorWrites.data(),
// 0, nullptr);
//}
//_skybox._vec.push_back(sky);
// todo
}
_skybox._vec.push_back(sky);
}
size_t VK::CreateTexture(std::string_view path_name, bool srgb)
+2 -1
View File
@@ -959,7 +959,7 @@ private:
VkImage _iblImage; // ibl cube
VkImageView _iblImageView; // ibl cube view
};
std::vector<Skybox> _vec;
std::vector<Instance> _vec;
};
Skybox _skybox;
@@ -1016,6 +1016,7 @@ private:
_create_descriptor_sets_bone();
_create_skybox();
LoadSkybox("skybox/");
_create_command_buffer();
_create_sync_objects();