Skip to content

Commit f9c0025

Browse files
authored
Merge pull request #103 from guzba/master
int32 better supported in shaders.nim
2 parents ce0c714 + 7b4a925 commit f9c0025

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

src/fidget/opengl/shaders.nim

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type
99
name: string
1010
componentType: GLenum
1111
kind: BufferKind
12-
values: array[16, float32]
12+
values: array[64, uint8]
1313
location: GLint
1414
changed: bool # Flag for if this uniform has changed since last bound.
1515

@@ -257,7 +257,7 @@ proc setUniform(
257257
name: string,
258258
componentType: GLenum,
259259
kind: BufferKind,
260-
values: array[16, float32]
260+
values: array[64, uint8]
261261
) =
262262
for uniform in shader.uniforms.mitems:
263263
if uniform.name == name:
@@ -272,6 +272,26 @@ proc setUniform(
272272

273273
echo &"Ignoring setUniform for \"{name}\", not active"
274274

275+
proc setUniform(
276+
shader: Shader,
277+
name: string,
278+
componentType: GLenum,
279+
kind: BufferKind,
280+
values: array[16, float32]
281+
) =
282+
assert componentType == cGL_FLOAT
283+
setUniform(shader, name, componentType, kind, cast[array[64, uint8]](values))
284+
285+
proc setUniform(
286+
shader: Shader,
287+
name: string,
288+
componentType: GLenum,
289+
kind: BufferKind,
290+
values: array[16, int32]
291+
) =
292+
assert componentType == cGL_INT
293+
setUniform(shader, name, componentType, kind, cast[array[64, uint8]](values))
294+
275295
proc raiseUniformVarargsException(name: string, count: int) =
276296
raise newException(
277297
Exception,
@@ -295,9 +315,9 @@ proc raiseUniformKindException(name: string, kind: BufferKind) =
295315
)
296316

297317
proc setUniform*(shader: Shader, name: string, args: varargs[int32]) =
298-
var values: array[16, float32]
318+
var values: array[16, int32]
299319
for i in 0 ..< min(len(args), 16):
300-
values[i] = args[i].float32
320+
values[i] = args[i]
301321

302322
var kind: BufferKind
303323
case len(args):
@@ -353,8 +373,8 @@ proc setUniform*(shader: Shader, name: string, m: Mat4) =
353373
shader.setUniform(name, cGL_FLOAT, bkMAT4, m)
354374

355375
proc setUniform*(shader: Shader, name: string, b: bool) =
356-
var values: array[16, float32]
357-
values[0] = b.float32
376+
var values: array[16, int32]
377+
values[0] = b.int32
358378
shader.setUniform(name, cGL_INT, bkSCALAR, values)
359379

360380
proc bindUniforms*(shader: Shader) =
@@ -368,62 +388,49 @@ proc bindUniforms*(shader: Shader) =
368388
if not uniform.changed:
369389
continue
370390

371-
case uniform.kind:
391+
if uniform.componentType == cGL_INT:
392+
let values = cast[array[16, GLint]](uniform.values)
393+
394+
case uniform.kind:
372395
of bkSCALAR:
373-
if uniform.componentType == cGL_INT:
374-
glUniform1i(uniform.location, uniform.values[0].GLint)
375-
else:
376-
glUniform1f(uniform.location, uniform.values[0])
396+
glUniform1i(uniform.location, values[0])
377397
of bkVEC2:
378-
if uniform.componentType == cGL_INT:
379-
glUniform2i(
380-
uniform.location,
381-
uniform.values[0].GLint,
382-
uniform.values[1].GLint)
383-
else:
384-
glUniform2f(
385-
uniform.location,
386-
uniform.values[0],
387-
uniform.values[1]
388-
)
398+
glUniform2i(uniform.location, values[0], values[1])
389399
of bkVEC3:
390-
if uniform.componentType == cGL_INT:
391-
glUniform3i(
392-
uniform.location,
393-
uniform.values[0].GLint,
394-
uniform.values[1].GLint,
395-
uniform.values[2].GLint
396-
)
397-
else:
398-
glUniform3f(
399-
uniform.location,
400-
uniform.values[0],
401-
uniform.values[1],
402-
uniform.values[2]
403-
)
400+
glUniform3i(uniform.location, values[0], values[1], values[2])
404401
of bkVEC4:
405-
if uniform.componentType == cGL_INT:
406-
glUniform4i(
407-
uniform.location,
408-
uniform.values[0].GLint,
409-
uniform.values[1].GLint,
410-
uniform.values[2].GLint,
411-
uniform.values[3].GLint
412-
)
413-
else:
414-
glUniform4f(
415-
uniform.location,
416-
uniform.values[0],
417-
uniform.values[1],
418-
uniform.values[2],
419-
uniform.values[3]
420-
)
402+
glUniform4i(
403+
uniform.location,
404+
values[0],
405+
values[1],
406+
values[2],
407+
values[3]
408+
)
409+
else:
410+
raiseUniformKindException(uniform.name, uniform.kind)
411+
else:
412+
let values = cast[array[16, float32]](uniform.values)
413+
case uniform.kind:
414+
of bkSCALAR:
415+
glUniform1f(uniform.location, values[0])
416+
of bkVEC2:
417+
glUniform2f(uniform.location, values[0], values[1])
418+
of bkVEC3:
419+
glUniform3f(uniform.location, values[0], values[1], values[2])
420+
of bkVEC4:
421+
glUniform4f(
422+
uniform.location,
423+
values[0],
424+
values[1],
425+
values[2],
426+
values[3]
427+
)
421428
of bkMAT4:
422429
glUniformMatrix4fv(
423430
uniform.location,
424431
1,
425432
GL_FALSE,
426-
uniform.values[0].addr
433+
values[0].unsafeAddr
427434
)
428435
else:
429436
raiseUniformKindException(uniform.name, uniform.kind)

0 commit comments

Comments
 (0)