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 #if PLATFORM_SDK_VERSION < 26 20 #include <cutils/log.h> 21 #else 22 #include <log/log.h> 23 #endif 24 25 #include <stdio.h> 26 #include <stdlib.h> 27 28 #ifdef GL_API 29 #undef GL_API 30 #endif 31 #define GL_API 32 33 #ifdef GL_APIENTRY 34 #undef GL_APIENTRY 35 #endif 36 37 #ifdef GL_APIENTRYP 38 #undef GL_APIENTRYP 39 #endif 40 #define GL_APIENTRYP 41 42 #ifndef ANDROID 43 #define GL_APIENTRY 44 #endif 45 46 #include <GLES/gl.h> 47 #include <GLES/glext.h> 48 #include <GLES2/gl2.h> 49 #include <GLES2/gl2ext.h> 50 51 #ifdef __cplusplus 52 extern "C" { 53 #endif 54 55 typedef enum { 56 INDIRECT_COMMAND_DRAWARRAYS = 0, 57 INDIRECT_COMMAND_DRAWELEMENTS = 1, 58 } IndirectCommandType; 59 60 bool isSamplerType(GLenum type); 61 bool isIntegerType(GLenum type); 62 bool isUnsignedIntType(GLenum type); 63 bool isBoolType(GLenum type); 64 uint32_t getColumnsOfType(GLenum type); 65 uint32_t getRowsOfType(GLenum type); 66 uint32_t getAttributeCountOfType(GLenum type); 67 size_t glSizeof(GLenum type); 68 size_t glUtilsParamSize(GLenum param); 69 void glUtilsPackPointerData(unsigned char *dst, unsigned char *str, 70 int size, GLenum type, unsigned int stride, 71 unsigned int datalen); 72 void glUtilsWritePackPointerData(void* stream, unsigned char *src, 73 int size, GLenum type, unsigned int stride, 74 unsigned int datalen); 75 int glUtilsPixelBitSize(GLenum format, GLenum type); 76 void glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count); 77 int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count); 78 GLenum glUtilsColorAttachmentName(int i); 79 int glUtilsColorAttachmentIndex(GLenum attachment); 80 81 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType); 82 83 bool colorRenderableFormat(GLint internalformat, GLenum texturetype, int majorVersion, int minorVersion, bool hasColorBufferFloatExtension, bool hasColorBufferHalfFloatExtension); 84 85 bool depthRenderableFormat(GLint internalformat); 86 bool stencilRenderableFormat(GLint internalformat); 87 88 #ifdef __cplusplus 89 }; 90 #endif 91 92 namespace GLUtils { 93 minmax(const T * indices,int count,int * min,int * max)94 template <class T> void minmax(const T *indices, int count, int *min, int *max) { 95 *min = -1; 96 *max = -1; 97 const T *ptr = indices; 98 for (int i = 0; i < count; i++) { 99 if (*min == -1 || *ptr < *min) *min = *ptr; 100 if (*max == -1 || *ptr > *max) *max = *ptr; 101 ptr++; 102 } 103 } 104 minmaxExcept(const T * indices,int count,int * min,int * max,bool shouldExclude,T whatExclude)105 template <class T> void minmaxExcept 106 (const T *indices, int count, int *min, int *max, 107 bool shouldExclude, T whatExclude) { 108 109 if (!shouldExclude) return minmax(indices, count, min, max); 110 111 *min = -1; 112 *max = -1; 113 const T *ptr = indices; 114 for (int i = 0; i < count; i++) { 115 if (*ptr != whatExclude) { 116 if (*min == -1 || *ptr < *min) *min = *ptr; 117 if (*max == -1 || *ptr > *max) *max = *ptr; 118 } 119 ptr++; 120 } 121 } 122 shiftIndices(T * indices,int count,int offset)123 template <class T> void shiftIndices(T *indices, int count, int offset) { 124 T *ptr = indices; 125 for (int i = 0; i < count; i++) { 126 *ptr += offset; 127 ptr++; 128 } 129 } 130 131 shiftIndices(const T * src,T * dst,int count,int offset)132 template <class T> void shiftIndices(const T *src, T *dst, int count, int offset) 133 { 134 for (int i = 0; i < count; i++) { 135 *dst = *src + offset; 136 dst++; 137 src++; 138 } 139 } 140 shiftIndicesExcept(T * indices,int count,int offset,bool shouldExclude,T whatExclude)141 template <class T> void shiftIndicesExcept 142 (T *indices, int count, int offset, 143 bool shouldExclude, T whatExclude) { 144 145 if (!shouldExclude) return shiftIndices(indices, count, offset); 146 147 T *ptr = indices; 148 for (int i = 0; i < count; i++) { 149 if (*ptr != whatExclude) { 150 *ptr += offset; 151 } 152 ptr++; 153 } 154 } 155 shiftIndicesExcept(const T * src,T * dst,int count,int offset,bool shouldExclude,T whatExclude)156 template <class T> void shiftIndicesExcept 157 (const T *src, T *dst, int count, int offset, 158 bool shouldExclude, T whatExclude) { 159 160 if (!shouldExclude) return shiftIndices(src, dst, count, offset); 161 162 for (int i = 0; i < count; i++) { 163 if (*src == whatExclude) { 164 *dst = *src; 165 } else { 166 *dst = *src + offset; 167 } 168 dst++; 169 src++; 170 } 171 } 172 primitiveRestartIndex()173 template<class T> T primitiveRestartIndex() { 174 return -1; 175 } 176 177 }; // namespace GLUtils 178 #endif 179