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
2 changes: 1 addition & 1 deletion cocos/2d/CCSprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ void Sprite::updateShaders(const char* vert, const char* frag)
CC_SAFE_RELEASE(program);
}

void Sprite::setProgramState(backend::ProgramType type)
void Sprite::setProgramState(uint32_t type)
{
if(_programState != nullptr &&
_programState->getProgram()->getProgramType() == type)
Expand Down
2 changes: 1 addition & 1 deletion cocos/2d/CCSprite.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ CC_CONSTRUCTOR_ACCESS :
void updateStretchFactor();
void populateTriangle(int quadIndex, const V3F_C4B_T2F_Quad& quad);
void setMVPMatrixUniform();
void setProgramState(backend::ProgramType type);
void setProgramState(uint32_t type);
//
// Data used when the sprite is rendered using a SpriteSheet
//
Expand Down
14 changes: 10 additions & 4 deletions cocos/renderer/CCTrianglesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ void TrianglesCommand::init(float globalOrder, Texture2D* texture, const BlendFu

if (_programType != _pipelineDescriptor.programState->getProgram()->getProgramType() ||
_texture != texture->getBackendTexture() ||
_blendType != blendType)
_blendType != blendType ||
_programStateBatchId != _pipelineDescriptor.programState->getBatchId())
{
_programType = _pipelineDescriptor.programState->getProgram()->getProgramType();
_texture = texture->getBackendTexture();
_blendType = blendType;

//since it would be too expensive to check the uniforms, simplify enable batching for built-in program.
_programStateBatchId = _pipelineDescriptor.programState->getBatchId();

//since it would be too expensive to check the uniforms, simplify enable batching for built-in program, and user-specific unique IDs
// Users should assign custom shader programType by masking an ID value with with backend::ProgramType::CUSTOM_PROGRAM. For example, programType = backend::ProgramType::CUSTOM_PROGRAM | value;
// along with setting the ProgramState::setProgramStateId(uniqueId) for shaders using the same backend::Program but different ProgramState uniform values.
if(_programType == backend::ProgramType::CUSTOM_PROGRAM)
setSkipBatching(true);

Expand Down Expand Up @@ -86,7 +90,8 @@ void TrianglesCommand::generateMaterialID()
struct
{
void* texture;
backend::ProgramType programType;
uint32_t programType;
uint32_t programStateBatchId;
backend::BlendFactor src;
backend::BlendFactor dst;
}hashMe;
Expand All @@ -100,6 +105,7 @@ void TrianglesCommand::generateMaterialID()
hashMe.src = _blendType.src;
hashMe.dst = _blendType.dst;
hashMe.programType = _programType;
hashMe.programStateBatchId = _programStateBatchId;
_materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
}

Expand Down
3 changes: 2 additions & 1 deletion cocos/renderer/CCTrianglesCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class CC_DLL TrianglesCommand : public RenderCommand

// Cached value to determine to generate material id or not.
BlendFunc _blendType = BlendFunc::DISABLE;
backend::ProgramType _programType = backend::ProgramType::CUSTOM_PROGRAM;
uint32_t _programType = backend::ProgramType::CUSTOM_PROGRAM;
uint32_t _programStateBatchId = 0;
backend::TextureBackend* _texture = nullptr;
};

Expand Down
4 changes: 2 additions & 2 deletions cocos/renderer/backend/Program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Program::Program(const std::string& vs, const std::string& fs)
{
}

void Program::setProgramType(ProgramType type)
void Program::setProgramType(uint32_t type)
{
_programType = type;
}

Program* Program::getBuiltinProgram(ProgramType type)
Program* Program::getBuiltinProgram(uint32_t type)
{
return ProgramCache::getInstance()->getBuiltinProgram(type);
}
Expand Down
13 changes: 7 additions & 6 deletions cocos/renderer/backend/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class Program : public Ref
* Get engine built-in program.
* @param type Specifies the built-in program type.
*/
static Program* getBuiltinProgram(ProgramType type);
static Program* getBuiltinProgram(uint32_t type);

