1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "fsCRenderCommandLine.h"

#include <base/CCDirector.h>
#include <renderer/CCRenderer.h>

#include "../../../fsILineRenderData.h"


void fsCRenderCommandLine::batchDataRender()
{
	const auto& projectionMat = cocos2d::Director::getInstance()->getMatrix(
		cocos2d::MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	auto programState = _customCommandGLLine.getPipelineDescriptor().programState;
	programState->setUniform(_mvpMatrixLocation, projectionMat.m, sizeof(projectionMat.m));

	float alpha = 255.0 / 255.0;
	auto alphaUniformLocation = programState->getUniformLocation("u_alpha");
	programState->setUniform(alphaUniformLocation, &alpha, sizeof(alpha));

	_customCommandGLLine.init(_globalZOrder);
	cocos2d::Director::getInstance()->getRenderer()->addCommand(&_customCommandGLLine);
}

void fsCRenderCommandLine::batchDataPopulate(fsIRenderData const* pRenderData, fsS32 pOffest,
                                             fsIRenderer::eSPRITE_STRIP_PLACE pPlace)
{
	pRenderData->batchDataPopulate(_bufferGLLine, pPlace);
	_customCommandGLLine.updateVertexBuffer(_bufferGLLine, 0, _bufferCountGLLine * sizeof(cocos2d::V2F_C4B_T2F));
	_customCommandGLLine.setVertexDrawInfo(0, _bufferCountGLLine);
}

void fsCRenderCommandLine::buffersCreate(fsS32 pCount)
{
	if (_bufferCountGLLine + pCount > _bufferCapacityGLLine)
	{
		_bufferCapacityGLLine += MAX(_bufferCapacityGLLine, pCount);
		_bufferGLLine = static_cast<cocos2d::V2F_C4B_T2F*>(realloc(_bufferGLLine,
		                                                           _bufferCapacityGLLine * sizeof(cocos2d::V2F_C4B_T2F))
		);

		_customCommandGLLine.createVertexBuffer(sizeof(cocos2d::V2F_C4B_T2F), _bufferCapacityGLLine,
		                                        cocos2d::CustomCommand::BufferUsage::STATIC);
		_customCommandGLLine.updateVertexBuffer(_bufferGLLine, _bufferCapacityGLLine * sizeof(cocos2d::V2F_C4B_T2F));
	}

	_customCommandGLLine.setDrawType(cocos2d::CustomCommand::DrawType::ARRAY);
	_customCommandGLLine.setPrimitiveType(cocos2d::CustomCommand::PrimitiveType::LINE);

	_customCommandGLLine.setLineWidth(1);
}

void fsCRenderCommandLine::pipelineSetup(cocos2d::PipelineDescriptor& pPipelineDescriptor,
                                         cocos2d::backend::ProgramType pProgram)
{
	auto* program = cocos2d::backend::Program::getBuiltinProgram(pProgram);
	if ((_programState = new(std::nothrow) cocos2d::backend::ProgramState(program)))
	{
		_mvpMatrixLocation = _programState->getUniformLocation(cocos2d::backend::Uniform::MVP_MATRIX);
		_textureLocation = _programState->getUniformLocation(cocos2d::backend::Uniform::TEXTURE);
		_textColorLocation = _programState->getUniformLocation(cocos2d::backend::Uniform::TEXT_COLOR);

		const auto& vertexLayout = _programState->getVertexLayout();
		///a_position
		vertexLayout->setAttribute(cocos2d::backend::ATTRIBUTE_NAME_POSITION,
		                           _programState->getAttributeLocation(cocos2d::backend::Attribute::POSITION),
		                           cocos2d::backend::VertexFormat::FLOAT2,
		                           0,
		                           false);

		///a_texCoord
		vertexLayout->setAttribute(cocos2d::backend::ATTRIBUTE_NAME_TEXCOORD,
		                           _programState->getAttributeLocation(cocos2d::backend::Attribute::TEXCOORD),
		                           cocos2d::backend::VertexFormat::FLOAT2,
		                           offsetof(cocos2d::V2F_C4B_T2F, texCoords),
		                           false);

		///a_color
		vertexLayout->setAttribute(cocos2d::backend::ATTRIBUTE_NAME_COLOR,
		                           _programState->getAttributeLocation(cocos2d::backend::Attribute::COLOR),
		                           cocos2d::backend::VertexFormat::UBYTE4,
		                           offsetof(cocos2d::V2F_C4B_T2F, colors),
		                           true);
		vertexLayout->setLayout(sizeof(cocos2d::V2F_C4B_T2F));
		_programState->setUniform(_textColorLocation, &cocos2d::Color4B::WHITE,
		                          sizeof(cocos2d::backend::VertexFormat::UBYTE4));

		pPipelineDescriptor.programState = _programState;
	}

	_globalZOrder = 0;

	_blendFunc = cocos2d::BlendFunc::ALPHA_NON_PREMULTIPLIED;
	blendDescriptorSetup(pPipelineDescriptor);
}

void fsCRenderCommandLine::init(fsS32 pLines)
{
	buffersCreate(pLines);
	_bufferCountGLLine = pLines;
}

fsCRenderCommandLine::fsCRenderCommandLine(fsS32 pLines)
{
	pipelineSetup(_customCommandGLLine.getPipelineDescriptor(),
	              cocos2d::backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
	init(pLines);
}

fsCRenderCommandLine::~fsCRenderCommandLine()
{
	delete _customCommandGLLine.getPipelineDescriptor().programState;
}