Dynamic Textures Under 256x256

I am creating my own lighting engine of sorts and I am at the point where I am building the textures. I’ve run into an issue with the textures getting corrupted if the resolution of the texture is lower than 256x256.

It looks like this:

This is supposed to be all black.


Here’s the code where I create the actual texture. This part seems to work fine, and gives me a solid white texture. I call setPixelRegion() at the end, which is supposed to turn the image black:

   lightmapBuffer = new uint8[resX * resY * 4];
	lightmap = UTexture2D::CreateTransient(resX, resY);
	lightmap->MipGenSettings = TextureMipGenSettings::TMGS_NoMipmaps;
	lightmap->CompressionSettings = TextureCompressionSettings::TC_VectorDisplacementmap;
	lightmap->Filter = TextureFilter::TF_Nearest;
	lightmap->AddToRoot();
	lightmap->UpdateResource();

	updateTextureRegion = new FUpdateTextureRegion2D(0, 0, 0, 0, resX, resY);

	setPixelRegion(0, 0, resX, resY, 0);

Here’s the setPixelRegion code, which should be setting each pixel of the texture to have an RGBA value of (0, 0, 0, 255):

void ULightableComponent::setPixelRegion(int x, int y, int w, int h, uint8 b)
{
	uint8 valb = b;
	
	for (int row = y; row < h; row++)
	{
		for (int col = x; col < w; col++)
		{
			int i = (row * w) + col;
			lightmapBuffer[(4*i)+0] = valb;
			lightmapBuffer[(4*i)+1] = valb;
			lightmapBuffer[(4*i)+2] = valb;
			lightmapBuffer[(4*i)+3] = 255;
		}
	}

	FUpdateTextureRegion2D* updateRegion = new FUpdateTextureRegion2D(x, y, x, y, w, h);
	lightmap->UpdateTextureRegions(0, 1, updateRegion, 1024, 4, lightmapBuffer);
}

This code works totally fine if the image is at least 256x256, but anything less than that and it gets all garbled like the picture.

Here it is with a 256x256 resolution (this is with a light applied, so it’s not just an all black image. The first one is supposed to be all black, but is garbled):

What am I missing?

One thing I’ve noticed is the top half of the image seems to work most of the time, but the bottom half is all messed up. It’s like all of the data is shifted up to the top. It’s still not working properly in the top, but it looks more correct than the bottom.