A that maps the supported HLSL intrinsic functions that can be used in compute shaders.
예제 #1
0
            /// <inheritdoc/>
            public void Execute()
            {
                float value             = buffer[ThreadIds.X];
                ExternalStructType type = ExternalStructType.New((int)value, Hlsl.Abs(value));

                buffer[ThreadIds.X] = ExternalStructType.Sum(type);
            }
예제 #2
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        float4 color = D2D.GetInput(0);
        float3 rgb   = Hlsl.Saturate(1.0f - color.RGB);

        return(new(rgb, 1));
    }
        // Cheap shadows are hard. In fact, I'd almost say, shadowing particular scenes with limited
        // iterations is impossible... However, I'd be very grateful if someone could prove me wrong. :)
        private static float SoftShadow(Float3 ro, Float3 lp, Float3 n, float k)
        {
            const int iter = 24;

            ro += n * 0.0015f;

            Float3 rd    = lp - ro;
            float  shade = 1.0f;
            float  t     = 0;
            float  end   = Hlsl.Max(Hlsl.Length(rd), 0.0001f);

            rd /= end;

            for (int i = 0; i < iter; i++)
            {
                float d = M(ro + rd * t);

                shade = Hlsl.Min(shade, k * d / t);
                t    += Hlsl.Clamp(d, 0.01f, 0.25f);

                if (d < 0.0f || t > end)
                {
                    break;
                }
            }

            return(Hlsl.Max(shade, 0.0f));
        }
예제 #4
0
            public void Execute()
            {
                float exp = Hlsl.Exp(dword * row_major[ThreadIds.X]);
                float log = Hlsl.Log(1 + exp);

                row_major[ThreadIds.X] = log / dword + float2 + int2x2;
            }
        public void Execute(ThreadIds id)
        {
            var currentMax = 0;

            for (var j = 0; j < iterations; j++)
            {
                var i = (id.X * iterations) + j;
                if (i * 3 >= coordinatesSize)
                {
                    return;
                }
                var pX = (int)Hlsl.Floor(((coordinates[i * 3] - left) / (right - left)) * width);
                var pY = (int)Hlsl.Floor(((coordinates[(i * 3) + 2] - top) / (bottom - top)) * height);
                if (pX < 0 || pX >= width || pY < 0 || pY >= height)
                {
                    continue;
                }
                var denIndex = pX + (pY * width);
                density[denIndex]++;
                if (density[denIndex] > currentMax)
                {
                    currentMax = density[denIndex];
                }
            }

            Hlsl.InterlockedMax(maxDensity[0], currentMax);
            //if (currentMax > maxDensity[0]) { maxDensity[0] = currentMax; }
        }
예제 #6
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        float2 position = ((float2)(256 * ThreadIds.XY)) / DispatchSize.X + time;
        float4 color    = 0;

        for (int i = 0; i < 6; i++)
        {
            float2 a = Hlsl.Floor(position);
            float2 b = Hlsl.Frac(position);
            float4 w = Hlsl.Frac(
                (Hlsl.Sin(a.X * 7 + 31.0f * a.Y + 0.01f * time) +
                 new float4(0.035f, 0.01f, 0, 0.7f))
                * 13.545317f);

            color.XYZ += w.XYZ *
                         2.0f * Hlsl.SmoothStep(0.45f, 0.55f, w.W) *
                         Hlsl.Sqrt(16.0f * b.X * b.Y * (1.0f - b.X) * (1.0f - b.Y));

            position /= 2.0f;
            color    /= 2.0f;
        }

        color.XYZ = Hlsl.Pow(color.XYZ, new float3(0.7f, 0.8f, 0.5f));
        color.W   = 1.0f;

        return(color);
    }
예제 #7
0
        static float SDBox(float3 p, float3 b)
        {
            float3 q = Hlsl.Abs(p) - b;

            return(Hlsl.Length(Hlsl.Max(q, 0.0f)) +
                   Hlsl.Min(Hlsl.Max(q.X, Hlsl.Max(q.Y, q.Z)), 0.0f));
        }
예제 #8
0
    // Standard 2D rotation formula.
    private static float2x2 Rotate2x2(float a)
    {
        float c = Hlsl.Cos(a);
        float s = Hlsl.Sin(a);

        return(new(c, -s, s, c));
    }
            void Kernel(ThreadIds id)
            {
                int   offset = id.X + id.Y * 1;
                float pow    = Hlsl.Pow(xBuffer[offset], 2); // 9

                xBuffer[offset] = Sigmoid(pow);              // 0.9998766
            }
