| 209 | } |
| 210 | |
| 211 | void Profiles::generate_textures() |
| 212 | { |
| 213 | // Generate random textures to be sourced from a single descriptor |
| 214 | |
| 215 | // Image info is same for all textures |
| 216 | const int32_t dim = 2; |
| 217 | |
| 218 | VkImageCreateInfo image_info = vkb::initializers::image_create_info(); |
| 219 | image_info.format = VK_FORMAT_R8G8B8A8_UNORM; |
| 220 | image_info.extent = {dim, dim, 1}; |
| 221 | image_info.mipLevels = 1; |
| 222 | image_info.arrayLayers = 1; |
| 223 | image_info.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; |
| 224 | image_info.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 225 | image_info.imageType = VK_IMAGE_TYPE_2D; |
| 226 | image_info.samples = VK_SAMPLE_COUNT_1_BIT; |
| 227 | image_info.tiling = VK_IMAGE_TILING_OPTIMAL; |
| 228 | image_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE; |
| 229 | |
| 230 | VkImageViewCreateInfo image_view = vkb::initializers::image_view_create_info(); |
| 231 | image_view.viewType = VK_IMAGE_VIEW_TYPE_2D; |
| 232 | image_view.format = VK_FORMAT_R8G8B8A8_UNORM; |
| 233 | image_view.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 234 | image_view.subresourceRange.baseMipLevel = 0; |
| 235 | image_view.subresourceRange.levelCount = 1; |
| 236 | image_view.subresourceRange.baseArrayLayer = 0; |
| 237 | image_view.subresourceRange.layerCount = 1; |
| 238 | |
| 239 | auto staging_buffer = vkb::core::BufferC::create_staging_buffer(get_device(), image_info.extent.width * image_info.extent.height * sizeof(uint32_t), nullptr); |
| 240 | |
| 241 | textures.resize(32); |
| 242 | for (size_t i = 0; i < textures.size(); i++) |
| 243 | { |
| 244 | VK_CHECK(vkCreateImage(get_device().get_handle(), &image_info, nullptr, &textures[i].image)); |
| 245 | VkMemoryAllocateInfo memory_allocation_info = vkb::initializers::memory_allocate_info(); |
| 246 | VkMemoryRequirements memory_requirements; |
| 247 | vkGetImageMemoryRequirements(get_device().get_handle(), textures[i].image, &memory_requirements); |
| 248 | memory_allocation_info.allocationSize = memory_requirements.size; |
| 249 | memory_allocation_info.memoryTypeIndex = get_device().get_gpu().get_memory_type(memory_requirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); |
| 250 | VK_CHECK(vkAllocateMemory(get_device().get_handle(), &memory_allocation_info, nullptr, &textures[i].memory)); |
| 251 | VK_CHECK(vkBindImageMemory(get_device().get_handle(), textures[i].image, textures[i].memory, 0)); |
| 252 | image_view.image = textures[i].image; |
| 253 | VK_CHECK(vkCreateImageView(get_device().get_handle(), &image_view, nullptr, &textures[i].image_view)); |
| 254 | |
| 255 | // Generate a random texture |
| 256 | std::default_random_engine rnd_engine(lock_simulation_speed ? static_cast<unsigned>(i) : std::random_device{}()); |
| 257 | std::uniform_int_distribution<short> rnd_dist(0, 255); |
| 258 | const size_t buffer_size = dim * dim * 4; |
| 259 | uint8_t *buffer = staging_buffer.map(); |
| 260 | for (size_t i = 0; i < dim * dim; i++) |
| 261 | { |
| 262 | buffer[i * 4] = static_cast<uint8_t>(rnd_dist(rnd_engine)); |
| 263 | buffer[i * 4 + 1] = static_cast<uint8_t>(rnd_dist(rnd_engine)); |
| 264 | buffer[i * 4 + 2] = static_cast<uint8_t>(rnd_dist(rnd_engine)); |
| 265 | buffer[i * 4 + 3] = 255; |
| 266 | } |
| 267 | |
| 268 | staging_buffer.unmap(); |
nothing calls this directly
no test coverage detected