Texture.cpp (3392B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "Texture.h" 7 8 #include "TextureView.h" 9 #include "Utility.h" 10 #include "ipc/WebGPUChild.h" 11 #include "mozilla/dom/WebGPUBinding.h" 12 #include "mozilla/webgpu/CanvasContext.h" 13 #include "mozilla/webgpu/WebGPUTypes.h" 14 #include "mozilla/webgpu/ffi/wgpu.h" 15 16 namespace mozilla::webgpu { 17 18 GPU_IMPL_CYCLE_COLLECTION(Texture, mParent) 19 GPU_IMPL_JS_WRAP(Texture) 20 21 static Maybe<uint8_t> GetBytesPerBlockSingleAspect( 22 dom::GPUTextureFormat aFormat) { 23 auto format = ConvertTextureFormat(aFormat); 24 ffi::WGPUTextureAspect aspect = {ffi::WGPUTextureAspect_All}; 25 ffi::WGPUTextureFormatBlockInfo info = {}; 26 bool valid = ffi::wgpu_texture_format_get_block_info(format, aspect, &info); 27 if (!valid) { 28 // The above function returns false if the texture has multiple aspects like 29 // depth and stencil. 30 return Nothing(); 31 } 32 33 return Some((uint8_t)info.copy_size); 34 } 35 36 Texture::Texture(Device* const aParent, RawId aId, 37 const dom::GPUTextureDescriptor& aDesc) 38 : ObjectBase(aParent->GetChild(), aId, ffi::wgpu_client_drop_texture), 39 ChildOf(aParent), 40 mFormat(aDesc.mFormat), 41 mBytesPerBlock(GetBytesPerBlockSingleAspect(aDesc.mFormat)), 42 mSize(ConvertExtent(aDesc.mSize)), 43 mMipLevelCount(aDesc.mMipLevelCount), 44 mSampleCount(aDesc.mSampleCount), 45 mDimension(aDesc.mDimension), 46 mUsage(aDesc.mUsage) {} 47 48 Texture::~Texture() = default; 49 50 already_AddRefed<TextureView> Texture::CreateView( 51 const dom::GPUTextureViewDescriptor& aDesc) { 52 ffi::WGPUTextureViewDescriptor desc = {}; 53 54 webgpu::StringHelper label(aDesc.mLabel); 55 desc.label = label.Get(); 56 57 ffi::WGPUTextureFormat format = {ffi::WGPUTextureFormat_Sentinel}; 58 if (aDesc.mFormat.WasPassed()) { 59 format = ConvertTextureFormat(aDesc.mFormat.Value()); 60 desc.format = &format; 61 } 62 ffi::WGPUTextureViewDimension dimension = 63 ffi::WGPUTextureViewDimension_Sentinel; 64 if (aDesc.mDimension.WasPassed()) { 65 dimension = ffi::WGPUTextureViewDimension(aDesc.mDimension.Value()); 66 desc.dimension = &dimension; 67 } 68 69 // Ideally we'd just do something like "aDesc.mMipLevelCount.ptrOr(nullptr)" 70 // but dom::Optional does not seem to have very many nice things. 71 uint32_t mipCount = 72 aDesc.mMipLevelCount.WasPassed() ? aDesc.mMipLevelCount.Value() : 0; 73 uint32_t layerCount = 74 aDesc.mArrayLayerCount.WasPassed() ? aDesc.mArrayLayerCount.Value() : 0; 75 76 desc.aspect = ffi::WGPUTextureAspect(aDesc.mAspect); 77 desc.base_mip_level = aDesc.mBaseMipLevel; 78 desc.mip_level_count = aDesc.mMipLevelCount.WasPassed() ? &mipCount : nullptr; 79 desc.base_array_layer = aDesc.mBaseArrayLayer; 80 desc.array_layer_count = 81 aDesc.mArrayLayerCount.WasPassed() ? &layerCount : nullptr; 82 83 RawId id = ffi::wgpu_client_create_texture_view(GetClient(), mParent->GetId(), 84 GetId(), &desc); 85 86 RefPtr<TextureView> view = new TextureView(this, id); 87 view->SetLabel(aDesc.mLabel); 88 return view.forget(); 89 } 90 91 void Texture::Destroy() { 92 ffi::wgpu_client_destroy_texture(GetClient(), GetId()); 93 } 94 } // namespace mozilla::webgpu