cocos2d-x
cocos2d-x copied to clipboard
V4 custom shader batching support
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.
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.