display: Add pickle support to FrameBufferProperties

This commit is contained in:
rdb 2021-12-14 11:41:39 +01:00
parent a354b774b3
commit 2e0614138f
5 changed files with 122 additions and 0 deletions

View File

@ -67,6 +67,8 @@ set(P3DISPLAY_SOURCES
) )
set(P3DISPLAY_IGATEEXT set(P3DISPLAY_IGATEEXT
frameBufferProperties_ext.cxx
frameBufferProperties_ext.h
graphicsPipeSelection_ext.cxx graphicsPipeSelection_ext.cxx
graphicsPipeSelection_ext.h graphicsPipeSelection_ext.h
graphicsStateGuardian_ext.cxx graphicsStateGuardian_ext.cxx

View File

@ -16,6 +16,7 @@
#include "pandabase.h" #include "pandabase.h"
#include "pnotify.h" #include "pnotify.h"
#include "extension.h"
class Texture; class Texture;
@ -143,6 +144,9 @@ PUBLISHED:
MAKE_PROPERTY(float_color, get_float_color, set_float_color); MAKE_PROPERTY(float_color, get_float_color, set_float_color);
MAKE_PROPERTY(float_depth, get_float_depth, set_float_depth); MAKE_PROPERTY(float_depth, get_float_depth, set_float_depth);
EXTENSION(PyObject *__getstate__() const);
EXTENSION(void __setstate__(PyObject *self, PyObject *state));
// Other. // Other.
constexpr FrameBufferProperties() = default; constexpr FrameBufferProperties() = default;
@ -169,6 +173,8 @@ PUBLISHED:
bool setup_color_texture(Texture *tex) const; bool setup_color_texture(Texture *tex) const;
bool setup_depth_texture(Texture *tex) const; bool setup_depth_texture(Texture *tex) const;
friend class Extension<FrameBufferProperties>;
}; };
INLINE std::ostream &operator << (std::ostream &out, const FrameBufferProperties &properties); INLINE std::ostream &operator << (std::ostream &out, const FrameBufferProperties &properties);

View File

@ -0,0 +1,75 @@
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file frameBufferProperties_ext.cxx
* @author rdb
* @date 2021-12-13
*/
#include "frameBufferProperties_ext.h"
#ifdef HAVE_PYTHON
/**
* Returns the properties as a dictionary.
*/
PyObject *Extension<FrameBufferProperties>::
__getstate__() const {
static const char *props[FrameBufferProperties::FBP_COUNT] = {"depth_bits", "color_bits", "red_bits", "green_bits", "blue_bits", "alpha_bits", "stencil_bits", "accum_bits", "aux_rgba", "aux_hrgba", "aux_float", "multisamples", "coverage_samples", "back_buffers"};
static const char *flags[] = {"indexed_color", "rgb_color", "stereo", "force_hardware", "force_software", "srgb_color", "float_color", "float_depth", nullptr};
PyObject *state = PyDict_New();
for (size_t i = 0; i < FrameBufferProperties::FBP_COUNT; ++i) {
if (_this->_specified & (1 << i)) {
PyObject *value = PyLong_FromLong(_this->_property[i]);
PyDict_SetItemString(state, props[i], value);
Py_DECREF(value);
}
}
for (size_t i = 0; flags[i] != nullptr; ++i) {
if (_this->_flags_specified & (1 << i)) {
PyObject *value = (_this->_flags & (1 << i)) ? Py_True : Py_False;
PyDict_SetItemString(state, flags[i], value);
}
}
return state;
}
/**
*
*/
void Extension<FrameBufferProperties>::
__setstate__(PyObject *self, PyObject *props) {
PyTypeObject *type = Py_TYPE(self);
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(props, &pos, &key, &value)) {
// Look for a writable property on the type by this name.
PyObject *descr = _PyType_Lookup(type, key);
if (descr != nullptr && Py_TYPE(descr)->tp_descr_set != nullptr) {
if (Py_TYPE(descr)->tp_descr_set(descr, self, value) < 0) {
return;
}
} else {
PyObject *key_repr = PyObject_Repr(key);
PyErr_Format(PyExc_TypeError,
"%.100s is an invalid framebuffer property",
PyUnicode_AsUTF8(key_repr)
);
Py_DECREF(key_repr);
return;
}
}
}
#endif // HAVE_PYTHON

View File

@ -0,0 +1,38 @@
/**
* PANDA 3D SOFTWARE
* Copyright (c) Carnegie Mellon University. All rights reserved.
*
* All use of this software is subject to the terms of the revised BSD
* license. You should have received a copy of this license along
* with this source code in a file named "LICENSE."
*
* @file frameBufferProperties_ext.h
* @author rdb
* @date 2021-12-13
*/
#ifndef FRAMEBUFFERPROPERTIES_EXT_H
#define FRAMEBUFFERPROPERTIES_EXT_H
#include "dtoolbase.h"
#ifdef HAVE_PYTHON
#include "extension.h"
#include "frameBufferProperties.h"
#include "py_panda.h"
/**
* This class defines the extension methods for FrameBufferProperties, which are
* called instead of any C++ methods with the same prototype.
*/
template<>
class Extension<FrameBufferProperties> : public ExtensionBase<FrameBufferProperties> {
public:
PyObject *__getstate__() const;
void __setstate__(PyObject *self, PyObject *state);
};
#endif // HAVE_PYTHON
#endif // FRAMEBUFFERPROPERTIES_EXT_H

View File

@ -1,3 +1,4 @@
#include "frameBufferProperties_ext.cxx"
#include "graphicsPipeSelection_ext.cxx" #include "graphicsPipeSelection_ext.cxx"
#include "graphicsStateGuardian_ext.cxx" #include "graphicsStateGuardian_ext.cxx"
#include "graphicsWindow_ext.cxx" #include "graphicsWindow_ext.cxx"