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

Fixed #788: Encode/derive case classes as UniformBlocks #789

Merged
merged 2 commits into from
Nov 17, 2024
Merged
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
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,17 +4,16 @@ 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.ShaderProgram
import indigo.shared.shader.UltravioletShader
import indigo.shared.shader.Uniform
import indigo.shared.shader.UniformBlock
Expand Down Expand Up @@ -44,7 +43,7 @@ object Refraction:
)
)

val shaders: Set[Shader] =
val shaders: Set[ShaderProgram] =
Set(entityShader, blendShader)

/** Replicates Indigo's original refraction/distortion layer behaviour
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
12 changes: 6 additions & 6 deletions indigo/indigo/src/main/scala/indigo/BootResult.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package indigo

import indigo.shared.shader.Shader
import indigo.shared.shader.ShaderProgram
import indigo.shared.subsystems.SubSystem

/** The game bootstrapping process results in a `BootResult`, which only occurs once on initial game load. The boot
Expand All @@ -16,7 +16,7 @@ final case class BootResult[A, Model](
assets: Set[AssetType],
fonts: Set[FontInfo],
subSystems: Set[SubSystem[Model]],
shaders: Set[Shader]
shaders: Set[ShaderProgram]
) derives CanEqual {

def addAnimations(newAnimations: Set[Animation]): BootResult[A, Model] =
Expand Down Expand Up @@ -55,13 +55,13 @@ final case class BootResult[A, Model](
def withSubSystems(newSubSystems: SubSystem[Model]*): BootResult[A, Model] =
withSubSystems(newSubSystems.toSet)

def addShaders(newShaders: Set[Shader]): BootResult[A, Model] =
def addShaders(newShaders: Set[ShaderProgram]): BootResult[A, Model] =
this.copy(shaders = shaders ++ newShaders)
def addShaders(newShaders: Shader*): BootResult[A, Model] =
def addShaders(newShaders: ShaderProgram*): BootResult[A, Model] =
addShaders(newShaders.toSet)
def withShaders(newShaders: Set[Shader]): BootResult[A, Model] =
def withShaders(newShaders: Set[ShaderProgram]): BootResult[A, Model] =
this.copy(shaders = newShaders)
def withShaders(newShaders: Shader*): BootResult[A, Model] =
def withShaders(newShaders: ShaderProgram*): BootResult[A, Model] =
withShaders(newShaders.toSet)

}
Expand Down
3 changes: 1 addition & 2 deletions indigo/indigo/src/main/scala/indigo/IndigoSandbox.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package indigo

import indigo._
import indigo.entry.StandardFrameProcessor
import indigo.gameengine.GameEngine
import indigo.shared.subsystems.SubSystemsRegister
Expand Down Expand Up @@ -39,7 +38,7 @@ trait IndigoSandbox[StartUpData, Model] extends GameLauncher[StartUpData, Model,

/** A fixed set of custom shaders you will be able to render with
*/
def shaders: Set[Shader]
def shaders: Set[ShaderProgram]

/** The `setup` function is your only opportunity to do an initial work to set up your game. For example, perhaps one
* of your assets was a JSON description of a map or an animation sequence, you could process that now, which is why
Expand Down
7 changes: 3 additions & 4 deletions indigo/indigo/src/main/scala/indigo/IndigoShader.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package indigo

import indigo.*
import indigo.entry.StandardFrameProcessor
import indigo.gameengine.GameEngine
import indigo.shared.shader.library
Expand Down Expand Up @@ -55,7 +54,7 @@ trait IndigoShader extends GameLauncher[IndigoShaderModel, IndigoShaderModel, Un

/** The shader you want to render
*/
def shader: Shader
def shader: ShaderProgram

private def boot(flags: Map[String, String]): Outcome[BootResult[IndigoShaderModel, IndigoShaderModel]] =
val width = flags.get("width").map(_.toInt).getOrElse(config.viewport.width)
Expand Down Expand Up @@ -246,5 +245,5 @@ object SceneBlendShader:

val material: BlendMaterial =
new BlendMaterial:
def toShaderData: BlendShaderData =
BlendShaderData(shader.id)
def toShaderData: ShaderData =
ShaderData(shader.id)
24 changes: 14 additions & 10 deletions indigo/indigo/src/main/scala/indigo/gameengine/GameEngine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import indigo.shared.platform.AssetMapping
import indigo.shared.platform.SceneProcessor
import indigo.shared.shader.BlendShader
import indigo.shared.shader.EntityShader
import indigo.shared.shader.Shader
import indigo.shared.shader.ShaderProgram
import indigo.shared.shader.ShaderRegister
import indigo.shared.shader.StandardShaders
import indigo.shared.shader.UltravioletShader
Expand All @@ -40,7 +40,7 @@ import scala.concurrent.Future
final class GameEngine[StartUpData, GameModel, ViewModel](
fonts: Set[FontInfo],
animations: Set[Animation],
shaders: Set[Shader],
shaders: Set[ShaderProgram],
initialise: AssetCollection => Dice => Outcome[Startup[StartUpData]],
initialModel: StartUpData => Outcome[GameModel],
initialViewModel: StartUpData => GameModel => Outcome[ViewModel],
Expand Down Expand Up @@ -268,7 +268,11 @@ object GameEngine {
def registerFonts(fontRegister: FontRegister, fonts: Set[FontInfo]): Unit =
fonts.foreach(fontRegister.register)

def registerShaders(shaderRegister: ShaderRegister, shaders: Set[Shader], assetCollection: AssetCollection): Unit =
def registerShaders(
shaderRegister: ShaderRegister,
shaders: Set[ShaderProgram],
assetCollection: AssetCollection
): Unit =
shaders.foreach {
case s: EntityShader.Source =>
shaderRegister.remove(s.id)
Expand Down Expand Up @@ -299,19 +303,19 @@ object GameEngine {
id = external.id,
vertex = external.vertex
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-vertex", a))
.getOrElse(Shader.defaultVertexProgram),
.getOrElse(ShaderProgram.defaultVertexProgram),
fragment = external.fragment
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-fragment", a))
.getOrElse(Shader.defaultFragmentProgram),
.getOrElse(ShaderProgram.defaultFragmentProgram),
prepare = external.prepare
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-prepare", a))
.getOrElse(Shader.defaultPrepareProgram),
.getOrElse(ShaderProgram.defaultPrepareProgram),
light = external.light
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-light", a))
.getOrElse(Shader.defaultLightProgram),
.getOrElse(ShaderProgram.defaultLightProgram),
composite = external.composite
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-composite", a))
.getOrElse(Shader.defaultCompositeProgram)
.getOrElse(ShaderProgram.defaultCompositeProgram)
)

def externalBlendShaderToSource(
Expand All @@ -322,10 +326,10 @@ object GameEngine {
id = external.id,
vertex = external.vertex
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-vertex", a))
.getOrElse(Shader.defaultVertexProgram),
.getOrElse(ShaderProgram.defaultVertexProgram),
fragment = external.fragment
.map(a => extractShaderCode(assetCollection.findTextDataByName(a), "indigo-fragment", a))
.getOrElse(Shader.defaultFragmentProgram)
.getOrElse(ShaderProgram.defaultFragmentProgram)
)

private given CanEqual[Option[String], Option[String]] = CanEqual.derived
Expand Down
108 changes: 16 additions & 92 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,13 +180,10 @@ 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
type ShaderProgram = shared.shader.ShaderProgram

type BlendShader = shared.shader.BlendShader
val BlendShader: shared.shader.BlendShader.type = shared.shader.BlendShader
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
12 changes: 6 additions & 6 deletions indigo/indigo/src/main/scala/indigo/shared/Startup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package indigo.shared
import indigo.Batch
import indigo.shared.animation.Animation
import indigo.shared.datatypes.FontInfo
import indigo.shared.shader.Shader
import indigo.shared.shader.ShaderProgram

/** The Startup data type describes either a successful or failed start up sequence. It can hold a value, as well as new
* shaders, animations and fonts to be added to Indigo's registers. A new Startup instance is created each time the
Expand All @@ -28,7 +28,7 @@ sealed trait Startup[+SuccessType] extends Product with Serializable derives Can
f
}

def additionalShaders: Set[Shader] =
def additionalShaders: Set[ShaderProgram] =
this match
case Startup.Failure(_) =>
Set()
Expand All @@ -52,7 +52,7 @@ object Startup:
success: SuccessType,
animations: Set[Animation],
fonts: Set[FontInfo],
shaders: Set[Shader]
shaders: Set[ShaderProgram]
) extends Startup[SuccessType]
derives CanEqual:
def addAnimations(value: Animation*): Success[SuccessType] =
Expand All @@ -69,11 +69,11 @@ object Startup:
def addFonts(value: Batch[FontInfo]): Success[SuccessType] =
Success(success, animations, fonts ++ value.toSet, shaders)

def addShaders(value: Shader*): Success[SuccessType] =
def addShaders(value: ShaderProgram*): Success[SuccessType] =
addShaders(value.toList)
def addShaders(value: List[Shader]): Success[SuccessType] =
def addShaders(value: List[ShaderProgram]): Success[SuccessType] =
Success(success, animations, fonts, shaders ++ value)
def addShaders(value: Batch[Shader]): Success[SuccessType] =
def addShaders(value: Batch[ShaderProgram]): Success[SuccessType] =
Success(success, animations, fonts, shaders ++ value.toSet)

object Success:
Expand Down
Loading
Loading