/**
* Get uniform location by name.
Expand Down Expand Up @@ -118,7 +118,7 @@ class Program : public Ref
* Get engine built-in program type.
* @return The built-in program type.
*/
ProgramType getProgramType() const { return _programType; }
uint32_t getProgramType() const { return _programType; }

/**
* Get uniform buffer size in bytes that can hold all the uniforms.
Expand All @@ -140,13 +140,14 @@ class Program : public Ref
* @return The uniformInfos.
*/
virtual const std::unordered_map<std::string, UniformInfo>& getAllActiveUniformInfo(ShaderStage stage) const = 0;

protected:

/**
* Set engin built-in program type.
* @param type Specifies the program type.
*/
void setProgramType(ProgramType type);
void setProgramType(uint32_t type);

protected:

/**
* @param vs Specifes the vertex shader source.
Expand Down Expand Up @@ -184,7 +185,7 @@ class Program : public Ref

std::string _vertexShader; ///< Vertex shader.
std::string _fragmentShader; ///< Fragment shader.
ProgramType _programType = ProgramType::CUSTOM_PROGRAM; ///< built-in program type, initial value is CUSTOM_PROGRAM.
uint32_t _programType = ProgramType::CUSTOM_PROGRAM; ///< built-in program type, initial value is CUSTOM_PROGRAM.
};

//end of _backend group
Expand Down
20 changes: 3 additions & 17 deletions cocos/renderer/backend/ProgramCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,6 @@
#include "base/ccMacros.h"
#include "base/CCConfiguration.h"

namespace std
{
template <>
struct hash<cocos2d::backend::ProgramType>
{
typedef cocos2d::backend::ProgramType argument_type;
typedef std::size_t result_type;
result_type operator()(argument_type const& v) const
{
return hash<int>()(static_cast<int>(v));
}
};
}

CC_BACKEND_BEGIN

namespace
Expand All @@ -63,7 +49,7 @@ namespace
}
}

std::unordered_map<backend::ProgramType, backend::Program*> ProgramCache::_cachedPrograms;
std::unordered_map<uint32_t, backend::Program*> ProgramCache::_cachedPrograms;
std::unordered_map<std::string, backend::Program*> ProgramCache::_cachedCustomPrograms;

ProgramCache* ProgramCache::_sharedProgramCache = nullptr;
Expand Down Expand Up @@ -131,7 +117,7 @@ bool ProgramCache::init()
return true;
}

void ProgramCache::addProgram(ProgramType type)
void ProgramCache::addProgram(uint32_t type)
{
Program* program = nullptr;
switch (type) {
Expand Down Expand Up @@ -250,7 +236,7 @@ void ProgramCache::addProgram(ProgramType type)
ProgramCache::_cachedPrograms.emplace(type, program);
}

backend::Program* ProgramCache::getBuiltinProgram(ProgramType type) const
backend::Program* ProgramCache::getBuiltinProgram(uint32_t type) const
{
const auto& iter = ProgramCache::_cachedPrograms.find(type);
if (ProgramCache::_cachedPrograms.end() != iter)
Expand Down
6 changes: 3 additions & 3 deletions cocos/renderer/backend/ProgramCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ProgramCache : public Ref
static void destroyInstance();

/// get built-in program
backend::Program* getBuiltinProgram(ProgramType type) const;
backend::Program* getBuiltinProgram(uint32_t type) const;

/**
* Remove a program object from cache.
Expand Down Expand Up @@ -89,9 +89,9 @@ class ProgramCache : public Ref
bool init();

/// Add built-in program
void addProgram(ProgramType type);
void addProgram(uint32_t type);

static std::unordered_map<backend::ProgramType, backend::Program*> _cachedPrograms; ///< The cached program object.
static std::unordered_map<uint32_t, backend::Program*> _cachedPrograms; ///< The cached program object.
static std::unordered_map<std::string, backend::Program*> _cachedCustomPrograms; ///< The cached custom program object.
static ProgramCache *_sharedProgramCache; ///< A shared instance of the program cache.
};
Expand Down
4 changes: 4 additions & 0 deletions cocos/renderer/backend/ProgramState.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ class ProgramState : public Ref
*/
void setParameterAutoBinding(const std::string &uniformName, const std::string &autoBinding);