예제 #10
0
파일: Ray.cs 프로젝트: DominicMaas/Graphics
    public static Ray CreatePrime(int x, int y, Scene scene, int sample, float4x2 jitterMatrix)
    {
        // Determine X and Y
        var sensorX = (2f * (x + jitterMatrix[sample][0]) / scene.Width) - 1f;
        var sensorY = 1f - (2f * (y + jitterMatrix[sample][1]) / scene.Height);

        // Adjust for the aspect ratio
        var aspectRatio = (float)scene.Width / (float)scene.Height;

        sensorX *= aspectRatio;

        // Adjust for the FOV
        var fovAdjustment = Hlsl.Tan((scene.Camera.FOV * (MathF.PI / 180f)) / 2f);

        sensorX *= fovAdjustment;
        sensorY *= fovAdjustment;

#pragma warning disable IDE0017 // Simplify object initialization
        var ray = new Ray();
        ray.Origin    = new Vector3();
        ray.Direction = Hlsl.Normalize(new float3(sensorX, sensorY, -1.0f));
#pragma warning restore IDE0017 // Simplify object initialization

        return(ray);
    }
예제 #11
0
            public void Execute(ThreadIds ids)
            {
                int   offset = ids.X + ids.Y * 1;
                float pow    = Hlsl.Pow(B[offset], 2); // 9

                B[offset] = Sigmoid(pow);              // 0.9998766
            }
        // A standard square grid 2D blobby Truchet routine: Render circles
        // in opposite corners of a tile, reverse the pattern on alternate
        // checker tiles, and randomly rotate.
        private static float Truchet(Float2 p)
        {
            const float sc = 0.5f;
            Float2      ip = Hlsl.Floor(p / sc) + 0.5f;

            p -= ip * sc;

            float rnd = Hlsl.Frac(Hlsl.Sin(Hlsl.Dot(ip, new Float2(1, 113))) * 45758.5453f);

            if (rnd < .5)
            {
                p.Y = -p.Y;
            }

            float d = Hlsl.Min(Distance(p - 0.5f * sc), Distance(p + 0.5f * sc)) - 0.5f * sc;

            if (SHAPE == 4)
            {
                d += (0.5f - 0.5f / Hlsl.Sqrt(2.0f)) * sc;
            }

            if (rnd < 0.5f)
            {
                d = -d;
            }
            if (Mod(ip.X + ip.Y, 2.0f) < 0.5f)
            {
                d = -d;
            }

            return(d - 0.03f);
        }
예제 #13
0
            public void Execute()
            {
                float exp  = Hlsl.Exp(cellData.distance * row_major[ThreadIds.X]);
                float log  = Hlsl.Log(1 + exp);
                float temp = log + cellData.dword + cellData.int2x2;

                row_major[ThreadIds.X] = log / dword + float2 + int2x2 + cbuffer.float2;
            }
예제 #14
0
 public void Execute(ThreadIds ids)
 {
     B[0] = Hlsl.Sin(A);
     B[1] = Hlsl.Cos(A);
     Hlsl.SinCos(A, out float sine, out float cosine);
     B[2] = sine;
     B[3] = cosine;
 }
예제 #15
0
    // float2 to float2 hash.
    private float2 Hash22(float2 p)
    {
        float n = Hlsl.Sin(Hlsl.Dot(p, new float2(1, 113)));

        p = Hlsl.Frac(new float2(262144, 32768) * n);

        return(Hlsl.Sin(p * 6.2831853f + time));
    }
예제 #16
0
 public void Execute()
 {
     buffer[0] = DispatchSize.Count;
     buffer[1] = DispatchSize.X;
     buffer[2] = DispatchSize.Y;
     buffer[3] = DispatchSize.Z;
     buffer[4] = (int)Hlsl.Dot(DispatchSize.XY, Float2.One);
     buffer[5] = (int)Hlsl.Dot(DispatchSize.XYZ, Float3.One);
 }
예제 #17
0
 public void Execute()
 {
     buffer[0] = GroupSize.X;
     buffer[1] = GroupSize.Y;
     buffer[2] = GroupSize.Z;
     buffer[3] = GroupSize.Count;
     buffer[4] = (int)Hlsl.Dot(GroupSize.XY, float2.One);
     buffer[5] = (int)Hlsl.Dot(GroupSize.XYZ, float3.One);
 }
