axmol/cocos/renderer/CCGLProgramState.cpp

398 lines
10 KiB
C++
Raw Normal View History

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.
****************************************************************************/
#include "renderer/CCGLProgramState.h"
#include "renderer/CCGLProgram.h"
#include "renderer/ccGLStateCache.h"
2014-05-09 12:54:49 +08:00
#include "renderer/CCGLProgramStateCache.h"
2014-05-08 11:20:19 +08:00
NS_CC_BEGIN
//
//
// UniformValue
//
//
UniformValue::UniformValue()
: _useCallback(false)
, _uniform(nullptr)
2014-05-08 11:38:15 +08:00
, _glprogram(nullptr)
2014-05-08 11:20:19 +08:00
{
}
2014-05-08 11:38:15 +08:00
UniformValue::UniformValue(Uniform *uniform, GLProgram* glprogram)
2014-05-08 11:20:19 +08:00
: _useCallback(false)
, _uniform(uniform)
2014-05-08 11:38:15 +08:00
, _glprogram(glprogram)
2014-05-08 11:20:19 +08:00
{
}
2014-05-09 03:34:26 +08:00
UniformValue::~UniformValue()
{
}
2014-05-08 11:20:19 +08:00
void UniformValue::apply()
{
if(_useCallback) {
_value.callback(_uniform);
}
else
{
2014-05-08 11:38:15 +08:00
switch (_uniform->type) {
2014-05-08 11:20:19 +08:00
case GL_FLOAT:
2014-05-08 11:38:15 +08:00
_glprogram->setUniformLocationWith1f(_uniform->location, _value.floatValue);
2014-05-08 11:20:19 +08:00
break;
case GL_INT:
case GL_SAMPLER_2D:
2014-05-08 11:38:15 +08:00
_glprogram->setUniformLocationWith1i(_uniform->location, _value.intValue);
2014-05-08 11:20:19 +08:00
break;
case GL_FLOAT_VEC2:
2014-05-08 11:38:15 +08:00
_glprogram->setUniformLocationWith2f(_uniform->location, _value.v2Value.x, _value.v2Value.y);
2014-05-08 11:20:19 +08:00
break;
case GL_FLOAT_VEC3:
2014-05-08 11:38:15 +08:00
_glprogram->setUniformLocationWith3f(_uniform->location, _value.v3Value.x, _value.v3Value.y, _value.v3Value.z);
2014-05-08 11:20:19 +08:00
break;
case GL_FLOAT_VEC4:
2014-05-08 11:38:15 +08:00
_glprogram->setUniformLocationWith4f(_uniform->location, _value.v4Value.x, _value.v4Value.y, _value.v4Value.z, _value.v4Value.w);
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;
}
}
}
void UniformValue::setCallback(const std::function<void(Uniform*)> &callback)
2014-05-08 11:20:19 +08:00
{
_value.callback = callback;
_useCallback = true;
}
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
}
void UniformValue::setInt(int value)
2014-05-08 11:20:19 +08:00
{
2014-05-08 11:38:15 +08:00
CCASSERT ((_uniform->type == GL_INT || _uniform->type == GL_SAMPLER_2D), "");
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
}
void UniformValue::setVec2(const Vector2& 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-08 11:20:19 +08:00
_value.v2Value = value;
2014-05-09 01:12:12 +08:00
_useCallback = false;
2014-05-08 11:20:19 +08:00
}
void UniformValue::setVec3(const Vector3& 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-08 11:20:19 +08:00
_value.v3Value = value;
2014-05-09 01:12:12 +08:00
_useCallback = false;
2014-05-08 11:20:19 +08:00
}
void UniformValue::setVec4(const Vector4& 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-08 11:20:19 +08:00
_value.v4Value = value;
2014-05-09 01:12:12 +08:00
_useCallback = false;
2014-05-08 11:20:19 +08:00
}
void UniformValue::setMat4(const Matrix& 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-08 11:20:19 +08:00
_value.matrixValue = value;
2014-05-09 01:12:12 +08:00
_useCallback = false;
2014-05-08 11:20:19 +08:00
}
//
//
// VertexAttribValue
//
//
VertexAttribValue::VertexAttribValue()
: _useCallback(false)
, _vertexAttrib(nullptr)
{
}
VertexAttribValue::VertexAttribValue(VertexAttrib *vertexAttrib)
: _useCallback(false)
, _vertexAttrib(vertexAttrib)
{
}
2014-05-09 03:34:26 +08:00
VertexAttribValue::~VertexAttribValue()
2014-05-08 11:20:19 +08:00
{
2014-05-09 03:34:26 +08:00
}
void VertexAttribValue::apply()
{
2014-05-08 11:20:19 +08:00
if(_useCallback) {
_value.callback(_vertexAttrib);
}
else
{
2014-05-09 05:01:03 +08:00
glVertexAttribPointer(_vertexAttrib->index,
2014-05-08 11:20:19 +08:00
_value.pointer.size,
_value.pointer.type,
_value.pointer.normalized,
_value.pointer.stride,
_value.pointer.pointer);
}
}
void VertexAttribValue::setCallback(const std::function<void(VertexAttrib*)> &callback)
2014-05-08 11:20:19 +08:00
{
_value.callback = callback;
_useCallback = true;
}
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;
}
//
//
// GLProgramState
//
//
2014-05-09 12:54:49 +08:00
GLProgramState* GLProgramState::create(GLProgram *glprogram, bool useCache)
2014-05-08 11:20:19 +08:00
{
2014-05-09 12:54:49 +08:00
GLProgramState* ret = nullptr;
if (useCache)
{
ret = GLProgramStateCache::getInstance()->getProgramState(glprogram);
2014-05-10 01:23:24 +08:00
ret->_useCache = useCache;
2014-05-09 12:54:49 +08:00
}
else
{
ret = new (std::nothrow) GLProgramState;
if(!ret || !ret->init(glprogram))
CC_SAFE_RELEASE(ret);
2014-05-10 01:23:24 +08:00
ret->_useCache = useCache;
2014-05-08 11:20:19 +08:00
}
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()
: _vertexAttribsFlags(0)
2014-05-10 01:23:24 +08:00
, _useCache(false)
, _glprogram(nullptr)
2014-05-09 03:34:26 +08:00
{
}
2014-05-08 11:20:19 +08:00
GLProgramState::~GLProgramState()
{
2014-05-10 01:23:24 +08:00
if (_useCache)
GLProgramStateCache::getInstance()->removeProgramState(_glprogram);
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();
for(auto &attrib : _glprogram->_attributesDictionary) {
VertexAttribValue value(&attrib.second);
_attributes[attrib.first] = value;
}
for(auto &uniform : _glprogram->_uniformsDictionary) {
2014-05-08 11:38:15 +08:00
UniformValue value(&uniform.second, _glprogram);
2014-05-08 11:20:19 +08:00
_uniforms[uniform.first] = value;
}
return true;
}
2014-05-09 03:34:26 +08:00
void GLProgramState::resetGLProgram()
{
CC_SAFE_RELEASE(_glprogram);
_uniforms.clear();
_attributes.clear();
}
void GLProgramState::apply(const Matrix& modelView)
2014-05-08 11:20:19 +08:00
{
CCASSERT(_glprogram, "invalid glprogram");
// set shader
_glprogram->use();
2014-05-09 03:34:26 +08:00
_glprogram->setUniformsForBuiltins(modelView);
2014-05-08 11:20:19 +08:00
2014-05-09 03:34:26 +08:00
// enable/disable vertex attribs
GL::enableVertexAttribs(_vertexAttribsFlags);
// set attributes
for(auto &attribute : _attributes) {
attribute.second.apply();
}
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)
{
CCASSERT(glprogram, "invalid GLProgram");
if( _glprogram != glprogram) {
resetGLProgram();
init(glprogram);
2014-05-08 11:20:19 +08:00
}
}
UniformValue* GLProgramState::getUniformValue(const std::string &name)
{
const auto itr = _uniforms.find(name);
if( itr != _uniforms.end())
return &itr->second;
return nullptr;
}
VertexAttribValue* GLProgramState::getVertexAttribValue(const std::string &name)
{
const auto itr = _attributes.find(name);
if( itr != _attributes.end())
return &itr->second;
return nullptr;
}
// 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
{
CCASSERT(false, "attribute not found");
}
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
{
CCASSERT(false, "attribute not found");
}
2014-05-09 03:34:26 +08:00
}
// Uniform Setters
void GLProgramState::setUniformCallback(const std::string &uniformName, const std::function<void(Uniform*)> &callback)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setCallback(callback);
}
void GLProgramState::setUniformFloat(const std::string &uniformName, float value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setFloat(value);
}
void GLProgramState::setUniformInt(const std::string &uniformName, int value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setInt(value);
}
void GLProgramState::setUniformVec2(const std::string &uniformName, const Vector2& value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setVec2(value);
}
void GLProgramState::setUniformVec3(const std::string &uniformName, const Vector3& value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setVec3(value);
}
void GLProgramState::setUniformVec4(const std::string &uniformName, const Vector4& value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setVec4(value);
}
void GLProgramState::setUniformMat4(const std::string &uniformName, const Matrix& value)
{
auto v = getUniformValue(uniformName);
CCASSERT(v, "unknown uniform value");
v->setMat4(value);
}
2014-05-09 03:34:26 +08:00
2014-05-08 11:20:19 +08:00
NS_CC_END