Skip to content

Commit e683ab6

Browse files
committed
Merge branch 'minggo/metal-support' into feature/updateFPS
* minggo/metal-support: [Feature] update Scissor Test (cocos2d#161) fix motionstreak issue support motionstreak, effect is wrong # Conflicts: # cocos/renderer/CCRenderer.cpp
2 parents c104c07 + 265da99 commit e683ab6

17 files changed

+350
-272
lines changed

cocos/2d/CCLabel.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1505,14 +1505,14 @@ float Label::getBMFontSize()const
15051505

15061506
void Label::updateBuffer(TextureAtlas* textureAtlas, CustomCommand& customCommand)
15071507
{
1508-
if(textureAtlas->getTotalQuads() > customCommand.getVertexCount())
1508+
if(textureAtlas->getTotalQuads() > customCommand.getVertexCapacity())
15091509
{
15101510
customCommand.createVertexBuffer((unsigned int)sizeof(V3F_C4B_T2F_Quad), (unsigned int)textureAtlas->getTotalQuads());
15111511
customCommand.createIndexBuffer((unsigned int)sizeof(unsigned short), (unsigned int)textureAtlas->getTotalQuads()*6);
15121512
}
15131513
customCommand.updateVertexBuffer(textureAtlas->getQuads(), (unsigned int)(textureAtlas->getTotalQuads() * sizeof(V3F_C4B_T2F_Quad)) );
15141514
customCommand.updateIndexBuffer(textureAtlas->getIndices(), (unsigned int)(textureAtlas->getTotalQuads()*6*sizeof(unsigned short)) );
1515-
customCommand.setIndexDrawInfo(0, customCommand.getIndexCount());
1515+
customCommand.setIndexDrawInfo(0, (unsigned int)(textureAtlas->getTotalQuads()*6));
15161516
}
15171517

15181518
void Label::updateEffectUniforms(TextureAtlas* textureAtlas, Renderer *renderer, const Mat4 &transform)

cocos/2d/CCMotionStreak.cpp

+69-87
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,32 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2424
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
THE SOFTWARE.
2626
****************************************************************************/
27-
2827
#include "2d/CCMotionStreak.h"
2928
#include "math/CCVertex.h"
3029
#include "base/CCDirector.h"
3130
#include "base/ccUtils.h"
3231
#include "renderer/CCTextureCache.h"
3332
#include "renderer/CCTexture2D.h"
3433
#include "renderer/CCRenderer.h"
35-
#include "renderer/CCGLProgramState.h"
36-
#include "renderer/CCGLProgram.h"
34+
#include "renderer/CCShaderCache.h"
35+
#include "renderer/ccShaders.h"
3736

3837
NS_CC_BEGIN
3938

4039
MotionStreak::MotionStreak()
41-
: _fastMode(false)
42-
, _startingPositionInitialized(false)
43-
, _texture(nullptr)
44-
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
45-
, _stroke(0.0f)
46-
, _fadeDelta(0.0f)
47-
, _minSeg(0.0f)
48-
, _maxPoints(0)
49-
, _nuPoints(0)
50-
, _previousNuPoints(0)
51-
, _pointVertexes(nullptr)
52-
, _pointState(nullptr)
53-
, _vertices(nullptr)
54-
, _colorPointer(nullptr)
55-
, _texCoords(nullptr)
5640
{
41+
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
42+
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
43+
44+
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
45+
pipelineDescriptor.vertexShader = ShaderCache::newVertexShaderModule(positionTextureColor_vert);
46+
pipelineDescriptor.fragmentShader = ShaderCache::newFragmentShaderModule(positionTextureColor_frag);
47+
48+
auto& vertexLayout = pipelineDescriptor.vertexLayout;
49+
vertexLayout.setAtrribute("a_position", 0, backend::VertexFormat::FLOAT_R32G32, 0, false);
50+
vertexLayout.setAtrribute("a_texCoord", 1, backend::VertexFormat::FLOAT_R32G32, 2 * sizeof(float), false);
51+
vertexLayout.setAtrribute("a_color", 2, backend::VertexFormat::UBYTE_R8G8B8A8, 4 * sizeof(float), true);
52+
vertexLayout.setLayout(4 * sizeof(float) + 4 * sizeof(uint8_t), backend::VertexStepMode::VERTEX);
5753
}
5854

5955
MotionStreak::~MotionStreak()
@@ -109,28 +105,23 @@ bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Co
109105

110106
_positionR.setZero();
111107
_fastMode = true;
112-
_minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
108+
_minSeg = (minSeg == -1.0f) ? stroke/ 5.0f : minSeg;
113109
_minSeg *= _minSeg;
114110

115111
_stroke = stroke;
116-
_fadeDelta = 1.0f/fade;
112+
_fadeDelta = 1.0f / fade;
117113

118-
double fps = 1/Director::getInstance()->getAnimationInterval();
119-
_maxPoints = (int)(fade*fps)+2;
114+
double fps = 1 / Director::getInstance()->getAnimationInterval();
115+
_maxPoints = (int)(fade * fps) + 2;
120116

121-
_nuPoints = 0;
122117
_pointState = (float *)malloc(sizeof(float) * _maxPoints);
123118
_pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
124119

125-
_vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2);
126-
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
127-
_colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
128-
129-
// Set blend mode
130-
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
131-
132-
// shader state
133-
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR, texture));
120+
_vertexCount = _maxPoints * 2;
121+
_vertices = (Vec2*)malloc(sizeof(Vec2) * _vertexCount);
122+
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _vertexCount);
123+
_colorPointer = (uint8_t*)malloc(sizeof(uint8_t) * 4 * _vertexCount);
124+
_customCommand.createVertexBuffer(sizeof(Vec2) + sizeof(Tex2F) + sizeof(uint8_t) * 4, _vertexCount);
134125

