Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplying CloneTileData/CloneBatchData as a raw array #629

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions indigo/indigo/src/main/scala/indigo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -786,15 +786,12 @@ val CloneBlank: shared.scenegraph.CloneBlank.type = shared.scenegraph.CloneBlank
type CloneId = shared.scenegraph.CloneId
val CloneId: shared.scenegraph.CloneId.type = shared.scenegraph.CloneId

type CloneBatch = shared.scenegraph.CloneBatch
val CloneBatch: shared.scenegraph.CloneBatch.type = shared.scenegraph.CloneBatch
type Clones = shared.scenegraph.Clones
val Clones: shared.scenegraph.Clones.type = shared.scenegraph.Clones

type CloneBatchData = shared.scenegraph.CloneBatchData
val CloneBatchData: shared.scenegraph.CloneBatchData.type = shared.scenegraph.CloneBatchData

type CloneTiles = shared.scenegraph.CloneTiles
val CloneTiles: shared.scenegraph.CloneTiles.type = shared.scenegraph.CloneTiles

type CloneTileData = shared.scenegraph.CloneTileData
val CloneTileData: shared.scenegraph.CloneTileData.type = shared.scenegraph.CloneTileData

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import indigo.platform.renderer.shared.TextureLookupResult
import indigo.platform.renderer.shared.WebGLHelper
import indigo.shared.datatypes.RGBA
import indigo.shared.datatypes.mutable.CheapMatrix4
import indigo.shared.display.DisplayCloneBatch
import indigo.shared.display.DisplayCloneInstances
import indigo.shared.display.DisplayCloneRawInstances
import indigo.shared.display.DisplayCloneRawTiles
import indigo.shared.display.DisplayCloneTiles
import indigo.shared.display.DisplayEntity
import indigo.shared.display.DisplayGroup
Expand Down Expand Up @@ -115,16 +117,20 @@ class LayerRenderer(
textureSizeAtlasSizeData((i * 4) + 3) = d.atlasHeight
}

