fast changing of value types in array?

Started by
8 comments, last by Dragon_Strike 17 years, 6 months ago
ive got a 16bit raw file that i load to an unsiged short.. but i need it in floats in order to use it in my shaders.. right now im just looping through each compontent liek this..


	LoadTerrain("Data/ps_height_4k.raw");

	dest = new float[m_iSize*m_iSize];
	for (int z = 0; z < m_iSize; z++)
	{
		for (int x = 0; x < m_iSize; x++)
		{
			SetHeight(x,z, source[(x+z*(int)m_iSize)]); // source -> dest
		}
	}

this takes an awful lot of time.. is there a way to do this faster? using a shader or similar?
Advertisement
You have your source data in a one-dimensional array. I think you can circumvent der operations used in the array index in the SetHeight() call, but I can just guess because I don't know the internals.

The rest of the runtime depends quadratically on your m_iSize parameter, which seems to be given by the problem itself. What you could do is to dissolve your two loops into one by adressing the arrays directly as said above.
Why not write a simple commandline tool that converts your raw files into your own file format using floats... its a thing of round about 5 minutes...

t0}{!c
if (FileExists("Data/ps_height_4k.flt")) {  LoadProcessedTerrain("Data/ps_height_4k.flt");}else {  LoadTerrain("Data/ps_height_4k.raw");  dest = new float[m_iSize*m_iSize];  for (int z = 0; z < m_iSize; z++)  {    for (int x = 0; x < m_iSize; x++)    {      SetHeight(x,z, source[(x+z*(int)m_iSize)]); // source -> dest    }  }  SavedProcessedTerain("Data/ps_height_4k.flt");}


This will only recompile the terrain once. Loading a processed terrain should obviously be only a matter of reading a block of data from a file and into an array.
good and simple solution.. thx for the suggestion
Wait a second, what do you mean you need this as floats to use it inside a shader? Texture fetches always return floats and integer texture formats automatically return values in the float range [0.0, 1.0].
If there is no fancy rescale going on in your SetHeight() function and if the height map is only one unsigned short channel, just load it as a LUMINANCE16 texture and all is set.
Reading from it in the shader will return the floating point value in the rgb channels. Luminance is expanded to rgb.
Quote:Original post by Metalcore
Wait a second, what do you mean you need this as floats to use it inside a shader? Texture fetches always return floats and integer texture formats automatically return values in the float range [0.0, 1.0].
If there is no fancy rescale going on in your SetHeight() function and if the height map is only one unsigned short channel, just load it as a LUMINANCE16 texture and all is set.
Reading from it in the shader will return the floating point value in the rgb channels. Luminance is expanded to rgb.


i need to use a 32F texture becuase im sending it to a vertex shader..

but yea its a similar solution i was looking for..
I think the key question is what's going on in the SetHeight function? If it's just a simple conversion to float and scaling that loop should take no time at all to run (at least in comparison to loading the file). (ps_height_4k... not the Puget Sound data set again is it :P) If the SetHeight function is pretty simple you'll want to ensure the compiler has its definition by the time it reaches the function with the loop in so it can be inlined. Can't say much more than that without the code for SetHeight.

Edit: I've never used vertex textures (my graphics card is too old), but assuming MetalCore is right, then what he said.
Quote:Original post by Dragon_Strike
i need to use a 32F texture becuase im sending it to a vertex shader..
but yea its a similar solution i was looking for..


I see, you didn't say that. ;-)

According to the NVIDIA GPU Prgamming Guide vertex textures are only supported for one and four channel 32-bit floats today.
http://developer.nvidia.com/object/gpu_programming_guide.html

Again if SetHeight() does nothing fancy, just upload the texture with internalFormat set to a single 32-bit color format GL_LUMINANCE32F_ARB or GL_ALPHA32F_ARB (see http://www.opengl.org/registry/specs/ARB/texture_float.txt ) and leave the user type at unsigned short. The driver will convert it for you by doing the (float) data/65535.0f scaling during upload.
Quote:Original post by Metalcore
Quote:Original post by Dragon_Strike
i need to use a 32F texture becuase im sending it to a vertex shader..
but yea its a similar solution i was looking for..


I see, you didn't say that. ;-)

According to the NVIDIA GPU Prgamming Guide vertex textures are only supported for one and four channel 32-bit floats today.
http://developer.nvidia.com/object/gpu_programming_guide.html

Again if SetHeight() does nothing fancy, just upload the texture with internalFormat set to a single 32-bit color format GL_LUMINANCE32F_ARB or GL_ALPHA32F_ARB (see http://www.opengl.org/registry/specs/ARB/texture_float.txt ) and leave the user type at unsigned short. The driver will convert it for you by doing the (float) data/65535.0f scaling during upload.


thats exactly what i was looking for.. thx!

This topic is closed to new replies.

Advertisement