1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
|
/*
* Copyright 2015 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrVkRenderTarget.h"
#include "GrBackendSurface.h"
#include "GrRenderTargetPriv.h"
#include "GrVkCommandBuffer.h"
#include "GrVkFramebuffer.h"
#include "GrVkGpu.h"
#include "GrVkImageView.h"
#include "GrVkResourceProvider.h"
#include "GrVkUtil.h"
#include "vk/GrVkTypes.h"
#define VK_CALL(GPU, X) GR_VK_CALL(GPU->vkInterface(), X)
// We're virtually derived from GrSurface (via GrRenderTarget) so its
// constructor must be explicitly called.
GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
SkBudgeted budgeted,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout,
const GrVkImageInfo& msaaInfo,
sk_sp<GrVkImageLayout> msaaLayout,
const GrVkImageView* colorAttachmentView,
const GrVkImageView* resolveAttachmentView,
GrBackendObjectOwnership ownership)
: GrSurface(gpu, desc)
, GrVkImage(info, std::move(layout), ownership)
// for the moment we only support 1:1 color to stencil
, GrRenderTarget(gpu, desc)
, fColorAttachmentView(colorAttachmentView)
, fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), GrBackendObjectOwnership::kOwned))
, fResolveAttachmentView(resolveAttachmentView)
, fFramebuffer(nullptr)
, fCachedSimpleRenderPass(nullptr) {
SkASSERT(desc.fSampleCnt > 1);
this->createFramebuffer(gpu);
this->registerWithCache(budgeted);
}
// We're virtually derived from GrSurface (via GrRenderTarget) so its
// constructor must be explicitly called.
GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout,
const GrVkImageInfo& msaaInfo,
sk_sp<GrVkImageLayout> msaaLayout,
const GrVkImageView* colorAttachmentView,
const GrVkImageView* resolveAttachmentView,
GrBackendObjectOwnership ownership)
: GrSurface(gpu, desc)
, GrVkImage(info, std::move(layout), ownership)
// for the moment we only support 1:1 color to stencil
, GrRenderTarget(gpu, desc)
, fColorAttachmentView(colorAttachmentView)
, fMSAAImage(new GrVkImage(msaaInfo, std::move(msaaLayout), GrBackendObjectOwnership::kOwned))
, fResolveAttachmentView(resolveAttachmentView)
, fFramebuffer(nullptr)
, fCachedSimpleRenderPass(nullptr) {
SkASSERT(desc.fSampleCnt > 1);
this->createFramebuffer(gpu);
}
// We're virtually derived from GrSurface (via GrRenderTarget) so its
// constructor must be explicitly called.
GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
SkBudgeted budgeted,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout,
const GrVkImageView* colorAttachmentView,
GrBackendObjectOwnership ownership)
: GrSurface(gpu, desc)
, GrVkImage(info, std::move(layout), ownership)
, GrRenderTarget(gpu, desc)
, fColorAttachmentView(colorAttachmentView)
, fMSAAImage(nullptr)
, fResolveAttachmentView(nullptr)
, fFramebuffer(nullptr)
, fCachedSimpleRenderPass(nullptr) {
SkASSERT(1 == desc.fSampleCnt);
this->createFramebuffer(gpu);
this->registerWithCache(budgeted);
}
// We're virtually derived from GrSurface (via GrRenderTarget) so its
// constructor must be explicitly called.
GrVkRenderTarget::GrVkRenderTarget(GrVkGpu* gpu,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout,
const GrVkImageView* colorAttachmentView,
GrBackendObjectOwnership ownership)
: GrSurface(gpu, desc)
, GrVkImage(info, std::move(layout), ownership)
, GrRenderTarget(gpu, desc)
, fColorAttachmentView(colorAttachmentView)
, fMSAAImage(nullptr)
, fResolveAttachmentView(nullptr)
, fFramebuffer(nullptr)
, fCachedSimpleRenderPass(nullptr) {
SkASSERT(1 == desc.fSampleCnt);
this->createFramebuffer(gpu);
}
GrVkRenderTarget*
GrVkRenderTarget::Create(GrVkGpu* gpu,
SkBudgeted budgeted,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout,
GrBackendObjectOwnership ownership) {
SkASSERT(1 == info.fLevelCount);
VkFormat pixelFormat;
GrPixelConfigToVkFormat(desc.fConfig, &pixelFormat);
VkImage colorImage;
// create msaa surface if necessary
GrVkImageInfo msInfo;
sk_sp<GrVkImageLayout> msLayout;
const GrVkImageView* resolveAttachmentView = nullptr;
if (desc.fSampleCnt > 1) {
GrVkImage::ImageDesc msImageDesc;
msImageDesc.fImageType = VK_IMAGE_TYPE_2D;
msImageDesc.fFormat = pixelFormat;
msImageDesc.fWidth = desc.fWidth;
msImageDesc.fHeight = desc.fHeight;
msImageDesc.fLevels = 1;
msImageDesc.fSamples = desc.fSampleCnt;
msImageDesc.fImageTiling = VK_IMAGE_TILING_OPTIMAL;
msImageDesc.fUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
msImageDesc.fMemProps = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
if (!GrVkImage::InitImageInfo(gpu, msImageDesc, &msInfo)) {
return nullptr;
}
// Set color attachment image
colorImage = msInfo.fImage;
// Create Resolve attachment view
resolveAttachmentView = GrVkImageView::Create(gpu, info.fImage, pixelFormat,
GrVkImageView::kColor_Type, 1);
if (!resolveAttachmentView) {
GrVkImage::DestroyImageInfo(gpu, &msInfo);
return nullptr;
}
msLayout.reset(new GrVkImageLayout(msInfo.fImageLayout));
} else {
// Set color attachment image
colorImage = info.fImage;
}
// Get color attachment view
const GrVkImageView* colorAttachmentView = GrVkImageView::Create(gpu, colorImage, pixelFormat,
GrVkImageView::kColor_Type, 1);
if (!colorAttachmentView) {
if (desc.fSampleCnt > 1) {
resolveAttachmentView->unref(gpu);
GrVkImage::DestroyImageInfo(gpu, &msInfo);
}
return nullptr;
}
GrVkRenderTarget* texRT;
if (desc.fSampleCnt > 1) {
texRT = new GrVkRenderTarget(gpu, budgeted, desc, info, std::move(layout), msInfo,
std::move(msLayout), colorAttachmentView,
resolveAttachmentView, ownership);
} else {
texRT = new GrVkRenderTarget(gpu, budgeted, desc, info, std::move(layout),
colorAttachmentView, ownership);
}
return texRT;
}
GrVkRenderTarget*
GrVkRenderTarget::CreateNewRenderTarget(GrVkGpu* gpu,
SkBudgeted budgeted,
const GrSurfaceDesc& desc,
const GrVkImage::ImageDesc& imageDesc) {
SkASSERT(imageDesc.fUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT);
GrVkImageInfo info;
if (!GrVkImage::InitImageInfo(gpu, imageDesc, &info)) {
return nullptr;
}
sk_sp<GrVkImageLayout> layout(new GrVkImageLayout(info.fImageLayout));
GrVkRenderTarget* rt = GrVkRenderTarget::Create(gpu, budgeted, desc, info, std::move(layout),
GrBackendObjectOwnership::kOwned);
if (!rt) {
GrVkImage::DestroyImageInfo(gpu, &info);
}
return rt;
}
sk_sp<GrVkRenderTarget>
GrVkRenderTarget::MakeWrappedRenderTarget(GrVkGpu* gpu,
const GrSurfaceDesc& desc,
const GrVkImageInfo& info,
sk_sp<GrVkImageLayout> layout) {
SkASSERT(VK_NULL_HANDLE != info.fImage);
return sk_sp<GrVkRenderTarget>(
GrVkRenderTarget::Create(gpu, SkBudgeted::kNo, desc, info, std::move(layout),
GrBackendObjectOwnership::kBorrowed));
}
bool GrVkRenderTarget::completeStencilAttachment() {
this->createFramebuffer(this->getVkGpu());
return true;
}
void GrVkRenderTarget::createFramebuffer(GrVkGpu* gpu) {
if (fFramebuffer) {
fFramebuffer->unref(gpu);
}
if (fCachedSimpleRenderPass) {
fCachedSimpleRenderPass->unref(gpu);
}
// Vulkan requires us to create a compatible renderpass before we can create our framebuffer,
// so we use this to get a (cached) basic renderpass, only for creation.
fCachedSimpleRenderPass =
gpu->resourceProvider().findCompatibleRenderPass(*this, &fCompatibleRPHandle);
// Stencil attachment view is stored in the base RT stencil attachment
const GrVkImageView* stencilView = this->stencilAttachmentView();
fFramebuffer = GrVkFramebuffer::Create(gpu, this->width(), this->height(),
fCachedSimpleRenderPass, fColorAttachmentView,
stencilView);
SkASSERT(fFramebuffer);
}
void GrVkRenderTarget::getAttachmentsDescriptor(
GrVkRenderPass::AttachmentsDescriptor* desc,
GrVkRenderPass::AttachmentFlags* attachmentFlags) const {
VkFormat colorFormat;
GrPixelConfigToVkFormat(this->config(), &colorFormat);
desc->fColor.fFormat = colorFormat;
desc->fColor.fSamples = this->numColorSamples();
*attachmentFlags = GrVkRenderPass::kColor_AttachmentFlag;
uint32_t attachmentCount = 1;
const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
if (stencil) {
const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil);
desc->fStencil.fFormat = vkStencil->vkFormat();
desc->fStencil.fSamples = vkStencil->numSamples();
// Currently in vulkan stencil and color attachments must all have same number of samples
SkASSERT(desc->fColor.fSamples == desc->fStencil.fSamples);
*attachmentFlags |= GrVkRenderPass::kStencil_AttachmentFlag;
++attachmentCount;
}
desc->fAttachmentCount = attachmentCount;
}
GrVkRenderTarget::~GrVkRenderTarget() {
// either release or abandon should have been called by the owner of this object.
SkASSERT(!fMSAAImage);
SkASSERT(!fResolveAttachmentView);
SkASSERT(!fColorAttachmentView);
SkASSERT(!fFramebuffer);
SkASSERT(!fCachedSimpleRenderPass);
}
void GrVkRenderTarget::addResources(GrVkCommandBuffer& commandBuffer) const {
commandBuffer.addResource(this->framebuffer());
commandBuffer.addResource(this->colorAttachmentView());
commandBuffer.addResource(this->msaaImageResource() ? this->msaaImageResource()
: this->resource());
if (this->stencilImageResource()) {
commandBuffer.addResource(this->stencilImageResource());
commandBuffer.addResource(this->stencilAttachmentView());
}
}
void GrVkRenderTarget::releaseInternalObjects() {
GrVkGpu* gpu = this->getVkGpu();
if (fMSAAImage) {
fMSAAImage->releaseImage(gpu);
fMSAAImage.reset();
}
if (fResolveAttachmentView) {
fResolveAttachmentView->unref(gpu);
fResolveAttachmentView = nullptr;
}
if (fColorAttachmentView) {
fColorAttachmentView->unref(gpu);
fColorAttachmentView = nullptr;
}
if (fFramebuffer) {
fFramebuffer->unref(gpu);
fFramebuffer = nullptr;
}
if (fCachedSimpleRenderPass) {
fCachedSimpleRenderPass->unref(gpu);
fCachedSimpleRenderPass = nullptr;
}
}
void GrVkRenderTarget::abandonInternalObjects() {
if (fMSAAImage) {
fMSAAImage->abandonImage();
fMSAAImage.reset();
}
if (fResolveAttachmentView) {
fResolveAttachmentView->unrefAndAbandon();
fResolveAttachmentView = nullptr;
}
if (fColorAttachmentView) {
fColorAttachmentView->unrefAndAbandon();
fColorAttachmentView = nullptr;
}
if (fFramebuffer) {
fFramebuffer->unrefAndAbandon();
fFramebuffer = nullptr;
}
if (fCachedSimpleRenderPass) {
fCachedSimpleRenderPass->unrefAndAbandon();
fCachedSimpleRenderPass = nullptr;
}
}
void GrVkRenderTarget::onRelease() {
this->releaseInternalObjects();
this->releaseImage(this->getVkGpu());
GrRenderTarget::onRelease();
}
void GrVkRenderTarget::onAbandon() {
this->abandonInternalObjects();
this->abandonImage();
GrRenderTarget::onAbandon();
}
#ifdef SK_SUPPORT_LEGACY_BACKEND_OBJECTS
GrBackendObject GrVkRenderTarget::getRenderTargetHandle() const {
// If the render target is multisampled, we currently return the ImageInfo for the resolved
// image. If we only wrap the msaa target (currently not implemented) we should return a handle
// to that instead.
return (GrBackendObject)&fInfo;
}
#endif
GrBackendRenderTarget GrVkRenderTarget::getBackendRenderTarget() const {
return GrBackendRenderTarget(this->width(), this->height(), this->numColorSamples(),
fInfo, this->grVkImageLayout());
}
const GrVkResource* GrVkRenderTarget::stencilImageResource() const {
const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
if (stencil) {
const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil);
return vkStencil->imageResource();
}
return nullptr;
}
const GrVkImageView* GrVkRenderTarget::stencilAttachmentView() const {
const GrStencilAttachment* stencil = this->renderTargetPriv().getStencilAttachment();
if (stencil) {
const GrVkStencilAttachment* vkStencil = static_cast<const GrVkStencilAttachment*>(stencil);
return vkStencil->stencilView();
}
return nullptr;
}
GrVkGpu* GrVkRenderTarget::getVkGpu() const {
SkASSERT(!this->wasDestroyed());
return static_cast<GrVkGpu*>(this->getGpu());
}
|