Skip to content

Commit

Permalink
Merge pull request #15 from baaahs/uniform-v
Browse files Browse the repository at this point in the history
Support uniform float/int arrays
  • Loading branch information
gergelydaniel authored Aug 19, 2023
2 parents 9849284 + 5d71d03 commit 8ffc18d
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 94 deletions.
57 changes: 44 additions & 13 deletions kgl-android/src/main/kotlin/com/danielgergely/kgl/KglAndroid.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,46 @@ object KglAndroid : Kgl {
override fun enable(cap: Int) = GL.glEnable(cap)
override fun disable(cap: Int) = GL.glDisable(cap)

override fun uniform1f(location: UniformLocation, f: Float) = GL.glUniform1f(location, f)
override fun uniform1i(location: UniformLocation, i: Int) = GL.glUniform1i(location, i)

override fun uniform2f(location: UniformLocation, x: Float, y: Float) = GL.glUniform2f(location, x, y)
override fun uniform2i(location: UniformLocation, x: Int, y: Int) = GL.glUniform2i(location, x, y)

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) = GL.glUniform3f(location, x, y, z)
override fun uniform3fv(location: UniformLocation, value: FloatArray) = GL.glUniform3fv(location, 1, value, 0)
override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) = GL.glUniform3i(location, x, y, z)
override fun uniform1f(location: UniformLocation, f: Float) =
GL.glUniform1f(location, f)
override fun uniform1fv(location: UniformLocation, value: FloatArray) =
GL.glUniform1fv(location, value.vSize(1), value, 0)
override fun uniform1i(location: UniformLocation, i: Int) =
GL.glUniform1i(location, i)
override fun uniform1iv(location: UniformLocation, value: IntArray) =
GL.glUniform1iv(location, value.vSize(1), value, 0)

override fun uniform2f(location: UniformLocation, x: Float, y: Float) =
GL.glUniform2f(location, x, y)
override fun uniform2fv(location: UniformLocation, value: FloatArray) =
GL.glUniform2fv(location, value.vSize(2), value, 0)
override fun uniform2i(location: UniformLocation, x: Int, y: Int) =
GL.glUniform2i(location, x, y)
override fun uniform2iv(location: UniformLocation, value: IntArray) =
GL.glUniform2iv(location, value.vSize(2), value, 0)

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) =
GL.glUniform3f(location, x, y, z)
override fun uniform3fv(location: UniformLocation, value: FloatArray) =
GL.glUniform3fv(location, value.vSize(3), value, 0)
override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) =
GL.glUniform3i(location, x, y, z)
override fun uniform3iv(location: UniformLocation, value: IntArray) =
GL.glUniform3iv(location, value.vSize(3), value, 0)

override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) =
GL.glUniform4f(location, x, y, z, w)

override fun uniform4fv(location: UniformLocation, value: FloatArray) =
GL.glUniform4fv(location, value.vSize(4), value, 0)
override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) =
GL.glUniform4i(location, x, y, z, w)
override fun uniform4iv(location: UniformLocation, value: IntArray) =
GL.glUniform4iv(location, value.vSize(4), value, 0)

override fun uniformMatrix3fv(location: Int, transpose: Boolean, value: FloatArray) =
GL.glUniformMatrix3fv(location, 1, transpose, value, 0)

GL.glUniformMatrix3fv(location, value.vSize(3*3), transpose, value, 0)
override fun uniformMatrix4fv(location: Int, transpose: Boolean, value: FloatArray) =
GL.glUniformMatrix4fv(location, 1, transpose, value, 0)
GL.glUniformMatrix4fv(location, value.vSize(4*4), transpose, value, 0)

override fun blendFunc(sFactor: Int, dFactor: Int) = GL.glBlendFunc(sFactor, dFactor)

Expand Down Expand Up @@ -240,3 +259,15 @@ object KglAndroid : Kgl {
}
}
}

private fun FloatArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}

private fun IntArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}
70 changes: 40 additions & 30 deletions kgl-ios/src/nativeMain/kotlin/com.danielgergely.kgl/KglIos.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,49 +195,47 @@ object KglIos : Kgl {
)
}

override fun uniform1f(location: UniformLocation, f: Float) {
override fun uniform1f(location: UniformLocation, f: Float) =
glUniform1f(location, f)
}

override fun uniform1i(location: UniformLocation, i: Int) {
override fun uniform1fv(location: UniformLocation, value: FloatArray) =
glUniform1fv(location, value.vSize(1), value.refTo(0))
override fun uniform1i(location: UniformLocation, i: Int) =
glUniform1i(location, i)
}
override fun uniform1iv(location: UniformLocation, value: IntArray) =
glUniform1iv(location, value.vSize(1), value.refTo(0))

