add the ability to toggle parallelism
This commit is contained in:
parent
c6f5411f91
commit
0eda244ce2
40
Program.cs
40
Program.cs
|
|
@ -13,6 +13,8 @@ using SkiaSharp;
|
|||
|
||||
Main();
|
||||
|
||||
const string programsProsperoVm = "Programs/prospero.vm";
|
||||
|
||||
// STAThread is required if you deploy using NativeAOT on Windows
|
||||
// See https://github.com/raylib-cs/raylib-cs/issues/301
|
||||
[STAThread]
|
||||
|
|
@ -30,7 +32,7 @@ void Main()
|
|||
|
||||
void main()
|
||||
{
|
||||
float v = texture(texture0, fragTexCoord).r< 0 ? 1.0 : 0.0;
|
||||
float v = texture(texture0, fragTexCoord).r < 0 ? 1.0 : 0.0;
|
||||
outputColor = vec4(v, v, v, 1.0f) * fragColor;
|
||||
}
|
||||
""";
|
||||
|
|
@ -59,6 +61,10 @@ void Main()
|
|||
}
|
||||
}
|
||||
|
||||
bool shouldUseParallelism = true;
|
||||
bool shouldUseSimd = true;
|
||||
|
||||
bool isEvaluating = false;
|
||||
bool shouldEvaluate = false;
|
||||
bool shouldCancelUpdateTexture = false;
|
||||
bool shouldUpdateTexture = false;
|
||||
|
|
@ -69,17 +75,20 @@ void Main()
|
|||
|
||||
Raylib.ClearBackground(Color.White);
|
||||
|
||||
if (shouldEvaluate)
|
||||
if (shouldEvaluate & !isEvaluating)
|
||||
{
|
||||
shouldUpdateTexture = true;
|
||||
currentOutputImageData.AsSpan()[..currentOutputImageData.Length].Clear();
|
||||
|
||||
InterpreterOptions interpreterOptions = (shouldUseParallelism ? InterpreterOptions.Parallelism : default);
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
Instruction[] instructions = Parsing.Parse("Programs/prospero.vm");
|
||||
Interpreter.Evaluate(instructions, imageSize: currentOutputImageSize, currentOutputImageData);
|
||||
Instruction[] instructions = Parsing.Parse(programsProsperoVm);
|
||||
Interpreter.Evaluate(instructions, imageSize: currentOutputImageSize, interpreterOptions, currentOutputImageData);
|
||||
Raylib.UpdateTexture(currentOutputTexture, currentOutputImageData);
|
||||
shouldCancelUpdateTexture = true;
|
||||
isEvaluating = false;
|
||||
});
|
||||
|
||||
shouldEvaluate = false;
|
||||
|
|
@ -100,6 +109,7 @@ void Main()
|
|||
Raylib.EndShaderMode();
|
||||
|
||||
Raylib.DrawText("Sharpero (press R to evaluate, O to output to file)", 12, 12, 20, Color.White);
|
||||
Raylib.DrawText($" - parallelism {(shouldUseParallelism ? "enabled" : "disabled")} (P to toggle)", 12, 32, 20, Color.White);
|
||||
|
||||
if (Raylib.IsKeyPressed(KeyboardKey.R))
|
||||
{
|
||||
|
|
@ -114,6 +124,11 @@ void Main()
|
|||
});
|
||||
}
|
||||
|
||||
if (Raylib.IsKeyPressed(KeyboardKey.P))
|
||||
{
|
||||
shouldUseParallelism = !shouldUseParallelism;
|
||||
}
|
||||
|
||||
Raylib.EndDrawing();
|
||||
}
|
||||
|
||||
|
|
@ -130,7 +145,7 @@ float[] GenerateOutputImage(int currentImageSize, bool shouldWriteOutputImage =
|
|||
{
|
||||
(float[] result, double timeTakenSecondsEvaluate) = BenchmarkFunction(() =>
|
||||
{
|
||||
Instruction[] instructions = Parsing.Parse("Programs/prospero.vm");
|
||||
Instruction[] instructions = Parsing.Parse(programsProsperoVm);
|
||||
return Interpreter.Evaluate(instructions, imageSize: currentImageSize);
|
||||
});
|
||||
|
||||
|
|
@ -378,14 +393,25 @@ internal static class Parsing
|
|||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
internal enum InterpreterOptions
|
||||
{
|
||||
Parallelism = 0x1
|
||||
}
|
||||
|
||||
internal static class Interpreter
|
||||
{
|
||||
public static float[] Evaluate(Instruction[] instructions, int imageSize, float[]? result = null)
|
||||
public static float[] Evaluate(Instruction[] instructions, int imageSize, InterpreterOptions options = default, float[]? result = null)
|
||||
{
|
||||
result ??= new float[imageSize * imageSize];
|
||||
|
||||
ParallelOptions parallelOptions = new ParallelOptions()
|
||||
{
|
||||
MaxDegreeOfParallelism = (options & InterpreterOptions.Parallelism) != 0 ? -1 : 1
|
||||
};
|
||||
|
||||
int chunkSize = Vector<float>.Count;
|
||||
Parallel.For(0, (imageSize * imageSize) / chunkSize, chunkIdx =>
|
||||
Parallel.For(0, (imageSize * imageSize) / chunkSize, parallelOptions, chunkIdx =>
|
||||
{
|
||||
Span<float> xs = stackalloc float[chunkSize];
|
||||
Span<float> ys = stackalloc float[chunkSize];
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AArrayPool_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fc8beccd33913594a240a8bed807798632452943eba5864a26bfec56b2e617_003FArrayPool_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AColor_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F6a9fabe8949749a5835bf490db09a4cc272a00_003F66_003F1010c222_003FColor_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANumber_002EParsing_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fde8a243f75215d958afa80cf80a41b4981a44efea7db93bffcdaf7bd6ba378c0_003FNumber_002EParsing_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AParallel_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F1e8c32362ff486e4e342292ccce957781165d4f6305c795c1d6b1b473e4c0a3_003FParallel_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APersistedAssemblyBuilder_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F479daf628d169411ac8526d497865727d69e6e7f4832a1ba6e65e18abf4f90_003FPersistedAssemblyBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ARuntimeAssemblyBuilder_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F89f78f55baa267d67190b18dceab4e687d397e0573226d8f9d32ecc2271863e_003FRuntimeAssemblyBuilder_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AScalar_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FAppData_003FRoaming_003FJetBrains_003FRider2025_002E3_003Fresharper_002Dhost_003FSourcesCache_003F1a6f5a6d45979b9db8954e4e92811b8d7b853439d7953d79151da4586e57b3_003FScalar_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
|
||||
|
|
|
|||
Loading…
Reference in New Issue