Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Transition from PBuffers to FBOs breaks shaders [solved]  (Read 2719 times)
0 Members and 1 Guest are viewing this topic.
Offline Funkapotamus

Junior Member




wo shuo han yu


« Posted 2006-02-27 07:58:26 »

Just as the title implies, I've transitioned from PBuffers to FBOs in my engine.

I can draw to a FBO and render to a texture just fine.  Drawing the texture is no problem either.  I get problems however, when I try to send the FBO's texture ID to a shader via uniform variable.  The shader is receiving the texture ID as a sampler2D.  The result is blank pixels. 

If I mess with the texture format of the FBO, I am able to get the shader to spit out pixels *somewhat* representative of what it should.  For example, if I draw a blank green screen to the FBO, a piece of the shader's ouput will be green.  I can change the color and the end result will follow suit.  This tells me that the texture format the shader expects is different than what's created by the FBO.  Unfortunatly, this is the point where things are over my head.

Anybody care to share their FBO implementation who's resulting texture can be correctly fed into a sampler2D?

FYI: Radeon 9800, Driver Date 3rd of October 2005, WinXPsp2
Online Spasi
« Reply #1 - Posted 2006-02-27 11:08:59 »

I have no such problems with FBO. If no errors are raised during FBO setup and rendering, you should be able to use the result as a normal texture.

What you're describing sounds like bad texture coordinates used when rendering to the FBO or when using the texture. So, first make sure everything is fine wrt texcoords (e.g. bind another texture that you're sure is ok). If that doesn't work, try newer drivers, force an RGBA format (most supported) or try setting GL_NEAREST min/mag filters (GL_LINEAR is problematic sometimes with FBO and certain texture formats).
Offline Funkapotamus

Junior Member




wo shuo han yu


« Reply #2 - Posted 2006-02-27 14:24:07 »

Thanks for the suggestion.  That's odd how GL_NEAREST can mess things up.  I'll have to test things when I get home.  Also, I've noticed that the OpenGL specification sets up the texture like this:

1  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, width, height, 0, GL_RGB, GL_INT, NULL);


The difference from LWJGL is the NULL final parameter.  LWJGL of course, doesn't like this, so I do the following:

1  
2  
ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);


Could this perhaps be the issue?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Spasi
« Reply #3 - Posted 2006-02-27 14:53:29 »

That's odd how GL_NEAREST can mess things up.

Yeah, some GPUs don't like GL_LINEAR, especially with fp texture formats. Anyway, that's probably not your problem, it would result in an incomplete FBO status.

The difference from LWJGL is the NULL final parameter.  LWJGL of course, doesn't like this

You actually can pass a null parameter. Try this:

1  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer)null);
Offline Funkapotamus

Junior Member




wo shuo han yu


« Reply #4 - Posted 2006-02-27 22:34:06 »

GL_LINEAR->GL_NEAREST has no effect.  Updating drivers seems to cause more problems than good- the entire display slows down on a whole (Even in windows!  Wierd!)

I'll just post my code to see if there's anything wrong:

FBO Initialize
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
EXTFramebufferObject.glGenFramebuffersEXT(fbID);
EXTFramebufferObject.glGenRenderbuffersEXT(rbID);
EXTFramebufferObject.glBindRenderbufferEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, fbID.get(0));
EXTFramebufferObject.glRenderbufferStorageEXT(EXTFramebufferObject.GL_RENDERBUFFER_EXT, GL11.GL_DEPTH_COMPONENT, width, height);

GL11.glGenTextures(intBuffer);
textureID = intBuffer.get(0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, textureID);
GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, (ByteBuffer)null);
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST );
GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST );


Enable FBO
1  
2  
3  
EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, fbID.get(0));
EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, textureID, 0 );
EXTFramebufferObject.glFramebufferRenderbufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_DEPTH_ATTACHMENT_EXT, EXTFramebufferObject.GL_RENDERBUFFER_EXT, rbID.get(0) );
   

Pseudocode of what I Do
1  
2  
3  
4  
5  
6  
7  
enableFBO();
// Draw Some Random Scene
disableFBO();

enableShader(); // Set the shader's sampler2D to the FBO's texture.
// Draw Quad
disableShader();


Like I said, if I skipt he shader part and instead bind the FBO's texture via glBindTexture() the quad is drawn fine with the random scene's result.  Ugh.
Online Spasi
« Reply #5 - Posted 2006-02-28 02:28:49 »

The FBO code seems fine. Could you please post the shader code? The shader setup and draw code could help too.
Offline Funkapotamus

Junior Member




wo shuo han yu


« Reply #6 - Posted 2006-02-28 02:51:12 »

Sure thing.

