Skip to content

Commit

Permalink
Bump to kotlin-wrappers 1.0.0-pre.662.
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Dec 9, 2023
1 parent b0aed51 commit cf4edb5
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 104 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Versions {

// JS:
const val kotlinxHtml = "0.9.1"
const val kotlinWrappers = "1.0.0-pre.648"
const val kotlinWrappers = "1.0.0-pre.662"

// Test:
const val junit = "5.8.1"
Expand Down
8 changes: 6 additions & 2 deletions src/jsMain/kotlin/baaahs/View.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package baaahs

import web.canvas.RenderingContextId
import web.canvas.CanvasRenderingContext2D
import web.dom.DOMTokenList
import web.dom.Element
import web.gl.WebGL2RenderingContext
import web.gl.WebGLRenderingContext
import web.html.HTMLCanvasElement
import web.html.HTMLElement

Expand All @@ -29,4 +31,6 @@ fun DOMTokenList.clear() {
fun <T : HTMLElement> HTMLElement.first(className: String) : T =
(getElementsByClassName(className)[0] as T?)!!

fun HTMLCanvasElement.context2d() = this.getContext(RenderingContextId.canvas)!!
fun HTMLCanvasElement.get2DContext() = this.getContext(CanvasRenderingContext2D.ID)!!
fun HTMLCanvasElement.getWebGLContext() = this.getContext(WebGLRenderingContext.ID)
fun HTMLCanvasElement.getWebGL2Context() = this.getContext(WebGL2RenderingContext.ID)
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private val DevModeToolbarMenuView = xComponent<DevModeToolbarMenuProps>("DevMod
lastStats = curStats
forceRender()
}
}, ms = 50)
}, timeout = 50)

