Support vertex attrib divisors

This commit is contained in:
Kovid Goyal 2017-08-21 23:24:05 +05:30
parent 67ec04fba3
commit afa767c3a8
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 27 additions and 9 deletions

View File

@ -50,7 +50,7 @@ void main() {
final_color = vec4(color, 1);
}
''')
self.add_vertex_array('rect')
self.add_vertex_arrays(self.vertex_array('rect'))
def send_data(self, data):
self.send_vertex_data('rect', data)

View File

@ -695,6 +695,15 @@ VertexAttribPointer(PyObject UNUSED *self, PyObject *args) {
Py_RETURN_NONE;
}
static PyObject*
VertexAttribDivisor(PyObject UNUSED *self, PyObject *args) {
unsigned int index, divisor;
if (!PyArg_ParseTuple(args, "II", &index, &divisor)) return NULL;
glVertexAttribDivisor(index, divisor);
CHECK_ERROR;
Py_RETURN_NONE;
}
static PyObject*
Flush(PyObject UNUSED *self) {
glFinish();
@ -798,6 +807,7 @@ int add_module_gl_constants(PyObject *module) {
METH(Disable, METH_O) \
METH(EnableVertexAttribArray, METH_O) \
METH(VertexAttribPointer, METH_VARARGS) \
METH(VertexAttribDivisor, METH_VARARGS) \
METH(GetProgramInfoLog, METH_O) \
METH(GetShaderInfoLog, METH_O) \
METH(ActiveTexture, METH_O) \

View File

@ -4,6 +4,7 @@
import os
import sys
from collections import namedtuple
from ctypes import addressof, sizeof
from functools import lru_cache
from threading import Lock
@ -24,8 +25,8 @@ from .fast_data_types import (
glGetIntegerv, glGetProgramInfoLog, glGetProgramiv, glGetShaderInfoLog,
glGetShaderiv, glGetUniformLocation, glLinkProgram, glNamedBufferData,
glPixelStorei, glShaderSource, glTexBuffer, glTexParameteri,
glTexStorage3D, glTexSubImage3D, glUseProgram, glVertexAttribPointer,
replace_or_create_buffer
glTexStorage3D, glTexSubImage3D, glUseProgram, glVertexAttribDivisor,
glVertexAttribPointer, replace_or_create_buffer
)
from .fonts.render import render_cell
from .utils import safe_print
@ -35,6 +36,7 @@ VERSION = GL_VERSION[0] * 100 + GL_VERSION[1] * 10
ITALIC_MASK = 1 << ITALIC
BOLD_MASK = 1 << BOLD
BASE = os.path.dirname(os.path.abspath(__file__))
VertexArray = namedtuple('VertexArray', 'name size dtype normalized stride offset divisor')
@lru_cache()
@ -267,13 +269,19 @@ class ShaderProgram:
self.vao_id = glGenVertexArrays(1)
self.buffers = {}
def add_vertex_array(self, name, size=3, dtype=GL_FLOAT, normalized=False, stride=0, offset=0):
def vertex_array(self, name, size=3, dtype=GL_FLOAT, normalized=False, stride=0, offset=0, divisor=0):
return VertexArray(name, size, dtype, normalized, stride, offset, divisor)
def add_vertex_arrays(self, *arrays):
glBindVertexArray(self.vao_id)
self.buffers[name] = buf_id = buffer_manager.create(for_use=GL_ARRAY_BUFFER)
buffer_manager.bind(buf_id)
aid = self.attribute_location(name)
glEnableVertexAttribArray(aid)
glVertexAttribPointer(aid, size, dtype, normalized, stride, offset)
for x in arrays:
self.buffers[x.name] = buf_id = buffer_manager.create(for_use=GL_ARRAY_BUFFER)
buffer_manager.bind(buf_id)
aid = self.attribute_location(x.name)
glEnableVertexAttribArray(aid)
glVertexAttribPointer(aid, x.size, x.dtype, x.normalized, x.stride, x.offset)
if x.divisor > 0:
glVertexAttribDivisor(aid, x.divisor)
buffer_manager.unbind(buf_id)
glBindVertexArray(0)