Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
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 transform feedback  (Read 3577 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2012-11-12 00:51:15 »

Transform feedback is a relatively new feature in OpenGL. When OpenGL was first created it was only possible to render to a built-in frame buffer. When OGL2 came out, we got framebuffer objects (through extensions) which allow us to render to a texture. With OGL3 we get transform feedback (also through extensions), which allows us to render vertices to a vertex buffer object. Combined with other OGL3 features like geometry shaders and instancing, we can offload a huge amount of work from the CPU to the GPU. This tutorial won't be implementing anything really useful. Rather, it's meant to quickly explain how to set up and use transform feedback.

In short, transform feedback allows you to capture the raw output of the geometry shader (or vertex shader if there is no geometry shader). Note that fragments are still generated and the fragment shader is still used. That means that it's possible to for example both render to a transform feedback VBO while at the same time rendering to multiple FBOs. This might be useful in some cases, but you usually just want to do render to one or the other. Therefore there is a new GL state called GL_RASTERIZER_DISCARD. By enabling GL_RASTERIZER_DISCARD, the OGL pipeline is terminated after the geometry shader and no fragments are processed. This allows you to do much more generic computing since you can just render from a VBO and output directly to another VBO. The output VBO can then later be used for something else.

What you need to get started with transform feedback is:
 - At least a vertex shader and maybe a geometry shader.
 - A feedback object (similar to a texture object or a framebuffer object).
 - A VBO to render to.

In this example I will create a minimal transform feedback program which "renders" a number of points through transform feedback, then draws the processed points on the screen. This specific program culls points that are near the edge of the screen:



The basic structure of the program is as following:
 - Process the points through transform feedback. The geometry shader only outputs the point again if its x and y coordinates are between -0.5 and 0.5.
 - The processed points are then rendered to the screen.

It's obviously a pretty useless program. A similar much more realistic scenario would be to do the frustum culling of thousands of 3D models using transform feedback. You'd draw "points" representing each object, check if that point (with a radius) passes the frustum test in the geometry shader and only output the it if it does. You can then use instancing to draw the 3D model X times, where X is the number of points passed that passed the test, using the points that passed to position each instance.

First we'll take a look at the shaders. The vertex shader simply passes through the position of the point to the geometry shader:
1  
2  
3  
4  
5  
6  
7  
8  
9  
#version 330

layout(location = 0) in vec2 position;

out vec2 tPosition;

void main(){
   tPosition = position;
}


The geometry shader only outputs the point if it passes the if-statement:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
#version 330

layout(points) in;
layout(points, max_vertices = 1) out;

in vec2[] tPosition;

out vec2 outPosition;

void main() {
   vec2 pos = tPosition[0];
   if(pos.x > -0.5 && pos.x < 0.5 && pos.y > -0.5 && pos.y < 0.5){
      outPosition = pos;
      EmitVertex();
      EndPrimitive();
   }
}


Finally we have a few Java code highlights...

Loading the shader. Notice the (unmissable) call to glTransformFeedbackVaryings(); before linking.
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  
   private void initShader() {
     
      shaderProgram = glCreateProgram();
     
      int vertexShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShader, loadFileSource("shaders/tf/test.vert"));
      glCompileShader(vertexShader);
      glAttachShader(shaderProgram, vertexShader);
     
      int geometryShader = glCreateShader(GL_GEOMETRY_SHADER);
      glShaderSource(geometryShader, loadFileSource("shaders/tf/test.geom"));
      glCompileShader(geometryShader);
      glAttachShader(shaderProgram, geometryShader);
     
      //This line tells the shader which output attributes from the geometry shader
     //we want to save to the transform feedback output VBO. It's an array of varying
     //names followed by an enum controlling how we want the data to be stored.
     //GL_INTERLEAVED_ATTRIBS tells OpenGL to put them in the same VBO ordered in
     //the way specified by the array.
     //It's very important to call this BEFORE linking the program.
     glTransformFeedbackVaryings(shaderProgram, new CharSequence[]{"outPosition"}, GL_INTERLEAVED_ATTRIBS);
     
      //Note that we don't even have a fragment shader for this shader program.
     
      glLinkProgram(shaderProgram);
        String log = glGetProgramInfoLog(shaderProgram, 65536);
        if(log.length() != 0){
            System.out.println("Program link log:\n" + log);
        }
       
        //Save the input variable location
       positionLocation = glGetAttribLocation(shaderProgram, "position");
       
   }