inline private def updateCloneData(
inline private def updateCloneInstanceData(
i: Int,
clone: CloneBatchData
x: Float,
y: Float,
scaleX: Float,
scaleY: Float,
rotation: Float
): Unit = {
translateScaleData((i * 4) + 0) = clone.x.toFloat
translateScaleData((i * 4) + 1) = clone.y.toFloat
translateScaleData((i * 4) + 2) = clone.scaleX.toFloat
translateScaleData((i * 4) + 3) = clone.scaleY.toFloat
translateScaleData((i * 4) + 0) = x
translateScaleData((i * 4) + 1) = y
translateScaleData((i * 4) + 2) = scaleX
translateScaleData((i * 4) + 3) = scaleY

rotationData(i) = clone.rotation.toFloat
rotationData(i) = rotation
}

inline private def updateCloneTileData(
Expand Down Expand Up @@ -541,7 +547,7 @@ class LayerRenderer(
batchCount = batchCount + 1
i += 1

case c: DisplayCloneBatch =>
case c: DisplayCloneInstances =>
drawBuffer(batchCount)

var cloneBlankExists = false
Expand Down Expand Up @@ -582,6 +588,47 @@ class LayerRenderer(

i += 1

case c: DisplayCloneRawInstances =>
drawBuffer(batchCount)

var cloneBlankExists = false
var refreshCloneUBO = false

if c.id.toString != currentCloneId.toString then
cloneBlankDisplayObjects.get(c.id.toString) match
case None => ()
case Some(d) =>
currentCloneId = c.id
currentCloneRef = d
cloneBlankExists = true
refreshCloneUBO = true
else cloneBlankExists = true

if cloneBlankExists then
doContextChange(
currentCloneRef,
atlasName,
currentShader,
currentShaderHash,
customShaders,
baseTransform,
1
)

if refreshCloneUBO || currentShader != currentCloneRef.shaderId then uploadRefUBO(currentCloneRef)

val numberProcessed: Int =
processCloneRawBatch(c)

drawCloneBuffer(numberProcessed)

batchCount = 0
atlasName = currentCloneRef.atlasName
currentShader = currentCloneRef.shaderId
currentShaderHash = currentCloneRef.shaderUniformData.map(_.uniformHash)

i += 1

case c: DisplayCloneTiles =>
drawBuffer(batchCount)

Expand Down Expand Up @@ -623,6 +670,47 @@ class LayerRenderer(

i += 1

case c: DisplayCloneRawTiles =>
drawBuffer(batchCount)

var cloneBlankExists = false
var refreshCloneUBO = false

if c.id.toString != currentCloneId.toString then
cloneBlankDisplayObjects.get(c.id.toString) match
case None => ()
case Some(d) =>
currentCloneId = c.id
currentCloneRef = d
cloneBlankExists = true
refreshCloneUBO = true
else cloneBlankExists = true

if cloneBlankExists then
doContextChange(
currentCloneRef,
atlasName,
currentShader,
currentShaderHash,
customShaders,
baseTransform,
2
)

if refreshCloneUBO || currentShader != currentCloneRef.shaderId then uploadRefUBO(currentCloneRef)

val numberProcessed: Int =
processCloneRawTiles(c, currentCloneRef)

drawCloneTileBuffer(numberProcessed)

batchCount = 0
atlasName = currentCloneRef.atlasName
currentShader = currentCloneRef.shaderId
currentShaderHash = currentCloneRef.shaderUniformData.map(_.uniformHash)

i += 1

case c: DisplayMutants =>
drawBuffer(batchCount)

Expand Down Expand Up @@ -753,14 +841,42 @@ class LayerRenderer(
currentRefUBOHash = code

@SuppressWarnings(Array("scalafix:DisableSyntax.var", "scalafix:DisableSyntax.while"))
private def processCloneBatch(c: DisplayCloneBatch): Int = {
val count: Int = c.cloneData.length
private def processCloneBatch(c: DisplayCloneInstances): Int = {
val count: Int = c.data.length
var i: Int = 0

while (i < count) {
val d = c.data(i)
updateCloneInstanceData(
i,
d.x.toFloat,
d.y.toFloat,
d.scaleX.toFloat,
d.scaleY.toFloat,
d.rotation.toFloat
)

i += 1
}

count
}

@SuppressWarnings(Array("scalafix:DisableSyntax.var", "scalafix:DisableSyntax.while"))
private def processCloneRawBatch(c: DisplayCloneRawInstances): Int = {
val count: Int = c.count
var i: Int = 0

while (i < count) {
updateCloneData(
val offset = i * 5
val d = c.data
updateCloneInstanceData(
i,
c.cloneData(i)
d(offset),
d(offset + 1),
d(offset + 2),
d(offset + 3),
d(offset + 4)
)

i += 1
Expand All @@ -774,7 +890,7 @@ class LayerRenderer(
c: DisplayCloneTiles,
refDisplayObject: DisplayObject
): Int = {
val count: Int = c.cloneData.length
val count: Int = c.data.length
var i: Int = 0

val atlasWidth = refDisplayObject.atlasWidth
Expand All @@ -789,7 +905,7 @@ class LayerRenderer(
val c3Y = refDisplayObject.channelOffset3Y - refDisplayObject.channelOffset0Y

while (i < count) {
val clone = c.cloneData(i)
val clone = c.data(i)
val cropWidth = clone.cropWidth
val cropHeight = clone.cropHeight
val frameScaleX = cropWidth / atlasWidth
Expand Down Expand Up @@ -824,23 +940,81 @@ class LayerRenderer(
count
}

@SuppressWarnings(Array("scalafix:DisableSyntax.var", "scalafix:DisableSyntax.while"))
private def processCloneRawTiles(
c: DisplayCloneRawTiles,
refDisplayObject: DisplayObject
): Int = {
val count: Int = c.count
var i: Int = 0

val atlasWidth = refDisplayObject.atlasWidth
val atlasHeight = refDisplayObject.atlasHeight
val textureX = refDisplayObject.textureX
val textureY = refDisplayObject.textureY
val c1X = refDisplayObject.channelOffset1X - refDisplayObject.channelOffset0X
val c1Y = refDisplayObject.channelOffset1Y - refDisplayObject.channelOffset0Y
val c2X = refDisplayObject.channelOffset2X - refDisplayObject.channelOffset0X
val c2Y = refDisplayObject.channelOffset2Y - refDisplayObject.channelOffset0Y
val c3X = refDisplayObject.channelOffset3X - refDisplayObject.channelOffset0X
val c3Y = refDisplayObject.channelOffset3Y - refDisplayObject.channelOffset0Y

while (i < count) {
val offset = i * 9
val d = c.data
val cropX = d(offset + 5)
val cropY = d(offset + 6)
val cropWidth = d(offset + 7)
val cropHeight = d(offset + 8)
val frameScaleX = cropWidth / atlasWidth
val frameScaleY = cropHeight / atlasHeight
val channelOffset0X = frameScaleX * ((cropX + textureX) / cropWidth)
val channelOffset0Y = frameScaleY * ((cropY + textureY) / cropHeight)

updateCloneTileData(
i = i,
x = d(offset),
y = d(offset + 1),
rotation = d(offset + 2),
scaleX = d(offset + 3),
scaleY = d(offset + 4),
width = cropWidth.toFloat,
height = cropHeight.toFloat,
frameScaleX = frameScaleX,
frameScaleY = frameScaleY,
channelOffset0X = channelOffset0X,
channelOffset0Y = channelOffset0Y,
channelOffset1X = channelOffset0X + c1X,
channelOffset1Y = channelOffset0Y + c1Y,
channelOffset2X = channelOffset0X + c2X,
channelOffset2Y = channelOffset0Y + c2Y,
channelOffset3X = channelOffset0X + c3X,
channelOffset3Y = channelOffset0Y + c3Y
)

i += 1
}

count
}

@SuppressWarnings(Array("scalafix:DisableSyntax.var", "scalafix:DisableSyntax.while"))
private def processMutants(
c: DisplayMutants,
refDisplayObject: DisplayObject,
activeShader: WebGLProgram
): Unit =
if (c.cloneData.length > 0) {
if (c.data.length > 0) {
updateData(refDisplayObject, 0)
prepareCloneProgramBuffer()

val count: Int = c.cloneData.length
val count: Int = c.data.length
var i: Int = 0
var currentUniformHash: js.Array[String] = new js.Array()
val blockIndexLookup: js.Dictionary[Double] = js.Dictionary()

while (i < count) {
val shaderUniformData = c.cloneData(i)
val shaderUniformData = c.data(i)

// UBO data
if shaderUniformData.nonEmpty then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import indigo.shared.datatypes.Size
import indigo.shared.datatypes.TextAlignment
import indigo.shared.datatypes.Vector3
import indigo.shared.datatypes.mutable.CheapMatrix4
import indigo.shared.scenegraph.CloneBatch
import indigo.shared.scenegraph.CloneTiles
import indigo.shared.scenegraph.Clones
import indigo.shared.scenegraph.EntityNode
import indigo.shared.scenegraph.Graphic
import indigo.shared.scenegraph.Group
Expand Down Expand Up @@ -76,10 +75,7 @@ final class BoundaryLocator(
case g: Group =>
Option(groupBounds(g))

case _: CloneBatch =>
None

case _: CloneTiles =>
case _: Clones =>
None

case _: Mutants =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,40 @@ object DisplayTextLetters:
val empty: DisplayTextLetters =
DisplayTextLetters(scalajs.js.Array())

final case class DisplayCloneBatch(
final case class DisplayCloneInstances(
id: CloneId,
z: Double,
cloneData: scalajs.js.Array[CloneBatchData]
data: scalajs.js.Array[CloneBatchData]
) extends DisplayEntity
derives CanEqual

final case class DisplayCloneRawInstances(
id: CloneId,
z: Double,
count: Int,
data: scalajs.js.Array[Float]
) extends DisplayEntity
derives CanEqual

final case class DisplayCloneTiles(
id: CloneId,
z: Double,
cloneData: scalajs.js.Array[CloneTileData]
data: scalajs.js.Array[CloneTileData]
) extends DisplayEntity
derives CanEqual

final case class DisplayCloneRawTiles(
id: CloneId,
z: Double,
count: Int,
data: scalajs.js.Array[Float]
) extends DisplayEntity
derives CanEqual

final case class DisplayMutants(
id: CloneId,
z: Double,
cloneData: scalajs.js.Array[scalajs.js.Array[DisplayObjectUniformData]]
data: scalajs.js.Array[scalajs.js.Array[DisplayObjectUniformData]]
) extends DisplayEntity
derives CanEqual

Expand Down
Loading
Loading