Ticket #1859: ModelCache.patch
File ModelCache.patch, 4.1 KB (added by , 11 years ago) |
---|
-
Users/lancelot/Desktop/0ad-svn/trunk/source/renderer/ModelRenderer.cpp
453 468 } 454 469 455 470 SMRMaterialBucketKey key(model->GetMaterial().GetShaderEffect(), defs); 456 std::vector<CModel*>& bucketItems = materialBuckets[key]; 457 bucketItems.push_back(model); 471 materialBuckets[key].push_back(model); 458 472 } 459 473 } 460 461 474 std::vector<SMRSortByDistItem> sortByDistItems; 462 475 463 476 std::vector<CShaderTechniquePtr> sortByDistTechs; … … 574 587 size_t idxTechStart = 0; 575 588 576 589 // This vector keeps track of texture changes during rendering. It is kept outside the 577 // loops to avoid excessive reallocations. The token allocation of 64elements590 // loops to avoid excessive reallocations. The token allocation of 16 elements 578 591 // should be plenty, though it is reallocated below (at a cost) if necessary. 579 592 std::vector<CTexture*> currentTexs; 580 currentTexs.reserve( 64);593 currentTexs.reserve(16); 581 594 582 595 // texBindings holds the identifier bindings in the shader, which can no longer be defined 583 596 // statically in the ShaderRenderModifier class. texBindingNames uses interned strings to 584 597 // keep track of when bindings need to be reevaluated. 585 598 std::vector<CShaderProgram::Binding> texBindings; 586 texBindings.reserve( 64);599 texBindings.reserve(16); 587 600 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 590 611 while (idxTechStart < techBuckets.size()) 591 612 { 592 613 CShaderTechniquePtr currentTech = techBuckets[idxTechStart].tech; … … 611 632 612 633 m->vertexRenderer->BeginPass(streamflags); 613 634 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 } 620 642 621 643 CModelDef* currentModeldef = NULL; 622 644 CShaderUniforms currentStaticUniforms; … … 625 647 { 626 648 CModel** models = techBuckets[idx].models; 627 649 size_t numModels = techBuckets[idx].numModels; 650 628 651 for (size_t i = 0; i < numModels; ++i) 629 652 { 630 653 CModel* model = models[i]; … … 637 660 638 661 // make sure the vectors are the right virtual sizes, and also 639 662 // reallocate if there are more samplers than expected. 640 if (currentTexs. size() !=samplersNum)663 if (currentTexs.capacity() < samplersNum) 641 664 { 642 665 currentTexs.resize(samplersNum, NULL); 643 666 texBindings.resize(samplersNum, CShaderProgram::Binding()); 644 667 texBindingNames.resize(samplersNum, CStrIntern()); 645 646 // ensure they are definitely empty647 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());650 668 } 651 669 652 670 // bind the samplers to the shader … … 657 675 CShaderProgram::Binding bind = texBindings[s]; 658 676 // check that the handles are current 659 677 // and reevaluate them if necessary 660 if ( texBindingNames[s] == samp.Name && bind.Active())678 if (!(texBindingNames[s] == samp.Name)) 661 679 { 662 bind = texBindings[s];663 }664 else665 {666 680 bind = shader->GetTextureBinding(samp.Name.c_str()); 667 681 texBindings[s] = bind; 668 682 texBindingNames[s] = samp.Name;