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.

OpenGL: Pass a double vector from vertex shader to fragment shader

+3
−0

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

Any idea how to pass a dvec2? Or any workaround?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

0 comment threads

1 answer

+0
−0

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.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »