22 #include "refresh/images.h"
27 #include "../stb/stb_image.h"
28 #include "../stb/stb_image_resize.h"
29 #include "../stb/stb_image_write.h"
31 #define MAX_RBUFFERS 16
44 #define DESTROY_LATENCY MAX_FRAMES_IN_FLIGHT * 4
78 ssize_t buffer_size = 0;
79 char const * filename =
"prefetch.txt";
80 buffer_size = FS_LoadFile(filename, (
void**)&buffer);
83 Com_EPrintf(
"Can't load '%s'\n", filename);
87 char const * ptr = buffer;
88 char linebuf[MAX_QPATH];
89 while (
sgets(linebuf,
sizeof(linebuf), &ptr))
91 char* line = strtok(linebuf,
" \t\r\n");
97 char other_name[MAX_QPATH];
100 if (!
Q_strlcpy(other_name, line, strlen(line) - 3))
102 Q_concat(other_name,
sizeof(other_name), other_name,
"_n.tga", NULL);
109 if (!
Q_strlcpy(other_name, line, strlen(line) - 3))
111 Q_concat(other_name,
sizeof(other_name), other_name,
"_light.tga", NULL);
113 image_t const * img3 =
IMG_Find(other_name, IT_SKIN, IF_PERMANENT | IF_SRGB);
123 for (uint32_t i = 0; i < unused_resources->
image_num; i++)
125 if (unused_resources->
image_views[i] != VK_NULL_HANDLE)
128 if(unused_resources->
images[i] != VK_NULL_HANDLE)
136 for (uint32_t i = 0; i < unused_resources->
buffer_num; i++)
162 const int num_images = 6;
163 size_t img_size = w * h * 4;
166 buffer_create(&buf_img_upload, img_size * num_images, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
167 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
169 byte *envmap = (
byte *)
buffer_map(&buf_img_upload);
170 memcpy(envmap, data, img_size * num_images);
174 VkImageCreateInfo img_info = {
175 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
181 .imageType = VK_IMAGE_TYPE_2D,
182 .format = VK_FORMAT_R8G8B8A8_UNORM,
184 .arrayLayers = num_images,
185 .samples = VK_SAMPLE_COUNT_1_BIT,
186 .tiling = VK_IMAGE_TILING_OPTIMAL,
187 .usage = VK_IMAGE_USAGE_STORAGE_BIT
188 | VK_IMAGE_USAGE_TRANSFER_DST_BIT
189 | VK_IMAGE_USAGE_SAMPLED_BIT,
190 .flags = VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT,
191 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
193 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
199 VkMemoryRequirements mem_req;
201 assert(mem_req.size >= buf_img_upload.
size);
207 VkImageViewCreateInfo img_view_info = {
208 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
209 .viewType = VK_IMAGE_VIEW_TYPE_CUBE,
210 .format = VK_FORMAT_R8G8B8A8_UNORM,
212 .subresourceRange = {
213 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
217 .layerCount = num_images,
220 VK_COMPONENT_SWIZZLE_R,
221 VK_COMPONENT_SWIZZLE_G,
222 VK_COMPONENT_SWIZZLE_B,
223 VK_COMPONENT_SWIZZLE_A,
231 for(
int layer = 0; layer < num_images; layer++) {
233 VkImageSubresourceRange subresource_range = {
234 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
237 .baseArrayLayer = layer,
243 .subresourceRange = subresource_range,
245 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
246 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
247 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
250 VkBufferImageCopy cpy_info = {
251 .bufferOffset = img_size * layer,
252 .imageSubresource = {
253 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
255 .baseArrayLayer = layer,
258 .imageOffset = { 0, 0, 0 },
259 .imageExtent = { w, h, 1 }
262 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &cpy_info);
267 .subresourceRange = subresource_range,
268 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
269 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
270 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
271 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
278 VkDescriptorImageInfo desc_img_info = {
279 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
284 VkWriteDescriptorSet s = {
285 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
288 .dstArrayElement = 0,
289 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
290 .descriptorCount = 1,
291 .pImageInfo = &desc_img_info,
294 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
297 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
312 size_t img_size = res * res;
313 size_t total_size = img_size *
sizeof(uint16_t);
317 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
319 uint16_t *bn_tex = (uint16_t *)
buffer_map(&buf_img_upload);
321 for(
int i = 0; i < num_images; i++) {
325 snprintf(buf,
sizeof buf,
"blue_noise/%d_%d/HDR_RGBA_%04d.png", res, res, i);
329 ssize_t filelen = FS_LoadFile(buf, &filedata);
332 data = stbi_load_16_from_memory(filedata, filelen, &w, &h, &n, 4);
337 Com_EPrintf(
"error loading blue noise tex %s\n", buf);
340 return VK_ERROR_INITIALIZATION_FAILED;
344 for(
int k = 0; k < 4; k++) {
345 for(
int j = 0; j < img_size; j++)
346 bn_tex[(i * 4 + k) * img_size + j] = data[j * 4 + k];
349 stbi_image_free(data);
354 VkImageCreateInfo img_info = {
355 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
361 .imageType = VK_IMAGE_TYPE_2D,
362 .format = VK_FORMAT_R16_UNORM,
365 .samples = VK_SAMPLE_COUNT_1_BIT,
366 .tiling = VK_IMAGE_TILING_OPTIMAL,
367 .usage = VK_IMAGE_USAGE_STORAGE_BIT
368 | VK_IMAGE_USAGE_TRANSFER_DST_BIT
369 | VK_IMAGE_USAGE_SAMPLED_BIT,
370 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
372 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
378 VkMemoryRequirements mem_req;
380 assert(mem_req.size >= buf_img_upload.size);
386 VkImageViewCreateInfo img_view_info = {
387 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
388 .viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY,
389 .format = VK_FORMAT_R16_UNORM,
391 .subresourceRange = {
392 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
399 VK_COMPONENT_SWIZZLE_R,
400 VK_COMPONENT_SWIZZLE_R,
401 VK_COMPONENT_SWIZZLE_R,
402 VK_COMPONENT_SWIZZLE_R,
412 VkImageSubresourceRange subresource_range = {
413 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
416 .baseArrayLayer = layer,
422 .subresourceRange = subresource_range,
424 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
425 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
426 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
429 VkBufferImageCopy cpy_info = {
430 .bufferOffset = total_size * layer,
431 .imageSubresource = {
432 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
434 .baseArrayLayer = layer,
437 .imageOffset = { 0, 0, 0 },
440 vkCmdCopyBufferToImage(cmd_buf, buf_img_upload.buffer,
img_blue_noise,
441 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &cpy_info);
446 .subresourceRange = subresource_range,
447 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
448 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
449 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
450 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
456 VkDescriptorImageInfo desc_img_info = {
457 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
462 VkWriteDescriptorSet s = {
463 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
466 .dstArrayElement = 0,
467 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
468 .descriptorCount = 1,
469 .pImageInfo = &desc_img_info,
472 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
475 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
487 return 1 + log2(
MAX(w, h));
499 float x = (float)pix / 255.f;
504 return powf((x + 0.055f) / 1.055f, 2.4f);
512 x = 1.055f * powf(x, 1.f / 2.4f) - 0.055f;
514 x = max(0.f, min(1.f, x));
516 return (
byte)roundf(x * 255.f);
521 return (
float)pix / 255.f;
526 x = max(0.f, min(1.f, x));
528 return (
byte)roundf(x * 255.f);
534 int w = image->upload_width;
535 int h = image->upload_height;
537 byte* current_pixel = image->pix_data;
538 vec3_t emissive_color;
539 VectorClear(emissive_color);
546 for (
int y = 0; y < h; y++) {
547 for (
int x = 0; x < w; x++) {
548 if(current_pixel[0] + current_pixel[1] + current_pixel[2] > 0)
559 VectorAdd(emissive_color,
color, emissive_color);
561 min_x = min(min_x, x);
562 min_y = min(min_y, y);
563 max_x = max(max_x, x);
564 max_y = max(max_y, y);
571 if (min_x <= max_x && min_y <= max_y)
573 float normalization = 1.f / (float)((max_x - min_x + 1) * (max_y - min_y + 1));
574 VectorScale(emissive_color, normalization, image->light_color);
578 VectorSet(image->light_color, 0.f, 0.f, 0.f);
581 image->min_light_texcoord[0] = (float)min_x / (
float)w;
582 image->min_light_texcoord[1] = (float)min_y / (
float)h;
583 image->max_light_texcoord[0] = (float)(max_x + 1) / (float)w;
584 image->max_light_texcoord[1] = (float)(max_y + 1) / (float)h;
586 image->entire_texture_emissive = (min_x == 0) && (min_y == 0) && (max_x == w - 1) && (max_y == h - 1);
588 image->processing_complete = qtrue;
594 int w = image->upload_width;
595 int h = image->upload_height;
597 byte* current_pixel = image->pix_data;
599 for (
int y = 0; y < h; y++) {
600 for (
int x = 0; x < w; x++)
628 image->processing_complete = qtrue;
634 image->pix_data = pic;
643 image->pix_data = NULL;
645 const uint32_t index = image -
r_images;
652 const uint32_t unused_index = unused_resources->
image_num++;
672 if (!image->registration_sequence)
675 if (image->type == IT_FONT || image->type == IT_PIC)
680 char const * filepath = image->filepath[0] ? image->filepath : image->name;
682 uint64_t last_modifed;
689 if (last_modifed <= image->last_modified)
694 if (
load_img(filepath, &new_image) == Q_ERR_SUCCESS)
698 image->pix_data = new_image.pix_data;
699 image->width = new_image.width;
700 image->height = new_image.width;
701 image->upload_width = new_image.upload_width;
702 image->upload_height = new_image.upload_height;
703 image->processing_complete = qfalse;
705 IMG_Load(image, new_image.pix_data);
707 if (strstr(filepath,
"_n."))
712 image->last_modified = last_modifed;
726 Com_Printf(
"Reloaded '%s'\n", image->name);
731 Com_Printf(
"Reloaded %d textures\n", reloaded);
736 const VkImageCreateInfo image_create_info = {
737 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
738 .imageType = VK_IMAGE_TYPE_2D,
739 .format = VK_FORMAT_R8G8B8A8_UNORM,
740 .extent = { 1, 1, 1 },
743 .samples = VK_SAMPLE_COUNT_1_BIT,
744 .tiling = VK_IMAGE_TILING_OPTIMAL,
745 .usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT,
746 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
747 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED
751 VkMemoryRequirements memory_requirements;
763 const VkImageSubresourceRange subresource_range = {
764 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
769 const VkImageViewCreateInfo image_view_create_info = {
770 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
772 .viewType = VK_IMAGE_VIEW_TYPE_2D,
773 .format = image_create_info.format,
774 .subresourceRange = subresource_range
783 .subresourceRange = subresource_range,
785 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
786 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
787 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
790 const VkClearColorValue
color = {
796 const VkImageSubresourceRange
range = subresource_range;
801 .subresourceRange = subresource_range,
802 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
803 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
804 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
805 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
830 VkSamplerCreateInfo sampler_info = {
831 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
832 .magFilter = VK_FILTER_LINEAR,
833 .minFilter = VK_FILTER_LINEAR,
834 .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
835 .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
836 .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
837 .anisotropyEnable = VK_TRUE,
839 .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK,
840 .unnormalizedCoordinates = VK_FALSE,
841 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
847 VkSamplerCreateInfo sampler_nearest_info = {
848 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
849 .magFilter = VK_FILTER_NEAREST,
850 .minFilter = VK_FILTER_NEAREST,
851 .addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT,
852 .addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT,
853 .addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT,
854 .anisotropyEnable = VK_FALSE,
856 .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK,
857 .unnormalizedCoordinates = VK_FALSE,
858 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
863 VkSamplerCreateInfo sampler_linear_clamp_info = {
864 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
865 .magFilter = VK_FILTER_LINEAR,
866 .minFilter = VK_FILTER_LINEAR,
867 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
868 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
869 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
870 .anisotropyEnable = VK_FALSE,
872 .borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK,
873 .unnormalizedCoordinates = VK_FALSE,
874 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
879 VkDescriptorSetLayoutBinding layout_bindings[] = {
881 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
882 .descriptorCount = MAX_RIMAGES,
884 .stageFlags = VK_SHADER_STAGE_ALL,
886 #define IMG_DO(_name, _binding, ...) \
888 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, \
889 .descriptorCount = 1, \
890 .binding = BINDING_OFFSET_IMAGES + _binding, \
891 .stageFlags = VK_SHADER_STAGE_ALL, \
896 #define IMG_DO(_name, _binding, ...) \
898 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, \
899 .descriptorCount = 1, \
900 .binding = BINDING_OFFSET_TEXTURES + _binding, \
901 .stageFlags = VK_SHADER_STAGE_ALL, \
907 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
908 .descriptorCount = 1,
910 .stageFlags = VK_SHADER_STAGE_ALL,
913 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
914 .descriptorCount = 1,
916 .stageFlags = VK_SHADER_STAGE_ALL,
919 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
920 .descriptorCount = 1,
922 .stageFlags = VK_SHADER_STAGE_ALL,
925 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE,
926 .descriptorCount = 1,
928 .stageFlags = VK_SHADER_STAGE_ALL,
931 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
932 .descriptorCount = 1,
934 .stageFlags = VK_SHADER_STAGE_ALL,
937 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
938 .descriptorCount = 1,
940 .stageFlags = VK_SHADER_STAGE_ALL,
943 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
944 .descriptorCount = 1,
946 .stageFlags = VK_SHADER_STAGE_ALL,
949 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
950 .descriptorCount = 1,
952 .stageFlags = VK_SHADER_STAGE_ALL,
955 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
956 .descriptorCount = 1,
958 .stageFlags = VK_SHADER_STAGE_ALL,
961 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
962 .descriptorCount = 1,
964 .stageFlags = VK_SHADER_STAGE_ALL,
967 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
968 .descriptorCount = 1,
970 .stageFlags = VK_SHADER_STAGE_ALL,
973 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
974 .descriptorCount = 1,
976 .stageFlags = VK_SHADER_STAGE_ALL,
980 VkDescriptorSetLayoutCreateInfo layout_info = {
981 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
982 .bindingCount =
LENGTH(layout_bindings),
983 .pBindings = layout_bindings,
988 VkDescriptorPoolSize pool_size = {
989 .type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
993 VkDescriptorPoolCreateInfo pool_info = {
994 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
996 .pPoolSizes = &pool_size,
1003 VkDescriptorSetAllocateInfo descriptor_set_alloc_info = {
1004 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
1006 .descriptorSetCount = 1,
1017 return VK_ERROR_INITIALIZATION_FAILED;
1026 for(
int i = 0; i < MAX_RIMAGES; i++) {
1079 #ifdef VKPT_DEVICE_GROUPS
1080 static VkMemoryAllocateFlagsInfoKHR mem_alloc_flags_broadcast = {
1081 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR,
1082 .flags = VK_MEMORY_ALLOCATE_DEVICE_MASK_BIT_KHR,
1093 VkImageCreateInfo img_info = {
1094 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1100 .imageType = VK_IMAGE_TYPE_2D,
1101 .format = VK_FORMAT_UNDEFINED,
1104 .samples = VK_SAMPLE_COUNT_1_BIT,
1105 .tiling = VK_IMAGE_TILING_OPTIMAL,
1106 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT
1107 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT
1108 | VK_IMAGE_USAGE_SAMPLED_BIT,
1109 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1111 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1114 VkImageViewCreateInfo img_view_info = {
1115 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
1116 .viewType = VK_IMAGE_VIEW_TYPE_2D,
1117 .format = VK_FORMAT_UNDEFINED,
1118 .subresourceRange = {
1119 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1122 .baseArrayLayer = 0,
1126 VK_COMPONENT_SWIZZLE_R,
1127 VK_COMPONENT_SWIZZLE_G,
1128 VK_COMPONENT_SWIZZLE_B,
1129 VK_COMPONENT_SWIZZLE_A
1133 #ifdef VKPT_DEVICE_GROUPS
1139 uint32_t new_image_num = 0;
1140 size_t total_size = 0;
1141 for(
int i = 0; i < MAX_RIMAGES; i++) {
1144 if (
tex_images[i] != VK_NULL_HANDLE || !q_img->registration_sequence || q_img->pix_data == NULL)
1147 img_info.extent.width = q_img->upload_width;
1148 img_info.extent.height = q_img->upload_height;
1149 img_info.mipLevels =
get_num_miplevels(q_img->upload_width, q_img->upload_height);
1150 img_info.format = q_img->is_srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
1155 VkMemoryRequirements mem_req;
1158 assert(!(mem_req.alignment & (mem_req.alignment - 1)));
1159 total_size += mem_req.alignment - 1;
1160 total_size &= ~(mem_req.alignment - 1);
1161 total_size += mem_req.size;
1164 image_memory->
size = mem_req.size;
1165 image_memory->
alignment = mem_req.alignment;
1171 bind_info->sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO;
1173 bind_info->memory = image_memory->
memory;
1179 if (new_image_num == 0)
1184 buffer_create(&buf_img_upload, total_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
1185 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
1189 char *staging_buffer =
buffer_map(&buf_img_upload);
1192 for(
int i = 0; i < MAX_RIMAGES; i++) {
1198 int num_mip_levels =
get_num_miplevels(q_img->upload_width, q_img->upload_height);
1200 VkMemoryRequirements mem_req;
1203 assert(!(mem_req.alignment & (mem_req.alignment - 1)));
1204 offset += mem_req.alignment - 1;
1205 offset &= ~(mem_req.alignment - 1);
1207 uint32_t wd = q_img->upload_width;
1208 uint32_t ht = q_img->upload_height;
1210 VkImageSubresourceRange subresource_range = {
1211 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1213 .levelCount = num_mip_levels,
1214 .baseArrayLayer = 0,
1220 .subresourceRange = subresource_range,
1222 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
1223 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1224 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
1228 memcpy(staging_buffer + offset, q_img->pix_data, wd * ht * 4);
1230 VkBufferImageCopy cpy_info = {
1231 .bufferOffset = offset,
1232 .imageSubresource = {
1233 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1235 .baseArrayLayer = 0,
1238 .imageOffset = { 0, 0, 0 },
1239 .imageExtent = { wd, ht, 1 }
1243 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &cpy_info);
1246 subresource_range.levelCount = 1;
1248 for (
int mip = 1; mip < num_mip_levels; mip++)
1250 subresource_range.baseMipLevel = mip - 1;
1254 .subresourceRange = subresource_range,
1255 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
1256 .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
1257 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1258 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1261 int nwd = (wd > 1) ? (wd >> 1) : wd;
1262 int nht = (ht > 1) ? (ht >> 1) : ht;
1264 VkImageBlit region = {
1266 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1267 .mipLevel = mip - 1,
1268 .baseArrayLayer = 0,
1276 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1278 .baseArrayLayer = 0,
1288 tex_images[i], VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1289 tex_images[i], VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1293 subresource_range.baseMipLevel = mip - 1;
1297 .subresourceRange = subresource_range,
1298 .srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
1299 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
1300 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
1301 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1308 subresource_range.baseMipLevel = num_mip_levels - 1;
1312 .subresourceRange = subresource_range,
1313 .srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
1314 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
1315 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1316 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1320 img_view_info.subresourceRange.levelCount = num_mip_levels;
1321 img_view_info.format = q_img->is_srgb ? VK_FORMAT_R8G8B8A8_SRGB : VK_FORMAT_R8G8B8A8_UNORM;
1325 offset += mem_req.size;
1329 staging_buffer = NULL;
1337 const uint32_t unused_index = unused_resources->
buffer_num++;
1338 unused_resources->
buffers[unused_index] = buf_img_upload.
buffer;
1353 for(
int i = 0; i < MAX_RIMAGES; i++) {
1357 if (image_view == VK_NULL_HANDLE)
1361 if (!strcmp(q_img->name,
"pics/conchars.pcx") || !strcmp(q_img->name,
"pics/ch1.pcx"))
1363 else if (q_img->type == IT_SPRITE)
1366 VkDescriptorImageInfo img_info = {
1367 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
1368 .imageView = image_view,
1372 if (i >= VKPT_IMG_BLOOM_HBLUR &&
1373 i <= VKPT_IMG_BLOOM_VBLUR) {
1377 VkWriteDescriptorSet descriptor_set_write = {
1378 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
1381 .dstArrayElement = i,
1382 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
1383 .descriptorCount = 1,
1384 .pImageInfo = &img_info,
1387 vkUpdateDescriptorSets(
qvk.
device, 1, &descriptor_set_write, 0, NULL);
1394 VkImageCreateInfo dump_image_info = {
1395 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
1396 .imageType = VK_IMAGE_TYPE_2D,
1398 .extent.width =
width,
1403 .samples = VK_SAMPLE_COUNT_1_BIT,
1404 .tiling = VK_IMAGE_TILING_LINEAR,
1405 .usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT,
1406 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
1407 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1410 _VK(vkCreateImage(
qvk.
device, &dump_image_info, NULL, image));
1412 VkMemoryRequirements mem_req;
1413 vkGetImageMemoryRequirements(
qvk.
device, *image, &mem_req);
1415 VkMemoryAllocateInfo mem_alloc_info = {
1416 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
1417 .allocationSize = mem_req.size,
1419 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT),
1422 _VK(vkAllocateMemory(
qvk.
device, &mem_alloc_info, NULL, memory));
1423 _VK(vkBindImageMemory(
qvk.
device, *image, *memory, 0));
1425 *memory_size = mem_req.size;
1433 vkDestroyImage(
qvk.
device, *image, NULL);
1434 *image = VK_NULL_HANDLE;
1436 vkFreeMemory(
qvk.
device, *memory, NULL);
1437 *memory = VK_NULL_HANDLE;
1445 #define IMG_DO(_name, _binding, _vkformat, _glslformat, _w, _h) \
1446 [VKPT_IMG_##_name] = { \
1447 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, \
1448 .imageType = VK_IMAGE_TYPE_2D, \
1449 .format = VK_FORMAT_##_vkformat, \
1457 .samples = VK_SAMPLE_COUNT_1_BIT, \
1458 .tiling = VK_IMAGE_TILING_OPTIMAL, \
1459 .usage = VK_IMAGE_USAGE_STORAGE_BIT \
1460 | VK_IMAGE_USAGE_TRANSFER_SRC_BIT \
1461 | VK_IMAGE_USAGE_TRANSFER_DST_BIT \
1462 | VK_IMAGE_USAGE_SAMPLED_BIT \
1463 | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, \
1464 .sharingMode = VK_SHARING_MODE_EXCLUSIVE, \
1465 .queueFamilyIndexCount = qvk.queue_idx_graphics, \
1466 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED, \
1473 #ifdef VKPT_DEVICE_GROUPS
1474 #define IMG_DO(_name, _binding, _vkformat, _glslformat, _w, _h) \
1475 images_create_info[VKPT_IMG_##_name].flags |= \
1476 VK_IMAGE_CREATE_SPLIT_INSTANCE_BIND_REGIONS_BIT;
1482 size_t total_size = 0;
1489 VkMemoryRequirements mem_req;
1492 total_size +=
align(mem_req.size, mem_req.alignment);
1500 #ifdef VKPT_DEVICE_GROUPS
1507 _VK(vkCreateImage(
qvk.
device, images_create_info + i, NULL, &
qvk.images_local[d][i]));
1515 device_indices[j] = (uint32_t)d;
1520 VkMemoryRequirements mem_req;
1521 vkGetImageMemoryRequirements(
qvk.
device,
qvk.images_local[d][i], &mem_req);
1524 VkBindImageMemoryDeviceGroupInfoKHR device_group_info = {
1525 .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_DEVICE_GROUP_INFO_KHR,
1528 .pDeviceIndices = device_indices,
1529 .splitInstanceBindRegionCount = 0,
1530 .pSplitInstanceBindRegions = NULL,
1533 VkBindImageMemoryInfoKHR bind_info = {
1534 .sType = VK_STRUCTURE_TYPE_BIND_IMAGE_MEMORY_INFO_KHR,
1535 .pNext = &device_group_info,
1536 .image =
qvk.images_local[d][i],
1541 _VK(qvkBindImageMemory2KHR(
qvk.
device, 1, &bind_info));
1547 Com_Printf(
"Screen-space image memory: %.2f MB\n", (
float)total_size / 1048576.f);
1550 #define IMG_DO(_name, _binding, ...) \
1551 ATTACH_LABEL_VARIABLE_NAME(qvk.images[VKPT_IMG_##_name], IMAGE, #_name);
1556 #define IMG_DO(_name, _binding, ...) \
1557 ATTACH_LABEL_VARIABLE_NAME(qvk.images[VKPT_IMG_##_name], IMAGE, #_name);
1563 #define IMG_DO(_name, _binding, _vkformat, _glslformat, _w, _h) \
1564 [VKPT_IMG_##_name] = { \
1565 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, \
1566 .viewType = VK_IMAGE_VIEW_TYPE_2D, \
1567 .format = VK_FORMAT_##_vkformat, \
1568 .image = qvk.images[VKPT_IMG_##_name], \
1569 .subresourceRange = { \
1570 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, \
1571 .baseMipLevel = 0, \
1573 .baseArrayLayer = 0, \
1577 VK_COMPONENT_SWIZZLE_R, \
1578 VK_COMPONENT_SWIZZLE_G, \
1579 VK_COMPONENT_SWIZZLE_B, \
1580 VK_COMPONENT_SWIZZLE_A \
1594 #ifdef VKPT_DEVICE_GROUPS
1597 VkImageViewCreateInfo info = images_view_create_info[i];
1598 info.image =
qvk.images_local[d][i];
1599 _VK(vkCreateImageView(
qvk.
device, &info, NULL, &
qvk.images_views_local[d][i]));
1606 #define IMG_DO(_name, ...) \
1607 ATTACH_LABEL_VARIABLE_NAME(qvk.images_views[VKPT_IMG_##_name], IMAGE_VIEW, #_name);
1612 #define IMG_DO(_name, ...) \
1613 [VKPT_IMG_##_name] = { \
1614 .sampler = VK_NULL_HANDLE, \
1615 .imageView = qvk.images_views[VKPT_IMG_##_name], \
1616 .imageLayout = VK_IMAGE_LAYOUT_GENERAL \
1618 VkDescriptorImageInfo desc_output_img_info[] = {
1624 VkDescriptorImageInfo img_info[] = {
1625 #define IMG_DO(_name, ...) \
1626 [VKPT_IMG_##_name] = { \
1627 .imageLayout = VK_IMAGE_LAYOUT_GENERAL, \
1628 .imageView = qvk.images_views[VKPT_IMG_##_name], \
1629 .sampler = qvk.tex_sampler, \
1637 for(
int i = VKPT_IMG_BLOOM_HBLUR; i <= VKPT_IMG_BLOOM_VBLUR; i++) {
1641 VkWriteDescriptorSet output_img_write[
NUM_IMAGES * 2];
1643 for (
int even_odd = 0; even_odd < 2; even_odd++)
1646 #define IMG_DO(_name, _binding, ...) { \
1647 VkWriteDescriptorSet elem_image = { \
1648 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, \
1649 .dstSet = even_odd ? qvk.desc_set_textures_odd : qvk.desc_set_textures_even, \
1650 .dstBinding = BINDING_OFFSET_IMAGES + _binding, \
1651 .dstArrayElement = 0, \
1652 .descriptorCount = 1, \
1653 .descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, \
1654 .pImageInfo = desc_output_img_info + VKPT_IMG_##_name, \
1656 VkWriteDescriptorSet elem_texture = { \
1657 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, \
1658 .dstSet = even_odd ? qvk.desc_set_textures_odd : qvk.desc_set_textures_even, \
1659 .dstBinding = BINDING_OFFSET_TEXTURES + _binding, \
1660 .dstArrayElement = 0, \
1661 .descriptorCount = 1, \
1662 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, \
1663 .pImageInfo = img_info + VKPT_IMG_##_name, \
1665 output_img_write[VKPT_IMG_##_name] = elem_image; \
1666 output_img_write[VKPT_IMG_##_name + NUM_VKPT_IMAGES] = elem_texture; \
1679 vkUpdateDescriptorSets(
qvk.
device,
LENGTH(output_img_write), output_img_write, 0, NULL);
1683 #ifdef VKPT_IMAGE_DUMPS
1689 VkImageSubresourceRange subresource_range = {
1690 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
1693 .baseArrayLayer = 0,
1700 .subresourceRange = subresource_range,
1702 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
1703 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1704 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
1707 #ifdef VKPT_DEVICE_GROUPS
1714 .image =
qvk.images_local[d][i],
1715 .subresourceRange = subresource_range,
1717 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT,
1718 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1719 .newLayout = VK_IMAGE_LAYOUT_GENERAL
1731 .subresourceRange = subresource_range,
1733 .dstAccessMask = VK_ACCESS_HOST_READ_BIT,
1734 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1735 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
1738 #ifdef VKPT_IMAGE_DUMPS
1740 .image =
qvk.dump_image,
1741 .subresourceRange = subresource_range,
1743 .dstAccessMask = VK_ACCESS_HOST_READ_BIT,
1744 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
1745 .newLayout = VK_IMAGE_LAYOUT_GENERAL,
1766 #ifdef VKPT_DEVICE_GROUPS
1770 vkDestroyImageView(
qvk.
device,
qvk.images_views_local[d][i], NULL);
1771 vkDestroyImage(
qvk.
device,
qvk.images_local[d][i], NULL);
1772 qvk.images_views_local[d][i] = VK_NULL_HANDLE;
1773 qvk.images_local[d][i] = VK_NULL_HANDLE;
1786 #ifdef VKPT_IMAGE_DUMPS