| 1 | // Copyright 2015 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/UberShaderVertex.h" |
| 5 | |
| 6 | #include <utility> |
| 7 | |
| 8 | #include "VideoCommon/ConstantManager.h" |
| 9 | #include "VideoCommon/NativeVertexFormat.h" |
| 10 | #include "VideoCommon/UberShaderCommon.h" |
| 11 | #include "VideoCommon/VertexShaderGen.h" |
| 12 | #include "VideoCommon/VideoCommon.h" |
| 13 | #include "VideoCommon/XFMemory.h" |
| 14 | |
| 15 | namespace UberShader |
| 16 | { |
| 17 | VertexShaderUid GetVertexShaderUid() |
| 18 | { |
| 19 | VertexShaderUid out; |
| 20 | |
| 21 | vertex_ubershader_uid_data* const uid_data = out.GetUidData(); |
| 22 | uid_data->num_texgens = xfmem.numTexGen.numTexGens; |
| 23 | |
| 24 | return out; |
| 25 | } |
| 26 | |
| 27 | static void GenVertexShaderTexGens(APIType api_type, const ShaderHostConfig& host_config, |
| 28 | u32 num_texgen, ShaderCode& out); |
| 29 | static void LoadVertexAttribute(ShaderCode& code, const ShaderHostConfig& host_config, u32 indent, |
| 30 | std::string_view name, std::string_view shader_type, |
| 31 | std::string_view stored_type, std::string_view offset_name = {}); |
| 32 | |
| 33 | ShaderCode GenVertexShader(APIType api_type, const ShaderHostConfig& host_config, |
| 34 | const vertex_ubershader_uid_data* uid_data) |
| 35 | { |
| 36 | const bool msaa = host_config.msaa; |
| 37 | const bool ssaa = host_config.ssaa; |
| 38 | const bool per_pixel_lighting = host_config.per_pixel_lighting; |
| 39 | const bool vertex_rounding = host_config.vertex_rounding; |
| 40 | const bool vertex_loader = |
| 41 | host_config.backend_dynamic_vertex_loader || host_config.backend_vs_point_line_expand; |
| 42 | const u32 num_texgen = uid_data->num_texgens; |
| 43 | ShaderCode out; |
| 44 | |
| 45 | out.Write("// {}\n\n", *uid_data); |
| 46 | out.Write("{}", s_lighting_struct); |
| 47 | |
| 48 | // uniforms |
| 49 | out.Write("UBO_BINDING(std140, 2) uniform VSBlock {{\n"); |
| 50 | out.Write("{}", s_shader_uniforms); |
| 51 | out.Write("}};\n"); |
| 52 | |
| 53 | if (vertex_loader) |
| 54 | { |
| 55 | out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n"); |
| 56 | out.Write("{}", s_geometry_shader_uniforms); |
| 57 | out.Write("}};\n"); |
| 58 | } |
| 59 | |
| 60 | out.Write("struct VS_OUTPUT {{\n"); |
| 61 | GenerateVSOutputMembers(out, api_type, num_texgen, host_config, "", ShaderStage::Vertex); |
| 62 | out.Write("}};\n\n"); |
| 63 | |
| 64 | WriteIsNanHeader(out, api_type); |
| 65 | WriteBitfieldExtractHeader(out, api_type, host_config); |
| 66 | WriteLightingFunction(out); |
| 67 | |
| 68 | if (vertex_loader) |
| 69 | { |
| 70 | out.Write(R"( |
| 71 | SSBO_BINDING(1) readonly restrict buffer Vertices {{ |
| 72 | uint vertex_buffer[]; |
| 73 | }}; |
| 74 | )"); |
| 75 | if (api_type == APIType::D3D) |
| 76 | { |
| 77 | // Write a function to get an offset into vertex_buffer corresponding to this vertex. |
| 78 | // This must be done differently for D3D compared to OpenGL/Vulkan/Metal, as on OpenGL, etc., |
| 79 | // gl_VertexID starts counting at the base vertex specified in glDrawElementsBaseVertex, |
| 80 | // while on D3D, SV_VertexID (which spirv-cross translates gl_VertexID into) starts counting |
| 81 | // at 0 regardless of the BaseVertexLocation value passed to DrawIndexed. In both cases, |
| 82 | // offset 0 of vertex_buffer corresponds to index 0 with basevertex set to 0, so we have to |
| 83 | // manually apply the basevertex offset for D3D |
| 84 | // D3D12 uses a root constant for this uniform, since it changes with every draw. |
| 85 | // D3D11 doesn't currently support dynamic vertex loader, and we'll have to figure something |
| 86 | // out for it if we want to support it in the future. |
| 87 | out.Write("UBO_BINDING(std140, 5) uniform DX_Constants {{\n" |
| 88 | " uint base_vertex;\n" |
| 89 | "}};\n\n" |
| 90 | "uint GetVertexBaseOffset(uint vertex_id) {{\n" |
| 91 | " return (vertex_id + base_vertex) * vertex_stride;\n" |
| 92 | "}}\n"); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | out.Write("uint GetVertexBaseOffset(uint vertex_id) {{\n" |
| 97 | " return vertex_id * vertex_stride;\n" |
| 98 | "}}\n"); |
| 99 | } |
| 100 | |
| 101 | out.Write(R"( |
| 102 | uint4 load_input_uint4_ubyte4(uint vtx_offset, uint attr_offset) {{ |
| 103 | uint value = vertex_buffer[vtx_offset + attr_offset]; |
| 104 | return uint4(value & 0xffu, (value >> 8) & 0xffu, (value >> 16) & 0xffu, value >> 24); |
| 105 | }} |
| 106 | |
| 107 | float4 load_input_float4_ubyte4(uint vtx_offset, uint attr_offset) {{ |
| 108 | return float4(load_input_uint4_ubyte4(vtx_offset, attr_offset)) / 255.0f; |
| 109 | }} |
| 110 | |
| 111 | float3 load_input_float3_float3(uint vtx_offset, uint attr_offset) {{ |
| 112 | uint offset = vtx_offset + attr_offset; |
| 113 | return float3(uintBitsToFloat(vertex_buffer[offset + 0]), |
| 114 | uintBitsToFloat(vertex_buffer[offset + 1]), |
| 115 | uintBitsToFloat(vertex_buffer[offset + 2])); |
| 116 | }} |
| 117 | |
| 118 | float4 load_input_float4_rawpos(uint vtx_offset, uint attr_offset) {{ |
| 119 | uint components = attr_offset >> 16; |
| 120 | uint offset = vtx_offset + (attr_offset & 0xffff); |
| 121 | if (components < 3) |
| 122 | return float4(uintBitsToFloat(vertex_buffer[offset + 0]), |
| 123 | uintBitsToFloat(vertex_buffer[offset + 1]), |
| 124 | 0.0f, 1.0f); |
| 125 | else |
| 126 | return float4(uintBitsToFloat(vertex_buffer[offset + 0]), |
| 127 | uintBitsToFloat(vertex_buffer[offset + 1]), |
| 128 | uintBitsToFloat(vertex_buffer[offset + 2]), |
| 129 | 1.0f); |
| 130 | }} |
| 131 | |
| 132 | float3 load_input_float3_rawtex(uint vtx_offset, uint attr_offset) {{ |
| 133 | uint components = attr_offset >> 16; |
| 134 | uint offset = vtx_offset + (attr_offset & 0xffff); |
| 135 | if (components < 2) |
| 136 | return float3(uintBitsToFloat(vertex_buffer[offset + 0]), 0.0f, 0.0f); |
| 137 | else if (components < 3) |
| 138 | return float3(uintBitsToFloat(vertex_buffer[offset + 0]), |
| 139 | uintBitsToFloat(vertex_buffer[offset + 1]), |
| 140 | 0.0f); |
| 141 | else |
| 142 | return float3(uintBitsToFloat(vertex_buffer[offset + 0]), |
| 143 | uintBitsToFloat(vertex_buffer[offset + 1]), |
| 144 | uintBitsToFloat(vertex_buffer[offset + 2])); |
| 145 | }} |
| 146 | |
| 147 | )"); |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawpos;\n", ShaderAttrib::Position); |
| 152 | out.Write("ATTRIBUTE_LOCATION({:s}) in uint4 posmtx;\n", ShaderAttrib::PositionMatrix); |
| 153 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawnormal;\n", ShaderAttrib::Normal); |
| 154 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawtangent;\n", ShaderAttrib::Tangent); |
| 155 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawbinormal;\n", ShaderAttrib::Binormal); |
| 156 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawcolor0;\n", ShaderAttrib::Color0); |
| 157 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawcolor1;\n", ShaderAttrib::Color1); |
| 158 | for (u32 i = 0; i < 8; ++i) |
| 159 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawtex{};\n", ShaderAttrib::TexCoord0 + i, i); |
| 160 | } |
| 161 | |
| 162 | if (host_config.backend_geometry_shaders) |
| 163 | { |
| 164 | out.Write("VARYING_LOCATION(0) out VertexData {{\n"); |
| 165 | GenerateVSOutputMembers(out, api_type, num_texgen, host_config, |
| 166 | GetInterpolationQualifier(msaa, ssaa, true, false), |
| 167 | ShaderStage::Vertex); |
| 168 | out.Write("}} vs;\n"); |
| 169 | } |
| 170 | else |
| 171 | { |
| 172 | // Let's set up attributes |
| 173 | u32 counter = 0; |
| 174 | out.Write("VARYING_LOCATION({}) {} out float4 colors_0;\n", counter++, |
| 175 | GetInterpolationQualifier(msaa, ssaa)); |
| 176 | out.Write("VARYING_LOCATION({}) {} out float4 colors_1;\n", counter++, |
| 177 | GetInterpolationQualifier(msaa, ssaa)); |
| 178 | for (u32 i = 0; i < num_texgen; ++i) |
| 179 | { |
| 180 | out.Write("VARYING_LOCATION({}) {} out float3 tex{};\n", counter++, |
| 181 | GetInterpolationQualifier(msaa, ssaa), i); |
| 182 | } |
| 183 | if (!host_config.fast_depth_calc) |
| 184 | { |
| 185 | out.Write("VARYING_LOCATION({}) {} out float4 clipPos;\n", counter++, |
| 186 | GetInterpolationQualifier(msaa, ssaa)); |
| 187 | } |
| 188 | if (per_pixel_lighting) |
| 189 | { |
| 190 | out.Write("VARYING_LOCATION({}) {} out float3 Normal;\n", counter++, |
| 191 | GetInterpolationQualifier(msaa, ssaa)); |
| 192 | out.Write("VARYING_LOCATION({}) {} out float3 WorldPos;\n", counter++, |
| 193 | GetInterpolationQualifier(msaa, ssaa)); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | out.Write("void main()\n{{\n"); |
| 198 | |
| 199 | out.Write("VS_OUTPUT o;\n" |
| 200 | "\n"); |
| 201 | if (host_config.backend_vs_point_line_expand) |
| 202 | { |
| 203 | out.Write("uint vertex_id = gl_VertexID;\n" |
| 204 | "if (vs_expand != 0u) {{\n" |
| 205 | " vertex_id = vertex_id >> 2;\n" |
| 206 | "}}\n" |
| 207 | "uint vertex_base_offset = GetVertexBaseOffset(vertex_id);\n"); |
| 208 | } |
| 209 | else if (host_config.backend_dynamic_vertex_loader) |
| 210 | { |
| 211 | out.Write("uint vertex_base_offset = GetVertexBaseOffset(gl_VertexID);\n"); |
| 212 | } |
| 213 | // rawpos is always needed |
| 214 | LoadVertexAttribute(out, host_config, 0, "rawpos", "float4", "rawpos"); |
| 215 | // Transforms |
| 216 | out.Write("// Position matrix\n" |
| 217 | "float4 P0;\n" |
| 218 | "float4 P1;\n" |
| 219 | "float4 P2;\n" |
| 220 | "\n" |
| 221 | "// Normal matrix\n" |
| 222 | "float3 N0;\n" |
| 223 | "float3 N1;\n" |
| 224 | "float3 N2;\n" |
| 225 | "\n" |
| 226 | "if ((components & {}u) != 0u) {{ // VB_HAS_POSMTXIDX\n", |
| 227 | std::to_underlying(VB_HAS_POSMTXIDX)); |
| 228 | LoadVertexAttribute(out, host_config, 2, "posmtx", "uint4", "ubyte4"); |
| 229 | out.Write(" // Vertex format has a per-vertex matrix\n" |
| 230 | " int posidx = int(posmtx.r);\n" |
| 231 | " P0 = " I_TRANSFORMMATRICES "[posidx];\n" |
| 232 | " P1 = " I_TRANSFORMMATRICES "[posidx+1];\n" |
| 233 | " P2 = " I_TRANSFORMMATRICES "[posidx+2];\n" |
| 234 | "\n" |
| 235 | " int normidx = posidx >= 32 ? (posidx - 32) : posidx;\n" |
| 236 | " N0 = " I_NORMALMATRICES "[normidx].xyz;\n" |
| 237 | " N1 = " I_NORMALMATRICES "[normidx+1].xyz;\n" |
| 238 | " N2 = " I_NORMALMATRICES "[normidx+2].xyz;\n" |
| 239 | "}} else {{\n" |
| 240 | " // One shared matrix\n" |
| 241 | " P0 = " I_POSNORMALMATRIX "[0];\n" |
| 242 | " P1 = " I_POSNORMALMATRIX "[1];\n" |
| 243 | " P2 = " I_POSNORMALMATRIX "[2];\n" |
| 244 | " N0 = " I_POSNORMALMATRIX "[3].xyz;\n" |
| 245 | " N1 = " I_POSNORMALMATRIX "[4].xyz;\n" |
| 246 | " N2 = " I_POSNORMALMATRIX "[5].xyz;\n" |
| 247 | "}}\n" |
| 248 | "\n" |
| 249 | "// Multiply the position vector by the position matrix\n" |
| 250 | "float4 pos = float4(dot(P0, rawpos), dot(P1, rawpos), dot(P2, rawpos), 1.0);\n" |
| 251 | "o.pos = float4(dot(" I_PROJECTION "[0], pos), dot(" I_PROJECTION |
| 252 | "[1], pos), dot(" I_PROJECTION "[2], pos), dot(" I_PROJECTION "[3], pos));\n" |
| 253 | "\n" |
| 254 | "float3 _rawnormal;\n" |
| 255 | "float3 _rawtangent;\n" |
| 256 | "float3 _rawbinormal;\n" |
| 257 | "if ((components & {}u) != 0u) // VB_HAS_NORMAL\n" |
| 258 | "{{\n", |
| 259 | std::to_underlying(VB_HAS_NORMAL)); |
| 260 | LoadVertexAttribute(out, host_config, 2, "rawnormal", "float3", "float3"); |
| 261 | out.Write(" _rawnormal = rawnormal;\n" |
| 262 | "}}\n" |
| 263 | "else\n" |
| 264 | "{{\n" |
| 265 | " _rawnormal = " I_CACHED_NORMAL ".xyz;\n" |
| 266 | "}}\n" |
| 267 | "\n" |
| 268 | "if ((components & {}u) != 0u) // VB_HAS_TANGENT\n" |
| 269 | "{{\n", |
| 270 | std::to_underlying(VB_HAS_TANGENT)); |
| 271 | LoadVertexAttribute(out, host_config, 2, "rawtangent", "float3", "float3"); |
| 272 | out.Write(" _rawtangent = rawtangent;\n" |
| 273 | "}}\n" |
| 274 | "else\n" |
| 275 | "{{\n" |
| 276 | " _rawtangent = " I_CACHED_TANGENT ".xyz;\n" |
| 277 | "}}\n" |
| 278 | "\n" |
| 279 | "if ((components & {}u) != 0u) // VB_HAS_BINORMAL\n" |
| 280 | "{{\n", |
| 281 | std::to_underlying(VB_HAS_BINORMAL)); |
| 282 | LoadVertexAttribute(out, host_config, 2, "rawbinormal", "float3", "float3"); |
| 283 | out.Write(" _rawbinormal = rawbinormal;\n" |
| 284 | "}}\n" |
| 285 | "else\n" |
| 286 | "{{\n" |
| 287 | " _rawbinormal = " I_CACHED_BINORMAL ".xyz;\n" |
| 288 | "}}\n" |
| 289 | "\n" |
| 290 | "// The scale of the transform matrix is used to control the size of the emboss map\n" |
| 291 | "// effect by changing the scale of the transformed binormals (which only get used by\n" |
| 292 | "// emboss map texgens). By normalising the first transformed normal (which is used\n" |
| 293 | "// by lighting calculations and needs to be unit length), the same transform matrix\n" |
| 294 | "// can do double duty, scaling for emboss mapping, and not scaling for lighting.\n" |
| 295 | "float3 _normal = normalize(float3(dot(N0, _rawnormal), dot(N1, _rawnormal), dot(N2, " |
| 296 | "_rawnormal)));\n" |
| 297 | "float3 _tangent = float3(dot(N0, _rawtangent), dot(N1, _rawtangent), dot(N2, " |
| 298 | "_rawtangent));\n" |
| 299 | "float3 _binormal = float3(dot(N0, _rawbinormal), dot(N1, _rawbinormal), dot(N2, " |
| 300 | "_rawbinormal));\n"); |
| 301 | |
| 302 | // Hardware Lighting |
| 303 | out.Write("// xfmem.numColorChans controls the number of color channels available to TEV,\n" |
| 304 | "// but we still need to generate all channels here, as it can be used in texgen.\n" |
| 305 | "// Cel-damage is an example of this.\n" |
| 306 | "float4 vertex_color_0, vertex_color_1;\n" |
| 307 | "\n"); |
| 308 | out.Write("// To use color 1, the vertex descriptor must have color 0 and 1.\n" |
| 309 | "// If color 1 is present but not color 0, it is used for lighting channel 0.\n" |
| 310 | "bool use_color_1 = ((components & {0}u) == {0}u); // VB_HAS_COL0 | VB_HAS_COL1\n", |
| 311 | static_cast<u32>(VB_HAS_COL0 | VB_HAS_COL1)); |
| 312 | |
| 313 | out.Write("if ((components & {0}u) == {0}u) // VB_HAS_COL0 | VB_HAS_COL1\n" |
| 314 | "{{\n", |
| 315 | static_cast<u32>(VB_HAS_COL0 | VB_HAS_COL1)); |
| 316 | LoadVertexAttribute(out, host_config, 2, "rawcolor0", "float4", "ubyte4"); |
| 317 | LoadVertexAttribute(out, host_config, 2, "rawcolor1", "float4", "ubyte4"); |
| 318 | out.Write(" vertex_color_0 = rawcolor0;\n" |
| 319 | " vertex_color_1 = rawcolor1;\n" |
| 320 | "}}\n" |
| 321 | "else if ((components & {}u) != 0u) // VB_HAS_COL0\n" |
| 322 | "{{\n", |
| 323 | std::to_underlying(VB_HAS_COL0)); |
| 324 | LoadVertexAttribute(out, host_config, 2, "rawcolor0", "float4", "ubyte4"); |
| 325 | out.Write(" vertex_color_0 = rawcolor0;\n" |
| 326 | " vertex_color_1 = rawcolor0;\n" |
| 327 | "}}\n" |
| 328 | "else if ((components & {}u) != 0u) // VB_HAS_COL1\n" |
| 329 | "{{\n", |
| 330 | std::to_underlying(VB_HAS_COL1)); |
| 331 | LoadVertexAttribute(out, host_config, 2, "rawcolor1", "float4", "ubyte4"); |
| 332 | out.Write(" vertex_color_0 = rawcolor1;\n" |
| 333 | " vertex_color_1 = rawcolor1;\n" |
| 334 | "}}\n" |
| 335 | "else\n" |
| 336 | "{{\n" |
| 337 | " vertex_color_0 = missing_color_value;\n" |
| 338 | " vertex_color_1 = missing_color_value;\n" |
| 339 | "}}\n"); |
| 340 | |
| 341 | WriteVertexLighting(out, api_type, "pos.xyz", "_normal", "vertex_color_0", "vertex_color_1", |
| 342 | "o.colors_0", "o.colors_1"); |
| 343 | |
| 344 | // Texture Coordinates |
| 345 | if (num_texgen > 0) |
| 346 | GenVertexShaderTexGens(api_type, host_config, num_texgen, out); |
| 347 | |
| 348 | if (host_config.backend_vs_point_line_expand) |
| 349 | { |
| 350 | out.Write("if (vs_expand == {}u) {{ // Line\n", std::to_underlying(VSExpand::Line)); |
| 351 | out.Write(" bool is_bottom = (gl_VertexID & 2) != 0;\n" |
| 352 | " bool is_right = (gl_VertexID & 1) != 0;\n" |
| 353 | " uint other_base_offset = vertex_base_offset;\n" |
| 354 | " if (is_bottom) {{\n" |
| 355 | " other_base_offset -= vertex_stride;\n" |
| 356 | " }} else {{\n" |
| 357 | " other_base_offset += vertex_stride;\n" |
| 358 | " }}\n" |
| 359 | " float4 other_rawpos = load_input_float4_rawpos(other_base_offset, " |
| 360 | "vertex_offset_rawpos);\n" |
| 361 | " float4 other_p0 = P0;\n" |
| 362 | " float4 other_p1 = P1;\n" |
| 363 | " float4 other_p2 = P2;\n" |
| 364 | " if ((components & {}u) != 0u) {{ // VB_HAS_POSMTXIDX\n", |
| 365 | std::to_underlying(VB_HAS_POSMTXIDX)); |
| 366 | out.Write(" uint other_posidx = load_input_uint4_ubyte4(other_base_offset, " |
| 367 | "vertex_offset_posmtx).r;\n" |
| 368 | " other_p0 = " I_TRANSFORMMATRICES "[other_posidx];\n" |
| 369 | " other_p1 = " I_TRANSFORMMATRICES "[other_posidx+1];\n" |
| 370 | " other_p2 = " I_TRANSFORMMATRICES "[other_posidx+2];\n" |
| 371 | " }}\n" |
| 372 | " float4 other_pos = float4(dot(other_p0, other_rawpos), " |
| 373 | "dot(other_p1, other_rawpos), dot(other_p2, other_rawpos), 1.0);\n"); |
| 374 | GenerateVSLineExpansion(out, " ", num_texgen); |
| 375 | out.Write("}} else if (vs_expand == {}u) {{ // Point\n", std::to_underlying(VSExpand::Point)); |
| 376 | out.Write(" bool is_bottom = (gl_VertexID & 2) != 0;\n" |
| 377 | " bool is_right = (gl_VertexID & 1) != 0;\n"); |
| 378 | GenerateVSPointExpansion(out, " ", num_texgen); |
| 379 | out.Write("}}\n"); |
| 380 | } |
| 381 | |
| 382 | if (per_pixel_lighting) |
| 383 | { |
| 384 | out.Write("// When per-pixel lighting is enabled, the vertex colors are passed through\n" |
| 385 | "// unmodified so we can evaluate the lighting in the pixel shader.\n" |
| 386 | "// Lighting is also still computed in the vertex shader since it can be used to\n" |
| 387 | "// generate texture coordinates. We generated them above, so now the colors can\n" |
| 388 | "// be reverted to their previous stage.\n" |
| 389 | "o.colors_0 = vertex_color_0;\n" |
| 390 | "o.colors_1 = vertex_color_1;\n" |
| 391 | "// Note that the numColorChans logic should be (but currently isn't)\n" |
| 392 | "// performed in the pixel shader.\n"); |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | out.Write("// The number of colors available to TEV is determined by numColorChans.\n" |
| 397 | "// We have to provide the fields to match the interface, so set to zero\n" |
| 398 | "// if it's not enabled.\n" |
| 399 | "if (xfmem_numColorChans == 0u)\n" |
| 400 | " o.colors_0 = float4(0.0, 0.0, 0.0, 0.0);\n" |
| 401 | "if (xfmem_numColorChans <= 1u)\n" |
| 402 | " o.colors_1 = float4(0.0, 0.0, 0.0, 0.0);\n"); |
| 403 | } |
| 404 | |
| 405 | if (!host_config.fast_depth_calc) |
| 406 | { |
| 407 | // clipPos/w needs to be done in pixel shader, not here |
| 408 | out.Write("o.clipPos = o.pos;\n"); |
| 409 | } |
| 410 | |
| 411 | if (per_pixel_lighting) |
| 412 | { |
| 413 | out.Write("o.Normal = _normal;\n" |
| 414 | "o.WorldPos = pos.xyz;\n"); |
| 415 | } |
| 416 | |
| 417 | // If we can disable the incorrect depth clipping planes using depth clamping, then we can do |
| 418 | // our own depth clipping and calculate the depth range before the perspective divide if |
| 419 | // necessary. |
| 420 | if (host_config.backend_depth_clamp) |
| 421 | { |
| 422 | // Since we're adjusting z for the depth range before the perspective divide, we have to do our |
| 423 | // own clipping. We want to clip so that -w <= z <= 0, which matches the console -1..0 range. |
| 424 | // We adjust our depth value for clipping purposes to match the perspective projection in the |
| 425 | // software backend, which is a hack to fix Sonic Adventure and Unleashed games. |
| 426 | out.Write("float clipDepth = o.pos.z * (1.0 - 1e-7);\n" |
| 427 | "float clipDist0 = clipDepth + o.pos.w;\n" // Near: z < -w |
| 428 | "float clipDist1 = -clipDepth;\n"); // Far: z > 0 |
| 429 | if (host_config.backend_geometry_shaders) |
| 430 | { |
| 431 | out.Write("o.clipDist0 = clipDist0;\n" |
| 432 | "o.clipDist1 = clipDist1;\n"); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | // Write the true depth value. If the game uses depth textures, then the pixel shader will |
| 437 | // override it with the correct values if not then early z culling will improve speed. |
| 438 | // There are two different ways to do this, when the depth range is oversized, we process |
| 439 | // the depth range in the vertex shader, if not we let the host driver handle it. |
| 440 | // |
| 441 | // Adjust z for the depth range. We're using an equation which incorporates a depth inversion, |
| 442 | // so we can map the console -1..0 range to the 0..1 range used in the depth buffer. |
| 443 | // We have to handle the depth range in the vertex shader instead of after the perspective |
| 444 | // divide, because some games will use a depth range larger than what is allowed by the |
| 445 | // graphics API. These large depth ranges will still be clipped to the 0..1 range, so these |
| 446 | // games effectively add a depth bias to the values written to the depth buffer. |
| 447 | out.Write("o.pos.z = o.pos.w * " I_PIXELCENTERCORRECTION ".w - " |
| 448 | "o.pos.z * " I_PIXELCENTERCORRECTION ".z;\n"); |
| 449 | |
| 450 | if (!host_config.backend_clip_control) |
| 451 | { |
| 452 | // If the graphics API doesn't support a depth range of 0..1, then we need to map z to |
| 453 | // the -1..1 range. Unfortunately we have to use a subtraction, which is a lossy floating-point |
| 454 | // operation that can introduce a round-trip error. |
| 455 | out.Write("o.pos.z = o.pos.z * 2.0 - o.pos.w;\n"); |
| 456 | } |
| 457 | |
| 458 | // Correct for negative viewports by mirroring all vertices. We need to negate the height here, |
| 459 | // since the viewport height is already negated by the render backend. |
| 460 | out.Write("o.pos.xy *= sign(" I_PIXELCENTERCORRECTION ".xy * float2(1.0, -1.0));\n"); |
| 461 | |
| 462 | // The console GPU places the pixel center at 7/12 in screen space unless |
| 463 | // antialiasing is enabled, while D3D and OpenGL place it at 0.5. This results |
| 464 | // in some primitives being placed one pixel too far to the bottom-right, |
| 465 | // which in turn can be critical if it happens for clear quads. |
| 466 | // Hence, we compensate for this pixel center difference so that primitives |
| 467 | // get rasterized correctly. |
| 468 | out.Write("o.pos.xy = o.pos.xy - o.pos.w * " I_PIXELCENTERCORRECTION ".xy;\n"); |
| 469 | |
| 470 | if (vertex_rounding) |
| 471 | { |
| 472 | // By now our position is in clip space. However, higher resolutions than the Wii outputs |
| 473 | // cause an additional pixel offset. Due to a higher pixel density we need to correct this |
| 474 | // by converting our clip-space position into the Wii's screen-space. |
| 475 | // Acquire the right pixel and then convert it back. |
| 476 | out.Write("if (o.pos.w == 1.0f)\n" |
| 477 | "{{\n"); |
| 478 | |
| 479 | out.Write("\tfloat ss_pixel_x = ((o.pos.x + 1.0f) * (" I_VIEWPORT_SIZE ".x * 0.5f));\n" |
| 480 | "\tfloat ss_pixel_y = ((o.pos.y + 1.0f) * (" I_VIEWPORT_SIZE ".y * 0.5f));\n"); |
| 481 | |
| 482 | out.Write("\tss_pixel_x = round(ss_pixel_x);\n" |
| 483 | "\tss_pixel_y = round(ss_pixel_y);\n"); |
| 484 | |
| 485 | out.Write("\to.pos.x = ((ss_pixel_x / (" I_VIEWPORT_SIZE ".x * 0.5f)) - 1.0f);\n" |
| 486 | "\to.pos.y = ((ss_pixel_y / (" I_VIEWPORT_SIZE ".y * 0.5f)) - 1.0f);\n" |
| 487 | "}}\n"); |
| 488 | } |
| 489 | |
| 490 | if (host_config.backend_geometry_shaders) |
| 491 | { |
| 492 | AssignVSOutputMembers(out, "vs", "o", num_texgen, host_config); |
| 493 | } |
| 494 | else |
| 495 | { |
| 496 | // TODO: Pass interface blocks between shader stages even if geometry shaders |
| 497 | // are not supported, however that will require at least OpenGL 3.2 support. |
| 498 | for (u32 i = 0; i < num_texgen; ++i) |
| 499 | out.Write("tex{}.xyz = o.tex{};\n", i, i); |
| 500 | if (!host_config.fast_depth_calc) |
| 501 | out.Write("clipPos = o.clipPos;\n"); |
| 502 | if (per_pixel_lighting) |
| 503 | { |
| 504 | out.Write("Normal = o.Normal;\n" |
| 505 | "WorldPos = o.WorldPos;\n"); |
| 506 | } |
| 507 | out.Write("colors_0 = o.colors_0;\n" |
| 508 | "colors_1 = o.colors_1;\n"); |
| 509 | } |
| 510 | |
| 511 | if (host_config.backend_depth_clamp) |
| 512 | { |
| 513 | out.Write("gl_ClipDistance[0] = clipDist0;\n" |
| 514 | "gl_ClipDistance[1] = clipDist1;\n"); |
| 515 | } |
| 516 | |
| 517 | // Vulkan NDC space has Y pointing down (right-handed NDC space). |
| 518 | if (api_type == APIType::Vulkan) |
| 519 | out.Write("gl_Position = float4(o.pos.x, -o.pos.y, o.pos.z, o.pos.w);\n"); |
| 520 | else |
| 521 | out.Write("gl_Position = o.pos;\n"); |
| 522 | out.Write("}}\n"); |
| 523 | |
| 524 | return out; |
| 525 | } |
| 526 | |
| 527 | static void GenVertexShaderTexGens(APIType api_type, const ShaderHostConfig& host_config, |
| 528 | u32 num_texgen, ShaderCode& out) |
| 529 | { |
| 530 | // The HLSL compiler complains that the output texture coordinates are uninitialized when trying |
| 531 | // to dynamically index them. |
| 532 | for (u32 i = 0; i < num_texgen; i++) |
| 533 | out.Write("o.tex{} = float3(0.0, 0.0, 0.0);\n", i); |
| 534 | |
| 535 | out.Write("// Texture coordinate generation\n"); |
| 536 | if (num_texgen == 1) |
| 537 | { |
| 538 | out.Write("{{ const uint texgen = 0u;\n"); |
| 539 | } |
| 540 | else |
| 541 | { |
| 542 | out.Write("for (uint texgen = 0u; texgen < {}u; texgen++) {{\n", num_texgen); |
| 543 | } |
| 544 | |
| 545 | out.Write(" // Texcoord transforms\n"); |
| 546 | out.Write(" float4 coord = float4(0.0, 0.0, 1.0, 1.0);\n" |
| 547 | " uint texMtxInfo = xfmem_texMtxInfo(texgen);\n"); |
| 548 | out.Write(" switch ({}) {{\n", BitfieldExtract<&TexMtxInfo::sourcerow>("texMtxInfo")); |
| 549 | out.Write(" case {:s}:\n", SourceRow::Geom); |
| 550 | out.Write(" coord.xyz = rawpos.xyz;\n"); |
| 551 | out.Write(" break;\n\n"); |
| 552 | out.Write(" case {:s}:\n", SourceRow::Normal); |
| 553 | out.Write(" if ((components & {}u) != 0u) // VB_HAS_NORMAL\n" |
| 554 | " {{\n", |
| 555 | std::to_underlying(VB_HAS_NORMAL)); |
| 556 | LoadVertexAttribute(out, host_config, 6, "rawnormal", "float3", "float3"); |
| 557 | out.Write(" coord.xyz = rawnormal.xyz;\n" |
| 558 | " }}\n" |
| 559 | " break;\n\n"); |
| 560 | out.Write(" case {:s}:\n", SourceRow::BinormalT); |
| 561 | out.Write(" if ((components & {}u) != 0u) // VB_HAS_TANGENT\n" |
| 562 | " {{\n", |
| 563 | std::to_underlying(VB_HAS_TANGENT)); |
| 564 | LoadVertexAttribute(out, host_config, 6, "rawtangent", "float3", "float3"); |
| 565 | out.Write(" coord.xyz = rawtangent.xyz;\n" |
| 566 | " }}\n" |
| 567 | " break;\n\n"); |
| 568 | out.Write(" case {:s}:\n", SourceRow::BinormalB); |
| 569 | out.Write(" if ((components & {}u) != 0u) // VB_HAS_BINORMAL\n" |
| 570 | " {{\n", |
| 571 | std::to_underlying(VB_HAS_BINORMAL)); |
| 572 | LoadVertexAttribute(out, host_config, 6, "rawbinormal", "float3", "float3"); |
| 573 | out.Write(" coord.xyz = rawbinormal.xyz;\n" |
| 574 | " }}\n" |
| 575 | " break;\n\n"); |
| 576 | for (u32 i = 0; i < 8; i++) |
| 577 | { |
| 578 | out.Write(" case {:s}:\n", static_cast<SourceRow>(std::to_underlying(SourceRow::Tex0) + i)); |
| 579 | out.Write(" if ((components & {}u) != 0u) // VB_HAS_UV{}\n" |
| 580 | " {{\n", |
| 581 | VB_HAS_UV0 << i, i); |
| 582 | LoadVertexAttribute(out, host_config, 6, fmt::format("rawtex{}", i), "float3", "rawtex", |
| 583 | fmt::format("rawtex[{}][{}]", i / 4, i % 4)); |
| 584 | out.Write(" coord = float4(rawtex{}.x, rawtex{}.y, 1.0f, 1.0f);\n" |
| 585 | " }}\n", |
| 586 | i, i); |
| 587 | out.Write(" break;\n\n"); |
| 588 | } |
| 589 | out.Write(" }}\n" |
| 590 | "\n"); |
| 591 | |
| 592 | out.Write(" // Input form of AB11 sets z element to 1.0\n"); |
| 593 | out.Write(" if ({} == {:s}) // inputform == AB11\n", |
| 594 | BitfieldExtract<&TexMtxInfo::inputform>("texMtxInfo"), TexInputForm::AB11); |
| 595 | out.Write(" coord.z = 1.0f;\n" |
| 596 | "\n"); |
| 597 | |
| 598 | // Convert NaNs to 1 - needed to fix eyelids in Shadow the Hedgehog during cutscenes |
| 599 | // See https://bugs.dolphin-emu.org/issues/11458 |
| 600 | out.Write(" // Convert NaN to 1\n"); |
| 601 | out.Write(" if (dolphin_isnan(coord.x)) coord.x = 1.0;\n"); |
| 602 | out.Write(" if (dolphin_isnan(coord.y)) coord.y = 1.0;\n"); |
| 603 | out.Write(" if (dolphin_isnan(coord.z)) coord.z = 1.0;\n"); |
| 604 | |
| 605 | out.Write(" // first transformation\n"); |
| 606 | out.Write(" uint texgentype = {};\n", BitfieldExtract<&TexMtxInfo::texgentype>("texMtxInfo")); |
| 607 | out.Write(" float3 output_tex;\n" |
| 608 | " switch (texgentype)\n" |
| 609 | " {{\n"); |
| 610 | out.Write(" case {:s}:\n", TexGenType::EmbossMap); |
| 611 | out.Write(" {{\n"); |
| 612 | out.Write(" uint light = {};\n", |
| 613 | BitfieldExtract<&TexMtxInfo::embosslightshift>("texMtxInfo")); |
| 614 | out.Write(" uint source = {};\n", |
| 615 | BitfieldExtract<&TexMtxInfo::embosssourceshift>("texMtxInfo")); |
| 616 | out.Write(" switch (source) {{\n"); |
| 617 | for (u32 i = 0; i < num_texgen; i++) |
| 618 | out.Write(" case {}u: output_tex.xyz = o.tex{}; break;\n", i, i); |
| 619 | out.Write(" default: output_tex.xyz = float3(0.0, 0.0, 0.0); break;\n" |
| 620 | " }}\n" |
| 621 | " float3 ldir = normalize(" I_LIGHTS "[light].pos.xyz - pos.xyz);\n" |
| 622 | " output_tex.xyz += float3(dot(ldir, _tangent), dot(ldir, _binormal), 0.0);\n" |
| 623 | " }}\n" |
| 624 | " break;\n\n"); |
| 625 | out.Write(" case {:s}:\n", TexGenType::Color0); |
| 626 | out.Write(" output_tex.xyz = float3(o.colors_0.x, o.colors_0.y, 1.0);\n" |
| 627 | " break;\n\n"); |
| 628 | out.Write(" case {:s}:\n", TexGenType::Color1); |
| 629 | out.Write(" output_tex.xyz = float3(o.colors_1.x, o.colors_1.y, 1.0);\n" |
| 630 | " break;\n\n"); |
| 631 | out.Write(" case {:s}:\n", TexGenType::Regular); |
| 632 | out.Write(" default:\n" |
| 633 | " {{\n"); |
| 634 | out.Write(" if ((components & ({}u /* VB_HAS_TEXMTXIDX0 */ << texgen)) != 0u) {{\n", |
| 635 | std::to_underlying(VB_HAS_TEXMTXIDX0)); |
| 636 | if (host_config.backend_dynamic_vertex_loader || host_config.backend_vs_point_line_expand) |
| 637 | { |
| 638 | out.Write(" int tmp = int(load_input_float3_rawtex(vertex_base_offset, " |
| 639 | "vertex_offset_rawtex[texgen / 4][texgen % 4]).z);\n" |
| 640 | "\n"); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | out.Write( |
| 645 | " // This is messy, due to dynamic indexing of the input texture coordinates.\n" |
| 646 | " // Hopefully the compiler will unroll this whole loop anyway and the switch.\n" |
| 647 | " int tmp = 0;\n" |
| 648 | " switch (texgen) {{\n"); |
| 649 | for (u32 i = 0; i < num_texgen; i++) |
| 650 | out.Write(" case {}u: tmp = int(rawtex{}.z); break;\n", i, i); |
| 651 | out.Write(" }}\n" |
| 652 | "\n"); |
| 653 | } |
| 654 | out.Write(" if ({} == {:s}) {{\n", BitfieldExtract<&TexMtxInfo::projection>("texMtxInfo"), |
| 655 | TexSize::STQ); |
| 656 | out.Write(" output_tex.xyz = float3(dot(coord, " I_TRANSFORMMATRICES "[tmp]),\n" |
| 657 | " dot(coord, " I_TRANSFORMMATRICES "[tmp + 1]),\n" |
| 658 | " dot(coord, " I_TRANSFORMMATRICES "[tmp + 2]));\n" |
| 659 | " }} else {{\n" |
| 660 | " output_tex.xyz = float3(dot(coord, " I_TRANSFORMMATRICES "[tmp]),\n" |
| 661 | " dot(coord, " I_TRANSFORMMATRICES "[tmp + 1]),\n" |
| 662 | " 1.0);\n" |
| 663 | " }}\n" |
| 664 | " }} else {{\n"); |
| 665 | out.Write(" if ({} == {:s}) {{\n", BitfieldExtract<&TexMtxInfo::projection>("texMtxInfo"), |
| 666 | TexSize::STQ); |
| 667 | out.Write(" output_tex.xyz = float3(dot(coord, " I_TEXMATRICES "[3u * texgen]),\n" |
| 668 | " dot(coord, " I_TEXMATRICES "[3u * texgen + 1u]),\n" |
| 669 | " dot(coord, " I_TEXMATRICES "[3u * texgen + 2u]));\n" |
| 670 | " }} else {{\n" |
| 671 | " output_tex.xyz = float3(dot(coord, " I_TEXMATRICES "[3u * texgen]),\n" |
| 672 | " dot(coord, " I_TEXMATRICES "[3u * texgen + 1u]),\n" |
| 673 | " 1.0);\n" |
| 674 | " }}\n" |
| 675 | " }}\n" |
| 676 | " }}\n" |
| 677 | " break;\n\n" |
| 678 | " }}\n" |
| 679 | "\n"); |
| 680 | |
| 681 | out.Write(" if (xfmem_dualTexInfo != 0u) {{\n"); |
| 682 | out.Write(" uint postMtxInfo = xfmem_postMtxInfo(texgen);"); |
| 683 | out.Write(" uint base_index = {};\n", BitfieldExtract<&PostMtxInfo::index>("postMtxInfo")); |
| 684 | out.Write(" float4 P0 = " I_POSTTRANSFORMMATRICES "[base_index & 0x3fu];\n" |
| 685 | " float4 P1 = " I_POSTTRANSFORMMATRICES "[(base_index + 1u) & 0x3fu];\n" |
| 686 | " float4 P2 = " I_POSTTRANSFORMMATRICES "[(base_index + 2u) & 0x3fu];\n" |
| 687 | "\n"); |
| 688 | out.Write(" if ({} != 0u)\n", BitfieldExtract<&PostMtxInfo::normalize>("postMtxInfo")); |
| 689 | out.Write(" output_tex.xyz = normalize(output_tex.xyz);\n" |
| 690 | "\n" |
| 691 | " // multiply by postmatrix\n" |
| 692 | " output_tex.xyz = float3(dot(P0.xyz, output_tex.xyz) + P0.w,\n" |
| 693 | " dot(P1.xyz, output_tex.xyz) + P1.w,\n" |
| 694 | " dot(P2.xyz, output_tex.xyz) + P2.w);\n" |
| 695 | " }}\n\n"); |
| 696 | |
| 697 | // When q is 0, the GameCube appears to have a special case |
| 698 | // This can be seen in devkitPro's neheGX Lesson08 example for Wii |
| 699 | // Makes differences in Rogue Squadron 3 (Hoth sky) and The Last Story (shadow culling) |
| 700 | out.Write(" if (texgentype == {:s} && output_tex.z == 0.0)\n", TexGenType::Regular); |
| 701 | out.Write( |
| 702 | " output_tex.xy = clamp(output_tex.xy / 2.0f, float2(-1.0f,-1.0f), float2(1.0f,1.0f));\n" |
| 703 | "\n"); |
| 704 | |
| 705 | out.Write(" // Hopefully GPUs that can support dynamic indexing will optimize this.\n"); |
| 706 | out.Write(" switch (texgen) {{\n"); |
| 707 | for (u32 i = 0; i < num_texgen; i++) |
| 708 | out.Write(" case {}u: o.tex{} = output_tex; break;\n", i, i); |
| 709 | out.Write(" }}\n" |
| 710 | "}}\n"); |
| 711 | } |
| 712 | |
| 713 | static void LoadVertexAttribute(ShaderCode& code, const ShaderHostConfig& host_config, u32 indent, |
| 714 | std::string_view name, std::string_view shader_type, |
| 715 | std::string_view stored_type, std::string_view offset_name) |
| 716 | { |
| 717 | if (host_config.backend_dynamic_vertex_loader || host_config.backend_vs_point_line_expand) |
| 718 | { |
| 719 | code.Write("{:{}}{} {} = load_input_{}_{}(vertex_base_offset, vertex_offset_{});\n", "", indent, |
| 720 | shader_type, name, shader_type, stored_type, |
| 721 | offset_name.empty() ? name : offset_name); |
| 722 | } |
| 723 | // else inputs are always available |
| 724 | } |
| 725 | |
| 726 | void EnumerateVertexShaderUids(const std::function<void(const VertexShaderUid&)>& callback) |
| 727 | { |
| 728 | VertexShaderUid uid; |
| 729 | |
| 730 | for (u32 texgens = 0; texgens <= 8; texgens++) |
| 731 | { |
| 732 | vertex_ubershader_uid_data* const vuid = uid.GetUidData(); |
| 733 | vuid->num_texgens = texgens; |
| 734 | callback(uid); |
| 735 | } |
| 736 | } |
| 737 | } // namespace UberShader |