From 72f453ca77b8c12a7de62af288956f7b9763d3e5 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 22 Jun 2025 01:51:52 +0200 Subject: [PATCH 01/16] Init work on new cyfra --- build.sbt | 8 +- .../computenode/cyfra/core/Allocation.scala | 14 ++++ .../cyfra/core/GBufferRegion.scala | 37 ++++++++++ .../computenode/cyfra/core/GExecution.scala | 42 +++++++++++ .../io/computenode/cyfra/core/GProgram.scala | 28 +++++++ .../cyfra/core/aalegacy}/Executable.scala | 4 +- .../cyfra/core/aalegacy}/GContext.scala | 7 +- .../cyfra/core/aalegacy}/GFunction.scala | 7 +- .../cyfra/core/aalegacy}/UniformContext.scala | 3 +- .../cyfra/core/aalegacy}/mem/FloatMem.scala | 2 +- .../cyfra/core/aalegacy}/mem/GMem.scala | 4 +- .../cyfra/core/aalegacy}/mem/IntMem.scala | 2 +- .../cyfra/core/aalegacy}/mem/RamGMem.scala | 2 +- .../core/aalegacy}/mem/Vec4FloatMem.scala | 4 +- .../cyfra/core/buffer/BufferRef.scala | 8 ++ .../cyfra/core/buffer/SizedBuffer.scala | 6 ++ .../cyfra/core/layout/Layout.scala | 7 ++ .../cyfra/core/layout/LayoutStruct.scala | 73 +++++++++++++++++++ .../io/computenode/cyfra/dsl/Value.scala | 4 + .../cyfra/dsl/buffer/GBuffer.scala | 7 ++ .../computenode/cyfra/dsl/buffer/Read.scala | 5 ++ .../computenode/cyfra/dsl/buffer/Write.scala | 7 ++ .../io/computenode/cyfra/dsl/gio/GIO.scala | 35 +++++++++ .../cyfra/e2e/ArithmeticsE2eTest.scala | 2 +- .../cyfra/e2e/FunctionsE2eTest.scala | 2 +- .../cyfra/e2e/GStructE2eTest.scala | 2 +- .../computenode/cyfra/e2e/GseqE2eTest.scala | 2 +- .../computenode/cyfra/e2e/WhenE2eTest.scala | 2 +- .../cyfra/e2e/juliaset/JuliaSet.scala | 4 +- .../cyfra/samples/TestingStuff.scala | 35 +++++++++ .../samples}/foton/AnimatedJulia.scala | 2 +- .../samples}/foton/AnimatedRaytrace.scala | 2 +- .../samples}/oldsamples/Raytracing.scala | 6 +- .../samples}/slides/1sample.scala | 6 +- .../samples}/slides/2simpleray.scala | 6 +- .../samples}/slides/3rays.scala | 6 +- .../samples}/slides/4random.scala | 6 +- .../animation/AnimatedFunctionRenderer.scala | 6 +- .../foton/animation/AnimationRenderer.scala | 4 +- .../cyfra/foton/rt/ImageRtRenderer.scala | 6 +- .../cyfra/foton/rt/RtRenderer.scala | 2 +- .../rt/animation/AnimationRtRenderer.scala | 6 +- 42 files changed, 369 insertions(+), 54 deletions(-) create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/Executable.scala (63%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/GContext.scala (93%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/GFunction.scala (93%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/UniformContext.scala (81%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/mem/FloatMem.scala (93%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/mem/GMem.scala (93%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/mem/IntMem.scala (93%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/mem/RamGMem.scala (81%) rename {cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime => cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy}/mem/Vec4FloatMem.scala (90%) create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala create mode 100644 cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/foton/AnimatedJulia.scala (97%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/foton/AnimatedRaytrace.scala (98%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/oldsamples/Raytracing.scala (99%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/slides/1sample.scala (69%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/slides/2simpleray.scala (91%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/slides/3rays.scala (97%) rename cyfra-examples/src/main/scala/io/computenode/{samples/cyfra => cyfra/samples}/slides/4random.scala (98%) diff --git a/build.sbt b/build.sbt index 8005cccd..a36c04dc 100644 --- a/build.sbt +++ b/build.sbt @@ -76,10 +76,14 @@ lazy val compiler = (project in file("cyfra-compiler")) .settings(commonSettings) .dependsOn(dsl, utility) -lazy val runtime = (project in file("cyfra-runtime")) +lazy val core = (project in file("cyfra-core")) .settings(commonSettings) .dependsOn(compiler, dsl, vulkan, utility, spirvTools) +lazy val runtime = (project in file("cyfra-runtime")) + .settings(commonSettings) + .dependsOn(core) + lazy val foton = (project in file("cyfra-foton")) .settings(commonSettings) .dependsOn(compiler, dsl, runtime, utility) @@ -98,7 +102,7 @@ lazy val e2eTest = (project in file("cyfra-e2e-test")) lazy val root = (project in file(".")) .settings(name := "Cyfra") - .aggregate(compiler, dsl, foton, runtime, vulkan, examples) + .aggregate(compiler, dsl, foton, core, runtime, vulkan, examples) e2eTest / Test / javaOptions ++= Seq("-Dorg.lwjgl.system.stackSize=1024", "-DuniqueLibraryNames=true") diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala new file mode 100644 index 00000000..504225d1 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -0,0 +1,14 @@ +package io.computenode.cyfra.core + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer + +import java.nio.ByteBuffer + +trait Allocation: + extension [R, T <: Value](buffer: GBuffer[T]) + def read(bb: ByteBuffer): Unit = + () + + def write(bb: ByteBuffer): Unit = + () diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala new file mode 100644 index 00000000..8de7f667 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -0,0 +1,37 @@ +package io.computenode.cyfra.core + +import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} + +sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: LayoutStruct]: + val initAlloc: ReqAlloc + +object GBufferRegion: + + def allocate[Alloc <: Layout: LayoutStruct]: GBufferRegion[Alloc, Alloc] = + AllocRegion(summon[LayoutStruct[Alloc]].layoutRef) + + case class AllocRegion[Alloc <: Layout: LayoutStruct](l: Alloc) extends GBufferRegion[Alloc, Alloc]: + val initAlloc: Alloc = l + + case class MapRegion[ReqAlloc <: Layout: LayoutStruct, BodyAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: LayoutStruct]( + reqRegion: GBufferRegion[ReqAlloc, BodyAlloc], + f: Allocation => BodyAlloc => ResAlloc, + ) extends GBufferRegion[ReqAlloc, ResAlloc]: + val initAlloc: ReqAlloc = reqRegion.initAlloc + + extension [ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: LayoutStruct](region: GBufferRegion[ReqAlloc, ResAlloc]) + def map[NewAlloc <: Layout: LayoutStruct](f: Allocation ?=> ResAlloc => NewAlloc): GBufferRegion[ReqAlloc, NewAlloc] = + MapRegion(region, (alloc: Allocation) => (resAlloc: ResAlloc) => f(using alloc)(resAlloc)) + + def runUnsafe(init: Allocation ?=> ReqAlloc, onDone: Allocation ?=> ResAlloc => Unit): Unit = + val alloc = new Allocation {} + init(using alloc) + val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): + case _: AllocRegion[?] => None + case MapRegion(req, f) => + Some((f.asInstanceOf[Allocation => Layout => Layout], req)) + + val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) => + step(alloc)(acc) + + onDone(using alloc)(bodyAlloc.asInstanceOf[ResAlloc]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala new file mode 100644 index 00000000..fd3ea228 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -0,0 +1,42 @@ +package io.computenode.cyfra.core + +import io.computenode.cyfra.core.GExecution.* +import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.layout.* +import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.gio.GIO + +case class GExecution[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[?]]): + def execute(layout: L)(using Allocation): L = + println("Executing GExecution...") + layout // Return the layout after execution + +object GExecution: + + def forLayout[Params, L <: Layout](using layoutStruct: LayoutStruct[L]): GExecutionBuilder[Params, L] = + GExecutionBuilder(layoutStruct, Seq.empty) + + case class GExecutionBuilder[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[?]]): + def addProgram[ProgramLayout <: Layout, ProgramParams, P <: GProgram[ProgramParams, ProgramLayout]]( + program: P, + )(mapLayout: L => ProgramBinding[ProgramLayout], mapParams: Params => ProgramParams): GExecutionBuilder[Params, L] = + val ProgramBinding(mappedLayout, dispatch) = mapLayout(layoutStruct.layoutRef) + val boundProgram = BoundProgram(mappedLayout, dispatch, program.body(mappedLayout)) + GExecutionBuilder(layoutStruct, boundPrograms :+ boundProgram) + + def compile(using GContext): GExecution[Params, L] = + println("Compiling GExecution...") + GExecution(layoutStruct, boundPrograms) + + type WorkDimensions = (Int, Int, Int) + + sealed trait ProgramDispatch + + case class DynamicDispatch[L <: Layout](buffer: GBuffer[?], offset: Int) extends ProgramDispatch + + case class StaticDispatch[L <: Layout](size: WorkDimensions) extends ProgramDispatch + + case class ProgramBinding[L <: Layout](programLayout: L, programDispatch: ProgramDispatch) + + case class BoundProgram[L <: Layout](layout: L, dispatch: ProgramDispatch, body: GIO[?]) + diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala new file mode 100644 index 00000000..ca676d3e --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -0,0 +1,28 @@ +package io.computenode.cyfra.core + +import io.computenode.cyfra.core.layout.LayoutStruct +import io.computenode.cyfra.dsl.gio.GIO +import io.computenode.cyfra.core.layout.Layout + +import java.nio.ByteBuffer +import GProgram.* +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer +import izumi.reflect.Tag + +case class GProgram[Params, L <: Layout: LayoutStruct] private (body: L => GIO[?], layout: BufferOfSize => Params => L, workgroupSize: WorkgroupSize): + private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] + +object GProgram: + type WorkgroupSize = (Int, Int, Int) + + private[cyfra] case class BufferSizeSpec[T <: Value](size: Int) extends GBuffer[T] + + trait BufferOfSize: + extension (buffers: GBuffer.type) + def apply[T <: Value](size: Int): BufferSizeSpec[T] = + BufferSizeSpec[T](size) + + + def apply[Params, L <: Layout : LayoutStruct](layout: BufferOfSize ?=> Params => L, workgroupSize: WorkgroupSize = (128, 1, 1))(body: L => GIO[?]): GProgram[Params, L] = + new GProgram(body, s => layout(using s), workgroupSize) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/Executable.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala similarity index 63% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/Executable.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala index b72be392..0b876c39 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/Executable.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala @@ -1,7 +1,7 @@ -package io.computenode.cyfra.runtime +package io.computenode.cyfra.core.aalegacy +import io.computenode.cyfra.core.aalegacy.mem.{GMem, RamGMem} import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.runtime.mem.{GMem, RamGMem} import scala.concurrent.Future diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala similarity index 93% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GContext.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala index 3ebd43d9..d06382dd 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala @@ -1,11 +1,12 @@ -package io.computenode.cyfra.runtime +package io.computenode.cyfra.core.aalegacy +import io.computenode.cyfra.core.aalegacy.mem.GMem.totalStride +import io.computenode.cyfra.core.aalegacy.mem.{FloatMem, GMem, IntMem, Vec4FloatMem} +import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.{Float32, FromExpr, Int32, Vec4} import io.computenode.cyfra.dsl.collections.GArray import io.computenode.cyfra.dsl.struct.* -import io.computenode.cyfra.runtime.mem.GMem.totalStride -import io.computenode.cyfra.runtime.mem.{FloatMem, GMem, IntMem, Vec4FloatMem} import io.computenode.cyfra.spirv.SpirvTypes.typeStride import io.computenode.cyfra.spirv.compilers.DSLCompiler import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.{UniformStructRef, WorkerIndex} diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GFunction.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala similarity index 93% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GFunction.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala index 1c85b3fd..d7be2ac6 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/GFunction.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala @@ -1,9 +1,10 @@ -package io.computenode.cyfra.runtime +package io.computenode.cyfra.core.aalegacy -import io.computenode.cyfra.dsl.{*, given} +import io.computenode.cyfra.core.aalegacy.GFunction import io.computenode.cyfra.dsl.Value.* -import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.collections.{GArray, GArray2D} +import io.computenode.cyfra.dsl.struct.* +import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.vulkan.compute.ComputePipeline import izumi.reflect.Tag diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/UniformContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala similarity index 81% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/UniformContext.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala index 04df0996..aa326fdc 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/UniformContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala @@ -1,5 +1,6 @@ -package io.computenode.cyfra.runtime +package io.computenode.cyfra.core.aalegacy +import io.computenode.cyfra.core.aalegacy.UniformContext import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/FloatMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala similarity index 93% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/FloatMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala index 4264233d..4d7d3c59 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/FloatMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.runtime.mem +package io.computenode.cyfra.core.aalegacy.mem import io.computenode.cyfra.dsl.Value.Float32 import org.lwjgl.BufferUtils diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/GMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala similarity index 93% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/GMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala index a6efd211..d268e504 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/GMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala @@ -1,9 +1,9 @@ -package io.computenode.cyfra.runtime.mem +package io.computenode.cyfra.core.aalegacy.mem +import io.computenode.cyfra.core.aalegacy.{GContext, GFunction, UniformContext} import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.{*, given} -import io.computenode.cyfra.runtime.{GContext, GFunction, UniformContext} import io.computenode.cyfra.spirv.SpirvTypes.typeStride import izumi.reflect.Tag import org.lwjgl.BufferUtils diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/IntMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala similarity index 93% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/IntMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala index 72d12a82..4143dd2e 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/IntMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.runtime.mem +package io.computenode.cyfra.core.aalegacy.mem import io.computenode.cyfra.dsl.Value.Int32 import org.lwjgl.BufferUtils diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/RamGMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala similarity index 81% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/RamGMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala index 43e45f30..1c10bab8 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/RamGMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.runtime.mem +package io.computenode.cyfra.core.aalegacy.mem import io.computenode.cyfra.dsl.Value diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/Vec4FloatMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala similarity index 90% rename from cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/Vec4FloatMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala index ff48aa6b..553103da 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/mem/Vec4FloatMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala @@ -1,7 +1,7 @@ -package io.computenode.cyfra.runtime.mem +package io.computenode.cyfra.core.aalegacy.mem +import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA import io.computenode.cyfra.dsl.Value.{Float32, Vec4} -import io.computenode.cyfra.runtime.mem.GMem.fRGBA import org.lwjgl.BufferUtils import java.nio.ByteBuffer diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala new file mode 100644 index 00000000..d4564f1d --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala @@ -0,0 +1,8 @@ +package io.computenode.cyfra.core.buffer + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer +import izumi.reflect.Tag +import izumi.reflect.macrortti.LightTypeTag + +case class BufferRef[T <: Value](layoutOffset: Int, valueTag: Tag[T]) extends GBuffer[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala new file mode 100644 index 00000000..5cfc3477 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala @@ -0,0 +1,6 @@ +package io.computenode.cyfra.core.buffer + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer + +case class SizedBuffer[T <: Value](size: Int) extends GBuffer[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala new file mode 100644 index 00000000..505b8a57 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.core.layout + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer + +trait Layout + diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala new file mode 100644 index 00000000..a630ea0c --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -0,0 +1,73 @@ +package io.computenode.cyfra.core.layout + +import io.computenode.cyfra.core.buffer.BufferRef +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer +import izumi.reflect.Tag +import izumi.reflect.macrortti.LightTypeTag + +import scala.compiletime.{error, summonAll} +import scala.deriving.Mirror +import scala.quoted.{Expr, Quotes, Type} + +case class LayoutStruct[T <: Layout: Tag](private[cyfra] val layoutRef: T, private[cyfra] val elementTypes: List[Tag[? <: Value]]) + +object LayoutStruct: + + inline given derived[T <: Layout: Tag]: LayoutStruct[T] = ${derivedImpl} + + def derivedImpl[T <: Layout: Type](using quotes: Quotes): Expr[LayoutStruct[T]] = + import quotes.reflect.* + + val tpe = TypeRepr.of[T] + val sym = tpe.typeSymbol + + if !sym.isClassDef || !sym.flags.is(Flags.Case) then + report.errorAndAbort("LayoutStruct can only be derived for case classes") + + val fieldTypes = sym.caseFields.map(_.tree).map: + case ValDef(_, tpt, _) => tpt.tpe + case _ => report.errorAndAbort("Unexpected field type in case class") + + if !fieldTypes.forall(_ <:< TypeRepr.of[GBuffer[?]]) then + report.errorAndAbort("LayoutStruct can only be derived for case classes with GBuffer elements") + + val valueTypes = fieldTypes.map(_.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type"))) + + // summon izumi tags + val tags = valueTypes.map: tpe => + tpe.asType match + case '[t] => + (tpe.asType, Expr.summon[Tag[t]] match + case Some(tagExpr) => tagExpr + case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}") + ) + + val buffers = tags.zipWithIndex.map: + case ((tpe, tag), i) => + tpe match + case '[type t <: Value; t] => + '{ + BufferRef[t](${ Expr(i) }, ${tag.asExprOf[Tag[t]]}) + } + + val constructor = sym.primaryConstructor + + val layoutInstance = Apply( + Select( + New(TypeIdent(sym)), + constructor, + ), + buffers.map(_.asTerm) + ) + + val layoutRef = layoutInstance.asExprOf[T] + + val soleTags = tags.map(_._2.asExprOf[Tag[? <: Value]]).toList + + '{ + LayoutStruct[T]( + $layoutRef, + ${Expr.ofList(soleTags)} + ) + } \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala index 03754998..764bf2fc 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala @@ -18,6 +18,10 @@ object Value: trait FromExpr[T <: Value]: def fromExpr(expr: E[T])(using name: Source): T + object FromExpr: + def fromExpr[T <: Value](expr: E[T])(using f: FromExpr[T]): T = + f.fromExpr(expr) + sealed trait Scalar extends Value trait FloatType extends Scalar diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala new file mode 100644 index 00000000..83623053 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.dsl.buffer + +import io.computenode.cyfra.dsl.Value + +trait GBuffer[T <: Value] + +object GBuffer \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala new file mode 100644 index 00000000..40203d87 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala @@ -0,0 +1,5 @@ +package io.computenode.cyfra.dsl.buffer + +import io.computenode.cyfra.dsl.{Expression, Value} + +case class Read[T <: Value](buffer: GBuffer[T], index: Int) extends Expression[T] \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala new file mode 100644 index 00000000..d6b2d2de --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.dsl.buffer + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.gio.GIO + +case class Write[T <: Value](buffer: GBuffer[T], index: Int, value: T) extends GIO[Unit]: + override def underlying: Unit = () diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala new file mode 100644 index 00000000..83c82922 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala @@ -0,0 +1,35 @@ +package io.computenode.cyfra.dsl.gio + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr +import io.computenode.cyfra.dsl.Value.FromExpr.fromExpr +import io.computenode.cyfra.dsl.buffer.{GBuffer, Read, Write} +import io.computenode.cyfra.dsl.gio.GIO.* + +trait GIO[T]: + + def flatMap[U](f: T => GIO[U]): GIO[U] = FlatMap(this, f(this.underlying)) + + def map[U](f: T => U): GIO[U] = flatMap(t => GIO.pure(f(t))) + + private[cyfra] def underlying: T + +object GIO: + + case class Pure[T](value: T) extends GIO[T]: + override def underlying: T = value + + case class FlatMap[T, U](gio: GIO[T], next: GIO[U]) extends GIO[U]: + override def underlying: U = next.underlying + + + def pure[T](value: T): GIO[T] = Pure(value) + + def value[T](value: T): GIO[T] = Pure(value) + + def write[T <: Value](buffer: GBuffer[T], index: Int, value: T): GIO[Unit] = + Write(buffer, index, value) + + def read[T <: Value : FromExpr](buffer: GBuffer[T], index: Int): T = + fromExpr(Read(buffer, index)) + diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala index 3ffd6b71..ae9f1cfe 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.e2e -import io.computenode.cyfra.runtime.* +import io.computenode.cyfra.core.aalegacy.* import mem.* import GMem.fRGBA import io.computenode.cyfra.dsl.algebra.VectorAlgebra diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala index 6a332ac8..34bae9f0 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.e2e -import io.computenode.cyfra.runtime.*, mem.* +import io.computenode.cyfra.core.aalegacy.*, mem.* import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import GMem.fRGBA diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala index 61b4db15..0f2ff751 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala @@ -2,7 +2,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.runtime.* +import io.computenode.cyfra.core.aalegacy.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala index 8b70999e..b100e239 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala @@ -2,7 +2,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.runtime.* +import io.computenode.cyfra.core.aalegacy.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala index 3a53669f..0355ac2d 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala @@ -1,7 +1,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.runtime.* +import io.computenode.cyfra.core.aalegacy.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala index 7431960d..ab5c158c 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala @@ -6,8 +6,8 @@ import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.control.Pure.pure import io.computenode.cyfra.dsl.struct.GStruct.Empty import io.computenode.cyfra.e2e.ImageTests -import io.computenode.cyfra.runtime.mem.Vec4FloatMem -import io.computenode.cyfra.runtime.{GContext, GFunction} +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.{GContext, GFunction} import io.computenode.cyfra.spirvtools.* import io.computenode.cyfra.spirvtools.SpirvTool.{Param, ToFile} import io.computenode.cyfra.utility.ImageUtility diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala new file mode 100644 index 00000000..eca2f89d --- /dev/null +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -0,0 +1,35 @@ +package io.computenode.cyfra.samples + +import io.computenode.cyfra.core.GProgram +import io.computenode.cyfra.core.layout.* +import io.computenode.cyfra.dsl.Value.{GBoolean, Int32} +import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.gio.GIO + +object TestingStuff: + @main + def test = + + case class EmitProgramParams( + inSize: Int, + emitN: Int + ) + + case class EmitProgramLayout( + in: GBuffer[Int32], + out: GBuffer[Int32] + ) extends Layout + + val emitProgram = GProgram[EmitProgramParams, EmitProgramLayout]( + params => EmitProgramLayout( + in = GBuffer[Int32](params.inSize), + out = GBuffer[Int32](params.emitN) + )) { layout => + for + _ <- GIO.pure(2) + yield () + } + + + + \ No newline at end of file diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedJulia.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedJulia.scala similarity index 97% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedJulia.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedJulia.scala index 200e16a6..99bd6759 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedJulia.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedJulia.scala @@ -1,4 +1,4 @@ -package io.computenode.samples.cyfra.foton +package io.computenode.cyfra.samples.foton import io.computenode.cyfra import io.computenode.cyfra.* diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedRaytrace.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedRaytrace.scala similarity index 98% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedRaytrace.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedRaytrace.scala index bd2c65de..f478647a 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/foton/AnimatedRaytrace.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/foton/AnimatedRaytrace.scala @@ -1,4 +1,4 @@ -package io.computenode.samples.cyfra.foton +package io.computenode.cyfra.samples.foton import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.library.Color.hex diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/oldsamples/Raytracing.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala similarity index 99% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/oldsamples/Raytracing.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala index b9c2279d..667a7fe0 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/oldsamples/Raytracing.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala @@ -1,10 +1,10 @@ -package io.computenode.samples.cyfra.oldsamples +package io.computenode.cyfra.samples.oldsamples import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.runtime.* -import io.computenode.cyfra.runtime.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/1sample.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala similarity index 69% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/1sample.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala index 518687d3..c42c9ea0 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/1sample.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala @@ -1,8 +1,8 @@ -package io.computenode.samples.cyfra.slides +package io.computenode.cyfra.samples.slides import io.computenode.cyfra.dsl.{*, given} -import io.computenode.cyfra.runtime.* -import io.computenode.cyfra.runtime.mem.FloatMem +import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.aalegacy.mem.FloatMem given GContext = new GContext() diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/2simpleray.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala similarity index 91% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/2simpleray.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala index 6575bbaa..963759ea 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/2simpleray.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala @@ -1,10 +1,10 @@ -package io.computenode.samples.cyfra.slides +package io.computenode.cyfra.samples.slides import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.runtime.* -import io.computenode.cyfra.runtime.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/3rays.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala similarity index 97% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/3rays.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala index 7784be27..a8415aa4 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/3rays.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala @@ -1,12 +1,12 @@ -package io.computenode.samples.cyfra.slides +package io.computenode.cyfra.samples.slides import io.computenode.cyfra.* import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.runtime.* -import io.computenode.cyfra.runtime.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/4random.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala similarity index 98% rename from cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/4random.scala rename to cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala index 4ecd8e8b..66dcee6c 100644 --- a/cyfra-examples/src/main/scala/io/computenode/samples/cyfra/slides/4random.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala @@ -1,11 +1,11 @@ -package io.computenode.samples.cyfra.slides +package io.computenode.cyfra.samples.slides import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.runtime.* -import io.computenode.cyfra.runtime.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala index d8d6dff5..8041d1ba 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala @@ -6,9 +6,9 @@ import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.animation.AnimatedFunctionRenderer.{AnimationIteration, RenderFn} import io.computenode.cyfra.foton.animation.AnimationFunctions.AnimationInstant -import io.computenode.cyfra.runtime.mem.GMem.fRGBA -import io.computenode.cyfra.runtime.mem.Vec4FloatMem -import io.computenode.cyfra.runtime.{GContext, GFunction, UniformContext} +import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.{GContext, GFunction, UniformContext} import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala index df4ea7ca..f955fce9 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala @@ -3,8 +3,8 @@ package io.computenode.cyfra.foton.animation import io.computenode.cyfra import io.computenode.cyfra.dsl.Value.* import io.computenode.cyfra.dsl.{*, given} -import io.computenode.cyfra.runtime.GFunction -import io.computenode.cyfra.runtime.mem.GMem.fRGBA +import io.computenode.cyfra.core.aalegacy.GFunction +import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA import io.computenode.cyfra.utility.ImageUtility import io.computenode.cyfra.utility.Units.Milliseconds import io.computenode.cyfra.utility.Utility.timed diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala index 6a314eb5..48f1589e 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala @@ -6,9 +6,9 @@ import io.computenode.cyfra.dsl.Value.* import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.rt.ImageRtRenderer.RaytracingIteration -import io.computenode.cyfra.runtime.mem.GMem.fRGBA -import io.computenode.cyfra.runtime.mem.Vec4FloatMem -import io.computenode.cyfra.runtime.{GFunction, UniformContext} +import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} import io.computenode.cyfra.utility.ImageUtility import io.computenode.cyfra.utility.Utility.timed diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala index 1c591a71..6bea8f3b 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala @@ -10,7 +10,7 @@ import io.computenode.cyfra.dsl.library.Random import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.rt.RtRenderer.RayHitInfo -import io.computenode.cyfra.runtime.GContext +import io.computenode.cyfra.core.aalegacy.GContext import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala index 2674c237..ba212de3 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala @@ -7,9 +7,9 @@ import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.animation.AnimationRenderer import io.computenode.cyfra.foton.rt.RtRenderer import io.computenode.cyfra.foton.rt.animation.AnimationRtRenderer.RaytracingIteration -import io.computenode.cyfra.runtime.mem.GMem.fRGBA -import io.computenode.cyfra.runtime.mem.Vec4FloatMem -import io.computenode.cyfra.runtime.{GFunction, UniformContext} +import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA +import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} class AnimationRtRenderer(params: AnimationRtRenderer.Parameters) extends RtRenderer(params) From 6182ee0856a264da6880e3b061906e1ba6b83538 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 22 Jun 2025 15:55:20 +0200 Subject: [PATCH 02/16] Scaffold ready --- .../cyfra/core/GBufferRegion.scala | 32 ++++- .../computenode/cyfra/core/GExecution.scala | 42 ++++--- .../io/computenode/cyfra/core/GProgram.scala | 33 ++++-- .../cyfra/core/layout/Layout.scala | 1 - .../cyfra/core/layout/LayoutStruct.scala | 38 +++--- .../io/computenode/cyfra/dsl/Expression.scala | 3 +- .../io/computenode/cyfra/dsl/Value.scala | 4 +- .../cyfra/dsl/buffer/GBuffer.scala | 2 +- .../computenode/cyfra/dsl/buffer/Read.scala | 4 +- .../computenode/cyfra/dsl/buffer/Write.scala | 3 +- .../io/computenode/cyfra/dsl/gio/GIO.scala | 24 ++-- .../cyfra/samples/TestingStuff.scala | 111 ++++++++++++++---- 12 files changed, 205 insertions(+), 92 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index 8de7f667..9703f645 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -1,12 +1,34 @@ package io.computenode.cyfra.core +import io.computenode.cyfra.core.GProgram.BufferSizeSpec import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.buffer.GBuffer + +import java.nio.ByteBuffer sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: LayoutStruct]: val initAlloc: ReqAlloc object GBufferRegion: + private[cyfra] case class ZeroedBuffer[T <: Value](size: Int) extends GBuffer[T] + + private[cyfra] case class BufferFromRam[T <: Value](buff: ByteBuffer) extends GBuffer[T] + + trait InitAlloc: + extension (buffers: GBuffer.type) + def apply[T <: Value](size: Int): GBuffer[T] = + ZeroedBuffer[T](size) + + def apply[T <: Value](buff: ByteBuffer): GBuffer[T] = + BufferFromRam[T](buff) + + trait FinalizeAlloc: + extension [T <: Value](buffer: GBuffer[T]) + def readTo(bb: ByteBuffer): Unit = + () + def allocate[Alloc <: Layout: LayoutStruct]: GBufferRegion[Alloc, Alloc] = AllocRegion(summon[LayoutStruct[Alloc]].layoutRef) @@ -23,9 +45,10 @@ object GBufferRegion: def map[NewAlloc <: Layout: LayoutStruct](f: Allocation ?=> ResAlloc => NewAlloc): GBufferRegion[ReqAlloc, NewAlloc] = MapRegion(region, (alloc: Allocation) => (resAlloc: ResAlloc) => f(using alloc)(resAlloc)) - def runUnsafe(init: Allocation ?=> ReqAlloc, onDone: Allocation ?=> ResAlloc => Unit): Unit = + def runUnsafe(init: InitAlloc ?=> ReqAlloc, onDone: FinalizeAlloc ?=> ResAlloc => Unit): Unit = + val initAlloc = new InitAlloc {} + init(using initAlloc) val alloc = new Allocation {} - init(using alloc) val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): case _: AllocRegion[?] => None case MapRegion(req, f) => @@ -33,5 +56,6 @@ object GBufferRegion: val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) => step(alloc)(acc) - - onDone(using alloc)(bodyAlloc.asInstanceOf[ResAlloc]) + + val finalizeAlloc = new FinalizeAlloc {} + onDone(using finalizeAlloc)(bodyAlloc.asInstanceOf[ResAlloc]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index fd3ea228..d4efaeda 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -5,9 +5,12 @@ import io.computenode.cyfra.core.aalegacy.GContext import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.buffer.GBuffer import io.computenode.cyfra.dsl.gio.GIO +import io.computenode.cyfra.dsl.struct.{GStruct, GStructSchema} +import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef +import izumi.reflect.Tag -case class GExecution[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[?]]): - def execute(layout: L)(using Allocation): L = +case class GExecution[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): + def execute(layout: L, params: Params)(using Allocation): L = println("Executing GExecution...") layout // Return the layout after execution @@ -15,28 +18,23 @@ object GExecution: def forLayout[Params, L <: Layout](using layoutStruct: LayoutStruct[L]): GExecutionBuilder[Params, L] = GExecutionBuilder(layoutStruct, Seq.empty) - - case class GExecutionBuilder[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[?]]): - def addProgram[ProgramLayout <: Layout, ProgramParams, P <: GProgram[ProgramParams, ProgramLayout]]( - program: P, - )(mapLayout: L => ProgramBinding[ProgramLayout], mapParams: Params => ProgramParams): GExecutionBuilder[Params, L] = - val ProgramBinding(mappedLayout, dispatch) = mapLayout(layoutStruct.layoutRef) - val boundProgram = BoundProgram(mappedLayout, dispatch, program.body(mappedLayout)) + + case class GExecutionBuilder[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): + def addProgram[ProgramParams, Uniform <: GStruct[Uniform]: GStructSchema: Tag, ProgramLayout <: Layout, P <: GProgram[ + ProgramParams, + Uniform, + ProgramLayout, + ]](program: P)(mapLayout: L => ProgramLayout, mapParams: Params => ProgramParams): GExecutionBuilder[Params, L] = + val mappedLayout = mapLayout(layoutStruct.layoutRef) + val boundProgram = BoundProgram(mappedLayout, mapParams, program) GExecutionBuilder(layoutStruct, boundPrograms :+ boundProgram) - def compile(using GContext): GExecution[Params, L] = + def compile()(using GContext): GExecution[Params, L] = println("Compiling GExecution...") GExecution(layoutStruct, boundPrograms) - type WorkDimensions = (Int, Int, Int) - - sealed trait ProgramDispatch - - case class DynamicDispatch[L <: Layout](buffer: GBuffer[?], offset: Int) extends ProgramDispatch - - case class StaticDispatch[L <: Layout](size: WorkDimensions) extends ProgramDispatch - - case class ProgramBinding[L <: Layout](programLayout: L, programDispatch: ProgramDispatch) - - case class BoundProgram[L <: Layout](layout: L, dispatch: ProgramDispatch, body: GIO[?]) - + case class BoundProgram[LParams, Params, Uniform <: GStruct[Uniform], L <: Layout]( + layout: L, + paramsMapping: LParams => Params, + program: GProgram[Params, Uniform, L], + ) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index ca676d3e..5467f55b 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -8,21 +8,40 @@ import java.nio.ByteBuffer import GProgram.* import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.struct.GStruct +import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag -case class GProgram[Params, L <: Layout: LayoutStruct] private (body: L => GIO[?], layout: BufferOfSize => Params => L, workgroupSize: WorkgroupSize): +class GProgram[Params, Uniform <: GStruct[Uniform], L <: Layout: LayoutStruct] private ( + val body: (L, Uniform) => GIO[?], + val layout: InitProgramLayout => Params => L, + val uniform: Params => Uniform, + val dispatch: (L, Params) => ProgramDispatch, + val workgroupSize: WorkDimensions, +): private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] object GProgram: - type WorkgroupSize = (Int, Int, Int) + + type WorkDimensions = (Int, Int, Int) + + sealed trait ProgramDispatch + + case class DynamicDispatch[L <: Layout](buffer: GBuffer[?], offset: Int) extends ProgramDispatch + + case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch private[cyfra] case class BufferSizeSpec[T <: Value](size: Int) extends GBuffer[T] - trait BufferOfSize: + trait InitProgramLayout: extension (buffers: GBuffer.type) - def apply[T <: Value](size: Int): BufferSizeSpec[T] = + def apply[T <: Value](size: Int): GBuffer[T] = BufferSizeSpec[T](size) - - def apply[Params, L <: Layout : LayoutStruct](layout: BufferOfSize ?=> Params => L, workgroupSize: WorkgroupSize = (128, 1, 1))(body: L => GIO[?]): GProgram[Params, L] = - new GProgram(body, s => layout(using s), workgroupSize) + def apply[Params, Uniform <: GStruct[Uniform], L <: Layout: LayoutStruct]( + layout: InitProgramLayout ?=> Params => L, + uniform: Params => Uniform, + dispatch: (L, Params) => ProgramDispatch, + workgroupSize: WorkDimensions = (128, 1, 1), + )(body: (L, Uniform) => GIO[?]): GProgram[Params, Uniform, L] = + new GProgram(body, s => layout(using s), uniform, dispatch, workgroupSize) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala index 505b8a57..b89f09a3 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala @@ -4,4 +4,3 @@ import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.buffer.GBuffer trait Layout - diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala index a630ea0c..fb76dfa0 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -14,7 +14,7 @@ case class LayoutStruct[T <: Layout: Tag](private[cyfra] val layoutRef: T, priva object LayoutStruct: - inline given derived[T <: Layout: Tag]: LayoutStruct[T] = ${derivedImpl} + inline given derived[T <: Layout: Tag]: LayoutStruct[T] = ${ derivedImpl } def derivedImpl[T <: Layout: Type](using quotes: Quotes): Expr[LayoutStruct[T]] = import quotes.reflect.* @@ -22,12 +22,13 @@ object LayoutStruct: val tpe = TypeRepr.of[T] val sym = tpe.typeSymbol - if !sym.isClassDef || !sym.flags.is(Flags.Case) then - report.errorAndAbort("LayoutStruct can only be derived for case classes") + if !sym.isClassDef || !sym.flags.is(Flags.Case) then report.errorAndAbort("LayoutStruct can only be derived for case classes") - val fieldTypes = sym.caseFields.map(_.tree).map: - case ValDef(_, tpt, _) => tpt.tpe - case _ => report.errorAndAbort("Unexpected field type in case class") + val fieldTypes = sym.caseFields + .map(_.tree) + .map: + case ValDef(_, tpt, _) => tpt.tpe + case _ => report.errorAndAbort("Unexpected field type in case class") if !fieldTypes.forall(_ <:< TypeRepr.of[GBuffer[?]]) then report.errorAndAbort("LayoutStruct can only be derived for case classes with GBuffer elements") @@ -38,9 +39,11 @@ object LayoutStruct: val tags = valueTypes.map: tpe => tpe.asType match case '[t] => - (tpe.asType, Expr.summon[Tag[t]] match - case Some(tagExpr) => tagExpr - case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}") + ( + tpe.asType, + Expr.summon[Tag[t]] match + case Some(tagExpr) => tagExpr + case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}"), ) val buffers = tags.zipWithIndex.map: @@ -48,26 +51,17 @@ object LayoutStruct: tpe match case '[type t <: Value; t] => '{ - BufferRef[t](${ Expr(i) }, ${tag.asExprOf[Tag[t]]}) + BufferRef[t](${ Expr(i) }, ${ tag.asExprOf[Tag[t]] }) } val constructor = sym.primaryConstructor - val layoutInstance = Apply( - Select( - New(TypeIdent(sym)), - constructor, - ), - buffers.map(_.asTerm) - ) + val layoutInstance = Apply(Select(New(TypeIdent(sym)), constructor), buffers.map(_.asTerm)) val layoutRef = layoutInstance.asExprOf[T] val soleTags = tags.map(_._2.asExprOf[Tag[? <: Value]]).toList '{ - LayoutStruct[T]( - $layoutRef, - ${Expr.ofList(soleTags)} - ) - } \ No newline at end of file + LayoutStruct[T]($layoutRef, ${ Expr.ofList(soleTags) }) + } diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Expression.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Expression.scala index 52b8b844..18f81033 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Expression.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Expression.scala @@ -1,5 +1,5 @@ package io.computenode.cyfra.dsl -import io.computenode.cyfra.dsl.Expression + import Expression.{Const, treeidState} import io.computenode.cyfra.dsl.library.Functions.* import io.computenode.cyfra.dsl.Value.* @@ -107,6 +107,7 @@ object Expression: case class ExtFunctionCall[R <: Value: Tag](fn: FunctionName, args: List[Value]) extends Expression[R] case class FunctionCall[R <: Value: Tag](fn: FnIdentifier, body: Scope[R], args: List[Value]) extends E[R] + case object InvocationId extends E[Int32] case class Pass[T <: Value: Tag](value: T) extends E[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala index 764bf2fc..985357e7 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/Value.scala @@ -19,9 +19,9 @@ object Value: def fromExpr(expr: E[T])(using name: Source): T object FromExpr: - def fromExpr[T <: Value](expr: E[T])(using f: FromExpr[T]): T = + def fromExpr[T <: Value](expr: E[T])(using f: FromExpr[T]): T = f.fromExpr(expr) - + sealed trait Scalar extends Value trait FloatType extends Scalar diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala index 83623053..85cf7e35 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala @@ -4,4 +4,4 @@ import io.computenode.cyfra.dsl.Value trait GBuffer[T <: Value] -object GBuffer \ No newline at end of file +object GBuffer diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala index 40203d87..1d0aadf7 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala @@ -1,5 +1,7 @@ package io.computenode.cyfra.dsl.buffer +import io.computenode.cyfra.dsl.Value.Int32 import io.computenode.cyfra.dsl.{Expression, Value} +import izumi.reflect.Tag -case class Read[T <: Value](buffer: GBuffer[T], index: Int) extends Expression[T] \ No newline at end of file +case class Read[T <: Value: Tag](buffer: GBuffer[T], index: Int32) extends Expression[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala index d6b2d2de..1ac845b9 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala @@ -1,7 +1,8 @@ package io.computenode.cyfra.dsl.buffer import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.Int32 import io.computenode.cyfra.dsl.gio.GIO -case class Write[T <: Value](buffer: GBuffer[T], index: Int, value: T) extends GIO[Unit]: +case class Write[T <: Value](buffer: GBuffer[T], index: Int32, value: T) extends GIO[Unit]: override def underlying: Unit = () diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala index 83c82922..c944a63f 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala @@ -1,10 +1,12 @@ package io.computenode.cyfra.dsl.gio -import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.Value.FromExpr +import io.computenode.cyfra.dsl.{*, given} +import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} import io.computenode.cyfra.dsl.Value.FromExpr.fromExpr import io.computenode.cyfra.dsl.buffer.{GBuffer, Read, Write} +import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.gio.GIO.* +import izumi.reflect.Tag trait GIO[T]: @@ -21,15 +23,23 @@ object GIO: case class FlatMap[T, U](gio: GIO[T], next: GIO[U]) extends GIO[U]: override def underlying: U = next.underlying - + + // TODO repeat that collects results + case class Repeat(n: Int32, f: Int32 => GIO[?]) extends GIO[Unit]: + override def underlying: Unit = () def pure[T](value: T): GIO[T] = Pure(value) def value[T](value: T): GIO[T] = Pure(value) - - def write[T <: Value](buffer: GBuffer[T], index: Int, value: T): GIO[Unit] = + + def repeat(n: Int32)(f: Int32 => GIO[?]): GIO[Unit] = + Repeat(n, f) + + def write[T <: Value](buffer: GBuffer[T], index: Int32, value: T): GIO[Unit] = Write(buffer, index, value) - - def read[T <: Value : FromExpr](buffer: GBuffer[T], index: Int): T = + + def read[T <: Value: FromExpr: Tag](buffer: GBuffer[T], index: Int32): T = fromExpr(Read(buffer, index)) + def invocationId: Int32 = + fromExpr(InvocationId) diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index eca2f89d..bbfe2749 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -1,35 +1,100 @@ package io.computenode.cyfra.samples -import io.computenode.cyfra.core.GProgram +import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.{GBufferRegion, GExecution, GProgram} import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.Value.{GBoolean, Int32} import io.computenode.cyfra.dsl.buffer.GBuffer import io.computenode.cyfra.dsl.gio.GIO +import io.computenode.cyfra.dsl.struct.GStruct +import io.computenode.cyfra.dsl.{*, given} +import org.lwjgl.BufferUtils object TestingStuff: + + given GContext = GContext() + + // === Emit program === + + case class EmitProgramParams(inSize: Int, emitN: Int) + + case class EmitProgramLayout(in: GBuffer[Int32], out: GBuffer[Int32]) extends Layout + + case class EmitProgramUniform(emitN: Int32) extends GStruct[EmitProgramUniform] + + val emitProgram = GProgram[EmitProgramParams, EmitProgramUniform, EmitProgramLayout]( + layout = params => EmitProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[Int32](params.inSize * params.emitN)), + uniform = params => EmitProgramUniform(params.emitN), + dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)) + ): (layout, args) => + val invocId = GIO.invocationId + val element = GIO.read(layout.in, invocId) + val bufferOffset = invocId * args.emitN + GIO.repeat(args.emitN): i => + GIO.write(layout.out, bufferOffset + i, element) + + // === Filter program === + case class FilterProgramParams(inSize: Int, filterValue: Int32) + + case class FilterProgramLayout(in: GBuffer[Int32], out: GBuffer[GBoolean]) extends Layout + + case class FilterProgramUniform(filterValue: Int32) extends GStruct[FilterProgramUniform] + + val filterProgram = GProgram[FilterProgramParams, FilterProgramUniform, FilterProgramLayout]( + layout = params => FilterProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[GBoolean](params.inSize)), + uniform = params => FilterProgramUniform(params.filterValue), + dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)) + ): (layout, args) => + val invocId = GIO.invocationId + val element = GIO.read(layout.in, invocId) + val isMatch = element === args.filterValue + GIO.write(layout.out, invocId, isMatch) + + // === GExecution === + + case class EmitFilterParams(inSize: Int, emitN: Int, filterValue: Int) + + case class EmitFilterLayout(inBuffer: GBuffer[Int32], emitBuffer: GBuffer[Int32], filterBuffer: GBuffer[GBoolean]) extends Layout + + val emitFilterExecution = GExecution + .forLayout[EmitFilterParams, EmitFilterLayout] + .addProgram(emitProgram)( + mapLayout = layout => EmitProgramLayout(in = layout.inBuffer, out = layout.emitBuffer), + mapParams = params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN) + ) + .addProgram(filterProgram)( + mapLayout = layout => FilterProgramLayout(in = layout.emitBuffer, out = layout.filterBuffer), + mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue) + ) + .compile() + @main def test = - case class EmitProgramParams( - inSize: Int, - emitN: Int + val emitFilterParams = EmitFilterParams( + inSize = 1024, + emitN = 2, + filterValue = 42 + ) + + val region = GBufferRegion.allocate[EmitFilterLayout] + .map: region => + emitFilterExecution.execute(region, emitFilterParams) + + val data = (0 to 1024).toArray + val buffer = BufferUtils.createByteBuffer(data.length * 4) + buffer.asIntBuffer().put(data).flip() + + val result = BufferUtils.createByteBuffer(data.length * 2) + region.runUnsafe( + init = EmitFilterLayout( + inBuffer = GBuffer[Int32](buffer), + emitBuffer = GBuffer[Int32](data.length * 2), + filterBuffer = GBuffer[GBoolean](data.length * 2) + ), + onDone = layout => + layout.filterBuffer.readTo(result) ) - - case class EmitProgramLayout( - in: GBuffer[Int32], - out: GBuffer[Int32] - ) extends Layout - - val emitProgram = GProgram[EmitProgramParams, EmitProgramLayout]( - params => EmitProgramLayout( - in = GBuffer[Int32](params.inSize), - out = GBuffer[Int32](params.emitN) - )) { layout => - for - _ <- GIO.pure(2) - yield () - } - - - - \ No newline at end of file + + + From 58e7893e17165b858c32a3bd165a897e68572636 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 22 Jun 2025 15:56:14 +0200 Subject: [PATCH 03/16] Some formatting --- .../cyfra/core/GBufferRegion.scala | 8 +++--- .../io/computenode/cyfra/core/GProgram.scala | 2 +- .../cyfra/samples/TestingStuff.scala | 25 +++++++------------ 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index 9703f645..f6560a24 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -13,17 +13,17 @@ sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: object GBufferRegion: private[cyfra] case class ZeroedBuffer[T <: Value](size: Int) extends GBuffer[T] - + private[cyfra] case class BufferFromRam[T <: Value](buff: ByteBuffer) extends GBuffer[T] trait InitAlloc: extension (buffers: GBuffer.type) def apply[T <: Value](size: Int): GBuffer[T] = ZeroedBuffer[T](size) - + def apply[T <: Value](buff: ByteBuffer): GBuffer[T] = BufferFromRam[T](buff) - + trait FinalizeAlloc: extension [T <: Value](buffer: GBuffer[T]) def readTo(bb: ByteBuffer): Unit = @@ -56,6 +56,6 @@ object GBufferRegion: val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) => step(alloc)(acc) - + val finalizeAlloc = new FinalizeAlloc {} onDone(using finalizeAlloc)(bodyAlloc.asInstanceOf[ResAlloc]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 5467f55b..6f78f9ed 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -32,7 +32,7 @@ object GProgram: case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch private[cyfra] case class BufferSizeSpec[T <: Value](size: Int) extends GBuffer[T] - + trait InitProgramLayout: extension (buffers: GBuffer.type) def apply[T <: Value](size: Int): GBuffer[T] = diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index bbfe2749..1dd584f4 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -25,7 +25,7 @@ object TestingStuff: val emitProgram = GProgram[EmitProgramParams, EmitProgramUniform, EmitProgramLayout]( layout = params => EmitProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[Int32](params.inSize * params.emitN)), uniform = params => EmitProgramUniform(params.emitN), - dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)) + dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), ): (layout, args) => val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) @@ -43,7 +43,7 @@ object TestingStuff: val filterProgram = GProgram[FilterProgramParams, FilterProgramUniform, FilterProgramLayout]( layout = params => FilterProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[GBoolean](params.inSize)), uniform = params => FilterProgramUniform(params.filterValue), - dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)) + dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), ): (layout, args) => val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) @@ -60,24 +60,21 @@ object TestingStuff: .forLayout[EmitFilterParams, EmitFilterLayout] .addProgram(emitProgram)( mapLayout = layout => EmitProgramLayout(in = layout.inBuffer, out = layout.emitBuffer), - mapParams = params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN) + mapParams = params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN), ) .addProgram(filterProgram)( mapLayout = layout => FilterProgramLayout(in = layout.emitBuffer, out = layout.filterBuffer), - mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue) + mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue), ) .compile() @main def test = - val emitFilterParams = EmitFilterParams( - inSize = 1024, - emitN = 2, - filterValue = 42 - ) + val emitFilterParams = EmitFilterParams(inSize = 1024, emitN = 2, filterValue = 42) - val region = GBufferRegion.allocate[EmitFilterLayout] + val region = GBufferRegion + .allocate[EmitFilterLayout] .map: region => emitFilterExecution.execute(region, emitFilterParams) @@ -90,11 +87,7 @@ object TestingStuff: init = EmitFilterLayout( inBuffer = GBuffer[Int32](buffer), emitBuffer = GBuffer[Int32](data.length * 2), - filterBuffer = GBuffer[GBoolean](data.length * 2) + filterBuffer = GBuffer[GBoolean](data.length * 2), ), - onDone = layout => - layout.filterBuffer.readTo(result) + onDone = layout => layout.filterBuffer.readTo(result), ) - - - From d3a2878d96221c5d82215c86b744a1f355e59dda Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 22 Jun 2025 21:43:33 +0200 Subject: [PATCH 04/16] Added Execution Result --- .../computenode/cyfra/core/GExecution.scala | 16 ++++++++-------- cyfra-examples/src/main/resources/gio.scala | 12 ++++++++++++ .../cyfra/samples/TestingStuff.scala | 19 +++++++++++++------ 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 cyfra-examples/src/main/resources/gio.scala diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index d4efaeda..e5131fda 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -9,29 +9,29 @@ import io.computenode.cyfra.dsl.struct.{GStruct, GStructSchema} import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef import izumi.reflect.Tag -case class GExecution[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): - def execute(layout: L, params: Params)(using Allocation): L = +case class GExecution[Params, L <: Layout, RL <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]], toResult: L => RL): + def execute(layout: L, params: Params)(using Allocation): RL = println("Executing GExecution...") - layout // Return the layout after execution + toResult(layout) object GExecution: - def forLayout[Params, L <: Layout](using layoutStruct: LayoutStruct[L]): GExecutionBuilder[Params, L] = + def build[Params, L <: Layout, RL <: Layout](using layoutStruct: LayoutStruct[L]): GExecutionBuilder[Params, L, RL] = GExecutionBuilder(layoutStruct, Seq.empty) - case class GExecutionBuilder[Params, L <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): + case class GExecutionBuilder[Params, L <: Layout, RL <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): def addProgram[ProgramParams, Uniform <: GStruct[Uniform]: GStructSchema: Tag, ProgramLayout <: Layout, P <: GProgram[ ProgramParams, Uniform, ProgramLayout, - ]](program: P)(mapLayout: L => ProgramLayout, mapParams: Params => ProgramParams): GExecutionBuilder[Params, L] = + ]](program: P)(mapLayout: L => ProgramLayout, mapParams: Params => ProgramParams): GExecutionBuilder[Params, L, RL] = val mappedLayout = mapLayout(layoutStruct.layoutRef) val boundProgram = BoundProgram(mappedLayout, mapParams, program) GExecutionBuilder(layoutStruct, boundPrograms :+ boundProgram) - def compile()(using GContext): GExecution[Params, L] = + def compile(toResult: L => RL)(using GContext): GExecution[Params, L, RL] = println("Compiling GExecution...") - GExecution(layoutStruct, boundPrograms) + GExecution(layoutStruct, boundPrograms, toResult) case class BoundProgram[LParams, Params, Uniform <: GStruct[Uniform], L <: Layout]( layout: L, diff --git a/cyfra-examples/src/main/resources/gio.scala b/cyfra-examples/src/main/resources/gio.scala new file mode 100644 index 00000000..1ef1889a --- /dev/null +++ b/cyfra-examples/src/main/resources/gio.scala @@ -0,0 +1,12 @@ +import io.computenode.cyfra.dsl.Value.Int32 + +val inBuffer = GBuffer[Int32]() +val outBuffer = GBuffer[Int32]() + +val program = GProgram.on(inBuffer, outBuffer): + case (in, out) => for + index <- GIO.workerIndex + a <- in.read(index) + _ <- out.write(index, a + 1) + _ <- out.write(index * 2, a * 2) + yield () \ No newline at end of file diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index 1dd584f4..e63fa80a 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -22,6 +22,10 @@ object TestingStuff: case class EmitProgramUniform(emitN: Int32) extends GStruct[EmitProgramUniform] + // 1 2 3 4 + // VVVV (inSize = 4, emitN = 3) + // 1 1 1 2 2 2 3 3 3 4 4 4 + val emitProgram = GProgram[EmitProgramParams, EmitProgramUniform, EmitProgramLayout]( layout = params => EmitProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[Int32](params.inSize * params.emitN)), uniform = params => EmitProgramUniform(params.emitN), @@ -34,7 +38,7 @@ object TestingStuff: GIO.write(layout.out, bufferOffset + i, element) // === Filter program === - case class FilterProgramParams(inSize: Int, filterValue: Int32) + case class FilterProgramParams(inSize: Int, filterValue: Int) case class FilterProgramLayout(in: GBuffer[Int32], out: GBuffer[GBoolean]) extends Layout @@ -56,17 +60,19 @@ object TestingStuff: case class EmitFilterLayout(inBuffer: GBuffer[Int32], emitBuffer: GBuffer[Int32], filterBuffer: GBuffer[GBoolean]) extends Layout + case class EmitFilterResult(out: GBuffer[GBoolean]) extends Layout + val emitFilterExecution = GExecution - .forLayout[EmitFilterParams, EmitFilterLayout] - .addProgram(emitProgram)( + .build[EmitFilterParams, EmitFilterLayout, EmitFilterResult] + .addProgram(emitProgram)( // (inBuffer, emitBuffer) mapLayout = layout => EmitProgramLayout(in = layout.inBuffer, out = layout.emitBuffer), mapParams = params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN), ) - .addProgram(filterProgram)( + .addProgram(filterProgram)( // (emitBuffer, filterBuffer) mapLayout = layout => FilterProgramLayout(in = layout.emitBuffer, out = layout.filterBuffer), mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue), ) - .compile() + .compile(layout => EmitFilterResult(layout.filterBuffer)) @main def test = @@ -81,6 +87,7 @@ object TestingStuff: val data = (0 to 1024).toArray val buffer = BufferUtils.createByteBuffer(data.length * 4) buffer.asIntBuffer().put(data).flip() + val result = BufferUtils.createByteBuffer(data.length * 2) region.runUnsafe( @@ -89,5 +96,5 @@ object TestingStuff: emitBuffer = GBuffer[Int32](data.length * 2), filterBuffer = GBuffer[GBoolean](data.length * 2), ), - onDone = layout => layout.filterBuffer.readTo(result), + onDone = layout => layout.out.readTo(result), ) From f52c7851546c28d6eef0c8dd6ded4beab2c7259e Mon Sep 17 00:00:00 2001 From: Szymon Date: Mon, 30 Jun 2025 02:36:16 +0200 Subject: [PATCH 05/16] Construction works in progress --- .../computenode/cyfra/core/Allocation.scala | 2 +- .../cyfra/core/GBufferRegion.scala | 2 +- .../computenode/cyfra/core/GExecution.scala | 68 +++++++++++++------ .../io/computenode/cyfra/core/GProgram.scala | 27 +++++--- .../core/{buffer => binding}/BufferRef.scala | 4 +- .../cyfra/core/buffer/SizedBuffer.scala | 6 -- .../cyfra/core/layout/Layout.scala | 4 +- .../cyfra/core/layout/LayoutStruct.scala | 4 +- .../cyfra/dsl/binding/GBinding.scala | 3 + .../cyfra/dsl/binding/GBuffer.scala | 7 ++ .../cyfra/dsl/binding/GUniform.scala | 8 +++ .../cyfra/dsl/binding/ReadBuffer.scala | 7 ++ .../cyfra/dsl/binding/ReadUniform.scala | 7 ++ .../Write.scala => binding/WriteBuffer.scala} | 4 +- .../cyfra/dsl/binding/WriteUniform.scala | 9 +++ .../cyfra/dsl/buffer/GBuffer.scala | 7 -- .../computenode/cyfra/dsl/buffer/Read.scala | 7 -- .../io/computenode/cyfra/dsl/gio/GIO.scala | 6 +- .../cyfra/samples/TestingStuff.scala | 31 +++++---- 19 files changed, 140 insertions(+), 73 deletions(-) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{buffer => binding}/BufferRef.scala (68%) delete mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadBuffer.scala create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala rename cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/{buffer/Write.scala => binding/WriteBuffer.scala} (53%) create mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala delete mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala delete mode 100644 cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index 504225d1..67715efd 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -1,7 +1,7 @@ package io.computenode.cyfra.core import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer import java.nio.ByteBuffer diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index f6560a24..ec6f3f7b 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -3,7 +3,7 @@ package io.computenode.cyfra.core import io.computenode.cyfra.core.GProgram.BufferSizeSpec import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer import java.nio.ByteBuffer diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index e5131fda..abd38b35 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -3,38 +3,66 @@ package io.computenode.cyfra.core import io.computenode.cyfra.core.GExecution.* import io.computenode.cyfra.core.aalegacy.GContext import io.computenode.cyfra.core.layout.* -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.{GStruct, GStructSchema} import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef import izumi.reflect.Tag +import GExecution.* -case class GExecution[Params, L <: Layout, RL <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]], toResult: L => RL): +trait GExecution[-Params, -L <: Layout, +RL <: Layout]: def execute(layout: L, params: Params)(using Allocation): RL = println("Executing GExecution...") - toResult(layout) + ??? -object GExecution: + def flatMap[NRL <: Layout, NP <: Params, NL <: L](f: RL => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] = + FlatMap(this, f) + + def mapResult[NRL <: Layout](f: RL => NRL): GExecution[Params, L, NRL] = + Map(this, f, identity, identity) + + def contramapLayout[NL <: Layout](f: NL => L): GExecution[Params, NL, RL] = + Map(this, identity, f, identity) - def build[Params, L <: Layout, RL <: Layout](using layoutStruct: LayoutStruct[L]): GExecutionBuilder[Params, L, RL] = - GExecutionBuilder(layoutStruct, Seq.empty) + def contramapParams[NP](f: NP => Params): GExecution[NP, L, RL] = + Map(this, identity, identity, f) + + def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: L, P <: GProgram[ + ProgramParams, + ProgramLayout, + ]](program: P)(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ProgramLayout] = + val adapted = program.contramapParams(mapParams).contramapLayout(mapLayout) + flatMap(_ => adapted) + +object GExecution: - case class GExecutionBuilder[Params, L <: Layout, RL <: Layout](layoutStruct: LayoutStruct[L], boundPrograms: Seq[BoundProgram[Params, ?, ?, ?]]): - def addProgram[ProgramParams, Uniform <: GStruct[Uniform]: GStructSchema: Tag, ProgramLayout <: Layout, P <: GProgram[ - ProgramParams, - Uniform, - ProgramLayout, - ]](program: P)(mapLayout: L => ProgramLayout, mapParams: Params => ProgramParams): GExecutionBuilder[Params, L, RL] = - val mappedLayout = mapLayout(layoutStruct.layoutRef) - val boundProgram = BoundProgram(mappedLayout, mapParams, program) - GExecutionBuilder(layoutStruct, boundPrograms :+ boundProgram) + def forParams[Params, L <: Layout, LR <: Layout, NP](fn: Params => GExecution[NP, L, LR]): GExecution[Params & NP, L, LR] = + ??? + + private case class FlatMap[Params, L <: Layout, RL <: Layout, NRL <: Layout]( + execution: GExecution[Params, L, RL], + f: RL => GExecution[Params, L, NRL], + ) extends GExecution[Params, L, NRL] + + + private case class Map[P, NP, L <: Layout, NL <: Layout, RL <: Layout, NRL <: Layout]( + execution: GExecution[P, L, RL], + mapResult: RL => NRL, + contramapLayout: NL => L, + contramapParams: NP => P, + ) extends GExecution[NP, NL, NRL]: - def compile(toResult: L => RL)(using GContext): GExecution[Params, L, RL] = - println("Compiling GExecution...") - GExecution(layoutStruct, boundPrograms, toResult) + override def mapResult[NNRL <: Layout](f: NRL => NNRL): GExecution[NP, NL, NNRL] = + Map(execution, mapResult andThen f, contramapLayout, contramapParams) + + override def contramapParams[NNP](f: NNP => NP): GExecution[NNP, NL, NRL] = + Map(execution, mapResult, contramapLayout, f andThen contramapParams) + + override def contramapLayout[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] = + Map(execution, mapResult, f andThen contramapLayout, contramapParams) - case class BoundProgram[LParams, Params, Uniform <: GStruct[Uniform], L <: Layout]( + case class BoundProgram[LParams, Params, L <: Layout]( layout: L, paramsMapping: LParams => Params, - program: GProgram[Params, Uniform, L], + program: GProgram[Params, L], ) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 6f78f9ed..6c327058 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -7,18 +7,17 @@ import io.computenode.cyfra.core.layout.Layout import java.nio.ByteBuffer import GProgram.* import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag -class GProgram[Params, Uniform <: GStruct[Uniform], L <: Layout: LayoutStruct] private ( - val body: (L, Uniform) => GIO[?], +class GProgram[Params, L <: Layout: LayoutStruct] private ( + val body: L => GIO[?], val layout: InitProgramLayout => Params => L, - val uniform: Params => Uniform, val dispatch: (L, Params) => ProgramDispatch, val workgroupSize: WorkDimensions, -): +) extends GExecution[Params, L, L]: private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] object GProgram: @@ -32,16 +31,26 @@ object GProgram: case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch private[cyfra] case class BufferSizeSpec[T <: Value](size: Int) extends GBuffer[T] + + private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag](value: T) extends GUniform[T] + + private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag]() extends GUniform[T] trait InitProgramLayout: extension (buffers: GBuffer.type) def apply[T <: Value](size: Int): GBuffer[T] = BufferSizeSpec[T](size) - def apply[Params, Uniform <: GStruct[Uniform], L <: Layout: LayoutStruct]( + extension (uniforms: GUniform.type) + def apply[T <: GStruct[T]: Tag](value: T): GUniform[T] = + ParamUniform[T](value) + + def apply[T <: GStruct[T]: Tag](): GUniform[T] = + DynamicUniform[T]() + + def apply[Params, L <: Layout: LayoutStruct]( layout: InitProgramLayout ?=> Params => L, - uniform: Params => Uniform, dispatch: (L, Params) => ProgramDispatch, workgroupSize: WorkDimensions = (128, 1, 1), - )(body: (L, Uniform) => GIO[?]): GProgram[Params, Uniform, L] = - new GProgram(body, s => layout(using s), uniform, dispatch, workgroupSize) + )(body: L => GIO[?]): GProgram[Params, L] = + new GProgram(body, s => layout(using s), dispatch, workgroupSize) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala similarity index 68% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala index d4564f1d..758b6077 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/BufferRef.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala @@ -1,7 +1,7 @@ -package io.computenode.cyfra.core.buffer +package io.computenode.cyfra.core.binding import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala deleted file mode 100644 index 5cfc3477..00000000 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/buffer/SizedBuffer.scala +++ /dev/null @@ -1,6 +0,0 @@ -package io.computenode.cyfra.core.buffer - -import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer - -case class SizedBuffer[T <: Value](size: Int) extends GBuffer[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala index b89f09a3..01f1d71f 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala @@ -1,6 +1,8 @@ package io.computenode.cyfra.core.layout import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer trait Layout + + \ No newline at end of file diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala index fb76dfa0..b8e64ed8 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -1,8 +1,8 @@ package io.computenode.cyfra.core.layout -import io.computenode.cyfra.core.buffer.BufferRef +import io.computenode.cyfra.core.binding.BufferRef import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.GBuffer import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala new file mode 100644 index 00000000..256413cf --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala @@ -0,0 +1,3 @@ +package io.computenode.cyfra.dsl.binding + +trait GBinding diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala new file mode 100644 index 00000000..d56d4b83 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.dsl.binding + +import io.computenode.cyfra.dsl.Value + +trait GBuffer[T <: Value] extends GBinding + +object GBuffer \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala new file mode 100644 index 00000000..869b2831 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala @@ -0,0 +1,8 @@ +package io.computenode.cyfra.dsl.binding + +import io.computenode.cyfra.dsl.struct.GStruct + +trait GUniform[T <: GStruct[T]] extends GBinding + def read: ReadUniform[T] + +object GUniform \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadBuffer.scala new file mode 100644 index 00000000..e0057720 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadBuffer.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.dsl.binding + +import io.computenode.cyfra.dsl.Value.Int32 +import io.computenode.cyfra.dsl.{Expression, Value} +import izumi.reflect.Tag + +case class ReadBuffer[T <: Value: Tag](buffer: GBuffer[T], index: Int32) extends Expression[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala new file mode 100644 index 00000000..e6cac8fb --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala @@ -0,0 +1,7 @@ +package io.computenode.cyfra.dsl.binding + +import io.computenode.cyfra.dsl.Expression +import io.computenode.cyfra.dsl.struct.GStruct +import izumi.reflect.Tag + +case class ReadUniform[T <: GStruct[T] : Tag](uniform: GUniform[T]) extends Expression[T] \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteBuffer.scala similarity index 53% rename from cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala rename to cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteBuffer.scala index 1ac845b9..53b0abf9 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Write.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteBuffer.scala @@ -1,8 +1,8 @@ -package io.computenode.cyfra.dsl.buffer +package io.computenode.cyfra.dsl.binding import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.Int32 import io.computenode.cyfra.dsl.gio.GIO -case class Write[T <: Value](buffer: GBuffer[T], index: Int32, value: T) extends GIO[Unit]: +case class WriteBuffer[T <: Value](buffer: GBuffer[T], index: Int32, value: T) extends GIO[Unit]: override def underlying: Unit = () diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala new file mode 100644 index 00000000..3fd8dc33 --- /dev/null +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala @@ -0,0 +1,9 @@ +package io.computenode.cyfra.dsl.binding + +import io.computenode.cyfra.dsl.gio.GIO +import io.computenode.cyfra.dsl.struct.GStruct +import izumi.reflect.Tag + +case class WriteUniform[T <: GStruct[T]: Tag](uniform: GUniform[T], value: T) extends GIO[Unit]: + override def underlying: Unit = () + diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala deleted file mode 100644 index 85cf7e35..00000000 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/GBuffer.scala +++ /dev/null @@ -1,7 +0,0 @@ -package io.computenode.cyfra.dsl.buffer - -import io.computenode.cyfra.dsl.Value - -trait GBuffer[T <: Value] - -object GBuffer diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala deleted file mode 100644 index 1d0aadf7..00000000 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/buffer/Read.scala +++ /dev/null @@ -1,7 +0,0 @@ -package io.computenode.cyfra.dsl.buffer - -import io.computenode.cyfra.dsl.Value.Int32 -import io.computenode.cyfra.dsl.{Expression, Value} -import izumi.reflect.Tag - -case class Read[T <: Value: Tag](buffer: GBuffer[T], index: Int32) extends Expression[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala index c944a63f..85cb5002 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala @@ -3,7 +3,7 @@ package io.computenode.cyfra.dsl.gio import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} import io.computenode.cyfra.dsl.Value.FromExpr.fromExpr -import io.computenode.cyfra.dsl.buffer.{GBuffer, Read, Write} +import io.computenode.cyfra.dsl.binding.{GBuffer, ReadBuffer, WriteBuffer} import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.gio.GIO.* import izumi.reflect.Tag @@ -36,10 +36,10 @@ object GIO: Repeat(n, f) def write[T <: Value](buffer: GBuffer[T], index: Int32, value: T): GIO[Unit] = - Write(buffer, index, value) + WriteBuffer(buffer, index, value) def read[T <: Value: FromExpr: Tag](buffer: GBuffer[T], index: Int32): T = - fromExpr(Read(buffer, index)) + fromExpr(ReadBuffer(buffer, index)) def invocationId: Int32 = fromExpr(InvocationId) diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index e63fa80a..ef4d8d28 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -1,10 +1,11 @@ package io.computenode.cyfra.samples import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.binding.ParamUniform import io.computenode.cyfra.core.{GBufferRegion, GExecution, GProgram} import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.Value.{GBoolean, Int32} -import io.computenode.cyfra.dsl.buffer.GBuffer +import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform} import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} @@ -26,11 +27,10 @@ object TestingStuff: // VVVV (inSize = 4, emitN = 3) // 1 1 1 2 2 2 3 3 3 4 4 4 - val emitProgram = GProgram[EmitProgramParams, EmitProgramUniform, EmitProgramLayout]( + val emitProgram = GProgram[EmitProgramParams, EmitProgramLayout]( layout = params => EmitProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[Int32](params.inSize * params.emitN)), - uniform = params => EmitProgramUniform(params.emitN), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), - ): (layout, args) => + ): (layout) => val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) val bufferOffset = invocId * args.emitN @@ -40,18 +40,25 @@ object TestingStuff: // === Filter program === case class FilterProgramParams(inSize: Int, filterValue: Int) - case class FilterProgramLayout(in: GBuffer[Int32], out: GBuffer[GBoolean]) extends Layout - case class FilterProgramUniform(filterValue: Int32) extends GStruct[FilterProgramUniform] - val filterProgram = GProgram[FilterProgramParams, FilterProgramUniform, FilterProgramLayout]( - layout = params => FilterProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[GBoolean](params.inSize)), - uniform = params => FilterProgramUniform(params.filterValue), + case class FilterProgramLayout( + in: GBuffer[Int32], + out: GBuffer[GBoolean], + params: GUniform[FilterProgramUniform] + ) extends Layout + + val filterProgram = GProgram[FilterProgramParams, FilterProgramLayout]( + layout = params => FilterProgramLayout( + in = GBuffer[Int32](params.inSize), + out = GBuffer[GBoolean](params.inSize), + params = ParamUniform(FilterProgramUniform(params.filterValue)) + ), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), - ): (layout, args) => + ): layout => val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) - val isMatch = element === args.filterValue + val isMatch = element === layout.params.filterValue GIO.write(layout.out, invocId, isMatch) // === GExecution === @@ -73,7 +80,7 @@ object TestingStuff: mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue), ) .compile(layout => EmitFilterResult(layout.filterBuffer)) - + @main def test = From 79b53390e2defdf9259f8a422caf5a1826b3496f Mon Sep 17 00:00:00 2001 From: Szymon Date: Tue, 1 Jul 2025 00:42:35 +0200 Subject: [PATCH 06/16] another version --- .../computenode/cyfra/core/Allocation.scala | 24 +++++-- .../computenode/cyfra/core/CyfraRuntime.scala | 13 ++++ .../cyfra/core/GBufferRegion.scala | 33 +++------ .../computenode/cyfra/core/GExecution.scala | 33 ++++----- .../io/computenode/cyfra/core/GProgram.scala | 13 ++-- .../cyfra/core/binding/BufferRef.scala | 3 +- .../cyfra/core/binding/UniformRef.scala | 10 +++ .../cyfra/core/layout/LayoutStruct.scala | 55 +++++++++------ .../cyfra/dsl/binding/GBuffer.scala | 8 ++- .../cyfra/dsl/binding/GUniform.scala | 17 ++++- .../cyfra/dsl/binding/ReadUniform.scala | 5 +- .../cyfra/dsl/binding/WriteUniform.scala | 3 +- .../cyfra/samples/TestingStuff.scala | 67 ++++++++++++------- .../cyfra/runtime/ExecutionHandler.scala | 18 +++++ .../cyfra/runtime/VkAllocation.scala | 18 +++++ .../cyfra/runtime/VkCyfraRuntime.scala | 14 ++++ 16 files changed, 231 insertions(+), 103 deletions(-) create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala create mode 100644 cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala create mode 100644 cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala create mode 100644 cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala create mode 100644 cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index 67715efd..9145f884 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -1,14 +1,30 @@ package io.computenode.cyfra.core +import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.GBuffer +import izumi.reflect.Tag import java.nio.ByteBuffer trait Allocation: extension [R, T <: Value](buffer: GBuffer[T]) - def read(bb: ByteBuffer): Unit = - () + def read(bb: ByteBuffer): Unit + + def write(bb: ByteBuffer): Unit + + extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) + def execute(params: Params, layout: L): RL - def write(bb: ByteBuffer): Unit = - () +object Allocation: + + trait InitAlloc: + extension (buffers: GBuffer.type) + def apply[T <: Value : Tag : FromExpr](size: Int): GBuffer[T] + + def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GBuffer[T] + + trait FinalizeAlloc: + extension [T <: Value](buffer: GBuffer[T]) + def readTo(bb: ByteBuffer): Unit diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala new file mode 100644 index 00000000..a9e6e535 --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala @@ -0,0 +1,13 @@ +package io.computenode.cyfra.core + +import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} + +trait CyfraRuntime: + + def allocation(): Allocation + + def initAlloc(allocation: Allocation): InitAlloc + + def finalizeAlloc(allocation: Allocation): FinalizeAlloc + + \ No newline at end of file diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index ec6f3f7b..082aa9cb 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -1,9 +1,12 @@ package io.computenode.cyfra.core +import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} import io.computenode.cyfra.core.GProgram.BufferSizeSpec import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.GBuffer +import izumi.reflect.Tag import java.nio.ByteBuffer @@ -11,24 +14,7 @@ sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: val initAlloc: ReqAlloc object GBufferRegion: - - private[cyfra] case class ZeroedBuffer[T <: Value](size: Int) extends GBuffer[T] - - private[cyfra] case class BufferFromRam[T <: Value](buff: ByteBuffer) extends GBuffer[T] - - trait InitAlloc: - extension (buffers: GBuffer.type) - def apply[T <: Value](size: Int): GBuffer[T] = - ZeroedBuffer[T](size) - - def apply[T <: Value](buff: ByteBuffer): GBuffer[T] = - BufferFromRam[T](buff) - - trait FinalizeAlloc: - extension [T <: Value](buffer: GBuffer[T]) - def readTo(bb: ByteBuffer): Unit = - () - + def allocate[Alloc <: Layout: LayoutStruct]: GBufferRegion[Alloc, Alloc] = AllocRegion(summon[LayoutStruct[Alloc]].layoutRef) @@ -45,17 +31,18 @@ object GBufferRegion: def map[NewAlloc <: Layout: LayoutStruct](f: Allocation ?=> ResAlloc => NewAlloc): GBufferRegion[ReqAlloc, NewAlloc] = MapRegion(region, (alloc: Allocation) => (resAlloc: ResAlloc) => f(using alloc)(resAlloc)) - def runUnsafe(init: InitAlloc ?=> ReqAlloc, onDone: FinalizeAlloc ?=> ResAlloc => Unit): Unit = - val initAlloc = new InitAlloc {} + def runUnsafe(init: InitAlloc ?=> ReqAlloc, onDone: FinalizeAlloc ?=> ResAlloc => Unit)(using cyfraRuntime: CyfraRuntime): Unit = + val allocation = cyfraRuntime.allocation() + val initAlloc = cyfraRuntime.initAlloc(allocation) init(using initAlloc) - val alloc = new Allocation {} + val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): case _: AllocRegion[?] => None case MapRegion(req, f) => Some((f.asInstanceOf[Allocation => Layout => Layout], req)) val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) => - step(alloc)(acc) + step(allocation)(acc) - val finalizeAlloc = new FinalizeAlloc {} + val finalizeAlloc = cyfraRuntime.finalizeAlloc(allocation) onDone(using finalizeAlloc)(bodyAlloc.asInstanceOf[ResAlloc]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index abd38b35..498a022a 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -11,12 +11,9 @@ import izumi.reflect.Tag import GExecution.* trait GExecution[-Params, -L <: Layout, +RL <: Layout]: - def execute(layout: L, params: Params)(using Allocation): RL = - println("Executing GExecution...") - ??? def flatMap[NRL <: Layout, NP <: Params, NL <: L](f: RL => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] = - FlatMap(this, f) + FlatMap(this, (p, r) => f(r)) def mapResult[NRL <: Layout](f: RL => NRL): GExecution[Params, L, NRL] = Map(this, f, identity, identity) @@ -36,16 +33,26 @@ trait GExecution[-Params, -L <: Layout, +RL <: Layout]: object GExecution: - def forParams[Params, L <: Layout, LR <: Layout, NP](fn: Params => GExecution[NP, L, LR]): GExecution[Params & NP, L, LR] = - ??? - - private case class FlatMap[Params, L <: Layout, RL <: Layout, NRL <: Layout]( + def apply[Params, L <: Layout]() = + Pure[Params, L, L]() + + def forParams[Params, L <: Layout, RL <: Layout]( + f: Params => GExecution[Params, L, RL], + ): GExecution[Params, L, RL] = + FlatMap[Params, L, RL, RL]( + Pure[Params, L, RL](), + (params: Params, _: RL) => f(params), + ) + + case class Pure[Params, L <: Layout, RL <: Layout]() extends GExecution[Params, L, RL] + + case class FlatMap[Params, L <: Layout, RL <: Layout, NRL <: Layout]( execution: GExecution[Params, L, RL], - f: RL => GExecution[Params, L, NRL], + f: (Params, RL) => GExecution[Params, L, NRL], ) extends GExecution[Params, L, NRL] - private case class Map[P, NP, L <: Layout, NL <: Layout, RL <: Layout, NRL <: Layout]( + case class Map[P, NP, L <: Layout, NL <: Layout, RL <: Layout, NRL <: Layout]( execution: GExecution[P, L, RL], mapResult: RL => NRL, contramapLayout: NL => L, @@ -60,9 +67,3 @@ object GExecution: override def contramapLayout[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] = Map(execution, mapResult, f andThen contramapLayout, contramapParams) - - case class BoundProgram[LParams, Params, L <: Layout]( - layout: L, - paramsMapping: LParams => Params, - program: GProgram[Params, L], - ) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 6c327058..9fd8ab84 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -7,6 +7,7 @@ import io.computenode.cyfra.core.layout.Layout import java.nio.ByteBuffer import GProgram.* import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty @@ -30,22 +31,22 @@ object GProgram: case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch - private[cyfra] case class BufferSizeSpec[T <: Value](size: Int) extends GBuffer[T] + private[cyfra] case class BufferSizeSpec[T <: Value: Tag: FromExpr](size: Int) extends GBuffer[T] - private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag](value: T) extends GUniform[T] + private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag: FromExpr](value: T) extends GUniform[T] - private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag]() extends GUniform[T] + private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] trait InitProgramLayout: extension (buffers: GBuffer.type) - def apply[T <: Value](size: Int): GBuffer[T] = + def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = BufferSizeSpec[T](size) extension (uniforms: GUniform.type) - def apply[T <: GStruct[T]: Tag](value: T): GUniform[T] = + def apply[T <: GStruct[T]: Tag: FromExpr](value: T): GUniform[T] = ParamUniform[T](value) - def apply[T <: GStruct[T]: Tag](): GUniform[T] = + def apply[T <: GStruct[T]: Tag: FromExpr](): GUniform[T] = DynamicUniform[T]() def apply[Params, L <: Layout: LayoutStruct]( diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala index 758b6077..ff3e8c90 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala @@ -1,8 +1,9 @@ package io.computenode.cyfra.core.binding import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.GBuffer import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag -case class BufferRef[T <: Value](layoutOffset: Int, valueTag: Tag[T]) extends GBuffer[T] +case class BufferRef[T <: Value: Tag: FromExpr](layoutOffset: Int, valueTag: Tag[T]) extends GBuffer[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala new file mode 100644 index 00000000..3f6d67ee --- /dev/null +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala @@ -0,0 +1,10 @@ +package io.computenode.cyfra.core.binding + +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr +import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform} +import io.computenode.cyfra.dsl.struct.GStruct +import izumi.reflect.Tag +import izumi.reflect.macrortti.LightTypeTag + +case class UniformRef[T <: Value: Tag: FromExpr](layoutOffset: Int, valueTag: Tag[T]) extends GUniform[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala index b8e64ed8..38e703de 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -1,8 +1,9 @@ package io.computenode.cyfra.core.layout -import io.computenode.cyfra.core.binding.BufferRef +import io.computenode.cyfra.core.binding.{BufferRef, UniformRef} import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.binding.GBuffer +import io.computenode.cyfra.dsl.Value.FromExpr +import io.computenode.cyfra.dsl.binding.{GBinding, GBuffer, GUniform} import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag @@ -30,29 +31,41 @@ object LayoutStruct: case ValDef(_, tpt, _) => tpt.tpe case _ => report.errorAndAbort("Unexpected field type in case class") - if !fieldTypes.forall(_ <:< TypeRepr.of[GBuffer[?]]) then - report.errorAndAbort("LayoutStruct can only be derived for case classes with GBuffer elements") + if !fieldTypes.forall(_ <:< TypeRepr.of[GBinding]) then + report.errorAndAbort("LayoutStruct can only be derived for case classes with GBinding elements") - val valueTypes = fieldTypes.map(_.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type"))) + val valueTypes = fieldTypes.map: + ftype => (ftype, ftype.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type"))) // summon izumi tags - val tags = valueTypes.map: tpe => - tpe.asType match - case '[t] => - ( - tpe.asType, - Expr.summon[Tag[t]] match - case Some(tagExpr) => tagExpr - case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}"), - ) - - val buffers = tags.zipWithIndex.map: - case ((tpe, tag), i) => + val typeGivens = valueTypes.map: + case (ftype, farg) => + farg.asType match + case '[type t <: Value; t] => + ( + ftype.asType, + farg.asType, + Expr.summon[Tag[t]] match + case Some(tagExpr) => tagExpr + case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}"), + Expr.summon[FromExpr[t]] match + case Some(fromExpr) => fromExpr + case None => report.errorAndAbort(s"Cannot summon FromExpr for type ${tpe.show}") + ) + + val buffers = typeGivens.zipWithIndex.map: + case ((ftype, tpe, tag, fromExpr), i) => tpe match case '[type t <: Value; t] => - '{ - BufferRef[t](${ Expr(i) }, ${ tag.asExprOf[Tag[t]] }) - } + ftype match + case '[type tg <: GBuffer[?]; tg] => + '{ + BufferRef[t](${ Expr(i) }, ${ tag.asExprOf[Tag[t]] })(using summon[Tag[t]], ${ fromExpr.asExprOf[FromExpr[t]] }) + } + case '[type tg <: GUniform[?]; tg] => + '{ + UniformRef[t](${ Expr(i) }, ${ tag.asExprOf[Tag[t]] })(using summon[Tag[t]], ${ fromExpr.asExprOf[FromExpr[t]] }) + } val constructor = sym.primaryConstructor @@ -60,7 +73,7 @@ object LayoutStruct: val layoutRef = layoutInstance.asExprOf[T] - val soleTags = tags.map(_._2.asExprOf[Tag[? <: Value]]).toList + val soleTags = typeGivens.map(_._3.asExprOf[Tag[? <: Value]]).toList '{ LayoutStruct[T]($layoutRef, ${ Expr.ofList(soleTags) }) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala index d56d4b83..0f86b15a 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala @@ -1,7 +1,13 @@ package io.computenode.cyfra.dsl.binding import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} +import io.computenode.cyfra.dsl.gio.GIO +import izumi.reflect.Tag -trait GBuffer[T <: Value] extends GBinding +trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding: + def read(index: Int32): T = FromExpr.fromExpr(ReadBuffer(this, index)) + + def write(index: Int32, value: T): GIO[Unit] = GIO.write(this, index, value) object GBuffer \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala index 869b2831..772f3b62 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala @@ -1,8 +1,19 @@ package io.computenode.cyfra.dsl.binding +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr +import io.computenode.cyfra.dsl.Value.FromExpr.fromExpr +import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct +import izumi.reflect.Tag -trait GUniform[T <: GStruct[T]] extends GBinding - def read: ReadUniform[T] +trait GUniform[T <: Value: Tag: FromExpr] extends GBinding: + def read: T = fromExpr(ReadUniform(this)) + + def write(value: T): GIO[Unit] = WriteUniform(this, value) -object GUniform \ No newline at end of file +object GUniform: + + case class ParamUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] + + def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]() \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala index e6cac8fb..55cd3455 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala @@ -1,7 +1,6 @@ package io.computenode.cyfra.dsl.binding -import io.computenode.cyfra.dsl.Expression -import io.computenode.cyfra.dsl.struct.GStruct +import io.computenode.cyfra.dsl.{Expression, Value} import izumi.reflect.Tag -case class ReadUniform[T <: GStruct[T] : Tag](uniform: GUniform[T]) extends Expression[T] \ No newline at end of file +case class ReadUniform[T <: Value : Tag](uniform: GUniform[T]) extends Expression[T] \ No newline at end of file diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala index 3fd8dc33..5539c07e 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala @@ -1,9 +1,10 @@ package io.computenode.cyfra.dsl.binding +import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag -case class WriteUniform[T <: GStruct[T]: Tag](uniform: GUniform[T], value: T) extends GIO[Unit]: +case class WriteUniform[T <: Value: Tag](uniform: GUniform[T], value: T) extends GIO[Unit]: override def underlying: Unit = () diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index ef4d8d28..3cb181fe 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -1,7 +1,6 @@ package io.computenode.cyfra.samples import io.computenode.cyfra.core.aalegacy.GContext -import io.computenode.cyfra.core.binding.ParamUniform import io.computenode.cyfra.core.{GBufferRegion, GExecution, GProgram} import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.Value.{GBoolean, Int32} @@ -10,7 +9,7 @@ import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import org.lwjgl.BufferUtils - +import io.computenode.cyfra.runtime.VkCyfraRuntime object TestingStuff: given GContext = GContext() @@ -19,25 +18,30 @@ object TestingStuff: case class EmitProgramParams(inSize: Int, emitN: Int) - case class EmitProgramLayout(in: GBuffer[Int32], out: GBuffer[Int32]) extends Layout - case class EmitProgramUniform(emitN: Int32) extends GStruct[EmitProgramUniform] - // 1 2 3 4 - // VVVV (inSize = 4, emitN = 3) - // 1 1 1 2 2 2 3 3 3 4 4 4 + case class EmitProgramLayout( + in: GBuffer[Int32], + out: GBuffer[Int32], + args: GUniform[EmitProgramUniform] = GUniform.fromParams // todo will be different in the future + ) extends Layout val emitProgram = GProgram[EmitProgramParams, EmitProgramLayout]( - layout = params => EmitProgramLayout(in = GBuffer[Int32](params.inSize), out = GBuffer[Int32](params.inSize * params.emitN)), + layout = params => EmitProgramLayout( + in = GBuffer[Int32](params.inSize), + out = GBuffer[Int32](params.inSize * params.emitN), + args = GUniform(EmitProgramUniform(params.emitN))), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), ): (layout) => + val EmitProgramUniform(emitN) = layout.args.read val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) - val bufferOffset = invocId * args.emitN - GIO.repeat(args.emitN): i => + val bufferOffset = invocId * emitN + GIO.repeat(emitN): i => GIO.write(layout.out, bufferOffset + i, element) // === Filter program === + case class FilterProgramParams(inSize: Int, filterValue: Int) case class FilterProgramUniform(filterValue: Int32) extends GStruct[FilterProgramUniform] @@ -45,51 +49,66 @@ object TestingStuff: case class FilterProgramLayout( in: GBuffer[Int32], out: GBuffer[GBoolean], - params: GUniform[FilterProgramUniform] + params: GUniform[FilterProgramUniform] = GUniform.fromParams ) extends Layout val filterProgram = GProgram[FilterProgramParams, FilterProgramLayout]( layout = params => FilterProgramLayout( in = GBuffer[Int32](params.inSize), out = GBuffer[GBoolean](params.inSize), - params = ParamUniform(FilterProgramUniform(params.filterValue)) + params = GUniform(FilterProgramUniform(params.filterValue)) ), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), ): layout => val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) - val isMatch = element === layout.params.filterValue + val isMatch = element === layout.params.read.filterValue GIO.write(layout.out, invocId, isMatch) // === GExecution === case class EmitFilterParams(inSize: Int, emitN: Int, filterValue: Int) - case class EmitFilterLayout(inBuffer: GBuffer[Int32], emitBuffer: GBuffer[Int32], filterBuffer: GBuffer[GBoolean]) extends Layout + case class EmitFilterLayout( + inBuffer: GBuffer[Int32], + emitBuffer: GBuffer[Int32], + filterBuffer: GBuffer[GBoolean] + ) extends Layout case class EmitFilterResult(out: GBuffer[GBoolean]) extends Layout - val emitFilterExecution = GExecution - .build[EmitFilterParams, EmitFilterLayout, EmitFilterResult] - .addProgram(emitProgram)( // (inBuffer, emitBuffer) - mapLayout = layout => EmitProgramLayout(in = layout.inBuffer, out = layout.emitBuffer), - mapParams = params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN), + val emitFilterExecution = GExecution[EmitFilterParams, EmitFilterLayout]() + .addProgram(emitProgram)( + params => EmitProgramParams( + inSize = params.inSize, + emitN = params.emitN + ), + layout => EmitProgramLayout( + in = layout.inBuffer, + out = layout.emitBuffer + ) ) - .addProgram(filterProgram)( // (emitBuffer, filterBuffer) - mapLayout = layout => FilterProgramLayout(in = layout.emitBuffer, out = layout.filterBuffer), - mapParams = params => FilterProgramParams(inSize = params.inSize * params.emitN, filterValue = params.filterValue), + .addProgram(filterProgram)( + params => FilterProgramParams( + inSize = params.inSize, + filterValue = params.filterValue + ), + layout => FilterProgramLayout( + in = layout.emitBuffer, + out = layout.filterBuffer + ) ) - .compile(layout => EmitFilterResult(layout.filterBuffer)) @main def test = + given VkCyfraRuntime = VkCyfraRuntime() val emitFilterParams = EmitFilterParams(inSize = 1024, emitN = 2, filterValue = 42) val region = GBufferRegion .allocate[EmitFilterLayout] .map: region => - emitFilterExecution.execute(region, emitFilterParams) + emitFilterExecution.execute(emitFilterParams, region) val data = (0 to 1024).toArray val buffer = BufferUtils.createByteBuffer(data.length * 4) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala new file mode 100644 index 00000000..42d14d08 --- /dev/null +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala @@ -0,0 +1,18 @@ +package io.computenode.cyfra.runtime + +import io.computenode.cyfra.core.{GExecution, GProgram} +import io.computenode.cyfra.core.layout.Layout + +object ExecutionHandler: + + def handle[Params, L <: Layout, RL <: Layout]( + execution: GExecution[Params, L, RL], + params: Params + ): List[BoundProgram[?, ? ,?]] = + ??? + + case class BoundProgram[LParams, Params, L <: Layout]( + layout: L, + paramsMapping: LParams => Params, + program: GProgram[Params, L], + ) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala new file mode 100644 index 00000000..d24557cf --- /dev/null +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala @@ -0,0 +1,18 @@ +package io.computenode.cyfra.runtime + +import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} +import io.computenode.cyfra.core.{Allocation, GExecution} +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.binding.GBuffer + +import java.nio.ByteBuffer + +class VkAllocation extends Allocation: + extension [R, T <: Value](buffer: GBuffer[T]) + override def read(bb: ByteBuffer): Unit = () + + override def write(bb: ByteBuffer): Unit = () + + extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) + override def execute(params: Params, layout: L): RL = + ??? diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala new file mode 100644 index 00000000..8563d0cd --- /dev/null +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala @@ -0,0 +1,14 @@ +package io.computenode.cyfra.runtime + +import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} +import io.computenode.cyfra.core.{Allocation, CyfraRuntime} + +class VkCyfraRuntime extends CyfraRuntime: + override def allocation(): Allocation = + new VkAllocation() + + override def initAlloc(allocation: Allocation): InitAlloc = + ??? + + override def finalizeAlloc(allocation: Allocation): FinalizeAlloc = + ??? \ No newline at end of file From 6a5d41487aba3c7b0acbfdf01d6adc3606d06d36 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 13:42:46 +0200 Subject: [PATCH 07/16] More review fixes. --- .../computenode/cyfra/core/Allocation.scala | 31 ++++----- .../computenode/cyfra/core/CyfraRuntime.scala | 10 +-- .../cyfra/core/GBufferRegion.scala | 14 ++--- .../computenode/cyfra/core/GExecution.scala | 49 +++++++-------- .../io/computenode/cyfra/core/GProgram.scala | 24 ++++--- .../cyfra/core/layout/Layout.scala | 2 - .../cyfra/core/layout/LayoutStruct.scala | 6 +- .../cyfra/dsl/binding/GBuffer.scala | 4 +- .../cyfra/dsl/binding/GUniform.scala | 8 +-- .../cyfra/dsl/binding/ReadUniform.scala | 2 +- .../cyfra/dsl/binding/WriteUniform.scala | 1 - .../cyfra/samples/TestingStuff.scala | 63 +++++++------------ .../cyfra/runtime/ExecutionHandler.scala | 11 +--- .../cyfra/runtime/VkAllocation.scala | 18 ++++-- .../cyfra/runtime/VkCyfraRuntime.scala | 8 +-- 15 files changed, 107 insertions(+), 144 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index 9145f884..aaeb20a7 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -4,27 +4,22 @@ import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.GBuffer +import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag import java.nio.ByteBuffer trait Allocation: extension [R, T <: Value](buffer: GBuffer[T]) - def read(bb: ByteBuffer): Unit - - def write(bb: ByteBuffer): Unit - - extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) - def execute(params: Params, layout: L): RL - -object Allocation: - - trait InitAlloc: - extension (buffers: GBuffer.type) - def apply[T <: Value : Tag : FromExpr](size: Int): GBuffer[T] - - def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GBuffer[T] - - trait FinalizeAlloc: - extension [T <: Value](buffer: GBuffer[T]) - def readTo(bb: ByteBuffer): Unit + def read(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit + + def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit + + extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL + + extension (buffers: GBuffer.type) + def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] + + def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] + + extension (structs: GStruct.type) def apply[T <: GStruct[T]: Tag: FromExpr](buff: ByteBuffer): GStruct[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala index a9e6e535..a7c029e4 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/CyfraRuntime.scala @@ -1,13 +1,7 @@ package io.computenode.cyfra.core -import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} +import io.computenode.cyfra.core.Allocation trait CyfraRuntime: - + def allocation(): Allocation - - def initAlloc(allocation: Allocation): InitAlloc - - def finalizeAlloc(allocation: Allocation): FinalizeAlloc - - \ No newline at end of file diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index 082aa9cb..87a42989 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.core -import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} +import io.computenode.cyfra.core.Allocation import io.computenode.cyfra.core.GProgram.BufferSizeSpec import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value @@ -14,7 +14,7 @@ sealed trait GBufferRegion[ReqAlloc <: Layout: LayoutStruct, ResAlloc <: Layout: val initAlloc: ReqAlloc object GBufferRegion: - + def allocate[Alloc <: Layout: LayoutStruct]: GBufferRegion[Alloc, Alloc] = AllocRegion(summon[LayoutStruct[Alloc]].layoutRef) @@ -31,11 +31,10 @@ object GBufferRegion: def map[NewAlloc <: Layout: LayoutStruct](f: Allocation ?=> ResAlloc => NewAlloc): GBufferRegion[ReqAlloc, NewAlloc] = MapRegion(region, (alloc: Allocation) => (resAlloc: ResAlloc) => f(using alloc)(resAlloc)) - def runUnsafe(init: InitAlloc ?=> ReqAlloc, onDone: FinalizeAlloc ?=> ResAlloc => Unit)(using cyfraRuntime: CyfraRuntime): Unit = + def runUnsafe(init: Allocation ?=> ReqAlloc, onDone: Allocation ?=> ResAlloc => Unit)(using cyfraRuntime: CyfraRuntime): Unit = val allocation = cyfraRuntime.allocation() - val initAlloc = cyfraRuntime.initAlloc(allocation) - init(using initAlloc) - + init(using allocation) + val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): case _: AllocRegion[?] => None case MapRegion(req, f) => @@ -44,5 +43,4 @@ object GBufferRegion: val bodyAlloc = steps.foldLeft[Layout](region.initAlloc): (acc, step) => step(allocation)(acc) - val finalizeAlloc = cyfraRuntime.finalizeAlloc(allocation) - onDone(using finalizeAlloc)(bodyAlloc.asInstanceOf[ResAlloc]) + onDone(using allocation)(bodyAlloc.asInstanceOf[ResAlloc]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index 498a022a..4634e3e8 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -10,48 +10,41 @@ import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef import izumi.reflect.Tag import GExecution.* -trait GExecution[-Params, -L <: Layout, +RL <: Layout]: +trait GExecution[-Params, -ExecLayout <: Layout, +ResLayout <: Layout]: - def flatMap[NRL <: Layout, NP <: Params, NL <: L](f: RL => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] = + def flatMap[NRL <: Layout, NP <: Params, NL <: ExecLayout](f: ResLayout => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] = FlatMap(this, (p, r) => f(r)) - def mapResult[NRL <: Layout](f: RL => NRL): GExecution[Params, L, NRL] = + def map[NRL <: Layout](f: ResLayout => NRL): GExecution[Params, ExecLayout, NRL] = Map(this, f, identity, identity) - def contramapLayout[NL <: Layout](f: NL => L): GExecution[Params, NL, RL] = + def contramap[NL <: Layout](f: NL => ExecLayout): GExecution[Params, NL, ResLayout] = Map(this, identity, f, identity) - def contramapParams[NP](f: NP => Params): GExecution[NP, L, RL] = + def contramapParams[NP](f: NP => Params): GExecution[NP, ExecLayout, ResLayout] = Map(this, identity, identity, f) - def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: L, P <: GProgram[ - ProgramParams, - ProgramLayout, - ]](program: P)(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ProgramLayout] = - val adapted = program.contramapParams(mapParams).contramapLayout(mapLayout) - flatMap(_ => adapted) + def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: ExecLayout, P <: GProgram[ProgramParams, ProgramLayout]]( + program: P, + )(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ResLayout] = + val adapted = program.contramapParams(mapParams).contramap(mapLayout) + flatMap(r => adapted.map(_ => r)) object GExecution: def apply[Params, L <: Layout]() = Pure[Params, L, L]() - - def forParams[Params, L <: Layout, RL <: Layout]( - f: Params => GExecution[Params, L, RL], - ): GExecution[Params, L, RL] = - FlatMap[Params, L, RL, RL]( - Pure[Params, L, RL](), - (params: Params, _: RL) => f(params), - ) - + + def forParams[Params, L <: Layout, RL <: Layout](f: Params => GExecution[Params, L, RL]): GExecution[Params, L, RL] = + FlatMap[Params, L, RL, RL](Pure[Params, L, RL](), (params: Params, _: RL) => f(params)) + case class Pure[Params, L <: Layout, RL <: Layout]() extends GExecution[Params, L, RL] - + case class FlatMap[Params, L <: Layout, RL <: Layout, NRL <: Layout]( execution: GExecution[Params, L, RL], f: (Params, RL) => GExecution[Params, L, NRL], ) extends GExecution[Params, L, NRL] - - + case class Map[P, NP, L <: Layout, NL <: Layout, RL <: Layout, NRL <: Layout]( execution: GExecution[P, L, RL], mapResult: RL => NRL, @@ -59,11 +52,11 @@ object GExecution: contramapParams: NP => P, ) extends GExecution[NP, NL, NRL]: - override def mapResult[NNRL <: Layout](f: NRL => NNRL): GExecution[NP, NL, NNRL] = + override def map[NNRL <: Layout](f: NRL => NNRL): GExecution[NP, NL, NNRL] = Map(execution, mapResult andThen f, contramapLayout, contramapParams) - - override def contramapParams[NNP](f: NNP => NP): GExecution[NNP, NL, NRL] = + + override def contramapParams[NNP](f: NNP => NP): GExecution[NNP, NL, NRL] = Map(execution, mapResult, contramapLayout, f andThen contramapParams) - - override def contramapLayout[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] = + + override def contramap[NNL <: Layout](f: NNL => NL): GExecution[NP, NNL, NRL] = Map(execution, mapResult, f andThen contramapLayout, contramapParams) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 9fd8ab84..cdc5f447 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -6,9 +6,9 @@ import io.computenode.cyfra.core.layout.Layout import java.nio.ByteBuffer import GProgram.* -import io.computenode.cyfra.dsl.Value -import io.computenode.cyfra.dsl.Value.FromExpr -import io.computenode.cyfra.dsl.binding.{GBuffer, GUniform} +import io.computenode.cyfra.dsl.{Expression, Value} +import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} +import io.computenode.cyfra.dsl.binding.{GBinding, GBuffer, GUniform} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag @@ -27,14 +27,14 @@ object GProgram: sealed trait ProgramDispatch - case class DynamicDispatch[L <: Layout](buffer: GBuffer[?], offset: Int) extends ProgramDispatch + case class DynamicDispatch[L <: Layout](buffer: GBinding, offset: Int) extends ProgramDispatch case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch private[cyfra] case class BufferSizeSpec[T <: Value: Tag: FromExpr](size: Int) extends GBuffer[T] - + private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag: FromExpr](value: T) extends GUniform[T] - + private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] trait InitProgramLayout: @@ -45,13 +45,21 @@ object GProgram: extension (uniforms: GUniform.type) def apply[T <: GStruct[T]: Tag: FromExpr](value: T): GUniform[T] = ParamUniform[T](value) - + def apply[T <: GStruct[T]: Tag: FromExpr](): GUniform[T] = DynamicUniform[T]() - + def apply[Params, L <: Layout: LayoutStruct]( layout: InitProgramLayout ?=> Params => L, dispatch: (L, Params) => ProgramDispatch, workgroupSize: WorkDimensions = (128, 1, 1), )(body: L => GIO[?]): GProgram[Params, L] = new GProgram(body, s => layout(using s), dispatch, workgroupSize) + +case class Write(buffer: GBuffer[?], index: Int, value: Any) + +case class Read(buffer: GBuffer[?], index: Int) + +case class SimulationResult(invocs: List[InvocationSimResult]) + +case class InvocationSimResult(invocId: Int, instructions: List[Expression[_]], writes: List[Write], read: List[Read]) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala index 01f1d71f..d03d858f 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/Layout.scala @@ -4,5 +4,3 @@ import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.binding.GBuffer trait Layout - - \ No newline at end of file diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala index 38e703de..601ed3e3 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -34,8 +34,8 @@ object LayoutStruct: if !fieldTypes.forall(_ <:< TypeRepr.of[GBinding]) then report.errorAndAbort("LayoutStruct can only be derived for case classes with GBinding elements") - val valueTypes = fieldTypes.map: - ftype => (ftype, ftype.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type"))) + val valueTypes = fieldTypes.map: ftype => + (ftype, ftype.typeArgs.headOption.getOrElse(report.errorAndAbort("GBuffer must have a value type"))) // summon izumi tags val typeGivens = valueTypes.map: @@ -50,7 +50,7 @@ object LayoutStruct: case None => report.errorAndAbort(s"Cannot summon Tag for type ${tpe.show}"), Expr.summon[FromExpr[t]] match case Some(fromExpr) => fromExpr - case None => report.errorAndAbort(s"Cannot summon FromExpr for type ${tpe.show}") + case None => report.errorAndAbort(s"Cannot summon FromExpr for type ${tpe.show}"), ) val buffers = typeGivens.zipWithIndex.map: diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala index 0f86b15a..480f44c2 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala @@ -7,7 +7,7 @@ import izumi.reflect.Tag trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding: def read(index: Int32): T = FromExpr.fromExpr(ReadBuffer(this, index)) - + def write(index: Int32, value: T): GIO[Unit] = GIO.write(this, index, value) -object GBuffer \ No newline at end of file +object GBuffer diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala index 772f3b62..f64e4780 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala @@ -9,11 +9,11 @@ import izumi.reflect.Tag trait GUniform[T <: Value: Tag: FromExpr] extends GBinding: def read: T = fromExpr(ReadUniform(this)) - + def write(value: T): GIO[Unit] = WriteUniform(this, value) object GUniform: - + case class ParamUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] - - def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]() \ No newline at end of file + + def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]() diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala index 55cd3455..9f75a278 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/ReadUniform.scala @@ -3,4 +3,4 @@ package io.computenode.cyfra.dsl.binding import io.computenode.cyfra.dsl.{Expression, Value} import izumi.reflect.Tag -case class ReadUniform[T <: Value : Tag](uniform: GUniform[T]) extends Expression[T] \ No newline at end of file +case class ReadUniform[T <: Value: Tag](uniform: GUniform[T]) extends Expression[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala index 5539c07e..240aa643 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/WriteUniform.scala @@ -7,4 +7,3 @@ import izumi.reflect.Tag case class WriteUniform[T <: Value: Tag](uniform: GUniform[T], value: T) extends GIO[Unit]: override def underlying: Unit = () - diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index 3cb181fe..b235dd1e 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -23,16 +23,18 @@ object TestingStuff: case class EmitProgramLayout( in: GBuffer[Int32], out: GBuffer[Int32], - args: GUniform[EmitProgramUniform] = GUniform.fromParams // todo will be different in the future + args: GUniform[EmitProgramUniform] = GUniform.fromParams, // todo will be different in the future ) extends Layout val emitProgram = GProgram[EmitProgramParams, EmitProgramLayout]( - layout = params => EmitProgramLayout( - in = GBuffer[Int32](params.inSize), - out = GBuffer[Int32](params.inSize * params.emitN), - args = GUniform(EmitProgramUniform(params.emitN))), + layout = params => + EmitProgramLayout( + in = GBuffer[Int32](params.inSize), + out = GBuffer[Int32](params.inSize * params.emitN), + args = GUniform(EmitProgramUniform(params.emitN)), + ), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), - ): (layout) => + ): layout => val EmitProgramUniform(emitN) = layout.args.read val invocId = GIO.invocationId val element = GIO.read(layout.in, invocId) @@ -46,18 +48,16 @@ object TestingStuff: case class FilterProgramUniform(filterValue: Int32) extends GStruct[FilterProgramUniform] - case class FilterProgramLayout( - in: GBuffer[Int32], - out: GBuffer[GBoolean], - params: GUniform[FilterProgramUniform] = GUniform.fromParams - ) extends Layout + case class FilterProgramLayout(in: GBuffer[Int32], out: GBuffer[GBoolean], params: GUniform[FilterProgramUniform] = GUniform.fromParams) + extends Layout val filterProgram = GProgram[FilterProgramParams, FilterProgramLayout]( - layout = params => FilterProgramLayout( - in = GBuffer[Int32](params.inSize), - out = GBuffer[GBoolean](params.inSize), - params = GUniform(FilterProgramUniform(params.filterValue)) - ), + layout = params => + FilterProgramLayout( + in = GBuffer[Int32](params.inSize), + out = GBuffer[GBoolean](params.inSize), + params = GUniform(FilterProgramUniform(params.filterValue)), + ), dispatch = (layout, args) => GProgram.StaticDispatch((args.inSize, 1, 1)), ): layout => val invocId = GIO.invocationId @@ -69,36 +69,20 @@ object TestingStuff: case class EmitFilterParams(inSize: Int, emitN: Int, filterValue: Int) - case class EmitFilterLayout( - inBuffer: GBuffer[Int32], - emitBuffer: GBuffer[Int32], - filterBuffer: GBuffer[GBoolean] - ) extends Layout + case class EmitFilterLayout(inBuffer: GBuffer[Int32], emitBuffer: GBuffer[Int32], filterBuffer: GBuffer[GBoolean]) extends Layout case class EmitFilterResult(out: GBuffer[GBoolean]) extends Layout val emitFilterExecution = GExecution[EmitFilterParams, EmitFilterLayout]() .addProgram(emitProgram)( - params => EmitProgramParams( - inSize = params.inSize, - emitN = params.emitN - ), - layout => EmitProgramLayout( - in = layout.inBuffer, - out = layout.emitBuffer - ) + params => EmitProgramParams(inSize = params.inSize, emitN = params.emitN), + layout => EmitProgramLayout(in = layout.inBuffer, out = layout.emitBuffer), ) .addProgram(filterProgram)( - params => FilterProgramParams( - inSize = params.inSize, - filterValue = params.filterValue - ), - layout => FilterProgramLayout( - in = layout.emitBuffer, - out = layout.filterBuffer - ) + params => FilterProgramParams(inSize = params.inSize, filterValue = params.filterValue), + layout => FilterProgramLayout(in = layout.emitBuffer, out = layout.filterBuffer), ) - + @main def test = given VkCyfraRuntime = VkCyfraRuntime() @@ -113,7 +97,6 @@ object TestingStuff: val data = (0 to 1024).toArray val buffer = BufferUtils.createByteBuffer(data.length * 4) buffer.asIntBuffer().put(data).flip() - val result = BufferUtils.createByteBuffer(data.length * 2) region.runUnsafe( @@ -122,5 +105,5 @@ object TestingStuff: emitBuffer = GBuffer[Int32](data.length * 2), filterBuffer = GBuffer[GBoolean](data.length * 2), ), - onDone = layout => layout.out.readTo(result), + onDone = layout => layout.filterBuffer.read(result), ) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala index 42d14d08..df0b470e 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/ExecutionHandler.scala @@ -5,14 +5,7 @@ import io.computenode.cyfra.core.layout.Layout object ExecutionHandler: - def handle[Params, L <: Layout, RL <: Layout]( - execution: GExecution[Params, L, RL], - params: Params - ): List[BoundProgram[?, ? ,?]] = + def handle[Params, L <: Layout, RL <: Layout](execution: GExecution[Params, L, RL], params: Params): List[BoundProgram[?, ?, ?]] = ??? - case class BoundProgram[LParams, Params, L <: Layout]( - layout: L, - paramsMapping: LParams => Params, - program: GProgram[Params, L], - ) + case class BoundProgram[LParams, Params, L <: Layout](layout: L, paramsMapping: LParams => Params, program: GProgram[Params, L]) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala index d24557cf..21f5e6a5 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala @@ -3,16 +3,24 @@ package io.computenode.cyfra.runtime import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.core.{Allocation, GExecution} import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.binding.GBuffer +import io.computenode.cyfra.dsl.struct.GStruct +import izumi.reflect.Tag import java.nio.ByteBuffer class VkAllocation extends Allocation: extension [R, T <: Value](buffer: GBuffer[T]) - override def read(bb: ByteBuffer): Unit = () + def read(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit = ??? - override def write(bb: ByteBuffer): Unit = () + def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit = ??? - extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) - override def execute(params: Params, layout: L): RL = - ??? + extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? + + extension (buffers: GBuffer.type) + def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = ??? + + def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] = ??? + + extension (structs: GStruct.type) def apply[T <: GStruct[T]: Tag: FromExpr](buff: ByteBuffer): GStruct[T] = ??? diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala index 8563d0cd..87dbd5ab 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkCyfraRuntime.scala @@ -1,14 +1,8 @@ package io.computenode.cyfra.runtime -import io.computenode.cyfra.core.Allocation.{FinalizeAlloc, InitAlloc} +import io.computenode.cyfra.core.Allocation import io.computenode.cyfra.core.{Allocation, CyfraRuntime} class VkCyfraRuntime extends CyfraRuntime: override def allocation(): Allocation = new VkAllocation() - - override def initAlloc(allocation: Allocation): InitAlloc = - ??? - - override def finalizeAlloc(allocation: Allocation): FinalizeAlloc = - ??? \ No newline at end of file From 9d3b21e0bebaa391dee50d16115f1bb0d8a15f56 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 13:43:57 +0200 Subject: [PATCH 08/16] Rename package --- .../main/scala/io/computenode/cyfra/core/GExecution.scala | 2 +- .../cyfra/core/{aalegacy => archive}/Executable.scala | 4 ++-- .../cyfra/core/{aalegacy => archive}/GContext.scala | 8 ++++---- .../cyfra/core/{aalegacy => archive}/GFunction.scala | 4 ++-- .../cyfra/core/{aalegacy => archive}/UniformContext.scala | 4 ++-- .../cyfra/core/{aalegacy => archive}/mem/FloatMem.scala | 2 +- .../cyfra/core/{aalegacy => archive}/mem/GMem.scala | 4 ++-- .../cyfra/core/{aalegacy => archive}/mem/IntMem.scala | 2 +- .../cyfra/core/{aalegacy => archive}/mem/RamGMem.scala | 2 +- .../core/{aalegacy => archive}/mem/Vec4FloatMem.scala | 4 ++-- .../io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala | 2 +- .../scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala | 2 +- .../scala/io/computenode/cyfra/e2e/GStructE2eTest.scala | 2 +- .../test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala | 2 +- .../test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala | 2 +- .../io/computenode/cyfra/e2e/juliaset/JuliaSet.scala | 4 ++-- .../scala/io/computenode/cyfra/samples/TestingStuff.scala | 2 +- .../computenode/cyfra/samples/oldsamples/Raytracing.scala | 4 ++-- .../io/computenode/cyfra/samples/slides/1sample.scala | 4 ++-- .../io/computenode/cyfra/samples/slides/2simpleray.scala | 4 ++-- .../scala/io/computenode/cyfra/samples/slides/3rays.scala | 4 ++-- .../io/computenode/cyfra/samples/slides/4random.scala | 4 ++-- .../cyfra/foton/animation/AnimatedFunctionRenderer.scala | 6 +++--- .../cyfra/foton/animation/AnimationRenderer.scala | 4 ++-- .../io/computenode/cyfra/foton/rt/ImageRtRenderer.scala | 6 +++--- .../scala/io/computenode/cyfra/foton/rt/RtRenderer.scala | 2 +- .../cyfra/foton/rt/animation/AnimationRtRenderer.scala | 6 +++--- 27 files changed, 48 insertions(+), 48 deletions(-) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/Executable.scala (63%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/GContext.scala (93%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/GFunction.scala (93%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/UniformContext.scala (81%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/mem/FloatMem.scala (93%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/mem/GMem.scala (93%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/mem/IntMem.scala (93%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/mem/RamGMem.scala (81%) rename cyfra-core/src/main/scala/io/computenode/cyfra/core/{aalegacy => archive}/mem/Vec4FloatMem.scala (90%) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index 4634e3e8..3acffc47 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -1,7 +1,7 @@ package io.computenode.cyfra.core import io.computenode.cyfra.core.GExecution.* -import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.archive.GContext import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.binding.GBuffer import io.computenode.cyfra.dsl.gio.GIO diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/Executable.scala similarity index 63% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/Executable.scala index 0b876c39..68e0b273 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/Executable.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/Executable.scala @@ -1,6 +1,6 @@ -package io.computenode.cyfra.core.aalegacy +package io.computenode.cyfra.core.archive -import io.computenode.cyfra.core.aalegacy.mem.{GMem, RamGMem} +import io.computenode.cyfra.core.archive.mem.{GMem, RamGMem} import io.computenode.cyfra.dsl.Value import scala.concurrent.Future diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala similarity index 93% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala index d06382dd..df19e3f0 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala @@ -1,8 +1,8 @@ -package io.computenode.cyfra.core.aalegacy +package io.computenode.cyfra.core.archive -import io.computenode.cyfra.core.aalegacy.mem.GMem.totalStride -import io.computenode.cyfra.core.aalegacy.mem.{FloatMem, GMem, IntMem, Vec4FloatMem} -import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} +import io.computenode.cyfra.core.archive.mem.GMem.totalStride +import io.computenode.cyfra.core.archive.mem.{FloatMem, GMem, IntMem, Vec4FloatMem} +import io.computenode.cyfra.core.archive.{GFunction, UniformContext} import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.{Float32, FromExpr, Int32, Vec4} import io.computenode.cyfra.dsl.collections.GArray diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala similarity index 93% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala index d7be2ac6..47759b90 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/GFunction.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala @@ -1,6 +1,6 @@ -package io.computenode.cyfra.core.aalegacy +package io.computenode.cyfra.core.archive -import io.computenode.cyfra.core.aalegacy.GFunction +import io.computenode.cyfra.core.archive.GFunction import io.computenode.cyfra.dsl.Value.* import io.computenode.cyfra.dsl.collections.{GArray, GArray2D} import io.computenode.cyfra.dsl.struct.* diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala similarity index 81% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala index aa326fdc..f35ac5be 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/UniformContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala @@ -1,6 +1,6 @@ -package io.computenode.cyfra.core.aalegacy +package io.computenode.cyfra.core.archive -import io.computenode.cyfra.core.aalegacy.UniformContext +import io.computenode.cyfra.core.archive.UniformContext import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/FloatMem.scala similarity index 93% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/FloatMem.scala index 4d7d3c59..2e51f2ee 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/FloatMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/FloatMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.core.aalegacy.mem +package io.computenode.cyfra.core.archive.mem import io.computenode.cyfra.dsl.Value.Float32 import org.lwjgl.BufferUtils diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala similarity index 93% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala index d268e504..8badfe3c 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/GMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala @@ -1,6 +1,6 @@ -package io.computenode.cyfra.core.aalegacy.mem +package io.computenode.cyfra.core.archive.mem -import io.computenode.cyfra.core.aalegacy.{GContext, GFunction, UniformContext} +import io.computenode.cyfra.core.archive.{GContext, GFunction, UniformContext} import io.computenode.cyfra.dsl.Value.FromExpr import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/IntMem.scala similarity index 93% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/IntMem.scala index 4143dd2e..ee9e61e8 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/IntMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/IntMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.core.aalegacy.mem +package io.computenode.cyfra.core.archive.mem import io.computenode.cyfra.dsl.Value.Int32 import org.lwjgl.BufferUtils diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/RamGMem.scala similarity index 81% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/RamGMem.scala index 1c10bab8..a136d7d4 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/RamGMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/RamGMem.scala @@ -1,4 +1,4 @@ -package io.computenode.cyfra.core.aalegacy.mem +package io.computenode.cyfra.core.archive.mem import io.computenode.cyfra.dsl.Value diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/Vec4FloatMem.scala similarity index 90% rename from cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala rename to cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/Vec4FloatMem.scala index 553103da..0fdb63cb 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/aalegacy/mem/Vec4FloatMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/Vec4FloatMem.scala @@ -1,6 +1,6 @@ -package io.computenode.cyfra.core.aalegacy.mem +package io.computenode.cyfra.core.archive.mem -import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA +import io.computenode.cyfra.core.archive.mem.GMem.fRGBA import io.computenode.cyfra.dsl.Value.{Float32, Vec4} import org.lwjgl.BufferUtils diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala index ae9f1cfe..17797a77 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/ArithmeticsE2eTest.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.e2e -import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.archive.* import mem.* import GMem.fRGBA import io.computenode.cyfra.dsl.algebra.VectorAlgebra diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala index 34bae9f0..31966edf 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/FunctionsE2eTest.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.e2e -import io.computenode.cyfra.core.aalegacy.*, mem.* +import io.computenode.cyfra.core.archive.*, mem.* import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import GMem.fRGBA diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala index 0f2ff751..c1183c2a 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GStructE2eTest.scala @@ -2,7 +2,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.archive.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala index b100e239..d10cca51 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/GseqE2eTest.scala @@ -2,7 +2,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.archive.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala index 0355ac2d..0a374c26 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/WhenE2eTest.scala @@ -1,7 +1,7 @@ package io.computenode.cyfra.e2e import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.core.aalegacy.* +import io.computenode.cyfra.core.archive.* import mem.* import io.computenode.cyfra.dsl.{*, given} diff --git a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala index ab5c158c..df9b0191 100644 --- a/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala +++ b/cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/juliaset/JuliaSet.scala @@ -6,8 +6,8 @@ import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.control.Pure.pure import io.computenode.cyfra.dsl.struct.GStruct.Empty import io.computenode.cyfra.e2e.ImageTests -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem -import io.computenode.cyfra.core.aalegacy.{GContext, GFunction} +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.{GContext, GFunction} import io.computenode.cyfra.spirvtools.* import io.computenode.cyfra.spirvtools.SpirvTool.{Param, ToFile} import io.computenode.cyfra.utility.ImageUtility diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala index b235dd1e..d94c895b 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/TestingStuff.scala @@ -1,6 +1,6 @@ package io.computenode.cyfra.samples -import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.archive.GContext import io.computenode.cyfra.core.{GBufferRegion, GExecution, GProgram} import io.computenode.cyfra.core.layout.* import io.computenode.cyfra.dsl.Value.{GBoolean, Int32} diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala index 667a7fe0..ab35810e 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/oldsamples/Raytracing.scala @@ -3,8 +3,8 @@ package io.computenode.cyfra.samples.oldsamples import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct -import io.computenode.cyfra.core.aalegacy.* -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.* +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala index c42c9ea0..16121da4 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/1sample.scala @@ -1,8 +1,8 @@ package io.computenode.cyfra.samples.slides import io.computenode.cyfra.dsl.{*, given} -import io.computenode.cyfra.core.aalegacy.* -import io.computenode.cyfra.core.aalegacy.mem.FloatMem +import io.computenode.cyfra.core.archive.* +import io.computenode.cyfra.core.archive.mem.FloatMem given GContext = new GContext() diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala index 963759ea..145fe5e6 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/2simpleray.scala @@ -3,8 +3,8 @@ package io.computenode.cyfra.samples.slides import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.core.aalegacy.* -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.* +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala index a8415aa4..49ff0d46 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/3rays.scala @@ -5,8 +5,8 @@ import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.core.aalegacy.* -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.* +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala index 66dcee6c..bac16f3b 100644 --- a/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala +++ b/cyfra-examples/src/main/scala/io/computenode/cyfra/samples/slides/4random.scala @@ -4,8 +4,8 @@ import io.computenode.cyfra.dsl.collections.GSeq import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty -import io.computenode.cyfra.core.aalegacy.* -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.* +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem import io.computenode.cyfra.utility.ImageUtility import java.nio.file.Paths diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala index 8041d1ba..1e18cd28 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimatedFunctionRenderer.scala @@ -6,9 +6,9 @@ import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.animation.AnimatedFunctionRenderer.{AnimationIteration, RenderFn} import io.computenode.cyfra.foton.animation.AnimationFunctions.AnimationInstant -import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem -import io.computenode.cyfra.core.aalegacy.{GContext, GFunction, UniformContext} +import io.computenode.cyfra.core.archive.mem.GMem.fRGBA +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.{GContext, GFunction, UniformContext} import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala index f955fce9..9a262e95 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/animation/AnimationRenderer.scala @@ -3,8 +3,8 @@ package io.computenode.cyfra.foton.animation import io.computenode.cyfra import io.computenode.cyfra.dsl.Value.* import io.computenode.cyfra.dsl.{*, given} -import io.computenode.cyfra.core.aalegacy.GFunction -import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA +import io.computenode.cyfra.core.archive.GFunction +import io.computenode.cyfra.core.archive.mem.GMem.fRGBA import io.computenode.cyfra.utility.ImageUtility import io.computenode.cyfra.utility.Units.Milliseconds import io.computenode.cyfra.utility.Utility.timed diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala index 48f1589e..8f4d2b70 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/ImageRtRenderer.scala @@ -6,9 +6,9 @@ import io.computenode.cyfra.dsl.Value.* import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.rt.ImageRtRenderer.RaytracingIteration -import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem -import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} +import io.computenode.cyfra.core.archive.mem.GMem.fRGBA +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.{GFunction, UniformContext} import io.computenode.cyfra.utility.ImageUtility import io.computenode.cyfra.utility.Utility.timed diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala index 6bea8f3b..b59765b1 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/RtRenderer.scala @@ -10,7 +10,7 @@ import io.computenode.cyfra.dsl.library.Random import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.rt.RtRenderer.RayHitInfo -import io.computenode.cyfra.core.aalegacy.GContext +import io.computenode.cyfra.core.archive.GContext import scala.concurrent.ExecutionContext import scala.concurrent.ExecutionContext.Implicits diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala index ba212de3..c339bec9 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/animation/AnimationRtRenderer.scala @@ -7,9 +7,9 @@ import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.foton.animation.AnimationRenderer import io.computenode.cyfra.foton.rt.RtRenderer import io.computenode.cyfra.foton.rt.animation.AnimationRtRenderer.RaytracingIteration -import io.computenode.cyfra.core.aalegacy.mem.GMem.fRGBA -import io.computenode.cyfra.core.aalegacy.mem.Vec4FloatMem -import io.computenode.cyfra.core.aalegacy.{GFunction, UniformContext} +import io.computenode.cyfra.core.archive.mem.GMem.fRGBA +import io.computenode.cyfra.core.archive.mem.Vec4FloatMem +import io.computenode.cyfra.core.archive.{GFunction, UniformContext} class AnimationRtRenderer(params: AnimationRtRenderer.Parameters) extends RtRenderer(params) From 393f397fbd3093a546744563de8a61f1b2e59ad4 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 14:13:47 +0200 Subject: [PATCH 09/16] Make ExecLayout invariant --- .../main/scala/io/computenode/cyfra/core/GExecution.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala index 3acffc47..0caabec6 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GExecution.scala @@ -10,9 +10,9 @@ import io.computenode.cyfra.spirv.compilers.ExpressionCompiler.UniformStructRef import izumi.reflect.Tag import GExecution.* -trait GExecution[-Params, -ExecLayout <: Layout, +ResLayout <: Layout]: +trait GExecution[-Params, ExecLayout <: Layout, +ResLayout <: Layout]: - def flatMap[NRL <: Layout, NP <: Params, NL <: ExecLayout](f: ResLayout => GExecution[NP, NL, NRL]): GExecution[NP, NL, NRL] = + def flatMap[NRL <: Layout, NP <: Params](f: ResLayout => GExecution[NP, ExecLayout, NRL]): GExecution[NP, ExecLayout, NRL] = FlatMap(this, (p, r) => f(r)) def map[NRL <: Layout](f: ResLayout => NRL): GExecution[Params, ExecLayout, NRL] = @@ -24,9 +24,9 @@ trait GExecution[-Params, -ExecLayout <: Layout, +ResLayout <: Layout]: def contramapParams[NP](f: NP => Params): GExecution[NP, ExecLayout, ResLayout] = Map(this, identity, identity, f) - def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, PL <: ExecLayout, P <: GProgram[ProgramParams, ProgramLayout]]( + def addProgram[ProgramParams, PP <: Params, ProgramLayout <: Layout, P <: GProgram[ProgramParams, ProgramLayout]]( program: P, - )(mapParams: PP => ProgramParams, mapLayout: PL => ProgramLayout): GExecution[PP, PL, ResLayout] = + )(mapParams: PP => ProgramParams, mapLayout: ExecLayout => ProgramLayout): GExecution[PP, ExecLayout, ResLayout] = val adapted = program.contramapParams(mapParams).contramap(mapLayout) flatMap(r => adapted.map(_ => r)) From 953de8682ff25a3d44414a9677ecbde8a34376b3 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 14:21:37 +0200 Subject: [PATCH 10/16] allocation fixes --- .../scala/io/computenode/cyfra/core/Allocation.scala | 11 ++++++++--- .../scala/io/computenode/cyfra/core/GProgram.scala | 7 ------- .../io/computenode/cyfra/dsl/binding/GBinding.scala | 6 +++++- .../io/computenode/cyfra/dsl/binding/GBuffer.scala | 2 +- .../io/computenode/cyfra/dsl/binding/GUniform.scala | 2 +- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index aaeb20a7..2c51fd22 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -3,14 +3,14 @@ package io.computenode.cyfra.core import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.FromExpr -import io.computenode.cyfra.dsl.binding.GBuffer +import io.computenode.cyfra.dsl.binding.{GBinding, GBuffer, GUniform} import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag import java.nio.ByteBuffer trait Allocation: - extension [R, T <: Value](buffer: GBuffer[T]) + extension (buffer: GBinding[?]) def read(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit @@ -22,4 +22,9 @@ trait Allocation: def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] - extension (structs: GStruct.type) def apply[T <: GStruct[T]: Tag: FromExpr](buff: ByteBuffer): GStruct[T] + extension (buffers: GUniform.type) + def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GUniform[T] + + def apply[T <: Value : Tag : FromExpr](): GUniform[T] + + diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index cdc5f447..1b54f36b 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -56,10 +56,3 @@ object GProgram: )(body: L => GIO[?]): GProgram[Params, L] = new GProgram(body, s => layout(using s), dispatch, workgroupSize) -case class Write(buffer: GBuffer[?], index: Int, value: Any) - -case class Read(buffer: GBuffer[?], index: Int) - -case class SimulationResult(invocs: List[InvocationSimResult]) - -case class InvocationSimResult(invocId: Int, instructions: List[Expression[_]], writes: List[Write], read: List[Read]) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala index 256413cf..fb96b3ca 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala @@ -1,3 +1,7 @@ package io.computenode.cyfra.dsl.binding -trait GBinding +import io.computenode.cyfra.dsl.Value +import io.computenode.cyfra.dsl.Value.FromExpr +import izumi.reflect.Tag + +trait GBinding[T <: Value: Tag: FromExpr] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala index 480f44c2..5a93699f 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala @@ -5,7 +5,7 @@ import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} import io.computenode.cyfra.dsl.gio.GIO import izumi.reflect.Tag -trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding: +trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding[T]: def read(index: Int32): T = FromExpr.fromExpr(ReadBuffer(this, index)) def write(index: Int32, value: T): GIO[Unit] = GIO.write(this, index, value) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala index f64e4780..fbe81580 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala @@ -7,7 +7,7 @@ import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag -trait GUniform[T <: Value: Tag: FromExpr] extends GBinding: +trait GUniform[T <: Value: Tag: FromExpr] extends GBinding[T]: def read: T = fromExpr(ReadUniform(this)) def write(value: T): GIO[Unit] = WriteUniform(this, value) From 9857cfa683a7bd7d33fea7dff9345aeb64de0274 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 14:23:20 +0200 Subject: [PATCH 11/16] more fixes --- .../io/computenode/cyfra/core/GProgram.scala | 2 +- .../cyfra/core/layout/LayoutStruct.scala | 2 +- .../computenode/cyfra/runtime/VkAllocation.scala | 16 ++++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 1b54f36b..7a79ded3 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -27,7 +27,7 @@ object GProgram: sealed trait ProgramDispatch - case class DynamicDispatch[L <: Layout](buffer: GBinding, offset: Int) extends ProgramDispatch + case class DynamicDispatch[L <: Layout](buffer: GBinding[?], offset: Int) extends ProgramDispatch case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala index 601ed3e3..3d79b409 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/layout/LayoutStruct.scala @@ -31,7 +31,7 @@ object LayoutStruct: case ValDef(_, tpt, _) => tpt.tpe case _ => report.errorAndAbort("Unexpected field type in case class") - if !fieldTypes.forall(_ <:< TypeRepr.of[GBinding]) then + if !fieldTypes.forall(_ <:< TypeRepr.of[GBinding[?]]) then report.errorAndAbort("LayoutStruct can only be derived for case classes with GBinding elements") val valueTypes = fieldTypes.map: ftype => diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala index 21f5e6a5..bc851e03 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala @@ -4,23 +4,27 @@ import io.computenode.cyfra.core.layout.{Layout, LayoutStruct} import io.computenode.cyfra.core.{Allocation, GExecution} import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.FromExpr -import io.computenode.cyfra.dsl.binding.GBuffer +import io.computenode.cyfra.dsl.binding.{GBinding, GBuffer, GUniform} import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag import java.nio.ByteBuffer class VkAllocation extends Allocation: - extension [R, T <: Value](buffer: GBuffer[T]) + extension (buffer: GBinding[?]) def read(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit = ??? def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit = ??? - extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? + extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? extension (buffers: GBuffer.type) - def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = ??? + def apply[T <: Value : Tag : FromExpr](size: Int): GBuffer[T] = ??? - def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] = ??? + def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GBuffer[T] = ??? + + extension (buffers: GUniform.type) + def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GUniform[T] = ??? + + def apply[T <: Value : Tag : FromExpr](): GUniform[T] = ??? - extension (structs: GStruct.type) def apply[T <: GStruct[T]: Tag: FromExpr](buff: ByteBuffer): GStruct[T] = ??? From 55727304cba609598904b579a99142104b301874 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 14:30:44 +0200 Subject: [PATCH 12/16] changed program --- .../io/computenode/cyfra/core/GProgram.scala | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 7a79ded3..85dbb3fd 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -13,16 +13,29 @@ import io.computenode.cyfra.dsl.struct.GStruct import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag -class GProgram[Params, L <: Layout: LayoutStruct] private ( - val body: L => GIO[?], - val layout: InitProgramLayout => Params => L, - val dispatch: (L, Params) => ProgramDispatch, - val workgroupSize: WorkDimensions, -) extends GExecution[Params, L, L]: - private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] +sealed trait GProgram[Params, L <: Layout: LayoutStruct] extends GExecution[Params, L, L]: + val layout: InitProgramLayout => Params => L + val dispatch: (L, Params) => ProgramDispatch + val workgroupSize: WorkDimensions object GProgram: + class GioProgram[Params, L <: Layout : LayoutStruct]( + val body: L => GIO[?], + val layout: InitProgramLayout => Params => L, + val dispatch: (L, Params) => ProgramDispatch, + val workgroupSize: WorkDimensions, + ) extends GProgram[Params, L]: + private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] + + class SpirvProgram[Params, L <: Layout : LayoutStruct]( + val code: ByteBuffer, + val layout: InitProgramLayout => Params => L, + val dispatch: (L, Params) => ProgramDispatch, + val workgroupSize: WorkDimensions, + ) extends GProgram[Params, L]: + private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] + type WorkDimensions = (Int, Int, Int) sealed trait ProgramDispatch @@ -54,5 +67,5 @@ object GProgram: dispatch: (L, Params) => ProgramDispatch, workgroupSize: WorkDimensions = (128, 1, 1), )(body: L => GIO[?]): GProgram[Params, L] = - new GProgram(body, s => layout(using s), dispatch, workgroupSize) + new GioProgram[Params, L](body, s => layout(using s), dispatch, workgroupSize) From cf8aec812b1a0a1d9dd4ee51a6194c3d805db5d9 Mon Sep 17 00:00:00 2001 From: MarconZet <25779550+MarconZet@users.noreply.github.com> Date: Sun, 6 Jul 2025 14:39:57 +0200 Subject: [PATCH 13/16] ignore redunctant cast --- .../src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index 87a42989..0e44f61c 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -35,6 +35,7 @@ object GBufferRegion: val allocation = cyfraRuntime.allocation() init(using allocation) + //noinspection ScalaRedundantCast val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): case _: AllocRegion[?] => None case MapRegion(req, f) => From 49bc9abf22aec8c725cbcb915a40fa20e4d963b6 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 14:42:56 +0200 Subject: [PATCH 14/16] fmt --- .../scala/io/computenode/cyfra/core/Allocation.scala | 6 ++---- .../io/computenode/cyfra/core/GBufferRegion.scala | 2 +- .../scala/io/computenode/cyfra/core/GProgram.scala | 5 ++--- .../io/computenode/cyfra/runtime/VkAllocation.scala | 11 +++++------ 4 files changed, 10 insertions(+), 14 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index 2c51fd22..956c92a6 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -23,8 +23,6 @@ trait Allocation: def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] extension (buffers: GUniform.type) - def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GUniform[T] - - def apply[T <: Value : Tag : FromExpr](): GUniform[T] - + def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GUniform[T] + def apply[T <: Value: Tag: FromExpr](): GUniform[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala index 0e44f61c..2aec9159 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GBufferRegion.scala @@ -35,7 +35,7 @@ object GBufferRegion: val allocation = cyfraRuntime.allocation() init(using allocation) - //noinspection ScalaRedundantCast + // noinspection ScalaRedundantCast val steps: Seq[Allocation => Layout => Layout] = Seq.unfold(region: GBufferRegion[?, ?]): case _: AllocRegion[?] => None case MapRegion(req, f) => diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index 85dbb3fd..b684f5f2 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -20,7 +20,7 @@ sealed trait GProgram[Params, L <: Layout: LayoutStruct] extends GExecution[Para object GProgram: - class GioProgram[Params, L <: Layout : LayoutStruct]( + class GioProgram[Params, L <: Layout: LayoutStruct]( val body: L => GIO[?], val layout: InitProgramLayout => Params => L, val dispatch: (L, Params) => ProgramDispatch, @@ -28,7 +28,7 @@ object GProgram: ) extends GProgram[Params, L]: private[cyfra] def layoutStruct: LayoutStruct[L] = summon[LayoutStruct[L]] - class SpirvProgram[Params, L <: Layout : LayoutStruct]( + class SpirvProgram[Params, L <: Layout: LayoutStruct]( val code: ByteBuffer, val layout: InitProgramLayout => Params => L, val dispatch: (L, Params) => ProgramDispatch, @@ -68,4 +68,3 @@ object GProgram: workgroupSize: WorkDimensions = (128, 1, 1), )(body: L => GIO[?]): GProgram[Params, L] = new GioProgram[Params, L](body, s => layout(using s), dispatch, workgroupSize) - diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala index bc851e03..6d068d9b 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala @@ -16,15 +16,14 @@ class VkAllocation extends Allocation: def write(bb: ByteBuffer, offset: Int = 0, length: Int = -1): Unit = ??? - extension [Params, L <: Layout, RL <: Layout : LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? + extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? extension (buffers: GBuffer.type) - def apply[T <: Value : Tag : FromExpr](size: Int): GBuffer[T] = ??? + def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = ??? - def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GBuffer[T] = ??? + def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] = ??? extension (buffers: GUniform.type) - def apply[T <: Value : Tag : FromExpr](buff: ByteBuffer): GUniform[T] = ??? - - def apply[T <: Value : Tag : FromExpr](): GUniform[T] = ??? + def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GUniform[T] = ??? + def apply[T <: Value: Tag: FromExpr](): GUniform[T] = ??? From 89f17619cbc13c15181f9898ddf37f669e85be93 Mon Sep 17 00:00:00 2001 From: MarconZet <25779550+MarconZet@users.noreply.github.com> Date: Sun, 6 Jul 2025 14:45:35 +0200 Subject: [PATCH 15/16] fixed sth --- .../computenode/cyfra/core/Allocation.scala | 8 ++--- .../io/computenode/cyfra/core/GProgram.scala | 12 +++---- .../cyfra/core/archive/GContext.scala | 6 ++-- .../cyfra/core/archive/GFunction.scala | 6 ++-- .../cyfra/core/archive/UniformContext.scala | 4 +-- .../cyfra/core/archive/mem/GMem.scala | 2 +- .../cyfra/core/binding/BufferRef.scala | 2 +- .../cyfra/core/binding/UniformRef.scala | 2 +- .../cyfra/dsl/algebra/ScalarAlgebra.scala | 36 +++++++++---------- .../cyfra/dsl/algebra/VectorAlgebra.scala | 34 +++++++++--------- .../cyfra/dsl/binding/GBinding.scala | 2 +- .../cyfra/dsl/binding/GBuffer.scala | 2 +- .../cyfra/dsl/binding/GUniform.scala | 6 ++-- .../cyfra/dsl/collections/GArray.scala | 2 +- .../cyfra/dsl/collections/GArray2D.scala | 2 +- .../cyfra/dsl/collections/GSeq.scala | 14 ++++---- .../computenode/cyfra/dsl/control/Pure.scala | 2 +- .../computenode/cyfra/dsl/control/When.scala | 4 +-- .../io/computenode/cyfra/dsl/gio/GIO.scala | 2 +- .../cyfra/dsl/library/Functions.scala | 26 +++++++------- .../cyfra/dsl/struct/GStruct.scala | 2 +- .../foton/rt/shapes/ShapeCollection.scala | 2 +- .../cyfra/runtime/VkAllocation.scala | 8 ++--- 23 files changed, 93 insertions(+), 93 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala index 956c92a6..194b3a2f 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/Allocation.scala @@ -18,11 +18,11 @@ trait Allocation: extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL extension (buffers: GBuffer.type) - def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] + def apply[T <: Value: {Tag, FromExpr}](size: Int): GBuffer[T] - def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] + def apply[T <: Value: {Tag, FromExpr}](buff: ByteBuffer): GBuffer[T] extension (buffers: GUniform.type) - def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GUniform[T] + def apply[T <: Value: {Tag, FromExpr}](buff: ByteBuffer): GUniform[T] - def apply[T <: Value: Tag: FromExpr](): GUniform[T] + def apply[T <: Value: {Tag, FromExpr}](): GUniform[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala index b684f5f2..77586f5e 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/GProgram.scala @@ -44,22 +44,22 @@ object GProgram: case class StaticDispatch(size: WorkDimensions) extends ProgramDispatch - private[cyfra] case class BufferSizeSpec[T <: Value: Tag: FromExpr](size: Int) extends GBuffer[T] + private[cyfra] case class BufferSizeSpec[T <: Value: {Tag, FromExpr}](size: Int) extends GBuffer[T] - private[cyfra] case class ParamUniform[T <: GStruct[T]: Tag: FromExpr](value: T) extends GUniform[T] + private[cyfra] case class ParamUniform[T <: GStruct[T]: {Tag, FromExpr}](value: T) extends GUniform[T] - private[cyfra] case class DynamicUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] + private[cyfra] case class DynamicUniform[T <: GStruct[T]: {Tag, FromExpr}]() extends GUniform[T] trait InitProgramLayout: extension (buffers: GBuffer.type) - def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = + def apply[T <: Value: {Tag, FromExpr}](size: Int): GBuffer[T] = BufferSizeSpec[T](size) extension (uniforms: GUniform.type) - def apply[T <: GStruct[T]: Tag: FromExpr](value: T): GUniform[T] = + def apply[T <: GStruct[T]: {Tag, FromExpr}](value: T): GUniform[T] = ParamUniform[T](value) - def apply[T <: GStruct[T]: Tag: FromExpr](): GUniform[T] = + def apply[T <: GStruct[T]: {Tag, FromExpr}](): GUniform[T] = DynamicUniform[T]() def apply[Params, L <: Layout: LayoutStruct]( diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala index df19e3f0..d2246b8f 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala @@ -28,7 +28,7 @@ class GContext(spirvToolsRunner: SpirvToolsRunner = SpirvToolsRunner()): implicit val ec: ExecutionContextExecutor = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(16)) - def compile[G <: GStruct[G]: Tag: GStructSchema, H <: Value: Tag: FromExpr, R <: Value: Tag: FromExpr]( + def compile[G <: GStruct[G]: {Tag, GStructSchema}, H <: Value: {Tag, FromExpr}, R <: Value: {Tag, FromExpr}]( function: GFunction[G, H, R], ): ComputePipeline = val uniformStructSchema = summon[GStructSchema[G]] @@ -46,8 +46,8 @@ class GContext(spirvToolsRunner: SpirvToolsRunner = SpirvToolsRunner()): val shader = Shader(optimizedShaderCode, org.joml.Vector3i(256, 1, 1), layoutInfo, "main", vkContext.device) ComputePipeline(shader, vkContext) - def execute[G <: GStruct[G]: Tag: GStructSchema, H <: Value, R <: Value](mem: GMem[H], fn: GFunction[G, H, R])(using - uniformContext: UniformContext[G], + def execute[G <: GStruct[G]: {Tag, GStructSchema}, H <: Value, R <: Value](mem: GMem[H], fn: GFunction[G, H, R])(using + uniformContext: UniformContext[G], ): GMem[R] = val isUniformEmpty = uniformContext.uniform.schema.fields.isEmpty val actions = Map(LayoutLocation(0, 0) -> BufferAction.LoadTo, LayoutLocation(0, 1) -> BufferAction.LoadFrom) ++ diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala index 47759b90..c633f909 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GFunction.scala @@ -8,7 +8,7 @@ import io.computenode.cyfra.dsl.{*, given} import io.computenode.cyfra.vulkan.compute.ComputePipeline import izumi.reflect.Tag -case class GFunction[G <: GStruct[G]: GStructSchema: Tag, H <: Value: Tag: FromExpr, R <: Value: Tag: FromExpr](fn: (G, Int32, GArray[H]) => R)( +case class GFunction[G <: GStruct[G]: {GStructSchema, Tag}, H <: Value: {Tag, FromExpr}, R <: Value: {Tag, FromExpr}](fn: (G, Int32, GArray[H]) => R)( implicit context: GContext, ): def arrayInputs: List[Tag[?]] = List(summon[Tag[H]]) @@ -16,10 +16,10 @@ case class GFunction[G <: GStruct[G]: GStructSchema: Tag, H <: Value: Tag: FromE val pipeline: ComputePipeline = context.compile(this) object GFunction: - def apply[H <: Value: Tag: FromExpr, R <: Value: Tag: FromExpr](fn: H => R)(using context: GContext): GFunction[GStruct.Empty, H, R] = + def apply[H <: Value: {Tag, FromExpr}, R <: Value: {Tag, FromExpr}](fn: H => R)(using context: GContext): GFunction[GStruct.Empty, H, R] = new GFunction[GStruct.Empty, H, R]((_, index: Int32, gArray: GArray[H]) => fn(gArray.at(index))) - def from2D[G <: GStruct[G]: GStructSchema: Tag, H <: Value: Tag: FromExpr, R <: Value: Tag: FromExpr]( + def from2D[G <: GStruct[G]: {GStructSchema, Tag}, H <: Value: {Tag, FromExpr}, R <: Value: {Tag, FromExpr}]( width: Int, )(fn: (G, (Int32, Int32), GArray2D[H]) => R)(using context: GContext): GFunction[G, H, R] = GFunction[G, H, R]((g: G, index: Int32, a: GArray[H]) => diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala index f35ac5be..093698ae 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/UniformContext.scala @@ -5,9 +5,9 @@ import io.computenode.cyfra.dsl.struct.* import io.computenode.cyfra.dsl.struct.GStruct.Empty import izumi.reflect.Tag -class UniformContext[G <: GStruct[G]: Tag: GStructSchema](val uniform: G) +class UniformContext[G <: GStruct[G]: {Tag, GStructSchema}](val uniform: G) object UniformContext: - def withUniform[G <: GStruct[G]: Tag: GStructSchema, T](uniform: G)(fn: UniformContext[G] ?=> T): T = + def withUniform[G <: GStruct[G]: {Tag, GStructSchema}, T](uniform: G)(fn: UniformContext[G] ?=> T): T = fn(using UniformContext(uniform)) given empty: UniformContext[Empty] = new UniformContext(Empty()) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala index 8badfe3c..41961aa4 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/mem/GMem.scala @@ -13,7 +13,7 @@ import java.nio.ByteBuffer trait GMem[H <: Value]: def size: Int def toReadOnlyBuffer: ByteBuffer - def map[G <: GStruct[G]: Tag: GStructSchema, R <: Value: FromExpr: Tag]( + def map[G <: GStruct[G]: {Tag, GStructSchema}, R <: Value: {FromExpr, Tag}]( fn: GFunction[G, H, R], )(using context: GContext, uc: UniformContext[G]): GMem[R] = context.execute(this, fn) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala index ff3e8c90..1ad1c3af 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/BufferRef.scala @@ -6,4 +6,4 @@ import io.computenode.cyfra.dsl.binding.GBuffer import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag -case class BufferRef[T <: Value: Tag: FromExpr](layoutOffset: Int, valueTag: Tag[T]) extends GBuffer[T] +case class BufferRef[T <: Value: {Tag, FromExpr}](layoutOffset: Int, valueTag: Tag[T]) extends GBuffer[T] diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala index 3f6d67ee..d7c3b308 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/binding/UniformRef.scala @@ -7,4 +7,4 @@ import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag import izumi.reflect.macrortti.LightTypeTag -case class UniformRef[T <: Value: Tag: FromExpr](layoutOffset: Int, valueTag: Tag[T]) extends GUniform[T] +case class UniformRef[T <: Value: {Tag, FromExpr}](layoutOffset: Int, valueTag: Tag[T]) extends GUniform[T] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/ScalarAlgebra.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/ScalarAlgebra.scala index 4684c61d..92cbe6ae 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/ScalarAlgebra.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/ScalarAlgebra.scala @@ -11,7 +11,7 @@ import scala.annotation.targetName object ScalarAlgebra: - trait BasicScalarAlgebra[T <: Scalar: FromExpr: Tag] + trait BasicScalarAlgebra[T <: Scalar: {FromExpr, Tag}] extends ScalarSummable[T] with ScalarDiffable[T] with ScalarMulable[T] @@ -20,54 +20,54 @@ object ScalarAlgebra: with Comparable[T] with ScalarNegatable[T] - trait BasicScalarIntAlgebra[T <: Scalar: FromExpr: Tag] extends BasicScalarAlgebra[T] with BitwiseOperable[T] + trait BasicScalarIntAlgebra[T <: Scalar: {FromExpr, Tag}] extends BasicScalarAlgebra[T] with BitwiseOperable[T] given BasicScalarAlgebra[Float32] = new BasicScalarAlgebra[Float32] {} given BasicScalarIntAlgebra[Int32] = new BasicScalarIntAlgebra[Int32] {} given BasicScalarIntAlgebra[UInt32] = new BasicScalarIntAlgebra[UInt32] {} - trait ScalarSummable[T <: Scalar: FromExpr: Tag]: + trait ScalarSummable[T <: Scalar: {FromExpr, Tag}]: def sum(a: T, b: T)(using name: Source): T = summon[FromExpr[T]].fromExpr(Sum(a, b)) - extension [T <: Scalar: ScalarSummable: Tag](a: T) + extension [T <: Scalar: {ScalarSummable, Tag}](a: T) @targetName("add") inline def +(b: T)(using Source): T = summon[ScalarSummable[T]].sum(a, b) - trait ScalarDiffable[T <: Scalar: FromExpr: Tag]: + trait ScalarDiffable[T <: Scalar: {FromExpr, Tag}]: def diff(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(Diff(a, b)) - extension [T <: Scalar: ScalarDiffable: Tag](a: T) + extension [T <: Scalar: {ScalarDiffable, Tag}](a: T) @targetName("sub") inline def -(b: T)(using Source): T = summon[ScalarDiffable[T]].diff(a, b) // T and S ??? so two - trait ScalarMulable[T <: Scalar: FromExpr: Tag]: + trait ScalarMulable[T <: Scalar: {FromExpr, Tag}]: def mul(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(Mul(a, b)) - extension [T <: Scalar: ScalarMulable: Tag](a: T) + extension [T <: Scalar: {ScalarMulable, Tag}](a: T) @targetName("mul") inline def *(b: T)(using Source): T = summon[ScalarMulable[T]].mul(a, b) - trait ScalarDivable[T <: Scalar: FromExpr: Tag]: + trait ScalarDivable[T <: Scalar: {FromExpr, Tag}]: def div(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(Div(a, b)) - extension [T <: Scalar: ScalarDivable: Tag](a: T) + extension [T <: Scalar: {ScalarDivable, Tag}](a: T) @targetName("div") inline def /(b: T)(using Source): T = summon[ScalarDivable[T]].div(a, b) - trait ScalarNegatable[T <: Scalar: FromExpr: Tag]: + trait ScalarNegatable[T <: Scalar: {FromExpr, Tag}]: def negate(a: T)(using Source): T = summon[FromExpr[T]].fromExpr(Negate(a)) - extension [T <: Scalar: ScalarNegatable: Tag](a: T) + extension [T <: Scalar: {ScalarNegatable, Tag}](a: T) @targetName("negate") inline def unary_-(using Source): T = summon[ScalarNegatable[T]].negate(a) - trait ScalarModable[T <: Scalar: FromExpr: Tag]: + trait ScalarModable[T <: Scalar: {FromExpr, Tag}]: def mod(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(Mod(a, b)) - extension [T <: Scalar: ScalarModable: Tag](a: T) inline infix def mod(b: T)(using Source): T = summon[ScalarModable[T]].mod(a, b) + extension [T <: Scalar: {ScalarModable, Tag}](a: T) inline infix def mod(b: T)(using Source): T = summon[ScalarModable[T]].mod(a, b) - trait Comparable[T <: Scalar: FromExpr: Tag]: + trait Comparable[T <: Scalar: {FromExpr, Tag}]: def greaterThan(a: T, b: T)(using Source): GBoolean = GBoolean(GreaterThan(a, b)) def lessThan(a: T, b: T)(using Source): GBoolean = GBoolean(LessThan(a, b)) @@ -78,7 +78,7 @@ object ScalarAlgebra: def equal(a: T, b: T)(using Source): GBoolean = GBoolean(Equal(a, b)) - extension [T <: Scalar: Comparable: Tag](a: T) + extension [T <: Scalar: {Comparable, Tag}](a: T) inline def >(b: T)(using Source): GBoolean = summon[Comparable[T]].greaterThan(a, b) inline def <(b: T)(using Source): GBoolean = summon[Comparable[T]].lessThan(a, b) inline def >=(b: T)(using Source): GBoolean = summon[Comparable[T]].greaterThanEqual(a, b) @@ -102,7 +102,7 @@ object ScalarAlgebra: inline def asFloat(using Source): Float32 = Float32(ToFloat32(u32)) inline def signed(using Source): Int32 = Int32(ToInt32(u32)) - trait BitwiseOperable[T <: Scalar: FromExpr: Tag]: + trait BitwiseOperable[T <: Scalar: {FromExpr, Tag}]: def bitwiseAnd(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(BitwiseAnd(a, b)) def bitwiseOr(a: T, b: T)(using Source): T = summon[FromExpr[T]].fromExpr(BitwiseOr(a, b)) @@ -115,7 +115,7 @@ object ScalarAlgebra: def shiftRight(a: T, by: UInt32)(using Source): T = summon[FromExpr[T]].fromExpr(ShiftRight(a, by)) - extension [T <: Scalar: BitwiseOperable: Tag](a: T) + extension [T <: Scalar: {BitwiseOperable, Tag}](a: T) inline def &(b: T)(using Source): T = summon[BitwiseOperable[T]].bitwiseAnd(a, b) inline def |(b: T)(using Source): T = summon[BitwiseOperable[T]].bitwiseOr(a, b) inline def ^(b: T)(using Source): T = summon[BitwiseOperable[T]].bitwiseXor(a, b) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/VectorAlgebra.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/VectorAlgebra.scala index f307f9b5..1f82a539 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/VectorAlgebra.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/algebra/VectorAlgebra.scala @@ -11,7 +11,7 @@ import scala.annotation.targetName object VectorAlgebra: - trait BasicVectorAlgebra[S <: Scalar, V <: Vec[S]: FromExpr: Tag] + trait BasicVectorAlgebra[S <: Scalar, V <: Vec[S]: {FromExpr, Tag}] extends VectorSummable[V] with VectorDiffable[V] with VectorDotable[S, V] @@ -19,36 +19,36 @@ object VectorAlgebra: with VectorScalarMulable[S, V] with VectorNegatable[V] - given [T <: Scalar: FromExpr: Tag]: BasicVectorAlgebra[T, Vec2[T]] = new BasicVectorAlgebra[T, Vec2[T]] {} - given [T <: Scalar: FromExpr: Tag]: BasicVectorAlgebra[T, Vec3[T]] = new BasicVectorAlgebra[T, Vec3[T]] {} - given [T <: Scalar: FromExpr: Tag]: BasicVectorAlgebra[T, Vec4[T]] = new BasicVectorAlgebra[T, Vec4[T]] {} + given [T <: Scalar: {FromExpr, Tag}]: BasicVectorAlgebra[T, Vec2[T]] = new BasicVectorAlgebra[T, Vec2[T]] {} + given [T <: Scalar: {FromExpr, Tag}]: BasicVectorAlgebra[T, Vec3[T]] = new BasicVectorAlgebra[T, Vec3[T]] {} + given [T <: Scalar: {FromExpr, Tag}]: BasicVectorAlgebra[T, Vec4[T]] = new BasicVectorAlgebra[T, Vec4[T]] {} - trait VectorSummable[V <: Vec[?]: FromExpr: Tag]: + trait VectorSummable[V <: Vec[?]: {FromExpr, Tag}]: def sum(a: V, b: V)(using Source): V = summon[FromExpr[V]].fromExpr(Sum(a, b)) - extension [V <: Vec[?]: VectorSummable: Tag](a: V) + extension [V <: Vec[?]: {VectorSummable, Tag}](a: V) @targetName("addVector") inline def +(b: V)(using Source): V = summon[VectorSummable[V]].sum(a, b) - trait VectorDiffable[V <: Vec[?]: FromExpr: Tag]: + trait VectorDiffable[V <: Vec[?]: {FromExpr, Tag}]: def diff(a: V, b: V)(using Source): V = summon[FromExpr[V]].fromExpr(Diff(a, b)) - extension [V <: Vec[?]: VectorDiffable: Tag](a: V) + extension [V <: Vec[?]: {VectorDiffable, Tag}](a: V) @targetName("subVector") inline def -(b: V)(using Source): V = summon[VectorDiffable[V]].diff(a, b) - trait VectorDotable[S <: Scalar: FromExpr: Tag, V <: Vec[S]: Tag]: + trait VectorDotable[S <: Scalar: {FromExpr, Tag}, V <: Vec[S]: Tag]: def dot(a: V, b: V)(using Source): S = summon[FromExpr[S]].fromExpr(DotProd[S, V](a, b)) extension [S <: Scalar: Tag, V <: Vec[S]: Tag](a: V)(using VectorDotable[S, V]) infix def dot(b: V)(using Source): S = summon[VectorDotable[S, V]].dot(a, b) - trait VectorCrossable[V <: Vec[?]: FromExpr: Tag]: + trait VectorCrossable[V <: Vec[?]: {FromExpr, Tag}]: def cross(a: V, b: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Cross, List(a, b))) - extension [V <: Vec[?]: VectorCrossable: Tag](a: V) infix def cross(b: V)(using Source): V = summon[VectorCrossable[V]].cross(a, b) + extension [V <: Vec[?]: {VectorCrossable, Tag}](a: V) infix def cross(b: V)(using Source): V = summon[VectorCrossable[V]].cross(a, b) - trait VectorScalarMulable[S <: Scalar: Tag, V <: Vec[S]: FromExpr: Tag]: + trait VectorScalarMulable[S <: Scalar: Tag, V <: Vec[S]: {FromExpr, Tag}]: def mul(a: V, b: S)(using Source): V = summon[FromExpr[V]].fromExpr(ScalarProd[S, V](a, b)) extension [S <: Scalar: Tag, V <: Vec[S]: Tag](a: V)(using VectorScalarMulable[S, V]) @@ -56,10 +56,10 @@ object VectorAlgebra: extension [S <: Scalar: Tag, V <: Vec[S]: Tag](s: S)(using VectorScalarMulable[S, V]) def *(v: V)(using Source): V = summon[VectorScalarMulable[S, V]].mul(v, s) - trait VectorNegatable[V <: Vec[?]: FromExpr: Tag]: + trait VectorNegatable[V <: Vec[?]: {FromExpr, Tag}]: def negate(a: V)(using Source): V = summon[FromExpr[V]].fromExpr(Negate(a)) - extension [V <: Vec[?]: VectorNegatable: Tag](a: V) + extension [V <: Vec[?]: {VectorNegatable, Tag}](a: V) @targetName("negateVector") def unary_-(using Source): V = summon[VectorNegatable[V]].negate(a) @@ -92,11 +92,11 @@ object VectorAlgebra: inline def vclamp(v: Vec3[Float32], min: Float32, max: Float32)(using Source): Vec3[Float32] = (clamp(v.x, min, max), clamp(v.y, min, max), clamp(v.z, min, max)) - extension [T <: Scalar: FromExpr: Tag](v2: Vec2[T]) + extension [T <: Scalar: {FromExpr, Tag}](v2: Vec2[T]) inline def x(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v2, Int32(ConstInt32(0)))) inline def y(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v2, Int32(ConstInt32(1)))) - extension [T <: Scalar: FromExpr: Tag](v3: Vec3[T]) + extension [T <: Scalar: {FromExpr, Tag}](v3: Vec3[T]) inline def x(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v3, Int32(ConstInt32(0)))) inline def y(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v3, Int32(ConstInt32(1)))) inline def z(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v3, Int32(ConstInt32(2)))) @@ -104,7 +104,7 @@ object VectorAlgebra: inline def g(using Source): T = y inline def b(using Source): T = z - extension [T <: Scalar: FromExpr: Tag](v4: Vec4[T]) + extension [T <: Scalar: {FromExpr, Tag}](v4: Vec4[T]) inline def x(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v4, Int32(ConstInt32(0)))) inline def y(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v4, Int32(ConstInt32(1)))) inline def z(using Source): T = summon[FromExpr[T]].fromExpr(ExtractScalar(v4, Int32(ConstInt32(2)))) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala index fb96b3ca..d9006d69 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBinding.scala @@ -4,4 +4,4 @@ import io.computenode.cyfra.dsl.Value import io.computenode.cyfra.dsl.Value.FromExpr import izumi.reflect.Tag -trait GBinding[T <: Value: Tag: FromExpr] +trait GBinding[T <: Value: {Tag, FromExpr}] diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala index 5a93699f..b9f849cf 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GBuffer.scala @@ -5,7 +5,7 @@ import io.computenode.cyfra.dsl.Value.{FromExpr, Int32} import io.computenode.cyfra.dsl.gio.GIO import izumi.reflect.Tag -trait GBuffer[T <: Value: FromExpr: Tag] extends GBinding[T]: +trait GBuffer[T <: Value: {FromExpr, Tag}] extends GBinding[T]: def read(index: Int32): T = FromExpr.fromExpr(ReadBuffer(this, index)) def write(index: Int32, value: T): GIO[Unit] = GIO.write(this, index, value) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala index fbe81580..9e753e77 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/binding/GUniform.scala @@ -7,13 +7,13 @@ import io.computenode.cyfra.dsl.gio.GIO import io.computenode.cyfra.dsl.struct.GStruct import izumi.reflect.Tag -trait GUniform[T <: Value: Tag: FromExpr] extends GBinding[T]: +trait GUniform[T <: Value: {Tag, FromExpr}] extends GBinding[T]: def read: T = fromExpr(ReadUniform(this)) def write(value: T): GIO[Unit] = WriteUniform(this, value) object GUniform: - case class ParamUniform[T <: GStruct[T]: Tag: FromExpr]() extends GUniform[T] + case class ParamUniform[T <: GStruct[T]: {Tag, FromExpr}]() extends GUniform[T] - def fromParams[T <: GStruct[T]: Tag: FromExpr] = ParamUniform[T]() + def fromParams[T <: GStruct[T]: {Tag, FromExpr}] = ParamUniform[T]() diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray.scala index f91383a0..6e9daf13 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray.scala @@ -6,7 +6,7 @@ import io.computenode.cyfra.dsl.macros.Source import io.computenode.cyfra.dsl.{Expression, Value} import izumi.reflect.Tag -case class GArray[T <: Value: Tag: FromExpr](index: Int): +case class GArray[T <: Value: {Tag, FromExpr}](index: Int): def at(i: Int32)(using Source): T = summon[FromExpr[T]].fromExpr(GArrayElem(index, i.tree)) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray2D.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray2D.scala index 090797bf..70d6df19 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray2D.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GArray2D.scala @@ -7,6 +7,6 @@ import io.computenode.cyfra.dsl.macros.Source import izumi.reflect.Tag import io.computenode.cyfra.dsl.Value.FromExpr -class GArray2D[T <: Value: Tag: FromExpr](width: Int, val arr: GArray[T]): +class GArray2D[T <: Value: {Tag, FromExpr}](width: Int, val arr: GArray[T]): def at(x: Int32, y: Int32)(using Source): T = arr.at(y * width + x) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GSeq.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GSeq.scala index 103c5050..b4265a1b 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GSeq.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/collections/GSeq.scala @@ -10,7 +10,7 @@ import io.computenode.cyfra.dsl.macros.Source import io.computenode.cyfra.dsl.{Expression, Value} import izumi.reflect.Tag -class GSeq[T <: Value: Tag: FromExpr]( +class GSeq[T <: Value: {Tag, FromExpr}]( val uninitSource: Expression[?] => GSeqStream[?], val elemOps: List[GSeq.ElemOp[?]], val limit: Option[Int], @@ -19,7 +19,7 @@ class GSeq[T <: Value: Tag: FromExpr]( val aggregateElemExprTreeId: Int = treeidState.getAndIncrement(), ): - def copyWithDynamicTrees[R <: Value: Tag: FromExpr]( + def copyWithDynamicTrees[R <: Value: {Tag, FromExpr}]( elemOps: List[GSeq.ElemOp[?]] = elemOps, limit: Option[Int] = limit, currentElemExprTreeId: Int = currentElemExprTreeId, @@ -29,9 +29,9 @@ class GSeq[T <: Value: Tag: FromExpr]( private val currentElemExpr = CurrentElem[T](currentElemExprTreeId) val source = uninitSource(currentElemExpr) private def currentElem: T = summon[FromExpr[T]].fromExpr(currentElemExpr) - private def aggregateElem[R <: Value: Tag: FromExpr]: R = summon[FromExpr[R]].fromExpr(AggregateElem[R](aggregateElemExprTreeId)) + private def aggregateElem[R <: Value: {Tag, FromExpr}]: R = summon[FromExpr[R]].fromExpr(AggregateElem[R](aggregateElemExprTreeId)) - def map[R <: Value: Tag: FromExpr](fn: T => R): GSeq[R] = + def map[R <: Value: {Tag, FromExpr}](fn: T => R): GSeq[R] = this.copyWithDynamicTrees[R](elemOps = elemOps :+ GSeq.MapOp[T, R](fn(currentElem).tree)) def filter(fn: T => GBoolean): GSeq[T] = @@ -43,7 +43,7 @@ class GSeq[T <: Value: Tag: FromExpr]( def limit(n: Int): GSeq[T] = this.copyWithDynamicTrees(limit = Some(n)) - def fold[R <: Value: Tag: FromExpr](zero: R, fn: (R, T) => R): R = + def fold[R <: Value: {Tag, FromExpr}](zero: R, fn: (R, T) => R): R = summon[FromExpr[R]].fromExpr(GSeq.FoldSeq(zero, fn(aggregateElem, currentElem).tree, this)) def count: Int32 = @@ -54,11 +54,11 @@ class GSeq[T <: Value: Tag: FromExpr]( object GSeq: - def gen[T <: Value: Tag: FromExpr](first: T, next: T => T)(using name: Source) = + def gen[T <: Value: {Tag, FromExpr}](first: T, next: T => T)(using name: Source) = GSeq(ce => GSeqStream(first, next(summon[FromExpr[T]].fromExpr(ce.asInstanceOf[E[T]])).tree), Nil, None, name) // REALLY naive implementation, should be replaced with dynamic array (O(1)) access - def of[T <: Value: Tag: FromExpr](xs: List[T]) = + def of[T <: Value: {Tag, FromExpr}](xs: List[T]) = GSeq .gen[Int32](0, _ + 1) .map: i => diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/Pure.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/Pure.scala index 6f0bd5ff..2e517641 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/Pure.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/Pure.scala @@ -7,6 +7,6 @@ import io.computenode.cyfra.dsl.{Expression, Value} import izumi.reflect.Tag object Pure: - def pure[V <: Value: FromExpr: Tag](f: => V)(using fnCall: FnCall): V = + def pure[V <: Value: {FromExpr, Tag}](f: => V)(using fnCall: FnCall): V = val call = FunctionCall[V](fnCall.identifier, Scope(f.tree.asInstanceOf[Expression[V]], isDetached = true), fnCall.params) summon[FromExpr[V]].fromExpr(call) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala index 33df3207..34f102f2 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala @@ -7,7 +7,7 @@ import io.computenode.cyfra.dsl.Value.{FromExpr, GBoolean} import io.computenode.cyfra.dsl.macros.Source import izumi.reflect.Tag -case class When[T <: Value: Tag: FromExpr](when: GBoolean, thenCode: T, otherConds: List[Scope[GBoolean]], otherCases: List[Scope[T]], name: Source): +case class When[T <: Value: {Tag, FromExpr}](when: GBoolean, thenCode: T, otherConds: List[Scope[GBoolean]], otherCases: List[Scope[T]], name: Source): def elseWhen(cond: GBoolean)(t: T): When[T] = When(when, thenCode, otherConds :+ Scope(cond.tree), otherCases :+ Scope(t.tree.asInstanceOf[E[T]]), name) infix def otherwise(t: T): T = @@ -24,5 +24,5 @@ object When: otherwise: Scope[T], ) extends Expression[T] - def when[T <: Value: Tag: FromExpr](cond: GBoolean)(fn: T)(using name: Source): When[T] = + def when[T <: Value: {Tag, FromExpr}](cond: GBoolean)(fn: T)(using name: Source): When[T] = When(cond, fn, Nil, Nil, name) diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala index 85cb5002..02a018f8 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/gio/GIO.scala @@ -38,7 +38,7 @@ object GIO: def write[T <: Value](buffer: GBuffer[T], index: Int32, value: T): GIO[Unit] = WriteBuffer(buffer, index, value) - def read[T <: Value: FromExpr: Tag](buffer: GBuffer[T], index: Int32): T = + def read[T <: Value: {FromExpr, Tag}](buffer: GBuffer[T], index: Int32): T = fromExpr(ReadBuffer(buffer, index)) def invocationId: Int32 = diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/library/Functions.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/library/Functions.scala index 0de27564..26b4a970 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/library/Functions.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/library/Functions.scala @@ -16,7 +16,7 @@ object Functions: case object Cos extends FunctionName def cos(v: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Cos, List(v))) - def cos[V <: Vec[Float32]: Tag: FromExpr](v: V)(using Source): V = + def cos[V <: Vec[Float32]: {Tag, FromExpr}](v: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Cos, List(v))) case object Tan extends FunctionName @@ -43,7 +43,7 @@ object Functions: case object Pow extends FunctionName def pow(v: Float32, p: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Pow, List(v, p))) - def pow[V <: Vec[?]: Tag: FromExpr](v: V, p: V)(using Source): V = + def pow[V <: Vec[?]: {Tag, FromExpr}](v: V, p: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Pow, List(v, p))) case object Smoothstep extends FunctionName @@ -61,48 +61,48 @@ object Functions: case object Exp extends FunctionName def exp(f: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Exp, List(f))) - def exp[V <: Vec[Float32]: Tag: FromExpr](v: V)(using Source): V = + def exp[V <: Vec[Float32]: {Tag, FromExpr}](v: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Exp, List(v))) case object Max extends FunctionName def max(f1: Float32, f2: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Max, List(f1, f2))) def max(f1: Float32, f2: Float32, fx: Float32*)(using Source): Float32 = fx.foldLeft(max(f1, f2))((a, b) => max(a, b)) - def max[V <: Vec[Float32]: Tag: FromExpr](v1: V, v2: V)(using Source): V = + def max[V <: Vec[Float32]: {Tag, FromExpr}](v1: V, v2: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Max, List(v1, v2))) - def max[V <: Vec[Float32]: Tag: FromExpr](v1: V, v2: V, vx: V*)(using Source): V = + def max[V <: Vec[Float32]: {Tag, FromExpr}](v1: V, v2: V, vx: V*)(using Source): V = vx.foldLeft(max(v1, v2))((a, b) => max(a, b)) case object Min extends FunctionName def min(f1: Float32, f2: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Min, List(f1, f2))) def min(f1: Float32, f2: Float32, fx: Float32*)(using Source): Float32 = fx.foldLeft(min(f1, f2))((a, b) => min(a, b)) - def min[V <: Vec[Float32]: Tag: FromExpr](v1: V, v2: V)(using Source): V = + def min[V <: Vec[Float32]: {Tag, FromExpr}](v1: V, v2: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Min, List(v1, v2))) - def min[V <: Vec[Float32]: Tag: FromExpr](v1: V, v2: V, vx: V*)(using Source): V = + def min[V <: Vec[Float32]: {Tag, FromExpr}](v1: V, v2: V, vx: V*)(using Source): V = vx.foldLeft(min(v1, v2))((a, b) => min(a, b)) // todo add F/U/S to all functions that need it case object Abs extends FunctionName def abs(f: Float32)(using Source): Float32 = Float32(ExtFunctionCall(Abs, List(f))) - def abs[V <: Vec[Float32]: Tag: FromExpr](v: V)(using Source): V = + def abs[V <: Vec[Float32]: {Tag, FromExpr}](v: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Abs, List(v))) case object Mix extends FunctionName - def mix[V <: Vec[Float32]: Tag: FromExpr](a: V, b: V, t: V)(using Source) = + def mix[V <: Vec[Float32]: {Tag, FromExpr}](a: V, b: V, t: V)(using Source) = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Mix, List(a, b, t))) def mix(a: Float32, b: Float32, t: Float32)(using Source) = Float32(ExtFunctionCall(Mix, List(a, b, t))) - def mix[V <: Vec[Float32]: Tag: FromExpr](a: V, b: V, t: Float32)(using Source) = + def mix[V <: Vec[Float32]: {Tag, FromExpr}](a: V, b: V, t: Float32)(using Source) = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Mix, List(a, b, vec3(t)))) case object Reflect extends FunctionName - def reflect[I <: Vec[Float32]: Tag: FromExpr, N <: Vec[Float32]: Tag: FromExpr](I: I, N: N)(using Source): I = + def reflect[I <: Vec[Float32]: {Tag, FromExpr}, N <: Vec[Float32]: {Tag, FromExpr}](I: I, N: N)(using Source): I = summon[FromExpr[I]].fromExpr(ExtFunctionCall(Reflect, List(I, N))) case object Refract extends FunctionName - def refract[V <: Vec[Float32]: Tag: FromExpr](I: V, N: V, eta: Float32)(using Source): V = + def refract[V <: Vec[Float32]: {Tag, FromExpr}](I: V, N: V, eta: Float32)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Refract, List(I, N, eta))) case object Normalize extends FunctionName - def normalize[V <: Vec[Float32]: Tag: FromExpr](v: V)(using Source): V = + def normalize[V <: Vec[Float32]: {Tag, FromExpr}](v: V)(using Source): V = summon[FromExpr[V]].fromExpr(ExtFunctionCall(Normalize, List(v))) case object Log extends FunctionName diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/struct/GStruct.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/struct/GStruct.scala index 6ed56ab0..9ec4199b 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/struct/GStruct.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/struct/GStruct.scala @@ -10,7 +10,7 @@ import izumi.reflect.Tag import scala.compiletime.* import scala.deriving.Mirror -abstract class GStruct[T <: GStruct[T]: Tag: GStructSchema] extends Value with Product: +abstract class GStruct[T <: GStruct[T]: {Tag, GStructSchema}] extends Value with Product: self: T => private[cyfra] var _schema: GStructSchema[T] = summon[GStructSchema[T]] // a nasty hack def schema: GStructSchema[T] = _schema diff --git a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/shapes/ShapeCollection.scala b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/shapes/ShapeCollection.scala index efe2c76a..27fce060 100644 --- a/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/shapes/ShapeCollection.scala +++ b/cyfra-foton/src/main/scala/io/computenode/cyfra/foton/rt/shapes/ShapeCollection.scala @@ -36,7 +36,7 @@ class ShapeCollection(val boxes: List[Box], val spheres: List[Sphere], val quads case _ => assert(false, "Unknown shape type: Broken sealed hierarchy") def testRay(rayPos: Vec3[Float32], rayDir: Vec3[Float32], noHit: RayHitInfo): RayHitInfo = - def testShapeType[T <: GStruct[T] & Shape: FromExpr: Tag: TestRay](shapes: List[T], currentHit: RayHitInfo): RayHitInfo = + def testShapeType[T <: GStruct[T] & Shape: {FromExpr, Tag, TestRay}](shapes: List[T], currentHit: RayHitInfo): RayHitInfo = val testRay = summon[TestRay[T]] if shapes.isEmpty then currentHit else GSeq.of(shapes).fold(currentHit, (currentHit, shape) => testRay.testRay(shape, rayPos, rayDir, currentHit)) diff --git a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala index 6d068d9b..d1474976 100644 --- a/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala +++ b/cyfra-runtime/src/main/scala/io/computenode/cyfra/runtime/VkAllocation.scala @@ -19,11 +19,11 @@ class VkAllocation extends Allocation: extension [Params, L <: Layout, RL <: Layout: LayoutStruct](execution: GExecution[Params, L, RL]) def execute(params: Params, layout: L): RL = ??? extension (buffers: GBuffer.type) - def apply[T <: Value: Tag: FromExpr](size: Int): GBuffer[T] = ??? + def apply[T <: Value: {Tag, FromExpr}](size: Int): GBuffer[T] = ??? - def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GBuffer[T] = ??? + def apply[T <: Value: {Tag, FromExpr}](buff: ByteBuffer): GBuffer[T] = ??? extension (buffers: GUniform.type) - def apply[T <: Value: Tag: FromExpr](buff: ByteBuffer): GUniform[T] = ??? + def apply[T <: Value: {Tag, FromExpr}](buff: ByteBuffer): GUniform[T] = ??? - def apply[T <: Value: Tag: FromExpr](): GUniform[T] = ??? + def apply[T <: Value: {Tag, FromExpr}](): GUniform[T] = ??? From 601d8a35e01d74262cfd9bb2b9f8f664abc85d00 Mon Sep 17 00:00:00 2001 From: Szymon Date: Sun, 6 Jul 2025 15:02:43 +0200 Subject: [PATCH 16/16] LF! --- .../io/computenode/cyfra/core/archive/GContext.scala | 2 +- .../scala/io/computenode/cyfra/dsl/control/When.scala | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala index d2246b8f..c9f32763 100644 --- a/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala +++ b/cyfra-core/src/main/scala/io/computenode/cyfra/core/archive/GContext.scala @@ -47,7 +47,7 @@ class GContext(spirvToolsRunner: SpirvToolsRunner = SpirvToolsRunner()): ComputePipeline(shader, vkContext) def execute[G <: GStruct[G]: {Tag, GStructSchema}, H <: Value, R <: Value](mem: GMem[H], fn: GFunction[G, H, R])(using - uniformContext: UniformContext[G], + uniformContext: UniformContext[G], ): GMem[R] = val isUniformEmpty = uniformContext.uniform.schema.fields.isEmpty val actions = Map(LayoutLocation(0, 0) -> BufferAction.LoadTo, LayoutLocation(0, 1) -> BufferAction.LoadFrom) ++ diff --git a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala index 34f102f2..9e7be3ad 100644 --- a/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala +++ b/cyfra-dsl/src/main/scala/io/computenode/cyfra/dsl/control/When.scala @@ -7,7 +7,13 @@ import io.computenode.cyfra.dsl.Value.{FromExpr, GBoolean} import io.computenode.cyfra.dsl.macros.Source import izumi.reflect.Tag -case class When[T <: Value: {Tag, FromExpr}](when: GBoolean, thenCode: T, otherConds: List[Scope[GBoolean]], otherCases: List[Scope[T]], name: Source): +case class When[T <: Value: {Tag, FromExpr}]( + when: GBoolean, + thenCode: T, + otherConds: List[Scope[GBoolean]], + otherCases: List[Scope[T]], + name: Source, +): def elseWhen(cond: GBoolean)(t: T): When[T] = When(when, thenCode, otherConds :+ Scope(cond.tree), otherCases :+ Scope(t.tree.asInstanceOf[E[T]]), name) infix def otherwise(t: T): T =