Browse Source

Pure Python korlib

Yeah, korlib was a bit faster than this implementation, but, honestly,
having a second python module made life more difficult than it needed to
be. This fixes that.
pull/6/head
Adam Johnson 11 years ago
parent
commit
f3b7ae3ad5
  1. 3
      .gitignore
  2. 30
      korlib/CMakeLists.txt
  3. 210
      korlib/generate_mipmap.cpp
  4. 48
      korlib/module.cpp
  5. 27
      korlib/pyMipmap.h
  6. 82
      korlib/utils.hpp
  7. 5
      korman/exporter/explosions.py
  8. 116
      korman/exporter/material.py

3
.gitignore vendored

@ -31,6 +31,3 @@ pip-log.txt
*.komodoproject
*.project
*.pydevproject
# Korlib build
korlib/build

30
korlib/CMakeLists.txt

@ -1,30 +0,0 @@
project(korman)
cmake_minimum_required(VERSION 2.8.9)
find_package(HSPlasma REQUIRED)
find_package(OpenGL REQUIRED)
find_package(PythonLibs REQUIRED)
include_directories(${HSPlasma_INCLUDE_DIRS})
include_directories(${OPENGL_INCLUDE_DIR})
include_directories(${PYTHON_INCLUDE_DIR})
set(korlib_HEADERS
pyMipmap.h
utils.hpp
)
set(korlib_SOURCES
generate_mipmap.cpp
module.cpp
)
add_library(korlib SHARED ${korlib_HEADERS} ${korlib_SOURCES})
target_link_libraries(korlib HSPlasma ${OPENGL_LIBRARIES} ${PYTHON_LIBRARIES})
if(WIN32)
set_target_properties(korlib PROPERTIES SUFFIX ".pyd")
endif(WIN32)
source_group("Header Files" FILES ${korlib_HEADERS})
source_group("Source Files" FILES ${korlib_SOURCES})

210
korlib/generate_mipmap.cpp

