Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HlslToGlslWriter.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.Globalization;
5 using SiliconStudio.Shaders.Ast;
6 using SiliconStudio.Shaders.Ast.Hlsl;
8 
9 namespace SiliconStudio.Shaders.Writer.Hlsl
10 {
11  /// <summary>
12  /// A writer for a shader.
13  /// </summary>
15  {
16  #region Constructors and Destructors
17 
18  /// <summary>
19  /// Initializes a new instance of the <see cref="HlslWriter"/> class.
20  /// </summary>
21  /// <param name="useNodeStack">
22  /// if set to <c>true</c> [use node stack].
23  /// </param>
24  public HlslToGlslWriter(bool useNodeStack = false)
25  : base(useNodeStack)
26  {
27  GenerateUniformBlocks = true;
28  }
29 
30  #endregion
31 
32  public bool GenerateUniformBlocks { get; set; }
33 
34  public bool TrimFloatSuffix { get; set; }
35 
36  #region Public Methods
37 
38  /// <inheritdoc/>
39  [Visit]
40  public override void Visit(Literal literal)
41  {
42  if (TrimFloatSuffix && literal.Value is float)
43  literal.Text = literal.Text.Trim('f', 'F', 'l', 'L');
44 
45  base.Visit(literal);
46  }
47 
48  /// <inheritdoc />
49  [Visit]
50  public virtual void Visit(Ast.Glsl.InterfaceType interfaceType)
51  {
52  Write(interfaceType.Qualifiers, true);
53 
54  Write(" ");
55  Write(interfaceType.Name);
56  WriteSpace();
57 
58  // Post Attributes
59  Write(interfaceType.Attributes, false);
60 
61  OpenBrace();
62 
63  foreach (var variableDeclaration in interfaceType.Fields)
64  VisitDynamic(variableDeclaration);
65 
66  CloseBrace(false);
67 
68  if (IsDeclaratingVariable.Count == 0 || !IsDeclaratingVariable.Peek())
69  {
70  Write(";").WriteLine();
71  }
72  }
73 
74  /// <inheritdoc/>
75  [Visit]
76  public override void Visit(Annotations annotations)
77  {
78  }
79 
80  /// <inheritdoc/>
81  [Visit]
82  public override void Visit(ClassType classType)
83  {
84  }
85 
86  /// <inheritdoc/>
87  [Visit]
88  public override void Visit(InterfaceType interfaceType)
89  {
90  }
91 
92  /// <inheritdoc/>
93  [Visit]
94  public override void Visit(AsmExpression asmExpression)
95  {
96  }
97 
98  /// <inheritdoc/>
99  [Visit]
100  public override void Visit(ConstantBuffer constantBuffer)
101  {
102  // Flatten the constant buffers
103  if (constantBuffer.Members.Count > 0)
104  {
105  if (GenerateUniformBlocks)
106  {
107  Write(constantBuffer.Qualifiers, true);
108  if (constantBuffer.Register != null)
109  {
110  if (constantBuffer.Qualifiers != Qualifier.None)
111  throw new NotImplementedException();
112 
113  Write("layout(binding = ").Write(constantBuffer.Register.Register.Text).Write(") ");
114  }
115  Write("uniform").Write(" ").Write(constantBuffer.Name).WriteSpace().Write("{").WriteLine();
116  Indent();
117  VisitDynamicList(constantBuffer.Members);
118  }
119  else
120  {
121  Write("// Begin cbuffer ").Write(constantBuffer.Name).WriteLine();
122  foreach (var member in constantBuffer.Members)
123  {
124  // Prefix each variable with "uniform "
125  if (member is Variable)
126  {
127  Write("uniform");
128  Write(" ");
129  }
130  VisitDynamic(member);
131  }
132  }
133 
134  if (GenerateUniformBlocks)
135  {
136  Outdent();
137  Write("};").WriteLine();
138  }
139  else
140  {
141  Write("// End buffer ").Write(constantBuffer.Name).WriteLine();
142  }
143  }
144  }
145 
146  /// <inheritdoc/>
147  [Visit]
148  public override void Visit(Typedef typedef)
149  {
150  }
151 
152  /// <inheritdoc/>
153  [Visit]
154  public override void Visit(AttributeDeclaration attributeDeclaration)
155  {
156 
157  }
158 
159  /// <inheritdoc/>
160  [Visit]
161  public override void Visit(CastExpression castExpression)
162  {
163  }
164 
165  /// <summary>
166  /// Visits the specified technique.
167  /// </summary>
168  /// <param name="technique">The technique.</param>
169  [Visit]
170  public override void Visit(Technique technique)
171  {
172  }
173 
174  /// <inheritdoc />
175  [Visit]
176  public override void Visit(StateInitializer stateInitializer)
177  {
178  }
179 
180  /// <inheritdoc />
181  [Visit]
182  public override void Visit(StateExpression stateExpression)
183  {
184  }
185 
186  /// <inheritdoc />
187  [Visit]
188  public override void Visit(Semantic semantic)
189  {
190  }
191 
192  /// <inheritdoc />
193  [Visit]
194  public override void Visit(PackOffset packOffset)
195  {
196  }
197 
198  /// <inheritdoc />
199  [Visit]
200  public override void Visit(RegisterLocation registerLocation)
201  {
202  }
203 
204  /// <inheritdoc />
205  [Visit]
206  public void Visit(Ast.Glsl.LayoutQualifier layoutQualifier)
207  {
208  Write("layout(");
209  for (int i = 0; i < layoutQualifier.Layouts.Count; i++)
210  {
211  var layout = layoutQualifier.Layouts[i];
212  if (i > 0) Write(",").WriteSpace();
213  Write(layout.Name);
214  if (layout.Value != null)
215  {
216  WriteSpace().Write("=").WriteSpace();
217  Visit((Node)layout.Value);
218  }
219  }
220  Write(")");
221  WriteSpace();
222  }
223 
224  #endregion
225  }
226 }
override void Visit(Typedef typedef)
Visits the specified typedef. The typedef.
RegisterLocation Register
Gets or sets the register.
A state expresion in the form: sampler {...}.
override void Visit(StateInitializer stateInitializer)
override void Visit(RegisterLocation registerLocation)
override void Visit(Annotations annotations)
Visits the specified Annotations. The Annotations.
override void Visit(PackOffset packOffset)
override void Visit(StateExpression stateExpression)
Visits the specified state expression. The state expression.
Abstract node.
Definition: Node.cs:15
override void Visit(ClassType classType)
Visits the specified class type. Type of the class.
A variable declaration.
Definition: Variable.cs:11
Identifier Name
Gets or sets the name.
Describes a packoffset(value).
Definition: PackOffset.cs:13
object Value
Gets or sets the value.
Definition: Literal.cs:57
Qualifier Qualifiers
Gets or sets the qualifiers.
List< Node > Members
Gets or sets the members.
Declaration of a constant buffer.
override void Visit(InterfaceType interfaceType)
Visits the specified interface type. Type of the interface.
HlslToGlslWriter(bool useNodeStack=false)
Initializes a new instance of the HlslWriter class.
override void Visit(AsmExpression asmExpression)
Visits the specified asm expression. The asm expression.
static readonly Qualifier None
None Enum.
Definition: Qualifier.cs:18
override void Visit(Technique technique)
Visits the specified technique.
SiliconStudio.Shaders.Ast.Glsl.LayoutQualifier LayoutQualifier
virtual void Visit(Ast.Glsl.InterfaceType interfaceType)
A field of a struct.
Definition: Literal.cs:13
override void Visit(CastExpression castExpression)
Visits the specified cast expression. The cast expression.
override void Visit(ConstantBuffer constantBuffer)
Visits the specified constant buffer. The constant buffer.
override void Visit(AttributeDeclaration attributeDeclaration)
Visits the specified attribute declaration. The attribute declaration.
void Visit(Ast.Glsl.LayoutQualifier layoutQualifier)