Skip to content

Commit 8a6d8a3

Browse files
committed
Re-apply all the arcade.gl changes in a new PR
This reverts commit bfee79a.
1 parent 2c5bde3 commit 8a6d8a3

10 files changed

+60
-50
lines changed

arcade/gl/buffer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def __init__(
6666
elif reserve > 0:
6767
self._size = reserve
6868
# populate the buffer with zero byte values
69-
data = (gl.GLubyte * self._size)(0)
69+
data = (gl.GLubyte * self._size)()
7070
gl.glBufferData(gl.GL_ARRAY_BUFFER, self._size, data, self._usage)
7171
else:
7272
raise ValueError("Buffer takes byte data or number of reserved bytes")

arcade/gl/compute_shader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ComputeShader:
1717
def __init__(self, ctx: "Context", glsl_source: str) -> None:
1818
self._ctx = ctx
1919
self._source = glsl_source
20-
self._uniforms: Dict[str, Uniform] = dict()
20+
self._uniforms: Dict[str, Union[UniformBlock, Uniform]] = dict()
2121

2222
from arcade.gl import ShaderException
2323

arcade/gl/context.py

+20-13
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import logging
2+
import typing
23
import weakref
34
from collections import deque
45
from contextlib import contextmanager
56
from ctypes import c_char_p, c_float, c_int, cast
6-
from typing import (Any, Deque, Dict, Iterable, List, Optional, Sequence, Set, Tuple,
7+
from typing import (TYPE_CHECKING, Any, Deque, Dict, Iterable, List, Optional, Sequence, Set, Tuple,
78
Union)
89

910
import pyglet
11+
import pyglet.gl.lib
1012
from pyglet import gl
1113
from pyglet.window import Window
1214

@@ -17,7 +19,7 @@
1719
from .program import Program
1820
from .query import Query
1921
from .texture import Texture2D
20-
from .types import BufferDescription
22+
from .types import BufferDescription, GLenumLike, PyGLenum
2123
from .vertex_array import Geometry
2224
from ..types import BufferProtocol
2325

@@ -872,9 +874,9 @@ def texture(
872874
components: int = 4,
873875
dtype: str = "f1",
874876
data: Optional[BufferProtocol] = None,
875-
wrap_x: Optional[gl.GLenum] = None,
876-
wrap_y: Optional[gl.GLenum] = None,
877-
filter: Optional[Tuple[gl.GLenum, gl.GLenum]] = None,
877+
wrap_x: Optional[PyGLenum] = None,
878+
wrap_y: Optional[PyGLenum] = None,
879+
filter: Optional[Tuple[GLenumLike, GLenumLike]] = None,
878880
samples: int = 0,
879881
immutable: bool = False,
880882
) -> Texture2D:
@@ -1313,36 +1315,41 @@ def __init__(self, ctx):
13131315

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

1316-
def get_int_tuple(self, enum: gl.GLenum, length: int):
1318+
def get_int_tuple(self, enum: GLenumLike, length: int):
13171319
"""Get an enum as an int tuple"""
13181320
try:
13191321
values = (c_int * length)()
13201322
gl.glGetIntegerv(enum, values)
13211323
return tuple(values)
1322-
except gl.lib.GLException:
1324+
except pyglet.gl.lib.GLException:
13231325
return tuple([0] * length)
13241326

1325-
def get(self, enum: gl.GLenum, default=0) -> int:
1327+
def get(self, enum: GLenumLike, default=0) -> int:
13261328
"""Get an integer limit"""
13271329
try:
13281330
value = c_int()
13291331
gl.glGetIntegerv(enum, value)
13301332
return value.value
1331-
except gl.lib.GLException:
1333+
except pyglet.gl.lib.GLException:
13321334
return default
13331335

1334-
def get_float(self, enum: gl.GLenum, default=0.0) -> float:
1336+
def get_float(self, enum: GLenumLike, default=0.0) -> float:
13351337
"""Get a float limit"""
13361338
try:
13371339
value = c_float()
13381340
gl.glGetFloatv(enum, value)
13391341
return value.value
1340-
except gl.lib.GLException:
1342+
except pyglet.gl.lib.GLException:
13411343
return default
13421344

1343-
def get_str(self, enum: gl.GLenum) -> str:
1345+
def get_str(self, enum: GLenumLike) -> str:
13441346
"""Get a string limit"""
13451347
try:
13461348
return cast(gl.glGetString(enum), c_char_p).value.decode() # type: ignore
1347-
except gl.lib.GLException:
1349+
except pyglet.gl.lib.GLException:
13481350
return "Unknown"
1351+
1352+
if TYPE_CHECKING:
1353+
# Arcade's Context is passed into code paths that require a pyglet Context
1354+
# So we must prove they are compatible.
1355+
__typecheck__: pyglet.gl.base.Context = typing.cast(Context, '__typecheck__')

arcade/gl/geometry.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,9 @@ def sphere(
231231
R = 1.0 / (rings - 1)
232232
S = 1.0 / (sectors - 1)
233233

234-
vertices = [0] * (rings * sectors * 3)
235-
normals = [0] * (rings * sectors * 3)
236-
uvs = [0] * (rings * sectors * 2)
234+
vertices = [0.0] * (rings * sectors * 3)
235+
normals = [0.0] * (rings * sectors * 3)
236+
uvs = [0.0] * (rings * sectors * 2)
237237

238238
v, n, t = 0, 0, 0
239239
for r in range(rings):

arcade/gl/glsl.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from pyglet import gl
55

66
from .exceptions import ShaderException
7-
from .types import SHADER_TYPE_NAMES
7+
from .types import SHADER_TYPE_NAMES, PyGLenum
88

99

1010
class ShaderSource:
@@ -31,7 +31,7 @@ def __init__(
3131
ctx: gl.Context,
3232
source: str,
3333
common: Optional[Iterable[str]],
34-
source_type: gl.GLenum,
34+
source_type: PyGLenum,
3535
):
3636
"""Create a shader source wrapper."""
3737
self._source = source.strip()

arcade/gl/program.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pyglet import gl
1616

1717
from .uniform import Uniform, UniformBlock
18-
from .types import AttribFormat, GLTypes, SHADER_TYPE_NAMES
18+
from .types import AttribFormat, GLTypes, SHADER_TYPE_NAMES, PyGLenum
1919
from .exceptions import ShaderException
2020

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

96-
shaders = [(vertex_shader, gl.GL_VERTEX_SHADER)]
96+
shaders: list[tuple[str, int]] = [(vertex_shader, gl.GL_VERTEX_SHADER)]
9797
if fragment_shader:
9898
shaders.append((fragment_shader, gl.GL_FRAGMENT_SHADER))
9999
if geometry_shader:
@@ -486,7 +486,7 @@ def _query_uniform_block(self, location: int) -> Tuple[int, int, str]:
486486
return index, b_size.value, u_name.value.decode()
487487

488488
@staticmethod
489-
def compile_shader(source: str, shader_type: gl.GLenum) -> gl.GLuint:
489+
def compile_shader(source: str, shader_type: PyGLenum) -> gl.GLuint:
490490
"""Compile the shader code of the given type.
491491
492492
`shader_type` could be GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, ...

arcade/gl/texture.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .buffer import Buffer
88
from .utils import data_to_ctypes
9-
from .types import pixel_formats, BufferOrBufferProtocol
9+
from .types import PyGLuint, pixel_formats, BufferOrBufferProtocol
1010
from ..types import BufferProtocol
1111

1212
if TYPE_CHECKING: # handle import cycle caused by type hinting
@@ -115,8 +115,8 @@ def __init__(
115115
dtype: str = "f1",
116116
data: Optional[BufferProtocol] = None,
117117
filter: Optional[Tuple[gl.GLuint, gl.GLuint]] = None,
118-
wrap_x: Optional[gl.GLuint] = None,
119-
wrap_y: Optional[gl.GLuint] = None,
118+
wrap_x: Optional[PyGLuint] = None,
119+
wrap_y: Optional[PyGLuint] = None,
120120
target=gl.GL_TEXTURE_2D,
121121
depth=False,
122122
samples: int = 0,

arcade/gl/types.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import re
2-
from typing import Optional, Iterable, List, Union
2+
from typing import Optional, Iterable, List, Sequence, Union
33

44
from pyglet import gl
55

@@ -9,6 +9,10 @@
99

1010
BufferOrBufferProtocol = Union[BufferProtocol, Buffer]
1111

12+
GLenumLike = Union[gl.GLenum, int]
13+
PyGLenum = int
14+
GLuintLike = Union[gl.GLuint, int]
15+
PyGLuint = int
1216

1317
_float_base_format = (0, gl.GL_RED, gl.GL_RG, gl.GL_RGB, gl.GL_RGBA)
1418
_int_base_format = (
@@ -102,7 +106,7 @@
102106
}
103107

104108

105-
def gl_name(gl_type: gl.GLenum) -> str:
109+
def gl_name(gl_type: PyGLenum) -> Union[str, PyGLenum]:
106110
"""Return the name of a gl type"""
107111
return GL_NAMES.get(gl_type, gl_type)
108112

@@ -112,7 +116,7 @@ class AttribFormat:
112116
Represents an attribute in a BufferDescription or a Program.
113117
114118
:param str name: Name of the attribute
115-
:param gl.GLEnum gl_type: The OpenGL type such as GL_FLOAT, GL_HALF_FLOAT etc.
119+
:param GLenumLike gl_type: The OpenGL type such as GL_FLOAT, GL_HALF_FLOAT etc.
116120
:param int bytes_per_component: Number of bytes a single component takes
117121
:param int offset: (Optional offset for BufferDescription)
118122
:param int location: (Optional location for program attribute)
@@ -128,9 +132,10 @@ class AttribFormat:
128132
)
129133

130134
def __init__(
131-
self, name: str, gl_type: gl.GLenum, components: int, bytes_per_component: int, offset=0, location=0
135+
self, name: Optional[str], gl_type: Optional[PyGLenum], components: int, bytes_per_component: int, offset=0,
136+
location=0
132137
):
133-
self.name: str = name
138+
self.name = name
134139
self.gl_type = gl_type
135140
self.components = components
136141
self.bytes_per_component = bytes_per_component
@@ -188,7 +193,7 @@ class BufferDescription:
188193

189194
# Describe all variants of a format string to simplify parsing (single component)
190195
# format: gl_type, byte_size
191-
_formats = {
196+
_formats: dict[str, tuple[Optional[PyGLenum], int]] = {
192197
# (gl enum, byte size)
193198
# Floats
194199
"f": (gl.GL_FLOAT, 4),
@@ -227,7 +232,7 @@ def __init__(
227232
self,
228233
buffer: Buffer,
229234
formats: str,
230-
attributes: Iterable[str],
235+
attributes: Sequence[str],
231236
normalized: Optional[Iterable[str]] = None,
232237
instanced: bool = False,
233238
):
@@ -266,7 +271,7 @@ def __init__(
266271
f"attributes ({len(self.attributes)})"
267272
)
268273

269-
def zip_attrs(formats, attributes):
274+
def zip_attrs(formats: list[str], attributes: Sequence[str]):
270275
"""Join together formats and attribute names taking padding into account"""
271276
attr_index = 0
272277
for f in formats:
@@ -344,7 +349,7 @@ class TypeInfo:
344349
"""
345350
__slots__ = "name", "enum", "gl_type", "gl_size", "components"
346351

347-
def __init__(self, name: str, enum: gl.GLenum, gl_type: gl.GLenum, gl_size: int, components: int):
352+
def __init__(self, name: str, enum: GLenumLike, gl_type: PyGLenum, gl_size: int, components: int):
348353
self.name = name
349354
self.enum = enum
350355
self.gl_type = gl_type

arcade/gl/uniform.py

-2
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ def __init__(self, ctx, program_id, location, name, data_type, array_length):
114114
self._components = 0
115115
#: The getter function configured for this uniform
116116
#: The setter function configured for this uniform
117-
self.getter = None
118-
self.setter = None
119117
self._setup_getters_and_setters()
120118

121119
@property

arcade/gl/vertex_array.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from pyglet import gl
66

77
from .buffer import Buffer
8-
from .types import BufferDescription, gl_name
8+
from .types import BufferDescription, GLenumLike, GLuintLike, gl_name
99
from .program import Program
1010

1111
if TYPE_CHECKING: # handle import cycle caused by type hinting
@@ -239,7 +239,7 @@ def _build(
239239
gl.glVertexAttribDivisor(prog_attr.location, 1)
240240

241241
def render(
242-
self, mode: gl.GLenum, first: int = 0, vertices: int = 0, instances: int = 1
242+
self, mode: GLenumLike, first: int = 0, vertices: int = 0, instances: int = 1
243243
):
244244
"""Render the VertexArray to the currently active framebuffer.
245245
@@ -259,7 +259,7 @@ def render(
259259
else:
260260
gl.glDrawArraysInstanced(mode, first, vertices, instances)
261261

262-
def render_indirect(self, buffer: Buffer, mode: gl.GLuint, count, first, stride):
262+
def render_indirect(self, buffer: Buffer, mode: GLuintLike, count, first, stride):
263263
"""
264264
Render the VertexArray to the framebuffer using indirect rendering.
265265
@@ -300,8 +300,8 @@ def render_indirect(self, buffer: Buffer, mode: gl.GLuint, count, first, stride)
300300
def transform_interleaved(
301301
self,
302302
buffer: Buffer,
303-
mode: gl.GLenum,
304-
output_mode: gl.GLenum,
303+
mode: GLenumLike,
304+
output_mode: GLenumLike,
305305
first: int = 0,
306306
vertices: int = 0,
307307
instances: int = 1,
@@ -310,8 +310,8 @@ def transform_interleaved(
310310
"""Run a transform feedback.
311311
312312
:param Buffer buffer: The buffer to write the output
313-
:param gl.GLenum mode: The input primitive mode
314-
:param gl.GLenum output_mode: The output primitive mode
313+
:param GLenumLike mode: The input primitive mode
314+
:param GLenumLike output_mode: The output primitive mode
315315
:param int first: Offset start vertex
316316
:param int vertices: Number of vertices to render
317317
:param int instances: Number of instances to render
@@ -355,8 +355,8 @@ def transform_interleaved(
355355
def transform_separate(
356356
self,
357357
buffers: List[Buffer],
358-
mode: gl.GLenum,
359-
output_mode: gl.GLenum,
358+
mode: GLenumLike,
359+
output_mode: GLenumLike,
360360
first: int = 0,
361361
vertices: int = 0,
362362
instances: int = 1,
@@ -366,8 +366,8 @@ def transform_separate(
366366
Run a transform feedback writing to separate buffers.
367367
368368
:param List[Buffer] buffers: The buffers to write the output
369-
:param gl.GLenum mode: The input primitive mode
370-
:param gl.GLenum output_mode: The output primitive mode
369+
:param GLenumLike mode: The input primitive mode
370+
:param GLenumLike output_mode: The output primitive mode
371371
:param int first: Offset start vertex
372372
:param int vertices: Number of vertices to render
373373
:param int instances: Number of instances to render
@@ -537,7 +537,7 @@ def render(
537537
self,
538538
program: Program,
539539
*,
540-
mode: Optional[gl.GLenum] = None,
540+
mode: Optional[GLenumLike] = None,
541541
first: int = 0,
542542
vertices: Optional[int] = None,
543543
instances: int = 1,
@@ -549,7 +549,7 @@ def render(
549549
or have resized the buffers after the geometry instance was created.
550550
551551
:param Program program: The Program to render with
552-
:param gl.GLenum mode: Override what primitive mode should be used
552+
:param GLenumLike mode: Override what primitive mode should be used
553553
:param int first: Offset start vertex
554554
:param int vertices: Override the number of vertices to render
555555
:param int instances: Number of instances to render

0 commit comments

Comments
 (0)