PageRenderTime 37ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Core/VideoCommon/GeometryShaderGen.cpp

https://gitlab.com/Hexexpeck/dolphin-emulator
C++ | 337 lines | 256 code | 54 blank | 27 comment | 50 complexity | fa610724e30995718463a5364a030d3a MD5 | raw file
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <cmath>
  5. #include <cstring>
  6. #include "Common/CommonTypes.h"
  7. #include "VideoCommon/BPMemory.h"
  8. #include "VideoCommon/GeometryShaderGen.h"
  9. #include "VideoCommon/LightingShaderGen.h"
  10. #include "VideoCommon/VideoCommon.h"
  11. #include "VideoCommon/VideoConfig.h"
  12. static const char* primitives_ogl[] = {"points", "lines", "triangles"};
  13. static const char* primitives_d3d[] = {"point", "line", "triangle"};
  14. template <class T>
  15. static void EmitVertex(T& out, const char* vertex, APIType ApiType, bool first_vertex = false);
  16. template <class T>
  17. static void EndPrimitive(T& out, APIType ApiType);
  18. GeometryShaderUid GetGeometryShaderUid(u32 primitive_type)
  19. {
  20. ShaderUid<geometry_shader_uid_data> out;
  21. geometry_shader_uid_data* uid_data = out.GetUidData<geometry_shader_uid_data>();
  22. memset(uid_data, 0, sizeof(geometry_shader_uid_data));
  23. uid_data->primitive_type = primitive_type;
  24. uid_data->wireframe = g_ActiveConfig.bWireFrame;
  25. uid_data->msaa = g_ActiveConfig.iMultisamples > 1;
  26. uid_data->ssaa = g_ActiveConfig.iMultisamples > 1 && g_ActiveConfig.bSSAA;
  27. uid_data->stereo = g_ActiveConfig.iStereoMode > 0;
  28. uid_data->numTexGens = xfmem.numTexGen.numTexGens;
  29. uid_data->pixel_lighting = g_ActiveConfig.bEnablePixelLighting;
  30. return out;
  31. }
  32. static void EmitVertex(ShaderCode& out, const geometry_shader_uid_data* uid_data,
  33. const char* vertex, APIType ApiType, bool first_vertex = false);
  34. static void EndPrimitive(ShaderCode& out, const geometry_shader_uid_data* uid_data,
  35. APIType ApiType);
  36. ShaderCode GenerateGeometryShaderCode(APIType ApiType, const geometry_shader_uid_data* uid_data)
  37. {
  38. ShaderCode out;
  39. // Non-uid template parameters will write to the dummy data (=> gets optimized out)
  40. const unsigned int vertex_in = uid_data->primitive_type + 1;
  41. unsigned int vertex_out = uid_data->primitive_type == PRIMITIVE_TRIANGLES ? 3 : 4;
  42. if (uid_data->wireframe)
  43. vertex_out++;
  44. if (ApiType == APIType::OpenGL)
  45. {
  46. // Insert layout parameters
  47. if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
  48. {
  49. out.Write("layout(%s, invocations = %d) in;\n", primitives_ogl[uid_data->primitive_type],
  50. uid_data->stereo ? 2 : 1);
  51. out.Write("layout(%s_strip, max_vertices = %d) out;\n",
  52. uid_data->wireframe ? "line" : "triangle", vertex_out);
  53. }
  54. else
  55. {
  56. out.Write("layout(%s) in;\n", primitives_ogl[uid_data->primitive_type]);
  57. out.Write("layout(%s_strip, max_vertices = %d) out;\n",
  58. uid_data->wireframe ? "line" : "triangle",
  59. uid_data->stereo ? vertex_out * 2 : vertex_out);
  60. }
  61. }
  62. out.Write("%s", s_lighting_struct);
  63. // uniforms
  64. if (ApiType == APIType::OpenGL)
  65. out.Write("layout(std140%s) uniform GSBlock {\n",
  66. g_ActiveConfig.backend_info.bSupportsBindingLayout ? ", binding = 3" : "");
  67. else
  68. out.Write("cbuffer GSBlock {\n");
  69. out.Write("\tfloat4 " I_STEREOPARAMS ";\n"
  70. "\tfloat4 " I_LINEPTPARAMS ";\n"
  71. "\tint4 " I_TEXOFFSET ";\n"
  72. "};\n");
  73. out.Write("struct VS_OUTPUT {\n");
  74. GenerateVSOutputMembers<ShaderCode>(out, ApiType, uid_data->numTexGens, uid_data->pixel_lighting,
  75. "");
  76. out.Write("};\n");
  77. if (ApiType == APIType::OpenGL)
  78. {
  79. if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
  80. out.Write("#define InstanceID gl_InvocationID\n");
  81. out.Write("in VertexData {\n");
  82. GenerateVSOutputMembers<ShaderCode>(
  83. out, ApiType, uid_data->numTexGens, uid_data->pixel_lighting,
  84. GetInterpolationQualifier(uid_data->msaa, uid_data->ssaa, true, true));
  85. out.Write("} vs[%d];\n", vertex_in);
  86. out.Write("out VertexData {\n");
  87. GenerateVSOutputMembers<ShaderCode>(
  88. out, ApiType, uid_data->numTexGens, uid_data->pixel_lighting,
  89. GetInterpolationQualifier(uid_data->msaa, uid_data->ssaa, false, true));
  90. if (uid_data->stereo)
  91. out.Write("\tflat int layer;\n");
  92. out.Write("} ps;\n");
  93. out.Write("void main()\n{\n");
  94. }
  95. else // D3D
  96. {
  97. out.Write("struct VertexData {\n");
  98. out.Write("\tVS_OUTPUT o;\n");
  99. if (uid_data->stereo)
  100. out.Write("\tuint layer : SV_RenderTargetArrayIndex;\n");
  101. out.Write("};\n");
  102. if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
  103. {
  104. out.Write("[maxvertexcount(%d)]\n[instance(%d)]\n", vertex_out, uid_data->stereo ? 2 : 1);
  105. out.Write("void main(%s VS_OUTPUT o[%d], inout %sStream<VertexData> output, in uint "
  106. "InstanceID : SV_GSInstanceID)\n{\n",
  107. primitives_d3d[uid_data->primitive_type], vertex_in,
  108. uid_data->wireframe ? "Line" : "Triangle");
  109. }
  110. else
  111. {
  112. out.Write("[maxvertexcount(%d)]\n", uid_data->stereo ? vertex_out * 2 : vertex_out);
  113. out.Write("void main(%s VS_OUTPUT o[%d], inout %sStream<VertexData> output)\n{\n",
  114. primitives_d3d[uid_data->primitive_type], vertex_in,
  115. uid_data->wireframe ? "Line" : "Triangle");
  116. }
  117. out.Write("\tVertexData ps;\n");
  118. }
  119. if (uid_data->primitive_type == PRIMITIVE_LINES)
  120. {
  121. if (ApiType == APIType::OpenGL)
  122. {
  123. out.Write("\tVS_OUTPUT start, end;\n");
  124. AssignVSOutputMembers(out, "start", "vs[0]", uid_data->numTexGens, uid_data->pixel_lighting);
  125. AssignVSOutputMembers(out, "end", "vs[1]", uid_data->numTexGens, uid_data->pixel_lighting);
  126. }
  127. else
  128. {
  129. out.Write("\tVS_OUTPUT start = o[0];\n");
  130. out.Write("\tVS_OUTPUT end = o[1];\n");
  131. }
  132. // GameCube/Wii's line drawing algorithm is a little quirky. It does not
  133. // use the correct line caps. Instead, the line caps are vertical or
  134. // horizontal depending the slope of the line.
  135. out.Write("\tfloat2 offset;\n"
  136. "\tfloat2 to = abs(end.pos.xy / end.pos.w - start.pos.xy / start.pos.w);\n"
  137. // FIXME: What does real hardware do when line is at a 45-degree angle?
  138. // FIXME: Lines aren't drawn at the correct width. See Twilight Princess map.
  139. "\tif (" I_LINEPTPARAMS ".y * to.y > " I_LINEPTPARAMS ".x * to.x) {\n"
  140. // Line is more tall. Extend geometry left and right.
  141. // Lerp LineWidth/2 from [0..VpWidth] to [-1..1]
  142. "\t\toffset = float2(" I_LINEPTPARAMS ".z / " I_LINEPTPARAMS ".x, 0);\n"
  143. "\t} else {\n"
  144. // Line is more wide. Extend geometry up and down.
  145. // Lerp LineWidth/2 from [0..VpHeight] to [1..-1]
  146. "\t\toffset = float2(0, -" I_LINEPTPARAMS ".z / " I_LINEPTPARAMS ".y);\n"
  147. "\t}\n");
  148. }
  149. else if (uid_data->primitive_type == PRIMITIVE_POINTS)
  150. {
  151. if (ApiType == APIType::OpenGL)
  152. {
  153. out.Write("\tVS_OUTPUT center;\n");
  154. AssignVSOutputMembers(out, "center", "vs[0]", uid_data->numTexGens, uid_data->pixel_lighting);
  155. }
  156. else
  157. {
  158. out.Write("\tVS_OUTPUT center = o[0];\n");
  159. }
  160. // Offset from center to upper right vertex
  161. // Lerp PointSize/2 from [0,0..VpWidth,VpHeight] to [-1,1..1,-1]
  162. out.Write("\tfloat2 offset = float2(" I_LINEPTPARAMS ".w / " I_LINEPTPARAMS
  163. ".x, -" I_LINEPTPARAMS ".w / " I_LINEPTPARAMS ".y) * center.pos.w;\n");
  164. }
  165. if (uid_data->stereo)
  166. {
  167. // If the GPU supports invocation we don't need a for loop and can simply use the
  168. // invocation identifier to determine which layer we're rendering.
  169. if (g_ActiveConfig.backend_info.bSupportsGSInstancing)
  170. out.Write("\tint eye = InstanceID;\n");
  171. else
  172. out.Write("\tfor (int eye = 0; eye < 2; ++eye) {\n");
  173. }
  174. if (uid_data->wireframe)
  175. out.Write("\tVS_OUTPUT first;\n");
  176. out.Write("\tfor (int i = 0; i < %d; ++i) {\n", vertex_in);
  177. if (ApiType == APIType::OpenGL)
  178. {
  179. out.Write("\tVS_OUTPUT f;\n");
  180. AssignVSOutputMembers(out, "f", "vs[i]", uid_data->numTexGens, uid_data->pixel_lighting);
  181. }
  182. else
  183. {
  184. out.Write("\tVS_OUTPUT f = o[i];\n");
  185. }
  186. if (uid_data->stereo)
  187. {
  188. // Select the output layer
  189. out.Write("\tps.layer = eye;\n");
  190. if (ApiType == APIType::OpenGL)
  191. out.Write("\tgl_Layer = eye;\n");
  192. // For stereoscopy add a small horizontal offset in Normalized Device Coordinates proportional
  193. // to the depth of the vertex. We retrieve the depth value from the w-component of the projected
  194. // vertex which contains the negated z-component of the original vertex.
  195. // For negative parallax (out-of-screen effects) we subtract a convergence value from
  196. // the depth value. This results in objects at a distance smaller than the convergence
  197. // distance to seemingly appear in front of the screen.
  198. // This formula is based on page 13 of the "Nvidia 3D Vision Automatic, Best Practices Guide"
  199. out.Write("\tf.pos.x += " I_STEREOPARAMS "[eye] * (f.pos.w - " I_STEREOPARAMS "[2]);\n");
  200. }
  201. if (uid_data->primitive_type == PRIMITIVE_LINES)
  202. {
  203. out.Write("\tVS_OUTPUT l = f;\n"
  204. "\tVS_OUTPUT r = f;\n");
  205. out.Write("\tl.pos.xy -= offset * l.pos.w;\n"
  206. "\tr.pos.xy += offset * r.pos.w;\n");
  207. out.Write("\tif (" I_TEXOFFSET "[2] != 0) {\n");
  208. out.Write("\tfloat texOffset = 1.0 / float(" I_TEXOFFSET "[2]);\n");
  209. for (unsigned int i = 0; i < uid_data->numTexGens; ++i)
  210. {
  211. out.Write("\tif (((" I_TEXOFFSET "[0] >> %d) & 0x1) != 0)\n", i);
  212. out.Write("\t\tr.tex%d.x += texOffset;\n", i);
  213. }
  214. out.Write("\t}\n");
  215. EmitVertex(out, uid_data, "l", ApiType, true);
  216. EmitVertex(out, uid_data, "r", ApiType);
  217. }
  218. else if (uid_data->primitive_type == PRIMITIVE_POINTS)
  219. {
  220. out.Write("\tVS_OUTPUT ll = f;\n"
  221. "\tVS_OUTPUT lr = f;\n"
  222. "\tVS_OUTPUT ul = f;\n"
  223. "\tVS_OUTPUT ur = f;\n");
  224. out.Write("\tll.pos.xy += float2(-1,-1) * offset;\n"
  225. "\tlr.pos.xy += float2(1,-1) * offset;\n"
  226. "\tul.pos.xy += float2(-1,1) * offset;\n"
  227. "\tur.pos.xy += offset;\n");
  228. out.Write("\tif (" I_TEXOFFSET "[3] != 0) {\n");
  229. out.Write("\tfloat2 texOffset = float2(1.0 / float(" I_TEXOFFSET
  230. "[3]), 1.0 / float(" I_TEXOFFSET "[3]));\n");
  231. for (unsigned int i = 0; i < uid_data->numTexGens; ++i)
  232. {
  233. out.Write("\tif (((" I_TEXOFFSET "[1] >> %d) & 0x1) != 0) {\n", i);
  234. out.Write("\t\tll.tex%d.xy += float2(0,1) * texOffset;\n", i);
  235. out.Write("\t\tlr.tex%d.xy += texOffset;\n", i);
  236. out.Write("\t\tur.tex%d.xy += float2(1,0) * texOffset;\n", i);
  237. out.Write("\t}\n");
  238. }
  239. out.Write("\t}\n");
  240. EmitVertex(out, uid_data, "ll", ApiType, true);
  241. EmitVertex(out, uid_data, "lr", ApiType);
  242. EmitVertex(out, uid_data, "ul", ApiType);
  243. EmitVertex(out, uid_data, "ur", ApiType);
  244. }
  245. else
  246. {
  247. EmitVertex(out, uid_data, "f", ApiType, true);
  248. }
  249. out.Write("\t}\n");
  250. EndPrimitive(out, uid_data, ApiType);
  251. if (uid_data->stereo && !g_ActiveConfig.backend_info.bSupportsGSInstancing)
  252. out.Write("\t}\n");
  253. out.Write("}\n");
  254. return out;
  255. }
  256. static void EmitVertex(ShaderCode& out, const geometry_shader_uid_data* uid_data,
  257. const char* vertex, APIType ApiType, bool first_vertex)
  258. {
  259. if (uid_data->wireframe && first_vertex)
  260. out.Write("\tif (i == 0) first = %s;\n", vertex);
  261. if (ApiType == APIType::OpenGL)
  262. {
  263. out.Write("\tgl_Position = %s.pos;\n", vertex);
  264. AssignVSOutputMembers(out, "ps", vertex, uid_data->numTexGens, uid_data->pixel_lighting);
  265. }
  266. else
  267. {
  268. out.Write("\tps.o = %s;\n", vertex);
  269. }
  270. if (ApiType == APIType::OpenGL)
  271. out.Write("\tEmitVertex();\n");
  272. else
  273. out.Write("\toutput.Append(ps);\n");
  274. }
  275. static void EndPrimitive(ShaderCode& out, const geometry_shader_uid_data* uid_data, APIType ApiType)
  276. {
  277. if (uid_data->wireframe)
  278. EmitVertex(out, uid_data, "first", ApiType);
  279. if (ApiType == APIType::OpenGL)
  280. out.Write("\tEndPrimitive();\n");
  281. else
  282. out.Write("\toutput.RestartStrip();\n");
  283. }