https://github.com/shader-slang/slang
Raw File
Tip revision: 259a015feb9d4ab65e8fbba32f6c777e92780cc7 authored by Yong He on 23 March 2023, 04:16:35 UTC
Type legalization and autodiff bug fixes. (#2722)
Tip revision: 259a015
slang-downstream-compiler-set.cpp
// slang-downstream-compiler-set.cpp
#include "slang-downstream-compiler-set.h"

namespace Slang
{

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DownstreamCompilerSet !!!!!!!!!!!!!!!!!!!!!!*/

void DownstreamCompilerSet::getCompilerDescs(List<DownstreamCompilerDesc>& outCompilerDescs) const
{
    outCompilerDescs.clear();
    for (IDownstreamCompiler* compiler : m_compilers)
    {
        outCompilerDescs.add(compiler->getDesc());
    }
}

Index DownstreamCompilerSet::_findIndex(const DownstreamCompilerDesc& desc) const
{
    const Index count = m_compilers.getCount();
    for (Index i = 0; i < count; ++i)
    { 
        if (m_compilers[i]->getDesc() == desc)
        {
            return i;
        }
    }
    return -1;
}

IDownstreamCompiler* DownstreamCompilerSet::getCompiler(const DownstreamCompilerDesc& compilerDesc) const
{
    const Index index = _findIndex(compilerDesc);
    return index >= 0 ? m_compilers[index] : nullptr;
}

void DownstreamCompilerSet::getCompilers(List<IDownstreamCompiler*>& outCompilers) const
{
    outCompilers.clear();
    outCompilers.addRange((IDownstreamCompiler*const*)m_compilers.begin(), m_compilers.getCount());
}

bool DownstreamCompilerSet::hasSharedLibrary(ISlangSharedLibrary* lib)
{
    const Index foundIndex = m_sharedLibraries.findFirstIndex([lib](ISlangSharedLibrary* inLib) -> bool { return lib == inLib;  });
    return(foundIndex >= 0);
}

void DownstreamCompilerSet::addSharedLibrary(ISlangSharedLibrary* lib)
{
    SLANG_ASSERT(lib);
    if (!hasSharedLibrary(lib))
    {
        m_sharedLibraries.add(ComPtr<ISlangSharedLibrary>(lib));
    }
}

bool DownstreamCompilerSet::hasCompiler(SlangPassThrough compilerType) const
{
    for (IDownstreamCompiler* compiler : m_compilers)
    {
        const auto& desc = compiler->getDesc();
        if (desc.type == compilerType)
        {
            return true;
        }
    }
    return false;
}

void DownstreamCompilerSet::remove(SlangPassThrough compilerType)
{
    for (Index i = 0; i < m_compilers.getCount(); ++i)
    {
        IDownstreamCompiler* compiler = m_compilers[i];
        if (compiler->getDesc().type == compilerType)
        {
            m_compilers.fastRemoveAt(i);
            i--;
        }
    }
}

void DownstreamCompilerSet::addCompiler(IDownstreamCompiler* compiler)
{
    const Index index = _findIndex(compiler->getDesc());
    if (index >= 0)
    {
        m_compilers[index] = compiler;
    }
    else
    {
        m_compilers.add(ComPtr<IDownstreamCompiler>(compiler));
    }
}

}
back to top