hostsupport/hostopengles20/src/GLES2/buffer.c
branchbug235_bringup_0
changeset 53 c2ef9095503a
parent 20 d2d6724aef32
equal deleted inserted replaced
52:39e5f73667ba 53:c2ef9095503a
       
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and associated documentation files (the "Software"),
       
     5  * to deal in the Software without restriction, including without limitation
       
     6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
       
     7  * and/or sell copies of the Software, and to permit persons to whom the
       
     8  * Software is furnished to do so, subject to the following conditions:
       
     9  *
       
    10  * The above copyright notice and this permission notice shall be included
       
    11  * in all copies or substantial portions of the Software.
       
    12  *
       
    13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
       
    14  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
       
    16  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
       
    17  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
       
    18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    19  *
       
    20  * Initial Contributors:
       
    21  * Nokia Corporation - initial contribution.
       
    22  *
       
    23  * Contributors:
       
    24  *
       
    25  * Description:
       
    26  *
       
    27  */
       
    28 
       
    29 #include "common.h"
       
    30 #include "hgl.h"
       
    31 #include "context.h"
       
    32 
       
    33 DGLBuffer* DGLBuffer_create(GLuint name)
       
    34 {
       
    35 	DGLBuffer* buffer = malloc(sizeof(DGLBuffer));
       
    36 	if(buffer == NULL)
       
    37 	{
       
    38 		return NULL;
       
    39 	}
       
    40 
       
    41 	buffer->obj.name = name;
       
    42 	buffer->obj.next = NULL;
       
    43 
       
    44 	buffer->data = NULL;
       
    45 	buffer->size = 0;
       
    46 	buffer->usage = GL_STATIC_DRAW;
       
    47 
       
    48 	return buffer;
       
    49 }
       
    50 
       
    51 void DGLBuffer_destroy(DGLBuffer *buffer)
       
    52 {
       
    53 	DGLES2_ASSERT(buffer != NULL);
       
    54 	if(buffer->data != NULL)
       
    55 	{
       
    56 		free(buffer->data);
       
    57 		buffer->data = NULL;
       
    58 	}
       
    59 	free(buffer);
       
    60 }
       
    61 
       
    62 GL_APICALL_BUILD void GL_APIENTRY glBindBuffer(GLenum target, GLuint buffer)
       
    63 {
       
    64 	DGLES2_ENTER();
       
    65 
       
    66 	DGLContext_getHostError(ctx);
       
    67 	
       
    68 	// The buffers are stored in both the wrapper and host.
       
    69 	ctx->hgl.BindBuffer(target, buffer);
       
    70 
       
    71 	if(DGLContext_getHostError(ctx) == GL_NO_ERROR)
       
    72 	{
       
    73 		if(target == GL_ARRAY_BUFFER)
       
    74 		{
       
    75 			if(!DGLContext_bindBuffer(ctx, buffer))
       
    76 			{
       
    77 				DGLES2_ERROR(GL_OUT_OF_MEMORY);
       
    78 			}
       
    79 		}
       
    80 		else
       
    81 		{
       
    82 			DGLES2_ERROR_IF(target != GL_ELEMENT_ARRAY_BUFFER, GL_INVALID_ENUM);
       
    83 		}
       
    84 	}
       
    85 
       
    86 	DGLES2_LEAVE();
       
    87 }
       
    88 
       
    89 GL_APICALL_BUILD void GL_APIENTRY glBufferData(GLenum target, GLsizeiptr size, const void* data, GLenum usage)
       
    90 {
       
    91 	DGLES2_ENTER();
       
    92 	DGLES2_ERROR_IF(size < 0, GL_INVALID_VALUE);
       
    93 	DGLES2_ERROR_IF(usage != GL_STATIC_DRAW && usage != GL_DYNAMIC_DRAW && usage != GL_STREAM_DRAW, GL_INVALID_ENUM);
       
    94 	
       
    95 	DGLContext_getHostError(ctx);
       
    96 
       
    97 	// The buffers are stored in both the wrapper and host.
       
    98 	ctx->hgl.BufferData(target, size, data, usage);
       
    99 
       
   100 	if(DGLContext_getHostError(ctx) == GL_NO_ERROR)
       
   101 	{
       
   102 		if(target == GL_ARRAY_BUFFER)
       
   103 		{
       
   104 			DGLBuffer* buffer;
       
   105 
       
   106 			DGLES2_ERROR_IF(ctx->buffer_binding == 0, GL_INVALID_OPERATION);
       
   107 			
       
   108 			buffer = DGLContext_findBuffer(ctx, ctx->buffer_binding);
       
   109 			DGLES2_ASSERT(buffer != NULL);
       
   110 			
       
   111 			if(buffer->data != NULL)
       
   112 			{
       
   113 				// Delete old data.
       
   114 				free(buffer->data);
       
   115 			}
       
   116 
       
   117 			buffer->data = malloc(size);
       
   118 			if(buffer->data == NULL)
       
   119 			{
       
   120 				DGLES2_ERROR(GL_OUT_OF_MEMORY);
       
   121 			}
       
   122 
       
   123 			if(data != NULL)
       
   124 			{
       
   125 				// Copy new data.
       
   126 				memcpy(buffer->data, data, size);
       
   127 			}
       
   128 
       
   129 			buffer->size = size;
       
   130 			buffer->usage = usage;
       
   131 		}
       
   132 		else
       
   133 		{
       
   134 			DGLES2_ERROR_IF(target != GL_ELEMENT_ARRAY_BUFFER, GL_INVALID_ENUM);
       
   135 		}
       
   136 	}
       
   137 
       
   138 	DGLES2_LEAVE();
       
   139 }
       
   140 
       
   141 GL_APICALL_BUILD void GL_APIENTRY glBufferSubData(GLenum target, GLintptr offset, GLsizeiptr size, const void* data)
       
   142 {
       
   143 	DGLES2_ENTER();
       
   144 	DGLES2_ERROR_IF(offset < 0, GL_INVALID_VALUE);
       
   145 	DGLES2_ERROR_IF(size < 0, GL_INVALID_VALUE);
       
   146 	
       
   147 	DGLContext_getHostError(ctx);
       
   148 
       
   149 	// The buffers are stored in both the wrapper and host.
       
   150 	ctx->hgl.BufferSubData(target, offset, size, data);
       
   151 
       
   152 	if(DGLContext_getHostError(ctx) == GL_NO_ERROR)
       
   153 	{
       
   154 		if(target == GL_ARRAY_BUFFER)
       
   155 		{
       
   156 			DGLBuffer* buffer;
       
   157 
       
   158 			DGLES2_ERROR_IF(target != GL_ARRAY_BUFFER, GL_INVALID_ENUM);
       
   159 			DGLES2_ERROR_IF(ctx->buffer_binding == 0, GL_INVALID_OPERATION);
       
   160 
       
   161 			buffer = DGLContext_findBuffer(ctx, ctx->buffer_binding);
       
   162 			DGLES2_ASSERT(buffer != NULL);
       
   163 
       
   164 			memcpy((char*)buffer->data + offset, data, size);
       
   165 		}
       
   166 		else
       
   167 		{
       
   168 			DGLES2_ERROR_IF(target != GL_ELEMENT_ARRAY_BUFFER, GL_INVALID_ENUM);
       
   169 		}
       
   170 	}
       
   171 
       
   172 	DGLES2_LEAVE();
       
   173 }
       
   174 
       
   175 GL_APICALL_BUILD void GL_APIENTRY glDeleteBuffers(GLsizei n, const GLuint* buffers)
       
   176 {
       
   177 	DGLES2_ENTER();
       
   178 	DGLES2_ERROR_IF(n < 0, GL_INVALID_VALUE);
       
   179 	
       
   180 	DGLContext_getHostError(ctx);
       
   181 
       
   182 	// The buffers are stored in both the wrapper and host.
       
   183 	ctx->hgl.DeleteBuffers(n, buffers);
       
   184 
       
   185 	if(DGLContext_getHostError(ctx) == GL_NO_ERROR)
       
   186 	{
       
   187 		int i;
       
   188 		for(i = 0; i < n; i++)
       
   189 		{
       
   190 			DGLContext_destroyBuffer(ctx, buffers[i]);
       
   191 		}
       
   192 	}
       
   193 
       
   194 	DGLES2_LEAVE();
       
   195 }
       
   196 
       
   197 
       
   198 GL_APICALL_BUILD void GL_APIENTRY glGenBuffers(GLsizei n, GLuint* buffers)
       
   199 {
       
   200 	DGLES2_ENTER();
       
   201 	DGLES2_ERROR_IF(n < 0, GL_INVALID_VALUE);
       
   202 
       
   203 	// The buffers are stored in both the wrapper and host.
       
   204 	// Let the host come up with free buffer names. 
       
   205 	ctx->hgl.GenBuffers(n, buffers);
       
   206 
       
   207 	DGLES2_LEAVE();
       
   208 }
       
   209 
       
   210 GL_APICALL_BUILD void GL_APIENTRY glGetBufferParameteriv(GLenum target, GLenum pname, GLint* params)
       
   211 {
       
   212 	DGLES2_ENTER();
       
   213 	DGLES2_ERROR_IF(pname != GL_BUFFER_USAGE && pname != GL_BUFFER_SIZE, GL_INVALID_ENUM);
       
   214 	ctx->hgl.GetBufferParameteriv(target, pname, params);
       
   215 	DGLES2_LEAVE();
       
   216 }
       
   217 
       
   218 GL_APICALL_BUILD GLboolean GL_APIENTRY glIsBuffer(GLuint buffer)
       
   219 {
       
   220 	DGLES2_ENTER_RET(GL_FALSE);
       
   221 	DGLES2_LEAVE_RET(ctx->hgl.IsBuffer(buffer));
       
   222 }