VOX
A little voxel engine
Loading...
Searching...
No Matches
RenderAPI.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "vk_define.hpp"
4#include "vk_helper.hpp"
6#include "Command.hpp"
7#include "Image.hpp"
8#include "Descriptor.hpp"
9#include "Swapchain.hpp"
10#include "Pipeline.hpp"
11#include "Chunk.hpp"
12#include "CreateMeshData.hpp"
13#include "List.hpp"
14#include "TextRenderer.hpp"
15#include "ShaderCommon.hpp"
16#include "Buffer.hpp"
17#include "Transform.hpp"
18#include "Camera.hpp"
19#include "Model.hpp"
20#include "Item.hpp"
21#include "MemoryRange.hpp"
22#include "Settings.hpp"
23#include "DebugGui.hpp"
24
25#include "Tracy.hpp"
26#include "tracy_globals.hpp"
27#include "TracyVulkan.hpp"
28
29#define GLFW_INCLUDE_VULKAN
30#include <GLFW/glfw3.h>
31
32#include <glm/glm.hpp>
33
34#include "Tracy.hpp"
35
36#include "imgui.h"
37#include "imgui_impl_glfw.h"
38#include "imgui_impl_vulkan.h"
39
40#include <stdexcept>
41#include <vector>
42#include <optional>
43#include <unordered_map>
44#include <mutex>
45#include <map>
46#include <queue>
47#include <memory>
48#include <list>
49
51{
52 std::optional<uint32_t> graphics_family;
53 std::optional<uint32_t> present_family;
54 std::optional<uint32_t> transfer_family;
55
57 {
58 return graphics_family.has_value() && present_family.has_value() && transfer_family.has_value();
59 }
60};
61
63{
64 glm::vec3 pos;
65
66 static VkVertexInputBindingDescription getBindingDescription()
67 {
68 VkVertexInputBindingDescription bindingDescription{};
69 bindingDescription.binding = 0;
70 bindingDescription.stride = sizeof(LineVertex);
71 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
72
73 return bindingDescription;
74 }
75
76 static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
77 {
78 std::vector<VkVertexInputAttributeDescription> attributeDescriptions(1);
79
80 attributeDescriptions[0].binding = 0;
81 attributeDescriptions[0].location = 0;
82 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
83 attributeDescriptions[0].offset = offsetof(LineVertex, pos);
84
85 return attributeDescriptions;
86 }
87};
88
90{
91 glm::vec3 pos;
92 glm::vec3 normal;
93
94 static VkVertexInputBindingDescription getBindingDescription()
95 {
96 VkVertexInputBindingDescription bindingDescription{};
97 bindingDescription.binding = 0;
98 bindingDescription.stride = sizeof(EntityVertex);
99 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
100
101 return bindingDescription;
102 }
103
104 static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
105 {
106 std::vector<VkVertexInputAttributeDescription> attributeDescriptions(2);
107
108 attributeDescriptions[0].binding = 0;
109 attributeDescriptions[0].location = 0;
110 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
111 attributeDescriptions[0].offset = offsetof(EntityVertex, pos);
112
113 attributeDescriptions[1].binding = 0;
114 attributeDescriptions[1].location = 1;
115 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
116 attributeDescriptions[1].offset = offsetof(EntityVertex, normal);
117
118 return attributeDescriptions;
119 }
120};
121
123{
124 glm::vec3 pos;
125 glm::vec3 normal;
126 glm::vec2 tex_coord;
128
129 static VkVertexInputBindingDescription getBindingDescription()
130 {
131 VkVertexInputBindingDescription bindingDescription{};
132 bindingDescription.binding = 0;
133 bindingDescription.stride = sizeof(ItemVertex);
134 bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
135
136 return bindingDescription;
137 }
138
139 static std::vector<VkVertexInputAttributeDescription> getAttributeDescriptions()
140 {
141 std::vector<VkVertexInputAttributeDescription> attributeDescriptions(4);
142
143 attributeDescriptions[0].binding = 0;
144 attributeDescriptions[0].location = 0;
145 attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
146 attributeDescriptions[0].offset = offsetof(ItemVertex, pos);
147
148 attributeDescriptions[1].binding = 0;
149 attributeDescriptions[1].location = 1;
150 attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
151 attributeDescriptions[1].offset = offsetof(ItemVertex, normal);
152
153 attributeDescriptions[2].binding = 0;
154 attributeDescriptions[2].location = 2;
155 attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
156 attributeDescriptions[2].offset = offsetof(ItemVertex, tex_coord);
157
158 attributeDescriptions[3].binding = 0;
159 attributeDescriptions[3].location = 3;
160 attributeDescriptions[3].format = VK_FORMAT_R32_UINT;
161 attributeDescriptions[3].offset = offsetof(ItemVertex, texture_index);
162
163 return attributeDescriptions;
164 }
165};
166
167struct Mesh
168{
169 VkBuffer buffer;
170 VkDeviceMemory buffer_memory;
171 uint32_t vertex_count;
172 uint32_t vertex_size;
173 VkDeviceSize index_offset;
174 uint32_t index_count;
175
176 uint32_t memory_size;
177
178 VkDeviceAddress buffer_address;
179
180 union
181 {
182 uint64_t is_used = 0;
183 uint8_t used_by_frame[8];
184 };
185};
186
187struct UBO
188{
189 std::vector<VkBuffer> buffers;
190 std::vector<VkDeviceMemory> memory;
191 std::vector<void *> mapped_memory;
192};
193
195{
196 glm::vec3 sun_direction;
201 float h_mie;
202 float g;
206 alignas(16) glm::vec3 beta_rayleigh;
207 alignas(16) glm::vec3 beta_mie;
208};
209
211{
212 VkImage image;
213 VkDeviceMemory memory;
214 VkImageView view;
215 VkSampler sampler;
216
218
219 VkDescriptorSet descriptor_set;
220
221 VkFormat format;
222 VkExtent2D extent;
223
224 uint32_t width() const { return extent.width; }
225 uint32_t height() const { return extent.height; }
226
227 void clear()
228 {
229 memset(mapped_memory, 0, extent.width * extent.height * 4);
230 }
231
232 void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
233 {
234 uint8_t * pixel = (uint8_t *)mapped_memory + (y * extent.width + x) * 4;
235 pixel[0] = r;
236 pixel[1] = g;
237 pixel[2] = b;
238 pixel[3] = a;
239 }
240};
241
243{
244 uint32_t dst_binding = 0;
245 uint32_t dst_array_element = 0;
246 uint32_t descriptor_count = 1;
247 VkDescriptorType descriptor_type = VK_DESCRIPTOR_TYPE_MAX_ENUM;
248 VkDescriptorImageInfo image_info = {};
249 VkDescriptorBufferInfo buffer_info = {};
250};
251
253{
256 glm::dmat4 model;
257};
258
260{
261 uint64_t id;
262 glm::dmat4 model;
263};
264
266{
267 glm::dvec3 position;
268 double yaw = 0;
269 double pitch = 0;
270
273
274 bool visible = true;
275};
276
278{
279 alignas(16) mat4 matrice;
280 VkDeviceAddress block_vertex_buffer;
281 VkDeviceAddress water_vertex_buffer;
282};
283
284// forward declaration
285struct BlockVertex;
286
288{
289
290public:
291
292 typedef uint32_t InstanceId;
293
294 RenderAPI(GLFWwindow * window);
295 ~RenderAPI();
296
297 RenderAPI(const RenderAPI &) = delete;
298 RenderAPI(RenderAPI &&) = delete;
299 RenderAPI & operator=(const RenderAPI &) = delete;
301
302 void renderFrame();
303
304
305 void setCamera(const Camera & camera);
306 void toggleDebugText();
307 void setTargetBlock(const std::optional<glm::vec3> & target_block);
308
309 void addPlayer(const uint64_t id, const PlayerRenderData & player_data);
310 void removePlayer(const uint64_t id);
311 void updatePlayer(const uint64_t id, std::function<void(PlayerRenderData &)> fct);
312
313 void addEntity(const uint64_t id, const MeshRenderData & entity_data);
314 void removeEntity(const uint64_t id);
315 void updateEntity(const uint64_t id, std::function<void(MeshRenderData &)> fct);
316
317 void setToolbarItem(const int index, const ItemInfo::Type type);
318 void setToolbarCursor(const int index);
319
321 {
322 std::vector<BlockVertex> block_vertex;
323 std::vector<uint32_t> block_index;
324
325 std::vector<BlockVertex> water_vertex;
326 std::vector<uint32_t> water_index;
327
328 glm::dmat4 model;
329 };
331 void removeChunkFromScene(const uint64_t chunk_id);
332
333 uint64_t storeMesh(
334 const void * vertices,
335 const uint32_t vertex_count,
336 const uint32_t vertex_size,
337 const void * indices,
338 const uint32_t index_count
339 );
340 void destroyMesh(const uint64_t & mesh_id);
341 uint64_t getCubeMeshId() const { return cube_mesh_id; }
342
343
344 uint64_t createImGuiTexture(const uint32_t width, const uint32_t height);
346 const uint64_t texture_id,
347 const uint32_t x,
348 const uint32_t y,
349 const uint8_t r,
350 const uint8_t g,
351 const uint8_t b,
352 const uint8_t a = 255
353 );
354 void ImGuiTextureClear(const uint64_t texture_id);
355 void ImGuiTextureDraw(const uint64_t texture_id);
356
357private:
358
359 //###########################################################################################################
360 // #
361 // Generics #
362 // #
363 //###########################################################################################################
364
365 const std::vector<const char *> validation_layers = {
366 "VK_LAYER_KHRONOS_validation"
367 };
368
369 const std::vector<const char *> device_extensions = {
370 VK_KHR_SWAPCHAIN_EXTENSION_NAME,
371 VK_KHR_DYNAMIC_RENDERING_EXTENSION_NAME,
372 VK_EXT_CALIBRATED_TIMESTAMPS_EXTENSION_NAME,
373 VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME,
374 VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME,
375 VK_KHR_8BIT_STORAGE_EXTENSION_NAME,
376 VK_KHR_SHADER_NON_SEMANTIC_INFO_EXTENSION_NAME
377 };
378
379 GLFWwindow * window;
380
381 VkInstance instance;
382 VkDebugUtilsMessengerEXT debug_messenger;
383 VkPhysicalDevice physical_device = VK_NULL_HANDLE;
384
385 VkSurfaceKHR surface;
386
387 VkDevice device;
388 VkQueue graphics_queue;
389 VkQueue present_queue;
390 VkQueue transfer_queue;
391 QueueFamilyIndices queue_family_indices;
392
393 mutable TracyLockableN (std::mutex, global_mutex, "Vulkan Global Mutex");
394
395 // function pointers
396 PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT;
397 PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT;
398
399 PFN_vkGetPhysicalDeviceCalibrateableTimeDomainsEXT vkGetPhysicalDeviceCalibrateableTimeDomainsEXT;
400 PFN_vkGetCalibratedTimestampsEXT vkGetCalibratedTimestampsEXT;
401
402 PFN_vkGetBufferDeviceAddress vkGetBufferDeviceAddress;
403
404
405 void _createInstance();
406 bool _checkValidationLayerSupport();
407 std::vector<const char *> _getRequiredExtensions();
408
409 void _loadVulkanFunctions();
410
411 void _createSurface(GLFWwindow * window);
412
413 void _setupDebugMessenger();
414 void _populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT & create_info);
415 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
416 VkDebugUtilsMessageSeverityFlagBitsEXT message_severity,
417 VkDebugUtilsMessageTypeFlagsEXT message_type,
418 const VkDebugUtilsMessengerCallbackDataEXT * callback_data,
419 void * user_data
420 );
421
422 void _pickPhysicalDevice();
423 bool _isDeviceSuitable(VkPhysicalDevice device);
424 int _ratePhysicalDevice(VkPhysicalDevice device);
425 QueueFamilyIndices _findQueueFamilies(VkPhysicalDevice device);
426 bool _checkDeviceExtensionSupport(VkPhysicalDevice device);
427
428 void _createLogicalDevice();
429
430 //###########################################################################################################
431 // #
432 // Swapchain and framebuffers #
433 // #
434 //###########################################################################################################
435
436 Swapchain swapchain;
437
438 std::vector<VkFramebuffer> lighting_framebuffers;
439 std::vector<VkFramebuffer> shadow_framebuffers;
440 std::vector<VkFramebuffer> water_framebuffers;
441 std::vector<VkFramebuffer> hud_framebuffers;
442 VkFramebuffer prerender_item_icon_framebuffer;
443
444 void _createSwapChain(GLFWwindow * window);
445 void _recreateSwapChain(GLFWwindow * window);
446 VkSurfaceFormatKHR _chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> & available_formats);
447 VkPresentModeKHR _chooseSwapPresentMode(const std::vector<VkPresentModeKHR> & available_present_modes);
448 VkExtent2D _chooseSwapExtent(const VkSurfaceCapabilitiesKHR & capabilities, GLFWwindow * window);
449
450 void _createFramebuffers();
451 void _destroyFramebuffers();
452
453 //###########################################################################################################
454 // #
455 // Command buffer #
456 // #
457 //###########################################################################################################
458
459 VkCommandPool command_pool;
460 std::vector<VkCommandBuffer> draw_shadow_pass_command_buffers;
461 std::vector<VkCommandBuffer> draw_command_buffers;
462 std::vector<VkCommandBuffer> copy_command_buffers;
463 std::vector<VkCommandBuffer> compute_command_buffers;
464 std::vector<VkCommandBuffer> imgui_command_buffers;
465
466 VkCommandPool transfer_command_pool;
467 VkCommandBuffer transfer_command_buffers;
468 TracyLockableN (std::mutex, transfer_operation_mutex, "Vulkan Transfer Operation");
469
470 void _createCommandPool();
471 void _createCommandBuffer();
472
473 //###########################################################################################################
474 // #
475 // Synchronisation #
476 // #
477 //###########################################################################################################
478
479 std::vector<VkSemaphore> image_available_semaphores;
480 std::vector<VkSemaphore> shadow_pass_finished_semaphores;
481 std::vector<VkSemaphore> main_render_finished_semaphores;
482 std::vector<VkSemaphore> compute_finished_semaphores;
483 std::vector<VkSemaphore> copy_finished_semaphores;
484 std::vector<VkSemaphore> imgui_render_finished_semaphores;
485 std::vector<VkFence> in_flight_fences;
486 VkFence single_time_command_fence;
487
488 void _createSyncObjects();
489
490 //###########################################################################################################
491 // #
492 // Shadow maps #
493 // #
494 //###########################################################################################################
495
496 const uint shadow_maps_count = SHADOW_MAP_MAX_COUNT;
497 const uint shadow_map_size;
498
499 Image shadow_map_depth_attachement;
500
501 std::vector<VkImageView> shadow_map_views;
502
503 void _createShadowMapRessources();
504 void _destroyShadowMapRessources();
505
506 //###########################################################################################################
507 // #
508 // Images and buffers #
509 // #
510 //###########################################################################################################
511
512 Image output_attachement;
513 Image color_attachement;
514 Image depth_attachement;
515 Image block_textures;
516 Image skybox_cube_map;
517
518 Image crosshair_image;
519 Image toolbar_image;
520 Image toolbar_cursor_image;
521 Image player_skin_image;
522 Image item_icon_images;
523
524 Image debug_info_image;
525 std::vector<Buffer> debug_info_buffers;
526
527 UBO camera_ubo;
528 UBO light_mat_ubo;
529 UBO atmosphere_ubo;
530
531 void _createColorAttachement();
532 void _createDepthAttachement();
533
534 void _createUBO(UBO & ubo, const VkDeviceSize size, const uint32_t count);
535 void _createUniformBuffers();
536 void _createTextureArray(const std::vector<std::string> & file_paths, uint32_t size);
537 void _createCubeMap(const std::array<std::string, 6> & file_paths, uint32_t size);
538 void _createHudImages(
539 const std::string & file_path,
540 Image & image
541 );
542 void _createTextureImage();
543
544 //###########################################################################################################
545 // #
546 // Instance data #
547 // #
548 //###########################################################################################################
549
550 uint32_t instance_data_size;
551 uint32_t instance_data_max_count;
552 std::vector<Buffer> instance_data_buffers;
553
554 void _createInstanceData();
555 void _destroyInstanceData();
556
557 void _updateInstancesData();
558
559 //###########################################################################################################
560 // #
561 // Descriptors #
562 // #
563 //###########################################################################################################
564
565 Descriptor crosshair_image_descriptor;
566 Descriptor toolbar_image_descriptor;
567 Descriptor toolbar_cursor_image_descriptor;
568 Descriptor debug_info_image_descriptor;
569 Descriptor global_descriptor;
570
571 void _createHudDescriptors(
572 const Image & image,
573 Descriptor & descriptor
574 );
575 void _createDescriptors();
576 void _createGlobalDescriptor();
577 void _updateGlobalDescriptor();
578
579 //###########################################################################################################
580 // #
581 // Render pass #
582 // #
583 //###########################################################################################################
584
585 VkRenderPass lighting_render_pass;
586 VkRenderPass shadow_render_pass;
587 VkRenderPass water_render_pass;
588 VkRenderPass hud_render_pass;
589 VkRenderPass prerender_item_icon_render_pass;
590
591 void _createRenderPass();
592
593 //###########################################################################################################
594 // #
595 // Pipelines #
596 // #
597 //###########################################################################################################
598
599 Pipeline chunk_pipeline;
600 Pipeline water_pipeline;
601 Pipeline line_pipeline;
602 Pipeline skybox_pipeline;
603 Pipeline sun_pipeline;
604 Pipeline shadow_pipeline;
605 Pipeline test_image_pipeline;
606 Pipeline entity_pipeline;
607 Pipeline player_pipeline;
608 Pipeline hud_pipeline;
609 Pipeline prerender_item_icon_pipeline;
610 Pipeline item_icon_pipeline;
611
612 void _createPipelines();
613
614 //###########################################################################################################
615 // #
616 // ImGui and Tracy #
617 // #
618 //###########################################################################################################
619
620 VkDescriptorPool imgui_descriptor_pool;
621 std::unordered_map<uint64_t, ImGuiTexture> imgui_textures;
622 uint64_t next_imgui_texture_id = 1;
623 TracyLockableN (std::mutex, imgui_textures_mutex, "Vulkan Imgui Texture Mutex");
624
625 TracyVkCtx draw_ctx;
626
627 void _setupImgui();
628 void _destroyImGuiTextures();
629
630 void _setupTracy();
631 void _destroyTracy();
632
633 //###########################################################################################################
634 // #
635 // Meshes #
636 // #
637 //###########################################################################################################
638
639 std::unordered_map<uint64_t, Mesh> mesh_map;
640 uint64_t next_mesh_id = 1;
641 const uint64_t invalid_mesh_id = 0;
642 TracyLockableN (std::mutex, mesh_map_mutex, "Vulkan Mesh Map Mutex");
643 std::vector<uint64_t> mesh_ids_to_destroy;
644
645 uint64_t cube_mesh_id;
646 uint64_t icosphere_mesh_id;
647
648 uint64_t player_chest_mesh_id;
649 uint64_t player_head_mesh_id;
650 uint64_t player_right_leg_mesh_id;
651 uint64_t player_left_leg_mesh_id;
652 uint64_t player_right_arm_mesh_id;
653 uint64_t player_left_arm_mesh_id;
654
655 void _createMeshes();
656 void _createItemMeshes();
657 void _destroyMesh(const uint64_t & mesh_id);
658 void _destroyMeshes();
659
660 //###########################################################################################################
661 // #
662 // Chunks #
663 // #
664 //###########################################################################################################
665
666 struct ChunkMeshesInfo
667 {
668 Buffer vertex_buffer;
669
670 VkDeviceAddress block_vertex_address;
671 uint32_t block_index_offset;
672 uint32_t block_index_count;
673
674 VkDeviceAddress water_vertex_address;
675 uint32_t water_index_offset;
676 uint32_t water_index_count;
677
678 glm::dmat4 model;
679
680 union
681 {
682 uint64_t is_used = 0;
683 uint8_t used_by_frame[8];
684 };
685 };
686
687 Buffer m_chunks_indices_buffer;
688 MemoryRange m_chunks_indices_buffer_memory_range;
689
690 std::map<InstanceId, ChunkMeshesInfo> m_chunks_in_scene;
691 std::map<InstanceId, glm::dmat4> m_chunks_in_scene_rendered;
692 std::list<InstanceId> m_free_chunk_ids;
693 const InstanceId m_null_instance_id = 0;
694 std::vector<InstanceId> m_chunk_instance_to_destroy;
695 mutable TracyLockableN(std::mutex, m_chunks_in_scene_mutex, "Chunk Render Data");
696
697 void _setupChunksRessources();
698 void _resizeChunksIndicesBuffer(const VkDeviceSize & size);
699 void _deleteUnusedChunks();
700
701 void _bindChunkIndexBuffer(VkCommandBuffer command_buffer);
702 void _drawChunksBlock(
703 VkCommandBuffer command_buffer,
704 Buffer & indirect_buffer,
705 const std::vector<InstanceId> & ids
706 );
707 void _drawChunkWater(VkCommandBuffer command_buffer, const InstanceId & id);
708
709 //###########################################################################################################
710 // #
711 // Indirect Draw #
712 // #
713 //###########################################################################################################
714
715 uint32_t m_max_draw_count;
716 std::vector<std::vector<Buffer>> m_draw_chunk_block_shadow_pass_buffer;
717 std::vector<Buffer> m_draw_chunk_block_light_pass_buffer;
718
719 void _createDrawBuffer();
720 void _destroyDrawBuffer();
721
722 void _drawChunkBlock(VkCommandBuffer command_buffer, const InstanceId & id);
723
724 //###########################################################################################################
725 // #
726 // Text #
727 // #
728 //###########################################################################################################
729
730 TextRenderer text_renderer;
731
732 void _setupTextRenderer();
733 void _destroyTextRenderer();
734
735 //###########################################################################################################
736 // #
737 // Prerender #
738 // #
739 //###########################################################################################################
740
741 void _prerenderItemIconImages();
742
743 //###########################################################################################################
744 // #
745 // Helper #
746 // #
747 //###########################################################################################################
748
749 void _transitionImageLayout(
750 VkImage image,
751 VkImageLayout oldLayout,
752 VkImageLayout newLayout,
753 VkImageAspectFlags aspectMask,
754 uint32_t mipLevels,
755 VkAccessFlags srcAccessMask,
756 VkAccessFlags dstAccessMask,
757 VkPipelineStageFlags srcStageMask,
758 VkPipelineStageFlags dstStageMask
759 );
760 void _setImageLayout(
761 VkCommandBuffer command_buffer,
762 VkImage image,
763 VkImageLayout old_layout,
764 VkImageLayout new_layout,
765 VkImageSubresourceRange subresource_range,
766 VkAccessFlags srcAccessMask,
767 VkAccessFlags dstAccessMask,
768 VkPipelineStageFlags srcStageMask,
769 VkPipelineStageFlags dstStageMask
770 );
771 VkFormat _findSupportedFormat(
772 const std::vector<VkFormat> & candidates,
773 VkImageTiling tiling,
774 VkFormatFeatureFlags features
775 );
776 void _createImage(
777 uint32_t width,
778 uint32_t height,
779 uint32_t mip_levels,
780 VkFormat format,
781 VkImageTiling tiling,
782 VkImageUsageFlags usage,
783 VkMemoryPropertyFlags properties,
784 VkImage & image,
785 VkDeviceMemory & image_memory
786 );
787 void _createImageView(
788 VkImage image,
789 VkFormat format,
790 VkImageAspectFlags aspect_flags,
791 VkImageView & image_view
792 );
793 void _createBuffer(
794 VkDeviceSize size,
795 VkBufferUsageFlags usage,
796 VkMemoryPropertyFlags properties,
797 VkBuffer & buffer,
798 VkDeviceMemory & buffer_memory
799 );
800 void _copyBuffer(
801 VkBuffer src_buffer,
802 VkBuffer dst_buffer,
803 const VkBufferCopy & copy_region
804 );
805 void _copyBufferToImage(
806 VkBuffer buffer,
807 VkImage image,
808 uint32_t width,
809 uint32_t height
810 );
811
812 //###########################################################################################################
813 // #
814 // Render update #
815 // #
816 //###########################################################################################################
817
818 Camera m_camera_update;
819
820 bool m_show_debug_text_update = false;
821
822 std::optional<glm::vec3> m_target_block_update;
823
824 std::map<InstanceId, ChunkMeshCreateInfo> m_chunk_to_create;
825 std::map<InstanceId, ChunkMeshCreateInfo> m_chunk_to_delete;
826
827 std::list<std::function<void(void)>> m_update_functions;
828
829 mutable TracyLockableN(std::mutex, m_render_data_update_mutex, "Render Data Update");
830
831 void _updateRenderData();
832
833 void _updateChunksData();
834 void _updatePlayersData();
835
836 //###########################################################################################################
837 // #
838 // Render loop #
839 // #
840 //###########################################################################################################
841
842 const int m_max_frames_in_flight = 2;
843 int m_current_frame = 0;
844 uint32_t m_current_image_index = 0;
845
846 std::chrono::nanoseconds m_start_time;
847 std::chrono::nanoseconds m_current_time;
848 std::chrono::nanoseconds m_last_frame_time;
849 std::chrono::nanoseconds m_delta_time;
850
851 // For DebugGui
852 int m_frame_count;
853 std::chrono::nanoseconds m_start_time_counting_fps;
854
855 // Scene data
856 int m_window_width, m_window_height;
857 double m_aspect_ratio;
858
859 const glm::mat4 m_clip = glm::mat4(
860 1.0f, 0.0f, 0.0f, 0.0f,
861 0.0f,-1.0f, 0.0f, 0.0f,
862 0.0f, 0.0f, 0.5f, 0.0f,
863 0.0f, 0.0f, 0.5f, 1.0f
864 );
865 Camera::RenderInfo m_camera_render_info;
866 ViewProjMatrices m_camera_matrices = {};
867 ViewProjMatrices m_camera_matrices_fc = {};
868
869 std::map<uint64_t, PlayerRenderData> m_players;
870 std::map<uint64_t, MeshRenderData> m_entities;
871
872 std::vector<InstanceId> m_visible_chunks;
873 std::vector<std::vector<InstanceId>> m_shadow_visible_chunks;
874
875 ViewProjMatrices m_sun_matrices = {};
876 glm::dvec3 m_sun_position;
877
878 std::optional<glm::vec3> m_target_block;
879 mutable TracyLockableN(std::mutex, m_target_block_mutex, "Target Block");
880
881
882 AtmosphereParams m_atmosphere_params = {};
883
884 std::vector<glm::mat4> m_light_view_proj_matrices;
885 ShadowMapLight m_shadow_map_light = {};
886
887 bool m_show_debug_text = false;
888 std::string m_debug_text;
889
890 std::array<ItemInfo::Type, 9> m_toolbar_items;
891 int m_toolbar_cursor_index = 0;
892
893 void _createRenderFrameRessources();
894
895 void _prepareFrame();
896 void _updateTime();
897 void _updateDebugText();
898 void _updateVisibleChunks();
899
900 void _startFrame();
901 void _endFrame();
902
903 void _shadowPass();
904 void _lightingPass();
905
906 void _drawPlayerBodyPart(
907 const uint64_t mesh_id,
908 const glm::mat4 & model
909 );
910
911 void _copyToSwapchain();
912
913 void _drawDebugGui();
914 void _updateImGui();
915
916 std::vector<glm::mat4> _getCSMLightViewProjMatrices(
917 const glm::vec3 & light_dir,
918 const std::vector<float> & split,
919 const float blend_distance,
920 const glm::mat4 & camera_view,
921 const float cam_fov,
922 const float cam_ratio,
923 const float cam_near_plane,
924 const float cam_far_plane,
925 std::vector<float> & far_plane_distances
926 );
927
928 bool _isInsideFrustum_ndcSpace(const glm::mat4 & model, const glm::vec3 & size) const;
929 bool _isInsideFrustum_planes(
930 const glm::mat4 & view_proj,
931 const glm::mat4 & model,
932 const glm::vec3 & size
933 ) const;
934
935 std::pair<bool, Mesh> _getMesh(const uint64_t id);
936 void _drawMesh(
937 VkCommandBuffer command_buffer,
938 const Pipeline & pipeline,
939 const uint64_t mesh_id,
940 const void * push_constants,
941 const uint32_t push_constants_size,
942 const VkShaderStageFlags push_constants_stage,
943 const uint32_t instance_id = 0
944 );
945
946 void _writeTextToDebugImage(
947 VkCommandBuffer command_buffer,
948 const std::string & text,
949 const uint32_t x,
950 const uint32_t y,
951 const uint32_t font_size
952 );
953
954 void _drawHudImage(
955 const Descriptor & descriptor,
956 const VkViewport & viewport
957 );
958 void _drawItemIcon(
959 const VkViewport & viewport,
960 const uint32_t layer
961 );
962
963};
#define SHADOW_MAP_MAX_COUNT
Definition: ShaderCommon.hpp:20
Definition: Buffer.hpp:11
A simple camera class.
Definition: Camera.hpp:41
Definition: Descriptor.hpp:8
Definition: Image.hpp:11
Type
Definition: Item.hpp:14
Definition: MemoryRange.hpp:9
Definition: Pipeline.hpp:13
Definition: Model.hpp:154
Definition: Model.hpp:86
Definition: RenderAPI.hpp:288
uint64_t storeMesh(const void *vertices, const uint32_t vertex_count, const uint32_t vertex_size, const void *indices, const uint32_t index_count)
Definition: mesh.cpp:8
void addEntity(const uint64_t id, const MeshRenderData &entity_data)
Definition: render_update.cpp:50
InstanceId addChunkToScene(const ChunkMeshCreateInfo &mesh_info)
Definition: chunk_mesh.cpp:6
void removeEntity(const uint64_t id)
Definition: render_update.cpp:59
void setToolbarCursor(const int index)
Definition: render_update.cpp:87
void setToolbarItem(const int index, const ItemInfo::Type type)
Definition: render_update.cpp:78
RenderAPI(RenderAPI &&)=delete
RenderAPI(const RenderAPI &)=delete
void ImGuiTextureClear(const uint64_t texture_id)
Definition: RenderAPI.cpp:2309
void renderFrame()
Definition: render_loop.cpp:55
void setCamera(const Camera &camera)
Definition: render_update.cpp:3
void removePlayer(const uint64_t id)
Definition: render_update.cpp:31
void addPlayer(const uint64_t id, const PlayerRenderData &player_data)
Definition: render_update.cpp:22
void updateEntity(const uint64_t id, std::function< void(MeshRenderData &)> fct)
Definition: render_update.cpp:68
RenderAPI & operator=(RenderAPI &&)=delete
void toggleDebugText()
Definition: render_update.cpp:9
RenderAPI & operator=(const RenderAPI &)=delete
~RenderAPI()
Definition: RenderAPI.cpp:73
void destroyMesh(const uint64_t &mesh_id)
Definition: mesh.cpp:92
uint64_t createImGuiTexture(const uint32_t width, const uint32_t height)
Definition: RenderAPI.cpp:2198
void ImGuiTexturePutPixel(const uint64_t texture_id, const uint32_t x, const uint32_t y, const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a=255)
Definition: RenderAPI.cpp:2294
void removeChunkFromScene(const uint64_t chunk_id)
Definition: chunk_mesh.cpp:33
uint32_t InstanceId
Definition: RenderAPI.hpp:292
uint64_t getCubeMeshId() const
Definition: RenderAPI.hpp:341
void setTargetBlock(const std::optional< glm::vec3 > &target_block)
Definition: render_update.cpp:15
void updatePlayer(const uint64_t id, std::function< void(PlayerRenderData &)> fct)
Definition: render_update.cpp:40
void ImGuiTextureDraw(const uint64_t texture_id)
Definition: RenderAPI.cpp:2316
Definition: Swapchain.hpp:8
Definition: TextRenderer.hpp:12
Definition: RenderAPI.hpp:195
glm::vec3 beta_mie
Definition: RenderAPI.hpp:207
float h_rayleigh
Definition: RenderAPI.hpp:200
float n_samples
Definition: RenderAPI.hpp:204
float atmosphere_radius
Definition: RenderAPI.hpp:198
glm::vec3 sun_direction
Definition: RenderAPI.hpp:196
float h_mie
Definition: RenderAPI.hpp:201
glm::vec3 beta_rayleigh
Definition: RenderAPI.hpp:206
float sun_intensity
Definition: RenderAPI.hpp:203
float g
Definition: RenderAPI.hpp:202
float player_height
Definition: RenderAPI.hpp:199
float n_light_samples
Definition: RenderAPI.hpp:205
float earth_radius
Definition: RenderAPI.hpp:197
Definition: CreateMeshData.hpp:46
Definition: Camera.hpp:46
Definition: RenderAPI.hpp:253
glm::dmat4 model
Definition: RenderAPI.hpp:256
uint64_t block_mesh_id
Definition: RenderAPI.hpp:254
uint64_t water_mesh_id
Definition: RenderAPI.hpp:255
Definition: RenderAPI.hpp:90
glm::vec3 pos
Definition: RenderAPI.hpp:91
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition: RenderAPI.hpp:104
glm::vec3 normal
Definition: RenderAPI.hpp:92
static VkVertexInputBindingDescription getBindingDescription()
Definition: RenderAPI.hpp:94
Definition: RenderAPI.hpp:243
uint32_t dst_binding
Definition: RenderAPI.hpp:244
VkDescriptorImageInfo image_info
Definition: RenderAPI.hpp:248
VkDescriptorType descriptor_type
Definition: RenderAPI.hpp:247
uint32_t dst_array_element
Definition: RenderAPI.hpp:245
uint32_t descriptor_count
Definition: RenderAPI.hpp:246
VkDescriptorBufferInfo buffer_info
Definition: RenderAPI.hpp:249
Definition: RenderAPI.hpp:211
void putPixel(uint32_t x, uint32_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Definition: RenderAPI.hpp:232
VkFormat format
Definition: RenderAPI.hpp:221
VkImageView view
Definition: RenderAPI.hpp:214
VkDescriptorSet descriptor_set
Definition: RenderAPI.hpp:219
uint32_t width() const
Definition: RenderAPI.hpp:224
uint32_t height() const
Definition: RenderAPI.hpp:225
VkExtent2D extent
Definition: RenderAPI.hpp:222
void * mapped_memory
Definition: RenderAPI.hpp:217
void clear()
Definition: RenderAPI.hpp:227
VkSampler sampler
Definition: RenderAPI.hpp:215
VkImage image
Definition: RenderAPI.hpp:212
VkDeviceMemory memory
Definition: RenderAPI.hpp:213
Definition: RenderAPI.hpp:278
mat4 matrice
Definition: RenderAPI.hpp:279
VkDeviceAddress water_vertex_buffer
Definition: RenderAPI.hpp:281
VkDeviceAddress block_vertex_buffer
Definition: RenderAPI.hpp:280
Definition: RenderAPI.hpp:123
glm::vec3 normal
Definition: RenderAPI.hpp:125
glm::vec2 tex_coord
Definition: RenderAPI.hpp:126
static VkVertexInputBindingDescription getBindingDescription()
Definition: RenderAPI.hpp:129
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition: RenderAPI.hpp:139
uint32_t texture_index
Definition: RenderAPI.hpp:127
glm::vec3 pos
Definition: RenderAPI.hpp:124
Definition: RenderAPI.hpp:63
glm::vec3 pos
Definition: RenderAPI.hpp:64
static std::vector< VkVertexInputAttributeDescription > getAttributeDescriptions()
Definition: RenderAPI.hpp:76
static VkVertexInputBindingDescription getBindingDescription()
Definition: RenderAPI.hpp:66
Definition: RenderAPI.hpp:260
uint64_t id
Definition: RenderAPI.hpp:261
glm::dmat4 model
Definition: RenderAPI.hpp:262
Definition: RenderAPI.hpp:168
uint32_t vertex_count
Definition: RenderAPI.hpp:171
VkDeviceSize index_offset
Definition: RenderAPI.hpp:173
uint32_t memory_size
Definition: RenderAPI.hpp:176
uint64_t is_used
Definition: RenderAPI.hpp:182
uint32_t vertex_size
Definition: RenderAPI.hpp:172
VkDeviceMemory buffer_memory
Definition: RenderAPI.hpp:170
uint32_t index_count
Definition: RenderAPI.hpp:174
VkDeviceAddress buffer_address
Definition: RenderAPI.hpp:178
uint8_t used_by_frame[8]
Definition: RenderAPI.hpp:183
VkBuffer buffer
Definition: RenderAPI.hpp:169
Definition: RenderAPI.hpp:266
glm::dvec3 position
Definition: RenderAPI.hpp:267
bool visible
Definition: RenderAPI.hpp:274
double yaw
Definition: RenderAPI.hpp:268
PlayerModel::WalkAnimation walk_animation
Definition: RenderAPI.hpp:271
double pitch
Definition: RenderAPI.hpp:269
PlayerModel::AttackAnimation attack_animation
Definition: RenderAPI.hpp:272
Definition: RenderAPI.hpp:51
std::optional< uint32_t > graphics_family
Definition: RenderAPI.hpp:52
std::optional< uint32_t > present_family
Definition: RenderAPI.hpp:53
std::optional< uint32_t > transfer_family
Definition: RenderAPI.hpp:54
bool isComplete()
Definition: RenderAPI.hpp:56
Definition: RenderAPI.hpp:321
std::vector< uint32_t > water_index
Definition: RenderAPI.hpp:326
std::vector< uint32_t > block_index
Definition: RenderAPI.hpp:323
std::vector< BlockVertex > water_vertex
Definition: RenderAPI.hpp:325
std::vector< BlockVertex > block_vertex
Definition: RenderAPI.hpp:322
glm::dmat4 model
Definition: RenderAPI.hpp:328
Definition: ShaderCommon.hpp:49
Definition: RenderAPI.hpp:188
std::vector< VkBuffer > buffers
Definition: RenderAPI.hpp:189
std::vector< VkDeviceMemory > memory
Definition: RenderAPI.hpp:190
std::vector< void * > mapped_memory
Definition: RenderAPI.hpp:191
Definition: ShaderCommon.hpp:43