Exemplo n.º 1
0
        public override object Execute(List <string> args)
        {
            if (args.Count < 2)
            {
                return(false);
            }



            bool   use_assembly_compiler = args.Count >= 2 && args[0].ToLower() == "asm";
            string file        = args[args.Count - 1];
            string shader_code = File.ReadAllText(file);

            if (use_assembly_compiler && args.Count < 3)
            {
                return(false);
            }

            int index = use_assembly_compiler ? int.Parse(args[1]) : int.Parse(args[0]);

            if (!use_assembly_compiler)
            {
                using (var reader = new StringReader(shader_code))
                {
                    for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
                    {
                        var str = line.Trim();
                        if (str == "ps_3_0" || str == "vs_3_0")
                        {
                            use_assembly_compiler = true;
                            break;
                        }
                    }
                }
            }

            byte[] bytecode = null;
            if (use_assembly_compiler)
            {
                bytecode = D3DCompiler.Assemble(shader_code);
            }
            else
            {
                var profile = IsVertexShader ? "vs_3_0" : "ps_3_0";

                try
                {
                    bytecode = D3DCompiler.CompileFromFile(shader_code, "main", profile);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(true);
                }
            }

            var disassembly = D3DCompiler.Disassemble(bytecode);

            Console.WriteLine(disassembly);

            if (typeof(T) == typeof(PixelShader) || typeof(T) == typeof(GlobalPixelShader))
            {
                var shader_data_block = new PixelShaderBlock
                {
                    PCShaderBytecode = bytecode
                };

                if (typeof(T) == typeof(PixelShader))
                {
                    var _definition    = Definition as PixelShader;
                    var existing_block = _definition.Shaders[index];
                    shader_data_block.PCParameters = existing_block.PCParameters;

                    _definition.Shaders[index] = shader_data_block;
                }

                if (typeof(T) == typeof(GlobalPixelShader))
                {
                    var _definition    = Definition as GlobalPixelShader;
                    var existing_block = _definition.Shaders[index];
                    shader_data_block.PCParameters = existing_block.PCParameters;

                    _definition.Shaders[index] = shader_data_block;
                }
            }

            if (typeof(T) == typeof(VertexShader) || typeof(T) == typeof(GlobalVertexShader))
            {
                var shader_data_block = new VertexShaderBlock
                {
                    PCShaderBytecode = bytecode
                };

                if (typeof(T) == typeof(VertexShader))
                {
                    var _definition    = Definition as VertexShader;
                    var existing_block = _definition.Shaders[index];
                    shader_data_block.PCParameters = existing_block.PCParameters;

                    _definition.Shaders[index] = shader_data_block;
                }

                if (typeof(T) == typeof(GlobalVertexShader))
                {
                    var _definition    = Definition as GlobalVertexShader;
                    var existing_block = _definition.Shaders[index];
                    shader_data_block.PCParameters = existing_block.PCParameters;


                    _definition.Shaders[index] = shader_data_block;
                }
            }

            return(true);
        }