| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsGpuParams.h"
- #include "BsGpuParamDesc.h"
- #include "BsGpuParamBlockBuffer.h"
- #include "BsVector2.h"
- #include "BsTexture.h"
- #include "BsGpuBuffer.h"
- #include "BsSamplerState.h"
- #include "BsFrameAlloc.h"
- #include "BsDebug.h"
- #include "BsException.h"
- #include "BsVectorNI.h"
- #include "BsMatrixNxM.h"
- #include "BsHardwareBufferManager.h"
- namespace BansheeEngine
- {
- GpuParamsBase::GpuParamsBase(const GPU_PARAMS_DESC& desc)
- {
- mParamDescs[GPT_FRAGMENT_PROGRAM] = desc.fragmentParams;
- mParamDescs[GPT_VERTEX_PROGRAM] = desc.vertexParams;
- mParamDescs[GPT_GEOMETRY_PROGRAM] = desc.geometryParams;
- mParamDescs[GPT_HULL_PROGRAM] = desc.hullParams;
- mParamDescs[GPT_DOMAIN_PROGRAM] = desc.domainParams;
- mParamDescs[GPT_COMPUTE_PROGRAM] = desc.computeParams;
- }
- GpuParamsBase::~GpuParamsBase()
- { }
- UINT32 GpuParamsBase::getDataParamSize(GpuProgramType type, const String& name) const
- {
- GpuParamDataDesc* desc = getParamDesc(type, name);
- if(desc != nullptr)
- return desc->elementSize * 4;
- return 0;
- }
- bool GpuParamsBase::hasParam(GpuProgramType type, const String& name) const
- {
- return getParamDesc(type, name) != nullptr;
- }
- bool GpuParamsBase::hasTexture(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return false;
- auto paramIter = paramDesc->textures.find(name);
- if(paramIter != paramDesc->textures.end())
- return true;
- return false;
- }
- bool GpuParamsBase::hasBuffer(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return false;
- auto paramIter = paramDesc->buffers.find(name);
- if (paramIter != paramDesc->buffers.end())
- return true;
- return false;
- }
- bool GpuParamsBase::hasLoadStoreTexture(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return false;
- auto paramIter = paramDesc->loadStoreTextures.find(name);
- if (paramIter != paramDesc->loadStoreTextures.end())
- return true;
- return false;
- }
- bool GpuParamsBase::hasSamplerState(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return false;
- auto paramIter = paramDesc->samplers.find(name);
- if(paramIter != paramDesc->samplers.end())
- return true;
- return false;
- }
- bool GpuParamsBase::hasParamBlock(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return false;
- auto paramBlockIter = paramDesc->paramBlocks.find(name);
- if(paramBlockIter != paramDesc->paramBlocks.end())
- return true;
- return false;
- }
- GpuParamDataDesc* GpuParamsBase::getParamDesc(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return nullptr;
- auto paramIter = paramDesc->params.find(name);
- if (paramIter != paramDesc->params.end())
- return ¶mIter->second;
- return nullptr;
- }
- GpuParamBlockDesc* GpuParamsBase::getParamBlockDesc(GpuProgramType type, const String& name) const
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[(int)type];
- if (paramDesc == nullptr)
- return nullptr;
- auto paramBlockIter = paramDesc->paramBlocks.find(name);
- if (paramBlockIter != paramDesc->paramBlocks.end())
- return ¶mBlockIter->second;
- return nullptr;
- }
- template<bool Core>
- TGpuParams<Core>::TGpuParams(const GPU_PARAMS_DESC& desc)
- : GpuParamsBase(desc)
- {
- for(UINT32 i = 0; i < (UINT32)ElementType::Count; i++)
- {
- mNumSets[i] = 0;
- mNumElements[i] = 0;
- mOffsets[i] = nullptr;
- }
- UINT32 numParamDescs = sizeof(mParamDescs) / sizeof(mParamDescs[0]);
- for (UINT32 i = 0; i < numParamDescs; i++)
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[i];
- if (paramDesc == nullptr)
- continue;
- for (auto& paramBlock : paramDesc->paramBlocks)
- {
- if ((paramBlock.second.set + 1) > mNumSets[(int)ElementType::ParamBlock])
- mNumSets[(int)ElementType::ParamBlock] = paramBlock.second.set + 1;
- }
- for (auto& texture : paramDesc->textures)
- {
- if ((texture.second.set + 1) > mNumSets[(int)ElementType::Texture])
- mNumSets[(int)ElementType::Texture] = texture.second.set + 1;
- }
- for (auto& texture : paramDesc->loadStoreTextures)
- {
- if ((texture.second.set + 1) > mNumSets[(int)ElementType::LoadStoreTexture])
- mNumSets[(int)ElementType::LoadStoreTexture] = texture.second.set + 1;
- }
- for (auto& buffer : paramDesc->buffers)
- {
- if ((buffer.second.set + 1) > mNumSets[(int)ElementType::Buffer])
- mNumSets[(int)ElementType::Buffer] = buffer.second.set + 1;
- }
- for (auto& sampler : paramDesc->samplers)
- {
- if ((sampler.second.set + 1) > mNumSets[(int)ElementType::SamplerState])
- mNumSets[(int)ElementType::SamplerState] = sampler.second.set + 1;
- }
- }
- UINT32 totalNumSets = 0;
- for (UINT32 i = 0; i < (UINT32)ElementType::Count; i++)
- totalNumSets += mNumSets[i];
- UINT32* slotsPerSetData = bs_stack_alloc<UINT32>(totalNumSets);
- memset(slotsPerSetData, 0, sizeof(UINT32) * totalNumSets);
- UINT32* slotsPerSet[(UINT32)ElementType::Count];
- for (UINT32 i = 0; i < (UINT32)ElementType::Count; i++)
- {
- if (i == 0)
- slotsPerSet[i] = slotsPerSetData;
- else
- slotsPerSet[i] = slotsPerSet[i - 1] + mNumSets[i - 1];
- }
- for (UINT32 i = 0; i < numParamDescs; i++)
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[i];
- if (paramDesc == nullptr)
- continue;
-
- for (auto& paramBlock : paramDesc->paramBlocks)
- {
- UINT32* slots = slotsPerSet[(int)ElementType::ParamBlock];
- slots[paramBlock.second.set] = std::max(slots[paramBlock.second.set], paramBlock.second.slot + 1);
- }
- for (auto& texture : paramDesc->textures)
- {
- UINT32* slots = slotsPerSet[(int)ElementType::Texture];
- slots[texture.second.set] = std::max(slots[texture.second.set], texture.second.slot + 1);
- }
- for (auto& texture : paramDesc->loadStoreTextures)
- {
- UINT32* slots = slotsPerSet[(int)ElementType::LoadStoreTexture];
- slots[texture.second.set] = std::max(slots[texture.second.set], texture.second.slot + 1);
- }
- for (auto& buffer : paramDesc->buffers)
- {
- UINT32* slots = slotsPerSet[(int)ElementType::Buffer];
- slots[buffer.second.set] = std::max(slots[buffer.second.set], buffer.second.slot + 1);
- }
- for (auto& sampler : paramDesc->samplers)
- {
- UINT32* slots = slotsPerSet[(int)ElementType::SamplerState];
- slots[sampler.second.set] = std::max(slots[sampler.second.set], sampler.second.slot + 1);
- }
- }
- for (UINT32 i = 0; i < (UINT32)ElementType::Count; i++)
- {
- for (UINT32 j = 0; j < mNumSets[i]; j++)
- mNumElements[i] += slotsPerSet[i][j];
- }
- UINT32 paramBlocksSize = sizeof(ParamsBufferType) * mNumElements[(int)ElementType::ParamBlock];
- UINT32 texturesSize = sizeof(TextureType) * mNumElements[(int)ElementType::Texture];
- UINT32 loadStoreTexturesSize = sizeof(TextureType) * mNumElements[(int)ElementType::LoadStoreTexture];
- UINT32 loadStoreSurfacesSize = sizeof(TextureSurface) * mNumElements[(int)ElementType::LoadStoreTexture];
- UINT32 buffersSize = sizeof(BufferType) * mNumElements[(int)ElementType::Buffer];
- UINT32 samplerStatesSize = sizeof(SamplerType) * mNumElements[(int)ElementType::SamplerState];
- UINT32 setOffsetsSize = sizeof(UINT32) * totalNumSets;
- UINT32 totalSize = paramBlocksSize + texturesSize + loadStoreTexturesSize + loadStoreSurfacesSize +
- buffersSize + samplerStatesSize + setOffsetsSize;
- UINT8* data = (UINT8*)bs_alloc(totalSize);
- mParamBlockBuffers = (ParamsBufferType*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::ParamBlock]; i++)
- new (&mParamBlockBuffers[i]) ParamsBufferType();
- data += sizeof(ParamsBufferType) * mNumElements[(int)ElementType::ParamBlock];
- mTextures = (TextureType*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Texture]; i++)
- new (&mTextures[i]) TextureType();
- data += sizeof(TextureType) * mNumElements[(int)ElementType::Texture];
- mLoadStoreTextures = (TextureType*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- new (&mLoadStoreTextures[i]) TextureType();
- data += sizeof(TextureType) * mNumElements[(int)ElementType::LoadStoreTexture];
- mLoadStoreSurfaces = (TextureSurface*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- new (&mLoadStoreSurfaces[i]) TextureSurface();
- data += sizeof(TextureSurface) * mNumElements[(int)ElementType::LoadStoreTexture];
- mBuffers = (BufferType*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Buffer]; i++)
- new (&mBuffers[i]) BufferType();
- data += sizeof(BufferType) * mNumElements[(int)ElementType::Buffer];
- mSamplerStates = (SamplerType*)data;
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::SamplerState]; i++)
- new (&mSamplerStates[i]) SamplerType();
- data += sizeof(SamplerType) * mNumElements[(int)ElementType::SamplerState];
- for (UINT32 i = 0; i < (UINT32)ElementType::Count; i++)
- {
- mOffsets[i] = (UINT32*)data;
- data += sizeof(UINT32) * mNumSets[i];
- if (mNumSets[i] == 0)
- continue;
- mOffsets[i][0] = 0;
- for (UINT32 j = 0; j < mNumSets[i] - 1; j++)
- mOffsets[i][j + 1] = mOffsets[i][j] + slotsPerSet[i][j];
- }
- bs_stack_free(slotsPerSetData);
- }
- template<bool Core>
- TGpuParams<Core>::~TGpuParams()
- {
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::ParamBlock]; i++)
- mParamBlockBuffers[i].~ParamsBufferType();
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Texture]; i++)
- mTextures[i].~TextureType();
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- {
- mLoadStoreTextures[i].~TextureType();
- mLoadStoreSurfaces[i].~TextureSurface();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Buffer]; i++)
- mBuffers[i].~BufferType();
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::SamplerState]; i++)
- mSamplerStates[i].~SamplerType();
- // Everything is allocated in a single block, so it's enough to free the first element
- bs_free(mParamBlockBuffers);
- }
- template<bool Core>
- UINT32 TGpuParams<Core>::getGlobalSlot(ElementType type, UINT32 set, UINT32 slot) const
- {
- #if BS_DEBUG_MODE
- if (set >= mNumSets[(int)type])
- {
- LOGERR("Set index out of range: Valid range: 0 .. " +
- toString(mNumSets[(int)type] - 1) + ". Requested: " + toString(set));
- return (UINT32)-1;
- }
- #endif
- UINT32 globalSlot = mOffsets[(int)type][set] + slot;
- #if BS_DEBUG_MODE
- if (globalSlot >= mNumElements[(int)type])
- {
- UINT32 maxSlot;
- if (set < (mNumSets[(int)type] - 1))
- maxSlot = mOffsets[(int)type][set + 1];
- else
- maxSlot = mNumElements[(int)type];
- maxSlot -= mOffsets[(int)type][set];
- LOGERR("Slot index out of range: Valid range: 0 .. " +
- toString(maxSlot - 1) + ". Requested: " + toString(slot));
- return (UINT32)-1;
- }
- #endif
- return globalSlot;
- }
- template<bool Core>
- void TGpuParams<Core>::setParamBlockBuffer(UINT32 set, UINT32 slot, const ParamsBufferType& paramBlockBuffer)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::ParamBlock, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mParamBlockBuffers[globalSlot] = paramBlockBuffer;
- _markCoreDirty();
- }
- template<bool Core>
- void TGpuParams<Core>::setParamBlockBuffer(GpuProgramType type, const String& name, const ParamsBufferType& paramBlockBuffer)
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if(paramDescs == nullptr)
- {
- LOGWRN("Cannot find parameter block with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->paramBlocks.find(name);
- if (iterFind == paramDescs->paramBlocks.end())
- {
- LOGWRN("Cannot find parameter block with the name: '" + name + "'");
- return;
- }
- setParamBlockBuffer(iterFind->second.set, iterFind->second.slot, paramBlockBuffer);
- }
- template<bool Core>
- template<class T>
- void TGpuParams<Core>::getParam(GpuProgramType type, const String& name, TGpuDataParam<T, Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuDataParam<T, Core>(nullptr, nullptr);
- LOGWRN("Cannot find parameter block with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->params.find(name);
- if (iterFind == paramDescs->params.end())
- {
- output = TGpuDataParam<T, Core>(nullptr, nullptr);
- LOGWRN("Cannot find parameter with the name '" + name + "'");
- }
- else
- output = TGpuDataParam<T, Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- void TGpuParams<Core>::getStructParam(GpuProgramType type, const String& name, TGpuParamStruct<Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuParamStruct<Core>(nullptr, nullptr);
- LOGWRN("Cannot find struct parameter with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->params.find(name);
- if (iterFind == paramDescs->params.end() || iterFind->second.type != GPDT_STRUCT)
- {
- output = TGpuParamStruct<Core>(nullptr, nullptr);
- LOGWRN("Cannot find struct parameter with the name '" + name + "'");
- }
- else
- output = TGpuParamStruct<Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- void TGpuParams<Core>::getTextureParam(GpuProgramType type, const String& name, TGpuParamTexture<Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuParamTexture<Core>(nullptr, nullptr);
- LOGWRN("Cannot find texture parameter with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->textures.find(name);
- if (iterFind == paramDescs->textures.end())
- {
- output = TGpuParamTexture<Core>(nullptr, nullptr);
- LOGWRN("Cannot find texture parameter with the name '" + name + "'");
- }
- else
- output = TGpuParamTexture<Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- void TGpuParams<Core>::getLoadStoreTextureParam(GpuProgramType type, const String& name, TGpuParamLoadStoreTexture<Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuParamLoadStoreTexture<Core>(nullptr, nullptr);
- LOGWRN("Cannot find texture parameter with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->loadStoreTextures.find(name);
- if (iterFind == paramDescs->loadStoreTextures.end())
- {
- output = TGpuParamLoadStoreTexture<Core>(nullptr, nullptr);
- LOGWRN("Cannot find texture parameter with the name '" + name + "'");
- }
- else
- output = TGpuParamLoadStoreTexture<Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- void TGpuParams<Core>::getBufferParam(GpuProgramType type, const String& name, TGpuParamBuffer<Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuParamBuffer<Core>(nullptr, nullptr);
- LOGWRN("Cannot find buffer parameter with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->buffers.find(name);
- if (iterFind == paramDescs->buffers.end())
- {
- output = TGpuParamBuffer<Core>(nullptr, nullptr);
- LOGWRN("Cannot find buffer parameter with the name '" + name + "'");
- }
- else
- output = TGpuParamBuffer<Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- void TGpuParams<Core>::getSamplerStateParam(GpuProgramType type, const String& name, TGpuParamSampState<Core>& output) const
- {
- const SPtr<GpuParamDesc>& paramDescs = mParamDescs[(int)type];
- if (paramDescs == nullptr)
- {
- output = TGpuParamSampState<Core>(nullptr, nullptr);
- LOGWRN("Cannot find sampler state parameter with the name: '" + name + "'");
- return;
- }
- auto iterFind = paramDescs->samplers.find(name);
- if (iterFind == paramDescs->samplers.end())
- {
- output = TGpuParamSampState<Core>(nullptr, nullptr);
- LOGWRN("Cannot find sampler state parameter with the name '" + name + "'");
- }
- else
- output = TGpuParamSampState<Core>(&iterFind->second, _getThisPtr());
- }
- template<bool Core>
- typename TGpuParams<Core>::ParamsBufferType TGpuParams<Core>::getParamBlockBuffer(UINT32 set, UINT32 slot) const
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::ParamBlock, set, slot);
- if (globalSlot == (UINT32)-1)
- return nullptr;
- return mParamBlockBuffers[globalSlot];
- }
- template<bool Core>
- typename TGpuParams<Core>::TextureType TGpuParams<Core>::getTexture(UINT32 set, UINT32 slot) const
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::Texture, set, slot);
- if (globalSlot == (UINT32)-1)
- return TGpuParams<Core>::TextureType();
- return mTextures[globalSlot];
- }
- template<bool Core>
- typename TGpuParams<Core>::TextureType TGpuParams<Core>::getLoadStoreTexture(UINT32 set, UINT32 slot) const
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::LoadStoreTexture, set, slot);
- if (globalSlot == (UINT32)-1)
- return TGpuParams<Core>::TextureType();
- return mLoadStoreTextures[globalSlot];
- }
- template<bool Core>
- typename TGpuParams<Core>::BufferType TGpuParams<Core>::getBuffer(UINT32 set, UINT32 slot) const
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::Buffer, set, slot);
- if (globalSlot == (UINT32)-1)
- return nullptr;
- return mBuffers[globalSlot];
- }
- template<bool Core>
- typename TGpuParams<Core>::SamplerType TGpuParams<Core>::getSamplerState(UINT32 set, UINT32 slot) const
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::SamplerState, set, slot);
- if (globalSlot == (UINT32)-1)
- return nullptr;
- return mSamplerStates[globalSlot];
- }
- template<bool Core>
- const TextureSurface& TGpuParams<Core>::getLoadStoreSurface(UINT32 set, UINT32 slot) const
- {
- static TextureSurface emptySurface;
- UINT32 globalSlot = getGlobalSlot(ElementType::LoadStoreTexture, set, slot);
- if (globalSlot == (UINT32)-1)
- return emptySurface;
- return mLoadStoreSurfaces[globalSlot];
- }
- template<bool Core>
- void TGpuParams<Core>::setTexture(UINT32 set, UINT32 slot, const TextureType& texture)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::Texture, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mTextures[globalSlot] = texture;
- _markResourcesDirty();
- _markCoreDirty();
- }
- template<bool Core>
- void TGpuParams<Core>::setLoadStoreTexture(UINT32 set, UINT32 slot, const TextureType& texture, const TextureSurface& surface)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::LoadStoreTexture, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mLoadStoreTextures[globalSlot] = texture;
- _markResourcesDirty();
- _markCoreDirty();
- }
- template<bool Core>
- void TGpuParams<Core>::setBuffer(UINT32 set, UINT32 slot, const BufferType& buffer)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::Buffer, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mBuffers[globalSlot] = buffer;
- _markResourcesDirty();
- _markCoreDirty();
- }
- template<bool Core>
- void TGpuParams<Core>::setSamplerState(UINT32 set, UINT32 slot, const SamplerType& sampler)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::SamplerState, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mSamplerStates[globalSlot] = sampler;
- _markResourcesDirty();
- _markCoreDirty();
- }
- template<bool Core>
- void TGpuParams<Core>::setLoadStoreSurface(UINT32 set, UINT32 slot, const TextureSurface& surface)
- {
- UINT32 globalSlot = getGlobalSlot(ElementType::LoadStoreTexture, set, slot);
- if (globalSlot == (UINT32)-1)
- return;
- mLoadStoreSurfaces[globalSlot] = surface;
- }
- template class TGpuParams < false > ;
- template class TGpuParams < true > ;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<float>(GpuProgramType type, const String&, TGpuDataParam<float, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<int>(GpuProgramType type, const String&, TGpuDataParam<int, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Color>(GpuProgramType type, const String&, TGpuDataParam<Color, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2>(GpuProgramType type, const String&, TGpuDataParam<Vector2, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3>(GpuProgramType type, const String&, TGpuDataParam<Vector3, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4>(GpuProgramType type, const String&, TGpuDataParam<Vector4, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector2I>(GpuProgramType type, const String&, TGpuDataParam<Vector2I, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector3I>(GpuProgramType type, const String&, TGpuDataParam<Vector3I, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Vector4I>(GpuProgramType type, const String&, TGpuDataParam<Vector4I, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2>(GpuProgramType type, const String&, TGpuDataParam<Matrix2, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x3>(GpuProgramType type, const String&, TGpuDataParam<Matrix2x3, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix2x4>(GpuProgramType type, const String&, TGpuDataParam<Matrix2x4, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3>(GpuProgramType type, const String&, TGpuDataParam<Matrix3, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x2>(GpuProgramType type, const String&, TGpuDataParam<Matrix3x2, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix3x4>(GpuProgramType type, const String&, TGpuDataParam<Matrix3x4, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4>(GpuProgramType type, const String&, TGpuDataParam<Matrix4, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x2>(GpuProgramType type, const String&, TGpuDataParam<Matrix4x2, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<false>::getParam<Matrix4x3>(GpuProgramType type, const String&, TGpuDataParam<Matrix4x3, false>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<float>(GpuProgramType type, const String&, TGpuDataParam<float, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<int>(GpuProgramType type, const String&, TGpuDataParam<int, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Color>(GpuProgramType type, const String&, TGpuDataParam<Color, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2>(GpuProgramType type, const String&, TGpuDataParam<Vector2, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3>(GpuProgramType type, const String&, TGpuDataParam<Vector3, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4>(GpuProgramType type, const String&, TGpuDataParam<Vector4, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector2I>(GpuProgramType type, const String&, TGpuDataParam<Vector2I, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector3I>(GpuProgramType type, const String&, TGpuDataParam<Vector3I, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Vector4I>(GpuProgramType type, const String&, TGpuDataParam<Vector4I, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2>(GpuProgramType type, const String&, TGpuDataParam<Matrix2, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x3>(GpuProgramType type, const String&, TGpuDataParam<Matrix2x3, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix2x4>(GpuProgramType type, const String&, TGpuDataParam<Matrix2x4, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3>(GpuProgramType type, const String&, TGpuDataParam<Matrix3, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x2>(GpuProgramType type, const String&, TGpuDataParam<Matrix3x2, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix3x4>(GpuProgramType type, const String&, TGpuDataParam<Matrix3x4, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4>(GpuProgramType type, const String&, TGpuDataParam<Matrix4, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x2>(GpuProgramType type, const String&, TGpuDataParam<Matrix4x2, true>&) const;
- template BS_CORE_EXPORT void TGpuParams<true>::getParam<Matrix4x3>(GpuProgramType type, const String&, TGpuDataParam<Matrix4x3, true>&) const;
- GpuParamsCore::GpuParamsCore(const GPU_PARAMS_DESC& desc, GpuDeviceFlags deviceMask)
- : TGpuParams(desc)
- {
- }
- SPtr<GpuParamsCore> GpuParamsCore::_getThisPtr() const
- {
- return std::static_pointer_cast<GpuParamsCore>(getThisPtr());
- }
- void GpuParamsCore::syncToCore(const CoreSyncData& data)
- {
- UINT32 loadStoreSurfacesSize = mNumElements[(int)ElementType::LoadStoreTexture] * sizeof(TextureSurface);
- UINT32 paramBufferSize = mNumElements[(int)ElementType::ParamBlock] * sizeof(SPtr<GpuParamBlockBufferCore>);
- UINT32 textureArraySize = mNumElements[(int)ElementType::Texture] * sizeof(SPtr<TextureCore>);
- UINT32 loadStoreTextureArraySize = mNumElements[(int)ElementType::LoadStoreTexture] * sizeof(SPtr<TextureCore>);
- UINT32 bufferArraySize = mNumElements[(int)ElementType::Buffer] * sizeof(SPtr<GpuBufferCore>);
- UINT32 samplerArraySize = mNumElements[(int)ElementType::SamplerState] * sizeof(SPtr<SamplerStateCore>);
- UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
- + bufferArraySize + samplerArraySize;
- UINT32 textureInfoOffset = 0;
- UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
- UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
- UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
- UINT32 bufferArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
- UINT32 samplerArrayOffset = bufferArrayOffset + bufferArraySize;
- assert(data.getBufferSize() == totalSize);
- UINT8* dataPtr = data.getBuffer();
- TextureSurface* loadStoreSurfaces = (TextureSurface*)(dataPtr + textureInfoOffset);
- SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(dataPtr + paramBufferOffset);
- SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(dataPtr + textureArrayOffset);
- SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(dataPtr + loadStoreTextureArrayOffset);
- SPtr<GpuBufferCore>* buffers = (SPtr<GpuBufferCore>*)(dataPtr + bufferArrayOffset);
- SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(dataPtr + samplerArrayOffset);
- // Copy & destruct
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::ParamBlock]; i++)
- {
- mParamBlockBuffers[i] = paramBuffers[i];
- paramBuffers[i].~SPtr<GpuParamBlockBufferCore>();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Texture]; i++)
- {
- mTextures[i] = textures[i];
- textures[i].~SPtr<TextureCore>();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- {
- mLoadStoreSurfaces[i] = loadStoreSurfaces[i];
- loadStoreSurfaces[i].~TextureSurface();
- mLoadStoreTextures[i] = loadStoreTextures[i];
- loadStoreTextures[i].~SPtr<TextureCore>();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Buffer]; i++)
- {
- mBuffers[i] = buffers[i];
- buffers[i].~SPtr<GpuBufferCore>();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::SamplerState]; i++)
- {
- mSamplerStates[i] = samplers[i];
- samplers[i].~SPtr<SamplerStateCore>();
- }
- }
- SPtr<GpuParamsCore> GpuParamsCore::create(const GPU_PARAMS_DESC& desc, GpuDeviceFlags deviceMask)
- {
- return HardwareBufferCoreManager::instance().createGpuParams(desc, deviceMask);
- }
- const GpuDataParamInfos GpuParams::PARAM_SIZES;
- GpuParams::GpuParams(const GPU_PARAMS_DESC& desc)
- : TGpuParams(desc)
- {
- }
- SPtr<GpuParams> GpuParams::_getThisPtr() const
- {
- return std::static_pointer_cast<GpuParams>(getThisPtr());
- }
- SPtr<GpuParamsCore> GpuParams::getCore() const
- {
- return std::static_pointer_cast<GpuParamsCore>(mCoreSpecific);
- }
- SPtr<CoreObjectCore> GpuParams::createCore() const
- {
- GPU_PARAMS_DESC desc;
- desc.vertexParams = mParamDescs[GPT_VERTEX_PROGRAM];
- desc.fragmentParams = mParamDescs[GPT_FRAGMENT_PROGRAM];
- desc.geometryParams = mParamDescs[GPT_GEOMETRY_PROGRAM];
- desc.hullParams = mParamDescs[GPT_HULL_PROGRAM];
- desc.domainParams = mParamDescs[GPT_DOMAIN_PROGRAM];
- desc.computeParams = mParamDescs[GPT_COMPUTE_PROGRAM];
- return HardwareBufferCoreManager::instance().createGpuParams(desc);
- }
- void GpuParams::_markCoreDirty()
- {
- markCoreDirty();
- }
- void GpuParams::_markResourcesDirty()
- {
- markListenerResourcesDirty();
- }
- SPtr<GpuParams> GpuParams::create(const GPU_PARAMS_DESC& desc)
- {
- return HardwareBufferManager::instance().createGpuParams(desc);
- }
- CoreSyncData GpuParams::syncToCore(FrameAlloc* allocator)
- {
- UINT32 loadStoreSurfacesSize = mNumElements[(int)ElementType::LoadStoreTexture] * sizeof(TextureSurface);
- UINT32 paramBufferSize = mNumElements[(int)ElementType::ParamBlock] * sizeof(SPtr<GpuParamBlockBufferCore>);
- UINT32 textureArraySize = mNumElements[(int)ElementType::Texture] * sizeof(SPtr<TextureCore>);
- UINT32 loadStoreTextureArraySize = mNumElements[(int)ElementType::LoadStoreTexture] * sizeof(SPtr<TextureCore>);
- UINT32 bufferArraySize = mNumElements[(int)ElementType::Buffer] * sizeof(SPtr<GpuBufferCore>);
- UINT32 samplerArraySize = mNumElements[(int)ElementType::SamplerState] * sizeof(SPtr<SamplerStateCore>);
- UINT32 totalSize = loadStoreSurfacesSize + paramBufferSize + textureArraySize + loadStoreTextureArraySize
- + bufferArraySize + samplerArraySize;
- UINT32 textureInfoOffset = 0;
- UINT32 paramBufferOffset = textureInfoOffset + loadStoreSurfacesSize;
- UINT32 textureArrayOffset = paramBufferOffset + paramBufferSize;
- UINT32 loadStoreTextureArrayOffset = textureArrayOffset + textureArraySize;
- UINT32 bufferArrayOffset = loadStoreTextureArrayOffset + loadStoreTextureArraySize;
- UINT32 samplerArrayOffset = bufferArrayOffset + bufferArraySize;
- UINT8* data = allocator->alloc(totalSize);
- TextureSurface* loadStoreSurfaces = (TextureSurface*)(data + textureInfoOffset);
- SPtr<GpuParamBlockBufferCore>* paramBuffers = (SPtr<GpuParamBlockBufferCore>*)(data + paramBufferOffset);
- SPtr<TextureCore>* textures = (SPtr<TextureCore>*)(data + textureArrayOffset);
- SPtr<TextureCore>* loadStoreTextures = (SPtr<TextureCore>*)(data + loadStoreTextureArrayOffset);
- SPtr<GpuBufferCore>* buffers = (SPtr<GpuBufferCore>*)(data + bufferArrayOffset);
- SPtr<SamplerStateCore>* samplers = (SPtr<SamplerStateCore>*)(data + samplerArrayOffset);
- // Construct & copy
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::ParamBlock]; i++)
- {
- new (¶mBuffers[i]) SPtr<GpuParamBlockBufferCore>();
- if (mParamBlockBuffers[i] != nullptr)
- paramBuffers[i] = mParamBlockBuffers[i]->getCore();
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Texture]; i++)
- {
- new (&textures[i]) SPtr<TextureCore>();
- if (mTextures[i].isLoaded())
- textures[i] = mTextures[i]->getCore();
- else
- textures[i] = nullptr;
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- {
- new (&loadStoreSurfaces[i]) TextureSurface();
- loadStoreSurfaces[i] = mLoadStoreSurfaces[i];
- new (&loadStoreTextures[i]) SPtr<TextureCore>();
- if (mLoadStoreTextures[i].isLoaded())
- loadStoreTextures[i] = mLoadStoreTextures[i]->getCore();
- else
- loadStoreTextures[i] = nullptr;
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Buffer]; i++)
- {
- new (&buffers[i]) SPtr<GpuBufferCore>();
- if (mBuffers[i] != nullptr)
- buffers[i] = mBuffers[i]->getCore();
- else
- buffers[i] = nullptr;
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::SamplerState]; i++)
- {
- new (&samplers[i]) SPtr<SamplerStateCore>();
- if (mSamplerStates[i] != nullptr)
- samplers[i] = mSamplerStates[i]->getCore();
- else
- samplers[i] = nullptr;
- }
- return CoreSyncData(data, totalSize);
- }
- void GpuParams::getListenerResources(Vector<HResource>& resources)
- {
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::Texture]; i++)
- {
- if (mTextures[i] != nullptr)
- resources.push_back(mTextures[i]);
- }
- for (UINT32 i = 0; i < mNumElements[(int)ElementType::LoadStoreTexture]; i++)
- {
- if (mLoadStoreTextures[i] != nullptr)
- resources.push_back(mLoadStoreTextures[i]);
- }
- }
- }
|