1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "BsPrerequisites.h" |
6 | #include "Material/BsMaterialParam.h" |
7 | #include "Math/BsVector2I.h" |
8 | #include "Image/BsColor.h" |
9 | #include "Material/BsShaderVariation.h" |
10 | |
11 | namespace bs |
12 | { |
13 | /** @addtogroup 2D-Internal |
14 | * @{ |
15 | */ |
16 | |
17 | /** Extension structure that can be used by SpriteMaterial%s to access specialized data. */ |
18 | struct |
19 | { |
20 | virtual () = default; |
21 | |
22 | /** Creates a new deep copy of the object. */ |
23 | virtual SPtr<SpriteMaterialExtraInfo> () const |
24 | { |
25 | return bs_shared_ptr_new<SpriteMaterialExtraInfo>(); |
26 | } |
27 | }; |
28 | |
29 | /** Contains information for initializing a sprite material. */ |
30 | struct SpriteMaterialInfo |
31 | { |
32 | SpriteMaterialInfo() { } |
33 | |
34 | /** |
35 | * Creates a new deep copy of the object. This is different from standard copy constructor which will just reference |
36 | * the original "additionalData" field, while this will copy it. |
37 | */ |
38 | SpriteMaterialInfo clone() const |
39 | { |
40 | SpriteMaterialInfo info; |
41 | info.groupId = groupId; |
42 | info.texture = texture; |
43 | info.spriteTexture = spriteTexture; |
44 | info.tint = tint; |
45 | info.animationStartTime = animationStartTime; |
46 | |
47 | if(additionalData != nullptr) |
48 | info.additionalData = additionalData->clone(); |
49 | |
50 | return info; |
51 | } |
52 | |
53 | UINT64 groupId = 0; |
54 | HTexture texture; |
55 | HSpriteTexture spriteTexture; |
56 | Color tint; |
57 | float animationStartTime; |
58 | SPtr<SpriteMaterialExtraInfo> additionalData; |
59 | }; |
60 | |
61 | /** Interfaced implemented by materials used for rendering sprites. This is expected to be used as a singleton. */ |
62 | class BS_EXPORT SpriteMaterial |
63 | { |
64 | public: |
65 | SpriteMaterial(UINT32 id, const HMaterial& material, const ShaderVariation& variation = ShaderVariation::EMPTY, |
66 | bool allowBatching = true); |
67 | virtual ~SpriteMaterial(); |
68 | |
69 | /** Returns the unique ID of the sprite material. */ |
70 | UINT32 getId() const { return mId; }; |
71 | |
72 | /** Determines is this material allowed to be batched with other materials with the same merge hash. */ |
73 | bool allowBatching() const { return mAllowBatching; } |
74 | |
75 | /** |
76 | * Generates a hash value that describes the contents of the sprite material info structure. Returned hash doesn't |
77 | * guarantee that the two objects with the same hash are identical, but rather that the objects are mergeable via |
78 | * merge(). |
79 | */ |
80 | virtual UINT64 getMergeHash(const SpriteMaterialInfo& info) const; |
81 | |
82 | /** |
83 | * Merges two SpriteMaterialInfo%s into one structure. User must guarantee that the two objects are mergeable |
84 | * by ensuring their merge hashes match (by calling getMergeHash()). |
85 | * |
86 | * @param[in, out] mergeInto Object that contains the first part of the data, and will contain the result of the |
87 | * merge. |
88 | * @param[in] mergeFrom Object that contains the second part of the data to merge, which will be merged into |
89 | * the first object. |
90 | */ |
91 | virtual void merge(SpriteMaterialInfo& mergeInto, const SpriteMaterialInfo& mergeFrom) const { } |
92 | |
93 | /** |
94 | * Renders the provided mesh using the current material. |
95 | * |
96 | * @param[in] mesh Mesh to render, containing vertices in screen space. |
97 | * @param[in] subMesh Portion of @p mesh to render. |
98 | * @param[in] texture Optional texture to render the mesh with. |
99 | * @param[in] sampler Optional sampler to render the texture with. |
100 | * @param[in] paramBuffer Buffer containing data GPU parameters. |
101 | * @param[in] additionalData Optional additional data that might be required by the renderer. |
102 | */ |
103 | virtual void render(const SPtr<ct::MeshBase>& mesh, const SubMesh& subMesh, const SPtr<ct::Texture>& texture, |
104 | const SPtr<ct::SamplerState>& sampler, const SPtr<ct::GpuParamBlockBuffer>& paramBuffer, |
105 | const SPtr<SpriteMaterialExtraInfo>& additionalData) const; |
106 | |
107 | protected: |
108 | /** Perform initialization of core-thread specific objects. */ |
109 | virtual void initialize(); |
110 | |
111 | /** Destroys the core thread material. */ |
112 | static void destroy(const SPtr<ct::Material>& material, const SPtr<ct::GpuParamsSet>& params); |
113 | |
114 | UINT32 mId; |
115 | bool mAllowBatching; |
116 | |
117 | // Core thread only (everything below) |
118 | SPtr<ct::Material> mMaterial; |
119 | UINT32 mTechnique; |
120 | std::atomic<bool> mMaterialStored; |
121 | |
122 | SPtr<ct::GpuParamsSet> mParams; |
123 | UINT32 mParamBufferIdx; |
124 | mutable ct::MaterialParamTexture mTextureParam; |
125 | mutable ct::MaterialParamSampState mSamplerParam; |
126 | }; |
127 | |
128 | /** @} */ |
129 | } |
130 | |
131 | |