override fun uniform2f(location: UniformLocation, x: Float, y: Float) {
override fun uniform2f(location: UniformLocation, x: Float, y: Float) =
glUniform2f(location, x, y)
}

override fun uniform2i(location: UniformLocation, x: Int, y: Int) {
override fun uniform2fv(location: UniformLocation, value: FloatArray) =
glUniform2fv(location, value.vSize(2), value.refTo(0))
override fun uniform2i(location: UniformLocation, x: Int, y: Int) =
glUniform2i(location, x, y)
}
override fun uniform2iv(location: UniformLocation, value: IntArray) =
glUniform2iv(location, value.vSize(2), value.refTo(0))

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) {
override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) =
glUniform3f(location, x, y, z)
}

override fun uniform3fv(location: UniformLocation, value: FloatArray) {
glUniform3fv(location, 1, value.refTo(0))
}

override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) {
override fun uniform3fv(location: UniformLocation, value: FloatArray) =
glUniform3fv(location, value.vSize(3), value.refTo(0))
override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) =
glUniform3i(location, x, y, z)
}
override fun uniform3iv(location: UniformLocation, value: IntArray) =
glUniform3iv(location, value.vSize(3), value.refTo(0))

override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) {
override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) =
glUniform4f(location, x, y, z, w)
}

override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) {
override fun uniform4fv(location: UniformLocation, value: FloatArray) =
glUniform4fv(location, value.vSize(4), value.refTo(0))
override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) =
glUniform4i(location, x, y, z, w)
}
override fun uniform4iv(location: UniformLocation, value: IntArray) =
glUniform4iv(location, value.vSize(4), value.refTo(0))

override fun uniformMatrix3fv(location: UniformLocation, transpose: Boolean, value: FloatArray) {
glUniformMatrix3fv(location, 1, transpose.toGl(), value.refTo(0))
}
override fun uniformMatrix3fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
glUniformMatrix3fv(location, value.vSize(3*3), transpose.toGl(), value.refTo(0))

override fun uniformMatrix4fv(location: UniformLocation, transpose: Boolean, value: FloatArray) {
glUniformMatrix4fv(location, 1, transpose.toGl(), value.refTo(0))
}
override fun uniformMatrix4fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
glUniformMatrix4fv(location, value.vSize(4*4), transpose.toGl(), value.refTo(0))

override fun blendFunc(sFactor: Int, dFactor: Int) {
glBlendFunc(sFactor.toUInt(), dFactor.toUInt())
Expand Down Expand Up @@ -468,3 +466,15 @@ object KglIos : Kgl {
private fun Boolean.toGl(): GLboolean = if (this) 1u else 0u

private fun UByte.toBoolean() = this != platform.gles3.GL_FALSE.toUByte()

private fun FloatArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}

private fun IntArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}
61 changes: 44 additions & 17 deletions kgl-jogl/src/main/kotlin/com/danielgergely/kgl/KglJogl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,31 +142,46 @@ class KglJogl(private val gl: GL) : Kgl {
return gl.glVertexAttribPointer(location, size, type, normalized, stride, offset.toLong())
}

override fun uniform1f(location: UniformLocation, f: Float) = gl.glUniform1f(location, f)

override fun uniform1i(location: UniformLocation, i: Int) = gl.glUniform1i(location, i)

override fun uniform2f(location: UniformLocation, x: Float, y: Float) = gl.glUniform2f(location, x, y)

override fun uniform2i(location: UniformLocation, x: Int, y: Int) = gl.glUniform2i(location, x, y)

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) = gl.glUniform3f(location, x, y, z)

override fun uniform3fv(location: UniformLocation, value: FloatArray) = gl.glUniform3fv(location, 1, value, 0)

override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) = gl.glUniform3i(location, x, y, z)
override fun uniform1f(location: UniformLocation, f: Float) =
gl.glUniform1f(location, f)
override fun uniform1fv(location: UniformLocation, value: FloatArray) =
gl.glUniform1fv(location, value.vSize(1), value, 0)
override fun uniform1i(location: UniformLocation, i: Int) =
gl.glUniform1i(location, i)
override fun uniform1iv(location: UniformLocation, value: IntArray) =
gl.glUniform1iv(location, value.vSize(1), value, 0)

override fun uniform2f(location: UniformLocation, x: Float, y: Float) =
gl.glUniform2f(location, x, y)
override fun uniform2fv(location: UniformLocation, value: FloatArray) =
gl.glUniform2fv(location, value.vSize(2), value, 0)
override fun uniform2i(location: UniformLocation, x: Int, y: Int) =
gl.glUniform2i(location, x, y)
override fun uniform2iv(location: UniformLocation, value: IntArray) =
gl.glUniform2iv(location, value.vSize(2), value, 0)

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) =
gl.glUniform3f(location, x, y, z)
override fun uniform3fv(location: UniformLocation, value: FloatArray) =
gl.glUniform3fv(location, value.vSize(3), value, 0)
override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) =
gl.glUniform3i(location, x, y, z)
override fun uniform3iv(location: UniformLocation, value: IntArray) =
gl.glUniform3iv(location, value.vSize(3), value, 0)

