4
4
5
5
#define MAX (x, y ) (((x) > (y)) ? (x) : (y))
6
6
7
- enum
7
+ enum VertexFormat
8
8
{
9
9
kVertexFormatFloat ,
10
10
kVertexFormatFloat16 ,
@@ -27,46 +27,42 @@ PyObject *unpack_vertexdata(PyObject *self, PyObject *args)
27
27
uint32_t m_VertexCount;
28
28
uint8_t swap;
29
29
// char format;
30
- PyObject *vertexDataPy;
31
30
Py_buffer vertexDataView;
32
- uint8_t *vertexData; // m_VertexData.m_DataSize
33
31
uint32_t m_StreamOffset;
34
32
uint32_t m_StreamStride;
35
33
uint32_t m_ChannelOffset;
36
34
uint32_t m_ChannelDimension;
37
- Py_ssize_t vertexDataSize;
38
35
39
- if (!PyArg_ParseTuple (args, " OiIIIIIb" , &vertexDataPy, &componentByteSize, &m_VertexCount, &m_StreamOffset, &m_StreamStride, &m_ChannelOffset, &m_ChannelDimension, &swap))
40
- return NULL ;
41
-
42
- // check type of src
43
- if (!PyObject_CheckBuffer (vertexDataPy))
36
+ if (!PyArg_ParseTuple (args, " y*iIIIIIb" , &vertexDataView, &componentByteSize, &m_VertexCount, &m_StreamOffset, &m_StreamStride, &m_ChannelOffset, &m_ChannelDimension, &swap))
44
37
{
45
- PyErr_SetString (PyExc_TypeError, " src must be of a type that supports the buffer protocol" );
46
- return NULL ;
47
- }
48
- // get buffer from src
49
- if (PyObject_GetBuffer (vertexDataPy, &vertexDataView, PyBUF_SIMPLE) == -1 )
50
- {
51
- PyErr_SetString (PyExc_ValueError, " Failed to get buffer from src" );
38
+ if (vertexDataView.buf )
39
+ {
40
+ PyBuffer_Release (&vertexDataView);
41
+ }
52
42
return NULL ;
53
43
}
54
- vertexData = ( uint8_t *)vertexDataView. buf ;
55
- vertexDataSize = vertexDataView.len ;
44
+
45
+ uint8_t *vertexData = ( uint8_t *) vertexDataView.buf ;
56
46
57
47
Py_ssize_t componentBytesLength = m_VertexCount * m_ChannelDimension * componentByteSize;
58
- uint8_t *componentBytes = (uint8_t *)PyMem_Malloc (componentBytesLength + 1 );
59
- componentBytes[componentBytesLength] = 0 ;
60
48
61
49
// check if max values are ok
62
50
uint32_t maxVertexDataAccess = (m_VertexCount - 1 ) * m_StreamStride + m_ChannelOffset + m_StreamOffset + componentByteSize * (m_ChannelDimension - 1 ) + componentByteSize;
63
- if (maxVertexDataAccess > vertexDataSize )
51
+ if (maxVertexDataAccess > vertexDataView. len )
64
52
{
65
53
PyBuffer_Release (&vertexDataView);
66
54
PyErr_SetString (PyExc_ValueError, " Vertex data access out of bounds" );
67
55
return NULL ;
68
56
}
69
57
58
+ PyObject *res = PyBytes_FromStringAndSize (nullptr , componentBytesLength);
59
+ if (!res)
60
+ {
61
+ PyBuffer_Release (&vertexDataView);
62
+ return NULL ;
63
+ }
64
+ uint8_t *componentBytes = (uint8_t *)PyBytes_AS_STRING (res);
65
+
70
66
for (uint32_t v = 0 ; v < m_VertexCount; v++)
71
67
{
72
68
uint32_t vertexOffset = m_StreamOffset + m_ChannelOffset + m_StreamStride * v;
@@ -99,8 +95,6 @@ PyObject *unpack_vertexdata(PyObject *self, PyObject *args)
99
95
}
100
96
}
101
97
102
- PyObject *res = PyByteArray_FromStringAndSize ((const char *)componentBytes, componentBytesLength);
103
- PyMem_Free (componentBytes);
104
98
PyBuffer_Release (&vertexDataView);
105
99
return res;
106
100
0 commit comments