ConstantBuffer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "Assert.h"
  25. #include "RendererTypes.h"
  26. namespace crown
  27. {
  28. extern const size_t UNIFORM_SIZE_TABLE[UNIFORM_END];
  29. #define MAX_CONSTANT_BUFFER_SIZE 1024 * 1024
  30. class ConstantBuffer
  31. {
  32. public:
  33. ConstantBuffer()
  34. : m_size(0)
  35. {
  36. commit();
  37. }
  38. void clear()
  39. {
  40. m_size = 0;
  41. }
  42. void write(uint32_t data)
  43. {
  44. write(&data, sizeof(uint32_t));
  45. }
  46. uint32_t read()
  47. {
  48. uint32_t data;
  49. read(&data, sizeof(uint32_t));
  50. return data;
  51. }
  52. const void* read(size_t size)
  53. {
  54. CE_ASSERT(m_size + size < MAX_CONSTANT_BUFFER_SIZE, "Constant buffer overflow");
  55. const void* data = (void*) &m_buffer[m_size];
  56. m_size += size;
  57. return data;
  58. }
  59. void write(const void* data, size_t size)
  60. {
  61. CE_ASSERT(m_size + size < MAX_CONSTANT_BUFFER_SIZE, "Constant buffer overflow");
  62. memcpy(&m_buffer[m_size], data, size);
  63. m_size += size;
  64. }
  65. void read(void* data, size_t size)
  66. {
  67. CE_ASSERT(m_size + size < MAX_CONSTANT_BUFFER_SIZE, "Constant buffer overflow");
  68. memcpy(data, &m_buffer[m_size], size);
  69. m_size += size;
  70. }
  71. void write_constant(UniformId id, UniformType type, void* data, uint8_t num)
  72. {
  73. size_t size = UNIFORM_SIZE_TABLE[type] * num;
  74. write(type);
  75. write(&id, sizeof(UniformId));
  76. write(&size, sizeof(size_t));
  77. write(data, size);
  78. }
  79. void commit()
  80. {
  81. write(UNIFORM_END);
  82. m_size = 0;
  83. }
  84. private:
  85. size_t m_size;
  86. char m_buffer[MAX_CONSTANT_BUFFER_SIZE];
  87. };
  88. } // namespace crown