From a2d40daef281f43cfe3a98d9d9097f814b0ba198 Mon Sep 17 00:00:00 2001 From: Patrick Stevens Date: Sat, 6 May 2023 23:40:09 +0100 Subject: [PATCH] Remove most asyncs (#14) --- RayTracing.App/SampleImages.fs | 10 +++--- RayTracing.Test/TestPpmOutput.fs | 22 +++++-------- RayTracing/Domain.fs | 33 +++---------------- RayTracing/ImageOutput.fs | 54 +++++++++++--------------------- RayTracing/Scene.fs | 23 +++++++------- 5 files changed, 49 insertions(+), 93 deletions(-) diff --git a/RayTracing.App/SampleImages.fs b/RayTracing.App/SampleImages.fs index 197fb34..17113fa 100644 --- a/RayTracing.App/SampleImages.fs +++ b/RayTracing.App/SampleImages.fs @@ -43,12 +43,14 @@ module SampleImages = } let image = - Array.init + Seq.init 256 (fun height -> - let output = Array.init 256 (fun i -> async { return pixelAt height i }) - progressIncrement 1.0 - output + async { + let output = Array.init 256 (pixelAt height) + progressIncrement 1.0 + return output + } ) |> Image.make 256 256 diff --git a/RayTracing.Test/TestPpmOutput.fs b/RayTracing.Test/TestPpmOutput.fs index 1aee69d..73390d8 100644 --- a/RayTracing.Test/TestPpmOutput.fs +++ b/RayTracing.Test/TestPpmOutput.fs @@ -16,24 +16,18 @@ module TestRayTracing = let image = [| + [| Colour.Red ; Colour.Green ; Colour.Blue |] [| - async { return Colour.Red } - async { return Colour.Green } - async { return Colour.Blue } - |] - [| - async { - return - { - Red = 255uy - Blue = 0uy - Green = 255uy - } + { + Red = 255uy + Blue = 0uy + Green = 255uy } - async { return Colour.White } - async { return Colour.Black } + Colour.White + Colour.Black |] |] + |> Array.map async.Return |> Image.make 2 3 let outputFile = fs.Path.GetTempFileName () |> fs.FileInfo.FromFileName diff --git a/RayTracing/Domain.fs b/RayTracing/Domain.fs index 6578cdb..00ecc22 100644 --- a/RayTracing/Domain.fs +++ b/RayTracing/Domain.fs @@ -1,5 +1,6 @@ namespace RayTracing +open System.Threading.Tasks open RayTracing [] @@ -8,7 +9,7 @@ type progress type Image = private { - Rows : Pixel Async[] seq + Rows : Async seq RowCount : int ColCount : int } @@ -19,34 +20,10 @@ module Image = let colCount i = i.ColCount - let render (i : Image) : (Pixel * Async)[] seq = - i.Rows - |> Seq.map (fun imageRow -> - if imageRow.Length <> i.ColCount then - failwithf - "Thought the image had %i columns, got a pixel array with %i columns" - i.ColCount - imageRow.Length + let render (i : Image) : Pixel[][] Task = + i.Rows |> Async.Parallel |> Async.StartAsTask - let outputRow = Array.zeroCreate> i.ColCount - - let doIt = - imageRow - |> Array.mapi (fun i p -> - async { - let! pixel = p - let _, a = outputRow.[i] - outputRow.[i] <- pixel, a - } - ) - - for k in 0 .. i.ColCount - 1 do - outputRow.[k] <- Unchecked.defaultof<_>, doIt.[k] - - outputRow - ) - - let make (rowCount : int) (colCount : int) (pixels : Async[] seq) : Image = + let make (rowCount : int) (colCount : int) (pixels : Async seq) : Image = { RowCount = rowCount ColCount = colCount diff --git a/RayTracing/ImageOutput.fs b/RayTracing/ImageOutput.fs index ce999e5..f25f4f1 100644 --- a/RayTracing/ImageOutput.fs +++ b/RayTracing/ImageOutput.fs @@ -148,46 +148,28 @@ module ImageOutput = = let tempFile = fs.Path.GetTempFileName () |> fs.FileInfo.FromFileName - tempFile, - task { - use outputStream = tempFile.OpenWrite () - use enumerator = image.Rows.GetEnumerator () - let mutable rowNum = 0 + let task = + async { + use outputStream = tempFile.OpenWrite () + let! pixels = image.Rows |> Async.Parallel - while enumerator.MoveNext () do - - let row = enumerator.Current - - let! _ = + pixels + |> Array.iteri (fun rowNum row -> row - |> Array.mapi (fun colNum pixel -> - backgroundTask { - let! pixel = - match soFar.TryGetValue ((rowNum, colNum)) with - | false, _ -> pixel - | true, v -> async { return v } - - lock - outputStream - (fun () -> - writeAsciiInt outputStream rowNum - outputStream.WriteByte 44uy // ',' - writeAsciiInt outputStream colNum - outputStream.WriteByte 10uy // '\n' - outputStream.WriteByte pixel.Red - outputStream.WriteByte pixel.Green - outputStream.WriteByte pixel.Blue - ) - - incrementProgress 1.0 - return () - } + |> Array.iteri (fun colNum pixel -> + writeAsciiInt outputStream rowNum + outputStream.WriteByte 44uy // ',' + writeAsciiInt outputStream colNum + outputStream.WriteByte 10uy // '\n' + outputStream.WriteByte pixel.Red + outputStream.WriteByte pixel.Green + outputStream.WriteByte pixel.Blue + incrementProgress 1.0 ) - |> Task.WhenAll - - rowNum <- rowNum + 1 - } + ) + } + tempFile, Async.StartAsTask task let writePpm (gammaCorrect : bool) diff --git a/RayTracing/Scene.fs b/RayTracing/Scene.fs index d06a6ae..5f7f23d 100644 --- a/RayTracing/Scene.fs +++ b/RayTracing/Scene.fs @@ -213,21 +213,22 @@ module Scene = RowCount = rowsIter ColCount = colsIter Rows = - Array.init + Seq.init rowsIter (fun row -> let row = maxHeightCoord - row - 1 - Array.init - colsIter - (fun col -> - let col = col - maxWidthCoord + async { + return + Array.init + colsIter + (fun col -> + let col = col - maxWidthCoord - async { - let ret = renderPixel print s rand camera maxWidthCoord maxHeightCoord row col - progressIncrement 1.0 - return ret - } - ) + let ret = renderPixel print s rand camera maxWidthCoord maxHeightCoord row col + progressIncrement 1.0 + ret + ) + } ) }