Exemplo n.º 1
0
 /// <summary>
 /// Creates an effect compiler from a stream containing an ASCII effect description .
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="defines">The defines.</param>
 /// <param name="includeFile">The include file.</param>
 /// <param name="flags">The flags.</param>
 /// <returns>An instance of <see cref="EffectCompiler"/></returns>
 /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged>
 public static EffectCompiler FromStream(Stream stream, Macro[] defines, Include includeFile, ShaderFlags flags)
 {
     if (stream is DataStream)
     {
         var compiler = new EffectCompiler(IntPtr.Zero);
         CreateEffectCompiler(((DataStream)stream).PositionPointer, (int)(stream.Length - stream.Length), defines, includeFile, flags, compiler);
         return(compiler);
     }
     return(FromMemory(Utilities.ReadStream(stream), defines, includeFile, flags));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an effect compiler from a memory buffer containing an ASCII effect description .
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="defines">The defines.</param>
        /// <param name="includeFile">The include file.</param>
        /// <param name="flags">The flags.</param>
        /// <returns>
        /// An instance of <see cref="EffectCompiler"/>
        /// </returns>
        /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged>
        public static EffectCompiler FromMemory(byte[] data, Macro[] defines, Include includeFile, ShaderFlags flags)
        {
            unsafe
            {
                var compiler = new EffectCompiler(IntPtr.Zero);

                fixed(void *pData = data)
                CreateEffectCompiler((IntPtr)pData, data.Length, defines, includeFile, flags, compiler);

                return(compiler);
            }
        }
Exemplo n.º 3
0
 private static void CreateEffectCompiler(IntPtr data, int length, Macro[] defines, Include includeFile, ShaderFlags flags, EffectCompiler instance)
 {
     Blob blobForErrors = null;
     try
     {
         D3DX9.CreateEffectCompiler(data, length, defines, IncludeShadow.ToIntPtr(includeFile), (int)flags, instance, out blobForErrors);
     }
     catch (SharpDXException ex)
     {
         if (blobForErrors != null)
             throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors));
         throw;
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Creates an effect compiler from a stream containing an ASCII effect description .
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="defines">The defines.</param>
 /// <param name="includeFile">The include file.</param>
 /// <param name="flags">The flags.</param>
 /// <returns>An instance of <see cref="EffectCompiler"/></returns>
 /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged>
 public static EffectCompiler FromStream(Stream stream, Macro[] defines, Include includeFile, ShaderFlags flags)
 {
     if (stream is DataStream)
     {
         var compiler = new EffectCompiler(IntPtr.Zero);
         CreateEffectCompiler(((DataStream)stream).PositionPointer, (int)(stream.Length - stream.Position), defines, includeFile, flags, compiler);
         return compiler;
     } 
     return FromMemory(Utilities.ReadStream(stream), defines, includeFile, flags);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Creates an effect compiler from a memory buffer containing an ASCII effect description .
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="defines">The defines.</param>
 /// <param name="includeFile">The include file.</param>
 /// <param name="flags">The flags.</param>
 /// <returns>
 /// An instance of <see cref="EffectCompiler"/>
 /// </returns>
 /// <unmanaged>HRESULT D3DXCreateEffectCompiler([In] const void* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[Out, Fast] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)</unmanaged>
 public static EffectCompiler FromMemory(byte[] data, Macro[] defines, Include includeFile, ShaderFlags flags)
 {
     unsafe
     {
         var compiler = new EffectCompiler(IntPtr.Zero);
         fixed (void* pData = data)
             CreateEffectCompiler((IntPtr)pData, data.Length, defines, includeFile, flags, compiler);
         return compiler;
     }
 }
Exemplo n.º 6
0
		protected override void LoadFromSource()
		{
			// Populate preprocessor defines
			var defines = new List<D3D9.Macro>();
			if ( !string.IsNullOrEmpty( PreprocessorDefines ) )
			{
				var tmp = PreprocessorDefines.Split( ' ', ',', ';' );
				foreach ( string define in tmp )
				{
					var macro = new D3D9.Macro();
					if ( define.Contains( "=" ) )
					{
						var split = define.Split( '=' );
						macro.Name = split[ 0 ];
						macro.Definition = split[ 1 ];
					}
					else
					{
						macro.Name = define;
						macro.Definition = "1";
					}

					if ( !string.IsNullOrEmpty( macro.Name ) )
					{
						defines.Add( macro );
					}
				}
			}

			// Populate compile flags
			var compileFlags = UseColumnMajorMatrices
			                   	? D3D9.ShaderFlags.PackMatrixColumnMajor
			                   	: D3D9.ShaderFlags.PackMatrixRowMajor;

#if DEBUG
			compileFlags |= D3D9.ShaderFlags.Debug;
#endif
			switch ( OptimizationLevel )
			{
				case OptimizationLevel.Default:
					compileFlags |= D3D9.ShaderFlags.OptimizationLevel1;
					break;

				case OptimizationLevel.None:
					compileFlags |= D3D9.ShaderFlags.SkipOptimization;
					break;

				case OptimizationLevel.LevelZero:
					compileFlags |= D3D9.ShaderFlags.OptimizationLevel0;
					break;

				case OptimizationLevel.LevelOne:
					compileFlags |= D3D9.ShaderFlags.OptimizationLevel1;
					break;

				case OptimizationLevel.LevelTwo:
					compileFlags |= D3D9.ShaderFlags.OptimizationLevel2;
					break;

				case OptimizationLevel.LevelThree:
					compileFlags |= D3D9.ShaderFlags.OptimizationLevel3;
					break;
			}

			var parseFlags = compileFlags;
			compileFlags ^= UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor;

			// include handler
			var includeHandler = new HLSLIncludeHandler( this );

			// Compile & assemble into microcode
			var effectCompiler = new D3D9.EffectCompiler( Source, defines.ToArray(), includeHandler, parseFlags );
			var effectHandle = new D3D9.EffectHandle( EntryPoint );
			var errors = string.Empty;

			try
			{
				MicroCode = effectCompiler.CompileShader( effectHandle, Target, compileFlags, out this.constTable );
			}
			catch ( DX.SharpDXException ex )
			{
				if ( ex is DX.CompilationException )
				{
					errors = ex.Message;
				}

				// check for errors
				if ( !string.IsNullOrEmpty( errors ) )
				{
					if ( MicroCode != null )
					{
						if ( LogManager.Instance != null )
						{
							LogManager.Instance.Write( "HLSL: Warnings while compiling high level shader {0}:\n{1}", Name, errors );
						}
					}
					else
					{
						throw new AxiomException( "HLSL: Unable to compile high level shader {0}:\n{1}", Name, errors );
					}
				}
			}
			finally
			{
				effectCompiler.Dispose();
				effectHandle.Dispose();
				includeHandler.Dispose();
			}
		}
Exemplo n.º 7
0
        private static void CreateEffectCompiler(IntPtr data, int length, Macro[] defines, Include includeFile, ShaderFlags flags, EffectCompiler instance)
        {
            Blob blobForErrors = null;

            try
            {
                D3DX9.CreateEffectCompiler(data, length, defines, IncludeShadow.ToIntPtr(includeFile), (int)flags, instance, out blobForErrors);
            }
            catch (SharpDXException ex)
            {
                if (blobForErrors != null)
                {
                    throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors));
                }
                throw;
            }
        }