@ -1,210 +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 <algorithm>
#include <cmath>
#include <cstdint>
#include <tuple>
#ifdef _WINDOWS
# define NOMINMAX
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# define GL_GENERATE_MIPMAP 0x8191
#endif // _WINDOWS
#include <gl/gl.h>
#include <ResManager/plFactory.h>
#include <PRP/Surface/plMipmap.h>
#include <Python.h>
#include "pyMipmap.h"
#include "utils.hpp"
// ========================================================================
class gl_loadimage
{
bool m_weLoadedIt;
bool m_success;
GLint m_genMipMapState;
korlib::pyref m_image;
public:
gl_loadimage(const korlib::pyref& image) : m_success(true), m_image(image)
{
size_t bindcode = korlib::getattr<size_t>(image, "bindcode");
m_weLoadedIt = (bindcode == 0);
if (m_weLoadedIt) {
m_success = (korlib::call_method<size_t>(image, "gl_load") == 0);
bindcode = korlib::getattr<size_t>(image, "bindcode");
}
if (m_success) {
glBindTexture(GL_TEXTURE_2D, bindcode);
}
// We want to gen mipmaps
// GIANTLY GNARLY DISCLAIMER:
// This requires OpenGL 1.4, which is above Windows' "built-in" headers (1.1)
// It was also deprecated in 3.0, and removed in 3.1.
// In other words, we should probably use glGenerateMipmap (3.0) or Blender's scale function
glGetTexParameteriv(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, &m_genMipMapState);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
}
~gl_loadimage()
{
if (m_success && m_weLoadedIt)
korlib::call_method<size_t>(m_image, "gl_free");
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, m_genMipMapState);
}
bool success() const { return m_success; }
};
// ========================================================================
typedef std::tuple<size_t, size_t> imagesize_t;
/** Gets the dimensions of a Blender Image in pixels (WxH) */
static imagesize_t get_image_size(PyObject* image)
{
korlib::pyref size = PyObject_GetAttrString(image, "size");
size_t width = PyLong_AsSize_t(PySequence_GetItem(size, 0));
size_t height = PyLong_AsSize_t(PySequence_GetItem(size, 1));
return std::make_tuple(width, height);
}
static void resize_image(PyObject* image, size_t width, size_t height)
{
korlib::pyref _w = PyLong_FromSize_t(width);
korlib::pyref _h = PyLong_FromSize_t(height);
korlib::pyref callable = korlib::getattr<PyObject*>(image, "scale");
korlib::pyref result = PyObject_CallFunctionObjArgs(callable, _w, _h);
}
// ========================================================================
static void stuff_mip_level(plMipmap* mipmap, size_t level, PyObject* image, bool calcAlpha)
{
// How big is this doggone level?
GLint width, height;
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_WIDTH, &width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, level, GL_TEXTURE_HEIGHT, &height);
print(" Level %d: %dx%d...", level, width, height);
// Grab the stuff from the place and the things
size_t dataSize = width * height * 4;
uint8_t* data = new uint8_t[dataSize]; // optimization: use stack for small images...
glGetTexImage(GL_TEXTURE_2D, level, GL_RGBA, GL_UNSIGNED_BYTE, data);
// Need to calculate alpha?
if (calcAlpha) {
uint8_t* ptr = data;
uint8_t* end = data + dataSize;
while (ptr < end) {
uint8_t r = *ptr++;
uint8_t g = *ptr++;
uint8_t b = *ptr++;
*ptr++ = (r + g + b) / 255;
}
}
// Stuff into plMipmap. Unfortunately, it's not smart enough to just work, so we have to do
// a little bit of TESTing here.
try {
mipmap->CompressImage(level, data, dataSize);
} catch (hsNotImplementedException&) {
mipmap->setLevelData(level, data, dataSize);
}
delete[] data;
}
// ========================================================================
extern "C" PyObject* generate_mipmap(PyObject*, PyObject* args)
{
// Convert some of this Python nonsense to good old C
PyObject* blTexImage = nullptr; // unchecked... better be right
PyObject* pymm = nullptr;
if (PyArg_ParseTuple(args, "OO", &blTexImage, &pymm) && blTexImage && pymm) {
// Since we can't link with PyHSPlasma easily, let's do some roundabout type-checking
korlib::pyref classindex = PyObject_CallMethod(pymm, "ClassIndex", "");
static short mipmap_classindex = plFactory::ClassIndex("plMipmap");
if (PyLong_AsLong(classindex) != mipmap_classindex) {
PyErr_SetString(PyExc_TypeError, "generate_mipmap expects a Blender ImageTexture and a plMipmap");
return nullptr;
}
} else {
PyErr_SetString(PyExc_TypeError, "generate_mipmap expects a Blender ImageTexture and a plMipmap");
return nullptr;
}
// Grab the important stuff
plMipmap* mipmap = ((pyMipmap*)pymm)->fThis;
korlib::pyref blImage = korlib::getattr<PyObject*>(blTexImage, "image");
bool makeMipMap = korlib::getattr<bool>(blTexImage, "use_mipmap");
bool useAlpha = korlib::getattr<bool>(blTexImage, "use_alpha");
bool calcAlpha = korlib::getattr<bool>(blTexImage, "use_calculate_alpha");
// Okay, so, here are the assumptions.
// We assume that the Korman Python code as already created the mipmap's key and named it appropriately
// So, if we're mipmapping nb01StoneSquareCobble.tga -> nb01StoneSquareCobble.dds as the key name
// What we now need to do:
// 1) Make sure this is a POT texture (if not, call scale on the Blender Image)
// 2) Check calcAlpha and all that rubbish--det DXT1/DXT5/uncompressed
// 3) "Create" the plMipmap--this allocates internal buffers and such
// 4) Loop through the levels, going down through the POTs and fill in the pixel data
// The reason we do this in C instead of python is because it's a lot of iterating over a lot of
// floating point data (we have to convert to RGB8888, joy). Should be faster here!
print("Exporting '%s'...", mipmap->getKey()->getName().cstr());
// Step 1: Resize to POT (if needed) -- don't rely on GLU for this because it may not suppport
// NPOT if we're being run on some kind of dinosaur...
imagesize_t dimensions = get_image_size(blImage);
size_t width = pow(2, log2(std::get<0>(dimensions)));
size_t height = pow(2, log2(std::get<1>(dimensions)));
if (std::get<0>(dimensions) != width || std::get<1>(dimensions) != height) {
print("\tImage is not a POT (%dx%d)... resizing to %dx%d", std::get<0>(dimensions),
std::get<1>(dimensions), width, height);
resize_image(blImage, width, height);
}
// Steps 2+3: Translate flags and pass to plMipmap::Create
// TODO: PNG compression for lossless images
uint8_t numLevels = (makeMipMap) ? 0 : 1; // 0 means "you figure it out"
uint8_t compType = (makeMipMap) ? plBitmap::kDirectXCompression : plBitmap::kUncompressed;
bool alphaChannel = useAlpha || calcAlpha;
mipmap->Create(width, height, numLevels, compType, plBitmap::kRGB8888, alphaChannel ? plBitmap::kDXT5 : plBitmap::kDXT1);
// Step 3.9: Load the image into OpenGL
gl_loadimage guard(blImage);
if (!guard.success()) {
PyErr_SetString(PyExc_RuntimeError, "failed to load image into OpenGL");
return nullptr;
}
// Step 4: Now it's a matter of looping through all the levels and exporting the image
for (size_t i = 0; i < mipmap->getNumLevels(); ++i) {
stuff_mip_level(mipmap, i, blImage, calcAlpha);
}
Py_RETURN_NONE;
}

