hostsupport/hostopengles11/src/GLESArray.cpp
branchbug235_bringup_0
changeset 53 c2ef9095503a
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 /or associated documentation files
       
     5  * (the "Materials "), to deal in the Materials without restriction,
       
     6  * including without limitation the rights to use, copy, modify, merge,
       
     7  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
     8  * and to permit persons to whom the Materials are furnished to do so,
       
     9  * subject to the following conditions:
       
    10  *
       
    11  * The above copyright notice and this permission notice shall be included
       
    12  * in all copies or substantial portions of the Materials.
       
    13  *
       
    14  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    20  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    21  *
       
    22  * Initial Contributors:
       
    23  * Nokia Corporation - initial contribution.
       
    24  *
       
    25  * Contributors:
       
    26  *
       
    27  * Description:
       
    28  *
       
    29  */
       
    30 
       
    31 #include "GLESArray.h"
       
    32 #include "glesInternal.h"
       
    33 #include "GLESBuffer.h"
       
    34 #include "GLESContext.h"
       
    35 
       
    36 GLESArrayPointer::GLESArrayPointer() :
       
    37     m_pointer(NULL),
       
    38     m_stride(0),
       
    39     m_size(4),
       
    40     m_type(GL_FLOAT)
       
    41 {
       
    42 }
       
    43 
       
    44 GLESArrayPointer::GLESArrayPointer(const void* pointer, int stride, int size, GLenum type) :
       
    45     m_pointer(static_cast<const char*>(pointer)),
       
    46     m_stride(stride),
       
    47     m_size(size),
       
    48     m_type(type)
       
    49 {
       
    50 }
       
    51 
       
    52 GLESArray::GLESArray() :
       
    53     m_size(4),
       
    54     m_type(GL_FLOAT),
       
    55     m_stride(0),
       
    56     m_pointer(NULL),
       
    57     m_ownsData(false),
       
    58     m_buffer(NULL)
       
    59 {
       
    60 }
       
    61 
       
    62 GLESArray::GLESArray(int size, GLenum type, int stride, void* pointer, const GLESBuffer* buffer, bool ownsData) :
       
    63     m_size(size),
       
    64     m_type(type),
       
    65     m_stride(stride),
       
    66     m_pointer(pointer),
       
    67     m_ownsData(ownsData),
       
    68     m_buffer(buffer)
       
    69 {
       
    70 }
       
    71 
       
    72 GLESArray::~GLESArray()
       
    73 {
       
    74     if(m_ownsData)
       
    75     {
       
    76         delete[] m_pointer;
       
    77     }
       
    78 }
       
    79 
       
    80 GLESArrayPointer GLESArray::ArrayPointer() const
       
    81 {
       
    82 	int stride;
       
    83 	if(m_stride)
       
    84 	{
       
    85 		stride = m_stride;
       
    86 	}
       
    87 	else
       
    88 	{
       
    89 		switch(m_type)
       
    90 		{
       
    91 		case GL_BYTE:
       
    92 			stride = sizeof(GLbyte);
       
    93 			break;
       
    94 		case GL_SHORT:
       
    95 			stride = sizeof(GLshort);
       
    96 			break;
       
    97 		case GL_FLOAT:
       
    98 			stride = sizeof(GLfloat);
       
    99 			break;
       
   100 		case GL_UNSIGNED_BYTE:
       
   101 			stride = sizeof(GLubyte);
       
   102 			break;
       
   103 		default:
       
   104 			GLES_ASSERT(false);
       
   105 		}
       
   106 	}
       
   107 	return GLESArrayPointer(m_pointer, m_size * stride, m_size, m_type);
       
   108 }
       
   109 
       
   110 GLESArray* GLESArray::Convert(int count) const
       
   111 {
       
   112 	if(m_type == GL_BYTE)
       
   113 	{
       
   114 		const char* oldPtr = NULL;
       
   115 		unsigned int size = m_size;
       
   116 		if(m_buffer)
       
   117 		{
       
   118 			oldPtr = static_cast<const char*>(m_buffer->data);
       
   119 			size = GLES_MIN(m_buffer->size / sizeof(GLbyte), count * size);
       
   120 		}
       
   121 		else
       
   122 		{
       
   123 			oldPtr = static_cast<const char*>(m_pointer);
       
   124 		}
       
   125 
       
   126 		GLshort* newArr = GLES_NEW GLshort[count * m_size];
       
   127 		if(newArr == NULL)
       
   128 		{
       
   129 			return NULL;
       
   130 		}
       
   131 		int stride = m_stride ? m_stride : sizeof(GLbyte);
       
   132 		for(unsigned int i = 0; i < count * size; i++)
       
   133 		{
       
   134 			newArr[i] = *reinterpret_cast<const GLbyte*>(oldPtr);
       
   135 			oldPtr += stride;
       
   136 		}
       
   137 		return GLES_NEW GLESArray(size, GL_SHORT, 0, newArr, NULL, true);
       
   138 	}
       
   139 	else if(m_type == GL_FIXED)
       
   140 	{
       
   141 		const char* oldPtr = NULL;
       
   142 		unsigned int size = m_size;
       
   143 		if(m_buffer)
       
   144 		{
       
   145 			oldPtr = static_cast<const char*>(m_buffer->data);
       
   146 			size = GLES_MIN(m_buffer->size / sizeof(GLfixed), count * size);
       
   147 		}
       
   148 		else
       
   149 		{
       
   150 			oldPtr = static_cast<const char*>(m_pointer);
       
   151 		}
       
   152 
       
   153 		GLfloat* newArr = GLES_NEW GLfloat[count * m_size];
       
   154 		if(newArr == NULL)
       
   155 		{
       
   156 			return NULL;
       
   157 		}
       
   158 		int stride = m_stride ? m_stride : sizeof(GLfixed);
       
   159 		for(unsigned int i = 0; i < count * size; i++)
       
   160 		{
       
   161 			newArr[i] = X_TO_F(*reinterpret_cast<const GLfixed*>(oldPtr));
       
   162 			oldPtr += stride;
       
   163 		}
       
   164 		return GLES_NEW GLESArray(size, GL_FLOAT, 0, newArr, NULL, true);
       
   165 	}
       
   166 	else
       
   167 	{
       
   168 		return GLES_NEW GLESArray(m_size, m_type, m_stride, m_buffer ? m_buffer->data : m_pointer, NULL);
       
   169 	}
       
   170 
       
   171     return NULL;
       
   172 }