Ticket #1859: ModelCache.patch

File ModelCache.patch, 4.1 KB (added by wraitii, 11 years ago)
  • Users/lancelot/Desktop/0ad-svn/trunk/source/renderer/ModelRenderer.cpp

     
    453468            }
    454469
    455470            SMRMaterialBucketKey key(model->GetMaterial().GetShaderEffect(), defs);
    456             std::vector<CModel*>& bucketItems = materialBuckets[key];
    457             bucketItems.push_back(model);
     471            materialBuckets[key].push_back(model);
    458472        }
    459473    }
    460 
    461474    std::vector<SMRSortByDistItem> sortByDistItems;
    462475
    463476    std::vector<CShaderTechniquePtr> sortByDistTechs;
     
    574587        size_t idxTechStart = 0;
    575588       
    576589        // This vector keeps track of texture changes during rendering. It is kept outside the
    577         // loops to avoid excessive reallocations. The token allocation of 64 elements
     590        // loops to avoid excessive reallocations. The token allocation of 16 elements
    578591        // should be plenty, though it is reallocated below (at a cost) if necessary.
    579592        std::vector<CTexture*> currentTexs;
    580         currentTexs.reserve(64);
     593        currentTexs.reserve(16);
    581594       
    582595        // texBindings holds the identifier bindings in the shader, which can no longer be defined
    583596        // statically in the ShaderRenderModifier class. texBindingNames uses interned strings to
    584597        // keep track of when bindings need to be reevaluated.
    585598        std::vector<CShaderProgram::Binding> texBindings;
    586         texBindings.reserve(64);
     599        texBindings.reserve(16);
    587600        std::vector<CStrIntern> texBindingNames;
    588         texBindingNames.reserve(64);
    589 
     601        texBindingNames.reserve(16);
     602       
     603        // Initialize the arrays with default values.
     604        for (int i = 0; i < 16; ++i)
     605        {
     606            texBindingNames.push_back(CStrIntern());
     607            currentTexs.push_back(NULL);
     608            texBindings.push_back(CShaderProgram::Binding());
     609        }
     610       
    590611        while (idxTechStart < techBuckets.size())
    591612        {
    592613            CShaderTechniquePtr currentTech = techBuckets[idxTechStart].tech;
     
    611632
    612633                m->vertexRenderer->BeginPass(streamflags);
    613634               
    614                 // When the shader technique changes, textures need to be
    615                 // rebound, so ensure there are no remnants from the last pass.
    616                 // (the vector size is set to 0, but memory is not freed)
    617                 currentTexs.clear();
    618                 texBindings.clear();
    619                 texBindingNames.clear();
     635                // reinitialize the values since we're using a new shader that has not be bound yet.
     636                for (size_t i = 0; i < currentTexs.capacity(); ++i)
     637                {
     638                    texBindingNames[i] = CStrIntern();
     639                    currentTexs[i] = NULL;
     640                    texBindings[i] = CShaderProgram::Binding();
     641                }
    620642               
    621643                CModelDef* currentModeldef = NULL;
    622644                CShaderUniforms currentStaticUniforms;
     
    625647                {
    626648                    CModel** models = techBuckets[idx].models;
    627649                    size_t numModels = techBuckets[idx].numModels;
     650
    628651                    for (size_t i = 0; i < numModels; ++i)
    629652                    {
    630653                        CModel* model = models[i];
     
    637660                       
    638661                        // make sure the vectors are the right virtual sizes, and also
    639662                        // reallocate if there are more samplers than expected.
    640                         if (currentTexs.size() != samplersNum)
     663                        if (currentTexs.capacity() < samplersNum)
    641664                        {
    642665                            currentTexs.resize(samplersNum, NULL);
    643666                            texBindings.resize(samplersNum, CShaderProgram::Binding());
    644667                            texBindingNames.resize(samplersNum, CStrIntern());
    645                            
    646                             // ensure they are definitely empty
    647                             std::fill(texBindings.begin(), texBindings.end(), CShaderProgram::Binding());
    648                             std::fill(currentTexs.begin(), currentTexs.end(), (CTexture*)NULL);
    649                             std::fill(texBindingNames.begin(), texBindingNames.end(), CStrIntern());
    650668                        }
    651669                       
    652670                        // bind the samplers to the shader
     
    657675                            CShaderProgram::Binding bind = texBindings[s];
    658676                            // check that the handles are current
    659677                            // and reevaluate them if necessary
    660                             if (texBindingNames[s] == samp.Name && bind.Active())
     678                            if (!(texBindingNames[s] == samp.Name))
    661679                            {
    662                                 bind = texBindings[s];
    663                             }
    664                             else
    665                             {
    666680                                bind = shader->GetTextureBinding(samp.Name.c_str());       
    667681                                texBindings[s] = bind;
    668682                                texBindingNames[s] = samp.Name;