2014-05-08 11:20:19 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright 2011 Jeff Lamarche
|
|
|
|
Copyright 2012 Goffredo Marocchi
|
|
|
|
Copyright 2012 Ricardo Quesada
|
|
|
|
Copyright 2012 cocos2d-x.org
|
|
|
|
Copyright 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgramState.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
#include "renderer/CCGLProgram.h"
|
2014-05-09 12:54:49 +08:00
|
|
|
#include "renderer/CCGLProgramStateCache.h"
|
2014-05-10 09:39:25 +08:00
|
|
|
#include "renderer/CCGLProgramCache.h"
|
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2014-05-28 15:16:00 +08:00
|
|
|
#include "base/CCEventCustom.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCDirector.h"
|
2014-08-26 18:19:28 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2015-06-03 07:56:59 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
// static vector with all the registered custom binding resolvers
|
|
|
|
std::vector<GLProgramState::AutoBindingResolver*> GLProgramState::_customAutoBindingResolvers;
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// UniformValue
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
UniformValue::UniformValue()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _uniform(nullptr)
|
2014-05-08 11:38:15 +08:00
|
|
|
, _glprogram(nullptr)
|
2015-05-06 04:07:32 +08:00
|
|
|
, _type(Type::VALUE)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:38:15 +08:00
|
|
|
UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
|
2014-10-30 22:25:46 +08:00
|
|
|
: _uniform(uniform)
|
2014-05-08 11:38:15 +08:00
|
|
|
, _glprogram(glprogram)
|
2015-05-06 04:07:32 +08:00
|
|
|
, _type(Type::VALUE)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
UniformValue::~UniformValue()
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
if (_type == Type::CALLBACK_FN)
|
2014-05-10 04:24:56 +08:00
|
|
|
delete _value.callback;
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
void UniformValue::apply()
|
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
if (_type == Type::CALLBACK_FN)
|
|
|
|
{
|
2014-06-11 14:05:35 +08:00
|
|
|
(*_value.callback)(_glprogram, _uniform);
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
else if (_type == Type::POINTER)
|
|
|
|
{
|
|
|
|
switch (_uniform->type) {
|
|
|
|
case GL_FLOAT:
|
|
|
|
_glprogram->setUniformLocationWith1fv(_uniform->location, _value.floatv.pointer, _value.floatv.size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_VEC2:
|
|
|
|
_glprogram->setUniformLocationWith2fv(_uniform->location, _value.v2f.pointer, _value.v2f.size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_VEC3:
|
|
|
|
_glprogram->setUniformLocationWith3fv(_uniform->location, _value.v3f.pointer, _value.v3f.size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_VEC4:
|
|
|
|
_glprogram->setUniformLocationWith4fv(_uniform->location, _value.v4f.pointer, _value.v4f.size);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "Unsupported type");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* _type == VALUE */
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
switch (_uniform->type) {
|
2014-05-13 14:51:37 +08:00
|
|
|
case GL_SAMPLER_2D:
|
|
|
|
_glprogram->setUniformLocationWith1i(_uniform->location, _value.tex.textureUnit);
|
|
|
|
GL::bindTexture2DN(_value.tex.textureUnit, _value.tex.textureId);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
2015-01-29 09:28:14 +08:00
|
|
|
case GL_SAMPLER_CUBE:
|
|
|
|
_glprogram->setUniformLocationWith1i(_uniform->location, _value.tex.textureUnit);
|
2015-01-30 17:23:30 +08:00
|
|
|
GL::bindTextureN(_value.tex.textureUnit, _value.tex.textureId, GL_TEXTURE_CUBE_MAP);
|
2015-01-29 09:28:14 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
case GL_INT:
|
2014-05-08 11:38:15 +08:00
|
|
|
_glprogram->setUniformLocationWith1i(_uniform->location, _value.intValue);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
case GL_FLOAT:
|
|
|
|
_glprogram->setUniformLocationWith1f(_uniform->location, _value.floatValue);
|
|
|
|
break;
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
case GL_FLOAT_VEC2:
|
2014-05-10 04:24:56 +08:00
|
|
|
_glprogram->setUniformLocationWith2f(_uniform->location, _value.v2Value[0], _value.v2Value[1]);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_VEC3:
|
2014-05-10 04:24:56 +08:00
|
|
|
_glprogram->setUniformLocationWith3f(_uniform->location, _value.v3Value[0], _value.v3Value[1], _value.v3Value[2]);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_VEC4:
|
2014-05-10 04:24:56 +08:00
|
|
|
_glprogram->setUniformLocationWith4f(_uniform->location, _value.v4Value[0], _value.v4Value[1], _value.v4Value[2], _value.v4Value[3]);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case GL_FLOAT_MAT4:
|
2014-05-08 11:38:15 +08:00
|
|
|
_glprogram->setUniformLocationWithMatrix4fv(_uniform->location, (GLfloat*)&_value.matrixValue, 1);
|
2014-05-08 11:20:19 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CCASSERT(false, "Invalid UniformValue");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-11 14:05:35 +08:00
|
|
|
void UniformValue::setCallback(const std::function<void(GLProgram*, Uniform*)> &callback)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
// delete previously set callback
|
2014-08-30 03:54:24 +08:00
|
|
|
// TODO: memory will leak if the user does:
|
2014-05-10 04:24:56 +08:00
|
|
|
// value->setCallback();
|
|
|
|
// value->setFloat();
|
2015-05-06 04:07:32 +08:00
|
|
|
if (_type == Type::CALLBACK_FN)
|
2014-05-10 04:24:56 +08:00
|
|
|
delete _value.callback;
|
|
|
|
|
2015-12-16 14:02:55 +08:00
|
|
|
_value.callback = new (std::nothrow) std::function<void(GLProgram*, Uniform*)>();
|
2014-05-10 04:24:56 +08:00
|
|
|
*_value.callback = callback;
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::CALLBACK_FN;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
void UniformValue::setTexture(GLuint textureId, GLuint textureUnit)
|
|
|
|
{
|
2015-01-29 09:28:14 +08:00
|
|
|
//CCASSERT(_uniform->type == GL_SAMPLER_2D, "Wrong type. expecting GL_SAMPLER_2D");
|
2014-05-13 14:51:37 +08:00
|
|
|
_value.tex.textureId = textureId;
|
|
|
|
_value.tex.textureUnit = textureUnit;
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
2014-05-13 14:51:37 +08:00
|
|
|
}
|
2014-05-09 09:01:48 +08:00
|
|
|
void UniformValue::setInt(int value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-13 14:51:37 +08:00
|
|
|
CCASSERT(_uniform->type == GL_INT, "Wrong type: expecting GL_INT");
|
2014-05-08 11:20:19 +08:00
|
|
|
_value.intValue = value;
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UniformValue::setFloat(float value)
|
|
|
|
{
|
|
|
|
CCASSERT(_uniform->type == GL_FLOAT, "Wrong type: expecting GL_FLOAT");
|
|
|
|
_value.floatValue = value;
|
|
|
|
_type = Type::VALUE;
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void UniformValue::setFloatv(ssize_t size, const float* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT(_uniform->type == GL_FLOAT, "Wrong type: expecting GL_FLOAT");
|
|
|
|
_value.floatv.pointer = (const float*)pointer;
|
|
|
|
_value.floatv.size = (GLsizei)size;
|
|
|
|
_type = Type::POINTER;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void UniformValue::setVec2(const Vec2& value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_VEC2, "Wrong type: expecting GL_FLOAT_VEC2");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v2Value, &value, sizeof(_value.v2Value));
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void UniformValue::setVec2v(ssize_t size, const Vec2* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_VEC2, "Wrong type: expecting GL_FLOAT_VEC2");
|
|
|
|
_value.v2f.pointer = (const float*)pointer;
|
|
|
|
_value.v2f.size = (GLsizei)size;
|
|
|
|
_type = Type::POINTER;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void UniformValue::setVec3(const Vec3& value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_VEC3, "Wrong type: expecting GL_FLOAT_VEC3");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v3Value, &value, sizeof(_value.v3Value));
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void UniformValue::setVec3v(ssize_t size, const Vec3* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_VEC3, "Wrong type: expecting GL_FLOAT_VEC3");
|
|
|
|
_value.v3f.pointer = (const float*)pointer;
|
|
|
|
_value.v3f.size = (GLsizei)size;
|
|
|
|
_type = Type::POINTER;
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void UniformValue::setVec4(const Vec4& value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2015-05-06 04:07:32 +08:00
|
|
|
CCASSERT (_uniform->type == GL_FLOAT_VEC4, "Wrong type: expecting GL_FLOAT_VEC4");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v4Value, &value, sizeof(_value.v4Value));
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void UniformValue::setVec4v(ssize_t size, const Vec4* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
CCASSERT (_uniform->type == GL_FLOAT_VEC4, "Wrong type: expecting GL_FLOAT_VEC4");
|
|
|
|
_value.v4f.pointer = (const float*)pointer;
|
|
|
|
_value.v4f.size = (GLsizei)size;
|
|
|
|
_type = Type::POINTER;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void UniformValue::setMat4(const Mat4& value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2015-07-14 15:28:36 +08:00
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_MAT4, "_uniform's type should be equal GL_FLOAT_MAT4.");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.matrixValue, &value, sizeof(_value.matrixValue));
|
2015-05-06 04:07:32 +08:00
|
|
|
_type = Type::VALUE;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// VertexAttribValue
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
VertexAttribValue::VertexAttribValue()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _vertexAttrib(nullptr)
|
|
|
|
, _useCallback(false)
|
2014-05-13 10:12:56 +08:00
|
|
|
, _enabled(false)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
|
2014-10-30 22:25:46 +08:00
|
|
|
: _vertexAttrib(vertexAttrib)
|
|
|
|
, _useCallback(false)
|
2014-05-13 10:12:56 +08:00
|
|
|
, _enabled(false)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
VertexAttribValue::~VertexAttribValue()
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
if (_useCallback)
|
|
|
|
delete _value.callback;
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VertexAttribValue::apply()
|
2014-05-13 10:12:56 +08:00
|
|
|
{
|
|
|
|
if(_enabled) {
|
|
|
|
if(_useCallback) {
|
|
|
|
(*_value.callback)(_vertexAttrib);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glVertexAttribPointer(_vertexAttrib->index,
|
|
|
|
_value.pointer.size,
|
|
|
|
_value.pointer.type,
|
|
|
|
_value.pointer.normalized,
|
|
|
|
_value.pointer.stride,
|
|
|
|
_value.pointer.pointer);
|
|
|
|
}
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2015-12-16 14:02:55 +08:00
|
|
|
_value.callback = new (std::nothrow) std::function<void(VertexAttrib*)>();
|
2014-05-10 04:24:56 +08:00
|
|
|
*_value.callback = callback;
|
2014-05-08 11:20:19 +08:00
|
|
|
_useCallback = true;
|
2014-05-13 10:12:56 +08:00
|
|
|
_enabled = true;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
void VertexAttribValue::setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
_value.pointer.size = size;
|
|
|
|
_value.pointer.type = type;
|
|
|
|
_value.pointer.normalized = normalized;
|
|
|
|
_value.pointer.stride = stride;
|
|
|
|
_value.pointer.pointer = pointer;
|
2014-05-13 10:12:56 +08:00
|
|
|
_enabled = true;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// GLProgramState
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
GLProgramState* GLProgramState::create(GLProgram *glprogram)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-09 12:54:49 +08:00
|
|
|
GLProgramState* ret = nullptr;
|
2014-05-17 02:02:23 +08:00
|
|
|
ret = new (std::nothrow) GLProgramState();
|
|
|
|
if(ret && ret->init(glprogram))
|
|
|
|
{
|
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
2014-05-10 09:39:25 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
GLProgramState* GLProgramState::getOrCreateWithGLProgramName(const std::string& glProgramName )
|
2014-05-10 09:39:25 +08:00
|
|
|
{
|
|
|
|
GLProgram *glProgram = GLProgramCache::getInstance()->getGLProgram(glProgramName);
|
|
|
|
if( glProgram )
|
2014-05-14 09:12:58 +08:00
|
|
|
return getOrCreateWithGLProgram(glProgram);
|
2014-05-10 09:39:25 +08:00
|
|
|
|
|
|
|
CCLOG("cocos2d: warning: GLProgram '%s' not found", glProgramName.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-14 09:12:58 +08:00
|
|
|
GLProgramState* GLProgramState::getOrCreateWithGLProgram(GLProgram *glprogram)
|
2014-05-10 09:39:25 +08:00
|
|
|
{
|
|
|
|
GLProgramState* ret = GLProgramStateCache::getInstance()->getGLProgramState(glprogram);
|
2014-05-09 12:54:49 +08:00
|
|
|
return ret;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
GLProgramState* GLProgramState::getOrCreateWithShaders(const std::string& vertexShader, const std::string& fragShader, const std::string& compileTimeDefines)
|
|
|
|
{
|
|
|
|
auto glprogramcache = GLProgramCache::getInstance();
|
|
|
|
const std::string key = vertexShader + "+" + fragShader + "+" + compileTimeDefines;
|
|
|
|
auto glprogram = glprogramcache->getGLProgram(key);
|
|
|
|
|
|
|
|
if (!glprogram) {
|
|
|
|
glprogram = GLProgram::createWithFilenames(vertexShader, fragShader, compileTimeDefines);
|
|
|
|
glprogramcache->addGLProgram(glprogram, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
return create(glprogram);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
GLProgramState::GLProgramState()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _uniformAttributeValueDirty(true)
|
2015-06-06 05:53:30 +08:00
|
|
|
, _textureUnitIndex(4) // first 4 textures unites are reserved for CC_Texture0-3
|
2014-10-30 22:25:46 +08:00
|
|
|
, _vertexAttribsFlags(0)
|
|
|
|
, _glprogram(nullptr)
|
2015-06-03 07:56:59 +08:00
|
|
|
, _nodeBinding(nullptr)
|
2014-05-09 03:34:26 +08:00
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-07-09 23:03:04 +08:00
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
CCLOG("create rendererRecreatedListener for GLProgramState");
|
2014-12-30 11:13:14 +08:00
|
|
|
_backToForegroundlistener = EventListenerCustom::create(EVENT_RENDERER_RECREATED,
|
|
|
|
[this](EventCustom*)
|
|
|
|
{
|
|
|
|
CCLOG("Dirty Uniform and Attributes of GLProgramState");
|
2014-12-30 18:32:09 +08:00
|
|
|
_uniformAttributeValueDirty = true;
|
2014-12-30 11:13:14 +08:00
|
|
|
});
|
2014-05-28 15:16:00 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(_backToForegroundlistener, -1);
|
|
|
|
#endif
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
GLProgramState::~GLProgramState()
|
2014-05-28 16:51:33 +08:00
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-05-28 16:51:33 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_backToForegroundlistener);
|
|
|
|
#endif
|
|
|
|
|
2014-05-10 01:23:24 +08:00
|
|
|
CC_SAFE_RELEASE(_glprogram);
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
GLProgramState* GLProgramState::clone() const
|
|
|
|
{
|
|
|
|
auto glprogramstate = new (std::nothrow) GLProgramState();
|
|
|
|
|
|
|
|
// copy everything manually, instead of calling init since this is faster
|
|
|
|
|
|
|
|
glprogramstate->_glprogram = this->_glprogram;
|
|
|
|
CC_SAFE_RETAIN(glprogramstate->_glprogram);
|
|
|
|
|
|
|
|
glprogramstate->_attributes = this->_attributes;
|
|
|
|
glprogramstate->_vertexAttribsFlags = this->_vertexAttribsFlags;
|
|
|
|
|
|
|
|
// copy uniforms
|
|
|
|
glprogramstate->_uniformsByName = this->_uniformsByName;
|
|
|
|
glprogramstate->_uniforms = this->_uniforms;
|
|
|
|
glprogramstate->_uniformAttributeValueDirty = this->_uniformAttributeValueDirty;
|
|
|
|
|
|
|
|
// copy textures
|
|
|
|
glprogramstate->_textureUnitIndex = this->_textureUnitIndex;
|
|
|
|
glprogramstate->_boundTextureUnits = this->_boundTextureUnits;
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
// _nodeBinding is null since a node can only have one state.
|
2015-09-22 16:08:23 +08:00
|
|
|
// making the null explicit to avoid possible bugs in the future
|
2015-06-03 07:56:59 +08:00
|
|
|
glprogramstate->_nodeBinding = nullptr;
|
|
|
|
|
|
|
|
// copy autobindings... rebound them once a target is set again
|
|
|
|
glprogramstate->_autoBindings = _autoBindings;
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
glprogramstate->autorelease();
|
|
|
|
return glprogramstate;
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
bool GLProgramState::init(GLProgram* glprogram)
|
|
|
|
{
|
2014-05-09 03:34:26 +08:00
|
|
|
CCASSERT(glprogram, "invalid shader");
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
_glprogram = glprogram;
|
|
|
|
_glprogram->retain();
|
|
|
|
|
2014-05-14 09:12:58 +08:00
|
|
|
for(auto &attrib : _glprogram->_vertexAttribs) {
|
2014-05-08 11:20:19 +08:00
|
|
|
VertexAttribValue value(&attrib.second);
|
|
|
|
_attributes[attrib.first] = value;
|
|
|
|
}
|
|
|
|
|
2014-05-14 09:12:58 +08:00
|
|
|
for(auto &uniform : _glprogram->_userUniforms) {
|
2014-05-08 11:38:15 +08:00
|
|
|
UniformValue value(&uniform.second, _glprogram);
|
2014-06-26 18:26:41 +08:00
|
|
|
_uniforms[uniform.second.location] = value;
|
|
|
|
_uniformsByName[uniform.first] = uniform.second.location;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-05-09 03:34:26 +08:00
|
|
|
|
|
|
|
void GLProgramState::resetGLProgram()
|
|
|
|
{
|
|
|
|
CC_SAFE_RELEASE(_glprogram);
|
2015-06-03 07:56:59 +08:00
|
|
|
_glprogram = nullptr;
|
2014-05-09 03:34:26 +08:00
|
|
|
_uniforms.clear();
|
|
|
|
_attributes.clear();
|
2014-05-13 14:51:37 +08:00
|
|
|
// first texture is GL_TEXTURE1
|
|
|
|
_textureUnitIndex = 1;
|
2015-06-03 07:56:59 +08:00
|
|
|
_nodeBinding = nullptr;
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void GLProgramState::apply(const Mat4& modelView)
|
2014-06-24 19:45:51 +08:00
|
|
|
{
|
|
|
|
applyGLProgram(modelView);
|
|
|
|
|
|
|
|
applyAttributes();
|
|
|
|
|
|
|
|
applyUniforms();
|
|
|
|
}
|
|
|
|
|
2014-12-30 11:13:14 +08:00
|
|
|
void GLProgramState::updateUniformsAndAttributes()
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
CCASSERT(_glprogram, "invalid glprogram");
|
2014-05-28 15:16:00 +08:00
|
|
|
if(_uniformAttributeValueDirty)
|
|
|
|
{
|
2014-06-26 18:26:41 +08:00
|
|
|
for(auto& uniformLocation : _uniformsByName)
|
2014-05-28 15:16:00 +08:00
|
|
|
{
|
2014-06-26 18:26:41 +08:00
|
|
|
_uniforms[uniformLocation.second]._uniform = _glprogram->getUniform(uniformLocation.first);
|
2014-05-28 15:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
_vertexAttribsFlags = 0;
|
|
|
|
for(auto& attributeValue : _attributes)
|
|
|
|
{
|
|
|
|
attributeValue.second._vertexAttrib = _glprogram->getVertexAttrib(attributeValue.first);;
|
|
|
|
if(attributeValue.second._enabled)
|
|
|
|
_vertexAttribsFlags |= 1 << attributeValue.second._vertexAttrib->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
_uniformAttributeValueDirty = false;
|
|
|
|
|
|
|
|
}
|
2014-12-30 11:13:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GLProgramState::applyGLProgram(const Mat4& modelView)
|
|
|
|
{
|
|
|
|
CCASSERT(_glprogram, "invalid glprogram");
|
|
|
|
updateUniformsAndAttributes();
|
2014-05-08 11:20:19 +08:00
|
|
|
// set shader
|
|
|
|
_glprogram->use();
|
2014-05-09 03:34:26 +08:00
|
|
|
_glprogram->setUniformsForBuiltins(modelView);
|
2014-06-24 19:45:51 +08:00
|
|
|
}
|
2015-05-06 04:07:32 +08:00
|
|
|
|
2014-06-25 17:36:55 +08:00
|
|
|
void GLProgramState::applyAttributes(bool applyAttribFlags)
|
2014-06-24 19:45:51 +08:00
|
|
|
{
|
2014-05-13 14:51:37 +08:00
|
|
|
// Don't set attributes if they weren't set
|
|
|
|
// Use Case: Auto-batching
|
2015-01-12 14:43:31 +08:00
|
|
|
updateUniformsAndAttributes();
|
2014-05-13 10:12:56 +08:00
|
|
|
if(_vertexAttribsFlags) {
|
|
|
|
// enable/disable vertex attribs
|
2014-06-25 17:36:55 +08:00
|
|
|
if (applyAttribFlags)
|
|
|
|
GL::enableVertexAttribs(_vertexAttribsFlags);
|
2014-05-13 10:12:56 +08:00
|
|
|
// set attributes
|
2014-05-28 15:16:00 +08:00
|
|
|
for(auto &attribute : _attributes)
|
|
|
|
{
|
2014-05-13 10:12:56 +08:00
|
|
|
attribute.second.apply();
|
|
|
|
}
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
2014-06-24 19:45:51 +08:00
|
|
|
}
|
|
|
|
void GLProgramState::applyUniforms()
|
|
|
|
{
|
2014-05-08 11:20:19 +08:00
|
|
|
// set uniforms
|
2015-01-12 14:43:31 +08:00
|
|
|
updateUniformsAndAttributes();
|
2014-05-08 11:20:19 +08:00
|
|
|
for(auto& uniform : _uniforms) {
|
|
|
|
uniform.second.apply();
|
|
|
|
}
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
2014-05-08 11:20:19 +08:00
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
void GLProgramState::setGLProgram(GLProgram *glprogram)
|
|
|
|
{
|
2014-05-13 14:51:37 +08:00
|
|
|
CCASSERT(glprogram, "invalid GLProgram");
|
2014-05-09 03:34:26 +08:00
|
|
|
|
|
|
|
if( _glprogram != glprogram) {
|
|
|
|
resetGLProgram();
|
|
|
|
init(glprogram);
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
uint32_t GLProgramState::getVertexAttribsFlags() const
|
|
|
|
{
|
|
|
|
return _vertexAttribsFlags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t GLProgramState::getVertexAttribCount() const
|
|
|
|
{
|
|
|
|
return _attributes.size();
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
UniformValue* GLProgramState::getUniformValue(GLint uniformLocation)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-12-30 18:32:09 +08:00
|
|
|
updateUniformsAndAttributes();
|
2014-06-26 18:26:41 +08:00
|
|
|
const auto itr = _uniforms.find(uniformLocation);
|
|
|
|
if (itr != _uniforms.end())
|
2014-05-08 11:20:19 +08:00
|
|
|
return &itr->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
UniformValue* GLProgramState::getUniformValue(const std::string& name)
|
2014-06-26 18:26:41 +08:00
|
|
|
{
|
2014-12-30 18:32:09 +08:00
|
|
|
updateUniformsAndAttributes();
|
2014-06-26 18:26:41 +08:00
|
|
|
const auto itr = _uniformsByName.find(name);
|
|
|
|
if (itr != _uniformsByName.end())
|
|
|
|
return &_uniforms[itr->second];
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string& name)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-12-30 18:32:09 +08:00
|
|
|
updateUniformsAndAttributes();
|
2014-05-08 11:20:19 +08:00
|
|
|
const auto itr = _attributes.find(name);
|
|
|
|
if( itr != _attributes.end())
|
|
|
|
return &itr->second;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
// VertexAttrib Setters
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setVertexAttribCallback(const std::string& name, const std::function<void(VertexAttrib*)> &callback)
|
2014-05-09 03:34:26 +08:00
|
|
|
{
|
|
|
|
VertexAttribValue *v = getVertexAttribValue(name);
|
2014-05-09 06:43:12 +08:00
|
|
|
if(v) {
|
|
|
|
v->setCallback(callback);
|
|
|
|
_vertexAttribsFlags |= 1 << v->_vertexAttrib->index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
|
|
|
|
}
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setVertexAttribPointer(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
|
2014-05-09 03:34:26 +08:00
|
|
|
{
|
|
|
|
auto v = getVertexAttribValue(name);
|
2014-05-09 06:43:12 +08:00
|
|
|
if(v) {
|
|
|
|
v->setPointer(size, type, normalized, stride, pointer);
|
|
|
|
_vertexAttribsFlags |= 1 << v->_vertexAttrib->index;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
CCLOG("cocos2d: warning: Attribute not found: %s", name.c_str());
|
2014-05-09 06:43:12 +08:00
|
|
|
}
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
// Uniform Setters
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformCallback(const std::string& uniformName, const std::function<void(GLProgram*, Uniform*)> &callback)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setCallback(callback);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformCallback(GLint uniformLocation, const std::function<void(GLProgram*, Uniform*)> &callback)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setCallback(callback);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformFloat(const std::string& uniformName, float value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setFloat(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformFloat(GLint uniformLocation, float value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setFloat(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformInt(const std::string& uniformName, int value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-10 04:24:56 +08:00
|
|
|
if(v)
|
2014-05-13 14:57:46 +08:00
|
|
|
v->setInt(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformInt(GLint uniformLocation, int value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setInt(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformFloatv(const std::string& uniformName, ssize_t size, const float* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setFloatv(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void GLProgramState::setUniformFloatv(GLint uniformLocation, ssize_t size, const float* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setFloatv(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec2(const std::string& uniformName, const Vec2& value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setVec2(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformVec2(GLint uniformLocation, const Vec2& value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setVec2(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec2v(const std::string& uniformName, ssize_t size, const Vec2* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec2v(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void GLProgramState::setUniformVec2v(GLint uniformLocation, ssize_t size, const Vec2* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec2v(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec3(const std::string& uniformName, const Vec3& value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setVec3(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformVec3(GLint uniformLocation, const Vec3& value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setVec3(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec3v(const std::string& uniformName, ssize_t size, const Vec3* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec3v(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void GLProgramState::setUniformVec3v(GLint uniformLocation, ssize_t size, const Vec3* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec3v(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec4(const std::string& uniformName, const Vec4& value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setVec4(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformVec4(GLint uniformLocation, const Vec4& value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setVec4(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformVec4v(const std::string& uniformName, ssize_t size, const Vec4* value)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec4v(size, value);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
|
|
|
}
|
|
|
|
|
2015-05-13 13:24:52 +08:00
|
|
|
void GLProgramState::setUniformVec4v(GLint uniformLocation, ssize_t size, const Vec4* pointer)
|
2015-05-06 04:07:32 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
2015-05-13 13:24:52 +08:00
|
|
|
v->setVec4v(size, pointer);
|
2015-05-06 04:07:32 +08:00
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformMat4(const std::string& uniformName, const Mat4& value)
|
2014-05-09 09:01:48 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
|
|
|
v->setMat4(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-05-09 09:01:48 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformMat4(GLint uniformLocation, const Mat4& value)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
v->setMat4(value);
|
|
|
|
else
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
// Textures
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformTexture(const std::string& uniformName, Texture2D *texture)
|
2014-05-13 14:51:37 +08:00
|
|
|
{
|
|
|
|
CCASSERT(texture, "Invalid texture");
|
|
|
|
setUniformTexture(uniformName, texture->getName());
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformTexture(GLint uniformLocation, Texture2D *texture)
|
|
|
|
{
|
|
|
|
CCASSERT(texture, "Invalid texture");
|
|
|
|
setUniformTexture(uniformLocation, texture->getName());
|
|
|
|
}
|
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
void GLProgramState::setUniformTexture(const std::string& uniformName, GLuint textureId)
|
2014-05-13 14:51:37 +08:00
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformName);
|
2014-05-13 14:57:46 +08:00
|
|
|
if (v)
|
2014-06-09 10:58:28 +08:00
|
|
|
{
|
|
|
|
if (_boundTextureUnits.find(uniformName) != _boundTextureUnits.end())
|
|
|
|
{
|
|
|
|
v->setTexture(textureId, _boundTextureUnits[uniformName]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->setTexture(textureId, _textureUnitIndex);
|
|
|
|
_boundTextureUnits[uniformName] = _textureUnitIndex++;
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 14:57:46 +08:00
|
|
|
else
|
2014-06-09 10:58:28 +08:00
|
|
|
{
|
2014-05-13 14:57:46 +08:00
|
|
|
CCLOG("cocos2d: warning: Uniform not found: %s", uniformName.c_str());
|
2014-06-09 10:58:28 +08:00
|
|
|
}
|
2014-05-13 14:51:37 +08:00
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
void GLProgramState::setUniformTexture(GLint uniformLocation, GLuint textureId)
|
|
|
|
{
|
|
|
|
auto v = getUniformValue(uniformLocation);
|
|
|
|
if (v)
|
|
|
|
{
|
|
|
|
if (_boundTextureUnits.find(v->_uniform->name) != _boundTextureUnits.end())
|
|
|
|
{
|
|
|
|
v->setTexture(textureId, _boundTextureUnits[v->_uniform->name]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
v->setTexture(textureId, _textureUnitIndex);
|
|
|
|
_boundTextureUnits[v->_uniform->name] = _textureUnitIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: warning: Uniform at location not found: %i", uniformLocation);
|
|
|
|
}
|
|
|
|
}
|
2014-05-13 14:51:37 +08:00
|
|
|
|
2015-06-03 07:56:59 +08:00
|
|
|
// Auto bindings
|
|
|
|
void GLProgramState::setParameterAutoBinding(const std::string& uniformName, const std::string& autoBinding)
|
|
|
|
{
|
|
|
|
_autoBindings[uniformName] = autoBinding;
|
|
|
|
|
|
|
|
if (_nodeBinding)
|
|
|
|
applyAutoBinding(uniformName, autoBinding);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLProgramState::applyAutoBinding(const std::string& uniformName, const std::string& autoBinding)
|
|
|
|
{
|
|
|
|
// This code tries to replace GLProgram::setUniformsForBuiltins. But it is unfinished ATM.
|
|
|
|
// The idea is that users will be able to use variables from cocos2d-x without hardcoding the
|
|
|
|
// information on GLProgram and other objects.
|
|
|
|
// Instead, the Cocos2d uniform variables will be callbacks.
|
|
|
|
// As an example of how bad the current design is, the ModelView matrix is being passed from Node, to the Commands, to the GLProgram.
|
|
|
|
// Instead, the GLProgramState should obtain it from its target.
|
|
|
|
|
|
|
|
bool resolved = false;
|
|
|
|
for (const auto resolver: _customAutoBindingResolvers)
|
|
|
|
{
|
|
|
|
resolved = resolver->resolveAutoBinding(this, _nodeBinding, uniformName, autoBinding);
|
|
|
|
if (resolved)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resolved)
|
|
|
|
{
|
|
|
|
// add cocos2d-x variables here like:
|
|
|
|
// PROJECT_MATRIX
|
|
|
|
// MODEL_MATRIX
|
|
|
|
// MODEL_VIEW
|
|
|
|
// MODEL_VIEW_PROJECTION
|
|
|
|
// etc...
|
|
|
|
//
|
|
|
|
// and remove them from GLProgram::setUniformsForBuiltins
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLProgramState::setNodeBinding(Node* target)
|
|
|
|
{
|
|
|
|
CCASSERT(target, "must be non-null");
|
|
|
|
|
|
|
|
// weak ref
|
|
|
|
_nodeBinding = target;
|
|
|
|
|
|
|
|
for (const auto autobinding: _autoBindings)
|
|
|
|
applyAutoBinding(autobinding.first, autobinding.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* GLProgramState::getNodeBinding() const
|
|
|
|
{
|
|
|
|
return _nodeBinding;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// MARK: AutoBindingResolver
|
|
|
|
//
|
|
|
|
GLProgramState::AutoBindingResolver::AutoBindingResolver()
|
|
|
|
{
|
|
|
|
_customAutoBindingResolvers.push_back(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLProgramState::AutoBindingResolver::~AutoBindingResolver()
|
|
|
|
{
|
|
|
|
std::vector<GLProgramState::AutoBindingResolver*>::iterator itr = std::find(_customAutoBindingResolvers.begin(), _customAutoBindingResolvers.end(), this);
|
|
|
|
if (itr != _customAutoBindingResolvers.end())
|
|
|
|
_customAutoBindingResolvers.erase(itr);
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
NS_CC_END
|