Beispiel #1
0
        public void LocalBoolAssignToBool2Buffer()
        {
            Bool2 b2 = Bool2.TrueY;

            using ReadWriteBuffer <Bool2> buffer = Gpu.Default.AllocateReadWriteBuffer <Bool2>(1);

            var shader = new LocalBoolAssignToBool2Buffer_Shader {
                B2 = b2, B = buffer
            };

            Gpu.Default.For(1, shader);

            Bool2[] result = buffer.GetData();

            Assert.IsTrue(result[0].X == b2.X);
            Assert.IsTrue(result[0].Y == b2.Y);
        }
        public void LocalKnownVectorAssignToBuffer()
        {
            Vector4 vector4 = new Vector4(1, 2, 3, 4);

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

            var shader = new LocalKnownVectorAssignToBuffer_Shader {
                V4 = vector4, B = buffer
            };

            Gpu.Default.For(4, shader);

            float[] result   = buffer.GetData();
            float[] expected = { 1, 2, 3, 4 };

            Assert.IsTrue(result.AsSpan().ContentEquals(expected));
        }
Beispiel #3
0
        private void MethodRunner(Func <float, float> func, float input, float expected)
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(1);

            var shader = new ChangingFuncsFromExternalMethod_Shader
            {
                F = func,
                B = buffer,
                I = input
            };

            Gpu.Default.For(1, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - expected) < 0.0001f);
        }
        public void CopyBetweenWriteableBuffers()
        {
            float[] source = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();

            using ReadWriteBuffer <float> input  = Gpu.Default.AllocateReadWriteBuffer(source);
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(source.Length);

            var shader = new CopyBetweenWriteableBuffers_Shader {
                IN = input, OUT = buffer
            };

            Gpu.Default.For(source.Length, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(result.AsSpan().ContentEquals(source));
        }
        public void IntrinsicWithOutParamaters()
        {
            float angle = 80;

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

            var shader = new IntrinsicWithOutParamaters_Shader {
                A = angle, B = buffer
            };

            Gpu.Default.For(1, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - result[2]) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[1] - result[3]) < 0.0001f);
        }
Beispiel #6
0
        public void LocalFloatAssignToFloat2Buffer()
        {
            Float2 f2 = new Float2(1, 2);

            using ReadWriteBuffer <Float2> buffer = Gpu.Default.AllocateReadWriteBuffer <Float2>(1);

            var shader = new LocalFloatAssignToFloat2Buffer_Shader {
                F2 = f2, B = buffer
            };

            Gpu.Default.For(1, shader);

            Float2[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0].X - f2.Y) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[0].Y - f2.X) < 0.0001f);
        }
Beispiel #7
0
        public void FloatToFloat4Func()
        {
            using ReadWriteBuffer <Float4> buffer = Gpu.Default.AllocateReadWriteBuffer <Float4>(1);

            var shader = new FloatToFloat4Func_Shader {
                B = buffer
            };

            Gpu.Default.For(1, shader);

            Float4[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0].X - 3) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[0].Y - 4) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[0].Z - 5) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[0].W - 6) < 0.0001f);
        }
Beispiel #8
0
        public void CustomStaticDelegate()
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(1);

            Squarer square = StaticMethodsContainer.Square;

            var shader = new CustomStaticDelegate_Shader
            {
                F = square,
                B = buffer
            };

            Gpu.Default.For(1, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
        }
Beispiel #9
0
        public void InlineStatelessDelegate()
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(1);

            Func <float, float> f = x => x * x;

            var shader = new FloatToFloatFunc_Shader
            {
                F = f,
                B = buffer
            };

            Gpu.Default.For(1, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
        }
Beispiel #10
0
        public void InternalFloatToFloatFunc()
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(1);

            Func <float, float> square = StaticMethodsContainer.InternalSquare;

            var shader = new FloatToFloatFunc_Shader
            {
                F = square,
                B = buffer
            };

            Gpu.Default.For(1, shader);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - 9) < 0.0001f);
        }
