Example #1
0
        static int Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Usage: ShaderDisassembler.exe <shaderFile> <outPutFile>");
                return(0);
            }

            try
            {
                SharpDX.D3DCompiler.ShaderBytecode sb = SharpDX.D3DCompiler.ShaderBytecode.FromFile(args[0]);

                string code = sb.Disassemble(
                    /* SharpDX.D3DCompiler.DisassemblyFlags.EnableColorCode */
                    SharpDX.D3DCompiler.DisassemblyFlags.EnableDefaultValuePrints
                    /*| SharpDX.D3DCompiler.DisassemblyFlags.EnableInstructionCycle // this will cause an error, lol */
                    | SharpDX.D3DCompiler.DisassemblyFlags.EnableInstructionNumbering
                    );

                System.IO.StreamWriter sw = new System.IO.StreamWriter(args[1]);

                sw.Write(code);

                sw.Close();

                return(0);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine(e.ToString());

                return(1);
            }
        }
Example #2
0
        private D3D11.ComputeShader BuildCompute(string fileName, string methodName)
        {
            string errors      = null;
            var    shaderFlags = SharpDX.D3DCompiler.ShaderFlags.None;

#if DEBUG
            shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
#endif

            SharpDX.D3DCompiler.ShaderBytecode compiledShader = null;
            try
            {
                //var v = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile("FX/compute.fx", "VectorAdd", "cs_5_0");

                compiledShader = SharpDX.D3DCompiler.ShaderBytecode.CompileFromFile(
                    fileName,
                    methodName,
                    "cs_5_0");

                return(new D3D11.ComputeShader(Device, compiledShader));
            }
            catch (Exception ex)
            {
                if (!string.IsNullOrEmpty(errors))
                {
                    throw new Exception(errors);
                }
                throw ex;
            }
            finally
            {
                Util.ReleaseCom(ref compiledShader);
            }
        }
Example #3
0
        public static double[] Add(string objPath, double[] left, double[] right)
        {
            using (var fileStream = File.Create("AddArray.dxil"))
            {
                ShaderCompilation.CompileFromFile("AddArray.hlsl", "CSMain", "cs_5_0").Bytecode.Save(fileStream);
            }

            objPath = "AddArray.dxil";

            using (var fileStream = File.OpenRead(objPath))
            {
                AddArrayShaderCode = new byte[fileStream.Length];
                fileStream.Read(AddArrayShaderCode, 0, AddArrayShaderCode.Length);
            }

            device           = new Device(null, FeatureLevel.Level_11_0);
            fence            = device.CreateFence(0, FenceFlags.None);
            commandQueue     = device.CreateCommandQueue(CommandListType.Compute);
            commandAllocator = device.CreateCommandAllocator(CommandListType.Compute);
            commandList      = device.CreateCommandList(0, CommandListType.Compute, commandAllocator, null);

            currentFence = 0;
            fenceEvent   = new AutoResetEvent(false);

            var gpuResult = AddGpu(left, right);

            return(gpuResult);
        }
Example #4
0
        //FOR TESTING
        public bool CreateShaderGraphAsset(string name, SharpDX.D3DCompiler.ShaderBytecode bytecode)
        {
            BaseAsset asset = new ShaderAsset()
            {
                Name       = name,
                ShaderType = ShaderTypeEnum.Pixel,
                Bytecode   = bytecode,
            };

            return(FSWorker.CreateAssetFile(asset, true));
        }
Example #5
0
 private Shader( IGraphicsDevice graphicsDevice, SharpDX.D3DCompiler.ShaderBytecode function, Graphics.ShaderType shaderType )
 {
     ShaderType = shaderType;
     this.bytecode = function;
     switch ( ShaderType )
     {
         case Graphics.ShaderType.VertexShader:
             Handle = new SharpDX.Direct3D11.VertexShader ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, function );
             break;
         case Graphics.ShaderType.PixelShader:
             Handle = new SharpDX.Direct3D11.PixelShader ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, function );
             break;
         case Graphics.ShaderType.GeometryShader:
             Handle = new SharpDX.Direct3D11.GeometryShader ( graphicsDevice.Handle as SharpDX.Direct3D11.Device, function );
             break;
     }
 }
