pipeline.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/types.h"
  6. #include "device/pipeline.h"
  7. #include "world/shader_manager.h"
  8. #include <bx/math.h>
  9. namespace crown
  10. {
  11. /*
  12. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  13. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  14. */
  15. struct PosTexCoord0Vertex
  16. {
  17. float m_x;
  18. float m_y;
  19. float m_z;
  20. float m_u;
  21. float m_v;
  22. static void init()
  23. {
  24. ms_layout
  25. .begin()
  26. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  27. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float)
  28. .end();
  29. }
  30. static bgfx::VertexLayout ms_layout;
  31. };
  32. bgfx::VertexLayout PosTexCoord0Vertex::ms_layout;
  33. /*
  34. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  35. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  36. */
  37. void screenSpaceQuad(float _textureWidth, float _textureHeight, float _texelHalf, bool _originBottomLeft, float _width = 1.0f, float _height = 1.0f)
  38. {
  39. if (3 == bgfx::getAvailTransientVertexBuffer(3, PosTexCoord0Vertex::ms_layout) )
  40. {
  41. bgfx::TransientVertexBuffer tvb;
  42. bgfx::allocTransientVertexBuffer(&tvb, 3, PosTexCoord0Vertex::ms_layout);
  43. PosTexCoord0Vertex* vertex = (PosTexCoord0Vertex*)tvb.data;
  44. const float minx = -_width;
  45. const float maxx = _width;
  46. const float miny = _height;
  47. const float maxy = -_height;
  48. const float texelHalfW = _texelHalf/_textureWidth;
  49. const float texelHalfH = _texelHalf/_textureHeight;
  50. const float minu = -1.0f + texelHalfW;
  51. const float maxu = 1.0f + texelHalfH;
  52. const float zz = 0.0f;
  53. float minv = texelHalfH;
  54. float maxv = 2.0f + texelHalfH;
  55. if (_originBottomLeft)
  56. {
  57. float temp = minv;
  58. minv = maxv;
  59. maxv = temp;
  60. minv -= 1.0f;
  61. maxv -= 1.0f;
  62. }
  63. vertex[0].m_x = maxx;
  64. vertex[0].m_y = maxy;
  65. vertex[0].m_z = zz;
  66. vertex[0].m_u = maxu;
  67. vertex[0].m_v = maxv;
  68. vertex[1].m_x = maxx;
  69. vertex[1].m_y = miny;
  70. vertex[1].m_z = zz;
  71. vertex[1].m_u = maxu;
  72. vertex[1].m_v = minv;
  73. vertex[2].m_x = minx;
  74. vertex[2].m_y = miny;
  75. vertex[2].m_z = zz;
  76. vertex[2].m_u = minu;
  77. vertex[2].m_v = minv;
  78. bgfx::setVertexBuffer(0, &tvb);
  79. }
  80. }
  81. Pipeline::Pipeline()
  82. : _main_color_texture(BGFX_INVALID_HANDLE)
  83. , _main_depth_texture(BGFX_INVALID_HANDLE)
  84. , _main_frame_buffer(BGFX_INVALID_HANDLE)
  85. , _main_color_texture_sampler(BGFX_INVALID_HANDLE)
  86. , _main_depth_texture_sampler(BGFX_INVALID_HANDLE)
  87. , _selection_texture(BGFX_INVALID_HANDLE)
  88. , _selection_depth_texture(BGFX_INVALID_HANDLE)
  89. , _selection_frame_buffer(BGFX_INVALID_HANDLE)
  90. , _selection_texture_sampler(BGFX_INVALID_HANDLE)
  91. , _selection_depth_texture_sampler(BGFX_INVALID_HANDLE)
  92. , _outline_color_uniform(BGFX_INVALID_HANDLE)
  93. {
  94. }
  95. void Pipeline::create(uint16_t width, uint16_t height)
  96. {
  97. reset(width, height);
  98. _main_color_texture_sampler = bgfx::createUniform("s_color", bgfx::UniformType::Sampler);
  99. _main_depth_texture_sampler = bgfx::createUniform("s_main_depth", bgfx::UniformType::Sampler);
  100. _selection_texture_sampler = bgfx::createUniform("s_selection", bgfx::UniformType::Sampler);
  101. _selection_depth_texture_sampler = bgfx::createUniform("s_selection_depth", bgfx::UniformType::Sampler);
  102. _outline_color_uniform = bgfx::createUniform("u_outline_color", bgfx::UniformType::Vec4);
  103. PosTexCoord0Vertex::init();
  104. }
  105. void Pipeline::destroy()
  106. {
  107. bgfx::destroy(_outline_color_uniform);
  108. bgfx::destroy(_selection_depth_texture_sampler);
  109. bgfx::destroy(_selection_texture_sampler);
  110. bgfx::destroy(_main_depth_texture_sampler);
  111. bgfx::destroy(_main_color_texture_sampler);
  112. bgfx::destroy(_selection_frame_buffer);
  113. bgfx::destroy(_selection_depth_texture);
  114. bgfx::destroy(_selection_texture);
  115. bgfx::destroy(_main_frame_buffer);
  116. bgfx::destroy(_main_depth_texture);
  117. bgfx::destroy(_main_color_texture);
  118. }
  119. void Pipeline::reset(u16 width, u16 height)
  120. {
  121. // Create main frame buffer.
  122. if (bgfx::isValid(_main_color_texture))
  123. bgfx::destroy(_main_color_texture);
  124. _main_color_texture = bgfx::createTexture2D(width
  125. , height
  126. , false
  127. , 1
  128. , bgfx::TextureFormat::BGRA8
  129. , BGFX_TEXTURE_RT
  130. );
  131. if (bgfx::isValid(_main_depth_texture))
  132. bgfx::destroy(_main_depth_texture);
  133. _main_depth_texture = bgfx::createTexture2D(width
  134. , height
  135. , false
  136. , 1
  137. , bgfx::TextureFormat::D24S8
  138. , BGFX_TEXTURE_RT
  139. );
  140. const bgfx::TextureHandle _main_frame_buffer_attachments[] =
  141. {
  142. _main_color_texture,
  143. _main_depth_texture
  144. };
  145. if (bgfx::isValid(_main_frame_buffer))
  146. bgfx::destroy(_main_frame_buffer);
  147. _main_frame_buffer = bgfx::createFrameBuffer(countof(_main_frame_buffer_attachments), _main_frame_buffer_attachments);
  148. // Create outline frame buffer.
  149. if (bgfx::isValid(_selection_texture))
  150. bgfx::destroy(_selection_texture);
  151. _selection_texture = bgfx::createTexture2D(width
  152. , height
  153. , false
  154. , 1
  155. , bgfx::TextureFormat::R32U
  156. , BGFX_TEXTURE_RT
  157. );
  158. if (bgfx::isValid(_selection_depth_texture))
  159. bgfx::destroy(_selection_depth_texture);
  160. _selection_depth_texture = bgfx::createTexture2D(width
  161. , height
  162. , false
  163. , 1
  164. , bgfx::TextureFormat::D24
  165. , BGFX_TEXTURE_RT
  166. );
  167. const bgfx::TextureHandle _selection_frame_buffer_attachments[] =
  168. {
  169. _selection_texture,
  170. _selection_depth_texture
  171. };
  172. if (bgfx::isValid(_selection_frame_buffer))
  173. bgfx::destroy(_selection_frame_buffer);
  174. _selection_frame_buffer = bgfx::createFrameBuffer(countof(_selection_frame_buffer_attachments), _selection_frame_buffer_attachments);
  175. }
  176. void Pipeline::render(ShaderManager& sm, StringId32 program, u8 view, u16 width, u16 height)
  177. {
  178. const bgfx::Caps* caps = bgfx::getCaps();
  179. f32 ortho[16];
  180. bx::mtxOrtho(ortho, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 100.0f, 0.0f, caps->homogeneousDepth);
  181. bgfx::setViewRect(view, 0, 0, width, height);
  182. bgfx::setViewTransform(view, NULL, ortho);
  183. const u32 samplerFlags = 0
  184. | BGFX_SAMPLER_MIN_POINT
  185. | BGFX_SAMPLER_MAG_POINT
  186. | BGFX_SAMPLER_MIP_POINT
  187. | BGFX_SAMPLER_U_CLAMP
  188. | BGFX_SAMPLER_V_CLAMP
  189. ;
  190. bgfx::setTexture(0, _main_color_texture_sampler, _main_color_texture, samplerFlags);
  191. screenSpaceQuad(width, height, 0.0f, caps->originBottomLeft);
  192. sm.submit(program, view, 0, UINT64_MAX);
  193. bgfx::setTexture(0, _selection_texture_sampler, _selection_texture, samplerFlags);
  194. bgfx::setTexture(1, _selection_depth_texture_sampler, _selection_depth_texture, samplerFlags);
  195. bgfx::setTexture(2, _main_depth_texture_sampler, _main_depth_texture, samplerFlags);
  196. screenSpaceQuad(width, height, 0.0f, caps->originBottomLeft);
  197. const f32 outline_color[] = { 1.0f, 0.37f, 0.05f, 1.0f };
  198. bgfx::setUniform(_outline_color_uniform, outline_color);
  199. sm.submit(STRING_ID_32("outline", UINT32_C(0x57fddcc9)), view, 0, UINT64_MAX);
  200. }
  201. } // namespace crown