39 VkAttachmentDescription depth_attachment = {
40 .format = VK_FORMAT_D32_SFLOAT,
41 .samples = VK_SAMPLE_COUNT_1_BIT,
42 .loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR,
43 .storeOp = VK_ATTACHMENT_STORE_OP_STORE,
44 .stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE,
45 .stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE,
46 .initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
47 .finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
50 VkAttachmentReference depth_attachment_ref = {
52 .layout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL_KHR,
55 VkSubpassDescription subpass = {
56 .pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS,
57 .pDepthStencilAttachment = &depth_attachment_ref,
60 VkSubpassDependency dependencies[] = {
62 .srcSubpass = VK_SUBPASS_EXTERNAL,
64 .srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
66 .dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT,
67 .dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_READ_BIT
68 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
72 VkRenderPassCreateInfo render_pass_info = {
73 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO,
75 .pAttachments = &depth_attachment,
77 .pSubpasses = &subpass,
78 .dependencyCount =
LENGTH(dependencies),
79 .pDependencies = dependencies,
91 VkPushConstantRange push_constant_range = {
92 .stageFlags = VK_SHADER_STAGE_VERTEX_BIT,
94 .size =
sizeof(float) * 16,
98 .pushConstantRangeCount = 1,
99 .pPushConstantRanges = &push_constant_range,
103 VkImageCreateInfo img_info = {
104 .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
110 .imageType = VK_IMAGE_TYPE_2D,
111 .format = VK_FORMAT_D32_SFLOAT,
114 .samples = VK_SAMPLE_COUNT_1_BIT,
115 .tiling = VK_IMAGE_TILING_OPTIMAL,
116 .usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
117 | VK_IMAGE_USAGE_SAMPLED_BIT,
118 .sharingMode = VK_SHARING_MODE_EXCLUSIVE,
120 .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
126 VkMemoryRequirements mem_req;
133 VkImageViewCreateInfo img_view_info = {
134 .sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO,
135 .viewType = VK_IMAGE_VIEW_TYPE_2D,
136 .format = VK_FORMAT_D32_SFLOAT,
138 .subresourceRange = {
139 .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,
146 VK_COMPONENT_SWIZZLE_R,
147 VK_COMPONENT_SWIZZLE_G,
148 VK_COMPONENT_SWIZZLE_B,
149 VK_COMPONENT_SWIZZLE_A,
155 img_view_info.subresourceRange.baseArrayLayer = 1;
159 img_view_info.viewType = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
160 img_view_info.subresourceRange.baseArrayLayer = 0;
161 img_view_info.subresourceRange.layerCount = 2;
169 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,.levelCount = 1,.layerCount = 2 },
170 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
171 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
172 .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
173 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
213 VkPipelineShaderStageCreateInfo shader_info[] = {
214 SHADER_STAGE(QVK_MOD_SHADOW_MAP_VERT, VK_SHADER_STAGE_VERTEX_BIT)
217 VkVertexInputBindingDescription vertex_binding_desc = {
219 .stride =
sizeof(float) * 3,
220 .inputRate = VK_VERTEX_INPUT_RATE_VERTEX
223 VkVertexInputAttributeDescription vertex_attribute_desc = {
226 .format = VK_FORMAT_R32G32B32_SFLOAT,
230 VkPipelineVertexInputStateCreateInfo vertex_input_info = {
231 .sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO,
232 .vertexBindingDescriptionCount = 1,
233 .pVertexBindingDescriptions = &vertex_binding_desc,
234 .vertexAttributeDescriptionCount = 1,
235 .pVertexAttributeDescriptions = &vertex_attribute_desc,
238 VkPipelineInputAssemblyStateCreateInfo input_assembly_info = {
239 .sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO,
240 .topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
243 VkViewport viewport = {
257 VkPipelineViewportStateCreateInfo viewport_state = {
258 .sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO,
260 .pViewports = &viewport,
262 .pScissors = &scissor,
265 VkPipelineRasterizationStateCreateInfo rasterizer_state = {
266 .sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO,
267 .polygonMode = VK_POLYGON_MODE_FILL,
269 .cullMode = VK_CULL_MODE_FRONT_BIT,
270 .frontFace = VK_FRONT_FACE_CLOCKWISE,
273 VkPipelineMultisampleStateCreateInfo multisample_state = {
274 .sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO,
275 .sampleShadingEnable = VK_FALSE,
276 .rasterizationSamples = VK_SAMPLE_COUNT_1_BIT,
277 .minSampleShading = 1.0f,
279 .alphaToCoverageEnable = VK_FALSE,
280 .alphaToOneEnable = VK_FALSE,
283 VkPipelineDepthStencilStateCreateInfo depth_stencil_state = {
284 .sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO,
285 .depthTestEnable = VK_TRUE,
286 .depthWriteEnable = VK_TRUE,
287 .depthCompareOp = VK_COMPARE_OP_LESS,
290 VkDynamicState dynamic_states[] = {
291 VK_DYNAMIC_STATE_VIEWPORT,
292 VK_DYNAMIC_STATE_SCISSOR
295 VkPipelineDynamicStateCreateInfo dynamic_state_info = {
296 .sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO,
297 .dynamicStateCount =
LENGTH(dynamic_states),
298 .pDynamicStates = dynamic_states
301 VkGraphicsPipelineCreateInfo pipeline_info = {
302 .sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO,
303 .stageCount =
LENGTH(shader_info),
304 .pStages = shader_info,
306 .pVertexInputState = &vertex_input_info,
307 .pInputAssemblyState = &input_assembly_info,
308 .pViewportState = &viewport_state,
309 .pRasterizationState = &rasterizer_state,
310 .pMultisampleState = &multisample_state,
311 .pDepthStencilState = &depth_stencil_state,
312 .pColorBlendState = NULL,
313 .pDynamicState = &dynamic_state_info,
319 .basePipelineHandle = VK_NULL_HANDLE,
320 .basePipelineIndex = -1,
326 VkImageView attachments[] = {
330 VkFramebufferCreateInfo fb_create_info = {
331 .sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
333 .attachmentCount = 1,
334 .pAttachments = attachments,
364 vkpt_shadow_map_render(VkCommandBuffer cmd_buf,
float* view_projection_matrix,
int num_static_verts,
int num_dynamic_verts,
int transparent_offset,
int num_transparent_verts)
368 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT, .levelCount = 1, .layerCount = 2 },
369 .srcAccessMask = VK_ACCESS_SHADER_READ_BIT,
370 .dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
371 .oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
372 .newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
375 VkClearValue clear_depth = { .depthStencil = { .depth = 1.f } };
377 VkRenderPassBeginInfo render_pass_info = {
378 .sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
381 .renderArea.offset = { 0, 0 },
383 .clearValueCount = 1,
384 .pClearValues = &clear_depth
387 vkCmdBeginRenderPass(cmd_buf, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
388 vkCmdBindPipeline(cmd_buf, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipeline_smap);
390 VkViewport viewport =
398 vkCmdSetViewport(cmd_buf, 0, 1, &viewport);
408 vkCmdSetScissor(cmd_buf, 0, 1, &scissor);
410 vkCmdPushConstants(cmd_buf,
pipeline_layout_smap, VK_SHADER_STAGE_VERTEX_BIT, 0,
sizeof(
float) * 16, view_projection_matrix);
412 VkDeviceSize vertex_offset = offsetof(
struct VertexBuffer, positions_bsp);
415 vkCmdDraw(cmd_buf, num_static_verts, 1, 0, 0);
417 vertex_offset = offsetof(
struct VertexBuffer, positions_instanced);
420 vkCmdDraw(cmd_buf, num_dynamic_verts, 1, 0, 0);
422 vkCmdEndRenderPass(cmd_buf);
426 vkCmdBeginRenderPass(cmd_buf, &render_pass_info, VK_SUBPASS_CONTENTS_INLINE);
428 vertex_offset = offsetof(
struct VertexBuffer, positions_bsp);
431 vkCmdDraw(cmd_buf, num_transparent_verts, 1, transparent_offset, 0);
433 vkCmdEndRenderPass(cmd_buf);
438 .subresourceRange = { .aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT,.levelCount = 1,.layerCount = 2 },
439 .srcAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
440 .dstAccessMask = VK_ACCESS_SHADER_READ_BIT,
441 .oldLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
442 .newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
453 float theta = 2.0 * M_PI * a;
462 vec3_t up_dir = { 0.0f, 0.0f, 1.0f };
464 VectorSet(up_dir, 1.f, 0.f, 0.f);
467 VectorScale(light->
direction, -1.f, look_dir);
470 CrossProduct(up_dir, look_dir, left_dir);
472 CrossProduct(look_dir, left_dir, up_dir);
485 CrossProduct(up_dir, look_dir, left_dir);
487 CrossProduct(look_dir, left_dir, up_dir);
491 float view_matrix[16] = {
492 left_dir[0], up_dir[0], look_dir[0], 0.f,
493 left_dir[1], up_dir[1], look_dir[1], 0.f,
494 left_dir[2], up_dir[2], look_dir[2], 0.f,
498 vec3_t view_aabb_min = { FLT_MAX, FLT_MAX, FLT_MAX };
499 vec3_t view_aabb_max = { -FLT_MAX, -FLT_MAX, -FLT_MAX };
501 for (
int i = 0; i < 8; i++)
504 corner[0] = (i & 1) ? bbox_max[0] : bbox_min[0];
505 corner[1] = (i & 2) ? bbox_max[1] : bbox_min[1];
506 corner[2] = (i & 4) ? bbox_max[2] : bbox_min[2];
509 float view_corner[4];
512 view_aabb_min[0] = min(view_aabb_min[0], view_corner[0]);
513 view_aabb_min[1] = min(view_aabb_min[1], view_corner[1]);
514 view_aabb_min[2] = min(view_aabb_min[2], view_corner[2]);
515 view_aabb_max[0] = max(view_aabb_max[0], view_corner[0]);
516 view_aabb_max[1] = max(view_aabb_max[1], view_corner[1]);
517 view_aabb_max[2] = max(view_aabb_max[2], view_corner[2]);
521 VectorSubtract(view_aabb_max, view_aabb_min, diagonal);
523 float maxXY = max(diagonal[0], diagonal[1]);
525 diff[0] = (maxXY - diagonal[0]) * 0.5f;
526 diff[1] = (maxXY - diagonal[1]) * 0.5f;
528 VectorSubtract(view_aabb_min,
diff, view_aabb_min);
529 VectorAdd(view_aabb_max,
diff, view_aabb_max);
531 float projection_matrix[16];
532 create_orthographic_matrix(projection_matrix, view_aabb_min[0], view_aabb_max[0], view_aabb_min[1], view_aabb_max[1], view_aabb_min[2], view_aabb_max[2]);
535 *depth_scale = view_aabb_max[2] - view_aabb_min[2];