From 27373919f4369c0c511f9f0ac66854b7f76e101d Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 1 Dec 2012 03:02:16 +0200 Subject: [PATCH] Implement a global shader parameter passing system and useful shaders --- client/shaders/test_shader_1/base.txt | 1 + .../test_shader_1/opengl_fragment.glsl | 25 +++++++ .../shaders/test_shader_1/opengl_vertex.glsl | 25 +++++++ .../test_shader_2/.opengl_fragment.glsl.swo | Bin 0 -> 12288 bytes client/shaders/test_shader_2/base.txt | 1 + .../test_shader_2/opengl_fragment.glsl | 23 ++++++ .../shaders/test_shader_2/opengl_vertex.glsl | 20 +++++ .../the_darkness_of_light/opengl_fragment.asm | 17 ----- .../opengl_fragment.glsl | 9 --- .../the_darkness_of_light/opengl_vertex.asm | 38 ---------- .../the_darkness_of_light/opengl_vertex.glsl | 16 ---- src/game.cpp | 52 ++++++++++++- src/mapblock_mesh.cpp | 15 +++- src/shader.cpp | 70 +++++++++++++++++- src/shader.h | 17 +++++ 15 files changed, 240 insertions(+), 89 deletions(-) create mode 100644 client/shaders/test_shader_1/base.txt create mode 100644 client/shaders/test_shader_1/opengl_fragment.glsl create mode 100644 client/shaders/test_shader_1/opengl_vertex.glsl create mode 100644 client/shaders/test_shader_2/.opengl_fragment.glsl.swo create mode 100644 client/shaders/test_shader_2/base.txt create mode 100644 client/shaders/test_shader_2/opengl_fragment.glsl create mode 100644 client/shaders/test_shader_2/opengl_vertex.glsl delete mode 100644 client/shaders/the_darkness_of_light/opengl_fragment.asm delete mode 100644 client/shaders/the_darkness_of_light/opengl_fragment.glsl delete mode 100644 client/shaders/the_darkness_of_light/opengl_vertex.asm delete mode 100644 client/shaders/the_darkness_of_light/opengl_vertex.glsl diff --git a/client/shaders/test_shader_1/base.txt b/client/shaders/test_shader_1/base.txt new file mode 100644 index 00000000..080df30d --- /dev/null +++ b/client/shaders/test_shader_1/base.txt @@ -0,0 +1 @@ +trans_alphach_ref diff --git a/client/shaders/test_shader_1/opengl_fragment.glsl b/client/shaders/test_shader_1/opengl_fragment.glsl new file mode 100644 index 00000000..ebf943ce --- /dev/null +++ b/client/shaders/test_shader_1/opengl_fragment.glsl @@ -0,0 +1,25 @@ + +uniform sampler2D myTexture; +uniform vec4 skyBgColor; +uniform float fogDistance; + +varying vec3 vPosition; + +void main (void) +{ + //vec4 col = vec4(1.0, 0.0, 0.0, 1.0); + vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0])); + float a = col.a; + col *= gl_Color; + col = col * col; // SRGB -> Linear + col *= 1.8; + col.r = 1.0 - exp(1.0 - col.r) / exp(1.0); + col.g = 1.0 - exp(1.0 - col.g) / exp(1.0); + col.b = 1.0 - exp(1.0 - col.b) / exp(1.0); + col = sqrt(col); // Linear -> SRGB + if(fogDistance != 0.0){ + float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0)); + col = mix(col, skyBgColor, d); + } + gl_FragColor = vec4(col.r, col.g, col.b, a); +} diff --git a/client/shaders/test_shader_1/opengl_vertex.glsl b/client/shaders/test_shader_1/opengl_vertex.glsl new file mode 100644 index 00000000..49808505 --- /dev/null +++ b/client/shaders/test_shader_1/opengl_vertex.glsl @@ -0,0 +1,25 @@ + +uniform mat4 mWorldViewProj; +uniform mat4 mInvWorld; +uniform mat4 mTransWorld; + +varying vec3 vPosition; + +void main(void) +{ + gl_Position = mWorldViewProj * gl_Vertex; + + vPosition = (mWorldViewProj * gl_Vertex).xyz; + + if(gl_Normal.y > 0.5) + gl_FrontColor = gl_BackColor = gl_Color; + else + gl_FrontColor = gl_BackColor = gl_Color * 0.7; + + /*if(gl_Normal.y > 0.5) + gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0); + else + gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0) * 0.7;*/ + + gl_TexCoord[0] = gl_MultiTexCoord0; +} diff --git a/client/shaders/test_shader_2/.opengl_fragment.glsl.swo b/client/shaders/test_shader_2/.opengl_fragment.glsl.swo new file mode 100644 index 0000000000000000000000000000000000000000..e1cac6222bb8406e3d4359cb8cd8f96187e6d898 GIT binary patch literal 12288 zcmeI2&ui2`6vw9(k%HD=M=!4`CEIS3bl2KSU7@xJ9+W*)5k$*mHyZ;LqA zKsMbtvGm+pK3J<*YTb>zV67EPFT^U-3zGf3uJ>B*nE(^mNLw=tNfC(^x|A;`kJ|Z6ByradLj~D&OKwk{GV**To2`~XBzyz28 z6JP>NfC(@GCcp#^AOYDF!W$Rj6{P?F?|uJ&$HOP+BlH$}1HFV^KwFRt-Gr8)BhWYO ze_i<5L4O~*1YLwCAc77-qtMr5LVSUqLr^cdi}A zx=h;tL6SUOEguSrwDe}B5&v-&FdFW z@!+C<*jKVQ!w8 zf?4Qq8nO(YcA1*G8e4&qDK)i8Zle4!kIU&syRH&H^;KkUp$r#n7_#&%l#ymz6V-Xn ot9Y*OM!%s~GNxS22>w<{I`F@k#kPz)e&lJFRqBV01n!;QAB9CulmGw# literal 0 HcmV?d00001 diff --git a/client/shaders/test_shader_2/base.txt b/client/shaders/test_shader_2/base.txt new file mode 100644 index 00000000..1c264711 --- /dev/null +++ b/client/shaders/test_shader_2/base.txt @@ -0,0 +1 @@ +trans_alphach diff --git a/client/shaders/test_shader_2/opengl_fragment.glsl b/client/shaders/test_shader_2/opengl_fragment.glsl new file mode 100644 index 00000000..38bc9431 --- /dev/null +++ b/client/shaders/test_shader_2/opengl_fragment.glsl @@ -0,0 +1,23 @@ + +uniform sampler2D myTexture; +uniform float fogDistance; + +varying vec3 vPosition; + +void main (void) +{ + vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0])); + col *= gl_Color; + float a = gl_Color.a; + col = col * col; // SRGB -> Linear + col *= 1.8; + col.r = 1.0 - exp(1.0 - col.r) / exp(1.0); + col.g = 1.0 - exp(1.0 - col.g) / exp(1.0); + col.b = 1.0 - exp(1.0 - col.b) / exp(1.0); + col = sqrt(col); // Linear -> SRGB + if(fogDistance != 0.0){ + float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0)); + a = mix(a, 0.0, d); + } + gl_FragColor = vec4(col.r, col.g, col.b, a); +} diff --git a/client/shaders/test_shader_2/opengl_vertex.glsl b/client/shaders/test_shader_2/opengl_vertex.glsl new file mode 100644 index 00000000..6286fc0d --- /dev/null +++ b/client/shaders/test_shader_2/opengl_vertex.glsl @@ -0,0 +1,20 @@ + +uniform mat4 mWorldViewProj; +uniform mat4 mInvWorld; +uniform mat4 mTransWorld; + +varying vec3 vPosition; + +void main(void) +{ + vec4 pos = gl_Vertex; + pos.y -= 2.0; + gl_Position = mWorldViewProj * pos; + + vPosition = (mWorldViewProj * gl_Vertex).xyz; + + gl_FrontColor = gl_BackColor = gl_Color; + //gl_FrontColor = gl_BackColor = vec4(1.0, 1.0, 1.0, 1.0); + + gl_TexCoord[0] = gl_MultiTexCoord0; +} diff --git a/client/shaders/the_darkness_of_light/opengl_fragment.asm b/client/shaders/the_darkness_of_light/opengl_fragment.asm deleted file mode 100644 index 8297f8ec..00000000 --- a/client/shaders/the_darkness_of_light/opengl_fragment.asm +++ /dev/null @@ -1,17 +0,0 @@ -!!ARBfp1.0 - -#Input -ATTRIB inTexCoord = fragment.texcoord; # texture coordinates -ATTRIB inColor = fragment.color.primary; # interpolated diffuse color - -#Output -OUTPUT outColor = result.color; - -TEMP texelColor; -TXP texelColor, inTexCoord, texture, 2D; -MUL texelColor, texelColor, inColor; # multiply with color -SUB outColor, {1.0,1.0,1.0,1.0}, texelColor; -MOV outColor.w, 1.0; - -END - diff --git a/client/shaders/the_darkness_of_light/opengl_fragment.glsl b/client/shaders/the_darkness_of_light/opengl_fragment.glsl deleted file mode 100644 index e447918e..00000000 --- a/client/shaders/the_darkness_of_light/opengl_fragment.glsl +++ /dev/null @@ -1,9 +0,0 @@ - -uniform sampler2D myTexture; - -void main (void) -{ - vec4 col = texture2D(myTexture, vec2(gl_TexCoord[0])); - col *= gl_Color; - gl_FragColor = vec4(1.0-col.r, 1.0-col.g, 1.0-col.b, 1.0); -} diff --git a/client/shaders/the_darkness_of_light/opengl_vertex.asm b/client/shaders/the_darkness_of_light/opengl_vertex.asm deleted file mode 100644 index adfee130..00000000 --- a/client/shaders/the_darkness_of_light/opengl_vertex.asm +++ /dev/null @@ -1,38 +0,0 @@ -!!ARBvp1.0 - -#input -ATTRIB InPos = vertex.position; -ATTRIB InColor = vertex.color; -ATTRIB InNormal = vertex.normal; -ATTRIB InTexCoord = vertex.texcoord; - -#output -OUTPUT OutPos = result.position; -OUTPUT OutColor = result.color; -OUTPUT OutTexCoord = result.texcoord; - -PARAM MVP[4] = { state.matrix.mvp }; # modelViewProjection matrix. -TEMP Temp; -TEMP TempColor; -TEMP TempCompare; - -#transform position to clip space -DP4 Temp.x, MVP[0], InPos; -DP4 Temp.y, MVP[1], InPos; -DP4 Temp.z, MVP[2], InPos; -DP4 Temp.w, MVP[3], InPos; - -# check if normal.y > 0.5 -SLT TempCompare, InNormal, {0.5,0.5,0.5,0.5}; -MUL TempCompare.z, TempCompare.y, 0.5; -SUB TempCompare.x, 1.0, TempCompare.z; -MOV TempCompare.y, TempCompare.x; -MOV TempCompare.z, TempCompare.x; - -# calculate light color -MUL OutColor, InColor, TempCompare; -MOV OutColor.w, 1.0; # we want alpha to be always 1 -MOV OutTexCoord, InTexCoord; # store texture coordinate -MOV OutPos, Temp; - -END diff --git a/client/shaders/the_darkness_of_light/opengl_vertex.glsl b/client/shaders/the_darkness_of_light/opengl_vertex.glsl deleted file mode 100644 index 0182c859..00000000 --- a/client/shaders/the_darkness_of_light/opengl_vertex.glsl +++ /dev/null @@ -1,16 +0,0 @@ - -uniform mat4 mWorldViewProj; -uniform mat4 mInvWorld; -uniform mat4 mTransWorld; - -void main(void) -{ - gl_Position = mWorldViewProj * gl_Vertex; - - if(gl_Normal.y > 0.5) - gl_FrontColor = gl_BackColor = gl_Color; - else - gl_FrontColor = gl_BackColor = gl_Color * 0.5; - - gl_TexCoord[0] = gl_MultiTexCoord0; -} diff --git a/src/game.cpp b/src/game.cpp index 1339afbf..0c1a2137 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include +#include #include "client.h" #include "server.h" #include "guiPauseMenu.h" @@ -835,6 +836,49 @@ public: } }; +class GameGlobalShaderConstantSetter : public IShaderConstantSetter +{ + Sky *m_sky; + bool *m_force_fog_off; + f32 *m_fog_range; + +public: + GameGlobalShaderConstantSetter(Sky *sky, bool *force_fog_off, + f32 *fog_range): + m_sky(sky), + m_force_fog_off(force_fog_off), + m_fog_range(fog_range) + {} + ~GameGlobalShaderConstantSetter() {} + + virtual void onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel) + { + if(!is_highlevel) + return; + + // Background color + video::SColor bgcolor = m_sky->getBgColor(); + video::SColorf bgcolorf(bgcolor); + float bgcolorfa[4] = { + bgcolorf.r, + bgcolorf.g, + bgcolorf.b, + bgcolorf.a, + }; + services->setPixelShaderConstant("skyBgColor", bgcolorfa, 4); + + // Fog distance + float fog_distance = *m_fog_range; + if(*m_force_fog_off) + fog_distance = 10000*BS; + services->setPixelShaderConstant("fogDistance", &fog_distance, 1); + } + +private: + IrrlichtDevice *m_device; +}; + void the_game( bool &kill, bool random_input, @@ -1250,6 +1294,7 @@ void the_game( bool show_hud = true; bool show_chat = true; bool force_fog_off = false; + f32 fog_range = 100*BS; bool disable_camera_update = false; bool show_debug = g_settings->getBool("show_debug"); bool show_profiler_graph = false; @@ -1259,6 +1304,12 @@ void the_game( float time_of_day = 0; float time_of_day_smooth = 0; + /* + Shader constants + */ + shsrc->addGlobalConstantSetter( + new GameGlobalShaderConstantSetter(sky, &force_fog_off, &fog_range)); + /* Main loop */ @@ -2434,7 +2485,6 @@ void the_game( Fog range */ - f32 fog_range; if(farmesh) { fog_range = BS*farmesh_range; diff --git a/src/mapblock_mesh.cpp b/src/mapblock_mesh.cpp index cbc38ddb..c871b6db 100644 --- a/src/mapblock_mesh.cpp +++ b/src/mapblock_mesh.cpp @@ -1012,8 +1012,11 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data): Convert MeshCollector to SMesh Also store animation info */ - video::E_MATERIAL_TYPE shadermat = m_gamedef->getShaderSource()-> - getShader("the_darkness_of_light").material; + bool enable_shaders = (g_settings->getS32("enable_shaders") > 0); + video::E_MATERIAL_TYPE shadermat1 = m_gamedef->getShaderSource()-> + getShader("test_shader_1").material; + video::E_MATERIAL_TYPE shadermat2 = m_gamedef->getShaderSource()-> + getShader("test_shader_2").material; for(u32 i = 0; i < collector.prebuffers.size(); i++) { PreMeshBuffer &p = collector.prebuffers[i]; @@ -1080,8 +1083,12 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data): material.setTexture(0, p.tile.texture.atlas); p.tile.applyMaterialOptions(material); - //if(material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF) - material.MaterialType = shadermat; + if(enable_shaders){ + if(material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF) + material.MaterialType = shadermat1; + if(material.MaterialType == video::EMT_TRANSPARENT_VERTEX_ALPHA) + material.MaterialType = shadermat2; + } // Create meshbuffer diff --git a/src/shader.cpp b/src/shader.cpp index ba0b8600..9e1a51f2 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -35,6 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "EShaderTypes.h" #include "log.h" #include "gamedef.h" +#include "strfnd.h" // trim() /* A cache from shader name to shader path @@ -171,10 +172,24 @@ private: ShaderCallback: Sets constants that can be used in shaders */ +class IShaderConstantSetterRegistry +{ +public: + virtual ~IShaderConstantSetterRegistry(){}; + virtual void onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel, const std::string &name) = 0; +}; + class ShaderCallback : public video::IShaderConstantSetCallBack { + IShaderConstantSetterRegistry *m_scsr; + std::string m_name; + public: - ShaderCallback(IrrlichtDevice *device): m_device(device) {} + ShaderCallback(IShaderConstantSetterRegistry *scsr, const std::string &name): + m_scsr(scsr), + m_name(name) + {} ~ShaderCallback() {} virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData) @@ -184,6 +199,28 @@ public: bool is_highlevel = userData; + m_scsr->onSetConstants(services, is_highlevel, m_name); + } +}; + +/* + MainShaderConstantSetter: Set basic constants required for almost everything +*/ + +class MainShaderConstantSetter : public IShaderConstantSetter +{ +public: + MainShaderConstantSetter(IrrlichtDevice *device): + m_device(device) + {} + ~MainShaderConstantSetter() {} + + virtual void onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel) + { + video::IVideoDriver *driver = services->getVideoDriver(); + assert(driver); + // set inverted world matrix core::matrix4 invWorld = driver->getTransform(video::ETS_WORLD); invWorld.makeInverse(); @@ -219,7 +256,7 @@ private: ShaderSource */ -class ShaderSource : public IWritableShaderSource +class ShaderSource : public IWritableShaderSource, public IShaderConstantSetterRegistry { public: ShaderSource(IrrlichtDevice *device); @@ -272,6 +309,14 @@ public: // Shall be called from the main thread. void rebuildShaders(); + void addGlobalConstantSetter(IShaderConstantSetter *setter) + { + m_global_setters.push_back(setter); + } + + void onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel, const std::string &name); + private: // The id of the thread that is allowed to use irrlicht directly @@ -295,6 +340,10 @@ private: // Queued shader fetches (to be processed by the main thread) RequestQueue m_get_shader_queue; + + // Global constant setters + // TODO: Delete these in the destructor + core::array m_global_setters; }; IWritableShaderSource* createShaderSource(IrrlichtDevice *device) @@ -322,7 +371,7 @@ ShaderSource::ShaderSource(IrrlichtDevice *device): { assert(m_device); - m_shader_callback = new ShaderCallback(device); + m_shader_callback = new ShaderCallback(this, "default"); m_shaderinfo_cache_mutex.Init(); @@ -331,6 +380,9 @@ ShaderSource::ShaderSource(IrrlichtDevice *device): // Add a dummy ShaderInfo as the first index, named "" m_shaderinfo_cache.push_back(ShaderInfo()); m_name_to_id[""] = 0; + + // Add main global constant setter + addGlobalConstantSetter(new MainShaderConstantSetter(device)); } ShaderSource::~ShaderSource() @@ -531,6 +583,15 @@ void ShaderSource::rebuildShaders() m_shader_callback, &m_sourcecache); } } + +void ShaderSource::onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel, const std::string &name) +{ + for(u32 i=0; ionSetConstants(services, is_highlevel); + } +} ShaderInfo generate_shader(std::string name, IrrlichtDevice *device, video::IShaderConstantSetCallBack *callback, @@ -546,7 +607,8 @@ ShaderInfo generate_shader(std::string name, IrrlichtDevice *device, /* Get the base material */ - std::string base_material_name = sourcecache->getOrLoad(name, "base.txt"); + std::string base_material_name = + trim(sourcecache->getOrLoad(name, "base.txt")); for(s32 i = 0; video::sBuiltInMaterialTypeNames[i] != 0; i++){ if(video::sBuiltInMaterialTypeNames[i] == base_material_name){ shaderinfo.material = (video::E_MATERIAL_TYPE) i; diff --git a/src/shader.h b/src/shader.h index d6a42531..774a17b2 100644 --- a/src/shader.h +++ b/src/shader.h @@ -51,6 +51,22 @@ struct ShaderInfo ShaderInfo(): name(""), material(video::EMT_SOLID) {} }; +/* + Setter of constants for shaders +*/ + +namespace irr { namespace video { + class IMaterialRendererServices; +} } + +class IShaderConstantSetter +{ +public: + virtual ~IShaderConstantSetter(){}; + virtual void onSetConstants(video::IMaterialRendererServices *services, + bool is_highlevel) = 0; +}; + /* ShaderSource creates and caches shaders. */ @@ -82,6 +98,7 @@ public: virtual void insertSourceShader(const std::string &name_of_shader, const std::string &filename, const std::string &program)=0; virtual void rebuildShaders()=0; + virtual void addGlobalConstantSetter(IShaderConstantSetter *setter)=0; }; IWritableShaderSource* createShaderSource(IrrlichtDevice *device); -- 2.30.2