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 <IMaterialRendererServices.h>
  19. #include "irrlichttypes_bloated.h"
  20. #include <string>
  21. class IGameDef;
  22. /*
  23. shader.{h,cpp}: Shader handling stuff.
  24. */
  25. /*
  26. Gets the path to a shader by first checking if the file
  27. name_of_shader/filename
  28. exists in shader_path and if not, using the data path.
  29. If not found, returns "".
  30. Utilizes a thread-safe cache.
  31. */
  32. std::string getShaderPath(const std::string &name_of_shader,
  33. const std::string &filename);
  34. struct ShaderInfo {
  35. std::string name = "";
  36. video::E_MATERIAL_TYPE base_material = video::EMT_SOLID;
  37. video::E_MATERIAL_TYPE material = video::EMT_SOLID;
  38. u8 drawtype = 0;
  39. u8 material_type = 0;
  40. ShaderInfo() = default;
  41. virtual ~ShaderInfo() = default;
  42. };
  43. /*
  44. Setter of constants for shaders
  45. */
  46. namespace irr { namespace video {
  47. class IMaterialRendererServices;
  48. } }
  49. class IShaderConstantSetter {
  50. public:
  51. virtual ~IShaderConstantSetter() = default;
  52. virtual void onSetConstants(video::IMaterialRendererServices *services,
  53. bool is_highlevel) = 0;
  54. };
  55. class IShaderConstantSetterFactory {
  56. public:
  57. virtual ~IShaderConstantSetterFactory() = default;
  58. virtual IShaderConstantSetter* create() = 0;
  59. };
  60. template <typename T, std::size_t count=1>
  61. class CachedShaderSetting {
  62. const char *m_name;
  63. T m_sent[count];
  64. bool has_been_set = false;
  65. bool is_pixel;
  66. protected:
  67. CachedShaderSetting(const char *name, bool is_pixel) :
  68. m_name(name), is_pixel(is_pixel)
  69. {}
  70. public:
  71. void set(const T value[count], video::IMaterialRendererServices *services)
  72. {
  73. if (has_been_set && std::equal(m_sent, m_sent + count, value))
  74. return;
  75. if (is_pixel)
  76. services->setPixelShaderConstant(m_name, value, count);
  77. else
  78. services->setVertexShaderConstant(m_name, value, count);
  79. std::copy(value, value + count, m_sent);
  80. has_been_set = true;
  81. }
  82. };
  83. template <typename T, std::size_t count = 1>
  84. class CachedPixelShaderSetting : public CachedShaderSetting<T, count> {
  85. public:
  86. CachedPixelShaderSetting(const char *name) :
  87. CachedShaderSetting<T, count>(name, true){}
  88. };
  89. template <typename T, std::size_t count = 1>
  90. class CachedVertexShaderSetting : public CachedShaderSetting<T, count> {
  91. public:
  92. CachedVertexShaderSetting(const char *name) :
  93. CachedShaderSetting<T, count>(name, false){}
  94. };
  95. /*
  96. ShaderSource creates and caches shaders.
  97. */
  98. class IShaderSource {
  99. public:
  100. IShaderSource() = default;
  101. virtual ~IShaderSource() = default;
  102. virtual u32 getShaderIdDirect(const std::string &name,
  103. const u8 material_type, const u8 drawtype){return 0;}
  104. virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
  105. virtual u32 getShader(const std::string &name,
  106. const u8 material_type, const u8 drawtype){return 0;}
  107. };
  108. class IWritableShaderSource : public IShaderSource {
  109. public:
  110. IWritableShaderSource() = default;
  111. virtual ~IWritableShaderSource() = default;
  112. virtual u32 getShaderIdDirect(const std::string &name,
  113. const u8 material_type, const u8 drawtype){return 0;}
  114. virtual ShaderInfo getShaderInfo(u32 id){return ShaderInfo();}
  115. virtual u32 getShader(const std::string &name,
  116. const u8 material_type, const u8 drawtype){return 0;}
  117. virtual void processQueue()=0;
  118. virtual void insertSourceShader(const std::string &name_of_shader,
  119. const std::string &filename, const std::string &program)=0;
  120. virtual void rebuildShaders()=0;
  121. virtual void addShaderConstantSetterFactory(IShaderConstantSetterFactory *setter) = 0;
  122. };
  123. IWritableShaderSource *createShaderSource();
  124. void dumpShaderProgram(std::ostream &output_stream,
  125. const std::string &program_type, const std::string &program);