Coder Social home page Coder Social logo

Comments (6)

rh101 avatar rh101 commented on September 17, 2024

PR #2105 shows the expected output (with 4 draw calls):
image

from axmol.

rh101 avatar rh101 commented on September 17, 2024

It seems that once a uniform value is updated, ProgramState::updateBatchId() needs to be called in order to update the batch ID, and that in turn fixes the issue. This also ensures that same-value uniforms are batched together.

from axmol.

smilediver avatar smilediver commented on September 17, 2024

I think it's a bug. You should not be required to call updateBatchId().

from axmol.

rh101 avatar rh101 commented on September 17, 2024

The batch ID is equal to the program ID if it is using one of the built in shaders:

bool ProgramState::init(Program* program)
{
...
    const auto programId = program->getProgramId();
    if (programId < ProgramType::BUILTIN_COUNT)
        this->_batchId = programId
...
}

The problem is when we do not use any of the built-in set of shaders; what does the batch ID get set to? Right now, it's set to a hash value. which is calculated via this:

void ProgramState::updateBatchId()
{
    _batchId = XXH64(_uniformBuffers.data(), _uniformBuffers.size(), _program->getProgramId());
}

This is an expensive call, and should only be made if and only if a custom shader is being used, and the uniform data has changed. The only way to set this would be to add a call in the setUniform method:

void ProgramState::setUniform(const backend::UniformLocation& uniformLocation, const void* data, std::size_t size)
{
    if (uniformLocation.vertStage)
        setVertexUniform(uniformLocation.vertStage.location, data, size, uniformLocation.vertStage.offset);
#ifdef AX_USE_METAL
    if (uniformLocation.fragStage)
        setFragmentUniform(uniformLocation.fragStage.location, data, size, uniformLocation.fragStage.offset);
#endif
    
    const auto programId = program->getProgramId();
    if (programId >= ProgramType::BUILTIN_COUNT)
        updateBatchId();
}

In this case we're adding extra logic that should only ever apply when using custom shaders, and I'm not sure how many developers would be using their own. Would this be a worthwhile change to ProgramState::setUniform?

Also, the change above doesn't check if the uniform data has changed, and only relies on the program using custom shaders.

@halx99 One other thing, is the hash generated by XXH64 guaranteed to be greater than the value of ProgramType::BUILTIN_COUNT?

from axmol.

halx99 avatar halx99 commented on September 17, 2024

@halx99 One other thing, is the hash generated by XXH64 guaranteed to be greater than the value of ProgramType::BUILTIN_COUNT?

Yes

from axmol.

smilediver avatar smilediver commented on September 17, 2024

Builtin shaders can also have different uniforms, so there will be situations when they can't be batched either. I don't see why builtin shaders should have different logic than user shaders. And now I have suspicion that builtin shader batching is working by chance if their uniforms are ignored.

Maybe this should be solved with a dirty flag set when modifying uniforms and a possibility to trigger update manually by using ProgramState::updateBatchId(), so you can do it after updating uniforms, while the uniform buffers are still in cache. But otherwise, it should be automatic.

from axmol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.