Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
EffectByteCodeToSourceCodeWriter.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.IO;
5 using System.Text;
6 using SiliconStudio.Core.Serialization;
7 using SiliconStudio.Paradox.Graphics;
8 using SiliconStudio.Paradox.Shaders;
9 using SiliconStudio.Paradox.Shaders.Compiler;
10 
11 namespace SiliconStudio.Paradox.Assets.Effect
12 {
13  /// <summary>
14  /// Use this class to generate a code with embedded effect bytecode.
15  /// </summary>
17  {
18  public static void Write(string name, CompilerParameters parameters, EffectBytecode effectData, TextWriter writer)
19  {
20  const string codeTemplate = @"//------------------------------------------------------------------------------
21 // <auto-generated>
22 // Paradox Effect Compiler File Generated:
23 {0}//
24 // Command Line: {7}
25 // Changes to this file may cause incorrect behavior and will be lost if
26 // the code is regenerated.
27 // </auto-generated>
28 //------------------------------------------------------------------------------
29 
30 namespace {1}
31 {{
32  {2} class {3}
33  {{
34  {4} static readonly byte[] {5} = new byte[] {{
35 {6}
36  }};
37  }}
38 }}
39 ";
40  var effectToGenerateText = new StringBuilder();
41  effectToGenerateText.AppendFormat("// Effect [{0}]\r\n", name);
42 
43  var buffer = new MemoryStream();
44  var serializer = new BinarySerializationWriter(buffer);
45  serializer.Write(effectData);
46  serializer.Flush();
47 
48  var bufferAsText = new StringBuilder();
49  var bufferArray = buffer.ToArray();
50  for (int i = 0; i < bufferArray.Length; i++)
51  {
52  bufferAsText.Append(bufferArray[i]).Append(", ");
53  if (i > 0 && (i % 64) == 0)
54  {
55  bufferAsText.AppendLine();
56  }
57  }
58 
59  var classDeclaration = parameters.Get(EffectSourceCodeKeys.ClassDeclaration);
60  var fieldDeclaration = parameters.Get(EffectSourceCodeKeys.FieldDeclaration);
61  var nameSpace = parameters.Get(EffectSourceCodeKeys.Namespace);
62  var className = parameters.Get(EffectSourceCodeKeys.ClassName) ?? name;
63  var fieldName = parameters.Get(EffectSourceCodeKeys.FieldName);
64 
65  var commandLine = string.Join(" ", Environment.GetCommandLineArgs());
66 
67  var graphicsPlatform = parameters.Get(CompilerParameters.GraphicsPlatformKey);
68  string paradoxDefine = "undefined";
69  switch (graphicsPlatform)
70  {
71  case GraphicsPlatform.Direct3D11:
72  paradoxDefine = "SILICONSTUDIO_PARADOX_GRAPHICS_API_DIRECT3D11";
73  break;
74  case GraphicsPlatform.OpenGL:
75  paradoxDefine = "SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLCORE";
76  break;
77  case GraphicsPlatform.OpenGLES:
78  paradoxDefine = "SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES";
79  break;
80  }
81 
82  writer.WriteLine("#if {0}", paradoxDefine);
83  writer.Write(codeTemplate,
84  effectToGenerateText, // {0}
85  nameSpace, // {1}
86  classDeclaration, // {2}
87  className, // {3}
88  fieldDeclaration, // {4}
89  fieldName, // {5}
90  bufferAsText, // {6}
91  commandLine); // {7}
92 
93  writer.WriteLine("#endif");
94 
95  writer.Flush();
96  }
97  }
98 }
Implements SerializationStream as a binary writer.
static void Write(string name, CompilerParameters parameters, EffectBytecode effectData, TextWriter writer)
Use this class to generate a code with embedded effect bytecode.
Contains a compiled shader with bytecode for each stage.