1 // Sample file using SLConverter.exe assembly
2 using System;
3 using SLConverter;
4 
5 namespace CustomConvertorApp
6 {
7     /// <summary>
8     /// Sample program using SLConverter with 2 usecases:
9     /// 1) Convert from an inline HLSL sourcecode and get the glsl code result back
10     /// 2) Convert a file on the disk to GLSL files on the disk
11     /// </summary>
12     class Program
13     {
14         /// <summary>
15         /// Main entry.
16         /// </summary>
17         /// <param name="args">Command line args</param>
Main(string[] args)18         static void Main(string[] args)
19         {
20             // Create a converter using the CafeCompiler as the main Glsl compiler
21             var converter = new SLConverter.Converter();
22 
23             // Use Cafe compiler
24             converter.GlslCompiler = SLConverter.CafeCompiler.NewInstance();
25 
26             // Uncomment the following lines to disable some features
27             //converter.UseBindingLayout = false;
28             //converter.UseLocationLayout = false;
29             //converter.UseMultiThreading = true;
30             //converter.UseSemanticForLocation = true;
31             //converter.UseSemanticForVariable = true;
32             //converter.NoSwapForBinaryMatrixOperation = true;
33             //converter.IsPointSpriteShader = true;
34 
35             // --------------------------------------------------------------------
36             // Usecase 1) Convert a single HLSL inline source code to GLSL
37             // --------------------------------------------------------------------
38             converter.Macros.Add(new ShaderMacro("CUSTOM_MACRO", "COLOR"));
39 
40             var hlslSourceCode = @"
41 struct VS_IN
42 {
43     float4 pos : POSITION;
44     float4 col : CUSTOM_MACRO;
45 };
46 
47 struct PS_IN
48 {
49     float4 pos : SV_POSITION;
50     float4 col : CUSTOM_MACRO;
51 };
52 
53 float4x4 worldViewProj;
54 
55 PS_IN VS( VS_IN input )
56 {
57     PS_IN output = (PS_IN)0;
58 
59     output.pos = mul(input.pos, worldViewProj);
60     output.col = input.col;
61 
62     return output;
63 }
64 
65 float4 PS( PS_IN input ) : SV_Target
66 {
67     return input.col;
68 }
69 ";
70             string glslCode;
71 
72             var log = new TextLogger();
73 
74             // Convert VertexShader VS entrypoint to GLSL
75             if (!converter.ConvertFromText(hlslSourceCode, "VS", PipelineStage.Vertex, ShaderModel.Model40, out glslCode, log, "inlineHLSL"))
76             {
77                 Console.WriteLine("Error while converting code: {0}", log);
78             }
79 
80             Console.WriteLine("GLSL VS code:");
81             Console.WriteLine(glslCode);
82 
83             // Convert PixelShader PS entrypoint to GLSL
84             log = new TextLogger();
85             if (!converter.ConvertFromText(hlslSourceCode, "PS", PipelineStage.Pixel, ShaderModel.Model40, out glslCode, log, "inlineHLSL"))
86             {
87                 Console.WriteLine("Error while converting code: {0}", log);
88             }
89 
90             Console.WriteLine("GLSL PS code:");
91             Console.WriteLine(glslCode);
92 
93             // --------------------------------------------------------------------
94             // Usecase 2) Convert a HLSL file to GLSL files
95             // --------------------------------------------------------------------
96             log = new TextLogger();
97 
98             converter.UseMultiThreading = true;
99             converter.ConvertFromFile("test.fx", @"output\$0_$1.glsl", (isSuccess, message) => Console.WriteLine("{0} : {1}", isSuccess ? "Success" : "Error", message), log);
100         }
101     }
102 }
103