| 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 "BsCorePrerequisites.h" |
| 6 | #include "RenderAPI/BsGpuParam.h" |
| 7 | #include "RenderAPI/BsGpuParams.h" |
| 8 | #include "Animation/BsAnimationCurve.h" |
| 9 | |
| 10 | namespace bs |
| 11 | { |
| 12 | template <class T> |
| 13 | class TAnimationCurve; |
| 14 | class ColorGradient; |
| 15 | |
| 16 | /** @addtogroup Implementation |
| 17 | * @{ |
| 18 | */ |
| 19 | |
| 20 | /** Common functionality for all material data params. */ |
| 21 | template<int DATA_TYPE, bool Core> |
| 22 | class BS_CORE_EXPORT TMaterialDataCommon |
| 23 | { |
| 24 | protected: |
| 25 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 26 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 27 | |
| 28 | public: |
| 29 | TMaterialDataCommon() = default; |
| 30 | TMaterialDataCommon(const String& name, const MaterialPtrType& material); |
| 31 | |
| 32 | /** Checks if param is initialized. */ |
| 33 | bool operator==(const std::nullptr_t& nullval) const |
| 34 | { |
| 35 | return mMaterial == nullptr; |
| 36 | } |
| 37 | |
| 38 | protected: |
| 39 | UINT32 mParamIndex; |
| 40 | UINT32 mArraySize; |
| 41 | MaterialPtrType mMaterial; |
| 42 | }; |
| 43 | |
| 44 | /** |
| 45 | * A handle that allows you to set a Material parameter. Internally keeps a reference to the material parameters so that |
| 46 | * possibly expensive lookup of parameter name can be avoided each time the parameter is accessed, and instead the |
| 47 | * handle can be cached. |
| 48 | * |
| 49 | * @note |
| 50 | * This is pretty much identical to GPU parameter version (for example TGpuDataParam), except that this will get/set |
| 51 | * parameter values on all GPU programs attached to the material, while TGpuDataParam works only for single GPU |
| 52 | * program's parameters. Also, additional parameters that might be optimized out in the GPU program will still exist |
| 53 | * here as long as they're defined in the shader used by the material, which is not the case with TGpuDataParam. |
| 54 | * @note |
| 55 | * For core-thread version of this class no shader-based caching is done, and instead this represents just a wrapper |
| 56 | * for multiple GPU parameters. |
| 57 | * |
| 58 | * @see Material |
| 59 | */ |
| 60 | template<class T, bool Core> |
| 61 | class BS_CORE_EXPORT TMaterialDataParam : public TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core> |
| 62 | { |
| 63 | using Base = TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core>; |
| 64 | |
| 65 | public: |
| 66 | using TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core>::TMaterialDataCommon; |
| 67 | |
| 68 | /** @copydoc TGpuDataParam::set */ |
| 69 | void set(const T& value, UINT32 arrayIdx = 0) const; |
| 70 | |
| 71 | /** @copydoc TGpuDataParam::get */ |
| 72 | T get(UINT32 arrayIdx = 0) const; |
| 73 | }; |
| 74 | |
| 75 | /** @copydoc TMaterialDataParam */ |
| 76 | template<class T, bool Core> |
| 77 | class BS_CORE_EXPORT TMaterialCurveParam : public TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core> |
| 78 | { |
| 79 | using Base = TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core>; |
| 80 | |
| 81 | public: |
| 82 | using TMaterialDataCommon<TGpuDataParamInfo<T>::TypeId, Core>::TMaterialDataCommon; |
| 83 | |
| 84 | /** @copydoc TGpuDataParam::set */ |
| 85 | void set(TAnimationCurve<T> value, UINT32 arrayIdx = 0) const; |
| 86 | |
| 87 | /** @copydoc TGpuDataParam::get */ |
| 88 | const TAnimationCurve<T>& get(UINT32 arrayIdx = 0) const; |
| 89 | }; |
| 90 | |
| 91 | /** @copydoc TMaterialDataParam */ |
| 92 | template<bool Core> |
| 93 | class BS_CORE_EXPORT TMaterialColorGradientParam : public TMaterialDataCommon<GPDT_COLOR, Core> |
| 94 | { |
| 95 | using Base = TMaterialDataCommon<GPDT_COLOR, Core>; |
| 96 | |
| 97 | public: |
| 98 | using TMaterialDataCommon<GPDT_COLOR, Core>::TMaterialDataCommon; |
| 99 | |
| 100 | /** @copydoc TGpuDataParam::set */ |
| 101 | void set(const ColorGradient& value, UINT32 arrayIdx = 0) const; |
| 102 | |
| 103 | /** @copydoc TGpuDataParam::get */ |
| 104 | const ColorGradient& get(UINT32 arrayIdx = 0) const; |
| 105 | }; |
| 106 | |
| 107 | /** @copydoc TMaterialDataParam */ |
| 108 | template<bool Core> |
| 109 | class BS_CORE_EXPORT TMaterialParamStruct : public TMaterialDataCommon<GPDT_STRUCT, Core> |
| 110 | { |
| 111 | using Base = TMaterialDataCommon<GPDT_STRUCT, Core>; |
| 112 | |
| 113 | public: |
| 114 | using TMaterialDataCommon<GPDT_STRUCT, Core>::TMaterialDataCommon; |
| 115 | |
| 116 | /** @copydoc TGpuParamStruct::set */ |
| 117 | void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const; |
| 118 | |
| 119 | /** @copydoc TGpuParamStruct::get */ |
| 120 | void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0) const; |
| 121 | |
| 122 | /** @copydoc TGpuParamStruct::getElementSize */ |
| 123 | UINT32 getElementSize() const; |
| 124 | }; |
| 125 | |
| 126 | /** @copydoc TMaterialDataParam */ |
| 127 | template<bool Core> |
| 128 | class BS_CORE_EXPORT TMaterialParamTexture |
| 129 | { |
| 130 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 131 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 132 | using TextureType = CoreVariantHandleType<Texture, Core>; |
| 133 | |
| 134 | public: |
| 135 | TMaterialParamTexture(const String& name, const MaterialPtrType& material); |
| 136 | TMaterialParamTexture() { } |
| 137 | |
| 138 | /** @copydoc GpuParamTexture::set */ |
| 139 | void set(const TextureType& texture, const TextureSurface& surface = TextureSurface::COMPLETE) const; |
| 140 | |
| 141 | /** @copydoc GpuParamTexture::get */ |
| 142 | TextureType get() const; |
| 143 | |
| 144 | /** Checks if param is initialized. */ |
| 145 | bool operator==(const std::nullptr_t& nullval) const |
| 146 | { |
| 147 | return mMaterial == nullptr; |
| 148 | } |
| 149 | |
| 150 | protected: |
| 151 | UINT32 mParamIndex; |
| 152 | MaterialPtrType mMaterial; |
| 153 | }; |
| 154 | |
| 155 | /** @copydoc TMaterialDataParam */ |
| 156 | template<bool Core> |
| 157 | class BS_CORE_EXPORT TMaterialParamSpriteTexture |
| 158 | { |
| 159 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 160 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 161 | using SpriteTextureType = CoreVariantHandleType<SpriteTexture, Core>; |
| 162 | using TextureType = CoreVariantHandleType<Texture, Core>; |
| 163 | |
| 164 | public: |
| 165 | TMaterialParamSpriteTexture(const String& name, const MaterialPtrType& material); |
| 166 | TMaterialParamSpriteTexture() { } |
| 167 | |
| 168 | /** @copydoc GpuParamTexture::set */ |
| 169 | void set(const SpriteTextureType& texture)const; |
| 170 | |
| 171 | /** @copydoc GpuParamTexture::get */ |
| 172 | SpriteTextureType get() const; |
| 173 | |
| 174 | /** Checks if param is initialized. */ |
| 175 | bool operator==(const std::nullptr_t& nullval) const |
| 176 | { |
| 177 | return mMaterial == nullptr; |
| 178 | } |
| 179 | |
| 180 | protected: |
| 181 | UINT32 mParamIndex; |
| 182 | MaterialPtrType mMaterial; |
| 183 | }; |
| 184 | |
| 185 | /** @copydoc TMaterialDataParam */ |
| 186 | template<bool Core> |
| 187 | class BS_CORE_EXPORT TMaterialParamLoadStoreTexture |
| 188 | { |
| 189 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 190 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 191 | using TextureType = CoreVariantHandleType<Texture, Core>; |
| 192 | |
| 193 | public: |
| 194 | TMaterialParamLoadStoreTexture(const String& name, const MaterialPtrType& material); |
| 195 | TMaterialParamLoadStoreTexture() { } |
| 196 | |
| 197 | /** @copydoc GpuParamLoadStoreTexture::set */ |
| 198 | void set(const TextureType& texture, const TextureSurface& surface = TextureSurface()) const; |
| 199 | |
| 200 | /** @copydoc GpuParamLoadStoreTexture::get */ |
| 201 | TextureType get() const; |
| 202 | |
| 203 | /** Checks if param is initialized. */ |
| 204 | bool operator==(const std::nullptr_t& nullval) const |
| 205 | { |
| 206 | return mMaterial == nullptr; |
| 207 | } |
| 208 | |
| 209 | protected: |
| 210 | UINT32 mParamIndex; |
| 211 | MaterialPtrType mMaterial; |
| 212 | }; |
| 213 | |
| 214 | /** @copydoc TMaterialDataParam */ |
| 215 | template<bool Core> |
| 216 | class BS_CORE_EXPORT TMaterialParamBuffer |
| 217 | { |
| 218 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 219 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 220 | using BufferType = SPtr<CoreVariantType<GpuBuffer, Core>>; |
| 221 | |
| 222 | public: |
| 223 | TMaterialParamBuffer(const String& name, const MaterialPtrType& material); |
| 224 | TMaterialParamBuffer() { } |
| 225 | |
| 226 | /** @copydoc GpuParamBuffer::set */ |
| 227 | void set(const BufferType& buffer) const; |
| 228 | |
| 229 | /** @copydoc GpuParamBuffer::get */ |
| 230 | BufferType get() const; |
| 231 | |
| 232 | /** Checks if param is initialized. */ |
| 233 | bool operator==(const std::nullptr_t& nullval) const |
| 234 | { |
| 235 | return mMaterial == nullptr; |
| 236 | } |
| 237 | |
| 238 | protected: |
| 239 | UINT32 mParamIndex; |
| 240 | MaterialPtrType mMaterial; |
| 241 | }; |
| 242 | |
| 243 | /** @copydoc TMaterialDataParam */ |
| 244 | template<bool Core> |
| 245 | class BS_CORE_EXPORT TMaterialParamSampState |
| 246 | { |
| 247 | using MaterialPtrType = SPtr<CoreVariantType<Material, Core>>; |
| 248 | using MaterialParamsType = CoreVariantType<MaterialParams, Core>; |
| 249 | using SamplerStateType = SPtr<CoreVariantType<SamplerState, Core>>; |
| 250 | |
| 251 | public: |
| 252 | TMaterialParamSampState(const String& name, const MaterialPtrType& material); |
| 253 | TMaterialParamSampState() { } |
| 254 | |
| 255 | /** @copydoc GpuParamSampState::set */ |
| 256 | void set(const SamplerStateType& sampState) const; |
| 257 | |
| 258 | /** @copydoc GpuParamSampState::get */ |
| 259 | SamplerStateType get() const; |
| 260 | |
| 261 | /** Checks if param is initialized. */ |
| 262 | bool operator==(const std::nullptr_t& nullval) const |
| 263 | { |
| 264 | return mMaterial == nullptr; |
| 265 | } |
| 266 | |
| 267 | protected: |
| 268 | UINT32 mParamIndex; |
| 269 | MaterialPtrType mMaterial; |
| 270 | }; |
| 271 | |
| 272 | /** @} */ |
| 273 | |
| 274 | /** @addtogroup Material |
| 275 | * @{ |
| 276 | */ |
| 277 | |
| 278 | typedef TMaterialDataParam<float, false> MaterialParamFloat; |
| 279 | typedef TMaterialDataParam<Vector2, false> MaterialParamVec2; |
| 280 | typedef TMaterialDataParam<Vector3, false> MaterialParamVec3; |
| 281 | typedef TMaterialDataParam<Vector4, false> MaterialParamVec4; |
| 282 | typedef TMaterialDataParam<int, false> MaterialParamInt; |
| 283 | typedef TMaterialDataParam<Vector2I, false> MaterialParamVec2I; |
| 284 | typedef TMaterialDataParam<Vector3I, false> MaterialParamVec3I; |
| 285 | typedef TMaterialDataParam<Vector4I, false> MaterialParamVec4I; |
| 286 | typedef TMaterialDataParam<Matrix3, false> MaterialParamMat3; |
| 287 | typedef TMaterialDataParam<Matrix4, false> MaterialParamMat4; |
| 288 | typedef TMaterialDataParam<Color, false> MaterialParamColor; |
| 289 | |
| 290 | typedef TMaterialParamStruct<false> MaterialParamStruct; |
| 291 | typedef TMaterialParamTexture<false> MaterialParamTexture; |
| 292 | typedef TMaterialParamLoadStoreTexture<false> MaterialParamLoadStoreTexture; |
| 293 | typedef TMaterialParamBuffer<false> MaterialParamBuffer; |
| 294 | typedef TMaterialParamSampState<false> MaterialParamSampState; |
| 295 | |
| 296 | typedef TMaterialCurveParam<float, false> MaterialParamFloatCurve; |
| 297 | typedef TMaterialColorGradientParam<false> MaterialParamColorGradient; |
| 298 | typedef TMaterialParamSpriteTexture<false> MaterialParamSpriteTexture; |
| 299 | |
| 300 | namespace ct |
| 301 | { |
| 302 | typedef TMaterialDataParam<float, true> MaterialParamFloat; |
| 303 | typedef TMaterialDataParam<Vector2, true> MaterialParamVec2; |
| 304 | typedef TMaterialDataParam<Vector3, true> MaterialParamVec3; |
| 305 | typedef TMaterialDataParam<Vector4, true> MaterialParamVec4; |
| 306 | typedef TMaterialDataParam<int, true> MaterialParamInt; |
| 307 | typedef TMaterialDataParam<Vector2I, true> MaterialParamVec2I; |
| 308 | typedef TMaterialDataParam<Vector3I, true> MaterialParamVec3I; |
| 309 | typedef TMaterialDataParam<Vector4I, true> MaterialParamVec4I; |
| 310 | typedef TMaterialDataParam<Matrix3, true> MaterialParamMat3; |
| 311 | typedef TMaterialDataParam<Matrix4, true> MaterialParamMat4; |
| 312 | typedef TMaterialDataParam<Color, true> MaterialParamColor; |
| 313 | |
| 314 | typedef TMaterialParamStruct<true> MaterialParamStruct; |
| 315 | typedef TMaterialParamTexture<true> MaterialParamTexture; |
| 316 | typedef TMaterialParamLoadStoreTexture<true> MaterialParamLoadStoreTexture; |
| 317 | typedef TMaterialParamBuffer<true> MaterialParamBuffer; |
| 318 | typedef TMaterialParamSampState<true> MaterialParamSampState; |
| 319 | |
| 320 | typedef TMaterialCurveParam<float, true> MaterialParamFloatCurve; |
| 321 | typedef TMaterialColorGradientParam<true> MaterialParamColorGradient; |
| 322 | typedef TMaterialParamSpriteTexture<true> MaterialParamSpriteTexture; |
| 323 | } |
| 324 | |
| 325 | /** @} */ |
| 326 | } |