| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- #include "CmGpuParams.h"
- #include "CmGpuParamDesc.h"
- #include "CmGpuParamBlock.h"
- #include "CmVector2.h"
- #include "CmDebug.h"
- #include "CmException.h"
- namespace CamelotEngine
- {
- GpuParams::GpuParams(GpuParamDesc& paramDesc)
- :mParamDesc(paramDesc), mTransposeMatrices(false)
- {
- UINT32 numParamBlockSlots = 0;
- for(auto iter = mParamDesc.paramBlocks.begin(); iter != mParamDesc.paramBlocks.end(); ++iter)
- {
- if((iter->second.slot + 1) > numParamBlockSlots)
- numParamBlockSlots = iter->second.slot + 1;
- }
- mParamBlocks.resize(numParamBlockSlots);
- UINT32 numTextureSlots = 0;
- for(auto iter = mParamDesc.textures.begin(); iter != mParamDesc.textures.end(); ++iter)
- {
- if((iter->second.slot + 1) > numTextureSlots)
- numTextureSlots = iter->second.slot + 1;
- }
- mTextures.resize(numTextureSlots);
- UINT32 numSamplerSlots = 0;
- for(auto iter = mParamDesc.samplers.begin(); iter != mParamDesc.samplers.end(); ++iter)
- {
- if((iter->second.slot + 1) > numSamplerSlots)
- numSamplerSlots = iter->second.slot + 1;
- }
- mSamplerStates.resize(numSamplerSlots);
- }
- GpuParamBlockPtr GpuParams::getParamBlock(UINT32 slot) const
- {
- if(slot < 0 || slot >= (UINT32)mParamBlocks.size())
- {
- CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
- toString(mParamBlocks.size() - 1) + ". Requested: " + toString(slot));
- }
- return mParamBlocks[slot];
- }
- GpuParamBlockPtr GpuParams::getParamBlock(const String& name) const
- {
- auto iterFind = mParamDesc.paramBlocks.find(name);
- if(iterFind == mParamDesc.paramBlocks.end())
- {
- LOGWRN("Cannot find parameter block with the name: " + name);
- return nullptr;
- }
- return mParamBlocks[iterFind->second.slot];
- }
- void GpuParams::setParamBlock(UINT32 slot, GpuParamBlockPtr paramBlock)
- {
- if(slot < 0 || slot >= (UINT32)mParamBlocks.size())
- {
- CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
- toString(mParamBlocks.size() - 1) + ". Requested: " + toString(slot));
- }
- mParamBlocks[slot] = paramBlock;
- }
- void GpuParams::setParamBlock(const String& name, GpuParamBlockPtr paramBlock)
- {
- auto iterFind = mParamDesc.paramBlocks.find(name);
- if(iterFind == mParamDesc.paramBlocks.end())
- {
- LOGWRN("Cannot find parameter block with the name: " + name);
- return;
- }
- mParamBlocks[iterFind->second.slot] = paramBlock;
- }
- bool GpuParams::hasParam(const String& name) const
- {
- return getParamDesc(name) != nullptr;
- }
- bool GpuParams::hasTexture(const String& name) const
- {
- auto paramIter = mParamDesc.textures.find(name);
- if(paramIter != mParamDesc.textures.end())
- return true;
- return false;
- }
- bool GpuParams::hasSamplerState(const String& name) const
- {
- auto paramIter = mParamDesc.samplers.find(name);
- if(paramIter != mParamDesc.samplers.end())
- return true;
- return false;
- }
- bool GpuParams::hasParamBlock(const String& name) const
- {
- auto paramBlockIter = mParamDesc.paramBlocks.find(name);
- if(paramBlockIter != mParamDesc.paramBlocks.end())
- return true;
- return false;
- }
- void GpuParams::setParam(const String& name, float value, UINT32 arrayIndex)
- {
- setParam(name, (void*)&value, 1 * sizeof(float), arrayIndex);
- }
- void GpuParams::setParam(const String& name, int value, UINT32 arrayIndex)
- {
- setParam(name, (void*)&value, 1 * sizeof(int), arrayIndex);
- }
- void GpuParams::setParam(const String& name, bool value, UINT32 arrayIndex)
- {
- setParam(name, (void*)&value, 1 * sizeof(bool), arrayIndex);
- }
- void GpuParams::setParam(const String& name, const Vector4& vec, UINT32 arrayIndex)
- {
- setParam(name, (void*)&vec, 4 * sizeof(float), arrayIndex);
- }
- void GpuParams::setParam(const String& name, const Vector3& vec, UINT32 arrayIndex)
- {
- setParam(name, (void*)&vec, 3 * sizeof(float), arrayIndex);
- }
- void GpuParams::setParam(const String& name, const Vector2& vec, UINT32 arrayIndex)
- {
- setParam(name, (void*)&vec, 2 * sizeof(float), arrayIndex);
- }
- void GpuParams::setParam(const String& name, const Matrix4& mat, UINT32 arrayIndex)
- {
- if (mTransposeMatrices)
- {
- Matrix4 transMat = mat.transpose();
- setParam(name, (void*)&transMat, 16 * sizeof(float), arrayIndex);
- }
- else
- {
- setParam(name, (void*)&mat, 16 * sizeof(float), arrayIndex);
- }
- }
- void GpuParams::setParam(const String& name, const Matrix3& mat, UINT32 arrayIndex)
- {
- if (mTransposeMatrices)
- {
- Matrix3 transMat = mat.transpose();
- setParam(name, (void*)&transMat, 9 * sizeof(float), arrayIndex);
- }
- else
- {
- setParam(name, (void*)&mat, 9 * sizeof(float), arrayIndex);
- }
- }
- void GpuParams::setParam(const String& name, const Color& color, UINT32 arrayIndex)
- {
- setParam(name, (void*)&color, 4 * sizeof(float), arrayIndex);
- }
- void GpuParams::setParam(const String& name, const void* value, UINT32 sizeBytes, UINT32 arrayIndex)
- {
- GpuParamDataDesc* desc = getParamDesc(name);
- if(desc == nullptr)
- {
- LOGWRN("Cannot find parameter with the name '" + name + "'");
- return;
- }
- if(arrayIndex >= desc->arraySize)
- {
- CM_EXCEPT(InvalidParametersException, "Array index out of range. Array size: " +
- toString(desc->arraySize) + ". Requested size: " + toString(arrayIndex));
- }
- UINT32 elementSizeBytes = desc->elementSize * sizeof(UINT32);
- if(sizeBytes > elementSizeBytes)
- {
- CM_EXCEPT(InvalidParametersException, "Provided element size larger than maximum element size. Maximum size: " +
- toString(elementSizeBytes) + ". Supplied size: " + toString(sizeBytes));
- }
- GpuParamBlockPtr paramBlock = mParamBlocks[desc->paramBlockSlot];
- if(paramBlock == nullptr)
- {
- LOGWRN("Parameter exists but there is no ParamBlock set.");
- return;
- }
- paramBlock->write((desc->cpuMemOffset + arrayIndex * desc->elementSize) * sizeof(UINT32), value, sizeBytes);
- // Set unused bytes to 0
- if(sizeBytes < elementSizeBytes)
- {
- UINT32 diffSize = elementSizeBytes - sizeBytes;
- paramBlock->zeroOut((desc->cpuMemOffset + arrayIndex * desc->elementSize + sizeBytes) * sizeof(UINT32), diffSize);
- }
- }
- void GpuParams::setTexture(const String& name, TextureHandle& val)
- {
- auto paramIter = mParamDesc.textures.find(name);
- if(paramIter == mParamDesc.textures.end())
- {
- LOGWRN("Texture with the name '" + name + "' doesn't exist.");
- return;
- }
- mTextures[paramIter->second.slot] = val;
- }
- TextureHandle GpuParams::getTexture(UINT32 slot)
- {
- if(slot < 0 || slot >= (UINT32)mTextures.size())
- {
- CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
- toString(mTextures.size() - 1) + ". Requested: " + toString(slot));
- }
- return mTextures[slot];
- }
- void GpuParams::setSamplerState(const String& name, SamplerStateHandle& val)
- {
- auto paramIter = mParamDesc.samplers.find(name);
- if(paramIter == mParamDesc.samplers.end())
- {
- LOGWRN("Sampler with the name '" + name + "' doesn't exist.");
- return;
- }
- mSamplerStates[paramIter->second.slot] = val;
- }
- SamplerStateHandle GpuParams::getSamplerState(UINT32 slot)
- {
- if(slot < 0 || slot >= (UINT32)mSamplerStates.size())
- {
- CM_EXCEPT(InvalidParametersException, "Index out of range: Valid range: 0 .. " +
- toString(mSamplerStates.size() - 1) + ". Requested: " + toString(slot));
- }
- return mSamplerStates[slot];
- }
- GpuParamsPtr GpuParams::clone() const
- {
- GpuParamsPtr clonedParams(new GpuParams(*this));
-
- for(size_t i = 0; i < mParamBlocks.size(); i++)
- {
- if(mParamBlocks[i] != nullptr)
- clonedParams->mParamBlocks[i] = mParamBlocks[i]->clone();
- }
- return clonedParams;
- }
- void GpuParams::updateIfDirty()
- {
- for(size_t i = 0; i < mParamBlocks.size(); i++)
- {
- if(mParamBlocks[i] != nullptr)
- mParamBlocks[i]->updateIfDirty();
- }
- }
- GpuParamDataDesc* GpuParams::getParamDesc(const String& name) const
- {
- auto paramIter = mParamDesc.params.find(name);
- if(paramIter != mParamDesc.params.end())
- return ¶mIter->second;
- return nullptr;
- }
- }
|