From 74fce5d5745df954226df6efb52979982fe674f2 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sat, 18 Oct 2025 13:45:28 +0200 Subject: [PATCH] Refactor `ShaderProgram` class for improved readability and structure; add missing imports. Minor HTML formatting adjustments in `index.html`. --- src/jsMain/kotlin/ShaderProgram.kt | 182 +++++++++++++++++------------ src/jsMain/resources/index.html | 5 +- 2 files changed, 108 insertions(+), 79 deletions(-) diff --git a/src/jsMain/kotlin/ShaderProgram.kt b/src/jsMain/kotlin/ShaderProgram.kt index 1ab8738..bb21752 100644 --- a/src/jsMain/kotlin/ShaderProgram.kt +++ b/src/jsMain/kotlin/ShaderProgram.kt @@ -1,6 +1,10 @@ package com.persesgames.shader -import org.khronos.webgl.* +import org.khronos.webgl.Float32Array +import org.khronos.webgl.WebGLBuffer +import org.khronos.webgl.WebGLProgram +import org.khronos.webgl.WebGLRenderingContext +import org.khronos.webgl.WebGLShader /** * User: rnentjes @@ -9,8 +13,8 @@ import org.khronos.webgl.* */ class VertextAttributeInfo(val locationName: String, val numElements: Int) { - var location = 0 - var offset = 0 + var location = 0 + var offset = 0 } class ShaderProgram( @@ -19,98 +23,122 @@ class ShaderProgram( vertexShaderSource: String, fragmentShaderSource: String, val vainfo: Array, - val setter: (program: ShaderProgram, data: T) -> Unit) { + val setter: (program: ShaderProgram, data: T) -> Unit +) { - var shaderProgram: WebGLProgram - var vertex: WebGLShader - var fragment: WebGLShader + var shaderProgram: WebGLProgram + var vertex: WebGLShader + var fragment: WebGLShader - var verticesBlockSize = 0 - var drawLength = 0 + var verticesBlockSize = 0 + var drawLength = 0 - init { - vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) - fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) + init { + vertex = compileShader(vertexShaderSource, WebGLRenderingContext.VERTEX_SHADER) + fragment = compileShader(fragmentShaderSource, WebGLRenderingContext.FRAGMENT_SHADER) - shaderProgram = webgl.createProgram() ?: throw IllegalStateException("Unable to request shader program from webgl context!") - webgl.attachShader(shaderProgram, vertex) - webgl.attachShader(shaderProgram, fragment) - webgl.linkProgram(shaderProgram) + shaderProgram = webgl.createProgram() + ?: throw IllegalStateException("Unable to request shader program from webgl context!") + webgl.attachShader(shaderProgram, vertex) + webgl.attachShader(shaderProgram, fragment) + webgl.linkProgram(shaderProgram) - if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) { - println(webgl.getProgramInfoLog(shaderProgram)) - throw IllegalStateException("Unable to compile shader program!") - } - - webgl.useProgram(shaderProgram) - - this.verticesBlockSize = 0; - - // set attribute locations... - for (info in vainfo.iterator()) { - info.location = webgl.getAttribLocation(shaderProgram, info.locationName) - info.offset = verticesBlockSize; - - verticesBlockSize += info.numElements; - println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); - } - - when(drawType) { - WebGLRenderingContext.TRIANGLES -> { - drawLength = verticesBlockSize * 3 - } - else -> { - drawLength = verticesBlockSize - } - } - - println("verticesBlockSize $verticesBlockSize"); - - println("ShaderProgram constructor done"); + if (webgl.getProgramParameter(shaderProgram, WebGLRenderingContext.LINK_STATUS) == false) { + println(webgl.getProgramInfoLog(shaderProgram)) + throw IllegalStateException("Unable to compile shader program!") } - private fun compileShader(source: String, type: Int): WebGLShader { - val result: WebGLShader + webgl.useProgram(shaderProgram) - result = webgl.createShader(type) ?: throw IllegalStateException("Unable to request shader from webgl context!") - webgl.shaderSource(result, source) - webgl.compileShader(result) + this.verticesBlockSize = 0; - if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) { - throw IllegalStateException("Unable to compile shader!\n${source}\n\n${webgl.getShaderInfoLog(result)}") - } + // set attribute locations... + for (info in vainfo.iterator()) { + info.location = webgl.getAttribLocation(shaderProgram, info.locationName) + info.offset = verticesBlockSize; - return result; + verticesBlockSize += info.numElements; + println("attrib: ${info.locationName}, info.location: ${info.location}, info.offset: ${info.offset}"); } - fun begin(attribBuffer: WebGLBuffer, userdata: T) { - webgl.useProgram(shaderProgram); - webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); + when (drawType) { + WebGLRenderingContext.TRIANGLES -> { + drawLength = verticesBlockSize * 3 + } - // set attribute locations... - for (info in vainfo.iterator()) { - webgl.enableVertexAttribArray(info.location); - webgl.vertexAttribPointer(info.location, info.numElements, WebGLRenderingContext.FLOAT, false, verticesBlockSize * 4, info.offset * 4); - } - - setter(this, userdata) + else -> { + drawLength = verticesBlockSize + } } - fun end() { - for (info in vainfo.iterator()) { - webgl.disableVertexAttribArray(info.location); - } - webgl.useProgram(null) + println("verticesBlockSize $verticesBlockSize"); + + println("ShaderProgram constructor done"); + } + + private fun compileShader(source: String, type: Int): WebGLShader { + val result: WebGLShader = webgl.createShader(type) + ?: throw IllegalStateException("Unable to request shader from webgl context!") + webgl.shaderSource(result, source) + webgl.compileShader(result) + + if (webgl.getShaderParameter(result, WebGLRenderingContext.COMPILE_STATUS) == false) { + throw IllegalStateException( + "Unable to compile shader!\n${source}\n\n${ + webgl.getShaderInfoLog( + result + ) + }" + ) } - fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location); + return result; + } - fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location); + fun begin(attribBuffer: WebGLBuffer, userdata: T) { + webgl.useProgram(shaderProgram); + webgl.bindBuffer(WebGLRenderingContext.ARRAY_BUFFER, attribBuffer); - fun setUniform1f(location: String, value: Float) { webgl.uniform1f(getUniformLocation(location), value); } - fun setUniform2f(location: String, v1: Float, v2: Float) { webgl.uniform2f(getUniformLocation(location), v1, v2); } - fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } - fun setUniform1i(location: String, value: Int) { webgl.uniform1i(getUniformLocation(location), value); } - fun setUniformMatrix4fv(location: String, value: Float32Array) { webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } + // set attribute locations... + for (info in vainfo.iterator()) { + webgl.enableVertexAttribArray(info.location); + webgl.vertexAttribPointer( + info.location, + info.numElements, + WebGLRenderingContext.FLOAT, + false, + verticesBlockSize * 4, + info.offset * 4 + ); + } + + setter(this, userdata) + } + + fun end() { + for (info in vainfo.iterator()) { + webgl.disableVertexAttribArray(info.location); + } + webgl.useProgram(null) + } + + fun getAttribLocation(location: String) = webgl.getAttribLocation(shaderProgram, location); + + fun getUniformLocation(location: String) = webgl.getUniformLocation(shaderProgram, location); + + fun setUniform1f(location: String, value: Float) { + webgl.uniform1f(getUniformLocation(location), value); } + + fun setUniform2f(location: String, v1: Float, v2: Float) { + webgl.uniform2f(getUniformLocation(location), v1, v2); } + + fun setUniform4f(location: String, v1: Float, v2: Float, v3: Float, v4: Float) { + webgl.uniform4f(getUniformLocation(location), v1, v2, v3, v4); } + + fun setUniform1i(location: String, value: Int) { + webgl.uniform1i(getUniformLocation(location), value); } + + fun setUniformMatrix4fv(location: String, value: Float32Array) { + webgl.uniformMatrix4fv(getUniformLocation(location), false, value); } } diff --git a/src/jsMain/resources/index.html b/src/jsMain/resources/index.html index 89e21c7..bb44fb3 100644 --- a/src/jsMain/resources/index.html +++ b/src/jsMain/resources/index.html @@ -11,12 +11,13 @@ padding: 0; overflow: hidden; } + canvas { - position:absolute; + position: absolute; } - +