Example #6
0
        public Effect(SharpDX.Direct3D11.Device Device, string ShaderCode)
        {
            // Load vertex shader
            SharpDX.D3DCompiler.CompilationResult result = SharpDX.D3DCompiler.ShaderBytecode.Compile(ShaderCode, VertexShaderEntrypoint, "vs_4_0");
            SharpDX.D3DCompiler.ShaderBytecode    vsb    = result.Bytecode;
            VertexShader = new VertexShader(Device, vsb);

            // Load pixel shader
            result = SharpDX.D3DCompiler.ShaderBytecode.Compile(ShaderCode, PixelShaderEntrypoint, "ps_4_0");
            SharpDX.D3DCompiler.ShaderBytecode psb = result.Bytecode;
            PixelShader = new PixelShader(Device, psb);
            psb.Dispose();

            // Create constant buffer
            ConstantBuffer = new SharpDX.Direct3D11.Buffer(Device, Utilities.SizeOf <ConstantBufferData>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0);

            // Create input layout. This tells the input-assembler stage how to map items from our vertex structures into vertices for the vertex shader.
            // It is validated against the vertex shader bytecode because it needs to match properly.
            InputLayout = new InputLayout(Device, vsb, new Vertex().InputElements);
            vsb.Dispose();
        }
Example #7
0
        private void BuildFX()
        {
            SharpDX.D3DCompiler.ShaderBytecode compiledShader = null;
            try
            {
                compiledShader = new SharpDX.D3DCompiler.ShaderBytecode(System.IO.File.ReadAllBytes("fx/lighting.fxo"));
                _fx            = new D3D11.Effect(Device, compiledShader);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            finally
            {
                Util.ReleaseCom(ref compiledShader);
            }

            _tech                     = _fx.GetTechniqueByName("LightTech");
            _fxWVP                    = _fx.GetVariableByName("gWorldViewProj").AsMatrix();
            _fxWorld                  = _fx.GetVariableByName("gWorld").AsMatrix();
            _fxWIT                    = _fx.GetVariableByName("gWorldInvTranspose").AsMatrix();
            _fxEyePosW                = _fx.GetVariableByName("gEyePosW").AsVector();
            _fxDirLight               = _fx.GetVariableByName("gDirLight");
            _fxPointLight             = _fx.GetVariableByName("gPointLight");
            _fxSpotLight              = _fx.GetVariableByName("gSpotLight");
            _fxMaterial               = _fx.GetVariableByName("gMaterial");
            _fxDiffuseMap             = _fx.GetVariableByName("gDiffuseMap").AsShaderResource();
            _fxRefractiveMap          = _fx.GetVariableByName("gRefractiveMap").AsShaderResource();
            _fxClipPlane              = _fx.GetVariableByName("gClipPlane").AsVector();
            _fxReflectViewProj        = _fx.GetVariableByName("gReflectViewProj").AsMatrix();
            _fxReflectiveMap          = _fx.GetVariableByName("gReflectiveMap").AsShaderResource();
            _fxgRefractionPositionMap = _fx.GetVariableByName("gRefractionPositionMap").AsShaderResource();
            _fxUseStructBuf           = _fx.GetVariableByName("gUseStructBuf").AsScalar();
            _fxSolutionSR             = _fx.GetVariableByName("gSolution").AsShaderResource();
        }
        public static ShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List <ConstantBufferData> cbuffers, int sharedIndex, Dictionary <string, SamplerStateInfo> samplerStates, bool debug)
        {
            var dxshader = new ShaderData();

            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex    = sharedIndex;
            dxshader.Bytecode       = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
            {
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;
            }

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        //
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List <Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            var samplerName = rdesc.Name;

                            var sampler = new Sampler
                            {
                                textureSlot   = rdesc.BindPoint,
                                samplerSlot   = rdesc.BindPoint,
                                parameterName = samplerName
                            };

                            SamplerStateInfo state;
                            if (samplerStates.TryGetValue(samplerName, out state))
                            {
                                sampler.parameterName = state.TextureName ?? samplerName;
                                sampler.state         = state.State;
                            }
                            else
                            {
                                foreach (var s in samplerStates.Values)
                                {
                                    if (samplerName == s.TextureName)
                                    {
                                        sampler.state = s.State;
                                        samplerName   = s.Name;
                                        break;
                                    }
                                }
                            }

                            // Find sampler slot, which can be different from the texture slot.
                            for (int j = 0; j < refelect.Description.BoundResources; j++)
                            {
                                var samplerrdesc = refelect.GetResourceBindingDescription(j);

                                if (samplerrdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Sampler &&
                                    samplerrdesc.Name == samplerName)
                                {
                                    sampler.samplerSlot = samplerrdesc.BindPoint;
                                    break;
                                }
                            }

                            switch (rdesc.Dimension)
                            {
                            case ShaderResourceViewDimension.Texture1D:
                            case ShaderResourceViewDimension.Texture1DArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D;
                                break;

                            case ShaderResourceViewDimension.Texture2D:
                            case ShaderResourceViewDimension.Texture2DArray:
                            case ShaderResourceViewDimension.Texture2DMultisampled:
                            case ShaderResourceViewDimension.Texture2DMultisampledArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D;
                                break;

                            case ShaderResourceViewDimension.Texture3D:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME;
                                break;

                            case ShaderResourceViewDimension.TextureCube:
                            case ShaderResourceViewDimension.TextureCubeArray:
                                sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE;
                                break;
                            }

                            samplers.Add(sampler);
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new ConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return(dxshader);
        }
