2 * Copyright © 2012-2021 VMware, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
28 #include "immediate.h"
29 #include "binaryoperator.h"
40 addVariable(Variable("vTexture0", Variable::BuiltIn
, Value::vec4
));
41 addVariable(Variable("vFragColor", Variable::BuiltIn
, Value::vec4
));
43 addVariable(Variable("gl_Color", Variable::BuiltIn
, Value::vec4
));
44 addVariable(Variable("gl_Vertex", Variable::BuiltIn
, Value::vec4
));
45 addVariable(Variable("gl_Position", Variable::BuiltIn
, Value::vec4
));
46 addVariable(Variable("gl_BackColor", Variable::BuiltIn
, Value::vec4
));
47 addVariable(Variable("gl_FragColor", Variable::BuiltIn
, Value::vec4
));
48 addVariable(Variable("gl_FrontColor", Variable::BuiltIn
, Value::vec4
));
49 addVariable(Variable("gl_TexCoord[0]", Variable::BuiltIn
, Value::vec4
));
50 addVariable(Variable("gl_MultiTexCoord0", Variable::BuiltIn
, Value::vec4
));
51 addVariable(Variable("gl_ModelViewProjectionMatrix", Variable::BuiltIn
, Value::vec4
));
56 for (std::vector
<Operator
*>::iterator itr
= mProgram
.begin(); itr
!= mProgram
.end(); ++itr
)
60 Constant
addLocalVec4(const std::string
& name
)
62 return addVariable(Variable(name
, Variable::Local
, Value::vec4
));
65 Constant
addUniformVec4(const std::string
& name
)
67 return addVariable(Variable(name
, Variable::Uniform
, Value::vec4
));
70 Constant
addUniformSampler2D(const std::string
& name
)
72 return addVariable(Variable(name
, Variable::UniformSampler
, Value::none
));
75 Constant
addVariable(const Variable
& variable
)
77 mVariables
[variable
.name()] = variable
;
78 return Constant(variable
);
81 Constant
operator[](const std::string
& name
)
83 std::map
<std::string
, Variable
>::iterator itr
= mVariables
.find(name
);
84 assert(itr
!= mVariables
.end());
85 return Constant(itr
->second
);
88 Variable
& variable(const std::string
& name
)
90 return mVariables
[name
];
93 Shader
& operator<<(Operator
* op
)
95 mProgram
.push_back(op
);
99 void start(ShaderState
& state
)
101 state
= ShaderState(mVariables
);
104 void run(ShaderState
& state
)
106 for (std::vector
<Operator
*>::const_iterator itr
= mProgram
.begin(); itr
!= mProgram
.end(); ++itr
) {
107 (*itr
)->execute(state
);
111 void end(ShaderState
& state
)
115 std::string
toGLSL() const
117 std::stringstream ss
;
120 for (std::map
<std::string
, Variable
>::const_iterator itr
= mVariables
.begin(); itr
!= mVariables
.end(); ++itr
) {
121 const Variable
& var
= itr
->second
;
124 ss
<< var
.toGLSL() << ";" << std::endl
;
130 ss
<< "void main(void)" << std::endl
;
131 ss
<< "{" << std::endl
;
133 for (std::map
<std::string
, Variable
>::const_iterator itr
= mVariables
.begin(); itr
!= mVariables
.end(); ++itr
) {
134 const Variable
& var
= itr
->second
;
136 if (var
.type() == Variable::Local
)
137 ss
<< "\t" << var
.toGLSL() << ";" << std::endl
;
140 for (std::vector
<Operator
*>::const_iterator itr
= mProgram
.begin(); itr
!= mProgram
.end(); ++itr
) {
141 ss
<< "\t" << (*itr
)->toGLSL() << ";" << std::endl
;
144 ss
<< "}" << std::endl
;
148 const std::map
<std::string
, Variable
>& variables() const
154 std::map
<std::string
, Variable
> mVariables
;
155 std::vector
<Operator
*> mProgram
;