public static DXShaderData CreateGLSL (byte[] byteCode, List<DXConstantBufferData> cbuffers, int sharedIndex, Dictionary<string, SamplerStateInfo> samplerStates)
		{
			var dxshader = new DXShaderData ();
			dxshader.SharedIndex = sharedIndex;
			dxshader.Bytecode = (byte[])byteCode.Clone ();

			// Use MojoShader to convert the HLSL bytecode to GLSL.

			var parseDataPtr = MojoShader.NativeMethods.MOJOSHADER_parse (
				"glsl",
				byteCode,
				byteCode.Length,
				IntPtr.Zero,
				0,
				IntPtr.Zero,
				0,
				IntPtr.Zero,
				IntPtr.Zero,
				IntPtr.Zero);

			var parseData = DXHelper.Unmarshal<MojoShader.MOJOSHADER_parseData> (parseDataPtr);
			if (parseData.error_count > 0) {
				var errors = DXHelper.UnmarshalArray<MojoShader.MOJOSHADER_error> (
					parseData.errors,
					parseData.error_count
				);
				throw new Exception (errors [0].error);
			}

			switch (parseData.shader_type) {
			case MojoShader.MOJOSHADER_shaderType.MOJOSHADER_TYPE_PIXEL:
				dxshader.IsVertexShader = false;
				break;
			case MojoShader.MOJOSHADER_shaderType.MOJOSHADER_TYPE_VERTEX:
				dxshader.IsVertexShader = true;
				break;
			default:
				throw new NotSupportedException ();
			}
	

			// Conver the attributes.
			//
			// TODO: Could this be done using DX shader reflection?
			//
			{
				var attributes = DXHelper.UnmarshalArray<MojoShader.MOJOSHADER_attribute> (
						parseData.attributes, parseData.attribute_count);

				dxshader._attributes = new Attribute[attributes.Length];
				for (var i = 0; i < attributes.Length; i++) {
					dxshader._attributes [i].name = attributes [i].name;
					dxshader._attributes [i].index = attributes [i].index;
					dxshader._attributes [i].usage = DXEffectObject.ToXNAVertexElementUsage (attributes [i].usage);
				}
			}

			var symbols = DXHelper.UnmarshalArray<MojoShader.MOJOSHADER_symbol> (
					parseData.symbols, parseData.symbol_count);

			//try to put the symbols in the order they are eventually packed into the uniform arrays
			//this /should/ be done by pulling the info from mojoshader
			Array.Sort (symbols, delegate(MojoShader.MOJOSHADER_symbol a, MojoShader.MOJOSHADER_symbol b) {
				uint va = a.register_index;
				if (a.info.elements == 1)
					va += 1024; //hax. mojoshader puts array objects first
				uint vb = b.register_index;
				if (b.info.elements == 1)
					vb += 1024;
				return va.CompareTo (vb);
			}
			);//(a, b) => ((int)(a.info.elements > 1))a.register_index.CompareTo(b.register_index));

            // NOTE: It seems the latest versions of MojoShader only 
            // output vec4 register sets.  We leave the code below, but
            // the runtime has been optimized for this case.

			// For whatever reason the register indexing is 
			// incorrect from MojoShader.
			{
				uint bool_index = 0;
				uint float4_index = 0;
				uint int4_index = 0;

				for (var i = 0; i < symbols.Length; i++) {
					switch (symbols [i].register_set) {
					case MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_BOOL:
						symbols [i].register_index = bool_index;
						bool_index += symbols [i].register_count;
						break;

					case MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_FLOAT4:
						symbols [i].register_index = float4_index;
						float4_index += symbols[i].register_count;
						break;

					case MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_INT4:
						symbols [i].register_index = int4_index;
						int4_index += symbols [i].register_count;
						break;
					}
				}
			}

			// Get the samplers.
			var samplers = DXHelper.UnmarshalArray<MojoShader.MOJOSHADER_sampler> (
					parseData.samplers, parseData.sampler_count);
			dxshader._samplers = new Sampler[samplers.Length];
			for (var i = 0; i < samplers.Length; i++) 
            {
                // We need the original sampler name... look for that in the symbols.
                var originalSamplerName =
                    symbols.First(e => e.register_set == MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_SAMPLER &&
                    e.register_index == samplers[i].index
                ).name;

                var sampler = new Sampler
                {
                    //sampler mapping to parameter is unknown atm
                    parameter = -1,
                                      
                    // GLSL needs the MojoShader mangled sampler name.
                    samplerName = samplers[i].name,

                    // By default use the original sampler name for the parameter name.
                    parameterName = originalSamplerName,

                    textureSlot = samplers[i].index,
                    samplerSlot = samplers[i].index,
                    type = samplers[i].type,
                };

                SamplerStateInfo state;
                if (samplerStates.TryGetValue(originalSamplerName, out state))
                {
                    sampler.state = state.State;
                    sampler.parameterName = state.TextureName ?? originalSamplerName;
                }

                // Store the sampler.
			    dxshader._samplers[i] = sampler;
			}

			// Gather all the parameters used by this shader.
			var symbol_types = new [] { 
				new { name = dxshader.IsVertexShader ? "vs_uniforms_bool" : "ps_uniforms_bool", set = MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_BOOL, },
				new { name = dxshader.IsVertexShader ? "vs_uniforms_ivec4" : "ps_uniforms_ivec4", set = MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_INT4, },
				new { name = dxshader.IsVertexShader ? "vs_uniforms_vec4" : "ps_uniforms_vec4", set = MojoShader.MOJOSHADER_symbolRegisterSet.MOJOSHADER_SYMREGSET_FLOAT4, },
			};

			var cbuffer_index = new List<int> ();
			for (var i = 0; i < symbol_types.Length; i++) {
				var cbuffer = new DXConstantBufferData (symbol_types [i].name,
													   symbol_types [i].set,
													   symbols);
				if (cbuffer.Size == 0)
					continue;

				var match = cbuffers.FindIndex (e => e.SameAs (cbuffer));
				if (match == -1) {
					cbuffer_index.Add (cbuffers.Count);
					cbuffers.Add (cbuffer);
				} else
					cbuffer_index.Add (match);
			}
			dxshader._cbuffers = cbuffer_index.ToArray ();

			var glslCode = parseData.output;

#if GLSLOPTIMIZER
			//glslCode = GLSLOptimizer.Optimize(glslCode, ShaderType);
#endif

			// TODO: This sort of sucks... why does MojoShader not produce
			// code valid for GLES out of the box?

			// GLES platforms do not like this.
			glslCode = glslCode.Replace ("#version 110", "");

			// Add the required precision specifiers for GLES.

            var floatPrecision = dxshader.IsVertexShader ? "precision highp float;\r\n" : "precision mediump float;\r\n";

			glslCode = "#ifdef GL_ES\r\n" +
                 floatPrecision +
				"precision mediump int;\r\n" +
				"#endif\r\n" +
				glslCode;

			// Store the code for serialization.
			dxshader.ShaderCode = Encoding.ASCII.GetBytes (glslCode);

			return dxshader;
		}
