Revision: 2200
Author: mike.popoloski
Date: Mon Mar 26 13:44:47 2012
Log: Precompiled shaders to reduce dependencies and start up costs.
http://code.google.com/p/slimdx/source/detail?r=2200
Added:
/branches/lite/SlimDX.Toolkit/Shaders
/branches/lite/SlimDX.Toolkit/Shaders/ClippingFontEffect.fx
/branches/lite/SlimDX.Toolkit/Shaders/CompileShaders.bat
/branches/lite/SlimDX.Toolkit/Shaders/Compiled
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/ClippingFontEffect_PS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/ClippingFontEffect_VS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/SimpleFontEffect_PS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/SimpleFontEffect_VS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/SpriteEffect_PS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/SpriteEffect_VS.fxo
/branches/lite/SlimDX.Toolkit/Shaders/SimpleFontEffect.fx
/branches/lite/SlimDX.Toolkit/Shaders/SpriteEffect.fx
Deleted:
/branches/lite/SlimDX.Toolkit/Fonts/Internal/FontShaders.cs
/branches/lite/SlimDX.Toolkit/Sprites/SpriteShaders.cs
Modified:
/branches/lite/SlimDX.Toolkit/Fonts/Internal/RenderData.cs
/branches/lite/SlimDX.Toolkit/SlimDX.Toolkit.csproj
/branches/lite/SlimDX.Toolkit/Sprites/SpriteDeviceResources.cs
/branches/lite/source/d3dcompiler/ShaderBytecodeDC.cpp
/branches/lite/source/d3dcompiler/ShaderBytecodeDC.h
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/ClippingFontEffect.fx Mon Mar 26
13:44:47 2012
@@ -0,0 +1,54 @@
+cbuffer ShaderConstants : register(b0)
+{
+ row_major float4x4 TransformMatrix : packoffset(c0);
+ float4 ClipRect : packoffset(c4);
+};
+
+SamplerState sampler0 : register(s0);
+Texture2D<float> tex0 : register(t0);
+
+struct VSIn
+{
+ float4 Position : POSITION;
+ float4 GlyphColor : GLYPHCOLOR;
+};
+
+struct VSOut
+{
+ float4 Position : SV_Position;
+ float4 GlyphColor : COLOR;
+ float2 TexCoord : TEXCOORD;
+ float4 ClipDistance : CLIPDISTANCE;
+};
+
+VSOut VS(VSIn Input)
+{
+ VSOut Output;
+
+ Output.Position = mul(TransformMatrix, float4(Input.Position.xy, 0.0f,
1.0f));
+ Output.GlyphColor = Input.GlyphColor;
+ Output.TexCoord =
Input.Position.zw;
+ Output.ClipDistance = ClipRect + float4(Input.Position.xy,
-Input.Position.xy);
+
+ return Output;
+}
+
+struct PSIn
+{
+ float4 Position : SV_Position;
+ float4 GlyphColor : COLOR;
+ float2 TexCoord : TEXCOORD;
+ float4 ClipDistance : CLIPDISTANCE;
+};
+
+float4 PS(PSIn Input) : SV_Target
+{
+ clip(Input.ClipDistance);
+
+ float a = tex0.Sample(sampler0, Input.TexCoord);
+
+ if(a == 0.0f)
+ discard;
+
+ return (a * Input.GlyphColor.a) * float4(Input.GlyphColor.rgb, 1.0f);
+}
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/CompileShaders.bat Mon Mar 26
13:44:47 2012
@@ -0,0 +1,31 @@
+@echo off
+
+setlocal
+set error=0
+
+call :CompileShader SimpleFontEffect vs VS
+call :CompileShader SimpleFontEffect ps PS
+
+call :CompileShader ClippingFontEffect vs VS
+call :CompileShader ClippingFontEffect ps PS
+
+call :CompileShader SpriteEffect vs VS
+call :CompileShader SpriteEffect ps PS
+
+echo.
+
+if %error% == 0 (
+ echo Shaders compiled ok
+) else (
+ echo There were shader compilation errors!
+)
+
+endlocal
+exit /b
+
+:CompileShader
+set fxc="%DXSDK_DIR%/utilities/bin/x64/fxc" /nologo %1.fx
/T%2_4_0_level_9_1 /Zpc /Qstrip_reflect /Qstrip_debug /E%3
/FoCompiled\%1_%3.fxo
+echo.
+echo %fxc%
+%fxc% || set error=1
+exit /b
=======================================
--- /dev/null
+++
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/ClippingFontEffect_PS.fxo
Mon Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++
/branches/lite/SlimDX.Toolkit/Shaders/Compiled/ClippingFontEffect_VS.fxo
Mon Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/Compiled/SimpleFontEffect_PS.fxo
Mon Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/Compiled/SimpleFontEffect_VS.fxo
Mon Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/Compiled/SpriteEffect_PS.fxo Mon
Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/Compiled/SpriteEffect_VS.fxo Mon
Mar 26 13:44:47 2012
Binary file, no diff available.
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/SimpleFontEffect.fx Mon Mar 26
13:44:47 2012
@@ -0,0 +1,48 @@
+cbuffer ShaderConstants : register(b0)
+{
+ row_major float4x4 TransformMatrix : packoffset(c0);
+};
+
+SamplerState sampler0 : register(s0);
+Texture2D<float> tex0 : register(t0);
+
+struct VSIn
+{
+ float4 Position : POSITION;
+ float4 GlyphColor : GLYPHCOLOR;
+};
+
+struct VSOut
+{
+ float4 Position : SV_Position;
+ float4 GlyphColor : COLOR;
+ float2 TexCoord : TEXCOORD;
+};
+
+VSOut VS(VSIn Input)
+{
+ VSOut Output;
+
+ Output.Position = mul(TransformMatrix, float4(Input.Position.xy, 0.0f,
1.0f));
+ Output.GlyphColor = Input.GlyphColor;
+ Output.TexCoord =
Input.Position.zw;
+
+ return Output;
+}
+
+struct PSIn
+{
+ float4 Position : SV_Position;
+ float4 GlyphColor : COLOR;
+ float2 TexCoord : TEXCOORD;
+};
+
+float4 PS(PSIn Input) : SV_Target
+{
+ float a = tex0.Sample(sampler0, Input.TexCoord);
+
+ if(a == 0.0f)
+ discard;
+
+ return (a * Input.GlyphColor.a) * float4(Input.GlyphColor.rgb, 1.0f);
+}
=======================================
--- /dev/null
+++ /branches/lite/SlimDX.Toolkit/Shaders/SpriteEffect.fx Mon Mar 26
13:44:47 2012
@@ -0,0 +1,20 @@
+cbuffer Parameters : register(b0)
+{
+ row_major float4x4 MatrixTransform;
+};
+
+Texture2D<float4> Texture : register(t0);
+sampler TextureSampler : register(s0);
+
+void VS(inout float4 color : COLOR0,
+ inout float2 texCoord : TEXCOORD0,
+ inout float4 position : SV_Position)
+{
+ position = mul(position, MatrixTransform);
+}
+
+float4 PS(float4 color : COLOR0,
+ float2 texCoord : TEXCOORD0) : SV_Target0
+{
+ return Texture.Sample(TextureSampler, texCoord) * color;
+}
=======================================
--- /branches/lite/SlimDX.Toolkit/Fonts/Internal/FontShaders.cs Sat Mar 24
20:12:36 2012
+++ /dev/null
@@ -1,110 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Runtime.InteropServices;
-using SlimMath;
-using System.Drawing;
-
-namespace SlimDX.Toolkit
-{
- static class FontShaders
- {
- public const string SimpleVertexShader = @"
-cbuffer ShaderConstants : register(b0) {
- float4x4 TransformMatrix : packoffset(c0);
-};
-
-struct VSIn {
- float4 Position : POSITION;
- float4 GlyphColor : GLYPHCOLOR;
-};
-
-struct VSOut {
- float4 Position : SV_Position;
- float4 GlyphColor : COLOR;
- float2 TexCoord : TEXCOORD;
-};
-
-VSOut VS(VSIn Input) {
- VSOut Output;
-
- Output.Position = mul(TransformMatrix, float4(Input.Position.xy, 0.0f,
1.0f));
- Output.GlyphColor = Input.GlyphColor;
- Output.TexCoord =
Input.Position.zw;
-
- return Output;
-}
-";
- public const string ClippingVertexShader = @"
-cbuffer ShaderConstants : register(b0) {
- float4x4 TransformMatrix : packoffset(c0);
- float4 ClipRect : packoffset(c4);
-};
-
-struct VSIn {
- float4 Position : POSITION;
- float4 GlyphColor : GLYPHCOLOR;
-};
-
-struct VSOut {
- float4 Position : SV_Position;
- float4 GlyphColor : COLOR;
- float2 TexCoord : TEXCOORD;
- float4 ClipDistance : CLIPDISTANCE;
-};
-
-VSOut VS(VSIn Input) {
- VSOut Output;
-
- Output.Position = mul(TransformMatrix, float4(Input.Position.xy, 0.0f,
1.0f));
- Output.GlyphColor = Input.GlyphColor;
- Output.TexCoord =
Input.Position.zw;
- Output.ClipDistance = ClipRect + float4(Input.Position.xy,
-Input.Position.xy);
-
- return Output;
-}
-";
- public const string SimplePixelShader = @"
-SamplerState sampler0 : register(s0);
-Texture2D<float> tex0 : register(t0);
-
-struct PSIn {
- float4 Position : SV_Position;
- float4 GlyphColor : COLOR;
- float2 TexCoord : TEXCOORD;
-};
-
-float4 PS(PSIn Input) : SV_Target {
- float a = tex0.Sample(sampler0, Input.TexCoord);
-
- if(a == 0.0f)
- discard;
-
- return (a * Input.GlyphColor.a) * float4(Input.GlyphColor.rgb, 1.0f);
-}
-";
- public const string ClippingPixelShader = @"
-SamplerState sampler0 : register(s0);
-Texture2D<float> tex0 : register(t0);
-
-struct PSIn {
- float4 Position : SV_Position;
- float4 GlyphColor : COLOR;
- float2 TexCoord : TEXCOORD;
- float4 ClipDistance : CLIPDISTANCE;
-};
-
-float4 PS(PSIn Input) : SV_Target {
- clip(Input.ClipDistance);
-
- float a = tex0.Sample(sampler0, Input.TexCoord);
-
- if(a == 0.0f)
- discard;
-
- return (a * Input.GlyphColor.a) * float4(Input.GlyphColor.rgb, 1.0f);
-}
-";
- }
-}
=======================================
--- /branches/lite/SlimDX.Toolkit/Sprites/SpriteShaders.cs Sat Mar 17
21:28:36 2012
+++ /dev/null
@@ -1,35 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-
-namespace SlimDX.Toolkit
-{
- static class SpriteShaders
- {
- public const string VertexShader = @"
-cbuffer Parameters : register(b0)
-{
- row_major float4x4 MatrixTransform;
-};
-
-void VS(inout float4 color : COLOR0,
- inout float2 texCoord : TEXCOORD0,
- inout float4 position : SV_Position)
-{
- position = mul(position, MatrixTransform);
-}
-";
-
- public const string PixelShader = @"
-Texture2D<float4> Texture : register(t0);
-sampler TextureSampler : register(s0);
-
-float4 PS(float4 color : COLOR0,
- float2 texCoord : TEXCOORD0) : SV_Target0
-{
- return Texture.Sample(TextureSampler, texCoord) * color;
-}
-";
- }
-}
=======================================
--- /branches/lite/SlimDX.Toolkit/Fonts/Internal/RenderData.cs Sat Mar 24
20:12:36 2012
+++ /branches/lite/SlimDX.Toolkit/Fonts/Internal/RenderData.cs Mon Mar 26
13:44:47 2012
@@ -19,6 +19,8 @@
/// </summary>
class RenderData : IDisposable
{
+ const string ResourcePath = "SlimDX.Toolkit.Shaders.Compiled.";
+
[StructLayout(LayoutKind.Sequential)]
struct ShaderConstants
{
@@ -41,7 +43,7 @@
featureLevel = device.FeatureLevel;
// compile vertex shaders
- using (var bytecode =
ShaderBytecode.Compile(FontShaders.SimpleVertexShader, "VS",
device.VertexShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(RenderData).Assembly, ResourcePath
+ "SimpleFontEffect_VS.fxo"))
{
simpleVertexShader = new VertexShader(device, bytecode);
inputLayout = new InputLayout(device, bytecode, new[] {
@@ -50,14 +52,14 @@
});
}
- using (var bytecode =
ShaderBytecode.Compile(FontShaders.ClippingVertexShader, "VS",
device.VertexShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(RenderData).Assembly, ResourcePath
+ "ClippingFontEffect_VS.fxo"))
clippingVertexShader = new VertexShader(device, bytecode);
// compile pixel shaders
- using (var bytecode =
ShaderBytecode.Compile(FontShaders.SimplePixelShader, "PS",
device.PixelShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(RenderData).Assembly, ResourcePath
+ "SimpleFontEffect_PS.fxo"))
simplePixelShader = new PixelShader(device, bytecode);
- using (var bytecode =
ShaderBytecode.Compile(FontShaders.ClippingPixelShader, "PS",
device.PixelShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(RenderData).Assembly, ResourcePath
+ "ClippingFontEffect_PS.fxo"))
clippingPixelShader = new PixelShader(device, bytecode);
constantBuffer = new ConstantBuffer<ShaderConstants>(device);
=======================================
--- /branches/lite/SlimDX.Toolkit/SlimDX.Toolkit.csproj Sat Mar 24 18:37:53
2012
+++ /branches/lite/SlimDX.Toolkit/SlimDX.Toolkit.csproj Mon Mar 26 13:44:47
2012
@@ -55,7 +55,6 @@
<Compile Include="Fonts\Internal\TextRenderer.cs" />
<Compile Include="Fonts\Internal\GlyphAtlas.cs" />
<Compile Include="Fonts\Internal\GlyphProvider.cs" />
- <Compile Include="Fonts\Internal\FontShaders.cs" />
<Compile Include="Fonts\Internal\NativeMethods.cs" />
<Compile Include="Fonts\Internal\RenderData.cs" />
<Compile Include="Fonts\Internal\GlyphImageRenderer.cs" />
@@ -64,7 +63,6 @@
<Compile Include="Sprites\SpriteContextResources.cs" />
<Compile Include="Sprites\SpriteDeviceResources.cs" />
<Compile Include="Sprites\SpriteInfo.cs" />
- <Compile Include="Sprites\SpriteShaders.cs" />
<Compile Include="Utilities\AlignedArray.cs" />
<Compile Include="Utilities\SafeHGlobal.cs" />
<Compile Include="Utilities\SharedResourcePool.cs" />
@@ -90,6 +88,18 @@
</ProjectReference>
</ItemGroup>
<ItemGroup />
+ <ItemGroup>
+ <None Include="Shaders\ClippingFontEffect.fx" />
+ <EmbeddedResource Include="Shaders\Compiled\ClippingFontEffect_PS.fxo"
/>
+ <EmbeddedResource Include="Shaders\Compiled\ClippingFontEffect_VS.fxo"
/>
+ <EmbeddedResource Include="Shaders\Compiled\SimpleFontEffect_PS.fxo" />
+ <EmbeddedResource Include="Shaders\Compiled\SimpleFontEffect_VS.fxo" />
+ <EmbeddedResource Include="Shaders\Compiled\SpriteEffect_PS.fxo" />
+ <EmbeddedResource Include="Shaders\Compiled\SpriteEffect_VS.fxo" />
+ <None Include="Shaders\CompileShaders.bat" />
+ <None Include="Shaders\SimpleFontEffect.fx" />
+ <None Include="Shaders\SpriteEffect.fx" />
+ </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the
targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
=======================================
--- /branches/lite/SlimDX.Toolkit/Sprites/SpriteDeviceResources.cs Tue Mar
20 12:01:54 2012
+++ /branches/lite/SlimDX.Toolkit/Sprites/SpriteDeviceResources.cs Mon Mar
26 13:44:47 2012
@@ -9,6 +9,8 @@
// contains shared resources that are associated with a particular
device
class SpriteDeviceResources : IDisposable
{
+ const string ResourcePath = "SlimDX.Toolkit.Shaders.Compiled.";
+
public VertexShader VertexShader;
public PixelShader PixelShader;
public InputLayout InputLayout;
@@ -18,14 +20,14 @@
public SpriteDeviceResources(Device device, int maxBatchSize)
{
// create the vertex shader and input layout
- using (var bytecode =
ShaderBytecode.Compile(SpriteShaders.VertexShader, "VS",
device.VertexShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(SpriteDeviceResources).Assembly,
ResourcePath + "SpriteEffect_VS.fxo"))
{
VertexShader = new VertexShader(device, bytecode);
InputLayout = new InputLayout(device, bytecode,
InputElementFactory.DemandCreate(typeof(VertexPositionColorTexture)));
}
// create the pixel shader
- using (var bytecode =
ShaderBytecode.Compile(SpriteShaders.PixelShader, "PS",
device.PixelShaderProfile, ShaderFlags.OptimizationLevel3,
EffectFlags.None))
+ using (var bytecode =
ShaderBytecode.LoadResource(typeof(SpriteDeviceResources).Assembly,
ResourcePath + "SpriteEffect_PS.fxo"))
PixelShader = new PixelShader(device, bytecode);
// generate indices for simple quads
=======================================
--- /branches/lite/source/d3dcompiler/ShaderBytecodeDC.cpp Sat Mar 17
11:15:48 2012
+++ /branches/lite/source/d3dcompiler/ShaderBytecodeDC.cpp Mon Mar 26
13:44:47 2012
@@ -23,6 +23,7 @@
#include "../CompilationException.h"
#include "../DataStream.h"
+#include "../Utilities.h"
#include "D3DCompilerException.h"
#include "ShaderBytecodeDC.h"
@@ -63,6 +64,20 @@
Construct( blob );
}
+
+ ShaderBytecode^
ShaderBytecode::LoadResource(System::Reflection::Assembly^ assembly,
String^ resourceName)
+ {
+ Stream^ stream = assembly->GetManifestResourceStream(resourceName);
+
+ int size = 0;
+ bool cleanup = true;
+ std::unique_ptr<char> dataPtr;
+ char *data = Utilities::ReadStream(stream, size, cleanup);
+ if (cleanup)
+ dataPtr.reset(data);
+
+ return gcnew ShaderBytecode((BYTE*)data, (UINT)size);
+ }
ShaderBytecode^ ShaderBytecode::Compile( String^ shaderSource, String^
profile )
{
=======================================
--- /branches/lite/source/d3dcompiler/ShaderBytecodeDC.h Sat Mar 17
11:15:48 2012
+++ /branches/lite/source/d3dcompiler/ShaderBytecodeDC.h Mon Mar 26
13:44:47 2012
@@ -48,6 +48,14 @@
/// <param name="data">A <see cref="DataStream"/> containing the
compiled bytecode.</param>
ShaderBytecode( DataStream^ data );
+ /// <summary>
+ /// Loads a precompiled shader from an embedded resource.
+ /// </summary>
+ /// <param name="assembly">The assembly containing the resource.</param>
+ /// <param name="resourceName">The name of the embedded resource as
specified in the assembly manifest.</param>
+ /// <returns>The compiled shader bytecode, or <c>null</c> if the method
fails.</returns>
+ static ShaderBytecode^ LoadResource(System::Reflection::Assembly^
assembly, System::String^ resourceName);
+
/// <summary>
/// Compiles the provided shader or effect source.
/// </summary>