GeometryShaderGen.cpp 11 KB

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