PageRenderTime 465ms CodeModel.GetById 211ms app.highlight 17ms RepoModel.GetById 235ms app.codeStats 0ms

/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
 39
 40#include "stdafx.h"
 41#include "Context.h"
 42#include "Color.h"
 43#include "Surface.h"
 44#include "fixed.h"
 45
 46
 47using namespace EGL;
 48
 49
 50// --------------------------------------------------------------------------
 51// Allocation and selection of buffer objects
 52// --------------------------------------------------------------------------
 53
 54
 55size_t * Context :: CurrentBufferForTarget(GLenum target) {
 56	switch (target) {
 57	case GL_ARRAY_BUFFER:
 58		return &m_CurrentArrayBuffer;
 59
 60	case GL_ELEMENT_ARRAY_BUFFER:
 61		return &m_CurrentElementArrayBuffer;
 62
 63	default:
 64		RecordError(GL_INVALID_ENUM);
 65		return 0;
 66	}
 67}
 68
 69
 70GLboolean Context :: IsBuffer(GLuint buffer) {
 71	return m_Buffers.IsObject(buffer);
 72}
 73
 74void Context :: BindBuffer(GLenum target, GLuint index) {
 75
 76	size_t * currentBuffer = CurrentBufferForTarget(target);
 77
 78	if (!currentBuffer) {
 79		return;
 80	}
 81
 82	if (!index) {
 83		*currentBuffer = 0;
 84	} else {
 85		if (m_Buffers.GetObject(index)) {
 86			*currentBuffer = index;
 87		} else {
 88			RecordError(GL_OUT_OF_MEMORY);
 89		}
 90	}
 91}
 92
 93void Context :: DeleteBuffers(GLsizei n, const GLuint *buffers) {
 94
 95	if (n < 0) {
 96		RecordError(GL_INVALID_VALUE);
 97		return;
 98	}
 99
100	while (n-- != 0) {
101		U32 buffer = *buffers++;
102
103		if (buffer != 0) {
104			
105			if (buffer == m_CurrentArrayBuffer) {
106				m_CurrentArrayBuffer = 0;
107			}
108
109			if (buffer == m_CurrentElementArrayBuffer) {
110				m_CurrentElementArrayBuffer = 0;
111			}
112
113			if (buffer != 0) {
114				m_Buffers.Deallocate(buffer);
115			}
116		}
117	}
118}
119
120void Context :: GenBuffers(GLsizei n, GLuint *buffers) {
121
122	if (n < 0) {
123		RecordError(GL_INVALID_VALUE);
124		return;
125	}
126
127	while (n != 0) {
128		*buffers++ = m_Buffers.Allocate();
129		--n;
130	}
131}
132
133void Context :: BufferData(GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage) {
134
135	size_t * currentBuffer = CurrentBufferForTarget(target);
136
137	if (!currentBuffer) {
138		return;
139	}
140
141	BufferUsage bufferUsage;
142
143	switch (usage) {
144	case GL_STATIC_DRAW:
145		bufferUsage = BufferUsageStaticDraw;
146		break;
147
148	case GL_DYNAMIC_DRAW:
149		bufferUsage = BufferUsageDynamicDraw;
150		break;
151
152	default:
153		RecordError(GL_INVALID_VALUE);
154		return;
155	}
156
157	if (size < 0) {
158		RecordError(GL_INVALID_VALUE);
159		return;
160	}
161
162	if (*currentBuffer == 0 || !m_Buffers.IsObject(*currentBuffer)) {
163		RecordError(GL_INVALID_OPERATION);
164	}
165
166	Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
167
168	if (buffer->Allocate(size, bufferUsage)) {
169		memcpy(buffer->GetData(), data, size);
170	} else {
171		RecordError(GL_OUT_OF_MEMORY);
172	}
173}
174
175void Context :: BufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const GLvoid *data) {
176
177	size_t * currentBuffer = CurrentBufferForTarget(target);
178
179	if (!currentBuffer) {
180		return;
181	}
182
183	if (*currentBuffer == 0 || !m_Buffers.IsObject(*currentBuffer)) {
184		RecordError(GL_INVALID_OPERATION);
185	}
186
187	Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
188
189	if (size < 0 || offset < 0 || static_cast<size_t>(size + offset) > buffer->GetSize()) {
190		RecordError(GL_INVALID_VALUE);
191		return;
192	}
193
194	U8 * bufferData = static_cast<U8 *>(buffer->GetData()) + offset;
195
196	memcpy(bufferData, data, size);
197}
198
199void Context :: GetBufferParameteriv(GLenum target, GLenum pname, GLint *params) {
200
201	size_t * currentBuffer = CurrentBufferForTarget(target);
202
203	if (!currentBuffer) {
204		return;
205	}
206
207	Buffer * buffer = m_Buffers.GetObject(*currentBuffer);
208
209	switch (pname) {
210	case GL_BUFFER_SIZE:
211		params[0] = buffer->GetSize();
212		break;
213
214	case GL_BUFFER_USAGE:
215		params[0] = (buffer->GetUsage() == BufferUsageStaticDraw) ? GL_STATIC_DRAW : GL_DYNAMIC_DRAW;
216		break;
217
218	case GL_BUFFER_ACCESS:
219		params[0] = GL_WRITE_ONLY;
220		break;
221
222	default:
223		RecordError(GL_INVALID_ENUM);
224		return;
225	}
226}
227
228