135126
setTexture(texture);
136127
setColor(color);
@@ -249,35 +240,33 @@ void MotionStreak::setOpacityModifyRGB(bool /*bValue*/)
249240
{
250241
}
251242

252-
bool MotionStreak::isOpacityModifyRGB(void) const
243+
bool MotionStreak::isOpacityModifyRGB() const
253244
{
254245
return false;
255246
}
256247

257248
void MotionStreak::update(float delta)
258249
{
259250
if (!_startingPositionInitialized)
260-
{
261251
return;
262-
}
263-
252+
264253
delta *= _fadeDelta;
265254

266255
unsigned int newIdx, newIdx2, i, i2;
267256
unsigned int mov = 0;
268257

269258
// Update current points
270-
for(i = 0; i<_nuPoints; i++)
259+
for (i = 0; i < _nuPoints; i++)
271260
{
272-
_pointState[i]-=delta;
261+
_pointState[i] -= delta;
273262

274-
if(_pointState[i] <= 0)
263+
if (_pointState[i] <= 0)
275264
mov++;
276265
else
277266
{
278-
newIdx = i-mov;
267+
newIdx = i - mov;
279268

280-
if(mov>0)
269+
if (mov > 0)
281270
{
282271
// Move data
283272
_pointState[newIdx] = _pointState[i];
@@ -286,8 +275,8 @@ void MotionStreak::update(float delta)
286275
_pointVertexes[newIdx] = _pointVertexes[i];
287276

288277
// Move vertices
289-
i2 = i*2;
290-
newIdx2 = newIdx*2;
278+
i2 = i * 2;
279+
newIdx2 = newIdx * 2;
291280
_vertices[newIdx2] = _vertices[i2];
292281
_vertices[newIdx2+1] = _vertices[i2+1];
293282

@@ -301,39 +290,34 @@ void MotionStreak::update(float delta)
301290
_colorPointer[newIdx2+5] = _colorPointer[i2+5];
302291
_colorPointer[newIdx2+6] = _colorPointer[i2+6];
303292
}else
304-
newIdx2 = newIdx*8;
293+
newIdx2 = newIdx * 8;
305294

306-
const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
295+
const uint8_t op = (uint8_t)(_pointState[newIdx] * 255.0f);
307296
_colorPointer[newIdx2+3] = op;
308297
_colorPointer[newIdx2+7] = op;
309298
}
310299
}
311-
_nuPoints-=mov;
300+
_nuPoints -= mov;
312301

313302
// Append new point
314303
bool appendNewPoint = true;
315-
if(_nuPoints >= _maxPoints)
316-
{
304+
if (_nuPoints >= _maxPoints)
317305
appendNewPoint = false;
318-
}
319-
320-
else if(_nuPoints>0)
306+
else if (_nuPoints > 0)
321307
{
322308
bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
323-
bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
324-
if(a1 || a2)
325-
{
309+
bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR) < (_minSeg * 2.0f));
310+
if (a1 || a2)
326311
appendNewPoint = false;
327-
}
328312
}
329313

