21 #include "refresh/refresh.h"
22 #define _USE_MATH_DEFINES
32 #define EARTH_SURFACE_RADIUS (6360.0f)
33 #define EARTH_ATMOSPHERE_RADIUS (6420.f)
34 #define STROGGOS_SURFACE_RADIUS (6360.0f)
35 #define STROGGOS_ATMOSPHERE_RADIUS (6520.f)
36 #define DIST_TO_HORIZON(LOW,HIGH) ( HIGH*HIGH - LOW*LOW )
40 .sun_angular_radius = 0.00467499997f,
41 .rayleigh_scattering = { 0.00580233941f, 0.0135577619f, 0.0331000052f },
43 .mie_scattering = { 0.0014985f, 0.0014985f, 0.0014985f },
45 .mie_phase_function_g = 0.8f,
52 .sun_angular_radius = 0.00934999995f,
53 .rayleigh_scattering = { 0.0270983186, 0.0414223559, 0.0647224262 },
55 .mie_scattering = { 0.00342514296, 0.00342514296, 0.00342514296 },
57 .mie_phase_function_g = 0.9f,
78 memset(Info, 0,
sizeof(*Info));
93 #define PRECOMPUTED_SKY_BINDING_IDX 0
94 #define PRECOMPUTED_SKY_UBO_DESC_SET_IDX 3
103 VkResult
UploadImage(
void* FirstPixel,
size_t total_size,
unsigned int Width,
unsigned int Height,
unsigned int Depth,
unsigned int ArraySize,
unsigned char Cube, VkFormat PixelFormat, uint32_t Binding,
struct ImageGPUInfo* Info,
const char* DebugName)
105 size_t img_size = Width * Height * Depth;
108 buffer_create(&buf_img_upload, total_size, VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
109 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
113 memcpy(bn_tex, FirstPixel, total_size);
118 enum VkImageType ImageType = VK_IMAGE_TYPE_2D;
122 ImageType = VK_IMAGE_TYPE_3D;
124 VkImageCreateInfo img_info =
126 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
132 .imageType = ImageType,
133 .format = PixelFormat,
135 .arrayLayers = ArraySize,
136 .samples = VK_SAMPLE_COUNT_1_BIT,
137 .tiling = VK_IMAGE_TILING_OPTIMAL,
138 .usage = VK_IMAGE_USAGE_STORAGE_BIT
139 | VK_IMAGE_USAGE_TRANSFER_DST_BIT
140 | VK_IMAGE_USAGE_SAMPLED_BIT,
141 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
143 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
148 img_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
153 VkMemoryRequirements mem_req;
155 assert(mem_req.size >= buf_img_upload.
size);
157 VkMemoryAllocateInfo mem_alloc_info = {
158 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
159 .allocationSize = mem_req.size,
160 .memoryTypeIndex =
get_memory_type(mem_req.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
163 #ifdef VKPT_DEVICE_GROUPS
164 VkMemoryAllocateFlagsInfoKHR mem_alloc_flags = {
165 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR,
166 .flags = VK_MEMORY_ALLOCATE_DEVICE_MASK_BIT_KHR,
171 mem_alloc_info.pNext = &mem_alloc_flags;
181 VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D;
183 viewType = VK_IMAGE_VIEW_TYPE_3D;
184 if (ArraySize == 6 && Cube)
185 viewType = VK_IMAGE_VIEW_TYPE_CUBE;
187 VkImageViewCreateInfo img_view_info = {
188 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
189 .viewType = viewType,
190 .format = PixelFormat,
191 .image = Info->
Image,
192 .subresourceRange = {
193 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
197 .layerCount = ArraySize,
200 VK_COMPONENT_SWIZZLE_R,
201 VK_COMPONENT_SWIZZLE_G,
202 VK_COMPONENT_SWIZZLE_B,
203 VK_COMPONENT_SWIZZLE_A,
212 VkImageSubresourceRange subresource_range = {
213 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
217 .layerCount = ArraySize,
221 .image = Info->
Image,
222 .subresourceRange = subresource_range,
224 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
225 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
226 .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
229 VkBufferImageCopy cpy_info = {
231 .imageSubresource = {
232 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
235 .layerCount = ArraySize,
237 .imageOffset = { 0, 0, 0 },
238 .imageExtent = { Width, Height, Depth }
240 vkCmdCopyBufferToImage(cmd_buf, buf_img_upload.
buffer, Info->
Image,
241 VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &cpy_info);
245 .image = Info->
Image,
246 .subresourceRange = subresource_range,
247 .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
248 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
249 .oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
250 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
256 VkDescriptorImageInfo desc_img_info = {
257 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
258 .imageView = Info->
View,
262 VkWriteDescriptorSet s = {
263 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
265 .dstBinding = Binding,
266 .dstArrayElement = 0,
267 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
268 .descriptorCount = 1,
269 .pImageInfo = &desc_img_info,
272 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
275 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
284 #define ISBITMASK(header,r,g,b,a) ( header.RBitMask == r && header.GBitMask == g && header.BBitMask == b && header.ABitMask == a )
288 unsigned char* data = NULL;
289 ssize_t len = FS_LoadFile(FileName, (
void**)&data);
293 Com_EPrintf(
"Couldn't read file %s\n", FileName);
297 qboolean retval = qfalse;
304 Com_EPrintf(
"File %s does not have the expected DDS file format\n", FileName);
310 VkFormat PixelFormat = VK_FORMAT_UNDEFINED;
316 PixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
318 PixelFormat = VK_FORMAT_R32_SFLOAT;
320 PixelFormat = VK_FORMAT_R32G32B32A32_SFLOAT;
323 Com_EPrintf(
"File %s uses an unsupported pixel format (%d)\n", FileName, dxt10->
dxgiFormat);
328 ArraySize = dxt10->
arraySize * (Cube ? 6 : 1);
339 if (
ISBITMASK(dds->
ddspf, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000))
340 PixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
341 else if (
ISBITMASK(dds->
ddspf, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000))
342 PixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
343 else if (
ISBITMASK(dds->
ddspf, 0xffffffff, 0x00000000, 0x00000000, 0x00000000))
344 PixelFormat = VK_FORMAT_R32_SFLOAT;
346 Com_EPrintf(
"File %s uses an unsupported pixel format.\n", FileName);
349 UploadImage(data + dds_header_size, len - dds_header_size, dds->
width, dds->
height, dds->
depth, ArraySize, Cube, PixelFormat, Binding, Info, DebugName);
369 VkDescriptorSetLayoutBinding ubo_layout_binding = {
370 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
371 .descriptorCount = 1,
373 .stageFlags = VK_SHADER_STAGE_ALL,
376 VkDescriptorSetLayoutCreateInfo layout_info = {
377 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
379 .pBindings = &ubo_layout_binding,
386 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
387 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
392 VkDescriptorPoolSize pool_size = {
393 .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
397 VkDescriptorPoolCreateInfo pool_info = {
398 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
400 .pPoolSizes = &pool_size,
406 VkDescriptorSetAllocateInfo descriptor_set_alloc_info =
408 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
410 .descriptorSetCount = 1,
419 VkDescriptorBufferInfo buf_info = {
425 VkWriteDescriptorSet output_buf_write = {
426 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
429 .dstArrayElement = 0,
430 .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
431 .descriptorCount = 1,
432 .pBufferInfo = &buf_info,
435 vkUpdateDescriptorSets(
qvk.
device, 1, &output_buf_write, 0, NULL);
458 assert(ubo->
memory != VK_NULL_HANDLE);
459 assert(ubo->
buffer != VK_NULL_HANDLE);
471 #define MATRIX(row, col) (row * 4 + col)
475 uint32_t size = 16 *
sizeof(float);
476 memset(matrix, 0, size);
477 matrix[
MATRIX(0, 0)] = 1.0f;
478 matrix[
MATRIX(1, 1)] = 1.0f;
479 matrix[
MATRIX(2, 2)] = 1.0f;
480 matrix[
MATRIX(3, 3)] = 1.0f;
489 CrossProduct(UpDirection, f, s);
493 CrossProduct(f, s, u);
495 float D0 = DotProduct(s, EyePosition);
496 float D1 = DotProduct(u, EyePosition);
497 float D2 = DotProduct(f, EyePosition);
502 matrix[
MATRIX(0, 0)] = s[0];
503 matrix[
MATRIX(1, 0)] = s[1];
504 matrix[
MATRIX(2, 0)] = s[2];
505 matrix[
MATRIX(0, 1)] = u[0];
507 matrix[
MATRIX(1, 1)] = u[1];
508 matrix[
MATRIX(2, 1)] = u[2];
509 matrix[
MATRIX(0, 2)] = f[0];
510 matrix[
MATRIX(1, 2)] = f[1];
512 matrix[
MATRIX(2, 2)] = f[2];
513 matrix[
MATRIX(3, 0)] = -D0;
514 matrix[
MATRIX(3, 1)] = -D1;
515 matrix[
MATRIX(3, 2)] = -D2;
520 float ymin,
float ymax,
float znear,
float zfar)
527 float fRange = 1.0f / (zfar - znear);
529 matrix[0] = 2 /
width;
532 matrix[12] = -(xmax + xmin) /
width;
537 matrix[13] = -(ymax + ymin) /
height;
542 matrix[14] = -fRange * znear;
557 vec3_t
up = { 0, 0, 1.0f };
584 char FileBuf[MAX_QPATH];
585 Q_snprintf(FileBuf,
sizeof(FileBuf),
"env/transmittance_%s.dds", Planet);
587 Q_snprintf(FileBuf,
sizeof(FileBuf),
"env/inscatter_%s.dds", Planet);
589 Q_snprintf(FileBuf,
sizeof(FileBuf),
"env/irradiance_%s.dds", Planet);
684 unsigned char* file_data = NULL;
685 ssize_t file_len = FS_LoadFile(FileName, (
void**)&file_data);
689 Com_EPrintf(
"Couldn't read file %s\n", FileName);
698 Com_EPrintf(
"File %s does not have the expected DDS file format\n", FileName);
705 size_t VertexBufferSize = SideSize * SideSize *
sizeof(
struct ShadowVertex);
706 size_t IndexBufferSize = (SideSize - 1) * (SideSize - 1) * 2 *
sizeof(
struct ShadowFace);
707 size_t IndexCount = IndexBufferSize /
sizeof(uint32_t);
709 VertexBufferSize =
align(VertexBufferSize, 64 * 1024);
710 IndexBufferSize =
align(IndexBufferSize, 64 * 1024);
713 buffer_create(&upload_buffer, VertexBufferSize + IndexBufferSize,
714 VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
715 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
717 byte* mapped_upload_buf = (
byte*)
buffer_map(&upload_buffer);
722 if (!vertexes || !indexes)
727 float delta = size_km / (float)SideSize;
728 float X = -0.5f * size_km;
729 float Y = -0.5f * size_km;
731 for (
unsigned int y = 0; y < SideSize; y++)
733 for (
unsigned int x = 0; x < SideSize; x++)
735 unsigned int index = y * SideSize + x;
737 float Z = file_pixels[index];
739 vertexes[index].
x = X;
740 vertexes[index].
y = Y;
741 vertexes[index].
z = (x == 0) || (y == 0) || (x == SideSize - 1) || (y == SideSize - 1) ? -6.f : Z * 3.f;
749 for (
unsigned int y = 0; y < SideSize-1; y++)
751 for (
unsigned int x = 0; x < SideSize-1; x++)
753 unsigned int upper_index = (y + 1) * SideSize + x;
754 unsigned int lower_index = y * SideSize + x;
755 indexes[i].
A = lower_index;
756 indexes[i].
B = upper_index + 1;
757 indexes[i].
C = lower_index + 1;
759 indexes[i].
A = lower_index;
760 indexes[i].
B = upper_index;
761 indexes[i].
C = upper_index + 1;
770 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
771 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
775 VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
776 VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
782 .buffer = upload_buffer.
buffer,
784 .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
786 .size = VK_WHOLE_SIZE);
791 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
793 .size = VK_WHOLE_SIZE);
798 .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
800 .size = VK_WHOLE_SIZE);
802 VkBufferCopy region = {
805 .size = VertexBufferSize
810 region.srcOffset = VertexBufferSize;
811 region.size = IndexBufferSize;
818 .dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
820 .size = VK_WHOLE_SIZE);
825 .dstAccessMask = VK_ACCESS_INDEX_READ_BIT,
827 .size = VK_WHOLE_SIZE);
831 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,.levelCount = 1,.layerCount = 1 },
832 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
833 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
834 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
835 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
844 FS_FreeFile(file_data);
870 InOutShadowmap->
DepthFormat = VK_FORMAT_D32_SFLOAT;
874 VkImageCreateInfo ShadowTexInfo = {
875 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
876 .imageType = VK_IMAGE_TYPE_2D,
877 .extent.width = InOutShadowmap->
Width,
878 .extent.height = InOutShadowmap->
Height,
882 .samples = VK_SAMPLE_COUNT_1_BIT,
883 .tiling = VK_IMAGE_TILING_OPTIMAL,
885 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
891 VkMemoryRequirements memReqs = {0};
894 VkMemoryAllocateInfo memAlloc =
896 .sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
897 .allocationSize = memReqs.size,
898 .memoryTypeIndex =
get_memory_type(memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
904 VkImageViewCreateInfo depthStencilView =
906 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
907 .viewType = VK_IMAGE_VIEW_TYPE_2D,
909 .subresourceRange = {0},
910 .subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
911 .subresourceRange.baseMipLevel = 0,
912 .subresourceRange.levelCount = 1,
913 .subresourceRange.baseArrayLayer = 0,
914 .subresourceRange.layerCount = 1,
920 VkDescriptorImageInfo desc_img_info = {
921 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
926 VkWriteDescriptorSet s = {
927 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
930 .dstArrayElement = 0,
931 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
932 .descriptorCount = 1,
933 .pImageInfo = &desc_img_info,
936 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
939 vkUpdateDescriptorSets(
qvk.
device, 1, &s, 0, NULL);
941 VkSamplerCreateInfo sampler =
943 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
944 .magFilter = VK_FILTER_LINEAR,
945 .minFilter = VK_FILTER_LINEAR,
946 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR,
947 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE,
948 .addressModeV = sampler.addressModeU,
949 .addressModeW = sampler.addressModeU,
951 .maxAnisotropy = 1.0f,
954 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
960 VkFramebufferCreateInfo fbufCreateInfo =
962 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
964 .attachmentCount = 1,
965 .pAttachments = &InOutShadowmap->
DepthView,
966 .width = InOutShadowmap->
Width,
967 .height = InOutShadowmap->
Height,
995 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .levelCount = 1, .layerCount = 1 },
996 .srcAccessMask = VK_ACCESS_SHADER_READ_BIT,
997 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
998 .oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
999 .newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1002 VkClearValue clearValues;
1003 clearValues.depthStencil.depth = 1.0f;
1004 clearValues.depthStencil.stencil = 0;
1006 VkRenderPassBeginInfo renderPassBeginInfo =
1008 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1013 .clearValueCount = 1,
1014 .pClearValues = &clearValues,
1017 vkCmdBeginRenderPass(cmd_buf, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
1019 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline_smap);
1021 VkViewport viewport =
1029 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
1039 vkCmdSetScissor(cmd_buf, 0, 1, &rect2D);
1040 float depthBiasConstant = 0.f;
1041 float depthBiasSlope = 0.f;
1045 VkDeviceSize offsets[1] = { 0 };
1049 vkCmdEndRenderPass(cmd_buf);
1053 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,.levelCount = 1,.layerCount = 1 },
1054 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
1055 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
1056 .oldLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
1057 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,