Skip to content

Commit

Permalink
Move AG enums and structures outside AG class and add the AG prefix +…
Browse files Browse the repository at this point in the history
… convert enum class to inline class to get faster performance (#1138)
  • Loading branch information
soywiz authored Dec 1, 2022
1 parent 515664c commit 4104e42
Show file tree
Hide file tree
Showing 47 changed files with 1,203 additions and 1,050 deletions.
9 changes: 7 additions & 2 deletions kmem/src/commonTest/kotlin/com/soywiz/kmem/BufferTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.soywiz.kmem

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.*

class BufferTest {
@Test
Expand Down Expand Up @@ -163,6 +162,12 @@ class BufferTest {
val buf2 = Int32Buffer(intArrayOf(-1, -2, -3, -4, -5, -6, -7, -8, -9, -10))
arraycopy(buf1, 6, buf2, 7, 3)
assertEquals(listOf(-1, -2, -3, -4, -5, -6, -7, 7, 8, 9), IntArray(buf2.size) { buf2[it] }.toList())
}

@Test
fun testEquality() {
assertEquals(Int32Buffer(intArrayOf(1, 2, 3)), Int32Buffer(intArrayOf(1, 2, 3)))
assertNotEquals(Int32Buffer(intArrayOf(1, 2, 3)), Int32Buffer(intArrayOf(1, 2, 4)))
assertEquals(Int32Buffer(intArrayOf(1, 2, 3)).slice(1), Int32Buffer(intArrayOf(2, 3)))
}
}
17 changes: 17 additions & 0 deletions kmem/src/jsMain/kotlin/com/soywiz/kmem/BufferJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ actual class Buffer(val dataView: DataView) {
dst.sliceUint8Array(dstPosBytes, sizeInBytes).set(src.sliceUint8Array(srcPosBytes, sizeInBytes), 0)
}
}


// @TODO: Optimize by using words instead o bytes
override fun hashCode(): Int {
var h = 1
for (n in 0 until size) h = 31 * h + dataView.getInt8(n)
return h
}

