Update RT views and framebuffer in vulkan after mipmaping
authoregdaniel <egdaniel@google.com>
Wed, 13 Jul 2016 21:23:26 +0000 (14:23 -0700)
committerCommit bot <commit-bot@chromium.org>
Wed, 13 Jul 2016 21:23:26 +0000 (14:23 -0700)
I've also changed it so all attachment views (texture, color, and resolve) are created separately and not shared with each other. This just added a lot more complexity than we were probably even saving in time.

A quick fix to make sure we don't reuse keys in resource tracking also
got merged into this change.

BUG=skia:5223
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2146103002

Review-Url: https://codereview.chromium.org/2146103002

12 files changed:
src/gpu/vk/GrVkBuffer.h
src/gpu/vk/GrVkGpu.cpp
src/gpu/vk/GrVkGpu.h
src/gpu/vk/GrVkGpuCommandBuffer.cpp
src/gpu/vk/GrVkRenderTarget.cpp
src/gpu/vk/GrVkRenderTarget.h
src/gpu/vk/GrVkResource.h
src/gpu/vk/GrVkResourceProvider.cpp
src/gpu/vk/GrVkTexture.cpp
src/gpu/vk/GrVkTexture.h
src/gpu/vk/GrVkTextureRenderTarget.cpp
src/gpu/vk/GrVkTextureRenderTarget.h

index 966b8102e578356affecf18f8c73b499d1130018..232de176ce3eca9b672f55f5eee8e9d046a02363 100644 (file)
@@ -68,7 +68,7 @@ protected:
         Type               fType;
 
     private:
-        void freeGPUData(const GrVkGpu* gpu) const;
+        void freeGPUData(const GrVkGpu* gpu) const override;
 
         typedef GrVkResource INHERITED;
     };
index b995ed91aa10303d4a6cedc58b4d116611565ed9..76d50b0959beaaface643b798ebb55b25f7e0ee8 100644 (file)
@@ -770,7 +770,7 @@ GrRenderTarget* GrVkGpu::onWrapBackendRenderTarget(const GrBackendRenderTargetDe
     return tgt;
 }
 
