Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 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 | |
| 8 | #ifndef GrVkGpu_DEFINED |
| 9 | #define GrVkGpu_DEFINED |
| 10 | |
| 11 | #include "GrGpu.h" |
| 12 | #include "GrGpuFactory.h" |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 13 | #include "vk/GrVkBackendContext.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrVkCaps.h" |
| 15 | #include "GrVkIndexBuffer.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 16 | #include "GrVkResourceProvider.h" |
| 17 | #include "GrVkVertexBuffer.h" |
| 18 | #include "GrVkUtil.h" |
| 19 | |
| 20 | #include "shaderc/shaderc.h" |
| 21 | #include "vulkan/vulkan.h" |
| 22 | |
| 23 | class GrPipeline; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 24 | class GrNonInstancedMesh; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | |
| 26 | class GrVkBufferImpl; |
| 27 | class GrVkCommandBuffer; |
| 28 | class GrVkPipeline; |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 29 | class GrVkPipelineState; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 30 | class GrVkRenderPass; |
| 31 | class GrVkTexture; |
| 32 | struct GrVkInterface; |
| 33 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 34 | #ifdef SK_DEBUG |
| 35 | #define ENABLE_VK_LAYERS |
| 36 | #endif |
| 37 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 38 | class GrVkGpu : public GrGpu { |
| 39 | public: |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 40 | static GrGpu* Create(GrBackendContext backendContext, const GrContextOptions& options, |
| 41 | GrContext* context); |
| 42 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 43 | ~GrVkGpu() override; |
| 44 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 45 | const GrVkInterface* vkInterface() const { return fBackendContext->fInterface; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 46 | 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 | |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 70 | void discard(GrRenderTarget*) override {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 71 | |
| 72 | bool onCopySurface(GrSurface* dst, |
| 73 | GrSurface* src, |
| 74 | const SkIRect& srcRect, |
| 75 | const SkIPoint& dstPoint) override; |
| 76 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 77 | void onGetMultisampleSpecs(GrRenderTarget* rt, |
| 78 | const GrStencilSettings&, |
| 79 | int* effectiveSampleCnt, |
| 80 | SkAutoTDeleteArray<SkPoint>*); |
| 81 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 82 | bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 83 | 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 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 97 | void clearStencil(GrRenderTarget* target) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 98 | |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 99 | void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 100 | |
| 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 | |
| 120 | private: |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 121 | GrVkGpu(GrContext* context, const GrContextOptions& options, |
| 122 | const GrVkBackendContext* backendContext); |
| 123 | |
egdaniel | cfcd181 | 2016-03-22 07:16:10 -0700 | [diff] [blame] | 124 | void onResetContext(uint32_t resetBits) override {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 125 | |
| 126 | GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 127 | const SkTArray<GrMipLevel>&) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 128 | |
| 129 | GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle, |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 130 | const SkTArray<GrMipLevel>&) override { return NULL; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 131 | |
| 132 | GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, GrWrapOwnership) override; |
| 133 | |
| 134 | GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&, |
| 135 | GrWrapOwnership) override; |
jvanverth | f7e865a | 2016-02-25 07:59:45 -0800 | [diff] [blame] | 136 | GrRenderTarget* onWrapBackendTextureAsRenderTarget(const GrBackendTextureDesc&, |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 137 | GrWrapOwnership) override { return NULL; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 138 | |
| 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 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 145 | void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 146 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 147 | void onDraw(const GrPipeline&, |
| 148 | const GrPrimitiveProcessor&, |
| 149 | const GrMesh*, |
| 150 | int meshCount) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 151 | |
| 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, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 160 | GrPixelConfig config, const SkTArray<GrMipLevel>&) override; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 161 | |
| 162 | bool onTransferPixels(GrSurface*, |
| 163 | int left, int top, int width, int height, |
| 164 | GrPixelConfig config, GrTransferBuffer* buffer, |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 165 | size_t offset, size_t rowBytes) override { return false; } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 166 | |
egdaniel | e267893 | 2016-03-24 09:50:58 -0700 | [diff] [blame^] | 167 | void onResolveRenderTarget(GrRenderTarget* target) override {} |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 168 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 169 | bool prepareDrawState(const GrPipeline&, |
| 170 | const GrPrimitiveProcessor&, |
| 171 | GrPrimitiveType, |
| 172 | const GrVkRenderPass&, |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 173 | GrVkPipelineState** pipelineState); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 174 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 175 | // Bind vertex and index buffers |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 176 | void bindGeometry(const GrPrimitiveProcessor&, const GrNonInstancedMesh&); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 177 | |
| 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 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 200 | 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 Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 214 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 215 | #ifdef ENABLE_VK_LAYERS |
| 216 | // For reporting validation layer errors |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 217 | VkDebugReportCallbackEXT fCallback; |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 218 | #endif |
| 219 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 220 | // 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 Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 224 | |
| 225 | typedef GrGpu INHERITED; |
| 226 | }; |
| 227 | |
| 228 | #endif |