4
4
mirror of https://github.com/H-uru/korman.git synced 2025-07-14 02:27:36 -04:00

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...
This commit is contained in:
2018-09-02 17:59:46 -04:00
parent 654af0e56a
commit 2a49634e43
6 changed files with 2 additions and 193 deletions

View File

@ -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

View File

@ -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;
}
};

View File

@ -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

View File

@ -14,7 +14,6 @@
* along with Korman. If not, see <http://www.gnu.org/licenses/>.
*/
#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;

View File

@ -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 }
};