Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arcade/gl/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def __init__(
elif reserve > 0:
self._size = reserve
# populate the buffer with zero byte values
data = (gl.GLubyte * self._size)(0)
data = (gl.GLubyte * self._size)()
gl.glBufferData(gl.GL_ARRAY_BUFFER, self._size, data, self._usage)
else:
raise ValueError("Buffer takes byte data or number of reserved bytes")
Expand Down
2 changes: 1 addition & 1 deletion arcade/gl/compute_shader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ComputeShader:
def __init__(self, ctx: "Context", glsl_source: str) -> None:
self._ctx = ctx
self._source = glsl_source
self._uniforms: Dict[str, Uniform] = dict()
self._uniforms: Dict[str, Union[UniformBlock, Uniform]] = dict()

from arcade.gl import ShaderException

Expand Down
25 changes: 13 additions & 12 deletions arcade/gl/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Union)

import pyglet
import pyglet.gl.lib
from pyglet import gl
from pyglet.window import Window

Expand All @@ -17,7 +18,7 @@
from .program import Program
from .query import Query
from .texture import Texture2D
from .types import BufferDescription
from .types import BufferDescription, GLenumLike, PyGLenum
from .vertex_array import Geometry
from ..types import BufferProtocol

Expand Down Expand Up @@ -872,9 +873,9 @@ def texture(
components: int = 4,
dtype: str = "f1",
data: Optional[BufferProtocol] = None,
wrap_x: Optional[gl.GLenum] = None,
wrap_y: Optional[gl.GLenum] = None,
filter: Optional[Tuple[gl.GLenum, gl.GLenum]] = None,
wrap_x: Optional[PyGLenum] = None,
wrap_y: Optional[PyGLenum] = None,
filter: Optional[Tuple[GLenumLike, GLenumLike]] = None,
samples: int = 0,
immutable: bool = False,
) -> Texture2D:
Expand Down Expand Up @@ -1313,36 +1314,36 @@ def __init__(self, ctx):

warn("Error happened while querying of limits. Moving on ..")

def get_int_tuple(self, enum: gl.GLenum, length: int):
def get_int_tuple(self, enum: GLenumLike, length: int):
"""Get an enum as an int tuple"""
try:
values = (c_int * length)()
gl.glGetIntegerv(enum, values)
return tuple(values)
except gl.lib.GLException:
except pyglet.gl.lib.GLException:
return tuple([0] * length)

def get(self, enum: gl.GLenum, default=0) -> int:
def get(self, enum: GLenumLike, default=0) -> int:
"""Get an integer limit"""
try:
value = c_int()
gl.glGetIntegerv(enum, value)
return value.value
except gl.lib.GLException:
except pyglet.gl.lib.GLException:
return default

def get_float(self, enum: gl.GLenum, default=0.0) -> float:
def get_float(self, enum: GLenumLike, default=0.0) -> float:
"""Get a float limit"""
try:
value = c_float()
gl.glGetFloatv(enum, value)
return value.value
except gl.lib.GLException:
except pyglet.gl.lib.GLException:
return default

