Sign in to follow this  
Dragon_Strike

fast changing of value types in array?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this