lwjgl2-arm64/src/java/org/lwjgl/opengl/GLChecks.java

250 lines
11 KiB
Java
Raw Normal View History

2004-11-09 22:29:17 +01:00
/*
2008-04-07 20:36:09 +02:00
* Copyright (c) 2002-2008 LWJGL Project
2004-06-12 22:28:34 +02:00
* All rights reserved.
2004-11-09 22:29:17 +01:00
*
* Redistribution and use in source and binary forms, with or without
2004-11-09 22:29:17 +01:00
* modification, are permitted provided that the following conditions are
2004-06-12 22:28:34 +02:00
* met:
2004-11-09 22:29:17 +01:00
*
* * Redistributions of source code must retain the above copyright
2004-06-12 22:28:34 +02:00
* 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.
*
2004-11-09 22:29:17 +01:00
* * Neither the name of 'LWJGL' nor the names of
* its contributors may be used to endorse or promote products derived
2004-06-12 22:28:34 +02:00
* from this software without specific prior written permission.
2004-11-09 22:29:17 +01:00
*
2004-06-12 22:28:34 +02:00
* 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
2004-11-09 22:29:17 +01:00
* 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
2004-06-12 22:28:34 +02:00
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2004-11-09 22:29:17 +01:00
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2004-06-12 22:28:34 +02:00
* 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.opengl;
2005-05-04 22:59:44 +02:00
import java.nio.Buffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLUtil;
2004-04-03 23:08:23 +02:00
import static org.lwjgl.opengl.ARBBufferObject.*;
import static org.lwjgl.opengl.ATIVertexArrayObject.*;
import static org.lwjgl.opengl.EXTAbgr.*;
import static org.lwjgl.opengl.EXTBgra.*;
import static org.lwjgl.opengl.EXTDirectStateAccess.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
/**
2006-03-23 20:32:21 +01:00
* A class to check buffer boundaries in GL methods. Many GL
2004-11-25 23:20:45 +01:00
* methods read data from the GL into a native Buffer at its current position. If there is unsufficient space in the buffer when
* the call is made then a buffer overflow would otherwise occur and cause unexpected behaviour, a crash, or worse, a security
* risk. Therefore in those methods where GL reads data back into a buffer, we will call a bounds check method from this class
* to ensure that there is sufficient space in the buffer.
* <p/>
* Thrown by the debug build library of the LWJGL if any OpenGL operation causes an error.
2004-11-09 22:29:17 +01:00
*
* @author cix_foo <cix_foo@users.sourceforge.net>
* @version $Revision$
2006-03-23 20:32:21 +01:00
* $Id$
*/
class GLChecks {
2004-11-25 23:20:45 +01:00
/** Static methods only! */
private GLChecks() {
}
static References getReferences(ContextCapabilities caps) {
return StateTracker.getReferencesStack(caps).getReferences();
}
2004-02-18 23:31:00 +01:00
static int getBufferObjectSize(ContextCapabilities caps, int buffer_enum) {
return glGetBufferParameter(buffer_enum, GL_BUFFER_SIZE);
}
static int getBufferObjectSizeARB(ContextCapabilities caps, int buffer_enum) {
return glGetBufferParameterARB(buffer_enum, GL_BUFFER_SIZE_ARB);
}
static int getBufferObjectSizeATI(ContextCapabilities caps, int buffer) {
return glGetObjectBufferATI(buffer, GL_OBJECT_BUFFER_SIZE_ATI);
}
static int getNamedBufferObjectSize(ContextCapabilities caps, int buffer) {
return glGetNamedBufferParameterEXT(buffer, GL_BUFFER_SIZE);
}
2005-01-13 03:17:42 +01:00
/** Helper method to ensure that array buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */
static void ensureArrayVBOdisabled(ContextCapabilities caps) {
if( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().arrayBuffer != 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use Buffers when Array Buffer Object is enabled");
}
2004-11-25 23:20:45 +01:00
2005-01-13 03:17:42 +01:00
/** Helper method to ensure that array buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */
static void ensureArrayVBOenabled(ContextCapabilities caps) {
if( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().arrayBuffer == 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use offsets when Array Buffer Object is disabled");
}
2004-11-25 23:20:45 +01:00
2005-01-13 03:17:42 +01:00
/** Helper method to ensure that element array buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */
static void ensureElementVBOdisabled(ContextCapabilities caps) {
if( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().elementArrayBuffer != 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use Buffers when Element Array Buffer Object is enabled");
}
2004-11-25 23:20:45 +01:00
2005-01-13 03:17:42 +01:00
/** Helper method to ensure that element array buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */
static void ensureElementVBOenabled(ContextCapabilities caps) {
if( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().elementArrayBuffer == 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use offsets when Element Array Buffer Object is disabled");
}
/** Helper method to ensure that array buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */
static void ensureIndirectBOdisabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().indirectBuffer != 0 )
throw new OpenGLException("Cannot use Buffers when Draw Indirect Object is enabled");
}
/** Helper method to ensure that array buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */
static void ensureIndirectBOenabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().indirectBuffer == 0 )
throw new OpenGLException("Cannot use offsets when Draw Indirect Object is disabled");
}
2005-01-13 03:17:42 +01:00
/** Helper method to ensure that pixel pack buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */
static void ensurePackPBOdisabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().pixelPackBuffer != 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use Buffers when Pixel Pack Buffer Object is enabled");
}
/** Helper method to ensure that pixel pack buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */
static void ensurePackPBOenabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().pixelPackBuffer == 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use offsets when Pixel Pack Buffer Object is disabled");
}
/** Helper method to ensure that pixel unpack buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */
static void ensureUnpackPBOdisabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().pixelUnpackBuffer != 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use Buffers when Pixel Unpack Buffer Object is enabled");
}
/** Helper method to ensure that pixel unpack buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */
static void ensureUnpackPBOenabled(ContextCapabilities caps) {
if ( LWJGLUtil.CHECKS && StateTracker.getReferencesStack(caps).getReferences().pixelUnpackBuffer == 0 )
2005-01-13 03:17:42 +01:00
throw new OpenGLException("Cannot use offsets when Pixel Unpack Buffer Object is disabled");
}
2004-11-25 23:20:45 +01:00
2004-02-15 20:41:51 +01:00
/**
* Calculate the storage required for an image in elements
*
* @param format The format of the image (example: GL_RGBA)
* @param type The type of the image elements (example: GL_UNSIGNED_BYTE)
* @param width The width of the image
* @param height The height of the image (1 for 1D images)
* @param depth The depth of the image (1 for 2D images)
*
* @return the size, in elements, of the image
*/
static int calculateImageStorage(Buffer buffer, int format, int type, int width, int height, int depth) {
return LWJGLUtil.CHECKS ? calculateImageStorage(format, type, width, height, depth) >> BufferUtils.getElementSizeExponent(buffer) : 0;
}
static int calculateTexImage1DStorage(Buffer buffer, int format, int type, int width) {
return LWJGLUtil.CHECKS ? calculateTexImage1DStorage(format, type, width) >> BufferUtils.getElementSizeExponent(buffer) : 0;
}
static int calculateTexImage2DStorage(Buffer buffer, int format, int type, int width, int height) {
return LWJGLUtil.CHECKS ? calculateTexImage2DStorage(format, type, width, height) >> BufferUtils.getElementSizeExponent(buffer) : 0;
}
static int calculateTexImage3DStorage(Buffer buffer, int format, int type, int width, int height, int depth) {
return LWJGLUtil.CHECKS ? calculateTexImage3DStorage(format, type, width, height, depth) >> BufferUtils.getElementSizeExponent(buffer) : 0;
}
/**
* Calculate the storage required for an image in bytes.
2004-11-09 22:29:17 +01:00
*
2004-11-25 23:20:45 +01:00
* @param format The format of the image (example: GL_RGBA)
* @param type The type of the image elements (example: GL_UNSIGNED_BYTE)
* @param width The width of the image
* @param height The height of the image (1 for 1D images)
* @param depth The depth of the image (1 for 2D images)
*
2004-02-15 20:41:51 +01:00
* @return the size, in bytes, of the image
*/
private static int calculateImageStorage(int format, int type, int width, int height, int depth) {
return calculateBytesPerPixel(format, type) * width * height * depth;
2004-11-25 23:20:45 +01:00
}
private static int calculateTexImage1DStorage(int format, int type, int width) {
return calculateBytesPerPixel(format, type) * width;
2004-11-25 23:20:45 +01:00
}
private static int calculateTexImage2DStorage(int format, int type, int width, int height) {
return calculateTexImage1DStorage(format, type, width) * height;
2004-11-25 23:20:45 +01:00
}
private static int calculateTexImage3DStorage(int format, int type, int width, int height, int depth) {
return calculateTexImage2DStorage(format, type, width, height) * depth;
2004-11-25 23:20:45 +01:00
}
private static int calculateBytesPerPixel(int format, int type) {
2004-02-15 20:41:51 +01:00
int bpe;
2004-11-25 23:20:45 +01:00
switch ( type ) {
case GL_UNSIGNED_BYTE:
case GL_BYTE:
2004-02-15 20:41:51 +01:00
bpe = 1;
break;
case GL_UNSIGNED_SHORT:
case GL_SHORT:
2004-02-15 20:41:51 +01:00
bpe = 2;
break;
case GL_UNSIGNED_INT:
case GL_INT:
case GL_FLOAT:
2004-02-15 20:41:51 +01:00
bpe = 4;
break;
default :
// TODO: Add more types (like the GL12 types GL_UNSIGNED_INT_8_8_8_8
return 0;
2004-11-25 23:20:45 +01:00
// throw new IllegalArgumentException("Unknown type " + type);
2004-02-15 20:41:51 +01:00
}
int epp;
2004-11-25 23:20:45 +01:00
switch ( format ) {
case GL_LUMINANCE:
case GL_ALPHA:
2004-02-15 20:41:51 +01:00
epp = 1;
break;
case GL_LUMINANCE_ALPHA:
2004-02-15 20:41:51 +01:00
epp = 2;
break;
case GL_RGB:
case GL_BGR_EXT:
2004-02-15 20:41:51 +01:00
epp = 3;
break;
case GL_RGBA:
case GL_ABGR_EXT:
case GL_BGRA_EXT:
2004-02-15 20:41:51 +01:00
epp = 4;
break;
default :
// TODO: Add more formats. Assuming 4 is too wasteful on buffer sizes where e.g. 1 is enough (like GL_DEPTH_COMPONENT)
return 0;
/* // Assume 4 elements per pixel
epp = 4;*/
2004-02-15 20:41:51 +01:00
}
2004-11-25 23:20:45 +01:00
return bpe * epp;
2004-02-15 20:41:51 +01:00
}
}