Skip to content

Commit

Permalink
Support getting geometry shader input, output and vertices from python
Browse files Browse the repository at this point in the history
  • Loading branch information
einarf committed Apr 11, 2020
1 parent c88c4d0 commit a0c118c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
36 changes: 33 additions & 3 deletions arcade/shader.py
Expand Up @@ -167,7 +167,7 @@ class Program:
Example:
program['MyUniform'] = value
"""
__slots__ = '_ctx', '_glo', '_uniforms', '_out_attributes', '__weakref__'
__slots__ = '_ctx', '_glo', '_uniforms', '_out_attributes', '_geometry_info', '__weakref__'

def __init__(self,
ctx,
Expand All @@ -187,6 +187,7 @@ def __init__(self,
self._ctx = ctx
self._glo = glo = gl.glCreateProgram()
self._out_attributes = out_attributes or []
self._geometry_info = (0, 0, 0)

shaders = [(vertex_shader, gl.GL_VERTEX_SHADER)]
if fragment_shader:
Expand All @@ -205,13 +206,23 @@ def __init__(self,
self._setup_out_attributes()

Program.link(self._glo)

if geometry_shader:
geometry_in = gl.GLint()
geometry_out = gl.GLint()
geometry_vertices = gl.GLint()
gl.glGetProgramiv(self._glo, gl.GL_GEOMETRY_INPUT_TYPE, geometry_in);
gl.glGetProgramiv(self._glo, gl.GL_GEOMETRY_OUTPUT_TYPE, geometry_out);
gl.glGetProgramiv(self._glo, gl.GL_GEOMETRY_VERTICES_OUT, geometry_vertices);
self._geometry_info = (geometry_in.value, geometry_out.value, geometry_vertices.value)

# Flag shaders for deletion. Will only be deleted once detached from program.
for shader in shaders_id:
# Flag shaders for deletion. Will only be deleted once detached from program.
gl.glDeleteShader(shader)

# Handle uniforms
self._uniforms: Dict[str, Uniform] = {}
self._introspect_uniforms()

weakref.finalize(self, Program._delete, shaders_id, glo)

@property
Expand All @@ -229,6 +240,25 @@ def out_attributes(self) -> List[str]:
"""Out attributes names used in transform feedback"""
return self._out_attributes

@property
def geometry_input(self) -> int:
"""The geometry shader's input primitive type.
This an be compared with ``GL_TRIANGLES``, ``GL_POINTS`` etc.
"""
return self._geometry_info[0]

@property
def geometry_output(self) -> int:
"""The geometry shader's output primitive type.
This an be compared with ``GL_TRIANGLES``, ``GL_POINTS`` etc.
"""
return self._geometry_info[1]

@property
def geometry_vertices(self) -> int:
"""The maximum number of vertices that can be emitted"""
return self._geometry_info[2]

@staticmethod
def _delete(shaders_id, prog_id):
# Check to see if the context was already cleaned up from program
Expand Down
6 changes: 4 additions & 2 deletions tests/unit2/test_opengl.py
Expand Up @@ -192,7 +192,7 @@ def test_program(self):
#version 330
layout (points) in;
layout (line_strip, max_vertices = 2) out;
layout (triangle_strip, max_vertices = 2) out;
void main() {
gl_Position = gl_in[0].gl_Position + vec4(-0.1, 0.0, 0.0, 0.0);
Expand All @@ -205,7 +205,9 @@ def test_program(self):
}
""",
)
# TODO: Test in/out primitives
assert program.geometry_input == self.ctx.POINTS
assert program.geometry_output == self.ctx.TRIANGLE_STRIP
assert program.geometry_vertices == 2

# Uniform testing
# .. mother of all uniform programs trying to cram in as many as possible!
Expand Down

0 comments on commit a0c118c

Please sign in to comment.