import msp_interface;
import shadow;
-const bool use_normal_map = false;
+layout(constant_id=auto) const bool use_normal_map = false;
#pragma MSP stage(vertex)
vec4 get_vertex_position()
import common;
import shadow;
-const bool use_base_color_map = false;
-const bool use_metalness_map = false;
-const bool use_roughness_map = false;
-const bool use_occlusion_map = false;
-const bool use_emission = false;
-const bool use_emission_map = false;
+layout(constant_id=auto) const bool use_base_color_map = false;
+layout(constant_id=auto) const bool use_metalness_map = false;
+layout(constant_id=auto) const bool use_roughness_map = false;
+layout(constant_id=auto) const bool use_occlusion_map = false;
+layout(constant_id=auto) const bool use_emission = false;
+layout(constant_id=auto) const bool use_emission_map = false;
const float PI = 3.1415926535;
import common;
import shadow;
-const bool use_diffuse_map = false;
-const bool use_specular = false;
-const bool use_specular_map = false;
-const bool use_shininess_map = false;
-const bool use_emission = false;
-const bool use_emission_map = false;
-const bool use_reflectivity = false;
-const bool use_reflectivity_map = false;
-const bool use_sky = false;
-const bool use_fog = false;
+layout(constant_id=auto) const bool use_diffuse_map = false;
+layout(constant_id=auto) const bool use_specular = false;
+layout(constant_id=auto) const bool use_specular_map = false;
+layout(constant_id=auto) const bool use_shininess_map = false;
+layout(constant_id=auto) const bool use_emission = false;
+layout(constant_id=auto) const bool use_emission_map = false;
+layout(constant_id=auto) const bool use_reflectivity = false;
+layout(constant_id=auto) const bool use_reflectivity_map = false;
+layout(constant_id=auto) const bool use_sky = false;
+layout(constant_id=auto) const bool use_fog = false;
#pragma MSP stage(fragment)
vec4 get_diffuse_color()
import msp_interface;
-const bool use_shadow_map = false;
+layout(constant_id=auto) const bool use_shadow_map = false;
#pragma MSP stage(vertex)
void shadow_transform(vec4 eye_vertex)
set_reflectivity(0.0f);
}
-string BasicMaterial::create_program_source() const
-{
- string source = "import phong;\n";
- if(diffuse.texture)
- source += "const bool use_diffuse_map = true;\n";
- if(specular.texture || specular.value.r || specular.value.g || specular.value.b)
- {
- source += "const bool use_specular = true;\n";
- if(specular.texture)
- source += "const bool use_specular_map = true;\n";
- if(shininess.texture)
- source += "const bool use_shininess_map = true;\n";
- }
- if(normal.texture)
- source += "const bool use_normal_map = true;\n";
- if(emission.texture || emission.value.r || emission.value.g || emission.value.b)
- {
- source += "const bool use_emission = true;\n";
- if(emission.texture)
- source += "const bool use_emission_map = true;\n";
- }
- if(reflectivity.value || reflectivity.texture)
- {
- source += "const bool use_reflectivity = true;\n";
- if (reflectivity.texture)
- source += "const bool use_reflectivity_map = true;\n";
- }
- if(receive_shadows)
- source += "const bool use_shadow_map = true;\n";
- return source;
+void BasicMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
+{
+ module_name = "phong.glsl";
+ spec_values["use_diffuse_map"] = (diffuse.texture!=0);
+ bool use_specular = (specular.texture || specular.value.r || specular.value.g || specular.value.b);
+ spec_values["use_specular"] = use_specular;
+ spec_values["use_specular_map"] = (specular.texture!=0);
+ spec_values["use_shininess_map"] = (use_specular && shininess.texture!=0);
+ spec_values["use_normal_map"] = (normal.texture!=0);
+ bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
+ spec_values["use_emission"] = use_emission;
+ spec_values["use_emission_map"] = (emission.texture!=0);
+ spec_values["use_reflectivity"] = (reflectivity.value!=0 || reflectivity.texture!=0);
+ spec_values["use_reflectivity_map"] = (reflectivity.texture!=0);
+ spec_values["use_shadow_map"] = receive_shadows;
}
void BasicMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
BasicMaterial();
protected:
- virtual std::string create_program_source() const;
+ virtual void fill_program_info(std::string &, std::map<std::string, int> &) const;
public:
virtual void attach_textures_to(Texturing &, ProgramData &) const;
namespace Msp {
namespace GL {
-Program *Material::create_compatible_shader() const
-{
- return new Program(create_program_source());
-}
-
const Program *Material::create_compatible_shader(DataFile::Collection &coll) const
{
- string source = create_program_source();
- string name = format("_material_%016x.glsl", hash64(source));
+ string module_name;
+ map<string, int> spec_values;
+ fill_program_info(module_name, spec_values);
+
+ string info = module_name;
+ for(map<string, int>::const_iterator i=spec_values.begin(); i!=spec_values.end(); ++i)
+ info += format(",%s:%d", i->first, i->second);
+
+ string name = format("_material_%016x.shader", hash64(info));
Program *shprog = coll.find<Program>(name);
if(shprog)
return shprog;
- shprog = new Program(create_program_source());
+ const Module &module = coll.get<Module>(module_name);
+ shprog = new Program(module, spec_values);
try
{
coll.add(name, shprog);
public:
virtual ~Material() { }
- virtual Program *create_compatible_shader() const;
virtual const Program *create_compatible_shader(DataFile::Collection &) const;
protected:
- virtual std::string create_program_source() const = 0;
+ virtual void fill_program_info(std::string &, std::map<std::string, int> &) const = 0;
public:
/** Returns the uniforms for the material. */
set_emission(0.0f);
}
-string PbrMaterial::create_program_source() const
-{
- string source = "import cooktorrance;\n";
- if(base_color.texture)
- source += "const bool use_base_color_map = true;\n";
- if(normal.texture)
- source += "const bool use_normal_map = true;\n";
- if(metalness.texture)
- source += "const bool use_metalness_map = true;\n";
- if(roughness.texture)
- source += "const bool use_roughness_map = true;\n";
- if(occlusion.texture)
- source += "const bool use_occlusion_map = true;\n";
- if(emission.texture || emission.value.r || emission.value.g || emission.value.b)
- {
- source += "const bool use_emission = true;\n";
- if(emission.texture)
- source += "const bool use_emission_map = true;\n";
- }
- if(receive_shadows)
- source += "const bool use_shadow_map = true;\n";
- return source;
+void PbrMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
+{
+ module_name = "cooktorrance.glsl";
+ spec_values["use_base_color_map"] = (base_color.texture!=0);
+ spec_values["use_normal_map"] = (normal.texture!=0);
+ spec_values["use_metalness_map"] = (metalness.texture!=0);
+ spec_values["use_roughness_map"] = (roughness.texture!=0);
+ spec_values["use_occlusion_map"] = (occlusion.texture!=0);
+ bool use_emission = (emission.texture || emission.value.r || emission.value.g || emission.value.b);
+ spec_values["use_emission"] = use_emission;
+ spec_values["use_emission_map"] = (emission.texture!=0);
+ spec_values["use_shadow_map"] = receive_shadows;
}
void PbrMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
PbrMaterial();
protected:
- virtual std::string create_program_source() const;
+ virtual void fill_program_info(std::string &, std::map<std::string, int> &) const;
public:
virtual void attach_textures_to(Texturing &, ProgramData &) const;
shprog.keep();
}
else
- shprog = material->create_compatible_shader();
+ throw invalid_operation("no collection");
if(shdata)
shdata = new ProgramData(*shdata, shprog.get());
set_tint(Color(1.0f));
}
-string UnlitMaterial::create_program_source() const
+void UnlitMaterial::fill_program_info(string &module_name, map<string, int> &spec_values) const
{
- string source = "import unlit;\n";
- if(texture)
- source += "const bool use_texture = true;\n";
- if(vertex_color)
- source += "const bool use_vertex_color = true;\n";
- return source;
+ module_name = "unlit.glsl";
+ spec_values["use_texture"] = (texture!=0);
+ spec_values["use_vertex_color"] = vertex_color;
}
void UnlitMaterial::attach_textures_to(Texturing &texturing, ProgramData &tex_shdata) const
UnlitMaterial();
protected:
- virtual std::string create_program_source() const;
+ virtual void fill_program_info(std::string &, std::map<std::string, int> &) const;
public:
virtual void attach_textures_to(Texturing &, ProgramData &) const;