

#include <metal_stdlib>
using namespace metal;
/*
struct FragmentArgumentBuffer{

    texture2d<half> exampleTexture  [[ id(0)  ]];
    sampler         exampleSampler  [[ id(1)  ]];
    device float   *exampleBuffer   [[ id(2)   ]];
    uint32_t        exampleConstant [[ id(3) ]];

     float           exampleFloat [[ id(0)]];
};
*/
typedef struct
{
    float4x4 rotation_matrix;
} Uniforms;

typedef struct
{
    float4 position;
    float4 color;
} VertexIn;

typedef struct {
    float4 position [[position]];
    half4  color;
} VertexOut;

struct FragmentUniforms {
    float data1;
    int polyphony;
    float width, height;
    float values[4096];
       float buffer[16384];
    
};

vertex VertexOut vertex_function(device VertexIn *vertices [[buffer(0)]],
                                 constant Uniforms &uniforms [[buffer(1)]],
                                 uint vid [[vertex_id]])
{
    VertexOut out;
    out.position = uniforms.rotation_matrix * vertices[vid].position;
    out.color = half4(vertices[vid].color);
    return out;
}

float rand(float2 co)
{
    return fract(sin(dot(co.xy ,float2(12.9898,78.236))) * 43758.5453);
}

float2x2 rotation_matrix(float angle) {
    float ca = cos(angle);
    float sa = sin(angle);

    return float2x2(ca, -sa, sa, ca);
}


float2 moveRotateScalePow(float2 uv, float2 mxy, float angle, float scale) {
    uv += mxy;
    float ra = angle*3.14159265;
    float ss = sin(ra);
    float cs = cos(ra);
    uv = float2(uv.x*cs-uv.y*ss,uv.x*ss+uv.y*cs);
    uv=uv*pow(2.0,-scale*6.0);
    return uv;
}


    
   

