RenderContext.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "Color4.h"
  25. #include "Matrix4x4.h"
  26. #include "CommandBuffer.h"
  27. #include "ConstantBuffer.h"
  28. #include "RendererTypes.h"
  29. #include "Log.h"
  30. namespace crown
  31. {
  32. #define MAX_RENDER_LAYERS 32
  33. #define MAX_RENDER_STATES 1024
  34. // State flags
  35. #define STATE_NONE 0x0000000000000000
  36. #define STATE_DEPTH_WRITE 0x0000000000000001
  37. #define STATE_COLOR_WRITE 0x0000000000000002
  38. #define STATE_ALPHA_WRITE 0x0000000000000004
  39. #define STATE_CULL_CW 0x0000000000000010
  40. #define STATE_CULL_CCW 0x0000000000000020
  41. #define STATE_TEXTURE_0 0x0000000000000100
  42. #define STATE_TEXTURE_1 0x0000000000000200
  43. #define STATE_TEXTURE_2 0x0000000000000400
  44. #define STATE_TEXTURE_3 0x0000000000000800
  45. #define STATE_TEXTURE_MASK 0x0000000000000F00
  46. #define STATE_MAX_TEXTURES 4
  47. #define STATE_PRIMITIVE_TRIANGLES 0x0000000000000000
  48. #define STATE_PRIMITIVE_POINTS 0x0000000000001000
  49. #define STATE_PRIMITIVE_LINES 0x0000000000002000
  50. #define STATE_PRIMITIVE_MASK 0x000000000000F000
  51. #define STATE_PRIMITIVE_SHIFT 12
  52. #define STATE_BLEND_FUNC_ZERO 0x0000000000010000
  53. #define STATE_BLEND_FUNC_ONE 0x0000000000020000
  54. #define STATE_BLEND_FUNC_SRC_COLOR 0x0000000000030000
  55. #define STATE_BLEND_FUNC_ONE_MINUS_SRC_COLOR 0x0000000000040000
  56. #define STATE_BLEND_FUNC_DST_COLOR 0x0000000000050000
  57. #define STATE_BLEND_FUNC_ONE_MINUS_DST_COLOR 0x0000000000060000
  58. #define STATE_BLEND_FUNC_SRC_ALPHA 0x0000000000070000
  59. #define STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA 0x0000000000080000
  60. #define STATE_BLEND_FUNC_DST_ALPHA 0x0000000000090000
  61. #define STATE_BLEND_FUNC_ONE_MINUS_DST_ALPHA 0x00000000000A0000
  62. #define STATE_BLEND_FUNC_MASK 0x0000000000FF0000
  63. #define STATE_BLEND_FUNC_SHIFT 16
  64. #define STATE_BLEND_EQUATION_ADD 0x0000000001000000
  65. #define STATE_BLEND_EQUATION_SUBTRACT 0x0000000002000000
  66. #define STATE_BLEND_EQUATION_REVERSE_SUBTRACT 0x0000000003000000
  67. #define STATE_BLEND_EQUATION_MASK 0x0000000003000000
  68. #define STATE_BLEND_EQUATION_SHIFT 24
  69. #define STATE_DEPTH_TEST_NEVER 0x0000000010000000
  70. #define STATE_DEPTH_TEST_LESS 0x0000000020000000
  71. #define STATE_DEPTH_TEST_EQUAL 0x0000000030000000
  72. #define STATE_DEPTH_TEST_LEQUAL 0x0000000040000000
  73. #define STATE_DEPTH_TEST_GREATER 0x0000000050000000
  74. #define STATE_DEPTH_TEST_NOTEQUAL 0x0000000060000000
  75. #define STATE_DEPTH_TEST_GEQUAL 0x0000000070000000
  76. #define STATE_DEPTH_TEST_ALWAYS 0x0000000080000000
  77. #define STATE_DEPTH_TEST_MASK 0x00000000F0000000
  78. #define STATE_DEPTH_TEST_SHIFT 28
  79. #define STATE_BLEND_FUNC(src, dst) (uint64_t(src << 4) | uint64_t(dst))
  80. #define CLEAR_COLOR 0x1
  81. #define CLEAR_DEPTH 0x2
  82. // Texture flags
  83. #define TEXTURE_FILTER_NEAREST 0x00000001
  84. #define TEXTURE_FILTER_LINEAR 0x00000002
  85. #define TEXTURE_FILTER_BILINEAR 0x00000003
  86. #define TEXTURE_FILTER_TRILINEAR 0x00000004
  87. #define TEXTURE_FILTER_MASK 0x0000000F
  88. #define TEXTURE_FILTER_SHIFT 0
  89. #define TEXTURE_WRAP_CLAMP_EDGE 0x00000010
  90. #define TEXTURE_WRAP_CLAMP_REPEAT 0x00000020
  91. #define TEXTURE_WRAP_MASK 0x000000F0
  92. #define TEXTURE_WRAP_SHIFT 4
  93. // Sampler flags
  94. #define SAMPLER_TEXTURE 0x10000000
  95. #define SAMPLER_MASK 0xF0000000
  96. #define SAMPLER_SHIFT 28
  97. struct ViewRect
  98. {
  99. void clear()
  100. {
  101. m_x = 0;
  102. m_y = 0;
  103. m_width = 0;
  104. m_height = 0;
  105. }
  106. uint32_t area() const
  107. {
  108. return (m_width - m_x) * (m_height - m_y);
  109. }
  110. uint16_t m_x;
  111. uint16_t m_y;
  112. uint16_t m_width;
  113. uint16_t m_height;
  114. };
  115. struct ClearState
  116. {
  117. void clear()
  118. {
  119. m_color = Color4::GRAY;
  120. m_depth = 1.0f;
  121. }
  122. public:
  123. uint8_t m_flags;
  124. Color4 m_color;
  125. float m_depth;
  126. };
  127. struct Sampler
  128. {
  129. Id sampler_id;
  130. uint32_t flags;
  131. };
  132. struct RenderState
  133. {
  134. void clear()
  135. {
  136. m_flags = STATE_NONE;
  137. pose = Matrix4x4::IDENTITY;
  138. program.id = INVALID_ID;
  139. vb.id = INVALID_ID;
  140. ib.id = INVALID_ID;
  141. for (uint32_t i = 0; i < STATE_MAX_TEXTURES; i++)
  142. {
  143. samplers[i].sampler_id.id = INVALID_ID;
  144. samplers[i].flags = SAMPLER_TEXTURE;
  145. }
  146. }
  147. public:
  148. uint64_t m_flags;
  149. Matrix4x4 pose;
  150. GPUProgramId program;
  151. VertexBufferId vb;
  152. IndexBufferId ib;
  153. Sampler samplers[STATE_MAX_TEXTURES];
  154. };
  155. struct RenderKey
  156. {
  157. void clear()
  158. {
  159. m_layer = 0;
  160. }
  161. uint64_t encode()
  162. {
  163. return uint64_t(m_layer) << 56;
  164. }
  165. void decode(uint64_t key)
  166. {
  167. m_layer = (key >> 56) & 0xFF;
  168. }
  169. public:
  170. uint8_t m_layer;
  171. };
  172. struct RenderContext
  173. {
  174. RenderContext()
  175. {
  176. clear();
  177. }
  178. void set_state(uint64_t flags)
  179. {
  180. m_state.m_flags = flags;
  181. }
  182. void set_pose(const Matrix4x4& pose)
  183. {
  184. m_state.pose = pose;
  185. }
  186. void set_program(GPUProgramId program)
  187. {
  188. m_state.program = program;
  189. }
  190. void set_vertex_buffer(VertexBufferId vb)
  191. {
  192. m_state.vb = vb;
  193. }
  194. void set_index_buffer(IndexBufferId ib)
  195. {
  196. m_state.ib = ib;
  197. }
  198. void set_uniform(UniformId id, UniformType::Enum type, const void* value, uint8_t num)
  199. {
  200. m_constants.write_constant(id, type, value, num);
  201. }
  202. void set_texture(uint8_t unit, UniformId sampler_uniform, TextureId texture, uint32_t flags)
  203. {
  204. m_flags |= STATE_TEXTURE_0 << unit;
  205. Sampler& sampler = m_state.samplers[unit];
  206. sampler.sampler_id = texture;
  207. sampler.flags |= SAMPLER_TEXTURE | flags;
  208. const uint32_t val = unit;
  209. set_uniform(sampler_uniform, UniformType::INTEGER_1, &val, 1);
  210. }
  211. void set_layer_render_target(uint8_t layer, RenderTargetId target)
  212. {
  213. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  214. m_targets[layer] = target;
  215. }
  216. void set_layer_clear(uint8_t layer, uint8_t flags, const Color4& color, float depth)
  217. {
  218. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  219. m_clears[layer].m_flags = flags;
  220. m_clears[layer].m_color = color;
  221. m_clears[layer].m_depth = depth;
  222. }
  223. void set_layer_view(uint8_t layer, const Matrix4x4& view)
  224. {
  225. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  226. m_view_matrices[layer] = view;
  227. }
  228. void set_layer_projection(uint8_t layer, const Matrix4x4& projection)
  229. {
  230. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  231. m_projection_matrices[layer] = projection;
  232. }
  233. void set_layer_viewport(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  234. {
  235. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  236. m_viewports[layer].m_x = x;
  237. m_viewports[layer].m_y = y;
  238. m_viewports[layer].m_width = width;
  239. m_viewports[layer].m_height = height;
  240. }
  241. void set_layer_scissor(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  242. {
  243. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  244. m_scissors[layer].m_x = x;
  245. m_scissors[layer].m_y = y;
  246. m_scissors[layer].m_width = width;
  247. m_scissors[layer].m_height = height;
  248. }
  249. void commit(uint8_t layer)
  250. {
  251. m_render_key.m_layer = layer;
  252. m_states[m_num_states] = m_state;
  253. m_keys[m_num_states] = m_render_key.encode();
  254. m_num_states++;
  255. m_render_key.clear();
  256. m_state.clear();
  257. }
  258. void clear()
  259. {
  260. m_flags = STATE_NONE;
  261. m_render_key.clear();
  262. m_num_states = 0;
  263. m_state.clear();
  264. }
  265. void push()
  266. {
  267. m_commands.commit();
  268. m_constants.commit();
  269. }
  270. public:
  271. uint64_t m_flags;
  272. RenderKey m_render_key;
  273. RenderState m_state;
  274. // Per-state data
  275. uint32_t m_num_states;
  276. RenderState m_states[MAX_RENDER_STATES];
  277. ClearState m_clears[MAX_RENDER_STATES];
  278. uint64_t m_keys[MAX_RENDER_STATES];
  279. // Per-layer data
  280. RenderTargetId m_targets[MAX_RENDER_LAYERS];
  281. Matrix4x4 m_view_matrices[MAX_RENDER_LAYERS];
  282. Matrix4x4 m_projection_matrices[MAX_RENDER_LAYERS];
  283. ViewRect m_viewports[MAX_RENDER_LAYERS];
  284. ViewRect m_scissors[MAX_RENDER_LAYERS];
  285. CommandBuffer m_commands;
  286. ConstantBuffer m_constants;
  287. };
  288. } // namespace crown