Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: src/gpu/vk/GrVkTexture.cpp

Issue 1925303002: Add mipmap loading to Vulkan. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fixes to handle mipmap allocations and autogen Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/gpu/vk/GrVkGpu.cpp ('K') | « src/gpu/vk/GrVkTexture.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrVkTexture.h" 8 #include "GrVkTexture.h"
9 #include "GrVkGpu.h" 9 #include "GrVkGpu.h"
10 #include "GrVkImageView.h" 10 #include "GrVkImageView.h"
11 #include "GrTexturePriv.h" 11 #include "GrTexturePriv.h"
12 #include "GrVkUtil.h" 12 #include "GrVkUtil.h"
13 #include "SkMipMap.h"
14 13
15 #include "vk/GrVkTypes.h" 14 #include "vk/GrVkTypes.h"
16 15
17 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X) 16 #define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
18 17
19 // Because this class is virtually derived from GrSurface we must explicitly cal l its constructor. 18 // Because this class is virtually derived from GrSurface we must explicitly cal l its constructor.
20 GrVkTexture::GrVkTexture(GrVkGpu* gpu, 19 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
21 SkBudgeted budgeted, 20 SkBudgeted budgeted,
22 const GrSurfaceDesc& desc, 21 const GrSurfaceDesc& desc,
23 const GrVkImage::Resource* imageResource, 22 const GrVkImage::Resource* imageResource,
24 const GrVkImageView* view) 23 const GrVkImageView* view)
25 : GrSurface(gpu, desc) 24 : GrSurface(gpu, desc)
26 , GrVkImage(imageResource) 25 , GrVkImage(imageResource)
27 , INHERITED(gpu, desc, kSampler2D_GrSLType, 26 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped)
28 false) // false because we don't upload MIP data in Vk yet
29 , fTextureView(view) { 27 , fTextureView(view) {
30 this->registerWithCache(budgeted); 28 this->registerWithCache(budgeted);
31 } 29 }
32 30
33 GrVkTexture::GrVkTexture(GrVkGpu* gpu, 31 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
34 Wrapped, 32 Wrapped,
35 const GrSurfaceDesc& desc, 33 const GrSurfaceDesc& desc,
36 const GrVkImage::Resource* imageResource, 34 const GrVkImage::Resource* imageResource,
37 const GrVkImageView* view) 35 const GrVkImageView* view)
38 : GrSurface(gpu, desc) 36 : GrSurface(gpu, desc)
39 , GrVkImage(imageResource) 37 , GrVkImage(imageResource)
40 , INHERITED(gpu, desc, kSampler2D_GrSLType, 38 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped)
41 false) // false because we don't upload MIP data in Vk yet
42 , fTextureView(view) { 39 , fTextureView(view) {
43 this->registerWithCacheWrapped(); 40 this->registerWithCacheWrapped();
44 } 41 }
45 42
46 // Because this class is virtually derived from GrSurface we must explicitly cal l its constructor. 43 // Because this class is virtually derived from GrSurface we must explicitly cal l its constructor.
47 GrVkTexture::GrVkTexture(GrVkGpu* gpu, 44 GrVkTexture::GrVkTexture(GrVkGpu* gpu,
48 const GrSurfaceDesc& desc, 45 const GrSurfaceDesc& desc,
49 const GrVkImage::Resource* imageResource, 46 const GrVkImage::Resource* imageResource,
50 const GrVkImageView* view) 47 const GrVkImageView* view)
51 : GrSurface(gpu, desc) 48 : GrSurface(gpu, desc)
52 , GrVkImage(imageResource) 49 , GrVkImage(imageResource)
53 , INHERITED(gpu, desc, kSampler2D_GrSLType, 50 , INHERITED(gpu, desc, kSampler2D_GrSLType, desc.fIsMipMapped)
54 false) // false because we don't upload MIP data in Vk yet
55 , fTextureView(view) {} 51 , fTextureView(view) {}
56 52
57 53
58 template<typename ResourceType> 54 template<typename ResourceType>
59 GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu, 55 GrVkTexture* GrVkTexture::Create(GrVkGpu* gpu,
60 ResourceType type, 56 ResourceType type,
61 const GrSurfaceDesc& desc, 57 const GrSurfaceDesc& desc,
62 VkFormat format, 58 VkFormat format, uint32_t levels,
63 const GrVkImage::Resource* imageResource) { 59 const GrVkImage::Resource* imageResource) {
64 VkImage image = imageResource->fImage; 60 VkImage image = imageResource->fImage;
65 61
66 uint32_t mipLevels = 1;
67 // TODO: enable when mipLevel loading is implemented in GrVkGpu
68 //if (desc.fIsMipMapped) {
69 // mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height()) ;
70 //}
71 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format, 62 const GrVkImageView* imageView = GrVkImageView::Create(gpu, image, format,
72 GrVkImageView::kColor _Type, mipLevels); 63 GrVkImageView::kColor _Type,
64 levels);
73 if (!imageView) { 65 if (!imageView) {
74 return nullptr; 66 return nullptr;
75 } 67 }
76 68
77 return new GrVkTexture(gpu, type, desc, imageResource, imageView); 69 return new GrVkTexture(gpu, type, desc, imageResource, imageView);
78 } 70 }
79 71
80 GrVkTexture* GrVkTexture::CreateNewTexture(GrVkGpu* gpu, SkBudgeted budgeted, 72 GrVkTexture* GrVkTexture::CreateNewTexture(GrVkGpu* gpu, SkBudgeted budgeted,
81 const GrSurfaceDesc& desc, 73 const GrSurfaceDesc& desc,
82 const GrVkImage::ImageDesc& imageDesc ) { 74 const GrVkImage::ImageDesc& imageDesc ) {
83 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT); 75 SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT);
84 76
85 const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, im ageDesc); 77 const GrVkImage::Resource* imageResource = GrVkImage::CreateResource(gpu, im ageDesc);
86 if (!imageResource) { 78 if (!imageResource) {
87 return nullptr; 79 return nullptr;
88 } 80 }
89 81
90 GrVkTexture* texture = Create(gpu, budgeted, desc, imageDesc.fFormat, imageR esource); 82 GrVkTexture* texture = Create(gpu, budgeted, desc, imageDesc.fFormat, imageD esc.fLevels,
83 imageResource);
91 // Create() will increment the refCount of the image resource if it succeeds 84 // Create() will increment the refCount of the image resource if it succeeds
92 imageResource->unref(gpu); 85 imageResource->unref(gpu);
93 86
94 return texture; 87 return texture;
95 } 88 }
96 89
97 GrVkTexture* GrVkTexture::CreateWrappedTexture(GrVkGpu* gpu, 90 GrVkTexture* GrVkTexture::CreateWrappedTexture(GrVkGpu* gpu,
98 const GrSurfaceDesc& desc, 91 const GrSurfaceDesc& desc,
99 GrWrapOwnership ownership, 92 GrWrapOwnership ownership,
100 VkFormat format, 93 VkFormat format,
(...skipping 11 matching lines...) Expand all
112 info->fAlloc, 105 info->fAlloc,
113 flags, 106 flags,
114 info->fFormat); 107 info->fFormat);
115 } else { 108 } else {
116 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flag s, info->fFormat); 109 imageResource = new GrVkImage::Resource(info->fImage, info->fAlloc, flag s, info->fFormat);
117 } 110 }
118 if (!imageResource) { 111 if (!imageResource) {
119 return nullptr; 112 return nullptr;
120 } 113 }
121 114
122 GrVkTexture* texture = Create(gpu, kWrapped, desc, format, imageResource); 115 // We have no other information so we have to assume that wrapped textures h ave only one level
116 GrVkTexture* texture = Create(gpu, kWrapped, desc, format, 1, imageResource) ;
123 if (texture) { 117 if (texture) {
124 texture->fCurrentLayout = info->fImageLayout; 118 texture->fCurrentLayout = info->fImageLayout;
125 } 119 }
126 // Create() will increment the refCount of the image resource if it succeeds 120 // Create() will increment the refCount of the image resource if it succeeds
127 imageResource->unref(gpu); 121 imageResource->unref(gpu);
128 122
129 return texture; 123 return texture;
130 } 124 }
131 125
132 GrVkTexture::~GrVkTexture() { 126 GrVkTexture::~GrVkTexture() {
(...skipping 26 matching lines...) Expand all
159 GrBackendObject GrVkTexture::getTextureHandle() const { 153 GrBackendObject GrVkTexture::getTextureHandle() const {
160 // Currently just passing back the pointer to the Resource as the handle 154 // Currently just passing back the pointer to the Resource as the handle
161 return (GrBackendObject)&fResource; 155 return (GrBackendObject)&fResource;
162 } 156 }
163 157
164 GrVkGpu* GrVkTexture::getVkGpu() const { 158 GrVkGpu* GrVkTexture::getVkGpu() const {
165 SkASSERT(!this->wasDestroyed()); 159 SkASSERT(!this->wasDestroyed());
166 return static_cast<GrVkGpu*>(this->getGpu()); 160 return static_cast<GrVkGpu*>(this->getGpu());
167 } 161 }
168 162
169 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu) { 163 bool GrVkTexture::reallocForMipmap(const GrVkGpu* gpu, uint32_t mipLevels) {
164 if (mipLevels == 1) {
165 // don't need to do anything for a 1x1 texture
166 return false;
167 }
168
170 const GrVkImage::Resource* oldResource = fResource; 169 const GrVkImage::Resource* oldResource = fResource;
171 170
171 // We shouldn't realloc something that doesn't belong to us
172 if (GrVkImage::Resource::kBorrowed_Flag & oldResource->fFlags) {
173 return false;
174 }
175
172 // Does this even make sense for rendertargets? 176 // Does this even make sense for rendertargets?
173 bool renderTarget = SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag); 177 bool renderTarget = SkToBool(fDesc.fFlags & kRenderTarget_GrSurfaceFlag);
174 178
175 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT; 179 VkImageUsageFlags usageFlags = VK_IMAGE_USAGE_SAMPLED_BIT;
176 if (renderTarget) { 180 if (renderTarget) {
177 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; 181 usageFlags |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
178 } 182 }
179 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_ BIT; 183 usageFlags |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_ BIT;
180 184
181 uint32_t mipLevels = SkMipMap::ComputeLevelCount(this->width(), this->height ());
182 if (mipLevels == 1) {
183 // don't need to do anything for a 1x1 texture
184 return false;
185 }
186
187 GrVkImage::ImageDesc imageDesc; 185 GrVkImage::ImageDesc imageDesc;
188 imageDesc.fImageType = VK_IMAGE_TYPE_2D; 186 imageDesc.fImageType = VK_IMAGE_TYPE_2D;
189 imageDesc.fFormat = oldResource->fFormat; 187 imageDesc.fFormat = oldResource->fFormat;
190 imageDesc.fWidth = fDesc.fWidth; 188 imageDesc.fWidth = fDesc.fWidth;
191 imageDesc.fHeight = fDesc.fHeight; 189 imageDesc.fHeight = fDesc.fHeight;
192 imageDesc.fLevels = mipLevels; 190 imageDesc.fLevels = mipLevels;
193 imageDesc.fSamples = 1; 191 imageDesc.fSamples = 1;
194 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL; 192 imageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
195 imageDesc.fUsageFlags = usageFlags; 193 imageDesc.fUsageFlags = usageFlags;
196 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT; 194 imageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
(...skipping 14 matching lines...) Expand all
211 } 209 }
212 210
213 oldResource->unref(gpu); 211 oldResource->unref(gpu);
214 oldView->unref(gpu); 212 oldView->unref(gpu);
215 fResource = imageResource; 213 fResource = imageResource;
216 fTextureView = textureView; 214 fTextureView = textureView;
217 this->texturePriv().setMaxMipMapLevel(mipLevels); 215 this->texturePriv().setMaxMipMapLevel(mipLevels);
218 216
219 return true; 217 return true;
220 } 218 }
OLDNEW
« src/gpu/vk/GrVkGpu.cpp ('K') | « src/gpu/vk/GrVkTexture.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698