• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
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 
16 #ifndef SCENE_SRC_MESH_SAMPLER_H
17 #define SCENE_SRC_MESH_SAMPLER_H
18 
19 #include <scene/ext/component.h>
20 #include <scene/interface/intf_texture.h>
21 
22 #include <meta/ext/implementation_macros.h>
23 #include <meta/ext/object.h>
24 #include <meta/interface/interface_helpers.h>
25 #include <meta/interface/resource/intf_dynamic_resource.h>
26 
27 SCENE_BEGIN_NAMESPACE()
28 
29 META_REGISTER_CLASS(Sampler, "29637952-7747-4233-bdbf-16e51405d2e9", META_NS::ObjectCategoryBits::NO_CATEGORY)
30 
31 class ISamplerInternal : public CORE_NS::IInterface {
32     META_INTERFACE(CORE_NS::IInterface, ISamplerInternal, "20f22389-2662-4890-9d40-8e2401748f8e")
33 public:
34     virtual void SetScene(const IInternalScene::Ptr& scene) = 0;
35     virtual bool UpdateSamplerFromHandle(
36         RENDER_NS::IRenderContext& context, const RENDER_NS::RenderHandleReference& handle) = 0;
37     virtual RENDER_NS::RenderHandleReference GetHandleFromSampler(RENDER_NS::IRenderContext& context) const = 0;
38 };
39 
40 class Sampler
41     : public META_NS::IntroduceInterfaces<META_NS::MetaObject, ISampler, ISamplerInternal, META_NS::IPropertyOwner,
42           META_NS::IDynamicResource, META_NS::IMetadataOwner, META_NS::IResetableObject> {
43 public:
44     META_OBJECT(Sampler, ClassId::Sampler, IntroduceInterfaces)
45     META_BEGIN_STATIC_DATA()
46     META_STATIC_PROPERTY_DATA(ISampler, SamplerFilter, MagFilter)
47     META_STATIC_PROPERTY_DATA(ISampler, SamplerFilter, MinFilter)
48     META_STATIC_PROPERTY_DATA(ISampler, SamplerFilter, MipMapMode)
49     META_STATIC_PROPERTY_DATA(ISampler, SamplerAddressMode, AddressModeU)
50     META_STATIC_PROPERTY_DATA(ISampler, SamplerAddressMode, AddressModeV)
51     META_STATIC_PROPERTY_DATA(ISampler, SamplerAddressMode, AddressModeW)
52     META_STATIC_EVENT_DATA(META_NS::IDynamicResource, META_NS::IOnChanged, OnResourceChanged)
53     META_END_STATIC_DATA()
54 
55     META_IMPLEMENT_PROPERTY(SamplerFilter, MagFilter)
56     META_IMPLEMENT_PROPERTY(SamplerFilter, MinFilter)
57     META_IMPLEMENT_PROPERTY(SamplerFilter, MipMapMode)
58     META_IMPLEMENT_PROPERTY(SamplerAddressMode, AddressModeU)
59     META_IMPLEMENT_PROPERTY(SamplerAddressMode, AddressModeV)
60     META_IMPLEMENT_PROPERTY(SamplerAddressMode, AddressModeW)
61 
62     META_IMPLEMENT_EVENT(META_NS::IOnChanged, OnResourceChanged)
63 
64 public:
65     void OnMetadataConstructed(const META_NS::StaticMetadata& m, CORE_NS::IInterface& i) override;
66     void OnPropertyChanged(const META_NS::IProperty&) override;
67 
68 protected: // ISamplerInternal
69     void SetScene(const IInternalScene::Ptr& scene) override;
70     bool UpdateSamplerFromHandle(
71         RENDER_NS::IRenderContext& context, const RENDER_NS::RenderHandleReference& handle) override;
72     RENDER_NS::RenderHandleReference GetHandleFromSampler(RENDER_NS::IRenderContext& context) const override;
73 
74 protected: // IResetableObject
75     void ResetObject() override;
76 
77 private:
78     struct SamplerInfo {
79         RENDER_NS::RenderHandleReference handle;
80         RENDER_NS::GpuSamplerDesc descriptor;
81         bool isDefault {};
82     };
83 
84     const META_NS::IProperty* GetExistingProperty(BASE_NS::string_view name) const;
85     META_NS::IProperty* GetExistingProperty(BASE_NS::string_view name);
86     RENDER_NS::RenderHandleReference GetSamplerHandle(const Sampler::SamplerInfo& info) const noexcept;
87     SamplerInfo GetDefaultSampler() const;
88     mutable SamplerInfo sampler_;
89     IInternalScene::WeakPtr scene_;
90 
91 private: // Transaction related
92     bool CanNotifyChanged();
93     void NotifyChanged();
94     void StartTransaction();
95     void EndTransaction(bool changed);
96     mutable CORE_NS::Mutex transaction_;
97     uint32_t setting_ {};
98     bool changedDuringTransaction_ {};
99 };
100 
101 SCENE_END_NAMESPACE()
102 
103 #endif // SCENE_SRC_MESH_SAMPLER_H
104