Show Posts
|
|
Pages: [1]
|
|
1
|
Game Development / Performance Tuning / Properly using VolatileImage
|
on: 2011-03-24 07:46:28
|
I've been reading about VolatileImage but I'm not sure I know how to use it properly yet, so I was hoping somebody could look at this program I wrote. Is there anything I should be checking but aren't? Things that I'm checking but I shouldn't? If it means anything, the picture I'm trying to draw is translucent. Also, I read I should use sun.java2d.trace to detect software rendering, but I don't quite understand it. I set it to "count" and I see the following when my program ends. 1 call to sun.java2d.loops.Blit$GeneralMaskBlit::Blit(FourByteAbgr, SrcNoEa, IntArgbPre) 101 calls to D3DFillRect 1 call to sun.java2d.d3d.D3DSurfaceToGDIWindowSurfaceBlit::Blit("D3D Surface", AnyAlpha, "GDI") 89 calls to sun.java2d.d3d.D3DRTTSurfaceToSurfaceBlit::Blit("D3D Surface (render-to-texture)", AnyAlpha, "D3D Surface") 1 call to sun.java2d.d3d.D3DGeneralBlit::Blit(Any, AnyAlpha, "D3D Surface") 3 calls to sun.java2d.loops.FillRect::FillRect(AnyColor, SrcNoEa, AnyInt) 1 call to sun.java2d.loops.MaskBlit::MaskBlit(IntArgb, AnyAlpha, IntArgbPre) 1 call to sun.java2d.windows.GDIBlitLoops::Blit(IntRgb, SrcNoEa, "GDI") 1 call to sun.java2d.loops.MaskBlit$General::MaskBlit(FourByteAbgr, SrcNoEa, IntArgbPre) 1 call to sun.java2d.loops.Blit::Blit(FourByteAbgr, SrcNoEa, IntArgb) 200 total calls to 10 different primitives I've assumed that "sun.java2d.loops" indicates software rendering but...well I'm not sure what to do about that. 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
| import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.VolatileImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.Timer;
public class Main implements ActionListener { static final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); static final GraphicsDevice gd = ge.getDefaultScreenDevice(); static final GraphicsConfiguration gc = gd.getDefaultConfiguration(); static BufferedImage bufferedImage; static VolatileImage image; static BufferStrategy bs; static JFrame frame;
public static void fillImage(){ Graphics2D g = image.createGraphics(); g.setComposite(AlphaComposite.Src); g.drawImage(bufferedImage, null, 0, 0); g.dispose(); }
public static void main(String[] args) throws IOException { String os = System.getProperty("os.name"); System.setProperty("sun.java2d.trace", "count"); if(os.contains("Windows")){ System.setProperty("sun.java2d.d3d", "True"); }else{ System.setProperty("sun.java2d.opengl", "True"); } bufferedImage = ImageIO.read(new File("fig6.png")); do{ image = gc.createCompatibleVolatileImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getTransparency()); }while(image.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE); fillImage();
frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); frame.setIgnoreRepaint(true); frame.createBufferStrategy(2); bs = frame.getBufferStrategy(); frame.setSize(500, 500); frame.setLocationRelativeTo(null); new Timer(16, new Main()).start(); }
public void actionPerformed(ActionEvent e) { while(image.validate(gc) != VolatileImage.IMAGE_OK){ while(image.validate(gc) == VolatileImage.IMAGE_INCOMPATIBLE || image.contentsLost()){ image = gc.createCompatibleVolatileImage(bufferedImage.getWidth(), bufferedImage.getHeight(), bufferedImage.getTransparency()); } fillImage(); } do{ Graphics g = bs.getDrawGraphics(); g.setColor(Color.black); g.fillRect(frame.getInsets().left, frame.getInsets().top, frame.getContentPane().getWidth(), frame.getContentPane().getHeight()); g.drawImage(image, 0, 0, null); g.dispose(); }while(bs.contentsLost()); bs.show(); } } |
|
|
|
|
|
2
|
Java Game APIs & Engines / JOGL Development / Re: JOGL says FBOs are unsupported
|
on: 2010-04-14 03:25:07
|
This is my latest rewrite, now I'm getting GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT, I wasn't sure what kind of format to try so I just made the texture before the FBO and I've attached a depth and stencil buffer. I'd hate to sound lazy but would anyone spare a minimal FBO example of their own? 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
| package org.yourorghere;
import java.nio.ByteBuffer; import java.nio.IntBuffer; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU;
public class GLRenderer implements GLEventListener { private final int texture = 0, frame = 1, depth = 2, stencil = 3; private IntBuffer buffer;
public void init(GLAutoDrawable drawable) { GL gl = drawable.getGL(); System.err.println("INIT GL IS: " + gl.getClass().getName());
gl.setSwapInterval(1);
buffer = IntBuffer.allocate(4);
buffer.position(texture); gl.glGenTextures(1, buffer); gl.glBindTexture( gl.GL_TEXTURE_2D, buffer.get(texture)); ByteBuffer pixels = ByteBuffer.allocate(512*512*4); gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA8, 512, 512, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixels);
buffer.position(depth); gl.glGenRenderbuffersEXT(1, buffer); gl.glBindRenderbufferEXT(gl.GL_RENDERBUFFER_EXT, buffer.get(depth)); gl.glRenderbufferStorageEXT(gl.GL_RENDERBUFFER_EXT, gl.GL_DEPTH_COMPONENT, 512, 512);
buffer.position(stencil); gl.glGenRenderbuffersEXT(1, buffer); gl.glBindRenderbufferEXT(gl.GL_RENDERBUFFER_EXT, buffer.get(stencil)); gl.glRenderbufferStorageEXT(gl.GL_RENDERBUFFER_EXT, gl.GL_STENCIL_INDEX, 512, 512);
buffer.position(frame); gl.glGenFramebuffersEXT(1, buffer); gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, buffer.get(frame)); gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, buffer.get(texture), 0); gl.glFramebufferRenderbufferEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_DEPTH_ATTACHMENT_EXT, gl.GL_RENDERBUFFER_EXT, buffer.get(depth)); gl.glFramebufferRenderbufferEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_STENCIL_ATTACHMENT_EXT, gl.GL_RENDERBUFFER_EXT, buffer.get(stencil));
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH); System.out.println(gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT));
gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity();
gl.glOrtho(0, 512, 512, 0, -1, 1);
gl.glMatrixMode(gl.GL_MODELVIEW); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity();
gl.glBegin(gl.GL_TRIANGLES); gl.glColor3f(1, 0, 0); gl.glVertex2f(256, 0); gl.glColor3f(0, 1, 0); gl.glVertex2f(512, 512); gl.glColor3f(0, 0, 1); gl.glVertex2f(0, 512); gl.glEnd();
gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); GLU glu = new GLU();
if (height <= 0) { height = 1; } final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); }
public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
gl.glBegin(GL.GL_TRIANGLES); gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glColor3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glEnd();
gl.glTranslatef(3.0f, 0.0f, 0.0f); gl.glBegin(GL.GL_QUADS); gl.glColor3f(0.5f, 0.5f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glEnd();
gl.glFlush(); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } } |
|
|
|
|
|
3
|
Java Game APIs & Engines / JOGL Development / Re: JOGL says FBOs are unsupported
|
on: 2010-04-11 05:20:57
|
I've made that change but I still get the same FBO status. I then tried passing a ByteBuffer instead of null to glTexImage2D but that didn't change anything. 1 2
| ByteBuffer pixels = ByteBuffer.allocate(512*512*4); gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA8, 512, 512, 0, gl.GL_RGBA, gl.GL_UNSIGNED_BYTE, pixels); |
|
|
|
|
|
4
|
Java Game APIs & Engines / JOGL Development / JOGL says FBOs are unsupported
|
on: 2010-04-10 12:32:39
|
First, I got the JOGL plugin for NetBeans, started a stock project, and put the FBO code into the following class, which I kind "Javaized" from here http://www.songho.ca/opengl/gl_fbo.htmlNow, I can run the example at that web page, under the OpenGL Capabilities that Netbeans says I have is GL_ARB_framebuffer_object, and I have a NVIDIA 8800 GT that I figured would be high end enough. But the following line prints 36061, which according to the documents is GL_FRAMEBUFFER_UNSUPPORTED_EXT. System.out.println(gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT)); Have I done anything wrong here? Class with my FBO 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
| package org.yourorghere;
import java.nio.IntBuffer; import javax.media.opengl.GL; import javax.media.opengl.GLAutoDrawable; import javax.media.opengl.GLEventListener; import javax.media.opengl.glu.GLU;
public class GLRenderer implements GLEventListener { private final int texture = 0, frame = 1; private IntBuffer buffer;
public void init(GLAutoDrawable drawable) {
GL gl = drawable.getGL(); System.err.println("INIT GL IS: " + gl.getClass().getName());
gl.setSwapInterval(1);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH);
buffer = IntBuffer.allocate(2);
buffer.position(frame); gl.glGenFramebuffersEXT(1, buffer); gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, buffer.get(frame));
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH);
buffer.position(texture); gl.glGenTextures(1, buffer); gl.glBindTexture( gl.GL_TEXTURE_2D, buffer.get(texture)); gl.glTexImage2D(gl.GL_TEXTURE_2D, 0, gl.GL_RGBA, 512, 512, 0, gl.GL_RGBA, gl.GL_INT, null);
gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT, gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, buffer.get(texture), 0);
System.out.println(gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT));
gl.glMatrixMode(gl.GL_PROJECTION); gl.glLoadIdentity();
gl.glOrtho(0, 512, 512, 0, -1, 1);
gl.glMatrixMode(gl.GL_MODELVIEW); gl.glLoadIdentity();
gl.glBegin(gl.GL_TRIANGLES); gl.glColor3f(1, 0, 0); gl.glVertex2f(256, 0); gl.glColor3f(0, 1, 0); gl.glVertex2f(512, 512); gl.glColor3f(0, 0, 1); gl.glVertex2f(0, 512); gl.glEnd();
gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); gl.glShadeModel(GL.GL_SMOOTH); }
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { GL gl = drawable.getGL(); GLU glu = new GLU();
if (height <= 0) { height = 1; } final float h = (float) width / (float) height; gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL.GL_PROJECTION); gl.glLoadIdentity(); glu.gluPerspective(45.0f, h, 1.0, 20.0); gl.glMatrixMode(GL.GL_MODELVIEW); gl.glLoadIdentity(); }
public void display(GLAutoDrawable drawable) { GL gl = drawable.getGL();
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); gl.glLoadIdentity();
gl.glTranslatef(-1.5f, 0.0f, -6.0f);
gl.glBegin(GL.GL_TRIANGLES); gl.glColor3f(1.0f, 0.0f, 0.0f); gl.glVertex3f(0.0f, 1.0f, 0.0f); gl.glColor3f(0.0f, 1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glColor3f(0.0f, 0.0f, 1.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glEnd();
gl.glTranslatef(3.0f, 0.0f, 0.0f); gl.glBegin(GL.GL_QUADS); gl.glColor3f(0.5f, 0.5f, 1.0f); gl.glVertex3f(-1.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, 1.0f, 0.0f); gl.glVertex3f(1.0f, -1.0f, 0.0f); gl.glVertex3f(-1.0f, -1.0f, 0.0f); gl.glEnd();
gl.glFlush(); }
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { } } |
And if you need it, the main class that NetBeans made: 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
| package org.yourorghere;
import com.sun.opengl.util.Animator; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.logging.Level; import java.util.logging.Logger; import javax.media.opengl.GLCanvas; import javax.media.opengl.GLCapabilities; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPopupMenu; import javax.swing.LayoutStyle.ComponentPlacement; import javax.swing.UIManager; import javax.swing.WindowConstants;
public class FBOtest extends JFrame {
static { JPopupMenu.setDefaultLightWeightPopupEnabled(false); } private Animator animator;
public FBOtest() { initComponents(); setTitle("Simple JOGL Application");
canvas.addGLEventListener(new GLRenderer()); animator = new Animator(canvas);
canvas.setMinimumSize(new Dimension()); this.addWindowListener(new WindowAdapter() {
@Override public void windowClosing(WindowEvent e) { new Thread(new Runnable() {
public void run() { animator.stop(); System.exit(0); } }).start(); } }); }
@Override public void setVisible(boolean show){ if(!show) animator.stop(); super.setVisible(show); if(!show) animator.start(); }
@SuppressWarnings("unchecked") private void initComponents() { JLabel label = new JLabel(); canvas = new GLCanvas(createGLCapabilites());
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label.setText("Below you see a GLCanvas");
GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(Alignment.LEADING) .addComponent(canvas, GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) .addComponent(label)) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(label) .addPreferredGap(ComponentPlacement.RELATED) .addComponent(canvas, GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE) .addContainerGap()) );
pack(); }
private GLCapabilities createGLCapabilites() { GLCapabilities capabilities = new GLCapabilities(); capabilities.setHardwareAccelerated(true);
capabilities.setNumSamples(2); capabilities.setSampleBuffers(true); return capabilities; } public static void main(String args[]) { EventQueue.invokeLater(new Runnable() { public void run() {
try{ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception ex) { Logger.getLogger(getClass().getName()).log(Level.INFO, "can not enable system look and feel", ex); }
FBOtest frame = new FBOtest(); frame.setVisible(true); } }); }
private GLCanvas canvas;
} |
|
|
|
|
|