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"
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// UniformValue
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
UniformValue::UniformValue()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _uniform(nullptr)
|
2014-05-08 11:38:15 +08:00
|
|
|
, _glprogram(nullptr)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _useCallback(false)
|
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)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _useCallback(false)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
UniformValue::~UniformValue()
|
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
if (_useCallback)
|
|
|
|
delete _value.callback;
|
2014-05-09 03:34:26 +08:00
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
void UniformValue::apply()
|
|
|
|
{
|
|
|
|
if(_useCallback) {
|
2014-06-11 14:05:35 +08:00
|
|
|
(*_value.callback)(_glprogram, _uniform);
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
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();
|
|
|
|
if (_useCallback)
|
|
|
|
delete _value.callback;
|
|
|
|
|
2014-06-11 14:05:35 +08:00
|
|
|
_value.callback = new std::function<void(GLProgram*, Uniform*)>();
|
2014-05-10 04:24:56 +08:00
|
|
|
*_value.callback = callback;
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
_useCallback = true;
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
void UniformValue::setFloat(float value)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
CCASSERT (_uniform->type == GL_FLOAT, "");
|
2014-05-08 11:20:19 +08:00
|
|
|
_value.floatValue = value;
|
2014-05-09 01:12:12 +08:00
|
|
|
_useCallback = false;
|
2014-05-08 11:20:19 +08:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
void UniformValue::setTexture(GLuint textureId, GLuint textureUnit)
|
|
|
|
{
|
|
|
|
CCASSERT(_uniform->type == GL_SAMPLER_2D, "Wrong type. expecting GL_SAMPLER_2D");
|
|
|
|
_value.tex.textureId = textureId;
|
|
|
|
_value.tex.textureUnit = textureUnit;
|
|
|
|
_useCallback = false;
|
|
|
|
}
|
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;
|
2014-05-09 01:12:12 +08:00
|
|
|
_useCallback = false;
|
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
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
CCASSERT (_uniform->type == GL_FLOAT_VEC2, "");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v2Value, &value, sizeof(_value.v2Value));
|
2014-05-09 01:12:12 +08:00
|
|
|
_useCallback = false;
|
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
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
CCASSERT (_uniform->type == GL_FLOAT_VEC3, "");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v3Value, &value, sizeof(_value.v3Value));
|
|
|
|
_useCallback = false;
|
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
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
CCASSERT (_uniform->type == GL_FLOAT_VEC4, "");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.v4Value, &value, sizeof(_value.v4Value));
|
|
|
|
_useCallback = false;
|
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
|
|
|
{
|
2014-05-08 11:38:15 +08:00
|
|
|
CCASSERT(_uniform->type == GL_FLOAT_MAT4, "");
|
2014-05-10 04:24:56 +08:00
|
|
|
memcpy(_value.matrixValue, &value, sizeof(_value.matrixValue));
|
|
|
|
_useCallback = false;
|
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
|
|
|
{
|
2014-05-10 04:24:56 +08:00
|
|
|
_value.callback = new std::function<void(VertexAttrib*)>();
|
|
|
|
*_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
|
|
|
}
|
|
|
|
|
2014-05-13 10:12:56 +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
|
|
|
}
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
GLProgramState::GLProgramState()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _uniformAttributeValueDirty(true)
|
2014-05-13 14:51:37 +08:00
|
|
|
, _textureUnitIndex(1)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _vertexAttribsFlags(0)
|
|
|
|
, _glprogram(nullptr)
|
2014-05-09 03:34:26 +08:00
|
|
|
{
|
2014-10-24 21:00:08 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
2014-07-09 23:03:04 +08:00
|
|
|
/** listen the event that renderer was recreated on Android/WP8 */
|
|
|
|
CCLOG("create rendererRecreatedListener for GLProgramState");
|
|
|
|
_backToForegroundlistener = EventListenerCustom::create(EVENT_RENDERER_RECREATED, [this](EventCustom*) { _uniformAttributeValueDirty = true; });
|
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
|
|
|
{
|
2014-10-24 21:00:08 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
_uniforms.clear();
|
|
|
|
_attributes.clear();
|
2014-05-13 14:51:37 +08:00
|
|
|
// first texture is GL_TEXTURE1
|
|
|
|
_textureUnitIndex = 1;
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GLProgramState::applyGLProgram(const Mat4& modelView)
|
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-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
|
|
|
}
|
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
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
UniformValue* GLProgramState::getUniformValue(GLint uniformLocation)
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-26 18:26:41 +08:00
|
|
|
UniformValue* GLProgramState::getUniformValue(const std::string &name)
|
|
|
|
{
|
|
|
|
const auto itr = _uniformsByName.find(name);
|
|
|
|
if (itr != _uniformsByName.end())
|
|
|
|
return &_uniforms[itr->second];
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string &name)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void GLProgramState::setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2014-06-11 14:05:35 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
void GLProgramState::setUniformFloat(const std::string &uniformName, float value)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-05-09 09:01:48 +08:00
|
|
|
void GLProgramState::setUniformInt(const std::string &uniformName, int value)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +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);
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +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
|
|
|
|
|
|
|
|
void GLProgramState::setUniformTexture(const std::string &uniformName, Texture2D *texture)
|
|
|
|
{
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
void GLProgramState::setUniformTexture(const std::string &uniformName, GLuint textureId)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
NS_CC_END
|