Added Charset encoding to StringUtils
Changed shader tests to use the String APIs Finished ARB_uniform_buffer_object shader test Fixed native code compiler warnings Made the generator check file timestamps to avoid unnecessary 3+ min builds
This commit is contained in:
parent
197d6c43d2
commit
e97e601894
|
@ -49,6 +49,7 @@
|
|||
<include name="org/lwjgl/test/opengl/shaders/shaderFSH.vsh" />
|
||||
<include name="org/lwjgl/test/opengl/shaders/shaderVP.vp" />
|
||||
<include name="org/lwjgl/test/opengl/shaders/shaderVSH.vsh" />
|
||||
<include name="org/lwjgl/test/opengl/shaders/shaderUNI.vsh" />
|
||||
</fileset>
|
||||
|
||||
<!-- Files to include in the lwjgl_util.jar file -->
|
||||
|
|
|
@ -34,7 +34,10 @@ package org.lwjgl.opengl;
|
|||
import org.lwjgl.BufferUtils;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.CharBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.CharsetEncoder;
|
||||
|
||||
/** @author spasi */
|
||||
final class StringUtils {
|
||||
|
@ -60,6 +63,14 @@ final class StringUtils {
|
|||
}
|
||||
};
|
||||
|
||||
private static final ThreadLocal infiniteSeqTL = new ThreadLocal() {
|
||||
protected Object initialValue() {
|
||||
return new InfiniteCharSequence();
|
||||
}
|
||||
};
|
||||
|
||||
private static CharsetEncoder encoder = Charset.forName("US-ASCII").newEncoder();
|
||||
|
||||
private StringUtils() {
|
||||
}
|
||||
|
||||
|
@ -129,14 +140,17 @@ final class StringUtils {
|
|||
return lengths;
|
||||
}
|
||||
|
||||
/*
|
||||
static InfiniteCharSequence getInfiniteSeq() {
|
||||
return (InfiniteCharSequence)infiniteSeqTL.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a byte string from the specified buffer.
|
||||
*
|
||||
* @param buffer
|
||||
*
|
||||
* @return the buffer as a String.
|
||||
*/
|
||||
|
||||
static String getString(final ByteBuffer buffer) {
|
||||
final int length = buffer.remaining();
|
||||
final char[] charArray = getArray(length);
|
||||
|
@ -147,6 +161,12 @@ final class StringUtils {
|
|||
return new String(charArray, 0, length);
|
||||
}
|
||||
|
||||
private static void encode(final ByteBuffer buffer, final CharSequence string) {
|
||||
final InfiniteCharSequence infiniteSeq = getInfiniteSeq();
|
||||
infiniteSeq.setString(string);
|
||||
encoder.encode(infiniteSeq.buffer, buffer, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a buffer containing the specified string as bytes.
|
||||
*
|
||||
|
@ -157,8 +177,7 @@ final class StringUtils {
|
|||
static ByteBuffer getBuffer(final CharSequence string) {
|
||||
final ByteBuffer buffer = getBuffer(string.length());
|
||||
|
||||
for ( int i = 0; i < string.length(); i++ )
|
||||
buffer.put((byte)string.charAt(i));
|
||||
encode(buffer, string);
|
||||
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
|
@ -174,8 +193,7 @@ final class StringUtils {
|
|||
static ByteBuffer getBufferOffset(final CharSequence string, final int offset) {
|
||||
final ByteBuffer buffer = getBufferOffset(offset + string.length());
|
||||
|
||||
for ( int i = 0; i < string.length(); i++ )
|
||||
buffer.put((byte)string.charAt(i));
|
||||
encode(buffer, string);
|
||||
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
|
@ -191,14 +209,21 @@ final class StringUtils {
|
|||
static ByteBuffer getBufferNT(final CharSequence string) {
|
||||
final ByteBuffer buffer = getBuffer(string.length() + 1);
|
||||
|
||||
for ( int i = 0; i < string.length(); i++ )
|
||||
buffer.put((byte)string.charAt(i));
|
||||
encode(buffer, string);
|
||||
|
||||
buffer.put((byte)0);
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
}
|
||||
|
||||
private static int getTotalLength(final CharSequence[] strings) {
|
||||
int length = 0;
|
||||
for ( int i = 0; i < strings.length; i++ )
|
||||
length += strings[i].length();
|
||||
|
||||
return length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a buffer containing the specified strings as bytes.
|
||||
*
|
||||
|
@ -207,17 +232,14 @@ final class StringUtils {
|
|||
* @return the Strings as a ByteBuffer
|
||||
*/
|
||||
static ByteBuffer getBuffer(final CharSequence[] strings) {
|
||||
int length = 0;
|
||||
for ( int i = 0; i < strings.length; i++ )
|
||||
length += strings[i].length();
|
||||
|
||||
final ByteBuffer buffer = getBuffer(length);
|
||||
final ByteBuffer buffer = getBuffer(getTotalLength(strings));
|
||||
|
||||
final InfiniteCharSequence infiniteSeq = getInfiniteSeq();
|
||||
for ( int i = 0; i < strings.length; i++ ) {
|
||||
final CharSequence string = strings[i];
|
||||
for ( int j = 0; j < string.length(); j++ )
|
||||
buffer.put((byte)string.charAt(i));
|
||||
infiniteSeq.setString(strings[i]);
|
||||
encoder.encode(infiniteSeq.buffer, buffer, true);
|
||||
}
|
||||
infiniteSeq.clear();
|
||||
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
|
@ -231,18 +253,15 @@ final class StringUtils {
|
|||
* @return the Strings as a ByteBuffer
|
||||
*/
|
||||
static ByteBuffer getBufferNT(final CharSequence[] strings) {
|
||||
int length = 0;
|
||||
for ( int i = 0; i < strings.length; i++ )
|
||||
length += strings[i].length() + 1;
|
||||
|
||||
final ByteBuffer buffer = getBuffer(length);
|
||||
final ByteBuffer buffer = getBuffer(getTotalLength(strings) + strings.length);
|
||||
|
||||
final InfiniteCharSequence infiniteSeq = getInfiniteSeq();
|
||||
for ( int i = 0; i < strings.length; i++ ) {
|
||||
final CharSequence string = strings[i];
|
||||
for ( int j = 0; j < string.length(); j++ )
|
||||
buffer.put((byte)string.charAt(i));
|
||||
infiniteSeq.setString(strings[i]);
|
||||
encoder.encode(infiniteSeq.buffer, buffer, true);
|
||||
buffer.put((byte)0);
|
||||
}
|
||||
infiniteSeq.clear();
|
||||
|
||||
buffer.flip();
|
||||
return buffer;
|
||||
|
@ -265,4 +284,41 @@ final class StringUtils {
|
|||
return buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* A mutable CharSequence with very large initial length. We can wrap this in a re-usable CharBuffer for decoding.
|
||||
* We cannot subclass CharBuffer because of {@link CharBuffer#toString(int,int)}.
|
||||
*/
|
||||
private static class InfiniteCharSequence implements CharSequence {
|
||||
|
||||
final CharBuffer buffer;
|
||||
|
||||
CharSequence string;
|
||||
|
||||
InfiniteCharSequence() {
|
||||
buffer = CharBuffer.wrap(this);
|
||||
}
|
||||
|
||||
void setString(final CharSequence string) {
|
||||
this.string = string;
|
||||
this.buffer.position(0);
|
||||
this.buffer.limit(string.length());
|
||||
}
|
||||
|
||||
void clear() {
|
||||
this.string = null;
|
||||
}
|
||||
|
||||
public int length() {
|
||||
return Integer.MAX_VALUE;
|
||||
}
|
||||
|
||||
public char charAt(final int index) {
|
||||
return string.charAt(index);
|
||||
}
|
||||
|
||||
public CharSequence subSequence(final int start, final int end) {
|
||||
return string.subSequence(start, end);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,31 +1,31 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 2002-2008 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
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
*
|
||||
* * 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
|
||||
* * 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
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
@ -73,8 +73,8 @@ abstract class Shader {
|
|||
return int_buffer.get(0);
|
||||
}
|
||||
|
||||
protected static ByteBuffer getShaderText(String file) {
|
||||
ByteBuffer shader = null;
|
||||
protected static String getShaderText(String file) {
|
||||
String shader = null;
|
||||
|
||||
try {
|
||||
ClassLoader loader = ShadersTest.class.getClassLoader();
|
||||
|
@ -93,10 +93,10 @@ abstract class Shader {
|
|||
|
||||
fileBuffer.flip();
|
||||
|
||||
shader = BufferUtils.createByteBuffer(fileBuffer.limit());
|
||||
shader.put(fileBuffer);
|
||||
byte[] array = new byte[fileBuffer.remaining()];
|
||||
fileBuffer.get(array);
|
||||
shader = new String(array);
|
||||
|
||||
shader.clear();
|
||||
fileBuffer.clear();
|
||||
} catch (IOException e) {
|
||||
ShadersTest.kill("Failed to read the shader source file: " + file, e);
|
||||
|
@ -105,17 +105,13 @@ abstract class Shader {
|
|||
return shader;
|
||||
}
|
||||
|
||||
protected static void checkProgramError(String programFile, ByteBuffer programSource) {
|
||||
protected static void checkProgramError(String programFile, String programSource) {
|
||||
if ( GL11.glGetError() == GL11.GL_INVALID_OPERATION ) {
|
||||
programSource.clear();
|
||||
final byte[] bytes = new byte[programSource.capacity()];
|
||||
programSource.get(bytes);
|
||||
|
||||
final int errorPos = glGetInteger(ARBProgram.GL_PROGRAM_ERROR_POSITION_ARB);
|
||||
int lineStart = 0;
|
||||
int lineEnd = -1;
|
||||
for ( int i = 0; i < bytes.length; i++ ) {
|
||||
if ( bytes[i] == '\n' ) {
|
||||
for ( int i = 0; i < programSource.length(); i++ ) {
|
||||
if ( programSource.charAt(i) == '\n' ) {
|
||||
if ( i <= errorPos ) {
|
||||
lineStart = i + 1;
|
||||
} else {
|
||||
|
@ -126,28 +122,16 @@ abstract class Shader {
|
|||
}
|
||||
|
||||
if ( lineEnd == -1 )
|
||||
lineEnd = bytes.length;
|
||||
lineEnd = programSource.length();
|
||||
|
||||
ShadersTest.kill("Low-level program error in file: " + programFile
|
||||
+ "\n\tError line: " + new String(bytes, lineStart, lineEnd - lineStart)
|
||||
+ "\n\tError line: " + programSource.substring(lineStart, lineEnd)
|
||||
+ "\n\tError message: " + GL11.glGetString(ARBProgram.GL_PROGRAM_ERROR_STRING_ARB));
|
||||
}
|
||||
}
|
||||
|
||||
protected static int getUniformLocation(int ID, String name) {
|
||||
fileBuffer.clear();
|
||||
|
||||
int length = name.length();
|
||||
|
||||
char[] charArray = new char[length];
|
||||
name.getChars(0, length, charArray, 0);
|
||||
|
||||
for ( int i = 0; i < length; i++ )
|
||||
fileBuffer.put((byte)charArray[i]);
|
||||
fileBuffer.put((byte)0); // Must be null-terminated.
|
||||
fileBuffer.flip();
|
||||
|
||||
final int location = ARBShaderObjects.glGetUniformLocationARB(ID, fileBuffer);
|
||||
final int location = ARBShaderObjects.glGetUniformLocationARB(ID, name);
|
||||
|
||||
if ( location == -1 )
|
||||
throw new IllegalArgumentException("The uniform \"" + name + "\" does not exist in the Shader Program.");
|
||||
|
@ -159,42 +143,25 @@ abstract class Shader {
|
|||
ARBShaderObjects.glGetObjectParameterARB(ID, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, programBuffer);
|
||||
|
||||
final int logLength = programBuffer.get(0);
|
||||
|
||||
if ( logLength <= 1 )
|
||||
return;
|
||||
|
||||
final ByteBuffer log = BufferUtils.createByteBuffer(logLength);
|
||||
|
||||
ARBShaderObjects.glGetInfoLogARB(ID, null, log);
|
||||
|
||||
final char[] charArray = new char[logLength];
|
||||
for ( int i = 0; i < logLength; i++ )
|
||||
charArray[i] = (char)log.get();
|
||||
|
||||
System.out.println("\nInfo Log of Shader Object: " + file);
|
||||
System.out.println("--------------------------");
|
||||
System.out.println(new String(charArray, 0, logLength));
|
||||
System.out.println(ARBShaderObjects.glGetInfoLogARB(ID, logLength));
|
||||
|
||||
}
|
||||
|
||||
protected static void printShaderProgramInfoLog(int ID) {
|
||||
ARBShaderObjects.glGetObjectParameterARB(ID, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, programBuffer);
|
||||
|
||||
final int logLength = programBuffer.get(0);
|
||||
|
||||
if ( logLength <= 1 )
|
||||
return;
|
||||
|
||||
final ByteBuffer log = BufferUtils.createByteBuffer(logLength);
|
||||
|
||||
ARBShaderObjects.glGetInfoLogARB(ID, null, log);
|
||||
|
||||
final char[] charArray = new char[logLength];
|
||||
for ( int i = 0; i < logLength; i++ )
|
||||
charArray[i] = (char)log.get();
|
||||
|
||||
System.out.println("\nShader Program Info Log: ");
|
||||
System.out.println("--------------------------");
|
||||
System.out.println(new String(charArray, 0, logLength));
|
||||
System.out.println(ARBShaderObjects.glGetInfoLogARB(ID, logLength));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 2002-2008 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
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
*
|
||||
* * 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
|
||||
* * 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
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
@ -38,8 +38,6 @@
|
|||
|
||||
package org.lwjgl.test.opengl.shaders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.lwjgl.opengl.ARBFragmentProgram;
|
||||
import org.lwjgl.opengl.ARBProgram;
|
||||
import org.lwjgl.opengl.ARBVertexProgram;
|
||||
|
@ -48,12 +46,12 @@ import org.lwjgl.opengl.GL11;
|
|||
final class ShaderFP extends Shader {
|
||||
|
||||
final String vpFile;
|
||||
final ByteBuffer vpSource;
|
||||
final String vpSource;
|
||||
|
||||
final int vpID;
|
||||
|
||||
final String fpFile;
|
||||
final ByteBuffer fpSource;
|
||||
final String fpSource;
|
||||
|
||||
final int fpID;
|
||||
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 2002-2008 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
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
*
|
||||
* * 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
|
||||
* * 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
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
@ -38,8 +38,6 @@
|
|||
|
||||
package org.lwjgl.test.opengl.shaders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.lwjgl.opengl.ARBFragmentShader;
|
||||
import org.lwjgl.opengl.ARBShaderObjects;
|
||||
import org.lwjgl.opengl.ARBVertexShader;
|
||||
|
@ -48,12 +46,12 @@ import org.lwjgl.opengl.GL11;
|
|||
final class ShaderFSH extends Shader {
|
||||
|
||||
final String vshFile;
|
||||
final ByteBuffer vshSource;
|
||||
final String vshSource;
|
||||
|
||||
final int vshID;
|
||||
|
||||
final String fshFile;
|
||||
final ByteBuffer fshSource;
|
||||
final String fshSource;
|
||||
|
||||
final int fshID;
|
||||
|
||||
|
|
|
@ -38,23 +38,27 @@
|
|||
package org.lwjgl.test.opengl.shaders;
|
||||
|
||||
import org.lwjgl.BufferUtils;
|
||||
import org.lwjgl.opengl.ARBUniformBufferObject;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
import org.lwjgl.opengl.GL20;
|
||||
import org.lwjgl.opengl.*;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.FloatBuffer;
|
||||
import java.nio.IntBuffer;
|
||||
|
||||
final class ShaderUNI extends Shader {
|
||||
|
||||
final String file;
|
||||
final ByteBuffer source;
|
||||
final String source;
|
||||
|
||||
final int shaderID;
|
||||
final int programID;
|
||||
|
||||
final int uniformA;
|
||||
final int uniformB;
|
||||
final int bufferID;
|
||||
final FloatBuffer buffer;
|
||||
|
||||
final int uniformA_index;
|
||||
final int uniformA_offset;
|
||||
|
||||
final int uniformB_index;
|
||||
final int uniformB_offset;
|
||||
|
||||
ShaderUNI(final String shaderFile) {
|
||||
file = shaderFile;
|
||||
|
@ -81,38 +85,73 @@ final class ShaderUNI extends Shader {
|
|||
if ( programBuffer.get(0) == GL11.GL_FALSE )
|
||||
ShadersTest.kill("A linking error occured in a shader program.");
|
||||
|
||||
uniformA = getUniformLocation(programID, "uniformA");
|
||||
uniformB = getUniformLocation(programID, "uniformB");
|
||||
final String[] uniformNames = { "uniformA", "uniformB" };
|
||||
|
||||
String[] uniformNames = { "uniformA", "uniformB" };
|
||||
IntBuffer tmp = BufferUtils.createIntBuffer(uniformNames.length);
|
||||
IntBuffer indexes = BufferUtils.createIntBuffer(uniformNames.length);
|
||||
IntBuffer params = BufferUtils.createIntBuffer(uniformNames.length);
|
||||
IntBuffer getBuffer = BufferUtils.createIntBuffer(16);
|
||||
IntBuffer buffers = BufferUtils.createIntBuffer(1);
|
||||
|
||||
ARBUniformBufferObject.glGetUniformIndices(programID, toByteBuffer(uniformNames), tmp);
|
||||
// Get uniform block index and data size
|
||||
final int blockIndex = ARBUniformBufferObject.glGetUniformBlockIndex(programID, "test");
|
||||
ARBUniformBufferObject.glGetActiveUniformBlock(programID, blockIndex, ARBUniformBufferObject.GL_UNIFORM_BLOCK_DATA_SIZE, getBuffer);
|
||||
final int blockSize = getBuffer.get(0);
|
||||
|
||||
System.out.println("uniformA index = " + tmp.get(0));
|
||||
System.out.println("uniformB index = " + tmp.get(1));
|
||||
}
|
||||
System.out.println("blockSize = " + blockSize);
|
||||
|
||||
private static ByteBuffer toByteBuffer(String[] strs) {
|
||||
int length = 0;
|
||||
for ( int i = 0; i < strs.length; i++ )
|
||||
length += strs[i].length() + 1; // +1 for the NULL-character
|
||||
// Create uniform buffer object and allocate a ByteBuffer
|
||||
GL15.glGenBuffers(buffers);
|
||||
bufferID = buffers.get(0);
|
||||
GL15.glBindBuffer(ARBUniformBufferObject.GL_UNIFORM_BUFFER, bufferID);
|
||||
GL15.glBufferData(ARBUniformBufferObject.GL_UNIFORM_BUFFER, blockSize, GL15.GL_DYNAMIC_DRAW);
|
||||
buffer = BufferUtils.createFloatBuffer(blockSize);
|
||||
|
||||
final ByteBuffer buff = BufferUtils.createByteBuffer(length);
|
||||
for ( int i = 0; i < strs.length; i++ ) {
|
||||
buff.put(strs[i].getBytes());
|
||||
buff.put((byte)0); // The ending NULL-character
|
||||
}
|
||||
buff.flip();
|
||||
// Attach UBO and associate uniform block to binding point 0
|
||||
ARBUniformBufferObject.glBindBufferBase(ARBUniformBufferObject.GL_UNIFORM_BUFFER, 0, bufferID);
|
||||
ARBUniformBufferObject.glUniformBlockBinding(programID, blockIndex, 0);
|
||||
|
||||
return buff;
|
||||
// Get uniform information
|
||||
ARBUniformBufferObject.glGetUniformIndices(programID, uniformNames, indexes);
|
||||
uniformA_index = indexes.get(0);
|
||||
uniformB_index = indexes.get(1);
|
||||
|
||||
ARBUniformBufferObject.glGetActiveUniforms(programID, indexes, ARBUniformBufferObject.GL_UNIFORM_OFFSET, params);
|
||||
uniformA_offset = params.get(0);
|
||||
uniformB_offset = params.get(1);
|
||||
|
||||
System.out.println("\nuniformA index = " + uniformA_index);
|
||||
System.out.println("uniformB index = " + uniformB_index);
|
||||
|
||||
System.out.println("\nuniformA offset = " + uniformA_offset + " - should be 0 for std140");
|
||||
System.out.println("uniformB offset = " + uniformB_offset + " - should be 16 for std140");
|
||||
|
||||
Util.checkGLError();
|
||||
}
|
||||
|
||||
void render() {
|
||||
GL20.glUseProgram(programID);
|
||||
|
||||
GL20.glUniform2f(uniformA, ShadersTest.getSin(), ShadersTest.getSpecularity() * 8.0f);
|
||||
GL20.glUniform3f(uniformB, 0.0f, 0.7f, 0.0f);
|
||||
//* -- std140 layout
|
||||
// Uniform A
|
||||
buffer.put(0, ShadersTest.getSin()).put(1, ShadersTest.getSpecularity() * 8.0f);
|
||||
// Uniform B - str140 alignment at 16 bytes
|
||||
buffer.put(4, 0.0f).put(5, 0.7f).put(6, 0.0f);
|
||||
|
||||
GL15.glBindBuffer(ARBUniformBufferObject.GL_UNIFORM_BUFFER, bufferID);
|
||||
GL15.glBufferData(ARBUniformBufferObject.GL_UNIFORM_BUFFER, buffer, GL15.GL_DYNAMIC_DRAW);
|
||||
//*/
|
||||
|
||||
/* -- non-std140 layout
|
||||
// Uniform A
|
||||
buffer.put(ShadersTest.getSin()).put(ShadersTest.getSpecularity() * 8.0f);
|
||||
buffer.flip();
|
||||
GL15.glBufferSubData(ARBUniformBufferObject.GL_UNIFORM_BUFFER, uniformA_offset, buffer);
|
||||
// Uniform B
|
||||
buffer.clear();
|
||||
buffer.put(0.0f).put(0.7f).put(0.0f);
|
||||
buffer.flip();
|
||||
GL15.glBufferSubData(ARBUniformBufferObject.GL_UNIFORM_BUFFER, uniformB_offset, buffer);
|
||||
//*/
|
||||
|
||||
ShadersTest.renderObject();
|
||||
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 2002-2008 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
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
*
|
||||
* * 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
|
||||
* * 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
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
@ -38,8 +38,6 @@
|
|||
|
||||
package org.lwjgl.test.opengl.shaders;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import org.lwjgl.opengl.ARBProgram;
|
||||
import org.lwjgl.opengl.ARBVertexProgram;
|
||||
import org.lwjgl.opengl.GL11;
|
||||
|
@ -47,7 +45,7 @@ import org.lwjgl.opengl.GL11;
|
|||
final class ShaderVP extends Shader {
|
||||
|
||||
final String file;
|
||||
final ByteBuffer source;
|
||||
final String source;
|
||||
|
||||
final int ID;
|
||||
|
||||
|
|
|
@ -1,31 +1,31 @@
|
|||
/*
|
||||
/*
|
||||
* Copyright (c) 2002-2008 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
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
*
|
||||
* * 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
|
||||
* * 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
|
||||
* 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
|
||||
* 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.
|
||||
*/
|
||||
|
@ -47,7 +47,7 @@ import org.lwjgl.opengl.GL11;
|
|||
final class ShaderVSH extends Shader {
|
||||
|
||||
final String file;
|
||||
final ByteBuffer source;
|
||||
final String source;
|
||||
|
||||
final int shaderID;
|
||||
final int programID;
|
||||
|
|
|
@ -141,35 +141,37 @@ public final class ShadersTest {
|
|||
kill(e.getMessage());
|
||||
}
|
||||
|
||||
final ContextCapabilities caps = GLContext.getCapabilities();
|
||||
|
||||
if ( "NONE".equalsIgnoreCase(args[0]) ) {
|
||||
shader = null;
|
||||
} else if ( "VP".equalsIgnoreCase(args[0]) ) {
|
||||
if ( !GLContext.getCapabilities().GL_ARB_vertex_program )
|
||||
if ( !caps.GL_ARB_vertex_program )
|
||||
kill("The ARB_vertex_program extension is not supported.");
|
||||
|
||||
shader = new ShaderVP("shaderVP.vp");
|
||||
} else if ( "FP".equalsIgnoreCase(args[0]) ) {
|
||||
if ( !GLContext.getCapabilities().GL_ARB_vertex_program )
|
||||
if ( !caps.GL_ARB_vertex_program )
|
||||
kill("The ARB_vertex_program extension is not supported.");
|
||||
if ( !GLContext.getCapabilities().GL_ARB_fragment_program )
|
||||
if ( !caps.GL_ARB_fragment_program )
|
||||
kill("The ARB_fragment_program extension is not supported.");
|
||||
|
||||
shader = new ShaderFP("shaderFP.vp", "shaderFP.fp");
|
||||
} else if ( "VSH".equalsIgnoreCase(args[0]) ) {
|
||||
if ( !GLContext.getCapabilities().GL_ARB_vertex_shader )
|
||||
if ( !caps.GL_ARB_vertex_shader )
|
||||
kill("The ARB_vertex_shader extension is not supported.");
|
||||
|
||||
shader = new ShaderVSH("shaderVSH.vsh");
|
||||
} else if ( "FSH".equalsIgnoreCase(args[0]) ) {
|
||||
if ( !GLContext.getCapabilities().GL_ARB_vertex_shader )
|
||||
if ( !caps.GL_ARB_vertex_shader )
|
||||
kill("The ARB_vertex_shader extension is not supported.");
|
||||
if ( !GLContext.getCapabilities().GL_ARB_fragment_shader )
|
||||
if ( !caps.GL_ARB_fragment_shader )
|
||||
kill("The ARB_fragment_shader extension is not supported.");
|
||||
|
||||
shader = new ShaderFSH("shaderFSH.vsh", "shaderFSH.fsh");
|
||||
} else if ("UNI".equalsIgnoreCase(args[0]) ) {
|
||||
if ( !GLContext.getCapabilities().OpenGL31 )
|
||||
kill("OpenGL version 3.1 is not supported.");
|
||||
if ( !(caps.OpenGL31 || caps.GL_ARB_uniform_buffer_object) )
|
||||
kill("Neither OpenGL version 3.1 nor ARB_uniform_buffer_object are supported.");
|
||||
|
||||
shader = new ShaderUNI("shaderUNI.vsh");
|
||||
} else {
|
||||
|
@ -316,6 +318,7 @@ public final class ShadersTest {
|
|||
System.out.println("vsh\t- Use ARB_vertex_shader (GLSL) only.");
|
||||
System.out.println("fp\t- Use ARB_vertex_program + ARB_fragment_program (low-level).");
|
||||
System.out.println("fsh\t- Use ARB_vertex_shader + ARB_fragment_shader (GLSL).");
|
||||
System.out.println("uni\t- Use ARB_uniform_buffer_object to update shader uniforms (GLSL).");
|
||||
|
||||
cleanup();
|
||||
System.exit(-1);
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
#version 140
|
||||
#extension GL_ARB_uniform_buffer_object : enable
|
||||
|
||||
uniform vec2 uniformA;
|
||||
uniform vec3 uniformB;
|
||||
layout(std140) uniform test {
|
||||
vec2 uniformA;
|
||||
vec3 uniformB;
|
||||
};
|
||||
|
||||
void main(void) {
|
||||
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
|
||||
|
|
|
@ -250,6 +250,12 @@ public class GeneratorVisitor extends SimpleDeclarationVisitor {
|
|||
|
||||
public void visitInterfaceDeclaration(InterfaceDeclaration d) {
|
||||
try {
|
||||
// Skip this class if the output exists and the input has not been modified.
|
||||
File input = d.getPosition().file();
|
||||
File output = new File(env.getOptions().get("-s") + '/' + d.getPackage().getQualifiedName().replace('.', '/'), Utils.getSimpleClassName(d) + ".java");
|
||||
if ( output.exists() && input.lastModified() < output.lastModified() )
|
||||
return;
|
||||
|
||||
if (d.getMethods().size() > 0 || d.getFields().size() > 0)
|
||||
generateJavaSource(d);
|
||||
if (d.getMethods().size() > 0)
|
||||
|
|
|
@ -161,7 +161,7 @@ public class NativeMethodStubsGenerator {
|
|||
} else if (String.class.equals(java_result_type)) {
|
||||
writer.print("NewStringNativeUnsigned(env, ");
|
||||
} else if ( method.getAnnotation(GLpointer.class) != null ) {
|
||||
writer.print("(jlong)");
|
||||
writer.print("(intptr_t)");
|
||||
}
|
||||
writer.print(Utils.RESULT_VAR_NAME);
|
||||
if (Buffer.class.isAssignableFrom(java_result_type)) {
|
||||
|
@ -192,8 +192,8 @@ public class NativeMethodStubsGenerator {
|
|||
}
|
||||
|
||||
private static void generateCallParameter(PrintWriter writer, TypeMap type_map, ParameterDeclaration param) {
|
||||
boolean is_indirect = param.getAnnotation(Indirect.class) != null || param.getAnnotation(StringList.class) != null;
|
||||
if (is_indirect) {
|
||||
boolean is_indirect = param.getAnnotation(Indirect.class) != null;
|
||||
if (is_indirect || param.getAnnotation(StringList.class) != null) {
|
||||
writer.print("(");
|
||||
NativeTypeTranslator translator = new NativeTypeTranslator(type_map, param);
|
||||
param.getType().accept(translator);
|
||||
|
@ -201,7 +201,7 @@ public class NativeMethodStubsGenerator {
|
|||
writer.print("*)");
|
||||
}
|
||||
if ( param.getAnnotation(GLpointer.class) != null )
|
||||
writer.print("(" + param.getAnnotation(GLpointer.class).value() + ")");
|
||||
writer.print("(" + param.getAnnotation(GLpointer.class).value() + ")(intptr_t)");
|
||||
if (param.getAnnotation(Result.class) != null || is_indirect)
|
||||
writer.print("&");
|
||||
if (param.getAnnotation(Result.class) != null) {
|
||||
|
|
Loading…
Reference in New Issue