forked from unknownworlds/hlslparser
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathMSLGenerator.h
135 lines (105 loc) · 4.12 KB
/
MSLGenerator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef MSL_GENERATOR_H
#define MSL_GENERATOR_H
#include "CodeWriter.h"
#include "HLSLTree.h"
namespace M4
{
class HLSLTree;
struct HLSLFunction;
struct HLSLStruct;
/**
* This class is used to generate MSL shaders.
*/
class MSLGenerator
{
public:
enum Target
{
Target_VertexShader,
Target_FragmentShader,
};
enum Flags
{
Flag_ConstShadowSampler = 1 << 0,
Flag_PackMatrixRowMajor = 1 << 1,
Flag_NoIndexAttribute = 1 << 2,
};
struct Options
{
unsigned int flags;
unsigned int bufferRegisterOffset;
int (*attributeCallback)(const char* name, unsigned int index);
bool treatHalfAsFloat;
bool forceHalfPrecision; // Not implemented.
bool usePreciseFma;
bool use16BitIntegers;
Options()
{
flags = 0;
bufferRegisterOffset = 0;
attributeCallback = NULL;
treatHalfAsFloat = true;
forceHalfPrecision = false;
usePreciseFma = false;
use16BitIntegers = false;
}
};
MSLGenerator();
bool Generate(HLSLTree* tree, Target target, const char* entryName, const Options& options = Options());
const char* GetResult() const;
private:
// @@ Rename class argument. Add buffers & textures.
struct ClassArgument
{
const char* name;
HLSLType type;
//const char* typeName; // @@ Do we need more than the type name?
const char* registerName;
ClassArgument * nextArg;
ClassArgument(const char* name, HLSLType type, const char * registerName) :
name(name), type(type), registerName(registerName)
{
nextArg = NULL;
}
};
void AddClassArgument(ClassArgument * arg);
void Prepass(HLSLTree* tree, Target target, HLSLFunction* entryFunction);
void CleanPrepass();
void PrependDeclarations();
void OutputStaticDeclarations(int indent, HLSLStatement* statement);
void OutputStatements(int indent, HLSLStatement* statement);
void OutputAttributes(int indent, HLSLAttribute* attribute);
void OutputDeclaration(HLSLDeclaration* declaration);
void OutputStruct(int indent, HLSLStruct* structure);
void OutputBuffer(int indent, HLSLBuffer* buffer);
void OutputFunction(int indent, HLSLFunction* function);
void OutputExpression(HLSLExpression* expression, HLSLExpression* parentExpression);
void OutputTypedExpression(const HLSLType& type, HLSLExpression* expression, HLSLExpression* parentExpression);
bool NeedsCast(const HLSLType & target, const HLSLType & source);
void OutputCast(const HLSLType& type);
void OutputArguments(HLSLArgument* argument);
void OutputDeclaration(const HLSLType& type, const char* name, HLSLExpression* assignment, bool isRef = false, bool isConst = false, int alignment = 0);
void OutputDeclarationType(const HLSLType& type, bool isConst = false, bool isRef = false, int alignment = 0, bool isTypeCast = false);
void OutputDeclarationBody(const HLSLType& type, const char* name, HLSLExpression* assignment, bool isRef = false);
void OutputExpressionList(HLSLExpression* expression);
void OutputExpressionList(const HLSLType& type, HLSLExpression* expression);
void OutputExpressionList(HLSLArgument* argument, HLSLExpression* expression);
void OutputFunctionCallStatement(int indent, HLSLFunctionCall* functionCall, HLSLDeclaration* assingmentExpression);
void OutputFunctionCall(HLSLFunctionCall* functionCall, HLSLExpression * parentExpression);
const char* TranslateInputSemantic(const char* semantic);
const char* TranslateOutputSemantic(const char* semantic);
const char* GetTypeName(const HLSLType& type, bool exactType);
void Error(const char* format, ...);
private:
CodeWriter m_writer;
HLSLTree* m_tree;
const char* m_entryName;
Target m_target;
Options m_options;
bool m_error;
ClassArgument * m_firstClassArgument;
ClassArgument * m_lastClassArgument;
HLSLFunction * m_currentFunction;
};
} // M4
#endif