Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
OpenGL: Pass a double vector from vertex shader to fragment shader
TL;DR: How to pass a interpolated double
vector, such as a dvec2
, from the vertex shader to the fragment shader?
I have a vertex shader. In this the output variable out vec2 uv;
is set. And i have a fragment shader, in this the (intperpolated) input variable in vec2 uv;
is expected. This works, and the relevant shader parts looks like this:
Vertex shader:
#version 400
out vec2 uv;
...
void main()
{
uv = calculateUv();
...
}
Fragment shader:
#version 400
in vec2 uv;
...
void main()
{
doSomething(uv);
...
}
However, this uses only float
or 32 bit precision. When i change it to double
precision, by using dvec2
instead of vec2
, it no longer works and i get a
error C7570: 64 bit input 'uv' should be flat
error. I can set uv
to flat
, but then it is no longer interpolated and it doesn't work as expected (the uv
value is the same for all fragments
on a face, which is not what i want). Non-working shaders:
Vertex shader:
#version 400
out dvec2 uv;
...
void main()
{
uv = calculateUv();
...
}
Fragment shader:
#version 400
in dvec2 uv;
...
void main()
{
doSomething(uv);
...
}
It isn't a problem of not accepting double in general, inside the shader i can use double
based types such dvec2
without any problem as long as i don't want to pass it to a other shader.
Why double
precession is needed: I draw a fractal and want to zoom. This is possible by changing the uv
-coordinates but at a high zoom level, there are big artefacts from big, pixel-like, regions on the screen having the same uv
value.
Any idea how to pass a dvec2
? Or any workaround?
1 answer
The following users marked this post as Works for me:
User | Comment | Date |
---|---|---|
H_H | (no comment) | Aug 31, 2023 at 14:02 |
Found a workaround: Instead of doing the calculating in the vertex shader, do it in the fragment shader.
The precision error only occurs because the different vertexes have a uv
value that is close together relative to the absolute value of uv
. Which causes multiple fragments to have the same uv
coordinates which causes this artefacts. By passing uv
directly, with coordinates that only have the values -1
1
, each fragment shader instance gets its own uv
value that is different from the others. The calculation inside the fragment shader can be done with double precision.
The good thing is, the main program can pass double
values, when the correct extensions are supported.
The new fragment shader:
#version 400
//Moved zoom from the vertex shader. This is set by the main program.
// with glUniformMatrix3dv()
uniform dmat3 zoom;
//uv values are not transformed.
in vec2 uv;
...
void main()
{
dvec2 uv_double = uv;
uv_double = ( zoom * dvec3(uv_double,1)).xy ;
//us uv_double from here on and not uv
...
}
Disadvantage is that glUniformMatrix3dv()
is an extension any may not be supported everywhere. A other very minor disadvantage is that this is probably slower.
0 comment threads