#include <msp/core/raii.h>
+#include <msp/gl/extensions/ext_gpu_shader4.h>
#include <msp/strings/format.h>
#include <msp/strings/utils.h>
#include "error.h"
else if(i->type==FRAGMENT)
{
program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
- for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
- program.bind_fragment_data(j->second, j->first);
+ if(EXT_gpu_shader4)
+ {
+ for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
+ program.bind_fragment_data(j->second, j->first);
+ }
}
}
}
else
++i;
}
+ for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+ finalize(*i);
}
void ProgramCompiler::import(const string &name)
return !unused.empty();
}
+void ProgramCompiler::finalize(Stage &stage)
+{
+ if(get_gl_api()==OPENGL_ES2)
+ apply<DefaultPrecisionGenerator>(stage);
+ else
+ apply<PrecisionRemover>(stage);
+}
+
void ProgramCompiler::inject_block(Block &target, const Block &source)
{
list<RefPtr<Node> >::iterator insert_point = target.body.begin();
}
+ProgramCompiler::BlockModifier::BlockModifier():
+ remove_node(false)
+{ }
+
+void ProgramCompiler::BlockModifier::flatten_block(Block &block)
+{
+ insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
+ remove_node = true;
+}
+
+void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
+{
+ block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
+ insert_nodes.clear();
+
+ if(remove_node)
+ block.body.erase(i++);
+ else
+ ++i;
+ remove_node = false;
+}
+
+void ProgramCompiler::BlockModifier::visit(Block &block)
+{
+ for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
+ {
+ (*i)->visit(*this);
+ apply_and_increment(block, i);
+ }
+}
+
+
ProgramCompiler::Formatter::Formatter():
indent(0),
parameter_list(false),
formatted += '\n';
}
- if(api==OPENGL_ES2)
- {
- if(s.type==FRAGMENT)
- formatted += "precision mediump float;\n";
- }
-
Visitor::apply(s);
}
formatted += format("import %s;", import.module);
}
+void ProgramCompiler::Formatter::visit(Precision &prec)
+{
+ formatted += format("precision %s %s;", prec.precision, prec.type);
+}
+
void ProgramCompiler::Formatter::visit(Layout &layout)
{
formatted += "layout(";
}
formatted += format("%s ", interface);
}
+ if(!var.precision.empty())
+ formatted += format("%s ", var.precision);
formatted += format("%s %s", var.type, var.name);
if(var.array)
{
ProgramCompiler::DeclarationCombiner::DeclarationCombiner():
- toplevel(true),
- remove_node(false)
+ toplevel(true)
{ }
void ProgramCompiler::DeclarationCombiner::visit(Block &block)
return;
SetForScope<bool> set(toplevel, false);
- for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
- {
- remove_node = false;
- (*i)->visit(*this);
- if(remove_node)
- block.body.erase(i++);
- else
- ++i;
- }
+ BlockModifier::visit(block);
}
void ProgramCompiler::DeclarationCombiner::visit(FunctionDeclaration &func)
}
-ProgramCompiler::BlockModifier::BlockModifier():
- remove_node(false)
-{ }
-
-void ProgramCompiler::BlockModifier::flatten_block(Block &block)
-{
- insert_nodes.insert(insert_nodes.end(), block.body.begin(), block.body.end());
- remove_node = true;
-}
-
-void ProgramCompiler::BlockModifier::apply_and_increment(Block &block, list<RefPtr<Node> >::iterator &i)
-{
- block.body.insert(i, insert_nodes.begin(), insert_nodes.end());
- insert_nodes.clear();
-
- if(remove_node)
- block.body.erase(i++);
- else
- ++i;
- remove_node = false;
-}
-
-void ProgramCompiler::BlockModifier::visit(Block &block)
-{
- for(list<RefPtr<Node> >::iterator i=block.body.begin(); i!=block.body.end(); )
- {
- (*i)->visit(*this);
- apply_and_increment(block, i);
- }
-}
-
-
ProgramCompiler::InterfaceGenerator::InterfaceGenerator():
scope_level(0)
{ }
}
+void ProgramCompiler::PrecisionRemover::visit(Precision &)
+{
+ remove_node = true;
+}
+
+void ProgramCompiler::PrecisionRemover::visit(VariableDeclaration &var)
+{
+ var.precision.clear();
+}
+
+
+ProgramCompiler::DefaultPrecisionGenerator::DefaultPrecisionGenerator():
+ toplevel(true)
+{ }
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(Block &block)
+{
+ if(toplevel)
+ {
+ SetForScope<bool> set(toplevel, false);
+ BlockModifier::visit(block);
+ }
+ else
+ Visitor::visit(block);
+}
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(Precision &prec)
+{
+ have_default.insert(prec.type);
+}
+
+void ProgramCompiler::DefaultPrecisionGenerator::visit(VariableDeclaration &var)
+{
+ if(var.type_declaration)
+ return;
+
+ string type = var.type;
+ if(!type.compare(0, 3, "vec") || !type.compare(0, 3, "mat"))
+ type = "float";
+ else if(!type.compare(0, 3, "ivec") || type=="uint")
+ type = "int";
+
+ if(!have_default.count(type))
+ {
+ Precision *prec = new Precision;
+ if(!type.compare(0, 7, "sampler"))
+ prec->precision = "lowp";
+ else if(stage->type==FRAGMENT)
+ prec->precision = "mediump";
+ else
+ prec->precision = "highp";
+ prec->type = type;
+ insert_nodes.push_back(prec);
+
+ have_default.insert(type);
+ }
+}
+
+
ProgramCompiler::LegacyConverter::LegacyConverter():
target_api(get_gl_api()),
target_version(get_glsl_version()),
}
else if(stage->type==FRAGMENT && var.interface=="out")
{
+ if(location!=0)
+ static Require _req(EXT_gpu_shader4);
stage->locations[var.name] = location;
var.layout->qualifiers.erase(i);
}