blob: 3b9a2e5ea4310ec5147193559450af23bec3725f [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"
jvanverth633b3562016-03-23 11:01:22 -070012#include "vk/GrVkBackendContext.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050013#include "GrVkCaps.h"
egdanielbc9b2962016-09-27 08:00:53 -070014#include "GrVkCopyManager.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050015#include "GrVkIndexBuffer.h"
jvanverth6b6ffc42016-06-13 14:28:07 -070016#include "GrVkMemory.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050017#include "GrVkResourceProvider.h"
Greg Daniel6be35232017-03-01 17:01:09 -050018#include "GrVkSemaphore.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050019#include "GrVkVertexBuffer.h"
20#include "GrVkUtil.h"
jvanverthe50f3e72016-03-28 07:03:06 -070021#include "vk/GrVkDefines.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050022
23class GrPipeline;
Greg Daniel164a9f02016-02-22 09:56:40 -050024
25class GrVkBufferImpl;
Robert Phillips5b5d84c2018-08-09 15:12:18 -040026class GrVkGpuRTCommandBuffer;
27class GrVkGpuTextureCommandBuffer;
Greg Daniel81df0412018-05-31 13:13:33 -040028class GrVkMemoryAllocator;
Greg Daniel164a9f02016-02-22 09:56:40 -050029class GrVkPipeline;
egdaniel22281c12016-03-23 13:49:40 -070030class GrVkPipelineState;
egdaniel066df7c2016-06-08 14:02:27 -070031class GrVkPrimaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050032class GrVkRenderPass;
egdaniel066df7c2016-06-08 14:02:27 -070033class GrVkSecondaryCommandBuffer;
Greg Daniel164a9f02016-02-22 09:56:40 -050034class GrVkTexture;
35struct GrVkInterface;
36
Ethan Nicholas941e7e22016-12-12 15:33:30 -050037namespace SkSL {
38 class Compiler;
39}
40
Greg Daniel164a9f02016-02-22 09:56:40 -050041class GrVkGpu : public GrGpu {
42public:
Greg Danielf730c182018-07-02 20:15:37 +000043 static sk_sp<GrGpu> Make(const GrVkBackendContext&, const GrContextOptions&, GrContext*);
jvanverth633b3562016-03-23 11:01:22 -070044
Greg Daniel164a9f02016-02-22 09:56:40 -050045 ~GrVkGpu() override;
46
Greg Daniel8606cf82017-05-08 16:17:53 -040047 void disconnect(DisconnectType) override;
48
Greg Danielf730c182018-07-02 20:15:37 +000049 const GrVkInterface* vkInterface() const { return fInterface.get(); }
Greg Daniel164a9f02016-02-22 09:56:40 -050050 const GrVkCaps& vkCaps() const { return *fVkCaps; }
51
Greg Daniel81df0412018-05-31 13:13:33 -040052 GrVkMemoryAllocator* memoryAllocator() const { return fMemoryAllocator.get(); }
53
Greg Daniel637c06a2018-09-12 09:44:25 -040054 VkPhysicalDevice physicalDevice() const { return fPhysicalDevice; }
Greg Daniel164a9f02016-02-22 09:56:40 -050055 VkDevice device() const { return fDevice; }
56 VkQueue queue() const { return fQueue; }
Greg Danielecddbc02018-08-30 16:39:34 -040057 uint32_t queueIndex() const { return fQueueIndex; }
Greg Daniel164a9f02016-02-22 09:56:40 -050058 VkCommandPool cmdPool() const { return fCmdPool; }
Greg Daniel8385a8a2018-02-26 13:29:37 -050059 VkPhysicalDeviceProperties physicalDeviceProperties() const {
60 return fPhysDevProps;
61 }
Greg Daniel164a9f02016-02-22 09:56:40 -050062 VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties() const {
63 return fPhysDevMemProps;
64 }
65
egdanielbc9b2962016-09-27 08:00:53 -070066 GrVkResourceProvider& resourceProvider() { return fResourceProvider; }
67
68 GrVkPrimaryCommandBuffer* currentCommandBuffer() { return fCurrentCmdBuffer; }
Greg Daniel164a9f02016-02-22 09:56:40 -050069
70 enum SyncQueue {
71 kForce_SyncQueue,
72 kSkip_SyncQueue
73 };
74
Greg Daniel164a9f02016-02-22 09:56:40 -050075 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
76
Brian Salomonf865b052018-03-09 09:01:53 -050077#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -040078 GrBackendTexture createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillips646f6372018-09-25 09:31:10 -040079 GrColorType colorType, bool isRenderTarget,
80 GrMipMapped, size_t rowBytes = 0) override;
Robert Phillipsd21b2a52017-12-12 13:01:25 -050081 bool isTestingOnlyBackendTexture(const GrBackendTexture&) const override;
Brian Salomon26102cb2018-03-09 09:33:19 -050082 void deleteTestingOnlyBackendTexture(const GrBackendTexture&) override;
Greg Daniel164a9f02016-02-22 09:56:40 -050083
Brian Osman2d010b62018-08-09 10:55:09 -040084 GrBackendRenderTarget createTestingOnlyBackendRenderTarget(int w, int h, GrColorType) override;
Brian Salomonf865b052018-03-09 09:01:53 -050085 void deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) override;
86
Greg Daniel26b50a42018-03-08 09:49:58 -050087 void testingOnly_flushGpuAndSync() override;
Brian Salomonf865b052018-03-09 09:01:53 -050088#endif
Greg Daniel26b50a42018-03-08 09:49:58 -050089
Greg Daniel164a9f02016-02-22 09:56:40 -050090 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
91 int width,
92 int height) override;
93
Robert Phillips5b5d84c2018-08-09 15:12:18 -040094 GrGpuRTCommandBuffer* getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -040095 GrRenderTarget*, GrSurfaceOrigin, const SkRect&,
Greg Daniel500d58b2017-08-24 15:59:33 -040096 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
97 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
98
Robert Phillips5b5d84c2018-08-09 15:12:18 -040099 GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
100
egdaniel066df7c2016-06-08 14:02:27 -0700101
Greg Daniel164a9f02016-02-22 09:56:40 -0500102 void addMemoryBarrier(VkPipelineStageFlags srcStageMask,
103 VkPipelineStageFlags dstStageMask,
104 bool byRegion,
105 VkMemoryBarrier* barrier) const;
106 void addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
107 VkPipelineStageFlags dstStageMask,
108 bool byRegion,
109 VkBufferMemoryBarrier* barrier) const;
110 void addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
111 VkPipelineStageFlags dstStageMask,
112 bool byRegion,
113 VkImageMemoryBarrier* barrier) const;
halcanary9d524f22016-03-29 09:03:52 -0700114
ethannicholasb3058bd2016-07-01 08:22:01 -0700115 SkSL::Compiler* shaderCompiler() const {
116 return fCompiler;
117 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500118
Brian Salomon930f9392018-06-20 16:25:26 -0400119 bool onRegenerateMipMapLevels(GrTexture* tex) override;
120
Brian Salomon1fabd512018-02-09 09:54:25 -0500121 void onResolveRenderTarget(GrRenderTarget* target) override {
122 this->internalResolveRenderTarget(target, true);
Greg Daniel69d49922017-02-23 09:44:02 -0500123 }
egdaniel66933552016-08-24 07:22:19 -0700124
Greg Daniel22bc8652017-03-22 15:45:43 -0400125 void submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>&,
egdaniel9cb63402016-06-23 08:37:05 -0700126 const GrVkRenderPass*,
Robert Phillips8c326e92017-08-10 13:50:17 -0400127 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400128 GrVkRenderTarget*, GrSurfaceOrigin,
egdaniel9cb63402016-06-23 08:37:05 -0700129 const SkIRect& bounds);
egdaniel066df7c2016-06-08 14:02:27 -0700130
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400131 void submit(GrGpuCommandBuffer*) override;
132
Greg Daniel6be35232017-03-01 17:01:09 -0500133 GrFence SK_WARN_UNUSED_RESULT insertFence() override;
134 bool waitFence(GrFence, uint64_t timeout) override;
jvanverth84741b32016-09-30 08:39:02 -0700135 void deleteFence(GrFence) const override;
136
Greg Daniela5cb7812017-06-16 09:45:32 -0400137 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
Greg Daniel48661b82018-01-22 16:11:35 -0500138 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
139 GrResourceProvider::SemaphoreWrapType wrapType,
140 GrWrapOwnership ownership) override;
141 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override;
142 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override;
143
Brian Osman13dddce2017-05-09 13:19:50 -0400144 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
145
Greg Daniel6888c0d2017-08-25 11:55:50 -0400146 void copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
147 VkDeviceSize dstOffset, VkDeviceSize size);
jvanverthdb379092016-07-07 11:18:46 -0700148 bool updateBuffer(GrVkBuffer* buffer, const void* src, VkDeviceSize offset, VkDeviceSize size);
jvanvertha584de92016-06-30 09:10:52 -0700149
Greg Daniel164a9f02016-02-22 09:56:40 -0500150private:
Greg Daniel98bffae2018-08-01 13:25:41 -0400151 GrVkGpu(GrContext*, const GrContextOptions&, const GrVkBackendContext&,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400152 sk_sp<const GrVkInterface>);
jvanverth633b3562016-03-23 11:01:22 -0700153
egdanielcfcd1812016-03-22 07:16:10 -0700154 void onResetContext(uint32_t resetBits) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500155
Greg Daniel8606cf82017-05-08 16:17:53 -0400156 void destroyResources();
157
Brian Salomon58389b92018-03-07 13:01:25 -0500158 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel[],
Brian Salomon2a4f9832018-03-03 22:43:43 -0500159 int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500160
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400161 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400162 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400163 int sampleCnt,
164 GrWrapOwnership) override;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400165 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000166
167 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000168 int sampleCnt) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500169
cdalton1bf3e712016-04-19 10:00:02 -0700170 GrBuffer* onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern,
171 const void* data) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500172
Brian Salomona6948702018-06-01 15:33:20 -0400173 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
174 void* buffer, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500175
Brian Salomona9b04b92018-06-01 15:04:28 -0400176 bool onWritePixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
177 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500178
Brian Salomonc320b152018-02-20 14:05:36 -0500179 bool onTransferPixels(GrTexture*, int left, int top, int width, int height, GrColorType,
180 GrBuffer* transferBuffer, size_t offset, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500181
Brian Salomon9b009bb2018-02-14 13:53:55 -0500182 bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
183 GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Greg Daniel55fa6472018-03-16 16:13:10 -0400184 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) override;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500185
Greg Daniel51316782017-08-02 15:10:09 +0000186 void onFinishFlush(bool insertedSemaphores) override;
187
Greg Daniel164a9f02016-02-22 09:56:40 -0500188 // Ends and submits the current command buffer to the queue and then creates a new command
halcanary9d524f22016-03-29 09:03:52 -0700189 // buffer and begins it. If sync is set to kForce_SyncQueue, the function will wait for all
Greg Daniela5cb7812017-06-16 09:45:32 -0400190 // work in the queue to finish before returning. If this GrVkGpu object has any semaphores in
191 // fSemaphoreToSignal, we will add those signal semaphores to the submission of this command
192 // buffer. If this GrVkGpu object has any semaphores in fSemaphoresToWaitOn, we will add those
193 // wait semaphores to the submission of this command buffer.
194 void submitCommandBuffer(SyncQueue sync);
Greg Daniel164a9f02016-02-22 09:56:40 -0500195
Brian Salomon1fabd512018-02-09 09:54:25 -0500196 void internalResolveRenderTarget(GrRenderTarget*, bool requiresSubmit);
Greg Daniel69d49922017-02-23 09:44:02 -0500197
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400198 void copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
199 GrSurface* src, GrSurfaceOrigin srcOrigin,
200 GrVkImage* dstImage, GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -0500201 const SkIRect& srcRect,
202 const SkIPoint& dstPoint);
203
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400204 void copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
205 GrSurface* src, GrSurfaceOrigin srcOrigin,
206 GrVkImage* dstImage, GrVkImage* srcImage,
egdaniel17b89252016-04-05 07:23:38 -0700207 const SkIRect& srcRect,
208 const SkIPoint& dstPoint);
209
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400210 void copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin,
211 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel4bcd62e2016-08-31 07:37:31 -0700212 const SkIRect& srcRect,
213 const SkIPoint& dstPoint);
214
jvanverth900bd4a2016-04-29 13:53:12 -0700215 // helpers for onCreateTexture and writeTexturePixels
Brian Salomona9b04b92018-06-01 15:04:28 -0400216 bool uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
217 GrColorType colorType, const void* data, size_t rowBytes);
218 bool uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
219 GrColorType colorType, const GrMipLevel texels[], int mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500220
Brian Salomon1fabd512018-02-09 09:54:25 -0500221 void resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
222 const SkIPoint& dstPoint);
egdaniel4bcd62e2016-08-31 07:37:31 -0700223
Brian Salomon52e943a2018-03-13 09:32:39 -0400224#if GR_TEST_UTILS
225 bool createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
226 bool renderable, GrMipMapped mipMapped, const void* srcData,
Robert Phillips646f6372018-09-25 09:31:10 -0400227 size_t srcRowBytes, GrVkImageInfo* info);
Brian Salomon52e943a2018-03-13 09:32:39 -0400228#endif
229
Greg Danielf730c182018-07-02 20:15:37 +0000230 sk_sp<const GrVkInterface> fInterface;
231 sk_sp<GrVkMemoryAllocator> fMemoryAllocator;
232 sk_sp<GrVkCaps> fVkCaps;
jvanverth633b3562016-03-23 11:01:22 -0700233
Greg Danielf730c182018-07-02 20:15:37 +0000234 VkInstance fInstance;
Greg Daniel637c06a2018-09-12 09:44:25 -0400235 VkPhysicalDevice fPhysicalDevice;
Greg Danielf730c182018-07-02 20:15:37 +0000236 VkDevice fDevice;
237 VkQueue fQueue; // Must be Graphics queue
Greg Danielecddbc02018-08-30 16:39:34 -0400238 uint32_t fQueueIndex;
jvanverth633b3562016-03-23 11:01:22 -0700239
240 // Created by GrVkGpu
Greg Danielf730c182018-07-02 20:15:37 +0000241 GrVkResourceProvider fResourceProvider;
242 VkCommandPool fCmdPool;
Greg Daniel164a9f02016-02-22 09:56:40 -0500243
Greg Danielf730c182018-07-02 20:15:37 +0000244 GrVkPrimaryCommandBuffer* fCurrentCmdBuffer;
jvanverth6b6ffc42016-06-13 14:28:07 -0700245
Greg Danielf730c182018-07-02 20:15:37 +0000246 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToWaitOn;
247 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToSignal;
Greg Daniel6be35232017-03-01 17:01:09 -0500248
Greg Danielf730c182018-07-02 20:15:37 +0000249 VkPhysicalDeviceProperties fPhysDevProps;
250 VkPhysicalDeviceMemoryProperties fPhysDevMemProps;
Greg Daniel6be35232017-03-01 17:01:09 -0500251
Greg Danielf730c182018-07-02 20:15:37 +0000252 GrVkCopyManager fCopyManager;
egdanielbc9b2962016-09-27 08:00:53 -0700253
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500254 // compiler used for compiling sksl into spirv. We only want to create the compiler once since
255 // there is significant overhead to the first compile of any compiler.
Greg Danielf730c182018-07-02 20:15:37 +0000256 SkSL::Compiler* fCompiler;
Greg Daniel164a9f02016-02-22 09:56:40 -0500257
Greg Daniel8606cf82017-05-08 16:17:53 -0400258 // We need a bool to track whether or not we've already disconnected all the gpu resources from
259 // vulkan context.
Greg Danielf730c182018-07-02 20:15:37 +0000260 bool fDisconnected;
Greg Daniel8606cf82017-05-08 16:17:53 -0400261
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400262 std::unique_ptr<GrVkGpuRTCommandBuffer> fCachedRTCommandBuffer;
263 std::unique_ptr<GrVkGpuTextureCommandBuffer> fCachedTexCommandBuffer;
264
Greg Daniel164a9f02016-02-22 09:56:40 -0500265 typedef GrGpu INHERITED;
266};
267
268#endif