PolyShader.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyColor.h"
  23. #include "PolyVector2.h"
  24. #include "PolyVector3.h"
  25. #include "PolyMatrix4.h"
  26. #include "PolyResource.h"
  27. #include <string.h>
  28. namespace Polycode {
  29. class Cubemap;
  30. class ShaderBinding;
  31. class Texture;
  32. class _PolyExport ProgramParam {
  33. public:
  34. String name;
  35. int type;
  36. static void *createParamData(int type);
  37. static const int PARAM_UNKNOWN = 0;
  38. static const int PARAM_NUMBER = 1;
  39. static const int PARAM_VECTOR2 = 2;
  40. static const int PARAM_VECTOR3 = 3;
  41. static const int PARAM_COLOR = 4;
  42. static const int PARAM_MATRIX = 5;
  43. };
  44. typedef struct {
  45. Texture *texture;
  46. String name;
  47. } TextureBinding;
  48. typedef struct {
  49. Cubemap *cubemap;
  50. String name;
  51. } CubemapBinding;
  52. class _PolyExport ShaderProgram : public Resource {
  53. public:
  54. ShaderProgram(int type);
  55. virtual ~ShaderProgram();
  56. virtual void reloadProgram() {}
  57. static const int TYPE_VERT = 0;
  58. static const int TYPE_FRAG = 1;
  59. int type;
  60. void reloadResource();
  61. };
  62. class _PolyExport Shader : public Resource {
  63. public:
  64. Shader(int type);
  65. virtual ~Shader();
  66. int getType() const;
  67. void setName(const String& name);
  68. const String& getName() const;
  69. ShaderBinding *createBinding();
  70. virtual void reload() {}
  71. int getExpectedParamType(String name);
  72. virtual void setVertexProgram(ShaderProgram *vp) {}
  73. virtual void setFragmentProgram(ShaderProgram *fp) {}
  74. static const int FIXED_SHADER = 0;
  75. static const int MODULE_SHADER = 1;
  76. int numSpotLights;
  77. int numAreaLights;
  78. std::vector<String> expectedTextures;
  79. std::vector<String> expectedCubemaps;
  80. std::vector<ProgramParam> expectedParams;
  81. bool screenShader;
  82. ShaderProgram *vp;
  83. ShaderProgram *fp;
  84. protected:
  85. String name;
  86. int type;
  87. };
  88. class _PolyExport ShaderRenderTarget : public PolyBase {
  89. public:
  90. ShaderRenderTarget();
  91. String id;
  92. Number width;
  93. Number height;
  94. int sizeMode;
  95. Texture *texture;
  96. Number normalizedWidth;
  97. Number normalizedHeight;
  98. static const int SIZE_MODE_PIXELS = 0;
  99. static const int SIZE_MODE_NORMALIZED = 1;
  100. };
  101. class LocalShaderParam : public PolyBase {
  102. public:
  103. LocalShaderParam();
  104. ~LocalShaderParam();
  105. LocalShaderParam *Copy();
  106. String name;
  107. void *data;
  108. int type;
  109. bool ownsPointer;
  110. // Convenience getters/setters for Lua users
  111. Number getNumber() { return *((Number *)data); }
  112. Vector2 getVector2() { return *((Vector2 *)data); }
  113. Vector3 getVector3() { return *((Vector3 *)data); }
  114. Matrix4 getMatrix4() { return *((Matrix4 *)data); }
  115. Color getColor() { return *((Color *)data); }
  116. void setNumber(Number x) { memcpy(data, &x, sizeof(x)); }
  117. void setVector2(Vector2 x) { memcpy(data, &x, sizeof(x)); }
  118. void setVector3(Vector3 x) { memcpy(data, &x, sizeof(x)); }
  119. void setMatrix4(Matrix4 x) { memcpy(data, &x, sizeof(x)); }
  120. void setColor(Color x) { static_cast<Color*>(data)->setColor(&x); }
  121. void setParamValueFromString(int type, String pvalue);
  122. };
  123. class RenderTargetBinding : public PolyBase {
  124. public:
  125. String id;
  126. String name;
  127. int mode;
  128. Texture *texture;
  129. static const int MODE_IN= 0;
  130. static const int MODE_OUT = 1;
  131. static const int MODE_COLOR = 2;
  132. static const int MODE_DEPTH = 3;
  133. };
  134. class _PolyExport ShaderBinding : public PolyBase {
  135. public:
  136. ShaderBinding(Shader *shader);
  137. virtual ~ShaderBinding();
  138. void copyTo(ShaderBinding *targetBinding);
  139. Texture *getTexture(const String& name);
  140. Cubemap *getCubemap(const String& name);
  141. void clearTexture(const String& name);
  142. void clearCubemap(const String& name);
  143. void addTexture(const String& name, Texture *texture);
  144. void addCubemap(const String& name, Cubemap *cubemap);
  145. LocalShaderParam *addParam(int type, const String& name);
  146. LocalShaderParam *addParamPointer(int type, const String& name, void *ptr);
  147. unsigned int getNumLocalParams();
  148. LocalShaderParam *getLocalParam(unsigned int index);
  149. LocalShaderParam *getLocalParamByName(const String& name);
  150. void addRenderTargetBinding(RenderTargetBinding *binding);
  151. void removeRenderTargetBinding(RenderTargetBinding *binding);
  152. unsigned int getNumRenderTargetBindings();
  153. RenderTargetBinding *getRenderTargetBinding(unsigned int index);
  154. unsigned int getNumInTargetBindings();
  155. RenderTargetBinding *getInTargetBinding(unsigned int index);
  156. unsigned int getNumColorTargetBindings();
  157. RenderTargetBinding *getColorTargetBinding(unsigned int index);
  158. unsigned int getNumDepthTargetBindings();
  159. RenderTargetBinding *getDepthTargetBinding(unsigned int index);
  160. unsigned int getNumOutTargetBindings();
  161. RenderTargetBinding *getOutTargetBinding(unsigned int index);
  162. std::vector<TextureBinding> textures;
  163. std::vector<CubemapBinding> cubemaps;
  164. Shader* shader;
  165. std::vector<LocalShaderParam*> localParams;
  166. std::vector<RenderTargetBinding*> renderTargetBindings;
  167. std::vector<RenderTargetBinding*> inTargetBindings;
  168. std::vector<RenderTargetBinding*> outTargetBindings;
  169. std::vector<RenderTargetBinding*> colorTargetBindings;
  170. std::vector<RenderTargetBinding*> depthTargetBindings;
  171. };
  172. }