| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsMaterialParams.h"
- #include "BsMaterialParamsRTTI.h"
- #include "BsShader.h"
- namespace BansheeEngine
- {
- MaterialParams::MaterialParams(const HShader& shader)
- {
- mDataSize = 0;
- auto& dataParams = shader->getDataParams();
- for (auto& param : dataParams)
- {
- UINT32 arraySize = param.second.arraySize > 1 ? param.second.arraySize : 1;
- const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[(int)param.second.type];
- UINT32 paramSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
- mDataSize += arraySize * paramSize;
- }
- auto& textureParams = shader->getTextureParams();
- auto& samplerParams = shader->getSamplerParams();
- mNumTextureParams = (UINT32)textureParams.size();
- mNumSamplerParams = (UINT32)samplerParams.size();
- mDataParamsBuffer = mAlloc.alloc(mDataSize);
- memset(mDataParamsBuffer, 0, mDataSize);
- mStructParams = mAlloc.construct<StructParamData>(mNumStructParams);
- mTextureParams = mAlloc.construct<TextureParamData>(mNumTextureParams);
- mSamplerStateParams = mAlloc.construct<SamplerStatePtr>(mNumSamplerParams);
- mDefaultTextureParams = mAlloc.construct<HTexture>(mNumTextureParams);
- mDefaultSamplerStateParams = mAlloc.construct<SamplerStatePtr>(mNumSamplerParams);
- UINT32 mDataBufferIdx = 0;
- UINT32 mStructIdx = 0;
- UINT32 mTextureIdx = 0;
- UINT32 mSamplerIdx = 0;
- for (auto& entry : dataParams)
- {
- ParamData& dataParam = mParams[entry.first];
- UINT32 arraySize = entry.second.arraySize > 1 ? entry.second.arraySize : 1;
- dataParam.arraySize = arraySize;
- dataParam.type = ParamType::Data;
- dataParam.dataType = entry.second.type;
-
- const GpuParamDataTypeInfo& typeInfo = GpuParams::PARAM_SIZES.lookup[(int)dataParam.dataType];
- UINT32 paramSize = typeInfo.numColumns * typeInfo.numRows * typeInfo.baseTypeSize;
- dataParam.index = mDataBufferIdx;
- mDataBufferIdx += arraySize * paramSize;
- if(entry.second.type == GPDT_STRUCT)
- {
- for (UINT32 i = 0; i < arraySize; i++)
- {
- StructParamData& param = mStructParams[mStructIdx];
- param.dataSize = entry.second.elementSize;
- param.data = mAlloc.alloc(param.dataSize);
- dataParam.index = mStructIdx;
- mStructIdx++;
- }
- }
- }
- for (auto& entry : textureParams)
- {
- ParamData& dataParam = mParams[entry.first];
- dataParam.arraySize = 1;
- dataParam.type = ParamType::Texture;
- dataParam.dataType = GPDT_UNKNOWN;
- dataParam.index = mTextureIdx;
- TextureParamData& param = mTextureParams[mTextureIdx];
- param.isLoadStore = false;
- if (entry.second.defaultValueIdx != (UINT32)-1)
- mDefaultTextureParams[mTextureIdx] = shader->getDefaultTexture(entry.second.defaultValueIdx);
- mTextureIdx++;
- }
- for (auto& entry : samplerParams)
- {
- ParamData& dataParam = mParams[entry.first];
- dataParam.arraySize = 1;
- dataParam.type = ParamType::Sampler;
- dataParam.dataType = GPDT_UNKNOWN;
- dataParam.index = mSamplerIdx;
- if (entry.second.defaultValueIdx != (UINT32)-1)
- mDefaultSamplerStateParams[mTextureIdx] = shader->getDefaultSampler(entry.second.defaultValueIdx);
- mSamplerIdx++;
- }
- }
- MaterialParams::~MaterialParams()
- {
- if (mStructParams != nullptr)
- {
- for (UINT32 i = 0; mNumStructParams; i++)
- mAlloc.free(mStructParams[i].data);
- }
- mAlloc.free(mDataParamsBuffer);
- mAlloc.destruct(mStructParams, mNumStructParams);
- mAlloc.destruct(mTextureParams, mNumTextureParams);
- mAlloc.destruct(mSamplerStateParams, mNumSamplerParams);
- if(mDefaultTextureParams != nullptr)
- mAlloc.destruct(mDefaultTextureParams, mNumTextureParams);
- if (mDefaultSamplerStateParams != nullptr)
- mAlloc.destruct(mDefaultSamplerStateParams, mNumSamplerParams);
- mAlloc.clear();
- }
- void MaterialParams::getStructData(const String& name, void* value, UINT32 size, UINT32 arrayIdx) const
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, arrayIdx);
- return;
- }
- getStructData(param->index + arrayIdx, value, size);
- }
- void MaterialParams::setStructData(const String& name, const void* value, UINT32 size, UINT32 arrayIdx)
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Data, GPDT_STRUCT, arrayIdx, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, arrayIdx);
- return;
- }
- setStructData(param->index + arrayIdx, value, size);
- }
- void MaterialParams::getTexture(const String& name, HTexture& value) const
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- getTexture(param->index, value);
- }
- void MaterialParams::setTexture(const String& name, const HTexture& value)
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- setTexture(param->index, value);
- }
- void MaterialParams::getLoadStoreTexture(const String& name, HTexture& value, TextureSurface& surface) const
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- getLoadStoreTexture(param->index, value, surface);
- }
- void MaterialParams::setLoadStoreTexture(const String& name, const HTexture& value, const TextureSurface& surface)
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Texture, GPDT_UNKNOWN, 0, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- setLoadStoreTexture(param->index, value, surface);
- }
- void MaterialParams::getSamplerState(const String& name, SamplerStatePtr& value) const
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, ¶m);
- if (result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- getSamplerState(param->index, value);
- }
- void MaterialParams::setSamplerState(const String& name, const SamplerStatePtr& value)
- {
- const ParamData* param = nullptr;
- GetParamResult result = getParamData(name, ParamType::Sampler, GPDT_UNKNOWN, 0, ¶m);
- if(result != GetParamResult::Success)
- {
- reportGetParamError(result, name, 0);
- return;
- }
- setSamplerState(param->index, value);
- }
- MaterialParams::GetParamResult MaterialParams::getParamData(const String& name, ParamType type, GpuParamDataType dataType,
- UINT32 arrayIdx, const ParamData** output) const
- {
- auto iterFind = mParams.find(name);
- if (iterFind == mParams.end())
- return GetParamResult::NotFound;
- const ParamData& param = iterFind->second;
- *output = ¶m;
- if (param.type != type || (type == ParamType::Data && param.dataType != dataType))
- return GetParamResult::InvalidType;
- if (arrayIdx >= param.arraySize)
- return GetParamResult::IndexOutOfBounds;
- return GetParamResult::Success;
- }
- void MaterialParams::reportGetParamError(GetParamResult errorCode, const String& name, UINT32 arrayIdx) const
- {
- switch(errorCode)
- {
- case GetParamResult::NotFound:
- LOGWRN("Material doesn't have a parameter named " + name + ".");
- break;
- case GetParamResult::InvalidType:
- LOGWRN("Parameter \"" + name + "\" is not of the requested type.");
- break;
- case GetParamResult::IndexOutOfBounds:
- LOGWRN("Parameter \"" + name + "\" array index " + toString(arrayIdx) + " out of range.");
- break;
- }
- }
- void MaterialParams::getStructData(UINT32 index, void* value, UINT32 size) const
- {
- const StructParamData& structParam = mStructParams[index];
- if (structParam.dataSize != size)
- {
- LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
- "struct size is" + toString(structParam.dataSize) + " bytes");
- return;
- }
- memcpy(value, structParam.data, structParam.dataSize);
- }
- void MaterialParams::setStructData(UINT32 index, const void* value, UINT32 size)
- {
- const StructParamData& structParam = mStructParams[index];
- if (structParam.dataSize != size)
- {
- LOGWRN("Size mismatch when writing to a struct. Provided size was " + toString(size) + " bytes but the "
- "struct size is" + toString(structParam.dataSize) + " bytes");
- return;
- }
- memcpy(structParam.data, value, structParam.dataSize);
- }
- UINT32 MaterialParams::getStructSize(UINT32 index) const
- {
- const StructParamData& structParam = mStructParams[index];
- return structParam.dataSize;
- }
- void MaterialParams::getTexture(UINT32 index, HTexture& value) const
- {
- TextureParamData& textureParam = mTextureParams[index];
- value = textureParam.value;
- }
- void MaterialParams::setTexture(UINT32 index, const HTexture& value)
- {
- TextureParamData& textureParam = mTextureParams[index];
- textureParam.value = value;
- textureParam.isLoadStore = false;
- }
- void MaterialParams::getLoadStoreTexture(UINT32 index, HTexture& value, TextureSurface& surface) const
- {
- TextureParamData& textureParam = mTextureParams[index];
- value = textureParam.value;
- surface = textureParam.surface;
- }
- void MaterialParams::setLoadStoreTexture(UINT32 index, const HTexture& value, const TextureSurface& surface)
- {
- TextureParamData& textureParam = mTextureParams[index];
- textureParam.value = value;
- textureParam.isLoadStore = true;
- textureParam.surface = surface;
- }
- bool MaterialParams::getIsTextureLoadStore(UINT32 index) const
- {
- return mTextureParams[index].isLoadStore;
- }
- void MaterialParams::getSamplerState(UINT32 index, SamplerStatePtr& value) const
- {
- value = mSamplerStateParams[index];
- }
- void MaterialParams::setSamplerState(UINT32 index, const SamplerStatePtr& value)
- {
- mSamplerStateParams[index] = value;
- }
- void MaterialParams::getDefaultTexture(UINT32 index, HTexture& value) const
- {
- value = mDefaultTextureParams[index];
- }
- void MaterialParams::getDefaultSamplerState(UINT32 index, SamplerStatePtr& value) const
- {
- value = mDefaultSamplerStateParams[index];
- }
- RTTITypeBase* MaterialParams::TextureParamData::getRTTIStatic()
- {
- return TextureParamDataRTTI::instance();
- }
- RTTITypeBase* MaterialParams::TextureParamData::getRTTI() const
- {
- return getRTTIStatic();
- }
- RTTITypeBase* MaterialParams::StructParamData::getRTTIStatic()
- {
- return StructParamDataRTTI::instance();
- }
- RTTITypeBase* MaterialParams::StructParamData::getRTTI() const
- {
- return getRTTIStatic();
- }
- RTTITypeBase* MaterialParams::getRTTIStatic()
- {
- return MaterialParamsRTTI::instance();
- }
- RTTITypeBase* MaterialParams::getRTTI() const
- {
- return MaterialParams::getRTTIStatic();
- }
- }
|