def get_str(self, enum: gl.GLenum) -> str:
def get_str(self, enum: GLenumLike) -> str:
"""Get a string limit"""
try:
return cast(gl.glGetString(enum), c_char_p).value.decode() # type: ignore
except gl.lib.GLException:
except pyglet.gl.lib.GLException:
return "Unknown"
6 changes: 3 additions & 3 deletions arcade/gl/geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ def sphere(
R = 1.0 / (rings - 1)
S = 1.0 / (sectors - 1)

vertices = [0] * (rings * sectors * 3)
normals = [0] * (rings * sectors * 3)
uvs = [0] * (rings * sectors * 2)
vertices = [0.0] * (rings * sectors * 3)
normals = [0.0] * (rings * sectors * 3)
uvs = [0.0] * (rings * sectors * 2)

v, n, t = 0, 0, 0
for r in range(rings):
Expand Down
4 changes: 2 additions & 2 deletions arcade/gl/glsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pyglet import gl

from .exceptions import ShaderException
from .types import SHADER_TYPE_NAMES
from .types import SHADER_TYPE_NAMES, PyGLenum


class ShaderSource:
Expand All @@ -31,7 +31,7 @@ def __init__(
ctx: gl.Context,
source: str,
common: Optional[Iterable[str]],
source_type: gl.GLenum,
source_type: PyGLenum,
):
"""Create a shader source wrapper."""
self._source = source.strip()
Expand Down
6 changes: 3 additions & 3 deletions arcade/gl/program.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pyglet import gl

from .uniform import Uniform, UniformBlock
from .types import AttribFormat, GLTypes, SHADER_TYPE_NAMES
from .types import AttribFormat, GLTypes, SHADER_TYPE_NAMES, PyGLenum
from .exceptions import ShaderException

if TYPE_CHECKING: # handle import cycle caused by type hinting
Expand Down Expand Up @@ -93,7 +93,7 @@ def __init__(
f"Valid modes are: {self._valid_capture_modes}."
)

shaders = [(vertex_shader, gl.GL_VERTEX_SHADER)]
shaders: List[Tuple[str, int]] = [(vertex_shader, gl.GL_VERTEX_SHADER)]
if fragment_shader:
shaders.append((fragment_shader, gl.GL_FRAGMENT_SHADER))
if geometry_shader:
Expand Down Expand Up @@ -486,7 +486,7 @@ def _query_uniform_block(self, location: int) -> Tuple[int, int, str]:
return index, b_size.value, u_name.value.decode()

@staticmethod
def compile_shader(source: str, shader_type: gl.GLenum) -> gl.GLuint:
def compile_shader(source: str, shader_type: PyGLenum) -> gl.GLuint:
"""Compile the shader code of the given type.

`shader_type` could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, ...
Expand Down
6 changes: 3 additions & 3 deletions arcade/gl/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .buffer import Buffer
from .utils import data_to_ctypes
from .types import pixel_formats, BufferOrBufferProtocol
from .types import PyGLuint, pixel_formats, BufferOrBufferProtocol
from ..types import BufferProtocol

if TYPE_CHECKING: # handle import cycle caused by type hinting
Expand Down Expand Up @@ -115,8 +115,8 @@ def __init__(
dtype: str = "f1",
data: Optional[BufferProtocol] = None,
filter: Optional[Tuple[gl.GLuint, gl.GLuint]] = None,
wrap_x: Optional[gl.GLuint] = None,
wrap_y: Optional[gl.GLuint] = None,
wrap_x: Optional[PyGLuint] = None,
wrap_y: Optional[PyGLuint] = None,
target=gl.GL_TEXTURE_2D,
depth=False,
samples: int = 0,
Expand Down
25 changes: 16 additions & 9 deletions arcade/gl/types.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import re
from typing import Optional, Iterable, List, Union
from typing import Dict, Optional, Iterable, List, Sequence, Tuple, Union

from pyglet import gl

Expand All @@ -9,6 +9,10 @@

BufferOrBufferProtocol = Union[BufferProtocol, Buffer]

GLenumLike = Union[gl.GLenum, int]
PyGLenum = int
GLuintLike = Union[gl.GLuint, int]
PyGLuint = int

_float_base_format = (0, gl.GL_RED, gl.GL_RG, gl.GL_RGB, gl.GL_RGBA)
_int_base_format = (
Expand Down Expand Up @@ -102,8 +106,10 @@
}


def gl_name(gl_type: gl.GLenum) -> str:
def gl_name(gl_type: Optional[PyGLenum]) -> Union[str, PyGLenum, None]:
"""Return the name of a gl type"""
if gl_type is None:
return None
return GL_NAMES.get(gl_type, gl_type)


Expand All @@ -112,7 +118,7 @@ class AttribFormat:
Represents an attribute in a BufferDescription or a Program.

:param str name: Name of the attribute
:param gl.GLEnum gl_type: The OpenGL type such as GL_FLOAT, GL_HALF_FLOAT etc.
:param GLenumLike gl_type: The OpenGL type such as GL_FLOAT, GL_HALF_FLOAT etc.
:param int bytes_per_component: Number of bytes a single component takes
:param int offset: (Optional offset for BufferDescription)
:param int location: (Optional location for program attribute)
Expand All @@ -128,9 +134,10 @@ class AttribFormat:
)

def __init__(
self, name: str, gl_type: gl.GLenum, components: int, bytes_per_component: int, offset=0, location=0
self, name: Optional[str], gl_type: Optional[PyGLenum], components: int, bytes_per_component: int, offset=0,
location=0
):
self.name: str = name
self.name = name
self.gl_type = gl_type
self.components = components
self.bytes_per_component = bytes_per_component
Expand Down Expand Up @@ -188,7 +195,7 @@ class BufferDescription:

# Describe all variants of a format string to simplify parsing (single component)
# format: gl_type, byte_size
_formats = {
_formats: Dict[str, Tuple[Optional[PyGLenum], int]] = {
# (gl enum, byte size)
# Floats
"f": (gl.GL_FLOAT, 4),
Expand Down Expand Up @@ -227,7 +234,7 @@ def __init__(
self,
buffer: Buffer,
formats: str,
attributes: Iterable[str],
attributes: Sequence[str],
normalized: Optional[Iterable[str]] = None,
instanced: bool = False,
):
Expand Down Expand Up @@ -266,7 +273,7 @@ def __init__(
f"attributes ({len(self.attributes)})"
)

def zip_attrs(formats, attributes):
def zip_attrs(formats: List[str], attributes: Sequence[str]):
"""Join together formats and attribute names taking padding into account"""
attr_index = 0
for f in formats:
Expand Down Expand Up @@ -344,7 +351,7 @@ class TypeInfo:
"""
__slots__ = "name", "enum", "gl_type", "gl_size", "components"

def __init__(self, name: str, enum: gl.GLenum, gl_type: gl.GLenum, gl_size: int, components: int):
def __init__(self, name: str, enum: GLenumLike, gl_type: PyGLenum, gl_size: int, components: int):
self.name = name
self.enum = enum
self.gl_type = gl_type
Expand Down
2 changes: 0 additions & 2 deletions arcade/gl/uniform.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ def __init__(self, ctx, program_id, location, name, data_type, array_length):
self._components = 0
#: The getter function configured for this uniform
#: The setter function configured for this uniform
self.getter = None
self.setter = None
self._setup_getters_and_setters()

@property
Expand Down
28 changes: 14 additions & 14 deletions arcade/gl/vertex_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from pyglet import gl

from .buffer import Buffer
from .types import BufferDescription, gl_name
from .types import BufferDescription, GLenumLike, GLuintLike, gl_name
from .program import Program

if TYPE_CHECKING: # handle import cycle caused by type hinting
Expand Down Expand Up @@ -146,7 +146,7 @@ def _build(
# Build the vao according to the shader's attribute specifications
for _, prog_attr in enumerate(program.attributes):
# Do we actually have an attribute with this name in buffer descriptions?
if prog_attr.name.startswith("gl_"):
if prog_attr.name is not None and prog_attr.name.startswith("gl_"):
continue
try:
buff_descr, attr_descr = descr_attribs[prog_attr.name]
Expand Down Expand Up @@ -239,7 +239,7 @@ def _build(
gl.glVertexAttribDivisor(prog_attr.location, 1)

def render(
self, mode: gl.GLenum, first: int = 0, vertices: int = 0, instances: int = 1
self, mode: GLenumLike, first: int = 0, vertices: int = 0, instances: int = 1
):
"""Render the VertexArray to the currently active framebuffer.

Expand All @@ -259,7 +259,7 @@ def render(
else:
gl.glDrawArraysInstanced(mode, first, vertices, instances)

def render_indirect(self, buffer: Buffer, mode: gl.GLuint, count, first, stride):
def render_indirect(self, buffer: Buffer, mode: GLuintLike, count, first, stride):
"""
Render the VertexArray to the framebuffer using indirect rendering.

Expand Down Expand Up @@ -300,8 +300,8 @@ def render_indirect(self, buffer: Buffer, mode: gl.GLuint, count, first, stride)
def transform_interleaved(
self,
buffer: Buffer,
mode: gl.GLenum,
output_mode: gl.GLenum,
mode: GLenumLike,
output_mode: GLenumLike,
first: int = 0,
vertices: int = 0,
instances: int = 1,
Expand All @@ -310,8 +310,8 @@ def transform_interleaved(
"""Run a transform feedback.

:param Buffer buffer: The buffer to write the output
:param gl.GLenum mode: The input primitive mode
:param gl.GLenum output_mode: The output primitive mode
:param GLenumLike mode: The input primitive mode
:param GLenumLike output_mode: The output primitive mode
:param int first: Offset start vertex
:param int vertices: Number of vertices to render
:param int instances: Number of instances to render
Expand Down Expand Up @@ -355,8 +355,8 @@ def transform_interleaved(
def transform_separate(
self,
buffers: List[Buffer],
mode: gl.GLenum,
output_mode: gl.GLenum,
mode: GLenumLike,
output_mode: GLenumLike,
first: int = 0,
vertices: int = 0,
instances: int = 1,
Expand All @@ -366,8 +366,8 @@ def transform_separate(
Run a transform feedback writing to separate buffers.

:param List[Buffer] buffers: The buffers to write the output
:param gl.GLenum mode: The input primitive mode
:param gl.GLenum output_mode: The output primitive mode
:param GLenumLike mode: The input primitive mode
:param GLenumLike output_mode: The output primitive mode
:param int first: Offset start vertex
:param int vertices: Number of vertices to render
:param int instances: Number of instances to render
Expand Down Expand Up @@ -537,7 +537,7 @@ def render(
self,
program: Program,
*,
mode: Optional[gl.GLenum] = None,
mode: Optional[GLenumLike] = None,
first: int = 0,
vertices: Optional[int] = None,
instances: int = 1,
Expand All @@ -549,7 +549,7 @@ def render(
or have resized the buffers after the geometry instance was created.

:param Program program: The Program to render with
:param gl.GLenum mode: Override what primitive mode should be used
:param GLenumLike mode: Override what primitive mode should be used
:param int first: Offset start vertex
:param int vertices: Override the number of vertices to render
:param int instances: Number of instances to render
Expand Down