-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd8b9e4
commit 184ff4f
Showing
11 changed files
with
254 additions
and
187 deletions.
There are no files selected for viewing
This file contains 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 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,34 @@ | ||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
struct v2f | ||
{ | ||
float4 position [[position]]; | ||
half3 color; | ||
}; | ||
|
||
struct VertexData | ||
{ | ||
device float3* positions [[id(0)]]; | ||
device float3* colors [[id(1)]]; | ||
}; | ||
|
||
struct FrameData | ||
{ | ||
float angle; | ||
}; | ||
|
||
v2f vertex vertexMain( device const VertexData* vertexData [[buffer(0)]], constant FrameData* frameData [[buffer(1)]], uint vertexId [[vertex_id]] ) | ||
{ | ||
float a = frameData->angle; | ||
float3x3 rotationMatrix = float3x3( sin(a), cos(a), 0.0, cos(a), -sin(a), 0.0, 0.0, 0.0, 1.0 ); | ||
v2f o; | ||
o.position = float4( rotationMatrix * vertexData->positions[ vertexId ], 1.0 ); | ||
o.color = half3(vertexData->colors[ vertexId ]); | ||
return o; | ||
} | ||
|
||
half4 fragment fragmentMain( v2f in [[stage_in]] ) | ||
{ | ||
return half4( in.color, 1.0 ); | ||
} |
This file contains 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 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,62 @@ | ||
using System.Reflection; | ||
|
||
namespace SharpMetal.Examples.Common | ||
{ | ||
public static class EmbeddedResources | ||
{ | ||
private static readonly Assembly _resourceAssembly; | ||
|
||
static EmbeddedResources() | ||
{ | ||
_resourceAssembly = Assembly.GetAssembly(typeof(EmbeddedResources)); | ||
} | ||
|
||
public static string ReadAllText(string filename) | ||
{ | ||
var (assembly, path) = ResolveManifestPath(filename); | ||
|
||
return ReadAllText(assembly, path); | ||
} | ||
|
||
public static string ReadAllText(Assembly assembly, string filename) | ||
{ | ||
using var stream = GetStream(assembly, filename); | ||
if (stream == null) | ||
{ | ||
throw new FileNotFoundException($"{filename} in {assembly}"); | ||
} | ||
|
||
using var reader = new StreamReader(stream); | ||
return reader.ReadToEnd(); | ||
} | ||
|
||
public static Stream GetStream(Assembly assembly, string filename) | ||
{ | ||
// Assumes one namespace per assembly | ||
var @namespace = assembly.GetTypes()[0].Namespace; | ||
var manifestUri = @namespace + "." + filename.Replace('/', '.'); | ||
|
||
var stream = assembly.GetManifestResourceStream(manifestUri); | ||
|
||
return stream; | ||
} | ||
|
||
private static (Assembly, string) ResolveManifestPath(string filename) | ||
{ | ||
var segments = filename.Split('/', 2, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
if (segments.Length >= 2) | ||
{ | ||
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) | ||
{ | ||
if (assembly.GetName().Name == segments[0]) | ||
{ | ||
return (assembly, segments[1]); | ||
} | ||
} | ||
} | ||
|
||
return (_resourceAssembly, filename); | ||
} | ||
} | ||
} |
This file contains 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
44 changes: 44 additions & 0 deletions
44
src/SharpMetal.Examples.ComputeToRender/Shaders/Compute.metal
This file contains 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,44 @@ | ||
#include <metal_stdlib> | ||
using namespace metal; | ||
|
||
kernel void mandelbrot_set(texture2d< half, access::write > tex [[texture(0)]], | ||
uint2 index [[thread_position_in_grid]], | ||
uint2 gridSize [[threads_per_grid]], | ||
device const uint* frame [[buffer(0)]]) | ||
{ | ||
constexpr float kAnimationFrequency = 0.01; | ||
constexpr float kAnimationSpeed = 4; | ||
constexpr float kAnimationScaleLow = 0.62; | ||
constexpr float kAnimationScale = 0.38; | ||
|
||
constexpr float2 kMandelbrotPixelOffset = {-0.2, -0.35}; | ||
constexpr float2 kMandelbrotOrigin = {-1.2, -0.32}; | ||
constexpr float2 kMandelbrotScale = {2.2, 2.0}; | ||
|
||
// Map time to zoom value in [kAnimationScaleLow, 1] | ||
float zoom = kAnimationScaleLow + kAnimationScale * cos(kAnimationFrequency * *frame); | ||
// Speed up zooming | ||
zoom = pow(zoom, kAnimationSpeed); | ||
|
||
// Scale | ||
float x0 = zoom * kMandelbrotScale.x * ((float)index.x / gridSize.x + kMandelbrotPixelOffset.x) + kMandelbrotOrigin.x; | ||
float y0 = zoom * kMandelbrotScale.y * ((float)index.y / gridSize.y + kMandelbrotPixelOffset.y) + kMandelbrotOrigin.y; | ||
|
||
// Implement Mandelbrot set | ||
float x = 0.0; | ||
float y = 0.0; | ||
uint iteration = 0; | ||
uint max_iteration = 1000; | ||
float xtmp = 0.0; | ||
while(x * x + y * y <= 4 && iteration < max_iteration) | ||
{ | ||
xtmp = x * x - y * y + x0; | ||
y = 2 * x * y + y0; | ||
x = xtmp; | ||
iteration += 1; | ||
} | ||
|
||
// Convert iteration result to colors | ||
half color = (0.5 + 0.5 * cos(3.0 + iteration * 0.15)); | ||
tex.write(half4(color, color, color, 1.0), index, 0); | ||
} |
Oops, something went wrong.