Example #1
0
        private void compileShader(FixedFuncShaders.eFixedFuncShaderIndex id, string shaderText, string[] defines, bool vertexShader)
        {
            Macro [] Macrodefines = null;
            if (defines != null)
            {
                Macrodefines = new Macro[defines.Length];
                for (int k = 0; k < defines.Length; k++)
                {
                    Macrodefines[k].Definition = defines[k];
                }
            }
            GraphicsStream gs = ShaderLoader.CompileShader(shaderText, "main", Macrodefines, null, vertexShader ? "vs_2_0" : "ps_2_0", ShaderFlags.None);

            if (vertexShader)
            {
                BFixedFuncVS vsA = new BFixedFuncVS();
                vsA.mVS = new VertexShader(BRenderDevice.getDevice(), gs);
                vsA.mID = id;
                mVertexShaders.Add(vsA);
            }
            else
            {
                BFixedFuncPS vsA = new BFixedFuncPS();
                vsA.mPS = new PixelShader(BRenderDevice.getDevice(), gs);
                vsA.mID = id;
                mPixelShaders.Add(vsA);
            }
            gs.Close();
            gs = null;
        }
Example #2
0
        public string CreateTextureContentFromShader(string effectFilename, string functionName, int width, int height, string outputPath)
        {
            // Adopted from Nvidia DXSas Sample
            string        errors;
            ConstantTable constants;

            // We may be here for the first creation of the function texture, or we may be here
            // because the function texture is associated with the viewport size, and it needs to be rebuilt.
            string effectText = File.ReadAllText(effectFilename);

            GraphicsStream functionStream = ShaderLoader.CompileShader(effectText, functionName, null, null, "tx_1_0", 0, out errors, out constants);

            if (functionStream == null)
            {
                throw new InvalidOperationException("Couldn't find texture function: " + functionName);
            }

            TextureShader shader = new TextureShader(functionStream);

            if (shader == null)
            {
                throw new InvalidOperationException("Couldn't create texture shader from function stream for: " + functionName);
            }

            Texture texture = new Texture(Device, (int)width, (int)height, 1, 0, Format.A8R8G8B8, Pool.Managed);

            TextureLoader.FillTexture(texture, shader);

            outputPath = Path.Combine(outputPath, Path.ChangeExtension(Path.GetFileName(effectFilename), functionName + ".dds"));
            TextureLoader.Save(outputPath, ImageFileFormat.Dds, texture);
            return(outputPath);
        }
Example #3
0
        /// <summary>
        ///     Compiles the high level shader source to low level microcode.
        /// </summary>
        protected override void LoadFromSource()
        {
            string errors;

            Macro [] macros = null;
            if (preprocessorDefines != "")
            {
                string [] values = preprocessorDefines.Split(new char[] { ',', ';' });
                macros = new Macro[values.Length];
                for (int i = 0; i < values.Length; i++)
                {
                    string    s     = values[i].Trim();
                    string [] stm   = s.Split(new char[] { '=' });
                    Macro     macro = new Macro();
                    if (stm.Length == 1)
                    {
                        macro.Name = s;
                    }
                    else
                    {
                        macro.Name       = stm[0].Trim();
                        macro.Definition = stm[1].Trim();
                    }
                    macros[i] = macro;
                }
            }
            // compile the high level shader to low level microcode
            // note, we need to pack matrices in row-major format for HLSL
            microcode =
                ShaderLoader.CompileShader(
                    source,
                    entry,
                    macros,
                    new HLSLInclude(),
                    target,
                    (columnMajorMatrices ? ShaderFlags.PackMatrixColumnMajor : ShaderFlags.PackMatrixRowMajor),
                    out errors,
                    out constantTable);

            //LogManager.Instance.Write(ShaderLoader.DisassembleShader(microcode, false, null));
            // check for errors
            if (errors != null && errors.Length > 0)
            {
                LogManager.Instance.Write("Error compiling HLSL shader {0}:\n{1}", name, errors);
                // errors can include warnings, so don't throw unless the compile actually failed.
                if (microcode == null)
                {
                    throw new AxiomException("HLSL: Unable to compile high level shader {0}:\n{1}", name, errors);
                }
            }
        }
