fix ProgramState and Program performance (#20189)

* fix ProgramState and Program performance

* fix review

* fix

* add autorelease
This commit is contained in:
coulsonwang 2019-10-15 09:40:59 +08:00 committed by minggo
parent 327407d59b
commit 67ae2e756e
46 changed files with 345 additions and 238 deletions

View File

@ -44,7 +44,8 @@ NS_CC_BEGIN
AtlasNode::AtlasNode()
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");

View File

@ -122,7 +122,8 @@ CameraBackgroundDepthBrush* CameraBackgroundDepthBrush::create(float depth)
bool CameraBackgroundDepthBrush::init()
{
CC_SAFE_RELEASE_NULL(_programState);
_programState = new backend::ProgramState(cameraClear_vert, cameraClear_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::CAMERA_CLEAR);
_programState = new backend::ProgramState(program);
_locDepth = _programState->getUniformLocation("dpeth");
@ -409,7 +410,8 @@ bool CameraBackgroundSkyBoxBrush::init()
_customCommand.setAfterCallback(CC_CALLBACK_0(CameraBackgroundSkyBoxBrush::onAfterDraw, this));
CC_SAFE_RELEASE_NULL(_programState);
_programState = new backend::ProgramState(CC3D_skybox_vert, CC3D_skybox_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D);
_programState = new backend::ProgramState(program);
_uniformColorLoc = _programState->getUniformLocation("u_color");
_uniformCameraRotLoc = _programState->getUniformLocation("u_cameraRot");
_uniformEnvLoc = _programState->getUniformLocation("u_Env");

View File

@ -198,7 +198,8 @@ void ClippingNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32
auto alphaThreshold = this->getAlphaThreshold();
if (alphaThreshold < 1)
{
auto programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColorAlphaTest_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
auto programState = new (std::nothrow) backend::ProgramState(program);
auto alphaLocation = programState->getUniformLocation("u_alpha_value");
programState->setUniform(alphaLocation, &alphaThreshold, sizeof(alphaThreshold));
setProgramStateRecursively(_stencil, programState);

View File

@ -149,21 +149,24 @@ bool DrawNode::init()
void DrawNode::updateShader()
{
CC_SAFE_RELEASE(_programState);
_programState = new (std::nothrow) backend::ProgramState(positionColorLengthTexture_vert, positionColorLengthTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
_programState = new (std::nothrow) backend::ProgramState(program);
_customCommand.getPipelineDescriptor().programState = _programState;
setVertexLayout(_customCommand);
_customCommand.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE);
CC_SAFE_RELEASE(_programStatePoint);
_programStatePoint = new (std::nothrow) backend::ProgramState(positionColorTextureAsPointsize_vert, positionColor_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE);
_programStatePoint = new (std::nothrow) backend::ProgramState(program);
_customCommandGLPoint.getPipelineDescriptor().programState = _programStatePoint;
setVertexLayout(_customCommandGLPoint);
_customCommandGLPoint.setDrawType(CustomCommand::DrawType::ARRAY);
_customCommandGLPoint.setPrimitiveType(CustomCommand::PrimitiveType::POINT);
CC_SAFE_RELEASE(_programStateLine);
_programStateLine = new (std::nothrow) backend::ProgramState(positionColorLengthTexture_vert, positionColorLengthTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
_programStateLine = new (std::nothrow) backend::ProgramState(program);
_customCommandGLLine.getPipelineDescriptor().programState = _programStateLine;
setVertexLayout(_customCommandGLLine);
_customCommandGLLine.setDrawType(CustomCommand::DrawType::ARRAY);

View File

@ -408,16 +408,17 @@ void TMXLayer::updatePrimitives()
if (_useAutomaticVertexZ)
{
CC_SAFE_RELEASE(pipelineDescriptor.programState);
auto programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColorAlphaTest_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
auto programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = programState;
_alphaValueLocation = pipelineDescriptor.programState->getUniformLocation("u_alpha_value");
pipelineDescriptor.programState->setUniform(_alphaValueLocation, &_alphaFuncValue, sizeof(_alphaFuncValue));
}
else
{
CC_SAFE_RELEASE(pipelineDescriptor.programState);
auto programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
auto programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = programState;
}
auto vertexLayout = pipelineDescriptor.programState->getVertexLayout();

View File

@ -32,9 +32,10 @@ THE SOFTWARE.
#include "renderer/CCRenderer.h"
#include "renderer/CCTexture2D.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Device.h"
#include "2d/CCCamera.h"
#include "renderer/backend/ProgramState.h"
NS_CC_BEGIN
// implementation of GridBase
@ -108,7 +109,8 @@ bool GridBase::initWithSize(const Size& gridSize, Texture2D *texture, bool flipp
auto& pipelineDescriptor = _drawCommand.getPipelineDescriptor();
CC_SAFE_RELEASE(_programState);
_programState = new (std::nothrow) backend::ProgramState(positionTexture_vert, positionTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");

View File

@ -688,7 +688,8 @@ void Label::updateShaderProgram()
}
CC_SAFE_RELEASE(_programState);
_programState = new backend::ProgramState(programType);
auto* program = backend::Program::getBuiltinProgram(programType);
_programState = new backend::ProgramState(program);
updateUniformLocations();

View File

@ -288,7 +288,8 @@ LayerColor::LayerColor()
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionColor_vert, positionColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
auto vertexLayout = _programState->getVertexLayout();
@ -705,7 +706,8 @@ LayerRadialGradient* LayerRadialGradient::create()
LayerRadialGradient::LayerRadialGradient()
{
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(position_vert, layer_radialGradient_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::LAYER_RADIA_GRADIENT);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocation = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_startColorLocation = pipelineDescriptor.programState->getUniformLocation("u_startColor");

View File

@ -42,7 +42,8 @@ MotionStreak::MotionStreak()
_customCommand.setPrimitiveType(CustomCommand::PrimitiveType::TRIANGLE_STRIP);
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");

View File

@ -46,7 +46,8 @@ NS_CC_BEGIN
ParticleBatchNode::ParticleBatchNode()
{
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");

View File

@ -50,7 +50,8 @@ NS_CC_BEGIN
ParticleSystemQuad::ParticleSystemQuad()
{
auto& pipelieDescriptor = _quadCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelieDescriptor.programState = _programState;
_mvpMatrixLocaiton = pipelieDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelieDescriptor.programState->getUniformLocation("u_texture");

View File

@ -47,7 +47,8 @@ namespace
backend::ProgramState* initPipelineDescriptor(cocos2d::CustomCommand& command, bool ridal, backend::UniformLocation &locMVP, backend::UniformLocation &locTexture)
{
auto& pipelieDescriptor = command.getPipelineDescriptor();
auto programState = new (std::nothrow) backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
auto programState = new (std::nothrow) backend::ProgramState(program);
CC_SAFE_RELEASE(pipelieDescriptor.programState);
pipelieDescriptor.programState = programState;

View File

@ -42,6 +42,7 @@ THE SOFTWARE.
#include "platform/CCFileUtils.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Device.h"
NS_CC_BEGIN
@ -374,9 +375,11 @@ void Sprite::setVertexLayout()
void Sprite::updateShaders(const char* vert, const char* frag)
{
auto programState = new (std::nothrow) backend::ProgramState(vert, frag);
auto* program = backend::Device::getInstance()->newProgram(vert, frag);
auto programState = new (std::nothrow) backend::ProgramState(program);
setProgramState(programState);
CC_SAFE_RELEASE_NULL(programState);
CC_SAFE_RELEASE(programState);
CC_SAFE_RELEASE(program);
}
void Sprite::setProgramState(backend::ProgramType type)
@ -385,7 +388,8 @@ void Sprite::setProgramState(backend::ProgramType type)
_programState->getProgram()->getProgramType() == type)
return;
auto programState = new (std::nothrow) backend::ProgramState(type);
auto* program = backend::Program::getBuiltinProgram(type);
auto programState = new (std::nothrow) backend::ProgramState(program);
setProgramState(programState);
CC_SAFE_RELEASE_NULL(programState);
}

View File

@ -37,7 +37,7 @@ THE SOFTWARE.
#include "renderer/CCQuadCommand.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Device.h"
NS_CC_BEGIN
@ -115,12 +115,14 @@ bool SpriteBatchNode::initWithTexture(Texture2D *tex, ssize_t capacity/* = DEFAU
void SpriteBatchNode::updateShaders(const std::string &vertexShader, const std::string &fragmentShader)
{
auto& pipelineDescriptor = _quadCommand.getPipelineDescriptor();
auto* program = backend::Device::getInstance()->newProgram(vertexShader, fragmentShader);
CC_SAFE_RELEASE(_programState);
_programState = new (std::nothrow) backend::ProgramState(vertexShader, fragmentShader);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpMatrixLocaiton = pipelineDescriptor.programState->getUniformLocation("u_MVPMatrix");
_textureLocation = pipelineDescriptor.programState->getUniformLocation("u_texture");
CC_SAFE_RELEASE(program);
auto vertexLayout = _programState->getVertexLayout();
const auto& attributeInfo = _programState->getProgram()->getActiveAttributes();
auto iter = attributeInfo.find("a_position");

View File

@ -118,7 +118,8 @@ bool MotionStreak3D::initWithFade(float fade, float minSeg, float stroke, const
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
// shader state
_programState = new backend::ProgramState(positionTextureColor_vert, positionTextureColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR);
_programState = new backend::ProgramState(program);
_customCommand.getPipelineDescriptor().programState = _programState;

View File

@ -65,8 +65,8 @@ bool Skybox::init()
_customCommand.setAfterCallback(CC_CALLBACK_0(Skybox::onAfterDraw, this));
// create and set our custom shader
_programState = new backend::ProgramState(CC3D_skybox_vert, CC3D_skybox_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKYBOX_3D);
_programState = new backend::ProgramState(program);
auto &pipelineDescriptor = _customCommand.getPipelineDescriptor();
auto layout = _programState->getVertexLayout();

View File

@ -63,80 +63,68 @@ backend::ProgramState* Sprite3DMaterial::_vertexLitMaterialSkinProgState = nullp
backend::ProgramState* Sprite3DMaterial::_diffuseMaterialSkinProgState = nullptr;
backend::ProgramState* Sprite3DMaterial::_bumpedDiffuseMaterialSkinProgState = nullptr;
namespace
{
std::string getShaderMacrosForLight()
{
char def[256];
auto conf = Configuration::getInstance();
snprintf(def, sizeof(def)-1, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n",
conf->getMaxSupportDirLightInShader(),
conf->getMaxSupportPointLightInShader(),
conf->getMaxSupportSpotLightInShader());
return std::string(def);
}
}
void Sprite3DMaterial::createBuiltInMaterial()
{
std::string def = getShaderMacrosForLight();
std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
_unLitMaterialSkinProgState = new (std::nothrow) backend::ProgramState(CC3D_skinPositionTexture_vert, CC3D_colorTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_TEXTURE_3D);
_unLitMaterialSkinProgState = new (std::nothrow) backend::ProgramState(program);
_unLitMaterialSkin = new (std::nothrow) Sprite3DMaterial();
if (_unLitMaterialSkin && _unLitMaterialSkin->initWithProgramState(_unLitMaterialSkinProgState))
{
_unLitMaterialSkin->_type = Sprite3DMaterial::MaterialType::UNLIT;
}
_diffuseMaterialSkinProgState = new (std::nothrow) backend::ProgramState(def + CC3D_skinPositionNormalTexture_vert, def + CC3D_colorNormalTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D);
_diffuseMaterialSkinProgState = new (std::nothrow) backend::ProgramState(program);
_diffuseMaterialSkin = new (std::nothrow) Sprite3DMaterial();
if (_diffuseMaterialSkin && _diffuseMaterialSkin->initWithProgramState(_diffuseMaterialSkinProgState))
{
_diffuseMaterialSkin->_type = Sprite3DMaterial::MaterialType::DIFFUSE;
}
_diffuseMaterialProgState = new (std::nothrow) backend::ProgramState(def + CC3D_positionNormalTexture_vert, def + CC3D_colorNormalTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_NORMAL_TEXTURE_3D);
_diffuseMaterialProgState = new (std::nothrow) backend::ProgramState(program);
_diffuseMaterial = new (std::nothrow) Sprite3DMaterial();
if (_diffuseMaterial && _diffuseMaterial->initWithProgramState(_diffuseMaterialProgState))
{
_diffuseMaterial->_type = Sprite3DMaterial::MaterialType::DIFFUSE;
}
_unLitMaterialProgState = new (std::nothrow) backend::ProgramState(CC3D_positionTexture_vert, CC3D_colorTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_3D);
_unLitMaterialProgState = new (std::nothrow) backend::ProgramState(program);
_unLitMaterial = new (std::nothrow) Sprite3DMaterial();
if (_unLitMaterial && _unLitMaterial->initWithProgramState(_unLitMaterialProgState))
{
_unLitMaterial->_type = Sprite3DMaterial::MaterialType::UNLIT;
}
_unLitNoTexMaterialProgState = new (std::nothrow) backend::ProgramState(CC3D_positionTexture_vert, CC3D_color_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_3D);
_unLitNoTexMaterialProgState = new (std::nothrow) backend::ProgramState(program);
_unLitNoTexMaterial = new (std::nothrow) Sprite3DMaterial();
if (_unLitNoTexMaterial && _unLitNoTexMaterial->initWithProgramState(_unLitNoTexMaterialProgState))
{
_unLitNoTexMaterial->_type = Sprite3DMaterial::MaterialType::UNLIT_NOTEX;
}
_diffuseNoTexMaterialProgState = new (std::nothrow) backend::ProgramState(def + CC3D_positionNormalTexture_vert, def + CC3D_colorNormal_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_NORMAL_3D);
_diffuseNoTexMaterialProgState = new (std::nothrow) backend::ProgramState(program);
_diffuseNoTexMaterial = new (std::nothrow) Sprite3DMaterial();
if (_diffuseNoTexMaterial && _diffuseNoTexMaterial->initWithProgramState(_diffuseNoTexMaterialProgState))
{
_diffuseNoTexMaterial->_type = Sprite3DMaterial::MaterialType::DIFFUSE_NOTEX;
}
_bumpedDiffuseMaterialProgState = new (std::nothrow) backend::ProgramState(def + normalMapDef + CC3D_positionNormalTexture_vert, def + normalMapDef + CC3D_colorNormalTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D);
_bumpedDiffuseMaterialProgState = new (std::nothrow) backend::ProgramState(program);
_bumpedDiffuseMaterial = new (std::nothrow) Sprite3DMaterial();
if (_bumpedDiffuseMaterial && _bumpedDiffuseMaterial->initWithProgramState(_bumpedDiffuseMaterialProgState))
{
_bumpedDiffuseMaterial->_type = Sprite3DMaterial::MaterialType::BUMPED_DIFFUSE;
}
_bumpedDiffuseMaterialSkinProgState = new (std::nothrow) backend::ProgramState(def + normalMapDef + CC3D_skinPositionNormalTexture_vert, def + normalMapDef + CC3D_colorNormalTexture_frag);
program = backend::Program::getBuiltinProgram(backend::ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D);
_bumpedDiffuseMaterialSkinProgState = new (std::nothrow) backend::ProgramState(program);
_bumpedDiffuseMaterialSkin = new (std::nothrow) Sprite3DMaterial();
if (_bumpedDiffuseMaterialSkin && _bumpedDiffuseMaterialSkin->initWithProgramState(_bumpedDiffuseMaterialSkinProgState))
{

View File

@ -115,7 +115,8 @@ void cocos2d::Terrain::setLightDir(const Vec3& lightDir)
bool Terrain::initProperties()
{
_programState = new backend::ProgramState(CC3D_terrain_vert, CC3D_terrain_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::TERRAIN_3D);
_programState = new backend::ProgramState(program);
_stateBlock.depthWrite = true;
_stateBlock.depthTest = true;

View File

@ -36,7 +36,8 @@ int StencilStateManager::s_layer = -1;
StencilStateManager::StencilStateManager()
{
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) backend::ProgramState(positionUColor_vert, positionUColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_UCOLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
auto vertexLayout = _programState->getVertexLayout();

View File

@ -415,7 +415,8 @@ bool BoneNode::init()
updateColor();
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) cocos2d::backend::ProgramState(cocos2d::position_vert, cocos2d::positionColor_frag);
auto* program = cocos2d::backend::Program::getBuiltinProgram(cocos2d::backend::ProgramType::POSITION);
_programState = new (std::nothrow) cocos2d::backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpLocation = _programState->getUniformLocation("u_MVPMatrix");

View File

@ -53,7 +53,8 @@ bool SkeletonNode::init()
// init _customCommand
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
_programState = new (std::nothrow) cocos2d::backend::ProgramState(cocos2d::position_vert, cocos2d::positionColor_frag);
auto* program = cocos2d::backend::Program::getBuiltinProgram(cocos2d::backend::ProgramType::POSITION);
_programState = new (std::nothrow) cocos2d::backend::ProgramState(program);
pipelineDescriptor.programState = _programState;
_mvpLocation = _programState->getUniformLocation("u_MVPMatrix");

View File

@ -26,18 +26,20 @@
#if CC_USE_NAVMESH
#include "renderer/backend/ProgramState.h"
#include "renderer/backend/Device.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCRenderState.h"
#include "renderer/ccShaders.h"
#include "base/CCDirector.h"
#include "base/ccMacros.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
NS_CC_BEGIN
NavMeshDebugDraw::NavMeshDebugDraw()
{
_programState = new backend::ProgramState(positionColor_vert, positionColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
_programState = new backend::ProgramState(program);
_locMVP = _programState->getUniformLocation("u_MVPMatrix");
auto vertexLayout = _programState->getVertexLayout();

View File

@ -127,7 +127,8 @@ Physics3DDebugDrawer::~Physics3DDebugDrawer()
void Physics3DDebugDrawer::init()
{
CC_SAFE_RELEASE_NULL(_programState);
_programState = new backend::ProgramState(positionColor_vert, positionColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_COLOR);
_programState = new backend::ProgramState(program);
_locMVP = _programState->getUniformLocation("u_MVPMatrix");
auto attributes = _programState->getProgram()->getActiveAttributes();

View File

@ -32,6 +32,7 @@
#include "renderer/CCPass.h"
#include "renderer/CCTextureCache.h"
#include "renderer/CCTexture2D.h"
#include "renderer/backend/Device.h"
#include "base/CCProperties.h"
#include "base/CCDirector.h"
#include "platform/CCFileUtils.h"
@ -370,10 +371,10 @@ bool Material::parseShader(Pass* pass, Properties* shaderProperties)
vertShaderSrc = defs + "\n" + vertShaderSrc;
fragShaderSrc = defs + "\n" + fragShaderSrc;
auto programState = new backend::ProgramState(vertShaderSrc, fragShaderSrc);
auto* program = backend::Device::getInstance()->newProgram(vertShaderSrc, fragShaderSrc);
auto programState = new backend::ProgramState(program);
pass->setProgramState(programState);
// Parse uniforms only if the GLProgramState was created
auto property = shaderProperties->getNextProperty();
while (property)
@ -396,6 +397,8 @@ bool Material::parseShader(Pass* pass, Properties* shaderProperties)
}
space = shaderProperties->getNextNamespace();
}
CC_SAFE_RELEASE(program);
CC_SAFE_RELEASE(programState);
}
return true;

View File

@ -826,8 +826,8 @@ void Texture2D::initProgram()
auto& pipelineDescriptor = _customCommand.getPipelineDescriptor();
//create program state
_programState = new (std::nothrow) cocos2d::backend::ProgramState(
positionTexture_vert, positionTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE);
_programState = new (std::nothrow) cocos2d::backend::ProgramState(program);
_mvpMatrixLocation = _programState->getUniformLocation("u_MVPMatrix");
_textureLocation = _programState->getUniformLocation("u_texture");

View File

@ -113,11 +113,20 @@ public:
*/
virtual void setFrameBufferOnly(bool frameBufferOnly) = 0;
/**
* Create an auto released Program.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program instance.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) = 0;
/**
* Get a DeviceInfo object.
* @return A DeviceInfo object.
*/
inline DeviceInfo* getDeviceInfo() const { return _deviceInfo; }
protected:
/**
* New a shaderModule, not auto released.
@ -126,17 +135,9 @@ protected:
* @return A ShaderModule object.
*/
virtual ShaderModule* newShaderModule(ShaderStage stage, const std::string& source) = 0;
/**
* New a Program, not auto released.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program object.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) = 0;
DeviceInfo* _deviceInfo = nullptr; ///< Device information.
private:
static Device* _instance;
};

View File

@ -23,6 +23,7 @@
****************************************************************************/
#include "Program.h"
#include "ProgramCache.h"
CC_BACKEND_BEGIN
@ -37,4 +38,9 @@ void Program::setProgramType(ProgramType type)
_programType = type;
}
Program* Program::getBuiltinProgram(ProgramType type)
{
return ProgramCache::getInstance()->getBuiltinProgram(type);
}
CC_BACKEND_END

View File

@ -50,6 +50,12 @@ class ShaderModule;
class Program : public Ref
{
public:
/**
* Get engine built-in program.
* @param type Specifies the built-in program type.
*/
static Program* getBuiltinProgram(ProgramType type);
/**
* Get uniform location by name.
* @param uniform Specifies the uniform name.

View File

@ -27,37 +27,43 @@
#include "ShaderModule.h"
#include "renderer/ccShaders.h"
#include "base/ccMacros.h"
#include "base/CCConfiguration.h"
namespace {
struct Shader
namespace std
{
template <>
struct hash<cocos2d::backend::ProgramType>
{
std::string vert;
std::string frag;
Shader(const std::string& _vert, const std::string& _frag)
:vert(_vert),
frag(_frag)
{}
};
}
namespace std {
template<> struct hash<Shader>
{
typedef Shader argument_type;
typedef cocos2d::backend::ProgramType argument_type;
typedef std::size_t result_type;
result_type operator()(argument_type const& s) const noexcept
result_type operator()(argument_type const& v) const
{
result_type const h1(std::hash < std::string>{}(s.vert));
result_type const h2(std::hash<std::string>{}(s.frag));
return h1 ^ (h2 << 1);
return hash<int>()(static_cast<int>(v));
}
};
}
CC_BACKEND_BEGIN
std::unordered_map<std::size_t, backend::Program*> ProgramCache::_cachedPrograms;
namespace
{
std::string getShaderMacrosForLight()
{
char def[256];
auto conf = Configuration::getInstance();
snprintf(def, sizeof(def) - 1, "\n#define MAX_DIRECTIONAL_LIGHT_NUM %d \n"
"\n#define MAX_POINT_LIGHT_NUM %d \n"
"\n#define MAX_SPOT_LIGHT_NUM %d \n",
conf->getMaxSupportDirLightInShader(),
conf->getMaxSupportPointLightInShader(),
conf->getMaxSupportSpotLightInShader());
return std::string(def);
}
}
std::unordered_map<backend::ProgramType, backend::Program*> ProgramCache::_cachedPrograms;
ProgramCache* ProgramCache::_sharedProgramCache = nullptr;
ProgramCache* ProgramCache::getInstance()
@ -99,6 +105,7 @@ bool ProgramCache::init()
addProgram(ProgramType::POSITION_COLOR_LENGTH_TEXTURE);
addProgram(ProgramType::POSITION_COLOR_TEXTURE_AS_POINTSIZE);
addProgram(ProgramType::POSITION_COLOR);
addProgram(ProgramType::POSITION);
addProgram(ProgramType::LAYER_RADIA_GRADIENT);
addProgram(ProgramType::POSITION_TEXTURE);
addProgram(ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
@ -106,6 +113,19 @@ bool ProgramCache::init()
addProgram(ProgramType::ETC1_GRAY);
addProgram(ProgramType::GRAY_SCALE);
addProgram(ProgramType::LINE_COLOR_3D);
addProgram(ProgramType::CAMERA_CLEAR);
addProgram(ProgramType::SKYBOX_3D);
addProgram(ProgramType::SKINPOSITION_TEXTURE_3D);
addProgram(ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D);
addProgram(ProgramType::POSITION_NORMAL_TEXTURE_3D);
addProgram(ProgramType::POSITION_TEXTURE_3D);
addProgram(ProgramType::POSITION_3D);
addProgram(ProgramType::POSITION_NORMAL_3D);
addProgram(ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D);
addProgram(ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D);
addProgram(ProgramType::TERRAIN_3D);
addProgram(ProgramType::PARTICLE_TEXTURE_3D);
addProgram(ProgramType::PARTICLE_COLOR_3D);
return true;
}
@ -140,6 +160,9 @@ void ProgramCache::addProgram(ProgramType type)
case ProgramType::POSITION_COLOR:
program = backend::Device::getInstance()->newProgram(positionColor_vert, positionColor_frag);
break;
case ProgramType::POSITION:
program = backend::Device::getInstance()->newProgram(position_vert, positionColor_frag);
break;
case ProgramType::LAYER_RADIA_GRADIENT:
program = backend::Device::getInstance()->newProgram(position_vert, layer_radialGradient_frag);
break;
@ -161,6 +184,62 @@ void ProgramCache::addProgram(ProgramType type)
case ProgramType::LINE_COLOR_3D:
program = backend::Device::getInstance()->newProgram(lineColor3D_vert, lineColor3D_frag);
break;
case ProgramType::CAMERA_CLEAR:
program = backend::Device::getInstance()->newProgram(cameraClear_vert, cameraClear_frag);
break;
case ProgramType::SKYBOX_3D:
program = backend::Device::getInstance()->newProgram(CC3D_skybox_vert, CC3D_skybox_frag);
break;
case ProgramType::SKINPOSITION_TEXTURE_3D:
program = backend::Device::getInstance()->newProgram(CC3D_skinPositionTexture_vert, CC3D_colorTexture_frag);
break;
case ProgramType::SKINPOSITION_NORMAL_TEXTURE_3D:
{
std::string def = getShaderMacrosForLight();
program = backend::Device::getInstance()->newProgram(def + CC3D_skinPositionNormalTexture_vert, def + CC3D_colorNormalTexture_frag);
}
break;
case ProgramType::POSITION_NORMAL_TEXTURE_3D:
{
std::string def = getShaderMacrosForLight();
program = backend::Device::getInstance()->newProgram(def + CC3D_positionNormalTexture_vert, def + CC3D_colorNormalTexture_frag);
}
break;
case ProgramType::POSITION_TEXTURE_3D:
program = backend::Device::getInstance()->newProgram(CC3D_positionTexture_vert, CC3D_colorTexture_frag);
break;
case ProgramType::POSITION_3D:
program = backend::Device::getInstance()->newProgram(CC3D_positionTexture_vert, CC3D_color_frag);
break;
case ProgramType::POSITION_NORMAL_3D:
{
std::string def = getShaderMacrosForLight();
program = backend::Device::getInstance()->newProgram(def + CC3D_positionNormalTexture_vert, def + CC3D_colorNormal_frag);
}
break;
case ProgramType::POSITION_BUMPEDNORMAL_TEXTURE_3D:
{
std::string def = getShaderMacrosForLight();
std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
program = backend::Device::getInstance()->newProgram(def + normalMapDef + CC3D_positionNormalTexture_vert, def + normalMapDef + CC3D_colorNormalTexture_frag);
}
break;
case ProgramType::SKINPOSITION_BUMPEDNORMAL_TEXTURE_3D:
{
std::string def = getShaderMacrosForLight();
std::string normalMapDef = "\n#define USE_NORMAL_MAPPING 1 \n";
program = backend::Device::getInstance()->newProgram(def + normalMapDef + CC3D_skinPositionNormalTexture_vert, def + normalMapDef + CC3D_colorNormalTexture_frag);
}
break;
case ProgramType::TERRAIN_3D:
program = backend::Device::getInstance()->newProgram(CC3D_terrain_vert, CC3D_terrain_frag);
break;
case ProgramType::PARTICLE_TEXTURE_3D:
program = backend::Device::getInstance()->newProgram(CC3D_particle_vert, CC3D_particleTexture_frag);
break;
case ProgramType::PARTICLE_COLOR_3D:
program = backend::Device::getInstance()->newProgram(CC3D_particle_vert, CC3D_particleColor_frag);
break;
default:
CCASSERT(false, "Not built-in program type.");
break;
@ -169,35 +248,14 @@ void ProgramCache::addProgram(ProgramType type)
ProgramCache::_cachedPrograms.emplace(type, program);
}
void ProgramCache::addProgram(const std::string& vertexShader, const std::string& fragmentShader)
{
auto key = std::hash<Shader>{}(Shader(vertexShader, fragmentShader));
auto program = backend::Device::getInstance()->newProgram(vertexShader, fragmentShader);
ProgramCache::_cachedPrograms.emplace(key, program);
}
backend::Program* ProgramCache::newBuiltinProgram(ProgramType type)
backend::Program* ProgramCache::getBuiltinProgram(ProgramType type) const
{
const auto& iter = ProgramCache::_cachedPrograms.find(type);
if (ProgramCache::_cachedPrograms.end() != iter)
return iter->second;
return nullptr;
}
backend::Program* ProgramCache::newProgram(const std::string& vertexShader, const std::string& fragmentShader)
{
auto key = std::hash<Shader>{}(Shader(vertexShader, fragmentShader));
const auto& iter = ProgramCache::_cachedPrograms.find(key);
if (ProgramCache::_cachedPrograms.end() != iter)
{
CC_SAFE_RETAIN(iter->second);
return iter->second;
}
auto program = backend::Device::getInstance()->newProgram(vertexShader, fragmentShader);
ProgramCache::_cachedPrograms.emplace(key, program);
return program;
return nullptr;
}
void ProgramCache::removeProgram(backend::Program* program)

View File

@ -49,15 +49,9 @@ public:
/** purges the cache. It releases the retained instance. */
static void destroyInstance();
/**
* @param vertexShader Specifes the vertex shader source.
* @param fragmentShader Specifes the fragment shader source.
*/
backend::Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader);
/// get built-in program
backend::Program* newBuiltinProgram(ProgramType type);
backend::Program* getBuiltinProgram(ProgramType type) const;
/**
* Remove a program object from cache.
@ -84,16 +78,10 @@ protected:
*/
bool init();
/**
* @param vertexShader Specifes the vertex shader source.
* @param fragmentShader Specifes the fragment shader source.
*/
void addProgram(const std::string& vertexShader, const std::string& fragmentShader);
/// Add built-in program
void addProgram(ProgramType type);
static std::unordered_map<std::size_t, backend::Program*> _cachedPrograms; ///< The cached program object.
static std::unordered_map<backend::ProgramType, backend::Program*> _cachedPrograms; ///< The cached program object.
static ProgramCache *_sharedProgramCache; ///< A shared instance of the program cache.
};

View File

@ -155,25 +155,15 @@ TextureInfo& TextureInfo::operator=(const TextureInfo& rhs)
return *this;
}
ProgramState::ProgramState(ProgramType type)
ProgramState::ProgramState(Program* program)
{
_program = backend::ProgramCache::getInstance()->newBuiltinProgram(type);
CCASSERT(_program, "Not built-in program type, please use ProgramState(const std::string& vertexShader, const std::string& fragmentShader) instead.");
CC_SAFE_RETAIN(_program);
init();
init(program);
}
ProgramState::ProgramState(const std::string& vertexShader, const std::string& fragmentShader)
{
_program = backend::ProgramCache::getInstance()->newProgram(vertexShader, fragmentShader);
CC_SAFE_RETAIN(_program);
init();
}
void ProgramState::init()
bool ProgramState::init(Program* program)
{
CC_SAFE_RETAIN(program);
_program = program;
_vertexUniformBufferSize = _program->getUniformBufferSize(ShaderStage::VERTEX);
_vertexUniformBuffer = new char[_vertexUniformBufferSize];
memset(_vertexUniformBuffer, 0, _vertexUniformBufferSize);
@ -189,6 +179,7 @@ void ProgramState::init()
});
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundListener, -1);
#endif
return true;
}
void ProgramState::resetUniforms()

View File

@ -78,19 +78,10 @@ public:
using UniformCallback = std::function<void(ProgramState*, const UniformLocation &)>;
/**
* @param vertexShader Specifies the vertex shader.
* @param fragmentShader Specifies the fragment shader.
* @see `ProgramState(ProgramType type)`
* @param program Specifies the program.
*/
ProgramState(const std::string& vertexShader, const std::string& fragmentShader);
ProgramState(Program* program);
/**
* Create an program state object more efficient by engine built-in program type.
* @param type Specifies the built-in program type.
* @see `ProgramState(const std::string& vertexShader, const std::string& fragmentShader)`
*/
ProgramState(ProgramType type);
///destructor
virtual ~ProgramState();
@ -306,7 +297,7 @@ protected:
void resetUniforms();
///Initialize.
void init();
bool init(Program* program);
#ifdef CC_USE_METAL
/**

View File

@ -326,27 +326,42 @@ enum class TextureCubeFace : uint32_t
NEGATIVE_Z = 5
};
enum ProgramType
enum class ProgramType : int
{
INVALID_PROGRAM = -1,
POSITION_COLOR_LENGTH_TEXTURE,
POSITION_COLOR_TEXTURE_AS_POINTSIZE,
POSITION_COLOR,
POSITION_UCOLOR,
POSITION_TEXTURE,
POSITION_TEXTURE_COLOR,
POSITION_TEXTURE_COLOR_ALPHA_TEST,
LABEL_NORMAL,
LABLE_OUTLINE,
LABLE_DISTANCEFIELD_GLOW,
LABEL_DISTANCE_NORMAL,
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,
LAYER_RADIA_GRADIENT, //position_vert, layer_radialGradient_frag
ETC1,
ETC1_GRAY,
GRAY_SCALE,
LINE_COLOR_3D,
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
};
///built-in uniform name

View File

@ -119,6 +119,14 @@ public:
*/
virtual void setFrameBufferOnly(bool frameBufferOnly) override;
/**
* New a Program, not auto release.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program instance.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) override;
/**
* Get a MTLDevice object.
* @return A MTLDevice object.
@ -140,14 +148,6 @@ protected:
*/
virtual ShaderModule* newShaderModule(ShaderStage stage, const std::string& source) override;
/**
* New a Program.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program object.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) override;
private:
static CAMetalLayer* _metalLayer;
static id<CAMetalDrawable> _currentDrawable;

View File

@ -80,6 +80,14 @@ public:
*/
virtual void setFrameBufferOnly(bool frameBufferOnly) override {}
/**
* New a Program, not auto released.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program instance.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) override;
protected:
/**
* New a shaderModule, not auto released.
@ -89,14 +97,6 @@ protected:
*/
virtual ShaderModule* newShaderModule(ShaderStage stage, const std::string& source) override;
/**
* New a Program, not auto released.
* @param vertexShader Specifes this is a vertex shader source.
* @param fragmentShader Specifes this is a fragment shader source.
* @return A Program object.
*/
virtual Program* newProgram(const std::string& vertexShader, const std::string& fragmentShader) override;
};
//end of _opengl group
/// @}

View File

@ -194,13 +194,15 @@ bool Particle3DQuadRender::initQuadRender( const std::string& texFile )
if (tex)
{
_texture = tex;
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
_programState = new backend::ProgramState(program);
}
}
if (!_programState)
{
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
_programState = new backend::ProgramState(program);
}
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();

View File

@ -655,13 +655,15 @@ void PUBillboardChain::init( const std::string &texFile )
if (tex)
{
_texture = tex;
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
_programState = new backend::ProgramState(program);
}
}
if(!_programState)
{
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
_programState = new backend::ProgramState(program);
}
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();

