This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

Changeset 13911 for ps


Ignore:
Timestamp:
09/29/13 16:48:11 (11 years ago)
Author:
philip
Message:

Reduce memory allocations in the renderer

Use an arena allocator in ShaderModelRenderer::Render, to reduce
the allocation cost in STL containers.

Avoid unnecessary copying of std::vectors.

Location:
ps/trunk/source
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/source/graphics/ShaderDefines.cpp

    r13906 r13911  
    106106CShaderParams<value_t>::CShaderParams()
    107107{
     108    *this = s_Empty;
     109}
     110
     111template<typename value_t>
     112CShaderParams<value_t>::CShaderParams(SItems* items) : m_Items(items)
     113{
     114}
     115
     116template<typename value_t>
     117CShaderParams<value_t> CShaderParams<value_t>::CreateEmpty()
     118{
    108119    SItems items;
    109120    items.RecalcHash();
    110     m_Items = GetInterned(items);
     121    return CShaderParams(GetInterned(items));
    111122}
    112123
     
    268279template<> CShaderParams<CVector4D>::InternedItems_t CShaderParams<CVector4D>::s_InternedItems = CShaderParams<CVector4D>::InternedItems_t();
    269280
     281template<> CShaderParams<CStrIntern> CShaderParams<CStrIntern>::s_Empty = CShaderParams<CStrIntern>::CreateEmpty();
     282template<> CShaderParams<CVector4D> CShaderParams<CVector4D>::s_Empty = CShaderParams<CVector4D>::CreateEmpty();
     283
    270284template class CShaderParams<CStrIntern>;
    271285template class CShaderParams<CVector4D>;
  • ps/trunk/source/graphics/ShaderDefines.h

    r13906 r13911  
    119119     */
    120120    static SItems* GetInterned(const SItems& items);
     121
     122    CShaderParams(SItems* items);
     123    static CShaderParams CreateEmpty();
     124    static CShaderParams s_Empty;
    121125};
    122126
  • ps/trunk/source/lib/allocators/allocator_adapters.h

    r10410 r13911  
    116116    };
    117117
     118    // (required to be declared by boost::unordered_map, but should never be called)
     119    explicit NOTHROW_DEFINE ProxyAllocator();
     120
    118121    explicit NOTHROW_DEFINE ProxyAllocator(Allocator& allocator)
    119122        : allocator(&allocator)
  • ps/trunk/source/renderer/DecalRData.cpp

    r13906 r13911  
    122122            if (material.GetSamplers().size() != 0)
    123123            {
    124                 CMaterial::SamplersVector samplers = material.GetSamplers();
     124                const CMaterial::SamplersVector& samplers = material.GetSamplers();
    125125                size_t samplersNum = samplers.size();
    126126               
    127127                for (size_t s = 0; s < samplersNum; ++s)
    128128                {
    129                     CMaterial::TextureSampler &samp = samplers[s];
     129                    const CMaterial::TextureSampler& samp = samplers[s];
    130130                    shader->BindTexture(samp.Name, samp.Sampler);
    131131                }
  • ps/trunk/source/renderer/ModelRenderer.cpp

    r13906 r13911  
    1818#include "precompiled.h"
    1919
     20#include "lib/allocators/allocator_adapters.h"
     21#include "lib/allocators/arena.h"
    2022#include "lib/ogl.h"
    2123#include "maths/Vector3D.h"
     
    418420     */
    419421
    420     typedef boost::unordered_map<SMRMaterialBucketKey, std::vector<CModel*>, SMRMaterialBucketKeyHash> MaterialBuckets_t;
    421     MaterialBuckets_t materialBuckets;
     422    Allocators::Arena<> arena(1*MiB);
     423    typedef ProxyAllocator<void*, Allocators::Arena<> > ArenaProxyAllocator;
     424    typedef std::vector<CModel*, ArenaProxyAllocator> ModelList_t;
     425    typedef boost::unordered_map<SMRMaterialBucketKey, ModelList_t, SMRMaterialBucketKeyHash,
     426        std::equal_to<SMRMaterialBucketKey>, ProxyAllocator<void*, Allocators::Arena<> >
     427    > MaterialBuckets_t;
     428    MaterialBuckets_t materialBuckets((MaterialBuckets_t::allocator_type(arena)));
    422429
    423430    {
     
    433440            for (size_t j = 0; j < condefs.GetSize(); ++j)
    434441            {
    435                 const CShaderConditionalDefines::CondDefine &item = condefs.GetItem(j);
     442                const CShaderConditionalDefines::CondDefine& item = condefs.GetItem(j);
    436443                int type = item.m_CondType;
    437444                switch (type)
     
    455462            CShaderDefines defs = model->GetMaterial().GetShaderDefines(condFlags);
    456463            SMRMaterialBucketKey key(model->GetMaterial().GetShaderEffect(), defs);
    457             std::vector<CModel*>& bucketItems = materialBuckets[key];
    458             bucketItems.push_back(model);
    459         }
    460     }
    461 
    462     std::vector<SMRSortByDistItem> sortByDistItems;
    463 
    464     std::vector<CShaderTechniquePtr> sortByDistTechs;
     464
     465            MaterialBuckets_t::iterator it = materialBuckets.find(key);
     466            if (it == materialBuckets.end())
     467            {
     468                std::pair<MaterialBuckets_t::iterator, bool> inserted = materialBuckets.insert(
     469                    std::make_pair(key, ModelList_t(ModelList_t::allocator_type(arena))));
     470                inserted.first->second.reserve(32);
     471                inserted.first->second.push_back(model);
     472            }
     473            else
     474            {
     475                it->second.push_back(model);
     476            }
     477        }
     478    }
     479
     480    std::vector<SMRSortByDistItem, ArenaProxyAllocator> sortByDistItems((ArenaProxyAllocator(arena)));
     481
     482    std::vector<CShaderTechniquePtr, ArenaProxyAllocator> sortByDistTechs((ArenaProxyAllocator(arena)));
    465483        // indexed by sortByDistItems[i].techIdx
    466484        // (which stores indexes instead of CShaderTechniquePtr directly
     
    469487        // will keep it alive long enough)
    470488
    471     std::vector<SMRTechBucket> techBuckets;
     489    std::vector<SMRTechBucket, ArenaProxyAllocator> techBuckets((ArenaProxyAllocator(arena)));
    472490
    473491    {
     
    529547    // we could avoid the cost of copying into this list by adding
    530548    // a stride length into techBuckets and not requiring contiguous CModel*s)
    531     std::vector<CModel*> sortByDistModels;
     549    std::vector<CModel*, ArenaProxyAllocator> sortByDistModels((ArenaProxyAllocator(arena)));
    532550
    533551    if (!sortByDistItems.empty())
     
    578596        // loops to avoid excessive reallocations. The token allocation of 64 elements
    579597        // should be plenty, though it is reallocated below (at a cost) if necessary.
    580         std::vector<CTexture*> currentTexs;
     598        std::vector<CTexture*, ArenaProxyAllocator> currentTexs((ArenaProxyAllocator(arena)));
    581599        currentTexs.reserve(64);
    582600       
     
    584602        // statically in the ShaderRenderModifier class. texBindingNames uses interned strings to
    585603        // keep track of when bindings need to be reevaluated.
    586         std::vector<CShaderProgram::Binding> texBindings;
     604        std::vector<CShaderProgram::Binding, ArenaProxyAllocator> texBindings((ArenaProxyAllocator(arena)));
    587605        texBindings.reserve(64);
    588         std::vector<CStrIntern> texBindingNames;
     606        std::vector<CStrIntern, ArenaProxyAllocator> texBindingNames((ArenaProxyAllocator(arena)));
    589607        texBindingNames.reserve(64);
    590608
     
    634652                            continue;
    635653
    636                         CMaterial::SamplersVector samplers = model->GetMaterial().GetSamplers();
     654                        const CMaterial::SamplersVector& samplers = model->GetMaterial().GetSamplers();
    637655                        size_t samplersNum = samplers.size();
    638656                       
     
    654672                        for (size_t s = 0; s < samplersNum; ++s)
    655673                        {
    656                             CMaterial::TextureSampler &samp = samplers[s];
     674                            const CMaterial::TextureSampler& samp = samplers[s];
    657675                           
    658676                            CShaderProgram::Binding bind = texBindings[s];
     
    695713                        }
    696714                       
    697                         CShaderRenderQueries renderQueries = model->GetMaterial().GetRenderQueries();
     715                        const CShaderRenderQueries& renderQueries = model->GetMaterial().GetRenderQueries();
    698716                       
    699717                        for (size_t q = 0; q < renderQueries.GetSize(); q++)
  • ps/trunk/source/renderer/PatchRData.cpp

    r13906 r13911  
    788788            if (itt->first->GetMaterial().GetSamplers().size() != 0)
    789789            {
    790                 CMaterial::SamplersVector samplers = itt->first->GetMaterial().GetSamplers();
     790                const CMaterial::SamplersVector& samplers = itt->first->GetMaterial().GetSamplers();
    791791                size_t samplersNum = samplers.size();
    792792               
    793793                for (size_t s = 0; s < samplersNum; ++s)
    794794                {
    795                     CMaterial::TextureSampler &samp = samplers[s];
     795                    const CMaterial::TextureSampler& samp = samplers[s];
    796796                    shader->BindTexture(samp.Name, samp.Sampler);
    797797                }
     
    10181018            if (itt->m_Texture)
    10191019            {
    1020                 CMaterial::SamplersVector samplers = itt->m_Texture->GetMaterial().GetSamplers();
     1020                const CMaterial::SamplersVector& samplers = itt->m_Texture->GetMaterial().GetSamplers();
    10211021                size_t samplersNum = samplers.size();
    10221022               
    10231023                for (size_t s = 0; s < samplersNum; ++s)
    10241024                {
    1025                     CMaterial::TextureSampler &samp = samplers[s];
     1025                    const CMaterial::TextureSampler& samp = samplers[s];
    10261026                    shader->BindTexture(samp.Name, samp.Sampler);
    10271027                }
Note: See TracChangeset for help on using the changeset viewer.