Added support for non-direct in glGet* style functions

This commit is contained in:
Elias Naur 2007-04-23 20:17:42 +00:00
parent 7ee398fe4e
commit b386d2185b
37 changed files with 446 additions and 167 deletions

View file

@ -67,6 +67,36 @@ public final class NondirectBufferWrapper {
return buffers;
}
public static ByteBuffer wrapNoCopyBuffer(ByteBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static ShortBuffer wrapNoCopyBuffer(ShortBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static IntBuffer wrapNoCopyBuffer(IntBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static LongBuffer wrapNoCopyBuffer(LongBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static FloatBuffer wrapNoCopyBuffer(FloatBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static DoubleBuffer wrapNoCopyBuffer(DoubleBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapNoCopyDirect(buf);
}
public static ByteBuffer wrapBuffer(ByteBuffer buf, int size) {
BufferChecks.checkBufferSize(buf, size);
return wrapDirect(buf);
@ -133,8 +163,138 @@ public final class NondirectBufferWrapper {
return buffer;
}
public static ByteBuffer wrapNoCopyDirect(ByteBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static ShortBuffer wrapNoCopyDirect(ShortBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static FloatBuffer wrapNoCopyDirect(FloatBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static IntBuffer wrapNoCopyDirect(IntBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static LongBuffer wrapNoCopyDirect(LongBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static DoubleBuffer wrapNoCopyDirect(DoubleBuffer buffer) {
if (!buffer.isDirect())
return doNoCopyWrap(buffer);
return buffer;
}
public static void copy(ByteBuffer src, ByteBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
public static void copy(ShortBuffer src, ShortBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
public static void copy(IntBuffer src, IntBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
public static void copy(FloatBuffer src, FloatBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
public static void copy(LongBuffer src, LongBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
public static void copy(DoubleBuffer src, DoubleBuffer dst) {
if (dst != null && !dst.isDirect()) {
int saved_position = dst.position();
dst.put(src);
dst.position(saved_position);
}
}
private static ByteBuffer doNoCopyWrap(ByteBuffer buffer) {
ByteBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static ShortBuffer doNoCopyWrap(ShortBuffer buffer) {
ShortBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static IntBuffer doNoCopyWrap(IntBuffer buffer) {
IntBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static FloatBuffer doNoCopyWrap(FloatBuffer buffer) {
FloatBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static LongBuffer doNoCopyWrap(LongBuffer buffer) {
LongBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static DoubleBuffer doNoCopyWrap(DoubleBuffer buffer) {
DoubleBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.limit(buffer.limit());
direct_buffer.position(buffer.position());
return direct_buffer;
}
private static ByteBuffer lookupBuffer(ByteBuffer buffer) {
return getCachedBuffers(buffer.remaining()).byte_buffer;
}
private static ByteBuffer doWrap(ByteBuffer buffer) {
ByteBuffer direct_buffer = getCachedBuffers(buffer.remaining()).byte_buffer;
ByteBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
@ -143,9 +303,13 @@ public final class NondirectBufferWrapper {
return direct_buffer;
}
private static ShortBuffer lookupBuffer(ShortBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*2);
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.short_buffer_little : buffers.short_buffer_big;
}
private static ShortBuffer doWrap(ShortBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*2);
ShortBuffer direct_buffer = buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.short_buffer_little : buffers.short_buffer_big;
ShortBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
@ -154,9 +318,13 @@ public final class NondirectBufferWrapper {
return direct_buffer;
}
private static FloatBuffer lookupBuffer(FloatBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*4);
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.float_buffer_little : buffers.float_buffer_big;
}
private static FloatBuffer doWrap(FloatBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*4);
FloatBuffer direct_buffer = buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.float_buffer_little : buffers.float_buffer_big;
FloatBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
@ -165,9 +333,13 @@ public final class NondirectBufferWrapper {
return direct_buffer;
}
private static IntBuffer lookupBuffer(IntBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*4);
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.int_buffer_little : buffers.int_buffer_big;
}
private static IntBuffer doWrap(IntBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*4);
IntBuffer direct_buffer = buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.int_buffer_little : buffers.int_buffer_big;
IntBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
@ -176,9 +348,13 @@ public final class NondirectBufferWrapper {
return direct_buffer;
}
private static LongBuffer lookupBuffer(LongBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*8);
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.long_buffer_little : buffers.long_buffer_big;
}
private static LongBuffer doWrap(LongBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*8);
LongBuffer direct_buffer = buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.long_buffer_little : buffers.long_buffer_big;
LongBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);
@ -187,9 +363,13 @@ public final class NondirectBufferWrapper {
return direct_buffer;
}
private static DoubleBuffer doWrap(DoubleBuffer buffer) {
private static DoubleBuffer lookupBuffer(DoubleBuffer buffer) {
CachedBuffers buffers = getCachedBuffers(buffer.remaining()*8);
DoubleBuffer direct_buffer = buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.double_buffer_little : buffers.double_buffer_big;
return buffer.order() == ByteOrder.LITTLE_ENDIAN ? buffers.double_buffer_little : buffers.double_buffer_big;
}
private static DoubleBuffer doWrap(DoubleBuffer buffer) {
DoubleBuffer direct_buffer = lookupBuffer(buffer);
direct_buffer.clear();
int saved_position = buffer.position();
direct_buffer.put(buffer);

View file

@ -50,6 +50,8 @@ import java.util.*;
import java.nio.*;
public class JavaMethodsGenerator {
private final static String SAVED_PARAMETER_POSTFIX = "_saved";
public static void generateMethodsJava(AnnotationProcessorEnvironment env, TypeMap type_map, PrintWriter writer, InterfaceDeclaration interface_decl, boolean generate_error_checks, boolean context_specific) {
for (MethodDeclaration method : interface_decl.getMethods())
generateMethodJava(env, type_map, writer, interface_decl, method, generate_error_checks, context_specific);
@ -197,7 +199,7 @@ public class JavaMethodsGenerator {
if (code_annotation != null)
writer.println(code_annotation.value());
printBufferObjectChecks(writer, method, mode);
printParameterChecks(writer, method, mode);
printParameterChecks(writer, method, typeinfos_instance, mode);
printParameterCaching(writer, interface_decl, method, mode);
writer.print("\t\t");
boolean has_result = !result_type.equals(env.getTypeUtils().getVoidType());
@ -218,6 +220,7 @@ public class JavaMethodsGenerator {
writer.println(");");
if (generate_error_checks && method.getAnnotation(NoErrorCheck.class) == null)
writer.println("\t\t" + type_map.getErrorCheckMethodName() + ";");
printNondirectParameterCopies(writer, method, mode);
if (has_result)
writer.println("\t\treturn " + Utils.RESULT_VAR_NAME + ";");
writer.println("\t}");
@ -409,7 +412,23 @@ public class JavaMethodsGenerator {
}
}
private static void printParameterChecks(PrintWriter writer, MethodDeclaration method, Mode mode) {
private static void printNondirectParameterCopies(PrintWriter writer, MethodDeclaration method, Mode mode) {
for (ParameterDeclaration param : method.getParameters()) {
Class java_type = Utils.getJavaType(param.getType());
if (Utils.isAddressableType(java_type) &&
(mode != Mode.BUFFEROBJECT || param.getAnnotation(BufferObject.class) == null) &&
(mode != Mode.AUTOS || getAutoTypeParameter(method, param) == null) &&
param.getAnnotation(Result.class) == null) {
if (Buffer.class.isAssignableFrom(java_type)) {
boolean out_parameter = param.getAnnotation(OutParameter.class) != null;
if (out_parameter)
writer.println("\t\tNondirectBufferWrapper.copy(" + param.getSimpleName() + ", " + param.getSimpleName() + SAVED_PARAMETER_POSTFIX + ");");
}
}
}
}
private static void printParameterChecks(PrintWriter writer, MethodDeclaration method, Map<ParameterDeclaration, TypeInfo> typeinfos, Mode mode) {
for (ParameterDeclaration param : method.getParameters()) {
Class java_type = Utils.getJavaType(param.getType());
if (Utils.isAddressableType(java_type) &&
@ -426,7 +445,9 @@ public class JavaMethodsGenerator {
boolean null_terminated = param.getAnnotation(NullTerminated.class) != null;
if (Buffer.class.isAssignableFrom(java_type)) {
boolean indirect_buffer_allowed = param.getAnnotation(CachedReference.class) == null;
printParameterCheck(writer, param.getSimpleName(), check_value, can_be_null, null_terminated, indirect_buffer_allowed);
boolean out_parameter = param.getAnnotation(OutParameter.class) != null;
TypeInfo typeinfo = typeinfos.get(param);
printParameterCheck(writer, param.getSimpleName(), typeinfo.getType().getSimpleName(), check_value, can_be_null, null_terminated, indirect_buffer_allowed, out_parameter);
} else if (String.class.equals(java_type)) {
if (!can_be_null)
writer.println("\t\tBufferChecks.checkNotNull(" + param.getSimpleName() + ");");
@ -434,17 +455,22 @@ public class JavaMethodsGenerator {
}
}
if (method.getAnnotation(CachedResult.class) != null)
printParameterCheck(writer, Utils.CACHED_BUFFER_NAME, null, true, false, false);
printParameterCheck(writer, Utils.CACHED_BUFFER_NAME, null, null, true, false, false, false);
}
private static void printParameterCheck(PrintWriter writer, String name, String check_value, boolean can_be_null, boolean null_terminated, boolean indirect_buffer_allowed) {
private static void printParameterCheck(PrintWriter writer, String name, String type, String check_value, boolean can_be_null, boolean null_terminated, boolean indirect_buffer_allowed, boolean out_parameter) {
if (indirect_buffer_allowed && out_parameter) {
writer.println("\t\t" + type + " " + name + SAVED_PARAMETER_POSTFIX + " = " + name + ";");
}
if (can_be_null) {
writer.println("\t\tif (" + name + " != null)");
writer.print("\t");
}
if (indirect_buffer_allowed)
if (indirect_buffer_allowed) {
writer.print("\t\t" + name + " = NondirectBufferWrapper.wrap");
else
if (out_parameter)
writer.print("NoCopy");
} else
writer.print("\t\tBufferChecks.check");
if (check_value != null && !"".equals(check_value) ) {
writer.print("Buffer(" + name + ", " + check_value);

View file

@ -0,0 +1,48 @@
/*
* Copyright (c) 2002-2004 LWJGL Project
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.lwjgl.util.generator;
/**
* This annotation indicates that a parameter is written,
* not read.
*
* @author elias_naur <elias_naur@users.sourceforge.net>
* @version $Revision: 2286 $
* $Id: Check.java 2286 2006-03-23 19:32:21Z matzon $
*/
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Target(ElementType.PARAMETER)
public @interface OutParameter {
}