| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- #include "BsGpuPipelineParamInfo.h"
- #include "BsGpuParamDesc.h"
- #include "BsRenderStateManager.h"
- namespace bs
- {
- GpuPipelineParamInfoBase::GpuPipelineParamInfoBase(const GPU_PIPELINE_PARAMS_DESC& desc)
- :mNumSets(0), mNumElements(0), mSetInfos(nullptr)
- {
- bs_zero_out(mNumElementsPerType);
- 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;
- 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)
- mNumSets = paramBlock.second.set + 1;
- mNumElements++;
- }
- for (auto& texture : paramDesc->textures)
- {
- if ((texture.second.set + 1) > mNumSets)
- mNumSets = texture.second.set + 1;
- mNumElements++;
- }
- for (auto& texture : paramDesc->loadStoreTextures)
- {
- if ((texture.second.set + 1) > mNumSets)
- mNumSets = texture.second.set + 1;
- mNumElements++;
- }
- for (auto& buffer : paramDesc->buffers)
- {
- if ((buffer.second.set + 1) > mNumSets)
- mNumSets = buffer.second.set + 1;
- mNumElements++;
- }
- for (auto& sampler : paramDesc->samplers)
- {
- if ((sampler.second.set + 1) > mNumSets)
- mNumSets = sampler.second.set + 1;
- mNumElements++;
- }
- }
- UINT32* numSlotsPerSet = (UINT32*)bs_stack_alloc(mNumSets * sizeof(UINT32));
- bs_zero_out(numSlotsPerSet, mNumSets);
- for (UINT32 i = 0; i < numParamDescs; i++)
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[i];
- if (paramDesc == nullptr)
- continue;
- for (auto& paramBlock : paramDesc->paramBlocks)
- numSlotsPerSet[paramBlock.second.set] =
- std::max(numSlotsPerSet[paramBlock.second.set], paramBlock.second.slot + 1);
- for (auto& texture : paramDesc->textures)
- numSlotsPerSet[texture.second.set] =
- std::max(numSlotsPerSet[texture.second.set], texture.second.slot + 1);
- for (auto& texture : paramDesc->loadStoreTextures)
- numSlotsPerSet[texture.second.set] =
- std::max(numSlotsPerSet[texture.second.set], texture.second.slot + 1);
- for (auto& buffer : paramDesc->buffers)
- numSlotsPerSet[buffer.second.set] =
- std::max(numSlotsPerSet[buffer.second.set], buffer.second.slot + 1);
- for (auto& sampler : paramDesc->samplers)
- numSlotsPerSet[sampler.second.set] =
- std::max(numSlotsPerSet[sampler.second.set], sampler.second.slot + 1);
- }
- UINT32 totalNumSlots = 0;
- for (UINT32 i = 0; i < mNumSets; i++)
- totalNumSlots += numSlotsPerSet[i];
- UINT32 allocSize = mNumSets * sizeof(SetInfo) + totalNumSlots * (sizeof(UINT32) * sizeof(ParamType));
- mData = (UINT8*)bs_alloc(allocSize);
- UINT8* dataPtr = mData;
- mSetInfos = (SetInfo*)dataPtr;
- bs_zero_out(mSetInfos, mNumSets);
- dataPtr += mNumSets * sizeof(SetInfo);
- for (UINT32 i = 0; i < mNumSets; i++)
- mSetInfos[i].numSlots = numSlotsPerSet[i];
- bs_stack_free(numSlotsPerSet);
- for(UINT32 i = 0; i < mNumSets; i++)
- {
- mSetInfos[i].slotIndices = (UINT32*)dataPtr;
- memset(mSetInfos[i].slotIndices, -1, sizeof(UINT32) * mSetInfos[i].numSlots);
- dataPtr += sizeof(UINT32) * mSetInfos[i].numSlots;
- mSetInfos[i].slotTypes = (ParamType*)dataPtr;
- dataPtr += sizeof(ParamType) * mSetInfos[i].numSlots;
- }
- for (UINT32 i = 0; i < numParamDescs; i++)
- {
- const SPtr<GpuParamDesc>& paramDesc = mParamDescs[i];
- if (paramDesc == nullptr)
- continue;
- for (auto& paramBlock : paramDesc->paramBlocks)
- {
- SetInfo& setInfo = mSetInfos[paramBlock.second.set];
- setInfo.slotIndices[paramBlock.second.slot] = mNumElementsPerType[(int)ParamType::ParamBlock];
- setInfo.slotTypes[paramBlock.second.slot] = ParamType::ParamBlock;
- mNumElementsPerType[(int)ParamType::ParamBlock]++;
- }
- for (auto& texture : paramDesc->textures)
- {
- SetInfo& setInfo = mSetInfos[texture.second.set];
- setInfo.slotIndices[texture.second.slot] = mNumElementsPerType[(int)ParamType::Texture];
- setInfo.slotTypes[texture.second.slot] = ParamType::Texture;
- mNumElementsPerType[(int)ParamType::Texture]++;
- }
- for (auto& texture : paramDesc->loadStoreTextures)
- {
- SetInfo& setInfo = mSetInfos[texture.second.set];
- setInfo.slotIndices[texture.second.slot] = mNumElementsPerType[(int)ParamType::LoadStoreTexture];
- setInfo.slotTypes[texture.second.slot] = ParamType::LoadStoreTexture;
- mNumElementsPerType[(int)ParamType::LoadStoreTexture]++;
- }
- for (auto& buffer : paramDesc->buffers)
- {
- SetInfo& setInfo = mSetInfos[buffer.second.set];
- setInfo.slotIndices[buffer.second.slot] = mNumElementsPerType[(int)ParamType::Buffer];
- setInfo.slotTypes[buffer.second.slot] = ParamType::Buffer;
- mNumElementsPerType[(int)ParamType::Buffer]++;
- }
- for (auto& sampler : paramDesc->samplers)
- {
- SetInfo& setInfo = mSetInfos[sampler.second.set];
- setInfo.slotIndices[sampler.second.slot] = mNumElementsPerType[(int)ParamType::SamplerState];
- setInfo.slotTypes[sampler.second.slot] = ParamType::SamplerState;
- mNumElementsPerType[(int)ParamType::SamplerState]++;
- }
- }
- }
- GpuPipelineParamInfoBase::~GpuPipelineParamInfoBase()
- {
- bs_free(mData);
- }
- UINT32 GpuPipelineParamInfoBase::getSequentialSlot(ParamType type, UINT32 set, UINT32 slot) const
- {
- #if BS_DEBUG_MODE
- if (set >= mNumSets)
- {
- LOGERR("Set index out of range: Valid range: 0 .. " +
- toString(mNumSets - 1) + ". Requested: " + toString(set));
- return -1;
- }
- if (slot >= mSetInfos[set].numSlots)
- {
- LOGERR("Slot index out of range: Valid range: 0 .. " +
- toString(mSetInfos[set].numSlots - 1) + ". Requested: " + toString(slot));
- return -1;
- }
- ParamType slotType = mSetInfos[set].slotTypes[slot];
- if(slotType != type)
- {
- // Allow sampler states & textures to share the same slot, as some APIs combine them
- bool potentialCombinedSampler = (slotType == ParamType::SamplerState && type == ParamType::Texture) ||
- (slotType == ParamType::Texture && type == ParamType::SamplerState);
- if (!potentialCombinedSampler)
- {
- LOGERR("Requested parameter is not of the valid type. Requested: " + toString((UINT32)type) + ". Actual: " +
- toString((UINT32)mSetInfos[set].slotTypes[slot]) + ".");
- return -1;
- }
- }
- #endif
- return mSetInfos[set].slotIndices[slot];
- }
- GpuPipelineParamInfoCore::GpuPipelineParamInfoCore(const GPU_PIPELINE_PARAMS_DESC& desc, GpuDeviceFlags deviceMask)
- :GpuPipelineParamInfoBase(desc)
- { }
- SPtr<GpuPipelineParamInfoCore> GpuPipelineParamInfoCore::create(const GPU_PIPELINE_PARAMS_DESC& desc,
- GpuDeviceFlags deviceMask)
- {
- return RenderStateCoreManager::instance().createPipelineParamInfo(desc, deviceMask);
- }
- GpuPipelineParamInfo::GpuPipelineParamInfo(const GPU_PIPELINE_PARAMS_DESC& desc)
- :GpuPipelineParamInfoBase(desc)
- { }
- SPtr<GpuPipelineParamInfo> GpuPipelineParamInfo::create(const GPU_PIPELINE_PARAMS_DESC& desc)
- {
- SPtr<GpuPipelineParamInfo> paramInfo =
- bs_core_ptr<GpuPipelineParamInfo>(new (bs_alloc<GpuPipelineParamInfo>()) GpuPipelineParamInfo(desc));
- paramInfo->_setThisPtr(paramInfo);
- paramInfo->initialize();
- return paramInfo;
- }
- SPtr<GpuPipelineParamInfoCore> GpuPipelineParamInfo::getCore() const
- {
- return std::static_pointer_cast<GpuPipelineParamInfoCore>(mCoreSpecific);
- }
- SPtr<CoreObjectCore> GpuPipelineParamInfo::createCore() const
- {
- GPU_PIPELINE_PARAMS_DESC desc;
- desc.fragmentParams = mParamDescs[GPT_FRAGMENT_PROGRAM];
- desc.vertexParams = mParamDescs[GPT_VERTEX_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 RenderStateCoreManager::instance()._createPipelineParamInfo(desc);
- }
- }
|