blob: dc98ca7bdd66009dbb719ea149588bff4c38c2c8 [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,
79 GrPixelConfig config, bool isRenderTarget,
Robert Phillipsd21b2a52017-12-12 13:01:25 -050080 GrMipMapped) override;
81 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 Phillips95214472017-08-08 18:00:03 -040094 void clearStencil(GrRenderTarget* target, int clearValue) override;
Greg Daniel164a9f02016-02-22 09:56:40 -050095
Robert Phillips5b5d84c2018-08-09 15:12:18 -040096 GrGpuRTCommandBuffer* getCommandBuffer(
Robert Phillips95214472017-08-08 18:00:03 -040097 GrRenderTarget*, GrSurfaceOrigin,
Greg Daniel500d58b2017-08-24 15:59:33 -040098 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
99 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) override;
100
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400101 GrGpuTextureCommandBuffer* getCommandBuffer(GrTexture*, GrSurfaceOrigin) override;
102
egdaniel066df7c2016-06-08 14:02:27 -0700103
Greg Daniel164a9f02016-02-22 09:56:40 -0500104 void addMemoryBarrier(VkPipelineStageFlags srcStageMask,
105 VkPipelineStageFlags dstStageMask,
106 bool byRegion,
107 VkMemoryBarrier* barrier) const;
108 void addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
109 VkPipelineStageFlags dstStageMask,
110 bool byRegion,
111 VkBufferMemoryBarrier* barrier) const;
112 void addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
113 VkPipelineStageFlags dstStageMask,
114 bool byRegion,
115 VkImageMemoryBarrier* barrier) const;
halcanary9d524f22016-03-29 09:03:52 -0700116
ethannicholasb3058bd2016-07-01 08:22:01 -0700117 SkSL::Compiler* shaderCompiler() const {
118 return fCompiler;
119 }
Greg Daniel164a9f02016-02-22 09:56:40 -0500120
Brian Salomon930f9392018-06-20 16:25:26 -0400121 bool onRegenerateMipMapLevels(GrTexture* tex) override;
122
Brian Salomon1fabd512018-02-09 09:54:25 -0500123 void onResolveRenderTarget(GrRenderTarget* target) override {
124 this->internalResolveRenderTarget(target, true);
Greg Daniel69d49922017-02-23 09:44:02 -0500125 }
egdaniel66933552016-08-24 07:22:19 -0700126
Greg Daniel22bc8652017-03-22 15:45:43 -0400127 void submitSecondaryCommandBuffer(const SkTArray<GrVkSecondaryCommandBuffer*>&,
egdaniel9cb63402016-06-23 08:37:05 -0700128 const GrVkRenderPass*,
Robert Phillips8c326e92017-08-10 13:50:17 -0400129 const VkClearValue* colorClear,
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400130 GrVkRenderTarget*, GrSurfaceOrigin,
egdaniel9cb63402016-06-23 08:37:05 -0700131 const SkIRect& bounds);
egdaniel066df7c2016-06-08 14:02:27 -0700132
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400133 void submit(GrGpuCommandBuffer*) override;
134
Greg Daniel6be35232017-03-01 17:01:09 -0500135 GrFence SK_WARN_UNUSED_RESULT insertFence() override;
136 bool waitFence(GrFence, uint64_t timeout) override;
jvanverth84741b32016-09-30 08:39:02 -0700137 void deleteFence(GrFence) const override;
138
Greg Daniela5cb7812017-06-16 09:45:32 -0400139 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore(bool isOwned) override;
Greg Daniel48661b82018-01-22 16:11:35 -0500140 sk_sp<GrSemaphore> wrapBackendSemaphore(const GrBackendSemaphore& semaphore,
141 GrResourceProvider::SemaphoreWrapType wrapType,
142 GrWrapOwnership ownership) override;
143 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override;
144 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override;
145
Brian Osman13dddce2017-05-09 13:19:50 -0400146 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override;
147
Greg Daniel6888c0d2017-08-25 11:55:50 -0400148 void copyBuffer(GrVkBuffer* srcBuffer, GrVkBuffer* dstBuffer, VkDeviceSize srcOffset,
149 VkDeviceSize dstOffset, VkDeviceSize size);
jvanverthdb379092016-07-07 11:18:46 -0700150 bool updateBuffer(GrVkBuffer* buffer, const void* src, VkDeviceSize offset, VkDeviceSize size);
jvanvertha584de92016-06-30 09:10:52 -0700151
Greg Daniel164a9f02016-02-22 09:56:40 -0500152private:
Greg Daniel98bffae2018-08-01 13:25:41 -0400153 GrVkGpu(GrContext*, const GrContextOptions&, const GrVkBackendContext&,
Greg Danielc8cd45a2018-07-12 10:02:37 -0400154 sk_sp<const GrVkInterface>);
jvanverth633b3562016-03-23 11:01:22 -0700155
egdanielcfcd1812016-03-22 07:16:10 -0700156 void onResetContext(uint32_t resetBits) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500157
Greg Daniel8606cf82017-05-08 16:17:53 -0400158 void destroyResources();
159
Brian Salomon58389b92018-03-07 13:01:25 -0500160 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc&, SkBudgeted, const GrMipLevel[],
Brian Salomon2a4f9832018-03-03 22:43:43 -0500161 int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500162
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400163 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&, GrWrapOwnership) override;
Brian Salomond17f6582017-07-19 18:28:58 -0400164 sk_sp<GrTexture> onWrapRenderableBackendTexture(const GrBackendTexture&,
Brian Salomond17f6582017-07-19 18:28:58 -0400165 int sampleCnt,
166 GrWrapOwnership) override;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400167 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&) override;
Greg Daniel7ef28f32017-04-20 16:41:55 +0000168
169 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000170 int sampleCnt) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500171
cdalton1bf3e712016-04-19 10:00:02 -0700172 GrBuffer* onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern,
173 const void* data) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500174
Brian Salomona6948702018-06-01 15:33:20 -0400175 bool onReadPixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
176 void* buffer, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500177
Brian Salomona9b04b92018-06-01 15:04:28 -0400178 bool onWritePixels(GrSurface* surface, int left, int top, int width, int height, GrColorType,
179 const GrMipLevel texels[], int mipLevelCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500180
Brian Salomonc320b152018-02-20 14:05:36 -0500181 bool onTransferPixels(GrTexture*, int left, int top, int width, int height, GrColorType,
182 GrBuffer* transferBuffer, size_t offset, size_t rowBytes) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500183
Brian Salomon9b009bb2018-02-14 13:53:55 -0500184 bool onCopySurface(GrSurface* dst, GrSurfaceOrigin dstOrigin, GrSurface* src,
185 GrSurfaceOrigin srcOrigin, const SkIRect& srcRect,
Greg Daniel55fa6472018-03-16 16:13:10 -0400186 const SkIPoint& dstPoint, bool canDiscardOutsideDstRect) override;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500187
Greg Daniel51316782017-08-02 15:10:09 +0000188 void onFinishFlush(bool insertedSemaphores) override;
189
Greg Daniel164a9f02016-02-22 09:56:40 -0500190 // Ends and submits the current command buffer to the queue and then creates a new command
halcanary9d524f22016-03-29 09:03:52 -0700191 // buffer and begins it. If sync is set to kForce_SyncQueue, the function will wait for all
Greg Daniela5cb7812017-06-16 09:45:32 -0400192 // work in the queue to finish before returning. If this GrVkGpu object has any semaphores in
193 // fSemaphoreToSignal, we will add those signal semaphores to the submission of this command
194 // buffer. If this GrVkGpu object has any semaphores in fSemaphoresToWaitOn, we will add those
195 // wait semaphores to the submission of this command buffer.
196 void submitCommandBuffer(SyncQueue sync);
Greg Daniel164a9f02016-02-22 09:56:40 -0500197
Brian Salomon1fabd512018-02-09 09:54:25 -0500198 void internalResolveRenderTarget(GrRenderTarget*, bool requiresSubmit);
Greg Daniel69d49922017-02-23 09:44:02 -0500199
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400200 void copySurfaceAsCopyImage(GrSurface* dst, GrSurfaceOrigin dstOrigin,
201 GrSurface* src, GrSurfaceOrigin srcOrigin,
202 GrVkImage* dstImage, GrVkImage* srcImage,
Greg Daniel164a9f02016-02-22 09:56:40 -0500203 const SkIRect& srcRect,
204 const SkIPoint& dstPoint);
205
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400206 void copySurfaceAsBlit(GrSurface* dst, GrSurfaceOrigin dstOrigin,
207 GrSurface* src, GrSurfaceOrigin srcOrigin,
208 GrVkImage* dstImage, GrVkImage* srcImage,
egdaniel17b89252016-04-05 07:23:38 -0700209 const SkIRect& srcRect,
210 const SkIPoint& dstPoint);
211
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400212 void copySurfaceAsResolve(GrSurface* dst, GrSurfaceOrigin dstOrigin,
213 GrSurface* src, GrSurfaceOrigin srcOrigin,
egdaniel4bcd62e2016-08-31 07:37:31 -0700214 const SkIRect& srcRect,
215 const SkIPoint& dstPoint);
216
jvanverth900bd4a2016-04-29 13:53:12 -0700217 // helpers for onCreateTexture and writeTexturePixels
Brian Salomona9b04b92018-06-01 15:04:28 -0400218 bool uploadTexDataLinear(GrVkTexture* tex, int left, int top, int width, int height,
219 GrColorType colorType, const void* data, size_t rowBytes);
220 bool uploadTexDataOptimal(GrVkTexture* tex, int left, int top, int width, int height,
221 GrColorType colorType, const GrMipLevel texels[], int mipLevelCount);
Greg Daniel164a9f02016-02-22 09:56:40 -0500222
Brian Salomon1fabd512018-02-09 09:54:25 -0500223 void resolveImage(GrSurface* dst, GrVkRenderTarget* src, const SkIRect& srcRect,
224 const SkIPoint& dstPoint);
egdaniel4bcd62e2016-08-31 07:37:31 -0700225
Brian Salomon52e943a2018-03-13 09:32:39 -0400226#if GR_TEST_UTILS
227 bool createTestingOnlyVkImage(GrPixelConfig config, int w, int h, bool texturable,
228 bool renderable, GrMipMapped mipMapped, const void* srcData,
229 GrVkImageInfo* info);
230#endif
231
Greg Danielf730c182018-07-02 20:15:37 +0000232 sk_sp<const GrVkInterface> fInterface;
233 sk_sp<GrVkMemoryAllocator> fMemoryAllocator;
234 sk_sp<GrVkCaps> fVkCaps;
jvanverth633b3562016-03-23 11:01:22 -0700235
Greg Danielf730c182018-07-02 20:15:37 +0000236 VkInstance fInstance;
Greg Daniel637c06a2018-09-12 09:44:25 -0400237 VkPhysicalDevice fPhysicalDevice;
Greg Danielf730c182018-07-02 20:15:37 +0000238 VkDevice fDevice;
239 VkQueue fQueue; // Must be Graphics queue
Greg Danielecddbc02018-08-30 16:39:34 -0400240 uint32_t fQueueIndex;
jvanverth633b3562016-03-23 11:01:22 -0700241
242 // Created by GrVkGpu
Greg Danielf730c182018-07-02 20:15:37 +0000243 GrVkResourceProvider fResourceProvider;
244 VkCommandPool fCmdPool;
Greg Daniel164a9f02016-02-22 09:56:40 -0500245
Greg Danielf730c182018-07-02 20:15:37 +0000246 GrVkPrimaryCommandBuffer* fCurrentCmdBuffer;
jvanverth6b6ffc42016-06-13 14:28:07 -0700247
Greg Danielf730c182018-07-02 20:15:37 +0000248 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToWaitOn;
249 SkSTArray<1, GrVkSemaphore::Resource*> fSemaphoresToSignal;
Greg Daniel6be35232017-03-01 17:01:09 -0500250
Greg Danielf730c182018-07-02 20:15:37 +0000251 VkPhysicalDeviceProperties fPhysDevProps;
252 VkPhysicalDeviceMemoryProperties fPhysDevMemProps;
Greg Daniel6be35232017-03-01 17:01:09 -0500253
Greg Danielf730c182018-07-02 20:15:37 +0000254 GrVkCopyManager fCopyManager;
egdanielbc9b2962016-09-27 08:00:53 -0700255
Ethan Nicholas941e7e22016-12-12 15:33:30 -0500256 // compiler used for compiling sksl into spirv. We only want to create the compiler once since
257 // there is significant overhead to the first compile of any compiler.
Greg Danielf730c182018-07-02 20:15:37 +0000258 SkSL::Compiler* fCompiler;
Greg Daniel164a9f02016-02-22 09:56:40 -0500259
Greg Daniel8606cf82017-05-08 16:17:53 -0400260 // We need a bool to track whether or not we've already disconnected all the gpu resources from
261 // vulkan context.
Greg Danielf730c182018-07-02 20:15:37 +0000262 bool fDisconnected;
Greg Daniel8606cf82017-05-08 16:17:53 -0400263
Robert Phillips5b5d84c2018-08-09 15:12:18 -0400264 std::unique_ptr<GrVkGpuRTCommandBuffer> fCachedRTCommandBuffer;
265 std::unique_ptr<GrVkGpuTextureCommandBuffer> fCachedTexCommandBuffer;
266
Greg Daniel164a9f02016-02-22 09:56:40 -0500267 typedef GrGpu INHERITED;
268};
269
270#endif