1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #ifndef __GL_UTILS_H__ 17 #define __GL_UTILS_H__ 18 19 #include <cutils/log.h> 20 21 #include <stdio.h> 22 #include <stdlib.h> 23 24 #ifdef GL_API 25 #undef GL_API 26 #endif 27 #define GL_API 28 29 #ifdef GL_APIENTRY 30 #undef GL_APIENTRY 31 #endif 32 33 #ifdef GL_APIENTRYP 34 #undef GL_APIENTRYP 35 #endif 36 #define GL_APIENTRYP 37 38 #ifndef ANDROID 39 #define GL_APIENTRY 40 #endif 41 42 #include <GLES/gl.h> 43 #include <GLES/glext.h> 44 #include <GLES2/gl2.h> 45 #include <GLES2/gl2ext.h> 46 47 #ifdef __cplusplus 48 extern "C" { 49 #endif 50 51 typedef enum { 52 INDIRECT_COMMAND_DRAWARRAYS = 0, 53 INDIRECT_COMMAND_DRAWELEMENTS = 1, 54 } IndirectCommandType; 55 56 bool isSamplerType(GLenum type); 57 bool isIntegerType(GLenum type); 58 bool isUnsignedIntType(GLenum type); 59 bool isBoolType(GLenum type); 60 uint32_t getColumnsOfType(GLenum type); 61 uint32_t getRowsOfType(GLenum type); 62 uint32_t getAttributeCountOfType(GLenum type); 63 size_t glSizeof(GLenum type); 64 size_t glUtilsParamSize(GLenum param); 65 void glUtilsPackPointerData(unsigned char *dst, unsigned char *str, 66 int size, GLenum type, unsigned int stride, 67 unsigned int datalen); 68 void glUtilsWritePackPointerData(void* stream, unsigned char *src, 69 int size, GLenum type, unsigned int stride, 70 unsigned int datalen); 71 int glUtilsPixelBitSize(GLenum format, GLenum type); 72 void glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count); 73 int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count); 74 GLenum glUtilsColorAttachmentName(int i); 75 int glUtilsColorAttachmentIndex(GLenum attachment); 76 77 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType); 78 79 bool colorRenderableFormat(GLint internalformat, GLenum texturetype, int majorVersion, int minorVersion, bool hasColorBufferFloatExtension, bool hasColorBufferHalfFloatExtension); 80 81 bool depthRenderableFormat(GLint internalformat); 82 bool stencilRenderableFormat(GLint internalformat); 83 84 #ifdef __cplusplus 85 } 86 #endif 87 88 namespace GLUtils { 89 minmax(const T * indices,int count,int * min,int * max)90 template <class T> void minmax(const T *indices, int count, int *min, int *max) { 91 *min = -1; 92 *max = -1; 93 const T *ptr = indices; 94 for (int i = 0; i < count; i++) { 95 if (*min == -1 || *ptr < *min) *min = *ptr; 96 if (*max == -1 || *ptr > *max) *max = *ptr; 97 ptr++; 98 } 99 } 100 minmaxExcept(const T * indices,int count,int * min,int * max,bool shouldExclude,T whatExclude)101 template <class T> void minmaxExcept 102 (const T *indices, int count, int *min, int *max, 103 bool shouldExclude, T whatExclude) { 104 105 if (!shouldExclude) return minmax(indices, count, min, max); 106 107 *min = -1; 108 *max = -1; 109 const T *ptr = indices; 110 for (int i = 0; i < count; i++) { 111 if (*ptr != whatExclude) { 112 if (*min == -1 || *ptr < *min) *min = *ptr; 113 if (*max == -1 || *ptr > *max) *max = *ptr; 114 } 115 ptr++; 116 } 117 } 118 shiftIndices(T * indices,int count,int offset)119 template <class T> void shiftIndices(T *indices, int count, int offset) { 120 T *ptr = indices; 121 for (int i = 0; i < count; i++) { 122 *ptr += offset; 123 ptr++; 124 } 125 } 126 127 shiftIndices(const T * src,T * dst,int count,int offset)128 template <class T> void shiftIndices(const T *src, T *dst, int count, int offset) 129 { 130 for (int i = 0; i < count; i++) { 131 *dst = *src + offset; 132 dst++; 133 src++; 134 } 135 } 136 shiftIndicesExcept(T * indices,int count,int offset,bool shouldExclude,T whatExclude)137 template <class T> void shiftIndicesExcept 138 (T *indices, int count, int offset, 139 bool shouldExclude, T whatExclude) { 140 141 if (!shouldExclude) return shiftIndices(indices, count, offset); 142 143 T *ptr = indices; 144 for (int i = 0; i < count; i++) { 145 if (*ptr != whatExclude) { 146 *ptr += offset; 147 } 148 ptr++; 149 } 150 } 151 shiftIndicesExcept(const T * src,T * dst,int count,int offset,bool shouldExclude,T whatExclude)152 template <class T> void shiftIndicesExcept 153 (const T *src, T *dst, int count, int offset, 154 bool shouldExclude, T whatExclude) { 155 156 if (!shouldExclude) return shiftIndices(src, dst, count, offset); 157 158 for (int i = 0; i < count; i++) { 159 if (*src == whatExclude) { 160 *dst = *src; 161 } else { 162 *dst = *src + offset; 163 } 164 dst++; 165 src++; 166 } 167 } 168 primitiveRestartIndex()169 template<class T> T primitiveRestartIndex() { 170 return -1; 171 } 172 173 } // namespace GLUtils 174 #endif 175