• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright 2011 See AUTHORS file.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 package com.badlogic.gdx.graphics.profiling;
18 
19 import static com.badlogic.gdx.graphics.GL20.GL_INVALID_ENUM;
20 import static com.badlogic.gdx.graphics.GL20.GL_INVALID_FRAMEBUFFER_OPERATION;
21 import static com.badlogic.gdx.graphics.GL20.GL_INVALID_OPERATION;
22 import static com.badlogic.gdx.graphics.GL20.GL_INVALID_VALUE;
23 import static com.badlogic.gdx.graphics.GL20.GL_OUT_OF_MEMORY;
24 
25 import com.badlogic.gdx.Gdx;
26 import com.badlogic.gdx.math.FloatCounter;
27 
28 /** When enabled, collects statistics about GL calls and checks for GL errors.
29  * Enabling will wrap Gdx.gl* instances with delegate classes which provide described functionality
30  * and route GL calls to the actual GL instances.
31  *
32  * @see GL20Profiler
33  * @see GL30Profiler
34  *
35  * @author Daniel Holderbaum
36  * @author Jan Polák */
37 public abstract class GLProfiler {
38 
39 	/** All calls to any GL function since the last reset. */
40 	public static int calls;
41 
42 	/** The amount of times a texture binding has happened since the last reset. */
43 	public static int textureBindings;
44 
45 	/** The amount of draw calls that happened since the last reset. */
46 	public static int drawCalls;
47 
48 	/** The amount of times a shader was switched since the last reset. */
49 	public static int shaderSwitches;
50 
51 	/** The amount rendered vertices since the last reset. */
52 	public static final FloatCounter vertexCount = new FloatCounter(0);
53 
resolveErrorNumber(int error)54 	public static String resolveErrorNumber (int error) {
55 		switch (error) {
56 		case GL_INVALID_VALUE:
57 			return "GL_INVALID_VALUE";
58 		case GL_INVALID_OPERATION:
59 			return "GL_INVALID_OPERATION";
60 		case GL_INVALID_FRAMEBUFFER_OPERATION:
61 			return "GL_INVALID_FRAMEBUFFER_OPERATION";
62 		case GL_INVALID_ENUM:
63 			return "GL_INVALID_ENUM";
64 		case GL_OUT_OF_MEMORY:
65 			return "GL_OUT_OF_MEMORY";
66 		default:
67 			return "number " + error;
68 		}
69 	}
70 
71 	/** This listener will be called when GLProfiler is enabled and any GL call sets an error number (retrievable by glGetError call).
72 	 *
73 	 * Default is {@link GLErrorListener#LOGGING_LISTENER}. */
74 	public static GLErrorListener listener = GLErrorListener.LOGGING_LISTENER;
75 
76 	/** Enables profiling by replacing the {@code GL20} and {@code GL30} instances with profiling ones. */
enable()77 	public static void enable () {
78 		if (!isEnabled()) {
79 			Gdx.gl30 = Gdx.gl30 == null ? null : new GL30Profiler(Gdx.gl30);
80 			Gdx.gl20 = Gdx.gl30 != null ? Gdx.gl30 : new GL20Profiler(Gdx.gl20);
81 			Gdx.gl = Gdx.gl20;
82 		}
83 	}
84 
85 	/** Disables profiling by resetting the {@code GL20} and {@code GL30} instances with the original ones. */
disable()86 	public static void disable () {
87 		if (Gdx.gl30 != null && Gdx.gl30 instanceof GL30Profiler) Gdx.gl30 = ((GL30Profiler)Gdx.gl30).gl30;
88 		if (Gdx.gl20 != null && Gdx.gl20 instanceof GL20Profiler) Gdx.gl20 = ((GL20Profiler)Gdx.gl).gl20;
89 		if (Gdx.gl != null && Gdx.gl instanceof GL20Profiler) Gdx.gl = ((GL20Profiler)Gdx.gl).gl20;
90 	}
91 
92 	/** @return Whether profiling is currently enabled */
isEnabled()93 	public static boolean isEnabled() {
94 		return Gdx.gl30 instanceof GL30Profiler || Gdx.gl20 instanceof GL20Profiler;
95 	}
96 
97 	/** Will reset the statistical information which has been collected so far. This should be called after every frame.
98 	 * Error listener is kept as it is. */
reset()99 	public static void reset () {
100 		calls = 0;
101 		textureBindings = 0;
102 		drawCalls = 0;
103 		shaderSwitches = 0;
104 		vertexCount.reset();
105 	}
106 
107 }
108