Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

50%
+0 −0
Q&A OpenGL: Pass a double vector from vertex shader to fragment shader

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 clos...

posted 1y ago by H_H‭  ·  edited 1y ago by H_H‭

Answer
#2: Post edited by user avatar H_H‭ · 2023-08-22T17:56:02Z (over 1 year ago)
  • 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()` has to be supported and a other very minor disadvantage is that this is probably slower.
  • 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.
#1: Initial revision by user avatar H_H‭ · 2023-08-22T17:55:05Z (over 1 year ago)
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()` has to be supported and a other very minor disadvantage is that this is probably slower.