X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fprogram.cpp;h=dd22033e7e4369df1fa7818dbc59f70f29436bb1;hb=7c069241318b7133ac2df65ee13cb1d2968c5974;hp=4cdbf97c0fcdf84431b1ba88044cc8bd41addb07;hpb=73bef37da97b6da0b99227f63235cb52c4e56c44;p=libs%2Fgl.git diff --git a/source/core/program.cpp b/source/core/program.cpp index 4cdbf97c..dd22033e 100644 --- a/source/core/program.cpp +++ b/source/core/program.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -13,6 +15,7 @@ #include "buffer.h" #include "error.h" #include "misc.h" +#include "module.h" #include "program.h" #include "resources.h" #include "shader.h" @@ -32,27 +35,30 @@ Program::Program(const std::string &source) { init(); - SL::Compiler compiler; - if(source.find(';')==string::npos && source.size()>5 && !source.compare(source.size()-5, 5, ".glsl")) - { - if(RefPtr io = Resources::get_builtins().open(source)) - compiler.load_source(*io, source); - else - throw IO::file_not_found(source); - } - else - compiler.set_source(source); - compiler.compile(); - compiler.add_shaders(*this); + GlslModule mod; + mod.set_source(source); + add_stages(mod); + link(); + module = 0; } Program::Program(const string &vert, const string &frag) { init(); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" attach_shader_owned(new VertexShader(vert)); attach_shader_owned(new FragmentShader(frag)); +#pragma GCC diagnostic pop + link(); +} + +Program::Program(const Module &mod, const map &spec_values) +{ + init(); + add_stages(mod, spec_values); link(); } @@ -60,52 +66,152 @@ void Program::init() { static Require _req(ARB_shader_objects); - linked = false; id = glCreateProgram(); + module = 0; + linked = false; } Program::~Program() { - for(ShaderList::iterator i=owned_data.begin(); i!=owned_data.end(); ++i) - delete *i; + for(vector::iterator i=stage_ids.begin(); i!=stage_ids.end(); ++i) + glDeleteShader(*i); glDeleteProgram(id); } -void Program::attach_shader(Shader &shader) +void Program::add_stages(const Module &mod, const map &spec_values) { - if(find(shaders.begin(), shaders.end(), &shader)==shaders.end()) + if(!stage_ids.empty()) + throw invalid_operation("Program::add_stages"); + + switch(mod.get_format()) { - glAttachShader(id, shader.get_id()); - shaders.push_back(&shader); + case Module::GLSL: return add_glsl_stages(static_cast(mod), spec_values); + default: throw invalid_argument("Program::add_stages"); } } -void Program::attach_shader_owned(Shader *shader) +unsigned Program::add_stage(GLenum type) { - attach_shader(*shader); - if(find(owned_data.begin(), owned_data.end(), shader)==owned_data.end()) - owned_data.push_back(shader); + switch(type) + { + case GL_VERTEX_SHADER: { static Require _req(ARB_vertex_shader); } break; + case GL_GEOMETRY_SHADER: { static Require _req(ARB_geometry_shader4); } break; + case GL_FRAGMENT_SHADER: { static Require _req(ARB_fragment_shader); } break; + default: throw invalid_argument("Program::add_stage"); + } + + unsigned stage_id = glCreateShader(type); + stage_ids.push_back(stage_id); + glAttachShader(id, stage_id); + + return stage_id; } -void Program::detach_shader(Shader &shader) +void Program::add_glsl_stages(const GlslModule &mod, const map &spec_values) { - ShaderList::iterator i = remove(shaders.begin(), shaders.end(), &shader); - if(i!=shaders.end()) + module = &mod; + + SL::Compiler compiler; + compiler.set_source(mod.get_prepared_source(), ""); + compiler.specialize(spec_values); + compiler.compile(SL::Compiler::PROGRAM); +#ifdef DEBUG + string diagnostics = compiler.get_diagnostics(); + if(!diagnostics.empty()) + IO::print("Program diagnostics:\n%s\n", diagnostics); +#endif + + vector stages = compiler.get_stages(); + for(vector::const_iterator i=stages.begin(); i!=stages.end(); ++i) { - shaders.erase(i, shaders.end()); - glDetachShader(id, shader.get_id()); + unsigned stage_id = 0; + switch(*i) + { + case SL::Stage::VERTEX: stage_id = add_stage(GL_VERTEX_SHADER); break; + case SL::Stage::GEOMETRY: stage_id = add_stage(GL_GEOMETRY_SHADER); break; + case SL::Stage::FRAGMENT: stage_id = add_stage(GL_FRAGMENT_SHADER); break; + default: throw invalid_operation("Program::add_glsl_stages"); + } + + string stage_src = compiler.get_stage_glsl(*i); + const char *src_ptr = stage_src.data(); + int src_len = stage_src.size(); + glShaderSource(stage_id, 1, &src_ptr, &src_len); + + if(*i==SL::Stage::VERTEX) + { + const map &attribs = compiler.get_vertex_attributes(); + for(map::const_iterator j=attribs.begin(); j!=attribs.end(); ++j) + glBindAttribLocation(id, j->second, j->first.c_str()); + } + + if(*i==SL::Stage::FRAGMENT && EXT_gpu_shader4) + { + const map &frag_outs = compiler.get_fragment_outputs(); + for(map::const_iterator j=frag_outs.begin(); j!=frag_outs.end(); ++j) + glBindFragDataLocation(id, j->second, j->first.c_str()); + } + + compile_glsl_stage(stage_id); } } +void Program::compile_glsl_stage(unsigned stage_id) +{ + glCompileShader(stage_id); + bool compiled = get_shader_i(stage_id, GL_COMPILE_STATUS); + + GLsizei info_log_len = get_shader_i(stage_id, GL_INFO_LOG_LENGTH); + string info_log(info_log_len+1, 0); + glGetShaderInfoLog(stage_id, info_log_len+1, &info_log_len, &info_log[0]); + info_log.erase(info_log_len); + if(module && module->get_format()==Module::GLSL) + info_log = static_cast(module)->get_source_map().translate_errors(info_log); + + if(!compiled) + throw compile_error(info_log); +#ifdef DEBUG + if(!info_log.empty()) + IO::print("Shader compile info log:\n%s", info_log); +#endif +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +void Program::attach_shader(Shader &shader) +{ + unsigned shader_id = shader.steal_id(); + if(!shader_id) + throw invalid_argument("Program::attach_shader"); + stage_ids.push_back(shader_id); + compile_glsl_stage(shader_id); +} + +void Program::attach_shader_owned(Shader *shader) +{ + attach_shader(*shader); + delete shader; +} + +void Program::detach_shader(Shader &) +{ +} + +const vector &Program::get_attached_shaders() const +{ + static vector dummy; + return dummy; +} + void Program::bind_attribute(unsigned index, const string &name) { static Require _req(ARB_vertex_shader); glBindAttribLocation(id, index, name.c_str()); } -void Program::bind_attribute(VertexComponent comp, const string &name) +void Program::bind_attribute(VertexAttribute attr, const string &name) { - bind_attribute(get_component_type(comp), name); + bind_attribute(get_attribute_semantic(attr), name); } void Program::bind_fragment_data(unsigned index, const string &name) @@ -113,22 +219,28 @@ void Program::bind_fragment_data(unsigned index, const string &name) static Require _req(EXT_gpu_shader4); glBindFragDataLocation(id, index, name.c_str()); } +#pragma GCC diagnostic pop void Program::link() { - for(ShaderList::iterator i=shaders.begin(); i!=shaders.end(); ++i) - if(!(*i)->is_compiled()) - (*i)->compile(); + if(stage_ids.empty()) + throw invalid_operation("Program::link"); uniforms.clear(); glLinkProgram(id); linked = get_program_i(id, GL_LINK_STATUS); - if(!linked) - throw compile_error(get_info_log()); + GLsizei info_log_len = get_program_i(id, GL_INFO_LOG_LENGTH); + string info_log(info_log_len+1, 0); + glGetProgramInfoLog(id, info_log_len+1, &info_log_len, &info_log[0]); + info_log.erase(info_log_len); + if(module && module->get_format()==Module::GLSL) + info_log = static_cast(module)->get_source_map().translate_errors(info_log); + + if(!linked) + throw compile_error(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 @@ -142,21 +254,6 @@ void Program::link() require_type(i->second.type); } -void Program::require_type(GLenum t) -{ - switch(t) - { - case GL_FLOAT_MAT2x3: - case GL_FLOAT_MAT2x4: - case GL_FLOAT_MAT3x2: - case GL_FLOAT_MAT3x4: - case GL_FLOAT_MAT4x2: - case GL_FLOAT_MAT4x3: - { static Require _req(NV_non_square_matrices); } - break; - } -} - void Program::query_uniforms() { unsigned count = get_program_i(id, GL_ACTIVE_UNIFORMS); @@ -178,10 +275,10 @@ void Program::query_uniforms() UniformInfo &info = uniforms[name]; info.block = 0; info.name = name; - info.size = size; + info.array_size = size; info.array_stride = 0; info.matrix_stride = 0; - info.type = type; + info.type = from_gl_type(type); uniforms_by_index[i] = &info; } } @@ -246,7 +343,7 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde indices2.clear(); for(vector::iterator j=indices.begin(); j!=indices.end(); ++j) - if(uniforms_by_index[*j]->size>1) + if(uniforms_by_index[*j]->array_size>1) indices2.push_back(*j); if(!indices2.empty()) { @@ -258,10 +355,8 @@ void Program::query_uniform_blocks(const vector &uniforms_by_inde indices2.clear(); for(vector::iterator j=indices.begin(); j!=indices.end(); ++j) { - GLenum t = uniforms_by_index[*j]->type; - if(t==GL_FLOAT_MAT4 || t==GL_FLOAT_MAT3 || t==GL_FLOAT_MAT2 || - t==GL_FLOAT_MAT2x3 || t==GL_FLOAT_MAT2x4 || t==GL_FLOAT_MAT3x2 || - t==GL_FLOAT_MAT3x4 || t==GL_FLOAT_MAT4x2 || t==GL_FLOAT_MAT4x3) + DataType t = uniforms_by_index[*j]->type; + if(is_matrix(t)) indices2.push_back(*j); } if(!indices2.empty()) @@ -300,8 +395,8 @@ void Program::query_attributes() AttributeInfo &info = attributes[name]; info.name = name; info.location = glGetAttribLocation(id, name); - info.size = size; - info.type = type; + info.array_size = size; + info.type = from_gl_type(type); } } } @@ -310,7 +405,7 @@ Program::LayoutHash Program::compute_layout_hash(const vector::const_iterator i = uniforms.begin(); i!=uniforms.end(); ++i) - layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->size); + layout_descriptor += format("%d:%s:%x:%d\n", (*i)->location, (*i)->name, (*i)->type, (*i)->array_size); return hash32(layout_descriptor); } @@ -384,9 +479,12 @@ void Program::unbind() } -Program::Loader::Loader(Program &p): - DataFile::ObjectLoader(p) +Program::Loader::Loader(Program &p, Collection &c): + DataFile::CollectionObjectLoader(p, &c) { + add("module", &Loader::module); + + // Deprecated add("attribute", &Loader::attribute); add("fragment_shader", &Loader::fragment_shader); add("geometry_shader", &Loader::geometry_shader); @@ -398,6 +496,16 @@ void Program::Loader::finish() obj.link(); } +void Program::Loader::module(const string &n) +{ + map spec_values; + SpecializationLoader ldr(spec_values); + load_sub_with(ldr); + obj.add_stages(get_collection().get(n), spec_values); +} + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" void Program::Loader::attribute(unsigned i, const string &n) { obj.bind_attribute(i, n); @@ -417,6 +525,32 @@ void Program::Loader::vertex_shader(const string &src) { obj.attach_shader_owned(new VertexShader(src)); } +#pragma GCC diagnostic pop + + +DataFile::Loader::ActionMap Program::SpecializationLoader::shared_actions; + +Program::SpecializationLoader::SpecializationLoader(map &sv): + spec_values(sv) +{ + set_actions(shared_actions); +} + +void Program::SpecializationLoader::init_actions() +{ + add("specialize", &SpecializationLoader::specialize_bool); + add("specialize", &SpecializationLoader::specialize_int); +} + +void Program::SpecializationLoader::specialize_bool(const string &name, bool value) +{ + spec_values[name] = value; +} + +void Program::SpecializationLoader::specialize_int(const string &name, int value) +{ + spec_values[name] = value; +} } // namespace GL } // namespace Msp