]> git.tdb.fi Git - libs/gl.git/blobdiff - source/program.cpp
Some cleanup for View and WindowView
[libs/gl.git] / source / program.cpp
index f56beec73260b58a4eff644aacf4f8d89f4329e0..270dbdc3e5675163017349d0cef901fcffaccdf8 100644 (file)
@@ -8,11 +8,14 @@
 #include <msp/gl/extensions/arb_vertex_shader.h>
 #include <msp/gl/extensions/ext_gpu_shader4.h>
 #include <msp/gl/extensions/nv_non_square_matrices.h>
+#include <msp/io/print.h>
 #include <msp/strings/format.h>
 #include "buffer.h"
 #include "error.h"
 #include "misc.h"
 #include "program.h"
+#include "programcompiler.h"
+#include "resources.h"
 #include "shader.h"
 
 using namespace std;
@@ -34,6 +37,24 @@ Program::Program(const ProgramBuilder::StandardFeatures &features)
        link();
 }
 
+Program::Program(const std::string &source)
+{
+       init();
+
+       ProgramCompiler compiler;
+       if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl"))
+       {
+               if(RefPtr<IO::Seekable> io = Resources::get_builtins().open(source))
+                       compiler.compile(*io, source);
+               else
+                       throw IO::file_not_found(source);
+       }
+       else
+               compiler.compile(source);
+       compiler.add_shaders(*this);
+       link();
+}
+
 Program::Program(const string &vert, const string &frag)
 {
        init();
@@ -115,6 +136,12 @@ void Program::link()
        if(!linked)
                throw compile_error(get_info_log());
 
+#ifdef DEBUG
+       std::string info_log = get_info_log();
+       if(!info_log.empty())
+               IO::print("Program link info log:\n%s", info_log);
+#endif
+
        unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS);
        vector<UniformInfo *> uniforms_by_index(count);
        for(unsigned i=0; i<count; ++i)
@@ -284,25 +311,12 @@ const Program::UniformInfo &Program::get_uniform_info(const string &name) const
 
 int Program::get_uniform_location(const string &n) const
 {
+       if(n[n.size()-1]==']')
+               throw invalid_argument("Program::get_uniform_location");
+
        UniformMap::const_iterator i = uniforms.find(n);
        if(i==uniforms.end())
-       {
-               if(n[n.size()-1]==']')
-               {
-                       string::size_type open_bracket = n.rfind('[');
-                       if(open_bracket!=string::npos)
-                       {
-                               /* The requested name looks like an array.  glGetActiveUniform only
-                               gives us the first element of the array, so try to look that up and
-                               add an offset. */
-                               unsigned offset = lexical_cast<unsigned>(n.substr(open_bracket+1, n.size()-2-open_bracket));
-                               i = uniforms.find(n.substr(0, open_bracket));
-                               if(i!=uniforms.end() && i->second.block->bind_point<0 && offset<i->second.size)
-                                       return i->second.location+offset;
-                       }
-               }
                return -1;
-       }
 
        return i->second.block->bind_point<0 ? i->second.location : -1;
 }
@@ -332,6 +346,7 @@ Program::Loader::Loader(Program &p):
 {
        add("attribute",       &Loader::attribute);
        add("fragment_shader", &Loader::fragment_shader);
+       add("geometry_shader", &Loader::geometry_shader);
        add("standard",        &Loader::standard);
        add("vertex_shader",   &Loader::vertex_shader);
 }
@@ -351,6 +366,11 @@ void Program::Loader::fragment_shader(const string &src)
        obj.attach_shader_owned(new FragmentShader(src));
 }
 
+void Program::Loader::geometry_shader(const string &src)
+{
+       obj.attach_shader_owned(new GeometryShader(src));
+}
+
 void Program::Loader::standard()
 {
        ProgramBuilder::StandardFeatures feat;