Example #9
0
        /// <inheritdoc/>
        public ShaderBytecode StripReflection(ShaderBytecode shaderBytecode)
        {
            var bytecode = new SharpDX.D3DCompiler.ShaderBytecode(shaderBytecode).Strip(SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData);

            return(new ShaderBytecode(bytecode));
        }
Example #10
0
        public static ShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List<ConstantBufferData> cbuffers, int sharedIndex, Dictionary<string, SamplerStateInfo> samplerStates, bool debug)
        {
            var dxshader = new ShaderData();
            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex = sharedIndex;
            dxshader.Bytecode = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        // 
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List<Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            var samplerName = rdesc.Name;

                            var sampler = new Sampler
                            {
                                textureSlot = rdesc.BindPoint,
                                samplerSlot = rdesc.BindPoint,
                                parameterName = samplerName
                            };
                            
                            SamplerStateInfo state;
                            if (samplerStates.TryGetValue(samplerName, out state))
                            {
                                sampler.parameterName = state.TextureName ?? samplerName;
                                sampler.state = state.State;
                            }
                            else
                            {
                                foreach (var s in samplerStates.Values)
                                {
                                    if (samplerName == s.TextureName)
                                    {
                                        sampler.state = s.State;
                                        samplerName = s.Name;
                                        break;
                                    }
                                }
                            }

                            // Find sampler slot, which can be different from the texture slot.
                            for (int j = 0; j < refelect.Description.BoundResources; j++)
                            {
                                var samplerrdesc = refelect.GetResourceBindingDescription(j);

                                if (samplerrdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Sampler && 
                                    samplerrdesc.Name == samplerName)
                                {
                                    sampler.samplerSlot = samplerrdesc.BindPoint;
                                    break;
                                }
                            }

                            switch (rdesc.Dimension)
                            {
                                case ShaderResourceViewDimension.Texture1D:
                                case ShaderResourceViewDimension.Texture1DArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_1D;
                                    break;

                                case ShaderResourceViewDimension.Texture2D:
                                case ShaderResourceViewDimension.Texture2DArray:
                                case ShaderResourceViewDimension.Texture2DMultisampled:
                                case ShaderResourceViewDimension.Texture2DMultisampledArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D;
                                    break;

                                case ShaderResourceViewDimension.Texture3D:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_VOLUME;
                                    break;

                                case ShaderResourceViewDimension.TextureCube:
                                case ShaderResourceViewDimension.TextureCubeArray:
                                    sampler.type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_CUBE;
                                    break;
                            }

                            samplers.Add(sampler);
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new ConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return dxshader;
        }