Shader Enable
1  
2  
3  
4  
5  
6  
7  
ARBShaderObjects.glUseProgramObjectARB(programID);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, Engine.fbo.getTextureID()); // Quick and dirty- fbo is static in the engine.
GL11.glEnable(GL11.GL_TEXTURE_2D);
ARBShaderObjects.glUniform1iARB(texture1, 0); //texture1 = getUniformLocation(programID, "tex");
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glDisable(GL11.GL_TEXTURE_2D);


Fragment Shader
1  
2  
3  
4  
5  
6  
7  
8  
9  
uniform sampler2D tex;

void main()
{
vec2 verts = gl_TexCoord[0].xy;
vec4 color = texture2D(tex, verts);
color += color;
gl_FragColor = color;
}


Vertex Shader
1  
2  
3  
4  
5  
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = ftransform();
}


Drawing
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
fbo.enable();
   float width = 512.0f;
        float height = 512.0f;
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, RANDOMTEXTUREID);  // Texture ID of some picture      
  GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex3f(-width/2, -height/2, 0.0f);     GL11.glTexCoord2f(0.0f, 0.0f);
      GL11.glVertex3f(width/2, -height/2, 0.0f);   GL11.glTexCoord2f(1.0f, 0.0f);
      GL11.glVertex3f(width/2, height/2, 0.0f);   GL11.glTexCoord2f(1.0f, 1.0f);
      GL11.glVertex3f(-width/2, height/2, 0.0f);   GL11.glTexCoord2f(0.0f, 1.0f);      
   GL11.glEnd();
fbo.disable();

// Note: Enabling the fbo texture here does nothing
shader.enable();
// Note: Enabling here does nothing either
   GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex3f(-width/2, -height/2, 0.0f);     GL11.glTexCoord2f(0.0f, 0.0f);
      GL11.glVertex3f(width/2, -height/2, 0.0f);   GL11.glTexCoord2f(1.0f, 0.0f);
      GL11.glVertex3f(width/2, height/2, 0.0f);   GL11.glTexCoord2f(1.0f, 1.0f);
      GL11.glVertex3f(-width/2, height/2, 0.0f);   GL11.glTexCoord2f(0.0f, 1.0f);      
   GL11.glEnd();
shader.disable();


I think that's everything.  I simplified parts that would (i should hope not!) have no effect on things(translates, rotates).  Thanks for your help on this by the way. 
Offline Funkapotamus

Junior Member




wo shuo han yu


« Reply #7 - Posted 2006-02-28 04:12:09 »

Using updated drivers now - same effect :/
Online Spasi
« Reply #8 - Posted 2006-02-28 15:38:43 »

Everything looks fine again. With the above drawing code and if no GL errors are raised, you should have seen the contents of the RANDOMTEXTUREID texture. If not, then you're doing something wrong with the shader and the problem has nothing to do with FBO. If you can see RANDOMTEXTUREID, but not (the same result) when enabling the FBO texture, then there's something wrong with FBO indeed.

Other notes:

    - You don't need to bind the texture when you're setting the sampler2D uniform (in the Shader Enable code).
    - Have you tried binding any other texture? Does it get drawn with the shader enabled?
    - Have you tried outputting a debug color in the shader? Does it get drawn correctly?
Online Spasi
« Reply #9 - Posted 2006-02-28 15:41:16 »

Hehe, just noticed that you're calling glTexCoord2f after glVertex3f! In immediate mode, glVertex3f triggers the vertex submission to the pipeline. So, basically, each vertex is using completely wrong texcoords!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Funkapotamus

Junior Member




wo shuo han yu


« Reply #10 - Posted 2006-02-28 21:43:21 »

Well, I solved the issue, but you'll never guess what it was...

I was disabling GL_TEXTURE_2D in a line of code I overlooked.    Shocked
All I had to do was get rid of that line... everything functions normally.  Silly how I can be dealing with shaders and FBOs and all these other things... but the small stuff still catches me off guard.

Yep.  I'm a genious.   Undecided Roll Eyes

Side note: I still use glVertex3f and glTexCoord2f- doesn't seem to mess things up.
Sorry to waste your time Spasi, I appreciate your help.  Good work on the FBO context too, this will really speed things up from PBuffers.
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.

BurntPizza (14 views)
2014-09-19 03:14:18

Dwinin (31 views)
2014-09-12 09:08:26

Norakomi (58 views)
2014-09-10 13:57:51

TehJavaDev (79 views)
2014-09-10 06:39:09

Tekkerue (40 views)
2014-09-09 02:24:56

mitcheeb (62 views)
2014-09-08 06:06:29

BurntPizza (45 views)
2014-09-07 01:13:42

Longarmx (30 views)
2014-09-07 01:12:14

Longarmx (36 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
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!