blob: 2210699086f3ebcfcc861700c5b0a7538951f260 [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
19#include <stdio.h>
20#include <stdlib.h>
21
22#ifdef GL_API
23 #undef GL_API
24#endif
25#define GL_API
26
27#ifdef GL_APIENTRY
28 #undef GL_APIENTRY
29#endif
30
31#ifdef GL_APIENTRYP
32 #undef GL_APIENTRYP
33#endif
34#define GL_APIENTRYP
35
36#ifndef ANDROID
37#define GL_APIENTRY
38#endif
39
40#include <GLES/gl.h>
41#include <GLES/glext.h>
42#include <GLES2/gl2.h>
43#include <GLES2/gl2ext.h>
44
45#ifdef __cplusplus
46extern "C" {
47#endif
48
Lingfeng Yang6b437bf2017-01-09 13:23:42 -080049typedef enum {
50 INDIRECT_COMMAND_DRAWARRAYS = 0,
51 INDIRECT_COMMAND_DRAWELEMENTS = 1,
52} IndirectCommandType;
53
keunyoungb85b2752013-03-08 12:28:03 -080054 size_t glSizeof(GLenum type);
55 size_t glUtilsParamSize(GLenum param);
56 void glUtilsPackPointerData(unsigned char *dst, unsigned char *str,
57 int size, GLenum type, unsigned int stride,
58 unsigned int datalen);
59 void glUtilsWritePackPointerData(void* stream, unsigned char *src,
60 int size, GLenum type, unsigned int stride,
61 unsigned int datalen);
62 int glUtilsPixelBitSize(GLenum format, GLenum type);
63 void glUtilsPackStrings(char *ptr, char **strings, GLint *length, GLsizei count);
64 int glUtilsCalcShaderSourceLen(char **strings, GLint *length, GLsizei count);
Lingfeng Yang6b437bf2017-01-09 13:23:42 -080065 GLenum glUtilsColorAttachmentName(int i);
66 int glUtilsColorAttachmentIndex(GLenum attachment);
67
68 GLuint glUtilsIndirectStructSize(IndirectCommandType cmdType);
69
keunyoungb85b2752013-03-08 12:28:03 -080070#ifdef __cplusplus
71};
72#endif
73
74namespace GLUtils {
75
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070076 template <class T> void minmax(const T *indices, int count, int *min, int *max) {
keunyoungb85b2752013-03-08 12:28:03 -080077 *min = -1;
78 *max = -1;
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070079 const T *ptr = indices;
keunyoungb85b2752013-03-08 12:28:03 -080080 for (int i = 0; i < count; i++) {
81 if (*min == -1 || *ptr < *min) *min = *ptr;
82 if (*max == -1 || *ptr > *max) *max = *ptr;
83 ptr++;
84 }
85 }
86
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -070087 template <class T> void minmaxExcept
88 (const T *indices, int count, int *min, int *max,
89 bool shouldExclude, T whatExclude) {
90
91 if (!shouldExclude) return minmax(indices, count, min, max);
92
93 *min = -1;
94 *max = -1;
95 const T *ptr = indices;
96 for (int i = 0; i < count; i++) {
97 if (*ptr != whatExclude) {
98 if (*min == -1 || *ptr < *min) *min = *ptr;
99 if (*max == -1 || *ptr > *max) *max = *ptr;
100 }
101 ptr++;
102 }
103 }
104
keunyoungb85b2752013-03-08 12:28:03 -0800105 template <class T> void shiftIndices(T *indices, int count, int offset) {
106 T *ptr = indices;
107 for (int i = 0; i < count; i++) {
108 *ptr += offset;
109 ptr++;
110 }
111 }
112
113
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -0700114 template <class T> void shiftIndices(const T *src, T *dst, int count, int offset)
keunyoungb85b2752013-03-08 12:28:03 -0800115 {
116 for (int i = 0; i < count; i++) {
117 *dst = *src + offset;
118 dst++;
119 src++;
120 }
121 }
Lingfeng Yang8e2b6e02016-10-14 11:20:45 -0700122
123 template <class T> void shiftIndicesExcept
124 (T *indices, int count, int offset,
125 bool shouldExclude, T whatExclude) {
126
127 if (!shouldExclude) return shiftIndices(indices, count, offset);
128
129 T *ptr = indices;
130 for (int i = 0; i < count; i++) {
131 if (*ptr != whatExclude) {
132 *ptr += offset;
133 }
134 ptr++;
135 }
136 }
137
138 template <class T> void shiftIndicesExcept
139 (const T *src, T *dst, int count, int offset,
140 bool shouldExclude, T whatExclude) {
141
142 if (!shouldExclude) return shiftIndices(src, dst, count, offset);
143
144 for (int i = 0; i < count; i++) {
145 if (*src == whatExclude) {
146 *dst = *src;
147 } else {
148 *dst = *src + offset;
149 }
150 dst++;
151 src++;
152 }
153 }
Lingfeng Yang6b437bf2017-01-09 13:23:42 -0800154
155 template<class T> T primitiveRestartIndex() {
156 return -1;
157 }
158
keunyoungb85b2752013-03-08 12:28:03 -0800159}; // namespace GLUtils
160#endif