GLRenderer.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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. #include "Config.h"
  24. #if defined(LINUX) || defined(WINDOWS)
  25. #include <GL/glew.h>
  26. #elif defined(ANDROID)
  27. #include <GLES2/gl2.h>
  28. #else
  29. #error "Oops, wrong platform"
  30. #endif
  31. #include <algorithm>
  32. #include "Allocator.h"
  33. #include "Assert.h"
  34. #include "Types.h"
  35. #include "GLRenderer.h"
  36. #include "Log.h"
  37. #include "Vector4.h"
  38. #include "Matrix3x3.h"
  39. #include "Matrix4x4.h"
  40. #include "Device.h"
  41. #include "Hash.h"
  42. #include "StringUtils.h"
  43. namespace crown
  44. {
  45. //-----------------------------------------------------------------------------
  46. const GLenum PRIMITIVE_TYPE_TABLE[] =
  47. {
  48. GL_TRIANGLES,
  49. GL_POINTS,
  50. GL_LINES
  51. };
  52. //-----------------------------------------------------------------------------
  53. const GLenum TEXTURE_MIN_FILTER_TABLE[] =
  54. {
  55. 0, // Unused
  56. GL_NEAREST,
  57. GL_LINEAR,
  58. GL_NEAREST_MIPMAP_LINEAR,
  59. GL_LINEAR_MIPMAP_LINEAR
  60. };
  61. //-----------------------------------------------------------------------------
  62. const GLenum TEXTURE_MAG_FILTER_TABLE[] =
  63. {
  64. 0, // Unused
  65. GL_NEAREST,
  66. GL_LINEAR,
  67. GL_LINEAR,
  68. GL_LINEAR
  69. };
  70. //-----------------------------------------------------------------------------
  71. const GLenum TEXTURE_WRAP_TABLE[] =
  72. {
  73. 0, // Unused
  74. GL_CLAMP_TO_EDGE,
  75. GL_REPEAT
  76. };
  77. //-----------------------------------------------------------------------------
  78. const GLTextureFormatInfo TEXTURE_FORMAT_TABLE[PixelFormat::COUNT] =
  79. {
  80. { GL_RGB, GL_RGB },
  81. { GL_RGBA, GL_RGBA}
  82. };
  83. //-----------------------------------------------------------------------------
  84. const GLenum DEPTH_FUNCTION_TABLE[] =
  85. {
  86. 0, // Unused
  87. GL_NEVER,
  88. GL_LESS,
  89. GL_EQUAL,
  90. GL_LEQUAL,
  91. GL_GREATER,
  92. GL_NOTEQUAL,
  93. GL_GEQUAL,
  94. GL_ALWAYS
  95. };
  96. //-----------------------------------------------------------------------------
  97. const GLenum BLEND_FUNCTION_TABLE[] =
  98. {
  99. 0, // Unused
  100. GL_ZERO,
  101. GL_ONE,
  102. GL_SRC_COLOR,
  103. GL_ONE_MINUS_SRC_COLOR,
  104. GL_DST_COLOR,
  105. GL_ONE_MINUS_DST_COLOR,
  106. GL_SRC_ALPHA,
  107. GL_ONE_MINUS_SRC_ALPHA,
  108. GL_DST_ALPHA,
  109. GL_ONE_MINUS_DST_ALPHA
  110. };
  111. //-----------------------------------------------------------------------------
  112. const GLenum BLEND_EQUATION_TABLE[] =
  113. {
  114. 0, // Unused
  115. GL_FUNC_ADD,
  116. GL_FUNC_SUBTRACT,
  117. GL_FUNC_REVERSE_SUBTRACT
  118. };
  119. // Keep in sync with ShaderAttrib
  120. const char* const SHADER_ATTRIB_NAMES[ShaderAttrib::COUNT] =
  121. {
  122. "a_position",
  123. "a_normal",
  124. "a_color",
  125. "a_tex_coord0",
  126. "a_tex_coord1",
  127. "a_tex_coord2",
  128. "a_tex_coord3"
  129. };
  130. const char* const SHADER_UNIFORM_NAMES[ShaderUniform::COUNT] =
  131. {
  132. "u_view",
  133. "u_model",
  134. "u_model_view",
  135. "u_model_view_projection",
  136. "u_time_since_start"
  137. };
  138. const size_t UNIFORM_SIZE_TABLE[UniformType::END] =
  139. {
  140. sizeof(int32_t) * 1,
  141. sizeof(int32_t) * 2,
  142. sizeof(int32_t) * 3,
  143. sizeof(int32_t) * 4,
  144. sizeof(float) * 1,
  145. sizeof(float) * 2,
  146. sizeof(float) * 3,
  147. sizeof(float) * 4,
  148. sizeof(float) * 9,
  149. sizeof(float) * 16
  150. };
  151. ShaderUniform::Enum name_to_stock_uniform(const char* uniform)
  152. {
  153. for (uint8_t i = 0; i < ShaderUniform::COUNT; i++)
  154. {
  155. if (string::strcmp(uniform, SHADER_UNIFORM_NAMES[i]) == 0)
  156. {
  157. return (ShaderUniform::Enum) i;
  158. }
  159. }
  160. return ShaderUniform::COUNT;
  161. }
  162. /// OpenGL renderer
  163. class RendererImplementation
  164. {
  165. public:
  166. //-----------------------------------------------------------------------------
  167. RendererImplementation()
  168. : m_max_texture_size(0)
  169. , m_max_texture_units(0)
  170. , m_max_vertex_indices(0)
  171. , m_max_vertex_vertices(0)
  172. , m_max_anisotropy(0.0f)
  173. , m_num_uniforms(0)
  174. {
  175. m_min_max_point_size[0] = 0.0f;
  176. m_min_max_point_size[1] = 0.0f;
  177. m_min_max_line_width[0] = 0.0f;
  178. m_min_max_line_width[1] = 0.0f;
  179. }
  180. //-----------------------------------------------------------------------------
  181. ~RendererImplementation()
  182. {
  183. }
  184. //-----------------------------------------------------------------------------
  185. void init()
  186. {
  187. m_gl_context.create_context();
  188. #if defined(LINUX) || defined(WINDOWS)
  189. GLenum err = glewInit();
  190. CE_ASSERT(err == GLEW_OK, "Failed to initialize GLEW");
  191. #endif
  192. GL_CHECK(glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_max_texture_size));
  193. GL_CHECK(glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &m_max_texture_units));
  194. // GL_CHECK(glGetIntegerv(GL_MAX_ELEMENTS_INDICES, &m_max_vertex_indices));
  195. // GL_CHECK(glGetIntegerv(GL_MAX_ELEMENTS_VERTICES, &m_max_vertex_vertices));
  196. GL_CHECK(glGetFloatv(GL_ALIASED_POINT_SIZE_RANGE, &m_min_max_point_size[0]));
  197. // GL_CHECK(glGetFloatv(GL_LINE_WIDTH_RANGE, &m_min_max_line_width[0]));
  198. Log::i("OpenGL Vendor : %s", glGetString(GL_VENDOR));
  199. Log::i("OpenGL Renderer : %s", glGetString(GL_RENDERER));
  200. Log::i("OpenGL Version : %s", glGetString(GL_VERSION));
  201. Log::i("GLSL Version : %s", glGetString(GL_SHADING_LANGUAGE_VERSION));
  202. Log::d("Min Point Size : %f", m_min_max_point_size[0]);
  203. Log::d("Max Point Size : %f", m_min_max_point_size[1]);
  204. Log::d("Min Line Width : %f", m_min_max_line_width[0]);
  205. Log::d("Max Line Width : %f", m_min_max_line_width[1]);
  206. Log::d("Max Texture Size : %dx%d", m_max_texture_size, m_max_texture_size);
  207. Log::d("Max Texture Units : %d", m_max_texture_units);
  208. Log::d("Max Vertex Indices : %d", m_max_vertex_indices);
  209. Log::d("Max Vertex Vertices : %d", m_max_vertex_vertices);
  210. #if defined(LINUX) || defined(WINDOWS)
  211. // Point sprites enabled by default
  212. GL_CHECK(glEnable(GL_POINT_SPRITE));
  213. GL_CHECK(glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE));
  214. #endif
  215. Log::i("OpenGL Renderer initialized.");
  216. }
  217. //-----------------------------------------------------------------------------
  218. void shutdown()
  219. {
  220. m_gl_context.destroy_context();
  221. }
  222. //-----------------------------------------------------------------------------
  223. void render(RenderContext& context)
  224. {
  225. //RenderTargetId old_rt;
  226. //old_rt.id = INVALID_ID;
  227. uint8_t layer = 0xFF;
  228. // Sort render keys
  229. std::sort(context.m_keys, context.m_keys + context.m_num_states);
  230. for (uint32_t s = 0; s < context.m_num_states; s++)
  231. {
  232. const uint64_t key_s = context.m_keys[s];
  233. RenderKey key;
  234. key.decode(key_s);
  235. const RenderState& cur_state = context.m_states[s];
  236. const uint64_t flags = cur_state.m_flags;
  237. //const RenderTargetId& cur_rt = context.m_targets[layer];
  238. // Check if layer changed
  239. if (key.m_layer != layer)
  240. {
  241. layer = key.m_layer;
  242. // Viewport
  243. const ViewRect& viewport = context.m_viewports[layer];
  244. GL_CHECK(glViewport(viewport.m_x, viewport.m_y, viewport.m_width, viewport.m_height));
  245. // Clear frame/depth buffer
  246. const ClearState& clear = context.m_clears[layer];
  247. if (clear.m_flags & (CLEAR_COLOR | CLEAR_DEPTH))
  248. {
  249. GLbitfield gl_clear = (clear.m_flags & CLEAR_COLOR) ? GL_COLOR_BUFFER_BIT : 0;
  250. gl_clear |= (clear.m_flags & CLEAR_DEPTH) ? GL_DEPTH_BUFFER_BIT : 0;
  251. GL_CHECK(glClearColor(clear.m_color.r, clear.m_color.g, clear.m_color.b, clear.m_color.a));
  252. #if defined(LINUX) || defined(WINDOWS)
  253. GL_CHECK(glClearDepth(clear.m_depth));
  254. #elif defined(ANDROID)
  255. GL_CHECK(glClearDepthf(clear.m_depth));
  256. #endif
  257. GL_CHECK(glClear(gl_clear));
  258. }
  259. }
  260. // Depth test
  261. if (flags & STATE_DEPTH_TEST_MASK)
  262. {
  263. uint32_t depthf = (flags & STATE_DEPTH_TEST_MASK) >> STATE_DEPTH_TEST_SHIFT;
  264. GL_CHECK(glEnable(GL_DEPTH_TEST));
  265. GL_CHECK(glDepthFunc(DEPTH_FUNCTION_TABLE[depthf]));
  266. }
  267. else
  268. {
  269. GL_CHECK(glDisable(GL_DEPTH_TEST));
  270. }
  271. // Scissor
  272. const ViewRect& scissor = context.m_scissors[layer];
  273. if (scissor.area() != 0)
  274. {
  275. GL_CHECK(glEnable(GL_SCISSOR_TEST));
  276. GL_CHECK(glScissor(scissor.m_x, scissor.m_y, scissor.m_width, scissor.m_height));
  277. }
  278. else
  279. {
  280. GL_CHECK(glDisable(GL_SCISSOR_TEST));
  281. }
  282. // Depth write
  283. if (flags & (STATE_DEPTH_WRITE))
  284. {
  285. GL_CHECK(glDepthMask(flags & STATE_DEPTH_WRITE));
  286. }
  287. // Color/Alpha write
  288. if (flags & (STATE_COLOR_WRITE | STATE_ALPHA_WRITE))
  289. {
  290. GLboolean cw = !!(flags & STATE_COLOR_WRITE);
  291. GLboolean aw = !!(flags & STATE_ALPHA_WRITE);
  292. GL_CHECK(glColorMask(cw, cw, cw, aw));
  293. }
  294. // Face culling
  295. if (flags & STATE_CULL_CW)
  296. {
  297. GL_CHECK(glEnable(GL_CULL_FACE));
  298. GL_CHECK(glCullFace(GL_BACK));
  299. }
  300. else if (flags & STATE_CULL_CCW)
  301. {
  302. GL_CHECK(glEnable(GL_CULL_FACE));
  303. GL_CHECK(glCullFace(GL_FRONT));
  304. }
  305. else
  306. {
  307. GL_CHECK(glDisable(GL_CULL_FACE));
  308. }
  309. // Blending
  310. if (flags & (STATE_BLEND_FUNC_MASK | STATE_BLEND_EQUATION_MASK))
  311. {
  312. uint32_t function = (flags & STATE_BLEND_FUNC_MASK) >> STATE_BLEND_FUNC_SHIFT;
  313. uint32_t equation = (flags & STATE_BLEND_EQUATION_MASK) >> STATE_BLEND_EQUATION_SHIFT;
  314. uint32_t src = (function >> 4) & 0xF;
  315. uint32_t dst = function & 0xF;
  316. GL_CHECK(glEnable(GL_BLEND));
  317. GL_CHECK(glBlendFunc(BLEND_FUNCTION_TABLE[src], BLEND_FUNCTION_TABLE[dst]));
  318. GL_CHECK(glBlendEquation(BLEND_EQUATION_TABLE[equation]));
  319. }
  320. else
  321. {
  322. GL_CHECK(glDisable(GL_BLEND));
  323. }
  324. // Bind textures
  325. {
  326. uint64_t flags = STATE_TEXTURE_0;
  327. for (uint32_t unit = 0; unit < STATE_MAX_TEXTURES; unit++)
  328. {
  329. const Sampler& sampler = cur_state.samplers[unit];
  330. if (sampler.sampler_id.id != INVALID_ID)
  331. {
  332. switch (sampler.flags & SAMPLER_MASK)
  333. {
  334. case SAMPLER_TEXTURE:
  335. {
  336. Texture& texture = m_textures[sampler.sampler_id.index];
  337. texture.commit(unit, sampler.flags);
  338. break;
  339. }
  340. default:
  341. {
  342. CE_ASSERT(false, "Oops, sampler unknown");
  343. break;
  344. }
  345. }
  346. }
  347. flags <<= 1;
  348. }
  349. }
  350. // Bind GPU program
  351. if (cur_state.program.id != INVALID_ID)
  352. {
  353. const GPUProgram& gpu_program = m_gpu_programs[cur_state.program.index];
  354. GL_CHECK(glUseProgram(gpu_program.m_id));
  355. // Not necessarily here...
  356. gpu_program.commit();
  357. for (uint8_t uniform = 0; uniform < gpu_program.m_num_stock_uniforms; uniform++)
  358. {
  359. const GLint& uniform_location = gpu_program.m_stock_uniform_locations[uniform];
  360. const Matrix4x4& view = context.m_view_matrices[layer];
  361. const Matrix4x4& projection = context.m_projection_matrices[layer];
  362. switch (gpu_program.m_stock_uniforms[uniform])
  363. {
  364. case ShaderUniform::VIEW:
  365. {
  366. GL_CHECK(glUniformMatrix4fv(uniform_location, 1, GL_FALSE, view.to_float_ptr()));
  367. break;
  368. }
  369. case ShaderUniform::MODEL:
  370. {
  371. GL_CHECK(glUniformMatrix4fv(uniform_location, 1, GL_FALSE, cur_state.pose.to_float_ptr()));
  372. break;
  373. }
  374. case ShaderUniform::MODEL_VIEW:
  375. {
  376. GL_CHECK(glUniformMatrix4fv(uniform_location, 1, GL_FALSE, (view *
  377. cur_state.pose).to_float_ptr()));
  378. break;
  379. }
  380. case ShaderUniform::MODEL_VIEW_PROJECTION:
  381. {
  382. GL_CHECK(glUniformMatrix4fv(uniform_location, 1, GL_FALSE, (projection * view *
  383. cur_state.pose).to_float_ptr()));
  384. break;
  385. }
  386. case ShaderUniform::TIME_SINCE_START:
  387. {
  388. GL_CHECK(glUniform1f(uniform_location, device()->time_since_start()));
  389. break;
  390. }
  391. default:
  392. {
  393. CE_ASSERT(false, "Oops, wrong stock uniform!");
  394. break;
  395. }
  396. }
  397. }
  398. }
  399. else
  400. {
  401. GL_CHECK(glUseProgram(0));
  402. }
  403. // Bind array buffers
  404. const VertexBufferId& vb = cur_state.vb;
  405. if (vb.id != INVALID_ID)
  406. {
  407. const VertexBuffer& vertex_buffer = m_vertex_buffers[vb.index];
  408. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer.m_id));
  409. const GPUProgram& gpu_program = m_gpu_programs[cur_state.program.index];
  410. gpu_program.bind_attributes(vertex_buffer.m_format);
  411. }
  412. else
  413. {
  414. GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
  415. }
  416. const IndexBufferId& ib = cur_state.ib;
  417. if (ib.id != INVALID_ID)
  418. {
  419. const IndexBuffer& index_buffer = m_index_buffers[ib.index];
  420. uint32_t prim_type = (flags & STATE_PRIMITIVE_MASK) >> STATE_PRIMITIVE_SHIFT;
  421. GLenum gl_prim_type = PRIMITIVE_TYPE_TABLE[prim_type];
  422. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer.m_id));
  423. GL_CHECK(glDrawElements(gl_prim_type, index_buffer.m_index_count, GL_UNSIGNED_SHORT, 0));
  424. }
  425. else
  426. {
  427. GL_CHECK(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0));
  428. }
  429. }
  430. GL_CHECK(glFinish());
  431. m_gl_context.swap_buffers();
  432. }
  433. private:
  434. GLContext m_gl_context;
  435. // Limits
  436. int32_t m_max_texture_size;
  437. int32_t m_max_texture_units;
  438. int32_t m_max_vertex_indices;
  439. int32_t m_max_vertex_vertices;
  440. float m_max_anisotropy;
  441. float m_min_max_point_size[2];
  442. float m_min_max_line_width[2];
  443. VertexBuffer m_vertex_buffers[CROWN_MAX_VERTEX_BUFFERS];
  444. IndexBuffer m_index_buffers[CROWN_MAX_INDEX_BUFFERS];
  445. Texture m_textures[CROWN_MAX_TEXTURES];
  446. Shader m_shaders[CROWN_MAX_SHADERS];
  447. GPUProgram m_gpu_programs[CROWN_MAX_GPU_PROGRAMS];
  448. uint32_t m_num_uniforms;
  449. Uniform m_uniforms[CROWN_MAX_UNIFORMS];
  450. RenderTarget m_render_targets[CROWN_MAX_RENDER_TARGETS];
  451. private:
  452. friend class Renderer;
  453. };
  454. //-----------------------------------------------------------------------------
  455. Renderer::Renderer(Allocator& a)
  456. : m_allocator(a), m_impl(NULL), m_thread("render-thread"), m_submit(&m_contexts[0]), m_draw(&m_contexts[1]),
  457. m_is_initialized(false), m_should_run(false)
  458. {
  459. m_impl = CE_NEW(a, RendererImplementation);
  460. }
  461. //-----------------------------------------------------------------------------
  462. Renderer::~Renderer()
  463. {
  464. CE_ASSERT(m_vertex_buffers.size() == 0, "%d vertex buffers not freed", m_vertex_buffers.size());
  465. CE_ASSERT(m_index_buffers.size() == 0, "%d index buffers not freed", m_index_buffers.size());
  466. CE_ASSERT(m_textures.size() == 0, "%d textures not freed", m_textures.size());
  467. CE_ASSERT(m_shaders.size() == 0, "%d shaders not freed", m_shaders.size());
  468. CE_ASSERT(m_gpu_programs.size() == 0, "%d GPU programs not freed", m_gpu_programs.size());
  469. CE_ASSERT(m_uniforms.size() == 0, "%d uniforms not freed", m_uniforms.size());
  470. CE_ASSERT(m_render_targets.size() == 0, "%d render targets not freed", m_render_targets.size());
  471. CE_DELETE(m_allocator, m_impl);
  472. }
  473. //-----------------------------------------------------------------------------
  474. void Renderer::init_impl()
  475. {
  476. m_impl->init();
  477. }
  478. //-----------------------------------------------------------------------------
  479. void Renderer::shutdown_impl()
  480. {
  481. m_impl->shutdown();
  482. }
  483. //-----------------------------------------------------------------------------
  484. void Renderer::render_impl()
  485. {
  486. m_impl->render(*m_draw);
  487. m_draw->clear();
  488. }
  489. //-----------------------------------------------------------------------------
  490. void Renderer::create_vertex_buffer_impl(VertexBufferId id, size_t count, VertexFormat::Enum format, const void* vertices)
  491. {
  492. m_impl->m_vertex_buffers[id.index].create(count, format, vertices);
  493. }
  494. //-----------------------------------------------------------------------------
  495. void Renderer::create_dynamic_vertex_buffer_impl(VertexBufferId id, size_t count, VertexFormat::Enum format)
  496. {
  497. m_impl->m_vertex_buffers[id.index].create(count, format, NULL);
  498. }
  499. //-----------------------------------------------------------------------------
  500. void Renderer::update_vertex_buffer_impl(VertexBufferId id, size_t offset, size_t count, const void* vertices)
  501. {
  502. m_impl->m_vertex_buffers[id.index].update(offset, count, vertices);
  503. }
  504. //-----------------------------------------------------------------------------
  505. void Renderer::destroy_vertex_buffer_impl(VertexBufferId id)
  506. {
  507. m_impl->m_vertex_buffers[id.index].destroy();
  508. }
  509. //-----------------------------------------------------------------------------
  510. void Renderer::create_index_buffer_impl(IndexBufferId id, size_t count, const void* indices)
  511. {
  512. m_impl->m_index_buffers[id.index].create(count, indices);
  513. }
  514. //-----------------------------------------------------------------------------
  515. void Renderer::create_dynamic_index_buffer_impl(IndexBufferId id, size_t count)
  516. {
  517. m_impl->m_index_buffers[id.index].create(count, NULL);
  518. }
  519. //-----------------------------------------------------------------------------
  520. void Renderer::update_index_buffer_impl(IndexBufferId id, size_t offset, size_t count, const void* indices)
  521. {
  522. m_impl->m_index_buffers[id.index].update(offset, count, indices);
  523. }
  524. //-----------------------------------------------------------------------------
  525. void Renderer::destroy_index_buffer_impl(IndexBufferId id)
  526. {
  527. m_impl->m_index_buffers[id.index].destroy();
  528. }
  529. //-----------------------------------------------------------------------------
  530. void Renderer::create_texture_impl(TextureId id, uint32_t width, uint32_t height, PixelFormat::Enum format, const void* data)
  531. {
  532. m_impl->m_textures[id.index].create(width, height, format, data);
  533. }
  534. //-----------------------------------------------------------------------------
  535. void Renderer::update_texture_impl(TextureId id, uint32_t x, uint32_t y, uint32_t width, uint32_t height, const void* data)
  536. {
  537. m_impl->m_textures[id.index].update(x, y, width, height, data);
  538. }
  539. //-----------------------------------------------------------------------------
  540. void Renderer::destroy_texture_impl(TextureId id)
  541. {
  542. m_impl->m_textures[id.index].destroy();
  543. }
  544. //-----------------------------------------------------------------------------
  545. void Renderer::create_shader_impl(ShaderId id, ShaderType::Enum type, const char* text)
  546. {
  547. m_impl->m_shaders[id.index].create(type, text);
  548. }
  549. //-----------------------------------------------------------------------------
  550. void Renderer::destroy_shader_impl(ShaderId id)
  551. {
  552. m_impl->m_shaders[id.index].destroy();
  553. }
  554. //-----------------------------------------------------------------------------
  555. void Renderer::create_gpu_program_impl(GPUProgramId id, ShaderId vertex, ShaderId pixel)
  556. {
  557. Shader& vs = m_impl->m_shaders[vertex.index];
  558. Shader& ps = m_impl->m_shaders[pixel.index];
  559. m_impl->m_gpu_programs[id.index].create(vs, ps, m_impl->m_num_uniforms, m_impl->m_uniforms);
  560. }
  561. //-----------------------------------------------------------------------------
  562. void Renderer::destroy_gpu_program_impl(GPUProgramId id)
  563. {
  564. m_impl->m_gpu_programs[id.index].destroy();
  565. }
  566. //-----------------------------------------------------------------------------
  567. void Renderer::create_uniform_impl(UniformId id, const char* name, UniformType::Enum type, uint8_t num)
  568. {
  569. m_impl->m_uniforms[id.index].create(name, type, num);
  570. m_impl->m_num_uniforms++;
  571. }
  572. //-----------------------------------------------------------------------------
  573. void Renderer::update_uniform_impl(UniformId id, size_t size, const void* data)
  574. {
  575. m_impl->m_uniforms[id.index].update(size, data);
  576. }
  577. //-----------------------------------------------------------------------------
  578. void Renderer::destroy_uniform_impl(UniformId id)
  579. {
  580. m_impl->m_uniforms[id.index].destroy();
  581. m_impl->m_num_uniforms--;
  582. }
  583. // //-----------------------------------------------------------------------------
  584. // void Renderer::create_render_target_impl(RenderTargetId id, uint16_t width, uint16_t height, RenderTargetFormat::Enum format)
  585. // {
  586. // }
  587. // //-----------------------------------------------------------------------------
  588. // void Renderer::destroy_render_target_impl(RenderTargetId id)
  589. // {
  590. // }
  591. } // namespace crown