Beispiel #11
0
        public void Bool3Operations()
        {
            using ReadWriteBuffer <Bool3> buffer = Gpu.Default.AllocateReadWriteBuffer <Bool3>(3);

            Action <ThreadIds> action = id =>
            {
                buffer[0]    = Bool3.TrueX;
                buffer[1]    = Bool3.True;
                buffer[2].YZ = Bool2.True;
            };

            Gpu.Default.For(1, action);

            Bool3[] result = buffer.GetData();

            Assert.IsTrue(result[0].X && !result[0].Y && !result[0].Z);
            Assert.IsTrue(result[1].X && result[1].Y && result[1].Z);
            Assert.IsTrue(!result[2].X && result[2].Y && result[2].Z);
        }
        public void RunShaderOnAllDevices()
        {
            foreach (GraphicsDevice gpu in Gpu.EnumerateDevices())
            {
                using (ReadWriteBuffer <float> buffer = gpu.AllocateReadWriteBuffer <float>(100))
                {
                    Action <ThreadIds> action = id => buffer[id.X] = id.X;

                    gpu.For(100, action);

                    float[] array    = buffer.GetData();
                    float[] expected = Enumerable.Range(0, 100).Select(i => (float)i).ToArray();

                    Assert.IsTrue(array.AsSpan().ContentEquals(expected));
                }

                gpu.Dispose();
            }
        }
        public void CopyBetweenNestedClosuresEasy()
        {
            int value1 = 1;

            using (ReadWriteBuffer <int> buffer1 = Gpu.Default.AllocateReadWriteBuffer <int>(1))
            {
                int value2 = 2;
                Action <ThreadIds> action = id =>
                {
                    int sum = value1 + value2;
                    buffer1[0] = sum;
                };

                Gpu.Default.For(1, action);

                int[] result1 = buffer1.GetData();

                Assert.IsTrue(result1[0] == value1 + value2);
            }
        }
        public void CopyBetweenNestedClosuresHard()
        {
            int value1 = 1, value2 = 55, value3 = 77;

            using (ReadWriteBuffer <int> buffer1 = Gpu.Default.AllocateReadWriteBuffer <int>(1))
            {
                int value4 = 2, value5 = 24, value6 = 99;
                using (ReadWriteBuffer <int> buffer2 = Gpu.Default.AllocateReadWriteBuffer <int>(1))
                {
                    int value7 = 3, value8 = 12, value9 = 333;
                    using (ReadWriteBuffer <int> buffer3 = Gpu.Default.AllocateReadWriteBuffer <int>(1))
                    {
                        int value10 = 4, value11 = 22;
                        Action <ThreadIds> action = id =>
                        {
                            int sum_gpu =
                                value1 + value2 + value3 + value4 +
                                value5 + value6 + value7 + value8 +
                                value9 + value10 + value11;
                            buffer1[0] = buffer2[0] = buffer3[0] = sum_gpu;
                        };

                        Gpu.Default.For(1, action);

                        int[] result1             = buffer1.GetData();
                        int[] result2             = buffer2.GetData();
                        int[] result3             = buffer3.GetData();

                        int sum_cpu =
                            value1 + value2 + value3 + value4 +
                            value5 + value6 + value7 + value8 +
                            value9 + value10 + value11;

                        Assert.IsTrue(result1[0] == sum_cpu);
                        Assert.IsTrue(result2[0] == sum_cpu);
                        Assert.IsTrue(result3[0] == sum_cpu);
                    }
                }
            }
        }
        private static void LocalScalarAssignToBuffer <T>(T value, Func <T, T, bool> equals) where T : unmanaged
        {
            using ReadWriteBuffer <T> buffer = Gpu.Default.AllocateReadWriteBuffer <T>(4);

            T zero = default;

            var shader = new LocalScalarAssignToBuffer_Shader <T>
            {
                Value = value,
                Zero  = zero,
                B     = buffer
            };

            Gpu.Default.For(4, shader);

            T[] result = buffer.GetData();

            Assert.IsTrue(equals(result[0], value));
            Assert.IsFalse(equals(result[1], value));
            Assert.IsTrue(equals(result[2], value));
            Assert.IsFalse(equals(result[3], value));
        }
Beispiel #16
0
        public void StaticReadonlyVectorFieldAssignToBuffer()
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(2);

            Action <ThreadIds> action = id =>
            {
                if (id.X == 0)
                {
                    buffer[0] = StaticMembersContainer.ReadonlyVector2.X;
                }
                else
                {
                    buffer[1] = StaticMembersContainer.ReadonlyVector2.Y;
                }
            };

            Gpu.Default.For(2, action);

            float[] result = buffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - StaticMembersContainer.ReadonlyVector2.X) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[1] - StaticMembersContainer.ReadonlyVector2.Y) < 0.0001f);
        }
Beispiel #17
0
        public void StaticReadOnlyBufferFieldAssignToBuffer()
        {
            using ReadWriteBuffer <float> buffer = Gpu.Default.AllocateReadWriteBuffer <float>(2);

            Action <ThreadIds> action = id =>
            {
                if (id.X == 0)
                {
                    buffer[0] = StaticFieldsContainer.StaticBuffer[0];
                }
                else
                {
                    buffer[1] = StaticFieldsContainer.StaticBuffer[1];
                }
            };

            Gpu.Default.For(2, action);

            float[] result   = buffer.GetData();
            float[] expected = StaticFieldsContainer.StaticBuffer.GetData();

            Assert.IsTrue(MathF.Abs(result[0] - expected[0]) < 0.0001f);
            Assert.IsTrue(MathF.Abs(result[1] - expected[1]) < 0.0001f);
        }