Advertisement
Builderb0y

Shader concepts

Mar 21st, 2018 (edited)
8,684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.51 KB | None | 0 0
  1. OptiFine's rendering pipeline:
  2.  
  3. Shadows: (shadow.fsh/vsh)
  4. Shadows run first, and are responsible for
  5. generating the shadow map, which is effectively
  6. an image of how far away everything is from the sun.
  7. Other objects that render later can then compare their
  8. own distance to that of the shadow map to figure out
  9. if there's something between it and the sun or not.
  10.  
  11. Gbuffers: (files starting with gbuffers_)
  12. These files are used to render terrain, entities,
  13. the sky, and almost everything else in the game.
  14. The specific name of the file tells you
  15. a bit more about what it's used to render.
  16. Skybasic runs first, and handles the main sky color.
  17. This is followed by skytextured, which handles the sun and moon.
  18. Up next comes terrain, which handles all opaque blocks.
  19. Wind effects for tallgrass are typically implemented here.
  20. Entities and block (tile entities) are next.
  21. Entities include everything from creepers to cows,
  22. and tile entities covers things like chests and banners.
  23. Textured and textured_lit handle particles.
  24. Deferred comes next, but we'll explain that later.
  25. Lastly, weather handles rain and snow, and water handles all translucent blocks.
  26. There are a few other gbuffer programs, but these are the main ones.
  27. For a full list, see: https://github.com/sp614x/optifine/blob/master/OptiFineDoc/doc/shaders.txt#L43
  28. GUIs, the F3 menu, and other overlays are not handled by any of these programs.
  29.  
  30. Composites: (composite(N) or final)
  31. Composites run after all geometry in the world has
  32. finished rendering, and final runs after the composites.
  33. These render over the entire screen, so you can use them
  34. to apply post-processing effects, or anything else
  35. that has to be done after all the gbuffers.
  36. Many shader packs also use this for lighting, ambient
  37. occlusion, fancy clouds (not the square vanilla kind),
  38. reflections, refractions, and many other effects.
  39.  
  40. Deferred: (deferred(N).fsh/vsh)
  41. This is similar to the composite programs, but runs in
  42. the middle of terrain rendering instead of after it.
  43. More specifically, they run after all opaque objects
  44. have rendered, and before any transparent objects.
  45. There is no real goal here, so what you do here is
  46. up to you. Some specific effects require it, others
  47. don't. Like the composites, you can write to as many
  48. buffer(s) as you want, with whatever data you want.
  49.  
  50. So, what can you do with all of these programs?
  51. Well, that depends on what *stage* of the program it is.
  52. OptiFine allows 4 stages of shader programs:
  53.  
  54. The vertex stage: (files that end in .vsh)
  55. The vertex stage applies logic to a single vertex.
  56. They have access to many properties of their vertex,
  57. such as position, color, light level, and a few other things.
  58. The primary purpose of vertex shaders is to place the
  59. provided vertex at the correct location on your screen.
  60. They can also output various pieces of information to
  61. the fragment stage using "varyings". (explained below)
  62.  
  63. The geometry stage: (files that end in .gsh)
  64. The geometry stage applies logic to 3 vertexes at a time.
  65. More specifically, they have inputs for 3
  66. vertexes which are part of the same triangle.
  67. You might think things would be square instead,
  68. since this is Minecraft, but even in Minecraft
  69. squares are always made of 2 triangles because
  70. that's what the GPU was designed to handle.
  71. Anyway, the geometry stage can choose to modify
  72. vertex positions or change the varying data which
  73. is fed into the fragment stage (explained next),
  74. but the REAL power of the geometry stage is that it
  75. can change the *number* of vertexes (or triangles)
  76. that get drawn. The number still has to be a multiple of 3,
  77. and the GPU often has limits on the maximum number of
  78. vertexes which can be generated, but the geometry
  79. stage is still very powerful nevertheless.
  80.  
  81. The fragment stage: (files that end in .fsh)
  82. After all the vertexes of a specific triangle
  83. have been positioned, the GPU determines which
  84. pixels are inside it, and which are outside it.
  85. This process is called "rasterization". Once
  86. rasterization is finished, the GPU will call
  87. upon the fragment stage to choose a color for
  88. all the pixels that were inside the triangle.
  89. They can output more than one color by using
  90. more than one "framebuffer attachment". (explained below)
  91.  
  92. The compute stage: (files that end in .csh)
  93. The compute stage has no pre-defined job.
  94. It can still have the usual uniform inputs, but
  95. it does not know about any geometry in the world.
  96. It does have one powerful feature though: it can
  97. write *directly* to a buffer at any location.
  98. Normally when a fragment stage writes to a buffer,
  99. it is restricted to only writing at the
  100. location of the pixel it was assigned to.
  101. Compute shaders do not have this restriction.
  102. At the time of writing this, compute stages
  103. are still very new, experimental, and buggy.
  104. They have not yet been back-ported to any
  105. Minecraft versions earlier than 1.16.5.
  106.  
  107. Everything in the pipeline mentioned above
  108. contains a vertex stage and a fragment stage.
  109. Geometry and compute are both optional.
  110.  
  111. Methods of transferring data between stages and programs:
  112.  
  113. Varyings:
  114. Varyings can transfer data from a vertex
  115. stage to its associated fragment stage.
  116. A varying is a single value that gets interpolated
  117. between all vertexes that it's part of.
  118. For example, say you have a triangle
  119. which outputs a varying for "color".
  120. The first vertex outputs red, the second
  121. outputs green, and the third one outputs blue.
  122. A fragment in the middle of that triangle
  123. would read a gray-ish color from that varying.
  124. A fragment half way between the red vertex and
  125. the green vertex (on the edge of the triangle)
  126. would read a dark yellow-ish color.
  127.  
  128. Framebuffer attachments: (or just buffers, for short)
  129. Buffers can transfer data between fragment
  130. programs and any other program that runs after it*
  131. *Some restrictions apply that prevent certain buffers
  132. from being read/written to from specific programs.
  133. A buffer holds a value for every pixel on the screen.
  134. Typically, each value will be an RGBA color.
  135. They can also be formatted to use a specific precision
  136. (number of bits) for each component of the color.
  137. When a fragment shader writes to a buffer, the previous
  138. value is typically either overwritten, or blended together
  139. with the new value using the alpha component of the new value.
  140.  
  141. An example of what you can do with this pipeline:
  142. Create 2 buffers. one is a material buffer,
  143. the other is the translucent buffer.
  144. Make all transparent objects output their
  145. color to the translucent buffer, and a
  146. number representing their ID (passed in
  147. with varyings) to the material buffer.
  148. Composite can read the material buffer,
  149. and mix the translucent buffer with the opaque
  150. color buffer differently depending on the ID.
  151. This will allow effects such as fog behind water,
  152. or only applying reflections to glass and
  153. water but not slime blocks or nether portals.
  154. As you may have guessed though, the material
  155. buffer can only store one ID per pixel.
  156. In most cases, the ID will be that of the
  157. closest transparent object to the camera.
  158. Everything behind it will be ignored.
  159. This means that if you look at water
  160. through stained glass, suddenly it
  161. won't have thick blue fog anymore.
  162. A lot of shader packs have similar issues.
  163. Sadly, there's no easy way to fix this.
  164. Still, this should give you an idea of what is
  165. and isn't possible to do with OptiFine's pipeline.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement