Skip to content

Commit

Permalink
Fixed #788: Encode/derive case classes as UniformBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
davesmith00000 committed Nov 17, 2024
1 parent 6b066c3 commit 257523f
Show file tree
Hide file tree
Showing 28 changed files with 311 additions and 240 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import indigo.shared.datatypes.RGB
import indigo.shared.datatypes.RGBA
import indigo.shared.materials.FillType
import indigo.shared.materials.Material
import indigo.shared.materials.ShaderData
import indigo.shared.shader.EntityShader
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderId
import indigo.shared.shader.ShaderPrimitive
import indigo.shared.shader.ShaderPrimitive.rawJSArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import indigo.shared.assets.AssetName
import indigo.shared.collections.Batch
import indigo.shared.datatypes.RGBA
import indigo.shared.materials.BlendMaterial
import indigo.shared.materials.BlendShaderData
import indigo.shared.materials.FillType
import indigo.shared.materials.Material
import indigo.shared.materials.ShaderData
import indigo.shared.scenegraph.Blend
import indigo.shared.scenegraph.Blending
import indigo.shared.shader.BlendShader
import indigo.shared.shader.EntityShader
import indigo.shared.shader.Shader
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderId
import indigo.shared.shader.ShaderPrimitive.float
import indigo.shared.shader.UltravioletShader
Expand Down Expand Up @@ -104,8 +103,8 @@ object RefractionEntity:
RefractionEntity(diffuse, FillType.Normal)

final case class RefractionBlend(multiplier: Double) extends BlendMaterial derives CanEqual:
lazy val toShaderData: BlendShaderData =
BlendShaderData(
lazy val toShaderData: ShaderData =
ShaderData(
Refraction.blendShader.id,
Batch(
UniformBlock(
Expand Down
4 changes: 2 additions & 2 deletions indigo/indigo/src/main/scala/indigo/IndigoShader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,5 @@ object SceneBlendShader:

val material: BlendMaterial =
new BlendMaterial:
def toShaderData: BlendShaderData =
BlendShaderData(shader.id)
def toShaderData: ShaderData =
ShaderData(shader.id)
106 changes: 15 additions & 91 deletions indigo/indigo/src/main/scala/indigo/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ object syntax:
extension (l: Long) def millis: Millis = Millis(l)

extension (s: String)
def animationKey: AnimationKey = AnimationKey(s)
def assetName: AssetName = AssetName(s)
def assetPath: AssetPath = AssetPath(s)
def assetTag: AssetTag = AssetTag(s)
def cloneId: CloneId = CloneId(s)
def cycleLabel: CycleLabel = CycleLabel(s)
def fontKey: FontKey = FontKey(s)
def fontFamily: FontFamily = FontFamily(s)
def bindingKey: BindingKey = BindingKey(s)
def scene: scenes.SceneName = scenes.SceneName(s)
def shaderId: ShaderId = ShaderId(s)
def uniform: Uniform = Uniform(s)
def uniformBlockName: UniformBlockName = UniformBlockName(s)
def animationKey: AnimationKey = AnimationKey(s)
def assetName: AssetName = AssetName(s)
def assetPath: AssetPath = AssetPath(s)
def assetTag: AssetTag = AssetTag(s)
def cloneId: CloneId = CloneId(s)
def cycleLabel: CycleLabel = CycleLabel(s)
def fontKey: FontKey = FontKey(s)
def fontFamily: FontFamily = FontFamily(s)
def bindingKey: BindingKey = BindingKey(s)
def scene: scenes.SceneName = scenes.SceneName(s)
def shaderId: ShaderId = ShaderId(s)

extension (t: (Double, Double)) def vector2: Vector2 = Vector2(t._1, t._2)

Expand Down Expand Up @@ -121,44 +119,6 @@ object syntax:
export SignalFunction.multiply
end animations

// Shaders
object shaders:

extension (c: RGBA) def asVec4: vec4 = vec4.fromRGBA(c)
extension (c: RGB)
def asVec4: vec4 = vec4.fromRGB(c)
def asVec3: vec3 = vec3.fromRGB(c)
extension (p: Point) def asVec2: vec2 = vec2.fromPoint(p)
extension (s: Size) def asVec2: vec2 = vec2.fromSize(s)
extension (v: Vector2) def asVec2: vec2 = vec2.fromVector2(v)
extension (v: Vector3) def asVec3: vec3 = vec3.fromVector3(v)
extension (v: Vector4) def asVec4: vec4 = vec4.fromVector4(v)
extension (r: Rectangle) def asVec4: vec4 = vec4.fromRectangle(r)
extension (m: Matrix4) def asMat4: mat4 = mat4.fromMatrix4(m)
extension (d: Depth) def asFloat: float = float.fromDepth(d)
extension (m: Millis) def asFloat: float = float.fromMillis(m)
extension (r: Radians) def asFloat: float = float.fromRadians(r)
extension (s: Seconds)
@targetName("ext_Seconds_asFloat")
def asFloat: float = float.fromSeconds(s)
extension (d: Double)
@targetName("ext_Double_asFloat")
def asFloat: float = float(d)
extension (i: Int)
@targetName("ext_Int_asFloat")
def asFloat: float = float(i)
extension (l: Long)
@targetName("ext_Long_asFloat")
def asFloat: float = float(l)
extension (a: Array[Float])
def asMat4: mat4 = mat4(a)
def asRawArray: rawArray = rawArray(a)
extension (a: scalajs.js.Array[Float])
def asMat4: mat4 = mat4(a.toArray)
def asRawArray: rawJSArray = rawJSArray(a)

end shaders

end syntax

object mutable:
Expand Down Expand Up @@ -220,11 +180,8 @@ val Texture: shared.materials.Texture.type = shared.materials.Texture
type BlendMaterial = shared.materials.BlendMaterial
val BlendMaterial: shared.materials.BlendMaterial.type = shared.materials.BlendMaterial

type ShaderData = shared.materials.ShaderData
val ShaderData: shared.materials.ShaderData.type = shared.materials.ShaderData

type BlendShaderData = shared.materials.BlendShaderData
val BlendShaderData: shared.materials.BlendShaderData.type = shared.materials.BlendShaderData
type ShaderData = shared.shader.ShaderData
val ShaderData: shared.shader.ShaderData.type = shared.shader.ShaderData

type Shader = shared.shader.Shader

Expand Down Expand Up @@ -258,41 +215,8 @@ type BlendFragmentEnvReference = shared.shader.library.IndigoUV.BlendFragmentEnv
type ShaderId = shared.shader.ShaderId
val ShaderId: shared.shader.ShaderId.type = shared.shader.ShaderId

type Uniform = shared.shader.Uniform
val Uniform: shared.shader.Uniform.type = shared.shader.Uniform

type UniformBlockName = shared.shader.UniformBlockName
val UniformBlockName: shared.shader.UniformBlockName.type = shared.shader.UniformBlockName

type UniformBlock = shared.shader.UniformBlock
val UniformBlock: shared.shader.UniformBlock.type = shared.shader.UniformBlock

type ShaderPrimitive = shared.shader.ShaderPrimitive
val ShaderPrimitive: shared.shader.ShaderPrimitive.type = shared.shader.ShaderPrimitive

type float = shared.shader.ShaderPrimitive.float
val float: shared.shader.ShaderPrimitive.float.type = shared.shader.ShaderPrimitive.float

type vec2 = shared.shader.ShaderPrimitive.vec2
val vec2: shared.shader.ShaderPrimitive.vec2.type = shared.shader.ShaderPrimitive.vec2

type vec3 = shared.shader.ShaderPrimitive.vec3
val vec3: shared.shader.ShaderPrimitive.vec3.type = shared.shader.ShaderPrimitive.vec3

type vec4 = shared.shader.ShaderPrimitive.vec4
val vec4: shared.shader.ShaderPrimitive.vec4.type = shared.shader.ShaderPrimitive.vec4

type mat4 = shared.shader.ShaderPrimitive.mat4
val mat4: shared.shader.ShaderPrimitive.mat4.type = shared.shader.ShaderPrimitive.mat4

type array[T] = shared.shader.ShaderPrimitive.array[T]
val array: shared.shader.ShaderPrimitive.array.type = shared.shader.ShaderPrimitive.array

type rawArray = shared.shader.ShaderPrimitive.rawArray
val rawArray: shared.shader.ShaderPrimitive.rawArray.type = shared.shader.ShaderPrimitive.rawArray

type rawJSArray = shared.shader.ShaderPrimitive.rawJSArray
val rawJSArray: shared.shader.ShaderPrimitive.rawJSArray.type = shared.shader.ShaderPrimitive.rawJSArray
type ToUniformBlock[A] = shared.shader.ToUniformBlock[A]
val ToUniformBlock: shared.shader.ToUniformBlock.type = shared.shader.ToUniformBlock

val StandardShaders: shared.shader.StandardShaders.type = shared.shader.StandardShaders

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import indigo.shared.collections.Batch
import indigo.shared.datatypes.Fill
import indigo.shared.datatypes.RGB
import indigo.shared.datatypes.RGBA
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderPrimitive
import indigo.shared.shader.ShaderPrimitive.rawJSArray
import indigo.shared.shader.StandardShaders
Expand All @@ -12,21 +13,18 @@ import indigo.shared.shader.UniformBlock
import indigo.shared.shader.UniformBlockName

trait BlendMaterial:
def toShaderData: BlendShaderData
def toShaderData: ShaderData

object BlendMaterial {

case object Normal extends BlendMaterial derives CanEqual {
lazy val toShaderData: BlendShaderData =
BlendShaderData(
StandardShaders.NormalBlend.id,
Batch.empty
)
lazy val toShaderData: ShaderData =
ShaderData(StandardShaders.NormalBlend.id)
}

final case class Lighting(ambient: RGBA) extends BlendMaterial derives CanEqual {
lazy val toShaderData: BlendShaderData =
BlendShaderData(
lazy val toShaderData: ShaderData =
ShaderData(
StandardShaders.LightingBlend.id,
Batch(
UniformBlock(
Expand Down Expand Up @@ -74,15 +72,15 @@ object BlendMaterial {
def ignoreBackground: BlendMaterial =
this.copy(affectsBackground = false)

lazy val toShaderData: BlendShaderData = {
lazy val toShaderData: ShaderData = {
val overlayType: Float =
overlay match {
case _: Fill.Color => 0.0
case _: Fill.LinearGradient => 1.0
case _: Fill.RadialGradient => 2.0
}

BlendShaderData(
ShaderData(
StandardShaders.BlendEffects.id,
Batch(
UniformBlock(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package indigo.shared.materials

import indigo.shared.assets.AssetName
import indigo.shared.collections.Batch
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderId
import indigo.shared.shader.ShaderPrimitive.vec2
import indigo.shared.shader.Uniform
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import indigo.shared.datatypes.RGB
import indigo.shared.datatypes.RGBA
import indigo.shared.materials.LightingModel.Lit
import indigo.shared.materials.LightingModel.Unlit
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderId
import indigo.shared.shader.ShaderPrimitive
import indigo.shared.shader.ShaderPrimitive.rawJSArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import indigo.shared.display.DisplayTextLetters
import indigo.shared.display.SpriteSheetFrame
import indigo.shared.display.SpriteSheetFrame.SpriteSheetFrameCoordinateOffsets
import indigo.shared.events.GlobalEvent
import indigo.shared.materials.ShaderData
import indigo.shared.platform.AssetMapping
import indigo.shared.scenegraph.CloneBatch
import indigo.shared.scenegraph.CloneId
Expand All @@ -49,6 +48,7 @@ import indigo.shared.scenegraph.Sprite
import indigo.shared.scenegraph.Text
import indigo.shared.scenegraph.TextBox
import indigo.shared.scenegraph.TextLine
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderPrimitive
import indigo.shared.shader.Uniform
import indigo.shared.shader.UniformBlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import indigo.shared.display.DisplayObject
import indigo.shared.display.DisplayObjectUniformData
import indigo.shared.events.GlobalEvent
import indigo.shared.materials.BlendMaterial
import indigo.shared.materials.BlendShaderData
import indigo.shared.platform.AssetMapping
import indigo.shared.platform.ProcessedSceneData
import indigo.shared.scenegraph.AmbientLight
Expand All @@ -31,6 +30,7 @@ import indigo.shared.scenegraph.SceneUpdateFragment
import indigo.shared.scenegraph.Shape
import indigo.shared.scenegraph.SpotLight
import indigo.shared.scenegraph.Sprite
import indigo.shared.shader.ShaderData
import indigo.shared.time.GameTime

import scala.scalajs.js.JSConverters._
Expand Down Expand Up @@ -326,7 +326,7 @@ object SceneProcessor {
}

def mergeShaderToUniformData(
shaderData: BlendShaderData
shaderData: ShaderData
)(using QuickCache[scalajs.js.Array[Float]]): scalajs.js.Array[DisplayObjectUniformData] =
shaderData.uniformBlocks.toJSArray.map { ub =>
DisplayObjectUniformData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import indigo.shared.datatypes.Rectangle
import indigo.shared.datatypes.Size
import indigo.shared.datatypes.Vector2
import indigo.shared.events.GlobalEvent
import indigo.shared.materials.ShaderData
import indigo.shared.shader.ShaderData

final case class BlankEntity(
size: Size,
Expand Down
37 changes: 23 additions & 14 deletions indigo/indigo/src/main/scala/indigo/shared/scenegraph/Clip.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import indigo.shared.datatypes.Size
import indigo.shared.datatypes.Vector2
import indigo.shared.events.GlobalEvent
import indigo.shared.materials.Material
import indigo.shared.materials.ShaderData
import indigo.shared.shader.ShaderData
import indigo.shared.shader.ShaderPrimitive.float
import indigo.shared.shader.StandardShaders
import indigo.shared.shader.ToUniformBlock
import indigo.shared.shader.Uniform
import indigo.shared.shader.UniformBlock
import indigo.shared.shader.UniformBlockName
Expand Down Expand Up @@ -157,19 +158,16 @@ final case class Clip[M <: Material](
val data = material.toShaderData
data
.withShaderId(StandardShaders.shaderIdToClipShaderId(data.shaderId))
.addUniformBlock(
UniformBlock(
UniformBlockName("IndigoClipData"),
Batch(
Uniform("CLIP_SHEET_FRAME_COUNT") -> float(sheet.frameCount),
Uniform("CLIP_SHEET_FRAME_DURATION") -> float.fromSeconds(sheet.frameDuration),
Uniform("CLIP_SHEET_WRAP_AT") -> float(sheet.wrapAt),
Uniform("CLIP_SHEET_ARRANGEMENT") -> float(sheet.arrangement.toInt),
Uniform("CLIP_SHEET_START_OFFSET") -> float(sheet.startOffset),
Uniform("CLIP_PLAY_DIRECTION") -> float(playMode.direction.toInt),
Uniform("CLIP_PLAYMODE_START_TIME") -> float.fromSeconds(playMode.giveStartTime),
Uniform("CLIP_PLAYMODE_TIMES") -> float(playMode.giveTimes)
)
.addUniformData(
Clip.IndigoClipData(
sheet.frameCount,
sheet.frameDuration,
sheet.wrapAt,
sheet.arrangement.toInt,
sheet.startOffset,
playMode.direction.toInt,
playMode.giveStartTime,
playMode.giveTimes
)
)

Expand Down Expand Up @@ -228,6 +226,17 @@ final case class Clip[M <: Material](

object Clip:

final case class IndigoClipData(
CLIP_SHEET_FRAME_COUNT: Int,
CLIP_SHEET_FRAME_DURATION: Seconds,
CLIP_SHEET_WRAP_AT: Int,
CLIP_SHEET_ARRANGEMENT: Int,
CLIP_SHEET_START_OFFSET: Int,
CLIP_PLAY_DIRECTION: Int,
CLIP_PLAYMODE_START_TIME: Seconds,
CLIP_PLAYMODE_TIMES: Int
) derives ToUniformBlock

def apply[M <: Material](
width: Int,
height: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import indigo.shared.datatypes.Size
import indigo.shared.datatypes.Vector2
import indigo.shared.events.GlobalEvent
import indigo.shared.materials.Material
import indigo.shared.materials.ShaderData
import indigo.shared.shader.ShaderData

/** Graphics are used to draw images on the screen, in a cheap efficient but expressive way. Graphic's party trick is
* it's ability to crop images.
Expand Down
Loading

0 comments on commit 257523f

Please sign in to comment.