Browse Source

python: expose initial GL::Buffer bits.

pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
c94598d6c1
  1. 66
      src/python/magnum/gl.cpp
  2. 46
      src/python/magnum/test/test_gl_gl.py

66
src/python/magnum/gl.cpp

@ -24,13 +24,18 @@
*/
#include <pybind11/pybind11.h>
#include <Corrade/Containers/ArrayView.h>
#include <Magnum/GL/Attribute.h>
#include <Magnum/GL/Buffer.h>
#include "corrade/PyArrayView.h"
#include "magnum/bootstrap.h"
namespace magnum { namespace {
void gl(py::module& m) {
py::module::import("corrade.containers");
/* (Dynamic) attribute */
py::class_<GL::DynamicAttribute> attribute{m, "Attribute", "Vertex attribute location and type"};
@ -86,6 +91,67 @@ void gl(py::module& m) {
"Component count")
.def_property_readonly("data_type", &GL::DynamicAttribute::dataType,
"Type of passed data");
/* Buffer */
py::enum_<GL::BufferUsage>{m, "BufferUsage", "Buffer usage"}
.value("STREAM_DRAW", GL::BufferUsage::StreamDraw)
#ifndef MAGNUM_TARGET_GLES2
.value("STREAM_READ", GL::BufferUsage::StreamRead)
.value("STREAM_COPY", GL::BufferUsage::StreamCopy)
#endif
.value("STATIC_DRAW", GL::BufferUsage::StaticDraw)
#ifndef MAGNUM_TARGET_GLES2
.value("STATIC_READ", GL::BufferUsage::StaticRead)
.value("STATIC_COPY", GL::BufferUsage::StaticCopy)
#endif
.value("DYNAMIC_DRAW", GL::BufferUsage::DynamicDraw)
#ifndef MAGNUM_TARGET_GLES2
.value("DYNAMIC_READ", GL::BufferUsage::DynamicRead)
.value("DYNAMIC_COPY", GL::BufferUsage::DynamicCopy)
#endif
;
py::class_<GL::Buffer> buffer{m, "Buffer", "Buffer"};
py::enum_<GL::Buffer::TargetHint>{buffer, "TargetHint", "Buffer target"}
.value("ARRAY", GL::Buffer::TargetHint::Array)
#ifndef MAGNUM_TARGET_GLES2
#ifndef MAGNUM_TARGET_WEBGL
.value("ATOMIC_COUNTER", GL::Buffer::TargetHint::AtomicCounter)
#endif
.value("COPY_READ", GL::Buffer::TargetHint::CopyRead)
.value("COPY_WRITE", GL::Buffer::TargetHint::CopyWrite)
#ifndef MAGNUM_TARGET_WEBGL
.value("DISPATCH_INDIRECT", GL::Buffer::TargetHint::DispatchIndirect)
.value("DRAW_INDIRECT", GL::Buffer::TargetHint::DrawIndirect)
#endif
#endif
.value("ELEMENT_ARRAY", GL::Buffer::TargetHint::ElementArray)
#ifndef MAGNUM_TARGET_GLES2
.value("PIXEL_PACK", GL::Buffer::TargetHint::PixelPack)
.value("PIXEL_UNPACK", GL::Buffer::TargetHint::PixelUnpack)
#ifndef MAGNUM_TARGET_WEBGL
.value("SHADER_STORAGE", GL::Buffer::TargetHint::ShaderStorage)
#endif
#endif
#if !defined(MAGNUM_TARGET_GLES2) && !defined(MAGNUM_TARGET_WEBGL)
.value("TEXTURE", GL::Buffer::TargetHint::Texture)
#endif
#ifndef MAGNUM_TARGET_GLES2
.value("TRANSFORM_FEEDBACK", GL::Buffer::TargetHint::TransformFeedback)
.value("UNIFORM", GL::Buffer::TargetHint::Uniform)
#endif
;
buffer
.def(py::init<GL::Buffer::TargetHint>(), "Constructor", py::arg("target_hint") = GL::Buffer::TargetHint::Array)
.def_property_readonly("id", &GL::Buffer::id, "OpenGL buffer ID")
.def_property("target_hint", &GL::Buffer::targetHint, &GL::Buffer::setTargetHint, "Target hint")
/* Using lambdas to avoid method chaining getting into signatures */
.def("set_data", [](GL::Buffer& self, const corrade::PyArrayView<const char>& data, GL::BufferUsage usage) {
self.setData(data, usage);
}, "Set buffer data", py::arg("data"), py::arg("usage") = GL::BufferUsage::StaticDraw)
/** @todo more */;
}
}}

46
src/python/magnum/test/test_gl_gl.py

@ -0,0 +1,46 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
# Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import unittest
# setUpModule gets called before everything else, skipping if GL tests can't
# be run
from . import GLTestCase, setUpModule
from magnum import gl
class Buffer(GLTestCase):
def test_init(self):
a = gl.Buffer()
self.assertNotEqual(a.id, 0)
self.assertEqual(a.target_hint, gl.Buffer.TargetHint.ARRAY)
b = gl.Buffer(gl.Buffer.TargetHint.ELEMENT_ARRAY)
self.assertNotEqual(b.id, 0)
self.assertEqual(b.target_hint, gl.Buffer.TargetHint.ELEMENT_ARRAY)
def test_set_data(self):
a = gl.Buffer()
a.set_data(b'hello', gl.BufferUsage.STATIC_DRAW)
Loading…
Cancel
Save