void setBatchId(uint32_t batchId) { _programStateBatchId = batchId; }
uint32_t getBatchId() const { return _programStateBatchId; }

inline std::shared_ptr<VertexLayout> getVertexLayout() const { return _vertexLayout; }
protected:

Expand Down Expand Up @@ -317,6 +320,7 @@ class ProgramState : public Ref
*/
void applyAutoBinding(const std::string &, const std::string &);

uint32_t _programStateBatchId = 0;
backend::Program* _program = nullptr;
std::unordered_map<UniformLocation, UniformCallback, UniformLocation> _callbackUniforms;
char* _vertexUniformBuffer = nullptr;
Expand Down
72 changes: 36 additions & 36 deletions cocos/renderer/backend/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,43 +326,43 @@ enum class TextureCubeFace : uint32_t
NEGATIVE_Z = 5
};

enum class ProgramType : size_t
class ProgramType
{
POSITION_COLOR_LENGTH_TEXTURE, //positionColorLengthTexture_vert, positionColorLengthTexture_frag
POSITION_COLOR_TEXTURE_AS_POINTSIZE, //positionColorTextureAsPointsize_vert, positionColor_frag
POSITION_COLOR, //positionColor_vert, positionColor_frag
POSITION, //position_vert, positionColor_frag
POSITION_UCOLOR, //positionUColor_vert, positionUColor_frag
POSITION_TEXTURE, //positionTexture_vert, positionTexture_frag
POSITION_TEXTURE_COLOR, //positionTextureColor_vert, positionTextureColor_frag
POSITION_TEXTURE_COLOR_ALPHA_TEST, //positionTextureColor_vert, positionTextureColorAlphaTest_frag
LABEL_NORMAL, //positionTextureColor_vert, label_normal_frag
LABLE_OUTLINE, //positionTextureColor_vert, labelOutline_frag
LABLE_DISTANCEFIELD_GLOW, //positionTextureColor_vert, labelDistanceFieldGlow_frag
LABEL_DISTANCE_NORMAL, //positionTextureColor_vert, label_distanceNormal_frag

LAYER_RADIA_GRADIENT, //position_vert, layer_radialGradient_frag

ETC1, //positionTextureColor_vert, etc1_frag
ETC1_GRAY, //positionTextureColor_vert, etc1Gray_frag
GRAY_SCALE, //positionTextureColor_vert, grayScale_frag
CAMERA_CLEAR, //cameraClear_vert, cameraClear_frag

TERRAIN_3D, //CC3D_terrain_vert, CC3D_terrain_frag
LINE_COLOR_3D, //lineColor3D_vert, lineColor3D_frag
SKYBOX_3D, //CC3D_skybox_vert, CC3D_skybox_frag
SKINPOSITION_TEXTURE_3D, //CC3D_skinPositionTexture_vert, CC3D_colorTexture_frag
SKINPOSITION_NORMAL_TEXTURE_3D, //CC3D_skinPositionNormalTexture_vert, CC3D_colorNormalTexture_frag
POSITION_NORMAL_TEXTURE_3D, //CC3D_positionNormalTexture_vert, CC3D_colorNormalTexture_frag
POSITION_NORMAL_3D, //CC3D_positionNormalTexture_vert, CC3D_colorNormal_frag
POSITION_TEXTURE_3D, //CC3D_positionTexture_vert, CC3D_colorTexture_frag
POSITION_3D, //CC3D_positionTexture_vert, CC3D_color_frag
POSITION_BUMPEDNORMAL_TEXTURE_3D, //CC3D_positionNormalTexture_vert, CC3D_colorNormalTexture_frag
SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D, //CC3D_skinPositionNormalTexture_vert, CC3D_colorNormalTexture_frag
PARTICLE_TEXTURE_3D, //CC3D_particle_vert, CC3D_particleTexture_frag
PARTICLE_COLOR_3D, //CC3D_particle_vert, CC3D_particleColor_frag

CUSTOM_PROGRAM, //user-define program
public:
static const uint32_t POSITION_COLOR_LENGTH_TEXTURE = 0; //positionColorLengthTexture_vert, positionColorLengthTexture_frag
static const uint32_t POSITION_COLOR_TEXTURE_AS_POINTSIZE = 1; //positionColorTextureAsPointsize_vert, positionColor_frag
static const uint32_t POSITION_COLOR = 2; //positionColor_vert, positionColor_frag
static const uint32_t POSITION = 3; //position_vert, positionColor_frag
static const uint32_t POSITION_UCOLOR = 4; //positionUColor_vert, positionUColor_frag
static const uint32_t POSITION_TEXTURE = 5; //positionTexture_vert, positionTexture_frag
static const uint32_t POSITION_TEXTURE_COLOR = 6; //positionTextureColor_vert, positionTextureColor_frag
static const uint32_t POSITION_TEXTURE_COLOR_ALPHA_TEST = 7; //positionTextureColor_vert, positionTextureColorAlphaTest_frag
static const uint32_t LABEL_NORMAL = 8; //positionTextureColor_vert, label_normal_frag
static const uint32_t LABLE_OUTLINE = 9; //positionTextureColor_vert, labelOutline_frag
static const uint32_t LABLE_DISTANCEFIELD_GLOW = 10; //positionTextureColor_vert, labelDistanceFieldGlow_frag
static const uint32_t LABEL_DISTANCE_NORMAL = 11; //positionTextureColor_vert, label_distanceNormal_frag

static const uint32_t LAYER_RADIA_GRADIENT = 12; //position_vert, layer_radialGradient_frag

static const uint32_t ETC1 = 13; //positionTextureColor_vert, etc1_frag
static const uint32_t ETC1_GRAY = 14; //positionTextureColor_vert, etc1Gray_frag
static const uint32_t GRAY_SCALE = 15; //positionTextureColor_vert, grayScale_frag
static const uint32_t CAMERA_CLEAR = 16; //cameraClear_vert, cameraClear_frag
static const uint32_t TERRAIN_3D = 17; //CC3D_terrain_vert, CC3D_terrain_frag
static const uint32_t LINE_COLOR_3D = 18; //lineColor3D_vert, lineColor3D_frag
static const uint32_t SKYBOX_3D = 19; //CC3D_skybox_vert, CC3D_skybox_frag
static const uint32_t SKINPOSITION_TEXTURE_3D = 20; //CC3D_skinPositionTexture_vert, CC3D_colorTexture_frag
static const uint32_t SKINPOSITION_NORMAL_TEXTURE_3D = 21; //CC3D_skinPositionNormalTexture_vert, CC3D_colorNormalTexture_frag
static const uint32_t POSITION_NORMAL_TEXTURE_3D = 22; //CC3D_positionNormalTexture_vert, CC3D_colorNormalTexture_frag
static const uint32_t POSITION_NORMAL_3D = 23; //CC3D_positionNormalTexture_vert, CC3D_colorNormal_frag
static const uint32_t POSITION_TEXTURE_3D = 24; //CC3D_positionTexture_vert, CC3D_colorTexture_frag
static const uint32_t POSITION_3D = 25; //CC3D_positionTexture_vert, CC3D_color_frag
static const uint32_t POSITION_BUMPEDNORMAL_TEXTURE_3D = 26; //CC3D_positionNormalTexture_vert, CC3D_colorNormalTexture_frag
static const uint32_t SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D = 27; //CC3D_skinPositionNormalTexture_vert, CC3D_colorNormalTexture_frag
static const uint32_t PARTICLE_TEXTURE_3D = 28; //CC3D_particle_vert, CC3D_particleTexture_frag
static const uint32_t PARTICLE_COLOR_3D = 29; //CC3D_particle_vert, CC3D_particleColor_frag

static const uint32_t CUSTOM_PROGRAM = 0x1000; //user-define program
};

///built-in uniform name
Expand Down