blob: 2f20b689221456ff77a56eb89519d833cef71f2a [file] [log] [blame]
keunyoungb85b2752013-03-08 12:28:03 -08001/*
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
Luca Stefani1cb647a2019-03-07 21:58:17 +010019#if PLATFORM_SDK_VERSION < 26
Lingfeng Yang4fa970a2018-09-24 07:53:40 -070020#include <cutils/log.h>
Luca Stefani1cb647a2019-03-07 21:58:17 +010021#else
22#include <log/log.h>
23#endif
Lingfeng Yang4fa970a2018-09-24 07:53:40 -070024
keunyoungb85b2752013-03-08 12:28:03 -080025#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
52extern "C" {
53#endif
54
Lingfeng Yang6b437bf2017-01-09 13:23:42 -080055typedef enum {
56 INDIRECT_COMMAND_DRAWARRAYS = 0,
57 INDIRECT_COMMAND_DRAWELEMENTS = 1,
58} IndirectCommandType;
59
keunyoungb85b2752013-03-08 12:28:03 -080060 size_t glSizeof(GLenum type);
61 size_t glUtilsParamSize(GLenum param);
62 void glUtilsPackPointerData(unsigned char *dst, unsigned char *str,
63 int size, GLenum type, unsigned int stride,
64 unsigned int datalen);
65 void glUtilsWritePackPointerData(void* stream, unsigned char *src,
66 int size, GLenum type, unsigned int stride,
67 unsigned int datalen);
68 int glUtilsPixelBitSize(GLenum format, GLenum type);
69 void glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count);
70 int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count);
Lingfeng Yang6b437bf2017-01-09 13:23:42 -080071 GLenum glUtilsColorAttachmentName(int i);
72 int glUtilsColorAttachmentIndex(GLenum attachment);
73
74 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType);
75
keunyoungb85b2752013-03-08 12:28:03 -080076#ifdef __cplusplus
77};
78#endif
79
80namespace GLUtils {
81
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070082 template <class T> void minmax(const T *indices, int count, int *min, int *max) {
keunyoungb85b2752013-03-08 12:28:03 -080083 *min = -1;
84 *max = -1;
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070085 const T *ptr = indices;
keunyoungb85b2752013-03-08 12:28:03 -080086 for (int i = 0; i < count; i++) {
87 if (*min == -1 || *ptr < *min) *min = *ptr;
88 if (*max == -1 || *ptr > *max) *max = *ptr;
89 ptr++;
90 }
91 }
92
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070093 template <class T> void minmaxExcept
94 (const T *indices, int count, int *min, int *max,
95 bool shouldExclude, T whatExclude) {
96
97 if (!shouldExclude) return minmax(indices, count, min, max);
98
99 *min = -1;
100 *max = -1;
101 const T *ptr = indices;
102 for (int i = 0; i < count; i++) {
103 if (*ptr != whatExclude) {
104 if (*min == -1 || *ptr < *min) *min = *ptr;
105 if (*max == -1 || *ptr > *max) *max = *ptr;
106 }
107 ptr++;
108 }
109 }
110
keunyoungb85b2752013-03-08 12:28:03 -0800111 template <class T> void shiftIndices(T *indices, int count, int offset) {
112 T *ptr = indices;
113 for (int i = 0; i < count; i++) {
114 *ptr += offset;
115 ptr++;
116 }
117 }
118
119
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -0700120 template <class T> void shiftIndices(const T *src, T *dst, int count, int offset)
keunyoungb85b2752013-03-08 12:28:03 -0800121 {
122 for (int i = 0; i < count; i++) {
123 *dst = *src + offset;
124 dst++;
125 src++;
126 }
127 }
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -0700128
129 template <class T> void shiftIndicesExcept
130 (T *indices, int count, int offset,
131 bool shouldExclude, T whatExclude) {
132
133 if (!shouldExclude) return shiftIndices(indices, count, offset);
134
135 T *ptr = indices;
136 for (int i = 0; i < count; i++) {
137 if (*ptr != whatExclude) {
138 *ptr += offset;
139 }
140 ptr++;
141 }
142 }
143
144 template <class T> void shiftIndicesExcept
145 (const T *src, T *dst, int count, int offset,
146 bool shouldExclude, T whatExclude) {
147
148 if (!shouldExclude) return shiftIndices(src, dst, count, offset);
149
150 for (int i = 0; i < count; i++) {
151 if (*src == whatExclude) {
152 *dst = *src;
153 } else {
154 *dst = *src + offset;
155 }
156 dst++;
157 src++;
158 }
159 }
Lingfeng Yang6b437bf2017-01-09 13:23:42 -0800160
161 template<class T> T primitiveRestartIndex() {
162 return -1;
163 }
164
keunyoungb85b2752013-03-08 12:28:03 -0800165}; // namespace GLUtils
166#endif