//********************************** Banshee Engine (www.banshee3d.com) **************************************************// //**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************// #pragma once #include "BsCorePrerequisites.h" #include "BsMaterial.h" #include "BsShader.h" #include "BsPass.h" namespace bs { /** @addtogroup Implementation * @{ */ template struct TGpuParamsType { }; template<> struct TGpuParamsType { typedef GpuParams Type; }; template<> struct TGpuParamsType { typedef ct::GpuParams Type; }; /** Contains a set of GpuParams used for a single technique within a Material. */ template class BS_CORE_EXPORT TGpuParamsSet { typedef typename TGpuParamsType::Type GpuParamsType; typedef typename TMaterialParamsType::Type MaterialParamsType; typedef typename TGpuParamBlockBufferPtrType::Type ParamBlockPtrType; typedef typename TTechniqueType::Type TechniqueType; typedef typename TShaderType::Type ShaderType; typedef typename TPassType::Type PassType; typedef typename TGpuProgramType::Type GpuProgramPtrType; typedef typename TGpuParamBlockBufferType::Type ParamBlockType; typedef typename TGpuParamTextureType::Type TextureType; typedef typename TGpuBufferType::Type BufferType; typedef typename TGpuParamSamplerStateType::Type SamplerStateType; typedef typename TPassTypes::GraphicsPipelineStateType GraphicsPipelineStateType; typedef typename TPassTypes::ComputePipelineStateType ComputePipelineStateType; /** Binding location for a single GPU param block buffer. */ struct BlockBinding { UINT32 set; UINT32 slot; }; /** All bindings for GPU param block buffers, for a single pass. */ struct PassBlockBindings { BlockBinding bindings[GPT_COUNT]; }; /** Information about a parameter block buffer. */ struct BlockInfo { BlockInfo(const String& name, UINT32 set, UINT32 slot, const ParamBlockPtrType& buffer, bool shareable) : name(name), set(set), slot(slot), buffer(buffer), shareable(shareable), allowUpdate(true), isUsed(true) , passData(nullptr) { } String name; UINT32 set; UINT32 slot; ParamBlockPtrType buffer; bool shareable; bool allowUpdate; bool isUsed; PassBlockBindings* passData; }; /** Information about how a data parameter maps from a material parameter into a parameter block buffer. */ struct DataParamInfo { UINT32 paramIdx; UINT32 blockIdx; UINT32 offset; }; /** Information about how an object parameter maps from a material parameter to a GPU stage slot. */ struct ObjectParamInfo { UINT32 paramIdx; UINT32 slotIdx; UINT32 setIdx; }; /** Information about all object parameters for a specific GPU programmable stage. */ struct StageParamInfo { ObjectParamInfo* textures; UINT32 numTextures; ObjectParamInfo* loadStoreTextures; UINT32 numLoadStoreTextures; ObjectParamInfo* buffers; UINT32 numBuffers; ObjectParamInfo* samplerStates; UINT32 numSamplerStates; }; /** Information about all object parameters for a specific pass. */ struct PassParamInfo { StageParamInfo stages[GPT_COUNT]; }; public: TGpuParamsSet() {} TGpuParamsSet(const SPtr& technique, const ShaderType& shader, const SPtr& params); ~TGpuParamsSet(); /** * Returns a set of GPU parameters for the specified pass. * * @param[in] passIdx Pass in which to look the GPU program for in. * @return GPU parameters object that can be used for setting parameters of all GPU programs * in a pass. Returns null if pass doesn't exist. */ SPtr getGpuParams(UINT32 passIdx = 0); /** * Searches for a parameter block buffer with the specified name, and returns an index you can use for accessing it. * Returns -1 if buffer was not found. */ UINT32 getParamBlockBufferIndex(const String& name) const; /** * Assign a parameter block buffer with the specified index to all the relevant child GpuParams. * * @param[in] index Index of the buffer, as retrieved from getParamBlockBufferIndex(). * @param[in] paramBlock Parameter block to assign. * @param[in] ignoreInUpdate If true the buffer will not be updated during the update() call. This is useful * if the caller wishes to manually update the buffer contents externally, to prevent * overwriting manually written data during update. * * @note * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters * in the buffers need only be set once and then reused multiple times. */ void setParamBlockBuffer(UINT32 index, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false); /** * Assign a parameter block buffer with the specified name to all the relevant child GpuParams. * * @param[in] name Name of the buffer to set. * @param[in] paramBlock Parameter block to assign. * @param[in] ignoreInUpdate If true the buffer will not be updated during the update() call. This is useful * if the caller wishes to manually update the buffer contents externally, to prevent * overwriting manually written data during update. * * @note * Parameter block buffers can be used as quick way of setting multiple parameters on a material at once, or * potentially sharing parameters between multiple materials. This reduces driver overhead as the parameters * in the buffers need only be set once and then reused multiple times. */ void setParamBlockBuffer(const String& name, const ParamBlockPtrType& paramBlock, bool ignoreInUpdate = false); /** Returns the number of passes the set contains the parameters for. */ UINT32 getNumPasses() const { return (UINT32)mPassParams.size(); } /** * Updates parameter data in this object from the provided material parameters object. * * @param[in] params Object containing the parameter data to update from. Layout of the object must match * the object used for creating this object (be created for the same shader). * @param[in] updateAll Normally the system will track dirty parameters since the last call to this method, * and only update the dirty ones. Set this to true if you want to force all parameters * to update, regardless of their dirty state. */ void update(const SPtr& params, bool updateAll = false); static const UINT32 NUM_STAGES; private: template friend class TMaterial; Vector> mPassParams; Vector mBlocks; Vector mDataParamInfos; PassParamInfo* mPassParamInfos; UINT64 mParamVersion; UINT8* mData; }; /** Sim thread version of TGpuParamsSet. */ class BS_CORE_EXPORT GpuParamsSet : public TGpuParamsSet { public: GpuParamsSet() { } GpuParamsSet(const SPtr& technique, const HShader& shader, const SPtr& params) :TGpuParamsSet(technique, shader, params) { } }; namespace ct { /** Core thread version of TGpuParamsSet. */ class BS_CORE_EXPORT GpuParamsSet : public TGpuParamsSet { public: GpuParamsSet() { } GpuParamsSet(const SPtr& technique, const SPtr& shader, const SPtr& params) :TGpuParamsSet(technique, shader, params) { } }; } /** @} */ }