-#include <msp/core/algorithm.h>
-#include <msp/datafile/collection.h>
-#include <msp/io/print.h>
-#include <msp/strings/format.h>
-#include "error.h"
-#include "renderpass.h"
-#include "program.h"
-#include "programdata.h"
-#include "renderer.h"
-#include "texture.h"
-#include "texture2d.h"
-#include "uniform.h"
-
-using namespace std;
-
-namespace Msp {
-namespace GL {
-
-RenderPass::RenderPass():
- shprog(0),
- shprog_from_material(false),
- shdata(0),
- material(0),
- face_cull(CULL_BACK),
- receive_shadows(false),
- image_based_lighting(false)
-{ }
-
-void RenderPass::set_material_textures()
-{
- const Tag *material_texture_tags = material->get_texture_tags();
- for(const Tag *tag=material_texture_tags; tag->id; ++tag)
- set_texture(*tag, material->get_texture(*tag), material->get_sampler(*tag));
-}
-
-void RenderPass::maybe_create_material_shader()
-{
- if(shprog && !shprog_from_material)
- return;
-
- map<string, int> extra_spec;
- if(receive_shadows)
- extra_spec["use_shadow_map"] = true;
- if(image_based_lighting)
- extra_spec["use_image_based_lighting"] = true;
-
- shprog = material->create_compatible_shader(extra_spec);
-
- if(shdata)
- shdata = new ProgramData(*shdata, shprog);
-
- shprog_from_material = true;
-}
-
-void RenderPass::set_shader_program(const Program *prog, const ProgramData *data)
-{
- shprog = prog;
- shprog_from_material = false;
- shdata = (data ? new ProgramData(*data) : 0);
-}
-
-Tag RenderPass::get_slotted_uniform_tag(Tag slot) const
-{
- map<Tag, Tag>::const_iterator i = uniform_slots.find(slot);
- if(i==uniform_slots.end())
- return Tag();
- return i->second;
-}
-
-void RenderPass::set_material(const Material *mat)
-{
- material = mat;
- maybe_create_material_shader();
- set_material_textures();
-}
-
-void RenderPass::set_texture(Tag tag, const Texture *tex, const Sampler *samp)
-{
- vector<TextureSlot>::iterator i = find_member(textures, tag, &TextureSlot::tag);
- if(i==textures.end())
- {
- textures.push_back(TextureSlot(tag));
- i = textures.end()-1;
- }
- i->texture = tex;
- if(samp)
- i->sampler = samp;
-}
-
-Tag RenderPass::get_texture_tag(const string &slot) const
-{
- vector<TextureSlot>::const_iterator i = find_member(textures, slot, &TextureSlot::slot_name);
- return (i!=textures.end() ? i->tag : Tag());
-}
-
-void RenderPass::set_texture(unsigned index, const Texture *tex, const Sampler *samp)
-{
- if(!shprog)
- throw invalid_operation("RenderPass::set_texture");
-
- const vector<Program::UniformInfo> &uniforms = shprog->get_uniforms();
- for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(is_image(i->type) && i->binding==static_cast<int>(index))
- return set_texture(i->tag, tex, samp);
-
- if(shdata)
- {
- const vector<Tag> &tags = shdata->get_uniform_tags();
- for(vector<Tag>::const_iterator i=tags.begin(); i!=tags.end(); ++i)
- {
- vector<Program::UniformInfo>::const_iterator j = find_member(uniforms, *i, &Program::UniformInfo::tag);
- if(j==uniforms.end() || !is_image(j->type))
- continue;
- if(const Uniform1i *uni1i = dynamic_cast<const Uniform1i *>(shdata->find_uniform(*i)))
- if(uni1i->get()==static_cast<int>(index))
- return set_texture(*i, tex, samp);
- }
- }
-}
-
-int RenderPass::get_texture_index(const string &n) const
-{
- vector<TextureSlot>::const_iterator i = find_member(textures, n, &TextureSlot::slot_name);
- return (shprog && i!=textures.end() ? shprog->get_uniform_binding(i->tag) : -1);
-}
-
-void RenderPass::set_face_cull(CullMode fc)
-{
- face_cull = fc;
-}
-
-void RenderPass::set_receive_shadows(bool rs)
-{
- receive_shadows = rs;
-}
-
-void RenderPass::apply(Renderer &renderer) const
-{
- for(vector<TextureSlot>::const_iterator i=textures.begin(); i!=textures.end(); ++i)
- renderer.set_texture(i->tag, i->texture, i->sampler);
- renderer.set_shader_program(shprog, shdata.get());
- if(material)
- renderer.add_shader_data(material->get_shader_data());
- renderer.set_face_cull(face_cull);
-}
-
-void RenderPass::set_debug_name(const string &name)
-{
-#ifdef DEBUG
- if(shdata.refcount()==1)
- shdata->set_debug_name(name+" [UBO]");
-#else
- (void)name;
-#endif
-}
-
-
-DataFile::Loader::ActionMap RenderPass::Loader::shared_actions;
-
-RenderPass::Loader::Loader(RenderPass &p):
- DataFile::CollectionObjectLoader<RenderPass>(p, 0)
-{
- set_actions(shared_actions);
-}
-
-RenderPass::Loader::Loader(RenderPass &p, Collection &c):
- DataFile::CollectionObjectLoader<RenderPass>(p, &c)
-{
- set_actions(shared_actions);
-}
-
-void RenderPass::Loader::init_actions()
-{
- add("face_cull", &RenderPass::face_cull);
- add("shader", &Loader::shader);
- add("image_based_lighting", &RenderPass::image_based_lighting);
- add("material", &Loader::material_inline);
- add("material", &Loader::material);
- add("material_slot", &RenderPass::material_slot);
- add("receive_shadows", &RenderPass::receive_shadows);
- add("texture", &Loader::texture);
- add("uniforms", &Loader::uniforms);
- add("uniform_slot", &Loader::uniform_slot);
- add("uniform_slot", &Loader::uniform_slot2);
-
- // Deprecated
- add("texunit", &Loader::texunit);
- add("texunit", &Loader::texture);
- add("texunit", &Loader::texunit_named);
-}
-
-void RenderPass::Loader::set_inline_base_name(const string &n)
-{
- inline_base_name = n;
-}
-
-void RenderPass::Loader::finish()
-{
- if(obj.material)
- obj.maybe_create_material_shader();
-}
-
-// Temporary compatibility feature
-string RenderPass::Loader::get_shader_name(const string &n)
-{
- if(n.size()>=5 && !n.compare(n.size()-5, 5, ".glsl"))
- {
- IO::print(IO::cerr, "Warning: Loading module '%s' as shader is deprecated\n", n);
- return n+".shader";
- }
- return n;
-}
-
-void RenderPass::Loader::material_inline()
-{
- Material::GenericLoader ldr(coll);
- load_sub_with(ldr);
- RefPtr<Material> mat = ldr.get_material();
- get_collection().add(inline_base_name+".mat", mat.get());
- obj.material = mat.release();
- obj.set_material_textures();
-}
-
-void RenderPass::Loader::material(const string &name)
-{
- obj.material = &get_collection().get<Material>(name);
- obj.set_material_textures();
-}
-
-void RenderPass::Loader::shader(const string &n)
-{
- obj.shprog = &get_collection().get<Program>(get_shader_name(n));
- obj.shprog_from_material = false;
- if(obj.shdata)
- obj.shdata = new ProgramData(*obj.shdata, obj.shprog);
-}
-
-void RenderPass::Loader::texture(const string &n)
-{
- vector<TextureSlot>::iterator i = find_member(obj.textures, Tag(n), &TextureSlot::tag);
- if(i==obj.textures.end())
- {
- obj.textures.push_back(TextureSlot(n));
- i = obj.textures.end()-1;
- }
- TextureSlot::Loader ldr(*i, n, coll);
- load_sub_with(ldr);
-}
-
-void RenderPass::Loader::texunit(unsigned)
-{
- IO::print(IO::cerr, "Warning: specifying textures by unit number is deprecated and may not produce expected results");
- string name;
- if(obj.shprog)
- {
- const vector<Program::UniformInfo> &uniforms = obj.shprog->get_uniforms();
- for(vector<Program::UniformInfo>::const_iterator i=uniforms.begin(); i!=uniforms.end(); ++i)
- if(is_image(i->type) && i->binding>=0)
- {
- if(!name.empty())
- {
- name.clear();
- break;
- }
- name = i->name;
- }
- }
-
- if(name.empty())
- throw runtime_error("Could not determine name for texture");
-
- texture(name);
-}
-
-void RenderPass::Loader::texunit_named(unsigned, const string &n)
-{
- texture(n);
-}
-
-void RenderPass::Loader::uniforms()
-{
- if(!obj.shprog || obj.shprog_from_material)
- throw runtime_error("Shader is required for uniforms");
- if(!obj.shdata)
- obj.shdata = new ProgramData(obj.shprog);
- else if(obj.shdata.refcount()>1)
- obj.shdata = new ProgramData(*obj.shdata);
- load_sub(*obj.shdata);
-}
-
-void RenderPass::Loader::uniform_slot(const string &name)
-{
- uniform_slot2(name, name);
-}
-
-void RenderPass::Loader::uniform_slot2(const string &name, const string &slot)
-{
- obj.uniform_slots[slot] = name;
-}
-
-
-RenderPass::TextureSlot::Loader::Loader(TextureSlot &ts, const string &an, Collection *c):
- CollectionObjectLoader<TextureSlot>(ts, c),
- auto_slot_name(an)
-{
- add("sampler", &TextureSlot::sampler);
- add("slot", &Loader::slot_auto);
- add("slot", &TextureSlot::slot_name);
- add("texture", &TextureSlot::texture);
-}
-
-void RenderPass::TextureSlot::Loader::slot_auto()
-{
- obj.slot_name = auto_slot_name;
-}
-
-} // namespace GL
-} // namespace Msp