Hi Guys,
I realised that all of the uniform values in GLProgramState are passed as weak reference to shader. For instance the function setFloatv in CCGLProgramState.cpp takes a pointer to a float expecting the pointer is still valid when the uniform values is finally applied to an underlying shader program.
CCGLProgramState.cpp: void UniformValue::setFloatv(ssize_t size, const float* pointer)
{
I am writing a simple one-shot static function to bind uniform values to a node. In order to to support this constraint I would have to host new memory resources for the floats and be able to track the usage so to be able to recycle the memory. I have not figure out how to do that too.
This would create overhead logics for my simple “one-shot” function. I simply hoped that Coco2d-x developers honors shared_ptr in their API more. This way, we can create the values and let the shared_ptr worry about managing memory resource.
Any recommendation appreciated…
I have decided to patch thing up for now
diff --git a/cocos/renderer/CCGLProgramState.cpp b/cocos/renderer/CCGLProgramState.cpp
index 120950c…671516e 100644
— a/cocos/renderer/CCGLProgramState.cpp
+++ b/cocos/renderer/CCGLProgramState.cpp
@@ -56,6 +56,7 @@ UniformValue::UniformValue()
: _uniform(nullptr)
, _glprogram(nullptr)
, _type(Type::VALUE)
+, _spare(nullptr)
{
}
@@ -63,6 +64,7 @@ UniformValue::UniformValue(Uniform uniform, GLProgram glprogram)
: _uniform(uniform)
, _glprogram(glprogram)
, _type(Type::VALUE)
+, _spare(nullptr)
{
}
@@ -70,6 +72,9 @@ UniformValue::~UniformValue()
{
if (_type == Type::CALLBACK_FN)
delete _value.callback;
+
- if (_spare != nullptr)
-
CC_SAFE_DELETE(_spare);
}
void UniformValue::apply()
@@ -185,7 +190,14 @@ void UniformValue::setFloat(float value)
void UniformValue::setFloatv(ssize_t size, const float* pointer)
{
CCASSERT(_uniform->type == GL_FLOAT, “Wrong type: expecting GL_FLOAT”);
- _value.floatv.pointer = (const float*)pointer;
-
-
if (_spare != nullptr)
-
CC_SAFE_DELETE(_spare);
-
-
_spare = new float [size];
-
memcpy(_spare, pointer, size * sizeof(float));
-
-
_value.floatv.pointer = (const float*)_spare;
_value.floatv.size = (GLsizei)size;
_type = Type::POINTER;
}
diff --git a/cocos/renderer/CCGLProgramState.h b/cocos/renderer/CCGLProgramState.h
index ba070a4…dc1d877 100644
— a/cocos/renderer/CCGLProgramState.h
+++ b/cocos/renderer/CCGLProgramState.h
@@ -121,6 +121,9 @@ protected:
GLProgram* _glprogram;
/** What kind of type is the Uniform */
Type _type;
-
-
// host memory resource for vector of floats.
-
float *_spare;
/**
@name Uniform Value Uniform