// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.IO;
using System.Runtime.InteropServices;
using SharpDX.Direct3D;
namespace SharpDX.Direct3D9
{
public partial class EffectCompiler
{
///
/// Initializes a new instance of the class.
///
/// The data.
/// The defines.
/// The include file.
/// The flags.
/// HRESULT D3DXCreateEffectCompiler([In] const char* pSrcData,[In] unsigned int SrcDataLen,[In, Buffer] const D3DXMACRO* pDefines,[In] ID3DXInclude* pInclude,[In] unsigned int Flags,[In] ID3DXEffectCompiler** ppCompiler,[In] ID3DXBuffer** ppParseErrors)
public EffectCompiler(string data, Macro[] defines, Include includeFile, ShaderFlags flags) : base(IntPtr.Zero)
{
IntPtr dataPtr = Marshal.StringToHGlobalAnsi(data);
try
{
CreateEffectCompiler(dataPtr, data.Length, defines, includeFile, flags, this);
}
finally
{
Marshal.FreeHGlobal(dataPtr);
}
}
///
/// Compile an effect.
///
/// The flags.
/// If a compilation errors occurs
/// Buffer containing the compiled effect.
/// HRESULT ID3DXEffectCompiler::CompileEffect([In] unsigned int Flags,[In] ID3DXBuffer** ppEffect,[In] ID3DXBuffer** ppErrorMsgs)
public DataStream CompileEffect(ShaderFlags flags)
{
Blob effect;
Blob blobForErrors = null;
try
{
CompileEffect((int)flags, out effect, out blobForErrors);
}
catch (SharpDXException ex)
{
if (blobForErrors != null)
throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors));
throw;
}
return new DataStream(effect);
}
///
/// Compiles a shader from an effect that contains one or more functions.
///
/// The function handle.
/// The target.
/// The flags.
/// If a compilation errors occurs
/// The bytecode of the effect.
/// HRESULT ID3DXEffectCompiler::CompileShader([In] D3DXHANDLE hFunction,[In] const char* pTarget,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)
public ShaderBytecode CompileShader(EffectHandle functionHandle, string target, ShaderFlags flags)
{
ConstantTable constantTable;
var result = CompileShader(functionHandle, target, flags, out constantTable);
constantTable.Dispose();
return result;
}
///
/// Compiles a shader from an effect that contains one or more functions.
///
/// The function handle.
/// The target.
/// The flags.
/// The constant table.
/// If a compilation errors occurs
/// The bytecode of the effect.
/// HRESULT ID3DXEffectCompiler::CompileShader([In] D3DXHANDLE hFunction,[In] const char* pTarget,[In] unsigned int Flags,[In] ID3DXBuffer** ppShader,[In] ID3DXBuffer** ppErrorMsgs,[In] ID3DXConstantTable** ppConstantTable)
public ShaderBytecode CompileShader(EffectHandle functionHandle, string target, ShaderFlags flags, out ConstantTable constantTable)
{
Blob shaderBytecode;
Blob blobForErrors = null;
try
{
CompileShader(functionHandle, target, (int)flags, out shaderBytecode, out blobForErrors, out constantTable);
}
catch (SharpDXException ex)
{
if (blobForErrors != null)
throw new CompilationException(ex.ResultCode, Utilities.BlobToString(blobForErrors));
throw;
}
return new ShaderBytecode(shaderBytecode);
}
///
/// Creates an effect compiler from a file on disk containing an ASCII effect description .
///
/// Name of the file.
/// The flags.
///
/// An instance of
///
/// 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)
public static EffectCompiler FromFile(string fileName, ShaderFlags flags)
{
return new EffectCompiler(File.ReadAllText(fileName), null, null, flags);
}
///
/// Creates an effect compiler from a file on disk containing an ASCII effect description .
///
/// Name of the file.
/// The defines.
/// The include file.
/// The flags.
///
/// An instance of
///
/// 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)
public static EffectCompiler FromFile(string fileName, Macro[] defines, Include includeFile, ShaderFlags flags)
{
return new EffectCompiler(File.ReadAllText(fileName), defines, includeFile, flags);
}
///
/// Creates an effect compiler from a memory buffer containing an ASCII effect description .
///
/// The data.
/// The flags.
///
/// An instance of
///
/// 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)
public static EffectCompiler FromMemory(byte[] data, ShaderFlags flags)
{
return FromMemory(data, null, null, flags);
}
///
/// Creates an effect compiler from a memory buffer containing an ASCII effect description .
///
/// The data.
/// The defines.
/// The include file.
/// The flags.
///
/// An instance of
///
/// 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)
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;
}
}
///
/// Creates an effect compiler from a stream containing an ASCII effect description .
///
/// The stream.
/// The flags.
///
/// An instance of
///
/// 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)
public static EffectCompiler FromStream(Stream stream, ShaderFlags flags)
{
return FromStream(stream, null, null, flags);
}
///
/// Creates an effect compiler from a stream containing an ASCII effect description .
///
/// The stream.
/// The defines.
/// The include file.
/// The flags.
/// An instance of
/// 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)
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);
}
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;
}
}
}
}