• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "dawn_native/ExternalTexture.h"
16 
17 #include "dawn_native/Device.h"
18 #include "dawn_native/ObjectType_autogen.h"
19 #include "dawn_native/Texture.h"
20 
21 #include "dawn_native/dawn_platform.h"
22 
23 namespace dawn_native {
24 
ValidateExternalTexturePlane(const TextureViewBase * textureView,wgpu::TextureFormat format)25     MaybeError ValidateExternalTexturePlane(const TextureViewBase* textureView,
26                                             wgpu::TextureFormat format) {
27         if (textureView->GetFormat().format != format) {
28             return DAWN_VALIDATION_ERROR(
29                 "The external texture descriptor specifies a texture format that is different from "
30                 "at least one of the passed texture views.");
31         }
32 
33         DAWN_INVALID_IF(
34             (textureView->GetTexture()->GetUsage() & wgpu::TextureUsage::TextureBinding) == 0,
35             "The external texture plane (%s) usage (%s) doesn't include the required usage (%s)",
36             textureView, textureView->GetTexture()->GetUsage(), wgpu::TextureUsage::TextureBinding);
37 
38         DAWN_INVALID_IF(textureView->GetDimension() != wgpu::TextureViewDimension::e2D,
39                         "The external texture plane (%s) dimension (%s) is not 2D.", textureView,
40                         textureView->GetDimension());
41 
42         DAWN_INVALID_IF(textureView->GetLevelCount() > 1,
43                         "The external texture plane (%s) mip level count (%u) is not 1.",
44                         textureView, textureView->GetLevelCount());
45 
46         DAWN_INVALID_IF(textureView->GetTexture()->GetSampleCount() != 1,
47                         "The external texture plane (%s) sample count (%u) is not one.",
48                         textureView, textureView->GetTexture()->GetSampleCount());
49 
50         return {};
51     }
52 
ValidateExternalTextureDescriptor(const DeviceBase * device,const ExternalTextureDescriptor * descriptor)53     MaybeError ValidateExternalTextureDescriptor(const DeviceBase* device,
54                                                  const ExternalTextureDescriptor* descriptor) {
55         ASSERT(descriptor);
56         ASSERT(descriptor->plane0);
57 
58         DAWN_TRY(device->ValidateObject(descriptor->plane0));
59 
60         const Format* format;
61         DAWN_TRY_ASSIGN(format, device->GetInternalFormat(descriptor->format));
62         DAWN_UNUSED(format);
63 
64         switch (descriptor->format) {
65             case wgpu::TextureFormat::RGBA8Unorm:
66             case wgpu::TextureFormat::BGRA8Unorm:
67             case wgpu::TextureFormat::RGBA16Float:
68                 DAWN_TRY_CONTEXT(
69                     ValidateExternalTexturePlane(descriptor->plane0, descriptor->format),
70                     "validating plane0 against the external texture format (%s)",
71                     descriptor->format);
72                 break;
73             default:
74                 return DAWN_FORMAT_VALIDATION_ERROR(
75                     "Format (%s) is not a supported external texture format.", descriptor->format);
76         }
77 
78         return {};
79     }
80 
81     // static
Create(DeviceBase * device,const ExternalTextureDescriptor * descriptor)82     ResultOrError<Ref<ExternalTextureBase>> ExternalTextureBase::Create(
83         DeviceBase* device,
84         const ExternalTextureDescriptor* descriptor) {
85         Ref<ExternalTextureBase> externalTexture =
86             AcquireRef(new ExternalTextureBase(device, descriptor));
87         return std::move(externalTexture);
88     }
89 
ExternalTextureBase(DeviceBase * device,const ExternalTextureDescriptor * descriptor)90     ExternalTextureBase::ExternalTextureBase(DeviceBase* device,
91                                              const ExternalTextureDescriptor* descriptor)
92         : ApiObjectBase(device, descriptor->label), mState(ExternalTextureState::Alive) {
93         textureViews[0] = descriptor->plane0;
94         TrackInDevice();
95     }
96 
ExternalTextureBase(DeviceBase * device)97     ExternalTextureBase::ExternalTextureBase(DeviceBase* device)
98         : ApiObjectBase(device, kLabelNotImplemented), mState(ExternalTextureState::Alive) {
99         TrackInDevice();
100     }
101 
ExternalTextureBase(DeviceBase * device,ObjectBase::ErrorTag tag)102     ExternalTextureBase::ExternalTextureBase(DeviceBase* device, ObjectBase::ErrorTag tag)
103         : ApiObjectBase(device, tag) {
104     }
105 
106     const std::array<Ref<TextureViewBase>, kMaxPlanesPerFormat>&
GetTextureViews() const107     ExternalTextureBase::GetTextureViews() const {
108         return textureViews;
109     }
110 
ValidateCanUseInSubmitNow() const111     MaybeError ExternalTextureBase::ValidateCanUseInSubmitNow() const {
112         ASSERT(!IsError());
113         DAWN_INVALID_IF(mState == ExternalTextureState::Destroyed,
114                         "Destroyed external texture %s is used in a submit.", this);
115         return {};
116     }
117 
APIDestroy()118     void ExternalTextureBase::APIDestroy() {
119         if (GetDevice()->ConsumedError(GetDevice()->ValidateObject(this))) {
120             return;
121         }
122         Destroy();
123     }
124 
DestroyImpl()125     void ExternalTextureBase::DestroyImpl() {
126         mState = ExternalTextureState::Destroyed;
127     }
128 
129     // static
MakeError(DeviceBase * device)130     ExternalTextureBase* ExternalTextureBase::MakeError(DeviceBase* device) {
131         return new ExternalTextureBase(device, ObjectBase::kError);
132     }
133 
GetType() const134     ObjectType ExternalTextureBase::GetType() const {
135         return ObjectType::ExternalTexture;
136     }
137 
138 }  // namespace dawn_native
139