/opengles/src/ContextBuffer.cpp

http://ftk.googlecode.com/ · C++ · 228 lines · 133 code · 55 blank · 40 comment · 38 complexity · 902d75e2f0c448a3e00bbbc4bd0daee1 MD5 · raw file

  1. // ==========================================================================
  2. //
  3. // ContextBuffer.cpp Rendering Context Class for 3D Rendering Library
  4. //
  5. // Buffer Functions
  6. //
  7. // --------------------------------------------------------------------------
  8. //
  9. // 08-30-2004 Hans-Martin Will initial version
  10. //
  11. // --------------------------------------------------------------------------
  12. //
  13. // Copyright (c) 2004, Hans-Martin Will. All rights reserved.
  14. //
  15. // Redistribution and use in source and binary forms, with or without
  16. // modification, are permitted provided that the following conditions are
  17. // met:
  18. //
  19. // * Redistributions of source code must retain the above copyright
  20. // notice, this list of conditions and the following disclaimer.
  21. // * Redistributions in binary form must reproduce the above copyright
  22. // notice, this list of conditions and the following disclaimer in the
  23. // documentation and/or other materials provided with the distribution.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  30. // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  35. // THE POSSIBILITY OF SUCH DAMAGE.
  36. //
  37. // ==========================================================================
  38. #include "stdafx.h"
  39. #include "Context.h"
  40. #include "Color.h"
  41. #include "Surface.h"
  42. #include "fixed.h"
  43. using namespace EGL;
  44. // --------------------------------------------------------------------------
  45. // Allocation and selection of buffer objects
  46. // --------------------------------------------------------------------------
  47. size_t * Context :: CurrentBufferForTarget(GLenum target) {
  48. switch (target) {
  49. case GL_ARRAY_BUFFER:
  50. return &m_CurrentArrayBuffer;
  51. case GL_ELEMENT_ARRAY_BUFFER:
  52. return &m_CurrentElementArrayBuffer;
  53. default:
  54. RecordError(GL_INVALID_ENUM);
  55. return 0;
  56. }
  57. }
  58. GLboolean Context :: IsBuffer(GLuint buffer) {
  59. return m_Buffers.IsObject(buffer);
  60. }
  61. void Context :: BindBuffer(GLenum target, GLuint index) {
  62. size_t * currentBuffer = CurrentBufferForTarget(target);
  63. if (!currentBuffer) {
  64. return;
  65. }
  66. if (!index) {
  67. *currentBuffer = 0;
  68. } else {
  69. if (m_Buffers.GetObject(index)) {
  70. *currentBuffer = index;
  71. } else {
  72. RecordError(GL_OUT_OF_MEMORY);
  73. }
  74. }
  75. }
  76. void Context :: DeleteBuffers(GLsizei n, const GLuint *buffers) {
  77. if (n < 0) {
  78. RecordError(GL_INVALID_VALUE);
  79. return;
  80. }
  81. while (n-- != 0) {
  82. U32 buffer = *buffers++;
  83. if (buffer != 0) {
  84. if (buffer == m_CurrentArrayBuffer) {
  85. m_CurrentArrayBuffer = 0;
  86. }
  87. if (buffer == m_CurrentElementArrayBuffer) {
  88. m_CurrentElementArrayBuffer = 0;
  89. }
  90. if (buffer != 0) {
  91. m_Buffers.Deallocate(buffer);
  92. }
  93. }
  94. }
  95. }
  96. void Context :: GenBuffers(GLsizei n, GLuint *buffers) {
  97. if (n < 0) {
  98. RecordError(GL_INVALID_VALUE);
  99. return;
  100. }
  101. while (n != 0) {
  102. *buffers++ = m_Buffers.Allocate();
  103. --n;
  104. }
  105. }
  106. void Context :: BufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) {
  107. size_t * currentBuffer = CurrentBufferForTarget(target);
  108. if (!currentBuffer) {
  109. return;
  110. }
  111. BufferUsage bufferUsage;
  112. switch (usage) {
  113. case GL_STATIC_DRAW:
  114. bufferUsage = BufferUsageStaticDraw;
  115. break;
  116. case GL_DYNAMIC_DRAW:
  117. bufferUsage = BufferUsageDynamicDraw;
  118. break;
  119. default:
  120. RecordError(GL_INVALID_VALUE);
  121. return;
  122. }
  123. if (size < 0) {
  124. RecordError(GL_INVALID_VALUE);
  125. return;
  126. }
  127. if (*currentBuffer == 0 || !m_Buffers.IsObject(*currentBuffer)) {
  128. RecordError(GL_INVALID_OPERATION);
  129. }
  130. Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
  131. if (buffer->Allocate(size, bufferUsage)) {
  132. memcpy(buffer->GetData(), data, size);
  133. } else {
  134. RecordError(GL_OUT_OF_MEMORY);
  135. }
  136. }
  137. void Context :: BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data) {
  138. size_t * currentBuffer = CurrentBufferForTarget(target);
  139. if (!currentBuffer) {
  140. return;
  141. }
  142. if (*currentBuffer == 0 || !m_Buffers.IsObject(*currentBuffer)) {
  143. RecordError(GL_INVALID_OPERATION);
  144. }
  145. Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
  146. if (size < 0 || offset < 0 || static_cast<size_t>(size + offset) > buffer->GetSize()) {
  147. RecordError(GL_INVALID_VALUE);
  148. return;
  149. }
  150. U8 * bufferData = static_cast<U8 *>(buffer->GetData()) + offset;
  151. memcpy(bufferData, data, size);
  152. }
  153. void Context :: GetBufferParameteriv(GLenum target, GLenum pname, GLint *params) {
  154. size_t * currentBuffer = CurrentBufferForTarget(target);
  155. if (!currentBuffer) {
  156. return;
  157. }
  158. Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
  159. switch (pname) {
  160. case GL_BUFFER_SIZE:
  161. params[0] = buffer->GetSize();
  162. break;
  163. case GL_BUFFER_USAGE:
  164. params[0] = (buffer->GetUsage() == BufferUsageStaticDraw) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW;
  165. break;
  166. case GL_BUFFER_ACCESS:
  167. params[0] = GL_WRITE_ONLY;
  168. break;
  169. default:
  170. RecordError(GL_INVALID_ENUM);
  171. return;
  172. }
  173. }