Skip to content

Commit b6a72e8

Browse files
authored
Attrib ptr fix (#1519)
* Bug: Proper support for int and double attrib data * mypy * Unbreak the world
1 parent 2c4fd95 commit b6a72e8

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

arcade/gl/vertex_array.py

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,12 @@ def _build(
138138
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, index_buffer.glo)
139139

140140
# Lookup dict for BufferDescription attrib names
141-
# print(content)
142141
descr_attribs = {
143142
attr.name: (descr, attr) for descr in content for attr in descr.formats
144143
}
145-
# print('->', descr_attribs)
146144

147145
# Build the vao according to the shader's attribute specifications
148146
for i, prog_attr in enumerate(program.attributes):
149-
# print('prog_attr', prog_attr)
150147
# Do we actually have an attribute with this name in buffer descriptions?
151148
if prog_attr.name.startswith("gl_"):
152149
continue
@@ -160,10 +157,6 @@ def _build(
160157
)
161158
)
162159

163-
# TODO: Sanity check this
164-
# if buff_descr.instanced and i == 0:
165-
# raise ValueError("The first vertex attribute cannot be a per instance attribute.")
166-
167160
# Make sure components described in BufferDescription and in the shader match
168161
if prog_attr.components != attr_descr.components:
169162
raise ValueError(
@@ -173,25 +166,66 @@ def _build(
173166
)
174167
)
175168

176-
# TODO: Compare gltype between buffer descr and program attr
177-
178169
gl.glEnableVertexAttribArray(prog_attr.location)
179170
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, buff_descr.buffer.glo)
180171

181172
# TODO: Detect normalization
182173
normalized = (
183174
gl.GL_TRUE if attr_descr.name in buff_descr.normalized else gl.GL_FALSE
184175
)
185-
gl.glVertexAttribPointer(
186-
prog_attr.location, # attrib location
187-
attr_descr.components, # 1, 2, 3 or 4
188-
attr_descr.gl_type, # GL_FLOAT etc
189-
normalized, # normalize
190-
buff_descr.stride,
191-
c_void_p(attr_descr.offset),
176+
177+
# Map attributes groups
178+
float_types = (gl.GL_FLOAT, gl.GL_HALF_FLOAT)
179+
double_types = (gl.GL_DOUBLE,)
180+
int_types = (
181+
gl.GL_INT, gl.GL_UNSIGNED_INT,
182+
gl.GL_SHORT, gl.GL_UNSIGNED_SHORT,
183+
gl.GL_BYTE, gl.GL_UNSIGNED_BYTE,
192184
)
185+
attrib_type = attr_descr.gl_type
186+
# Normalized integers must be mapped as floats
187+
if attrib_type in int_types and buff_descr.normalized:
188+
attrib_type = prog_attr.gl_type
189+
190+
# Sanity check attribute types between shader and buffer description
191+
if attrib_type != prog_attr.gl_type:
192+
raise ValueError(
193+
(
194+
f"Program attribute '{prog_attr.name}' has type {prog_attr.gl_type} "
195+
f"while the buffer description has type {attr_descr.gl_type}. "
196+
)
197+
)
198+
199+
if attrib_type in float_types:
200+
gl.glVertexAttribPointer(
201+
prog_attr.location, # attrib location
202+
attr_descr.components, # 1, 2, 3 or 4
203+
attr_descr.gl_type, # GL_FLOAT etc
204+
normalized, # normalize
205+
buff_descr.stride,
206+
c_void_p(attr_descr.offset),
207+
)
208+
elif attrib_type in double_types:
209+
gl.glVertexAttribLPointer(
210+
prog_attr.location, # attrib location
211+
attr_descr.components, # 1, 2, 3 or 4
212+
attr_descr.gl_type, # GL_DOUBLE etc
213+
buff_descr.stride,
214+
c_void_p(attr_descr.offset),
215+
)
216+
elif attrib_type in int_types:
217+
gl.glVertexAttribIPointer(
218+
prog_attr.location, # attrib location
219+
attr_descr.components, # 1, 2, 3 or 4
220+
attr_descr.gl_type, # GL_FLOAT etc
221+
buff_descr.stride,
222+
c_void_p(attr_descr.offset),
223+
)
224+
else:
225+
raise ValueError(f"Unsupported attribute type: {attr_descr.gl_type}")
226+
193227
# print((
194-
# f"gl.glVertexAttribPointer(\n"
228+
# f"gl.glVertexAttribXPointer(\n"
195229
# f" {prog_attr.location}, # attrib location\n"
196230
# f" {attr_descr.components}, # 1, 2, 3 or 4\n"
197231
# f" {attr_descr.gl_type}, # GL_FLOAT etc\n"

arcade/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_raspberry_pi_info() -> Tuple[bool, str, str]:
5454

5555
# The platform for raspi should always be linux
5656
if not sys.platform == "linux":
57-
return False, 0, ""
57+
return False, "", ""
5858

5959
# Check for model info file
6060
MODEL_PATH = Path("/sys/firmware/devicetree/base/model")

0 commit comments

Comments
 (0)