Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4 custom shader batching support #20584

Open
wants to merge 10 commits into
base: v4
Choose a base branch
from

Conversation

rh101
Copy link
Contributor

@rh101 rh101 commented Sep 17, 2020

Changed ProgramType IDs to be uint32_t, so that users can specify their own custom ID. This will enable users to batch custom shaders.

Note that the changes do not affect the operation of the built-in types or CUSTOM_PROGRAM type. The code only comes into effect when users set their own ID via Program::setProgramType() and optional ProgramState::setBatchId (for ProgramStates with different uniform values).

An example:

const uint32_t uniqueCustomShaderId = 1234;

auto* fileUtils = cocos2d::FileUtils::getInstance();
const auto fragmentFullPath = fileUtils->fullPathForFilename(path);
const auto fragSource = fileUtils->getStringFromFile(fragmentFullPath);
auto* program = Device::getInstance()->newProgram(cocos2d::positionTextureColor_vert, fragSource);
program->setProgramType(ProgramType::CUSTOM_PROGRAM | uniqueCustomShaderId);

auto* programState = new (std::nothrow) ProgramState(program);
programState->setBatchId(1); 

// These sprites use the same uniform values, both have batch ID = 1
sprite1->setProgramState(programState);
sprite2->setProgramState(programState->clone());

// Create a new cloned ProgramState that will have different uniform values
auto programState2 = programState->clone();
programState2->setBatchId(2);
// These sprites use the same uniform values, both have batch ID = 2
sprite3->setProgramState(programState2);
sprite4->setProgramState(programState2->clone());

This gives the users the flexibility of providing their own custom shaders while still allowing the nodes using them to be batched.

Sync with master branch
…prite batching with custom shaders. This also requires that ProgramState also have its own unique ID, since ProgramState can have different uniform values, and there is no other way to differentiate between ProgramStates, as checking uniform values is an expensive process.
…to its purpose, which is now _programStateBatchId
@rh101
Copy link
Contributor Author

rh101 commented Sep 24, 2020

If anyone has any suggestions on how to implement this differently to achieve the same outcome (supporting batching with custom shaders), then please do put your ideas forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant