Shadow mapping optimizations

Looking at the code from:

discourse.panda3d.org/viewtopic.php?p=10093

I’d like to remove the excess multiply per vertex that uses the scale to create the texture matrix. I can do this because I already am computing world coordinates for the vertex position for another part of the shader. So I want to pass in a uniform float4x4 matrix that converts world->adjusted clip space.

So far, I’ve come up with:

void vshader(float4 vtx_position : POSITION,
             float2 vtx_texcoord0 : TEXCOORD0,

             in uniform float4x4 mat_modelproj,
             in uniform float4x4 trans_model_to_world,
             in uniform float4x4 trans_scaleMatrix_to_world, 

             out float4 l_position : POSITION,
             out float2 l_texcoord0 : TEXCOORD0,
             out float4 l_texcoord1 : TEXCOORD1,

) {


  l_wsvtxposition = mul(trans_model_to_world,vtx_position);
  l_texcoord0 = vtx_texcoord0;
  l_position = mul(mat_modelproj, vtx_position);

  l_texcoord1 = mul(trans_scaleMatrix_to_world,l_wsvtxposition);
}

and in the python code, I’m trying to set up that matrix by:

		scaleMatrix = NodePath("scaleMatrix")
		#Scale Matrix for transforming clip space -> texture space
		scaleMatrix.setScale(.5)
		scaleMatrix.setPos(.5,.5,.5)
		depthcamtrans = self.depthCameraPath.getNetTransform().getMat()
		depthcamtrans.invertInPlace()
		stashedMatrix = depthcamtrans * self.depthCameraPath.node().getLens().getProjectionMat() * scaleMatrix.getMat()
		scaleMatrix.setMat(stashedMatrix)
		
		shadowNode.setShaderInput( "scaleMatrix", scaleMatrix)

(depthCameraPath is a NodePath at the location of the shadow camera)

Based on the article on matrices, I assume they are pre-multiply, but I’ve tried it both ways just in case. Either way, this does not preform the shadow rendering like I would expect. Any ideas?

Thanks,
Chris

Matrix passing seems still not working ATM. Pass it per row, and build the matrix in the vtx shader, it works.

I have gotten the matrix passing to work. My first test in this route was just to take the scale matrix out of the shader and that worked fine. I am just trying to figure out the right set of matrices from the nodepaths and lens that create the trans_world_to_clip_of_light matrix. You have to pass it in as a NodePath, and it’s the transpose of what it is in Cg. My scale matrix worked like this:

scaleMatrix = NodePath("scaleMatrix")
scaleMatrix.setMat(Mat4(.5,  0,  0,  0,
                         0, .5,  0,  0,
                         0,  0, .5,  0,
                        .5, .5, .5,  1))

Try put the scale matrix as the first one :

stashedMatrix = scaleMatrix.getMat() * depthcamtrans * self.depthCameraPath.node().getLens().getProjectionMat()

The result is completely different !

I figured it out. I have the correct set of transforms in my first post. The problem was that I thought the

depthcamtrans = self.depthCameraPath.getNetTransform().getMat() 

would give me a copy of the matrix, not a pointer to the actual matrix. So I was modifying my camera transform matrix when I was doing my inplace invert. So instead, I now have:

depthcamtrans = Mat4()
depthcamtrans.invertFrom(self.depthCameraPath.getNetTransform().getMat())

and everything is happy.