Graphics.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /**
  2. * Copyright (c) 2006-2023 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #pragma once
  21. // löve
  22. #include "common/config.h"
  23. #include "graphics/Graphics.h"
  24. #include "StreamBuffer.h"
  25. #include "ShaderStage.h"
  26. #include "Shader.h"
  27. #include "Texture.h"
  28. // libraries
  29. #include "VulkanWrapper.h"
  30. #include "libraries/xxHash/xxhash.h"
  31. // c++
  32. #include <iostream>
  33. #include <memory>
  34. #include <functional>
  35. #include <set>
  36. namespace love
  37. {
  38. namespace graphics
  39. {
  40. namespace vulkan
  41. {
  42. struct ColorAttachment
  43. {
  44. VkFormat format = VK_FORMAT_UNDEFINED;
  45. VkAttachmentLoadOp loadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
  46. VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
  47. bool operator==(const ColorAttachment &attachment) const
  48. {
  49. return format == attachment.format &&
  50. loadOp == attachment.loadOp &&
  51. msaaSamples == attachment.msaaSamples;
  52. }
  53. };
  54. struct DepthStencilAttachment
  55. {
  56. VkFormat format = VK_FORMAT_UNDEFINED;
  57. VkAttachmentLoadOp depthLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
  58. VkAttachmentLoadOp stencilLoadOp = VK_ATTACHMENT_LOAD_OP_LOAD;
  59. VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
  60. bool operator==(const DepthStencilAttachment &attachment) const
  61. {
  62. return format == attachment.format &&
  63. depthLoadOp == attachment.depthLoadOp &&
  64. stencilLoadOp == attachment.stencilLoadOp &&
  65. msaaSamples == attachment.msaaSamples;
  66. }
  67. };
  68. struct RenderPassConfiguration
  69. {
  70. std::vector<ColorAttachment> colorAttachments;
  71. struct StaticRenderPassConfiguration
  72. {
  73. DepthStencilAttachment depthStencilAttachment;
  74. bool resolve = false;
  75. } staticData;
  76. bool operator==(const RenderPassConfiguration &conf) const
  77. {
  78. return colorAttachments == conf.colorAttachments &&
  79. (memcmp(&staticData, &conf.staticData, sizeof(StaticRenderPassConfiguration)) == 0);
  80. }
  81. };
  82. struct RenderPassConfigurationHasher
  83. {
  84. size_t operator()(const RenderPassConfiguration &configuration) const
  85. {
  86. size_t hashes[] = {
  87. XXH32(configuration.colorAttachments.data(), configuration.colorAttachments.size() * sizeof(ColorAttachment), 0),
  88. XXH32(&configuration.staticData, sizeof(configuration.staticData), 0),
  89. };
  90. return XXH32(hashes, sizeof(hashes), 0);
  91. }
  92. };
  93. struct FramebufferConfiguration
  94. {
  95. std::vector<VkImageView> colorViews;
  96. struct StaticFramebufferConfiguration
  97. {
  98. VkImageView depthView = VK_NULL_HANDLE;
  99. VkImageView resolveView = VK_NULL_HANDLE;
  100. uint32_t width = 0;
  101. uint32_t height = 0;
  102. VkRenderPass renderPass = VK_NULL_HANDLE;
  103. } staticData;
  104. bool operator==(const FramebufferConfiguration &conf) const
  105. {
  106. return colorViews == conf.colorViews &&
  107. (memcmp(&staticData, &conf.staticData, sizeof(StaticFramebufferConfiguration)) == 0);
  108. }
  109. };
  110. struct FramebufferConfigurationHasher
  111. {
  112. size_t operator()(const FramebufferConfiguration &configuration) const
  113. {
  114. size_t hashes[] = {
  115. XXH32(configuration.colorViews.data(), configuration.colorViews.size() * sizeof(VkImageView), 0),
  116. XXH32(&configuration.staticData, sizeof(configuration.staticData), 0),
  117. };
  118. return XXH32(hashes, sizeof(hashes), 0);
  119. }
  120. };
  121. struct OptionalInstanceExtensions
  122. {
  123. // VK_KHR_get_physical_device_properties2
  124. bool physicalDeviceProperties2 = false;
  125. };
  126. struct OptionalDeviceExtensions
  127. {
  128. // VK_EXT_extended_dynamic_state
  129. bool extendedDynamicState = false;
  130. // VK_KHR_get_memory_requirements2
  131. bool memoryRequirements2 = false;
  132. // VK_KHR_dedicated_allocation
  133. bool dedicatedAllocation = false;
  134. // VK_EXT_memory_budget
  135. bool memoryBudget = false;
  136. // VK_KHR_shader_float_controls
  137. bool shaderFloatControls = false;
  138. // VK_KHR_spirv_1_4
  139. bool spirv14 = false;
  140. };
  141. struct GraphicsPipelineConfiguration
  142. {
  143. VkRenderPass renderPass;
  144. VertexAttributes vertexAttributes;
  145. Shader *shader = nullptr;
  146. bool wireFrame;
  147. BlendState blendState;
  148. ColorChannelMask colorChannelMask;
  149. VkSampleCountFlagBits msaaSamples;
  150. uint32_t numColorAttachments;
  151. PrimitiveType primitiveType;
  152. struct DynamicState
  153. {
  154. CullMode cullmode = CULL_NONE;
  155. Winding winding = WINDING_MAX_ENUM;
  156. StencilAction stencilAction = STENCIL_MAX_ENUM;
  157. CompareMode stencilCompare = COMPARE_MAX_ENUM;
  158. DepthState depthState{};
  159. } dynamicState;
  160. GraphicsPipelineConfiguration()
  161. {
  162. memset(this, 0, sizeof(GraphicsPipelineConfiguration));
  163. }
  164. bool operator==(const GraphicsPipelineConfiguration &other) const
  165. {
  166. return memcmp(this, &other, sizeof(GraphicsPipelineConfiguration)) == 0;
  167. }
  168. };
  169. struct GraphicsPipelineConfigurationHasher
  170. {
  171. size_t operator() (const GraphicsPipelineConfiguration &configuration) const
  172. {
  173. return XXH32(&configuration, sizeof(GraphicsPipelineConfiguration), 0);
  174. }
  175. };
  176. struct QueueFamilyIndices
  177. {
  178. Optional<uint32_t> graphicsFamily;
  179. Optional<uint32_t> presentFamily;
  180. bool isComplete() const
  181. {
  182. return graphicsFamily.hasValue && presentFamily.hasValue;
  183. }
  184. };
  185. struct SwapChainSupportDetails
  186. {
  187. VkSurfaceCapabilitiesKHR capabilities{};
  188. std::vector<VkSurfaceFormatKHR> formats;
  189. std::vector<VkPresentModeKHR> presentModes;
  190. };
  191. struct RenderpassState
  192. {
  193. bool active = false;
  194. VkRenderPassBeginInfo beginInfo{};
  195. bool isWindow = false;
  196. RenderPassConfiguration renderPassConfiguration{};
  197. FramebufferConfiguration framebufferConfiguration{};
  198. VkPipeline pipeline = VK_NULL_HANDLE;
  199. std::vector<VkImage> transitionImages;
  200. uint32_t numColorAttachments = 0;
  201. float width = 0.0f;
  202. float height = 0.0f;
  203. VkSampleCountFlagBits msaa = VK_SAMPLE_COUNT_1_BIT;
  204. std::vector<VkClearValue> clearColors;
  205. bool windowClearRequested = false;
  206. OptionalColorD mainWindowClearColorValue;
  207. OptionalDouble mainWindowClearDepthValue;
  208. OptionalInt mainWindowClearStencilValue;
  209. };
  210. struct ScreenshotReadbackBuffer
  211. {
  212. VkBuffer buffer;
  213. VmaAllocation allocation;
  214. VmaAllocationInfo allocationInfo;
  215. VkImage image;
  216. VmaAllocation imageAllocation;
  217. };
  218. class Graphics final : public love::graphics::Graphics
  219. {
  220. public:
  221. Graphics();
  222. ~Graphics();
  223. const char *getName() const override;
  224. const VkDevice getDevice() const;
  225. const VmaAllocator getVmaAllocator() const;
  226. // implementation for virtual functions
  227. love::graphics::Texture *newTexture(const love::graphics::Texture::Settings &settings, const love::graphics::Texture::Slices *data) override;
  228. love::graphics::Buffer *newBuffer(const love::graphics::Buffer::Settings &settings, const std::vector<love::graphics::Buffer::DataDeclaration>& format, const void *data, size_t size, size_t arraylength) override;
  229. void clear(OptionalColorD color, OptionalInt stencil, OptionalDouble depth) override;
  230. void clear(const std::vector<OptionalColorD> &colors, OptionalInt stencil, OptionalDouble depth) override;
  231. Matrix4 computeDeviceProjection(const Matrix4 &projection, bool rendertotexture) const override;
  232. void discard(const std::vector<bool>& colorbuffers, bool depthstencil) override;
  233. void present(void *screenshotCallbackdata) override;
  234. void setViewportSize(int width, int height, int pixelwidth, int pixelheight) override;
  235. bool setMode(void *context, int width, int height, int pixelwidth, int pixelheight, bool windowhasstencil, int msaa) override;
  236. void unSetMode() override;
  237. void setActive(bool active) override;
  238. int getRequestedBackbufferMSAA() const override;
  239. int getBackbufferMSAA() const override;
  240. void setColor(Colorf c) override;
  241. void setScissor(const Rect &rect) override;
  242. void setScissor() override;
  243. void setStencilMode(StencilAction action, CompareMode compare, int value, love::uint32 readmask, love::uint32 writemask) override;
  244. void setDepthMode(CompareMode compare, bool write) override;
  245. void setFrontFaceWinding(Winding winding) override;
  246. void setColorMask(ColorChannelMask mask) override;
  247. void setBlendState(const BlendState &blend) override;
  248. void setPointSize(float size) override;
  249. void setWireframe(bool enable) override;
  250. PixelFormat getSizedFormat(PixelFormat format, bool rendertarget, bool readable) const override;
  251. bool isPixelFormatSupported(PixelFormat format, uint32 usage, bool sRGB) override;
  252. Renderer getRenderer() const override;
  253. bool usesGLSLES() const override;
  254. RendererInfo getRendererInfo() const override;
  255. void draw(const DrawCommand &cmd) override;
  256. void draw(const DrawIndexedCommand &cmd) override;
  257. void drawQuads(int start, int count, const VertexAttributes &attributes, const BufferBindings &buffers, graphics::Texture *texture) override;
  258. graphics::GraphicsReadback *newReadbackInternal(ReadbackMethod method, love::graphics::Buffer *buffer, size_t offset, size_t size, data::ByteData *dest, size_t destoffset) override;
  259. graphics::GraphicsReadback *newReadbackInternal(ReadbackMethod method, love::graphics::Texture *texture, int slice, int mipmap, const Rect &rect, image::ImageData *dest, int destx, int desty) override;
  260. // internal functions.
  261. VkCommandBuffer getCommandBufferForDataTransfer();
  262. void queueCleanUp(std::function<void()> cleanUp);
  263. void addReadbackCallback(std::function<void()> callback);
  264. void submitGpuCommands(bool present, void *screenshotCallbackData = nullptr);
  265. const VkDeviceSize getMinUniformBufferOffsetAlignment() const;
  266. graphics::Texture *getDefaultTexture() const;
  267. VkSampler getCachedSampler(const SamplerState &sampler);
  268. void setComputeShader(Shader *computeShader);
  269. graphics::Shader::BuiltinUniformData getCurrentBuiltinUniformData();
  270. const OptionalDeviceExtensions &getEnabledOptionalDeviceExtensions() const;
  271. VkSampleCountFlagBits getMsaaCount(int requestedMsaa) const;
  272. void setVsync(int vsync);
  273. int getVsync() const;
  274. protected:
  275. graphics::ShaderStage *newShaderStageInternal(ShaderStageType stage, const std::string &cachekey, const std::string &source, bool gles) override;
  276. graphics::Shader *newShaderInternal(StrongRef<love::graphics::ShaderStage> stages[SHADERSTAGE_MAX_ENUM]) override;
  277. graphics::StreamBuffer *newStreamBuffer(BufferUsage type, size_t size) override;
  278. bool dispatch(love::graphics::Shader *shader, int x, int y, int z) override;
  279. bool dispatch(love::graphics::Shader *shader, love::graphics::Buffer *indirectargs, size_t argsoffset) override;
  280. void initCapabilities() override;
  281. void getAPIStats(int &shaderswitches) const override;
  282. void setRenderTargetsInternal(const RenderTargets &rts, int pixelw, int pixelh, bool hasSRGBtexture) override;
  283. private:
  284. void createVulkanInstance();
  285. bool checkValidationSupport();
  286. void pickPhysicalDevice();
  287. int rateDeviceSuitability(VkPhysicalDevice device);
  288. QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device);
  289. void createLogicalDevice();
  290. void createPipelineCache();
  291. void initVMA();
  292. void createSurface();
  293. bool checkDeviceExtensionSupport(VkPhysicalDevice device);
  294. SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device);
  295. VkSurfaceFormatKHR chooseSwapSurfaceFormat(const std::vector<VkSurfaceFormatKHR> &availableFormats);
  296. VkPresentModeKHR chooseSwapPresentMode(const std::vector<VkPresentModeKHR> &availablePresentModes);
  297. VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR &capabilities);
  298. VkCompositeAlphaFlagBitsKHR chooseCompositeAlpha(const VkSurfaceCapabilitiesKHR &capabilities);
  299. void createSwapChain();
  300. void createImageViews();
  301. void createScreenshotCallbackBuffers();
  302. VkFramebuffer createFramebuffer(FramebufferConfiguration &configuration);
  303. VkFramebuffer getFramebuffer(FramebufferConfiguration &configuration);
  304. void createDefaultShaders();
  305. VkRenderPass createRenderPass(RenderPassConfiguration &configuration);
  306. VkRenderPass getRenderPass(RenderPassConfiguration &configuration);
  307. VkPipeline createGraphicsPipeline(GraphicsPipelineConfiguration &configuration);
  308. void createColorResources();
  309. VkFormat findSupportedFormat(const std::vector<VkFormat> &candidates, VkImageTiling tiling, VkFormatFeatureFlags features);
  310. VkFormat findDepthFormat();
  311. void createDepthResources();
  312. void createCommandPool();
  313. void createCommandBuffers();
  314. void createSyncObjects();
  315. void createDefaultTexture();
  316. void cleanup();
  317. void cleanupSwapChain();
  318. void recreateSwapChain();
  319. void initDynamicState();
  320. void beginFrame();
  321. void startRecordingGraphicsCommands();
  322. void endRecordingGraphicsCommands();
  323. void ensureGraphicsPipelineConfiguration(GraphicsPipelineConfiguration &configuration);
  324. bool usesConstantVertexColor(const VertexAttributes &attribs);
  325. void createVulkanVertexFormat(
  326. VertexAttributes vertexAttributes,
  327. std::vector<VkVertexInputBindingDescription> &bindingDescriptions,
  328. std::vector<VkVertexInputAttributeDescription> &attributeDescriptions);
  329. void prepareDraw(
  330. const VertexAttributes &attributes,
  331. const BufferBindings &buffers, graphics::Texture *texture,
  332. PrimitiveType, CullMode);
  333. void setRenderPass(const RenderTargets &rts, int pixelw, int pixelh, bool hasSRGBtexture);
  334. void setDefaultRenderPass();
  335. void startRenderPass();
  336. void endRenderPass();
  337. VkSampler createSampler(const SamplerState &sampler);
  338. void cleanupUnusedObjects();
  339. void requestSwapchainRecreation();
  340. VkInstance instance = VK_NULL_HANDLE;
  341. VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
  342. uint32_t deviceApiVersion = VK_API_VERSION_1_0;
  343. bool windowHasStencil = false;
  344. int requestedMsaa = 0;
  345. VkDevice device = VK_NULL_HANDLE;
  346. OptionalInstanceExtensions optionalInstanceExtensions;
  347. OptionalDeviceExtensions optionalDeviceExtensions;
  348. VkQueue graphicsQueue = VK_NULL_HANDLE;
  349. VkQueue presentQueue = VK_NULL_HANDLE;
  350. VkSurfaceKHR surface = VK_NULL_HANDLE;
  351. VkSwapchainKHR swapChain = VK_NULL_HANDLE;
  352. VkSurfaceTransformFlagBitsKHR preTransform = {};
  353. Matrix4 displayRotation;
  354. std::vector<VkImage> swapChainImages;
  355. VkFormat swapChainImageFormat = VK_FORMAT_UNDEFINED;
  356. VkFormat depthStencilFormat = VK_FORMAT_UNDEFINED;
  357. VkExtent2D swapChainExtent = VkExtent2D();
  358. std::vector<VkImageView> swapChainImageViews;
  359. VkSampleCountFlagBits msaaSamples = VK_SAMPLE_COUNT_1_BIT;
  360. VkImage colorImage = VK_NULL_HANDLE;
  361. VkImageView colorImageView = VK_NULL_HANDLE;
  362. VmaAllocation colorImageAllocation = VK_NULL_HANDLE;
  363. VkImage depthImage = VK_NULL_HANDLE;
  364. VkImageView depthImageView = VK_NULL_HANDLE;
  365. VmaAllocation depthImageAllocation = VK_NULL_HANDLE;
  366. VkPipelineCache pipelineCache = VK_NULL_HANDLE;
  367. std::unordered_map<RenderPassConfiguration, VkRenderPass, RenderPassConfigurationHasher> renderPasses;
  368. std::unordered_map<FramebufferConfiguration, VkFramebuffer, FramebufferConfigurationHasher> framebuffers;
  369. std::unordered_map<GraphicsPipelineConfiguration, VkPipeline, GraphicsPipelineConfigurationHasher> graphicsPipelines;
  370. std::unordered_map<VkRenderPass, bool> renderPassUsages;
  371. std::unordered_map<VkFramebuffer, bool> framebufferUsages;
  372. std::unordered_map<VkPipeline, bool> pipelineUsages;
  373. std::unordered_map<uint64, VkSampler> samplers;
  374. VkCommandPool commandPool = VK_NULL_HANDLE;
  375. std::vector<VkCommandBuffer> commandBuffers;
  376. Shader *computeShader = nullptr;
  377. std::vector<VkSemaphore> imageAvailableSemaphores;
  378. std::vector<VkSemaphore> renderFinishedSemaphores;
  379. std::vector<VkFence> inFlightFences;
  380. std::vector<VkFence> imagesInFlight;
  381. int vsync = 1;
  382. VkDeviceSize minUniformBufferOffsetAlignment = 0;
  383. bool imageRequested = false;
  384. uint32_t frameCounter = 0;
  385. size_t currentFrame = 0;
  386. uint32_t imageIndex = 0;
  387. bool swapChainRecreationRequested = false;
  388. bool transitionColorDepthLayouts = false;
  389. VmaAllocator vmaAllocator = VK_NULL_HANDLE;
  390. StrongRef<love::graphics::Texture> defaultTexture;
  391. StrongRef<love::graphics::Buffer> defaultConstantColor;
  392. // functions that need to be called to cleanup objects that were needed for rendering a frame.
  393. // We need a vector for each frame in flight.
  394. std::vector<std::vector<std::function<void()>>> cleanUpFunctions;
  395. std::vector<std::vector<std::function<void()>>> readbackCallbacks;
  396. std::vector<ScreenshotReadbackBuffer> screenshotReadbackBuffers;
  397. std::set<Shader*> usedShadersInFrame;
  398. RenderpassState renderPassState;
  399. };
  400. } // vulkan
  401. } // graphics
  402. } // love