Example #1
0
        public ShaderCompiler.Operand[] Compile(ShaderCompiler compiler, ShaderCompiler.Operand[] operands, FixedShaderParameters parameters, ref SharpMedia.Graphics.Shaders.Operations.DualShareContext shareContext)
        {
            // We obtain interfaces (CPU resolve).
            object[] interfaces = InterfaceHelper.ResolveInterfaceArray(inputs[4], parameters);

            // We first register all our "constants" (for fill IDs).
            ShaderCompiler.Operand[] integerConstants = new ShaderCompiler.Operand[interfaces.Length];
            for (int i = 0; i < integerConstants.Length; i++)
            {
                integerConstants[i] = compiler.CreateFixed(PinFormat.Integer, Pin.NotArray, i);
            }

            // We pack out "attributes".
            ShaderCompiler.Operand[] attributes = new ShaderCompiler.Operand[] { operands[2] };

            // We create output colour constant.
            ShaderCompiler.Operand outColour = compiler.CreateTemporary(PinFormat.Floatx4, Pin.NotArray);

            // We switch
            compiler.BeginSwitch(operands[3]);

            // We go through all interfaces.
            for (int i = 0; i < interfaces.Length; i++)
            {
                compiler.BeginCase(integerConstants[i]);

                // We cast to fill.
                IFill fill = interfaces[i] as IFill;

                ShaderCompiler.Operand[] constants =
                    InterfaceHelper.RegisterInterfaceConstants(compiler,
                                                               string.Format("Fills[{0}]", i), parameters, fill);

                fill.Compile(compiler, operands[0], operands[1], attributes, null, constants, outColour);

                compiler.EndCase();
            }

            // We also add "default" handler.
            compiler.BeginDefault();
            compiler.Mov(compiler.CreateFixed(PinFormat.Floatx4, Pin.NotArray, new Math.Vector4f(1.0f, 0.0f, 0.0f, 1.0f)),
                         outColour);
            compiler.EndCase();

            // And end switch.
            compiler.EndSwitch();

            return(new ShaderCompiler.Operand[] { outColour });
        }
Example #2
0
        public void Compile(ShaderCompiler compiler, ShaderCompiler.Operand position,
                            ShaderCompiler.Operand texCoord, ShaderCompiler.Operand[] attributes,
                            ShaderCompiler.Operand borderDistance, ShaderCompiler.Operand[] constants,
                            ShaderCompiler.Operand outputColour)
        {
            // 1) We copy attributes.
            ShaderCompiler.Operand[] att1 = new ShaderCompiler.Operand[fill1.CustomAttributeCount];
            ShaderCompiler.Operand[] att2 = new ShaderCompiler.Operand[fill2.CustomAttributeCount];

            for (int i = 0; i < att1.Length; i++)
            {
                att1[i] = attributes[i];
            }

            for (int i = 0; i < att2.Length; i++)
            {
                att2[i] = attributes[i + att1.Length];
            }

            // 2) We copy constants.
            ShaderCompiler.Operand[] const1 = new ShaderCompiler.Operand[fill1.AdditionalParameters.Length];
            ShaderCompiler.Operand[] const2 = new ShaderCompiler.Operand[fill2.AdditionalParameters.Length];

            for (int i = 0; i < const1.Length; i++)
            {
                const1[i] = constants[i];
            }

            for (int i = 0; i < const2.Length; i++)
            {
                const2[i] = constants[i + const1.Length];
            }

            // 3) We compute partial colours.
            ShaderCompiler.Operand colour1 = compiler.CreateTemporary(PinFormat.Floatx4, Pin.NotArray);
            ShaderCompiler.Operand colour2 = compiler.CreateTemporary(PinFormat.Floatx4, Pin.NotArray);

            fill1.Compile(compiler, position, texCoord, att1, borderDistance, const1, colour1);
            fill2.Compile(compiler, position, texCoord, att2, borderDistance, const2, colour2);

            // 4) We combine them.
            compiler.Mov(compiler.Add(compiler.Mul(compiler.Sub(compiler.CreateFixed(PinFormat.Float,
                                                                                     Pin.NotArray, 1.0f), constants[constants.Length - 1]), colour1),
                                      compiler.Mul(constants[constants.Length - 1], colour2)), outputColour);
        }