1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-15 18:57:08 +00:00

Optimize shaders code. Add settings at compile time.

This commit is contained in:
RealBadAngel 2013-12-09 13:25:43 +01:00
parent 50b0e9f7a4
commit dae03382bf
13 changed files with 181 additions and 195 deletions

View file

@ -815,44 +815,6 @@ public:
services->setPixelShaderConstant("eyePosition", (irr::f32*)&eye_position, 3);
services->setVertexShaderConstant("eyePosition", (irr::f32*)&eye_position, 3);
float enable_bumpmapping = 0;
if (g_settings->getBool("enable_bumpmapping"))
enable_bumpmapping = 1;
services->setPixelShaderConstant("enableBumpmapping", &enable_bumpmapping, 1);
float enable_parallax_occlusion = 0;
if (g_settings->getBool("enable_parallax_occlusion")) {
enable_parallax_occlusion = 1;
float parallax_occlusion_scale = g_settings->getFloat("parallax_occlusion_scale");
services->setPixelShaderConstant("parallaxOcclusionScale", &parallax_occlusion_scale, 1);
float parallax_occlusion_bias = g_settings->getFloat("parallax_occlusion_bias");
services->setPixelShaderConstant("parallaxOcclusionBias", &parallax_occlusion_bias, 1);
}
services->setPixelShaderConstant("enableParallaxOcclusion", &enable_parallax_occlusion, 1);
float enable_waving_water = 0;
if (g_settings->getBool("enable_waving_water")){
enable_waving_water = 1;
float water_wave_height_f = g_settings->getFloat("water_wave_height");
services->setVertexShaderConstant("waterWaveHeight", &water_wave_height_f, 1);
float water_wave_length_f = g_settings->getFloat("water_wave_length");
services->setVertexShaderConstant("waterWaveLength", &water_wave_length_f, 1);
float water_wave_speed_f = g_settings->getFloat("water_wave_speed");
services->setVertexShaderConstant("waterWaveSpeed", &water_wave_speed_f, 1);
}
services->setVertexShaderConstant("enableWavingWater", &enable_waving_water, 1);
float enable_waving_leaves = 0;
if (g_settings->getBool("enable_waving_leaves"))
enable_waving_leaves = 1;
services->setVertexShaderConstant("enableWavingLeaves", &enable_waving_leaves, 1);
float enable_waving_plants = 0;
if (g_settings->getBool("enable_waving_plants"))
enable_waving_plants = 1;
services->setVertexShaderConstant("enableWavingPlants", &enable_waving_plants, 1);
// Normal map texture layer
int layer1 = 1;
int layer2 = 2;

View file

@ -1111,6 +1111,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data):
*/
bool enable_shaders = g_settings->getBool("enable_shaders");
bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
video::E_MATERIAL_TYPE shadermat1, shadermat2, shadermat3,
shadermat4, shadermat5;
@ -1210,7 +1211,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data):
if (enable_shaders) {
ITextureSource *tsrc = data->m_gamedef->tsrc();
material.setTexture(2, tsrc->getTexture("disable_img.png"));
if (enable_bumpmapping) {
if (enable_bumpmapping || enable_parallax_occlusion) {
std::string fname_base = tsrc->getTextureName(p.tile.texture_id);
std::string normal_ext = "_normal.png";
size_t pos = fname_base.find(".");
@ -1292,7 +1293,8 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
{
bool enable_shaders = g_settings->getBool("enable_shaders");
bool enable_bumpmapping = g_settings->getBool("enable_bumpmapping");
bool enable_parallax_occlusion = g_settings->getBool("enable_parallax_occlusion");
if(!m_has_animation)
{
m_animation_force_timer = 100000;
@ -1362,7 +1364,7 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat
// Set the texture
buf->getMaterial().setTexture(0, tsrc->getTexture(os.str()));
buf->getMaterial().setTexture(2, tsrc->getTexture("disable_img.png"));
if (enable_shaders && enable_bumpmapping)
if (enable_shaders && (enable_bumpmapping || enable_parallax_occlusion))
{
std::string fname_base,fname_normal;
fname_base = tsrc->getTextureName(tile.texture_id);

View file

@ -673,15 +673,51 @@ ShaderInfo generate_shader(std::string name, IrrlichtDevice *device,
if(vertex_program == "" && pixel_program == "" && geometry_program == "")
return shaderinfo;
if (g_settings->getBool("enable_bumpmapping") || g_settings->getBool("enable_parallax_occlusion")) {
if(vertex_program != "")
vertex_program = "#define NORMALS\n" + vertex_program;
if(pixel_program != "")
pixel_program = "#define NORMALS\n" + pixel_program;
if(geometry_program != "")
geometry_program = "#define NORMALS\n" + geometry_program;
}
// Create shaders header
std::string shaders_header = "#version 120\n";
if (g_settings->getBool("enable_bumpmapping"))
shaders_header += "#define ENABLE_BUMPMAPPING\n";
if (g_settings->getBool("enable_parallax_occlusion")){
shaders_header += "#define ENABLE_PARALLAX_OCCLUSION\n";
shaders_header += "#define PARALLAX_OCCLUSION_SCALE ";
shaders_header += ftos(g_settings->getFloat("parallax_occlusion_scale"));
shaders_header += "\n";
shaders_header += "#define PARALLAX_OCCLUSION_BIAS ";
shaders_header += ftos(g_settings->getFloat("parallax_occlusion_bias"));
shaders_header += "\n";
}
if (g_settings->getBool("enable_bumpmapping") || g_settings->getBool("enable_parallax_occlusion"))
shaders_header += "#define USE_NORMALMAPS\n";
if (g_settings->getBool("enable_waving_water")){
shaders_header += "#define ENABLE_WAVING_WATER\n";
shaders_header += "#define WATER_WAVE_HEIGHT ";
shaders_header += ftos(g_settings->getFloat("water_wave_height"));
shaders_header += "\n";
shaders_header += "#define WATER_WAVE_LENGTH ";
shaders_header += ftos(g_settings->getFloat("water_wave_length"));
shaders_header += "\n";
shaders_header += "#define WATER_WAVE_SPEED ";
shaders_header += ftos(g_settings->getFloat("water_wave_speed"));
shaders_header += "\n";
}
if (g_settings->getBool("enable_waving_leaves"))
shaders_header += "#define ENABLE_WAVING_LEAVES\n";
if (g_settings->getBool("enable_waving_plants"))
shaders_header += "#define ENABLE_WAVING_PLANTS\n";
if(pixel_program != "")
pixel_program = shaders_header + pixel_program;
if(vertex_program != "")
vertex_program = shaders_header + vertex_program;
if(geometry_program != "")
geometry_program = shaders_header + geometry_program;
// Call addHighLevelShaderMaterial() or addShaderMaterial()
const c8* vertex_program_ptr = 0;
const c8* pixel_program_ptr = 0;