View File

@ -592,13 +592,15 @@ bool PUParticle3DEntityRender::initRender( const std::string &texFile )
if (tex)
{
_texture = tex;
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleTexture_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_TEXTURE_3D);
_programState = new backend::ProgramState(program);
}
}
if (!_programState)
{
_programState = new backend::ProgramState(CC3D_particle_vert, CC3D_particleColor_frag);
auto* program = backend::Program::getBuiltinProgram(backend::ProgramType::PARTICLE_COLOR_3D);
_programState = new backend::ProgramState(program);
}
auto &pipelineDescriptor = _meshCommand.getPipelineDescriptor();

View File

@ -28,6 +28,7 @@ THE SOFTWARE.
#include "testResource.h"
#include "ui/UISlider.h"
#include "platform/CCFileUtils.h"
#include "renderer/backend/Device.h"
USING_NS_CC;
@ -1270,9 +1271,10 @@ void FogTestDemo::onEnter()
auto vertexSource = FileUtils::getInstance()->getStringFromFile("Sprite3DTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("Sprite3DTest/fog.frag");
_programState1 = new backend::ProgramState(vertexSource, fragSource);
_programState2 = new backend::ProgramState(vertexSource, fragSource);
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
CC_SAFE_RELEASE(program);
_sprite3D1 = Sprite3D::create("Sprite3DTest/teapot.c3b");
_sprite3D2 = Sprite3D::create("Sprite3DTest/teapot.c3b");
@ -1322,12 +1324,13 @@ void FogTestDemo::onEnter()
auto vertexSource = FileUtils::getInstance()->getStringFromFile("Sprite3DTest/fog.vert");
auto fragSource = FileUtils::getInstance()->getStringFromFile("Sprite3DTest/fog.frag");
_programState1 = new backend::ProgramState(vertexSource, fragSource);
_programState2 = new backend::ProgramState(vertexSource, fragSource);
auto program = backend::Device::getInstance()->newProgram(vertexSource, fragSource);
_programState1 = new backend::ProgramState(program);
_programState2 = new backend::ProgramState(program);
_sprite3D1->setProgramState(_programState1);
_sprite3D2->setProgramState(_programState2);
CC_SAFE_RELEASE(program);
auto fogColor = Vec4(0.5, 0.5, 0.5, 1.0);
float fogStart = 10;

View File

@ -577,7 +577,8 @@ void RawStencilBufferTest::initCommands()
};
_disableStencilCallback.init(_globalZOrder);
_programState = new (std::nothrow) backend::ProgramState(positionUColor_vert, positionUColor_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_UCOLOR);
_programState = new (std::nothrow) backend::ProgramState(program);
_locColor = _programState->getProgram()->getUniformLocation("u_color");
_locMVPMatrix = _programState->getProgram()->getUniformLocation("u_MVPMatrix");
const auto& projectionMat = Director::getInstance()->getMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
@ -753,7 +754,8 @@ void RawStencilBufferTestAlphaTest::setup()
RawStencilBufferTest::setup();
for(int i = 0; i < _planeCount; ++i)
{
auto programState = new backend::ProgramState(positionTextureColor_vert, positionTextureColorAlphaTest_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_COLOR_ALPHA_TEST);
auto programState = new backend::ProgramState(program);
programState->setUniform(programState->getUniformLocation("u_alpha_value"), &_alphaThreshold, sizeof(_alphaThreshold));
_spritesStencil.at(i)->setProgramState(programState);
}

