sp614x / optifine

1.77k stars 420 forks source link

[Shaders] Support for tessellation shaders #2359

Open rre36 opened 5 years ago

rre36 commented 5 years ago

Tesselation shaders could/would allow for in-shader triangulation of minecrafts geometry (which is using quads) to allow geometry shaders to be used on non-nvidia configurations. Furthermore it would also allow for more advanced effects replacing the pom-way of achieving things in certain cases (eg. water or wind effects).

zombye commented 5 years ago

The two "obvious" use cases would be as a replacement of parallax and to subdivide the water geometry for detailed water waves, but there are many possible use cases which may be interesting.

Also, per the OpenGL 4.0 specification (see section 2.12.2, page 100), and the extension, the tesselation stage is required to output triangles if the input is either quads or triangles - as long as the tesselation evaluation shader is present. This means that inserting a dummy tesselation evaluation shader could also potentially be used to get geometry shaders working when not running on an Nvidia GPU.

Note: OpenGL 4.1 and 4.6 also specify the same thing as OpenGL 4.0, with very similar wording. Presumably this is the case for OpenGL 4.2 through OpenGL 4.5 as well.

NyaomiDEV commented 5 years ago

Taking into account the obvious problems of the GCN architecture, if tesselation shaders can really solve the quads-to-tris conversion problem for AMD users who want to use the new cool path traced shaders on their systems, then I am totally for it. This will also mean more Minecraft players who use shaders on AMD systems and less bugs for everyone.

rre36 commented 5 years ago

This and some more fancy effects, especially wind etc.

sp614x commented 4 years ago

One problem with the tessellation shaders (TS) is that the draw operations (glDrawArrays) have to use GL_PATCHES instead of GL_QUADS/TRIANGLES/LINES as with tessellation the draw operation has to give the control points not the geometry points.

TS are optional so the GL_PATCHES should only be used for programs which have the TS active.

rre36 commented 4 years ago

Will that cause any disadvantage tho?

sp614x commented 4 years ago

Currently the possible ways to fix the geometry shaders (GS) on AMD/Intel are:

A. Add Tessellation shaders (TS)

This is relatively complex with many possible points of failure. Not clear yet how much TS will impact performance.

B. Use input type GL_LINES_ADJACENCY for GS

This can work, because GL_LINES_ADJACENCY uses 4 vertices per primitive (2 line, 2 adjecency) the same as quads, so the quad vertex data doesn't have to be changed. The GS can take 4 vertices as input (line_adj) and produce a triangle strip.

C. Split quads into triangles

The split can also be done when building the geometry but this would affect translucent layer resorting and probably break some mods that like to manipulate the geometry directly.

For now solution B seems to be the one with minimal complexity and performance impact.

NyaomiDEV commented 4 years ago

So, those new path traced shaderpacks are not working on AMD/Intel due to geometry shaders? And the solution B will "fix" that with little impact?

rre36 commented 4 years ago

Alright then, I guess solution B would be appropriate as a fix for geometry shader usage on AMD/Intel. However i'd still like to see tesselation shaders at some point since these do give us shader devs the ability to improve vertex shader based effects like wind, waves etc.

sp614x commented 4 years ago

The first step is to fix geometry shaders on AMD/Intel. This render mode manipulation can later be reused when adding tessellation shaders.

sp614x commented 4 years ago

It looks like GL_LINES_ADJACENCY is not supported inside display lists on Intel and probably on AMD. Display lists are legacy (deprecated) and there is no requirement for them to support 3.0+ features. The whole entity/tileentity rendering is based on display lists (no VBO support). It is quite possible that GL_PATCHES has the same problem, which would make TS impossible to add without extending entity rendering to support VBOs.

sp614x commented 4 years ago

OptiFine 1.14.3_HD_U_F2_pre2

When geometry shaders are active and the GPU doesn't support quads as GS input (GL_NV_geometry_shader4) then all quads are split into triangles before rendering. This follows the OpenGL spec and should work on all GPUs.

Adding tessellation shaders is still open, but it may be problematic on Intel/AMD as long as the entity rendering uses display lists.