Euclid
Geometry Processing and Shape Analysis in C++
Rasterizer.h
1 
9 #pragma once
10 
11 #include <vector>
12 #include <boost/math/constants/constants.hpp>
13 #include <vulkan/vulkan.h>
14 #include <Euclid/Render/RenderCore.h>
15 #include <config.h>
16 
17 namespace Euclid
18 {
24 class RasCamera : public Camera
25 {
26 public:
30  RasCamera() = default;
31 
40  RasCamera(const Eigen::Vector3f& position,
41  const Eigen::Vector3f& focus,
42  const Eigen::Vector3f& up,
43  float tnear,
44  float tfar);
45 
46  virtual ~RasCamera();
47 
51  Eigen::Matrix4f view() const;
52 
56  virtual Eigen::Matrix4f projection() const = 0;
57 };
58 
64 class PerspRasCamera : public RasCamera
65 {
66 public:
71 
86  PerspRasCamera(const Eigen::Vector3f& position,
87  const Eigen::Vector3f& focus,
88  const Eigen::Vector3f& up,
89  float vfov,
90  float aspect,
91  float tnear,
92  float tfar);
93 
109  PerspRasCamera(const Eigen::Vector3f& position,
110  const Eigen::Vector3f& focus,
111  const Eigen::Vector3f& up,
112  float vfov,
113  unsigned width,
114  unsigned height,
115  float tnear,
116  float tfar);
117 
118  ~PerspRasCamera();
119 
123  void set_aspect(float aspect);
124 
128  void set_aspect(unsigned width, unsigned height);
129 
133  void set_fov(float vfov);
134 
138  virtual Eigen::Matrix4f projection() const override;
139 
140 private:
141  float _vfov = boost::math::float_constants::half_pi;
142  float _aspect = 1.0f;
143 };
144 
150 class OrthoRasCamera : public RasCamera
151 {
152 public:
157 
171  OrthoRasCamera(const Eigen::Vector3f& position,
172  const Eigen::Vector3f& focus,
173  const Eigen::Vector3f& up,
174  float xextent,
175  float yextent,
176  float tnear,
177  float tfar);
178 
179  ~OrthoRasCamera();
180 
184  void set_extent(float xextent, float yextent);
185 
189  virtual Eigen::Matrix4f projection() const override;
190 
191 private:
192  float _xextent;
193  float _yextent;
194 };
195 
201 {
202 public:
203  enum SampleCount
204  {
205  SAMPLE_COUNT_1 = 0x00000001,
206  SAMPLE_COUNT_2 = 0x00000002,
207  SAMPLE_COUNT_4 = 0x00000004,
208  SAMPLE_COUNT_8 = 0x00000008,
209  };
210 
211 public:
218  Rasterizer(uint32_t width = 256,
219  uint32_t height = 256,
220  SampleCount samples = SAMPLE_COUNT_1);
221 
222  ~Rasterizer();
223 
234  void attach_position_buffer(const float* positions, size_t size);
235 
244  void attach_normal_buffer(const float* normals, size_t size);
245 
257  void attach_color_buffer(const float* colors, size_t size);
258 
269  void attach_index_buffer(const unsigned* indices, size_t size);
270 
274  void release_buffers();
275 
279  void set_material(const Material& material);
280 
284  void set_light(const Light& light);
285 
289  void set_background(const Eigen::Array3f& color);
290 
294  void set_background(float r, float g, float b);
295 
299  void set_image(uint32_t width, uint32_t height, SampleCount samples);
300 
312  void render_shaded(const Eigen::Matrix4f& model,
313  const RasCamera& camera,
314  std::vector<uint8_t>& pixels,
315  bool interleaved = true);
316 
327  void render_unlit(const Eigen::Matrix4f& model,
328  const RasCamera& camera,
329  std::vector<uint8_t>& pixels,
330  bool interleaved = true);
331 
345  void render_depth(const Eigen::Matrix4f& model,
346  const RasCamera& camera,
347  std::vector<uint8_t>& pixels,
348  bool interleaved = true,
349  bool linear = true);
350 
362  void render_depth(const Eigen::Matrix4f& model,
363  const RasCamera& camera,
364  std::vector<float>& values,
365  bool linear = true);
366 
367 private:
368  enum ShaderType : int
369  {
370  SHADER_TYPE_SHADING = 0,
371  SHADER_TYPE_VCOLOR,
372  SHADER_TYPE_UNLIT,
373  SHADER_TYPE_UNLIT_VCOLOR,
374  SHADER_TYPE_DEPTH,
375  SHADER_TYPE_COUNT
376  };
377 
378 private:
379  void _create_instance();
380 
381  void _pick_physical_device();
382 
383  uint32_t _create_logical_device();
384 
385  void _create_command_pool(uint32_t queue_family_index);
386 
387  void _create_descriptor_pool();
388 
389  void _create_descriptors();
390 
391  void _create_render_pass(VkSampleCountFlagBits samples);
392 
393  void _create_pipeline(uint32_t width,
394  uint32_t height,
395  VkSampleCountFlagBits samples,
396  ShaderType shader);
397 
398  void _render_scene(const Eigen::Matrix4f& model,
399  const RasCamera& camera,
400  const std::vector<VkBuffer>& active_buffers,
401  ShaderType shader);
402 
403  void _copy_fb_to_host_buffer(VkImage src,
404  VkImageAspectFlags aspect,
405  VkBuffer dst);
406 
407  void _transfer_data(const void* data,
408  size_t byte_size,
409  VkBufferUsageFlagBits usage,
410  VkBuffer& buffer,
411  VkDeviceMemory& buffer_memory);
412 
413  uint32_t _find_proper_memory_type(uint32_t mem_type_bits,
414  VkMemoryPropertyFlags prop_flags);
415 
416  void _create_buffer(VkBuffer& bufer,
417  VkDeviceMemory& buffer_memory,
418  VkDeviceSize size,
419  VkBufferUsageFlags usage,
420  VkMemoryPropertyFlags props);
421 
422  void _release_buffer(VkBuffer& buffer, VkDeviceMemory& buffer_memory);
423 
424  void _copy_buffer(VkBuffer src, VkBuffer dst, VkDeviceSize size);
425 
426  void _create_image(VkImage& image,
427  VkDeviceMemory& memory,
428  VkFormat format,
429  VkImageUsageFlags usage,
430  VkImageTiling tiling,
431  VkMemoryPropertyFlags prop_flags,
432  uint32_t width,
433  uint32_t height,
434  VkSampleCountFlagBits samples);
435 
436  void _release_image(VkImage& buffer, VkDeviceMemory& memory);
437 
438  void _create_image_view(VkImageView& view,
439  VkImage image,
440  VkFormat format,
441  VkImageAspectFlags aspect);
442 
443  void _create_framebuffer(uint32_t width, uint32_t height);
444 
445  VkShaderModule _load_shader(const char* path);
446 
447  void _update_ubo(const VkDescriptorBufferInfo& info,
448  uint32_t binding,
449  ShaderType shader);
450 
451 private:
452  static const Material _default_material;
453 
454  static const Light _default_light;
455 
456 private:
457  Material _material;
458  Light _light;
459  Eigen::Array3f _background = Eigen::Array3f::Zero();
460  size_t _num_vertices;
461  size_t _num_indices;
462  uint32_t _width;
463  uint32_t _height;
464  SampleCount _samples;
465 
466  VkDebugUtilsMessengerEXT _debug_messenger;
467  VkInstance _instance;
468  VkPhysicalDevice _physical_device;
469  VkPhysicalDeviceMemoryProperties _physical_device_memory_properties;
470  VkDevice _device;
471  VkQueue _queue;
472  VkCommandPool _command_pool;
473  VkDescriptorPool _descriptor_pool;
474  std::vector<VkDescriptorSetLayout> _descriptor_set_layouts;
475  std::vector<VkDescriptorSet> _descriptor_sets;
476  std::vector<VkPipelineLayout> _pipeline_layouts;
477  std::vector<VkPipeline> _pipelines;
478  VkFramebuffer _framebuffer = VK_NULL_HANDLE;
479  VkRenderPass _render_pass = VK_NULL_HANDLE;
480  VkBuffer _position_buffer = VK_NULL_HANDLE;
481  VkDeviceMemory _position_buffer_memory;
482  VkBuffer _normal_buffer = VK_NULL_HANDLE;
483  VkDeviceMemory _normal_buffer_memory;
484  VkBuffer _color_buffer = VK_NULL_HANDLE;
485  VkDeviceMemory _color_buffer_memory;
486  VkBuffer _index_buffer = VK_NULL_HANDLE;
487  VkDeviceMemory _index_buffer_memory;
488  VkBuffer _transform_buffer = VK_NULL_HANDLE;
489  VkDeviceMemory _transform_buffer_memory;
490  VkBuffer _material_buffer = VK_NULL_HANDLE;
491  VkDeviceMemory _material_buffer_memory;
492  VkBuffer _light_buffer = VK_NULL_HANDLE;
493  VkDeviceMemory _light_buffer_memory;
494  VkBuffer _output_color_buffer = VK_NULL_HANDLE;
495  VkDeviceMemory _output_color_buffer_memory;
496  VkBuffer _output_depth_buffer = VK_NULL_HANDLE;
497  VkDeviceMemory _output_depth_buffer_memory;
498  VkImage _color_image = VK_NULL_HANDLE;
499  VkDeviceMemory _color_image_memory;
500  VkImageView _color_image_view = VK_NULL_HANDLE;
501  VkImage _depth_image = VK_NULL_HANDLE;
502  VkDeviceMemory _depth_image_memory;
503  VkImageView _depth_image_view = VK_NULL_HANDLE;
504  VkImage _color_resolve_image = VK_NULL_HANDLE;
505  VkDeviceMemory _color_resolve_image_memory;
506  VkImageView _color_resolve_image_view = VK_NULL_HANDLE;
507  VkImage _depth_resolve_image = VK_NULL_HANDLE;
508  VkDeviceMemory _depth_resolve_image_memory;
509  VkImageView _depth_resolve_image_view = VK_NULL_HANDLE;
510  std::vector<VkShaderModule> _shader_modules;
511 };
512 
514 } // namespace Euclid
515 
516 #include "src/Rasterizer.cpp"
virtual Eigen::Matrix4f projection() const =0
Return the projection matrix.
RasCamera()=default
Create a RasCamera.
float tnear
The near plane.
Definition: RenderCore.h:100
float tfar
The far plane.
Definition: RenderCore.h:105
Definition: AABB.h:6
A RasCamera using orthographic projection.
Definition: Rasterizer.h:150
A simple rasterizer.
Definition: Rasterizer.h:200
A camera model used for rasterization.
Definition: Rasterizer.h:24
Eigen::Matrix4f view() const
Return the view/lookat matrix.
Definition: Rasterizer.cpp:79
A simple Lambertian material model.
Definition: RenderCore.h:154
A basic positionable camera model.
Definition: RenderCore.h:17
OrthoRasCamera()
Create an OrthoRasCamera using default parameters.
Definition: Rasterizer.h:156
PerspRasCamera()
Create a PerspRasCamera using default parameters.
Definition: Rasterizer.h:70
A simple point light model.
Definition: RenderCore.h:170
A RasCamera using perspective projection.
Definition: Rasterizer.h:64