View File

@ -26,6 +26,8 @@
#include "NewRendererTest.h"
#include <chrono>
#include <sstream>
#include "renderer/backend/Device.h"
USING_NS_CC;
class DurationRecorder {
@ -861,9 +863,10 @@ cocos2d::backend::ProgramState* RendererUniformBatch::createBlurProgramState()
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(shaderName);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto programState = new backend::ProgramState(positionTextureColor_vert, fragSource.c_str());
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto programState = new backend::ProgramState(program);
programState->autorelease();
CC_SAFE_RELEASE(program);
backend::UniformLocation loc = programState->getUniformLocation("resolution");
auto resolution = Vec2(85, 121);
@ -888,9 +891,11 @@ cocos2d::backend::ProgramState* RendererUniformBatch::createSepiaProgramState()
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(shaderName);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto glprogram = new backend::ProgramState(positionTextureColor_vert, fragSource.c_str());
return glprogram;
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto programState = new backend::ProgramState(program);
programState->autorelease();
CC_SAFE_RELEASE(program);
return programState;
}
std::string RendererUniformBatch::title() const
@ -944,8 +949,10 @@ backend::ProgramState* RendererUniformBatch2::createBlurProgramState()
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(shaderName);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto programState = new backend::ProgramState(positionTextureColor_vert, fragSource.c_str());
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto programState = new backend::ProgramState(program);
programState->autorelease();
CC_SAFE_RELEASE(program);
backend::UniformLocation loc = programState->getUniformLocation("resolution");
auto resolution = Vec2(85, 121);
@ -970,9 +977,11 @@ backend::ProgramState* RendererUniformBatch2::createSepiaProgramState()
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(shaderName);
auto fragSource = fileUtiles->getStringFromFile(fragmentFullPath);
auto glprogram = new backend::ProgramState(positionTextureColor_vert, fragSource.c_str());
return glprogram;
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto programState = new backend::ProgramState(program);
programState->autorelease();
CC_SAFE_RELEASE(program);
return programState;
}
std::string RendererUniformBatch2::title() const

