2014-05-08 11:20:19 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright 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 NO 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CCGLPROGRAMSTATE_H__
|
|
|
|
#define __CCGLPROGRAMSTATE_H__
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
#include "base/ccTypes.h"
|
|
|
|
#include "base/CCVector.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "math/Vec2.h"
|
|
|
|
#include "math/Vec3.h"
|
|
|
|
#include "math/Vec4.h"
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
class GLProgram;
|
2014-05-13 14:51:37 +08:00
|
|
|
class Texture2D;
|
2014-05-11 12:14:17 +08:00
|
|
|
struct Uniform;
|
|
|
|
struct VertexAttrib;
|
2014-05-28 15:16:00 +08:00
|
|
|
class EventListenerCustom;
|
|
|
|
class EventCustom;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// UniformValue
|
|
|
|
//
|
|
|
|
//
|
|
|
|
class UniformValue
|
|
|
|
{
|
|
|
|
friend class GLProgram;
|
2014-05-28 15:16:00 +08:00
|
|
|
friend class GLProgramState;
|
2014-05-08 11:20:19 +08:00
|
|
|
public:
|
|
|
|
UniformValue();
|
2014-05-08 11:38:15 +08:00
|
|
|
UniformValue(Uniform *uniform, GLProgram* glprogram);
|
2014-05-08 11:20:19 +08:00
|
|
|
~UniformValue();
|
|
|
|
|
2014-05-13 15:10:01 +08:00
|
|
|
void setFloat(float value);
|
|
|
|
void setInt(int value);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setVec2(const Vec2& value);
|
|
|
|
void setVec3(const Vec3& value);
|
|
|
|
void setVec4(const Vec4& value);
|
|
|
|
void setMat4(const Mat4& value);
|
2014-06-11 14:05:35 +08:00
|
|
|
void setCallback(const std::function<void(GLProgram*, Uniform*)> &callback);
|
2014-05-13 14:51:37 +08:00
|
|
|
void setTexture(GLuint textureId, GLuint activeTexture);
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
void apply();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Uniform* _uniform; // weak ref
|
2014-05-08 11:38:15 +08:00
|
|
|
GLProgram* _glprogram; // weak ref
|
2014-05-08 11:20:19 +08:00
|
|
|
bool _useCallback;
|
2014-05-08 11:38:15 +08:00
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
union U{
|
2014-05-13 15:10:01 +08:00
|
|
|
float floatValue;
|
|
|
|
int intValue;
|
|
|
|
float v2Value[2];
|
|
|
|
float v3Value[3];
|
|
|
|
float v4Value[4];
|
|
|
|
float matrixValue[16];
|
2014-05-13 14:51:37 +08:00
|
|
|
struct {
|
|
|
|
GLuint textureId;
|
|
|
|
GLuint textureUnit;
|
|
|
|
} tex;
|
2014-06-11 14:05:35 +08:00
|
|
|
std::function<void(GLProgram*, Uniform*)> *callback;
|
2014-05-10 05:38:36 +08:00
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
U() { memset( this, 0, sizeof(*this) ); }
|
|
|
|
~U(){}
|
|
|
|
U& operator=( const U& other ) {
|
|
|
|
memcpy(this, &other, sizeof(*this));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// VertexAttribValue
|
|
|
|
//
|
|
|
|
//
|
|
|
|
class VertexAttribValue
|
|
|
|
{
|
|
|
|
friend class GLProgram;
|
2014-05-09 03:34:26 +08:00
|
|
|
friend class GLProgramState;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
VertexAttribValue(VertexAttrib *vertexAttrib);
|
|
|
|
VertexAttribValue();
|
|
|
|
~VertexAttribValue();
|
|
|
|
|
2014-05-09 03:34:26 +08:00
|
|
|
void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
2014-05-09 09:01:48 +08:00
|
|
|
void setCallback(const std::function<void(VertexAttrib*)> &callback);
|
2014-05-08 11:20:19 +08:00
|
|
|
void apply();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
VertexAttrib* _vertexAttrib; // weak ref
|
|
|
|
bool _useCallback;
|
2014-05-13 10:12:56 +08:00
|
|
|
bool _enabled;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
union U{
|
|
|
|
struct {
|
|
|
|
GLint size;
|
|
|
|
GLenum type;
|
|
|
|
GLboolean normalized;
|
|
|
|
GLsizei stride;
|
|
|
|
GLvoid *pointer;
|
|
|
|
} pointer;
|
2014-05-10 04:24:56 +08:00
|
|
|
std::function<void(VertexAttrib*)> *callback;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
U() { memset( this, 0, sizeof(*this) ); }
|
|
|
|
~U(){}
|
|
|
|
U& operator=( const U& other ) {
|
|
|
|
memcpy(this, &other, sizeof(*this));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
} _value;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-10 09:39:25 +08:00
|
|
|
/**
|
|
|
|
GLProgramState holds the 'state' (uniforms and attributes) of the GLProgram.
|
|
|
|
A GLProgram can be used by thousands of Nodes, but if different uniform values
|
|
|
|
are going to be used, then each node will need its own GLProgramState
|
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
class GLProgramState : public Ref
|
|
|
|
{
|
2014-05-09 12:54:49 +08:00
|
|
|
friend class GLProgramStateCache;
|
2014-05-08 11:20:19 +08:00
|
|
|
public:
|
2014-05-10 09:39:25 +08:00
|
|
|
|
|
|
|
/** returns a new instance of GLProgramState for a given GLProgram */
|
|
|
|
static GLProgramState* create(GLProgram* glprogram);
|
|
|
|
|
|
|
|
/** gets-or-creates an instance of GLProgramState for a given GLProgram */
|
2014-05-14 09:12:58 +08:00
|
|
|
static GLProgramState* getOrCreateWithGLProgram(GLProgram* glprogram);
|
2014-05-10 09:39:25 +08:00
|
|
|
|
|
|
|
/** gets-or-creates an instance of GLProgramState for a given GLProgramName */
|
2014-05-13 10:12:56 +08:00
|
|
|
static GLProgramState* getOrCreateWithGLProgramName(const std::string &glProgramName );
|
2014-05-08 11:20:19 +08:00
|
|
|
|
2014-06-24 19:45:51 +08:00
|
|
|
// apply GLProgram, attributes and uniforms
|
2014-05-15 01:07:09 +08:00
|
|
|
void apply(const Mat4& modelView);
|
2014-06-24 19:45:51 +08:00
|
|
|
|
|
|
|
void applyGLProgram(const Mat4& modelView);
|
2014-06-25 17:36:55 +08:00
|
|
|
/**
|
|
|
|
* apply vertex attributes
|
|
|
|
* @param applyAttribFlags Call GL::enableVertexAttribs(_vertexAttribsFlags) or not
|
|
|
|
*/
|
|
|
|
void applyAttributes(bool applyAttribFlags = true);
|
2014-06-24 19:45:51 +08:00
|
|
|
void applyUniforms();
|
2014-05-09 03:34:26 +08:00
|
|
|
|
|
|
|
void setGLProgram(GLProgram* glprogram);
|
|
|
|
GLProgram* getGLProgram() const { return _glprogram; }
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
// vertex attribs
|
2014-05-13 10:12:56 +08:00
|
|
|
uint32_t getVertexAttribsFlags() const { return _vertexAttribsFlags; }
|
|
|
|
ssize_t getVertexAttribCount() const { return _attributes.size(); }
|
2014-05-09 09:01:48 +08:00
|
|
|
void setVertexAttribCallback(const std::string &name, const std::function<void(VertexAttrib*)> &callback);
|
2014-05-09 03:34:26 +08:00
|
|
|
void setVertexAttribPointer(const std::string &name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
// user defined uniforms
|
2014-05-13 10:12:56 +08:00
|
|
|
ssize_t getUniformCount() const { return _uniforms.size(); }
|
2014-05-13 15:10:01 +08:00
|
|
|
void setUniformInt(const std::string &uniformName, int value);
|
2014-05-13 14:51:37 +08:00
|
|
|
void setUniformFloat(const std::string &uniformName, float value);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setUniformVec2(const std::string &uniformName, const Vec2& value);
|
|
|
|
void setUniformVec3(const std::string &uniformName, const Vec3& value);
|
|
|
|
void setUniformVec4(const std::string &uniformName, const Vec4& value);
|
|
|
|
void setUniformMat4(const std::string &uniformName, const Mat4& value);
|
2014-06-11 14:05:35 +08:00
|
|
|
void setUniformCallback(const std::string &uniformName, const std::function<void(GLProgram*, Uniform*)> &callback);
|
2014-05-13 14:51:37 +08:00
|
|
|
void setUniformTexture(const std::string &uniformName, Texture2D *texture);
|
|
|
|
void setUniformTexture(const std::string &uniformName, GLuint textureId);
|
2014-06-26 18:26:41 +08:00
|
|
|
|
|
|
|
void setUniformInt(GLint uniformLocation, int value);
|
|
|
|
void setUniformFloat(GLint uniformLocation, float value);
|
|
|
|
void setUniformVec2(GLint uniformLocation, const Vec2& value);
|
|
|
|
void setUniformVec3(GLint uniformLocation, const Vec3& value);
|
|
|
|
void setUniformVec4(GLint uniformLocation, const Vec4& value);
|
|
|
|
void setUniformMat4(GLint uniformLocation, const Mat4& value);
|
|
|
|
void setUniformCallback(GLint uniformLocation, const std::function<void(GLProgram*, Uniform*)> &callback);
|
|
|
|
void setUniformTexture(GLint uniformLocation, Texture2D *texture);
|
|
|
|
void setUniformTexture(GLint uniformLocation, GLuint textureId);
|
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
protected:
|
|
|
|
GLProgramState();
|
|
|
|
~GLProgramState();
|
|
|
|
bool init(GLProgram* program);
|
2014-05-09 03:34:26 +08:00
|
|
|
void resetGLProgram();
|
|
|
|
VertexAttribValue* getVertexAttribValue(const std::string &attributeName);
|
2014-05-09 09:01:48 +08:00
|
|
|
UniformValue* getUniformValue(const std::string &uniformName);
|
2014-06-26 18:26:41 +08:00
|
|
|
UniformValue* getUniformValue(GLint uniformLocation);
|
2014-05-28 15:16:00 +08:00
|
|
|
|
|
|
|
bool _uniformAttributeValueDirty;
|
2014-06-26 18:26:41 +08:00
|
|
|
std::unordered_map<std::string, GLint> _uniformsByName;
|
|
|
|
std::unordered_map<GLint, UniformValue> _uniforms;
|
2014-05-08 11:20:19 +08:00
|
|
|
std::unordered_map<std::string, VertexAttribValue> _attributes;
|
2014-06-09 10:58:28 +08:00
|
|
|
std::unordered_map<std::string, int> _boundTextureUnits;
|
2014-05-09 03:34:26 +08:00
|
|
|
|
2014-05-13 14:51:37 +08:00
|
|
|
int _textureUnitIndex;
|
2014-05-09 03:34:26 +08:00
|
|
|
uint32_t _vertexAttribsFlags;
|
2014-05-08 11:20:19 +08:00
|
|
|
GLProgram *_glprogram;
|
2014-05-28 15:16:00 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
EventListenerCustom* _backToForegroundlistener;
|
|
|
|
#endif
|
2014-05-08 11:20:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CCGLPROGRAMSTATE_H__ */
|