render_system.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "render_system.h"
  2. #include "vk_initializers.h"
  3. using namespace coral_3d;
  4. render_system::render_system(coral_device& device, std::vector<VkDescriptorSetLayout>& desc_set_layouts)
  5. : device_{device}
  6. , pipeline_layout_{VK_NULL_HANDLE}
  7. {
  8. create_pipeline_layout(device_, desc_set_layouts);
  9. }
  10. render_system::~render_system()
  11. {
  12. vkDestroyPipelineLayout(device_.device(), pipeline_layout_, nullptr);
  13. }
  14. void render_system::render_gameobjects(FrameInfo& frame_info)
  15. {
  16. vkCmdBindDescriptorSets(
  17. frame_info.command_buffer,
  18. VK_PIPELINE_BIND_POINT_GRAPHICS,
  19. pipeline_layout_,
  20. 0, 1,
  21. &frame_info.global_descriptor_set,
  22. 0, nullptr
  23. );
  24. for (auto& kv : frame_info.gameobjects)
  25. {
  26. auto& obj{ kv.second };
  27. if (obj->mesh_ == nullptr) continue;
  28. obj->mesh_->draw(frame_info.command_buffer, pipeline_layout_);
  29. }
  30. }
  31. void render_system::create_pipeline_layout(coral_device &device, std::vector<VkDescriptorSetLayout>& desc_set_layouts)
  32. {
  33. VkPushConstantRange push_constant_range{};
  34. push_constant_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  35. push_constant_range.offset = 0;
  36. push_constant_range.size = sizeof(PushConstant);
  37. VkPipelineLayoutCreateInfo layout_info{ vkinit::pipeline_layout_ci() };
  38. layout_info.pushConstantRangeCount = 1;
  39. layout_info.pPushConstantRanges = &push_constant_range;
  40. layout_info.setLayoutCount = static_cast<uint32_t>(desc_set_layouts.size());
  41. layout_info.pSetLayouts = desc_set_layouts.data();
  42. if (vkCreatePipelineLayout(device.device(), &layout_info, nullptr, &pipeline_layout_) != VK_SUCCESS)
  43. throw std::runtime_error("ERROR! coral_pipeline::create_pipeline_layout() >> Failed to create pipeline layout!");
  44. }