Exemplo n.º 8
0
        protected override void LoadFromSource()
        {
            // Populate preprocessor defines
            var defines = new List <D3D9.Macro>();

            if (!string.IsNullOrEmpty(PreprocessorDefines))
            {
                var tmp = PreprocessorDefines.Split(' ', ',', ';');
                foreach (string define in tmp)
                {
                    var macro = new D3D9.Macro();
                    if (define.Contains("="))
                    {
                        var split = define.Split('=');
                        macro.Name       = split[0];
                        macro.Definition = split[1];
                    }
                    else
                    {
                        macro.Name       = define;
                        macro.Definition = "1";
                    }

                    if (!string.IsNullOrEmpty(macro.Name))
                    {
                        defines.Add(macro);
                    }
                }
            }

            // Populate compile flags
            var compileFlags = UseColumnMajorMatrices
                                                ? D3D9.ShaderFlags.PackMatrixColumnMajor
                                                : D3D9.ShaderFlags.PackMatrixRowMajor;

#if DEBUG
            compileFlags |= D3D9.ShaderFlags.Debug;
#endif
            switch (OptimizationLevel)
            {
            case OptimizationLevel.Default:
                compileFlags |= D3D9.ShaderFlags.OptimizationLevel1;
                break;

            case OptimizationLevel.None:
                compileFlags |= D3D9.ShaderFlags.SkipOptimization;
                break;

            case OptimizationLevel.LevelZero:
                compileFlags |= D3D9.ShaderFlags.OptimizationLevel0;
                break;

            case OptimizationLevel.LevelOne:
                compileFlags |= D3D9.ShaderFlags.OptimizationLevel1;
                break;

            case OptimizationLevel.LevelTwo:
                compileFlags |= D3D9.ShaderFlags.OptimizationLevel2;
                break;

            case OptimizationLevel.LevelThree:
                compileFlags |= D3D9.ShaderFlags.OptimizationLevel3;
                break;
            }

            var parseFlags = compileFlags;
            compileFlags ^= UseColumnMajorMatrices ? D3D9.ShaderFlags.PackMatrixColumnMajor : D3D9.ShaderFlags.PackMatrixRowMajor;

            // include handler
            var includeHandler = new HLSLIncludeHandler(this);

            // Compile & assemble into microcode
            var effectCompiler = new D3D9.EffectCompiler(Source, defines.ToArray(), includeHandler, parseFlags);
            var effectHandle   = new D3D9.EffectHandle(EntryPoint);
            var errors         = string.Empty;

            try
            {
                MicroCode = effectCompiler.CompileShader(effectHandle, Target, compileFlags, out this.constTable);
            }
            catch (DX.SharpDXException ex)
            {
                if (ex is DX.CompilationException)
                {
                    errors = ex.Message;
                }

                // check for errors
                if (!string.IsNullOrEmpty(errors))
                {
                    if (MicroCode != null)
                    {
                        if (LogManager.Instance != null)
                        {
                            LogManager.Instance.Write("HLSL: Warnings while compiling high level shader {0}:\n{1}", Name, errors);
                        }
                    }
                    else
                    {
                        throw new AxiomException("HLSL: Unable to compile high level shader {0}:\n{1}", Name, errors);
                    }
                }
            }
            finally
            {
                effectCompiler.Dispose();
                effectHandle.Dispose();
                includeHandler.Dispose();
            }
        }