View File

@ -26,6 +26,7 @@
#include "../testResource.h"
#include "cocos2d.h"
#include "renderer/ccShaders.h"
#include "renderer/backend/Device.h"
USING_NS_CC;
USING_NS_CC_EXT;
@ -144,10 +145,11 @@ void ShaderNode::loadShaderVertex(const std::string &vert, const std::string &fr
std::string vertexFilePath = fileUtiles->fullPathForFilename(vert);
vertSource = fileUtiles->getStringFromFile(vertexFilePath);
}
auto programState = new backend::ProgramState(vertSource.c_str(), fragSource.c_str());
auto program = backend::Device::getInstance()->newProgram(vertSource.c_str(), fragSource.c_str());
auto programState = new backend::ProgramState(program);
setProgramState(programState);
CC_SAFE_RELEASE(programState);
CC_SAFE_RELEASE(program);
}
void ShaderNode::update(float dt)
@ -472,9 +474,11 @@ void SpriteBlur::initProgram()
std::string fragSource = FileUtils::getInstance()->getStringFromFile(
FileUtils::getInstance()->fullPathForFilename("Shaders/example_Blur.fsh"));
auto programState = new backend::ProgramState(positionTextureColor_vert, fragSource.data());
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.data());
auto programState = new backend::ProgramState(program);
setProgramState(programState);
CC_SAFE_RELEASE_NULL(programState);
CC_SAFE_RELEASE(programState);
CC_SAFE_RELEASE(program);
auto size = getTexture()->getContentSizeInPixels();
@ -603,7 +607,8 @@ bool ShaderRetroEffect::init()
auto fragStr = FileUtils::getInstance()->getStringFromFile(FileUtils::getInstance()->fullPathForFilename("Shaders/example_HorizontalColor.fsh"));
char * fragSource = (char*)fragStr.c_str();
auto p = new backend::ProgramState(positionTextureColor_vert, fragSource);
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource);
auto p = new backend::ProgramState(program);
auto director = Director::getInstance();
const auto& screenSizeLocation = p->getUniformLocation("u_screenSize");
const auto& frameSize = director->getOpenGLView()->getFrameSize();
@ -623,6 +628,7 @@ bool ShaderRetroEffect::init()
addChild(_label);
scheduleUpdate();
CC_SAFE_RELEASE(program);
return true;
}
@ -793,8 +799,8 @@ bool ShaderMultiTexture::init()
auto * fu = FileUtils::getInstance();
auto vertexShader = fu->getStringFromFile("Shaders/example_MultiTexture.vsh");
auto fragmentShader = fu->getStringFromFile("Shaders/example_MultiTexture.fsh");
auto programState = new backend::ProgramState(vertexShader.c_str(), fragmentShader.c_str() );
auto program = backend::Device::getInstance()->newProgram(vertexShader.c_str(), fragmentShader.c_str());
auto programState = new backend::ProgramState(program);
_sprite->setProgramState(programState);
SET_TEXTURE(programState, "u_texture1", 1, right->getTexture()->getBackendTexture());
@ -811,6 +817,7 @@ bool ShaderMultiTexture::init()
menu->setPosition(s.width * 7 / 8, s.height / 2);
CC_SAFE_RELEASE(programState);
CC_SAFE_RELEASE(program);
return true;
}

