Implemented proper buffer reference retaining for complicated functions like glVertexAttribPointer and glTexCoordPointer. Patch by MatthiasM.

This commit is contained in:
Elias Naur 2007-05-22 22:30:21 +00:00
parent 6768532934
commit 59a3176ace
11 changed files with 54 additions and 13 deletions

View File

@ -31,18 +31,41 @@
*/
package org.lwjgl.opengl;
import java.nio.Buffer;
import java.nio.IntBuffer;
import java.util.Arrays;
class BaseReferences {
int elementArrayBuffer;
int arrayBuffer;
Buffer[] glVertexAttribPointer_buffer;
Buffer[] glTexCoordPointer_buffer;
int glClientActiveTexture;
BaseReferences(ContextCapabilities caps) {
IntBuffer temp = caps.scratch_int_buffer;
GL11.glGetInteger(ARBVertexShader.GL_MAX_VERTEX_ATTRIBS_ARB, temp);
glVertexAttribPointer_buffer = new Buffer[temp.get(0)];
GL11.glGetInteger(GL13.GL_MAX_TEXTURE_UNITS, temp);
glTexCoordPointer_buffer = new Buffer[temp.get(0)];
}
void clear() {
this.elementArrayBuffer = 0;
this.arrayBuffer = 0;
this.glClientActiveTexture = 0;
Arrays.fill(glVertexAttribPointer_buffer, null);
Arrays.fill(glTexCoordPointer_buffer, null);
}
void copy(BaseReferences references) {
this.elementArrayBuffer = references.elementArrayBuffer;
this.arrayBuffer = references.arrayBuffer;
this.glClientActiveTexture = references.glClientActiveTexture;
System.arraycopy(references.glVertexAttribPointer_buffer, 0, glVertexAttribPointer_buffer, 0, glVertexAttribPointer_buffer.length);
System.arraycopy(references.glTexCoordPointer_buffer, 0, glTexCoordPointer_buffer, 0, glTexCoordPointer_buffer.length);
}
}

View File

@ -57,13 +57,14 @@ class ReferencesStack {
References[] new_references_stack = new References[references_stack.length + 1];
System.arraycopy(references_stack, 0, new_references_stack, 0, references_stack.length);
references_stack = new_references_stack;
references_stack[references_stack.length - 1] = new References();
references_stack[references_stack.length - 1] = new References(GLContext.getCapabilities());
}
ReferencesStack() {
ContextCapabilities caps = GLContext.getCapabilities();
references_stack = new References[1];
stack_pos = 0;
for (int i = 0; i < references_stack.length; i++)
references_stack[i] = new References();
references_stack[i] = new References(caps);
}
}

View File

@ -46,4 +46,9 @@ import java.lang.annotation.ElementType;
@Target(ElementType.PARAMETER)
public @interface CachedReference {
/** If set then this will be used as array index for accessing the stored reference. */
String index() default "";
/** If set then this name will be used for the reference and the reference field will not be auto generated in References. */
String name() default "";
}

View File

