| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #pragma once
- #include "BsCorePrerequisites.h"
- #include "RenderAPI/BsGpuParam.h"
- #include "CoreThread/BsCoreObject.h"
- #include "Resources/BsIResourceListener.h"
- #include "Math/BsVectorNI.h"
- #include "Math/BsMatrixNxM.h"
- namespace bs
- {
- /** @addtogroup Implementation
- * @{
- */
- /** Helper structure whose specializations convert an engine data type into a GPU program data parameter type. */
- template<class T> struct TGpuDataParamInfo { };
- template<> struct TGpuDataParamInfo < float > { enum { TypeId = GPDT_FLOAT1 }; };
- template<> struct TGpuDataParamInfo < Vector2 > { enum { TypeId = GPDT_FLOAT2 }; };
- template<> struct TGpuDataParamInfo < Vector3 > { enum { TypeId = GPDT_FLOAT3 }; };
- template<> struct TGpuDataParamInfo < Vector4 > { enum { TypeId = GPDT_FLOAT4 }; };
- template<> struct TGpuDataParamInfo < int > { enum { TypeId = GPDT_INT1 }; };
- template<> struct TGpuDataParamInfo < Vector2I > { enum { TypeId = GPDT_INT2 }; };
- template<> struct TGpuDataParamInfo < Vector3I > { enum { TypeId = GPDT_INT3 }; };
- template<> struct TGpuDataParamInfo < Vector4I > { enum { TypeId = GPDT_INT4 }; };
- template<> struct TGpuDataParamInfo < Matrix2 > { enum { TypeId = GPDT_MATRIX_2X2 }; };
- template<> struct TGpuDataParamInfo < Matrix2x3 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
- template<> struct TGpuDataParamInfo < Matrix2x4 > { enum { TypeId = GPDT_MATRIX_2X3 }; };
- template<> struct TGpuDataParamInfo < Matrix3 > { enum { TypeId = GPDT_MATRIX_3X3 }; };
- template<> struct TGpuDataParamInfo < Matrix3x2 > { enum { TypeId = GPDT_MATRIX_3X2 }; };
- template<> struct TGpuDataParamInfo < Matrix3x4 > { enum { TypeId = GPDT_MATRIX_3X4 }; };
- template<> struct TGpuDataParamInfo < Matrix4 > { enum { TypeId = GPDT_MATRIX_4X4 }; };
- template<> struct TGpuDataParamInfo < Matrix4x2 > { enum { TypeId = GPDT_MATRIX_4X2 }; };
- template<> struct TGpuDataParamInfo < Matrix4x3 > { enum { TypeId = GPDT_MATRIX_4X3 }; };
- template<> struct TGpuDataParamInfo < Color > { enum { TypeId = GPDT_COLOR }; };
- class GpuPipelineParamInfoBase;
- /** Contains functionality common for both sim and core thread version of GpuParams. */
- class BS_CORE_EXPORT GpuParamsBase
- {
- public:
- virtual ~GpuParamsBase();
- // Note: Disallow copy/assign because it would require some care when copying (copy internal data shared_ptr and
- // all the internal buffers too). Trivial to implement but not needed at this time. Un-delete and implement if necessary.
- GpuParamsBase(const GpuParamsBase& other) = delete;
- GpuParamsBase& operator=(const GpuParamsBase& rhs) = delete;
- /** Returns a description of all stored parameters. */
- SPtr<GpuParamDesc> getParamDesc(GpuProgramType type) const;
- /** Gets the object that contains the processed information about all parameters. */
- SPtr<GpuPipelineParamInfoBase> getParamInfo() const { return mParamInfo; }
- /**
- * Returns the size of a data parameter with the specified name, in bytes. Returns 0 if such parameter doesn't exist.
- */
- UINT32 getDataParamSize(GpuProgramType type, const String& name) const;
- /** Checks if parameter with the specified name exists. */
- bool hasParam(GpuProgramType type, const String& name) const;
- /** Checks if texture parameter with the specified name exists. */
- bool hasTexture(GpuProgramType type, const String& name) const;
- /** Checks if load/store texture parameter with the specified name exists. */
- bool hasLoadStoreTexture(GpuProgramType type, const String& name) const;
- /** Checks if buffer parameter with the specified name exists. */
- bool hasBuffer(GpuProgramType type, const String& name) const;
- /** Checks if sampler state parameter with the specified name exists. */
- bool hasSamplerState(GpuProgramType type, const String& name) const;
- /** Checks if a parameter block with the specified name exists. */
- bool hasParamBlock(GpuProgramType type, const String& name) const;
- /** Gets a descriptor for a parameter block buffer with the specified name. */
- GpuParamBlockDesc* getParamBlockDesc(GpuProgramType type, const String& name) const;
- /** Marks the sim thread object as dirty, causing it to sync its contents with its core thread counterpart. */
- virtual void _markCoreDirty() { }
- /** @copydoc IResourceListener::markListenerResourcesDirty */
- virtual void _markResourcesDirty() { }
- protected:
- GpuParamsBase(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
- /** Gets a descriptor for a data parameter with the specified name. */
- GpuParamDataDesc* getParamDesc(GpuProgramType type, const String& name) const;
- SPtr<GpuPipelineParamInfoBase> mParamInfo;
- };
- template<bool Core> struct TGpuParamsTypes { };
- template<> struct TGpuParamsTypes < false >
- {
- typedef GpuParams GpuParamsType;
- typedef HTexture TextureType;
- typedef SPtr<GpuBuffer> BufferType;
- typedef SPtr<SamplerState> SamplerType;
- typedef SPtr<GpuParamBlockBuffer> ParamsBufferType;
- };
- template<> struct TGpuParamsTypes < true >
- {
- typedef ct::GpuParams GpuParamsType;
- typedef SPtr<ct::Texture> TextureType;
- typedef SPtr<ct::GpuBuffer> BufferType;
- typedef SPtr<ct::SamplerState> SamplerType;
- typedef SPtr<ct::GpuParamBlockBuffer> ParamsBufferType;
- };
- /** Templated version of GpuParams that contains functionality for both sim and core thread versions of stored data. */
- template <bool Core>
- class BS_CORE_EXPORT TGpuParams : public GpuParamsBase
- {
- public:
- typedef typename TGpuParamsTypes<Core>::GpuParamsType GpuParamsType;
- typedef typename TGpuParamsTypes<Core>::TextureType TextureType;
- typedef typename TGpuParamsTypes<Core>::BufferType BufferType;
- typedef typename TGpuParamsTypes<Core>::SamplerType SamplerType;
- typedef typename TGpuParamsTypes<Core>::ParamsBufferType ParamsBufferType;
- virtual ~TGpuParams();
- /**
- * Returns a handle for the parameter with the specified name. Handle may then be stored and used for quickly
- * setting or retrieving values to/from that parameter.
- *
- * Throws exception if parameter with that name and type doesn't exist.
- *
- * Parameter handles will be invalidated when their parent GpuParams object changes.
- */
- template<class T> void getParam(GpuProgramType type, const String& name, TGpuDataParam<T, Core>& output) const;
- /** @copydoc getParam */
- void getStructParam(GpuProgramType type, const String& name, TGpuParamStruct<Core>& output) const;
- /** @copydoc getParam */
- void getTextureParam(GpuProgramType type, const String& name, TGpuParamTexture<Core>& output) const;
- /** @copydoc getParam */
- void getLoadStoreTextureParam(GpuProgramType type, const String& name, TGpuParamLoadStoreTexture<Core>& output) const;
- /** @copydoc getParam */
- void getBufferParam(GpuProgramType type, const String& name, TGpuParamBuffer<Core>& output) const;
- /** @copydoc getParam */
- void getSamplerStateParam(GpuProgramType type, const String& name, TGpuParamSampState<Core>& output) const;
- /** Gets a parameter block buffer from the specified set/slot combination. */
- ParamsBufferType getParamBlockBuffer(UINT32 set, UINT32 slot) const;
- /** Gets a texture bound to the specified set/slot combination. */
- TextureType getTexture(UINT32 set, UINT32 slot) const;
- /** Gets a load/store texture bound to the specified set/slot combination. */
- TextureType getLoadStoreTexture(UINT32 set, UINT32 slot) const;
- /** Gets a buffer bound to the specified set/slot combination. */
- BufferType getBuffer(UINT32 set, UINT32 slot) const;
- /** Gets a sampler state bound to the specified set/slot combination. */
- SamplerType getSamplerState(UINT32 set, UINT32 slot) const;
- /** Gets information that determines which texture surfaces to bind as a sampled texture parameter. */
- const TextureSurface& getTextureSurface(UINT32 set, UINT32 slot) const;
- /** Gets information that determines which texture surfaces to bind as a load/store parameter. */
- const TextureSurface& getLoadStoreSurface(UINT32 set, UINT32 slot) const;
- /**
- * Sets the parameter buffer with the specified name. Any following parameter reads or writes that are
- * referencing that buffer will use the new buffer.
- *
- * @note
- * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
- * values once and then share the buffer among all other GpuParams.
- * @note
- * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
- */
- void setParamBlockBuffer(GpuProgramType type, const String& name, const ParamsBufferType& paramBlockBuffer);
- /**
- * Binds a new parameter buffer to the specified slot. Any following parameter reads or writes that are referencing
- * that buffer slot will use the new buffer.
- *
- * @note
- * This is useful if you want to share a parameter buffer among multiple GPU programs. You would only set the
- * values once and then share the buffer among all other GpuParams.
- * @note
- * It is up to the caller to guarantee the provided buffer matches parameter block descriptor for this slot.
- */
- virtual void setParamBlockBuffer(UINT32 set, UINT32 slot, const ParamsBufferType& paramBlockBuffer);
- /** Sets a texture at the specified set/slot combination. */
- virtual void setTexture(UINT32 set, UINT32 slot, const TextureType& texture,
- const TextureSurface& surface = TextureSurface::COMPLETE);
- /** Sets a load/store texture at the specified set/slot combination. */
- virtual void setLoadStoreTexture(UINT32 set, UINT32 slot, const TextureType& texture, const TextureSurface& surface);
- /** Sets a buffer at the specified set/slot combination. */
- virtual void setBuffer(UINT32 set, UINT32 slot, const BufferType& buffer);
- /** Sets a sampler state at the specified set/slot combination. */
- virtual void setSamplerState(UINT32 set, UINT32 slot, const SamplerType& sampler);
- /** Assigns a data value to the parameter with the specified name. */
- template<class T> void setParam(GpuProgramType type, const String& name, const T& value)
- { TGpuDataParam<T, Core> param; getParam(type, name, param); param.set(value); }
- /** Assigns a texture to the parameter with the specified name. */
- void setTexture(GpuProgramType type, const String& name, const TextureType& texture,
- const TextureSurface& surface = TextureSurface::COMPLETE)
- { TGpuParamTexture<Core> param; getTextureParam(type, name, param); param.set(texture, surface); }
- /** Assigns a load/store texture to the parameter with the specified name. */
- void setLoadStoreTexture(GpuProgramType type, const String& name, const TextureType& texture, const TextureSurface& surface)
- { TGpuParamLoadStoreTexture<Core> param; getLoadStoreTextureParam(type, name, param); param.set(texture, surface); }
- /** Assigns a buffer to the parameter with the specified name. */
- void setBuffer(GpuProgramType type, const String& name, const BufferType& buffer)
- { TGpuParamBuffer<Core> param; getBufferParam(type, name, param); param.set(buffer); }
- /** Assigns a sampler state to the parameter with the specified name. */
- void setSamplerState(GpuProgramType type, const String& name, const SamplerType& sampler)
- { TGpuParamSampState<Core> param; getSamplerStateParam(type, name, param); param.set(sampler); }
- protected:
- TGpuParams(const SPtr<GpuPipelineParamInfoBase>& paramInfo);
- /** @copydoc CoreObject::getThisPtr */
- virtual SPtr<GpuParamsType> _getThisPtr() const = 0;
- /** Data for a single bound texture. */
- struct TextureData
- {
- TextureType texture;
- TextureSurface surface;
- };
- ParamsBufferType* mParamBlockBuffers = nullptr;
- TextureData* mSampledTextureData = nullptr;
- TextureData* mLoadStoreTextureData = nullptr;
- BufferType* mBuffers = nullptr;
- SamplerType* mSamplerStates = nullptr;
- };
- /** @} */
- /** @addtogroup RenderAPI
- * @{
- */
- /**
- * Contains descriptions for all parameters in a set of programs (ones for each stage) and allows you to write and read
- * those parameters. All parameter values are stored internally on the CPU, and are only submitted to the GPU once the
- * parameters are bound to the pipeline.
- *
- * @note Sim thread only.
- */
- class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<false>, public IResourceListener
- {
- public:
- ~GpuParams() { }
- /** Retrieves a core implementation of a mesh usable only from the core thread. */
- SPtr<ct::GpuParams> getCore() const;
- /**
- * Creates new GpuParams object that can serve for changing the GPU program parameters on the specified pipeline.
- *
- * @param[in] pipelineState Pipeline state for which this object can set parameters for.
- * @return New GpuParams object.
- */
- static SPtr<GpuParams> create(const SPtr<GraphicsPipelineState>& pipelineState);
- /** @copydoc GpuParams::create(const SPtr<GraphicsPipelineState>&) */
- static SPtr<GpuParams> create(const SPtr<ComputePipelineState>& pipelineState);
- /**
- * Creates a new set of GPU parameters using an object describing the parameters for a pipeline.
- *
- * @param[in] paramInfo Description of GPU parameters for a specific GPU pipeline state.
- */
- static SPtr<GpuParams> create(const SPtr<GpuPipelineParamInfo>& paramInfo);
- /** Contains a lookup table for sizes of all data parameters. Sizes are in bytes. */
- const static GpuDataParamInfos PARAM_SIZES;
- /** @name Internal
- * @{
- */
- /** @copydoc GpuParamsBase::_markCoreDirty */
- void _markCoreDirty() override;
- /** @copydoc IResourceListener::markListenerResourcesDirty */
- void _markResourcesDirty() override;
- /** @} */
- protected:
- friend class HardwareBufferManager;
- GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo);
- /** @copydoc CoreObject::getThisPtr */
- SPtr<GpuParams> _getThisPtr() const override;
- /** @copydoc CoreObject::createCore */
- SPtr<ct::CoreObject> createCore() const override;
- /** @copydoc CoreObject::syncToCore */
- CoreSyncData syncToCore(FrameAlloc* allocator) override;
- /** @copydoc IResourceListener::getListenerResources */
- void getListenerResources(Vector<HResource>& resources) override;
- /** @copydoc IResourceListener::notifyResourceLoaded */
- void notifyResourceLoaded(const HResource& resource) override { markCoreDirty(); }
- /** @copydoc IResourceListener::notifyResourceChanged */
- void notifyResourceChanged(const HResource& resource) override { markCoreDirty(); }
- };
- /** @} */
- namespace ct
- {
- /** @addtogroup RenderAPI-Internal
- * @{
- */
- /**
- * Core thread version of bs::GpuParams.
- *
- * @note Core thread only.
- */
- class BS_CORE_EXPORT GpuParams : public CoreObject, public TGpuParams<true>
- {
- public:
- virtual ~GpuParams() { }
- /**
- * @copydoc bs::GpuParams::create(const SPtr<GraphicsPipelineState>&)
- * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
- */
- static SPtr<GpuParams> create(const SPtr<GraphicsPipelineState>& pipelineState,
- GpuDeviceFlags deviceMask = GDF_DEFAULT);
- /**
- * @copydoc bs::GpuParams::create(const SPtr<ComputePipelineState>&)
- * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
- */
- static SPtr<GpuParams> create(const SPtr<ComputePipelineState>& pipelineState,
- GpuDeviceFlags deviceMask = GDF_DEFAULT);
- /**
- * @copydoc bs::GpuParams::create(const SPtr<GpuPipelineParamInfo>&)
- * @param[in] deviceMask Mask that determines on which GPU devices should the buffer be created on.
- */
- static SPtr<GpuParams> create(const SPtr<GpuPipelineParamInfo>& paramInfo,
- GpuDeviceFlags deviceMask = GDF_DEFAULT);
- protected:
- friend class bs::GpuParams;
- friend class HardwareBufferManager;
- GpuParams(const SPtr<GpuPipelineParamInfo>& paramInfo, GpuDeviceFlags deviceMask);
- /** @copydoc CoreObject::getThisPtr */
- SPtr<GpuParams> _getThisPtr() const override;
- /** @copydoc CoreObject::syncToCore */
- void syncToCore(const CoreSyncData& data) override;
- };
- /** @} */
- }
- }
|