View File

@ -27,6 +27,7 @@
#include "ShaderTest.h"
#include "../testResource.h"
#include "cocos2d.h"
#include "renderer/backend/Device.h"
#include <tuple>
USING_NS_CC;
@ -178,10 +179,10 @@ bool Effect::initProgramState(const std::string &fragmentFilename)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
_fragSource = fragSource;
#endif
auto programState = new backend::ProgramState(positionTextureColor_vert, fragSource.c_str());
auto program = backend::Device::getInstance()->newProgram(positionTextureColor_vert, fragSource.c_str());
auto programState = new backend::ProgramState(program);
CC_SAFE_RELEASE(_programState);
CC_SAFE_RELEASE(program);
_programState = programState;
return _programState != nullptr;

View File

@ -73,7 +73,8 @@ bool DrawNode3D::init()
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
auto &pd = _customCommand.getPipelineDescriptor();
_programStateLine = new backend::ProgramState(lineColor3D_vert, lineColor3D_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::LINE_COLOR_3D);
_programStateLine = new backend::ProgramState(program);
pd.programState = _programStateLine;
_locMVPMatrix = _programStateLine->getUniformLocation("u_MVPMatrix");

View File

@ -2221,14 +2221,14 @@ void Issue9767::menuCallback_SwitchShader(cocos2d::Ref* sender)
if (_shaderType == Issue9767::ShaderType::SHADER_TEX)
{
_shaderType = Issue9767::ShaderType::SHADER_COLOR;
//programState = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION);
_programState = new backend::ProgramState(CC3D_positionTexture_vert, CC3D_color_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_3D);
_programState = new backend::ProgramState(program);
}
else
{
_shaderType = Issue9767::ShaderType::SHADER_TEX;
//programState = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_3D_POSITION_TEXTURE);
_programState = new backend::ProgramState(CC3D_positionTexture_vert, CC3D_colorTexture_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::POSITION_TEXTURE_3D);
_programState = new backend::ProgramState(program);
}
_sprite->setProgramState(_programState);
}

View File

@ -151,8 +151,8 @@ bool DrawNode3D::init()
{
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
_programState = new backend::ProgramState(lineColor3D_vert, lineColor3D_frag);
auto program = backend::Program::getBuiltinProgram(backend::ProgramType::LINE_COLOR_3D);
_programState = new backend::ProgramState(program);
_locMVPMatrix = _programState->getUniformLocation("u_MVPMatrix");