Exemplo n.º 2
0
        public static DXShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List<DXConstantBufferData> cbuffers, int sharedIndex, Dictionary<string, SamplerStateInfo> samplerStates, 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)
                        {
                            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 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;
        }
Exemplo n.º 3
0
        public static DXShaderData CreateHLSL(byte[] byteCode, bool isVertexShader, List <DXConstantBufferData> cbuffers, int sharedIndex, Dictionary <string, SamplerStateInfo> samplerStates, 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)
                        {
                            var sampler = new Sampler {
                                index = rdesc.BindPoint, parameterName = rdesc.Name
                            };

                            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;
                            }

                            SamplerStateInfo state;
                            if (samplerStates.TryGetValue(rdesc.Name, out state))
                            {
                                sampler.parameterName = state.textureName ?? rdesc.Name;
                                sampler.state         = state.state;
                            }

                            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 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);
        }
Exemplo n.º 4
0
        private d3dx_state CreateShader(TwoMGFX.ShaderInfo shaderInfo, string shaderFunction, string shaderProfile, bool isVertexShader)
        {
            // Compile the shader.
            SharpDX.D3DCompiler.ShaderBytecode shaderByteCode;
            try
            {
                SharpDX.D3DCompiler.ShaderFlags shaderFlags = 0;

                // While we never allow preshaders, this flag is invalid for
                // the DX11 shader compiler which doesn't allow preshaders
                // in the first place.
                //shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.NoPreshader;

                if (shaderInfo.DX11Profile)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.EnableBackwardsCompatibility;
                }

                if (shaderInfo.Debug)
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.SkipOptimization;
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.Debug;
                }
                else
                {
                    shaderFlags |= SharpDX.D3DCompiler.ShaderFlags.OptimizationLevel3;
                }

                // Compile the shader into bytecode.
                var result = SharpDX.D3DCompiler.ShaderBytecode.Compile(
                    shaderInfo.fileContent,
                    shaderFunction,
                    shaderProfile,
                    shaderFlags,
                    0,
                    null,
                    null,
                    shaderInfo.fileName);

                if (result.HasErrors)
                {
                    throw new Exception(result.Message);
                }

                shaderByteCode = result.Bytecode;
                //var source = shaderByteCode.Disassemble();
            }
            catch (Exception ex)
            {
                throw ex;
            }

            // Get a copy of the shader bytecode.
            var bytecode = shaderByteCode.Data.ToArray();

            // First look to see if we already created this same shader.
            DXShaderData dxShader = null;

            foreach (var shader in Shaders)
            {
                if (bytecode.SequenceEqual(shader.Bytecode))
                {
                    dxShader = shader;
                    break;
                }
            }

            // Create a new shader.
            if (dxShader == null)
            {
                if (shaderInfo.DX11Profile)
                {
                    dxShader = DXShaderData.CreateHLSL(bytecode, isVertexShader, ConstantBuffers, Shaders.Count, shaderInfo.Debug);
                }
                else
                {
                    dxShader = DXShaderData.CreateGLSL(bytecode, ConstantBuffers, Shaders.Count);
                }

                Shaders.Add(dxShader);
            }

            //var assmbly = desc.Bytecode.Disassemble();
            //var buffer = reflection.GetConstantBuffer(0);

            var state = new d3dx_state();

            state.index     = 0;
            state.type      = STATE_TYPE.CONSTANT;
            state.operation = isVertexShader ? (uint)146 : (uint)147;

            state.parameter          = new d3dx_parameter();
            state.parameter.name     = string.Empty;
            state.parameter.semantic = string.Empty;
            state.parameter.class_   = D3DXPARAMETER_CLASS.OBJECT;
            state.parameter.type     = isVertexShader ? D3DXPARAMETER_TYPE.VERTEXSHADER : D3DXPARAMETER_TYPE.PIXELSHADER;
            state.parameter.rows     = 0;
            state.parameter.columns  = 0;
            state.parameter.data     = dxShader.SharedIndex;

            return(state);
        }