330-
if(appendNewPoint)
314+
if (appendNewPoint)
331315
{
332316
_pointVertexes[_nuPoints] = _positionR;
333317
_pointState[_nuPoints] = 1.0f;
334318

335319
// Color assignment
336-
const unsigned int offset = _nuPoints*8;
320+
const unsigned int offset = _nuPoints * 8;
337321
*((Color3B*)(_colorPointer + offset)) = _displayedColor;
338322
*((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
339323

@@ -342,7 +326,7 @@ void MotionStreak::update(float delta)
342326
_colorPointer[offset+7] = 255;
343327

344328
// Generate polygon
345-
if(_nuPoints > 0 && _fastMode )
329+
if (_nuPoints > 0 && _fastMode )
346330
{
347331
if(_nuPoints > 1)
348332
{
@@ -354,18 +338,17 @@ void MotionStreak::update(float delta)
354338
}
355339
}
356340

357-
_nuPoints ++;
341+
_nuPoints++;
358342
}
359343

360-
if( ! _fastMode )
361-
{
344+
345+
if (! _fastMode)
362346
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
363-
}
364347

365348
// Updated Tex Coords only if they are different than previous step
366-
if( _nuPoints && _previousNuPoints != _nuPoints ) {
349+
if (_nuPoints && _previousNuPoints != _nuPoints) {
367350
float texDelta = 1.0f / _nuPoints;
368-
for( i=0; i < _nuPoints; i++ ) {
351+
for (i = 0; i < _nuPoints; i++) {
369352
_texCoords[i*2] = Tex2F(0, texDelta*i);
370353
_texCoords[i*2+1] = Tex2F(1, texDelta*i);
371354
}
@@ -379,34 +362,33 @@ void MotionStreak::reset()
379362
_nuPoints = 0;
380363
}
381364

382-
void MotionStreak::onDraw(const Mat4 &transform, uint32_t /*flags*/)
383-
{
384-
getGLProgram()->use();
385-
getGLProgram()->setUniformsForBuiltins(transform);
386-
387-
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
388-
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
389-
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
390-
utils::setBlending(_blendFunc.src, _blendFunc.dst);
391-
392-
glActiveTexture(GL_TEXTURE0);
393-
glBindTexture(GL_TEXTURE_2D, _texture->getName());
394-
395-
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
396-
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
397-
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
398-
399-
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
400-
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints*2);
401-
}
402-
403365
void MotionStreak::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
404366
{
405367
if(_nuPoints <= 1)
406368
return;
407-
_customCommand.init(_globalZOrder, transform, flags);
408-
_customCommand.func = CC_CALLBACK_0(MotionStreak::onDraw, this, transform, flags);
369+
370+
auto drawCount = _nuPoints * 2;
371+
372+
_customCommand.init(_globalZOrder, _blendFunc);
373+
_customCommand.setVertexDrawInfo(0, drawCount);
409374
renderer->addCommand(&_customCommand);
375+
376+
auto& bindGroup = _customCommand.getPipelineDescriptor().bindGroup;
377+
bindGroup.setTexture("u_texture", 0, _texture->getBackendTexture());
378+
379+
const auto& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
380+
Mat4 finalMat = projectionMat * transform;
381+
bindGroup.setUniform("u_MVPMatrix", finalMat.m, sizeof(Mat4));
382+
383+
unsigned int offset = 0;
384+
unsigned int vertexSize = sizeof(Vec2) + sizeof(Vec2) + sizeof(uint8_t) * 4;
385+
for (unsigned int i = 0; i < drawCount; ++i)
386+
{
387+
offset = i * vertexSize;
388+
_customCommand.updateVertexBuffer(&_vertices[i], offset, sizeof(_vertices[0]));
389+
_customCommand.updateVertexBuffer(&_texCoords[i], offset + sizeof(_vertices[0]), sizeof(_texCoords[0]) );
390+
_customCommand.updateVertexBuffer(&_colorPointer[i * 4], offset + sizeof(_vertices[0]) + sizeof(_texCoords[0]), 4 * sizeof(uint8_t));
391+
}
410392
}
411393

412394
NS_CC_END

0 commit comments

Comments
 (0)