shader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2013 Kahrl <kahrl@gmx.net>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #pragma once
  18. #include "irrlichttypes_bloated.h"
  19. #include <IMaterialRendererServices.h>
  20. #include <string>
  21. #include "tile.h"
  22. #include "nodedef.h"
  23. class IGameDef;
  24. /*
  25. shader.{h,cpp}: Shader handling stuff.
  26. */
  27. /*
  28. Gets the path to a shader by first checking if the file
  29. name_of_shader/filename
  30. exists in shader_path and if not, using the data path.
  31. If not found, returns "".
  32. Utilizes a thread-safe cache.
  33. */
  34. std::string getShaderPath(const std::string &name_of_shader,
  35. const std::string &filename);
  36. struct ShaderInfo {
  37. std::string name = "";
  38. video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
  39. video::E_MATERIAL_TYPE material = video::EMT_SOLID;
  40. NodeDrawType drawtype = NDT_NORMAL;
  41. MaterialType material_type = TILE_MATERIAL_BASIC;
  42. ShaderInfo() = default;
  43. virtual ~ShaderInfo() = default;
  44. };
  45. /*
  46. Setter of constants for shaders
  47. */
  48. namespace irr { namespace video {
  49. class IMaterialRendererServices;
  50. } }
  51. class IShaderConstantSetter {
  52. public:
  53. virtual ~IShaderConstantSetter() = default;
  54. virtual void onSetConstants(video::IMaterialRendererServices *services) = 0;
  55. virtual void onSetMaterial(const video::SMaterial& material)
  56. { }
  57. };
  58. class IShaderConstantSetterFactory {
  59. public:
  60. virtual ~IShaderConstantSetterFactory() = default;
  61. virtual IShaderConstantSetter* create() = 0;
  62. };
  63. template <typename T, std::size_t count=1>
  64. class CachedShaderSetting {
  65. const char *m_name;
  66. T m_sent[count];
  67. bool has_been_set = false;
  68. bool is_pixel;
  69. protected:
  70. CachedShaderSetting(const char *name, bool is_pixel) :
  71. m_name(name), is_pixel(is_pixel)
  72. {}
  73. public:
  74. void set(const T value[count], video::IMaterialRendererServices *services)
  75. {
  76. if (has_been_set && std::equal(m_sent, m_sent + count, value))
  77. return;
  78. if (is_pixel)
  79. services->setPixelShaderConstant(services->getPixelShaderConstantID(m_name), value, count);
  80. else
  81. services->setVertexShaderConstant(services->getVertexShaderConstantID(m_name), value, count);
  82. std::copy(value, value + count, m_sent);
  83. has_been_set = true;
  84. }
  85. };
  86. template <typename T, std::size_t count = 1>
  87. class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
  88. public:
  89. CachedPixelShaderSetting(const char *name) :
  90. CachedShaderSetting<T, count>(name, true){}
  91. };
  92. template <typename T, std::size_t count = 1>
  93. class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
  94. public:
  95. CachedVertexShaderSetting(const char *name) :
  96. CachedShaderSetting<T, count>(name, false){}
  97. };
  98. /*
  99. ShaderSource creates and caches shaders.
  100. */
  101. class IShaderSource {
  102. public:
  103. IShaderSource() = default;
  104. virtual ~IShaderSource() = default;
  105. virtual u32 getShaderIdDirect(const std::string &name,
  106. MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
  107. virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
  108. virtual u32 getShader(const std::string &name,
  109. MaterialType material_type, NodeDrawType drawtype = NDT_NORMAL){return 0;}
  110. };
  111. class IWritableShaderSource : public IShaderSource {
  112. public:
  113. IWritableShaderSource() = default;
  114. virtual ~IWritableShaderSource() = default;
  115. virtual void processQueue()=0;
  116. virtual void insertSourceShader(const std::string &name_of_shader,
  117. const std::string &filename, const std::string &program)=0;
  118. virtual void rebuildShaders()=0;
  119. /// @note Takes ownership of @p setter.
  120. virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
  121. };
  122. IWritableShaderSource *createShaderSource();
  123. void dumpShaderProgram(std::ostream &output_stream,
  124. const std::string &program_type, const std::string &program);