RenderContext.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 <algorithm>
  25. #include "Color4.h"
  26. #include "Matrix4x4.h"
  27. #include "CommandBuffer.h"
  28. #include "ConstantBuffer.h"
  29. #include "RendererTypes.h"
  30. #include "Log.h"
  31. namespace crown
  32. {
  33. #define MAX_RENDER_LAYERS 8
  34. #define MAX_RENDER_STATES 1024
  35. // Layer flags
  36. #define RENDER_LAYER_MASK 0xE000000000000000
  37. #define RENDER_LAYER_SHIFT 61
  38. // State flags
  39. #define STATE_NONE 0x0000000000000000
  40. #define STATE_DEPTH_WRITE 0x0000000000000001
  41. #define STATE_COLOR_WRITE 0x0000000000000002
  42. #define STATE_ALPHA_WRITE 0x0000000000000004
  43. #define STATE_CULL_CW 0x0000000000000010
  44. #define STATE_CULL_CCW 0x0000000000000020
  45. #define STATE_TEXTURE_0 0x0000000000000100
  46. #define STATE_TEXTURE_1 0x0000000000000200
  47. #define STATE_TEXTURE_2 0x0000000000000400
  48. #define STATE_TEXTURE_3 0x0000000000000800
  49. #define STATE_TEXTURE_MASK 0x0000000000000F00
  50. #define STATE_MAX_TEXTURES 4
  51. #define STATE_PRIMITIVE_TRIANGLES 0x0000000000000000
  52. #define STATE_PRIMITIVE_POINTS 0x0000000000001000
  53. #define STATE_PRIMITIVE_LINES 0x0000000000002000
  54. #define STATE_PRIMITIVE_MASK 0x000000000000F000
  55. #define STATE_PRIMITIVE_SHIFT 12
  56. #define STATE_BLEND_FUNC_ZERO 0x0000000000010000
  57. #define STATE_BLEND_FUNC_ONE 0x0000000000020000
  58. #define STATE_BLEND_FUNC_SRC_COLOR 0x0000000000030000
  59. #define STATE_BLEND_FUNC_ONE_MINUS_SRC_COLOR 0x0000000000040000
  60. #define STATE_BLEND_FUNC_DST_COLOR 0x0000000000050000
  61. #define STATE_BLEND_FUNC_ONE_MINUS_DST_COLOR 0x0000000000060000
  62. #define STATE_BLEND_FUNC_SRC_ALPHA 0x0000000000070000
  63. #define STATE_BLEND_FUNC_ONE_MINUS_SRC_ALPHA 0x0000000000080000
  64. #define STATE_BLEND_FUNC_DST_ALPHA 0x0000000000090000
  65. #define STATE_BLEND_FUNC_ONE_MINUS_DST_ALPHA 0x00000000000A0000
  66. #define STATE_BLEND_FUNC_MASK 0x0000000000FF0000
  67. #define STATE_BLEND_FUNC_SHIFT 16
  68. #define STATE_BLEND_EQUATION_ADD 0x0000000001000000
  69. #define STATE_BLEND_EQUATION_SUBTRACT 0x0000000002000000
  70. #define STATE_BLEND_EQUATION_REVERSE_SUBTRACT 0x0000000003000000
  71. #define STATE_BLEND_EQUATION_MASK 0x0000000003000000
  72. #define STATE_BLEND_EQUATION_SHIFT 24
  73. #define STATE_DEPTH_TEST_NEVER 0x0000000010000000
  74. #define STATE_DEPTH_TEST_LESS 0x0000000020000000
  75. #define STATE_DEPTH_TEST_EQUAL 0x0000000030000000
  76. #define STATE_DEPTH_TEST_LEQUAL 0x0000000040000000
  77. #define STATE_DEPTH_TEST_GREATER 0x0000000050000000
  78. #define STATE_DEPTH_TEST_NOTEQUAL 0x0000000060000000
  79. #define STATE_DEPTH_TEST_GEQUAL 0x0000000070000000
  80. #define STATE_DEPTH_TEST_ALWAYS 0x0000000080000000
  81. #define STATE_DEPTH_TEST_MASK 0x00000000F0000000
  82. #define STATE_DEPTH_TEST_SHIFT 28
  83. #define STATE_BLEND_FUNC(src, dst) (uint64_t(src << 4) | uint64_t(dst))
  84. #define CLEAR_COLOR 0x1
  85. #define CLEAR_DEPTH 0x2
  86. // Texture flags
  87. #define TEXTURE_FILTER_NEAREST 0x00000001
  88. #define TEXTURE_FILTER_LINEAR 0x00000002
  89. #define TEXTURE_FILTER_BILINEAR 0x00000003
  90. #define TEXTURE_FILTER_TRILINEAR 0x00000004
  91. #define TEXTURE_FILTER_MASK 0x0000000F
  92. #define TEXTURE_FILTER_SHIFT 0
  93. #define TEXTURE_WRAP_CLAMP_EDGE 0x00000010
  94. #define TEXTURE_WRAP_CLAMP_REPEAT 0x00000020
  95. #define TEXTURE_WRAP_MASK 0x000000F0
  96. #define TEXTURE_WRAP_SHIFT 4
  97. // Sampler flags
  98. #define SAMPLER_TEXTURE 0x10000000
  99. #define SAMPLER_MASK 0xF0000000
  100. #define SAMPLER_SHIFT 28
  101. struct ViewRect
  102. {
  103. void clear()
  104. {
  105. m_x = 0;
  106. m_y = 0;
  107. m_width = 0;
  108. m_height = 0;
  109. }
  110. uint32_t area() const
  111. {
  112. return (m_width - m_x) * (m_height - m_y);
  113. }
  114. uint16_t m_x;
  115. uint16_t m_y;
  116. uint16_t m_width;
  117. uint16_t m_height;
  118. };
  119. struct ClearState
  120. {
  121. void clear()
  122. {
  123. m_flags = 0;
  124. m_color = Color4::GRAY;
  125. m_depth = 1.0f;
  126. }
  127. public:
  128. uint8_t m_flags;
  129. Color4 m_color;
  130. float m_depth;
  131. };
  132. struct Sampler
  133. {
  134. Id sampler_id;
  135. uint32_t flags;
  136. };
  137. /// Maintains the states necessary for a single draw call.
  138. struct RenderState
  139. {
  140. void clear()
  141. {
  142. m_flags = STATE_NONE;
  143. pose = Matrix4x4::IDENTITY;
  144. program.id = INVALID_ID;
  145. vb.id = INVALID_ID;
  146. ib.id = INVALID_ID;
  147. for (uint32_t i = 0; i < STATE_MAX_TEXTURES; i++)
  148. {
  149. samplers[i].sampler_id.id = INVALID_ID;
  150. samplers[i].flags = SAMPLER_TEXTURE;
  151. }
  152. }
  153. public:
  154. uint64_t m_flags;
  155. Matrix4x4 pose;
  156. GPUProgramId program;
  157. VertexBufferId vb;
  158. IndexBufferId ib;
  159. uint32_t start_index;
  160. uint32_t num_indices;
  161. Sampler samplers[STATE_MAX_TEXTURES];
  162. };
  163. struct RenderKey
  164. {
  165. void clear()
  166. {
  167. m_layer = 0;
  168. }
  169. uint64_t encode()
  170. {
  171. return uint64_t(m_layer) << RENDER_LAYER_SHIFT;
  172. }
  173. void decode(uint64_t key)
  174. {
  175. m_layer = (key & RENDER_LAYER_MASK) >> RENDER_LAYER_SHIFT;
  176. }
  177. public:
  178. uint8_t m_layer;
  179. };
  180. struct RenderContext
  181. {
  182. RenderContext()
  183. {
  184. clear();
  185. }
  186. void set_state(uint64_t flags)
  187. {
  188. m_state.m_flags = flags;
  189. }
  190. void set_pose(const Matrix4x4& pose)
  191. {
  192. m_state.pose = pose;
  193. }
  194. void set_program(GPUProgramId program)
  195. {
  196. m_state.program = program;
  197. }
  198. void set_vertex_buffer(VertexBufferId vb)
  199. {
  200. m_state.vb = vb;
  201. }
  202. void set_index_buffer(IndexBufferId ib, uint32_t start_index, uint32_t num_indices)
  203. {
  204. m_state.ib = ib;
  205. m_state.start_index = start_index;
  206. m_state.num_indices = num_indices;
  207. }
  208. void set_uniform(UniformId id, UniformType::Enum type, const void* value, uint8_t num)
  209. {
  210. m_constants.write_constant(id, type, value, num);
  211. }
  212. void set_texture(uint8_t unit, UniformId sampler_uniform, TextureId texture, uint32_t flags)
  213. {
  214. m_flags |= STATE_TEXTURE_0 << unit;
  215. Sampler& sampler = m_state.samplers[unit];
  216. sampler.sampler_id = texture;
  217. sampler.flags |= SAMPLER_TEXTURE | flags;
  218. const uint32_t val = unit;
  219. set_uniform(sampler_uniform, UniformType::INTEGER_1, &val, 1);
  220. }
  221. void set_layer_render_target(uint8_t layer, RenderTargetId target)
  222. {
  223. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  224. m_targets[layer] = target;
  225. }
  226. void set_layer_clear(uint8_t layer, uint8_t flags, const Color4& color, float depth)
  227. {
  228. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  229. m_clears[layer].m_flags = flags;
  230. m_clears[layer].m_color = color;
  231. m_clears[layer].m_depth = depth;
  232. }
  233. void set_layer_view(uint8_t layer, const Matrix4x4& view)
  234. {
  235. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  236. m_view_matrices[layer] = view;
  237. }
  238. void set_layer_projection(uint8_t layer, const Matrix4x4& projection)
  239. {
  240. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  241. m_projection_matrices[layer] = projection;
  242. }
  243. void set_layer_viewport(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  244. {
  245. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  246. m_viewports[layer].m_x = x;
  247. m_viewports[layer].m_y = y;
  248. m_viewports[layer].m_width = width;
  249. m_viewports[layer].m_height = height;
  250. }
  251. void set_layer_scissor(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  252. {
  253. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  254. m_scissors[layer].m_x = x;
  255. m_scissors[layer].m_y = y;
  256. m_scissors[layer].m_width = width;
  257. m_scissors[layer].m_height = height;
  258. }
  259. void commit(uint8_t layer)
  260. {
  261. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  262. m_render_key.m_layer = layer;
  263. m_states[m_num_states] = m_state;
  264. m_keys[m_num_states] = m_render_key.encode();
  265. m_num_states++;
  266. m_render_key.clear();
  267. m_state.clear();
  268. }
  269. void clear()
  270. {
  271. m_flags = STATE_NONE;
  272. m_render_key.clear();
  273. m_num_states = 0;
  274. m_state.clear();
  275. }
  276. void push()
  277. {
  278. m_commands.commit();
  279. m_constants.commit();
  280. }
  281. void sort()
  282. {
  283. std::sort(m_keys, m_keys + m_num_states);
  284. }
  285. public:
  286. uint64_t m_flags;
  287. RenderKey m_render_key;
  288. RenderState m_state;
  289. // Per-state data
  290. uint32_t m_num_states;
  291. RenderState m_states[MAX_RENDER_STATES];
  292. uint64_t m_keys[MAX_RENDER_STATES];
  293. // Per-layer data
  294. RenderTargetId m_targets[MAX_RENDER_LAYERS];
  295. Matrix4x4 m_view_matrices[MAX_RENDER_LAYERS];
  296. Matrix4x4 m_projection_matrices[MAX_RENDER_LAYERS];
  297. ViewRect m_viewports[MAX_RENDER_LAYERS];
  298. ViewRect m_scissors[MAX_RENDER_LAYERS];
  299. ClearState m_clears[MAX_RENDER_LAYERS];
  300. CommandBuffer m_commands;
  301. ConstantBuffer m_constants;
  302. };
  303. } // namespace crown