GrVkGpu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #ifndef GrVkGpu_DEFINED
  8. #define GrVkGpu_DEFINED
  9. #include "include/gpu/vk/GrVkBackendContext.h"
  10. #include "include/gpu/vk/GrVkTypes.h"
  11. #include "src/gpu/GrGpu.h"
  12. #include "src/gpu/vk/GrVkCaps.h"
  13. #include "src/gpu/vk/GrVkIndexBuffer.h"
  14. #include "src/gpu/vk/GrVkMemory.h"
  15. #include "src/gpu/vk/GrVkResourceProvider.h"
  16. #include "src/gpu/vk/GrVkSemaphore.h"
  17. #include "src/gpu/vk/GrVkUtil.h"
  18. #include "src/gpu/vk/GrVkVertexBuffer.h"
  19. class GrPipeline;
  20. class GrVkBufferImpl;
  21. class GrVkCommandPool;
  22. class GrVkGpuRTCommandBuffer;
  23. class GrVkGpuTextureCommandBuffer;
  24. class GrVkMemoryAllocator;
  25. class GrVkPipeline;
  26. class GrVkPipelineState;
  27. class GrVkPrimaryCommandBuffer;
  28. class GrVkRenderPass;
  29. class GrVkSecondaryCommandBuffer;
  30. class GrVkTexture;
  31. struct GrVkInterface;
  32. namespace SkSL {
  33. class Compiler;
  34. }
  35. class GrVkGpu : public GrGpu {
  36. public:
  37. static sk_sp<GrGpu> Make(const GrVkBackendContext&, const GrContextOptions&, GrContext*);
  38. ~GrVkGpu() override;
  39. void disconnect(DisconnectType) override;
  40. const GrVkInterface* vkInterface() const { return fInterface.get(); }
  41. const GrVkCaps& vkCaps() const { return *fVkCaps; }
  42. GrVkMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); }
  43. VkPhysicalDevice physicalDevice() const { return fPhysicalDevice; }
  44. VkDevice device() const { return fDevice; }
  45. VkQueue queue() const { return fQueue; }
  46. uint32_t queueIndex() const { return fQueueIndex; }
  47. GrVkCommandPool* cmdPool() const { return fCmdPool; }
  48. const VkPhysicalDeviceProperties& physicalDeviceProperties() const {
  49. return fPhysDevProps;
  50. }
  51. const VkPhysicalDeviceMemoryProperties& physicalDeviceMemoryProperties() const {
  52. return fPhysDevMemProps;
  53. }
  54. bool protectedContext() const { return fProtectedContext == GrProtected::kYes; }
  55. GrVkResourceProvider& resourceProvider() { return fResourceProvider; }
  56. GrVkPrimaryCommandBuffer* currentCommandBuffer() { return fCurrentCmdBuffer; }
  57. enum SyncQueue {
  58. kForce_SyncQueue,
  59. kSkip_SyncQueue
  60. };
  61. void querySampleLocations(GrRenderTarget*, SkTArray<SkPoint>*) override {
  62. SkASSERT(!this->caps()->sampleLocationsSupport());
  63. SK_ABORT("Sample locations not yet implemented for Vulkan.");
  64. }
  65. void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
  66. GrBackendTexture createBackendTexture(int w, int h, const GrBackendFormat&,
  67. GrMipMapped, GrRenderable,
  68. const void* pixels, size_t rowBytes,
  69. const SkColor4f* color,
  70. GrProtected isProtected) override;
  71. void deleteBackendTexture(const GrBackendTexture&) override;
  72. #if GR_TEST_UTILS
  73. bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
  74. GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override;
  75. void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
  76. void testingOnly_flushGpuAndSync() override;
  77. void resetShaderCacheForTesting() const override {
  78. fResourceProvider.resetShaderCacheForTesting();
  79. }
  80. #endif
  81. GrStencilAttachment* createStencilAttachmentForRenderTarget(
  82. const GrRenderTarget*, int width, int height, int numStencilSamples) override;
  83. GrGpuRTCommandBuffer* getCommandBuffer(
  84. GrRenderTarget*, GrSurfaceOrigin, const SkRect&,
  85. const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
  86. const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
  87. GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
  88. void addBufferMemoryBarrier(const GrVkResource*,
  89. VkPipelineStageFlags srcStageMask,
  90. VkPipelineStageFlags dstStageMask,
  91. bool byRegion,
  92. VkBufferMemoryBarrier* barrier) const;
  93. void addImageMemoryBarrier(const GrVkResource*,
  94. VkPipelineStageFlags srcStageMask,
  95. VkPipelineStageFlags dstStageMask,
  96. bool byRegion,
  97. VkImageMemoryBarrier* barrier) const;
  98. SkSL::Compiler* shaderCompiler() const {
  99. return fCompiler;
  100. }
  101. bool onRegenerateMipMapLevels(GrTexture* tex) override;
  102. void resolveRenderTargetNoFlush(GrRenderTarget* target) {
  103. this->internalResolveRenderTarget(target, false);
  104. }
  105. void onResolveRenderTarget(GrRenderTarget* target) override {
  106. // This resolve is called when we are preparing an msaa surface for external I/O. It is
  107. // called after flushing, so we need to make sure we submit the command buffer after doing
  108. // the resolve so that the resolve actually happens.
  109. this->internalResolveRenderTarget(target, true);
  110. }
  111. void submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>&,
  112. const GrVkRenderPass*,
  113. const VkClearValue* colorClear,
  114. GrVkRenderTarget*, GrSurfaceOrigin,
  115. const SkIRect& bounds);
  116. void submit(GrGpuCommandBuffer*) override;
  117. GrFence SK_WARN_UNUSED_RESULT insertFence() override;
  118. bool waitFence(GrFence, uint64_t timeout) override;
  119. void deleteFence(GrFence) const override;
  120. sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
  121. sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
  122. GrResourceProvider::SemaphoreWrapType wrapType,
  123. GrWrapOwnership ownership) override;
  124. void insertSemaphore(sk_sp<GrSemaphore> semaphore) override;
  125. void waitSemaphore(sk_sp<GrSemaphore> semaphore) override;
  126. // These match the definitions in SkDrawable, from whence they came
  127. typedef void* SubmitContext;
  128. typedef void (*SubmitProc)(SubmitContext submitContext);
  129. // Adds an SkDrawable::GpuDrawHandler that we will delete the next time we submit the primary
  130. // command buffer to the gpu.
  131. void addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable);
  132. void checkFinishProcs() override { fResourceProvider.checkCommandBuffers(); }
  133. sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
  134. void copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
  135. VkDeviceSize dstOffset, VkDeviceSize size);
  136. bool updateBuffer(GrVkBuffer* buffer, const void* src, VkDeviceSize offset, VkDeviceSize size);
  137. uint32_t getExtraSamplerKeyForProgram(const GrSamplerState&,
  138. const GrBackendFormat& format) override;
  139. enum PersistentCacheKeyType : uint32_t {
  140. kShader_PersistentCacheKeyType = 0,
  141. kPipelineCache_PersistentCacheKeyType = 1,
  142. };
  143. void storeVkPipelineCacheData() override;
  144. private:
  145. GrVkGpu(GrContext*, const GrContextOptions&, const GrVkBackendContext&,
  146. sk_sp<const GrVkInterface>, uint32_t instanceVersion, uint32_t physicalDeviceVersion);
  147. void onResetContext(uint32_t resetBits) override {}
  148. void destroyResources();
  149. sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, GrRenderable, int renderTargetSampleCnt,
  150. SkBudgeted, GrProtected, const GrMipLevel[],
  151. int mipLevelCount) override;
  152. sk_sp<GrTexture> onCreateCompressedTexture(int width, int height, SkImage::CompressionType,
  153. SkBudgeted, const void* data) override;
  154. sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrColorType, GrWrapOwnership,
  155. GrWrapCacheable, GrIOType) override;
  156. sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
  157. int sampleCnt,
  158. GrColorType colorType,
  159. GrWrapOwnership,
  160. GrWrapCacheable) override;
  161. sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&,
  162. GrColorType) override;
  163. sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
  164. int sampleCnt, GrColorType) override;
  165. sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
  166. const GrVkDrawableInfo&) override;
  167. sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType type, GrAccessPattern,
  168. const void* data) override;
  169. bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
  170. void* buffer, size_t rowBytes) override;
  171. bool onWritePixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
  172. const GrMipLevel texels[], int mipLevelCount) override;
  173. bool onTransferPixelsTo(GrTexture*, int left, int top, int width, int height, GrColorType,
  174. GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) override;
  175. bool onTransferPixelsFrom(GrSurface* surface, int left, int top, int width, int height,
  176. GrColorType, GrGpuBuffer* transferBuffer, size_t offset) override;
  177. bool onCopySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  178. const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) override;
  179. void onFinishFlush(GrSurfaceProxy*[], int, SkSurface::BackendSurfaceAccess access,
  180. const GrFlushInfo&, const GrPrepareForExternalIORequests&) override;
  181. // Ends and submits the current command buffer to the queue and then creates a new command
  182. // buffer and begins it. If sync is set to kForce_SyncQueue, the function will wait for all
  183. // work in the queue to finish before returning. If this GrVkGpu object has any semaphores in
  184. // fSemaphoreToSignal, we will add those signal semaphores to the submission of this command
  185. // buffer. If this GrVkGpu object has any semaphores in fSemaphoresToWaitOn, we will add those
  186. // wait semaphores to the submission of this command buffer.
  187. void submitCommandBuffer(SyncQueue sync, GrGpuFinishedProc finishedProc = nullptr,
  188. GrGpuFinishedContext finishedContext = nullptr);
  189. void internalResolveRenderTarget(GrRenderTarget*, bool requiresSubmit);
  190. void copySurfaceAsCopyImage(GrSurface* dst, GrSurface* src, GrVkImage* dstImage,
  191. GrVkImage* srcImage, const SkIRect& srcRect,
  192. const SkIPoint& dstPoint);
  193. void copySurfaceAsBlit(GrSurface* dst, GrSurface* src, GrVkImage* dstImage, GrVkImage* srcImage,
  194. const SkIRect& srcRect, const SkIPoint& dstPoint);
  195. void copySurfaceAsResolve(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
  196. const SkIPoint& dstPoint);
  197. // helpers for onCreateTexture and writeTexturePixels
  198. bool uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
  199. GrColorType colorType, const void* data, size_t rowBytes);
  200. bool uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
  201. GrColorType colorType, const GrMipLevel texels[], int mipLevelCount);
  202. bool uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
  203. SkImage::CompressionType, const void* data);
  204. void resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
  205. const SkIPoint& dstPoint);
  206. bool createVkImageForBackendSurface(VkFormat vkFormat, int w, int h, bool texturable,
  207. bool renderable, GrMipMapped mipMapped, const void* srcData,
  208. size_t srcRowBytes, const SkColor4f* color,
  209. GrVkImageInfo* info, GrProtected isProtected);
  210. sk_sp<const GrVkInterface> fInterface;
  211. sk_sp<GrVkMemoryAllocator> fMemoryAllocator;
  212. sk_sp<GrVkCaps> fVkCaps;
  213. VkInstance fInstance;
  214. VkPhysicalDevice fPhysicalDevice;
  215. VkDevice fDevice;
  216. VkQueue fQueue; // Must be Graphics queue
  217. uint32_t fQueueIndex;
  218. // Created by GrVkGpu
  219. GrVkResourceProvider fResourceProvider;
  220. GrVkCommandPool* fCmdPool;
  221. // just a raw pointer; object's lifespan is managed by fCmdPool
  222. GrVkPrimaryCommandBuffer* fCurrentCmdBuffer;
  223. SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToWaitOn;
  224. SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToSignal;
  225. SkTArray<std::unique_ptr<SkDrawable::GpuDrawHandler>> fDrawables;
  226. VkPhysicalDeviceProperties fPhysDevProps;
  227. VkPhysicalDeviceMemoryProperties fPhysDevMemProps;
  228. // compiler used for compiling sksl into spirv. We only want to create the compiler once since
  229. // there is significant overhead to the first compile of any compiler.
  230. SkSL::Compiler* fCompiler;
  231. // We need a bool to track whether or not we've already disconnected all the gpu resources from
  232. // vulkan context.
  233. bool fDisconnected;
  234. GrProtected fProtectedContext;
  235. std::unique_ptr<GrVkGpuRTCommandBuffer> fCachedRTCommandBuffer;
  236. std::unique_ptr<GrVkGpuTextureCommandBuffer> fCachedTexCommandBuffer;
  237. typedef GrGpu INHERITED;
  238. };
  239. #endif