-void GrVkGpu::generateMipmap(GrVkTexture* tex) const {
+void GrVkGpu::generateMipmap(GrVkTexture* tex) {
     // don't do anything for linearly tiled textures (can't have mipmaps)
     if (tex->isLinearTiled()) {
         SkDebugf("Trying to create mipmap for linear tiled texture");
index a0530f46a4c2b518b81e60e32e2d01fa334d8731..37d4e4f1e89ae63c46e2f85ae4e6a8bf48dfdc87 100644 (file)
@@ -136,7 +136,7 @@ public:
 
     void finishDrawTarget() override;
 
-    void generateMipmap(GrVkTexture* tex) const;
+    void generateMipmap(GrVkTexture* tex);
 
     bool updateBuffer(GrVkBuffer* buffer, const void* src, VkDeviceSize offset, VkDeviceSize size);
 
index 351fbbf53607c14a2125a35ba976b570efd96f69..b2336495a9ce837c70bd0ee06723c87d600a98fd 100644 (file)
@@ -337,7 +337,7 @@ sk_sp<GrVkPipelineState> GrVkGpuCommandBuffer::prepareDrawState(
 }
 
 static void append_sampled_images(const GrProcessor& processor,
-                                  const GrVkGpu* gpu,
+                                  GrVkGpu* gpu,
                                   SkTArray<GrVkImage*>* sampledImages) {
     if (int numTextures = processor.numTextures()) {
         GrVkImage** images = sampledImages->push_back_n(numTextures);
index 708af7970e3b32d911a548aa71ba7688d1b35209..83a4b44a59562e5d3fae11bc21260951dcb2f542 100644 (file)
@@ -33,10 +33,10 @@ GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
     , GrVkImage(info, wrapped)
     // for the moment we only support 1:1 color to stencil
     , GrRenderTarget(gpu, desc, kUnified_SampleConfig)
-    , fFramebuffer(nullptr)
     , fColorAttachmentView(colorAttachmentView)
     , fMSAAImage(new GrVkImage(msaaInfo, GrVkImage::kNot_Wrapped))
     , fResolveAttachmentView(resolveAttachmentView)
+    , fFramebuffer(nullptr)
     , fCachedSimpleRenderPass(nullptr) {
     SkASSERT(desc.fSampleCnt);
     // The plus 1 is to account for the resolve texture.
@@ -58,10 +58,10 @@ GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
     , GrVkImage(info, wrapped)
     // for the moment we only support 1:1 color to stencil
     , GrRenderTarget(gpu, desc, kUnified_SampleConfig)
-    , fFramebuffer(nullptr)
     , fColorAttachmentView(colorAttachmentView)
     , fMSAAImage(new GrVkImage(msaaInfo, GrVkImage::kNot_Wrapped))
     , fResolveAttachmentView(resolveAttachmentView)
+    , fFramebuffer(nullptr)
     , fCachedSimpleRenderPass(nullptr) {
     SkASSERT(desc.fSampleCnt);
     // The plus 1 is to account for the resolve texture.
@@ -80,10 +80,10 @@ GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
     : GrSurface(gpu, desc)
     , GrVkImage(info, wrapped)
     , GrRenderTarget(gpu, desc, kUnified_SampleConfig)
-    , fFramebuffer(nullptr)
     , fColorAttachmentView(colorAttachmentView)
     , fMSAAImage(nullptr)
     , fResolveAttachmentView(nullptr)
+    , fFramebuffer(nullptr)
     , fCachedSimpleRenderPass(nullptr) {
     SkASSERT(!desc.fSampleCnt);
     fColorValuesPerPixel = 1;
@@ -101,10 +101,10 @@ GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
     : GrSurface(gpu, desc)
     , GrVkImage(info, wrapped)
     , GrRenderTarget(gpu, desc, kUnified_SampleConfig)
-    , fFramebuffer(nullptr)
     , fColorAttachmentView(colorAttachmentView)
     , fMSAAImage(nullptr)
     , fResolveAttachmentView(nullptr)
+    , fFramebuffer(nullptr)
     , fCachedSimpleRenderPass(nullptr) {
     SkASSERT(!desc.fSampleCnt);
     fColorValuesPerPixel = 1;
@@ -117,6 +117,7 @@ GrVkRenderTarget::Create(GrVkGpu* gpu,
                          const GrSurfaceDesc& desc,
                          const GrVkImageInfo& info,
                          GrVkImage::Wrapped wrapped) {
+    SkASSERT(1 == info.fLevelCount);
     VkFormat pixelFormat;
     GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat);
 
index 9628e3bdce8878a82ba5d599232bbae4ed5eb9ea..019f56743322c50d03193f1d013f03a1add40be2 100644 (file)
@@ -103,6 +103,12 @@ protected:
         return fColorValuesPerPixel * fDesc.fWidth * fDesc.fHeight * colorBytes;
     }
 
+    void createFramebuffer(GrVkGpu* gpu);
+
+    const GrVkImageView*       fColorAttachmentView;
+    GrVkImage*                 fMSAAImage;
+    const GrVkImageView*       fResolveAttachmentView;
+
 private:
     GrVkRenderTarget(GrVkGpu* gpu,
                      SkBudgeted,
@@ -125,15 +131,10 @@ private:
 
     bool completeStencilAttachment() override;
 
-    void createFramebuffer(GrVkGpu* gpu);
-
     void releaseInternalObjects();
     void abandonInternalObjects();
 
     const GrVkFramebuffer*     fFramebuffer;
-    const GrVkImageView*       fColorAttachmentView;
-    GrVkImage*                 fMSAAImage;
-    const GrVkImageView*       fResolveAttachmentView;
     int                        fColorValuesPerPixel;
 
     // This is a cached pointer to a simple render pass. The render target should unref it
index 3999749b6e8790898fda2fdba944c57385465e5b..83e82fb5b1ac908f7e564c1ff6421946e759eb0e 100644 (file)
@@ -60,14 +60,14 @@ public:
     };
     static Trace  fTrace;
 
-    static SkRandom fRandom;
+    static uint32_t fKeyCounter;
 #endif
 
     /** Default construct, initializing the reference count to 1.
      */
     GrVkResource() : fRefCnt(1) {
 #ifdef SK_TRACE_VK_RESOURCES
-        fKey = fRandom.nextU();
+        fKey = sk_atomic_fetch_add(&fKeyCounter, 1u, sk_memory_order_relaxed);
         fTrace.add(this);
 #endif
     }
index c83c6a4711d1ae0340b22b39e5116a5b37162b3e..e3c19d67a068542d235f43561e1c36e5680601b2 100644 (file)
@@ -16,7 +16,7 @@
 
 #ifdef SK_TRACE_VK_RESOURCES
 GrVkResource::Trace GrVkResource::fTrace;
-SkRandom GrVkResource::fRandom;
+uint32_t GrVkResource::fKeyCounter = 0;
 #endif
 
 GrVkResourceProvider::GrVkResourceProvider(GrVkGpu* gpu)
index b0dd1079dc6311a50882c3563a43c72b2a32f8e0..bf399a8352f55bafa9338b1fe09a3aeba41050b0 100644 (file)
@@ -9,6 +9,7 @@
 #include "GrVkGpu.h"
 #include "GrVkImageView.h"
 #include "GrTexturePriv.h"
+#include "GrVkTextureRenderTarget.h"
 #include "GrVkUtil.h"
 
 #include "vk/GrVkTypes.h"
@@ -161,7 +162,7 @@ const GrVkImageView* GrVkTexture::textureView(bool allowSRGB) {
     return fLinearTextureView;
 }
 
-bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) {
+bool GrVkTexture::reallocForMipmap(GrVkGpu* gpu, uint32_t mipLevels) {
     if (mipLevels == 1) {
         // don't need to do anything for a 1x1 texture
         return false;
@@ -174,7 +175,6 @@ bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) {
         return false;
     }
 
-    // Does this even make sense for rendertargets?
     bool renderTarget = SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag);
 
     VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
@@ -209,6 +209,14 @@ bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) {
         return false;
     }
 
+    if (renderTarget) {
+        GrVkTextureRenderTarget* texRT = static_cast<GrVkTextureRenderTarget*>(this);
+        if (!texRT->updateForMipmap(gpu, info)) {
+            GrVkImage::DestroyImageInfo(gpu, &info);
+            return false;
+        }
+    }
+
     oldResource->unref(gpu);
     oldView->unref(gpu);
     if (fLinearTextureView) {
index de10a32b4c8ce2ce5707fe3a518b5722271c85a1..06d147584a35cb50022d769441e70815b9ebec57 100644 (file)
@@ -32,7 +32,7 @@ public:
 
     const GrVkImageView* textureView(bool allowSRGB);
 
-    bool reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels);
+    bool reallocForMipmap(GrVkGpu* gpu, uint32_t mipLevels);
 
 protected:
     GrVkTexture(GrVkGpu*, const GrSurfaceDesc&, const GrVkImageInfo&, const GrVkImageView*,
index df2ad69ce0f0f260b80b1aa05a5c4178e0135fbe..bee6a04fd28b6570b916ccf7cedf5a7646016296 100644 (file)
@@ -25,13 +25,9 @@ GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu,
                                                          GrVkImage::Wrapped wrapped) {
     VkImage image = info.fImage;
     // Create the texture ImageView
-    uint32_t mipLevels = 1;
-    //TODO: does a mipmapped textureRenderTarget make sense?
-    //if (desc.fIsMipMapped) {
-    //    mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height()) + 1;
-    //}
     const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, info.fFormat,
-                                                           GrVkImageView::kColor_Type, mipLevels);
+                                                           GrVkImageView::kColor_Type,
+                                                           info.fLevelCount);
     if (!imageView) {
         return nullptr;
     }
@@ -63,44 +59,31 @@ GrVkTextureRenderTarget* GrVkTextureRenderTarget::Create(GrVkGpu* gpu,
         // Set color attachment image
         colorImage = msInfo.fImage;
 
-        // Create resolve attachment view if necessary.
-        // If the format matches, this is the same as the texture imageView.
-        if (pixelFormat == info.fFormat) {
-            resolveAttachmentView = imageView;
-            resolveAttachmentView->ref();
-        } else {
-            resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelFormat,
-                                                          GrVkImageView::kColor_Type, 1);
-            if (!resolveAttachmentView) {
-                GrVkImage::DestroyImageInfo(gpu, &msInfo);
-                imageView->unref(gpu);
-                return nullptr;
-            }
+        // Create resolve attachment view.
+        resolveAttachmentView = GrVkImageView::Create(gpu, image, pixelFormat,
+                                                      GrVkImageView::kColor_Type,
+                                                      info.fLevelCount);
+        if (!resolveAttachmentView) {
+            GrVkImage::DestroyImageInfo(gpu, &msInfo);
+            imageView->unref(gpu);
+            return nullptr;
         }
     } else {
         // Set color attachment image
         colorImage = info.fImage;
     }
 
-    const GrVkImageView* colorAttachmentView;
-    // Get color attachment view.
-    // If the format matches and there's no multisampling,
-    // this is the same as the texture imageView
-    if (pixelFormat == info.fFormat && !resolveAttachmentView) {
-        colorAttachmentView = imageView;
-        colorAttachmentView->ref();
-    } else {
-        colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat,
-                                                    GrVkImageView::kColor_Type, 1);
-        if (!colorAttachmentView) {
-            if (desc.fSampleCnt) {
-                resolveAttachmentView->unref(gpu);
-                GrVkImage::DestroyImageInfo(gpu, &msInfo);
-            }
-            imageView->unref(gpu);
-            return nullptr;
+    const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat,
+                                                                     GrVkImageView::kColor_Type, 1);
+    if (!colorAttachmentView) {
+        if (desc.fSampleCnt) {
+            resolveAttachmentView->unref(gpu);
+            GrVkImage::DestroyImageInfo(gpu, &msInfo);
         }
+        imageView->unref(gpu);
+        return nullptr;
     }
+
     GrVkTextureRenderTarget* texRT;
     if (desc.fSampleCnt) {
         if (GrVkImage::kNot_Wrapped == wrapped) {
@@ -165,3 +148,36 @@ GrVkTextureRenderTarget::CreateWrappedTextureRenderTarget(GrVkGpu* gpu,
 
     return trt;
 }
+
+bool GrVkTextureRenderTarget::updateForMipmap(GrVkGpu* gpu, const GrVkImageInfo& newInfo) {
+    VkFormat pixelFormat;
+    GrPixelConfigToVkFormat(fDesc.fConfig, &pixelFormat);
+    if (fDesc.fSampleCnt) {
+        const GrVkImageView* resolveAttachmentView =
+                GrVkImageView::Create(gpu,
+                                      newInfo.fImage,
+                                      pixelFormat,
+                                      GrVkImageView::kColor_Type,
+                                      newInfo.fLevelCount);
+        if (!resolveAttachmentView) {
+            return false;
+        }
+        fResolveAttachmentView->unref(gpu);
+        fResolveAttachmentView = resolveAttachmentView;
+    } else {
+        const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu,
+                                                                         newInfo.fImage,
+                                                                         pixelFormat,
+                                                                         GrVkImageView::kColor_Type,
+                                                                         1);
+        if (!colorAttachmentView) {
+            return false;
+        }
+        fColorAttachmentView->unref(gpu);
+        fColorAttachmentView = colorAttachmentView;
+    }
+
+    this->createFramebuffer(gpu);
+    return true;
+}
+
index 6ea332a0ae2ed098ebd8a56befbbc75e2d35be68..225951106612350fc82012f8e189aa3bab23895c 100644 (file)
@@ -33,6 +33,8 @@ public:
                                                                      GrWrapOwnership,
                                                                      const GrVkImageInfo*);
 
+    bool updateForMipmap(GrVkGpu* gpu, const GrVkImageInfo& newInfo);
+
 protected:
     void onAbandon() override {
         GrVkRenderTarget::onAbandon();