Cleanup shader generation code (#10663)

Shader generation is a mess. This commit cleans some parts up, including dropping remains of HLSL support which was never actually implemented.
This commit is contained in:
Vitaliy 2020-12-19 22:57:10 +03:00 committed by GitHub
parent 664f5ce960
commit ccbf8029ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 157 additions and 377 deletions

@ -22,7 +22,7 @@ varying vec3 eyeVec;
const float fogStart = FOG_START; const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / ( 1.0 - fogStart); const float fogShadingParameter = 1.0 / ( 1.0 - fogStart);
#ifdef ENABLE_TONE_MAPPING #if ENABLE_TONE_MAPPING
/* Hable's UC2 Tone mapping parameters /* Hable's UC2 Tone mapping parameters
A = 0.22; A = 0.22;
@ -73,7 +73,7 @@ void main(void)
vec4 col = vec4(color.rgb * varColor.rgb, 1.0); vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
#ifdef ENABLE_TONE_MAPPING #if ENABLE_TONE_MAPPING
col = applyToneMapping(col); col = applyToneMapping(col);
#endif #endif

@ -19,7 +19,7 @@ const float BS = 10.0;
const float fogStart = FOG_START; const float fogStart = FOG_START;
const float fogShadingParameter = 1.0 / (1.0 - fogStart); const float fogShadingParameter = 1.0 / (1.0 - fogStart);
#ifdef ENABLE_TONE_MAPPING #if ENABLE_TONE_MAPPING
/* Hable's UC2 Tone mapping parameters /* Hable's UC2 Tone mapping parameters
A = 0.22; A = 0.22;
@ -75,7 +75,7 @@ void main(void)
col.rgb *= emissiveColor.rgb * vIDiff; col.rgb *= emissiveColor.rgb * vIDiff;
#ifdef ENABLE_TONE_MAPPING #if ENABLE_TONE_MAPPING
col = applyToneMapping(col); col = applyToneMapping(col);
#endif #endif

@ -51,12 +51,8 @@ public:
~CAOShaderConstantSetter() override = default; ~CAOShaderConstantSetter() override = default;
void onSetConstants(video::IMaterialRendererServices *services, void onSetConstants(video::IMaterialRendererServices *services) override
bool is_highlevel) override
{ {
if (!is_highlevel)
return;
// Ambient color // Ambient color
video::SColorf emissive_color(m_emissive_color); video::SColorf emissive_color(m_emissive_color);

@ -475,12 +475,8 @@ public:
g_settings->deregisterChangedCallback("enable_fog", settingsCallback, this); g_settings->deregisterChangedCallback("enable_fog", settingsCallback, this);
} }
virtual void onSetConstants(video::IMaterialRendererServices *services, void onSetConstants(video::IMaterialRendererServices *services) override
bool is_highlevel)
{ {
if (!is_highlevel)
return;
// Background color // Background color
video::SColor bgcolor = m_sky->getBgColor(); video::SColor bgcolor = m_sky->getBgColor();
video::SColorf bgcolorf(bgcolor); video::SColorf bgcolorf(bgcolor);

@ -100,7 +100,7 @@ Hud::Hud(gui::IGUIEnvironment *guienv, Client *client, LocalPlayer *player,
if (g_settings->getBool("enable_shaders")) { if (g_settings->getBool("enable_shaders")) {
IShaderSource *shdrsrc = client->getShaderSource(); IShaderSource *shdrsrc = client->getShaderSource();
u16 shader_id = shdrsrc->getShader( u16 shader_id = shdrsrc->getShader(
m_mode == HIGHLIGHT_HALO ? "selection_shader" : "default_shader", 1, 1); m_mode == HIGHLIGHT_HALO ? "selection_shader" : "default_shader", TILE_MATERIAL_ALPHA);
m_selection_material.MaterialType = shdrsrc->getShaderInfo(shader_id).material; m_selection_material.MaterialType = shdrsrc->getShaderInfo(shader_id).material;
} else { } else {
m_selection_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; m_selection_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;

@ -612,7 +612,7 @@ void Minimap::drawMinimap(core::rect<s32> rect) {
material.TextureLayer[1].Texture = data->heightmap_texture; material.TextureLayer[1].Texture = data->heightmap_texture;
if (m_enable_shaders && data->mode.type == MINIMAP_TYPE_SURFACE) { if (m_enable_shaders && data->mode.type == MINIMAP_TYPE_SURFACE) {
u16 sid = m_shdrsrc->getShader("minimap_shader", 1, 1); u16 sid = m_shdrsrc->getShader("minimap_shader", TILE_MATERIAL_ALPHA);
material.MaterialType = m_shdrsrc->getShaderInfo(sid).material; material.MaterialType = m_shdrsrc->getShaderInfo(sid).material;
} else { } else {
material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL; material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;

@ -36,7 +36,7 @@ void RenderingCoreInterlaced::initMaterial()
mat.UseMipMaps = false; mat.UseMipMaps = false;
mat.ZBuffer = false; mat.ZBuffer = false;
mat.ZWriteEnable = false; mat.ZWriteEnable = false;
u32 shader = s->getShader("3d_interlaced_merge", TILE_MATERIAL_BASIC, 0); u32 shader = s->getShader("3d_interlaced_merge", TILE_MATERIAL_BASIC);
mat.MaterialType = s->getShaderInfo(shader).material; mat.MaterialType = s->getShaderInfo(shader).material;
for (int k = 0; k < 3; ++k) { for (int k = 0; k < 3; ++k) {
mat.TextureLayer[k].AnisotropicFilter = false; mat.TextureLayer[k].AnisotropicFilter = false;

@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <iterator> #include <iterator>
#include "shader.h" #include "shader.h"
#include "irrlichttypes_extrabloated.h" #include "irrlichttypes_extrabloated.h"
#include "irr_ptr.h"
#include "debug.h" #include "debug.h"
#include "filesys.h" #include "filesys.h"
#include "util/container.h" #include "util/container.h"
@ -189,19 +190,14 @@ private:
class ShaderCallback : public video::IShaderConstantSetCallBack class ShaderCallback : public video::IShaderConstantSetCallBack
{ {
std::vector<IShaderConstantSetter*> m_setters; std::vector<std::unique_ptr<IShaderConstantSetter>> m_setters;
public: public:
ShaderCallback(const std::vector<IShaderConstantSetterFactory *> &factories) template <typename Factories>
ShaderCallback(const Factories &factories)
{ {
for (IShaderConstantSetterFactory *factory : factories) for (auto &&factory : factories)
m_setters.push_back(factory->create()); m_setters.push_back(std::unique_ptr<IShaderConstantSetter>(factory->create()));
}
~ShaderCallback()
{
for (IShaderConstantSetter *setter : m_setters)
delete setter;
} }
virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData) override virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData) override
@ -209,15 +205,13 @@ public:
video::IVideoDriver *driver = services->getVideoDriver(); video::IVideoDriver *driver = services->getVideoDriver();
sanity_check(driver != NULL); sanity_check(driver != NULL);
bool is_highlevel = userData; for (auto &&setter : m_setters)
setter->onSetConstants(services);
for (IShaderConstantSetter *setter : m_setters)
setter->onSetConstants(services, is_highlevel);
} }
virtual void OnSetMaterial(const video::SMaterial& material) override virtual void OnSetMaterial(const video::SMaterial& material) override
{ {
for (IShaderConstantSetter *setter : m_setters) for (auto &&setter : m_setters)
setter->onSetMaterial(material); setter->onSetMaterial(material);
} }
}; };
@ -252,47 +246,39 @@ public:
{} {}
~MainShaderConstantSetter() = default; ~MainShaderConstantSetter() = default;
virtual void onSetConstants(video::IMaterialRendererServices *services, virtual void onSetConstants(video::IMaterialRendererServices *services) override
bool is_highlevel)
{ {
video::IVideoDriver *driver = services->getVideoDriver(); video::IVideoDriver *driver = services->getVideoDriver();
sanity_check(driver); sanity_check(driver);
// Set world matrix // Set world matrix
core::matrix4 world = driver->getTransform(video::ETS_WORLD); core::matrix4 world = driver->getTransform(video::ETS_WORLD);
if (is_highlevel) m_world.set(*reinterpret_cast<float(*)[16]>(world.pointer()), services);
m_world.set(*reinterpret_cast<float(*)[16]>(world.pointer()), services);
else
services->setVertexShaderConstant(world.pointer(), 4, 4);
// Set clip matrix // Set clip matrix
core::matrix4 worldView; core::matrix4 worldView;
worldView = driver->getTransform(video::ETS_VIEW); worldView = driver->getTransform(video::ETS_VIEW);
worldView *= world; worldView *= world;
core::matrix4 worldViewProj; core::matrix4 worldViewProj;
worldViewProj = driver->getTransform(video::ETS_PROJECTION); worldViewProj = driver->getTransform(video::ETS_PROJECTION);
worldViewProj *= worldView; worldViewProj *= worldView;
if (is_highlevel) m_world_view_proj.set(*reinterpret_cast<float(*)[16]>(worldViewProj.pointer()), services);
m_world_view_proj.set(*reinterpret_cast<float(*)[16]>(worldViewProj.pointer()), services);
else
services->setVertexShaderConstant(worldViewProj.pointer(), 0, 4);
#if ENABLE_GLES #if ENABLE_GLES
if (is_highlevel) { core::matrix4 texture = driver->getTransform(video::ETS_TEXTURE_0);
core::matrix4 texture = driver->getTransform(video::ETS_TEXTURE_0); m_world_view.set(*reinterpret_cast<float(*)[16]>(worldView.pointer()), services);
m_world_view.set(*reinterpret_cast<float(*)[16]>(worldView.pointer()), services); m_texture.set(*reinterpret_cast<float(*)[16]>(texture.pointer()), services);
m_texture.set(*reinterpret_cast<float(*)[16]>(texture.pointer()), services);
core::matrix4 normal; core::matrix4 normal;
worldView.getTransposed(normal); worldView.getTransposed(normal);
sanity_check(normal.makeInverse()); sanity_check(normal.makeInverse());
float m[9] = { float m[9] = {
normal[0], normal[1], normal[2], normal[0], normal[1], normal[2],
normal[4], normal[5], normal[6], normal[4], normal[5], normal[6],
normal[8], normal[9], normal[10], normal[8], normal[9], normal[10],
}; };
m_normal.set(m, services); m_normal.set(m, services);
}
#endif #endif
} }
}; };
@ -314,7 +300,6 @@ class ShaderSource : public IWritableShaderSource
{ {
public: public:
ShaderSource(); ShaderSource();
~ShaderSource();
/* /*
- If shader material specified by name is found from cache, - If shader material specified by name is found from cache,
@ -324,7 +309,7 @@ public:
The id 0 points to a null shader. Its material is EMT_SOLID. The id 0 points to a null shader. Its material is EMT_SOLID.
*/ */
u32 getShaderIdDirect(const std::string &name, u32 getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype); MaterialType material_type, NodeDrawType drawtype) override;
/* /*
If shader specified by the name pointed by the id doesn't If shader specified by the name pointed by the id doesn't
@ -336,26 +321,26 @@ public:
*/ */
u32 getShader(const std::string &name, u32 getShader(const std::string &name,
const u8 material_type, const u8 drawtype); MaterialType material_type, NodeDrawType drawtype) override;
ShaderInfo getShaderInfo(u32 id); ShaderInfo getShaderInfo(u32 id) override;
// Processes queued shader requests from other threads. // Processes queued shader requests from other threads.
// Shall be called from the main thread. // Shall be called from the main thread.
void processQueue(); void processQueue() override;
// Insert a shader program into the cache without touching the // Insert a shader program into the cache without touching the
// filesystem. Shall be called from the main thread. // filesystem. Shall be called from the main thread.
void insertSourceShader(const std::string &name_of_shader, void insertSourceShader(const std::string &name_of_shader,
const std::string &filename, const std::string &program); const std::string &filename, const std::string &program) override;
// Rebuild shaders from the current set of source shaders // Rebuild shaders from the current set of source shaders
// Shall be called from the main thread. // Shall be called from the main thread.
void rebuildShaders(); void rebuildShaders() override;
void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) override
{ {
m_setter_factories.push_back(setter); m_setter_factories.push_back(std::unique_ptr<IShaderConstantSetterFactory>(setter));
} }
private: private:
@ -377,10 +362,11 @@ private:
RequestQueue<std::string, u32, u8, u8> m_get_shader_queue; RequestQueue<std::string, u32, u8, u8> m_get_shader_queue;
// Global constant setter factories // Global constant setter factories
std::vector<IShaderConstantSetterFactory *> m_setter_factories; std::vector<std::unique_ptr<IShaderConstantSetterFactory>> m_setter_factories;
// Shader callbacks // Generate shader given the shader name.
std::vector<ShaderCallback *> m_callbacks; ShaderInfo generateShader(const std::string &name,
MaterialType material_type, NodeDrawType drawtype);
}; };
IWritableShaderSource *createShaderSource() IWritableShaderSource *createShaderSource()
@ -388,22 +374,6 @@ IWritableShaderSource *createShaderSource()
return new ShaderSource(); return new ShaderSource();
} }
/*
Generate shader given the shader name.
*/
ShaderInfo generate_shader(const std::string &name,
u8 material_type, u8 drawtype, std::vector<ShaderCallback *> &callbacks,
const std::vector<IShaderConstantSetterFactory *> &setter_factories,
SourceShaderCache *sourcecache);
/*
Load shader programs
*/
void load_shaders(const std::string &name, SourceShaderCache *sourcecache,
video::E_DRIVER_TYPE drivertype, bool enable_shaders,
std::string &vertex_program, std::string &pixel_program,
std::string &geometry_program, bool &is_highlevel);
ShaderSource::ShaderSource() ShaderSource::ShaderSource()
{ {
m_main_thread = std::this_thread::get_id(); m_main_thread = std::this_thread::get_id();
@ -415,18 +385,8 @@ ShaderSource::ShaderSource()
addShaderConstantSetterFactory(new MainShaderConstantSetterFactory()); addShaderConstantSetterFactory(new MainShaderConstantSetterFactory());
} }
ShaderSource::~ShaderSource()
{
for (ShaderCallback *callback : m_callbacks) {
delete callback;
}
for (IShaderConstantSetterFactory *setter_factorie : m_setter_factories) {
delete setter_factorie;
}
}
u32 ShaderSource::getShader(const std::string &name, u32 ShaderSource::getShader(const std::string &name,
const u8 material_type, const u8 drawtype) MaterialType material_type, NodeDrawType drawtype)
{ {
/* /*
Get shader Get shader
@ -468,7 +428,7 @@ u32 ShaderSource::getShader(const std::string &name,
This method generates all the shaders This method generates all the shaders
*/ */
u32 ShaderSource::getShaderIdDirect(const std::string &name, u32 ShaderSource::getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype) MaterialType material_type, NodeDrawType drawtype)
{ {
//infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl; //infostream<<"getShaderIdDirect(): name=\""<<name<<"\""<<std::endl;
@ -495,8 +455,7 @@ u32 ShaderSource::getShaderIdDirect(const std::string &name,
return 0; return 0;
} }
ShaderInfo info = generate_shader(name, material_type, drawtype, ShaderInfo info = generateShader(name, material_type, drawtype);
m_callbacks, m_setter_factories, &m_sourcecache);
/* /*
Add shader to caches (add dummy shaders too) Add shader to caches (add dummy shaders too)
@ -560,18 +519,14 @@ void ShaderSource::rebuildShaders()
for (ShaderInfo &i : m_shaderinfo_cache) { for (ShaderInfo &i : m_shaderinfo_cache) {
ShaderInfo *info = &i; ShaderInfo *info = &i;
if (!info->name.empty()) { if (!info->name.empty()) {
*info = generate_shader(info->name, info->material_type, *info = generateShader(info->name, info->material_type, info->drawtype);
info->drawtype, m_callbacks,
m_setter_factories, &m_sourcecache);
} }
} }
} }
ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtype, ShaderInfo ShaderSource::generateShader(const std::string &name,
std::vector<ShaderCallback *> &callbacks, MaterialType material_type, NodeDrawType drawtype)
const std::vector<IShaderConstantSetterFactory *> &setter_factories,
SourceShaderCache *sourcecache)
{ {
ShaderInfo shaderinfo; ShaderInfo shaderinfo;
shaderinfo.name = name; shaderinfo.name = name;
@ -604,64 +559,27 @@ ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtyp
return shaderinfo; return shaderinfo;
video::IVideoDriver *driver = RenderingEngine::get_video_driver(); video::IVideoDriver *driver = RenderingEngine::get_video_driver();
if (!driver->queryFeature(video::EVDF_ARB_GLSL)) {
errorstream << "Shaders are enabled but GLSL is not supported by the driver\n";
return shaderinfo;
}
video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices(); video::IGPUProgrammingServices *gpu = driver->getGPUProgrammingServices();
if(!gpu){
errorstream<<"generate_shader(): "
"failed to generate \""<<name<<"\", "
"GPU programming not supported."
<<std::endl;
return shaderinfo;
}
// Choose shader language depending on driver type and settings
// Then load shaders
std::string vertex_program;
std::string pixel_program;
std::string geometry_program;
bool is_highlevel;
load_shaders(name, sourcecache, driver->getDriverType(),
enable_shaders, vertex_program, pixel_program,
geometry_program, is_highlevel);
// Check hardware/driver support
if (!vertex_program.empty() &&
!driver->queryFeature(video::EVDF_VERTEX_SHADER_1_1) &&
!driver->queryFeature(video::EVDF_ARB_VERTEX_PROGRAM_1)){
infostream<<"generate_shader(): vertex shaders disabled "
"because of missing driver/hardware support."
<<std::endl;
vertex_program = "";
}
if (!pixel_program.empty() &&
!driver->queryFeature(video::EVDF_PIXEL_SHADER_1_1) &&
!driver->queryFeature(video::EVDF_ARB_FRAGMENT_PROGRAM_1)){
infostream<<"generate_shader(): pixel shaders disabled "
"because of missing driver/hardware support."
<<std::endl;
pixel_program = "";
}
if (!geometry_program.empty() &&
!driver->queryFeature(video::EVDF_GEOMETRY_SHADER)){
infostream<<"generate_shader(): geometry shaders disabled "
"because of missing driver/hardware support."
<<std::endl;
geometry_program = "";
}
// If no shaders are used, don't make a separate material type
if (vertex_program.empty() && pixel_program.empty() && geometry_program.empty())
return shaderinfo;
// Create shaders header // Create shaders header
bool use_gles = false; bool use_gles = false;
#if ENABLE_GLES #if ENABLE_GLES
use_gles = driver->getDriverType() == video::EDT_OGLES2; use_gles = driver->getDriverType() == video::EDT_OGLES2;
#endif #endif
std::string shaders_header, vertex_header, pixel_header; // geometry shaders arent supported in GLES<3 std::stringstream shaders_header;
shaders_header
<< std::noboolalpha
<< std::showpoint // for GLSL ES
;
std::string vertex_header, fragment_header, geometry_header;
if (use_gles) { if (use_gles) {
shaders_header = shaders_header << R"(
"#version 100\n" #version 100
; )";
vertex_header = R"( vertex_header = R"(
uniform highp mat4 mWorldView; uniform highp mat4 mWorldView;
uniform highp mat4 mWorldViewProj; uniform highp mat4 mWorldViewProj;
@ -675,11 +593,11 @@ ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtyp
attribute mediump vec4 inVertexTangent; attribute mediump vec4 inVertexTangent;
attribute mediump vec4 inVertexBinormal; attribute mediump vec4 inVertexBinormal;
)"; )";
pixel_header = R"( fragment_header = R"(
precision mediump float; precision mediump float;
)"; )";
} else { } else {
shaders_header = R"( shaders_header << R"(
#version 120 #version 120
#define lowp #define lowp
#define mediump #define mediump
@ -708,224 +626,97 @@ ShaderInfo generate_shader(const std::string &name, u8 material_type, u8 drawtyp
use_discard = true; use_discard = true;
#endif #endif
if (use_discard && shaderinfo.base_material != video::EMT_SOLID) if (use_discard && shaderinfo.base_material != video::EMT_SOLID)
shaders_header += "#define USE_DISCARD\n"; shaders_header << "#define USE_DISCARD 1\n";
static const char* drawTypes[] = { #define PROVIDE(constant) shaders_header << "#define " #constant " " << (int)constant << "\n"
"NDT_NORMAL",
"NDT_AIRLIKE",
"NDT_LIQUID",
"NDT_FLOWINGLIQUID",
"NDT_GLASSLIKE",
"NDT_ALLFACES",
"NDT_ALLFACES_OPTIONAL",
"NDT_TORCHLIKE",
"NDT_SIGNLIKE",
"NDT_PLANTLIKE",
"NDT_FENCELIKE",
"NDT_RAILLIKE",
"NDT_NODEBOX",
"NDT_GLASSLIKE_FRAMED",
"NDT_FIRELIKE",
"NDT_GLASSLIKE_FRAMED_OPTIONAL",
"NDT_PLANTLIKE_ROOTED",
};
for (int i = 0; i < 14; i++){ PROVIDE(NDT_NORMAL);
shaders_header += "#define "; PROVIDE(NDT_AIRLIKE);
shaders_header += drawTypes[i]; PROVIDE(NDT_LIQUID);
shaders_header += " "; PROVIDE(NDT_FLOWINGLIQUID);
shaders_header += itos(i); PROVIDE(NDT_GLASSLIKE);
shaders_header += "\n"; PROVIDE(NDT_ALLFACES);
PROVIDE(NDT_ALLFACES_OPTIONAL);
PROVIDE(NDT_TORCHLIKE);
PROVIDE(NDT_SIGNLIKE);
PROVIDE(NDT_PLANTLIKE);
PROVIDE(NDT_FENCELIKE);
PROVIDE(NDT_RAILLIKE);
PROVIDE(NDT_NODEBOX);
PROVIDE(NDT_GLASSLIKE_FRAMED);
PROVIDE(NDT_FIRELIKE);
PROVIDE(NDT_GLASSLIKE_FRAMED_OPTIONAL);
PROVIDE(NDT_PLANTLIKE_ROOTED);
PROVIDE(TILE_MATERIAL_BASIC);
PROVIDE(TILE_MATERIAL_ALPHA);
PROVIDE(TILE_MATERIAL_LIQUID_TRANSPARENT);
PROVIDE(TILE_MATERIAL_LIQUID_OPAQUE);
PROVIDE(TILE_MATERIAL_WAVING_LEAVES);
PROVIDE(TILE_MATERIAL_WAVING_PLANTS);
PROVIDE(TILE_MATERIAL_OPAQUE);
PROVIDE(TILE_MATERIAL_WAVING_LIQUID_BASIC);
PROVIDE(TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT);
PROVIDE(TILE_MATERIAL_WAVING_LIQUID_OPAQUE);
PROVIDE(TILE_MATERIAL_PLAIN);
PROVIDE(TILE_MATERIAL_PLAIN_ALPHA);
#undef PROVIDE
shaders_header << "#define MATERIAL_TYPE " << (int)material_type << "\n";
shaders_header << "#define DRAW_TYPE " << (int)drawtype << "\n";
bool enable_waving_water = g_settings->getBool("enable_waving_water");
shaders_header << "#define ENABLE_WAVING_WATER " << enable_waving_water << "\n";
if (enable_waving_water) {
shaders_header << "#define WATER_WAVE_HEIGHT " << g_settings->getFloat("water_wave_height") << "\n";
shaders_header << "#define WATER_WAVE_LENGTH " << g_settings->getFloat("water_wave_length") << "\n";
shaders_header << "#define WATER_WAVE_SPEED " << g_settings->getFloat("water_wave_speed") << "\n";
} }
static const char* materialTypes[] = { shaders_header << "#define ENABLE_WAVING_LEAVES " << g_settings->getBool("enable_waving_leaves") << "\n";
"TILE_MATERIAL_BASIC", shaders_header << "#define ENABLE_WAVING_PLANTS " << g_settings->getBool("enable_waving_plants") << "\n";
"TILE_MATERIAL_ALPHA", shaders_header << "#define ENABLE_TONE_MAPPING " << g_settings->getBool("tone_mapping") << "\n";
"TILE_MATERIAL_LIQUID_TRANSPARENT",
"TILE_MATERIAL_LIQUID_OPAQUE",
"TILE_MATERIAL_WAVING_LEAVES",
"TILE_MATERIAL_WAVING_PLANTS",
"TILE_MATERIAL_OPAQUE",
"TILE_MATERIAL_WAVING_LIQUID_BASIC",
"TILE_MATERIAL_WAVING_LIQUID_TRANSPARENT",
"TILE_MATERIAL_WAVING_LIQUID_OPAQUE",
"TILE_MATERIAL_PLAIN",
"TILE_MATERIAL_PLAIN_ALPHA",
};
for (int i = 0; i < 12; i++){ shaders_header << "#define FOG_START " << core::clamp(g_settings->getFloat("fog_start"), 0.0f, 0.99f) << "\n";
shaders_header += "#define ";
shaders_header += materialTypes[i]; std::string common_header = shaders_header.str();
shaders_header += " ";
shaders_header += itos(i); std::string vertex_shader = m_sourcecache.getOrLoad(name, "opengl_vertex.glsl");
shaders_header += "\n"; std::string fragment_shader = m_sourcecache.getOrLoad(name, "opengl_fragment.glsl");
std::string geometry_shader = m_sourcecache.getOrLoad(name, "opengl_geometry.glsl");
vertex_shader = common_header + vertex_header + vertex_shader;
fragment_shader = common_header + fragment_header + fragment_shader;
const char *geometry_shader_ptr = nullptr; // optional
if (!geometry_shader.empty()) {
geometry_shader = common_header + geometry_header + geometry_shader;
geometry_shader_ptr = geometry_shader.c_str();
} }
shaders_header += "#define MATERIAL_TYPE "; irr_ptr<ShaderCallback> cb{new ShaderCallback(m_setter_factories)};
shaders_header += itos(material_type); infostream<<"Compiling high level shaders for "<<name<<std::endl;
shaders_header += "\n"; s32 shadermat = gpu->addHighLevelShaderMaterial(
shaders_header += "#define DRAW_TYPE "; vertex_shader.c_str(), nullptr, video::EVST_VS_1_1,
shaders_header += itos(drawtype); fragment_shader.c_str(), nullptr, video::EPST_PS_1_1,
shaders_header += "\n"; geometry_shader_ptr, nullptr, video::EGST_GS_4_0, scene::EPT_TRIANGLES, scene::EPT_TRIANGLES, 0,
cb.get(), shaderinfo.base_material, 1);
if (g_settings->getBool("enable_waving_water")){ if (shadermat == -1) {
shaders_header += "#define ENABLE_WAVING_WATER 1\n"; errorstream<<"generate_shader(): "
shaders_header += "#define WATER_WAVE_HEIGHT "; "failed to generate \""<<name<<"\", "
shaders_header += std::to_string(g_settings->getFloat("water_wave_height")); "addHighLevelShaderMaterial failed."
shaders_header += "\n"; <<std::endl;
shaders_header += "#define WATER_WAVE_LENGTH "; dumpShaderProgram(warningstream, "Vertex", vertex_shader);
shaders_header += std::to_string(g_settings->getFloat("water_wave_length")); dumpShaderProgram(warningstream, "Fragment", fragment_shader);
shaders_header += "\n"; dumpShaderProgram(warningstream, "Geometry", geometry_shader);
shaders_header += "#define WATER_WAVE_SPEED "; return shaderinfo;
shaders_header += std::to_string(g_settings->getFloat("water_wave_speed"));
shaders_header += "\n";
} else{
shaders_header += "#define ENABLE_WAVING_WATER 0\n";
} }
shaders_header += "#define ENABLE_WAVING_LEAVES ";
if (g_settings->getBool("enable_waving_leaves"))
shaders_header += "1\n";
else
shaders_header += "0\n";
shaders_header += "#define ENABLE_WAVING_PLANTS ";
if (g_settings->getBool("enable_waving_plants"))
shaders_header += "1\n";
else
shaders_header += "0\n";
if (g_settings->getBool("tone_mapping"))
shaders_header += "#define ENABLE_TONE_MAPPING\n";
shaders_header += "#define FOG_START ";
shaders_header += std::to_string(rangelim(g_settings->getFloat("fog_start"), 0.0f, 0.99f));
shaders_header += "\n";
// Call addHighLevelShaderMaterial() or addShaderMaterial()
const c8* vertex_program_ptr = 0;
const c8* pixel_program_ptr = 0;
const c8* geometry_program_ptr = 0;
if (!vertex_program.empty()) {
vertex_program = shaders_header + vertex_header + vertex_program;
vertex_program_ptr = vertex_program.c_str();
}
if (!pixel_program.empty()) {
pixel_program = shaders_header + pixel_header + pixel_program;
pixel_program_ptr = pixel_program.c_str();
}
if (!geometry_program.empty()) {
geometry_program = shaders_header + geometry_program;
geometry_program_ptr = geometry_program.c_str();
}
ShaderCallback *cb = new ShaderCallback(setter_factories);
s32 shadermat = -1;
if(is_highlevel){
infostream<<"Compiling high level shaders for "<<name<<std::endl;
shadermat = gpu->addHighLevelShaderMaterial(
vertex_program_ptr, // Vertex shader program
"vertexMain", // Vertex shader entry point
video::EVST_VS_1_1, // Vertex shader version
pixel_program_ptr, // Pixel shader program
"pixelMain", // Pixel shader entry point
video::EPST_PS_1_2, // Pixel shader version
geometry_program_ptr, // Geometry shader program
"geometryMain", // Geometry shader entry point
video::EGST_GS_4_0, // Geometry shader version
scene::EPT_TRIANGLES, // Geometry shader input
scene::EPT_TRIANGLE_STRIP, // Geometry shader output
0, // Support maximum number of vertices
cb, // Set-constant callback
shaderinfo.base_material, // Base material
1 // Userdata passed to callback
);
if(shadermat == -1){
errorstream<<"generate_shader(): "
"failed to generate \""<<name<<"\", "
"addHighLevelShaderMaterial failed."
<<std::endl;
dumpShaderProgram(warningstream, "Vertex", vertex_program);
dumpShaderProgram(warningstream, "Pixel", pixel_program);
dumpShaderProgram(warningstream, "Geometry", geometry_program);
delete cb;
return shaderinfo;
}
}
else{
infostream<<"Compiling assembly shaders for "<<name<<std::endl;
shadermat = gpu->addShaderMaterial(
vertex_program_ptr, // Vertex shader program
pixel_program_ptr, // Pixel shader program
cb, // Set-constant callback
shaderinfo.base_material, // Base material
0 // Userdata passed to callback
);
if(shadermat == -1){
errorstream<<"generate_shader(): "
"failed to generate \""<<name<<"\", "
"addShaderMaterial failed."
<<std::endl;
dumpShaderProgram(warningstream, "Vertex", vertex_program);
dumpShaderProgram(warningstream,"Pixel", pixel_program);
delete cb;
return shaderinfo;
}
}
callbacks.push_back(cb);
// HACK, TODO: investigate this better
// Grab the material renderer once more so minetest doesn't crash on exit
driver->getMaterialRenderer(shadermat)->grab();
// Apply the newly created material type // Apply the newly created material type
shaderinfo.material = (video::E_MATERIAL_TYPE) shadermat; shaderinfo.material = (video::E_MATERIAL_TYPE) shadermat;
return shaderinfo; return shaderinfo;
} }
void load_shaders(const std::string &name, SourceShaderCache *sourcecache,
video::E_DRIVER_TYPE drivertype, bool enable_shaders,
std::string &vertex_program, std::string &pixel_program,
std::string &geometry_program, bool &is_highlevel)
{
vertex_program = "";
pixel_program = "";
geometry_program = "";
is_highlevel = false;
if (!enable_shaders)
return;
// Look for high level shaders
switch (drivertype) {
case video::EDT_DIRECT3D9:
// Direct3D 9: HLSL
// (All shaders in one file)
vertex_program = sourcecache->getOrLoad(name, "d3d9.hlsl");
pixel_program = vertex_program;
geometry_program = vertex_program;
break;
case video::EDT_OPENGL:
#if ENABLE_GLES
case video::EDT_OGLES2:
#endif
// OpenGL: GLSL
vertex_program = sourcecache->getOrLoad(name, "opengl_vertex.glsl");
pixel_program = sourcecache->getOrLoad(name, "opengl_fragment.glsl");
geometry_program = sourcecache->getOrLoad(name, "opengl_geometry.glsl");
break;
default:
// e.g. OpenGL ES 1 (with no shader support)
break;
}
if (!vertex_program.empty() || !pixel_program.empty() || !geometry_program.empty()){
is_highlevel = true;
return;
}
}
void dumpShaderProgram(std::ostream &output_stream, void dumpShaderProgram(std::ostream &output_stream,
const std::string &program_type, const std::string &program) const std::string &program_type, const std::string &program)
{ {

@ -23,6 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <IMaterialRendererServices.h> #include <IMaterialRendererServices.h>
#include "irrlichttypes_bloated.h" #include "irrlichttypes_bloated.h"
#include <string> #include <string>
#include "tile.h"
#include "nodedef.h"
class IGameDef; class IGameDef;
@ -46,8 +48,8 @@ struct ShaderInfo {
std::string name = ""; std::string name = "";
video::E_MATERIAL_TYPE base_material = video::EMT_SOLID; video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
video::E_MATERIAL_TYPE material = video::EMT_SOLID; video::E_MATERIAL_TYPE material = video::EMT_SOLID;
u8 drawtype = 0; NodeDrawType drawtype = NDT_NORMAL;
u8 material_type = 0; MaterialType material_type = TILE_MATERIAL_BASIC;
ShaderInfo() = default; ShaderInfo() = default;
virtual ~ShaderInfo() = default; virtual ~ShaderInfo() = default;
@ -65,8 +67,7 @@ namespace irr { namespace video {
class IShaderConstantSetter { class IShaderConstantSetter {
public: public:
virtual ~IShaderConstantSetter() = default; virtual ~IShaderConstantSetter() = default;
virtual void onSetConstants(video::IMaterialRendererServices *services, virtual void onSetConstants(video::IMaterialRendererServices *services) = 0;
bool is_highlevel) = 0;
virtual void onSetMaterial(const video::SMaterial& material) virtual void onSetMaterial(const video::SMaterial& material)
{ } { }
}; };
@ -128,10 +129,10 @@ public:
virtual ~IShaderSource() = default; virtual ~IShaderSource() = default;
virtual u32 getShaderIdDirect(const std::string &name, virtual u32 getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype){return 0;} MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();} virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
virtual u32 getShader(const std::string &name, virtual u32 getShader(const std::string &name,
const u8 material_type, const u8 drawtype){return 0;} MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
}; };
class IWritableShaderSource : public IShaderSource { class IWritableShaderSource : public IShaderSource {
@ -139,16 +140,12 @@ public:
IWritableShaderSource() = default; IWritableShaderSource() = default;
virtual ~IWritableShaderSource() = default; virtual ~IWritableShaderSource() = default;
virtual u32 getShaderIdDirect(const std::string &name,
const u8 material_type, const u8 drawtype){return 0;}
virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
virtual u32 getShader(const std::string &name,
const u8 material_type, const u8 drawtype){return 0;}
virtual void processQueue()=0; virtual void processQueue()=0;
virtual void insertSourceShader(const std::string &name_of_shader, virtual void insertSourceShader(const std::string &name_of_shader,
const std::string &filename, const std::string &program)=0; const std::string &filename, const std::string &program)=0;
virtual void rebuildShaders()=0; virtual void rebuildShaders()=0;
/// @note Takes ownership of @p setter.
virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0; virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
}; };

@ -65,7 +65,7 @@ Sky::Sky(s32 id, ITextureSource *tsrc, IShaderSource *ssrc) :
// Create materials // Create materials
m_materials[0] = baseMaterial(); m_materials[0] = baseMaterial();
m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA, 0)).material; m_materials[0].MaterialType = ssrc->getShaderInfo(ssrc->getShader("stars_shader", TILE_MATERIAL_ALPHA)).material;
m_materials[0].Lighting = true; m_materials[0].Lighting = true;
m_materials[0].ColorMaterial = video::ECM_NONE; m_materials[0].ColorMaterial = video::ECM_NONE;

@ -774,7 +774,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
bool is_liquid = false; bool is_liquid = false;
u8 material_type = (alpha == 255) ? MaterialType material_type = (alpha == 255) ?
TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA; TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
switch (drawtype) { switch (drawtype) {
@ -892,7 +892,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
u32 tile_shader = shdsrc->getShader("nodes_shader", material_type, drawtype); u32 tile_shader = shdsrc->getShader("nodes_shader", material_type, drawtype);
u8 overlay_material = material_type; MaterialType overlay_material = material_type;
if (overlay_material == TILE_MATERIAL_OPAQUE) if (overlay_material == TILE_MATERIAL_OPAQUE)
overlay_material = TILE_MATERIAL_BASIC; overlay_material = TILE_MATERIAL_BASIC;
else if (overlay_material == TILE_MATERIAL_LIQUID_OPAQUE) else if (overlay_material == TILE_MATERIAL_LIQUID_OPAQUE)
@ -913,7 +913,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
tdef[j].backface_culling, tsettings); tdef[j].backface_culling, tsettings);
} }
u8 special_material = material_type; MaterialType special_material = material_type;
if (drawtype == NDT_PLANTLIKE_ROOTED) { if (drawtype == NDT_PLANTLIKE_ROOTED) {
if (waving == 1) if (waving == 1)
special_material = TILE_MATERIAL_WAVING_PLANTS; special_material = TILE_MATERIAL_WAVING_PLANTS;