48
korlib/module.cpp

@ -1,48 +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 <Python.h>
// ========================================================================
extern "C" PyObject* generate_mipmap(PyObject*, PyObject*);
// ========================================================================
static struct PyMethodDef s_korlibMethods[] =
{
{ "generate_mipmap", generate_mipmap, METH_VARARGS, "Generates a new plMipmap from a Blender ImageTexture" },
{ nullptr, nullptr, 0, nullptr },
};
static struct PyModuleDef s_korlibModule = {
PyModuleDef_HEAD_INIT,
"korlib",
NULL,
-1,
s_korlibMethods
};
#define ADD_CONSTANT(module, name) \
PyModule_AddIntConstant(module, #name, korlib::name)
PyMODINIT_FUNC PyInit_korlib()
{
PyObject* module = PyModule_Create(&s_korlibModule);
// Done!
return module;
}

27
korlib/pyMipmap.h

@ -1,27 +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/>.
*/
/**
* \file Declarations required to interop with PyHSPlasma's pyMipmap
*/
#include <Python.h>
typedef struct {
PyObject_HEAD
class plMipmap* fThis;
bool fPyOwned;
} pyMipmap;

82
korlib/utils.hpp

@ -1,82 +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_UTILS_HPP
#define __KORLIB_UTILS_HPP
#include <Python.h>
#define print(fmt, ...) PySys_WriteStdout(" " fmt "\n", __VA_ARGS__)
namespace korlib
{
/** RAII for PyObject pointers */
class pyref
{
PyObject* _ref;
public:
pyref(PyObject* o) : _ref(o) { }
pyref(const pyref& copy) : _ref((PyObject*)copy)
{
Py_INCREF(_ref);
}
~pyref()
{
Py_XDECREF(_ref);
}
operator PyObject*() const { return _ref; }
};
template<typename T>
T call_method(PyObject* o, const char* method);
template<>
size_t call_method(PyObject* o, const char* method)
{
pyref retval = PyObject_CallMethod(o, const_cast<char*>(method), "");
if ((PyObject*)retval)
return PyLong_AsSize_t(retval);
else
return static_cast<size_t>(-1);
}
template<typename T>
T getattr(PyObject* o, const char* name);
template<>
bool getattr(PyObject* o, const char* name)
{
pyref attr = PyObject_GetAttrString(o, name);
return PyLong_AsLong(attr) != 0;
}
template<>
PyObject* getattr(PyObject* o, const char* name)
{
return PyObject_GetAttrString(o, name);
}
template<>
size_t getattr(PyObject* o, const char* name)
{
pyref attr = PyObject_GetAttrString(o, name);
return PyLong_AsSize_t(attr);
}
};
#endif // __KORLIB_UTILS_HPP

5
korman/exporter/explosions.py

@ -18,6 +18,11 @@ class ExportError(Exception):
super(Exception, self).__init__(value)
class GLLoadError(ExportError):
def __init__(self, image):
super(ExportError, self).__init__("Failed to load '{}' into OpenGL".format(image.name))
class TooManyUVChannelsError(ExportError):
def __init__(self, obj, mat):
msg = "There are too many UV Textures on the material '{}' associated with object '{}'.".format(

116
korman/exporter/material.py

@ -14,13 +14,91 @@
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
import bpy
import korlib
import bgl
import math
from PyHSPlasma import *
import weakref
from . import explosions
from . import utils
# BGL doesn't know about this as of Blender 2.71
bgl.GL_GENERATE_MIPMAP = 0x8191
class _GLTexture:
def __init__(self, blimg):
self._ownit = (blimg.bindcode == 0)
if self._ownit:
if blimg.gl_load() != 0:
raise explosions.GLLoadError(blimg)
self._blimg = blimg
def __del__(self):
if self._ownit:
self._blimg.gl_free()
def __enter__(self):
"""Sets the Blender Image as the active OpenGL texture"""
self._previous_texture = self._get_integer(bgl.GL_TEXTURE_BINDING_2D)
self._changed_state = (self._previous_texture != self._blimg.bindcode)
if self._changed_state:
bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._blimg.bindcode)
return self
def __exit__(self, type, value, traceback):
mipmap_state = getattr(self, "_mipmap_state", None)
if mipmap_state is not None:
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_GENERATE_MIPMAP, mipmap_state)
if self._changed_state:
bgl.glBindTexture(bgl.GL_TEXTURE_2D, self._previous_texture)
def generate_mipmap(self):
"""Generates all mip levels for this texture"""
self._mipmap_state = self._get_tex_param(bgl.GL_GENERATE_MIPMAP)
# Note that this is a very old feature from OpenGL 1.x -- it's new enough that Windows (and
# Blender apparently) don't support it natively and yet old enough that it was thrown away
# in OpenGL 3.0. The new way is glGenerateMipmap, but Blender likes oldgl, so we don't have that
# function available to us in BGL. I don't want to deal with loading the GL dll in ctypes on
# many platforms right now (or context headaches). If someone wants to fix this, be my guest!
# It will simplify our state tracking a bit.
bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_GENERATE_MIPMAP, 1)
def get_level_data(self, level, calc_alpha=False):
"""Gets the uncompressed pixel data for a requested mip level, optionally calculating the alpha
channel from the image color data
"""
width = self._get_tex_param(bgl.GL_TEXTURE_WIDTH, level)
height = self._get_tex_param(bgl.GL_TEXTURE_HEIGHT, level)
# Grab the image data
size = width * height * 4
buf = bgl.Buffer(bgl.GL_BYTE, size)
bgl.glGetTexImage(bgl.GL_TEXTURE_2D, level, bgl.GL_RGBA, bgl.GL_UNSIGNED_BYTE, buf);
# Calculate le alphas
if calc_alpha:
for i in range(size, 4):
base = i*4
r, g, b = buf[base:base+2]
buf[base+3] = int((r + g + b) / 3)
return bytes(buf)
def _get_integer(self, arg):
buf = bgl.Buffer(bgl.GL_INT, 1)
bgl.glGetIntegerv(arg, buf)
return int(buf[0])
def _get_tex_param(self, param, level=None):
buf = bgl.Buffer(bgl.GL_INT, 1)
if level is None:
bgl.glGetTexParameteriv(bgl.GL_TEXTURE_2D, param, buf)
else:
bgl.glGetTexLevelParameteriv(bgl.GL_TEXTURE_2D, level, param, buf)
return int(buf[0])
class MaterialConverter:
def __init__(self, exporter):
self._exporter = weakref.ref(exporter)
@ -102,13 +180,45 @@ class MaterialConverter:
return
else:
location = self._mgr.get_textures_page(bo)
bitmap = self._mgr.add_object(plMipmap, name=name, loc=location)
korlib.generate_mipmap(texture, bitmap)
bitmap = self._TEMP_export_image(bo, name, texture)
# Store the created plBitmap and toss onto the layer
self._hsbitmaps[name] = bitmap
layer.texture = bitmap.key
def _TEMP_export_image(self, bo, name, texture):
print(" Exporting {}".format(name))
image = texture.image
oWidth, oHeight = image.size
eWidth = int(round(pow(2, math.log(oWidth, 2))))
eHeight = int(round(pow(2, math.log(oHeight, 2))))
if (eWidth != oWidth) or (eHeight != oHeight):
print(" Image is not a POT ({}x{}) resizing to {}x{}".format(oWidth, oHeight, eWidth, eHeight))
image.scale(eWidth, eHeight)
# Basic things
levelHint = 0 if texture.use_mipmap else 1
compression = plBitmap.kDirectXCompression if texture.use_mipmap else plBitmap.kUncompressed
dxt = plBitmap.kDXT5 if texture.use_alpha or texture.use_calculate_alpha else plBitmap.kDXT1
# This wraps the call to plMipmap::Create
mipmap = plMipmap(name=name, width=eWidth, height=eHeight, numLevels=levelHint,
compType=compression, format=plBitmap.kRGB8888, dxtLevel=dxt)
page = self._mgr.get_textures_page(bo)
self._mgr.AddObject(page, mipmap)
with _GLTexture(image) as glimage:
if texture.use_mipmap:
glimage.generate_mipmap()
stuff_func = mipmap.CompressImage if compression == plBitmap.kDirectXCompression else mipmap.setLevel
for i in range(mipmap.numLevels):
data = glimage.get_level_data(i, texture.use_calculate_alpha)
stuff_func(i, data)
return mipmap
def _export_texture_type_none(self, bo, hsgmat, layer, texture):
# We'll allow this, just for sanity's sake...
pass

Loading…
Cancel
Save