1*c8dee2aaSAndroid Build Coastguard Worker /*
2*c8dee2aaSAndroid Build Coastguard Worker * Copyright 2022 Google LLC
3*c8dee2aaSAndroid Build Coastguard Worker *
4*c8dee2aaSAndroid Build Coastguard Worker * Use of this source code is governed by a BSD-style license that can be
5*c8dee2aaSAndroid Build Coastguard Worker * found in the LICENSE file.
6*c8dee2aaSAndroid Build Coastguard Worker */
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/dawn/DawnTexture.h"
9*c8dee2aaSAndroid Build Coastguard Worker
10*c8dee2aaSAndroid Build Coastguard Worker #include "include/core/SkTraceMemoryDump.h"
11*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/MutableTextureState.h"
12*c8dee2aaSAndroid Build Coastguard Worker #include "include/gpu/graphite/dawn/DawnTypes.h"
13*c8dee2aaSAndroid Build Coastguard Worker #include "src/core/SkMipmap.h"
14*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/Log.h"
15*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/TextureUtils.h"
16*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/dawn/DawnCaps.h"
17*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/dawn/DawnGraphiteTypesPriv.h"
18*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/dawn/DawnGraphiteUtilsPriv.h"
19*c8dee2aaSAndroid Build Coastguard Worker #include "src/gpu/graphite/dawn/DawnSharedContext.h"
20*c8dee2aaSAndroid Build Coastguard Worker
21*c8dee2aaSAndroid Build Coastguard Worker namespace skgpu::graphite {
22*c8dee2aaSAndroid Build Coastguard Worker
MakeDawnTexture(const DawnSharedContext * sharedContext,SkISize dimensions,const TextureInfo & info)23*c8dee2aaSAndroid Build Coastguard Worker wgpu::Texture DawnTexture::MakeDawnTexture(const DawnSharedContext* sharedContext,
24*c8dee2aaSAndroid Build Coastguard Worker SkISize dimensions,
25*c8dee2aaSAndroid Build Coastguard Worker const TextureInfo& info) {
26*c8dee2aaSAndroid Build Coastguard Worker const Caps* caps = sharedContext->caps();
27*c8dee2aaSAndroid Build Coastguard Worker if (dimensions.width() > caps->maxTextureSize() ||
28*c8dee2aaSAndroid Build Coastguard Worker dimensions.height() > caps->maxTextureSize()) {
29*c8dee2aaSAndroid Build Coastguard Worker SKGPU_LOG_E("Texture creation failure: dimensions %d x %d too large.",
30*c8dee2aaSAndroid Build Coastguard Worker dimensions.width(), dimensions.height());
31*c8dee2aaSAndroid Build Coastguard Worker return {};
32*c8dee2aaSAndroid Build Coastguard Worker }
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Worker const DawnTextureSpec dawnSpec = TextureInfos::GetDawnTextureSpec(info);
35*c8dee2aaSAndroid Build Coastguard Worker
36*c8dee2aaSAndroid Build Coastguard Worker if (dawnSpec.fUsage & wgpu::TextureUsage::TextureBinding && !caps->isTexturable(info)) {
37*c8dee2aaSAndroid Build Coastguard Worker return {};
38*c8dee2aaSAndroid Build Coastguard Worker }
39*c8dee2aaSAndroid Build Coastguard Worker
40*c8dee2aaSAndroid Build Coastguard Worker if (dawnSpec.fUsage & wgpu::TextureUsage::RenderAttachment &&
41*c8dee2aaSAndroid Build Coastguard Worker !(caps->isRenderable(info) || DawnFormatIsDepthOrStencil(dawnSpec.fFormat))) {
42*c8dee2aaSAndroid Build Coastguard Worker return {};
43*c8dee2aaSAndroid Build Coastguard Worker }
44*c8dee2aaSAndroid Build Coastguard Worker
45*c8dee2aaSAndroid Build Coastguard Worker if (dawnSpec.fUsage & wgpu::TextureUsage::StorageBinding && !caps->isStorage(info)) {
46*c8dee2aaSAndroid Build Coastguard Worker return {};
47*c8dee2aaSAndroid Build Coastguard Worker }
48*c8dee2aaSAndroid Build Coastguard Worker
49*c8dee2aaSAndroid Build Coastguard Worker #if !defined(__EMSCRIPTEN__)
50*c8dee2aaSAndroid Build Coastguard Worker // If a non-default YCbCr descriptor is provided, either the vkFormat or the externalFormat must
51*c8dee2aaSAndroid Build Coastguard Worker // be defined.
52*c8dee2aaSAndroid Build Coastguard Worker if (ycbcrUtils::DawnDescriptorIsValid(dawnSpec.fYcbcrVkDescriptor) &&
53*c8dee2aaSAndroid Build Coastguard Worker dawnSpec.fYcbcrVkDescriptor.vkFormat == 0 &&
54*c8dee2aaSAndroid Build Coastguard Worker dawnSpec.fYcbcrVkDescriptor.externalFormat == 0) {
55*c8dee2aaSAndroid Build Coastguard Worker return {};
56*c8dee2aaSAndroid Build Coastguard Worker }
57*c8dee2aaSAndroid Build Coastguard Worker #endif
58*c8dee2aaSAndroid Build Coastguard Worker
59*c8dee2aaSAndroid Build Coastguard Worker int numMipLevels = 1;
60*c8dee2aaSAndroid Build Coastguard Worker if (info.mipmapped() == Mipmapped::kYes) {
61*c8dee2aaSAndroid Build Coastguard Worker numMipLevels = SkMipmap::ComputeLevelCount(dimensions.width(), dimensions.height()) + 1;
62*c8dee2aaSAndroid Build Coastguard Worker }
63*c8dee2aaSAndroid Build Coastguard Worker
64*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureDescriptor desc;
65*c8dee2aaSAndroid Build Coastguard Worker desc.usage = dawnSpec.fUsage;
66*c8dee2aaSAndroid Build Coastguard Worker desc.dimension = wgpu::TextureDimension::e2D;
67*c8dee2aaSAndroid Build Coastguard Worker desc.size.width = dimensions.width();
68*c8dee2aaSAndroid Build Coastguard Worker desc.size.height = dimensions.height();
69*c8dee2aaSAndroid Build Coastguard Worker desc.size.depthOrArrayLayers = 1;
70*c8dee2aaSAndroid Build Coastguard Worker desc.format = dawnSpec.fFormat;
71*c8dee2aaSAndroid Build Coastguard Worker desc.mipLevelCount = numMipLevels;
72*c8dee2aaSAndroid Build Coastguard Worker desc.sampleCount = info.numSamples();
73*c8dee2aaSAndroid Build Coastguard Worker desc.viewFormatCount = 0;
74*c8dee2aaSAndroid Build Coastguard Worker desc.viewFormats = nullptr;
75*c8dee2aaSAndroid Build Coastguard Worker
76*c8dee2aaSAndroid Build Coastguard Worker auto texture = sharedContext->device().CreateTexture(&desc);
77*c8dee2aaSAndroid Build Coastguard Worker if (!texture) {
78*c8dee2aaSAndroid Build Coastguard Worker return {};
79*c8dee2aaSAndroid Build Coastguard Worker }
80*c8dee2aaSAndroid Build Coastguard Worker
81*c8dee2aaSAndroid Build Coastguard Worker return texture;
82*c8dee2aaSAndroid Build Coastguard Worker }
83*c8dee2aaSAndroid Build Coastguard Worker
DawnTexture(const DawnSharedContext * sharedContext,SkISize dimensions,const TextureInfo & info,wgpu::Texture texture,wgpu::TextureView sampleTextureView,wgpu::TextureView renderTextureView,Ownership ownership,skgpu::Budgeted budgeted)84*c8dee2aaSAndroid Build Coastguard Worker DawnTexture::DawnTexture(const DawnSharedContext* sharedContext,
85*c8dee2aaSAndroid Build Coastguard Worker SkISize dimensions,
86*c8dee2aaSAndroid Build Coastguard Worker const TextureInfo& info,
87*c8dee2aaSAndroid Build Coastguard Worker wgpu::Texture texture,
88*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureView sampleTextureView,
89*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureView renderTextureView,
90*c8dee2aaSAndroid Build Coastguard Worker Ownership ownership,
91*c8dee2aaSAndroid Build Coastguard Worker skgpu::Budgeted budgeted)
92*c8dee2aaSAndroid Build Coastguard Worker : Texture(sharedContext,
93*c8dee2aaSAndroid Build Coastguard Worker dimensions,
94*c8dee2aaSAndroid Build Coastguard Worker info,
95*c8dee2aaSAndroid Build Coastguard Worker /*mutableState=*/nullptr,
96*c8dee2aaSAndroid Build Coastguard Worker ownership,
97*c8dee2aaSAndroid Build Coastguard Worker budgeted)
98*c8dee2aaSAndroid Build Coastguard Worker , fTexture(std::move(texture))
99*c8dee2aaSAndroid Build Coastguard Worker , fSampleTextureView(std::move(sampleTextureView))
100*c8dee2aaSAndroid Build Coastguard Worker , fRenderTextureView(std::move(renderTextureView)) {}
101*c8dee2aaSAndroid Build Coastguard Worker
102*c8dee2aaSAndroid Build Coastguard Worker // static
CreateTextureViews(const wgpu::Texture & texture,const TextureInfo & info)103*c8dee2aaSAndroid Build Coastguard Worker std::pair<wgpu::TextureView, wgpu::TextureView> DawnTexture::CreateTextureViews(
104*c8dee2aaSAndroid Build Coastguard Worker const wgpu::Texture& texture, const TextureInfo& info) {
105*c8dee2aaSAndroid Build Coastguard Worker const DawnTextureSpec dawnSpec = TextureInfos::GetDawnTextureSpec(info);
106*c8dee2aaSAndroid Build Coastguard Worker const auto aspect = dawnSpec.fAspect;
107*c8dee2aaSAndroid Build Coastguard Worker if (aspect == wgpu::TextureAspect::All) {
108*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureViewDescriptor viewDesc = {};
109*c8dee2aaSAndroid Build Coastguard Worker viewDesc.dimension = wgpu::TextureViewDimension::e2D;
110*c8dee2aaSAndroid Build Coastguard Worker viewDesc.baseArrayLayer = dawnSpec.fSlice;
111*c8dee2aaSAndroid Build Coastguard Worker viewDesc.arrayLayerCount = 1;
112*c8dee2aaSAndroid Build Coastguard Worker #if !defined(__EMSCRIPTEN__)
113*c8dee2aaSAndroid Build Coastguard Worker // Ensure that the TextureView is configured to use YCbCr sampling if the Texture is
114*c8dee2aaSAndroid Build Coastguard Worker // doing so.
115*c8dee2aaSAndroid Build Coastguard Worker const wgpu::YCbCrVkDescriptor& ycbcrDesc = dawnSpec.fYcbcrVkDescriptor;
116*c8dee2aaSAndroid Build Coastguard Worker if (ycbcrUtils::DawnDescriptorIsValid(ycbcrDesc)) {
117*c8dee2aaSAndroid Build Coastguard Worker viewDesc.nextInChain = &ycbcrDesc;
118*c8dee2aaSAndroid Build Coastguard Worker }
119*c8dee2aaSAndroid Build Coastguard Worker #endif
120*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureView sampleTextureView = texture.CreateView(&viewDesc);
121*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureView renderTextureView;
122*c8dee2aaSAndroid Build Coastguard Worker if (info.mipmapped() == Mipmapped::kYes) {
123*c8dee2aaSAndroid Build Coastguard Worker viewDesc.baseMipLevel = 0;
124*c8dee2aaSAndroid Build Coastguard Worker viewDesc.mipLevelCount = 1;
125*c8dee2aaSAndroid Build Coastguard Worker renderTextureView = texture.CreateView(&viewDesc);
126*c8dee2aaSAndroid Build Coastguard Worker } else {
127*c8dee2aaSAndroid Build Coastguard Worker renderTextureView = sampleTextureView;
128*c8dee2aaSAndroid Build Coastguard Worker }
129*c8dee2aaSAndroid Build Coastguard Worker return {sampleTextureView, renderTextureView};
130*c8dee2aaSAndroid Build Coastguard Worker }
131*c8dee2aaSAndroid Build Coastguard Worker
132*c8dee2aaSAndroid Build Coastguard Worker #if defined(__EMSCRIPTEN__)
133*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(false);
134*c8dee2aaSAndroid Build Coastguard Worker return {};
135*c8dee2aaSAndroid Build Coastguard Worker #else
136*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(aspect == wgpu::TextureAspect::Plane0Only ||
137*c8dee2aaSAndroid Build Coastguard Worker aspect == wgpu::TextureAspect::Plane1Only ||
138*c8dee2aaSAndroid Build Coastguard Worker aspect == wgpu::TextureAspect::Plane2Only);
139*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureView planeTextureView;
140*c8dee2aaSAndroid Build Coastguard Worker wgpu::TextureViewDescriptor planeViewDesc = {};
141*c8dee2aaSAndroid Build Coastguard Worker
142*c8dee2aaSAndroid Build Coastguard Worker planeViewDesc.format = dawnSpec.fViewFormat;
143*c8dee2aaSAndroid Build Coastguard Worker planeViewDesc.dimension = wgpu::TextureViewDimension::e2D;
144*c8dee2aaSAndroid Build Coastguard Worker planeViewDesc.aspect = aspect;
145*c8dee2aaSAndroid Build Coastguard Worker planeViewDesc.baseArrayLayer = dawnSpec.fSlice;
146*c8dee2aaSAndroid Build Coastguard Worker planeViewDesc.arrayLayerCount = 1;
147*c8dee2aaSAndroid Build Coastguard Worker planeTextureView = texture.CreateView(&planeViewDesc);
148*c8dee2aaSAndroid Build Coastguard Worker return {planeTextureView, planeTextureView};
149*c8dee2aaSAndroid Build Coastguard Worker #endif
150*c8dee2aaSAndroid Build Coastguard Worker }
151*c8dee2aaSAndroid Build Coastguard Worker
Make(const DawnSharedContext * sharedContext,SkISize dimensions,const TextureInfo & info,skgpu::Budgeted budgeted)152*c8dee2aaSAndroid Build Coastguard Worker sk_sp<Texture> DawnTexture::Make(const DawnSharedContext* sharedContext,
153*c8dee2aaSAndroid Build Coastguard Worker SkISize dimensions,
154*c8dee2aaSAndroid Build Coastguard Worker const TextureInfo& info,
155*c8dee2aaSAndroid Build Coastguard Worker skgpu::Budgeted budgeted) {
156*c8dee2aaSAndroid Build Coastguard Worker auto texture = MakeDawnTexture(sharedContext, dimensions, info);
157*c8dee2aaSAndroid Build Coastguard Worker if (!texture) {
158*c8dee2aaSAndroid Build Coastguard Worker return {};
159*c8dee2aaSAndroid Build Coastguard Worker }
160*c8dee2aaSAndroid Build Coastguard Worker auto [sampleTextureView, renderTextureView] = CreateTextureViews(texture, info);
161*c8dee2aaSAndroid Build Coastguard Worker return sk_sp<Texture>(new DawnTexture(sharedContext,
162*c8dee2aaSAndroid Build Coastguard Worker dimensions,
163*c8dee2aaSAndroid Build Coastguard Worker info,
164*c8dee2aaSAndroid Build Coastguard Worker std::move(texture),
165*c8dee2aaSAndroid Build Coastguard Worker std::move(sampleTextureView),
166*c8dee2aaSAndroid Build Coastguard Worker std::move(renderTextureView),
167*c8dee2aaSAndroid Build Coastguard Worker Ownership::kOwned,
168*c8dee2aaSAndroid Build Coastguard Worker budgeted));
169*c8dee2aaSAndroid Build Coastguard Worker }
170*c8dee2aaSAndroid Build Coastguard Worker
MakeWrapped(const DawnSharedContext * sharedContext,SkISize dimensions,const TextureInfo & info,wgpu::Texture texture)171*c8dee2aaSAndroid Build Coastguard Worker sk_sp<Texture> DawnTexture::MakeWrapped(const DawnSharedContext* sharedContext,
172*c8dee2aaSAndroid Build Coastguard Worker SkISize dimensions,
173*c8dee2aaSAndroid Build Coastguard Worker const TextureInfo& info,
174*c8dee2aaSAndroid Build Coastguard Worker wgpu::Texture texture) {
175*c8dee2aaSAndroid Build Coastguard Worker if (!texture) {
176*c8dee2aaSAndroid Build Coastguard Worker SKGPU_LOG_E("No valid texture passed into MakeWrapped\n");
177*c8dee2aaSAndroid Build Coastguard Worker return {};
178*c8dee2aaSAndroid Build Coastguard Worker }
179*c8dee2aaSAndroid Build Coastguard Worker
180*c8dee2aaSAndroid Build Coastguard Worker auto [sampleTextureView, renderTextureView] = CreateTextureViews(texture, info);
181*c8dee2aaSAndroid Build Coastguard Worker return sk_sp<Texture>(new DawnTexture(sharedContext,
182*c8dee2aaSAndroid Build Coastguard Worker dimensions,
183*c8dee2aaSAndroid Build Coastguard Worker info,
184*c8dee2aaSAndroid Build Coastguard Worker std::move(texture),
185*c8dee2aaSAndroid Build Coastguard Worker std::move(sampleTextureView),
186*c8dee2aaSAndroid Build Coastguard Worker std::move(renderTextureView),
187*c8dee2aaSAndroid Build Coastguard Worker Ownership::kWrapped,
188*c8dee2aaSAndroid Build Coastguard Worker skgpu::Budgeted::kNo));
189*c8dee2aaSAndroid Build Coastguard Worker }
190*c8dee2aaSAndroid Build Coastguard Worker
MakeWrapped(const DawnSharedContext * sharedContext,SkISize dimensions,const TextureInfo & info,const wgpu::TextureView & textureView)191*c8dee2aaSAndroid Build Coastguard Worker sk_sp<Texture> DawnTexture::MakeWrapped(const DawnSharedContext* sharedContext,
192*c8dee2aaSAndroid Build Coastguard Worker SkISize dimensions,
193*c8dee2aaSAndroid Build Coastguard Worker const TextureInfo& info,
194*c8dee2aaSAndroid Build Coastguard Worker const wgpu::TextureView& textureView) {
195*c8dee2aaSAndroid Build Coastguard Worker if (!textureView) {
196*c8dee2aaSAndroid Build Coastguard Worker SKGPU_LOG_E("No valid texture view passed into MakeWrapped\n");
197*c8dee2aaSAndroid Build Coastguard Worker return {};
198*c8dee2aaSAndroid Build Coastguard Worker }
199*c8dee2aaSAndroid Build Coastguard Worker return sk_sp<Texture>(new DawnTexture(sharedContext,
200*c8dee2aaSAndroid Build Coastguard Worker dimensions,
201*c8dee2aaSAndroid Build Coastguard Worker info,
202*c8dee2aaSAndroid Build Coastguard Worker /*texture=*/nullptr,
203*c8dee2aaSAndroid Build Coastguard Worker /*sampleTextureView=*/textureView,
204*c8dee2aaSAndroid Build Coastguard Worker /*renderTextureView=*/textureView,
205*c8dee2aaSAndroid Build Coastguard Worker Ownership::kWrapped,
206*c8dee2aaSAndroid Build Coastguard Worker skgpu::Budgeted::kNo));
207*c8dee2aaSAndroid Build Coastguard Worker }
208*c8dee2aaSAndroid Build Coastguard Worker
freeGpuData()209*c8dee2aaSAndroid Build Coastguard Worker void DawnTexture::freeGpuData() {
210*c8dee2aaSAndroid Build Coastguard Worker if (this->ownership() != Ownership::kWrapped && fTexture) {
211*c8dee2aaSAndroid Build Coastguard Worker // Destroy the texture even if it is still referenced by other BindGroup or views.
212*c8dee2aaSAndroid Build Coastguard Worker // Graphite should already guarantee that all command buffers using this texture (indirectly
213*c8dee2aaSAndroid Build Coastguard Worker // via BindGroup or views) are already completed.
214*c8dee2aaSAndroid Build Coastguard Worker fTexture.Destroy();
215*c8dee2aaSAndroid Build Coastguard Worker }
216*c8dee2aaSAndroid Build Coastguard Worker fTexture = nullptr;
217*c8dee2aaSAndroid Build Coastguard Worker fSampleTextureView = nullptr;
218*c8dee2aaSAndroid Build Coastguard Worker fRenderTextureView = nullptr;
219*c8dee2aaSAndroid Build Coastguard Worker }
220*c8dee2aaSAndroid Build Coastguard Worker
setBackendLabel(char const * label)221*c8dee2aaSAndroid Build Coastguard Worker void DawnTexture::setBackendLabel(char const* label) {
222*c8dee2aaSAndroid Build Coastguard Worker if (!sharedContext()->caps()->setBackendLabels()) {
223*c8dee2aaSAndroid Build Coastguard Worker return;
224*c8dee2aaSAndroid Build Coastguard Worker }
225*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(label);
226*c8dee2aaSAndroid Build Coastguard Worker // Wrapped texture views won't have an associated texture here.
227*c8dee2aaSAndroid Build Coastguard Worker if (fTexture) {
228*c8dee2aaSAndroid Build Coastguard Worker fTexture.SetLabel(label);
229*c8dee2aaSAndroid Build Coastguard Worker }
230*c8dee2aaSAndroid Build Coastguard Worker // But we always have the texture views available.
231*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(fSampleTextureView);
232*c8dee2aaSAndroid Build Coastguard Worker SkASSERT(fRenderTextureView);
233*c8dee2aaSAndroid Build Coastguard Worker if (fSampleTextureView.Get() == fRenderTextureView.Get()) {
234*c8dee2aaSAndroid Build Coastguard Worker fSampleTextureView.SetLabel(SkStringPrintf("%s_%s", label, "_TextureView").c_str());
235*c8dee2aaSAndroid Build Coastguard Worker } else {
236*c8dee2aaSAndroid Build Coastguard Worker fSampleTextureView.SetLabel(SkStringPrintf("%s_%s", label, "_SampleTextureView").c_str());
237*c8dee2aaSAndroid Build Coastguard Worker fRenderTextureView.SetLabel(SkStringPrintf("%s_%s", label, "_RenderTextureView").c_str());
238*c8dee2aaSAndroid Build Coastguard Worker }
239*c8dee2aaSAndroid Build Coastguard Worker }
240*c8dee2aaSAndroid Build Coastguard Worker
241*c8dee2aaSAndroid Build Coastguard Worker } // namespace skgpu::graphite
242*c8dee2aaSAndroid Build Coastguard Worker
243