Properly recycle uniform buffers in vulkan
authoregdaniel <egdaniel@google.com>
Fri, 29 Jul 2016 17:46:06 +0000 (10:46 -0700)
committerCommit bot <commit-bot@chromium.org>
Fri, 29 Jul 2016 17:46:06 +0000 (10:46 -0700)
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2195713002

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

src/gpu/vk/GrVkBuffer.cpp
src/gpu/vk/GrVkBuffer.h
src/gpu/vk/GrVkPipelineState.cpp
src/gpu/vk/GrVkUniformBuffer.cpp
src/gpu/vk/GrVkUniformBuffer.h

index 75f3901..a03ea0c 100644 (file)
@@ -180,8 +180,8 @@ bool GrVkBuffer::vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInByt
 
     if (!fResource->unique()) {
         // in use by the command buffer, so we need to create a new one
-        fResource->unref(gpu);
-        fResource = Create(gpu, fDesc);
+        fResource->recycle(gpu);
+        fResource = this->createResource(gpu, fDesc);
         if (createdNewBuffer) {
             *createdNewBuffer = true;
         }
index b400e09..bb529b3 100644 (file)
@@ -25,11 +25,11 @@ public:
         SkASSERT(!fResource);
     }
 
-    VkBuffer            buffer() const { return fResource->fBuffer; }
-    const GrVkAlloc&    alloc() const { return fResource->fAlloc; }
-    const GrVkResource* resource() const { return fResource; }
-    size_t              size() const { return fDesc.fSizeInBytes; }
-    VkDeviceSize        offset() const { return fOffset;  }
+    VkBuffer                    buffer() const { return fResource->fBuffer; }
+    const GrVkAlloc&            alloc() const { return fResource->fAlloc; }
+    const GrVkRecycledResource* resource() const { return fResource; }
+    size_t                      size() const { return fDesc.fSizeInBytes; }
+    VkDeviceSize                offset() const { return fOffset;  }
 
     void addMemoryBarrier(const GrVkGpu* gpu,
                           VkAccessFlags srcAccessMask,
@@ -94,6 +94,11 @@ protected:
     void vkRelease(const GrVkGpu* gpu);
 
 private:
+    virtual const Resource* createResource(GrVkGpu* gpu,
+                                           const Desc& descriptor) {
+        return Create(gpu, descriptor);
+    }
+
     void validate() const;
     bool vkIsMapped() const;
 
index 5b3fa8f..2dd691c 100644 (file)
@@ -382,13 +382,13 @@ void GrVkPipelineState::addUniformResources(GrVkCommandBuffer& commandBuffer) {
         commandBuffer.addRecycledResource(fSamplerDescriptorSet);
     }
 
-
     if (fVertexUniformBuffer.get()) {
-        commandBuffer.addResource(fVertexUniformBuffer->resource());
+        commandBuffer.addRecycledResource(fVertexUniformBuffer->resource());
     }
     if (fFragmentUniformBuffer.get()) {
-        commandBuffer.addResource(fFragmentUniformBuffer->resource());
+        commandBuffer.addRecycledResource(fFragmentUniformBuffer->resource());
     }
+
     for (int i = 0; i < fSamplers.count(); ++i) {
         commandBuffer.addResource(fSamplers[i]);
     }
index c3899b0..ac013f7 100644 (file)
@@ -82,6 +82,18 @@ const GrVkResource* GrVkUniformBuffer::CreateResource(GrVkGpu* gpu, size_t size)
     return resource;
 }
 
+const GrVkBuffer::Resource* GrVkUniformBuffer::createResource(GrVkGpu* gpu,
+                                                              const GrVkBuffer::Desc& descriptor) {
+    const GrVkResource* vkResource;
+    if (descriptor.fSizeInBytes <= GrVkUniformBuffer::kStandardSize) {
+        GrVkResourceProvider& provider = gpu->resourceProvider();
+        vkResource = provider.findOrCreateStandardUniformBufferResource();
+    } else {
+        vkResource = CreateResource(gpu, descriptor.fSizeInBytes);
+    }
+    return (const GrVkBuffer::Resource*) vkResource;
+}
+
 void GrVkUniformBuffer::Resource::onRecycle(GrVkGpu* gpu) const {
     if (fAlloc.fSize <= GrVkUniformBuffer::kStandardSize) {
         gpu->resourceProvider().recycleStandardUniformBufferResource(this);
index 7aa3af6..d2d9f32 100644 (file)
@@ -45,6 +45,9 @@ private:
         typedef GrVkBuffer::Resource INHERITED;
     };
 
+    const GrVkBuffer::Resource* createResource(GrVkGpu* gpu,
+                                               const GrVkBuffer::Desc& descriptor) override;
+
     GrVkUniformBuffer(GrVkGpu* gpu, const GrVkBuffer::Desc& desc,
                       const GrVkUniformBuffer::Resource* resource)
         : INHERITED(desc, resource)