Example #4
0
        private ConstantTable SetupDevice(OpsContext context, ShadeArgs args)
        {
            string         errStr = null;
            ConstantTable  constantTable;
            GraphicsStream pshader;
            GraphicsStream vshader;

            try
            {
                ConstantTable dummyTable;
                errStr  = null;
                vshader = ShaderLoader.CompileShader(ShadeVertex.VertexShader, "VertexShader", null, null, "vs_3_0", ShaderFlags.None, out errStr, out dummyTable);
            }
            finally
            {
                if (errStr != null && errStr.Length > 0)
                {
                    OpsConsole.WriteLine("Vertex Shader Compiler messages: " + errStr);
                    OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'.  Please use the argument 'Shader' to specify it.");
                }
            }


            try
            {
                Macro dxopsMacro = new Macro();
                dxopsMacro.Name       = "__DXOPS";
                dxopsMacro.Definition = "1";
                errStr  = null;
                pshader = ShaderLoader.CompileShaderFromFile(args.File, args.Shader, new Macro[] { dxopsMacro }, null, "ps_3_0", ShaderFlags.None, out errStr, out constantTable);
            }
            finally
            {
                if (errStr != null && errStr.Length > 0)
                {
                    OpsConsole.WriteLine("Pixel Shader Compiler messages: " + errStr);
                    OpsConsole.WriteLine("If this message is regarding your entry point, it may be something other than the default 'main'.  Please use the argument 'Shader' to specify it.");
                }
            }

            context.Device.SetRenderState(RenderStates.CullMode, (int)Cull.None);
            context.Device.SetRenderState(RenderStates.FillMode, (int)FillMode.Solid);
            context.Device.SetRenderState(RenderStates.AlphaTestEnable, false);
            context.Device.SetRenderState(RenderStates.AlphaBlendEnable, false);
            context.Device.SetRenderState(RenderStates.StencilEnable, false);
            context.Device.SetRenderState(RenderStates.ZEnable, false);
            context.Device.SetRenderState(RenderStates.ZBufferWriteEnable, false);

            context.Device.DepthStencilSurface = null;

            VertexDeclaration decl = new VertexDeclaration(context.Device, ShadeVertex.VertexDeclaration);

            context.Device.VertexDeclaration = decl;

            VertexShader vs = new VertexShader(context.Device, vshader);

            context.Device.VertexShader = vs;

            PixelShader ps = new PixelShader(context.Device, pshader);

            context.Device.PixelShader = ps;



            constantTable.SetDefaults(context.Device);

            foreach (OpsParsedArgument constant in args.Constants)
            {
                EffectHandle h = constantTable.GetConstant(null, constant.Name);
                if (h == null)
                {
                    OpsConsole.WriteLine("WARNING: Parameter '{0}' was not found in shader.", constant.Name);
                    continue;
                }

                ConstantDescription[] cds = constantTable.GetConstantDescription(h, 1);
                ConstantDescription   cd  = cds[0];
                switch (cd.Class)
                {
                case ParameterClass.Object:
                {    //texture
                    switch (cd.ParameterType)
                    {
                    case ParameterType.Texture:
                    case ParameterType.Texture1D:
                    case ParameterType.Texture2D:
                    case ParameterType.Texture3D:
                    case ParameterType.TextureCube:
                    {
                        OpsTexture container = context.GetTexture(constant.Value);

                        int sampler = constantTable.GetSamplerIndex(h);
                        context.Device.SetTexture(sampler, container.Texture);
                    }
                    break;
                    }
                    break;
                }

                case ParameterClass.Scalar:
                case ParameterClass.Vector:
                case ParameterClass.MatrixColumns:
                case ParameterClass.MatrixRows:
                {
                    ArrayList floatList    = new ArrayList();
                    string[]  floatStrings = constant.Value.Split(new char[] { ' ', ',' });
                    foreach (string floatStr in floatStrings)
                    {
                        if (floatStr.Length > 0)
                        {
                            floatList.Add(float.Parse(floatStr));
                        }
                    }
                    float[] floats = (float[])floatList.ToArray(typeof(float));

                    constantTable.SetValue(context.Device, h, floats);
                }
                break;
                }
            }

            return(constantTable);
        }