How to create *large* textures?

I am trying to pass textures on the order of magnitude of 11 million floats long, however, I am getting this error when I try to create the textures:

Apparently I can’t create textures larger than 128x128? That’s quite small for a game engine I think. Theoretically, a texture 4096x4096 should be more than enough to achieve this task.

I’m creating the textures like so:

UTexture2D* AMSPWindow::CreateTextureFrom32BitFloat(TArray<float> data, int width, int height) {
	UTexture2D* texture;
	texture = UTexture2D::CreateTransient(width, height, PF_R32_FLOAT);
	if (!texture) {
		GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, "Failed to create MSP data texture.");
		return nullptr;
	}
	texture->NeverStream = true;
	texture->SRGB = 0;
	texture->LODGroup = TextureGroup::TEXTUREGROUP_Pixels2D;
	FTexture2DMipMap& mip = texture->PlatformData->Mips[0];
	void* dataTarget = mip.BulkData.Lock(LOCK_READ_WRITE);
	FMemory::Memcpy(dataTarget, data.GetData(), width * height * 4);
	mip.BulkData.Unlock();
	texture->UpdateResource();
	return texture;
}

I found this technique in a reddit forum here: https://www.reddit.com/r/unrealengine/comments/gxmmmr/pass_an_array_of_floatsfcolors_to_material/

Any ideas as to what’s going wrong here or how I can get around this?

You’re trying to create a texture bigger than 16k res, which I think is the hard limit right now.

1 Like

Yes 16384 is the RHI limit - but that should give you 1 billion floats (if RGBA_32f rather than R32f)

You’ll need to split the data over multiple textures if you’re wanting more than that…

1 Like

But even though I’m only loading floats into the red channel shouldn’t a 4096x4096 texture still have enough? That has 16 million pixels, and if each pixel holds one float in the red channel I feel like there should still be enough pixels to hold the data.

I guess I may be misunderstanding the difference between rhi resolution and texture resolution. Are those not the same?

Yes that should be fine!

They are the same resolution between texture and RHI - but from the log you showed in the other post it was trying to allocate a texture 32433x32433 - the maxiumum is 16384x16384.

I just tried creating one here at 4096x4096xR32f and one at 16384x16384xR32f and they seem to work ok…

1 Like

Okay so I found the problem, it was quite an obvious mistake on my part. The error says that the texture exceeds the maximum allowed dimension, not size. In my code I was treating the texture like a float array so I had the dimensions of the texture as (data.Num(), 1), and since the data is typically millions of floats long this clearly exceeds the dimension limits.

It’s nice to have the textures structured like this because it makes shader sampling index wise quite simple, but it seems I will just have to pack the data asymmetrically. I tried making the texture (data.Num() / 1080, 1080) and the texture was successfully created. Thanks for everyone’s help!

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.