Beispiel #1
0
        static unsafe void Main(string[] args)
        {
            var shader = new TestShader();

            ShaderInterpreter <IVertexShader> .Interpret <TestShader>();

            Stopwatch sw = new Stopwatch();

            sw.Start();
            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            Vector4[] input = new Vector4[itr_count];
            for (int i = 0; i < 480000 / itr_count; i++)
            {
                for (int j = 0; j < itr_count; j++)
                {
                    input[j].X = i;
                    input[j].Y = i;
                    input[j].Z = i;
                    input[j].W = i;
                }
                ShaderInvoker <IVertexShader> .Invoke(itr_count, input);
            }
            sw.Stop();
            WriteLine(sw.ElapsedMilliseconds);
            ReadKey();
        }
Beispiel #2
0
        public unsafe void TestVertexShaderTransform()
        {
            var shader = ShaderDefault.Instance;

            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            ShaderValue.ObjectToView = Matrix4x4.Translation(Vector3.One);

            Vector4[] pos = new Vector4[] { Vector4.UnitXPositive_Point, Vector4.UnitYPositive_Point, Vector4.UnitZPositive_Point };
            for (int i = 0; i < pos.Length; i++)
            {
                var output = ShaderInvoker <IVertexShader> .Invoke(i, pos);

                Assert.AreEqual(*output.VertexPtr, new Vector4(pos[i].XYZ + Vector3.One, 1));
            }
        }
Beispiel #3
0
        public unsafe void TestShaderInvoke()
        {
            TestShaderInterpret();
            var shader = new TestShader();

            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            ShaderInvoker <IFragmentShader> .ChangeActiveShader(shader);

            Vector4[] pos          = new Vector4[] { new Vector4(-1f, 2f, -3f, 4f) };
            Vector4   vertexResult = *ShaderInvoker <IVertexShader> .Invoke(0, pos).VertexPtr;

            Vector4 fragmentResult = *ShaderInvoker <IFragmentShader> .Invoke(0, pos).VertexPtr;

            Assert.AreEqual(vertexResult, new Vector4(-2.5f, 5f, -7.5f, 10f));
            Assert.AreEqual(fragmentResult, new Vector4(0f, 3f, -2f, 5f));
        }
Beispiel #4
0
        public unsafe void TestVertexShaderTransform()
        {
            var shader = ShaderDefault.Instance;

            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            Matrix4x4.Translation(Vector3.One, ShaderValue.ObjectToView);

            Vector4[] pos      = new Vector4[] { Vector4.UnitXPositive_Point, Vector4.UnitYPositive_Point, Vector4.UnitZPositive_Point };
            float *   inputPtr = stackalloc float[4];

            for (int i = 0; i < pos.Length; i++)
            {
                Set(inputPtr, pos[i]);
                ShaderInvoker <IVertexShader> .Invoke(inputPtr);

                Assert.AreEqual(*ShaderInvoker <IVertexShader> .OutputLayoutPtr->VertexPtr, new Vector4(pos[i].XYZ + Vector3.One, 1));
            }
        }
Beispiel #5
0
        public unsafe void TestShaderInvoke()
        {
            TestShaderInterpret();
            TestShader shader = new TestShader();

            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            ShaderInvoker <IFragmentShader> .ChangeActiveShader(shader);

            float *inputPtr = stackalloc float[4];

            Set(inputPtr, new Vector4(-1f, 2f, -3f, 4f));
            ShaderInvoker <IVertexShader> .Invoke(inputPtr);

            ShaderInvoker <IFragmentShader> .Invoke(inputPtr);

            Vector4 vertexResult   = *ShaderInvoker <IVertexShader> .InputLayoutPtr->VertexPtr;
            Vector4 fragmentResult = *ShaderInvoker <IFragmentShader> .OutputLayoutPtr->VertexPtr;

            Assert.AreEqual(vertexResult, new Vector4(-2.5f, 5f, -7.5f, 10f));
            Assert.AreEqual(fragmentResult, new Vector4(0f, 3f, -2f, 5f));
        }
Beispiel #6
0
        static unsafe void Main(string[] args)
        {
            var shader = new TestShader();

            ShaderInterpreter <IVertexShader> .Interpret <TestShader>();

            Stopwatch sw = new Stopwatch();

            sw.Start();
            ShaderInvoker <IVertexShader> .ChangeActiveShader(shader);

            float *zero = stackalloc float[4];

            *(Vector4 *)zero = Vector4.Zero;
            for (int i = 0; i < 480000; i++)
            {
                ShaderInvoker <IVertexShader> .Invoke(zero);
            }
            sw.Stop();
            WriteLine(sw.ElapsedMilliseconds);
            ReadKey();
        }
Beispiel #7
0
        public unsafe RenderBuffer <float> Draw(RenderEntity[] entities, ICamera camera)
        {
            RenderTarget.Clear();

            int        entityCount         = entities.Length;
            Fragment **rasterizedFragments = stackalloc Fragment *[entityCount];
            int *      primitiveCounts     = stackalloc int[entityCount];

            BeginRasterize();
            ShaderValue.WorldToView = camera.WorldToView;
            ShaderValue.Time        = CRenderer.CurrentSecond;
            ShaderValue.SinTime     = MathF.Sin(ShaderValue.Time);
            for (int i = 0; i < entityCount; i++)
            {
                RenderEntity instanceCopy = entities[i].GetInstanceToApply();
                IMaterial    material     = instanceCopy.Material ?? DEFAULT_MATERIAL;
                Vector4[]    vertices     = instanceCopy.Model.Vertices;
                IPrimitive[] primitives   = instanceCopy.Model.Primitives;

                ShaderValue.ObjectToWorld = instanceCopy.Transform.LocalToWorld;
                ShaderValue.ObjectToView  = ShaderValue.WorldToView * ShaderValue.ObjectToWorld;

                ShaderInvoker <IVertexShader> .ChangeActiveShader(material.ShaderType, material.Shader);

                int      vertexCount  = vertices.Length;
                Vector2 *coordsOutput = stackalloc Vector2[vertexCount];
                Vector4 *vertexOutput = stackalloc Vector4[vertexCount];

                for (int j = 0; j < vertexCount; j++)
                {
                    ShaderInOutMap outputMap = ShaderInvoker <IVertexShader> .Invoke(j, vertices);

                    vertexOutput[j] = *outputMap.VertexPtr;
                    coordsOutput[j] = ViewToScreen(*outputMap.VertexPtr);
                }

                Fragment *fragments = stackalloc Fragment[primitives.Length];
                primitiveCounts[i] = primitives.Length;
                for (int j = 0; j < primitives.Length; j++)
                {
                    Rasterize(coordsOutput, primitives[j], fragments + j);
                }
                rasterizedFragments[i] = fragments;
            }
            EndRasterize();

            //Octree is so annoying
            //TODO: View frustum clip, triangle clip, pixel clip
            //Clipping();

            //This is not the proper way to output, just to check result as soon as possible
            GenericVector <float> white = new GenericVector <float>(3)
            {
                1, 1, 1
            };

            for (int i = 0; i < entityCount; i++)
            {
                for (int j = 0; j < primitiveCounts[i]; j++)
                {
                    RenderTarget.WritePixel(rasterizedFragments[i][j].Rasterization, rasterizedFragments[i][j].PixelCount, white);
                }
            }

            return(RenderTarget);
        }