RenderContext.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 "Mat4.h"
  26. #include "IdTable.h"
  27. #include "CommandBuffer.h"
  28. #include "ConstantBuffer.h"
  29. #include "RendererTypes.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 CLEAR_COLOR 0x1
  53. #define CLEAR_DEPTH 0x2
  54. // Texture flags
  55. #define TEXTURE_FILTER_NEAREST 0x00000001
  56. #define TEXTURE_FILTER_LINEAR 0x00000002
  57. #define TEXTURE_FILTER_BILINEAR 0x00000003
  58. #define TEXTURE_FILTER_TRILINEAR 0x00000004
  59. #define TEXTURE_FILTER_MASK 0x0000000F
  60. #define TEXTURE_FILTER_SHIFT 0
  61. #define TEXTURE_WRAP_CLAMP_EDGE 0x00000010
  62. #define TEXTURE_WRAP_CLAMP_REPEAT 0x00000020
  63. #define TEXTURE_WRAP_MASK 0x000000F0
  64. #define TEXTURE_WRAP_SHIFT 4
  65. // Sampler flags
  66. #define SAMPLER_TEXTURE 0x10000000
  67. #define SAMPLER_MASK 0xF0000000
  68. #define SAMPLER_SHIFT 28
  69. struct ViewRect
  70. {
  71. void clear()
  72. {
  73. m_x = 0;
  74. m_y = 0;
  75. m_width = 0;
  76. m_height = 0;
  77. }
  78. uint32_t area() const
  79. {
  80. return (m_width - m_x) * (m_height - m_y);
  81. }
  82. uint16_t m_x;
  83. uint16_t m_y;
  84. uint16_t m_width;
  85. uint16_t m_height;
  86. };
  87. struct ClearState
  88. {
  89. void clear()
  90. {
  91. m_color = Color4::GRAY;
  92. m_depth = 1.0f;
  93. }
  94. public:
  95. uint8_t m_flags;
  96. Color4 m_color;
  97. float m_depth;
  98. };
  99. struct Sampler
  100. {
  101. Id sampler_id;
  102. uint32_t flags;
  103. };
  104. struct RenderState
  105. {
  106. void clear()
  107. {
  108. m_flags = STATE_NONE;
  109. pose = Mat4::IDENTITY;
  110. program.id = INVALID_ID;
  111. vb.id = INVALID_ID;
  112. ib.id = INVALID_ID;
  113. for (uint32_t i = 0; i < STATE_MAX_TEXTURES; i++)
  114. {
  115. samplers[i].sampler_id.id = INVALID_ID;
  116. samplers[i].flags = SAMPLER_TEXTURE;
  117. }
  118. }
  119. public:
  120. uint64_t m_flags;
  121. Mat4 pose;
  122. GPUProgramId program;
  123. VertexBufferId vb;
  124. IndexBufferId ib;
  125. Sampler samplers[STATE_MAX_TEXTURES];
  126. };
  127. struct RenderKey
  128. {
  129. void clear()
  130. {
  131. m_layer = 0;
  132. }
  133. uint64_t encode()
  134. {
  135. return uint64_t(m_layer) << 56;
  136. }
  137. void decode(uint64_t key)
  138. {
  139. m_layer = (key >> 56) & 0xFF;
  140. }
  141. public:
  142. uint8_t m_layer;
  143. };
  144. struct RenderContext
  145. {
  146. RenderContext()
  147. {
  148. clear();
  149. }
  150. void set_state(uint64_t flags)
  151. {
  152. m_state.m_flags = flags;
  153. }
  154. void set_pose(const Mat4& pose)
  155. {
  156. m_state.pose = pose;
  157. }
  158. void set_program(GPUProgramId program)
  159. {
  160. m_state.program = program;
  161. }
  162. void set_vertex_buffer(VertexBufferId vb)
  163. {
  164. m_state.vb = vb;
  165. }
  166. void set_index_buffer(IndexBufferId ib)
  167. {
  168. m_state.ib = ib;
  169. }
  170. void set_uniform(UniformId id, UniformType type, void* value, uint8_t num)
  171. {
  172. m_constants.write_constant(id, type, value, num);
  173. }
  174. void set_texture(uint8_t unit, UniformId sampler_uniform, TextureId texture, uint32_t flags)
  175. {
  176. m_flags |= STATE_TEXTURE_0 << unit;
  177. Sampler& sampler = m_state.samplers[unit];
  178. sampler.sampler_id = texture;
  179. sampler.flags |= SAMPLER_TEXTURE | flags;
  180. set_uniform(sampler_uniform, UNIFORM_INTEGER_1, &unit, 1);
  181. }
  182. void set_layer_render_target(uint8_t layer, RenderTargetId target)
  183. {
  184. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  185. m_targets[layer] = target;
  186. }
  187. void set_layer_clear(uint8_t layer, uint8_t flags, const Color4& color, float depth)
  188. {
  189. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  190. m_clears[layer].m_flags = flags;
  191. m_clears[layer].m_color = color;
  192. m_clears[layer].m_depth = depth;
  193. }
  194. void set_layer_view(uint8_t layer, const Mat4& view)
  195. {
  196. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  197. m_view_matrices[layer] = view;
  198. }
  199. void set_layer_projection(uint8_t layer, const Mat4& projection)
  200. {
  201. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  202. m_projection_matrices[layer] = projection;
  203. }
  204. void set_layer_viewport(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  205. {
  206. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  207. m_viewports[layer].m_x = x;
  208. m_viewports[layer].m_y = y;
  209. m_viewports[layer].m_width = width;
  210. m_viewports[layer].m_height = height;
  211. }
  212. void set_layer_scissor(uint8_t layer, uint16_t x, uint16_t y, uint16_t width, uint16_t height)
  213. {
  214. CE_ASSERT(layer < MAX_RENDER_LAYERS, "Layer out of bounds");
  215. m_scissors[layer].m_x = x;
  216. m_scissors[layer].m_y = y;
  217. m_scissors[layer].m_width = width;
  218. m_scissors[layer].m_height = height;
  219. }
  220. void commit(uint8_t layer)
  221. {
  222. m_render_key.m_layer = layer;
  223. m_states[m_num_states] = m_state;
  224. m_keys[m_num_states] = m_render_key.encode();
  225. m_num_states++;
  226. m_render_key.clear();
  227. m_state.clear();
  228. }
  229. void clear()
  230. {
  231. m_flags = STATE_NONE;
  232. m_render_key.clear();
  233. m_num_states = 0;
  234. m_state.clear();
  235. }
  236. void push()
  237. {
  238. m_commands.commit();
  239. m_constants.commit();
  240. }
  241. public:
  242. uint64_t m_flags;
  243. RenderKey m_render_key;
  244. RenderState m_state;
  245. // Per-state data
  246. uint32_t m_num_states;
  247. RenderState m_states[MAX_RENDER_STATES];
  248. ClearState m_clears[MAX_RENDER_STATES];
  249. uint64_t m_keys[MAX_RENDER_STATES];
  250. // Per-layer data
  251. RenderTargetId m_targets[MAX_RENDER_LAYERS];
  252. Mat4 m_view_matrices[MAX_RENDER_LAYERS];
  253. Mat4 m_projection_matrices[MAX_RENDER_LAYERS];
  254. ViewRect m_viewports[MAX_RENDER_LAYERS];
  255. ViewRect m_scissors[MAX_RENDER_LAYERS];
  256. CommandBuffer m_commands;
  257. ConstantBuffer m_constants;
  258. };
  259. } // namespace crown