File: geomshader.py

package info (click to toggle)
pyopengl 3.1.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,024 kB
  • sloc: python: 108,056; sh: 13; makefile: 8
file content (110 lines) | stat: -rw-r--r-- 3,411 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""Transliteration of https://open.gl/feedback into Python"""
from __future__ import print_function
import pygamegltest
from OpenGL._bytes import as_8_bit
from OpenGL.GL import *
from OpenGL.GL import shaders

vertex_shader = """#version 150 core
    in float inValue;
    out float geoValue;
    uniform float junk; // Just here for the introspection test

    void main() {
        geoValue = sqrt(inValue) + junk;
    }
"""

geometry_shader = """#version 150 core
    layout(points) in;
    layout(triangle_strip, max_vertices = 3) out;

    in float[] geoValue;
    out float outValue;

    void main() {
        for (int i = 0; i < 3; i++) {
            outValue = geoValue[0] + i;
            EmitVertex();
        }

        EndPrimitive();
    }
"""


@pygamegltest.pygametest(name="Geometry Shader Test")
def main():
    program = shaders.compileProgram(
        shaders.compileShader([vertex_shader], GL_VERTEX_SHADER),
        shaders.compileShader([geometry_shader], GL_GEOMETRY_SHADER),
    )

    # test that we can describe the attributes in the shader
    for i in range(glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES)):
        name, size, type = glGetActiveAttrib(program, i)
        print('Atribute %s(%i) -> %s %s' % (name, i, size, type))

        length, size, type, name = GLsizei(), GLint(), GLenum(), (GLchar * 8)()
        glGetActiveAttrib(program, i, None, length, size, type, name)
        assert length.value == len(name.value), (length.value, name.value)
        assert type.value == 5126, type.value

    for i in range(glGetProgramiv(program, GL_ACTIVE_UNIFORMS)):
        name, size, type = glGetActiveUniform(program, i)
        print('Uniform %s(%i) -> %s %s' % (name, i, size, type))

        length, size, type, name = GLsizei(), GLint(), GLenum(), (GLchar * 5)()
        glGetActiveUniform(program, i, 5, length, size, type, name)
        assert length.value == len(name.value), (length.value, name.value)
        assert type.value == 5126, type.value

    buff = (ctypes.c_char_p * 1)(as_8_bit("outValue\000"))
    c_text = ctypes.cast(ctypes.pointer(buff), ctypes.POINTER(ctypes.POINTER(GLchar)))
    # modifies the state in the linking of the program
    glTransformFeedbackVaryings(program, 1, c_text, GL_INTERLEAVED_ATTRIBS)
    # so have to re-link
    glLinkProgram(program)

    glUseProgram(program)

    vao = glGenVertexArrays(1)
    glBindVertexArray(vao)

    data = (GLfloat * 5)(1.0, 2.0, 3.0, 4.0, 5.0)

    vbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, vbo)
    glBufferData(GL_ARRAY_BUFFER, ctypes.sizeof(data), data, GL_STATIC_DRAW)

    inputAttrib = glGetAttribLocation(program, "inValue")
    glEnableVertexAttribArray(inputAttrib)
    glVertexAttribPointer(inputAttrib, 1, GL_FLOAT, GL_FALSE, 0, 0)

    tbo = glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER, tbo)
    glBufferData(GL_ARRAY_BUFFER, ctypes.sizeof(data) * 3, None, GL_STATIC_READ)

    glEnable(GL_RASTERIZER_DISCARD)

    glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, tbo)

    glBeginTransformFeedback(GL_TRIANGLES)
    glDrawArrays(GL_POINTS, 0, 5)
    glEndTransformFeedback()

    glDisable(GL_RASTERIZER_DISCARD)

    glFlush()

    feedback = (GLfloat * 15)(*([0] * 15))
    glGetBufferSubData(
        GL_TRANSFORM_FEEDBACK_BUFFER, 0, ctypes.sizeof(feedback), feedback
    )

    for value in feedback:
        print(value)


if __name__ == "__main__":
    main()