blob: b956afccd41711f2445a3e52fb804acbe6e94cf8 [file] [log] [blame]
Greg Daniel164a9f02016-02-22 09:56:40 -05001/*
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
8#ifndef GrVkGpu_DEFINED
9#define GrVkGpu_DEFINED
10
11#include "GrGpu.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050012#include "GrVkCaps.h"
egdanielbc9b2962016-09-27 08:00:53 -070013#include "GrVkCopyManager.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkIndexBuffer.h"
jvanverth6b6ffc42016-06-13 14:28:07 -070015#include "GrVkMemory.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050017#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050018#include "GrVkVertexBuffer.h"
19#include "GrVkUtil.h"
Greg Daniel487132b2018-12-20 14:09:36 -050020#include "vk/GrVkBackendContext.h"
21#include "vk/GrVkTypes.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022
23class GrPipeline;
Greg Daniel164a9f02016-02-22 09:56:40 -050024
25class GrVkBufferImpl;
Ethan Nicholas8e265a72018-12-12 16:22:40 -050026class GrVkCommandPool;
Robert Phillips5b5d84c2018-08-09 15:12:18 -040027class GrVkGpuRTCommandBuffer;
28class GrVkGpuTextureCommandBuffer;
Greg Daniel81df0412018-05-31 13:13:33 -040029class GrVkMemoryAllocator;
Greg Daniel164a9f02016-02-22 09:56:40 -050030class GrVkPipeline;
egdaniel22281c12016-03-23 13:49:40 -070031class GrVkPipelineState;
egdaniel066df7c2016-06-08 14:02:27 -070032class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050033class GrVkRenderPass;
egdaniel066df7c2016-06-08 14:02:27 -070034class GrVkSecondaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050035class GrVkTexture;
36struct GrVkInterface;
37
Ethan Nicholas941e7e22016-12-12 15:33:30 -050038namespace SkSL {
39 class Compiler;
40}
41
Greg Daniel164a9f02016-02-22 09:56:40 -050042class GrVkGpu : public GrGpu {
43public:
Greg Danielf730c182018-07-02 20:15:37 +000044 static sk_sp<GrGpu> Make(const GrVkBackendContext&, const GrContextOptions&, GrContext*);
jvanverth633b3562016-03-23 11:01:22 -070045
Greg Daniel164a9f02016-02-22 09:56:40 -050046 ~GrVkGpu() override;
47
Greg Daniel8606cf82017-05-08 16:17:53 -040048 void disconnect(DisconnectType) override;
49
Greg Danielf730c182018-07-02 20:15:37 +000050 const GrVkInterface* vkInterface() const { return fInterface.get(); }
Greg Daniel164a9f02016-02-22 09:56:40 -050051 const GrVkCaps& vkCaps() const { return *fVkCaps; }
52
Greg Daniel81df0412018-05-31 13:13:33 -040053 GrVkMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); }
54
Greg Daniel637c06a2018-09-12 09:44:25 -040055 VkPhysicalDevice physicalDevice() const { return fPhysicalDevice; }
Greg Daniel164a9f02016-02-22 09:56:40 -050056 VkDevice device() const { return fDevice; }
57 VkQueue queue() const { return fQueue; }
Greg Danielecddbc02018-08-30 16:39:34 -040058 uint32_t queueIndex() const { return fQueueIndex; }
Ethan Nicholas8e265a72018-12-12 16:22:40 -050059 GrVkCommandPool* cmdPool() const { return fCmdPool; }
Greg Daniela870b462019-01-08 15:49:46 -050060 const VkPhysicalDeviceProperties& physicalDeviceProperties() const {
Greg Daniel8385a8a2018-02-26 13:29:37 -050061 return fPhysDevProps;
62 }
Greg Daniela870b462019-01-08 15:49:46 -050063 const VkPhysicalDeviceMemoryProperties& physicalDeviceMemoryProperties() const {
Greg Daniel164a9f02016-02-22 09:56:40 -050064 return fPhysDevMemProps;
65 }
66
egdanielbc9b2962016-09-27 08:00:53 -070067 GrVkResourceProvider& resourceProvider() { return fResourceProvider; }
68
69 GrVkPrimaryCommandBuffer* currentCommandBuffer() { return fCurrentCmdBuffer; }
Greg Daniel164a9f02016-02-22 09:56:40 -050070
71 enum SyncQueue {
72 kForce_SyncQueue,
73 kSkip_SyncQueue
74 };
75
Greg Daniel164a9f02016-02-22 09:56:40 -050076 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
77
Brian Salomonf865b052018-03-09 09:01:53 -050078#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -040079 GrBackendTexture createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -040080 GrColorType colorType, bool isRenderTarget,
81 GrMipMapped, size_t rowBytes = 0) override;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050082 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
Brian Salomon26102cb2018-03-09 09:33:19 -050083 void deleteTestingOnlyBackendTexture(const GrBackendTexture&) override;
Greg Daniel164a9f02016-02-22 09:56:40 -050084
Brian Osman2d010b62018-08-09 10:55:09 -040085 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override;
Brian Salomonf865b052018-03-09 09:01:53 -050086 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
87
Greg Daniel26b50a42018-03-08 09:49:58 -050088 void testingOnly_flushGpuAndSync() override;
Brian Salomonf865b052018-03-09 09:01:53 -050089#endif
Greg Daniel26b50a42018-03-08 09:49:58 -050090
Greg Daniel164a9f02016-02-22 09:56:40 -050091 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
92 int width,
93 int height) override;
94
Robert Phillips5b5d84c2018-08-09 15:12:18 -040095 GrGpuRTCommandBuffer* getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -040096 GrRenderTarget*, GrSurfaceOrigin, const SkRect&,
Greg Daniel500d58b2017-08-24 15:59:33 -040097 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
98 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
99
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400100 GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
101
egdaniel066df7c2016-06-08 14:02:27 -0700102
Greg Daniel164a9f02016-02-22 09:56:40 -0500103 void addMemoryBarrier(VkPipelineStageFlags srcStageMask,
104 VkPipelineStageFlags dstStageMask,
105 bool byRegion,
106 VkMemoryBarrier* barrier) const;
107 void addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
108 VkPipelineStageFlags dstStageMask,
109 bool byRegion,
110 VkBufferMemoryBarrier* barrier) const;
111 void addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
112 VkPipelineStageFlags dstStageMask,
113 bool byRegion,
114 VkImageMemoryBarrier* barrier) const;
halcanary9d524f22016-03-29 09:03:52 -0700115
ethannicholasb3058bd2016-07-01 08:22:01 -0700116 SkSL::Compiler* shaderCompiler() const {
117 return fCompiler;
118 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500119
Brian Salomon930f9392018-06-20 16:25:26 -0400120 bool onRegenerateMipMapLevels(GrTexture* tex) override;
121
Greg Daniel0a77f432018-12-06 11:23:32 -0500122 void resolveRenderTargetNoFlush(GrRenderTarget* target) {
123 this->internalResolveRenderTarget(target, false);
124 }
125
Brian Salomon1fabd512018-02-09 09:54:25 -0500126 void onResolveRenderTarget(GrRenderTarget* target) override {
Greg Daniel0a77f432018-12-06 11:23:32 -0500127 // This resolve is called when we are preparing an msaa surface for external I/O. It is
128 // called after flushing, so we need to make sure we submit the command buffer after doing
129 // the resolve so that the resolve actually happens.
Brian Salomon1fabd512018-02-09 09:54:25 -0500130 this->internalResolveRenderTarget(target, true);
Greg Daniel69d49922017-02-23 09:44:02 -0500131 }
egdaniel66933552016-08-24 07:22:19 -0700132
Greg Daniel22bc8652017-03-22 15:45:43 -0400133 void submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>&,
egdaniel9cb63402016-06-23 08:37:05 -0700134 const GrVkRenderPass*,
Robert Phillips8c326e92017-08-10 13:50:17 -0400135 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400136 GrVkRenderTarget*, GrSurfaceOrigin,
egdaniel9cb63402016-06-23 08:37:05 -0700137 const SkIRect& bounds);
egdaniel066df7c2016-06-08 14:02:27 -0700138
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400139 void submit(GrGpuCommandBuffer*) override;
140
Greg Daniel6be35232017-03-01 17:01:09 -0500141 GrFence SK_WARN_UNUSED_RESULT insertFence() override;
142 bool waitFence(GrFence, uint64_t timeout) override;
jvanverth84741b32016-09-30 08:39:02 -0700143 void deleteFence(GrFence) const override;
144
Greg Daniela5cb7812017-06-16 09:45:32 -0400145 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
Greg Daniel48661b82018-01-22 16:11:35 -0500146 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
147 GrResourceProvider::SemaphoreWrapType wrapType,
148 GrWrapOwnership ownership) override;
Greg Daniel858e12c2018-12-06 11:11:37 -0500149 void insertSemaphore(sk_sp<GrSemaphore> semaphore) override;
Greg Daniel48661b82018-01-22 16:11:35 -0500150 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override;
151
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400152 // These match the definitions in SkDrawable, from whence they came
153 typedef void* SubmitContext;
154 typedef void (*SubmitProc)(SubmitContext submitContext);
155
156 // Adds an SkDrawable::GpuDrawHandler that we will delete the next time we submit the primary
157 // command buffer to the gpu.
158 void addDrawable(std::unique_ptr<SkDrawable::GpuDrawHandler> drawable);
159
Brian Osman13dddce2017-05-09 13:19:50 -0400160 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
161
Greg Daniel6888c0d2017-08-25 11:55:50 -0400162 void copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
163 VkDeviceSize dstOffset, VkDeviceSize size);
jvanverthdb379092016-07-07 11:18:46 -0700164 bool updateBuffer(GrVkBuffer* buffer, const void* src, VkDeviceSize offset, VkDeviceSize size);
jvanvertha584de92016-06-30 09:10:52 -0700165
Greg Daniel7a82edf2018-12-04 10:54:34 -0500166 uint32_t getExtraSamplerKeyForProgram(const GrSamplerState&,
167 const GrBackendFormat& format) override;
168
Greg Daniela870b462019-01-08 15:49:46 -0500169 enum PersistentCacheKeyType : uint32_t {
170 kShader_PersistentCacheKeyType = 0,
171 kPipelineCache_PersistentCacheKeyType = 1,
172 };
173
174 void storeVkPipelineCacheData() override;
175
Greg Daniel164a9f02016-02-22 09:56:40 -0500176private:
Greg Daniel98bffae2018-08-01 13:25:41 -0400177 GrVkGpu(GrContext*, const GrContextOptions&, const GrVkBackendContext&,
Greg Daniel41f0e282019-01-28 13:15:05 -0500178 sk_sp<const GrVkInterface>, uint32_t instanceVersion, uint32_t physicalDeviceVersion);
jvanverth633b3562016-03-23 11:01:22 -0700179
egdanielcfcd1812016-03-22 07:16:10 -0700180 void onResetContext(uint32_t resetBits) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500181
Greg Daniel8606cf82017-05-08 16:17:53 -0400182 void destroyResources();
183
Brian Salomon58389b92018-03-07 13:01:25 -0500184 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel[],
Brian Salomon2a4f9832018-03-03 22:43:43 -0500185 int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500186
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500187 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership, GrWrapCacheable,
188 GrIOType) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400189 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400190 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500191 GrWrapOwnership,
192 GrWrapCacheable) override;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400193 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000194
195 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000196 int sampleCnt) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500197
Greg Danielb46add82019-01-02 14:51:29 -0500198 sk_sp<GrRenderTarget> onWrapVulkanSecondaryCBAsRenderTarget(const SkImageInfo&,
199 const GrVkDrawableInfo&) override;
200
Brian Salomondbf70722019-02-07 11:31:24 -0500201 sk_sp<GrGpuBuffer> onCreateBuffer(size_t size, GrGpuBufferType type, GrAccessPattern,
202 const void* data) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500203
Brian Salomona6948702018-06-01 15:33:20 -0400204 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
205 void* buffer, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500206
Brian Salomona9b04b92018-06-01 15:04:28 -0400207 bool onWritePixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
208 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500209
Brian Salomonc320b152018-02-20 14:05:36 -0500210 bool onTransferPixels(GrTexture*, int left, int top, int width, int height, GrColorType,
Brian Salomondbf70722019-02-07 11:31:24 -0500211 GrGpuBuffer* transferBuffer, size_t offset, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500212
Brian Salomon9b009bb2018-02-14 13:53:55 -0500213 bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
214 GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Greg Daniel55fa6472018-03-16 16:13:10 -0400215 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) override;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500216
Greg Daniel51316782017-08-02 15:10:09 +0000217 void onFinishFlush(bool insertedSemaphores) override;
218
Greg Daniel164a9f02016-02-22 09:56:40 -0500219 // Ends and submits the current command buffer to the queue and then creates a new command
halcanary9d524f22016-03-29 09:03:52 -0700220 // buffer and begins it. If sync is set to kForce_SyncQueue, the function will wait for all
Greg Daniela5cb7812017-06-16 09:45:32 -0400221 // work in the queue to finish before returning. If this GrVkGpu object has any semaphores in
222 // fSemaphoreToSignal, we will add those signal semaphores to the submission of this command
223 // buffer. If this GrVkGpu object has any semaphores in fSemaphoresToWaitOn, we will add those
224 // wait semaphores to the submission of this command buffer.
225 void submitCommandBuffer(SyncQueue sync);
Greg Daniel164a9f02016-02-22 09:56:40 -0500226
Brian Salomon1fabd512018-02-09 09:54:25 -0500227 void internalResolveRenderTarget(GrRenderTarget*, bool requiresSubmit);
Greg Daniel69d49922017-02-23 09:44:02 -0500228
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400229 void copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
230 GrSurface* src, GrSurfaceOrigin srcOrigin,
231 GrVkImage* dstImage, GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -0500232 const SkIRect& srcRect,
233 const SkIPoint& dstPoint);
234
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400235 void copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
236 GrSurface* src, GrSurfaceOrigin srcOrigin,
237 GrVkImage* dstImage, GrVkImage* srcImage,
egdaniel17b89252016-04-05 07:23:38 -0700238 const SkIRect& srcRect,
239 const SkIPoint& dstPoint);
240
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400241 void copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin,
242 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel4bcd62e2016-08-31 07:37:31 -0700243 const SkIRect& srcRect,
244 const SkIPoint& dstPoint);
245
jvanverth900bd4a2016-04-29 13:53:12 -0700246 // helpers for onCreateTexture and writeTexturePixels
Brian Salomona9b04b92018-06-01 15:04:28 -0400247 bool uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
248 GrColorType colorType, const void* data, size_t rowBytes);
249 bool uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
250 GrColorType colorType, const GrMipLevel texels[], int mipLevelCount);
Jim Van Verth1676cb92019-01-15 13:24:45 -0500251 bool uploadTexDataCompressed(GrVkTexture* tex, int left, int top, int width, int height,
252 GrColorType dataColorType, const GrMipLevel texels[],
253 int mipLevelCount);
Brian Salomon1fabd512018-02-09 09:54:25 -0500254 void resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
255 const SkIPoint& dstPoint);
egdaniel4bcd62e2016-08-31 07:37:31 -0700256
Brian Salomon52e943a2018-03-13 09:32:39 -0400257#if GR_TEST_UTILS
258 bool createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
259 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -0400260 size_t srcRowBytes, GrVkImageInfo* info);
Brian Salomon52e943a2018-03-13 09:32:39 -0400261#endif
262
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400263 sk_sp<const GrVkInterface> fInterface;
264 sk_sp<GrVkMemoryAllocator> fMemoryAllocator;
265 sk_sp<GrVkCaps> fVkCaps;
jvanverth633b3562016-03-23 11:01:22 -0700266
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400267 VkInstance fInstance;
268 VkPhysicalDevice fPhysicalDevice;
269 VkDevice fDevice;
270 VkQueue fQueue; // Must be Graphics queue
271 uint32_t fQueueIndex;
jvanverth633b3562016-03-23 11:01:22 -0700272
273 // Created by GrVkGpu
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400274 GrVkResourceProvider fResourceProvider;
Greg Daniel164a9f02016-02-22 09:56:40 -0500275
Ethan Nicholas8e265a72018-12-12 16:22:40 -0500276 GrVkCommandPool* fCmdPool;
277
278 // just a raw pointer; object's lifespan is managed by fCmdPool
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400279 GrVkPrimaryCommandBuffer* fCurrentCmdBuffer;
jvanverth6b6ffc42016-06-13 14:28:07 -0700280
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400281 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToWaitOn;
282 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToSignal;
Greg Daniel6be35232017-03-01 17:01:09 -0500283
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400284 SkTArray<std::unique_ptr<SkDrawable::GpuDrawHandler>> fDrawables;
Greg Daniel6be35232017-03-01 17:01:09 -0500285
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400286 VkPhysicalDeviceProperties fPhysDevProps;
287 VkPhysicalDeviceMemoryProperties fPhysDevMemProps;
288
289 GrVkCopyManager fCopyManager;
egdanielbc9b2962016-09-27 08:00:53 -0700290
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500291 // compiler used for compiling sksl into spirv. We only want to create the compiler once since
292 // there is significant overhead to the first compile of any compiler.
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400293 SkSL::Compiler* fCompiler;
Greg Daniel164a9f02016-02-22 09:56:40 -0500294
Greg Daniel8606cf82017-05-08 16:17:53 -0400295 // We need a bool to track whether or not we've already disconnected all the gpu resources from
296 // vulkan context.
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400297 bool fDisconnected;
Greg Daniel8606cf82017-05-08 16:17:53 -0400298
Greg Daniel64cc9aa2018-10-19 13:54:56 -0400299 std::unique_ptr<GrVkGpuRTCommandBuffer> fCachedRTCommandBuffer;
300 std::unique_ptr<GrVkGpuTextureCommandBuffer> fCachedTexCommandBuffer;
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400301
Greg Daniel164a9f02016-02-22 09:56:40 -0500302 typedef GrGpu INHERITED;
303};
304
305#endif