Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (767)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (854)
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  
  Instance specific data  (Read 2160 times)
0 Members and 1 Guest are viewing this topic.
Offline theagentd
« Posted 2011-12-06 15:14:25 »

I want to draw 1000 to 2000 circles, each with its own 2D location and radius. I am going to use instancing to draw a mesh approximating a circle (16 vertices or something), but I obviously need instance specific data for position and radius. I've found some info on UBOs, Uniform Buffer Objects, that seem to be perfect for instance specific data (and skinning, among other things). I'd then use the gl_InstanceID in my shader to fetch the data, something similar to this:

1  
gl_Position = position[gl_InstanceID] + circleVertex * radius[gl_InstanceID];

and then normalize the coordinates to screen coordinates.

This article provided good info on defining the uniform buffer stuff in the shader, but almost no info at all on creating and setting up the actual buffer with OpenGL commands in my program, nor any information on actually accessing the data in the vertex shader, so my code above is psuedocode until proven otherwise.  Tongue I can't find any tutorials or even a working example, some I'm pretty much shooting blindly with my experimenting at the moment. I will obviously post a working example when (if?) I get it working.

I know you guys usually stick to your glBegins and vertex arrays, but please help me out here! (joke) xDDDDDD

Myomyomyo.
Offline Spasi
« Reply #1 - Posted 2011-12-06 15:42:11 »

The LWJGL test package contains a sample that uses UBO. See org.lwjgl.test.opengl.shaders.ShaderUNI.

I think you'll find that instancing will be quite slow for the use case you're interested in. It's more useful when you have complex geometry (1000s of triangles) and a moderate amount of instances (a few 100s max). For circles/sprites, it'd be better to generate the geometry for all instances offline, then animate using transform feedback or OpenCL. Give it a go with instancing though, I might be wrong.
Offline theagentd
« Reply #2 - Posted 2011-12-07 11:39:45 »

I abandoned UBOs, as they require you to specify an array size in your SHADERS, which looks terrible and sucks. I instead went with ARB_instanced_arrays (see the last method, the code is so clear that it's scary). I ended up with a normal VBO for the circle positions drawing pizza slices to minimize the number of fragments that gets to the fragment shader. I then had a separate VBO with the instance specific data available using glVertexAttribDivisor().

My drawing is basically:
 1. Bind and map instance specific data buffer.
 2. Put instance specific data in buffer.
 3. Bind texture(s), VAO and shader
 4. glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, SUBDIVISIONS + 2, num_circles);

Speed at 1000 circles with radius=10, each made of 12 slices ( = 14 vertices as you can see above): 920 FPS
With my fragment shader which does 2D raytracing with about 20 samples for each pixel, this drops to 720 FPS. This is still more than a 10x speedup compared to my old algorithm, but that algorithm was CPU bound (too many draw calls). This one is GPU-bound and barely uses any CPU at all (just loading 3000 floats in a buffer), which is insanely important for my game. All in all, it's ridiculously fast compared to my earlier algorithm.

The goal was to reduce the CPU-load of my earlier algorithm, which is what instancing is all about in the first place. It's not about getting higher vertex performance or fill-rate (but it still can improve it), it's about reducing the number of draw calls from a varying number N=num_instances to a constant.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

nelsongames (1107 views)
2018-04-24 18:15:36

nelsongames (1339 views)
2018-04-24 18:14:32

ivj94 (2081 views)
2018-03-24 14:47:39

ivj94 (1232 views)
2018-03-24 14:46:31

ivj94 (2231 views)
2018-03-24 14:43:53

Solater (800 views)
2018-03-17 05:04:08
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

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
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!