582 | | // texBindings holds the identifier bindings in the shader, which can no longer be defined |
583 | | // statically in the ShaderRenderModifier class. texBindingNames uses interned strings to |
584 | | // keep track of when bindings need to be reevaluated. |
585 | | std::vector<CShaderProgram::Binding> texBindings; |
586 | | texBindings.reserve(64); |
587 | | std::vector<CStrIntern> texBindingNames; |
588 | | texBindingNames.reserve(64); |
589 | | |
| 575 | // using "4" as the maximal size of these arrays as it's what we currently use at most. |
| 576 | // will crash if using more. |
| 577 | // This is temporary. |
| 578 | |
| 579 | CTexture* currentTexs[4]; |
| 580 | CShaderProgram::Binding texBindings[4]; |
| 581 | CStrIntern texBindingNames[4]; |
| 582 | |
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(); |
| 607 | // reinitialize the values since we're using a new shader that has not be bound yet. |
| 608 | for (size_t i = 0; i < 4; ++i) |
| 609 | { |
| 610 | texBindingNames[i] = CStrIntern(); |
| 611 | currentTexs[i] = NULL; |
| 612 | texBindings[i] = CShaderProgram::Binding(); |
| 613 | } |
642 | | currentTexs.resize(samplersNum, NULL); |
643 | | texBindings.resize(samplersNum, CShaderProgram::Binding()); |
644 | | 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()); |
650 | | } |
651 | | |
652 | | // bind the samplers to the shader |
653 | | for (size_t s = 0; s < samplersNum; ++s) |
654 | | { |
655 | | CMaterial::TextureSampler &samp = samplers[s]; |
656 | | |
657 | | CShaderProgram::Binding bind = texBindings[s]; |
658 | | // check that the handles are current |
659 | | // and reevaluate them if necessary |
660 | | if (texBindingNames[s] == samp.Name && bind.Active()) |
| 637 | for (size_t s = 0; s < samplersNum; ++s) |
666 | | bind = shader->GetTextureBinding(samp.Name.c_str()); |
667 | | texBindings[s] = bind; |
668 | | texBindingNames[s] = samp.Name; |
| 649 | CMaterial::TextureSampler &samp = samplers[s]; |
| 650 | |
| 651 | CShaderProgram::Binding bind = texBindings[s]; |
| 652 | // check that the handles are current |
| 653 | // and reevaluate them if necessary |
| 654 | if (!(texBindingNames[s] == samp.Name)) |
| 655 | { |
| 656 | bind = shader->GetTextureBinding(samp.Name.c_str()); |
| 657 | texBindings[s] = bind; |
| 658 | texBindingNames[s] = samp.Name; |
| 659 | } |
| 660 | |
| 661 | // same with the actual sampler bindings |
| 662 | CTexture* newTex = samp.Sampler.get(); |
| 663 | if (bind.Active() && newTex != currentTexs[s]) |
| 664 | { |
| 665 | shader->BindTexture(bind, samp.Sampler->GetHandle()); |
| 666 | currentTexs[s] = newTex; |
| 667 | } |