override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) =
gl.glUniform4f(location, x, y, z, w)

override fun uniform4fv(location: UniformLocation, value: FloatArray) =
gl.glUniform4fv(location, value.vSize(4), value, 0)
override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) =
gl.glUniform4i(location, x, y, z, w)
override fun uniform4iv(location: UniformLocation, value: IntArray) =
gl.glUniform4iv(location, value.vSize(4), value, 0)

override fun uniformMatrix3fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
gl.glUniformMatrix3fv(location, 1, transpose, value, 0)

gl.glUniformMatrix3fv(location, value.vSize(3*3), transpose, value, 0)
override fun uniformMatrix4fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
gl.glUniformMatrix4fv(location, 1, transpose, value, 0)
gl.glUniformMatrix4fv(location, value.vSize(4*4), transpose, value, 0)

override fun blendFunc(sFactor: Int, dFactor: Int) = gl.glBlendFunc(sFactor, dFactor)

Expand Down Expand Up @@ -318,3 +333,15 @@ fun imageToByteBuffer(image: BufferedImage): ByteBuffer {

return buffer
}

private fun FloatArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}

private fun IntArray.vSize(vecSize: Int): Int {
if (size % vecSize != 0)
throw IllegalArgumentException("Array size must be a multiple of $vecSize.")
return size / vecSize
}
53 changes: 25 additions & 28 deletions kgl-lwjgl/src/main/kotlin/com/danielgergely/kgl/KglLwjgl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -267,49 +267,46 @@ object KglLwjgl : Kgl {
GL.glTexParameteri(target, pname, value)
}

override fun uniform1f(location: UniformLocation, f: Float) {
override fun uniform1f(location: UniformLocation, f: Float) =
GL.glUniform1f(location, f)
}

override fun uniform1i(location: UniformLocation, i: Int) {
override fun uniform1fv(location: UniformLocation, value: FloatArray) =
GL.glUniform1fv(location, value)
override fun uniform1i(location: UniformLocation, i: Int) =
GL.glUniform1i(location, i)
}
override fun uniform1iv(location: UniformLocation, value: IntArray) =
GL.glUniform1iv(location, value)

override fun uniform2f(location: UniformLocation, x: Float, y: Float) {
override fun uniform2f(location: UniformLocation, x: Float, y: Float) =
GL.glUniform2f(location, x, y)
}

override fun uniform2i(location: UniformLocation, x: Int, y: Int) {
override fun uniform2fv(location: UniformLocation, value: FloatArray) =
GL.glUniform2fv(location, value)
override fun uniform2i(location: UniformLocation, x: Int, y: Int) =
GL.glUniform2i(location, x, y)
}
override fun uniform2iv(location: UniformLocation, value: IntArray) =
GL.glUniform2iv(location, value)

override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) {
override fun uniform3f(location: UniformLocation, x: Float, y: Float, z: Float) =
GL.glUniform3f(location, x, y, z)
}

override fun uniform3fv(location: UniformLocation, value: FloatArray) {
override fun uniform3fv(location: UniformLocation, value: FloatArray) =
GL.glUniform3fv(location, value)
}

override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) {
override fun uniform3i(location: UniformLocation, x: Int, y: Int, z: Int) =
GL.glUniform3i(location, x, y, z)
}
override fun uniform3iv(location: UniformLocation, value: IntArray) =
GL.glUniform3iv(location, value)

override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) {
override fun uniform4f(location: UniformLocation, x: Float, y: Float, z: Float, w: Float) =
GL.glUniform4f(location, x, y, z, w)
}

override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) {
override fun uniform4fv(location: UniformLocation, value: FloatArray) =
GL.glUniform4fv(location, value)
override fun uniform4i(location: UniformLocation, x: Int, y: Int, z: Int, w: Int) =
GL.glUniform4i(location, x, y, z, w)
}
override fun uniform4iv(location: UniformLocation, value: IntArray) =
GL.glUniform4iv(location, value)

override fun uniformMatrix3fv(location: UniformLocation, transpose: Boolean, value: FloatArray) {
override fun uniformMatrix3fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
GL.glUniformMatrix3fv(location, transpose, value)
}

override fun uniformMatrix4fv(location: UniformLocation, transpose: Boolean, value: FloatArray) {
override fun uniformMatrix4fv(location: UniformLocation, transpose: Boolean, value: FloatArray) =
GL.glUniformMatrix4fv(location, transpose, value)
}

override fun useProgram(programId: Program) {
GL.glUseProgram(programId)
Expand Down
Loading

0 comments on commit 8ffc18d

Please sign in to comment.