gpu_shader_opensubdiv_geometry.glsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software Foundation,
  14. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. *
  16. * The Original Code is Copyright (C) 2014 Blender Foundation.
  17. * All rights reserved.
  18. */
  19. struct VertexData {
  20. vec4 position;
  21. vec3 normal;
  22. vec2 uv;
  23. };
  24. layout(lines_adjacency) in;
  25. #ifdef WIREFRAME
  26. layout(line_strip, max_vertices = 8) out;
  27. #else
  28. layout(triangle_strip, max_vertices = 4) out;
  29. #endif
  30. uniform mat4 modelViewMatrix;
  31. uniform mat4 projectionMatrix;
  32. uniform int PrimitiveIdBase;
  33. uniform int osd_fvar_count;
  34. uniform int osd_active_uv_offset;
  35. in block
  36. {
  37. VertexData v;
  38. }
  39. inpt[];
  40. #define INTERP_FACE_VARYING_2(result, fvarOffset, tessCoord) \
  41. { \
  42. vec2 v[4]; \
  43. int primOffset = (gl_PrimitiveID + PrimitiveIdBase) * 4; \
  44. for (int i = 0; i < 4; ++i) { \
  45. int index = (primOffset + i) * osd_fvar_count + fvarOffset; \
  46. v[i] = vec2(texelFetch(FVarDataBuffer, index).s, texelFetch(FVarDataBuffer, index + 1).s); \
  47. } \
  48. result = mix(mix(v[0], v[1], tessCoord.s), mix(v[3], v[2], tessCoord.s), tessCoord.t); \
  49. }
  50. uniform samplerBuffer FVarDataBuffer;
  51. uniform isamplerBuffer FVarDataOffsetBuffer;
  52. out block
  53. {
  54. VertexData v;
  55. }
  56. outpt;
  57. #ifdef FLAT_SHADING
  58. void emit(int index, vec3 normal)
  59. {
  60. outpt.v.position = inpt[index].v.position;
  61. outpt.v.normal = normal;
  62. /* TODO(sergey): Only uniform subdivisions atm. */
  63. vec2 quadst[4] = vec2[](vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1));
  64. vec2 st = quadst[index];
  65. INTERP_FACE_VARYING_2(outpt.v.uv, osd_active_uv_offset, st);
  66. gl_Position = projectionMatrix * inpt[index].v.position;
  67. EmitVertex();
  68. }
  69. # ifdef WIREFRAME
  70. void emit_edge(int v0, int v1, vec3 normal)
  71. {
  72. emit(v0, normal);
  73. emit(v1, normal);
  74. }
  75. # endif
  76. #else
  77. void emit(int index)
  78. {
  79. outpt.v.position = inpt[index].v.position;
  80. outpt.v.normal = inpt[index].v.normal;
  81. /* TODO(sergey): Only uniform subdivisions atm. */
  82. vec2 quadst[4] = vec2[](vec2(0, 0), vec2(1, 0), vec2(1, 1), vec2(0, 1));
  83. vec2 st = quadst[index];
  84. INTERP_FACE_VARYING_2(outpt.v.uv, osd_active_uv_offset, st);
  85. gl_Position = projectionMatrix * inpt[index].v.position;
  86. EmitVertex();
  87. }
  88. # ifdef WIREFRAME
  89. void emit_edge(int v0, int v1)
  90. {
  91. emit(v0);
  92. emit(v1);
  93. }
  94. # endif
  95. #endif
  96. void main()
  97. {
  98. gl_PrimitiveID = gl_PrimitiveIDIn;
  99. #ifdef FLAT_SHADING
  100. vec3 A = (inpt[0].v.position - inpt[1].v.position).xyz;
  101. vec3 B = (inpt[3].v.position - inpt[1].v.position).xyz;
  102. vec3 flat_normal = normalize(cross(B, A));
  103. # ifndef WIREFRAME
  104. emit(0, flat_normal);
  105. emit(1, flat_normal);
  106. emit(3, flat_normal);
  107. emit(2, flat_normal);
  108. # else
  109. emit_edge(0, 1, flat_normal);
  110. emit_edge(1, 2, flat_normal);
  111. emit_edge(2, 3, flat_normal);
  112. emit_edge(3, 0, flat_normal);
  113. # endif
  114. #else
  115. # ifndef WIREFRAME
  116. emit(0);
  117. emit(1);
  118. emit(3);
  119. emit(2);
  120. # else
  121. emit_edge(0, 1);
  122. emit_edge(1, 2);
  123. emit_edge(2, 3);
  124. emit_edge(3, 0);
  125. # endif
  126. #endif
  127. EndPrimitive();
  128. }