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 | #include "GrVkGpu.h" |
| 9 | |
| 10 | #include "GrContextOptions.h" |
| 11 | #include "GrGeometryProcessor.h" |
| 12 | #include "GrGpuResourceCacheAccess.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 14 | #include "GrPipeline.h" |
| 15 | #include "GrRenderTargetPriv.h" |
| 16 | #include "GrSurfacePriv.h" |
| 17 | #include "GrTexturePriv.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 18 | |
| 19 | #include "GrVkCommandBuffer.h" |
| 20 | #include "GrVkImage.h" |
| 21 | #include "GrVkIndexBuffer.h" |
| 22 | #include "GrVkMemory.h" |
| 23 | #include "GrVkPipeline.h" |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 24 | #include "GrVkPipelineState.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 25 | #include "GrVkRenderPass.h" |
| 26 | #include "GrVkResourceProvider.h" |
| 27 | #include "GrVkTexture.h" |
| 28 | #include "GrVkTextureRenderTarget.h" |
| 29 | #include "GrVkTransferBuffer.h" |
| 30 | #include "GrVkVertexBuffer.h" |
| 31 | |
| 32 | #include "SkConfig8888.h" |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 33 | #include "SkMipMap.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 34 | |
| 35 | #include "vk/GrVkInterface.h" |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 36 | #include "vk/GrVkTypes.h" |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 37 | |
| 38 | #define VK_CALL(X) GR_VK_CALL(this->vkInterface(), X) |
| 39 | #define VK_CALL_RET(RET, X) GR_VK_CALL_RET(this->vkInterface(), RET, X) |
| 40 | #define VK_CALL_ERRCHECK(X) GR_VK_CALL_ERRCHECK(this->vkInterface(), X) |
| 41 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 42 | #ifdef ENABLE_VK_LAYERS |
| 43 | VKAPI_ATTR VkBool32 VKAPI_CALL DebugReportCallback( |
| 44 | VkDebugReportFlagsEXT flags, |
| 45 | VkDebugReportObjectTypeEXT objectType, |
| 46 | uint64_t object, |
| 47 | size_t location, |
| 48 | int32_t messageCode, |
| 49 | const char* pLayerPrefix, |
| 50 | const char* pMessage, |
| 51 | void* pUserData) { |
| 52 | if (flags & VK_DEBUG_REPORT_ERROR_BIT_EXT) { |
| 53 | SkDebugf("Vulkan error [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 54 | } else if (flags & VK_DEBUG_REPORT_WARNING_BIT_EXT) { |
| 55 | SkDebugf("Vulkan warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 56 | } else if (flags & VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT) { |
| 57 | SkDebugf("Vulkan perf warning [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 58 | } else { |
| 59 | SkDebugf("Vulkan info/debug [%s]: code: %d: %s\n", pLayerPrefix, messageCode, pMessage); |
| 60 | } |
| 61 | return VK_FALSE; |
| 62 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 63 | #endif |
| 64 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 65 | GrGpu* GrVkGpu::Create(GrBackendContext backendContext, const GrContextOptions& options, |
| 66 | GrContext* context) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 67 | const GrVkBackendContext* vkBackendContext = |
| 68 | reinterpret_cast<const GrVkBackendContext*>(backendContext); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 69 | if (!vkBackendContext) { |
bsalomon | dc0fcd4 | 2016-04-11 14:21:33 -0700 | [diff] [blame] | 70 | vkBackendContext = GrVkBackendContext::Create(); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 71 | if (!vkBackendContext) { |
| 72 | return nullptr; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 73 | } |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 74 | } else { |
| 75 | vkBackendContext->ref(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 76 | } |
| 77 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 78 | return new GrVkGpu(context, options, vkBackendContext); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | //////////////////////////////////////////////////////////////////////////////// |
| 82 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 83 | GrVkGpu::GrVkGpu(GrContext* context, const GrContextOptions& options, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 84 | const GrVkBackendContext* backendCtx) |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 85 | : INHERITED(context) |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 86 | , fDevice(backendCtx->fDevice) |
| 87 | , fQueue(backendCtx->fQueue) |
| 88 | , fResourceProvider(this) { |
| 89 | fBackendContext.reset(backendCtx); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 90 | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 91 | #ifdef ENABLE_VK_LAYERS |
brianosman | 419ca64 | 2016-05-04 08:19:44 -0700 | [diff] [blame] | 92 | fCallback = VK_NULL_HANDLE; |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 93 | if (backendCtx->fExtensions & kEXT_debug_report_GrVkExtensionFlag) { |
| 94 | // Setup callback creation information |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 95 | VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; |
| 96 | callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; |
| 97 | callbackCreateInfo.pNext = nullptr; |
| 98 | callbackCreateInfo.flags = VK_DEBUG_REPORT_ERROR_BIT_EXT | |
egdaniel | ef0c10c | 2016-04-07 07:51:22 -0700 | [diff] [blame] | 99 | VK_DEBUG_REPORT_WARNING_BIT_EXT | |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 100 | //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | |
| 101 | //VK_DEBUG_REPORT_DEBUG_BIT_EXT | |
egdaniel | b4aa362 | 2016-04-06 13:47:08 -0700 | [diff] [blame] | 102 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT; |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 103 | callbackCreateInfo.pfnCallback = &DebugReportCallback; |
| 104 | callbackCreateInfo.pUserData = nullptr; |
| 105 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 106 | // Register the callback |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 107 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateDebugReportCallbackEXT( |
| 108 | backendCtx->fInstance, &callbackCreateInfo, nullptr, &fCallback)); |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 109 | } |
| 110 | #endif |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 111 | |
| 112 | fCompiler = shaderc_compiler_initialize(); |
| 113 | |
jvanverth | fd7bd45 | 2016-03-25 06:29:52 -0700 | [diff] [blame] | 114 | fVkCaps.reset(new GrVkCaps(options, this->vkInterface(), backendCtx->fPhysicalDevice, |
egdaniel | c5ec140 | 2016-03-28 12:14:42 -0700 | [diff] [blame] | 115 | backendCtx->fFeatures, backendCtx->fExtensions)); |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 116 | fCaps.reset(SkRef(fVkCaps.get())); |
| 117 | |
| 118 | VK_CALL(GetPhysicalDeviceMemoryProperties(backendCtx->fPhysicalDevice, &fPhysDevMemProps)); |
| 119 | |
| 120 | const VkCommandPoolCreateInfo cmdPoolInfo = { |
| 121 | VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, // sType |
| 122 | nullptr, // pNext |
| 123 | VK_COMMAND_POOL_CREATE_TRANSIENT_BIT, // CmdPoolCreateFlags |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 124 | backendCtx->fGraphicsQueueIndex, // queueFamilyIndex |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 125 | }; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 126 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateCommandPool(fDevice, &cmdPoolInfo, nullptr, |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 127 | &fCmdPool)); |
| 128 | |
| 129 | // must call this after creating the CommandPool |
| 130 | fResourceProvider.init(); |
| 131 | fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); |
| 132 | SkASSERT(fCurrentCmdBuffer); |
| 133 | fCurrentCmdBuffer->begin(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | GrVkGpu::~GrVkGpu() { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 137 | fCurrentCmdBuffer->end(this); |
| 138 | fCurrentCmdBuffer->unref(this); |
| 139 | |
| 140 | // wait for all commands to finish |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 141 | fResourceProvider.checkCommandBuffers(); |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 142 | SkDEBUGCODE(VkResult res = ) VK_CALL(QueueWaitIdle(fQueue)); |
jvanverth | ddf9835 | 2016-03-21 11:46:00 -0700 | [diff] [blame] | 143 | // VK_ERROR_DEVICE_LOST is acceptable when tearing down (see 4.2.4 in spec) |
| 144 | SkASSERT(VK_SUCCESS == res || VK_ERROR_DEVICE_LOST == res); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 145 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 146 | // must call this just before we destroy the VkDevice |
| 147 | fResourceProvider.destroyResources(); |
| 148 | |
jvanverth | 633b356 | 2016-03-23 11:01:22 -0700 | [diff] [blame] | 149 | VK_CALL(DestroyCommandPool(fDevice, fCmdPool, nullptr)); |
| 150 | |
| 151 | shaderc_compiler_release(fCompiler); |
| 152 | |
| 153 | #ifdef ENABLE_VK_LAYERS |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 154 | if (fCallback) { |
| 155 | VK_CALL(DestroyDebugReportCallbackEXT(fBackendContext->fInstance, fCallback, nullptr)); |
brianosman | 419ca64 | 2016-05-04 08:19:44 -0700 | [diff] [blame] | 156 | fCallback = VK_NULL_HANDLE; |
jvanverth | a00980e | 2016-05-02 13:24:48 -0700 | [diff] [blame] | 157 | } |
jvanverth | d2497f3 | 2016-03-18 12:39:05 -0700 | [diff] [blame] | 158 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
| 163 | void GrVkGpu::submitCommandBuffer(SyncQueue sync) { |
| 164 | SkASSERT(fCurrentCmdBuffer); |
| 165 | fCurrentCmdBuffer->end(this); |
| 166 | |
| 167 | fCurrentCmdBuffer->submitToQueue(this, fQueue, sync); |
| 168 | fResourceProvider.checkCommandBuffers(); |
| 169 | |
| 170 | // Release old command buffer and create a new one |
| 171 | fCurrentCmdBuffer->unref(this); |
| 172 | fCurrentCmdBuffer = fResourceProvider.createCommandBuffer(); |
| 173 | SkASSERT(fCurrentCmdBuffer); |
| 174 | |
| 175 | fCurrentCmdBuffer->begin(this); |
| 176 | } |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 179 | GrBuffer* GrVkGpu::onCreateBuffer(size_t size, GrBufferType type, GrAccessPattern accessPattern, |
| 180 | const void* data) { |
| 181 | GrBuffer* buff; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 182 | switch (type) { |
| 183 | case kVertex_GrBufferType: |
| 184 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 185 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 186 | buff = GrVkVertexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 187 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 188 | case kIndex_GrBufferType: |
| 189 | SkASSERT(kDynamic_GrAccessPattern == accessPattern || |
| 190 | kStatic_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 191 | buff = GrVkIndexBuffer::Create(this, size, kDynamic_GrAccessPattern == accessPattern); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 192 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 193 | case kXferCpuToGpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 194 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 195 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyRead_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 196 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 197 | case kXferGpuToCpu_GrBufferType: |
jvanverth | c3d706f | 2016-04-20 10:33:27 -0700 | [diff] [blame] | 198 | SkASSERT(kStream_GrAccessPattern == accessPattern); |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 199 | buff = GrVkTransferBuffer::Create(this, size, GrVkBuffer::kCopyWrite_Type); |
egdaniel | e05bbbb | 2016-04-19 12:13:41 -0700 | [diff] [blame] | 200 | break; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 201 | default: |
| 202 | SkFAIL("Unknown buffer type."); |
| 203 | return nullptr; |
| 204 | } |
cdalton | 1bf3e71 | 2016-04-19 10:00:02 -0700 | [diff] [blame] | 205 | if (data && buff) { |
| 206 | buff->updateData(data, size); |
| 207 | } |
| 208 | return buff; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | //////////////////////////////////////////////////////////////////////////////// |
| 212 | bool GrVkGpu::onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, |
| 213 | GrPixelConfig srcConfig, DrawPreference* drawPreference, |
| 214 | WritePixelTempDrawInfo* tempDrawInfo) { |
| 215 | if (kIndex_8_GrPixelConfig == srcConfig || GrPixelConfigIsCompressed(dstSurface->config())) { |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | // Currently we don't handle draws, so if the caller wants/needs to do a draw we need to fail |
| 220 | if (kNoDraw_DrawPreference != *drawPreference) { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | if (dstSurface->config() != srcConfig) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 225 | // TODO: This should fall back to drawing or copying to change config of dstSurface to |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 226 | // match that of srcConfig. |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool GrVkGpu::onWritePixels(GrSurface* surface, |
| 234 | int left, int top, int width, int height, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 235 | GrPixelConfig config, |
| 236 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 237 | GrVkTexture* vkTex = static_cast<GrVkTexture*>(surface->asTexture()); |
| 238 | if (!vkTex) { |
| 239 | return false; |
| 240 | } |
| 241 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 242 | // Make sure we have at least the base level |
jvanverth | 03509ea | 2016-03-02 13:19:47 -0800 | [diff] [blame] | 243 | if (texels.empty() || !texels.begin()->fPixels) { |
| 244 | return false; |
| 245 | } |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 246 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 247 | // We assume Vulkan doesn't do sRGB <-> linear conversions when reading and writing pixels. |
| 248 | if (GrPixelConfigIsSRGB(surface->config()) != GrPixelConfigIsSRGB(config)) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | bool success = false; |
| 253 | if (GrPixelConfigIsCompressed(vkTex->desc().fConfig)) { |
| 254 | // We check that config == desc.fConfig in GrGpu::getWritePixelsInfo() |
| 255 | SkASSERT(config == vkTex->desc().fConfig); |
| 256 | // TODO: add compressed texture support |
| 257 | // delete the following two lines and uncomment the two after that when ready |
| 258 | vkTex->unref(); |
| 259 | return false; |
| 260 | //success = this->uploadCompressedTexData(vkTex->desc(), buffer, false, left, top, width, |
| 261 | // height); |
| 262 | } else { |
| 263 | bool linearTiling = vkTex->isLinearTiled(); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 264 | if (linearTiling) { |
| 265 | if (texels.count() > 1) { |
| 266 | SkDebugf("Can't upload mipmap data to linear tiled texture"); |
| 267 | return false; |
| 268 | } |
| 269 | if (VK_IMAGE_LAYOUT_PREINITIALIZED != vkTex->currentLayout()) { |
| 270 | // Need to change the layout to general in order to perform a host write |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 271 | vkTex->setImageLayout(this, |
| 272 | VK_IMAGE_LAYOUT_GENERAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 273 | VK_ACCESS_HOST_WRITE_BIT, |
| 274 | VK_PIPELINE_STAGE_HOST_BIT, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 275 | false); |
egdaniel | bdf8811 | 2016-05-03 07:25:56 -0700 | [diff] [blame] | 276 | this->submitCommandBuffer(kForce_SyncQueue); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 277 | } |
| 278 | success = this->uploadTexDataLinear(vkTex, left, top, width, height, config, |
| 279 | texels.begin()->fPixels, texels.begin()->fRowBytes); |
| 280 | } else { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 281 | int newMipLevels = texels.count(); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 282 | int currentMipLevels = vkTex->texturePriv().maxMipMapLevel() + 1; |
| 283 | if (newMipLevels != currentMipLevels) { |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 284 | if (!vkTex->reallocForMipmap(this, newMipLevels)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 285 | return false; |
| 286 | } |
| 287 | } |
| 288 | success = this->uploadTexDataOptimal(vkTex, left, top, width, height, config, texels); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 289 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 290 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 291 | |
| 292 | return success; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 293 | } |
| 294 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 295 | bool GrVkGpu::uploadTexDataLinear(GrVkTexture* tex, |
| 296 | int left, int top, int width, int height, |
| 297 | GrPixelConfig dataConfig, |
| 298 | const void* data, |
| 299 | size_t rowBytes) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 300 | SkASSERT(data); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 301 | SkASSERT(tex->isLinearTiled()); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 302 | |
| 303 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 304 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 305 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 306 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 307 | |
| 308 | const GrSurfaceDesc& desc = tex->desc(); |
| 309 | |
| 310 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 311 | &width, &height, &data, &rowBytes)) { |
| 312 | return false; |
| 313 | } |
| 314 | size_t trimRowBytes = width * bpp; |
| 315 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 316 | SkASSERT(VK_IMAGE_LAYOUT_PREINITIALIZED == tex->currentLayout() || |
| 317 | VK_IMAGE_LAYOUT_GENERAL == tex->currentLayout()); |
| 318 | const VkImageSubresource subres = { |
| 319 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 320 | 0, // mipLevel |
| 321 | 0, // arraySlice |
| 322 | }; |
| 323 | VkSubresourceLayout layout; |
| 324 | VkResult err; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 325 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 326 | const GrVkInterface* interface = this->vkInterface(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 327 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 328 | GR_VK_CALL(interface, GetImageSubresourceLayout(fDevice, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 329 | tex->image(), |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 330 | &subres, |
| 331 | &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 332 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 333 | int texTop = kBottomLeft_GrSurfaceOrigin == desc.fOrigin ? tex->height() - top - height : top; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 334 | const GrVkAlloc& alloc = tex->alloc(); |
| 335 | VkDeviceSize offset = alloc.fOffset + texTop*layout.rowPitch + left*bpp; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 336 | VkDeviceSize size = height*layout.rowPitch; |
| 337 | void* mapPtr; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 338 | err = GR_VK_CALL(interface, MapMemory(fDevice, alloc.fMemory, offset, size, 0, &mapPtr)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 339 | if (err) { |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | if (kBottomLeft_GrSurfaceOrigin == desc.fOrigin) { |
| 344 | // copy into buffer by rows |
| 345 | const char* srcRow = reinterpret_cast<const char*>(data); |
| 346 | char* dstRow = reinterpret_cast<char*>(mapPtr)+(height - 1)*layout.rowPitch; |
| 347 | for (int y = 0; y < height; y++) { |
| 348 | memcpy(dstRow, srcRow, trimRowBytes); |
| 349 | srcRow += rowBytes; |
| 350 | dstRow -= layout.rowPitch; |
| 351 | } |
| 352 | } else { |
| 353 | // If there is no padding on the src (rowBytes) or dst (layout.rowPitch) we can memcpy |
| 354 | if (trimRowBytes == rowBytes && trimRowBytes == layout.rowPitch) { |
| 355 | memcpy(mapPtr, data, trimRowBytes * height); |
| 356 | } else { |
| 357 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), data, rowBytes, |
| 358 | trimRowBytes, height); |
| 359 | } |
| 360 | } |
| 361 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 362 | GR_VK_CALL(interface, UnmapMemory(fDevice, alloc.fMemory)); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 363 | |
| 364 | return true; |
| 365 | } |
| 366 | |
| 367 | bool GrVkGpu::uploadTexDataOptimal(GrVkTexture* tex, |
| 368 | int left, int top, int width, int height, |
| 369 | GrPixelConfig dataConfig, |
| 370 | const SkTArray<GrMipLevel>& texels) { |
| 371 | SkASSERT(!tex->isLinearTiled()); |
| 372 | // The assumption is either that we have no mipmaps, or that our rect is the entire texture |
| 373 | SkASSERT(1 == texels.count() || |
| 374 | (0 == left && 0 == top && width == tex->width() && height == tex->height())); |
| 375 | |
| 376 | // If we're uploading compressed data then we should be using uploadCompressedTexData |
| 377 | SkASSERT(!GrPixelConfigIsCompressed(dataConfig)); |
| 378 | |
| 379 | if (width == 0 || height == 0) { |
| 380 | return false; |
| 381 | } |
| 382 | |
| 383 | const GrSurfaceDesc& desc = tex->desc(); |
| 384 | SkASSERT(this->caps()->isConfigTexturable(desc.fConfig)); |
| 385 | size_t bpp = GrBytesPerPixel(dataConfig); |
| 386 | |
| 387 | // texels is const. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 388 | // But we may need to adjust the fPixels ptr based on the copyRect, or fRowBytes. |
| 389 | // Because of this we need to make a non-const shallow copy of texels. |
| 390 | SkTArray<GrMipLevel> texelsShallowCopy(texels); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 391 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 392 | for (int currentMipLevel = texelsShallowCopy.count() - 1; currentMipLevel >= 0; |
| 393 | currentMipLevel--) { |
| 394 | SkASSERT(texelsShallowCopy[currentMipLevel].fPixels); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 395 | } |
| 396 | |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 397 | // Determine whether we need to flip when we copy into the buffer |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 398 | bool flipY = (kBottomLeft_GrSurfaceOrigin == desc.fOrigin && !texelsShallowCopy.empty()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 399 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 400 | // adjust any params (left, top, currentWidth, currentHeight |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 401 | // find the combined size of all the mip levels and the relative offset of |
| 402 | // each into the collective buffer |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 403 | // Do the first level separately because we may need to adjust width and height |
| 404 | // (for the non-mipped case). |
| 405 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 406 | &width, |
| 407 | &height, |
| 408 | &texelsShallowCopy[0].fPixels, |
| 409 | &texelsShallowCopy[0].fRowBytes)) { |
| 410 | return false; |
| 411 | } |
| 412 | SkTArray<size_t> individualMipOffsets(texelsShallowCopy.count()); |
| 413 | individualMipOffsets.push_back(0); |
| 414 | size_t combinedBufferSize = width * bpp * height; |
| 415 | int currentWidth = width; |
| 416 | int currentHeight = height; |
| 417 | for (int currentMipLevel = 1; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) { |
| 418 | currentWidth = SkTMax(1, currentWidth/2); |
| 419 | currentHeight = SkTMax(1, currentHeight/2); |
| 420 | if (!GrSurfacePriv::AdjustWritePixelParams(desc.fWidth, desc.fHeight, bpp, &left, &top, |
| 421 | ¤tWidth, |
| 422 | ¤tHeight, |
| 423 | &texelsShallowCopy[currentMipLevel].fPixels, |
| 424 | &texelsShallowCopy[currentMipLevel].fRowBytes)) { |
| 425 | return false; |
| 426 | } |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 427 | const size_t trimmedSize = currentWidth * bpp * currentHeight; |
| 428 | individualMipOffsets.push_back(combinedBufferSize); |
| 429 | combinedBufferSize += trimmedSize; |
| 430 | } |
| 431 | |
| 432 | // allocate buffer to hold our mip data |
| 433 | GrVkTransferBuffer* transferBuffer = |
| 434 | GrVkTransferBuffer::Create(this, combinedBufferSize, GrVkBuffer::kCopyRead_Type); |
| 435 | |
| 436 | char* buffer = (char*) transferBuffer->map(); |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 437 | SkTArray<VkBufferImageCopy> regions(texelsShallowCopy.count()); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 438 | |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 439 | currentWidth = width; |
| 440 | currentHeight = height; |
| 441 | for (int currentMipLevel = 0; currentMipLevel < texelsShallowCopy.count(); currentMipLevel++) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 442 | const size_t trimRowBytes = currentWidth * bpp; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 443 | const size_t rowBytes = texelsShallowCopy[currentMipLevel].fRowBytes; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 444 | |
| 445 | // copy data into the buffer, skipping the trailing bytes |
| 446 | char* dst = buffer + individualMipOffsets[currentMipLevel]; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 447 | const char* src = (const char*)texelsShallowCopy[currentMipLevel].fPixels; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 448 | if (flipY) { |
| 449 | src += (currentHeight - 1) * rowBytes; |
| 450 | for (int y = 0; y < currentHeight; y++) { |
| 451 | memcpy(dst, src, trimRowBytes); |
| 452 | src -= rowBytes; |
| 453 | dst += trimRowBytes; |
| 454 | } |
| 455 | } else if (trimRowBytes == rowBytes) { |
| 456 | memcpy(dst, src, trimRowBytes * currentHeight); |
| 457 | } else { |
| 458 | SkRectMemcpy(dst, trimRowBytes, src, rowBytes, trimRowBytes, currentHeight); |
| 459 | } |
| 460 | |
| 461 | VkBufferImageCopy& region = regions.push_back(); |
| 462 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 463 | region.bufferOffset = individualMipOffsets[currentMipLevel]; |
| 464 | region.bufferRowLength = currentWidth; |
| 465 | region.bufferImageHeight = currentHeight; |
bsalomon | cf942c4 | 2016-04-29 18:30:06 -0700 | [diff] [blame] | 466 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, SkToU32(currentMipLevel), 0, 1 }; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 467 | region.imageOffset = { left, flipY ? tex->height() - top - currentHeight : top, 0 }; |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 468 | region.imageExtent = { (uint32_t)currentWidth, (uint32_t)currentHeight, 1 }; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 469 | |
| 470 | currentWidth = SkTMax(1, currentWidth/2); |
| 471 | currentHeight = SkTMax(1, currentHeight/2); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | transferBuffer->unmap(); |
| 475 | |
| 476 | // make sure the unmap has finished |
| 477 | transferBuffer->addMemoryBarrier(this, |
| 478 | VK_ACCESS_HOST_WRITE_BIT, |
| 479 | VK_ACCESS_TRANSFER_READ_BIT, |
| 480 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
| 481 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 482 | false); |
| 483 | |
| 484 | // Change layout of our target so it can be copied to |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 485 | tex->setImageLayout(this, |
| 486 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 487 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 488 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 489 | false); |
| 490 | |
| 491 | // Copy the buffer to the image |
| 492 | fCurrentCmdBuffer->copyBufferToImage(this, |
| 493 | transferBuffer, |
| 494 | tex, |
| 495 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 496 | regions.count(), |
| 497 | regions.begin()); |
| 498 | |
| 499 | // Submit the current command buffer to the Queue |
| 500 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 501 | |
| 502 | transferBuffer->unref(); |
| 503 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 504 | return true; |
| 505 | } |
| 506 | |
| 507 | //////////////////////////////////////////////////////////////////////////////// |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 508 | GrTexture* GrVkGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 509 | const SkTArray<GrMipLevel>& texels) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 510 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrSurfaceFlag); |
| 511 | |
| 512 | VkFormat pixelFormat; |
| 513 | if (!GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat)) { |
| 514 | return nullptr; |
| 515 | } |
| 516 | |
| 517 | if (!fVkCaps->isConfigTexturable(desc.fConfig)) { |
| 518 | return nullptr; |
| 519 | } |
| 520 | |
| 521 | bool linearTiling = false; |
| 522 | if (SkToBool(desc.fFlags & kZeroCopy_GrSurfaceFlag)) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 523 | // we can't have a linear texture with a mipmap |
| 524 | if (texels.count() > 1) { |
| 525 | SkDebugf("Trying to create linear tiled texture with mipmap"); |
| 526 | return nullptr; |
| 527 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 528 | if (fVkCaps->isConfigTexurableLinearly(desc.fConfig) && |
| 529 | (!renderTarget || fVkCaps->isConfigRenderableLinearly(desc.fConfig, false))) { |
| 530 | linearTiling = true; |
| 531 | } else { |
| 532 | return nullptr; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 537 | if (renderTarget) { |
| 538 | usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; |
| 539 | } |
| 540 | |
| 541 | // For now we will set the VK_IMAGE_USAGE_TRANSFER_DESTINATION_BIT and |
| 542 | // VK_IMAGE_USAGE_TRANSFER_SOURCE_BIT on every texture since we do not know whether or not we |
| 543 | // will be using this texture in some copy or not. Also this assumes, as is the current case, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 544 | // that all render targets in vulkan are also textures. If we change this practice of setting |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 545 | // both bits, we must make sure to set the destination bit if we are uploading srcData to the |
| 546 | // texture. |
| 547 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 548 | |
bsalomon | a1e6b3b | 2016-03-02 10:58:23 -0800 | [diff] [blame] | 549 | VkFlags memProps = (!texels.empty() && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 550 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 551 | |
| 552 | // This ImageDesc refers to the texture that will be read by the client. Thus even if msaa is |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 553 | // requested, this ImageDesc describes the resolved texture. Therefore we always have samples set |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 554 | // to 1. |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 555 | int mipLevels = texels.empty() ? 1 : texels.count(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 556 | GrVkImage::ImageDesc imageDesc; |
| 557 | imageDesc.fImageType = VK_IMAGE_TYPE_2D; |
| 558 | imageDesc.fFormat = pixelFormat; |
| 559 | imageDesc.fWidth = desc.fWidth; |
| 560 | imageDesc.fHeight = desc.fHeight; |
jvanverth | c578b063 | 2016-05-02 10:58:12 -0700 | [diff] [blame] | 561 | imageDesc.fLevels = linearTiling ? 1 : mipLevels; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 562 | imageDesc.fSamples = 1; |
| 563 | imageDesc.fImageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 564 | imageDesc.fUsageFlags = usageFlags; |
| 565 | imageDesc.fMemProps = memProps; |
| 566 | |
| 567 | GrVkTexture* tex; |
| 568 | if (renderTarget) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 569 | tex = GrVkTextureRenderTarget::CreateNewTextureRenderTarget(this, budgeted, desc, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 570 | imageDesc); |
| 571 | } else { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 572 | tex = GrVkTexture::CreateNewTexture(this, budgeted, desc, imageDesc); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (!tex) { |
| 576 | return nullptr; |
| 577 | } |
| 578 | |
bsalomon | e699d0c | 2016-03-09 06:25:15 -0800 | [diff] [blame] | 579 | if (!texels.empty()) { |
| 580 | SkASSERT(texels.begin()->fPixels); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 581 | bool success; |
| 582 | if (linearTiling) { |
| 583 | success = this->uploadTexDataLinear(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 584 | texels.begin()->fPixels, texels.begin()->fRowBytes); |
| 585 | } else { |
| 586 | success = this->uploadTexDataOptimal(tex, 0, 0, desc.fWidth, desc.fHeight, desc.fConfig, |
| 587 | texels); |
| 588 | } |
| 589 | if (!success) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 590 | tex->unref(); |
| 591 | return nullptr; |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | return tex; |
| 596 | } |
| 597 | |
| 598 | //////////////////////////////////////////////////////////////////////////////// |
| 599 | |
| 600 | static GrSurfaceOrigin resolve_origin(GrSurfaceOrigin origin) { |
| 601 | // By default, all textures in Vk use TopLeft |
| 602 | if (kDefault_GrSurfaceOrigin == origin) { |
| 603 | return kTopLeft_GrSurfaceOrigin; |
| 604 | } else { |
| 605 | return origin; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | GrTexture* GrVkGpu::onWrapBackendTexture(const GrBackendTextureDesc& desc, |
| 610 | GrWrapOwnership ownership) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 611 | if (0 == desc.fTextureHandle) { |
| 612 | return nullptr; |
| 613 | } |
| 614 | |
| 615 | int maxSize = this->caps()->maxTextureSize(); |
| 616 | if (desc.fWidth > maxSize || desc.fHeight > maxSize) { |
| 617 | return nullptr; |
| 618 | } |
| 619 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 620 | const GrVkImageInfo* info = reinterpret_cast<const GrVkImageInfo*>(desc.fTextureHandle); |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 621 | if (VK_NULL_HANDLE == info->fImage || VK_NULL_HANDLE == info->fAlloc.fMemory) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 622 | return nullptr; |
| 623 | } |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 624 | #ifdef SK_DEBUG |
| 625 | VkFormat format; |
| 626 | if (!GrPixelConfigToVkFormat(desc.fConfig, &format)) { |
| 627 | return nullptr; |
| 628 | } |
| 629 | SkASSERT(format == info->fFormat); |
| 630 | #endif |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 631 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 632 | GrSurfaceDesc surfDesc; |
| 633 | // next line relies on GrBackendTextureDesc's flags matching GrTexture's |
| 634 | surfDesc.fFlags = (GrSurfaceFlags)desc.fFlags; |
| 635 | surfDesc.fWidth = desc.fWidth; |
| 636 | surfDesc.fHeight = desc.fHeight; |
| 637 | surfDesc.fConfig = desc.fConfig; |
| 638 | surfDesc.fSampleCnt = SkTMin(desc.fSampleCnt, this->caps()->maxSampleCount()); |
| 639 | bool renderTarget = SkToBool(desc.fFlags & kRenderTarget_GrBackendTextureFlag); |
| 640 | // In GL, Chrome assumes all textures are BottomLeft |
| 641 | // In VK, we don't have this restriction |
| 642 | surfDesc.fOrigin = resolve_origin(desc.fOrigin); |
| 643 | |
| 644 | GrVkTexture* texture = nullptr; |
| 645 | if (renderTarget) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 646 | texture = GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(this, surfDesc, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 647 | ownership, info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 648 | } else { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 649 | texture = GrVkTexture::CreateWrappedTexture(this, surfDesc, ownership, info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 650 | } |
| 651 | if (!texture) { |
| 652 | return nullptr; |
| 653 | } |
| 654 | |
| 655 | return texture; |
| 656 | } |
| 657 | |
| 658 | GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDesc& wrapDesc, |
| 659 | GrWrapOwnership ownership) { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 660 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 661 | const GrVkImageInfo* info = |
| 662 | reinterpret_cast<const GrVkImageInfo*>(wrapDesc.fRenderTargetHandle); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 663 | if (VK_NULL_HANDLE == info->fImage || |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 664 | (VK_NULL_HANDLE == info->fAlloc.fMemory && kAdopt_GrWrapOwnership == ownership)) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 665 | return nullptr; |
| 666 | } |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 667 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 668 | GrSurfaceDesc desc; |
| 669 | desc.fConfig = wrapDesc.fConfig; |
| 670 | desc.fFlags = kCheckAllocation_GrSurfaceFlag; |
| 671 | desc.fWidth = wrapDesc.fWidth; |
| 672 | desc.fHeight = wrapDesc.fHeight; |
| 673 | desc.fSampleCnt = SkTMin(wrapDesc.fSampleCnt, this->caps()->maxSampleCount()); |
| 674 | |
| 675 | desc.fOrigin = resolve_origin(wrapDesc.fOrigin); |
| 676 | |
| 677 | GrVkRenderTarget* tgt = GrVkRenderTarget::CreateWrappedRenderTarget(this, desc, |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 678 | ownership, |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 679 | info); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 680 | if (tgt && wrapDesc.fStencilBits) { |
| 681 | if (!createStencilAttachmentForRenderTarget(tgt, desc.fWidth, desc.fHeight)) { |
| 682 | tgt->unref(); |
| 683 | return nullptr; |
| 684 | } |
| 685 | } |
| 686 | return tgt; |
| 687 | } |
| 688 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 689 | void GrVkGpu::generateMipmap(GrVkTexture* tex) const { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 690 | // don't do anything for linearly tiled textures (can't have mipmaps) |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 691 | if (tex->isLinearTiled()) { |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 692 | SkDebugf("Trying to create mipmap for linear tiled texture"); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 693 | return; |
| 694 | } |
| 695 | |
| 696 | // We cannot generate mipmaps for images that are multisampled. |
| 697 | // TODO: does it even make sense for rendertargets in general? |
| 698 | if (tex->asRenderTarget() && tex->asRenderTarget()->numColorSamples() > 1) { |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | // determine if we can blit to and from this format |
| 703 | const GrVkCaps& caps = this->vkCaps(); |
| 704 | if (!caps.configCanBeDstofBlit(tex->config(), false) || |
| 705 | !caps.configCanBeSrcofBlit(tex->config(), false)) { |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | // change the original image's layout |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 710 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 711 | VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 712 | |
| 713 | // grab handle to the original image resource |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 714 | const GrVkResource* oldResource = tex->resource(); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 715 | oldResource->ref(); |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 716 | VkImage oldImage = tex->image(); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 717 | |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 718 | // SkMipMap doesn't include the base level in the level count so we have to add 1 |
| 719 | uint32_t levelCount = SkMipMap::ComputeLevelCount(tex->width(), tex->height()) + 1; |
| 720 | if (!tex->reallocForMipmap(this, levelCount)) { |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 721 | oldResource->unref(this); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | // change the new image's layout |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 726 | tex->setImageLayout(this, VK_IMAGE_LAYOUT_GENERAL, |
| 727 | VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, false); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 728 | |
| 729 | // Blit original image |
| 730 | int width = tex->width(); |
| 731 | int height = tex->height(); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 732 | |
| 733 | VkImageBlit blitRegion; |
| 734 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 735 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 736 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
| 737 | blitRegion.srcOffsets[1] = { width, height, 0 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 738 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 739 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
| 740 | blitRegion.dstOffsets[1] = { width, height, 0 }; |
| 741 | |
| 742 | fCurrentCmdBuffer->blitImage(this, |
| 743 | oldResource, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 744 | oldImage, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 745 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 746 | tex->resource(), |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 747 | tex->image(), |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 748 | VK_IMAGE_LAYOUT_GENERAL, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 749 | 1, |
| 750 | &blitRegion, |
| 751 | VK_FILTER_LINEAR); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 752 | |
| 753 | // setup memory barrier |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 754 | SkASSERT(GrVkFormatToPixelConfig(tex->imageFormat(), nullptr)); |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 755 | VkImageAspectFlags aspectFlags = VK_IMAGE_ASPECT_COLOR_BIT; |
| 756 | VkImageMemoryBarrier imageMemoryBarrier = { |
| 757 | VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, // sType |
| 758 | NULL, // pNext |
| 759 | VK_ACCESS_TRANSFER_WRITE_BIT, // outputMask |
| 760 | VK_ACCESS_TRANSFER_READ_BIT, // inputMask |
| 761 | VK_IMAGE_LAYOUT_GENERAL, // oldLayout |
| 762 | VK_IMAGE_LAYOUT_GENERAL, // newLayout |
| 763 | VK_QUEUE_FAMILY_IGNORED, // srcQueueFamilyIndex |
| 764 | VK_QUEUE_FAMILY_IGNORED, // dstQueueFamilyIndex |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 765 | tex->image(), // image |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 766 | { aspectFlags, 0, 1, 0, 1 } // subresourceRange |
| 767 | }; |
| 768 | |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 769 | // Blit the miplevels |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 770 | uint32_t mipLevel = 1; |
| 771 | while (mipLevel < levelCount) { |
| 772 | int prevWidth = width; |
| 773 | int prevHeight = height; |
| 774 | width = SkTMax(1, width / 2); |
| 775 | height = SkTMax(1, height / 2); |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 776 | |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 777 | imageMemoryBarrier.subresourceRange.baseMipLevel = mipLevel - 1; |
| 778 | this->addImageMemoryBarrier(VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 779 | false, &imageMemoryBarrier); |
| 780 | |
| 781 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel - 1, 0, 1 }; |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 782 | blitRegion.srcOffsets[0] = { 0, 0, 0 }; |
| 783 | blitRegion.srcOffsets[1] = { prevWidth, prevHeight, 0 }; |
| 784 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, mipLevel, 0, 1 }; |
| 785 | blitRegion.dstOffsets[0] = { 0, 0, 0 }; |
| 786 | blitRegion.dstOffsets[1] = { width, height, 0 }; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 787 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 788 | *tex, |
| 789 | *tex, |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 790 | 1, |
| 791 | &blitRegion, |
| 792 | VK_FILTER_LINEAR); |
jvanverth | 82c0558 | 2016-05-03 11:19:01 -0700 | [diff] [blame] | 793 | ++mipLevel; |
jvanverth | 6234006 | 2016-04-26 08:01:44 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | oldResource->unref(this); |
| 797 | } |
| 798 | |
| 799 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 800 | //////////////////////////////////////////////////////////////////////////////// |
| 801 | |
| 802 | void GrVkGpu::bindGeometry(const GrPrimitiveProcessor& primProc, |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 803 | const GrNonInstancedMesh& mesh) { |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 804 | // There is no need to put any memory barriers to make sure host writes have finished here. |
| 805 | // When a command buffer is submitted to a queue, there is an implicit memory barrier that |
| 806 | // occurs for all host writes. Additionally, BufferMemoryBarriers are not allowed inside of |
| 807 | // an active RenderPass. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 808 | GrVkVertexBuffer* vbuf; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 809 | vbuf = (GrVkVertexBuffer*)mesh.vertexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 810 | SkASSERT(vbuf); |
| 811 | SkASSERT(!vbuf->isMapped()); |
| 812 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 813 | fCurrentCmdBuffer->bindVertexBuffer(this, vbuf); |
| 814 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 815 | if (mesh.isIndexed()) { |
| 816 | GrVkIndexBuffer* ibuf = (GrVkIndexBuffer*)mesh.indexBuffer(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 817 | SkASSERT(ibuf); |
| 818 | SkASSERT(!ibuf->isMapped()); |
| 819 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 820 | fCurrentCmdBuffer->bindIndexBuffer(this, ibuf); |
| 821 | } |
| 822 | } |
| 823 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 824 | //////////////////////////////////////////////////////////////////////////////// |
| 825 | |
| 826 | GrStencilAttachment* GrVkGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 827 | int width, |
| 828 | int height) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 829 | SkASSERT(width >= rt->width()); |
| 830 | SkASSERT(height >= rt->height()); |
| 831 | |
| 832 | int samples = rt->numStencilSamples(); |
| 833 | |
egdaniel | 8f1dcaa | 2016-04-01 10:10:45 -0700 | [diff] [blame] | 834 | const GrVkCaps::StencilFormat& sFmt = this->vkCaps().preferedStencilFormat(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 835 | |
| 836 | GrVkStencilAttachment* stencil(GrVkStencilAttachment::Create(this, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 837 | width, |
| 838 | height, |
| 839 | samples, |
| 840 | sFmt)); |
| 841 | fStats.incStencilAttachmentCreates(); |
| 842 | return stencil; |
| 843 | } |
| 844 | |
| 845 | //////////////////////////////////////////////////////////////////////////////// |
| 846 | |
| 847 | GrBackendObject GrVkGpu::createTestingOnlyBackendTexture(void* srcData, int w, int h, |
| 848 | GrPixelConfig config) { |
| 849 | |
| 850 | VkFormat pixelFormat; |
| 851 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 852 | return 0; |
| 853 | } |
| 854 | |
| 855 | bool linearTiling = false; |
| 856 | if (!fVkCaps->isConfigTexturable(config)) { |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | if (fVkCaps->isConfigTexurableLinearly(config)) { |
| 861 | linearTiling = true; |
| 862 | } |
| 863 | |
| 864 | // Currently this is not supported since it requires a copy which has not yet been implemented. |
| 865 | if (srcData && !linearTiling) { |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; |
| 870 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT; |
| 871 | usageFlags |= VK_IMAGE_USAGE_TRANSFER_DST_BIT; |
| 872 | |
| 873 | VkFlags memProps = (srcData && linearTiling) ? VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT : |
| 874 | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; |
| 875 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 876 | VkImage image = VK_NULL_HANDLE; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 877 | GrVkAlloc alloc = { VK_NULL_HANDLE, 0 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 878 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 879 | VkImageTiling imageTiling = linearTiling ? VK_IMAGE_TILING_LINEAR : VK_IMAGE_TILING_OPTIMAL; |
| 880 | VkImageLayout initialLayout = (VK_IMAGE_TILING_LINEAR == imageTiling) |
| 881 | ? VK_IMAGE_LAYOUT_PREINITIALIZED |
| 882 | : VK_IMAGE_LAYOUT_UNDEFINED; |
| 883 | |
| 884 | // Create Image |
| 885 | VkSampleCountFlagBits vkSamples; |
| 886 | if (!GrSampleCountToVkSampleCount(1, &vkSamples)) { |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | const VkImageCreateInfo imageCreateInfo = { |
| 891 | VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, // sType |
| 892 | NULL, // pNext |
| 893 | 0, // VkImageCreateFlags |
| 894 | VK_IMAGE_TYPE_2D, // VkImageType |
| 895 | pixelFormat, // VkFormat |
ethannicholas | 384b5e9 | 2016-03-25 11:04:06 -0700 | [diff] [blame] | 896 | { (uint32_t) w, (uint32_t) h, 1 }, // VkExtent3D |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 897 | 1, // mipLevels |
| 898 | 1, // arrayLayers |
| 899 | vkSamples, // samples |
| 900 | imageTiling, // VkImageTiling |
| 901 | usageFlags, // VkImageUsageFlags |
| 902 | VK_SHARING_MODE_EXCLUSIVE, // VkSharingMode |
| 903 | 0, // queueFamilyCount |
| 904 | 0, // pQueueFamilyIndices |
| 905 | initialLayout // initialLayout |
| 906 | }; |
| 907 | |
| 908 | GR_VK_CALL_ERRCHECK(this->vkInterface(), CreateImage(this->device(), &imageCreateInfo, nullptr, &image)); |
| 909 | |
| 910 | if (!GrVkMemory::AllocAndBindImageMemory(this, image, memProps, &alloc)) { |
| 911 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 912 | return 0; |
| 913 | } |
| 914 | |
| 915 | if (srcData) { |
| 916 | if (linearTiling) { |
| 917 | const VkImageSubresource subres = { |
| 918 | VK_IMAGE_ASPECT_COLOR_BIT, |
| 919 | 0, // mipLevel |
| 920 | 0, // arraySlice |
| 921 | }; |
| 922 | VkSubresourceLayout layout; |
| 923 | VkResult err; |
| 924 | |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 925 | VK_CALL(GetImageSubresourceLayout(fDevice, image, &subres, &layout)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 926 | |
| 927 | void* mapPtr; |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 928 | err = VK_CALL(MapMemory(fDevice, alloc.fMemory, alloc.fOffset, layout.rowPitch * h, |
| 929 | 0, &mapPtr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 930 | if (err) { |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 931 | GrVkMemory::FreeImageMemory(this, alloc); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 932 | VK_CALL(DestroyImage(this->device(), image, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 933 | return 0; |
| 934 | } |
| 935 | |
| 936 | size_t bpp = GrBytesPerPixel(config); |
| 937 | size_t rowCopyBytes = bpp * w; |
| 938 | // If there is no padding on dst (layout.rowPitch) we can do a single memcopy. |
| 939 | // This assumes the srcData comes in with no padding. |
| 940 | if (rowCopyBytes == layout.rowPitch) { |
| 941 | memcpy(mapPtr, srcData, rowCopyBytes * h); |
| 942 | } else { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 943 | SkRectMemcpy(mapPtr, static_cast<size_t>(layout.rowPitch), srcData, rowCopyBytes, |
| 944 | rowCopyBytes, h); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 945 | } |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 946 | VK_CALL(UnmapMemory(fDevice, alloc.fMemory)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 947 | } else { |
| 948 | // TODO: Add support for copying to optimal tiling |
| 949 | SkASSERT(false); |
| 950 | } |
| 951 | } |
| 952 | |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 953 | GrVkImageInfo* info = new GrVkImageInfo; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 954 | info->fImage = image; |
| 955 | info->fAlloc = alloc; |
| 956 | info->fImageTiling = imageTiling; |
| 957 | info->fImageLayout = initialLayout; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 958 | info->fFormat = pixelFormat; |
jvanverth | 2af0f1b | 2016-05-03 10:36:49 -0700 | [diff] [blame] | 959 | info->fLevelCount = 1; |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 960 | |
| 961 | return (GrBackendObject)info; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | bool GrVkGpu::isTestingOnlyBackendTexture(GrBackendObject id) const { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 965 | const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 966 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 967 | if (backend && backend->fImage && backend->fAlloc.fMemory) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 968 | VkMemoryRequirements req; |
| 969 | memset(&req, 0, sizeof(req)); |
| 970 | GR_VK_CALL(this->vkInterface(), GetImageMemoryRequirements(fDevice, |
| 971 | backend->fImage, |
| 972 | &req)); |
| 973 | // TODO: find a better check |
| 974 | // This will probably fail with a different driver |
| 975 | return (req.size > 0) && (req.size <= 8192 * 8192); |
| 976 | } |
| 977 | |
| 978 | return false; |
| 979 | } |
| 980 | |
| 981 | void GrVkGpu::deleteTestingOnlyBackendTexture(GrBackendObject id, bool abandon) { |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 982 | const GrVkImageInfo* backend = reinterpret_cast<const GrVkImageInfo*>(id); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 983 | |
| 984 | if (backend) { |
| 985 | if (!abandon) { |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 986 | // something in the command buffer may still be using this, so force submit |
| 987 | this->submitCommandBuffer(kForce_SyncQueue); |
| 988 | |
jvanverth | 1e305ba | 2016-06-01 09:39:15 -0700 | [diff] [blame] | 989 | GrVkMemory::FreeImageMemory(this, backend->fAlloc); |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 990 | VK_CALL(DestroyImage(this->device(), backend->fImage, nullptr)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 991 | } |
jvanverth | fd359ca | 2016-03-18 11:57:24 -0700 | [diff] [blame] | 992 | delete backend; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | |
| 996 | //////////////////////////////////////////////////////////////////////////////// |
| 997 | |
| 998 | void GrVkGpu::addMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 999 | VkPipelineStageFlags dstStageMask, |
| 1000 | bool byRegion, |
| 1001 | VkMemoryBarrier* barrier) const { |
| 1002 | SkASSERT(fCurrentCmdBuffer); |
| 1003 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1004 | srcStageMask, |
| 1005 | dstStageMask, |
| 1006 | byRegion, |
| 1007 | GrVkCommandBuffer::kMemory_BarrierType, |
| 1008 | barrier); |
| 1009 | } |
| 1010 | |
| 1011 | void GrVkGpu::addBufferMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1012 | VkPipelineStageFlags dstStageMask, |
| 1013 | bool byRegion, |
| 1014 | VkBufferMemoryBarrier* barrier) const { |
| 1015 | SkASSERT(fCurrentCmdBuffer); |
| 1016 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1017 | srcStageMask, |
| 1018 | dstStageMask, |
| 1019 | byRegion, |
| 1020 | GrVkCommandBuffer::kBufferMemory_BarrierType, |
| 1021 | barrier); |
| 1022 | } |
| 1023 | |
| 1024 | void GrVkGpu::addImageMemoryBarrier(VkPipelineStageFlags srcStageMask, |
| 1025 | VkPipelineStageFlags dstStageMask, |
| 1026 | bool byRegion, |
| 1027 | VkImageMemoryBarrier* barrier) const { |
| 1028 | SkASSERT(fCurrentCmdBuffer); |
| 1029 | fCurrentCmdBuffer->pipelineBarrier(this, |
| 1030 | srcStageMask, |
| 1031 | dstStageMask, |
| 1032 | byRegion, |
| 1033 | GrVkCommandBuffer::kImageMemory_BarrierType, |
| 1034 | barrier); |
| 1035 | } |
| 1036 | |
| 1037 | void GrVkGpu::finishDrawTarget() { |
| 1038 | // Submit the current command buffer to the Queue |
| 1039 | this->submitCommandBuffer(kSkip_SyncQueue); |
| 1040 | } |
| 1041 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1042 | void GrVkGpu::clearStencil(GrRenderTarget* target) { |
| 1043 | if (nullptr == target) { |
| 1044 | return; |
| 1045 | } |
| 1046 | GrStencilAttachment* stencil = target->renderTargetPriv().getStencilAttachment(); |
| 1047 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
| 1048 | |
| 1049 | |
| 1050 | VkClearDepthStencilValue vkStencilColor; |
| 1051 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 1052 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1053 | vkStencil->setImageLayout(this, |
| 1054 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1055 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1056 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1057 | false); |
| 1058 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1059 | VkImageSubresourceRange subRange; |
| 1060 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1061 | subRange.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1062 | subRange.baseMipLevel = 0; |
| 1063 | subRange.levelCount = 1; |
| 1064 | subRange.baseArrayLayer = 0; |
| 1065 | subRange.layerCount = 1; |
| 1066 | |
| 1067 | // TODO: I imagine that most times we want to clear a stencil it will be at the beginning of a |
| 1068 | // draw. Thus we should look into using the load op functions on the render pass to clear out |
| 1069 | // the stencil there. |
| 1070 | fCurrentCmdBuffer->clearDepthStencilImage(this, vkStencil, &vkStencilColor, 1, &subRange); |
| 1071 | } |
| 1072 | |
| 1073 | void GrVkGpu::onClearStencilClip(GrRenderTarget* target, const SkIRect& rect, bool insideClip) { |
| 1074 | SkASSERT(target); |
| 1075 | |
| 1076 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
| 1077 | GrStencilAttachment* sb = target->renderTargetPriv().getStencilAttachment(); |
| 1078 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)sb; |
| 1079 | |
| 1080 | // this should only be called internally when we know we have a |
| 1081 | // stencil buffer. |
| 1082 | SkASSERT(sb); |
| 1083 | int stencilBitCount = sb->bits(); |
| 1084 | |
| 1085 | // The contract with the callers does not guarantee that we preserve all bits in the stencil |
| 1086 | // during this clear. Thus we will clear the entire stencil to the desired value. |
| 1087 | |
| 1088 | VkClearDepthStencilValue vkStencilColor; |
| 1089 | memset(&vkStencilColor, 0, sizeof(VkClearDepthStencilValue)); |
| 1090 | if (insideClip) { |
| 1091 | vkStencilColor.stencil = (1 << (stencilBitCount - 1)); |
| 1092 | } else { |
| 1093 | vkStencilColor.stencil = 0; |
| 1094 | } |
| 1095 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1096 | vkStencil->setImageLayout(this, |
| 1097 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1098 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, |
| 1099 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1100 | false); |
| 1101 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1102 | // Change layout of our render target so it can be used as the color attachment. This is what |
| 1103 | // the render pass expects when it begins. |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1104 | vkRT->setImageLayout(this, |
| 1105 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1106 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 1107 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1108 | false); |
| 1109 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1110 | VkClearRect clearRect; |
| 1111 | // Flip rect if necessary |
| 1112 | SkIRect vkRect = rect; |
| 1113 | |
| 1114 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 1115 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 1116 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 1117 | } |
| 1118 | |
| 1119 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 1120 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
| 1121 | |
| 1122 | clearRect.baseArrayLayer = 0; |
| 1123 | clearRect.layerCount = 1; |
| 1124 | |
| 1125 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1126 | SkASSERT(renderPass); |
| 1127 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 1128 | |
| 1129 | uint32_t stencilIndex; |
| 1130 | SkAssertResult(renderPass->stencilAttachmentIndex(&stencilIndex)); |
| 1131 | |
| 1132 | VkClearAttachment attachment; |
| 1133 | attachment.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT; |
| 1134 | attachment.colorAttachment = 0; // this value shouldn't matter |
| 1135 | attachment.clearValue.depthStencil = vkStencilColor; |
| 1136 | |
| 1137 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 1138 | fCurrentCmdBuffer->endRenderPass(this); |
| 1139 | |
| 1140 | return; |
| 1141 | } |
| 1142 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1143 | void GrVkGpu::onClear(GrRenderTarget* target, const SkIRect& rect, GrColor color) { |
| 1144 | // parent class should never let us get here with no RT |
| 1145 | SkASSERT(target); |
| 1146 | |
| 1147 | VkClearColorValue vkColor; |
| 1148 | GrColorToRGBAFloat(color, vkColor.float32); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1149 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1150 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(target); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1151 | |
| 1152 | if (rect.width() != target->width() || rect.height() != target->height()) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1153 | vkRT->setImageLayout(this, |
| 1154 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1155 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 1156 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1157 | false); |
| 1158 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1159 | // If we are using a stencil attachment we also need to change its layout to what the render |
| 1160 | // pass is expecting. |
| 1161 | if (GrStencilAttachment* stencil = vkRT->renderTargetPriv().getStencilAttachment()) { |
| 1162 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1163 | vkStencil->setImageLayout(this, |
| 1164 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1165 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 1166 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, |
| 1167 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1168 | false); |
| 1169 | } |
| 1170 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1171 | VkClearRect clearRect; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1172 | // Flip rect if necessary |
| 1173 | SkIRect vkRect = rect; |
| 1174 | if (kBottomLeft_GrSurfaceOrigin == vkRT->origin()) { |
| 1175 | vkRect.fTop = vkRT->height() - rect.fBottom; |
| 1176 | vkRect.fBottom = vkRT->height() - rect.fTop; |
| 1177 | } |
| 1178 | clearRect.rect.offset = { vkRect.fLeft, vkRect.fTop }; |
| 1179 | clearRect.rect.extent = { (uint32_t)vkRect.width(), (uint32_t)vkRect.height() }; |
jvanverth | e77875a | 2016-03-04 15:16:25 -0800 | [diff] [blame] | 1180 | clearRect.baseArrayLayer = 0; |
| 1181 | clearRect.layerCount = 1; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1182 | |
| 1183 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1184 | SkASSERT(renderPass); |
| 1185 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
| 1186 | |
| 1187 | uint32_t colorIndex; |
| 1188 | SkAssertResult(renderPass->colorAttachmentIndex(&colorIndex)); |
| 1189 | |
| 1190 | VkClearAttachment attachment; |
| 1191 | attachment.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1192 | attachment.colorAttachment = colorIndex; |
| 1193 | attachment.clearValue.color = vkColor; |
| 1194 | |
| 1195 | fCurrentCmdBuffer->clearAttachments(this, 1, &attachment, 1, &clearRect); |
| 1196 | fCurrentCmdBuffer->endRenderPass(this); |
| 1197 | return; |
| 1198 | } |
| 1199 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1200 | vkRT->setImageLayout(this, |
| 1201 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1202 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1203 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1204 | false); |
| 1205 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1206 | VkImageSubresourceRange subRange; |
| 1207 | memset(&subRange, 0, sizeof(VkImageSubresourceRange)); |
| 1208 | subRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; |
| 1209 | subRange.baseMipLevel = 0; |
| 1210 | subRange.levelCount = 1; |
| 1211 | subRange.baseArrayLayer = 0; |
| 1212 | subRange.layerCount = 1; |
| 1213 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1214 | // In the future we may not actually be doing this type of clear at all. If we are inside a |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1215 | // render pass or doing a non full clear then we will use CmdClearColorAttachment. The more |
| 1216 | // common use case will be clearing an attachment at the start of a render pass, in which case |
| 1217 | // we will use the clear load ops. |
| 1218 | fCurrentCmdBuffer->clearColorImage(this, |
| 1219 | vkRT, |
| 1220 | &vkColor, |
| 1221 | 1, &subRange); |
| 1222 | } |
| 1223 | |
| 1224 | inline bool can_copy_image(const GrSurface* dst, |
| 1225 | const GrSurface* src, |
| 1226 | const GrVkGpu* gpu) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1227 | // Currently we don't support msaa |
| 1228 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1229 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1230 | return false; |
| 1231 | } |
| 1232 | |
| 1233 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1234 | // as image usage flags. |
| 1235 | if (src->origin() == dst->origin() && |
| 1236 | GrBytesPerPixel(src->config()) == GrBytesPerPixel(dst->config())) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1237 | return true; |
| 1238 | } |
| 1239 | |
| 1240 | // How does msaa play into this? If a VkTexture is multisampled, are we copying the multisampled |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1241 | // or the resolved image here? Im multisampled, Vulkan requires sample counts to be the same. |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1242 | |
| 1243 | return false; |
| 1244 | } |
| 1245 | |
| 1246 | void GrVkGpu::copySurfaceAsCopyImage(GrSurface* dst, |
| 1247 | GrSurface* src, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1248 | GrVkImage* dstImage, |
| 1249 | GrVkImage* srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1250 | const SkIRect& srcRect, |
| 1251 | const SkIPoint& dstPoint) { |
| 1252 | SkASSERT(can_copy_image(dst, src, this)); |
| 1253 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1254 | // These flags are for flushing/invalidating caches and for the dst image it doesn't matter if |
| 1255 | // the cache is flushed since it is only being written to. |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1256 | dstImage->setImageLayout(this, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1257 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1258 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1259 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1260 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1261 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1262 | srcImage->setImageLayout(this, |
| 1263 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1264 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1265 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1266 | false); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1267 | |
| 1268 | // Flip rect if necessary |
| 1269 | SkIRect srcVkRect = srcRect; |
| 1270 | int32_t dstY = dstPoint.fY; |
| 1271 | |
| 1272 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1273 | SkASSERT(kBottomLeft_GrSurfaceOrigin == dst->origin()); |
| 1274 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1275 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1276 | dstY = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1277 | } |
| 1278 | |
| 1279 | VkImageCopy copyRegion; |
| 1280 | memset(©Region, 0, sizeof(VkImageCopy)); |
| 1281 | copyRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1282 | copyRegion.srcOffset = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1283 | copyRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1284 | copyRegion.dstOffset = { dstPoint.fX, dstY, 0 }; |
egdaniel | c355bc8 | 2016-04-27 11:31:59 -0700 | [diff] [blame] | 1285 | // The depth value of the extent is ignored according the vulkan spec for 2D images. However, on |
| 1286 | // at least the nexus 5X it seems to be checking it. Thus as a working around we must have the |
| 1287 | // depth value be 1. |
| 1288 | copyRegion.extent = { (uint32_t)srcVkRect.width(), (uint32_t)srcVkRect.height(), 1 }; |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1289 | |
| 1290 | fCurrentCmdBuffer->copyImage(this, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1291 | srcImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1292 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1293 | dstImage, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1294 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
| 1295 | 1, |
| 1296 | ©Region); |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1297 | |
| 1298 | SkIRect dstRect = SkIRect::MakeXYWH(dstPoint.fX, dstPoint.fY, |
| 1299 | srcRect.width(), srcRect.height()); |
| 1300 | this->didWriteToSurface(dst, &dstRect); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1301 | } |
| 1302 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1303 | inline bool can_copy_as_blit(const GrSurface* dst, |
| 1304 | const GrSurface* src, |
| 1305 | const GrVkImage* dstImage, |
| 1306 | const GrVkImage* srcImage, |
| 1307 | const GrVkGpu* gpu) { |
| 1308 | // We require that all vulkan GrSurfaces have been created with transfer_dst and transfer_src |
| 1309 | // as image usage flags. |
| 1310 | const GrVkCaps& caps = gpu->vkCaps(); |
| 1311 | if (!caps.configCanBeDstofBlit(dst->config(), dstImage->isLinearTiled()) || |
| 1312 | !caps.configCanBeSrcofBlit(src->config(), srcImage->isLinearTiled())) { |
| 1313 | return false; |
| 1314 | } |
| 1315 | |
| 1316 | // We cannot blit images that are multisampled. Will need to figure out if we can blit the |
| 1317 | // resolved msaa though. |
| 1318 | if ((dst->asRenderTarget() && dst->asRenderTarget()->numColorSamples() > 1) || |
| 1319 | (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1)) { |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | void GrVkGpu::copySurfaceAsBlit(GrSurface* dst, |
| 1327 | GrSurface* src, |
| 1328 | GrVkImage* dstImage, |
| 1329 | GrVkImage* srcImage, |
| 1330 | const SkIRect& srcRect, |
| 1331 | const SkIPoint& dstPoint) { |
| 1332 | SkASSERT(can_copy_as_blit(dst, src, dstImage, srcImage, this)); |
| 1333 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1334 | dstImage->setImageLayout(this, |
| 1335 | VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1336 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1337 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1338 | false); |
| 1339 | |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1340 | srcImage->setImageLayout(this, |
| 1341 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1342 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1343 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1344 | false); |
| 1345 | |
| 1346 | // Flip rect if necessary |
| 1347 | SkIRect srcVkRect; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1348 | srcVkRect.fLeft = srcRect.fLeft; |
| 1349 | srcVkRect.fRight = srcRect.fRight; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1350 | SkIRect dstRect; |
| 1351 | dstRect.fLeft = dstPoint.fX; |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1352 | dstRect.fRight = dstPoint.fX + srcRect.width(); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1353 | |
| 1354 | if (kBottomLeft_GrSurfaceOrigin == src->origin()) { |
| 1355 | srcVkRect.fTop = src->height() - srcRect.fBottom; |
| 1356 | srcVkRect.fBottom = src->height() - srcRect.fTop; |
| 1357 | } else { |
egdaniel | 8af936d | 2016-04-07 10:17:47 -0700 | [diff] [blame] | 1358 | srcVkRect.fTop = srcRect.fTop; |
| 1359 | srcVkRect.fBottom = srcRect.fBottom; |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | if (kBottomLeft_GrSurfaceOrigin == dst->origin()) { |
| 1363 | dstRect.fTop = dst->height() - dstPoint.fY - srcVkRect.height(); |
| 1364 | } else { |
| 1365 | dstRect.fTop = dstPoint.fY; |
| 1366 | } |
| 1367 | dstRect.fBottom = dstRect.fTop + srcVkRect.height(); |
| 1368 | |
| 1369 | // If we have different origins, we need to flip the top and bottom of the dst rect so that we |
| 1370 | // get the correct origintation of the copied data. |
| 1371 | if (src->origin() != dst->origin()) { |
| 1372 | SkTSwap(dstRect.fTop, dstRect.fBottom); |
| 1373 | } |
| 1374 | |
| 1375 | VkImageBlit blitRegion; |
| 1376 | memset(&blitRegion, 0, sizeof(VkImageBlit)); |
| 1377 | blitRegion.srcSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1378 | blitRegion.srcOffsets[0] = { srcVkRect.fLeft, srcVkRect.fTop, 0 }; |
| 1379 | blitRegion.srcOffsets[1] = { srcVkRect.fRight, srcVkRect.fBottom, 0 }; |
| 1380 | blitRegion.dstSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1381 | blitRegion.dstOffsets[0] = { dstRect.fLeft, dstRect.fTop, 0 }; |
| 1382 | blitRegion.dstOffsets[1] = { dstRect.fRight, dstRect.fBottom, 0 }; |
| 1383 | |
| 1384 | fCurrentCmdBuffer->blitImage(this, |
egdaniel | b2df0c2 | 2016-05-13 11:30:37 -0700 | [diff] [blame] | 1385 | *srcImage, |
| 1386 | *dstImage, |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1387 | 1, |
| 1388 | &blitRegion, |
| 1389 | VK_FILTER_NEAREST); // We never scale so any filter works here |
jvanverth | 900bd4a | 2016-04-29 13:53:12 -0700 | [diff] [blame] | 1390 | |
| 1391 | this->didWriteToSurface(dst, &dstRect); |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1394 | inline bool can_copy_as_draw(const GrSurface* dst, |
| 1395 | const GrSurface* src, |
| 1396 | const GrVkGpu* gpu) { |
| 1397 | return false; |
| 1398 | } |
| 1399 | |
| 1400 | void GrVkGpu::copySurfaceAsDraw(GrSurface* dst, |
| 1401 | GrSurface* src, |
| 1402 | const SkIRect& srcRect, |
| 1403 | const SkIPoint& dstPoint) { |
| 1404 | SkASSERT(false); |
| 1405 | } |
| 1406 | |
| 1407 | bool GrVkGpu::onCopySurface(GrSurface* dst, |
| 1408 | GrSurface* src, |
| 1409 | const SkIRect& srcRect, |
| 1410 | const SkIPoint& dstPoint) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1411 | GrVkImage* dstImage; |
| 1412 | GrVkImage* srcImage; |
| 1413 | if (dst->asTexture()) { |
| 1414 | dstImage = static_cast<GrVkTexture*>(dst->asTexture()); |
| 1415 | } else { |
| 1416 | SkASSERT(dst->asRenderTarget()); |
| 1417 | dstImage = static_cast<GrVkRenderTarget*>(dst->asRenderTarget()); |
| 1418 | } |
| 1419 | if (src->asTexture()) { |
| 1420 | srcImage = static_cast<GrVkTexture*>(src->asTexture()); |
| 1421 | } else { |
| 1422 | SkASSERT(src->asRenderTarget()); |
| 1423 | srcImage = static_cast<GrVkRenderTarget*>(src->asRenderTarget()); |
| 1424 | } |
| 1425 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1426 | if (can_copy_image(dst, src, this)) { |
egdaniel | 17b8925 | 2016-04-05 07:23:38 -0700 | [diff] [blame] | 1427 | this->copySurfaceAsCopyImage(dst, src, dstImage, srcImage, srcRect, dstPoint); |
| 1428 | return true; |
| 1429 | } |
| 1430 | |
| 1431 | if (can_copy_as_blit(dst, src, dstImage, srcImage, this)) { |
| 1432 | this->copySurfaceAsBlit(dst, src, dstImage, srcImage, srcRect, dstPoint); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1433 | return true; |
| 1434 | } |
| 1435 | |
| 1436 | if (can_copy_as_draw(dst, src, this)) { |
| 1437 | this->copySurfaceAsDraw(dst, src, srcRect, dstPoint); |
| 1438 | return true; |
| 1439 | } |
| 1440 | |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
egdaniel | 37798fb | 2016-04-12 07:31:49 -0700 | [diff] [blame] | 1444 | bool GrVkGpu::initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const { |
| 1445 | // Currently we don't support msaa |
| 1446 | if (src->asRenderTarget() && src->asRenderTarget()->numColorSamples() > 1) { |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
| 1450 | // This will support copying the dst as CopyImage since all of our surfaces require transferSrc |
| 1451 | // and transferDst usage flags in Vulkan. |
| 1452 | desc->fOrigin = src->origin(); |
| 1453 | desc->fConfig = src->config(); |
| 1454 | desc->fFlags = kNone_GrSurfaceFlags; |
| 1455 | return true; |
| 1456 | } |
| 1457 | |
cdalton | 28f45b9 | 2016-03-07 13:58:26 -0800 | [diff] [blame] | 1458 | void GrVkGpu::onGetMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&, |
| 1459 | int* effectiveSampleCnt, SkAutoTDeleteArray<SkPoint>*) { |
| 1460 | // TODO: stub. |
| 1461 | SkASSERT(!this->caps()->sampleLocationsSupport()); |
| 1462 | *effectiveSampleCnt = rt->desc().fSampleCnt; |
| 1463 | } |
| 1464 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1465 | bool GrVkGpu::onGetReadPixelsInfo(GrSurface* srcSurface, int width, int height, size_t rowBytes, |
| 1466 | GrPixelConfig readConfig, DrawPreference* drawPreference, |
| 1467 | ReadPixelTempDrawInfo* tempDrawInfo) { |
| 1468 | // Currently we don't handle draws, so if the caller wants/needs to do a draw we need to fail |
| 1469 | if (kNoDraw_DrawPreference != *drawPreference) { |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | if (srcSurface->config() != readConfig) { |
| 1474 | // TODO: This should fall back to drawing or copying to change config of srcSurface to match |
| 1475 | // that of readConfig. |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | return true; |
| 1480 | } |
| 1481 | |
| 1482 | bool GrVkGpu::onReadPixels(GrSurface* surface, |
| 1483 | int left, int top, int width, int height, |
| 1484 | GrPixelConfig config, |
| 1485 | void* buffer, |
| 1486 | size_t rowBytes) { |
| 1487 | VkFormat pixelFormat; |
| 1488 | if (!GrPixelConfigToVkFormat(config, &pixelFormat)) { |
| 1489 | return false; |
| 1490 | } |
| 1491 | |
| 1492 | GrVkTexture* tgt = static_cast<GrVkTexture*>(surface->asTexture()); |
| 1493 | if (!tgt) { |
| 1494 | return false; |
| 1495 | } |
| 1496 | |
| 1497 | // Change layout of our target so it can be used as copy |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1498 | tgt->setImageLayout(this, |
| 1499 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1500 | VK_ACCESS_TRANSFER_READ_BIT, |
| 1501 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1502 | false); |
| 1503 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 1504 | GrVkTransferBuffer* transferBuffer = |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 1505 | static_cast<GrVkTransferBuffer*>(this->createBuffer(rowBytes * height, |
| 1506 | kXferGpuToCpu_GrBufferType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 1507 | kStream_GrAccessPattern)); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1508 | |
| 1509 | bool flipY = kBottomLeft_GrSurfaceOrigin == surface->origin(); |
| 1510 | VkOffset3D offset = { |
| 1511 | left, |
| 1512 | flipY ? surface->height() - top - height : top, |
| 1513 | 0 |
| 1514 | }; |
| 1515 | |
| 1516 | // Copy the image to a buffer so we can map it to cpu memory |
| 1517 | VkBufferImageCopy region; |
| 1518 | memset(®ion, 0, sizeof(VkBufferImageCopy)); |
| 1519 | region.bufferOffset = 0; |
| 1520 | region.bufferRowLength = 0; // Forces RowLength to be imageExtent.width |
| 1521 | region.bufferImageHeight = 0; // Forces height to be tightly packed. Only useful for 3d images. |
| 1522 | region.imageSubresource = { VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1 }; |
| 1523 | region.imageOffset = offset; |
| 1524 | region.imageExtent = { (uint32_t)width, (uint32_t)height, 1 }; |
| 1525 | |
| 1526 | fCurrentCmdBuffer->copyImageToBuffer(this, |
| 1527 | tgt, |
| 1528 | VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, |
| 1529 | transferBuffer, |
| 1530 | 1, |
| 1531 | ®ion); |
| 1532 | |
| 1533 | // make sure the copy to buffer has finished |
| 1534 | transferBuffer->addMemoryBarrier(this, |
| 1535 | VK_ACCESS_TRANSFER_WRITE_BIT, |
| 1536 | VK_ACCESS_HOST_READ_BIT, |
| 1537 | VK_PIPELINE_STAGE_TRANSFER_BIT, |
| 1538 | VK_PIPELINE_STAGE_HOST_BIT, |
| 1539 | false); |
| 1540 | |
| 1541 | // We need to submit the current command buffer to the Queue and make sure it finishes before |
| 1542 | // we can copy the data out of the buffer. |
| 1543 | this->submitCommandBuffer(kForce_SyncQueue); |
| 1544 | |
| 1545 | void* mappedMemory = transferBuffer->map(); |
| 1546 | |
| 1547 | memcpy(buffer, mappedMemory, rowBytes*height); |
| 1548 | |
| 1549 | transferBuffer->unmap(); |
| 1550 | transferBuffer->unref(); |
| 1551 | |
| 1552 | if (flipY) { |
| 1553 | SkAutoSMalloc<32 * sizeof(GrColor)> scratch; |
| 1554 | size_t tightRowBytes = GrBytesPerPixel(config) * width; |
| 1555 | scratch.reset(tightRowBytes); |
| 1556 | void* tmpRow = scratch.get(); |
| 1557 | // flip y in-place by rows |
| 1558 | const int halfY = height >> 1; |
| 1559 | char* top = reinterpret_cast<char*>(buffer); |
| 1560 | char* bottom = top + (height - 1) * rowBytes; |
| 1561 | for (int y = 0; y < halfY; y++) { |
| 1562 | memcpy(tmpRow, top, tightRowBytes); |
| 1563 | memcpy(top, bottom, tightRowBytes); |
| 1564 | memcpy(bottom, tmpRow, tightRowBytes); |
| 1565 | top += rowBytes; |
| 1566 | bottom -= rowBytes; |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | return true; |
| 1571 | } |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1572 | sk_sp<GrVkPipelineState> GrVkGpu::prepareDrawState(const GrPipeline& pipeline, |
| 1573 | const GrPrimitiveProcessor& primProc, |
| 1574 | GrPrimitiveType primitiveType, |
| 1575 | const GrVkRenderPass& renderPass) { |
| 1576 | sk_sp<GrVkPipelineState> pipelineState = |
| 1577 | fResourceProvider.findOrCreateCompatiblePipelineState(pipeline, |
| 1578 | primProc, |
| 1579 | primitiveType, |
| 1580 | renderPass); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1581 | if (!pipelineState) { |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1582 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1583 | } |
| 1584 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1585 | pipelineState->setData(this, primProc, pipeline); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1586 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1587 | pipelineState->bind(this, fCurrentCmdBuffer); |
egdaniel | 470d77a | 2016-03-18 12:50:27 -0700 | [diff] [blame] | 1588 | |
| 1589 | GrVkPipeline::SetDynamicState(this, fCurrentCmdBuffer, pipeline); |
| 1590 | |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1591 | return pipelineState; |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | void GrVkGpu::onDraw(const GrPipeline& pipeline, |
| 1595 | const GrPrimitiveProcessor& primProc, |
| 1596 | const GrMesh* meshes, |
| 1597 | int meshCount) { |
| 1598 | if (!meshCount) { |
| 1599 | return; |
| 1600 | } |
| 1601 | GrRenderTarget* rt = pipeline.getRenderTarget(); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1602 | GrVkRenderTarget* vkRT = static_cast<GrVkRenderTarget*>(rt); |
| 1603 | const GrVkRenderPass* renderPass = vkRT->simpleRenderPass(); |
| 1604 | SkASSERT(renderPass); |
| 1605 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1606 | GrPrimitiveType primitiveType = meshes[0].primitiveType(); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1607 | sk_sp<GrVkPipelineState> pipelineState = this->prepareDrawState(pipeline, |
| 1608 | primProc, |
| 1609 | primitiveType, |
| 1610 | *renderPass); |
| 1611 | if (!pipelineState) { |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1612 | return; |
| 1613 | } |
| 1614 | |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1615 | // Change layout of our render target so it can be used as the color attachment |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1616 | vkRT->setImageLayout(this, |
| 1617 | VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1618 | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, |
| 1619 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1620 | false); |
| 1621 | |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1622 | // If we are using a stencil attachment we also need to update its layout |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1623 | if (GrStencilAttachment* stencil = vkRT->renderTargetPriv().getStencilAttachment()) { |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1624 | GrVkStencilAttachment* vkStencil = (GrVkStencilAttachment*)stencil; |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1625 | vkStencil->setImageLayout(this, |
| 1626 | VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, |
jvanverth | 50c46c7 | 2016-05-06 12:31:28 -0700 | [diff] [blame] | 1627 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT | |
| 1628 | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT, |
| 1629 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, |
egdaniel | 3d5d9ac | 2016-03-01 12:56:15 -0800 | [diff] [blame] | 1630 | false); |
| 1631 | } |
| 1632 | |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 1633 | fCurrentCmdBuffer->beginRenderPass(this, renderPass, *vkRT); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1634 | |
| 1635 | for (int i = 0; i < meshCount; ++i) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1636 | const GrMesh& mesh = meshes[i]; |
| 1637 | GrMesh::Iterator iter; |
| 1638 | const GrNonInstancedMesh* nonIdxMesh = iter.init(mesh); |
| 1639 | do { |
| 1640 | if (nonIdxMesh->primitiveType() != primitiveType) { |
| 1641 | // Technically we don't have to call this here (since there is a safety check in |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1642 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1643 | // pipelineState sits in a cache for a while. |
| 1644 | pipelineState->freeTempResources(this); |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1645 | SkDEBUGCODE(pipelineState = nullptr); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1646 | primitiveType = nonIdxMesh->primitiveType(); |
egdaniel | af13277 | 2016-03-28 12:39:29 -0700 | [diff] [blame] | 1647 | pipelineState = this->prepareDrawState(pipeline, |
| 1648 | primProc, |
| 1649 | primitiveType, |
| 1650 | *renderPass); |
| 1651 | if (!pipelineState) { |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1652 | return; |
| 1653 | } |
| 1654 | } |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1655 | SkASSERT(pipelineState); |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 1656 | this->bindGeometry(primProc, *nonIdxMesh); |
| 1657 | |
| 1658 | if (nonIdxMesh->isIndexed()) { |
| 1659 | fCurrentCmdBuffer->drawIndexed(this, |
| 1660 | nonIdxMesh->indexCount(), |
| 1661 | 1, |
| 1662 | nonIdxMesh->startIndex(), |
| 1663 | nonIdxMesh->startVertex(), |
| 1664 | 0); |
| 1665 | } else { |
| 1666 | fCurrentCmdBuffer->draw(this, |
| 1667 | nonIdxMesh->vertexCount(), |
| 1668 | 1, |
| 1669 | nonIdxMesh->startVertex(), |
| 1670 | 0); |
| 1671 | } |
| 1672 | |
| 1673 | fStats.incNumDraws(); |
| 1674 | } while ((nonIdxMesh = iter.next())); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | fCurrentCmdBuffer->endRenderPass(this); |
| 1678 | |
egdaniel | 22281c1 | 2016-03-23 13:49:40 -0700 | [diff] [blame] | 1679 | // Technically we don't have to call this here (since there is a safety check in |
| 1680 | // pipelineState:setData but this will allow for quicker freeing of resources if the |
| 1681 | // pipelineState sits in a cache for a while. |
| 1682 | pipelineState->freeTempResources(this); |
Greg Daniel | 164a9f0 | 2016-02-22 09:56:40 -0500 | [diff] [blame] | 1683 | |
| 1684 | #if SWAP_PER_DRAW |
| 1685 | glFlush(); |
| 1686 | #if defined(SK_BUILD_FOR_MAC) |
| 1687 | aglSwapBuffers(aglGetCurrentContext()); |
| 1688 | int set_a_break_pt_here = 9; |
| 1689 | aglSwapBuffers(aglGetCurrentContext()); |
| 1690 | #elif defined(SK_BUILD_FOR_WIN32) |
| 1691 | SwapBuf(); |
| 1692 | int set_a_break_pt_here = 9; |
| 1693 | SwapBuf(); |
| 1694 | #endif |
| 1695 | #endif |
| 1696 | } |