@ -401,13 +401,21 @@ public class JavaMethodsGenerator {
private static void printParameterCaching(PrintWriter writer, InterfaceDeclaration interface_decl, MethodDeclaration method, Mode mode) {
for (ParameterDeclaration param : method.getParameters()) {
Class java_type = Utils.getJavaType(param.getType());
CachedReference cachedReference = param.getAnnotation(CachedReference.class);
if (Buffer.class.isAssignableFrom(java_type) &&
param.getAnnotation(CachedReference.class) != null &&
cachedReference != null &&
(mode != Mode.BUFFEROBJECT || param.getAnnotation(BufferObject.class) == null) &&
param.getAnnotation(Result.class) == null) {
writer.print("\t\t" + Utils.CHECKS_CLASS_NAME + ".getReferences(caps).");
writer.print(Utils.getReferenceName(interface_decl, method, param) + " = ");
writer.println(param.getSimpleName() + ";");
if(cachedReference.name().length() > 0) {
writer.print(cachedReference.name());
} else {
writer.print(Utils.getReferenceName(interface_decl, method, param));
}
if(cachedReference.index().length() > 0) {
writer.print("[" + cachedReference.index() + "]");
}
writer.println(" = " + param.getSimpleName() + ";");
}
}
}

View File

@ -108,7 +108,7 @@ public class ReferencesGeneratorProcessorFactory implements AnnotationProcessorF
private static void generateClearsFromParameters(PrintWriter writer, InterfaceDeclaration interface_decl, MethodDeclaration method) {
for (ParameterDeclaration param : method.getParameters()) {
CachedReference cached_reference_annotation = param.getAnnotation(CachedReference.class);
if (cached_reference_annotation != null) {
if (cached_reference_annotation != null && cached_reference_annotation.name().length() == 0) {
Class nio_type = Utils.getNIOBufferType(param.getType());
String reference_name = Utils.getReferenceName(interface_decl, method, param);
writer.println("\t\tthis." + reference_name + " = null;");
@ -119,7 +119,7 @@ public class ReferencesGeneratorProcessorFactory implements AnnotationProcessorF
private static void generateCopiesFromParameters(PrintWriter writer, InterfaceDeclaration interface_decl, MethodDeclaration method) {
for (ParameterDeclaration param : method.getParameters()) {
CachedReference cached_reference_annotation = param.getAnnotation(CachedReference.class);
if (cached_reference_annotation != null) {
if (cached_reference_annotation != null && cached_reference_annotation.name().length() == 0) {
Class nio_type = Utils.getNIOBufferType(param.getType());
String reference_name = Utils.getReferenceName(interface_decl, method, param);
writer.print("\t\tthis." + reference_name + " = ");
@ -143,7 +143,7 @@ public class ReferencesGeneratorProcessorFactory implements AnnotationProcessorF
private static void generateReferencesFromParameters(PrintWriter writer, InterfaceDeclaration interface_decl, MethodDeclaration method) {
for (ParameterDeclaration param : method.getParameters()) {
CachedReference cached_reference_annotation = param.getAnnotation(CachedReference.class);
if (cached_reference_annotation != null) {
if (cached_reference_annotation != null && cached_reference_annotation.name().length() == 0) {
Class nio_type = Utils.getNIOBufferType(param.getType());
if (nio_type == null)
throw new RuntimeException(param + " in method " + method + " in " + interface_decl + " is annotated with "
@ -167,6 +167,9 @@ public class ReferencesGeneratorProcessorFactory implements AnnotationProcessorF
writer.println("package org.lwjgl.opengl;");
writer.println();
writer.println("class " + REFERENCES_CLASS_NAME + " extends BaseReferences {");
writer.println("\t" + REFERENCES_CLASS_NAME + "(ContextCapabilities caps) {");
writer.println("\t\tsuper(caps);");
writer.println("\t}");
DeclarationFilter filter = DeclarationFilter.getFilter(InterfaceDeclaration.class);
Collection<TypeDeclaration> interface_decls = filter.filter(env.getSpecifiedTypeDeclarations());
for (TypeDeclaration typedecl : interface_decls) {

View File

@ -112,7 +112,7 @@ public interface ARB_vertex_program extends ARB_program {
void glVertexAttrib4NubARB(@GLuint int index, @GLubyte byte x, @GLubyte byte y, @GLubyte byte z, @GLubyte byte w);
void glVertexAttribPointerARB(@GLuint int index, int size, @AutoType("buffer") @GLenum int type, boolean normalized, @GLsizei int stride,
@CachedReference
@CachedReference(index="index",name="glVertexAttribPointer_buffer")
@BufferObject(BufferKind.ArrayVBO)
@Check
@Const

View File

@ -122,7 +122,7 @@ public interface ARB_vertex_shader {
void glVertexAttrib4NubARB(@GLuint int index, @GLubyte byte x, @GLubyte byte y, @GLubyte byte z, @GLubyte byte w);
void glVertexAttribPointerARB(@GLuint int index, int size, @AutoType("buffer") @GLenum int type, boolean normalized, @GLsizei int stride,
@CachedReference
@CachedReference(index="index",name="glVertexAttribPointer_buffer")
@BufferObject(BufferKind.ArrayVBO)
@Check
@Const

View File

@ -1336,7 +1336,7 @@ public interface GL11 {
void glTexEnviv(@GLenum int target, @GLenum int pname, @Check("4") @Const IntBuffer params);
void glTexCoordPointer(int size, @AutoType("pointer") @GLenum int type, @GLsizei int stride,
@CachedReference
@CachedReference(index="GLChecks.getReferences(caps).glClientActiveTexture", name="glTexCoordPointer_buffer")
@BufferObject(BufferKind.ArrayVBO)
@Check
@Const

View File

@ -148,6 +148,7 @@ public interface GL13 {
void glActiveTexture(@GLenum int texture);
@Code("\t\tGLChecks.getReferences(caps).glClientActiveTexture = texture - GL_TEXTURE0;")
void glClientActiveTexture(@GLenum int texture);
void glCompressedTexImage1D(@GLenum int target, int level, @GLenum int internalformat, @GLsizei int width, int border, @AutoSize("data") @GLsizei int imageSize,

View File

@ -278,7 +278,7 @@ public interface GL20 {
void glVertexAttrib4Nub(@GLuint int index, @GLubyte byte x, @GLubyte byte y, @GLubyte byte z, @GLubyte byte w);
void glVertexAttribPointer(@GLuint int index, int size, @AutoType("buffer") @GLenum int type, boolean normalized, @GLsizei int stride,
@CachedReference
@CachedReference(index="index",name="glVertexAttribPointer_buffer")
@BufferObject(BufferKind.ArrayVBO)
@Check
@Const

View File

@ -228,7 +228,7 @@ public interface NV_vertex_program extends NV_program {
void glTrackMatrixNV(@GLenum int target, @GLuint int address, @GLenum int matrix, @GLenum int transform);
void glVertexAttribPointerNV(@GLuint int index, int size, @GLenum int type, @GLsizei int stride,
@CachedReference
@CachedReference(index="index",name="glVertexAttribPointer_buffer")
@BufferObject(BufferKind.ArrayVBO)
@Check
@Const