1 /*
2  * Copyright (c) 2002-2008 LWJGL Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'LWJGL' nor the names of
17  *   its contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package org.lwjgl.util.generator.opengl;
34 
35 /**
36  *
37  * OpenGL sepcific generator behaviour
38  *
39  * @author elias_naur <elias_naur@users.sourceforge.net>
40  * @version $Revision: 3287 $
41  * $Id: GLTypeMap.java 3287 2010-03-14 23:24:40Z spasi $
42  */
43 
44 import org.lwjgl.util.generator.NativeTypeTranslator;
45 import org.lwjgl.util.generator.PointerWrapper;
46 import org.lwjgl.util.generator.Signedness;
47 import org.lwjgl.util.generator.TypeMap;
48 
49 import java.io.PrintWriter;
50 import java.lang.annotation.Annotation;
51 import java.nio.*;
52 import java.util.HashMap;
53 import java.util.Map;
54 import javax.lang.model.element.AnnotationMirror;
55 import javax.lang.model.element.ExecutableElement;
56 import javax.lang.model.type.TypeKind;
57 
58 public class GLESTypeMap implements TypeMap {
59 
60 	private static final Map<Class<? extends Annotation>, TypeKind> native_types_to_primitive;
61 
62 	static {
63 		native_types_to_primitive = new HashMap<Class<? extends Annotation>, TypeKind>();
native_types_to_primitive.put(GLbitfield.class, TypeKind.INT)64 		native_types_to_primitive.put(GLbitfield.class, TypeKind.INT);
native_types_to_primitive.put(GLclampf.class, TypeKind.FLOAT)65 		native_types_to_primitive.put(GLclampf.class, TypeKind.FLOAT);
native_types_to_primitive.put(GLfloat.class, TypeKind.FLOAT)66 		native_types_to_primitive.put(GLfloat.class, TypeKind.FLOAT);
native_types_to_primitive.put(GLint.class, TypeKind.INT)67 		native_types_to_primitive.put(GLint.class, TypeKind.INT);
native_types_to_primitive.put(GLshort.class, TypeKind.SHORT)68 		native_types_to_primitive.put(GLshort.class, TypeKind.SHORT);
native_types_to_primitive.put(GLsizeiptr.class, TypeKind.LONG)69 		native_types_to_primitive.put(GLsizeiptr.class, TypeKind.LONG);
native_types_to_primitive.put(GLuint.class, TypeKind.INT)70 		native_types_to_primitive.put(GLuint.class, TypeKind.INT);
native_types_to_primitive.put(GLboolean.class, TypeKind.BOOLEAN)71 		native_types_to_primitive.put(GLboolean.class, TypeKind.BOOLEAN);
native_types_to_primitive.put(GLchar.class, TypeKind.BYTE)72 		native_types_to_primitive.put(GLchar.class, TypeKind.BYTE);
native_types_to_primitive.put(GLhalf.class, TypeKind.SHORT)73 		native_types_to_primitive.put(GLhalf.class, TypeKind.SHORT);
native_types_to_primitive.put(GLsizei.class, TypeKind.INT)74 		native_types_to_primitive.put(GLsizei.class, TypeKind.INT);
native_types_to_primitive.put(GLushort.class, TypeKind.SHORT)75 		native_types_to_primitive.put(GLushort.class, TypeKind.SHORT);
native_types_to_primitive.put(GLbyte.class, TypeKind.BYTE)76 		native_types_to_primitive.put(GLbyte.class, TypeKind.BYTE);
native_types_to_primitive.put(GLenum.class, TypeKind.INT)77 		native_types_to_primitive.put(GLenum.class, TypeKind.INT);
native_types_to_primitive.put(GLintptr.class, TypeKind.LONG)78 		native_types_to_primitive.put(GLintptr.class, TypeKind.LONG);
native_types_to_primitive.put(GLubyte.class, TypeKind.BYTE)79 		native_types_to_primitive.put(GLubyte.class, TypeKind.BYTE);
native_types_to_primitive.put(GLvoid.class, TypeKind.BYTE)80 		native_types_to_primitive.put(GLvoid.class, TypeKind.BYTE);
native_types_to_primitive.put(EGLint64NV.class, TypeKind.LONG)81 		native_types_to_primitive.put(EGLint64NV.class, TypeKind.LONG);
native_types_to_primitive.put(EGLuint64NV.class, TypeKind.LONG)82 		native_types_to_primitive.put(EGLuint64NV.class, TypeKind.LONG);
native_types_to_primitive.put(GLint64.class, TypeKind.LONG)83 		native_types_to_primitive.put(GLint64.class, TypeKind.LONG);
native_types_to_primitive.put(GLuint64.class, TypeKind.LONG)84 		native_types_to_primitive.put(GLuint64.class, TypeKind.LONG);
85 	}
86 
87 	@Override
getPrimitiveTypeFromNativeType(Class<? extends Annotation> native_type)88 	public TypeKind getPrimitiveTypeFromNativeType(Class<? extends Annotation> native_type) {
89 		TypeKind kind = native_types_to_primitive.get(native_type);
90 		if ( kind == null )
91 			throw new RuntimeException("Unsupported type " + native_type);
92 		return kind;
93 	}
94 
95 	@Override
printCapabilitiesInit(final PrintWriter writer)96 	public void printCapabilitiesInit(final PrintWriter writer) {
97 		writer.println("\t\tContextCapabilities caps = GLContext.getCapabilities();");
98 	}
99 
100 	@Override
getCapabilities()101 	public String getCapabilities() {
102 		return "caps";
103 	}
104 
105 	@Override
getAPIUtilParam(boolean comma)106 	public String getAPIUtilParam(boolean comma) {
107 		return "";
108 	}
109 
110 	@Override
printErrorCheckMethod(final PrintWriter writer, final ExecutableElement method, final String tabs)111 	public void printErrorCheckMethod(final PrintWriter writer, final ExecutableElement method, final String tabs) {
112 		writer.println(tabs + "Util.checkGLError();");
113 	}
114 
115 	@Override
getRegisterNativesFunctionName()116 	public String getRegisterNativesFunctionName() {
117 		return "extgl_InitializeClass";
118 	}
119 
120 	@Override
getSignednessFromType(Class<? extends Annotation> type)121 	public Signedness getSignednessFromType(Class<? extends Annotation> type) {
122 		if ( GLuint.class.equals(type) )
123 			return Signedness.UNSIGNED;
124 		else if ( GLint.class.equals(type) )
125 			return Signedness.SIGNED;
126 		else if ( GLushort.class.equals(type) )
127 			return Signedness.UNSIGNED;
128 		else if ( GLshort.class.equals(type) )
129 			return Signedness.SIGNED;
130 		else if ( GLubyte.class.equals(type) )
131 			return Signedness.UNSIGNED;
132 		else if ( GLbyte.class.equals(type) )
133 			return Signedness.SIGNED;
134 		else if ( EGLuint64NV.class.equals(type) )
135 			return Signedness.UNSIGNED;
136 		else if ( EGLint64NV.class.equals(type) )
137 			return Signedness.SIGNED;
138 		else
139 			return Signedness.NONE;
140 	}
141 
142 	@Override
translateAnnotation(Class annotation_type)143 	public String translateAnnotation(Class annotation_type) {
144 		if ( annotation_type.equals(GLuint64.class) || annotation_type.equals(GLint64.class) )
145 			return "i64";
146 		else if ( annotation_type.equals(GLuint.class) || annotation_type.equals(GLint.class) )
147 			return "i";
148 		else if ( annotation_type.equals(GLushort.class) || annotation_type.equals(GLshort.class) )
149 			return "s";
150 		else if ( annotation_type.equals(GLubyte.class) || annotation_type.equals(GLbyte.class) )
151 			return "b";
152 		else if ( annotation_type.equals(GLfloat.class) )
153 			return "f";
154 		else if ( annotation_type.equals(GLhalf.class) )
155 			return "h";
156 		else if ( annotation_type.equals(GLboolean.class) || annotation_type.equals(GLvoid.class) )
157 			return "";
158 		else if ( annotation_type.equals(EGLuint64NV.class) || annotation_type.equals(EGLint64NV.class) )
159 			return "l";
160 		else
161 			throw new RuntimeException(annotation_type + " is not allowed");
162 	}
163 
164 	@Override
getNativeTypeFromPrimitiveType(TypeKind kind)165 	public Class<? extends Annotation> getNativeTypeFromPrimitiveType(TypeKind kind) {
166 		Class<? extends Annotation> type;
167 		switch ( kind ) {
168 			case INT:
169 				type = GLint.class;
170 				break;
171 			case FLOAT:
172 				type = GLfloat.class;
173 				break;
174 			case SHORT:
175 				type = GLshort.class;
176 				break;
177 			case BYTE:
178 				type = GLbyte.class;
179 				break;
180 			case BOOLEAN:
181 				type = GLboolean.class;
182 				break;
183 			case LONG:
184 				type = GLint64.class;
185 				break;
186 			default:
187 				throw new RuntimeException(kind + " is not allowed");
188 		}
189 		return type;
190 	}
191 
192 	@Override
getVoidType()193 	public Class<? extends Annotation> getVoidType() {
194 		return GLvoid.class;
195 	}
196 
197 	@Override
getStringElementType()198 	public Class<? extends Annotation> getStringElementType() {
199 		return GLubyte.class;
200 	}
201 
202 	@Override
getStringArrayType()203 	public Class<? extends Annotation> getStringArrayType() {
204 		return GLchar.class;
205 	}
206 
207 	@Override
getByteBufferArrayType()208 	public Class<? extends Annotation> getByteBufferArrayType() {
209 		return GLubyte.class;
210 	}
211 
getValidBufferTypes(Class type)212 	private static Class[] getValidBufferTypes(Class type) {
213 		if ( type.equals(IntBuffer.class) )
214 			return new Class[] { GLbitfield.class, GLenum.class, GLint.class, GLsizei.class, GLuint.class, GLvoid.class };
215 		else if ( type.equals(FloatBuffer.class) )
216 			return new Class[] { GLclampf.class, GLfloat.class };
217 		else if ( type.equals(ByteBuffer.class) )
218 			return new Class[] { GLboolean.class, GLbyte.class, GLchar.class, GLubyte.class, GLvoid.class };
219 		else if ( type.equals(ShortBuffer.class) )
220 			return new Class[] { GLhalf.class, GLshort.class, GLushort.class };
221 		else if ( type.equals(LongBuffer.class) )
222 			return new Class[] { GLint64.class, GLuint64.class, EGLint64NV.class, EGLuint64NV.class };
223 		else
224 			return new Class[] { };
225 	}
226 
getValidPrimitiveTypes(Class type)227 	private static Class[] getValidPrimitiveTypes(Class type) {
228 		if ( type.equals(long.class) )
229 			return new Class[] { GLintptr.class, GLsizeiptr.class, GLint64.class, GLuint64.class, EGLuint64NV.class, EGLint64NV.class };
230 		else if ( type.equals(int.class) )
231 			return new Class[] { GLbitfield.class, GLenum.class, GLint.class, GLuint.class, GLsizei.class };
232 		else if ( type.equals(float.class) )
233 			return new Class[] { GLclampf.class, GLfloat.class };
234 		else if ( type.equals(short.class) )
235 			return new Class[] { GLhalf.class, GLshort.class, GLushort.class };
236 		else if ( type.equals(byte.class) )
237 			return new Class[] { GLbyte.class, GLchar.class, GLubyte.class };
238 		else if ( type.equals(boolean.class) )
239 			return new Class[] { GLboolean.class };
240 		else if ( type.equals(void.class) )
241 			return new Class[] { GLvoid.class, GLreturn.class };
242 		else
243 			return new Class[] { };
244 	}
245 
246 	@Override
getTypedefPostfix()247 	public String getTypedefPostfix() {
248 		return "GL_APICALL ";
249 	}
250 
251 	@Override
getFunctionPrefix()252 	public String getFunctionPrefix() {
253 		return "GL_APIENTRY";
254 	}
255 
256 	@Override
printNativeIncludes(PrintWriter writer)257 	public void printNativeIncludes(PrintWriter writer) {
258 		writer.println("#include \"extgl.h\"");
259 	}
260 
261 	@Override
getValidAnnotationTypes(Class type)262 	public Class[] getValidAnnotationTypes(Class type) {
263 		Class[] valid_types;
264 		if ( Buffer.class.isAssignableFrom(type) )
265 			valid_types = getValidBufferTypes(type);
266 		else if ( type.isPrimitive() )
267 			valid_types = getValidPrimitiveTypes(type);
268 		else if ( String.class.equals(type) )
269 			valid_types = new Class[] { GLubyte.class };
270 		else if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(type) )
271 			valid_types = new Class[] { PointerWrapper.class };
272 		else if ( void.class.equals(type) )
273 			valid_types = new Class[] { GLreturn.class };
274 		else
275 			valid_types = new Class[] { };
276 		return valid_types;
277 	}
278 
279 	@Override
getInverseType(Class<? extends Annotation> type)280 	public Class<? extends Annotation> getInverseType(Class<? extends Annotation> type) {
281 		if ( GLuint64.class.equals(type) )
282 			return GLint64.class;
283 		if ( GLuint.class.equals(type) )
284 			return GLint.class;
285 		else if ( GLint.class.equals(type) )
286 			return GLuint.class;
287 		else if ( GLushort.class.equals(type) )
288 			return GLshort.class;
289 		else if ( GLshort.class.equals(type) )
290 			return GLushort.class;
291 		else if ( GLubyte.class.equals(type) )
292 			return GLbyte.class;
293 		else if ( GLbyte.class.equals(type) )
294 			return GLubyte.class;
295 		else
296 			return null;
297 	}
298 
299 	@Override
getAutoTypeFromAnnotation(AnnotationMirror annotation)300 	public String getAutoTypeFromAnnotation(AnnotationMirror annotation) {
301 		Class annotation_class = NativeTypeTranslator.getClassFromType(annotation.getAnnotationType());
302 		if ( annotation_class.equals(GLint.class) )
303 			return "GLES20.GL_INT";
304 		else if ( annotation_class.equals(GLbyte.class) )
305 			return "GLES20.GL_BYTE";
306 		else if ( annotation_class.equals(GLshort.class) )
307 			return "GLES20.GL_SHORT";
308 		if ( annotation_class.equals(GLuint.class) )
309 			return "GLES20.GL_UNSIGNED_INT";
310 		else if ( annotation_class.equals(GLubyte.class) )
311 			return "GLES20.GL_UNSIGNED_BYTE";
312 		else if ( annotation_class.equals(GLushort.class) )
313 			return "GLES20.GL_UNSIGNED_SHORT";
314 		else if ( annotation_class.equals(GLfloat.class) )
315 			return "GLES20.GL_FLOAT";
316 		else
317 			return null;
318 	}
319 }
320