HDRP GL Immediate Mode - Projection Offset?

I’m trying to do some simple line/text drawing to a RenderTexture during certain frames of my game, and chose to go down the GL immediate mode route via GL.Begin(…) / GL.Vertex3(…). Everything works as expected in the Editor, and in development builds, but for some reason release builds seem to behave at least slightly differently.

In Release, it seems my entire projection matrix is shifted like ~10% up and left. It also seems to be mucking with text rendering but I’m having a hard time figuring out quite what’s going on there, and I seem to have at least found a workaround for that.

I’ve built a standalone test case like this:

private void DrawTest() {
	// Matrix4x4 orthoProjection = Matrix4x4.Ortho(0, 1, 0, 1, -100, 100);
	Matrix4x4 orthoProjection = Matrix4x4.Ortho(0, renderTexture.width, 0, renderTexture.height, -100, 100);

	// Weird camera fix posted elsewhere for flickering, doesn't seem to change anything on or off?
	if (Camera.current != null) {
		orthoProjection *= Camera.current.worldToCameraMatrix.inverse;
	}

	RenderTexture prev = RenderTexture.active;
	RenderTexture.active = renderTexture;
			
	Shader shader = Shader.Find("Hidden/Internal-Colored");
	Material lineMaterial = new Material(shader);
	lineMaterial.hideFlags = HideFlags.HideAndDontSave;
	// Turn on alpha blending
	lineMaterial.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
	lineMaterial.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
	// Turn backface culling off
	lineMaterial.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off);
	// Turn off depth writes
	lineMaterial.SetInt("_ZWrite", 0);

	lineMaterial.SetPass(0);
			
	GL.PushMatrix();
	GL.LoadProjectionMatrix(orthoProjection);
		
	GL.Begin(GL.LINE_STRIP);
	GL.Clear(true, true, Color.red);
	GL.Color(new Color(0, 1, 0, 0.5f));
	GL.Vertex3(0, 0, 0);
	GL.Vertex3(renderTexture.width, renderTexture.height, 0);
	GL.End();


	TextPosition = new Vector3(renderTexture.width / 2, renderTexture.height / 2, 0);

	text.ForceMeshUpdate(true, true);
	float aspect = (float)renderTexture.width / renderTexture.height;
	float size = TextSize;
	Vector3 scale = new Vector3(size, size * aspect, 1);
	Matrix4x4 matrix = Matrix4x4.TRS(TextPosition, Quaternion.identity, scale);
	Material tmpMat = text.fontSharedMaterial;
	bool canRender = tmpMat.SetPass(0);
	if (canRender) {
		Graphics.DrawMeshNow(text.mesh, matrix);
	}
			
	GL.PopMatrix();
	GL.invertCulling = false;
	RenderTexture.active = prev;
}

In this config things mostly work. Dev/Editor builds draw a diagonal line as expected and text renders to the center. Release builds, everything is shifted down and right like 10%.

I was calling this from within Update(). I’ve tried moving that to all of the RenderPipelineManager.endContextRendering events and the like, but there’s no difference.

I also was originally using the first projection matrix listed that was 0-1 to do things by percentages, but for some reason that one ends up really messed up with text and I’m having trouble even locating what’s going on with it. I had it working in Edit/Dev builds but release everything just totally disappears.

I suspect this is something to do with the GL stack or something but I can’t find anyway around this except to try to just re-offset the matrix backwards, but I have no idea why it’s only happening in release and if this behavior will stay consistent.

I’m considering moving to CommandBuffers instead, but I’m not convinced I won’t have the same trouble there.

I would investigate this first. Having something labelled “weird” in your code, and getting weird results is kind of telling. :wink:

You understand that every time you draw, you are creating a new Material? Seems awfully wasteful.

The Shader may also play part in the issue. Try using a different simple shader such as Unlit, anything but a “hidden” one. These are hidden for a reason, and may not work properly in any context or by themselves.

Have you tried a simple LineRenderer to do the job?

What type is “text”? Just a regular mesh that you create yourself?
If it’s an old Legacy/Text object, don’t use that.

As mentioned in the comment, turning it off doesn’t change anything, so I don’t think it’s relevant. I left it in the sample because it’s something I found in other code snippets for similar logic posted here etc. It’s also an attempt at modifying the projection matrix (which seems to be the issue) so I had tried it. Something is clearly mangling the projection matrix, and I’m not sure what this fix from other similar code snippets is really trying to do and thought it might ring a bell or something for someone. It doesn’t end up impacting the behavior if I toggle that on/off.

Of course. As mentioned, this is just a standalone test case and I’m just trying to reduce variables here while trying to post a full example.

I don’t think this is relevant either, since as mentioned, the text exhibits the same behavior and is not using this shader.

This is entirely a different approach to a different problem. I’m trying to render simple lines to render textures without using a camera, etc. Invoking more components seems like overkill, and then trying to finagle entirely different APIs into something they aren’t intended for seems like a fool’s errand.

I’m not rendering into the normal 3D game world, I’m rendering offscreen to a separate RenderTexture.

Whoops, guess I wasn’t as specific here after re-editing my post. This is a non-canvas TextMeshPro 3D object and I’m grabbing it’s mesh.