Guest User

Untitled

a guest
Jun 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. // TODO :
  2. // * Rename into Effect
  3. // * Add attachRenderBuffer (depth, stencil)
  4.  
  5. #ifndef _PASS_H_
  6. #define _PASS_H_
  7.  
  8. #include "Program.h"
  9. #include "Texture.h"
  10. #include "Renderbuffer.h"
  11.  
  12. namespace blcks
  13. {
  14. class Pass
  15. {
  16. protected:
  17. struct Input
  18. {
  19. GLuint unit;
  20. Texture *tex;
  21. };
  22.  
  23. class BaseOutput
  24. {
  25. public:
  26. virtual ~BaseOutput() {}
  27. virtual void Attach() const = 0;
  28. virtual void Detach() const = 0;
  29. };
  30.  
  31. template<typename Image>
  32. class Output : public BaseOutput
  33. {
  34. public:
  35. Output(GLenum attachment, Image* target, int level=0, int layer=0);
  36. Output(const Output& out);
  37. virtual ~Output() {}
  38.  
  39. Output& operator=(const Output& out);
  40.  
  41. virtual void Attach() const { Log_Error(""); }
  42. virtual void Detach() const { Log_Error(""); }
  43. protected:
  44. GLenum _attachment;
  45. Image *_target;
  46. GLint _level;
  47. GLint _layer;
  48. };
  49.  
  50. public:
  51. typedef Output<Renderbuffer> RenderbufferOutput;
  52. typedef Output<Texture2D> Texture2DOutput;
  53. typedef Output<Texture3D> Texture3DOutput;
  54.  
  55. protected:
  56. enum State
  57. {
  58. UNITIALIZED = 0,
  59. COMPILATION_NEEDED,
  60. READY
  61. };
  62.  
  63. public:
  64. Pass();
  65. virtual ~Pass();
  66.  
  67. virtual bool Create (GLsizei width, GLsizei height, int nInputs=0);
  68. virtual void Destroy();
  69.  
  70. virtual bool Compile();
  71.  
  72. virtual void Begin();
  73. virtual void End();
  74.  
  75. virtual void Update() = 0;
  76.  
  77. bool AttachInput (unsigned int index, Texture* inTex);
  78.  
  79. template <typename Image>
  80. bool AttachOutput(GLenum attachment, Image* target, int level=0, int layer=0);
  81.  
  82. protected:
  83. int _state;
  84.  
  85. GLuint _framebuffer;
  86. GLsizei _width, _height;
  87.  
  88. GLenum* _drawBuffers;
  89.  
  90. unsigned int _nInputs;
  91. Input *_in;
  92.  
  93. unsigned int _nOutputs;
  94. Pass::BaseOutput **_out;
  95. };
  96.  
  97. template<typename Image>
  98. Pass::Output<typename Image>::Output(GLenum attachment, Image* target, int level, int layer) :
  99. _attachment(attachment),
  100. _target(target),
  101. _level(level),
  102. _layer(layer)
  103. {}
  104.  
  105. template<typename Image>
  106. Pass::Output<typename Image>::Output(const Output& out) :
  107. _attachment(out._attachment),
  108. _target(out._target),
  109. _level(out._level),
  110. _layer(out._layer)
  111. {}
  112.  
  113. template<typename Image>
  114. Pass::Output<typename Image>& Pass::Output<typename Image>::operator=(const Output& out)
  115. {
  116. _attachment = out._attachment;
  117. _target = out._target;
  118. _level = out._level;
  119. _layer = out._layer;
  120.  
  121. return *this;
  122. }
  123.  
  124. template<>
  125. void Pass::Output<Renderbuffer>::Attach() const
  126. {
  127. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, _attachment, GL_RENDERBUFFER, _target->Id());
  128. }
  129.  
  130. template<>
  131. void Pass::Output<Renderbuffer>::Detach() const
  132. {
  133. glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, _attachment, GL_RENDERBUFFER, 0);
  134. }
  135.  
  136. template<>
  137. void Pass::Output<Texture2D>::Attach () const
  138. {
  139. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, _attachment, _target->GetType(), _target->Id(), _level);
  140. }
  141.  
  142. template<>
  143. void Pass::Output<Texture2D>::Detach () const
  144. {
  145. glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, _attachment, _target->GetType(), 0, _level);
  146. }
  147.  
  148. template<>
  149. void Pass::Output<Texture3D>::Attach() const
  150. {
  151. glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, _attachment, _target->GetType(), _target->Id(), _level, _layer);
  152. }
  153.  
  154. template<>
  155. void Pass::Output<Texture3D>::Detach() const
  156. {
  157. glFramebufferTexture3DEXT(GL_FRAMEBUFFER_EXT, _attachment, _target->GetType(), 0, _level, _layer);
  158. }
  159.  
  160. template<typename Image>
  161. bool Pass::AttachOutput(GLenum attachment, Image* target, int level, int layer)
  162. {
  163. int index;
  164.  
  165. // Validate target
  166. if(target->Width() != _width)
  167. {
  168. Log_Error("Target width (%d) does not match framebuffer width (%d)", target->Width(), _width);
  169. return false;
  170. }
  171.  
  172. if(target->Height() != _height)
  173. {
  174. Log_Error("Target height (%d) does not match framebuffer height (%d)",target->Height(), _height);
  175. return false;
  176. }
  177.  
  178. switch(attachment)
  179. {
  180. case GL_DEPTH_ATTACHMENT:
  181. index = 0;
  182. break;
  183. case GL_STENCIL_ATTACHMENT:
  184. index = 1;
  185. break;
  186. case GL_DEPTH_STENCIL_ATTACHMENT:
  187. index = 2;
  188. break;
  189. default:
  190. if((attachment < GL_COLOR_ATTACHMENT0) || (attachment > (GL_COLOR_ATTACHMENT0+_nOutputs-3)))
  191. {
  192. Log_Error("Unsupported attachment type: %x", attachment);
  193. return false;
  194. }
  195. index = attachment - GL_COLOR_ATTACHMENT0;
  196. break;
  197. }
  198.  
  199. // Add it to output list
  200. Pass::Output<Image> *output = new Pass::Output<Image>(attachment, target, level, layer);
  201. if(output == NULL)
  202. {
  203. Log_Error("Memory allocation failed!");
  204. return false;
  205. }
  206.  
  207. _out[index] = output;
  208.  
  209. return true;
  210. }
  211.  
  212. }
  213. #endif // _PASS_H_
Add Comment
Please, Sign In to add comment