voxel_renderer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "voxel_renderer.h"
  2. // STD
  3. #include <stdexcept>
  4. // CORAL
  5. #include "vk_initializers.h"
  6. using namespace coral_3d;
  7. struct PushConstant
  8. {
  9. glm::vec2 position;
  10. };
  11. voxel_renderer::voxel_renderer(coral_device& device, VkRenderPass render_pass, VkDescriptorSetLayout global_set_layout)
  12. : device_{ device }
  13. {
  14. create_pipeline_layout(global_set_layout);
  15. create_pipeline(render_pass);
  16. }
  17. voxel_renderer::~voxel_renderer()
  18. {
  19. vkDestroyPipelineLayout(device_.device(), pipeline_layout_, nullptr);
  20. }
  21. void voxel_renderer::render_chunks(VoxelRenderInfo& render_info)
  22. {
  23. // Bind pipeline
  24. pipeline_->bind(render_info.command_buffer);
  25. // Bind global descriptor set
  26. vkCmdBindDescriptorSets(
  27. render_info.command_buffer,
  28. VK_PIPELINE_BIND_POINT_GRAPHICS,
  29. pipeline_layout_,
  30. 0, 1,
  31. &render_info.global_descriptor_set,
  32. 0, nullptr
  33. );
  34. for (auto& chunk : render_info.chunks)
  35. {
  36. if (!chunk.is_active || chunk.mesh.get() == nullptr) continue;
  37. PushConstant push{ chunk.world_position };
  38. vkCmdPushConstants(
  39. render_info.command_buffer,
  40. pipeline_layout_,
  41. VK_SHADER_STAGE_VERTEX_BIT,
  42. 0, sizeof(PushConstant), &push);
  43. chunk.mesh->bind(render_info.command_buffer);
  44. chunk.mesh->draw(render_info.command_buffer);
  45. }
  46. }
  47. void voxel_renderer::create_pipeline_layout(VkDescriptorSetLayout global_set_layout)
  48. {
  49. VkPushConstantRange push_constant_range{};
  50. push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  51. push_constant_range.offset = 0;
  52. push_constant_range.size = sizeof(PushConstant);
  53. // Add set layouts here
  54. std::vector<VkDescriptorSetLayout> descriptor_set_layouts{global_set_layout};
  55. VkPipelineLayoutCreateInfo layout_info{ vkinit::pipeline_layout_ci() };
  56. layout_info.pushConstantRangeCount = 1;
  57. layout_info.pPushConstantRanges = &push_constant_range;
  58. layout_info.setLayoutCount = static_cast<uint32_t>(descriptor_set_layouts.size());
  59. layout_info.pSetLayouts = descriptor_set_layouts.data();
  60. if (vkCreatePipelineLayout(device_.device(), &layout_info, nullptr, &pipeline_layout_) != VK_SUCCESS)
  61. throw std::runtime_error("ERROR! voxel_renderer::create_pipeline_layout() >> Failed to create pipeline layout!");
  62. }
  63. void voxel_renderer::create_pipeline(VkRenderPass render_pass)
  64. {
  65. assert(pipeline_layout_ != nullptr &&
  66. "ERROR! voxel_renderer::create_pipeline() >> Cannot create pipeline before pipeline layout!");
  67. PipelineConfigInfo pipeline_config{};
  68. coral_pipeline::default_pipeline_config_info(pipeline_config);
  69. pipeline_config.render_pass = render_pass;
  70. pipeline_config.pipeline_layout = pipeline_layout_;
  71. pipeline_ = std::make_unique<coral_pipeline>(
  72. device_,
  73. "assets/shaders/voxel_shader.vert.spv",
  74. "assets/shaders/voxel_shader.frag.spv",
  75. pipeline_config
  76. );
  77. }