00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027
#include <iostream>
00028
#include <fstream>
00029
#include <cassert>
00030
#include "ShSyntax.hpp"
00031
#include "ShEnvironment.hpp"
00032
#include "ShContext.hpp"
00033
#include "ShTokenizer.hpp"
00034
#include "ShToken.hpp"
00035
#include "ShProgram.hpp"
00036
#include "ShBackend.hpp"
00037
#include "ShOptimizations.hpp"
00038
00039
namespace SH {
00040
00041 ShProgram shBeginShader(
const std::string& target)
00042 {
00043 ShProgram prg(target);
00044 ShContext::current()->enter(prg.node());
00045
return prg;
00046 }
00047
00048
void shEndShader()
00049 {
00050 ShContext* context = ShContext::current();
00051
00052 ShProgramNodePtr parsing = context->parsing();
00053 assert(parsing);
00054
00055 parsing->ctrlGraph =
new ShCtrlGraph(parsing->tokenizer.blockList());
00056
00057
optimize(parsing);
00058
00059 parsing->collectVariables();
00060
00061 context->exit();
00062
00063 parsing->finish();
00064
00065
00066
00067
00068
00069 }
00070
00071 void shCompile(
ShProgram& prg)
00072 {
00073
if (!ShEnvironment::backend)
return;
00074 prg.
compile(ShEnvironment::backend);
00075 }
00076
00077 void shCompile(
ShProgram& prg,
const std::string& target)
00078 {
00079
if (!ShEnvironment::backend)
return;
00080 prg.
compile(target, ShEnvironment::backend);
00081 }
00082
00083 void shCompileShader(
ShProgram& prg)
00084 {
00085
shCompile(prg);
00086 }
00087
00088
void shCompileShader(
const std::string& target, ShProgram& prg)
00089 {
00090
shCompile(prg, target);
00091 }
00092
00093 void shBind(
ShProgram& prg)
00094 {
00095
if (!ShEnvironment::backend)
return;
00096 prg.
code(ShEnvironment::backend)->bind();
00097 }
00098
00099
void shBind(ShProgram& prg,
const std::string& target)
00100 {
00101
if (!ShEnvironment::backend)
return;
00102 prg.code(target, ShEnvironment::backend)->bind();
00103 }
00104
00105 void shBindShader(
ShProgram& shader)
00106 {
00107
shBind(shader);
00108 }
00109
00110 void shBindShader(
const std::string& target,
ShProgram& shader)
00111 {
00112
shBind(shader, target);
00113 }
00114
00115 bool shSetBackend(
const std::string& name)
00116 {
00117
ShBackendPtr backend = SH::ShBackend::lookup(name);
00118
if (!backend)
return false;
00119 SH::ShEnvironment::backend = backend;
00120
return true;
00121 }
00122
00123 void shInit()
00124 {
00125
00126 }
00127
00128
void shIf(
bool)
00129 {
00130 ShPointer<ShToken> token =
new ShToken(SH_TOKEN_IF);
00131
00132 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument());
00133 ShContext::current()->parsing()->tokenizer.popArgQueue();
00134
00135 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token);
00136 }
00137
00138
void shEndIf()
00139 {
00140 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_ENDIF));
00141 }
00142
00143
void shElse()
00144 {
00145 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_ELSE));
00146 }
00147
00148
void shWhile(
bool)
00149 {
00150 ShPointer<ShToken> token =
new ShToken(SH_TOKEN_WHILE);
00151
00152 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument());
00153 ShContext::current()->parsing()->tokenizer.popArgQueue();
00154
00155 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token);
00156 }
00157
00158
void shEndWhile()
00159 {
00160 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_ENDWHILE));
00161 }
00162
00163
void shDo()
00164 {
00165 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_DO));
00166 }
00167
00168
void shUntil(
bool)
00169 {
00170 ShPointer<ShToken> token =
new ShToken(SH_TOKEN_UNTIL);
00171
00172 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument());
00173 ShContext::current()->parsing()->tokenizer.popArgQueue();
00174
00175 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token);
00176 }
00177
00178
void shFor(
bool)
00179 {
00180 ShPointer<ShToken> token =
new ShToken(SH_TOKEN_FOR);
00181
00182
for (
int i = 0; i < 3; i++) {
00183 token->arguments.push_back(ShContext::current()->parsing()->tokenizer.getArgument());
00184 }
00185 ShContext::current()->parsing()->tokenizer.popArgQueue();
00186
00187 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(token);
00188 }
00189
00190
void shEndFor()
00191 {
00192 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_ENDFOR));
00193 }
00194
00195
void ShBreak()
00196 {
00197 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_BREAK));
00198 }
00199
00200
void ShContinue()
00201 {
00202 ShContext::current()->parsing()->tokenizer.blockList()->addBlock(
new ShToken(SH_TOKEN_CONTINUE));
00203 }
00204
00205 }