In the example JCudaDriverGLSample3.java, the rendering work is done by using OpenGL. I’ve found that in the method setupView(GLAutoDrawable), the ‘perspective’ is implemented by Java code, including the identity() method as well as matrix multiplication.
I’m not so familiar with OpenGL but I know that there is an OpenGL implementation of ‘perspective’ which could be used though calling glu.gluPerspective(). I guess the internal implementation of gluPerspective() is similar to the Java souce code in JCudaDriverGLSample3.java.
My question is why not use OpenGL implementation so that it could free CPU from computing the matrix multiplication etc. and let GPU do more rendering work (which is more suitable for GPU)?
First of all, the single creation of a 4x4 matrix is definitely better to perform on the CPU. There is nothing that could be done in parallel for a single matrix. Additionally, the gluPerspective call would also working on the CPU. In the end, the matrix has to be located in CPU memory, because it is passed to the shader as a ‘uniform’ input. (It should, however, be possible to use gluPerspective, and then obtain the matrix using the glGet* functions - but this would not be much easier… BTW: I think the ‘perspective’ function was originally taken from the reference GLU implementation - I should probably add a reference note for this…)
Thank you for your explanation. It’s clear to me now.
But there are default shaders (vertex shader, fragment shader) in OpenGL. It looks like the function of the new vertex shader program in JCudaDriverGLSample3.java is the same as that of the default one. The function of the fragment shader is properly not the same as that of the default one because it use a ‘red’ color. Is that true?
If the vertex shader is the same as the default one, could the program be written without the new vertex shader?
You may be referring to the “fixed function pipeline” (using glVertex3f, glPushMatrix and all that). This pipeline is deprecated in the latest OpenGL versions. (I think starting with OpenGL 3.2 or 3.3). In OpenGL >= 3.2, the only “standard conform” way of rendering is to use an own shader - even if it does the same as the fixed function pipeline.
But note that the shaders used in this sample are very minimalistic, since there is no mesh, but only points. Even the simplest lighting of a real mesh would require a much more complicated shader.