#include <msp/fs/utils.h>
#include "animation.h"
#include "armature.h"
+#include "backend.h"
+#include "basicmaterial.h"
#include "camera.h"
+#include "directionallight.h"
+#include "error.h"
#include "font.h"
#include "keyframe.h"
#include "lighting.h"
-#include "material.h"
#include "mesh.h"
+#include "module.h"
#include "object.h"
-#include "pipelinetemplate.h"
+#include "occludedscene.h"
+#include "orderedscene.h"
+#include "pbrmaterial.h"
+#include "sequencetemplate.h"
+#include "pointlight.h"
#include "pose.h"
#include "program.h"
#include "resourcemanager.h"
#include "resources.h"
#include "sampler.h"
+#include "simplescene.h"
#include "technique.h"
#include "texture1d.h"
#include "texture2d.h"
#include "texture2darray.h"
#include "texturecube.h"
+#include "unlitmaterial.h"
+#include "zsortedscene.h"
#include "glsl/compiler.h"
using namespace std;
void init_shaderlib(DataFile::BuiltinSource &);
void init_builtin_data(DataFile::BuiltinSource &);
-Resources::Resources():
- default_tex_filter(Texture::can_generate_mipmap() ? LINEAR_MIPMAP_LINEAR : LINEAR),
- default_tex_anisotropy(1.0f),
- srgb_conversion(false),
+Resources *Resources::global_resources = 0;
+
+Resources::Resources(bool set_as_global):
resource_manager(0)
{
add_type<Animation>().suffix(".anim").keyword("animation");
add_type<Armature>().suffix(".arma").keyword("armature");
- add_type<Camera>().keyword("camera");
+ add_type<BasicMaterial>().base<Material>().suffix(".mat")
+ .creator([this](const string &n) -> BasicMaterial * { create_generic<Material>(n); return 0; })
+ .notify(&set_debug_name<Material>);
+ add_type<Camera>().keyword("camera")
+ .notify(&set_debug_name<Camera>);
+ add_type<DirectionalLight>().base<Light>().suffix(".light")
+ .creator([this](const string &n) -> DirectionalLight * { create_generic<Light>(n); return 0; });
add_type<Font>().keyword("font");
add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
- add_type<Lighting>().suffix(".lightn").keyword("lighting");
- add_type<Material>().suffix(".mat").creator(&Resources::create_material);
- add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
- add_type<Object>().keyword("object");
- add_type<PipelineTemplate>().suffix(".pipe").keyword("pipeline");
+ add_type<Lighting>().suffix(".lightn").keyword("lighting")
+ .notify(&set_debug_name<Lighting>);
+ add_type<Mesh>().keyword("mesh")
+ .creator([this](const string &n){ return create_mesh(n); })
+ .notify(&set_debug_name<Mesh>);
+ add_type<Module>().suffix(".glsl").suffix(".spv")
+ .creator([this](const string &n){ return create_module(n); });
+ add_type<Object>().base<Renderable>().keyword("object");
+ add_type<OccludedScene>().base<Scene>().base<Renderable>().suffix(".scene")
+ .creator([this](const string &n) -> OccludedScene * { create_generic<Scene>(n); return 0; });
+ add_type<OrderedScene>().base<Scene>().base<Renderable>().suffix(".scene")
+ .creator([this](const string &n) -> OrderedScene * { create_generic<Scene>(n); return 0; });
+ add_type<PbrMaterial>().base<Material>().suffix(".mat")
+ .creator([this](const string &n) -> PbrMaterial * { create_generic<Material>(n); return 0; })
+ .notify(&set_debug_name<Material>);
+ add_type<PointLight>().base<Light>().suffix(".light")
+ .creator([this](const string &n) -> PointLight * { create_generic<Light>(n); return 0; });
+ add_type<SequenceTemplate>().suffix(".seq").keyword("sequence");
add_type<Pose>().keyword("pose");
- add_type<Program>().keyword("shader").suffix(".glsl").creator(&Resources::create_program);
- add_type<Sampler>().suffix(".samp").keyword("sampler");
- add_type<Technique>().suffix(".tech").keyword("technique");
- add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d");
- add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
- add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d");
- add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
- add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array");
+ add_type<Program>().keyword("shader")
+ .creator([this](const string &n){ return create_program(n); })
+ .notify(&set_debug_name<Program>);
+ add_type<Sampler>().suffix(".samp").keyword("sampler")
+ .notify(&set_debug_name<Sampler>);
+ add_type<SimpleScene>().base<Scene>().base<Renderable>().suffix(".scene")
+ .creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
+ add_type<Technique>().suffix(".tech").keyword("technique")
+ .notify(&set_debug_name<Technique>);
+ add_type<Texture1D>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; })
+ .notify(&set_debug_name<Texture1D>);
+ add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
+ .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; })
+ .notify(&set_debug_name<Texture2D>);
+ add_type<Texture3D>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; })
+ .notify(&set_debug_name<Texture3D>);
+ add_type<TextureCube>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; })
+ .notify(&set_debug_name<TextureCube>);
+ add_type<Texture2DArray>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; })
+ .notify(&set_debug_name<Texture2DArray>);
+ add_type<UnlitMaterial>().base<Material>().suffix(".mat")
+ .creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
+ .notify(&set_debug_name<Material>);
+ add_type<ZSortedScene>().base<Scene>().base<Renderable>().suffix(".scene")
+ .creator([this](const string &n) -> ZSortedScene * { create_generic<Scene>(n); return 0; });
add_source(get_builtins());
+
+ if(set_as_global && !global_resources)
+ global_resources = this;
+}
+
+Resources::~Resources()
+{
+ if(this==global_resources)
+ global_resources = 0;
+}
+
+Resources &Resources::get_global()
+{
+ if(!global_resources)
+ throw invalid_operation("no global resources");
+ return *global_resources;
}
const DataFile::CollectionSource &Resources::get_builtins()
return builtins;
}
-void Resources::set_default_texture_filter(TextureFilter tf)
-{
- default_tex_filter = tf;
-}
-
-void Resources::set_default_texture_anisotropy(float a)
-{
- default_tex_anisotropy = a;
-}
-
-void Resources::set_srgb_conversion(bool c)
-{
- srgb_conversion = c;
-}
-
void Resources::set_resource_manager(ResourceManager *m)
{
resource_manager = m;
}
-Material *Resources::create_material(const string &name)
+template<typename T, typename L>
+T *Resources::create_generic(const string &name)
{
if(RefPtr<IO::Seekable> io = open_raw(name))
{
DataFile::Parser parser(*io, name);
- Material::GenericLoader ldr(this);
+ L ldr(*this);
ldr.load(parser);
- return ldr.get_material();
+ ldr.store_object(*this, name);
}
return 0;
Mesh *Resources::create_mesh(const string &name)
{
- if(!resource_manager)
+ if(!resource_manager || name[0]=='_')
return 0;
if(RefPtr<IO::Seekable> io = open_raw(name))
{
- RefPtr<Mesh> mesh = new Mesh(resource_manager);
+ RefPtr<Mesh> mesh = new Mesh;
+ mesh->set_manager(resource_manager);
resource_manager->set_resource_location(*mesh, *this, name);
return mesh.release();
}
return 0;
}
-Texture2D *Resources::create_texture2d(const string &name)
+Texture *Resources::create_texture(const string &name)
{
+ bool managed = (resource_manager && name[0]!='_');
+
string ext = FS::extpart(name);
- if(ext==".tex2d" && !resource_manager)
- return 0;
+ if(ext==".tex")
+ {
+ if(managed)
+ return create_generic<Texture, GenericResourceLoader<Texture>>(name);
+ else
+ return create_generic<Texture>(name);
+ }
if(RefPtr<IO::Seekable> io = open_raw(name))
{
+ RefPtr<Texture2D> tex;
+
+ // Verify that the image is loadable
Graphics::Image image;
- if(!resource_manager)
+ if(!managed)
image.load_io(*io);
- RefPtr<Texture2D> tex = new Texture2D(resource_manager);
+ tex = new Texture2D;
- if(ext==".tex2d")
- {
- DataFile::Parser parser(*io, name);
- Texture2D::Loader ldr(*tex, *this);
- ldr.load(parser);
- }
- else
+ if(managed)
{
- Sampler &samp = tex->get_default_sampler();
- if(is_mipmapped(default_tex_filter))
- {
- tex->set_auto_generate_mipmap(true);
- samp.set_mag_filter(LINEAR);
- }
- else
- samp.set_mag_filter(default_tex_filter);
- samp.set_min_filter(default_tex_filter);
- samp.set_max_anisotropy(default_tex_anisotropy);
- }
-
- if(resource_manager)
+ tex->set_manager(resource_manager);
resource_manager->set_resource_location(*tex, *this, name);
+ }
else
tex->image(image);
- return tex.release();
+
+ add(name, tex.get());
+ tex.release();
}
return 0;
}
-Program *Resources::create_program(const string &name)
+Module *Resources::create_module(const string &name)
{
string ext = FS::extpart(name);
- if(ext==".shader")
+ if(ext!=".glsl" && ext!=".spv")
return 0;
if(RefPtr<IO::Seekable> io = open_raw(name))
{
- SL::Compiler compiler;
- compiler.load_source(*io, this, name);
- compiler.compile();
- RefPtr<Program> program = new Program;
- compiler.add_shaders(*program);
- program->link();
- return program.release();
+ if(ext==".glsl")
+ {
+ RefPtr<Module> module;
+ if(get_backend_api()==VULKAN)
+ module = new SpirVModule;
+ else
+ module = new GlslModule;
+ module->load_source(*io, this, name);
+ return module.release();
+ }
+ else if(ext==".spv")
+ {
+ RefPtr<SpirVModule> module = new SpirVModule;
+ module->load_code(*io);
+ return module.release();
+ }
+ }
+ else if(ext==".spv")
+ {
+ if((io = open_raw(FS::basepart(name)+".glsl")))
+ {
+ RefPtr<SpirVModule> module = new SpirVModule;
+ module->load_source(*io, this, name);
+ return module.release();
+ }
+ }
+
+ return 0;
+}
+
+Program *Resources::create_program(const string &name)
+{
+ string ext = FS::extpart(name);
+ string base = FS::basepart(name);
+ string ext2 = FS::extpart(base);
+ if(ext==".shader" && (ext2==".glsl" || ext2==".spv"))
+ {
+ Module &module = get<Module>(base);
+ RefPtr<Program> shprog = new Program;
+ shprog->add_stages(module);
+ return shprog.release();
}
return 0;
}
+template<typename T>
+void Resources::set_debug_name(const string &name, T &item)
+{
+#ifdef DEBUG
+ item.set_debug_name(name);
+#endif
+}
+
+
+Resources::Loader::Loader(Resources &r):
+ DerivedObjectLoader<Resources, Collection::Loader>(r)
+{
+ add("light", &Loader::generic<Light>);
+ add("material", &Loader::generic<Material>);
+ add("scene", &Loader::generic<Scene>);
+ add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
+}
+
+template<typename T, typename L>
+void Resources::Loader::generic(const string &name)
+{
+ L ldr(obj);
+ load_sub_with(ldr);
+ ldr.store_object(obj, name);
+}
+
+
+template<typename T>
+void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
+{
+ T::GenericLoader::type(t);
+ this->object->set_manager(resources.resource_manager);
+}
+
} // namespace GL
} // namespace Msp