[Bf-committers] [Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31331] branches/soc-2010-moguri-2/source/ gameengine/Ketsji: Adding a Uniform. value attribute which can be used to get/ set the value of all uniforms that are currently supported except for Samp

Dalai Felinto dfelinto at gmail.com
Sat Aug 14 10:16:43 CEST 2010


Hi Mitchell,

Out of the top of my head I see 3 different data types that users may expect
to use here:
1) texture channels (as we have for Multitexture custom GLSL shaders) 0,1
...
2) the bind GL_UINT number - bind id (which could be retrieved using
VideoTexture)
3) a buffer with the texture (seems overkill to me)

The advantages of (1) is that the texture is already loaded in the Graphic
Card, therefore there is no need to worry about loading/unloading it.
The (2) gives tons of flexibility. It requires some Python though.
I would really advice against (3). If this is needed I would rather see a
more improved interface for VideoTexture to handle custom textures (as we
sort of already have) than to have it all implemented in the Custom Shader
side.

So in the end you may be right. You may want to use a Texture datablock
there.
Cheers,
Dalai

2010/8/13 Mitchell Stokes <mogurijin at gmail.com>

> Revision: 31331
>
> http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31331
> Author:   moguri
> Date:     2010-08-14 03:33:26 +0200 (Sat, 14 Aug 2010)
>
> Log Message:
> -----------
> Adding a Uniform.value attribute which can be used to get/set the value of
> all uniforms that are currently supported except for Sampler2D. I'm not yet
> quite sure how to tackle this one. Maybe switching it from a Texture to an
> Image first will help.
>
> Modified Paths:
> --------------
>    branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
>    branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h
>
> Modified:
> branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
> ===================================================================
> ---
> branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
>  2010-08-13 23:08:22 UTC (rev 31330)
> +++
> branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.cpp
>  2010-08-14 01:33:26 UTC (rev 31331)
> @@ -211,6 +211,7 @@
>  {
>        m_name = cu->name;
>        m_type = cu->type;
> +       m_size = cu->size;
>        m_data = cu->data;
>  }
>
> @@ -260,6 +261,159 @@
>  PyAttributeDef KX_PythonUniform::Attributes[] = {
>        KX_PYATTRIBUTE_STRING_RO("name", KX_PythonUniform, m_name),
>        KX_PYATTRIBUTE_SHORT_RO("type", KX_PythonUniform, m_type),
> +       KX_PYATTRIBUTE_RW_FUNCTION("value", KX_PythonUniform,
> pyattr_get_value, pyattr_set_value),
>        {NULL}  //Sentinel
>  };
> +
> +PyObject *KX_PythonUniform::pyattr_get_value(void *self_v, const
> KX_PYATTRIBUTE_DEF *attrdef)
> +{
> +       KX_PythonUniform *self = static_cast<KX_PythonUniform*>(self_v);
> +
> +       switch(self->m_type)
> +       {
> +               case MA_UNF_FLOAT:
> +               {
> +                       float val = *(float*)(&self->m_data);
> +                       return PyFloat_FromDouble(val);
> +               }
> +               case MA_UNF_VEC2:
> +               case MA_UNF_VEC3:
> +               case MA_UNF_VEC4:
> +               {
> +                       float* val = static_cast<float*>(self->m_data);
> +                       PyObject* ret = PyList_New(self->m_size);
> +
> +                       for (int i=0; i<self->m_size; ++i)
> +                       {
> +                               PyList_SetItem(ret, i,
> PyFloat_FromDouble(val[i]));
> +                       }
> +
> +                       return ret;
> +
> +               }
> +               case MA_UNF_INT:
> +               {
> +                       int val = *(int*)(&self->m_data);
> +                       return PyLong_FromLong(val);
> +               }
> +               case MA_UNF_IVEC2:
> +               case MA_UNF_IVEC3:
> +               case MA_UNF_IVEC4:
> +               {
> +                       int* val = static_cast<int*>(self->m_data);
> +                       PyObject* ret = PyList_New(self->m_size);
> +
> +                       for (int i=0; i<self->m_size; ++i)
> +                       {
> +                               PyList_SetItem(ret, i,
> PyLong_FromLong(val[i]));
> +                       }
> +
> +                       return ret;
> +               }
> +               case MA_UNF_SAMPLER2D:
> +                       PyErr_SetString(PyExc_AttributeError, "Sampler2D
> value not yet supported");
> +                       return NULL;
> +               default:
> +                       // Should never happen
> +                       PyErr_SetString(PyExc_AttributeError, "invalid type
> for uniform, internal error");
> +                       return NULL;
> +       }
> +}
> +
> +int KX_PythonUniform::pyattr_set_value(void *self_v, const
> KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
> +{
> +       KX_PythonUniform *self = static_cast<KX_PythonUniform*>(self_v);
> +
> +       switch(self->m_type)
> +       {
> +               case MA_UNF_FLOAT:
> +               {
> +                       if (!PyFloat_Check(value))
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "float
> uniform type requires a float value");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       *(float*)(&self->m_data) = PyFloat_AsDouble(value);
> +                       return PY_SET_ATTR_SUCCESS;
> +               }
> +               case MA_UNF_VEC2:
> +               case MA_UNF_VEC3:
> +               case MA_UNF_VEC4:
> +               {
> +                       if (!PySequence_Check(value))
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "vector
> uniform types require a sequence of floats");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       if (PySequence_Size(value) != self->m_size)
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "not
> enough values in the sequence");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       for (int i=0; i<self->m_size; ++i)
> +                       {
> +                               PyObject* val =
> PySequence_Fast_GET_ITEM(value, i);
> +                               if (!PyFloat_Check(val))
> +                               {
> +                                       PyErr_SetString(PyExc_ValueError,
> "vector uniform types require a sequence of floats");
> +                                       return PY_SET_ATTR_FAIL;
> +                               }
> +                               ((float*)self->m_data)[i] =
> PyFloat_AsDouble(val);
> +                       }
> +
> +                       return PY_SET_ATTR_SUCCESS;
> +               }
> +               case MA_UNF_INT:
> +               {
> +                       if (!PyLong_Check(value))
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "integer
> uniform type requires an integer value");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       *(int*)(&self->m_data) = PyLong_AsLong(value);
> +                       return PY_SET_ATTR_SUCCESS;
> +               }
> +               case MA_UNF_IVEC2:
> +               case MA_UNF_IVEC3:
> +               case MA_UNF_IVEC4:
> +               {
> +                       if (!PySequence_Check(value))
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "integer
> vector uniform types require a sequence of integers");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       if (PySequence_Size(value) != self->m_size)
> +                       {
> +                               PyErr_SetString(PyExc_ValueError, "not
> enough values in the sequence");
> +                               return PY_SET_ATTR_FAIL;
> +                       }
> +
> +                       for (int i=0; i<self->m_size; ++i)
> +                       {
> +                               PyObject* val =
> PySequence_Fast_GET_ITEM(value, i);
> +                               if (!PyLong_Check(val))
> +                               {
> +                                       PyErr_SetString(PyExc_ValueError,
> "integer vector uniform types require a sequence of integers");
> +                                       return PY_SET_ATTR_FAIL;
> +                               }
> +                               ((int*)self->m_data)[i] =
> PyLong_AsLong(val);
> +                       }
> +
> +                       return PY_SET_ATTR_SUCCESS;
> +               }
> +               case MA_UNF_SAMPLER2D:
> +                       PyErr_SetString(PyExc_AttributeError, "Sampler2D
> value not yet supported");
> +                       return PY_SET_ATTR_FAIL;
> +               default:
> +                       // Should never happen
> +                       PyErr_SetString(PyExc_AttributeError, "invalid type
> for uniform, internal error");
> +                       return PY_SET_ATTR_FAIL;
> +       }
> +}
> +
>  #endif // ndef DISABLE_PYTHON
> \ No newline at end of file
>
> Modified:
> branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h
> ===================================================================
> --- branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h
>      2010-08-13 23:08:22 UTC (rev 31330)
> +++ branches/soc-2010-moguri-2/source/gameengine/Ketsji/KX_PythonShaders.h
>      2010-08-14 01:33:26 UTC (rev 31331)
> @@ -70,6 +70,7 @@
>
>        STR_String m_name;
>        short m_type;
> +       int m_size;
>        void *m_data;
>
>  public:
> @@ -85,6 +86,9 @@
>        }
>
>        static PyObject* py_uniform_new(PyTypeObject *type, PyObject *args,
> PyObject *kwds);
> +
> +       static PyObject*        pyattr_get_value(void *self_v, const
> KX_PYATTRIBUTE_DEF *attrdef);
> +       static int                      pyattr_set_value(void *self_v,
> const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
>  };
>
>  #endif //ndef DISABLE_PYTHON
>
>
> _______________________________________________
> Bf-blender-cvs mailing list
> Bf-blender-cvs at blender.org
> http://lists.blender.org/mailman/listinfo/bf-blender-cvs
>


More information about the Bf-committers mailing list