Newbie OpenGL ES 2.0 Math question
After resisting the mighty power of ES 2.0 for a while, I finally give up myself and try to swallow as much as my mouth can bite. Here are my questions. According to few good books and examples, my vertex shader cannot see the connection between vertices but the fragment shader does.
Then
1) is it safe to say I can perform all the OpenGL math (e.g. matrix calculation, vector manipulation, and et开发者_开发百科c) in the fragment shader in order to be GPU-bound? ( It seems to me that all the examples still perform 3d math in CPU side just before feeding vertex data to shader. )
2.a) If yes, should I just dump all my ES 1.1 math lib and write a new one in GLSL?
2.b) If yes, will fragment shader handle bone animation?
2.c) If yes, where should I put cullings? Let's say I'd like to apply AABB and Frustum. To me, the most logical spot to place such cullings would still be at CPU side b/c that will eventually reduce amount of vertices moving into shader. No?
The golden rule is don't do operations that are unneccessary! That's why you compute your transformation matrix on the CPU (as it doesn't change per fragment or even per vertex). So you only need to multiply every vertex by a single matrix (one matrix operation on the CPU is often still better than thousands on the GPU). Just moving everything into the fragment shader to make the application GPU limited is a bad idea. Of course, if you do many unneccessary operations per fragment you will probably be GPU limited, but for what, if avoiding these comptutations gains you more overall performance with the same results?
Keep in mind, that simple 4x4 matrix operations have a neglibable cost on the CPU, too, as they are done at worst once per object. Bone animation (by vertex blending) is another thing. This is usually done in the vertex shader, as the transformation changes per-vertex. But again moving these computations into the fragment shader gains you nothing except a probably lower performance.
Keep in mind that things like frustum culling only reduce per-vertex operations (and draw calls in general), as after vertex shader and primitive assembly (and a possible geometry shader) the primitves are clipped against the view frustum anyway. So concentrate on large scale culling on the CPU side, like frustum culling and possibly occlusion culling (which also reduces overdraw and therefore fragment shader invocations). But like Matias said, the overhead needs to be balanced against the overall performance gain.
1) It is safe and works? Yes.
2a) You should do it? Maybe, GPU performance depends on balance between CPU and GPU workload, so moving everything into the GPU might slow down everything. Putting some calculations on the CPU reduces the GPU workload. Balance it, and profile before making such big decisions. Most games have a CPU math lib because of this.
2b) No, that's usually handled by the Vertex Shader.
2c) Depends on the type of culling, OpenGL already does frustum culling, you are free to do culling on the CPU, but again, balance the workload between CPU and GPU. Per-pixel culling should be done in the fragment shader.
精彩评论