blob: a6911f773b57367357c010b34076537f82276c93 [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"
12#include "GrGpuFactory.h"
jvanverth633b3562016-03-23 11:01:22 -070013#include "vk/GrVkBackendContext.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050014#include "GrVkCaps.h"
15#include "GrVkIndexBuffer.h"
Greg Daniel164a9f02016-02-22 09:56:40 -050016#include "GrVkResourceProvider.h"
17#include "GrVkVertexBuffer.h"
18#include "GrVkUtil.h"
19
20#include "shaderc/shaderc.h"
21#include "vulkan/vulkan.h"
22
23class GrPipeline;
egdaniel0e1853c2016-03-17 11:35:45 -070024class GrNonInstancedMesh;
Greg Daniel164a9f02016-02-22 09:56:40 -050025
26class GrVkBufferImpl;
27class GrVkCommandBuffer;
28class GrVkPipeline;
egdaniel22281c12016-03-23 13:49:40 -070029class GrVkPipelineState;
Greg Daniel164a9f02016-02-22 09:56:40 -050030class GrVkRenderPass;
31class GrVkTexture;
32struct GrVkInterface;
33
jvanverthd2497f32016-03-18 12:39:05 -070034#ifdef SK_DEBUG
35#define ENABLE_VK_LAYERS
36#endif
37
Greg Daniel164a9f02016-02-22 09:56:40 -050038class GrVkGpu : public GrGpu {
39public:
jvanverth633b3562016-03-23 11:01:22 -070040 static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options,
41 GrContext* context);
42
Greg Daniel164a9f02016-02-22 09:56:40 -050043 ~GrVkGpu() override;
44
jvanverth633b3562016-03-23 11:01:22 -070045 const GrVkInterface* vkInterface() const { return fBackendContext->fInterface; }
Greg Daniel164a9f02016-02-22 09:56:40 -050046 const GrVkCaps& vkCaps() const { return *fVkCaps; }
47
48 VkDevice device() const { return fDevice; }
49 VkQueue queue() const { return fQueue; }
50 VkCommandPool cmdPool() const { return fCmdPool; }
51 VkPhysicalDeviceMemoryProperties physicalDeviceMemoryProperties() const {
52 return fPhysDevMemProps;
53 }
54
55 GrVkResourceProvider& resourceProvider() { return fResourceProvider; }
56
57 enum SyncQueue {
58 kForce_SyncQueue,
59 kSkip_SyncQueue
60 };
61
62 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
63 GrPixelConfig readConfig, DrawPreference*,
64 ReadPixelTempDrawInfo*) override;
65
66 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
67 GrPixelConfig srcConfig, DrawPreference*,
68 WritePixelTempDrawInfo*) override;
69
egdaniele2678932016-03-24 09:50:58 -070070 void discard(GrRenderTarget*) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -050071
72 bool onCopySurface(GrSurface* dst,
73 GrSurface* src,
74 const SkIRect& srcRect,
75 const SkIPoint& dstPoint) override;
76
cdalton28f45b92016-03-07 13:58:26 -080077 void onGetMultisampleSpecs(GrRenderTarget* rt,
78 const GrStencilSettings&,
79 int* effectiveSampleCnt,
80 SkAutoTDeleteArray<SkPoint>*);
81
Greg Daniel164a9f02016-02-22 09:56:40 -050082 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override {
Greg Daniel164a9f02016-02-22 09:56:40 -050083 return false;
84 }
85
86 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
87
88 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
89 GrPixelConfig config) override;
90 bool isTestingOnlyBackendTexture(GrBackendObject id) const override;
91 void deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandonTexture) override;
92
93 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
94 int width,
95 int height) override;
96
egdaniel3d5d9ac2016-03-01 12:56:15 -080097 void clearStencil(GrRenderTarget* target) override;
Greg Daniel164a9f02016-02-22 09:56:40 -050098
egdaniele2678932016-03-24 09:50:58 -070099 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500100
101 void addMemoryBarrier(VkPipelineStageFlags srcStageMask,
102 VkPipelineStageFlags dstStageMask,
103 bool byRegion,
104 VkMemoryBarrier* barrier) const;
105 void addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask,
106 VkPipelineStageFlags dstStageMask,
107 bool byRegion,
108 VkBufferMemoryBarrier* barrier) const;
109 void addImageMemoryBarrier(VkPipelineStageFlags srcStageMask,
110 VkPipelineStageFlags dstStageMask,
111 bool byRegion,
112 VkImageMemoryBarrier* barrier) const;
113
114 shaderc_compiler_t shadercCompiler() const {
115 return fCompiler;
116 }
117
118 void finishDrawTarget() override;
119
120private:
jvanverth633b3562016-03-23 11:01:22 -0700121 GrVkGpu(GrContext* context, const GrContextOptions& options,
122 const GrVkBackendContext* backendContext);
123
egdanielcfcd1812016-03-22 07:16:10 -0700124 void onResetContext(uint32_t resetBits) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500125
126 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
bsalomona1e6b3b2016-03-02 10:58:23 -0800127 const SkTArray<GrMipLevel>&) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500128
129 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
egdaniele2678932016-03-24 09:50:58 -0700130 const SkTArray<GrMipLevel>&) override { return NULL; }
Greg Daniel164a9f02016-02-22 09:56:40 -0500131
132 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override;
133
134 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
135 GrWrapOwnership) override;
jvanverthf7e865a2016-02-25 07:59:45 -0800136 GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&,
egdaniele2678932016-03-24 09:50:58 -0700137 GrWrapOwnership) override { return NULL; }
Greg Daniel164a9f02016-02-22 09:56:40 -0500138
139 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override;
140 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override;
141 GrTransferBuffer* onCreateTransferBuffer(size_t size, TransferType type) override;
142
143 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override;
144
egdaniel3d5d9ac2016-03-01 12:56:15 -0800145 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500146
egdaniel0e1853c2016-03-17 11:35:45 -0700147 void onDraw(const GrPipeline&,
148 const GrPrimitiveProcessor&,
149 const GrMesh*,
150 int meshCount) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500151
152 bool onReadPixels(GrSurface* surface,
153 int left, int top, int width, int height,
154 GrPixelConfig,
155 void* buffer,
156 size_t rowBytes) override;
157
158 bool onWritePixels(GrSurface* surface,
159 int left, int top, int width, int height,
bsalomona1e6b3b2016-03-02 10:58:23 -0800160 GrPixelConfig config, const SkTArray<GrMipLevel>&) override;
Greg Daniel164a9f02016-02-22 09:56:40 -0500161
162 bool onTransferPixels(GrSurface*,
163 int left, int top, int width, int height,
164 GrPixelConfig config, GrTransferBuffer* buffer,
egdaniele2678932016-03-24 09:50:58 -0700165 size_t offset, size_t rowBytes) override { return false; }
Greg Daniel164a9f02016-02-22 09:56:40 -0500166
egdaniele2678932016-03-24 09:50:58 -0700167 void onResolveRenderTarget(GrRenderTarget* target) override {}
Greg Daniel164a9f02016-02-22 09:56:40 -0500168
egdaniel0e1853c2016-03-17 11:35:45 -0700169 bool prepareDrawState(const GrPipeline&,
170 const GrPrimitiveProcessor&,
171 GrPrimitiveType,
172 const GrVkRenderPass&,
egdaniel22281c12016-03-23 13:49:40 -0700173 GrVkPipelineState** pipelineState);
egdaniel0e1853c2016-03-17 11:35:45 -0700174
Greg Daniel164a9f02016-02-22 09:56:40 -0500175 // Bind vertex and index buffers
egdaniel0e1853c2016-03-17 11:35:45 -0700176 void bindGeometry(const GrPrimitiveProcessor&, const GrNonInstancedMesh&);
Greg Daniel164a9f02016-02-22 09:56:40 -0500177
178 // Ends and submits the current command buffer to the queue and then creates a new command
179 // buffer and begins it. If sync is set to kForce_SyncQueue, the function will wait for all
180 // work in the queue to finish before returning.
181 void submitCommandBuffer(SyncQueue sync);
182
183 void copySurfaceAsCopyImage(GrSurface* dst,
184 GrSurface* src,
185 const SkIRect& srcRect,
186 const SkIPoint& dstPoint);
187
188 void copySurfaceAsDraw(GrSurface* dst,
189 GrSurface* src,
190 const SkIRect& srcRect,
191 const SkIPoint& dstPoint);
192
193 // helper for onCreateTexture and writeTexturePixels
194 bool uploadTexData(GrVkTexture* tex,
195 int left, int top, int width, int height,
196 GrPixelConfig dataConfig,
197 const void* data,
198 size_t rowBytes);
199
jvanverth633b3562016-03-23 11:01:22 -0700200 SkAutoTUnref<const GrVkBackendContext> fBackendContext;
201 SkAutoTUnref<GrVkCaps> fVkCaps;
202
203 // These Vulkan objects are provided by the client, and also stored in fBackendContext.
204 // They're copied here for convenient access.
205 VkInstance fVkInstance;
206 VkDevice fDevice;
207 VkQueue fQueue; // Must be Graphics queue
208
209 // Created by GrVkGpu
210 GrVkResourceProvider fResourceProvider;
211 VkCommandPool fCmdPool;
212 GrVkCommandBuffer* fCurrentCmdBuffer;
213 VkPhysicalDeviceMemoryProperties fPhysDevMemProps;
Greg Daniel164a9f02016-02-22 09:56:40 -0500214
jvanverthd2497f32016-03-18 12:39:05 -0700215#ifdef ENABLE_VK_LAYERS
216 // For reporting validation layer errors
jvanverth633b3562016-03-23 11:01:22 -0700217 VkDebugReportCallbackEXT fCallback;
jvanverthd2497f32016-03-18 12:39:05 -0700218#endif
219
Greg Daniel164a9f02016-02-22 09:56:40 -0500220 // Shaderc compiler used for compiling glsl in spirv. We only want to create the compiler once
221 // since there is significant overhead to the first compile of any compiler.
222 shaderc_compiler_t fCompiler;
223
Greg Daniel164a9f02016-02-22 09:56:40 -0500224
225 typedef GrGpu INHERITED;
226};
227
228#endif