예제 #18
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        // Normalized screen space UV coordinates from 0.0 to 1.0
        float2 uv = ThreadIds.Normalized.XY;

        // Time varying pixel color
        float3 col = 0.5f + 0.5f * Hlsl.Cos(time + new float3(uv, uv.X) + new float3(0, 2, 4));

        // Output to screen
        return(new(col, 1f));
    }
 public void Execute()
 {
     buffer[0] = Pi;
     buffer[1] = SinPi;
     buffer[2] = Hlsl.Sin(3.14f);
     buffer[3] = Mat.M11;
     buffer[4] = Mat.M12;
     buffer[5] = Mat.M21;
     buffer[6] = Mat.M22;
     buffer[7] = Combo;
 }
예제 #20
0
        public void IntrinsicWithInlineOutParamater()
        {
            float angle = 80;

            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(4);

            Action <ThreadIds> action = id =>
            {
                buffer[0] = Hlsl.Sin(angle);
                buffer[1] = Hlsl.Cos(angle);
                Hlsl.SinCos(angle, out float sine, out float cosine);
                buffer[2] = sine;
                buffer[3] = cosine;
            };
예제 #21
0
    /// <summary>
    /// Makes some magic happen.
    /// </summary>
    private float4 Tex(float3 p)
    {
        float  t   = time + 78.0f;
        float4 o   = new(p.X, p.Y, p.Z, 3.0f * Hlsl.Sin(t * 0.1f));
        float4 dec =
            new float4(1.0f, 0.9f, 0.1f, 0.15f) +
            new float4(0.06f * Hlsl.Cos(t * 0.1f), 0, 0, 0.14f * Hlsl.Cos(t * 0.23f));

        for (int i = 0; i++ < NumberOfIterations;)
        {
            o.XZYW = Hlsl.Abs(o / Hlsl.Dot(o, o) - dec);
        }

        return(o);
    }
예제 #22
0
            public void Execute()
            {
                float exp = Hlsl.Exp(dword * row_major[ThreadIds.X]);
                float log = Hlsl.Log(1 + exp);

                float s1 = this.cos * exp * this.sin * log;
                float t1 = -this.sin * exp * this.cos * log;

                float s2 = s1 + this.intensity + Hlsl.Tan(s1 * this.scale);
                float t2 = t1 + this.intensity + Hlsl.Tan(t1 * this.scale);

                float u2 = this.cos * s2 - this.sin * t2;
                float v2 = this.sin * s2 - this.cos * t2;

                row_major[ThreadIds.X] = log / dword + float2 + int2x2 + u2 + v2;
            }
예제 #23
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        float2 position = D2D.GetScenePosition().XY;
        uint   x        = (uint)Hlsl.Floor(position.X);
        uint   y        = (uint)Hlsl.Floor(position.Y);

        uint cellX = (uint)(int)Hlsl.Floor(x / cellSize);
        uint cellY = (uint)(int)Hlsl.Floor(y / cellSize);

        if ((cellX % 2 == 0 && cellY % 2 == 0) ||
            (cellX % 2 == 1 && cellY % 2 == 1))
        {
            Hlsl.Clip(-1);
        }

        return(D2D.GetInput(0));
    }
예제 #24
0
    // Based on IQ's gradient noise formula.
    private float Noise2D3G(float2 p)
    {
        float2 i = Hlsl.Floor(p);

        p -= i;

        float4 v = default;

        v.X = Hlsl.Dot(Hash22(i), p);
        v.Y = Hlsl.Dot(Hash22(i + float2.UnitX), p - float2.UnitX);
        v.Z = Hlsl.Dot(Hash22(i + float2.UnitY), p - float2.UnitY);
        v.W = Hlsl.Dot(Hash22(i + 1.0f), p - 1.0f);

        p = p * p * p * (p * (p * 6.0f - 15.0f) + 10.0f);

        return(Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y));
    }