withCleanup {
clearInterval(callback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import baaahs.app.ui.dialog.DialogStyles
import baaahs.app.ui.dialog.dialogPanels
import baaahs.ui.*
import external.ErrorBoundary
import external.mui.Portal
import js.core.jso
import mui.base.Portal
import mui.icons.material.Redo
import mui.icons.material.Undo
import mui.material.*
Expand Down
4 changes: 2 additions & 2 deletions src/jsMain/kotlin/baaahs/app/ui/gadgets/color/ColorWheel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package baaahs.app.ui.gadgets.color

import baaahs.Color
import baaahs.geom.Vector2F
import baaahs.get2DContext
import web.canvas.CanvasRenderingContext2D
import web.canvas.RenderingContextId
import web.html.HTMLCanvasElement
import kotlin.math.floor
import kotlin.math.max
Expand Down Expand Up @@ -50,7 +50,7 @@ class ColorWheel(
var harmonyMode: HarmonyMode = HarmonyMode.triad,
var colors: Array<Color> = Array(3) { Color.WHITE }
) {
private val ctx = canvasEl.getContext(RenderingContextId.canvas) as CanvasRenderingContext2D
private val ctx = canvasEl.get2DContext()

private var priorRadius: Int? = null

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import react.dom.*
import react.dom.events.MouseEvent
import react.useRef
import styled.inlineStyles
import web.animations.requestAnimationFrame
import web.html.HTMLCanvasElement
import web.html.HTMLElement
import web.timers.requestAnimationFrame
import kotlin.math.min

private const val pickerRadius = 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import baaahs.ui.unaryPlus
import baaahs.ui.xComponent
import baaahs.unknown
import baaahs.util.useResizeListener
import external.mui.Portal
import external.react_resizable.buildResizeHandle
import kotlinx.css.*
import materialui.icon
import mui.base.Portal
import mui.material.*
import react.*
import react.dom.div
Expand Down
2 changes: 1 addition & 1 deletion src/jsMain/kotlin/baaahs/app/ui/preview/ClientPreview.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import org.w3c.dom.get
import web.timers.requestAnimationFrame
import web.animations.requestAnimationFrame

class ClientPreview(
model: Model,
Expand Down
55 changes: 27 additions & 28 deletions src/jsMain/kotlin/baaahs/gl/JsGlBase.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package baaahs.gl

import baaahs.document
import baaahs.getWebGL2Context
import baaahs.getWebGLContext
import com.danielgergely.kgl.Kgl
import com.danielgergely.kgl.KglJs
import org.khronos.webgl.ArrayBufferView
import org.khronos.webgl.WebGLObject
import web.canvas.RenderingContextId
import org.khronos.webgl.WebGLRenderingContext
import web.gl.WebGL2RenderingContext
import web.html.HTMLCanvasElement
import web.prompts.alert

Expand All @@ -16,7 +17,7 @@ actual object GlBase {
class JsGlManager : GlManager() {
override val available: Boolean by lazy {
val canvas = document.createElement("canvas") as HTMLCanvasElement
val gl = canvas.getContext(RenderingContextId.webgl)
val gl = canvas.getWebGLContext()
gl != null
}

Expand All @@ -26,7 +27,7 @@ actual object GlBase {
}

fun createContext(canvas: HTMLCanvasElement, trace: Boolean = false): JsGlContext {
val webgl = canvas.getContext(RenderingContextId.webgl2) as WebGL2RenderingContext?
val webgl = canvas.getWebGL2Context()
if (webgl == null) {
alert(
"Running GLSL shows on iOS requires WebGL 2.0.\n" +
Expand All @@ -37,7 +38,7 @@ actual object GlBase {
}
return JsGlContext(
canvas,
maybeTrace(KglJs(webgl), trace),
maybeTrace(KglJs(webgl.unsafeCast<WebGLRenderingContext>()), trace),
"300 es",
webgl
)
Expand Down Expand Up @@ -75,11 +76,11 @@ actual object GlBase {

/** Creates a related context with shared state and the given Kgl. */
open fun requestAnimationFrame(callback: (Double) -> Unit) {
web.timers.requestAnimationFrame(callback)
web.animations.requestAnimationFrame(callback)
}

private fun ensureExtension(name: String, required: Boolean): Boolean {
val extension = webgl.getExtension(name)
val extension = webgl.getExtension(name) as Any?
if (required && extension == null) {
alert("$name not supported")
throw Exception("$name not supported")
Expand All @@ -89,23 +90,21 @@ actual object GlBase {
}
}

abstract external class WebGL2RenderingContext : com.danielgergely.kgl.WebGL2RenderingContext {
fun fenceSync(condition: Int, flags: Int): WebGLSync
fun clientWaitSync(sync: WebGLSync, flags: Int, timeout: Number): Int
fun deleteSync(sync: WebGLSync): WebGLSync
fun readPixels(x: Int, y: Int, width: Int, height: Int, format: Int, type: Int, offset: Int)
fun getBufferSubData(target: Int, srcByteOffset: Int, dstData: ArrayBufferView, dstOffset: Int, length: Int)

companion object {
val PIXEL_PACK_BUFFER: Int
val SYNC_GPU_COMMANDS_COMPLETE: Int
val ALREADY_SIGNALED: Int
val TIMEOUT_EXPIRED: Int
val CONDITION_SATISFIED: Int
val WAIT_FAILED: Int
val STATIC_READ: Int
val STREAM_READ: Int
}
}

external class WebGLSync : WebGLObject
//abstract external class WebGL2RenderingContext : com.danielgergely.kgl.WebGL2RenderingContext {
// fun fenceSync(condition: Int, flags: Int): WebGLSync
// fun clientWaitSync(sync: WebGLSync, flags: Int, timeout: Number): Int
// fun deleteSync(sync: WebGLSync): WebGLSync
// fun readPixels(x: Int, y: Int, width: Int, height: Int, format: Int, type: Int, offset: Int)
// fun getBufferSubData(target: Int, srcByteOffset: Int, dstData: ArrayBufferView, dstOffset: Int, length: Int)
//
// companion object {
// val PIXEL_PACK_BUFFER: Int
// val SYNC_GPU_COMMANDS_COMPLETE: Int
// val ALREADY_SIGNALED: Int
// val TIMEOUT_EXPIRED: Int
// val CONDITION_SATISFIED: Int
// val WAIT_FAILED: Int
// val STATIC_READ: Int
// val STREAM_READ: Int
// }
//}
12 changes: 7 additions & 5 deletions src/jsMain/kotlin/baaahs/gl/preview/MovingHeadPreview.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import baaahs.fixtures.FixtureTypeRenderPlan
import baaahs.fixtures.NullTransport
import baaahs.fixtures.ProgramRenderPlan
import baaahs.fixtures.movingHeadFixture
import baaahs.get2DContext
import baaahs.gl.GlContext
import baaahs.gl.glsl.GlslProgram
import baaahs.gl.render.ComponentRenderEngine
Expand All @@ -15,11 +16,12 @@ import baaahs.model.MovingHead
import baaahs.plugin.core.MovingHeadParams
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import web.canvas.CanvasRenderingContext2D
import web.canvas.RenderingContextId
import web.animations.requestAnimationFrame
import web.html.HTMLCanvasElement
import web.timers.requestAnimationFrame
import kotlin.math.*
import kotlin.math.PI
import kotlin.math.cos
import kotlin.math.roundToInt
import kotlin.math.sin


private val Float.short: String
Expand Down Expand Up @@ -50,7 +52,7 @@ class MovingHeadPreview(
val fixture = movingHeadFixture(movingHead, 1, movingHead.name, NullTransport, movingHead.adapter)
renderEngine.addFixture(fixture)
}
private val context2d = canvas2d.getContext(RenderingContextId.canvas) as CanvasRenderingContext2D
private val context2d = canvas2d.get2DContext()

override fun start() {
running = true
Expand Down
7 changes: 3 additions & 4 deletions src/jsMain/kotlin/baaahs/gl/preview/ProjectionPreview.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import baaahs.fixtures.NullTransport
import baaahs.fixtures.ProgramRenderPlan
import baaahs.geom.Vector2D
import baaahs.geom.Vector3F
import baaahs.get2DContext
import baaahs.gl.GlContext
import baaahs.gl.glsl.GlslProgram
import baaahs.gl.render.ComponentRenderEngine
Expand All @@ -20,11 +21,9 @@ import baaahs.model.PixelArray
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import three.js.Vector2
import web.canvas.CanvasRenderingContext2D
import web.animations.requestAnimationFrame
import web.canvas.Path2D
import web.canvas.RenderingContextId
import web.html.HTMLCanvasElement
import web.timers.requestAnimationFrame

class ProjectionPreview(
canvas2d: HTMLCanvasElement,
Expand Down Expand Up @@ -75,7 +74,7 @@ class ProjectionPreview(
}
}

private val context2d = canvas2d.getContext(RenderingContextId.canvas) as CanvasRenderingContext2D
private val context2d = canvas2d.get2DContext()

override fun start() {
running = true
Expand Down
22 changes: 16 additions & 6 deletions src/jsMain/kotlin/baaahs/gl/render/WebGL2ResultDeliveryStrategy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package baaahs.gl.render
import baaahs.document
import baaahs.gl.GlBase
import baaahs.gl.GlContext
import baaahs.gl.WebGL2RenderingContext
import baaahs.gl.WebGL2RenderingContext.Companion.PIXEL_PACK_BUFFER
import baaahs.gl.WebGL2RenderingContext.Companion.STREAM_READ
import baaahs.gl.WebGL2RenderingContext.Companion.SYNC_GPU_COMMANDS_COMPLETE
import baaahs.gl.WebGLSync
import baaahs.gl.result.ResultStorage
import baaahs.internalTimerClock
import com.danielgergely.kgl.Buffer
import com.danielgergely.kgl.GlBuffer
import com.danielgergely.kgl.Kgl
import js.buffer.ArrayBufferView
import kotlinx.coroutines.delay
import web.gl.GLenum
import web.gl.WebGL2RenderingContext
import web.gl.WebGL2RenderingContext.Companion.PIXEL_PACK_BUFFER
import web.gl.WebGL2RenderingContext.Companion.STREAM_READ
import web.gl.WebGL2RenderingContext.Companion.SYNC_GPU_COMMANDS_COMPLETE
import web.gl.WebGLSync

actual fun pickResultDeliveryStrategy(gl: GlContext): ResultDeliveryStrategy {
return SwitchingResultDeliveryStrategy(gl as GlBase.JsGlContext)
Expand Down Expand Up @@ -76,7 +79,9 @@ class WebGl2ResultDeliveryStrategy(private val gl: GlBase.JsGlContext) : ResultD
gl.check { bindBuffer(PIXEL_PACK_BUFFER, glBuf) }
gl.check {
webgl2.getBufferSubData(
PIXEL_PACK_BUFFER, 0, cpuBuffer.getJsBufferWithOffset(), 0, 0
PIXEL_PACK_BUFFER, 0,
cpuBuffer.getJsBufferWithOffset().unsafeCast<ArrayBufferView>(),
0, 0
)
}
gl.check { bindBuffer(PIXEL_PACK_BUFFER, null) }
Expand All @@ -86,12 +91,17 @@ class WebGl2ResultDeliveryStrategy(private val gl: GlBase.JsGlContext) : ResultD
}
}

private fun Kgl.bindBuffer(target: GLenum, bufferId: GlBuffer?) =
bindBuffer(target as Int, bufferId)

class FenceSync(private val gl: GlBase.JsGlContext) {
private val webgl2 = gl.webgl

private val fenceSync = gl.check { webgl2.fenceSync(SYNC_GPU_COMMANDS_COMPLETE, 0) }

suspend fun await() {
fenceSync ?: error("Fence sync not supported.")

gl.check { webgl2.flush() }

val startTime = internalTimerClock.now()
Expand Down
6 changes: 3 additions & 3 deletions src/jsMain/kotlin/baaahs/imaging/CanvasBitmap.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package baaahs.imaging

import baaahs.MediaDevices
import baaahs.context2d
import baaahs.get2DContext
import com.danielgergely.kgl.ByteBuffer
import org.khronos.webgl.ArrayBuffer
import org.khronos.webgl.Uint8Array
Expand All @@ -15,7 +15,7 @@ open class CanvasBitmap(internal val canvas: HTMLCanvasElement) : Bitmap {
override val width = canvas.width
override val height = canvas.height

internal val ctx = canvas.context2d()
internal val ctx = canvas.get2DContext()

override fun drawImage(image: Image) {
(image as JsImage).draw(ctx, 0, 0)
Expand Down Expand Up @@ -113,7 +113,7 @@ open class CanvasBitmap(internal val canvas: HTMLCanvasElement) : Bitmap {

override fun clone(): Bitmap {
val newCanvas = createCanvas(canvas.width, canvas.height)
val ctx = newCanvas.context2d()
val ctx = newCanvas.get2DContext()
ctx.drawImage(canvas, 0.0, 0.0)
return CanvasBitmap(newCanvas)
}
Expand Down
Loading

0 comments on commit cf4edb5

Please sign in to comment.