How to replace a single GLSL array with a 1D texture

I am trying to get the Ardor3D terrain system to work on SM3.0 hardware.

The current GLSL fragment shader uses a single vec2 array to pass an xy coordinate array to the fragment shader.

Since dynamic indexed homogeneous arrays only work with SM4.0 + hardware, to make it work on SM3.0, I need to replace it with a 1D floating texture.

The current array looks like this: uniform vec2 sliceOffset[8];

and opens as follows: vec2 offset = sliceOffset[int(unit)];

I am very experienced with OpenGL and GLSL, so I am having some problems with the conversion.

So far I have done this: Create a 1D texture - width = 8 - format = RGBA32F

Create 1D buffer for texture

  • width = 8 * 4 = 32 floats or 32 * 4 = 32 bytes
  • fill the float buffer as follows:

[x0,y0,0,0,x1,y1,0,0,x2,y2,0,0,x3,y3,0,0,x4,y4,0,0,x5,y5,0,0,x6,y6,0,0,x7,y7,0,0]

Create a 1D sampler for texture

  • min filter = nearest, no mip cards
  • mag filter = nearest
  • wrap mode = clamp to edge

In GLSL, I define a sampler as: uniform sampler1D sliceOffset;

And he turns to him:

 vec2 getSliceOffset(float unit) { float texCoord = (unit * 2.0f + 1.0f) / (2.0f * 8.0f); vec2 offset = texture1D(sliceOffset, texCoord); return offset; } 

But he is broken.

What am I doing wrong?

+4
source share
2 answers

Everything you show looks right. "He's broken," but not very descriptive. What are the symptoms?

Some things you don’t show may be wrong:

  • 2 sampling using the same texture unit.
  • enter texture data types (although if you're worried, I doubt it)

One more thing you could try: use texture2d instead, with just height = 1. DX does not require 1d texture support, so depending on the hardware, opengl support for it can be emulated. You can try to stay away from such emulation.

+1
source

The calculation of texCoord is correct. At first it was confusing, as I would do (unit +.5) /8.0, but this is the same.

One thing still bothers me. You do not need to write .rg at the end of your sampler example, since texFetch will be vec4? I am surprised that your compiler does not complain about this.

Try:

 texture1D(sliceOffset, texCoord).rg; 
+1
source

All Articles