// @TODO: Optimize by using words instead o bytes
override fun equals(other: Any?): Boolean {
if (other !is Buffer || this.size != other.size) return false
val t = this.dataView
val o = other.dataView
for (n in 0 until size) if (t.getInt8(n) != o.getInt8(n)) return false
return true
}
}
actual fun Buffer(size: Int, direct: Boolean): Buffer {
checkNBufferSize(size)
Expand Down
17 changes: 17 additions & 0 deletions kmem/src/jvmAndroidMain/kotlin/com/soywiz/kmem/BufferJvm.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.soywiz.kmem

import java.nio.*
import java.util.*

actual class Buffer(val buffer: ByteBuffer, val offset: Int, val size: Int) {
fun slicedBuffer(roffset: Int = 0, rsize: Int = this.size - roffset): ByteBuffer {
Expand All @@ -14,6 +15,22 @@ actual class Buffer(val buffer: ByteBuffer, val offset: Int, val size: Int) {

override fun toString(): String = NBuffer_toString(this)

// @TODO: Optimize by using words instead o bytes
override fun hashCode(): Int {
var h = 1
for (n in 0 until size) h = 31 * h + buffer[offset + n]
return h
}

// @TODO: Optimize by using words instead o bytes
override fun equals(other: Any?): Boolean {
if (other !is Buffer || this.size != other.size) return false
val t = this.buffer
val o = other.buffer
for (n in 0 until size) if (t[this.offset + n] != o[other.offset + n]) return false
return true
}

actual companion object {
actual fun copy(src: Buffer, srcPosBytes: Int, dst: Buffer, dstPosBytes: Int, sizeInBytes: Int) {

Expand Down
16 changes: 16 additions & 0 deletions kmem/src/nativeMain/kotlin/com/soywiz/kmem/BufferNative.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ actual class Buffer(val data: ByteArray, val offset: Int, val size: Int, dummy:
)
}
}

// @TODO: Optimize by using words instead o bytes
override fun hashCode(): Int {
var h = 1
for (n in 0 until size) h = 31 * h + data[offset + n]
return h
}

// @TODO: Optimize by using words instead o bytes
override fun equals(other: Any?): Boolean {
if (other !is Buffer || this.size != other.size) return false
val t = this.data
val o = other.data
for (n in 0 until size) if (t[this.offset + n] != o[other.offset + n]) return false
return true
}
}
actual fun Buffer(size: Int, direct: Boolean): Buffer {
checkNBufferSize(size)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package samples

import com.soywiz.korag.AG
import com.soywiz.korag.*
import com.soywiz.korev.Key
import com.soywiz.korge.input.keys
import com.soywiz.korge.scene.Scene
Expand Down Expand Up @@ -49,16 +49,16 @@ class MainGpuVectorRendering3 : Scene() {
}
}.xy(pos)

//debugVertexView(pointsList.map { it.vector }, type = AG.DrawType.POINTS)
//debugVertexView(pointsList.map { it.vector }, type = AGDrawType.POINTS)
text(desc, alignment = TextAlignment.BASELINE_LEFT).xy(pos - Point(0, 8))

debugVertexView(path.getPoints2List(), color = Colors.YELLOWGREEN, type = AG.DrawType.LINE_STRIP).xy(pos).apply {
debugVertexView(path.getPoints2List(), color = Colors.YELLOWGREEN, type = AGDrawType.LINE_STRIP).xy(pos).apply {
keys {
down(Key.N8) { visible = !visible }
}
}

debugVertexView(pointsList.map { it.vector }, type = AG.DrawType.LINE_STRIP).xy(pos).apply {
debugVertexView(pointsList.map { it.vector }, type = AGDrawType.LINE_STRIP).xy(pos).apply {
keys {
down(Key.N9) { visible = !visible }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.soywiz.kds.forEachRatio01
import com.soywiz.kds.getCyclic
import com.soywiz.kds.iterators.fastForEach
import com.soywiz.klock.seconds
import com.soywiz.korag.AG
import com.soywiz.korag.*
import com.soywiz.korev.Key
import com.soywiz.korge.input.keys
import com.soywiz.korge.scene.Scene
Expand Down Expand Up @@ -103,10 +103,10 @@ class MainStrokesExperiment2 : Scene() {
val path = buildVectorPath {}
val curves = path.getCurves()
val points = curves.toStrokePointsList(10.0, mode = StrokePointsMode.SCALABLE_POS_NORMAL_WIDTH)
//addChild(DebugVertexView(points.vector, type = AG.DrawType.LINE_STRIP).also { it.color = Colors.WHITE })
val dbv = debugVertexView(points.map { it.vector }, type = AG.DrawType.TRIANGLE_STRIP) { color = Colors.WHITE }
val dbv3 = debugVertexView(type = AG.DrawType.LINE_STRIP) { color = Colors.BLUE.withAd(0.1) }
val dbv2 = debugVertexView(type = AG.DrawType.POINTS) { color = Colors.RED }
//addChild(DebugVertexView(points.vector, type = AGDrawType.LINE_STRIP).also { it.color = Colors.WHITE })
val dbv = debugVertexView(points.map { it.vector }, type = AGDrawType.TRIANGLE_STRIP) { color = Colors.WHITE }
val dbv3 = debugVertexView(type = AGDrawType.LINE_STRIP) { color = Colors.BLUE.withAd(0.1) }
val dbv2 = debugVertexView(type = AGDrawType.POINTS) { color = Colors.RED }
val dbv4 = gpuShapeView { }
//val dbv4 = graphics { }

Expand Down Expand Up @@ -336,7 +336,7 @@ class StrokeView(val points: IVectorArrayList) : View() {
}
gpuShapeViewCommands.verticesEnd()
gpuShapeViewCommands.draw(
AG.DrawType.TRIANGLE_STRIP,
AGDrawType.TRIANGLE_STRIP,
GpuShapeViewPrograms.paintToShaderInfo(Matrix(), Colors.RED, 1.0, 6.0)
)
ctx.flush()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.soywiz.korge.particle

import com.soywiz.kds.flip
import com.soywiz.klock.TimeSpan
import com.soywiz.korag.AG
import com.soywiz.korag.*
import com.soywiz.korge.view.property.*
import com.soywiz.korim.bitmap.BmpSlice
import com.soywiz.korim.color.RGBAf
Expand Down Expand Up @@ -81,9 +81,9 @@ class ParticleEmitter() {
@ViewProperty(groupName = "rotation")
var rotatePerSecondVariance = 0.0.degrees
@ViewProperty
var blendFuncSource = AG.BlendFactor.SOURCE_ALPHA
var blendFuncSource = AGBlendFactor.SOURCE_ALPHA
@ViewProperty
var blendFuncDestination = AG.BlendFactor.ONE
var blendFuncDestination = AGBlendFactor.ONE
@ViewProperty(groupName = "rotation")
var rotationStart = 0.0.degrees
@ViewProperty(groupName = "rotation")
Expand All @@ -100,16 +100,16 @@ class ParticleEmitter() {

companion object {
val blendFactorMap = mapOf(
0 to AG.BlendFactor.ZERO,
1 to AG.BlendFactor.ONE,
0x300 to AG.BlendFactor.SOURCE_COLOR,
0x301 to AG.BlendFactor.ONE_MINUS_SOURCE_COLOR,
0x302 to AG.BlendFactor.SOURCE_ALPHA,
0x303 to AG.BlendFactor.ONE_MINUS_SOURCE_ALPHA,
0x304 to AG.BlendFactor.DESTINATION_ALPHA,
0x305 to AG.BlendFactor.ONE_MINUS_DESTINATION_ALPHA,
0x306 to AG.BlendFactor.DESTINATION_COLOR,
0x307 to AG.BlendFactor.ONE_MINUS_DESTINATION_COLOR,
0 to AGBlendFactor.ZERO,
1 to AGBlendFactor.ONE,
0x300 to AGBlendFactor.SOURCE_COLOR,
0x301 to AGBlendFactor.ONE_MINUS_SOURCE_COLOR,
0x302 to AGBlendFactor.SOURCE_ALPHA,
0x303 to AGBlendFactor.ONE_MINUS_SOURCE_ALPHA,
0x304 to AGBlendFactor.DESTINATION_ALPHA,
0x305 to AGBlendFactor.ONE_MINUS_DESTINATION_ALPHA,
0x306 to AGBlendFactor.DESTINATION_COLOR,
0x307 to AGBlendFactor.ONE_MINUS_DESTINATION_COLOR,
)
val blendFactorMapReversed = blendFactorMap.flip()
val typeMap = Type.values().associateBy { it.index }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.soywiz.korge.particle

import com.soywiz.kds.iterators.fastForEach
import com.soywiz.korag.AG
import com.soywiz.korag.*
import com.soywiz.korim.bitmap.Bitmap32
import com.soywiz.korim.bitmap.context2d
import com.soywiz.korim.bitmap.slice
Expand All @@ -20,13 +20,13 @@ suspend fun VfsFile.readParticleEmitter(): ParticleEmitter {
val emitter = ParticleEmitter()
val particleXml = file.readXml()

//var blendFuncSource = AG.BlendFactor.ONE
//var blendFuncDestination = AG.BlendFactor.ONE
//var blendFuncSource = AGBlendFactor.ONE
//var blendFuncDestination = AGBlendFactor.ONE

particleXml.allChildrenNoComments.fastForEach { item ->
fun point() = Point(item.double("x"), item.double("y"))
fun scalar() = item.double("value")
fun blendFactor() = ParticleEmitter.blendFactorMap[scalar().toInt()] ?: AG.BlendFactor.ONE
fun blendFactor() = ParticleEmitter.blendFactorMap[scalar().toInt()] ?: AGBlendFactor.ONE
fun type() = ParticleEmitter.typeMap[scalar().toInt()] ?: ParticleEmitter.Type.GRAVITY

fun angle() = item.double("value").degrees
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class ParticleEmitterView(
}

if (cachedBlending.factors.srcRGB != emitter.blendFuncSource || cachedBlending.factors.dstRGB != emitter.blendFuncDestination) {
cachedBlending = BlendMode(AG.Blending(emitter.blendFuncSource, emitter.blendFuncDestination))
cachedBlending = BlendMode(AGBlending(emitter.blendFuncSource, emitter.blendFuncDestination))
}

if (fsprites == null || fsprites!!.maxSize < simulator.particles.max) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.soywiz.korge.render

import com.soywiz.kds.*
import com.soywiz.korag.AG
import com.soywiz.korag.*
import com.soywiz.korge.annotations.KorgeExperimental
import com.soywiz.korge.internal.KorgeInternal
import com.soywiz.korim.bitmap.Bitmap
Expand Down Expand Up @@ -102,8 +102,8 @@ class AgBitmapTextureManager(
val base = it.textureBase
base.version = -1
base.base = ag.createTexture(bitmap.premultiplied, targetKind = when (bitmap) {
is MultiBitmap -> AG.TextureTargetKind.TEXTURE_CUBE_MAP
else -> AG.TextureTargetKind.TEXTURE_2D
is MultiBitmap -> AGTextureTargetKind.TEXTURE_CUBE_MAP
else -> AGTextureTargetKind.TEXTURE_2D
})
base.width = bitmap.width
base.height = bitmap.height
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import com.soywiz.kds.fastArrayListOf
import com.soywiz.kds.iterators.fastForEach
import com.soywiz.klogger.Logger
import com.soywiz.kmem.*
import com.soywiz.korag.AG
import com.soywiz.korag.DefaultShaders
import com.soywiz.korag.FragmentShaderDefault
import com.soywiz.korag.VertexShaderDefault
import com.soywiz.korag.*
import com.soywiz.korag.shader.Attribute
import com.soywiz.korag.shader.FragmentShader
import com.soywiz.korag.shader.Operand
Expand Down Expand Up @@ -64,7 +61,7 @@ class BatchBuilder2D constructor(
@KorgeInternal
val viewMat2D: Matrix get() = ctx.viewMat2D
@KorgeInternal
val uniforms: AG.UniformValues get() = ctx.uniforms
val uniforms: AGUniformValues get() = ctx.uniforms

inline fun use(block: (BatchBuilder2D) -> Unit) = ctx.useBatcher(this, block)

Expand Down Expand Up @@ -133,18 +130,18 @@ class BatchBuilder2D constructor(
init { logger.trace { "BatchBuilder2D[4]" } }

/** The current stencil state. If you change it, you must call the [flush] method to ensure everything has been drawn. */
var stencilRef = AG.StencilReferenceState.DEFAULT
var stencilOpFunc = AG.StencilOpFuncState.DEFAULT
var stencilRef = AGStencilReferenceState.DEFAULT
var stencilOpFunc = AGStencilOpFuncState.DEFAULT

init { logger.trace { "BatchBuilder2D[5]" } }

/** The current color mask state. If you change it, you must call the [flush] method to ensure everything has been drawn. */
var colorMask = AG.ColorMaskState()
var colorMask = AGColorMaskState()

init { logger.trace { "BatchBuilder2D[6]" } }

/** The current scissor state. If you change it, you must call the [flush] method to ensure everything has been drawn. */
var scissor: AG.Scissor = AG.Scissor.NIL
var scissor: AGScissor = AGScissor.NIL

private val identity = Matrix()

Expand Down Expand Up @@ -181,7 +178,7 @@ class BatchBuilder2D constructor(
//)

init {
ctx.uniforms.put(AG.UniformValues(
ctx.uniforms.put(AGUniformValues(
*Array(maxTextures) { BatchBuilder2D.u_TexN[it] to textureUnitN[it] },
))
}
Expand Down Expand Up @@ -924,7 +921,7 @@ class BatchBuilder2D constructor(
indices = indexBuffer,
program = program,
//program = PROGRAM_PRE,
type = AG.DrawType.TRIANGLES,
type = AGDrawType.TRIANGLES,
blending = factors.factors(ag.isRenderingToTexture),
uniforms = uniforms,
stencilOpFunc = stencilOpFunc,
Expand Down Expand Up @@ -963,15 +960,15 @@ class BatchBuilder2D constructor(
/**
* Executes [callback] while setting temporarily an [uniform] to a [value]
*/
inline fun setTemporalUniform(uniform: Uniform, value: Any?, flush: Boolean = true, callback: (AG.UniformValues) -> Unit) = ctx.setTemporalUniform(uniform, value, flush, callback)
inline fun setTemporalUniform(uniform: Uniform, value: Any?, flush: Boolean = true, callback: (AGUniformValues) -> Unit) = ctx.setTemporalUniform(uniform, value, flush, callback)

inline fun <reified T> setTemporalUniforms(uniforms: Array<Uniform>, values: Array<T>, count: Int = values.size, olds: Array<T?> = arrayOfNulls<T>(count), flush: Boolean = true, callback: (AG.UniformValues) -> Unit) =
inline fun <reified T> setTemporalUniforms(uniforms: Array<Uniform>, values: Array<T>, count: Int = values.size, olds: Array<T?> = arrayOfNulls<T>(count), flush: Boolean = true, callback: (AGUniformValues) -> Unit) =
ctx.setTemporalUniforms(uniforms, values, count, olds, flush, callback)

/**
* Executes [callback] while setting temporarily a set of [uniforms]
*/
inline fun setTemporalUniforms(uniforms: AG.UniformValues?, callback: (AG.UniformValues) -> Unit) = ctx.setTemporalUniforms(uniforms, callback)
inline fun setTemporalUniforms(uniforms: AGUniformValues?, callback: (AGUniformValues) -> Unit) = ctx.setTemporalUniforms(uniforms, callback)
}

@ThreadLocal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class LineRenderBatcher(
ag.draw(
vertices = vertexBuffer,
program = program,
type = AG.DrawType.LINES,
type = AGDrawType.LINES,
vertexLayout = LAYOUT,
vertexCount = vertexCount,
uniforms = uniforms,
Expand Down
24 changes: 12 additions & 12 deletions korge/src/commonMain/kotlin/com/soywiz/korge/render/MaskStates.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.soywiz.korge.render

import com.soywiz.korag.AG
import com.soywiz.korag.*

object MaskStates {
class RenderState(val stencilOpFunc: AG.StencilOpFuncState, val stencilRef: AG.StencilReferenceState, val colorMask: AG.ColorMaskState) {
val stencilFull get() = AG.StencilFullState(stencilOpFunc, stencilRef)
class RenderState(val stencilOpFunc: AGStencilOpFuncState, val stencilRef: AGStencilReferenceState, val colorMask: AGColorMaskState) {
val stencilFull get() = AGStencilFullState(stencilOpFunc, stencilRef)

@Suppress("DEPRECATION")
fun set(ctx: RenderContext, referenceValue: Int) {
Expand All @@ -22,18 +22,18 @@ object MaskStates {
}

val STATE_NONE = RenderState(
AG.StencilOpFuncState.DEFAULT,
AG.StencilReferenceState.DEFAULT,
AG.ColorMaskState.ALL_ENABLED,
AGStencilOpFuncState.DEFAULT,
AGStencilReferenceState.DEFAULT,
AGColorMaskState.ALL_ENABLED,
)
val STATE_SHAPE = RenderState(
AG.StencilOpFuncState.DEFAULT.withEnabled(true).withCompareMode(AG.CompareMode.ALWAYS).withAction(AG.StencilOp.SET),
AG.StencilReferenceState.DEFAULT.withReferenceValue(0).withReadMask(0x00).withWriteMask(0xFF),
AG.ColorMaskState.ALL_DISABLED
AGStencilOpFuncState.DEFAULT.withEnabled(true).withCompareMode(AGCompareMode.ALWAYS).withAction(AGStencilOp.SET),
AGStencilReferenceState.DEFAULT.withReferenceValue(0).withReadMask(0x00).withWriteMask(0xFF),
AGColorMaskState.ALL_DISABLED
)
val STATE_CONTENT = RenderState(
AG.StencilOpFuncState.DEFAULT.withEnabled(true).withCompareMode(AG.CompareMode.EQUAL).withAction(AG.StencilOp.KEEP),
AG.StencilReferenceState.DEFAULT.withReferenceValue(0).withReadMask(0xFF).withWriteMask(0x00),
AG.ColorMaskState.ALL_ENABLED
AGStencilOpFuncState.DEFAULT.withEnabled(true).withCompareMode(AGCompareMode.EQUAL).withAction(AGStencilOp.KEEP),
AGStencilReferenceState.DEFAULT.withReferenceValue(0).withReadMask(0xFF).withWriteMask(0x00),
AGColorMaskState.ALL_ENABLED
)
}
Loading

0 comments on commit 4104e42

Please sign in to comment.