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.
|
2015-06-03 07:56:59 +08:00
|
|
|
|
|
|
|
Autobinding Interface from GamePlay3D: http://www.gameplay3d.org/
|
2014-05-08 11:20:19 +08:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
2015-03-27 11:54:40 +08:00
|
|
|
/**
|
2015-06-01 13:43:56 +08:00
|
|
|
* @addtogroup renderer
|
2015-03-27 11:54:40 +08:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
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;
|
2015-06-03 07:56:59 +08:00
|
|
|
class Node;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
2015-03-20 11:24:52 +08:00
|
|
|
/**
|
2015-03-30 15:47:47 +08:00
|
|
|
* Uniform Value, which is used to store to value send to openGL pipe line by glUniformXXX.
|
|
|
|
*
|
|
|
|
* @lua NA
|
2015-03-20 11:24:52 +08:00
|
|
|
*/
|
2014-07-14 17:02:06 +08:00
|
|
|
class CC_DLL UniformValue
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
friend class GLProgram;
|
2014-05-28 15:16:00 +08:00
|
|
|
friend class GLProgramState;
|
2014-05-08 11:20:19 +08:00
|
|
|
public:
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Construtor. The Uniform and Glprogram will be nullptr.
|
2015-03-20 11:24:52 +08:00
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
UniformValue();
|
2015-03-20 11:24:52 +08:00
|
|
|
/**
|
2015-03-27 11:03:21 +08:00
|
|
|
Construtor with uniform and glprogram.
|
|
|
|
@param uniform Uniform to apply the value.
|
|
|
|
@param glprogram Specify the owner GLProgram of this uniform and uniform value.
|
2015-03-20 11:24:52 +08:00
|
|
|
*/
|
2015-03-27 11:03:21 +08:00
|
|
|
UniformValue(Uniform *uniform, GLProgram* glprogram);
|
|
|
|
|
2015-03-20 11:24:52 +08:00
|
|
|
/**Destructor.*/
|
2014-05-08 11:20:19 +08:00
|
|
|
~UniformValue();
|
2015-03-20 11:24:52 +08:00
|
|
|
/**@{
|
|
|
|
Set data to Uniform value. Generally, there are many type of data could be supported,
|
2015-03-27 11:03:21 +08:00
|
|
|
including float, int, Vec2/3/4, Mat4.
|
2015-03-20 11:24:52 +08:00
|
|
|
@param value Value to be sent, support float, int, Vec2/3/4, Mat4.
|
|
|
|
*/
|
2014-05-13 15:10:01 +08:00
|
|
|
void setFloat(float value);
|
|
|
|
void setInt(int value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setFloatv(ssize_t size, const float* pointer);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setVec2(const Vec2& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setVec2v(ssize_t size, const Vec2* pointer);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setVec3(const Vec3& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setVec3v(ssize_t size, const Vec3* pointer);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setVec4(const Vec4& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setVec4v(ssize_t size, const Vec4* pointer);
|
2014-05-15 01:07:09 +08:00
|
|
|
void setMat4(const Mat4& value);
|
2015-03-20 11:24:52 +08:00
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|
|
|
|
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Set call back to uniform value, which could be used for array and struct.
|
|
|
|
@param callback Callback function to send data to OpenGL pipeline.
|
|
|
|
*/
|
|
|
|
void setCallback(const std::function<void(GLProgram*, Uniform*)> &callback);
|
|
|
|
/**
|
|
|
|
Set texture to uniform value.
|
2015-03-27 11:05:01 +08:00
|
|
|
@param textureId The texture handle.
|
2015-03-27 11:03:21 +08:00
|
|
|
@param textureUnit The binding texture unit to be used in shader.
|
|
|
|
*/
|
|
|
|
void setTexture(GLuint textureId, GLuint textureUnit);
|
|
|
|
|
2015-03-20 11:24:52 +08:00
|
|
|
/**Apply the uniform value to openGL pipeline.*/
|
2014-05-08 11:20:19 +08:00
|
|
|
void apply();
|
|
|
|
|
|
|
|
protected:
|
2015-05-06 04:07:32 +08:00
|
|
|
|
|
|
|
enum class Type {
|
|
|
|
VALUE,
|
|
|
|
POINTER,
|
|
|
|
CALLBACK_FN // CALLBACK is already defined in windows, can't use it.
|
|
|
|
};
|
|
|
|
|
2015-03-20 11:24:52 +08:00
|
|
|
/**Weak reference to Uniform.*/
|
|
|
|
Uniform* _uniform;
|
|
|
|
/**Weak reference to GLprogram.*/
|
|
|
|
GLProgram* _glprogram;
|
2015-05-06 04:07:32 +08:00
|
|
|
/** What kind of type is the Uniform */
|
|
|
|
Type _type;
|
|
|
|
|
2015-03-20 11:24:52 +08:00
|
|
|
/**
|
|
|
|
@name Uniform Value Uniform
|
|
|
|
@{
|
|
|
|
*/
|
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;
|
2015-05-06 04:07:32 +08:00
|
|
|
struct {
|
|
|
|
const float* pointer;
|
|
|
|
GLsizei size;
|
|
|
|
} floatv;
|
|
|
|
struct {
|
|
|
|
const float* pointer;
|
|
|
|
GLsizei size;
|
|
|
|
} v2f;
|
|
|
|
struct {
|
|
|
|
const float* pointer;
|
|
|
|
GLsizei size;
|
|
|
|
} v3f;
|
|
|
|
struct {
|
|
|
|
const float* pointer;
|
|
|
|
GLsizei size;
|
|
|
|
} v4f;
|
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;
|
2015-03-20 11:24:52 +08:00
|
|
|
/**
|
|
|
|
@}
|
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
};
|
|
|
|
|
2015-03-30 15:47:47 +08:00
|
|
|
/**
|
|
|
|
* Vertex Attribute Value, which is an abstraction of VertexAttribute and data pointer.
|
|
|
|
*
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2014-07-14 17:02:06 +08:00
|
|
|
class CC_DLL VertexAttribValue
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
|
|
|
friend class GLProgram;
|
2014-05-09 03:34:26 +08:00
|
|
|
friend class GLProgramState;
|
2015-05-13 10:58:09 +08:00
|
|
|
friend class VertexAttribBinding;
|
2014-05-08 11:20:19 +08:00
|
|
|
|
|
|
|
public:
|
2015-03-23 20:30:37 +08:00
|
|
|
/**
|
2015-03-27 11:03:21 +08:00
|
|
|
Constructor.
|
|
|
|
@param vertexAttrib VertexAttrib from shader.
|
2015-03-23 20:30:37 +08:00
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
VertexAttribValue(VertexAttrib *vertexAttrib);
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Constructor.
|
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
VertexAttribValue();
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Destructor.
|
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
~VertexAttribValue();
|
2015-03-23 20:30:37 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Set the data pointer, which is similar as glVertexAttribPointer.
|
|
|
|
@param size The number of type in the vertex attribute.
|
|
|
|
@param type The type of data in vertex attribute.
|
|
|
|
@param normalized If true, 0-255 data will be mapped to 0.0-1.0.
|
|
|
|
@param stride The number of bytes if an interleaved vertex array is used. 0 means array is not interleaved.
|
|
|
|
@param pointer The pointer to the vertex data.
|
|
|
|
*/
|
2014-05-09 03:34:26 +08:00
|
|
|
void setPointer(GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Set a user call back for set VertexAttrib array.*/
|
2014-05-09 09:01:48 +08:00
|
|
|
void setCallback(const std::function<void(VertexAttrib*)> &callback);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Apply the vertex attribute to the openGL pipeline.*/
|
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
|
|
|
|
*/
|
2015-05-14 08:39:39 +08:00
|
|
|
class CC_DLL GLProgramState : public Ref
|
2014-05-08 11:20:19 +08:00
|
|
|
{
|
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 */
|
2015-06-03 07:56:59 +08:00
|
|
|
static GLProgramState* getOrCreateWithGLProgramName(const std::string& glProgramName );
|
2014-05-08 11:20:19 +08:00
|
|
|
|
2015-05-06 04:07:32 +08:00
|
|
|
/** gets-or-creates an instance of GLProgramState for given shaders */
|
|
|
|
static GLProgramState* getOrCreateWithShaders(const std::string& vertexShader, const std::string& fragShader, const std::string& compileTimeDefines);
|
|
|
|
|
|
|
|
/** Returns a new copy of the GLProgramState. The GLProgram is reused */
|
|
|
|
GLProgramState* clone() const;
|
|
|
|
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
2015-03-23 20:30:37 +08:00
|
|
|
Apply GLProgram, attributes and uniforms.
|
2015-03-27 11:03:21 +08:00
|
|
|
@param modelView The applied modelView matrix to shader.
|
2015-03-23 20:30:37 +08:00
|
|
|
*/
|
2014-05-15 01:07:09 +08:00
|
|
|
void apply(const Mat4& modelView);
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Apply GLProgram, and built in uniforms.
|
|
|
|
@param modelView The applied modelView matrix to shader.
|
|
|
|
*/
|
2014-06-24 19:45:51 +08:00
|
|
|
void applyGLProgram(const Mat4& modelView);
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Apply attributes.
|
|
|
|
@param applyAttribFlags Call GL::enableVertexAttribs(_vertexAttribsFlags) or not.
|
|
|
|
*/
|
2014-06-25 17:36:55 +08:00
|
|
|
void applyAttributes(bool applyAttribFlags = true);
|
2015-03-27 11:03:21 +08:00
|
|
|
/**
|
|
|
|
Apply user defined uniforms.
|
|
|
|
*/
|
2014-06-24 19:45:51 +08:00
|
|
|
void applyUniforms();
|
2015-03-23 20:30:37 +08:00
|
|
|
|
|
|
|
/**@{
|
|
|
|
Setter and Getter of the owner GLProgram binded in this program state.
|
|
|
|
*/
|
2014-05-09 03:34:26 +08:00
|
|
|
void setGLProgram(GLProgram* glprogram);
|
|
|
|
GLProgram* getGLProgram() const { return _glprogram; }
|
2015-03-23 20:30:37 +08:00
|
|
|
|
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** Get the flag of vertex attribs used by OR operation.*/
|
2015-05-06 04:07:32 +08:00
|
|
|
uint32_t getVertexAttribsFlags() const;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**Get the number of vertex attributes.*/
|
2015-05-06 04:07:32 +08:00
|
|
|
ssize_t getVertexAttribCount() const;
|
2015-03-23 20:30:37 +08:00
|
|
|
/**@{
|
|
|
|
Set the vertex attribute value.
|
|
|
|
*/
|
2015-06-03 07:56:59 +08:00
|
|
|
void setVertexAttribCallback(const std::string& name, const std::function<void(VertexAttrib*)> &callback);
|
|
|
|
void setVertexAttribPointer(const std::string& name, GLint size, GLenum type, GLboolean normalized, GLsizei stride, GLvoid *pointer);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/**Get the number of user defined uniform count.*/
|
2014-05-13 10:12:56 +08:00
|
|
|
ssize_t getUniformCount() const { return _uniforms.size(); }
|
2015-03-23 20:30:37 +08:00
|
|
|
|
|
|
|
/** @{
|
|
|
|
Setting user defined uniforms by uniform string name in the shader.
|
|
|
|
*/
|
2015-06-03 07:56:59 +08:00
|
|
|
void setUniformInt(const std::string& uniformName, int value);
|
|
|
|
void setUniformFloat(const std::string& uniformName, float value);
|
|
|
|
void setUniformFloatv(const std::string& uniformName, ssize_t size, const float* pointer);
|
|
|
|
void setUniformVec2(const std::string& uniformName, const Vec2& value);
|
|
|
|
void setUniformVec2v(const std::string& uniformName, ssize_t size, const Vec2* pointer);
|
|
|
|
void setUniformVec3(const std::string& uniformName, const Vec3& value);
|
|
|
|
void setUniformVec3v(const std::string& uniformName, ssize_t size, const Vec3* pointer);
|
|
|
|
void setUniformVec4(const std::string& uniformName, const Vec4& value);
|
|
|
|
void setUniformVec4v(const std::string& uniformName, ssize_t size, const Vec4* pointer);
|
|
|
|
void setUniformMat4(const std::string& uniformName, const Mat4& value);
|
|
|
|
void setUniformCallback(const std::string& uniformName, const std::function<void(GLProgram*, Uniform*)> &callback);
|
|
|
|
void setUniformTexture(const std::string& uniformName, Texture2D *texture);
|
|
|
|
void setUniformTexture(const std::string& uniformName, GLuint textureId);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**@}*/
|
|
|
|
|
|
|
|
/** @{
|
|
|
|
Setting user defined uniforms by uniform location in the shader.
|
|
|
|
*/
|
2014-06-26 18:26:41 +08:00
|
|
|
void setUniformInt(GLint uniformLocation, int value);
|
|
|
|
void setUniformFloat(GLint uniformLocation, float value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setUniformFloatv(GLint uniformLocation, ssize_t size, const float* pointer);
|
2014-06-26 18:26:41 +08:00
|
|
|
void setUniformVec2(GLint uniformLocation, const Vec2& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setUniformVec2v(GLint uniformLocation, ssize_t size, const Vec2* pointer);
|
2014-06-26 18:26:41 +08:00
|
|
|
void setUniformVec3(GLint uniformLocation, const Vec3& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setUniformVec3v(GLint uniformLocation, ssize_t size, const Vec3* pointer);
|
2014-06-26 18:26:41 +08:00
|
|
|
void setUniformVec4(GLint uniformLocation, const Vec4& value);
|
2015-05-13 13:24:52 +08:00
|
|
|
void setUniformVec4v(GLint uniformLocation, ssize_t size, const Vec4* pointer);
|
2014-06-26 18:26:41 +08:00
|
|
|
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);
|
2015-03-23 20:30:37 +08:00
|
|
|
/**@}*/
|
2015-06-03 07:56:59 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the Node bound to the GLProgramState
|
|
|
|
*/
|
|
|
|
Node* getNodeBinding() const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the node that this render state is bound to.
|
|
|
|
*
|
|
|
|
* The specified node is used to apply auto-bindings for the render state.
|
|
|
|
* This is typically set to the node of the model that a material is
|
|
|
|
* applied to.
|
|
|
|
*
|
|
|
|
* @param node The node to use for applying auto-bindings.
|
|
|
|
*/
|
|
|
|
void setNodeBinding(Node* node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Applies the specified custom auto-binding.
|
|
|
|
*
|
|
|
|
* @param uniformName Name of the shader uniform.
|
|
|
|
* @param autoBinding Name of the auto binding.
|
|
|
|
*/
|
|
|
|
void applyAutoBinding(const std::string& uniformName, const std::string& autoBinding);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets a uniform auto-binding.
|
|
|
|
*
|
|
|
|
* This method parses the passed in autoBinding string and attempts to convert it
|
|
|
|
* to an enumeration value. If it matches to one of the predefined strings, it will create a
|
|
|
|
* callback to get the correct value at runtime.
|
|
|
|
*
|
|
|
|
* @param name The name of the material parameter to store an auto-binding for.
|
|
|
|
* @param autoBinding A string matching one of the built-in AutoBinding enum constants.
|
|
|
|
*/
|
|
|
|
void setParameterAutoBinding(const std::string& uniformName, const std::string& autoBinding);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An abstract base class that can be extended to support custom material auto bindings.
|
|
|
|
*
|
|
|
|
* Implementing a custom auto binding resolver allows the set of built-in parameter auto
|
|
|
|
* bindings to be extended or overridden. Any parameter auto binding that is set on a
|
|
|
|
* material will be forwarded to any custom auto binding resolvers, in the order in which
|
|
|
|
* they are registered. If a registered resolver returns true (specifying that it handles
|
|
|
|
* the specified autoBinding), no further code will be executed for that autoBinding.
|
|
|
|
* This allows auto binding resolvers to not only implement new/custom binding strings,
|
|
|
|
* but it also lets them override existing/built-in ones. For this reason, you should
|
|
|
|
* ensure that you ONLY return true if you explicitly handle a custom auto binding; return
|
|
|
|
* false otherwise.
|
|
|
|
*
|
|
|
|
* Note that the custom resolver is called only once for a GLProgramState object when its
|
|
|
|
* node binding is initially set. This occurs when a material is initially bound to a
|
|
|
|
* Node. The resolver is NOT called each frame or each time the GLProgramState is bound.
|
|
|
|
*
|
|
|
|
* If no registered resolvers explicitly handle an auto binding, the binding will attempt
|
|
|
|
* to be resolved using the internal/built-in resolver, which is able to handle any
|
|
|
|
* auto bindings found in the GLProgramState::AutoBinding enumeration.
|
|
|
|
*
|
|
|
|
* When an instance of a class that extends AutoBindingResolver is created, it is automatically
|
|
|
|
* registered as a custom auto binding handler. Likewise, it is automatically deregistered
|
|
|
|
* on destruction.
|
|
|
|
*
|
|
|
|
* @script{ignore}
|
|
|
|
*/
|
2015-06-03 14:31:57 +08:00
|
|
|
class CC_DLL AutoBindingResolver
|
2015-06-03 07:56:59 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
virtual ~AutoBindingResolver();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when an unrecognized uniform variable is encountered
|
|
|
|
* during material loading.
|
|
|
|
*
|
|
|
|
* Implemenations of this method should do a string comparison on the passed
|
|
|
|
* in name parameter and decide whether or not they should handle the
|
|
|
|
* parameter. If the parameter is not handled, false should be returned so
|
|
|
|
* that other auto binding resolvers get a chance to handle the parameter.
|
|
|
|
* Otherwise, the parameter should be set or bound and true should be returned.
|
|
|
|
*
|
|
|
|
* @param glProgramState The glProgramState
|
|
|
|
* @param node The node that the material is attached to.
|
|
|
|
* @param uniformName Name of the uniform
|
|
|
|
* @param autoBinding Name of the auto binding to be resolved.
|
|
|
|
*
|
|
|
|
* @return True if the auto binding is handled and the associated parmeter is
|
|
|
|
* bound, false otherwise.
|
|
|
|
*/
|
|
|
|
virtual bool resolveAutoBinding(GLProgramState* glProgramState, Node* node, const std::string& uniformName, const std::string& autoBinding) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
AutoBindingResolver();
|
|
|
|
};
|
|
|
|
|
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();
|
2014-12-30 11:13:14 +08:00
|
|
|
void updateUniformsAndAttributes();
|
2015-06-03 07:56:59 +08:00
|
|
|
VertexAttribValue* getVertexAttribValue(const std::string& attributeName);
|
|
|
|
UniformValue* getUniformValue(const std::string& uniformName);
|
2014-06-26 18:26:41 +08:00
|
|
|
UniformValue* getUniformValue(GLint uniformLocation);
|
2015-06-03 07:56:59 +08:00
|
|
|
|
|
|
|
|
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;
|
2015-06-03 07:56:59 +08:00
|
|
|
GLProgram* _glprogram;
|
|
|
|
|
|
|
|
Node* _nodeBinding; // weak ref
|
|
|
|
|
|
|
|
// contains uniform name and variable
|
|
|
|
std::unordered_map<std::string, std::string> _autoBindings;
|
|
|
|
|
|
|
|
// Map of custom auto binding resolvers.
|
|
|
|
static std::vector<AutoBindingResolver*> _customAutoBindingResolvers;
|
2014-10-24 21:00:08 +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 15:16:00 +08:00
|
|
|
EventListenerCustom* _backToForegroundlistener;
|
|
|
|
#endif
|
2015-06-03 07:56:59 +08:00
|
|
|
|
2014-05-08 11:20:19 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
2015-03-27 11:54:40 +08:00
|
|
|
/**
|
|
|
|
end of support group
|
|
|
|
@}
|
|
|
|
*/
|
2014-05-08 11:20:19 +08:00
|
|
|
#endif /* __CCGLPROGRAMSTATE_H__ */
|