예제 #25
0
    // Smooth 2D noise
    private static float Noise2D(float2 p)
    {
        float2 i = Hlsl.Floor(p);

        p -= i;

        float4 v = default;

        v.X = Hlsl.Dot(Hash22B(i), p);
        v.Y = Hlsl.Dot(Hash22B(i + float2.UnitX), p - float2.UnitX);
        v.Z = Hlsl.Dot(Hash22B(i + float2.UnitY), p - float2.UnitY);
        v.W = Hlsl.Dot(Hash22B(i + 1.0f), p - 1.0f);

        p = p * p * (3.0f - 2.0f * p);

        return(Hlsl.Lerp(Hlsl.Lerp(v.X, v.Y, p.X), Hlsl.Lerp(v.Z, v.W, p.X), p.Y));
    }
예제 #26
0
        public void Execute()
        {
            int  index = ThreadIds.X / 2;
            bool isWritingToGroupShared = ThreadIds.X % 2 == 0;

            if (isWritingToGroupShared)
            {
                cache[index] = index;
            }

            Hlsl.GroupMemoryBarrier();

            if (!isWritingToGroupShared)
            {
                buffer[index] = cache[index];
            }
        }
예제 #27
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        float2 scenePos = D2D.GetScenePosition().XY;

        float xo = scenePos.X - (this.width >> 1);
        float yo = scenePos.Y - (this.height >> 1);

        float rm = 0.5f * this.diameter;
        float km = 0.7f / this.diameter * MathF.PI;
        float w  = rm / 10.0f;

        float yd = yo * yo;
        float xd = xo * xo;
        float d  = xd + yd;
        float v  = 1.0f + (1.0f + Hlsl.Tanh((rm - Hlsl.Sqrt(d)) / w)) * Hlsl.Sin(km * d) * 0.5f;

        return(new Float4(v, v, v, 1.0f));
    }
예제 #28
0
    /// <inheritdoc/>
    public float4 Execute()
    {
        float2 uv  = (ThreadIds.XY - (float2)DispatchSize.XY * 0.5f) / DispatchSize.Y;
        float3 col = 0;
        float  t   = time * 0.3f;

        for (float i = 0.0f; i <= 1.0f; i += 1.0f / NumberOfLayers)
        {
            float d = Hlsl.Frac(i + t);
            float s = Hlsl.Lerp(5.0f, 0.5f, d);
            float f = d * Hlsl.SmoothStep(1.0f, 0.9f, d);

            col += Tex(new float3(uv.X * s, uv.Y * s, i * 4.0f)).XYZ *f;
        }

        col /= NumberOfLayers;
        col *= new float3(2, 1.0f, 2.0f);
        col  = Hlsl.Pow(col, 0.5f);

        return(new(col.X, col.Y, col.Z, 1.0f));
    }
예제 #29
0
        private static RayIntersection IntersectSphereWithRay(RenderableEntity entity, Ray ray)
        {
            // Create a line segment between the ray origin and the center of the sphere
            var line = entity.Position - ray.Origin;

            // Use line as a hypotenuse and find the length of the adjacent side
            var adjacent = Hlsl.Dot(line, ray.Direction);

            // Find the length-squared of the opposite side
            var length2 = Hlsl.Dot(line, line) - (adjacent * adjacent);

            // Determine the radius squared
            var radius2 = entity.Radius * entity.Radius;

            // If that length-squared is greater than radius squared, the ray doesn't interact the sphere
            if (length2 > radius2)
            {
                return(new RayIntersection());
            }

            var thc = MathF.Sqrt(radius2 - length2);
            var t0  = adjacent - thc;
            var t1  = adjacent + thc;

            if (t0 < 0.0f && t1 < 0.0f)
            {
                return(new RayIntersection());
            }

            // Determine the intersect distance
            var distance = t0 < t1 ? t0 : t1;

#pragma warning disable IDE0017 // Simplify object initialization
            var rayIntersection = new RayIntersection();
            rayIntersection.Intersecting = true;
            rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

            return(rayIntersection);
        }
예제 #30
0
        private static RayIntersection IntersectPlaneWithRay(RenderableEntity entity, Ray ray)
        {
            var denom = Hlsl.Dot(entity.Normal, ray.Direction);

            if (denom > 0.000001f)
            {
                var v        = entity.Position - ray.Origin;
                var distance = Hlsl.Dot(v, entity.Normal) / denom;
                if (distance >= 0.0)
                {
#pragma warning disable IDE0017 // Simplify object initialization
                    var rayIntersection = new RayIntersection();
                    rayIntersection.Intersecting = true;
                    rayIntersection.Distance     = distance;
#pragma warning restore IDE0017 // Simplify object initialization

                    return(rayIntersection);
                }
            }

            return(new RayIntersection());
        }