Cannot get Compute Shader output

Hi all!

I have been following this tutorial on Compute Shaders and I have created one.

After some small battles, I got it to compile and run, but the results are often “uninitialized”.

This is how I call the shader, which is a copy of the original one:

int AUShaderTestActor::TestComputeShader()
{
	UE_LOG(LogTemp, Display, TEXT("**UShader** [%s] called TestComputeShader of object %s"), *FDateTime::Now().ToString() , *this->GetActorNameOrLabel());

	// Create the parameters
	FExampleShaderDispatchParameters Params(1, 1, 1);
	Params.Input[0] = Input0;
	Params.Input[1] = Input1;

	UE_LOG(LogTemp, Display,TEXT("**UShader** [%s] Input: %d, %d"), *FDateTime::Now().ToString(), Input0, Input1);

	// Execute the shader
	int OutputVal = 0;
	FExampleShaderInterface::Dispatch(
		Params, 
		[&OutputVal](int Output) 
		{ 
			UE_LOG(LogTemp, Display, TEXT("**UShader** [%s] Lambda: %d (OutputVal %d)"), *FDateTime::Now().ToString(), Output, OutputVal);

			OutputVal = Output; 
		}
	);
	UE_LOG(LogTemp, Display, TEXT("**UShader** [%s] outside of lambda %d"), *FDateTime::Now().ToString(), OutputVal);

	return OutputVal;
}

As you can see I have put some logging.

My BP actor calls this functions like the following (which I think is correct):

But this is what I’ve got as output:

LogTemp: Display: **UShader** [2024.09.05-16.35.58] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.35.58] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.35.58] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.35.58] Lambda: 33 (OutputVal 1065353216)
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Lambda: 33 (OutputVal 0)
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Lambda: 33 (OutputVal 0)
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Lambda: 33 (OutputVal 0)
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Lambda: 33 (OutputVal 0)
LogTemp: Display: **UShader** [2024.09.05-16.36.03] called TestComputeShader of object BP_UShaderTestActor
LogTemp: Display: **UShader** [2024.09.05-16.36.03] Input: 10, 23
LogTemp: Display: **UShader** [2024.09.05-16.36.03] outside of lambda 0

I don’t know why I cannot get the output outside of the lambda. Any hints?

Also, that spurious value… is it possible that the lambda is executed before OutputVal is even there?

Thank you!