#pragma once #include "BsCorePrerequisites.h" #include "BsVector2.h" #include "BsVector3.h" #include "BsVector4.h" #include "BsMatrix3.h" #include "BsMatrix4.h" #include "BsMatrixNxM.h" #include "BsColor.h" namespace BansheeEngine { template struct TGpuParamsPtrType { }; template<> struct TGpuParamsPtrType { typedef SPtr Type; }; template<> struct TGpuParamsPtrType { typedef SPtr Type; }; template struct TGpuParamTextureType { }; template<> struct TGpuParamTextureType < false > { typedef HTexture Type; }; template<> struct TGpuParamTextureType < true > { typedef SPtr Type; }; template struct TGpuParamSamplerStateType { }; template<> struct TGpuParamSamplerStateType < false > { typedef SamplerStatePtr Type; }; template<> struct TGpuParamSamplerStateType < true > { typedef SPtr Type; }; /** * @brief A handle that allows you to set a GpuProgram parameter. Internally keeps a reference to the * GPU parameter buffer and the necessary offsets. You should specialize this type for specific * parameter types. * * Object of this type must be returned by a Material. Setting/Getting parameter values will internally * access a GPU parameter buffer attached to the Material this parameter was created from. Anything * rendered with that material will then use those set values. * * @note Normally you can set a GpuProgram parameter by calling various set/get methods on a Material. * This class primarily used an as optimization in performance critical bits of code * where it is important to locate and set parameters quickly without any lookups * (Mentioned set/get methods expect a parameter name). You just retrieve the handle * once and then set the parameter value many times with minimal performance impact. * * @see Material */ template class BS_CORE_EXPORT TGpuDataParam { private: template struct TGpuParamBufferType { }; template<> struct TGpuParamBufferType < false > { typedef SPtr Type; }; template<> struct TGpuParamBufferType < true > { typedef SPtr Type; }; typedef typename TGpuParamBufferType::Type GpuParamBufferType; typedef typename TGpuParamsPtrType::Type GpuParamsType; /** * @brief Policy class that allows us to re-use this template class for matrices which might * need transposing, and other types which do not. Matrix needs to be transposed for * certain render systems depending on how they store them in memory. */ template struct TransposePolicy { static Type transpose(const Type& value) { return value; } static bool transposeEnabled(bool enabled) { return false; } }; /** * @brief Transpose policy for 3x3 matrix. */ template<> struct TransposePolicy { static Matrix3 transpose(const Matrix3& value) { return value.transpose(); } static bool transposeEnabled(bool enabled) { return enabled; } }; /** * @brief Transpose policy for 4x4 matrix. */ template<> struct TransposePolicy { static Matrix4 transpose(const Matrix4& value) { return value.transpose(); } static bool transposeEnabled(bool enabled) { return enabled; } }; /** * @brief Transpose policy for NxM matrix. */ template struct TransposePolicy> { static MatrixNxM transpose(const MatrixNxM& value) { return value.transpose(); } static bool transposeEnabled(bool enabled) { return enabled; } }; public: TGpuDataParam(); TGpuDataParam(GpuParamDataDesc* paramDesc, const GpuParamsType& parent); /** * @brief Sets a parameter value at the specified array index. If parameter does not * contain an array leave the index at 0. * * @note Like with all GPU parameters, the actual GPU buffer will not be updated until rendering * with material this parameter was created from starts on the core thread. */ void set(const T& value, UINT32 arrayIdx = 0); /** * @brief Returns a value of a parameter at the specified array index. If parameter does not * contain an array leave the index at 0. * * @note No GPU reads are done. Data returned was cached when it was written. */ T get(UINT32 arrayIdx = 0); /** * @brief Checks if param is initialized. */ bool operator==(const nullptr_t &nullval) const { return mParamDesc == nullptr; } protected: GpuParamsType mParent; GpuParamDataDesc* mParamDesc; }; /** * @copydoc TGpuDataParam */ template class BS_CORE_EXPORT TGpuParamStruct { public: template struct TGpuParamBufferType { }; template<> struct TGpuParamBufferType < false > { typedef SPtr Type; }; template<> struct TGpuParamBufferType < true > { typedef SPtr Type; }; typedef typename TGpuParamBufferType::Type GpuParamBufferType; typedef typename TGpuParamsPtrType::Type GpuParamsType; TGpuParamStruct(); TGpuParamStruct(GpuParamDataDesc* paramDesc, const GpuParamsType& parent); /** * @copydoc TGpuDataParam::set */ void set(const void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0); /** * @copydoc TGpuDataParam::get */ void get(void* value, UINT32 sizeBytes, UINT32 arrayIdx = 0); /** * @brief Returns the size of the struct in bytes. */ UINT32 getElementSize() const; /** * @brief Checks if param is initialized. */ bool operator==(const nullptr_t &nullval) const { return mParamDesc == nullptr; } protected: GpuParamsType mParent; GpuParamDataDesc* mParamDesc; }; /** * @copydoc TGpuObjectParam */ template class BS_CORE_EXPORT TGpuParamTexture { private: friend class GpuParams; friend class GpuParamsCore; typedef typename TGpuParamsPtrType::Type GpuParamsType; typedef typename TGpuParamTextureType::Type TextureType; public: TGpuParamTexture(); TGpuParamTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent); /** * @copydoc TGpuDataParam::set */ void set(const TextureType& texture); /** * @copydoc TGpuDataParam::get */ TextureType get(); /** * @brief Checks if param is initialized. */ bool operator==(const nullptr_t &nullval) const { return mParamDesc == nullptr; } protected: GpuParamsType mParent; GpuParamObjectDesc* mParamDesc; }; /** * @copydoc TGpuObjectParam */ template class BS_CORE_EXPORT TGpuParamLoadStoreTexture { private: friend class GpuParams; friend class GpuParamsCore; typedef typename TGpuParamsPtrType::Type GpuParamsType; typedef typename TGpuParamTextureType::Type TextureType; public: TGpuParamLoadStoreTexture(); TGpuParamLoadStoreTexture(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent); /** * @copydoc TGpuDataParam::set */ void set(const TextureType& texture, const TextureSurface& surface); /** * @copydoc TGpuDataParam::get */ TextureType get(); /** * @brief Checks if param is initialized. */ bool operator==(const nullptr_t &nullval) const { return mParamDesc == nullptr; } protected: GpuParamsType mParent; GpuParamObjectDesc* mParamDesc; }; /** * @copydoc TGpuObjectParam */ template class BS_CORE_EXPORT TGpuParamSampState { private: friend class GpuParams; friend class GpuParamsCore; typedef typename TGpuParamsPtrType::Type GpuParamsType; typedef typename TGpuParamSamplerStateType::Type SamplerStateType; public: TGpuParamSampState(); TGpuParamSampState(GpuParamObjectDesc* paramDesc, const GpuParamsType& parent); /** * @copydoc TGpuDataParam::set */ void set(const SamplerStateType& samplerState); /** * @copydoc TGpuDataParam::get */ SamplerStateType get(); /** * @brief Checks if param is initialized. */ bool operator==(const nullptr_t &nullval) const { return mParamDesc == nullptr; } protected: GpuParamsType mParent; GpuParamObjectDesc* mParamDesc; }; typedef TGpuDataParam GpuParamFloat; typedef TGpuDataParam GpuParamColor; typedef TGpuDataParam GpuParamVec2; typedef TGpuDataParam GpuParamVec3; typedef TGpuDataParam GpuParamVec4; typedef TGpuDataParam GpuParamMat3; typedef TGpuDataParam GpuParamMat4; typedef TGpuDataParam GpuParamFloatCore; typedef TGpuDataParam GpuParamColorCore; typedef TGpuDataParam GpuParamVec2Core; typedef TGpuDataParam GpuParamVec3Core; typedef TGpuDataParam GpuParamVec4Core; typedef TGpuDataParam GpuParamMat3Core; typedef TGpuDataParam GpuParamMat4Core; typedef TGpuParamStruct GpuParamStruct; typedef TGpuParamStruct GpuParamStructCore; typedef TGpuParamTexture GpuParamTexture; typedef TGpuParamTexture GpuParamTextureCore; typedef TGpuParamSampState GpuParamSampState; typedef TGpuParamSampState GpuParamSampStateCore; typedef TGpuParamLoadStoreTexture GpuParamLoadStoreTexture; typedef TGpuParamLoadStoreTexture GpuParamLoadStoreTextureCore; }