Browse Source

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...
pull/117/head
Adam Johnson 6 years ago
parent
commit
2a49634e43
Signed by: Hoikas
GPG Key ID: 0B6515D6FF6F271E
  1. 2
      korlib/CMakeLists.txt
  2. 111
      korlib/buffer.cpp
  3. 37
      korlib/buffer.h
  4. 2
      korlib/module.cpp
  5. 38
      korlib/texture.cpp
  6. 5
      korman/korlib/texture.py

2
korlib/CMakeLists.txt

@ -18,7 +18,6 @@ endif()
# Da files # Da files
set(korlib_HEADERS set(korlib_HEADERS
buffer.h
bumpmap.h bumpmap.h
korlib.h korlib.h
sound.h sound.h
@ -26,7 +25,6 @@ set(korlib_HEADERS
) )
set(korlib_SOURCES set(korlib_SOURCES
buffer.cpp
bumpmap.cpp bumpmap.cpp
module.cpp module.cpp
sound.cpp sound.cpp

111
korlib/buffer.cpp

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}
};

37
korlib/buffer.h

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

2
korlib/module.cpp

@ -14,7 +14,6 @@
* along with Korman. If not, see <http://www.gnu.org/licenses/>. * along with Korman. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "buffer.h"
#include "bumpmap.h" #include "bumpmap.h"
#include "sound.h" #include "sound.h"
#include "texture.h" #include "texture.h"
@ -44,7 +43,6 @@ PyMODINIT_FUNC PyInit__korlib() {
PyObject* module = PyModule_Create(&korlib_Module); PyObject* module = PyModule_Create(&korlib_Module);
// Module classes... // Module classes...
PyModule_AddObject(module, "Buffer", Init_pyBuffer_Type());
PyModule_AddObject(module, "GLTexture", Init_pyGLTexture_Type()); PyModule_AddObject(module, "GLTexture", Init_pyGLTexture_Type());
return module; return module;

38
korlib/texture.cpp

@ -15,7 +15,6 @@
*/ */
#include "texture.h" #include "texture.h"
#include "buffer.h"
#ifdef _WIN32 #ifdef _WIN32
# define WIN32_LEAN_AND_MEAN # 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); _LevelData data = _get_level_data(self, level, bgra, report);
if (fast) 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. // OpenGL returns a flipped image, so we must reflip it.
size_t row_stride = data.m_width * 4; 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; 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); return PyBytes_FromStringAndSize((char*)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;
} }
static PyMethodDef pyGLTexture_Methods[] = { static PyMethodDef pyGLTexture_Methods[] = {
@ -372,7 +339,6 @@ static PyMethodDef pyGLTexture_Methods[] = {
{ _pycs("generate_mipmap"), (PyCFunction)pyGLTexture_generate_mipmap, METH_NOARGS, NULL }, { _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("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 } { NULL, NULL, 0, NULL }
}; };

5
korman/korlib/texture.py

@ -181,8 +181,3 @@ class GLTexture:
# texture in a single pixel?" # texture in a single pixel?"
# :) # :)
return max(numLevels - 2, 2) 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)

Loading…
Cancel
Save