# How can I pass an array of floats to the fragment shader using textures?

I want to map out a 2D array of depth elements for the fragment shader to use to check depth against to create shadows. I want to be able to copy a float array into the GPU, but using large uniform arrays causes segfaults in openGL so that is not an option. I tried texturing but the best i got was to use GL_DEPTH_COMPONENT

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 512, 512, 0, GL_DEPTH_COMPONENT, GL_FLOAT, smap);


Which doesn't work because that stores depth components (0.0 - 1.0) which I don't want because I have no idea how to calculate them using the depth value produced by the light sources MVP matrix multiplied by the coordinate of each vertex.

Is there any way to store and access large 2D arrays of floats in openGL?

## 1 Answer

To pass big amounts of data for random access using textures is the correct way. Setting large amounts of uniforms is very slow and you only have a very limited amout of constant registers on most hardware.

You can use GL_R32F or GL_DEPTH_COMPONENT32F as a texture format, which has to be supported by your target hardware, to store a single single precision floating point value per sample.

A shadow map texture is normally done using a normalized depth between the near and far planes of your shadow light, eg by using the depth value after projective divide. This gives you are higher precision, fixed point textures are often more efficient than floating point textuers and are supported by a wider range of hardware.

• If I had the rep I'd upvote, that was a very detailed answer thanks! I managed to fix it to work with openGL depths now, so I'm using that! Thanks! – James Dec 8 '12 at 19:16
• How many floats would qualify as "big amounts of data"? Asking because I use uniform buffer objects for most data transfer. – Ian Young Feb 20 '18 at 11:45