X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbackends%2Fopengl%2Fprogram_backend.cpp;h=e83a94844dc4e79e28986e0d8dcf8e835e9cad7e;hb=89347a620294a1136ee111edeadec68390654f78;hp=767811802ebf5c10eded6718324f7f1a106f4ab7;hpb=282a10854eda602d874e200c8301cf57d6501e81;p=libs%2Fgl.git diff --git a/source/backends/opengl/program_backend.cpp b/source/backends/opengl/program_backend.cpp index 76781180..e83a9484 100644 --- a/source/backends/opengl/program_backend.cpp +++ b/source/backends/opengl/program_backend.cpp @@ -12,6 +12,7 @@ #include #include #include +#include "device.h" #include "error.h" #include "program.h" #include "program_backend.h" @@ -45,6 +46,17 @@ OpenGLProgram::OpenGLProgram() id = glCreateProgram(); } +OpenGLProgram::OpenGLProgram(OpenGLProgram &&other): + id(other.id), + linked(other.linked), + uniform_calls(move(other.uniform_calls)), + debug_name(move(other.debug_name)) +{ + move(other.stage_ids, other.stage_ids+MAX_STAGES, stage_ids); + other.id = 0; + fill(other.stage_ids, other.stage_ids+MAX_STAGES, 0); +} + OpenGLProgram::~OpenGLProgram() { for(unsigned i=0; i &spec_values, TransientData &transient) +void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map &spec_values) { - SL::Compiler compiler; + SL::Compiler compiler(Device::get_current().get_info().glsl_features); compiler.set_source(mod.get_prepared_source(), ""); compiler.specialize(spec_values); compiler.compile(SL::Compiler::PROGRAM); @@ -134,11 +146,44 @@ void OpenGLProgram::add_glsl_stages(const GlslModule &mod, const map(this)->reflect_data; rd.n_clip_distances = compiler.get_n_clip_distances(); + + link(mod); + query_uniforms(); + query_attributes(); + + const map &block_bindings = compiler.get_uniform_block_bindings(); + if(!block_bindings.empty()) + { + for(unsigned i=0; isecond); + rd.uniform_blocks[i].bind_point = j->second; + } + } + } + + const map &tex_bindings = compiler.get_texture_bindings(); + if(!tex_bindings.empty()) + { + if(!ARB_separate_shader_objects) + glUseProgram(id); + for(const auto &kvp: tex_bindings) + { + int location = static_cast(this)->get_uniform_location(kvp.first); + if(location>=0) + { + if(ARB_separate_shader_objects) + glProgramUniform1i(id, location, kvp.second); + else + glUniform1i(location, kvp.second); + } + } + } } void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id) @@ -162,7 +207,7 @@ void OpenGLProgram::compile_glsl_stage(const GlslModule &mod, unsigned stage_id) #endif } -void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map &spec_values, TransientData &transient) +void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const map &spec_values) { static Require _req(ARB_gl_spirv); static Require _req2(ARB_ES2_compatibility); @@ -201,7 +246,6 @@ void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const mapsecond); - transient.spec_values[c.constant_id] = i->second; } } @@ -209,9 +253,11 @@ void OpenGLProgram::add_spirv_stages(const SpirVModule &mod, const mapname.c_str(), spec_id_array.size(), &spec_id_array[0], &spec_value_array[0]); + + link(mod); } -void OpenGLProgram::finalize(const Module &mod) +void OpenGLProgram::link(const Module &mod) { glLinkProgram(id); int status = 0; @@ -256,7 +302,7 @@ void OpenGLProgram::query_uniforms() if(len>3 && !strcmp(name+len-3, "[0]")) name[len-3] = 0; - rd.uniforms.push_back(ReflectData::UniformInfo()); + rd.uniforms.emplace_back(); ReflectData::UniformInfo &info = rd.uniforms.back(); info.name = name; info.tag = name; @@ -278,7 +324,7 @@ void OpenGLProgram::query_uniforms() query_uniform_blocks(uniforms_by_index); } - rd.uniform_blocks.push_back(ReflectData::UniformBlockInfo()); + rd.uniform_blocks.emplace_back(); ReflectData::UniformBlockInfo &default_block = rd.uniform_blocks.back(); for(ReflectData::UniformInfo &u: rd.uniforms) @@ -291,57 +337,8 @@ void OpenGLProgram::query_uniforms() u.matrix_stride = get_type_size(get_matrix_column_type(u.type)); default_block.uniforms.push_back(&u); - if(u.location>=0) - { - UniformCall::FuncPtr func = 0; - if(is_image(u.type)) - glGetUniformiv(id, u.location, &u.binding); - else if(u.type==FLOAT) - func = &uniform_wrapper; - else if(u.type==FLOAT_VEC2) - func = &uniform_wrapper; - else if(u.type==FLOAT_VEC3) - func = &uniform_wrapper; - else if(u.type==FLOAT_VEC4) - func = &uniform_wrapper; - else if(u.type==INT) - func = &uniform_wrapper; - else if(u.type==INT_VEC2) - func = &uniform_wrapper; - else if(u.type==INT_VEC3) - func = &uniform_wrapper; - else if(u.type==INT_VEC4) - func = &uniform_wrapper; - else if(u.type==UNSIGNED_INT) - func = &uniform_wrapper; - else if(u.type==UINT_VEC2) - func = &uniform_wrapper; - else if(u.type==UINT_VEC3) - func = &uniform_wrapper; - else if(u.type==UINT_VEC4) - func = &uniform_wrapper; - else if(u.type==FLOAT_MAT2) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT3) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT4) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT2x3) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT3x2) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT2x4) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT4x2) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT3x4) - func = &uniform_matrix_wrapper; - else if(u.type==FLOAT_MAT4x3) - func = &uniform_matrix_wrapper; - - if(func) - uniform_calls.push_back(UniformCall(u.location, u.array_size, func)); - } + if(is_image(u.type) && u.location>=0) + glGetUniformiv(id, u.location, &u.binding); } default_block.sort_uniforms(); @@ -362,7 +359,7 @@ void OpenGLProgram::query_uniform_blocks(const vector3 && !strcmp(name+len-3, "[0]")) name[len-3] = 0; - rd.attributes.push_back(ReflectData::AttributeInfo()); + rd.attributes.emplace_back(); ReflectData::AttributeInfo &info = rd.attributes.back(); info.name = name; info.location = glGetAttribLocation(id, name); @@ -449,31 +446,68 @@ void OpenGLProgram::query_attributes() } } -void OpenGLProgram::apply_bindings(const TransientData &transient) +void OpenGLProgram::finalize_uniforms() { ReflectData &rd = static_cast(this)->reflect_data; - for(unsigned i=0; iuniforms.empty()) { - auto j = transient.blocks.find(rd.uniform_blocks[i].name); - if(j!=transient.blocks.end()) - { - glUniformBlockBinding(id, i, j->second); - rd.uniform_blocks[i].bind_point = j->second; - } - } + for(const ReflectData::UniformInfo *u: i->uniforms) + if(u->location>=0) + { + UniformCall::FuncPtr func = 0; + if(u->type==FLOAT) + func = &uniform_wrapper; + else if(u->type==FLOAT_VEC2) + func = &uniform_wrapper; + else if(u->type==FLOAT_VEC3) + func = &uniform_wrapper; + else if(u->type==FLOAT_VEC4) + func = &uniform_wrapper; + else if(u->type==INT) + func = &uniform_wrapper; + else if(u->type==INT_VEC2) + func = &uniform_wrapper; + else if(u->type==INT_VEC3) + func = &uniform_wrapper; + else if(u->type==INT_VEC4) + func = &uniform_wrapper; + else if(u->type==UNSIGNED_INT) + func = &uniform_wrapper; + else if(u->type==UINT_VEC2) + func = &uniform_wrapper; + else if(u->type==UINT_VEC3) + func = &uniform_wrapper; + else if(u->type==UINT_VEC4) + func = &uniform_wrapper; + else if(u->type==FLOAT_MAT2) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT3) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT4) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT2x3) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT3x2) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT2x4) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT4x2) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT3x4) + func = &uniform_matrix_wrapper; + else if(u->type==FLOAT_MAT4x3) + func = &uniform_matrix_wrapper; - if(!ARB_separate_shader_objects) - glUseProgram(id); - for(const auto &kvp: transient.textures) - { - int location = static_cast(this)->get_uniform_location(kvp.first); - if(location>=0) + if(func) + uniform_calls.push_back(UniformCall(u->location, u->array_size, func)); + } + + if(i->data_size<=0) { - if(ARB_separate_shader_objects) - glProgramUniform1i(id, location, kvp.second); - else - glUniform1i(location, kvp.second); + const ReflectData::UniformInfo &last = *i->uniforms.back(); + i->data_size = last.location*16+last.array_size*get_type_size(last.type); } } }