From 2a49634e434c600272c4018bb16c5de64ce16b57 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 2 Sep 2018 17:59:46 -0400 Subject: [PATCH] Update korlib for texture caching Previously, the C korlib used a custom buffer class to avoid a memcpy operation. However, pyMipmap expects binary string (PyBytes) objects, so this changes the code to use them. Future work would be to continue using PyBytes and removing the copy. For now, there are bigger fish to fry... --- korlib/CMakeLists.txt | 2 - korlib/buffer.cpp | 111 --------------------------------------- korlib/buffer.h | 37 ------------- korlib/module.cpp | 2 - korlib/texture.cpp | 38 +------------- korman/korlib/texture.py | 5 -- 6 files changed, 2 insertions(+), 193 deletions(-) delete mode 100644 korlib/buffer.cpp delete mode 100644 korlib/buffer.h diff --git a/korlib/CMakeLists.txt b/korlib/CMakeLists.txt index a00a011..b0fc144 100644 --- a/korlib/CMakeLists.txt +++ b/korlib/CMakeLists.txt @@ -18,7 +18,6 @@ endif() # Da files set(korlib_HEADERS - buffer.h bumpmap.h korlib.h sound.h @@ -26,7 +25,6 @@ set(korlib_HEADERS ) set(korlib_SOURCES - buffer.cpp bumpmap.cpp module.cpp sound.cpp diff --git a/korlib/buffer.cpp b/korlib/buffer.cpp deleted file mode 100644 index 120f526..0000000 --- a/korlib/buffer.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* This file is part of Korman. - * - * Korman 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 3 of the License, or - * (at your option) any later version. - * - * Korman 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 Korman. If not, see . - */ - -#include "buffer.h" - -extern "C" { - -static void pyBuffer_dealloc(pyBuffer* self) { - delete[] self->m_buffer; - Py_TYPE(self)->tp_free((PyObject*)self); -} - -static PyObject* pyBuffer_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { - PyErr_SetString(PyExc_RuntimeError, "Buffers cannot be created by mere mortals"); - return NULL; -} - -PyTypeObject pyBuffer_Type = { - PyVarObject_HEAD_INIT(NULL, 0) - "_korlib.Buffer", /* tp_name */ - sizeof(pyBuffer), /* tp_basicsize */ - 0, /* tp_itemsize */ - - (destructor)pyBuffer_dealloc, /* tp_dealloc */ - NULL, /* tp_print */ - NULL, /* tp_getattr */ - NULL, /* tp_setattr */ - NULL, /* tp_compare */ - NULL, /* tp_repr */ - NULL, /* tp_as_number */ - NULL, /* tp_as_sequence */ - NULL, /* tp_as_mapping */ - NULL, /* tp_hash */ - NULL, /* tp_call */ - NULL, /* tp_str */ - NULL, /* tp_getattro */ - NULL, /* tp_setattro */ - NULL, /* tp_as_buffer */ - - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ - "Buffer", /* tp_doc */ - - NULL, /* tp_traverse */ - NULL, /* tp_clear */ - NULL, /* tp_richcompare */ - 0, /* tp_weaklistoffset */ - NULL, /* tp_iter */ - NULL, /* tp_iternext */ - - NULL, /* tp_methods */ - NULL, /* tp_members */ - NULL, /* tp_getset */ - NULL, /* tp_base */ - NULL, /* tp_dict */ - NULL, /* tp_descr_get */ - NULL, /* tp_descr_set */ - 0, /* tp_dictoffset */ - - NULL, /* tp_init */ - NULL, /* tp_alloc */ - pyBuffer_new, /* tp_new */ - NULL, /* tp_free */ - NULL, /* tp_is_gc */ - - NULL, /* tp_bases */ - NULL, /* tp_mro */ - NULL, /* tp_cache */ - NULL, /* tp_subclasses */ - NULL, /* tp_weaklist */ - - NULL, /* tp_del */ - 0, /* tp_version_tag */ - NULL, /* tp_finalize */ -}; - -PyObject* Init_pyBuffer_Type() { - if (PyType_Ready(&pyBuffer_Type) < 0) - return NULL; - - Py_INCREF(&pyBuffer_Type); - return (PyObject*)&pyBuffer_Type; -} - -int pyBuffer_Check(PyObject* obj) { - if (obj->ob_type == &pyBuffer_Type - || PyType_IsSubtype(obj->ob_type, &pyBuffer_Type)) - return 1; - return 0; -} - -PyObject* pyBuffer_Steal(uint8_t* buffer, size_t size) { - pyBuffer* obj = PyObject_New(pyBuffer, &pyBuffer_Type); - obj->m_buffer = buffer; - obj->m_size = size; - return (PyObject*)obj; -} - -}; diff --git a/korlib/buffer.h b/korlib/buffer.h deleted file mode 100644 index 33b739b..0000000 --- a/korlib/buffer.h +++ /dev/null @@ -1,37 +0,0 @@ -/* This file is part of Korman. - * - * Korman 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 3 of the License, or - * (at your option) any later version. - * - * Korman 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 Korman. If not, see . - */ - -#ifndef _KORLIB_BUFFER_H -#define _KORLIB_BUFFER_H - -#include "korlib.h" - -extern "C" { - -typedef struct { - PyObject_HEAD - uint8_t* m_buffer; - size_t m_size; -} pyBuffer; - -extern PyTypeObject pyBuffer_Type; -PyObject* Init_pyBuffer_Type(); -int pyBuffer_Check(PyObject*); -PyObject* pyBuffer_Steal(uint8_t*, size_t); - -} - -#endif // _KORLIB_BUFFER_H diff --git a/korlib/module.cpp b/korlib/module.cpp index 8850715..4f6d257 100644 --- a/korlib/module.cpp +++ b/korlib/module.cpp @@ -14,7 +14,6 @@ * along with Korman. If not, see . */ -#include "buffer.h" #include "bumpmap.h" #include "sound.h" #include "texture.h" @@ -44,7 +43,6 @@ PyMODINIT_FUNC PyInit__korlib() { PyObject* module = PyModule_Create(&korlib_Module); // Module classes... - PyModule_AddObject(module, "Buffer", Init_pyBuffer_Type()); PyModule_AddObject(module, "GLTexture", Init_pyGLTexture_Type()); return module; diff --git a/korlib/texture.cpp b/korlib/texture.cpp index 0fe6615..049c94e 100644 --- a/korlib/texture.cpp +++ b/korlib/texture.cpp @@ -15,7 +15,6 @@ */ #include "texture.h" -#include "buffer.h" #ifdef _WIN32 # define WIN32_LEAN_AND_MEAN @@ -302,7 +301,7 @@ static PyObject* pyGLTexture_get_level_data(pyGLTexture* self, PyObject* args, P _LevelData data = _get_level_data(self, level, bgra, report); if (fast) - return pyBuffer_Steal(data.m_data, data.m_dataSize); + return PyBytes_FromStringAndSize((char*)data.m_data, data.m_dataSize); // OpenGL returns a flipped image, so we must reflip it. size_t row_stride = data.m_width * 4; @@ -331,39 +330,7 @@ static PyObject* pyGLTexture_get_level_data(pyGLTexture* self, PyObject* args, P data.m_data[i + 3] = (data.m_data[i + 0] + data.m_data[i + 1] + data.m_data[i + 2]) / 3; } - return pyBuffer_Steal(data.m_data, data.m_dataSize); -} - -static PyObject* pyGLTexture_store_in_mipmap(pyGLTexture* self, PyObject* args) { - pyMipmap* pymipmap; - PyObject* levels; - size_t compression; - if (!PyArg_ParseTuple(args, "OOn", &pymipmap, &levels, &compression) || !PySequence_Check(levels)) { - PyErr_SetString(PyExc_TypeError, "store_in_mipmap expects a plMipmap, sequence of Buffer and int"); - return NULL; - } - - // Since we actually have no way of knowing if that really is a pyMipmap... - plMipmap* mipmap = plMipmap::Convert(pymipmap->fThis, false); - if (!mipmap) { - PyErr_SetString(PyExc_TypeError, "store_in_mipmap expects a plMipmap, sequence of Buffer and int"); - return NULL; - } - - for (Py_ssize_t i = 0; i < PySequence_Size(levels); ++i) { - pyBuffer* item = (pyBuffer*)PySequence_GetItem(levels, i); - if (!pyBuffer_Check((PyObject*)item)) { - PyErr_SetString(PyExc_TypeError, "store_in_mipmap expects a plMipmap, sequence of Buffer and int"); - return NULL; - } - - if (compression == plBitmap::kDirectXCompression) - mipmap->CompressImage(i, item->m_buffer, item->m_size); - else - mipmap->setLevelData(i, item->m_buffer, item->m_size); - } - - Py_RETURN_NONE; + return PyBytes_FromStringAndSize((char*)data.m_data, data.m_dataSize); } static PyMethodDef pyGLTexture_Methods[] = { @@ -372,7 +339,6 @@ static PyMethodDef pyGLTexture_Methods[] = { { _pycs("generate_mipmap"), (PyCFunction)pyGLTexture_generate_mipmap, METH_NOARGS, NULL }, { _pycs("get_level_data"), (PyCFunction)pyGLTexture_get_level_data, METH_KEYWORDS | METH_VARARGS, NULL }, - { _pycs("store_in_mipmap"), (PyCFunction)pyGLTexture_store_in_mipmap, METH_VARARGS, NULL }, { NULL, NULL, 0, NULL } }; diff --git a/korman/korlib/texture.py b/korman/korlib/texture.py index 194add9..10d9388 100644 --- a/korman/korlib/texture.py +++ b/korman/korlib/texture.py @@ -181,8 +181,3 @@ class GLTexture: # texture in a single pixel?" # :) return max(numLevels - 2, 2) - - def store_in_mipmap(self, mipmap, data, compression): - func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel - for i, level in enumerate(data): - func(i, level)