Skip to content

Commit 2659f80

Browse files
Mee-guminggo
authored andcommitted
fix incorrect hash structure for RenderPipeline (#20373)
* fix incorrect hash structure for RenderPipeline
1 parent 138843c commit 2659f80

File tree

7 files changed

+31
-22
lines changed

7 files changed

+31
-22
lines changed

cocos/renderer/CCTrianglesCommand.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ void TrianglesCommand::init(float globalOrder, Texture2D* texture, const BlendFu
4848
}
4949
_mv = mv;
5050

51-
if (_program != _pipelineDescriptor.programState->getProgram() ||
51+
if (_programType != _pipelineDescriptor.programState->getProgram()->getProgramType() ||
5252
_texture != texture->getBackendTexture() ||
5353
_blendType != blendType)
5454
{
55-
_program = _pipelineDescriptor.programState->getProgram();
55+
_programType = _pipelineDescriptor.programState->getProgram()->getProgramType();
5656
_texture = texture->getBackendTexture();
5757
_blendType = blendType;
5858

5959
//since it would be too expensive to check the uniforms, simplify enable batching for built-in program.
60-
if(_program->getProgramType() == backend::ProgramType::INVALID_PROGRAM)
60+
if(_programType == backend::ProgramType::CUSTOM_PROGRAM)
6161
setSkipBatching(true);
6262

6363
//TODO: minggo set it in Node?
@@ -86,7 +86,7 @@ void TrianglesCommand::generateMaterialID()
8686
struct
8787
{
8888
void* texture;
89-
void* program;
89+
backend::ProgramType programType;
9090
backend::BlendFactor src;
9191
backend::BlendFactor dst;
9292
}hashMe;
@@ -99,7 +99,7 @@ void TrianglesCommand::generateMaterialID()
9999
hashMe.texture = _texture;
100100
hashMe.src = _blendType.src;
101101
hashMe.dst = _blendType.dst;
102-
hashMe.program = _program;
102+
hashMe.programType = _programType;
103103
_materialID = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
104104
}
105105

cocos/renderer/CCTrianglesCommand.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class CC_DLL TrianglesCommand : public RenderCommand
118118

119119
// Cached value to determine to generate material id or not.
120120
BlendFunc _blendType = BlendFunc::DISABLE;
121-
backend::Program* _program = nullptr;
121+
backend::ProgramType _programType = backend::ProgramType::CUSTOM_PROGRAM;
122122
backend::TextureBackend* _texture = nullptr;
123123
};
124124

cocos/renderer/backend/Program.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,6 @@ class Program : public Ref
120120
*/
121121
ProgramType getProgramType() const { return _programType; }
122122

123-
/**
124-
* Set engin built-in program type.
125-
* @param type Specifies the program type.
126-
*/
127-
void setProgramType(ProgramType type);
128-
129123
/**
130124
* Get uniform buffer size in bytes that can hold all the uniforms.
131125
* @param stage Specifies the shader stage. The symbolic constant can be either VERTEX or FRAGMENT.
@@ -146,8 +140,14 @@ class Program : public Ref
146140
* @return The uniformInfos.
147141
*/
148142
virtual const std::unordered_map<std::string, UniformInfo>& getAllActiveUniformInfo(ShaderStage stage) const = 0;
149-
143+
150144
protected:
145+
/**
146+
* Set engin built-in program type.
147+
* @param type Specifies the program type.
148+
*/
149+
void setProgramType(ProgramType type);
150+
151151
/**
152152
* @param vs Specifes the vertex shader source.
153153
* @param fs Specifes the fragment shader source.
@@ -179,12 +179,12 @@ class Program : public Ref
179179
*/
180180
virtual const std::unordered_map<std::string, int> getAllUniformsLocation() const = 0;
181181
friend class ProgramState;
182-
friend class ProgramCache;
183182
#endif
183+
friend class ProgramCache;
184184

185185
std::string _vertexShader; ///< Vertex shader.
186186
std::string _fragmentShader; ///< Fragment shader.
187-
ProgramType _programType = ProgramType::INVALID_PROGRAM; ///< built-in program type.
187+
ProgramType _programType = ProgramType::CUSTOM_PROGRAM; ///< built-in program type, initial value is CUSTOM_PROGRAM.
188188
};
189189

190190
//end of _backend group

cocos/renderer/backend/ShaderCache.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ backend::ShaderModule* ShaderCache::newShaderModule(backend::ShaderStage stage,
8282
return iter->second;
8383

8484
auto shader = backend::Device::getInstance()->newShaderModule(stage, shaderSource);
85+
shader->setHashValue(key);
8586
_cachedShaders.emplace(key, shader);
8687

8788
return shader;

cocos/renderer/backend/ShaderModule.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ class ShaderModule : public cocos2d::Ref
7171
*/
7272
ShaderStage getShaderStage() const;
7373

74+
std::size_t getHashValue() const { return _hash; }
7475

7576
protected:
7677
ShaderModule(ShaderStage stage);
7778
virtual ~ShaderModule();
78-
79+
void setHashValue(std::size_t hash) { _hash = hash; }
80+
81+
friend class ShaderCache;
7982
ShaderStage _stage = ShaderStage::VERTEX;
83+
std::size_t _hash = 0;
8084
};
8185

8286
//end of _backend group

cocos/renderer/backend/Types.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,8 @@ enum class TextureCubeFace : uint32_t
326326
NEGATIVE_Z = 5
327327
};
328328

329-
enum class ProgramType : int
329+
enum class ProgramType : size_t
330330
{
331-
INVALID_PROGRAM = -1,
332331
POSITION_COLOR_LENGTH_TEXTURE, //positionColorLengthTexture_vert, positionColorLengthTexture_frag
333332
POSITION_COLOR_TEXTURE_AS_POINTSIZE, //positionColorTextureAsPointsize_vert, positionColor_frag
334333
POSITION_COLOR, //positionColor_vert, positionColor_frag
@@ -362,6 +361,8 @@ enum class ProgramType : int
362361
SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D, //CC3D_skinPositionNormalTexture_vert, CC3D_colorNormalTexture_frag
363362
PARTICLE_TEXTURE_3D, //CC3D_particle_vert, CC3D_particleTexture_frag
364363
PARTICLE_COLOR_3D, //CC3D_particle_vert, CC3D_particleColor_frag
364+
365+
CUSTOM_PROGRAM, //user-define program
365366
};
366367

367368
///built-in uniform name

cocos/renderer/backend/metal/RenderPipelineMTL.mm

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ MTLBlendOperation toMTLBlendOperation(BlendOperation operation)
167167
{
168168
struct
169169
{
170-
void* program;
170+
size_t vertexShaderHash;
171+
size_t fragmentShaderHash;
171172
unsigned int vertexLayoutInfo[32];
172173
backend::PixelFormat colorAttachment;
173174
backend::PixelFormat depthAttachment;
@@ -185,7 +186,9 @@ MTLBlendOperation toMTLBlendOperation(BlendOperation operation)
185186
memset(&hashMe, 0, sizeof(hashMe));
186187
const auto& blendDescriptor = pipelineDescirptor.blendDescriptor;
187188
getAttachmentFormat(renderPassDescriptor, _colorAttachmentsFormat[0], _depthAttachmentFormat, _stencilAttachmentFormat);
188-
hashMe.program = pipelineDescirptor.programState->getProgram();
189+
auto program = static_cast<ProgramMTL*>(pipelineDescirptor.programState->getProgram());
190+
hashMe.vertexShaderHash = program->getVertexShader()->getHashValue();
191+
hashMe.fragmentShaderHash = program->getFragmentShader()->getHashValue();
189192
hashMe.colorAttachment = _colorAttachmentsFormat[0];
190193
hashMe.depthAttachment = _depthAttachmentFormat;
191194
hashMe.stencilAttachment =_stencilAttachmentFormat;
@@ -215,8 +218,8 @@ MTLBlendOperation toMTLBlendOperation(BlendOperation operation)
215218
((unsigned int)attribute.needToBeNormallized & 0x1);
216219
}
217220

218-
NSUInteger hash = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
219-
NSNumber* key = [[NSNumber numberWithUnsignedInteger:hash] autorelease];
221+
unsigned int hash = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
222+
NSNumber* key = @(hash);
220223
id obj = [_mtlRenderPipelineStateCache objectForKey:key];
221224
if (obj != nil)
222225
{

0 commit comments

Comments
 (0)