Skip to content

Commit a5de61e

Browse files
committed
Mesh.cpp - parse directly into buffer and be smarter about result creation
1 parent 68c8e36 commit a5de61e

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

UnityPyBoost/Mesh.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
66

7-
enum
7+
enum VertexFormat
88
{
99
kVertexFormatFloat,
1010
kVertexFormatFloat16,
@@ -27,46 +27,42 @@ PyObject *unpack_vertexdata(PyObject *self, PyObject *args)
2727
uint32_t m_VertexCount;
2828
uint8_t swap;
2929
// char format;
30-
PyObject *vertexDataPy;
3130
Py_buffer vertexDataView;
32-
uint8_t *vertexData; // m_VertexData.m_DataSize
3331
uint32_t m_StreamOffset;
3432
uint32_t m_StreamStride;
3533
uint32_t m_ChannelOffset;
3634
uint32_t m_ChannelDimension;
37-
Py_ssize_t vertexDataSize;
3835

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))
4437
{
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+
}
5242
return NULL;
5343
}
54-
vertexData = (uint8_t *)vertexDataView.buf;
55-
vertexDataSize = vertexDataView.len;
44+
45+
uint8_t *vertexData = (uint8_t *)vertexDataView.buf;
5646

5747
Py_ssize_t componentBytesLength = m_VertexCount * m_ChannelDimension * componentByteSize;
58-
uint8_t *componentBytes = (uint8_t *)PyMem_Malloc(componentBytesLength + 1);
59-
componentBytes[componentBytesLength] = 0;
6048

6149
// check if max values are ok
6250
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)
6452
{
6553
PyBuffer_Release(&vertexDataView);
6654
PyErr_SetString(PyExc_ValueError, "Vertex data access out of bounds");
6755
return NULL;
6856
}
6957

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+
7066
for (uint32_t v = 0; v < m_VertexCount; v++)
7167
{
7268
uint32_t vertexOffset = m_StreamOffset + m_ChannelOffset + m_StreamStride * v;
@@ -99,8 +95,6 @@ PyObject *unpack_vertexdata(PyObject *self, PyObject *args)
9995
}
10096
}
10197

102-
PyObject *res = PyByteArray_FromStringAndSize((const char *)componentBytes, componentBytesLength);
103-
PyMem_Free(componentBytes);
10498
PyBuffer_Release(&vertexDataView);
10599
return res;
106100

0 commit comments

Comments
 (0)