Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (806)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGL object counting snippet  (Read 11703 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2015-12-19 00:34:57 »

Hello everyone.

This is just a small snippet which calls glIs****() functions repeatedly to count how many objects are currently active. The point of it is to help find memory leaks in your code that can be fatal when a game is played for a long time. It's a bit hacky and may not always be accurate. The number of checks for each object type is tweakable with the constants at the top of the code. If all tested objects are active, the code prints an extra "+" after the count to signal that the count is most likely higher. If a feature isn't supported by your graphics card, it prints out a count of -1 for that object type (untested).

Note that this code is only meant to be an indicator. It may not report the count correctly.
 - Let's say the object limit is 1000. You create 3000 objects and delete the first 1000. The count is reported as 0 since the first 1000 IDs weren't active objects.
 - Let's say the object limit is 1000. You create 3000 objects and delete the first 100. The count is reported as 900 (not 900+ or 1000+).

In addition, it may not even work at all if the driver doesn't assign IDs the same way as the Nvidia driver does, but it does work well enough on Nvidia drivers.

Sample output:
Quote
OpenGL object counts
    Textures:  79
    Samplers:  0
    FBOs:      30
    RBOs:      0
    Buffers:   58
    VAOs:      70
    Shaders:   145
    Programs:  67
    Pipelines: 0
    Queries:   740
    TransFeed: 0

Code:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
import org.lwjgl.opengl.ARBFramebufferObject;
import org.lwjgl.opengl.ARBOcclusionQuery;
import org.lwjgl.opengl.ARBSamplerObjects;
import org.lwjgl.opengl.ARBSeparateShaderObjects;
import org.lwjgl.opengl.ARBTransformFeedback2;
import org.lwjgl.opengl.ARBVertexArrayObject;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GLCapabilities;

public class ActiveOpenGLObjectCounter {
   
   private ActiveOpenGLObjectCounter() {}
   
   private static final int TEXTURE_TESTS = 1000;
   private static final int SAMPLER_TESTS = 1000;
   
   private static final int FRAMEBUFFER_TESTS = 100;
   private static final int RENDERBUFFER_TESTS = 10;

   
   private static final int BUFFER_TESTS = 10000;
   private static final int VAO_TESTS = 10000;
   
   private static final int SHADER_TESTS = 1000;
   private static final int PROGRAM_TESTS = 1000;
   private static final int PIPELINE_TESTS = 100;

   private static final int QUERY_TESTS = 10000;
   
   private static final int TRANSFORM_FEEDBACK_TESTS = 100;
   
   public static void dumpCounts(){
     
      int textures = 0;
      int samplers = 0;
     
      int framebuffers = 0;
      int renderbuffers = 0;
     
      int buffers = 0;
      int vaos = 0;
     
      int shaders = 0;
      int programs = 0;
      int pipelines = 0;
     
      int queries = 0;
     
      int transformFeedbacks = 0;
     
     

      GLCapabilities caps = GL.getCapabilities();
     
      for(int i = 1; i <= TEXTURE_TESTS; i++){
         if(GL11.glIsTexture(i)){
            textures++;
         }
      }
      if(caps.GL_ARB_sampler_objects){
         for(int i = 1; i <= SAMPLER_TESTS; i++){
            if(ARBSamplerObjects.glIsSampler(i)){
               samplers++;
            }
         }
      }else{
         samplers = -1;
      }
     
      if(caps.GL_ARB_framebuffer_object){
         for(int i = 1; i <= FRAMEBUFFER_TESTS; i++){
            if(ARBFramebufferObject.glIsFramebuffer(i)){
               framebuffers++;
            }
         }
         for(int i = 1; i <= RENDERBUFFER_TESTS; i++){
            if(ARBFramebufferObject.glIsRenderbuffer(i)){
               renderbuffers++;
            }
         }
      }else{
         framebuffers = -1;
         renderbuffers = -1;
      }

     
      if(caps.GL_ARB_vertex_buffer_object){
         for(int i = 1; i <= BUFFER_TESTS; i++){
            if(ARBVertexBufferObject.glIsBufferARB(i)){
               buffers++;
            }
         }
      }else{
         buffers = -1;
      }
      if(caps.GL_ARB_vertex_array_object){
         for(int i = 1; i <= VAO_TESTS; i++){
            if(ARBVertexArrayObject.glIsVertexArray(i)){
               vaos++;
            }
         }
      }else{
         vaos = -1;
      }
     
      if(caps.OpenGL20){
         for(int i = 1; i <= SHADER_TESTS; i++){
            if(GL20.glIsShader(i)){
               shaders++;
            }
         }
         for(int i = 1; i <= PROGRAM_TESTS; i++){
            if(GL20.glIsProgram(i)){
               programs++;
            }
         }
      }else{
         shaders = -1;
         programs = -1;
      }
      if(caps.GL_ARB_separate_shader_objects){
         for(int i = 1; i <= PIPELINE_TESTS; i++){
            if(ARBSeparateShaderObjects.glIsProgramPipeline(i)){
               pipelines++;
            }
         }
      }else{
         pipelines = -1;
      }

      if(caps.GL_ARB_occlusion_query){
         for(int i = 1; i <= QUERY_TESTS; i++){
            if(ARBOcclusionQuery.glIsQueryARB(i)){
               queries++;
            }
         }
      }else{
         queries = -1;
      }
      if(caps.GL_ARB_transform_feedback2){
         for(int i = 1; i <= TRANSFORM_FEEDBACK_TESTS; i++){
            if(ARBTransformFeedback2.glIsTransformFeedback(i)){
               transformFeedbacks++;
            }
         }
      }else{
         transformFeedbacks = -1;
      }
     

      System.out.println("OpenGL object counts");

      System.out.println("    Textures:  " + textures + (textures == TEXTURE_TESTS ? "+" : ""));
      System.out.println("    Samplers:  " + samplers + (samplers == SAMPLER_TESTS ? "+" : ""));

      System.out.println("    FBOs:      " + framebuffers + (framebuffers == FRAMEBUFFER_TESTS ? "+" : ""));
      System.out.println("    RBOs:      " + renderbuffers + (renderbuffers == RENDERBUFFER_TESTS ? "+" : ""));

      System.out.println("    Buffers:   " + buffers + (buffers == BUFFER_TESTS ? "+" : ""));
      System.out.println("    VAOs:      " + vaos + (vaos == VAO_TESTS ? "+" : ""));

      System.out.println("    Shaders:   " + shaders + (shaders == SHADER_TESTS ? "+" : ""));
      System.out.println("    Programs:  " + programs + (programs == PROGRAM_TESTS ? "+" : ""));
      System.out.println("    Pipelines: " + pipelines + (pipelines == PIPELINE_TESTS ? "+" : ""));
     
      System.out.println("    Queries:   " + queries + (queries == QUERY_TESTS ? "+" : ""));
     
      System.out.println("    TransFeed: " + transformFeedbacks + (transformFeedbacks == TRANSFORM_FEEDBACK_TESTS ? "+" : ""));

      System.out.println();
     
   }
}

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (587 views)
2019-09-04 15:33:17

hadezbladez (5533 views)
2018-11-16 13:46:03

hadezbladez (2411 views)
2018-11-16 13:41:33

hadezbladez (5794 views)
2018-11-16 13:35:35

hadezbladez (1233 views)
2018-11-16 13:32:03

EgonOlsen (4669 views)
2018-06-10 19:43:48

EgonOlsen (5688 views)
2018-06-10 19:43:44

EgonOlsen (3205 views)
2018-06-10 19:43:20

DesertCoockie (4104 views)
2018-05-13 18:23:11

nelsongames (5125 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!