primitive_batch_pipeline.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #pragma once
  2. #include "../../primitive_batch.h"
  3. #include "../persistent_buffer.h"
  4. #include "../shader_cache.h"
  5. #include "pipeline_interface.h"
  6. #include <QMatrix4x4>
  7. #include <QVector3D>
  8. #include <memory>
  9. #include <vector>
  10. namespace Render::GL::BackendPipelines {
  11. class PrimitiveBatchPipeline : public IPipeline {
  12. public:
  13. explicit PrimitiveBatchPipeline(GL::ShaderCache *shaderCache);
  14. ~PrimitiveBatchPipeline() override;
  15. auto initialize() -> bool override;
  16. void shutdown() override;
  17. void cacheUniforms() override;
  18. [[nodiscard]] auto isInitialized() const -> bool override {
  19. return m_initialized;
  20. }
  21. void begin_frame();
  22. void upload_sphere_instances(const GL::PrimitiveInstanceGpu *data,
  23. std::size_t count);
  24. void upload_cylinder_instances(const GL::PrimitiveInstanceGpu *data,
  25. std::size_t count);
  26. void upload_cone_instances(const GL::PrimitiveInstanceGpu *data,
  27. std::size_t count);
  28. void draw_spheres(std::size_t count, const QMatrix4x4 &view_proj);
  29. void draw_cylinders(std::size_t count, const QMatrix4x4 &view_proj);
  30. void draw_cones(std::size_t count, const QMatrix4x4 &view_proj);
  31. [[nodiscard]] auto shader() const -> GL::Shader * { return m_shader; }
  32. struct Uniforms {
  33. GL::Shader::UniformHandle view_proj{GL::Shader::InvalidUniform};
  34. GL::Shader::UniformHandle light_dir{GL::Shader::InvalidUniform};
  35. GL::Shader::UniformHandle ambient_strength{GL::Shader::InvalidUniform};
  36. };
  37. Uniforms m_uniforms;
  38. private:
  39. void initialize_sphere_vao();
  40. void initialize_cylinder_vao();
  41. void initialize_cone_vao();
  42. void shutdown_vaos();
  43. void setup_instance_attributes(GLuint vao, GLuint instance_buffer);
  44. GL::ShaderCache *m_shader_cache;
  45. bool m_initialized{false};
  46. GL::Shader *m_shader{nullptr};
  47. GLuint m_sphere_vao{0};
  48. GLuint m_sphere_vertex_buffer{0};
  49. GLuint m_sphere_index_buffer{0};
  50. GLuint m_sphere_instance_buffer{0};
  51. GLsizei m_sphere_index_count{0};
  52. std::size_t m_sphere_instance_capacity{0};
  53. GLuint m_cylinder_vao{0};
  54. GLuint m_cylinder_vertex_buffer{0};
  55. GLuint m_cylinder_index_buffer{0};
  56. GLuint m_cylinder_instance_buffer{0};
  57. GLsizei m_cylinder_index_count{0};
  58. std::size_t m_cylinder_instance_capacity{0};
  59. GLuint m_cone_vao{0};
  60. GLuint m_cone_vertex_buffer{0};
  61. GLuint m_cone_index_buffer{0};
  62. GLuint m_cone_instance_buffer{0};
  63. GLsizei m_cone_index_count{0};
  64. std::size_t m_cone_instance_capacity{0};
  65. static constexpr std::size_t k_default_instance_capacity = 4096;
  66. static constexpr float k_growth_factor = 1.5F;
  67. };
  68. } // namespace Render::GL::BackendPipelines