-
Notifications
You must be signed in to change notification settings - Fork 13
fs2 interop #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
fs2 interop #58
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ on: | |
| push: | ||
| branches: | ||
| - main | ||
| - dev | ||
| tags: | ||
| - "v*" | ||
| pull_request: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
cyfra-e2e-test/src/test/scala/io/computenode/cyfra/e2e/fs2interop/Fs2Tests.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| package io.computenode.cyfra.e2e.fs2interop | ||
|
|
||
| import io.computenode.cyfra.core.archive.*, mem.*, GMem.fRGBA | ||
| import io.computenode.cyfra.dsl.{*, given}, algebra.VectorAlgebra | ||
| import io.computenode.cyfra.fs2interop.*, GPipe.*, Bridge.given | ||
| import io.computenode.cyfra.core.CyfraRuntime | ||
| import io.computenode.cyfra.runtime.VkCyfraRuntime | ||
|
|
||
| import fs2.{io as fs2io, *} | ||
|
|
||
| extension (f: fRGBA) | ||
| def neg = (-f._1, -f._2, -f._3, -f._4) | ||
| def scl(s: Float) = (f._1 * s, f._2 * s, f._3 * s, f._4 * s) | ||
| def add(g: fRGBA) = (f._1 + g._1, f._2 + g._2, f._3 + g._3, f._4 + g._4) | ||
| def close(g: fRGBA)(eps: Float): Boolean = | ||
| Math.abs(f._1 - g._1) < eps && Math.abs(f._2 - g._2) < eps && Math.abs(f._3 - g._3) < eps && Math.abs(f._4 - g._4) < eps | ||
|
|
||
| class Fs2Tests extends munit.FunSuite: | ||
| given cr: CyfraRuntime = VkCyfraRuntime() | ||
|
|
||
| test("fs2 through gPipeMap, just ints"): | ||
| val inSeq = (0 until 256).toSeq | ||
| val stream = Stream.emits(inSeq) | ||
| val pipe = gPipeMap[Pure, Int32, Int](_ + 1) | ||
| val result = stream.through(pipe).compile.toList | ||
| val expected = inSeq.map(_ + 1) | ||
| result | ||
| .zip(expected) | ||
| .foreach: (res, exp) => | ||
| assert(res == exp, s"Expected $exp, got $res") | ||
|
|
||
| test("fs2 through gPipeMap, floats and vectors"): | ||
| val inSeq = (0 to 255).map(_.toFloat).toSeq | ||
| val stream = Stream.emits(inSeq) | ||
| val pipe = gPipeMap[Pure, Float32, Vec4[Float32], Float, fRGBA](f => (f, f + 1f, f + 2f, f + 3f)) | ||
| val result = stream.through(pipe).compile.toList | ||
| val expected = inSeq.map(f => (f, f + 1f, f + 2f, f + 3f)) | ||
| result | ||
| .zip(expected) | ||
| .foreach: (res, exp) => | ||
| assert(res.close(exp)(0.001f), s"Expected $exp, got $res") | ||
|
|
||
| class Fs2LegacyTests extends munit.FunSuite: | ||
| given gc: GContext = GContext() | ||
|
|
||
| test("fs2 Float stream (legacy)"): | ||
| val inSeq = (0 to 255).map(_.toFloat) | ||
| val inStream = Stream.emits(inSeq) | ||
| val outStream = inStream.gPipeFloat(_ + 1f).toList | ||
| val expected = inStream.map(_ + 1f).toList | ||
| outStream | ||
| .zip(expected) | ||
| .foreach: (res, exp) => | ||
| assert(Math.abs(res - exp) < 0.001f, s"Expected $exp but got $res") | ||
|
|
||
| test("fs2 Int stream (legacy)"): | ||
| val inSeq = 0 to 255 | ||
| val inStream = Stream.emits(inSeq) | ||
| val outStream = inStream.gPipeInt(_ + 1).toList | ||
| val expected = inStream.map(_ + 1).toList | ||
| outStream | ||
| .zip(expected) | ||
| .foreach: (res, exp) => | ||
| assert(res == exp, s"Expected $exp but got $res") | ||
|
|
||
| test("fs2 Vec4Float stream (legacy)"): | ||
| val k = -2.1f | ||
| val f = (1.2f, 2.3f, 3.4f, 4.5f) | ||
| val v = VectorAlgebra.vec4.tupled(f) | ||
|
|
||
| val inSeq: Seq[fRGBA] = (0 to 1023) | ||
| .map(_.toFloat) | ||
| .grouped(4) | ||
| .map: | ||
| case Seq(a, b, c, d) => (a, b, c, d) | ||
| .toSeq | ||
| val inStream = Stream.emits(inSeq) | ||
| val outStream = inStream.gPipeVec4(vec => (-vec).*(k).+(v)).toList | ||
| val expected = inStream.map(vec => vec.neg.scl(k).add(f)).toList | ||
|
|
||
| outStream | ||
| .zip(expected) | ||
| .foreach: (res, exp) => | ||
| assert(res.close(exp)(0.001f), s"Expected $exp but got $res") |
58 changes: 58 additions & 0 deletions
58
cyfra-fs2/src/main/scala/io/computenode/cyfra/fs2interop/Bridge.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package io.computenode.cyfra.fs2interop | ||
|
|
||
| import io.computenode.cyfra.core.archive.mem.GMem.fRGBA | ||
| import io.computenode.cyfra.dsl.* | ||
|
|
||
| import fs2.* | ||
| import java.nio.ByteBuffer | ||
| import org.lwjgl.BufferUtils | ||
| import izumi.reflect.Tag | ||
|
|
||
| import scala.reflect.ClassTag | ||
|
|
||
| trait Bridge[CyfraType <: Value: FromExpr: Tag, ScalaType: ClassTag]: | ||
| def toByteBuffer(inBuf: ByteBuffer, chunk: Chunk[ScalaType]): ByteBuffer | ||
| def fromByteBuffer(outBuf: ByteBuffer, arr: Array[ScalaType]): Array[ScalaType] | ||
|
|
||
| object Bridge: | ||
| given Bridge[Int32, Int]: | ||
| def toByteBuffer(inBuf: ByteBuffer, chunk: Chunk[Int]): ByteBuffer = | ||
| inBuf.asIntBuffer().put(chunk.toArray[Int]).flip() | ||
| inBuf | ||
| def fromByteBuffer(outBuf: ByteBuffer, arr: Array[Int]): Array[Int] = | ||
| outBuf.asIntBuffer().get(arr).flip() | ||
| arr | ||
|
|
||
| given Bridge[Float32, Float]: | ||
| def toByteBuffer(inBuf: ByteBuffer, chunk: Chunk[Float]): ByteBuffer = | ||
| inBuf.asFloatBuffer().put(chunk.toArray[Float]).flip() | ||
| inBuf | ||
| def fromByteBuffer(outBuf: ByteBuffer, arr: Array[Float]): Array[Float] = | ||
| outBuf.asFloatBuffer().get(arr).flip() | ||
| arr | ||
|
|
||
| given Bridge[Vec4[Float32], fRGBA]: | ||
| def toByteBuffer(inBuf: ByteBuffer, chunk: Chunk[fRGBA]): ByteBuffer = | ||
| val vecs = chunk.toArray[fRGBA] | ||
| vecs.foreach: | ||
| case (x, y, z, a) => | ||
| inBuf.putFloat(x) | ||
| inBuf.putFloat(y) | ||
| inBuf.putFloat(z) | ||
| inBuf.putFloat(a) | ||
| inBuf.flip() | ||
| inBuf | ||
|
|
||
| def fromByteBuffer(outBuf: ByteBuffer, arr: Array[fRGBA]): Array[fRGBA] = | ||
| val res = outBuf.asFloatBuffer() | ||
| for i <- 0 until arr.size do arr(i) = (res.get(), res.get(), res.get(), res.get()) | ||
| outBuf.flip() | ||
| arr | ||
|
|
||
| given Bridge[GBoolean, Boolean]: | ||
| def toByteBuffer(inBuf: ByteBuffer, chunk: Chunk[Boolean]): ByteBuffer = | ||
| inBuf.put(chunk.toArray.asInstanceOf[Array[Byte]]).flip() | ||
| inBuf | ||
| def fromByteBuffer(outBuf: ByteBuffer, arr: Array[Boolean]): Array[Boolean] = | ||
| outBuf.get(arr.asInstanceOf[Array[Byte]]).flip() | ||
| arr |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I "fixed" the bug here, but I'm not sure if it's correct... it just compiles now.