Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
HlslWriter.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.Collections.Generic;
4 using SiliconStudio.Shaders.Ast;
5 using SiliconStudio.Shaders.Ast.Hlsl;
6 
7 namespace SiliconStudio.Shaders.Writer.Hlsl
8 {
9  /// <summary>
10  /// A writer for a shader.
11  /// </summary>
12  public class HlslWriter : ShaderWriter
13  {
14  #region Constructors and Destructors
15 
16  /// <summary>
17  /// Initializes a new instance of the <see cref="HlslWriter"/> class.
18  /// </summary>
19  /// <param name="useNodeStack">
20  /// if set to <c>true</c> [use node stack].
21  /// </param>
22  public HlslWriter(bool useNodeStack = false) : base(useNodeStack)
23  {
24  }
25 
26  #endregion
27 
28  #region Public Methods
29 
30  /// <summary>
31  /// Visits the specified Annotations.
32  /// </summary>
33  /// <param name="annotations">The Annotations.</param>
34  [Visit]
35  public virtual void Visit(Annotations annotations)
36  {
37  if (annotations.Variables.Count == 0) return;
38 
39  Write("<").WriteSpace();
40 
41  foreach (var variable in annotations.Variables)
42  {
43  VisitDynamic(variable);
44  }
45 
46  WriteSpace().Write(">");
47  }
48 
49  /// <summary>
50  /// Visits the specified class type.
51  /// </summary>
52  /// <param name="classType">Type of the class.</param>
53  [Visit]
54  public virtual void Visit(ClassType classType)
55  {
56  Write(classType.Attributes, true);
57 
58  Write("class").Write(" ").Write(classType.Name);
59 
60  if (classType.GenericParameters.Count > 0)
61  {
62  Write("<");
63  for (int i = 0; i < classType.GenericParameters.Count; i++)
64  {
65  var genericArgument = classType.GenericParameters[i];
66  if (i > 0) Write(", ");
67  Write(genericArgument.Name);
68  }
69  Write(">");
70  }
71 
72  if (classType.BaseClasses.Count > 0)
73  {
74  WriteSpace().Write(":").WriteSpace();
75  for (int i = 0; i < classType.BaseClasses.Count; i++)
76  {
77  var baseClass = classType.BaseClasses[i];
78  if (i > 0)
79  {
80  Write(",").WriteSpace();
81  }
82 
83  Write(baseClass.Name);
84  }
85 
86  WriteSpace();
87  }
88  else
89  {
90  WriteSpace();
91  }
92 
93  OpenBrace();
94 
95  VisitDynamicList(classType.Members);
96 
97  CloseBrace(false).Write(";").WriteLine();
98  }
99 
100  /// <summary>
101  /// Visits the specified interface type.
102  /// </summary>
103  /// <param name="interfaceType">Type of the interface.</param>
104  [Visit]
105  public virtual void Visit(InterfaceType interfaceType)
106  {
107  Write(interfaceType.Attributes, true);
108  Write("interface").Write(" ").Write(interfaceType.Name);
109  WriteSpace();
110  OpenBrace();
111  VisitDynamicList(interfaceType.Methods);
112  CloseBrace(false).Write(";").WriteLine();
113  }
114 
115  /// <summary>
116  /// Visits the specified asm expression.
117  /// </summary>
118  /// <param name="asmExpression">The asm expression.</param>
119  [Visit]
120  public virtual void Visit(AsmExpression asmExpression)
121  {
122  WriteLine();
123  Write("asm");
124  OpenBrace();
125  Write(asmExpression.Text);
126  CloseBrace();
127  }
128 
129  /// <summary>
130  /// Visits the specified constant buffer.
131  /// </summary>
132  /// <param name="constantBuffer">The constant buffer.</param>
133  [Visit]
134  public virtual void Visit(ConstantBuffer constantBuffer)
135  {
136  Write(constantBuffer.Attributes, true);
137 
138  Write(constantBuffer.Type.Key.ToString());
139 
140  if (constantBuffer.Name != null)
141  {
142  Write(" ").Write(constantBuffer.Name);
143  }
144 
145  WriteSpace();
146  VisitDynamic(constantBuffer.Register);
147  OpenBrace();
148  VisitDynamicList(constantBuffer.Members);
149  CloseBrace(false).Write(";").WriteLine();
150  }
151 
152  /// <summary>
153  /// Visits the specified typedef.
154  /// </summary>
155  /// <param name="typedef">The typedef.</param>
156  [Visit]
157  public virtual void Visit(Typedef typedef)
158  {
159  Write("typedef").Write(" ");
160  Write(typedef.Qualifiers, true);
161  VisitDynamic(typedef.Type);
162  Write(" ");
163 
164  if (typedef.IsGroup)
165  {
166  for (int i = 0; i < typedef.SubDeclarators.Count; i++)
167  {
168  var declarator = typedef.SubDeclarators[i];
169  if (i > 0)
170  {
171  Write(",").WriteSpace();
172  }
173 
174  Write(declarator.Name);
175  }
176  }
177  else
178  {
179  Write(typedef.Name);
180  }
181 
182  Write(";");
183  WriteLine();
184  }
185 
186  /// <summary>
187  /// Visits the specified attribute declaration.
188  /// </summary>
189  /// <param name="attributeDeclaration">The attribute declaration.</param>
190  [Visit]
191  public virtual void Visit(AttributeDeclaration attributeDeclaration)
192  {
193  Write("[").Write(attributeDeclaration.Name);
194  if (attributeDeclaration.Parameters.Count > 0)
195  {
196  Write("(");
197  for (int i = 0; i < attributeDeclaration.Parameters.Count; i++)
198  {
199  var parameter = attributeDeclaration.Parameters[i];
200  if (i > 0)
201  {
202  Write(",").WriteSpace();
203  }
204 
205  VisitDynamic(parameter);
206  }
207 
208  Write(")");
209  }
210 
211  WriteLine("]");
212  }
213 
214  /// <summary>
215  /// Visits the specified cast expression.
216  /// </summary>
217  /// <param name="castExpression">The cast expression.</param>
218  [Visit]
219  public virtual void Visit(CastExpression castExpression)
220  {
221  Write("(");
222  VisitDynamic(castExpression.Target);
223  Write(")");
224  VisitDynamic(castExpression.From);
225  }
226 
227  /// <summary>
228  /// Visits the specified composite identifier.
229  /// </summary>
230  /// <param name="compositeIdentifier">The composite identifier.</param>
231  [Visit]
232  public virtual void Visit(CompositeIdentifier compositeIdentifier)
233  {
234  Write((Identifier)compositeIdentifier);
235  }
236 
237  /// <summary>
238  /// Visits the specified state expression.
239  /// </summary>
240  /// <param name="stateExpression">The state expression.</param>
241  [Visit]
242  public virtual void Visit(StateExpression stateExpression)
243  {
244  VisitDynamic(stateExpression.StateType);
245  WriteSpace();
246  VisitDynamic(stateExpression.Initializer);
247  }
248 
249  /// <summary>
250  /// Visits the specified compile expression.
251  /// </summary>
252  /// <param name="compileExpression">The compile expression.</param>
253  [Visit]
254  public virtual void Visit(CompileExpression compileExpression)
255  {
256  Write("compile").Write(" ");
257  Write(compileExpression.Profile);
258  Write(" ");
259  VisitDynamic(compileExpression.Function);
260  }
261 
262  /// <summary>
263  /// Visits the specified technique.
264  /// </summary>
265  /// <param name="technique">The technique.</param>
266  [Visit]
267  public virtual void Visit(Technique technique)
268  {
269  Write(technique.Attributes, true);
270  Write(technique.Type);
271  if (technique.Name != null)
272  {
273  Write(" ").Write(technique.Name);
274  }
275 
276  WriteSpace();
277  Write(technique.Attributes, false);
278  OpenBrace();
279  VisitDynamicList(technique.Passes);
280  CloseBrace();
281  }
282 
283  /// <summary>
284  /// Visits the specified pass.
285  /// </summary>
286  /// <param name="pass">The pass.</param>
287  [Visit]
288  public virtual void Visit(Pass pass)
289  {
290  Write(pass.Attributes, true);
291  Write("pass");
292  if (pass.Name != null)
293  {
294  Write(" ").Write(pass.Name);
295  }
296 
297  WriteSpace();
298  Write(pass.Attributes, false);
299  OpenBrace();
300  foreach (var expression in pass.Items)
301  {
302  VisitDynamic(expression);
303  WriteLine(";");
304  }
305 
306  CloseBrace();
307  }
308 
309  /// <inheritdoc />
310  [Visit]
311  public virtual void Visit(StateInitializer stateInitializer)
312  {
313  OpenBrace();
314  for (int i = 0; i < stateInitializer.Items.Count; i++)
315  {
316  var item = stateInitializer.Items[i];
317  if (item is StateInitializer && i > 0)
318  {
319  WriteLine(",");
320  }
321 
322  VisitDynamic(item);
323 
324  if (!(item is StateInitializer))
325  {
326  WriteLine(";");
327  }
328  }
329 
330  CloseBrace(false);
331  }
332 
333  /// <inheritdoc />
334  public override void WriteInitializer(Expression expression)
335  {
336  if (expression == null) return;
337 
338  if (!(expression is StateInitializer))
339  WriteSpace().Write("=");
340  WriteSpace();
341  VisitDynamic(expression);
342  }
343 
344  /// <inheritdoc />
345  [Visit]
346  public virtual void Visit(Semantic semantic)
347  {
348  Write(":").WriteSpace();
349  Write(semantic.Name);
350  }
351 
352  /// <inheritdoc />
353  [Visit]
354  public virtual void Visit(PackOffset packOffset)
355  {
356  Write(":").WriteSpace();
357  Write("packoffset(");
358  Write((Identifier)packOffset.Value);
359  Write(")");
360  }
361 
362  /// <inheritdoc />
363  [Visit]
364  public virtual void Visit(RegisterLocation registerLocation)
365  {
366  Write(":").WriteSpace();
367  Write("register(");
368  if (registerLocation.Profile != null)
369  {
370  Write(registerLocation.Profile);
371  Write(",").WriteSpace();
372  }
373 
374  Write(registerLocation.Register);
375  Write(")");
376  }
377 
378  #endregion
379 
380  /// <summary>
381  /// Writes the specified identifier.
382  /// </summary>
383  /// <param name="identifier">The identifier.</param>
384  /// <returns>
385  /// This instance
386  /// </returns>
387  protected override ShaderWriter Write(Identifier identifier)
388  {
389  Write(identifier.Text);
390 
391  if (identifier.IsSpecialReference)
392  {
393  Write("<");
394  }
395 
396  if (identifier is CompositeIdentifier)
397  {
398  var compositeIdentifier = (CompositeIdentifier)identifier;
399  for (int i = 0; i < compositeIdentifier.Identifiers.Count; i++)
400  {
401  var subIdentifier = compositeIdentifier.Identifiers[i];
402  if (i > 0) Write(compositeIdentifier.Separator);
403  Write(subIdentifier);
404  }
405  }
406 
407  if (identifier.HasIndices)
408  {
409  WriteRankSpecifiers(identifier.Indices);
410  }
411 
412  if (identifier.IsSpecialReference)
413  {
414  Write(">");
415  }
416 
417  return this;
418  }
419  }
420 }
virtual void Visit(Technique technique)
Visits the specified technique.
Definition: HlslWriter.cs:267
virtual void Visit(Semantic semantic)
Definition: HlslWriter.cs:346
object Key
Gets or sets the key.
virtual void Visit(Pass pass)
Visits the specified pass.
Definition: HlslWriter.cs:288
bool IsSpecialReference
Gets or sets a value indicating whether this instance is a special reference using < > ...
Definition: Identifier.cs:69
RegisterLocation Register
Gets or sets the register.
virtual void Visit(Annotations annotations)
Visits the specified Annotations.
Definition: HlslWriter.cs:35
string Text
Gets or sets the asm expression in raw text form.
List< AttributeBase > Attributes
Gets or sets the attributes.
Definition: Pass.cs:35
string Text
Gets or sets the name.
Definition: Identifier.cs:77
override ShaderWriter Write(Identifier identifier)
Writes the specified identifier.
Definition: HlslWriter.cs:387
HlslWriter(bool useNodeStack=false)
Initializes a new instance of the HlslWriter class.
Definition: HlslWriter.cs:22
A state expresion in the form: sampler {...}.
List< AttributeBase > Attributes
Gets or sets the attributes.
List< Literal > Parameters
Gets or sets the parameters.
Identifier Name
Gets or sets the name of this declaration
Definition: IDeclaration.cs:16
virtual void Visit(StateInitializer stateInitializer)
Definition: HlslWriter.cs:311
virtual void Visit(Typedef typedef)
Visits the specified typedef.
Definition: HlslWriter.cs:157
Identifier Name
Gets or sets the name.
Definition: Semantic.cs:50
List< MethodDeclaration > Methods
Gets or sets the methods.
List< Pass > Passes
Gets or sets the passes.
Definition: Technique.cs:59
TypeBase StateType
Gets or sets the type of the sampler.
Identifier Name
Gets or sets the name.
Definition: Technique.cs:51
override void WriteInitializer(Expression expression)
Writes the initializer. The expression.
Definition: HlslWriter.cs:334
virtual void Visit(InterfaceType interfaceType)
Visits the specified interface type.
Definition: HlslWriter.cs:105
List< AttributeBase > Attributes
Gets or sets the attributes.
Definition: TypeBase.cs:61
Identifier Name
Gets or sets the name.
Describes a packoffset(value).
Definition: PackOffset.cs:13
virtual void Visit(PackOffset packOffset)
Definition: HlslWriter.cs:354
Identifier Profile
Gets or sets the profile.
Identifier Profile
Gets or sets the profile.
List< Node > Members
Gets or sets the members.
ConstantBufferType Type
Gets or sets a value indicating whether this instance is texture buffer.
Identifier Type
Gets or sets the type.
Definition: Technique.cs:35
StateInitializer Initializer
Gets or sets the initializer.
Declaration of a constant buffer.
List< Node > Members
Gets or sets the members.
Definition: ClassType.cs:64
List< Variable > Variables
Gets or sets the variable.
Definition: Annotations.cs:27
List< AttributeBase > Attributes
Gets or sets the attributes.
Definition: Technique.cs:43
virtual void Visit(CompileExpression compileExpression)
Visits the specified compile expression.
Definition: HlslWriter.cs:254
Identifier Name
Gets or sets the name.
Definition: Pass.cs:54
List< Expression > Indices
Gets or sets the indices.
Definition: Identifier.cs:61
virtual void Visit(ClassType classType)
Visits the specified class type.
Definition: HlslWriter.cs:54
Identifier Value
Gets or sets the value.
Definition: PackOffset.cs:51
bool HasIndices
Gets a value indicating whether this instance has indices.
Definition: Identifier.cs:45
virtual void Visit(AsmExpression asmExpression)
Visits the specified asm expression.
Definition: HlslWriter.cs:120
virtual void Visit(CastExpression castExpression)
Visits the specified cast expression.
Definition: HlslWriter.cs:219
virtual void Visit(CompositeIdentifier compositeIdentifier)
Visits the specified composite identifier.
Definition: HlslWriter.cs:232
virtual void Visit(AttributeDeclaration attributeDeclaration)
Visits the specified attribute declaration.
Definition: HlslWriter.cs:191
TypeBase Target
Gets or sets the target.
A technique pass.
Definition: Pass.cs:12
virtual void Visit(RegisterLocation registerLocation)
Definition: HlslWriter.cs:364
virtual void Visit(StateExpression stateExpression)
Visits the specified state expression.
Definition: HlslWriter.cs:242
virtual void Visit(ConstantBuffer constantBuffer)
Visits the specified constant buffer.
Definition: HlslWriter.cs:134
Expression Function
Gets or sets the function.
List< TypeName > BaseClasses
Gets or sets the base classes.
Definition: ClassType.cs:50
Identifier Register
Gets or sets the register.