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