Quake II RTX doxygen  1.0 dev
precomputed_sky.c
Go to the documentation of this file.
1 /*
2 Copyright (C) 2019, NVIDIA CORPORATION. All rights reserved.
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18 
19 #include "vkpt.h"
20 #include "precomputed_sky.h"
21 #include "refresh/refresh.h"
22 #define _USE_MATH_DEFINES
23 #include <math.h>
24 
25 #include <assert.h>
26 #include "dds.h"
27 
28 // ----------------------------------------------------------------------------
29 
30 const struct AtmosphereParameters* Constants = NULL;
31 
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 )
37 
39  .solar_irradiance = { 1.47399998f, 1.85039997f, 1.91198003f },
40  .sun_angular_radius = 0.00467499997f,
41  .rayleigh_scattering = { 0.00580233941f, 0.0135577619f, 0.0331000052f },
42  .bottom_radius = EARTH_SURFACE_RADIUS,
43  .mie_scattering = { 0.0014985f, 0.0014985f, 0.0014985f },
44  .top_radius = EARTH_ATMOSPHERE_RADIUS,
45  .mie_phase_function_g = 0.8f,
46  .SqDistanceToHorizontalBoundary = DIST_TO_HORIZON(EARTH_SURFACE_RADIUS, EARTH_ATMOSPHERE_RADIUS),
47  .AtmosphereHeight = EARTH_ATMOSPHERE_RADIUS - EARTH_SURFACE_RADIUS,
48 };
49 
51  .solar_irradiance = { 2.47399998, 1.85039997, 1.01198006 },
52  .sun_angular_radius = 0.00934999995f,
53  .rayleigh_scattering = { 0.0270983186, 0.0414223559, 0.0647224262 },
54  .bottom_radius = STROGGOS_SURFACE_RADIUS,
55  .mie_scattering = { 0.00342514296, 0.00342514296, 0.00342514296 },
56  .top_radius = STROGGOS_ATMOSPHERE_RADIUS,
57  .mie_phase_function_g = 0.9f,
58  .SqDistanceToHorizontalBoundary = DIST_TO_HORIZON(STROGGOS_SURFACE_RADIUS, STROGGOS_ATMOSPHERE_RADIUS),
60 };
61 
62 // ----------------------------------------------------------------------------
63 
65 {
66  VkImage Image;
67  VkDeviceMemory DeviceMemory;
68  VkImageView View;
69 };
70 
71 
72 
73 void ReleaseInfo(struct ImageGPUInfo* Info)
74 {
75  vkFreeMemory(qvk.device, Info->DeviceMemory, NULL);
76  vkDestroyImage(qvk.device, Info->Image, NULL);
77  vkDestroyImageView(qvk.device, Info->View, NULL);
78  memset(Info, 0, sizeof(*Info));
79 }
80 
85 
89 
91 VkDescriptorSet desc_set_precomputed_ubo;
92 
93 #define PRECOMPUTED_SKY_BINDING_IDX 0
94 #define PRECOMPUTED_SKY_UBO_DESC_SET_IDX 3
95 
97 static VkDescriptorPool desc_pool_precomputed_ubo;
98 
99 float terrain_shadowmap_viewproj[16] = { 0.f };
100 
101 // ----------------------------------------------------------------------------
102 
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)
104 {
105  size_t img_size = Width * Height * Depth;
106 
107  BufferResource_t buf_img_upload;
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);
110 
111  void* bn_tex = buffer_map(&buf_img_upload);
112 
113  memcpy(bn_tex, FirstPixel, total_size);
114 
115  buffer_unmap(&buf_img_upload);
116  bn_tex = NULL;
117 
118  enum VkImageType ImageType = VK_IMAGE_TYPE_2D;
119  if (Depth == 0)
120  Depth = 1;
121  if (Depth > 1)
122  ImageType = VK_IMAGE_TYPE_3D;
123 
124  VkImageCreateInfo img_info =
125  {
126  .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
127  .extent = {
128  .width = Width,
129  .height = Height,
130  .depth = Depth,
131  },
132  .imageType = ImageType,
133  .format = PixelFormat,
134  .mipLevels = 1,
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,
142  .queueFamilyIndexCount = qvk.queue_idx_graphics,
143  .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
144  .flags = 0
145  };
146 
147  if (Cube)
148  img_info.flags |= VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT;
149 
150  _VK(vkCreateImage(qvk.device, &img_info, NULL, &Info->Image));
151  ATTACH_LABEL_VARIABLE_NAME(Info->Image, IMAGE, DebugName);
152 
153  VkMemoryRequirements mem_req;
154  vkGetImageMemoryRequirements(qvk.device, Info->Image, &mem_req);
155  assert(mem_req.size >= buf_img_upload.size);
156 
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)
161  };
162 
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,
167  .deviceMask = (1 << qvk.device_count) - 1
168  };
169 
170  if (qvk.device_count > 1) {
171  mem_alloc_info.pNext = &mem_alloc_flags;
172  }
173 #endif
174 
175  _VK(vkAllocateMemory(qvk.device, &mem_alloc_info, NULL, &Info->DeviceMemory));
176 
177 
178  //_VK(allocate_gpu_memory(mem_req, &Info->DeviceMemory));
179  _VK(vkBindImageMemory(qvk.device, Info->Image, Info->DeviceMemory, 0));
180 
181  VkImageViewType viewType = VK_IMAGE_VIEW_TYPE_2D;
182  if (Depth > 1)
183  viewType = VK_IMAGE_VIEW_TYPE_3D;
184  if (ArraySize == 6 && Cube)
185  viewType = VK_IMAGE_VIEW_TYPE_CUBE;
186 
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,
194  .baseMipLevel = 0,
195  .levelCount = 1,
196  .baseArrayLayer = 0,
197  .layerCount = ArraySize,
198  },
199  .components = {
200  VK_COMPONENT_SWIZZLE_R,
201  VK_COMPONENT_SWIZZLE_G,
202  VK_COMPONENT_SWIZZLE_B,
203  VK_COMPONENT_SWIZZLE_A,
204  },
205  };
206 
207  _VK(vkCreateImageView(qvk.device, &img_view_info, NULL, &Info->View));
208  ATTACH_LABEL_VARIABLE_NAME(Info->View, IMAGE_VIEW, DebugName);
209 
210  VkCommandBuffer cmd_buf = vkpt_begin_command_buffer(&qvk.cmd_buffers_graphics);
211 
212  VkImageSubresourceRange subresource_range = {
213  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
214  .baseMipLevel = 0,
215  .levelCount = 1,
216  .baseArrayLayer = 0,
217  .layerCount = ArraySize,
218  };
219 
220  IMAGE_BARRIER(cmd_buf,
221  .image = Info->Image,
222  .subresourceRange = subresource_range,
223  .srcAccessMask = 0,
224  .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
225  .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
226  .newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
227  );
228 
229  VkBufferImageCopy cpy_info = {
230  .bufferOffset = 0,
231  .imageSubresource = {
232  .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
233  .mipLevel = 0,
234  .baseArrayLayer = 0,
235  .layerCount = ArraySize,
236  },
237  .imageOffset = { 0, 0, 0 },
238  .imageExtent = { Width, Height, Depth }
239  };
240  vkCmdCopyBufferToImage(cmd_buf, buf_img_upload.buffer, Info->Image,
241  VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &cpy_info);
242 
243 
244  IMAGE_BARRIER(cmd_buf,
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,
251  );
252 
254 
255 
256  VkDescriptorImageInfo desc_img_info = {
257  .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
258  .imageView = Info->View,
259  .sampler = qvk.tex_sampler,
260  };
261 
262  VkWriteDescriptorSet s = {
263  .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
264  .dstSet = qvk.desc_set_textures_even,
265  .dstBinding = Binding,
266  .dstArrayElement = 0,
267  .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
268  .descriptorCount = 1,
269  .pImageInfo = &desc_img_info,
270  };
271 
272  vkUpdateDescriptorSets(qvk.device, 1, &s, 0, NULL);
273 
274  s.dstSet = qvk.desc_set_textures_odd;
275  vkUpdateDescriptorSets(qvk.device, 1, &s, 0, NULL);
276 
277  vkQueueWaitIdle(qvk.queue_graphics);
278 
279  buffer_destroy(&buf_img_upload);
280 
281  return VK_SUCCESS;
282 }
283 
284 #define ISBITMASK(header,r,g,b,a) ( header.RBitMask == r && header.GBitMask == g && header.BBitMask == b && header.ABitMask == a )
285 
286 qboolean LoadImageFromDDS(const char* FileName, uint32_t Binding, struct ImageGPUInfo* Info, const char* DebugName)
287 {
288  unsigned char* data = NULL;
289  ssize_t len = FS_LoadFile(FileName, (void**)&data);
290 
291  if (!data)
292  {
293  Com_EPrintf("Couldn't read file %s\n", FileName);
294  return qfalse;
295  }
296 
297  qboolean retval = qfalse;
298 
299  const DDS_HEADER* dds = (DDS_HEADER*)data;
300  const DDS_HEADER_DXT10* dxt10 = (DDS_HEADER_DXT10*)(data + sizeof(DDS_HEADER));
301 
302  if (dds->magic != DDS_MAGIC || dds->size != sizeof(DDS_HEADER) - 4)
303  {
304  Com_EPrintf("File %s does not have the expected DDS file format\n", FileName);
305  goto done;
306  }
307 
308  int Cube = 0;
309  int ArraySize = 1;
310  VkFormat PixelFormat = VK_FORMAT_UNDEFINED;
311  size_t dds_header_size = sizeof(DDS_HEADER);
312 
313  if (dds->ddspf.fourCC == MAKEFOURCC('D', 'X', '1', '0'))
314  {
316  PixelFormat = VK_FORMAT_R8G8B8A8_UNORM;
317  else if (dxt10->dxgiFormat == DXGI_FORMAT_R32_FLOAT)
318  PixelFormat = VK_FORMAT_R32_SFLOAT;
319  else if (dxt10->dxgiFormat == DXGI_FORMAT_R32G32B32A32_FLOAT)
320  PixelFormat = VK_FORMAT_R32G32B32A32_SFLOAT;
321  else
322  {
323  Com_EPrintf("File %s uses an unsupported pixel format (%d)\n", FileName, dxt10->dxgiFormat);
324  goto done;
325  }
326 
327  Cube = (dxt10->miscFlag & D3D11_RESOURCE_MISC_TEXTURECUBE) != 0;
328  ArraySize = dxt10->arraySize * (Cube ? 6 : 1);
329  dds_header_size = sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10);
330  }
331  else
332  {
334  {
335  Cube = 1;
336  ArraySize = 6;
337  }
338 
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;
345  else
346  Com_EPrintf("File %s uses an unsupported pixel format.\n", FileName);
347  }
348 
349  UploadImage(data + dds_header_size, len - dds_header_size, dds->width, dds->height, dds->depth, ArraySize, Cube, PixelFormat, Binding, Info, DebugName);
350 
351 done:
352  FS_FreeFile(data);
353  return retval;
354 }
355 
356 VkDescriptorSetLayout* SkyGetDescriptorLayout()
357 {
359 }
360 
361 VkDescriptorSet SkyGetDescriptorSet()
362 {
364 }
365 
366 VkResult
368 {
369  VkDescriptorSetLayoutBinding ubo_layout_binding = {
370  .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
371  .descriptorCount = 1,
372  .binding = PRECOMPUTED_SKY_BINDING_IDX,
373  .stageFlags = VK_SHADER_STAGE_ALL,
374  };
375 
376  VkDescriptorSetLayoutCreateInfo layout_info = {
377  .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
378  .bindingCount = 1,
379  .pBindings = &ubo_layout_binding,
380  };
381 
382  _VK(vkCreateDescriptorSetLayout(qvk.device, &layout_info, NULL, &uniform_precomputed_descriptor_layout));
383 
384  {
386  VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
387  VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
388 
389  ATTACH_LABEL_VARIABLE_NAME(atmosphere_params_buffer.buffer, IMAGE_VIEW, "AtmosphereParameters");
390  }
391 
392  VkDescriptorPoolSize pool_size = {
393  .type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
394  .descriptorCount = MAX_FRAMES_IN_FLIGHT,
395  };
396 
397  VkDescriptorPoolCreateInfo pool_info = {
398  .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
399  .poolSizeCount = 1,
400  .pPoolSizes = &pool_size,
401  .maxSets = MAX_FRAMES_IN_FLIGHT,
402  };
403 
404  _VK(vkCreateDescriptorPool(qvk.device, &pool_info, NULL, &desc_pool_precomputed_ubo));
405 
406  VkDescriptorSetAllocateInfo descriptor_set_alloc_info =
407  {
408  .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
409  .descriptorPool = desc_pool_precomputed_ubo,
410  .descriptorSetCount = 1,
412  };
413 
414  {
415  _VK(vkAllocateDescriptorSets(qvk.device, &descriptor_set_alloc_info, &desc_set_precomputed_ubo));
416 
418 
419  VkDescriptorBufferInfo buf_info = {
420  .buffer = ubo->buffer,
421  .offset = 0,
422  .range = sizeof(struct AtmosphereParameters),
423  };
424 
425  VkWriteDescriptorSet output_buf_write = {
426  .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
427  .dstSet = desc_set_precomputed_ubo,
428  .dstBinding = PRECOMPUTED_SKY_BINDING_IDX,
429  .dstArrayElement = 0,
430  .descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
431  .descriptorCount = 1,
432  .pBufferInfo = &buf_info,
433  };
434 
435  vkUpdateDescriptorSets(qvk.device, 1, &output_buf_write, 0, NULL);
436  }
437 
438  return VK_SUCCESS;
439 }
440 
441 VkResult
443 {
444  vkDestroyDescriptorPool(qvk.device, desc_pool_precomputed_ubo, NULL);
445  vkDestroyDescriptorSetLayout(qvk.device, uniform_precomputed_descriptor_layout, NULL);
446  desc_pool_precomputed_ubo = VK_NULL_HANDLE;
447  uniform_precomputed_descriptor_layout = VK_NULL_HANDLE;
448 
450 
451  return VK_SUCCESS;
452 }
453 
454 VkResult
456 {
458  assert(ubo->memory != VK_NULL_HANDLE);
459  assert(ubo->buffer != VK_NULL_HANDLE);
461 
462  struct AtmosphereParameters *mapped_ubo = buffer_map(ubo);
463  assert(mapped_ubo);
464  memcpy(mapped_ubo, Constants, sizeof(struct AtmosphereParameters));
465  buffer_unmap(ubo);
466  mapped_ubo = NULL;
467 
468  return VK_SUCCESS;
469 }
470 
471 #define MATRIX(row, col) (row * 4 + col)
472 
473 void create_identity_matrix(float matrix[16])
474 {
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;
481 }
482 
483 void create_look_at_matrix(float matrix[16], vec3_t EyePosition, vec3_t EyeDirection, vec3_t UpDirection)
484 {
485  vec3_t f;
486  VectorNormalize2(EyeDirection, f);
487 
488  vec3_t s;
489  CrossProduct(UpDirection, f, s);
490  VectorNormalize2(s, s);
491 
492  vec3_t u;
493  CrossProduct(f, s, u);
494 
495  float D0 = DotProduct(s, EyePosition);
496  float D1 = DotProduct(u, EyePosition);
497  float D2 = DotProduct(f, EyePosition);
498 
499  // Set identity
500  create_identity_matrix(matrix);
501 
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];
506 
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];
511 
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;
516 }
517 
518 void
519 create_centered_orthographic_matrix(float matrix[16], float xmin, float xmax,
520  float ymin, float ymax, float znear, float zfar)
521 {
522  float width, height;
523 
524  width = xmax - xmin;
525  height = ymax - ymin;
526  //float fRange = 1.0f / (znear - zfar);
527  float fRange = 1.0f / (zfar - znear);
528 
529  matrix[0] = 2 / width;
530  matrix[4] = 0;
531  matrix[8] = 0;
532  matrix[12] = -(xmax + xmin) / width;
533 
534  matrix[1] = 0;
535  matrix[5] = 2 / height;
536  matrix[9] = 0;
537  matrix[13] = -(ymax + ymin) / height;
538 
539  matrix[2] = 0;
540  matrix[6] = 0;
541  matrix[10] = fRange;
542  matrix[14] = -fRange * znear;
543 
544  matrix[3] = 0;
545  matrix[7] = 0;
546  matrix[11] = 0;
547  matrix[15] = 1;
548 }
549 
551 {
552  float Proj[16];
553  float View[16];
554 
555  float BoundingOffset = ShadowmapWorldSize * 0.75f;
556  create_centered_orthographic_matrix(Proj, -BoundingOffset, BoundingOffset, -BoundingOffset, BoundingOffset, -ShadowmapWorldSize * 0.75f, ShadowmapWorldSize * 0.75f);
557  vec3_t up = { 0, 0, 1.0f };
558  vec3_t origin = { 0.f };
560 
562 }
563 
564 
566 {
567  const char* Planet;
568 
569  if (preset == SKY_EARTH)
570  {
571  Planet = "earth";
573  }
574  else if (preset == SKY_STROGGOS)
575  {
576  Planet = "stroggos";
578  }
579 
583 
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);
588  LoadImageFromDDS(FileBuf, BINDING_OFFSET_SKY_SCATTERING, &SkyInscatter, "SkyInscatter");
589  Q_snprintf(FileBuf, sizeof(FileBuf), "env/irradiance_%s.dds", Planet);
590  LoadImageFromDDS(FileBuf, BINDING_OFFSET_SKY_IRRADIANCE, &SkyIrradiance, "SkyIrradiance");
591 
593 
594  return VK_SUCCESS;
595 }
596 
598 {
599  LoadImageFromDDS("env/clouds.dds", BINDING_OFFSET_SKY_CLOUDS, &SkyClouds, "SkyClouds");
600 
601  LoadImageFromDDS("env/terrain_albedo.dds", BINDING_OFFSET_TERRAIN_ALBEDO, &TerrainAlbedo, "SkyGBufferAlbedo");
602  LoadImageFromDDS("env/terrain_normal.dds", BINDING_OFFSET_TERRAIN_NORMALS, &TerrainNormals, "SkyGBufferNormals");
603  LoadImageFromDDS("env/terrain_depth.dds", BINDING_OFFSET_TERRAIN_DEPTH, &TerrainDepth, "SkyGBufferDepth");
604 
606 
607  return VK_SUCCESS;
608 }
609 
611 {
615 
617 
621 
623 }
624 
625 // ----------------------------------------------------------------------------
626 // --Shadows--
627 //
628 // Data
629 // ----------------------------------------------------------------------------
630 
632 {
633  float x;
634  float y;
635  float z;
636 };
637 
639 {
640  uint32_t A;
641  uint32_t B;
642  uint32_t C;
643 };
644 
645 struct Shadowmap
646 {
647  VkImage TargetTexture;
648  VkDeviceMemory AllocatedMemory;
649  VkImageView DepthView;
650  VkSampler DepthSampler;
651  VkFramebuffer FrameBuffer;
652  uint32_t Width;
653  uint32_t Height;
654  VkFormat DepthFormat;
655 };
656 
658 {
661  uint32_t IndexCount;
662 };
663 
664 // ----------------------------------------------------------------------------
665 // Data
666 // ----------------------------------------------------------------------------
667 
670 
671 extern VkPipelineLayout pipeline_layout_smap;
672 extern VkRenderPass render_pass_smap;
673 extern VkPipeline pipeline_smap;
674 
675 // ----------------------------------------------------------------------------
676 // Functions
677 // ----------------------------------------------------------------------------
678 
679 struct ShadowmapGeometry FillVertexAndIndexBuffers(const char* FileName, unsigned int SideSize, float size_km)
680 {
681  struct ShadowmapGeometry result = { 0 };
682 
683 
684  unsigned char* file_data = NULL;
685  ssize_t file_len = FS_LoadFile(FileName, (void**)&file_data);
686 
687  if (!file_data)
688  {
689  Com_EPrintf("Couldn't read file %s\n", FileName);
690  goto done;
691  }
692 
693  DDS_HEADER* dds = (DDS_HEADER*)file_data;
694  DDS_HEADER_DXT10* dxt10 = (DDS_HEADER_DXT10*)(file_data + sizeof(DDS_HEADER));
695 
696  if (dds->magic != DDS_MAGIC || dds->size != sizeof(DDS_HEADER) - 4 || dds->ddspf.fourCC != MAKEFOURCC('D', 'X', '1', '0') || dxt10->dxgiFormat != DXGI_FORMAT_R32_FLOAT)
697  {
698  Com_EPrintf("File %s does not have the expected DDS file format\n", FileName);
699  goto done;
700  }
701 
702  float* file_pixels = (float*)(file_data + sizeof(DDS_HEADER) + sizeof(DDS_HEADER_DXT10));
703 
704 
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);
708 
709  VertexBufferSize = align(VertexBufferSize, 64 * 1024);
710  IndexBufferSize = align(IndexBufferSize, 64 * 1024);
711 
712  BufferResource_t upload_buffer;
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);
716 
717  byte* mapped_upload_buf = (byte*)buffer_map(&upload_buffer);
718 
719  struct ShadowVertex* vertexes = (struct ShadowVertex*)mapped_upload_buf;
720  struct ShadowFace* indexes = (struct ShadowFace*)(mapped_upload_buf + VertexBufferSize);
721 
722  if (!vertexes || !indexes)
723  return result;
724 
725  result.IndexCount = IndexCount;
726 
727  float delta = size_km / (float)SideSize;
728  float X = -0.5f * size_km;
729  float Y = -0.5f * size_km;
730 
731  for (unsigned int y = 0; y < SideSize; y++)
732  {
733  for (unsigned int x = 0; x < SideSize; x++)
734  {
735  unsigned int index = y * SideSize + x;
736 
737  float Z = file_pixels[index];
738 
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;
742  X += delta;
743  }
744  X = -0.5f * size_km;
745  Y += delta;
746  }
747 
748  unsigned int i = 0;
749  for (unsigned int y = 0; y < SideSize-1; y++)
750  {
751  for (unsigned int x = 0; x < SideSize-1; x++)
752  {
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;
758  i++;
759  indexes[i].A = lower_index;
760  indexes[i].B = upper_index;
761  indexes[i].C = upper_index + 1;
762  i++;
763  }
764  }
765 
766  buffer_unmap(&upload_buffer);
767 
768 
769  buffer_create(&result.Vertexes, VertexBufferSize,
770  VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
771  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
772  ATTACH_LABEL_VARIABLE_NAME(result.Vertexes.buffer, BUFFER, "Shadowmap Vertex Buffer");
773 
774  buffer_create(&result.Indexes, IndexBufferSize,
775  VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT,
776  VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
777  ATTACH_LABEL_VARIABLE_NAME(result.Indexes.buffer, BUFFER, "Shadowmap Index Buffer");
778 
779  VkCommandBuffer cmd_buf = vkpt_begin_command_buffer(&qvk.cmd_buffers_transfer);
780 
781  BUFFER_BARRIER(cmd_buf,
782  .buffer = upload_buffer.buffer,
783  .srcAccessMask = 0,
784  .dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
785  .offset = 0,
786  .size = VK_WHOLE_SIZE);
787 
788  BUFFER_BARRIER(cmd_buf,
789  .buffer = result.Vertexes.buffer,
790  .srcAccessMask = 0,
791  .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
792  .offset = 0,
793  .size = VK_WHOLE_SIZE);
794 
795  BUFFER_BARRIER(cmd_buf,
796  .buffer = result.Indexes.buffer,
797  .srcAccessMask = 0,
798  .dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
799  .offset = 0,
800  .size = VK_WHOLE_SIZE);
801 
802  VkBufferCopy region = {
803  .srcOffset = 0,
804  .dstOffset = 0,
805  .size = VertexBufferSize
806  };
807 
808  vkCmdCopyBuffer(cmd_buf, upload_buffer.buffer, result.Vertexes.buffer, 1, &region);
809 
810  region.srcOffset = VertexBufferSize;
811  region.size = IndexBufferSize;
812 
813  vkCmdCopyBuffer(cmd_buf, upload_buffer.buffer, result.Indexes.buffer, 1, &region);
814 
815  BUFFER_BARRIER(cmd_buf,
816  .buffer = result.Vertexes.buffer,
817  .srcAccessMask = 0,
818  .dstAccessMask = VK_ACCESS_VERTEX_ATTRIBUTE_READ_BIT,
819  .offset = 0,
820  .size = VK_WHOLE_SIZE);
821 
822  BUFFER_BARRIER(cmd_buf,
823  .buffer = result.Indexes.buffer,
824  .srcAccessMask = 0,
825  .dstAccessMask = VK_ACCESS_INDEX_READ_BIT,
826  .offset = 0,
827  .size = VK_WHOLE_SIZE);
828 
829  IMAGE_BARRIER(cmd_buf,
830  .image = ShadowmapData.TargetTexture,
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);
836 
838 
839  vkQueueWaitIdle(qvk.queue_transfer);
840  buffer_destroy(&upload_buffer);
841 
842 done:
843  if (file_data)
844  FS_FreeFile(file_data);
845 
846  return result;
847 }
848 
849 
850 // ----------------------------------------------------------------------------
851 
852 void ReleaseShadowmap(struct Shadowmap* InOutShadowmap)
853 {
854  if (InOutShadowmap->DepthSampler)
855  vkDestroySampler(qvk.device, InOutShadowmap->DepthSampler, NULL);
856 
857  if (InOutShadowmap->DepthView)
858  vkDestroyImageView(qvk.device, InOutShadowmap->DepthView, NULL);
859  if (InOutShadowmap->TargetTexture)
860  vkDestroyImage(qvk.device, InOutShadowmap->TargetTexture, NULL);
861  if (InOutShadowmap->AllocatedMemory)
862  vkFreeMemory(qvk.device, InOutShadowmap->AllocatedMemory, NULL);
863 
864  if (InOutShadowmap->FrameBuffer)
865  vkDestroyFramebuffer(qvk.device, InOutShadowmap->FrameBuffer, NULL);
866 }
867 
868 void CreateShadowMap(struct Shadowmap* InOutShadowmap)
869 {
870  InOutShadowmap->DepthFormat = VK_FORMAT_D32_SFLOAT;
871  InOutShadowmap->Width = ShadowmapSize;
872  InOutShadowmap->Height = ShadowmapSize;
873 
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,
879  .extent.depth = 1,
880  .mipLevels = 1,
881  .arrayLayers = 1,
882  .samples = VK_SAMPLE_COUNT_1_BIT,
883  .tiling = VK_IMAGE_TILING_OPTIMAL,
884  .format = InOutShadowmap->DepthFormat, // Depth stencil attachment
885  .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, // We will sample directly from the depth attachment for the shadow mapping
886  };
887 
888  _VK(vkCreateImage(qvk.device, &ShadowTexInfo, NULL, &InOutShadowmap->TargetTexture));
889  ATTACH_LABEL_VARIABLE_NAME(InOutShadowmap->TargetTexture, IMAGE_VIEW, "EnvShadowMap");
890 
891  VkMemoryRequirements memReqs = {0};
892  vkGetImageMemoryRequirements(qvk.device, InOutShadowmap->TargetTexture, &memReqs);
893 
894  VkMemoryAllocateInfo memAlloc =
895  {
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)
899  };
900 
901  _VK(vkAllocateMemory(qvk.device, &memAlloc, NULL, &InOutShadowmap->AllocatedMemory));
902  _VK(vkBindImageMemory(qvk.device, InOutShadowmap->TargetTexture, InOutShadowmap->AllocatedMemory, 0));
903 
904  VkImageViewCreateInfo depthStencilView =
905  {
906  .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
907  .viewType = VK_IMAGE_VIEW_TYPE_2D,
908  .format = InOutShadowmap->DepthFormat,
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,
915  .image = InOutShadowmap->TargetTexture,
916  };
917 
918  _VK(vkCreateImageView(qvk.device, &depthStencilView, NULL, &InOutShadowmap->DepthView));
919 
920  VkDescriptorImageInfo desc_img_info = {
921  .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
922  .imageView = InOutShadowmap->DepthView,
923  .sampler = qvk.tex_sampler,
924  };
925 
926  VkWriteDescriptorSet s = {
927  .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
928  .dstSet = qvk.desc_set_textures_even,
929  .dstBinding = BINDING_OFFSET_TERRAIN_SHADOWMAP,
930  .dstArrayElement = 0,
931  .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
932  .descriptorCount = 1,
933  .pImageInfo = &desc_img_info,
934  };
935 
936  vkUpdateDescriptorSets(qvk.device, 1, &s, 0, NULL);
937 
938  s.dstSet = qvk.desc_set_textures_odd;
939  vkUpdateDescriptorSets(qvk.device, 1, &s, 0, NULL);
940 
941  VkSamplerCreateInfo sampler =
942  {
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,
950  .mipLodBias = 0.0f,
951  .maxAnisotropy = 1.0f,
952  .minLod = 0.0f,
953  .maxLod = 1.0f,
954  .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK,
955  };
956 
957  _VK(vkCreateSampler(qvk.device, &sampler, NULL, &InOutShadowmap->DepthSampler));
958 
959  // Create frame buffer
960  VkFramebufferCreateInfo fbufCreateInfo =
961  {
962  .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
963  .renderPass = render_pass_smap,
964  .attachmentCount = 1,
965  .pAttachments = &InOutShadowmap->DepthView,
966  .width = InOutShadowmap->Width,
967  .height = InOutShadowmap->Height,
968  .layers = 1,
969  };
970  _VK(vkCreateFramebuffer(qvk.device, &fbufCreateInfo, NULL, &InOutShadowmap->FrameBuffer));
971 }
972 
973 // ----------------------------------------------------------------------------
974 
976 {
979 }
980 
982 {
985  memset(&ShadowmapGrid, 0, sizeof(ShadowmapGrid));
986 
988 }
989 // ----------------------------------------------------------------------------
990 
991 void RecordCommandBufferShadowmap(VkCommandBuffer cmd_buf)
992 {
993  IMAGE_BARRIER(cmd_buf,
994  .image = ShadowmapData.TargetTexture,
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,
1000  );
1001 
1002  VkClearValue clearValues;
1003  clearValues.depthStencil.depth = 1.0f;
1004  clearValues.depthStencil.stencil = 0;
1005 
1006  VkRenderPassBeginInfo renderPassBeginInfo =
1007  {
1008  .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
1009  .renderPass = render_pass_smap,
1010  .framebuffer = ShadowmapData.FrameBuffer,
1011  .renderArea.extent.width = ShadowmapData.Width,
1012  .renderArea.extent.height = ShadowmapData.Height,
1013  .clearValueCount = 1,
1014  .pClearValues = &clearValues,
1015  };
1016 
1017  vkCmdBeginRenderPass(cmd_buf, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
1018 
1019  vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline_smap);
1020 
1021  VkViewport viewport =
1022  {
1023  .width = ShadowmapData.Width,
1024  .height = ShadowmapData.Height,
1025  .minDepth = 0,
1026  .maxDepth = 1.0f,
1027  };
1028 
1029  vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
1030 
1031  VkRect2D rect2D =
1032  {
1033  .extent.width = ShadowmapData.Width,
1034  .extent.height = ShadowmapData.Height,
1035  .offset.x = 0,
1036  .offset.y = 0,
1037  };
1038 
1039  vkCmdSetScissor(cmd_buf, 0, 1, &rect2D);
1040  float depthBiasConstant = 0.f;
1041  float depthBiasSlope = 0.f;
1042 
1043  vkCmdPushConstants(cmd_buf, pipeline_layout_smap, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(float) * 16, terrain_shadowmap_viewproj);
1044 
1045  VkDeviceSize offsets[1] = { 0 };
1046  vkCmdBindVertexBuffers(cmd_buf, 0, 1, &ShadowmapGrid.Vertexes.buffer, offsets);
1047  vkCmdBindIndexBuffer(cmd_buf, ShadowmapGrid.Indexes.buffer, 0, VK_INDEX_TYPE_UINT32);
1048  vkCmdDrawIndexed(cmd_buf, ShadowmapGrid.IndexCount, 1, 0, 0, 0);
1049  vkCmdEndRenderPass(cmd_buf);
1050 
1051  IMAGE_BARRIER(cmd_buf,
1052  .image = ShadowmapData.TargetTexture,
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,
1058  );
1059 }
SkyTransmittance
struct ImageGPUInfo SkyTransmittance
Definition: precomputed_sky.c:81
BUFFER_BARRIER
#define BUFFER_BARRIER(cmd_buf,...)
Definition: vk_util.h:68
DDS_HEADER::width
uint32_t width
Definition: dds.h:265
Shadowmap::TargetTexture
VkImage TargetTexture
Definition: precomputed_sky.c:647
DXGI_FORMAT_R32_FLOAT
@ DXGI_FORMAT_R32_FLOAT
Definition: dds.h:93
ReleaseShadowmapResources
void ReleaseShadowmapResources()
Definition: precomputed_sky.c:981
ShadowmapGeometry::Vertexes
BufferResource_t Vertexes
Definition: precomputed_sky.c:659
AtmosphereParameters
Definition: precomputed_sky.h:39
MAX_FRAMES_IN_FLIGHT
#define MAX_FRAMES_IN_FLIGHT
Definition: vkpt.h:140
height
static int height
Definition: physical_sky.c:39
precomputed_sky.h
Q_snprintf
size_t Q_snprintf(char *dest, size_t size, const char *fmt,...)
Definition: shared.c:846
PRECOMPUTED_SKY_BINDING_IDX
#define PRECOMPUTED_SKY_BINDING_IDX
Definition: precomputed_sky.c:93
ShadowVertex::x
float x
Definition: precomputed_sky.c:633
ShadowFace::A
uint32_t A
Definition: precomputed_sky.c:640
QVK_s::device
VkDevice device
Definition: vkpt.h:172
ShadowmapGrid
struct ShadowmapGeometry ShadowmapGrid
Definition: precomputed_sky.c:669
pipeline_smap
VkPipeline pipeline_smap
Definition: shadow_map.c:26
align
static size_t align(size_t x, size_t alignment)
Definition: vk_util.h:143
uniform_precomputed_descriptor_layout
VkDescriptorSetLayout uniform_precomputed_descriptor_layout
Definition: precomputed_sky.c:90
ReleaseShadowmap
void ReleaseShadowmap(struct Shadowmap *InOutShadowmap)
Definition: precomputed_sky.c:852
DDS_MAGIC
#define DDS_MAGIC
Definition: dds.h:233
create_look_at_matrix
void create_look_at_matrix(float matrix[16], vec3_t EyePosition, vec3_t EyeDirection, vec3_t UpDirection)
Definition: precomputed_sky.c:483
QVK_s::cmd_buffers_transfer
cmd_buf_group_t cmd_buffers_transfer
Definition: vkpt.h:195
Params_Earth
static struct AtmosphereParameters Params_Earth
Definition: precomputed_sky.c:38
DXGI_FORMAT_R32G32B32A32_FLOAT
@ DXGI_FORMAT_R32G32B32A32_FLOAT
Definition: dds.h:54
BINDING_OFFSET_SKY_CLOUDS
#define BINDING_OFFSET_SKY_CLOUDS
Definition: global_textures.h:134
Shadowmap::DepthFormat
VkFormat DepthFormat
Definition: precomputed_sky.c:654
ImageGPUInfo::Image
VkImage Image
Definition: precomputed_sky.c:66
DIST_TO_HORIZON
#define DIST_TO_HORIZON(LOW, HIGH)
Definition: precomputed_sky.c:36
DDS_HEADER::depth
uint32_t depth
Definition: dds.h:267
UploadImage
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)
Definition: precomputed_sky.c:103
buffer_unmap
void buffer_unmap(BufferResource_t *buf)
Definition: vk_util.c:159
QVK_s::current_frame_index
uint32_t current_frame_index
Definition: vkpt.h:219
vkpt.h
QVK_s::tex_sampler
VkSampler tex_sampler
Definition: vkpt.h:255
DXGI_FORMAT_B8G8R8A8_UNORM
@ DXGI_FORMAT_B8G8R8A8_UNORM
Definition: dds.h:139
buffer_destroy
VkResult buffer_destroy(BufferResource_t *buf)
Definition: vk_util.c:132
DDS_HEADER::ddspf
DDS_PIXELFORMAT ddspf
Definition: dds.h:270
BINDING_OFFSET_TERRAIN_ALBEDO
#define BINDING_OFFSET_TERRAIN_ALBEDO
Definition: global_textures.h:135
ShadowmapGeometry::Indexes
BufferResource_t Indexes
Definition: precomputed_sky.c:660
ATTACH_LABEL_VARIABLE_NAME
#define ATTACH_LABEL_VARIABLE_NAME(a, type, name)
Definition: vk_util.h:138
Shadowmap::Width
uint32_t Width
Definition: precomputed_sky.c:652
Params_Stroggos
static struct AtmosphereParameters Params_Stroggos
Definition: precomputed_sky.c:50
SkyInscatter
struct ImageGPUInfo SkyInscatter
Definition: precomputed_sky.c:82
QVK_s::cmd_buffers_graphics
cmd_buf_group_t cmd_buffers_graphics
Definition: vkpt.h:193
D3D11_RESOURCE_MISC_TEXTURECUBE
@ D3D11_RESOURCE_MISC_TEXTURECUBE
Definition: dds.h:256
ShadowmapData
struct Shadowmap ShadowmapData
Definition: precomputed_sky.c:668
DDS_HEADER::caps2
uint32_t caps2
Definition: dds.h:272
QVK_s::desc_set_textures_even
VkDescriptorSet desc_set_textures_even
Definition: vkpt.h:229
DDS_HEADER::size
uint32_t size
Definition: dds.h:262
DDS_CUBEMAP
#define DDS_CUBEMAP
Definition: dds.h:229
width
static int width
Definition: physical_sky.c:38
SkyLoadScatterParameters
VkResult SkyLoadScatterParameters(SkyPreset preset)
Definition: precomputed_sky.c:565
Shadowmap::DepthView
VkImageView DepthView
Definition: precomputed_sky.c:649
FillVertexAndIndexBuffers
struct ShadowmapGeometry FillVertexAndIndexBuffers(const char *FileName, unsigned int SideSize, float size_km)
Definition: precomputed_sky.c:679
get_memory_type
uint32_t get_memory_type(uint32_t mem_req_type_bits, VkMemoryPropertyFlags mem_prop)
Definition: vk_util.c:45
_VK
#define _VK(...)
Definition: vkpt.h:65
BINDING_OFFSET_SKY_SCATTERING
#define BINDING_OFFSET_SKY_SCATTERING
Definition: global_textures.h:132
QVK_s::queue_idx_graphics
int32_t queue_idx_graphics
Definition: vkpt.h:176
Shadowmap::Height
uint32_t Height
Definition: precomputed_sky.c:653
ShadowFace::C
uint32_t C
Definition: precomputed_sky.c:642
forward
static vec3_t forward
Definition: p_view.c:27
DDS_HEADER_DXT10::miscFlag
uint32_t miscFlag
Definition: dds.h:282
SkyReleaseDataGPU
void SkyReleaseDataGPU()
Definition: precomputed_sky.c:610
MATRIX
#define MATRIX(row, col)
Definition: precomputed_sky.c:471
RecordCommandBufferShadowmap
void RecordCommandBufferShadowmap(VkCommandBuffer cmd_buf)
Definition: precomputed_sky.c:991
STROGGOS_SURFACE_RADIUS
#define STROGGOS_SURFACE_RADIUS
Definition: precomputed_sky.c:34
BufferResource_s::buffer
VkBuffer buffer
Definition: vk_util.h:34
ShadowmapGeometry
Definition: precomputed_sky.c:657
SkyGetDescriptorSet
VkDescriptorSet SkyGetDescriptorSet()
Definition: precomputed_sky.c:361
ShadowmapSize
static const uint32_t ShadowmapSize
Definition: precomputed_sky.h:22
STROGGOS_ATMOSPHERE_RADIUS
#define STROGGOS_ATMOSPHERE_RADIUS
Definition: precomputed_sky.c:35
origin
static vec3_t origin
Definition: mesh.c:27
CreateShadowMap
void CreateShadowMap(struct Shadowmap *InOutShadowmap)
Definition: precomputed_sky.c:868
DDS_HEADER
Definition: dds.h:259
UpdateTerrainShadowMapView
void UpdateTerrainShadowMapView(vec3_t forward)
Definition: precomputed_sky.c:550
atmosphere_params_buffer
static BufferResource_t atmosphere_params_buffer
Definition: precomputed_sky.c:96
terrain_shadowmap_viewproj
float terrain_shadowmap_viewproj[16]
Definition: precomputed_sky.c:99
TerrainNormals
struct ImageGPUInfo TerrainNormals
Definition: precomputed_sky.c:87
SkyInitializeDataGPU
VkResult SkyInitializeDataGPU()
Definition: precomputed_sky.c:597
ShadowmapGeometry::IndexCount
uint32_t IndexCount
Definition: precomputed_sky.c:661
EARTH_SURFACE_RADIUS
#define EARTH_SURFACE_RADIUS
Definition: precomputed_sky.c:32
BINDING_OFFSET_SKY_IRRADIANCE
#define BINDING_OFFSET_SKY_IRRADIANCE
Definition: global_textures.h:133
VectorNormalize2
vec_t VectorNormalize2(vec3_t v, vec3_t out)
Definition: shared.c:73
BINDING_OFFSET_TERRAIN_DEPTH
#define BINDING_OFFSET_TERRAIN_DEPTH
Definition: global_textures.h:137
SkyClouds
struct ImageGPUInfo SkyClouds
Definition: precomputed_sky.c:84
QVK_s::desc_set_textures_odd
VkDescriptorSet desc_set_textures_odd
Definition: vkpt.h:230
ImageGPUInfo::View
VkImageView View
Definition: precomputed_sky.c:68
qvk
QVK_t qvk
Definition: main.c:377
ImageGPUInfo::DeviceMemory
VkDeviceMemory DeviceMemory
Definition: precomputed_sky.c:67
vkpt_uniform_precomputed_buffer_create
VkResult vkpt_uniform_precomputed_buffer_create()
Definition: precomputed_sky.c:367
BINDING_OFFSET_TERRAIN_NORMALS
#define BINDING_OFFSET_TERRAIN_NORMALS
Definition: global_textures.h:136
SkyGetDescriptorLayout
VkDescriptorSetLayout * SkyGetDescriptorLayout()
Definition: precomputed_sky.c:356
DDS_HEADER::magic
uint32_t magic
Definition: dds.h:261
SKY_EARTH
@ SKY_EARTH
Definition: precomputed_sky.h:31
vkpt_submit_command_buffer_simple
void vkpt_submit_command_buffer_simple(VkCommandBuffer cmd_buf, VkQueue queue, qboolean all_gpus)
Definition: main.c:3473
up
static vec3_t up
Definition: p_view.c:27
TerrainDepth
struct ImageGPUInfo TerrainDepth
Definition: precomputed_sky.c:88
EARTH_ATMOSPHERE_RADIUS
#define EARTH_ATMOSPHERE_RADIUS
Definition: precomputed_sky.c:33
render_pass_smap
VkRenderPass render_pass_smap
Definition: shadow_map.c:25
Constants
const struct AtmosphereParameters * Constants
Definition: precomputed_sky.c:30
Shadowmap::DepthSampler
VkSampler DepthSampler
Definition: precomputed_sky.c:650
DDS_PIXELFORMAT::fourCC
uint32_t fourCC
Definition: dds.h:186
BufferResource_s
Definition: vk_util.h:33
AtmosphereParameters::solar_irradiance
float solar_irradiance[3]
Definition: precomputed_sky.h:41
vkpt_uniform_precomputed_buffer_destroy
VkResult vkpt_uniform_precomputed_buffer_destroy()
Definition: precomputed_sky.c:442
mult_matrix_matrix
void mult_matrix_matrix(float *p, const float *a, const float *b)
Definition: matrix.c:281
pipeline_layout_smap
VkPipelineLayout pipeline_layout_smap
Definition: shadow_map.c:24
ISBITMASK
#define ISBITMASK(header, r, g, b, a)
Definition: precomputed_sky.c:284
DDS_HEADER_DXT10
Definition: dds.h:278
InitializeShadowmapResources
void InitializeShadowmapResources()
Definition: precomputed_sky.c:975
ShadowmapGridSize
static const uint32_t ShadowmapGridSize
Definition: precomputed_sky.h:24
buffer_create
VkResult buffer_create(BufferResource_t *buf, VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags mem_properties)
Definition: vk_util.c:57
DDS_CUBEMAP_ALLFACES
#define DDS_CUBEMAP_ALLFACES
Definition: dds.h:225
ShadowVertex::z
float z
Definition: precomputed_sky.c:635
TerrainAlbedo
struct ImageGPUInfo TerrainAlbedo
Definition: precomputed_sky.c:86
IMAGE_BARRIER
#define IMAGE_BARRIER(cmd_buf,...)
Definition: vk_util.h:55
ShadowVertex::y
float y
Definition: precomputed_sky.c:634
Shadowmap::AllocatedMemory
VkDeviceMemory AllocatedMemory
Definition: precomputed_sky.c:648
vkpt_begin_command_buffer
VkCommandBuffer vkpt_begin_command_buffer(cmd_buf_group_t *group)
Definition: main.c:3301
SkyIrradiance
struct ImageGPUInfo SkyIrradiance
Definition: precomputed_sky.c:83
ImageGPUInfo
Definition: precomputed_sky.c:64
buffer_map
void * buffer_map(BufferResource_t *buf)
Definition: vk_util.c:147
ShadowmapWorldSize
static const float ShadowmapWorldSize
Definition: precomputed_sky.h:25
BufferResource_s::size
size_t size
Definition: vk_util.h:36
SKY_STROGGOS
@ SKY_STROGGOS
Definition: precomputed_sky.h:32
DDS_HEADER::height
uint32_t height
Definition: dds.h:264
Shadowmap
Definition: precomputed_sky.c:645
Shadowmap::FrameBuffer
VkFramebuffer FrameBuffer
Definition: precomputed_sky.c:651
BINDING_OFFSET_TERRAIN_SHADOWMAP
#define BINDING_OFFSET_TERRAIN_SHADOWMAP
Definition: global_textures.h:138
vkpt_uniform_precomputed_buffer_update
VkResult vkpt_uniform_precomputed_buffer_update()
Definition: precomputed_sky.c:455
MAKEFOURCC
#define MAKEFOURCC(ch0, ch1, ch2, ch3)
Definition: dds.h:179
desc_set_precomputed_ubo
VkDescriptorSet desc_set_precomputed_ubo
Definition: precomputed_sky.c:91
DDS_HEADER_DXT10::dxgiFormat
DXGI_FORMAT dxgiFormat
Definition: dds.h:280
desc_pool_precomputed_ubo
static VkDescriptorPool desc_pool_precomputed_ubo
Definition: precomputed_sky.c:97
ShadowFace::B
uint32_t B
Definition: precomputed_sky.c:641
create_centered_orthographic_matrix
void create_centered_orthographic_matrix(float matrix[16], float xmin, float xmax, float ymin, float ymax, float znear, float zfar)
Definition: precomputed_sky.c:519
ShadowVertex
Definition: precomputed_sky.c:631
SkyPreset
SkyPreset
Definition: precomputed_sky.h:28
ReleaseInfo
void ReleaseInfo(struct ImageGPUInfo *Info)
Definition: precomputed_sky.c:73
QVK_s::queue_graphics
VkQueue queue_graphics
Definition: vkpt.h:173
QVK_s::device_count
int device_count
Definition: vkpt.h:167
QVK_s::queue_transfer
VkQueue queue_transfer
Definition: vkpt.h:175
ShadowFace
Definition: precomputed_sky.c:638
create_identity_matrix
void create_identity_matrix(float matrix[16])
Definition: precomputed_sky.c:473
BINDING_OFFSET_SKY_TRANSMITTANCE
#define BINDING_OFFSET_SKY_TRANSMITTANCE
Definition: global_textures.h:131
LoadImageFromDDS
qboolean LoadImageFromDDS(const char *FileName, uint32_t Binding, struct ImageGPUInfo *Info, const char *DebugName)
Definition: precomputed_sky.c:286
BufferResource_s::memory
VkDeviceMemory memory
Definition: vk_util.h:35
dds.h
DDS_HEADER_DXT10::arraySize
uint32_t arraySize
Definition: dds.h:283