Initializing transform feedback.
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  
   private void initTransformFeedback() {

      //This is the buffer we fill with random points to process.
     inputData = BufferUtils.createFloatBuffer(NUM_POINTS * 2);
      //And the VBO which we upload the data to.
     inputVBO = glGenBuffers();
     
      //This is the data in which the processed points will end up.
     //We make it big enough to fit all input points, in case all
     //of them pass. If the buffer is filled, additional data will
     //simply be discarded.
     outputVBO = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
      glBufferData(GL_ARRAY_BUFFER, NUM_POINTS * 2 * 4, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
      //We create our transform feedback object. We then bind it and
     //tell it to store its output into outputVBO.
     feedbackObject = glGenTransformFeedbacks();
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackObject);
        glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, outputVBO);
        glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);
       
        //We also create a query object. This object will be used to
       //query how many points that were stored in the output VBO.
       queryObject = glGenQueries();
   }


Point processing with transform feedback:
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  
   private void processPoints() {
     
      //Disable pixel rendering, we're doing transform feedback baby!
     glEnable(GL_RASTERIZER_DISCARD);
     
      //Bind the shader...
     glUseProgram(shaderProgram);
     
      //and then the feedback object
     glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, feedbackObject);
      glBeginTransformFeedback(GL_POINTS);
      {
         //Between glBeginTransformFeedback(GL_POINTS) and glEndTransformFeedback()
        //we can of course only draw points.
       
         //Bind and update the input data VBO.
        glBindBuffer(GL_ARRAY_BUFFER, inputVBO);
         glBufferData(GL_ARRAY_BUFFER, inputData, GL_STREAM_DRAW);
         
         //Enable our only shader input attribute.
        glEnableVertexAttribArray(positionLocation);
         glVertexAttribPointer(positionLocation, 2, GL_FLOAT, false, 0, 0);

         //Draw the points with a standard glDrawArrays() call, but wrap it in
        //a query so we can determine exactly how many points that were stored
        //in outputVBO.
        //WARNING: Querying is VERY SLOW and is only done so we can write out
        //how many points that passed to the console! It's possible to draw
        //all points that passed without a query! See renderOutput()!
        glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, queryObject);
         glDrawArrays(GL_POINTS, 0, NUM_POINTS);
         glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN);
         System.out.println("Points drawn: " + glGetQueryObjecti(queryObject, GL_QUERY_RESULT));

         //Clean up after us...
        glDisableVertexAttribArray(positionLocation);
         glBindBuffer(GL_ARRAY_BUFFER, 0);
         
      }
      glEndTransformFeedback();

      glUseProgram(0);

      glDisable(GL_RASTERIZER_DISCARD);
   }


And finally rendering the output:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
   private void renderOutput() {
     
      //Bind the outputVBO just like any other VBO
     glBindBuffer(GL_ARRAY_BUFFER, outputVBO);
     
      //We're using the fixed functionality pipeline here,
     //so just set it up to read positions from the outputVBO.
     glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, 0);

      //glDrawTransformFeedback is a special draw call that is very similar to
     //glDrawArrays(). It allows us to draw all points that were output by our
     //shader without having to involve the CPU to determine how many they were.
     //This is the same as glDrawArrays(GL_POINTS, 0, num_points_that_passed);,
     //but is a LOT faster than if we had used a query object to get the count
     //and then manually calling glDrawArrays() with that count.
     glDrawTransformFeedback(GL_POINTS, feedbackObject);
     
      //Clean up...
     glDisableClientState(GL_VERTEX_ARRAY);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
     
   }


And finally the full Java source code: http://www.java-gaming.org/?action=pastebin&id=309

I'll leave it as a reader's exercise to create a particle engine with transform feedback. Hint: You'll need to use two transform feedback objects and output VBOs and pingpong between them to process the particles.

Myomyomyo.
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-11-12 23:48:26 »

How would you do something like this without a geometry shader?

Looks like I need to start learning how they work Cheesy

Offline theagentd
« Reply #2 - Posted 2012-11-13 00:32:25 »

Easy, just don't attach a geometry shader and pass the vertex shader output attributes you want to glTransformFeedbackVaryings() instead. It's not very useful without a geometry shader, though. The whole point is the ability to output a varying number of primitives since that allows you do stuff like culling (only output visible stuff) and particle updating (only output surviving particles). It can of course also be used to generate geometry that needs to be used more than once. On OGL4 hardware, a common usage is to catch hardware tessellated geometry. This geometry can then be used to render shadow maps etc to ensure that the same objects are tessellated the same from all view angles.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (21 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (56 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (29 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (65 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59: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!