Skip to content

Commit 935af58

Browse files
Mee-guminggo
authored andcommitted
remove arrays of vertex layout (#20023)
1 parent 23732ab commit 935af58

File tree

7 files changed

+58
-81
lines changed

7 files changed

+58
-81
lines changed

cocos/renderer/CCRenderer.cpp

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -785,26 +785,20 @@ backend::RenderPipeline* Renderer::getRenderPipeline(const backend::RenderPipeli
785785
hashMe.sourceAlphaBlendFactor = (unsigned int)blendDescriptor.sourceAlphaBlendFactor;
786786
hashMe.destinationAlphaBlendFactor = (unsigned int)blendDescriptor.destinationAlphaBlendFactor;
787787
int index = 0;
788-
for(const auto& vertexLayout : *renderPipelineDescriptor.vertexLayouts)
788+
const auto& attributes = renderPipelineDescriptor.vertexLayout.getAttributes();
789+
for (const auto& it : attributes)
789790
{
790-
if (!vertexLayout.isValid())
791-
continue;
792-
793-
const auto& attributes = vertexLayout.getAttributes();
794-
for (const auto& it : attributes)
795-
{
796-
auto &attribute = it.second;
797-
/*
798-
stepFunction:1 stride:15 offest:10 format:5 needNormalized:1
799-
bit31 bit30 ~ bit16 bit15 ~ bit6 bit5 ~ bit1 bit0
800-
*/
801-
hashMe.vertexLayoutInfo[index++] =
802-
((unsigned int)vertexLayout.getVertexStepMode() & 0x1) << 31 |
803-
((unsigned int)(vertexLayout.getStride() & 0x7FFF)) << 16 |
804-
((unsigned int)attribute.offset & 0x3FF) << 6 |
805-
((unsigned int)attribute.format & 0x1F) << 1 |
806-
((unsigned int)attribute.needToBeNormallized & 0x1);
807-
}
791+
auto &attribute = it.second;
792+
/*
793+
stepFunction:1 stride:15 offest:10 format:5 needNormalized:1
794+
bit31 bit30 ~ bit16 bit15 ~ bit6 bit5 ~ bit1 bit0
795+
*/
796+
hashMe.vertexLayoutInfo[index++] =
797+
((unsigned int)renderPipelineDescriptor.vertexLayout.getVertexStepMode() & 0x1) << 31 |
798+
((unsigned int)(renderPipelineDescriptor.vertexLayout.getStride() & 0x7FFF)) << 16 |
799+
((unsigned int)attribute.offset & 0x3FF) << 6 |
800+
((unsigned int)attribute.format & 0x1F) << 1 |
801+
((unsigned int)attribute.needToBeNormallized & 0x1);
808802
}
809803

810804
unsigned int hash = XXH32((const void*)&hashMe, sizeof(hashMe), 0);
@@ -825,7 +819,7 @@ void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, c
825819
{
826820
backend::RenderPipelineDescriptor renderPipelineDescriptor;
827821
renderPipelineDescriptor.programState = pipelineDescriptor.programState;
828-
renderPipelineDescriptor.vertexLayouts->push_back(pipelineDescriptor.vertexLayout);
822+
renderPipelineDescriptor.vertexLayout = pipelineDescriptor.vertexLayout;
829823

830824
auto device = backend::Device::getInstance();
831825
auto blendState = device->createBlendState(pipelineDescriptor.blendDescriptor);
@@ -867,7 +861,7 @@ void Renderer::setRenderPipeline(const PipelineDescriptor& pipelineDescriptor, c
867861
_commandBuffer->setDepthStencilState(depthStencilState);
868862
#ifndef CC_USE_METAL
869863
// Extra layout info is required in OpenGL, which can not be quried from ProgramGL
870-
_commandBuffer->updateVertexLayouts(renderPipelineDescriptor.vertexLayouts);
864+
_commandBuffer->updateVertexLayout(renderPipelineDescriptor.vertexLayout);
871865
#endif
872866
}
873867

cocos/renderer/backend/CommandBuffer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,20 @@ class CommandBuffer : public cocos2d::Ref
188188
* Get vertex layout.
189189
* @return A vector of vertex layout.
190190
*/
191-
inline const std::shared_ptr<std::vector<VertexLayout>>& getVertexLayouts() const { return _vertexLayouts; }
191+
inline const VertexLayout& getVertexLayout() const { return _vertexLayout; }
192192

193193
/**
194194
* Update vertex layoutjj.
195195
* @param layout Specifies the vertex layout.
196196
*/
197-
void updateVertexLayouts(const std::shared_ptr<std::vector<VertexLayout>> &layout) { _vertexLayouts = layout; }
197+
void updateVertexLayout(const VertexLayout &layout) { _vertexLayout = layout; }
198198

199199
protected:
200200
virtual ~CommandBuffer() = default;
201201

202202
unsigned int _stencilReferenceValueFront = 0; ///< front stencil reference value.
203203
unsigned int _stencilReferenceValueBack = 0; ///< back stencil reference value.
204-
std::shared_ptr<std::vector<VertexLayout>> _vertexLayouts; ///< vertex layout.
204+
VertexLayout _vertexLayout; ///< vertex layout.
205205
};
206206

207207
// end of _backend group

cocos/renderer/backend/RenderPipelineDescriptor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct RenderPipelineDescriptor
4848
{
4949
ProgramState* programState = nullptr;
5050
BlendState* blendState = nullptr;
51-
std::shared_ptr<std::vector<VertexLayout>> vertexLayouts = std::make_shared<std::vector<VertexLayout>>();
51+
VertexLayout vertexLayout;
5252
PixelFormat colorAttachmentsFormat[MAX_COLOR_ATTCHMENT] = { PixelFormat::DEFAULT };
5353
PixelFormat depthAttachmentFormat = PixelFormat::NONE;
5454
PixelFormat stencilAttachmentFormat = PixelFormat::NONE;

cocos/renderer/backend/metal/RenderPipelineMTL.mm

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -119,28 +119,24 @@ MTLVertexFormat toMTLVertexFormat(VertexFormat vertexFormat, bool needNormalize)
119119

120120
void RenderPipelineMTL::setVertexLayout(MTLRenderPipelineDescriptor* mtlDescriptor, const RenderPipelineDescriptor& descriptor)
121121
{
122-
const auto& vertexLayouts = *descriptor.vertexLayouts;
123122
int vertexIndex = 0;
124-
for (const auto& vertexLayout : vertexLayouts)
123+
124+
if (!descriptor.vertexLayout.isValid())
125+
return;
126+
127+
mtlDescriptor.vertexDescriptor.layouts[vertexIndex].stride = descriptor.vertexLayout.getStride();
128+
mtlDescriptor.vertexDescriptor.layouts[vertexIndex].stepFunction = toMTLVertexStepFunction(descriptor.vertexLayout.getVertexStepMode());
129+
130+
const auto& attributes = descriptor.vertexLayout.getAttributes();
131+
for (const auto& it : attributes)
125132
{
126-
if (!vertexLayout.isValid())
127-
continue;
128-
129-
mtlDescriptor.vertexDescriptor.layouts[vertexIndex].stride = vertexLayout.getStride();
130-
mtlDescriptor.vertexDescriptor.layouts[vertexIndex].stepFunction = toMTLVertexStepFunction(vertexLayout.getVertexStepMode());
131-
132-
const auto& attributes = vertexLayout.getAttributes();
133-
for (const auto& it : attributes)
134-
{
135-
auto attribute = it.second;
136-
mtlDescriptor.vertexDescriptor.attributes[attribute.index].format = toMTLVertexFormat(attribute.format, attribute.needToBeNormallized);
137-
mtlDescriptor.vertexDescriptor.attributes[attribute.index].offset = attribute.offset;
138-
// Buffer index will always be 0;
139-
mtlDescriptor.vertexDescriptor.attributes[attribute.index].bufferIndex = 0;
140-
}
141-
142-
++vertexIndex;
133+
auto attribute = it.second;
134+
mtlDescriptor.vertexDescriptor.attributes[attribute.index].format = toMTLVertexFormat(attribute.format, attribute.needToBeNormallized);
135+
mtlDescriptor.vertexDescriptor.attributes[attribute.index].offset = attribute.offset;
136+
// Buffer index will always be 0;
137+
mtlDescriptor.vertexDescriptor.attributes[attribute.index].bufferIndex = 0;
143138
}
139+
144140
}
145141

146142
void RenderPipelineMTL::setBlendState(MTLRenderPipelineColorAttachmentDescriptor* colorAttachmentDescriptor)

cocos/renderer/backend/opengl/CommandBufferGL.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -414,21 +414,17 @@ void CommandBufferGL::bindVertexBuffer(ProgramGL *program) const
414414
// Bind vertex buffers and set the attributes.
415415
int i = 0;
416416
const auto& attributeInfos = program->getAttributeInfos();
417-
const auto& vertexLayouts = getVertexLayouts();
418-
417+
const auto& vertexLayout = getVertexLayout();
419418
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getHandler());
420-
421-
const auto& attributeInfo = attributeInfos[i];
422-
const auto &layouts = vertexLayouts->at(i);
423-
for (const auto& attribute : attributeInfo)
419+
for (const auto& attribute : attributeInfos)
424420
{
425-
const auto &layoutInfo = layouts.getAttributes().at(attribute.name);
421+
const auto &layoutInfo = vertexLayout.getAttributes().at(attribute.name);
426422
glEnableVertexAttribArray(attribute.location);
427423
glVertexAttribPointer(attribute.location,
428424
UtilsGL::getGLAttributeSize(layoutInfo.format),
429425
UtilsGL::toGLAttributeType(layoutInfo.format),
430426
layoutInfo.needToBeNormallized,
431-
layouts.getStride(),
427+
vertexLayout.getStride(),
432428
(GLvoid*)layoutInfo.offset);
433429
}
434430

cocos/renderer/backend/opengl/ProgramGL.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -185,34 +185,27 @@ void ProgramGL::computeLocations()
185185
void ProgramGL::computeAttributeInfos(const RenderPipelineDescriptor& descriptor)
186186
{
187187
_attributeInfos.clear();
188-
const auto& vertexLayouts = descriptor.vertexLayouts;
189-
for (const auto& vertexLayout : *vertexLayouts)
188+
189+
if (! descriptor.vertexLayout.isValid())
190+
return;
191+
192+
const auto& attributes = descriptor.vertexLayout.getAttributes();
193+
for (const auto& it : attributes)
190194
{
191-
if (! vertexLayout.isValid())
192-
continue;
195+
auto &attribute = it.second;
196+
AttributeInfo attributeInfo;
193197

194-
VertexAttributeArray vertexAttributeArray;
195-
196-
const auto& attributes = vertexLayout.getAttributes();
197-
for (const auto& it : attributes)
198-
{
199-
auto &attribute = it.second;
200-
AttributeInfo attributeInfo;
201-
202-
if (!getAttributeLocation(attribute.name, attributeInfo.location))
203-
continue;
204-
205-
attributeInfo.stride = vertexLayout.getStride();
206-
attributeInfo.offset = attribute.offset;
207-
attributeInfo.type = UtilsGL::toGLAttributeType(attribute.format);
208-
attributeInfo.size = UtilsGL::getGLAttributeSize(attribute.format);
209-
attributeInfo.needToBeNormallized = attribute.needToBeNormallized;
210-
attributeInfo.name = attribute.name;
211-
212-
vertexAttributeArray.push_back(attributeInfo);
213-
}
198+
if (!getAttributeLocation(attribute.name, attributeInfo.location))
199+
continue;
214200

215-
_attributeInfos.push_back(std::move(vertexAttributeArray));
201+
attributeInfo.stride = descriptor.vertexLayout.getStride();
202+
attributeInfo.offset = attribute.offset;
203+
attributeInfo.type = UtilsGL::toGLAttributeType(attribute.format);
204+
attributeInfo.size = UtilsGL::getGLAttributeSize(attribute.format);
205+
attributeInfo.needToBeNormallized = attribute.needToBeNormallized;
206+
attributeInfo.name = attribute.name;
207+
208+
_attributeInfos.emplace_back(std::move(attributeInfo));
216209
}
217210
}
218211

cocos/renderer/backend/opengl/ProgramGL.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ struct AttributeInfo
6565
class ProgramGL : public Program
6666
{
6767
public:
68-
typedef std::vector<AttributeInfo> VertexAttributeArray;
69-
7068
/**
7169
* @param vertexShader Specifes the vertex shader source.
7270
* @param fragmentShader Specifes the fragment shader source.
@@ -79,7 +77,7 @@ class ProgramGL : public Program
7977
* Get attribute informations.
8078
* @return Attribute informations.
8179
*/
82-
inline const std::vector<VertexAttributeArray>& getAttributeInfos() const { return _attributeInfos; }
80+
inline const std::vector<AttributeInfo>& getAttributeInfos() const { return _attributeInfos; }
8381

8482
/**
8583
* Get program object.
@@ -175,7 +173,7 @@ class ProgramGL : public Program
175173
ShaderModuleGL* _vertexShaderModule = nullptr;
176174
ShaderModuleGL* _fragmentShaderModule = nullptr;
177175

178-
std::vector<VertexAttributeArray> _attributeInfos;
176+
std::vector<AttributeInfo> _attributeInfos;
179177
std::unordered_map<std::string, UniformInfo> _activeUniformInfos;
180178
#if CC_ENABLE_CACHE_TEXTURE_DATA
181179
std::unordered_map<std::string, int> _originalUniformLocations; ///< record the uniform location when shader was first created.

0 commit comments

Comments
 (0)