19 #include "shared/shared.h"
55 VkPhysicalDeviceProperties properties;
99 if (
god_rays.descriptor_set_layout) {
101 god_rays.descriptor_set_layout = NULL;
121 #define BARRIER_COMPUTE(cmd_buf, img) \
123 VkImageSubresourceRange subresource_range = { \
124 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT, \
127 .baseArrayLayer = 0, \
130 IMAGE_BARRIER(cmd_buf, \
132 .subresourceRange = subresource_range, \
133 .srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT, \
134 .dstAccessMask = VK_ACCESS_SHADER_WRITE_BIT, \
135 .oldLayout = VK_IMAGE_LAYOUT_GENERAL, \
136 .newLayout = VK_IMAGE_LAYOUT_GENERAL, \
145 vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
god_rays.pipelines[0]);
147 VkDescriptorSet desc_sets[] = {
154 vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
god_rays.pipeline_layout, 0,
LENGTH(desc_sets),
157 vkCmdPushConstants(command_buffer,
god_rays.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0,
sizeof(
int), &pass);
161 uint32_t group_num_y = (
qvk.
extent_render.height / 2 + (group_size - 1)) / group_size;
163 vkCmdDispatch(command_buffer, group_num_x, group_num_y, 1);
173 const VkImageSubresourceRange subresource_range = {
174 .aspectMask = VK_IMAGE_ASPECT_COLOR_BIT,
179 vkCmdBindPipeline(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
god_rays.pipelines[1]);
181 VkDescriptorSet desc_sets[] = {
188 vkCmdBindDescriptorSets(command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE,
god_rays.pipeline_layout, 0,
LENGTH(desc_sets),
192 vkCmdPushConstants(command_buffer,
god_rays.pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0,
sizeof(
int), &pass);
196 uint32_t group_num_y = (
qvk.
extent_render.height + (group_size - 1)) / group_size;
198 vkCmdDispatch(command_buffer, group_num_x, group_num_y, 1);
208 const float* shadowmap_viewproj,
209 float shadowmap_depth_scale)
211 VectorAdd(world_aabb->
mins, world_aabb->
maxs, ubo->world_center);
212 VectorScale(ubo->world_center, 0.5f, ubo->world_center);
213 VectorSubtract(world_aabb->
maxs, world_aabb->
mins, ubo->world_size);
214 VectorScale(ubo->world_size, 0.5f, ubo->world_half_size_inv);
215 ubo->world_half_size_inv[0] = 1.f / ubo->world_half_size_inv[0];
216 ubo->world_half_size_inv[1] = 1.f / ubo->world_half_size_inv[1];
217 ubo->world_half_size_inv[2] = 1.f / ubo->world_half_size_inv[2];
218 ubo->shadow_map_depth_scale = shadowmap_depth_scale;
220 ubo->god_rays_intensity = max(0.f,
god_rays.intensity->value);
221 ubo->god_rays_eccentricity =
god_rays.eccentricity->value;
224 memcpy(ubo->shadow_map_VP, shadowmap_viewproj, 16 *
sizeof(
float));
231 VkSamplerReductionModeCreateInfoEXT redutcion_create_info = {
232 .sType = VK_STRUCTURE_TYPE_SAMPLER_REDUCTION_MODE_CREATE_INFO_EXT,
233 .reductionMode = VK_SAMPLER_REDUCTION_MODE_MIN_EXT
236 const VkSamplerCreateInfo sampler_create_info = {
237 .sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
238 .pNext = &redutcion_create_info,
239 .magFilter = VK_FILTER_LINEAR,
240 .minFilter = VK_FILTER_LINEAR,
241 .mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST,
242 .addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
243 .addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
244 .addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_BORDER,
245 .borderColor = VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE
253 VkDescriptorSetLayoutBinding bindings[1] = { 0 };
254 bindings[0].binding = 0;
255 bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
256 bindings[0].descriptorCount = 1;
257 bindings[0].stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
259 const VkDescriptorSetLayoutCreateInfo set_layout_create_info = {
260 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
261 .bindingCount =
LENGTH(bindings),
262 .pBindings = bindings
265 _VK(vkCreateDescriptorSetLayout(
qvk.
device, &set_layout_create_info, NULL,
269 VkDescriptorSetLayout desc_set_layouts[] = {
276 VkPushConstantRange push_constant_range = {
277 .stageFlags = VK_SHADER_STAGE_COMPUTE_BIT,
282 const VkPipelineLayoutCreateInfo layout_create_info = {
283 .sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
284 .pSetLayouts = desc_set_layouts,
285 .setLayoutCount =
LENGTH(desc_set_layouts),
286 .pushConstantRangeCount = 1,
287 .pPushConstantRanges = &push_constant_range
290 _VK(vkCreatePipelineLayout(
qvk.
device, &layout_create_info, NULL,
296 const VkPipelineShaderStageCreateInfo shader = {
297 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
298 .stage = VK_SHADER_STAGE_COMPUTE_BIT,
303 const VkPipelineShaderStageCreateInfo filter_shader = {
304 .sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
305 .stage = VK_SHADER_STAGE_COMPUTE_BIT,
310 const VkComputePipelineCreateInfo pipeline_create_infos[2] = {
312 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
317 .sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO,
318 .stage = filter_shader,
323 _VK(vkCreateComputePipelines(
qvk.
device, VK_NULL_HANDLE,
LENGTH(pipeline_create_infos), pipeline_create_infos,
329 const VkDescriptorPoolSize pool_sizes[] = {
330 { VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1 }
333 const VkDescriptorPoolCreateInfo pool_create_info = {
334 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
336 .poolSizeCount =
LENGTH(pool_sizes),
337 .pPoolSizes = pool_sizes
342 const VkDescriptorSetAllocateInfo allocate_info = {
343 .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
344 .descriptorPool =
god_rays.descriptor_pool,
345 .descriptorSetCount = 1,
346 .pSetLayouts = &
god_rays.descriptor_set_layout
356 if (
god_rays.shadow_image_view == NULL)
359 VkDescriptorImageInfo image_info = {
360 .imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
361 .imageView =
god_rays.shadow_image_view,
365 VkWriteDescriptorSet writes[1] = {
367 .sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET,
369 .descriptorCount = 1,
371 .descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
372 .pImageInfo = &image_info