Example #11
0
 /// <inheritdoc/>
 public ShaderBytecode StripReflection(ShaderBytecode shaderBytecode)
 {
     var bytecode = new SharpDX.D3DCompiler.ShaderBytecode(shaderBytecode).Strip(SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData);
     return new ShaderBytecode(bytecode);
 }
        public static DXShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List <DXConstantBufferData> cbuffers, int sharedIndex, bool debug)
        {
            var dxshader = new DXShaderData();

            dxshader.IsVertexShader = isVertexShader;
            dxshader.SharedIndex    = sharedIndex;
            dxshader.Bytecode       = (byte[])byteCode.Clone();

            // Strip the bytecode we're gonna save!
            var stripFlags = SharpDX.D3DCompiler.StripFlags.CompilerStripReflectionData |
                             SharpDX.D3DCompiler.StripFlags.CompilerStripTestBlobs;

            if (!debug)
            {
                stripFlags |= SharpDX.D3DCompiler.StripFlags.CompilerStripDebugInformation;
            }

            using (var original = new SharpDX.D3DCompiler.ShaderBytecode(byteCode))
            {
                // Strip the bytecode for saving to disk.
                var stripped = original.Strip(stripFlags);
                {
                    // Only SM4 and above works with strip... so this can return null!
                    if (stripped != null)
                    {
                        dxshader.ShaderCode = stripped;
                    }
                    else
                    {
                        // TODO: There is a way to strip SM3 and below
                        // but we have to write the method ourselves.
                        //
                        // If we need to support it then consider porting
                        // this code over...
                        //
                        // http://entland.homelinux.com/blog/2009/01/15/stripping-comments-from-shader-bytecodes/
                        //
                        dxshader.ShaderCode = (byte[])dxshader.Bytecode.Clone();
                    }
                }

                // Use reflection to get details of the shader.
                using (var refelect = new SharpDX.D3DCompiler.ShaderReflection(byteCode))
                {
                    // Get the samplers.
                    var samplers = new List <Sampler>();
                    for (var i = 0; i < refelect.Description.BoundResources; i++)
                    {
                        var rdesc = refelect.GetResourceBindingDescription(i);
                        if (rdesc.Type == SharpDX.D3DCompiler.ShaderInputType.Texture)
                        {
                            samplers.Add(new Sampler
                            {
                                index         = rdesc.BindPoint,
                                parameterName = rdesc.Name,

                                // TODO: Detect the sampler type for realz.
                                type = MojoShader.MOJOSHADER_samplerType.MOJOSHADER_SAMPLER_2D
                            });
                        }
                    }
                    dxshader._samplers = samplers.ToArray();

                    // Gather all the constant buffers used by this shader.
                    dxshader._cbuffers = new int[refelect.Description.ConstantBuffers];
                    for (var i = 0; i < refelect.Description.ConstantBuffers; i++)
                    {
                        var cb = new DXConstantBufferData(refelect.GetConstantBuffer(i));

                        // Look for a duplicate cbuffer in the list.
                        for (var c = 0; c < cbuffers.Count; c++)
                        {
                            if (cb.SameAs(cbuffers[c]))
                            {
                                cb = null;
                                dxshader._cbuffers[i] = c;
                                break;
                            }
                        }

                        // Add a new cbuffer.
                        if (cb != null)
                        {
                            dxshader._cbuffers[i] = cbuffers.Count;
                            cbuffers.Add(cb);
                        }
                    }
                }
            }

            return(dxshader);
        }