| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-18 08:08:12 +11:00
										 |  |  | /** \file
 | 
					
						
							|  |  |  |  * \ingroup bpygpu | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * - Use ``bpygpu_`` for local API. | 
					
						
							|  |  |  |  * - Use ``BPyGPU`` for public API. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <Python.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "GPU_element.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_math.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "../generic/py_capi_utils.h"
 | 
					
						
							|  |  |  | #include "../generic/python_utildefines.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-03 00:55:07 +11:00
										 |  |  | #include "gpu_py_api.h"
 | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | #include "gpu_py_element.h" /* own include */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name IndexBuf Type
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args, PyObject *kwds) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-03 01:08:26 +11:00
										 |  |  | 	BPYGPU_IS_INIT_OR_ERROR_OBJ; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 	const char *error_prefix = "IndexBuf.__new__"; | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 	bool ok = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct { | 
					
						
							|  |  |  | 		GPUPrimType type_id; | 
					
						
							|  |  |  | 		PyObject *seq; | 
					
						
							|  |  |  | 	} params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	uint verts_per_prim; | 
					
						
							|  |  |  | 	uint index_len; | 
					
						
							|  |  |  | 	GPUIndexBufBuilder builder; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	static const char *_keywords[] = {"type", "seq", NULL}; | 
					
						
							|  |  |  | 	static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0}; | 
					
						
							| 
									
										
										
										
											2019-01-03 01:08:26 +11:00
										 |  |  | 	if (!_PyArg_ParseTupleAndKeywordsFast( | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 	        args, kwds, &_parser, | 
					
						
							|  |  |  | 	        bpygpu_ParsePrimType, ¶ms.type_id, | 
					
						
							|  |  |  | 	        ¶ms.seq)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	verts_per_prim = GPU_indexbuf_primitive_len(params.type_id); | 
					
						
							|  |  |  | 	if (verts_per_prim == -1) { | 
					
						
							| 
									
										
										
										
											2018-10-01 08:42:26 +10:00
										 |  |  | 		PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  | 		             "The argument 'type' must be " | 
					
						
							|  |  |  | 		             "'POINTS', 'LINES', 'TRIS' or 'LINES_ADJ'"); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (PyObject_CheckBuffer(params.seq)) { | 
					
						
							|  |  |  | 		Py_buffer pybuffer; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (PyObject_GetBuffer(params.seq, &pybuffer, PyBUF_FORMAT | PyBUF_ND) == -1) { | 
					
						
							|  |  |  | 			/* PyObject_GetBuffer already handles error messages. */ | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (pybuffer.ndim != 1 && pybuffer.shape[1] != verts_per_prim) { | 
					
						
							|  |  |  | 			PyErr_Format(PyExc_ValueError, | 
					
						
							|  |  |  | 			             "Each primitive must exactly %d indices", | 
					
						
							|  |  |  | 			             verts_per_prim); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-06 01:15:15 -03:00
										 |  |  | 		if (pybuffer.itemsize != 4 || | 
					
						
							| 
									
										
										
										
											2018-10-08 08:37:32 +11:00
										 |  |  | 		    PyC_StructFmt_type_is_float_any(PyC_StructFmt_type_from_str(pybuffer.format))) | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			PyErr_Format(PyExc_ValueError, | 
					
						
							| 
									
										
										
										
											2018-10-06 01:15:15 -03:00
										 |  |  | 			             "Each index must be an 4-bytes integer value"); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		index_len = pybuffer.shape[0]; | 
					
						
							|  |  |  | 		if (pybuffer.ndim != 1) { | 
					
						
							|  |  |  | 			index_len *= pybuffer.shape[1]; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 21:15:42 +10:00
										 |  |  | 		/* The `vertex_len` parameter is only used for asserts in the Debug build. */ | 
					
						
							|  |  |  | 		/* Not very useful in python since scripts are often tested in Release build. */ | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 		/* Use `INT_MAX` instead of the actual number of vertices. */ | 
					
						
							|  |  |  | 		GPU_indexbuf_init( | 
					
						
							|  |  |  | 		        &builder, params.type_id, index_len, INT_MAX); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if 0
 | 
					
						
							|  |  |  | 		uint *buf = pybuffer.buf; | 
					
						
							|  |  |  | 		for (uint i = index_len; i--; buf++) { | 
					
						
							|  |  |  | 			GPU_indexbuf_add_generic_vert(&builder, *buf); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-10-24 10:57:16 +02:00
										 |  |  | 		memcpy(builder.data, pybuffer.buf, index_len * sizeof(*builder.data)); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 		builder.index_len = index_len; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		PyBuffer_Release(&pybuffer); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 		PyObject *seq_fast = PySequence_Fast(params.seq, error_prefix); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (seq_fast == NULL) { | 
					
						
							|  |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const uint seq_len = PySequence_Fast_GET_SIZE(seq_fast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		PyObject **seq_items = PySequence_Fast_ITEMS(seq_fast); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		index_len = seq_len * verts_per_prim; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-27 21:15:42 +10:00
										 |  |  | 		/* The `vertex_len` parameter is only used for asserts in the Debug build. */ | 
					
						
							|  |  |  | 		/* Not very useful in python since scripts are often tested in Release build. */ | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 		/* Use `INT_MAX` instead of the actual number of vertices. */ | 
					
						
							|  |  |  | 		GPU_indexbuf_init( | 
					
						
							|  |  |  | 		        &builder, params.type_id, index_len, INT_MAX); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (verts_per_prim == 1) { | 
					
						
							|  |  |  | 			for (uint i = 0; i < seq_len; i++) { | 
					
						
							|  |  |  | 				GPU_indexbuf_add_generic_vert( | 
					
						
							|  |  |  | 				        &builder, PyC_Long_AsU32(seq_items[i])); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 			int values[4]; | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 			for (uint i = 0; i < seq_len; i++) { | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 				PyObject *seq_fast_item = PySequence_Fast(seq_items[i], error_prefix); | 
					
						
							|  |  |  | 				if (seq_fast_item == NULL) { | 
					
						
							|  |  |  | 					PyErr_Format(PyExc_TypeError, | 
					
						
							|  |  |  | 					             "%s: expected a sequence, got %s", | 
					
						
							|  |  |  | 					             error_prefix, Py_TYPE(seq_items[i])->tp_name); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 					ok = false; | 
					
						
							|  |  |  | 					goto finally; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 				ok = PyC_AsArray_FAST( | 
					
						
							|  |  |  | 				        values, seq_fast_item, verts_per_prim, | 
					
						
							|  |  |  | 				        &PyLong_Type, false, error_prefix) == 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (ok) { | 
					
						
							|  |  |  | 					for (uint j = 0; j < verts_per_prim; j++) { | 
					
						
							|  |  |  | 						GPU_indexbuf_add_generic_vert(&builder, values[j]); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2018-10-10 13:35:29 -03:00
										 |  |  | 				Py_DECREF(seq_fast_item); | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (PyErr_Occurred()) { | 
					
						
							|  |  |  | 			ok = false; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | finally: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Py_DECREF(seq_fast); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ok == false) { | 
					
						
							|  |  |  | 		MEM_freeN(builder.data); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return BPyGPUIndexBuf_CreatePyObject(GPU_indexbuf_build(&builder)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void bpygpu_IndexBuf_dealloc(BPyGPUIndexBuf *self) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPU_indexbuf_discard(self->elem); | 
					
						
							|  |  |  | 	Py_TYPE(self)->tp_free(self); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyDoc_STRVAR(py_gpu_element_doc, | 
					
						
							| 
									
										
										
										
											2018-11-13 14:55:15 +01:00
										 |  |  | ".. class:: GPUIndexBuf(type, seq)\n" | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2018-11-14 09:04:24 +11:00
										 |  |  | "   Contains an index buffer.\n" | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | "\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 14:55:15 +01:00
										 |  |  | "   :param type: One of these primitive types: {\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 13:33:09 +01:00
										 |  |  | "      `POINTS`,\n" | 
					
						
							|  |  |  | "      `LINES`,\n" | 
					
						
							|  |  |  | "      `TRIS`,\n" | 
					
						
							|  |  |  | "      `LINE_STRIP_ADJ` }\n" | 
					
						
							| 
									
										
										
										
											2018-11-13 14:55:15 +01:00
										 |  |  | "   :type type: `str`\n" | 
					
						
							| 
									
										
										
										
											2018-11-14 09:04:24 +11:00
										 |  |  | "   :param seq: Indices this index buffer will contain.\n" | 
					
						
							|  |  |  | "      Whether a 1D or 2D sequence is required depends on the type.\n" | 
					
						
							|  |  |  | "      Optionally the sequence can support the buffer protocol.\n" | 
					
						
							|  |  |  | "   :type seq: 1D or 2D sequence\n" | 
					
						
							| 
									
										
										
										
											2018-09-27 00:53:45 -03:00
										 |  |  | ); | 
					
						
							|  |  |  | PyTypeObject BPyGPUIndexBuf_Type = { | 
					
						
							|  |  |  | 	PyVarObject_HEAD_INIT(NULL, 0) | 
					
						
							|  |  |  | 	.tp_name = "GPUIndexBuf", | 
					
						
							|  |  |  | 	.tp_basicsize = sizeof(BPyGPUIndexBuf), | 
					
						
							|  |  |  | 	.tp_dealloc = (destructor)bpygpu_IndexBuf_dealloc, | 
					
						
							|  |  |  | 	.tp_flags = Py_TPFLAGS_DEFAULT, | 
					
						
							|  |  |  | 	.tp_doc = py_gpu_element_doc, | 
					
						
							|  |  |  | 	.tp_new = bpygpu_IndexBuf_new, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Public API
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *BPyGPUIndexBuf_CreatePyObject(GPUIndexBuf *elem) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	BPyGPUIndexBuf *self; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	self = PyObject_New(BPyGPUIndexBuf, &BPyGPUIndexBuf_Type); | 
					
						
							|  |  |  | 	self->elem = elem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return (PyObject *)self; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** \} */ |