| 1 | // Copyright 2008 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/VertexShaderGen.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/CommonTypes.h" |
| 8 | #include "VideoCommon/BPMemory.h" |
| 9 | #include "VideoCommon/ConstantManager.h" |
| 10 | #include "VideoCommon/LightingShaderGen.h" |
| 11 | #include "VideoCommon/NativeVertexFormat.h" |
| 12 | #include "VideoCommon/VertexLoaderManager.h" |
| 13 | #include "VideoCommon/VideoCommon.h" |
| 14 | #include "VideoCommon/VideoConfig.h" |
| 15 | #include "VideoCommon/XFMemory.h" |
| 16 | |
| 17 | VertexShaderUid GetVertexShaderUid() |
| 18 | { |
| 19 | ASSERT(bpmem.genMode.numtexgens == xfmem.numTexGen.numTexGens); |
| 20 | ASSERT(bpmem.genMode.numcolchans == xfmem.numChan.numColorChans); |
| 21 | |
| 22 | VertexShaderUid out; |
| 23 | vertex_shader_uid_data* const uid_data = out.GetUidData(); |
| 24 | uid_data->numTexGens = xfmem.numTexGen.numTexGens; |
| 25 | uid_data->components = VertexLoaderManager::g_current_components; |
| 26 | uid_data->numColorChans = xfmem.numChan.numColorChans; |
| 27 | |
| 28 | GetLightingShaderUid(uid_data->lighting); |
| 29 | |
| 30 | // transform texcoords |
| 31 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 32 | { |
| 33 | auto& texinfo = uid_data->texMtxInfo[i]; |
| 34 | |
| 35 | texinfo.sourcerow = xfmem.texMtxInfo[i].sourcerow; |
| 36 | texinfo.texgentype = xfmem.texMtxInfo[i].texgentype; |
| 37 | texinfo.inputform = xfmem.texMtxInfo[i].inputform; |
| 38 | |
| 39 | // first transformation |
| 40 | switch (texinfo.texgentype) |
| 41 | { |
| 42 | case TexGenType::EmbossMap: // calculate tex coords into bump map |
| 43 | if ((uid_data->components & (VB_HAS_TANGENT | VB_HAS_BINORMAL)) != 0) |
| 44 | { |
| 45 | // transform the light dir into tangent space |
| 46 | texinfo.embosslightshift = xfmem.texMtxInfo[i].embosslightshift; |
| 47 | texinfo.embosssourceshift = xfmem.texMtxInfo[i].embosssourceshift; |
| 48 | } |
| 49 | else |
| 50 | { |
| 51 | texinfo.embosssourceshift = xfmem.texMtxInfo[i].embosssourceshift; |
| 52 | } |
| 53 | break; |
| 54 | case TexGenType::Color0: |
| 55 | case TexGenType::Color1: |
| 56 | break; |
| 57 | case TexGenType::Regular: |
| 58 | default: |
| 59 | uid_data->texMtxInfo_n_projection |= static_cast<u32>(xfmem.texMtxInfo[i].projection.Value()) |
| 60 | << i; |
| 61 | break; |
| 62 | } |
| 63 | |
| 64 | uid_data->dualTexTrans_enabled = xfmem.dualTexTrans.enabled; |
| 65 | // CHECKME: does this only work for regular tex gen types? |
| 66 | if (uid_data->dualTexTrans_enabled && texinfo.texgentype == TexGenType::Regular) |
| 67 | { |
| 68 | auto& postInfo = uid_data->postMtxInfo[i]; |
| 69 | postInfo.index = xfmem.postMtxInfo[i].index; |
| 70 | postInfo.normalize = xfmem.postMtxInfo[i].normalize; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return out; |
| 75 | } |
| 76 | |
| 77 | static void WritePrimitiveExpand(APIType api_type, const ShaderHostConfig& host_config, |
| 78 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 79 | { |
| 80 | if (uid_data->vs_expand == VSExpand::None) |
| 81 | return; |
| 82 | |
| 83 | out.Write("InputData dolphin_primitive_expand_data(int index_offset)\n"); |
| 84 | out.Write("{{\n"); |
| 85 | if (api_type == APIType::D3D) |
| 86 | { |
| 87 | // D3D doesn't include the base vertex in SV_VertexID |
| 88 | // See comment in UberShaderVertex for details |
| 89 | out.Write("\tuint vertex_id = (gl_VertexID >> 2) + base_vertex;\n"); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | out.Write("\tuint vertex_id = uint(gl_VertexID) >> 2u;\n"); |
| 94 | } |
| 95 | out.Write("\treturn input_buffer[vertex_id + index_offset];\n"); |
| 96 | out.Write("}}\n\n"); |
| 97 | } |
| 98 | |
| 99 | static void WriteTransformMatrices(APIType api_type, const ShaderHostConfig& host_config, |
| 100 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 101 | { |
| 102 | out.Write("mat3x4 dolphin_position_matrix()\n"); |
| 103 | out.Write("{{\n"); |
| 104 | out.Write("\tmat3x4 result;\n"); |
| 105 | if ((uid_data->components & VB_HAS_POSMTXIDX) != 0) |
| 106 | { |
| 107 | if (uid_data->vs_expand != VSExpand::None) |
| 108 | { |
| 109 | out.Write("\tInputData i = dolphin_primitive_expand_data(0);\n"); |
| 110 | out.Write("\tuvec4 posmtx = unpack_ubyte4(i.posmtx);\n"); |
| 111 | } |
| 112 | // Vertex format has a per-vertex matrix |
| 113 | out.Write("\tint posidx = int(posmtx.r);\n" |
| 114 | "\tresult[0] = " I_TRANSFORMMATRICES "[posidx];\n" |
| 115 | "\tresult[1] = " I_TRANSFORMMATRICES "[posidx + 1];\n" |
| 116 | "\tresult[2] = " I_TRANSFORMMATRICES "[posidx + 2];\n"); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | // One shared matrix |
| 121 | out.Write("\tresult[0] = " I_POSNORMALMATRIX "[0];\n" |
| 122 | "\tresult[1] = " I_POSNORMALMATRIX "[1];\n" |
| 123 | "\tresult[2] = " I_POSNORMALMATRIX "[2];\n"); |
| 124 | } |
| 125 | out.Write("\treturn result;\n"); |
| 126 | out.Write("}}\n\n"); |
| 127 | |
| 128 | // The scale of the transform matrix is used to control the size of the emboss map effect, by |
| 129 | // changing the scale of the transformed binormals (which only get used by emboss map texgens). |
| 130 | // By normalising the first transformed normal (which is used by lighting calculations and needs |
| 131 | // to be unit length), the same transform matrix can do double duty, scaling for emboss mapping, |
| 132 | // and not scaling for lighting. |
| 133 | out.Write("mat3 dolphin_normal_matrix()\n"); |
| 134 | out.Write("{{\n"); |
| 135 | out.Write("\tmat3 result;\n"); |
| 136 | if ((uid_data->components & VB_HAS_POSMTXIDX) != 0) |
| 137 | { |
| 138 | if (uid_data->vs_expand != VSExpand::None) |
| 139 | { |
| 140 | out.Write("\tInputData i = dolphin_primitive_expand_data(0);\n"); |
| 141 | out.Write("\tuvec4 posmtx = unpack_ubyte4(i.posmtx);\n"); |
| 142 | } |
| 143 | // Vertex format has a per-vertex matrix |
| 144 | out.Write("\tint posidx = int(posmtx.r);\n"); |
| 145 | out.Write("\tint normidx = posidx & 31;\n" |
| 146 | "\tresult[0] = " I_NORMALMATRICES "[normidx].xyz;\n" |
| 147 | "\tresult[1] = " I_NORMALMATRICES "[normidx + 1].xyz;\n" |
| 148 | "\tresult[2] = " I_NORMALMATRICES "[normidx + 2].xyz;\n"); |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | // One shared matrix |
| 153 | out.Write("\tresult[0] = " I_POSNORMALMATRIX "[3].xyz;\n" |
| 154 | "\tresult[1] = " I_POSNORMALMATRIX "[4].xyz;\n" |
| 155 | "\tresult[2] = " I_POSNORMALMATRIX "[5].xyz;\n"); |
| 156 | } |
| 157 | out.Write("\treturn result;\n"); |
| 158 | out.Write("}}\n\n"); |
| 159 | } |
| 160 | |
| 161 | static void WriteTexCoordTransforms(APIType api_type, const ShaderHostConfig& host_config, |
| 162 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 163 | { |
| 164 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 165 | { |
| 166 | auto& texinfo = uid_data->texMtxInfo[i]; |
| 167 | out.Write("vec3 dolphin_transform_texcoord{}(vec4 coord)\n", i); |
| 168 | out.Write("{{\n"); |
| 169 | if (texinfo.texgentype != TexGenType::Regular) |
| 170 | { |
| 171 | out.Write("\treturn vec3(coord.xyz);\n"); |
| 172 | } |
| 173 | else |
| 174 | { |
| 175 | out.Write("\tvec3 result;\n"); |
| 176 | if ((uid_data->components & (VB_HAS_TEXMTXIDX0 << i)) != 0) |
| 177 | { |
| 178 | out.Write("\tint tmp = int(rawtex{}.z);\n", i); |
| 179 | if (static_cast<TexSize>((uid_data->texMtxInfo_n_projection >> i) & 1) == TexSize::STQ) |
| 180 | { |
| 181 | out.Write("\tresult = vec3(dot(coord, " I_TRANSFORMMATRICES |
| 182 | "[tmp]), dot(coord, " I_TRANSFORMMATRICES |
| 183 | "[tmp+1]), dot(coord, " I_TRANSFORMMATRICES "[tmp+2]));\n"); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | out.Write("\tresult = vec3(dot(coord, " I_TRANSFORMMATRICES |
| 188 | "[tmp]), dot(coord, " I_TRANSFORMMATRICES "[tmp+1]), 1);\n"); |
| 189 | } |
| 190 | } |
| 191 | else |
| 192 | { |
| 193 | if (static_cast<TexSize>((uid_data->texMtxInfo_n_projection >> i) & 1) == TexSize::STQ) |
| 194 | { |
| 195 | out.Write("\tresult = vec3(dot(coord, " I_TEXMATRICES "[{}]), dot(coord, " I_TEXMATRICES |
| 196 | "[{}]), dot(coord, " I_TEXMATRICES "[{}]));\n", |
| 197 | 3 * i, 3 * i + 1, 3 * i + 2); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | out.Write("\tresult = vec3(dot(coord, " I_TEXMATRICES "[{}]), dot(coord, " I_TEXMATRICES |
| 202 | "[{}]), 1);\n", |
| 203 | 3 * i, 3 * i + 1); |
| 204 | } |
| 205 | } |
| 206 | // CHECKME: does this only work for regular tex gen types? |
| 207 | if (uid_data->dualTexTrans_enabled) |
| 208 | { |
| 209 | auto& postInfo = uid_data->postMtxInfo[i]; |
| 210 | |
| 211 | out.Write("\tvec4 P0 = " I_POSTTRANSFORMMATRICES "[{}];\n" |
| 212 | "\tvec4 P1 = " I_POSTTRANSFORMMATRICES "[{}];\n" |
| 213 | "\tvec4 P2 = " I_POSTTRANSFORMMATRICES "[{}];\n", |
| 214 | postInfo.index & 0x3f, (postInfo.index + 1) & 0x3f, (postInfo.index + 2) & 0x3f); |
| 215 | |
| 216 | if (postInfo.normalize) |
| 217 | out.Write("\tresult = normalize(result);\n"); |
| 218 | |
| 219 | // multiply by postmatrix |
| 220 | out.Write("\tresult = vec3(dot(P0.xyz, result) + P0.w, dot(P1.xyz, result) + " |
| 221 | "P1.w, dot(P2.xyz, result) + P2.w);\n"); |
| 222 | } |
| 223 | |
| 224 | // When q is 0, the GameCube appears to have a special case |
| 225 | // This can be seen in devkitPro's neheGX Lesson08 example for Wii |
| 226 | // Makes differences in Rogue Squadron 3 (Hoth sky) and The Last Story (shadow culling) |
| 227 | // TODO: check if this only affects XF_TEXGEN_REGULAR |
| 228 | out.Write("\tif(result.z == 0.0f)\n" |
| 229 | "\t\tresult.xy = clamp(result.xy / 2.0f, vec2(-1.0f,-1.0f), vec2(1.0f,1.0f));\n"); |
| 230 | out.Write("\treturn result;\n"); |
| 231 | } |
| 232 | out.Write("}}\n\n"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | static void WriteVertexStructs(APIType api_type, const ShaderHostConfig& host_config, |
| 237 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 238 | { |
| 239 | out.Write("struct DolphinVertexInput\n"); |
| 240 | out.Write("{{\n"); |
| 241 | out.Write("\tvec4 color_0;\n"); |
| 242 | out.Write("\tvec4 color_1;\n"); |
| 243 | out.Write("\tvec4 position;\n"); |
| 244 | out.Write("\tvec3 normal;\n"); |
| 245 | out.Write("\tvec3 binormal;\n"); |
| 246 | out.Write("\tvec3 tangent;\n"); |
| 247 | for (u32 i = 0; i < 8; i++) |
| 248 | { |
| 249 | out.Write("\tvec4 texture_coord_{};\n", i); |
| 250 | } |
| 251 | out.Write("}};\n\n"); |
| 252 | |
| 253 | out.Write("struct DolphinVertexOutput\n"); |
| 254 | out.Write("{{\n"); |
| 255 | out.Write("\tvec4 color_0;\n"); |
| 256 | out.Write("\tvec4 color_1;\n"); |
| 257 | out.Write("\tvec4 position;\n"); |
| 258 | out.Write("\tvec3 normal;\n"); |
| 259 | for (u32 i = 0; i < 8; i++) |
| 260 | { |
| 261 | out.Write("\tvec3 texture_coord_{};\n", i); |
| 262 | } |
| 263 | out.Write("}};\n\n"); |
| 264 | } |
| 265 | |
| 266 | static void WriteVertexDefines(APIType, const ShaderHostConfig&, |
| 267 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 268 | { |
| 269 | if ((uid_data->components & VB_HAS_COL0) != 0) |
| 270 | { |
| 271 | out.Write("#define HAS_COLOR_0 1\n"); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | out.Write("#define HAS_COLOR_0 0\n"); |
| 276 | } |
| 277 | |
| 278 | if ((uid_data->components & VB_HAS_COL1) != 0) |
| 279 | { |
| 280 | out.Write("#define HAS_COLOR_1 1\n"); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | out.Write("#define HAS_COLOR_1 0\n"); |
| 285 | } |
| 286 | |
| 287 | if ((uid_data->components & VB_HAS_NORMAL) != 0) |
| 288 | { |
| 289 | out.Write("#define HAS_NORMAL 1\n"); |
| 290 | } |
| 291 | else |
| 292 | { |
| 293 | out.Write("#define HAS_NORMAL 0\n"); |
| 294 | } |
| 295 | |
| 296 | if ((uid_data->components & VB_HAS_BINORMAL) != 0) |
| 297 | { |
| 298 | out.Write("#define HAS_BINORMAL 1\n"); |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | out.Write("#define HAS_BINORMAL 0\n"); |
| 303 | } |
| 304 | |
| 305 | if ((uid_data->components & VB_HAS_TANGENT) != 0) |
| 306 | { |
| 307 | out.Write("#define HAS_TANGENT 1\n"); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | out.Write("#define HAS_TANGENT 0\n"); |
| 312 | } |
| 313 | |
| 314 | for (u32 i = 0; i < uid_data->numTexGens; i++) |
| 315 | { |
| 316 | if ((uid_data->components & (VB_HAS_UV0 << i)) != 0) |
| 317 | { |
| 318 | out.Write("#define HAS_TEXTURE_COORD_{} 1\n", i); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | out.Write("#define HAS_TEXTURE_COORD_{} 0\n", i); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | for (u32 i = uid_data->numTexGens; i < 8; i++) |
| 327 | { |
| 328 | out.Write("#define HAS_TEXTURE_COORD_{} 0\n", i); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static void WriteEmulatedVertexBodyHeader(APIType api_type, const ShaderHostConfig& host_config, |
| 333 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 334 | { |
| 335 | constexpr std::string_view emulated_fragment_definition = |
| 336 | "void dolphin_process_emulated_vertex(in DolphinVertexInput vertex_input, out " |
| 337 | "DolphinVertexOutput vertex_output)"; |
| 338 | out.Write("{}\n", emulated_fragment_definition); |
| 339 | out.Write("{{\n"); |
| 340 | |
| 341 | WriteVertexBody(api_type, host_config, uid_data, out); |
| 342 | |
| 343 | out.Write("}}\n"); |
| 344 | } |
| 345 | |
| 346 | ShaderCode GenerateVertexShaderCode(APIType api_type, const ShaderHostConfig& host_config, |
| 347 | const vertex_shader_uid_data* uid_data, |
| 348 | CustomVertexContents custom_contents) |
| 349 | { |
| 350 | ShaderCode out; |
| 351 | |
| 352 | const bool per_pixel_lighting = g_ActiveConfig.bEnablePixelLighting; |
| 353 | const bool msaa = host_config.msaa; |
| 354 | const bool ssaa = host_config.ssaa; |
| 355 | const bool vertex_rounding = host_config.vertex_rounding; |
| 356 | |
| 357 | ShaderCode input_extract; |
| 358 | |
| 359 | out.Write("{}", s_lighting_struct); |
| 360 | |
| 361 | // uniforms |
| 362 | out.Write("UBO_BINDING(std140, 2) uniform VSBlock {{\n"); |
| 363 | |
| 364 | out.Write("{}", s_shader_uniforms); |
| 365 | out.Write("}};\n"); |
| 366 | |
| 367 | if (!custom_contents.uniforms.empty()) |
| 368 | { |
| 369 | out.Write("UBO_BINDING(std140, 3) uniform CustomShaderBlock {{\n"); |
| 370 | out.Write("{}", custom_contents.uniforms); |
| 371 | out.Write("}} custom_uniforms;\n"); |
| 372 | } |
| 373 | |
| 374 | if (uid_data->vs_expand != VSExpand::None) |
| 375 | { |
| 376 | out.Write("UBO_BINDING(std140, 4) uniform GSBlock {{\n"); |
| 377 | out.Write("{}", s_geometry_shader_uniforms); |
| 378 | out.Write("}};\n"); |
| 379 | |
| 380 | if (api_type == APIType::D3D) |
| 381 | { |
| 382 | // D3D doesn't include the base vertex in SV_VertexID |
| 383 | out.Write("UBO_BINDING(std140, 5) uniform DX_Constants {{\n" |
| 384 | " uint base_vertex;\n" |
| 385 | "}};\n\n"); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | out.Write("struct VS_OUTPUT {{\n"); |
| 390 | GenerateVSOutputMembers(out, api_type, uid_data->numTexGens, host_config, "", |
| 391 | ShaderStage::Vertex); |
| 392 | out.Write("}};\n\n"); |
| 393 | |
| 394 | WriteIsNanHeader(out, api_type); |
| 395 | GenerateLightingShaderHeader(out, uid_data->lighting); |
| 396 | |
| 397 | if (uid_data->vs_expand == VSExpand::None) |
| 398 | { |
| 399 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawpos;\n", ShaderAttrib::Position); |
| 400 | if ((uid_data->components & VB_HAS_POSMTXIDX) != 0) |
| 401 | out.Write("ATTRIBUTE_LOCATION({:s}) in uint4 posmtx;\n", ShaderAttrib::PositionMatrix); |
| 402 | if ((uid_data->components & VB_HAS_NORMAL) != 0) |
| 403 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawnormal;\n", ShaderAttrib::Normal); |
| 404 | if ((uid_data->components & VB_HAS_TANGENT) != 0) |
| 405 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawtangent;\n", ShaderAttrib::Tangent); |
| 406 | if ((uid_data->components & VB_HAS_BINORMAL) != 0) |
| 407 | out.Write("ATTRIBUTE_LOCATION({:s}) in float3 rawbinormal;\n", ShaderAttrib::Binormal); |
| 408 | |
| 409 | if ((uid_data->components & VB_HAS_COL0) != 0) |
| 410 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawcolor0;\n", ShaderAttrib::Color0); |
| 411 | if ((uid_data->components & VB_HAS_COL1) != 0) |
| 412 | out.Write("ATTRIBUTE_LOCATION({:s}) in float4 rawcolor1;\n", ShaderAttrib::Color1); |
| 413 | |
| 414 | for (u32 i = 0; i < 8; ++i) |
| 415 | { |
| 416 | const u32 has_texmtx = (uid_data->components & (VB_HAS_TEXMTXIDX0 << i)); |
| 417 | |
| 418 | if ((uid_data->components & (VB_HAS_UV0 << i)) != 0 || has_texmtx != 0) |
| 419 | { |
| 420 | out.Write("ATTRIBUTE_LOCATION({:s}) in float{} rawtex{};\n", ShaderAttrib::TexCoord0 + i, |
| 421 | has_texmtx != 0 ? 3 : 2, i); |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | else |
| 426 | { |
| 427 | // Can't use float3, etc because we want 4-byte alignment |
| 428 | out.Write( |
| 429 | "uint4 unpack_ubyte4(uint value) {{\n" |
| 430 | " return uint4(value & 0xffu, (value >> 8) & 0xffu, (value >> 16) & 0xffu, value >> 24);\n" |
| 431 | "}}\n\n" |
| 432 | "struct InputData {{\n"); |
| 433 | if (uid_data->components & VB_HAS_POSMTXIDX) |
| 434 | { |
| 435 | out.Write(" uint posmtx;\n"); |
| 436 | // Note: posmtx is handled in the matrix transform functions and |
| 437 | // doesn't need to be added to 'input_extract' |
| 438 | } |
| 439 | if (uid_data->position_has_3_elems) |
| 440 | { |
| 441 | out.Write(" float pos0;\n" |
| 442 | " float pos1;\n" |
| 443 | " float pos2;\n"); |
| 444 | input_extract.Write("float4 rawpos = float4(i.pos0, i.pos1, i.pos2, 1.0f);\n"); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | out.Write(" float pos0;\n" |
| 449 | " float pos1;\n"); |
| 450 | input_extract.Write("float4 rawpos = float4(i.pos0, i.pos1, 0.0f, 1.0f);\n"); |
| 451 | } |
| 452 | std::array<std::string_view, 3> names = {"normal", "binormal", "tangent"}; |
| 453 | for (int i = 0; i < 3; i++) |
| 454 | { |
| 455 | if (uid_data->components & (VB_HAS_NORMAL << i)) |
| 456 | { |
| 457 | out.Write(" float {0}0;\n" |
| 458 | " float {0}1;\n" |
| 459 | " float {0}2;\n", |
| 460 | names[i]); |
| 461 | input_extract.Write("float3 raw{0} = float3(i.{0}0, i.{0}1, i.{0}2);\n", names[i]); |
| 462 | } |
| 463 | } |
| 464 | for (int i = 0; i < 2; i++) |
| 465 | { |
| 466 | if (uid_data->components & (VB_HAS_COL0 << i)) |
| 467 | { |
| 468 | out.Write(" uint color{};\n", i); |
| 469 | input_extract.Write("float4 rawcolor{0} = float4(unpack_ubyte4(i.color{0})) / 255.0f;\n", |
| 470 | i); |
| 471 | } |
| 472 | } |
| 473 | for (int i = 0; i < 8; i++) |
| 474 | { |
| 475 | if (uid_data->components & (VB_HAS_UV0 << i)) |
| 476 | { |
| 477 | u32 ncomponents = (uid_data->texcoord_elem_count >> (2 * i)) & 3; |
| 478 | if (ncomponents < 2) |
| 479 | { |
| 480 | out.Write(" float tex{};\n", i); |
| 481 | input_extract.Write("float3 rawtex{0} = float3(i.tex{0}, 0.0f, 0.0f);\n", i); |
| 482 | } |
| 483 | else if (ncomponents == 2) |
| 484 | { |
| 485 | out.Write(" float tex{0}_0;\n" |
| 486 | " float tex{0}_1;\n", |
| 487 | i); |
| 488 | input_extract.Write("float3 rawtex{0} = float3(i.tex{0}_0, i.tex{0}_1, 0.0f);\n", i); |
| 489 | } |
| 490 | else |
| 491 | { |
| 492 | out.Write(" float tex{0}_0;\n" |
| 493 | " float tex{0}_1;\n" |
| 494 | " float tex{0}_2;\n", |
| 495 | i); |
| 496 | input_extract.Write("float3 rawtex{0} = float3(i.tex{0}_0, i.tex{0}_1, i.tex{0}_2);\n", |
| 497 | i); |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | out.Write("}};\n\n" |
| 502 | "SSBO_BINDING(1) readonly restrict buffer InputBuffer {{\n" |
| 503 | " InputData input_buffer[];\n" |
| 504 | "}};\n\n"); |
| 505 | } |
| 506 | |
| 507 | if (host_config.backend_geometry_shaders) |
| 508 | { |
| 509 | out.Write("VARYING_LOCATION(0) out VertexData {{\n"); |
| 510 | GenerateVSOutputMembers(out, api_type, uid_data->numTexGens, host_config, |
| 511 | GetInterpolationQualifier(msaa, ssaa, true, false), |
| 512 | ShaderStage::Vertex); |
| 513 | out.Write("}} vs;\n"); |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | // Let's set up attributes |
| 518 | u32 counter = 0; |
| 519 | out.Write("VARYING_LOCATION({}) {} out float4 colors_0;\n", counter++, |
| 520 | GetInterpolationQualifier(msaa, ssaa)); |
| 521 | out.Write("VARYING_LOCATION({}) {} out float4 colors_1;\n", counter++, |
| 522 | GetInterpolationQualifier(msaa, ssaa)); |
| 523 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 524 | { |
| 525 | out.Write("VARYING_LOCATION({}) {} out float3 tex{};\n", counter++, |
| 526 | GetInterpolationQualifier(msaa, ssaa), i); |
| 527 | } |
| 528 | if (!host_config.fast_depth_calc) |
| 529 | { |
| 530 | out.Write("VARYING_LOCATION({}) {} out float4 clipPos;\n", counter++, |
| 531 | GetInterpolationQualifier(msaa, ssaa)); |
| 532 | } |
| 533 | if (per_pixel_lighting) |
| 534 | { |
| 535 | out.Write("VARYING_LOCATION({}) {} out float3 Normal;\n", counter++, |
| 536 | GetInterpolationQualifier(msaa, ssaa)); |
| 537 | out.Write("VARYING_LOCATION({}) {} out float3 WorldPos;\n", counter++, |
| 538 | GetInterpolationQualifier(msaa, ssaa)); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | // Note: this is done after to ensure above global variables are accessible |
| 543 | WritePrimitiveExpand(api_type, host_config, uid_data, out); |
| 544 | WriteTransformMatrices(api_type, host_config, uid_data, out); |
| 545 | WriteTexCoordTransforms(api_type, host_config, uid_data, out); |
| 546 | WriteVertexDefines(api_type, host_config, uid_data, out); |
| 547 | WriteVertexStructs(api_type, host_config, uid_data, out); |
| 548 | WriteEmulatedVertexBodyHeader(api_type, host_config, uid_data, out); |
| 549 | |
| 550 | if (custom_contents.shader.empty()) |
| 551 | { |
| 552 | out.Write("void process_vertex(in DolphinVertexInput vertex_input, out DolphinVertexOutput " |
| 553 | "vertex_output)\n"); |
| 554 | out.Write("{{\n"); |
| 555 | |
| 556 | out.Write("\tdolphin_process_emulated_vertex(vertex_input, vertex_output);\n"); |
| 557 | |
| 558 | out.Write("}}\n"); |
| 559 | } |
| 560 | else |
| 561 | { |
| 562 | out.Write("{}\n", custom_contents.shader); |
| 563 | } |
| 564 | |
| 565 | out.Write("void main()\n{{\n"); |
| 566 | |
| 567 | if (uid_data->vs_expand != VSExpand::None) |
| 568 | { |
| 569 | out.Write("InputData i = dolphin_primitive_expand_data(0);\n" |
| 570 | "{}", |
| 571 | input_extract.GetBuffer()); |
| 572 | } |
| 573 | |
| 574 | out.Write("VS_OUTPUT o;\n"); |
| 575 | |
| 576 | // xfmem.numColorChans controls the number of color channels available to TEV, but we still need |
| 577 | // to generate all channels here, as it can be used in texgen. Cel-damage is an example of this. |
| 578 | out.Write("float4 vertex_color_0, vertex_color_1;\n"); |
| 579 | |
| 580 | // To use color 1, the vertex descriptor must have color 0 and 1. |
| 581 | // If color 1 is present but not color 0, it is used for lighting channel 0. |
| 582 | const bool use_color_1 = |
| 583 | (uid_data->components & (VB_HAS_COL0 | VB_HAS_COL1)) == (VB_HAS_COL0 | VB_HAS_COL1); |
| 584 | for (u32 color = 0; color < NUM_XF_COLOR_CHANNELS; color++) |
| 585 | { |
| 586 | if ((color == 0 || use_color_1) && (uid_data->components & (VB_HAS_COL0 << color)) != 0) |
| 587 | { |
| 588 | // Use color0 for channel 0, and color1 for channel 1 if both colors 0 and 1 are present. |
| 589 | out.Write("vertex_color_{0} = rawcolor{0};\n", color); |
| 590 | } |
| 591 | else if (color == 0 && (uid_data->components & VB_HAS_COL1) != 0) |
| 592 | { |
| 593 | // Use color1 for channel 0 if color0 is not present. |
| 594 | out.Write("vertex_color_{} = rawcolor1;\n", color); |
| 595 | } |
| 596 | else |
| 597 | { |
| 598 | out.Write("vertex_color_{0} = missing_color_value;\n", color); |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | out.Write("\tDolphinVertexInput vertex_input;\n"); |
| 603 | out.Write("\tvertex_input.color_0 = vertex_color_0;\n"); |
| 604 | out.Write("\tvertex_input.color_1 = vertex_color_1;\n"); |
| 605 | out.Write("\tvertex_input.position = rawpos;\n"); |
| 606 | |
| 607 | if ((uid_data->components & VB_HAS_NORMAL) != 0) |
| 608 | { |
| 609 | out.Write("\tvertex_input.normal = rawnormal;\n"); |
| 610 | } |
| 611 | else |
| 612 | { |
| 613 | out.Write("\tvertex_input.normal = " I_CACHED_NORMAL ".xyz;\n"); |
| 614 | } |
| 615 | |
| 616 | if ((uid_data->components & VB_HAS_BINORMAL) != 0) |
| 617 | { |
| 618 | out.Write("\tvertex_input.binormal = rawbinormal;\n"); |
| 619 | } |
| 620 | else |
| 621 | { |
| 622 | out.Write("\tvertex_input.binormal = " I_CACHED_BINORMAL ".xyz;\n"); |
| 623 | } |
| 624 | |
| 625 | if ((uid_data->components & VB_HAS_TANGENT) != 0) |
| 626 | { |
| 627 | out.Write("\tvertex_input.tangent = rawtangent;\n"); |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | out.Write("\tvertex_input.tangent = " I_CACHED_TANGENT ".xyz;\n"); |
| 632 | } |
| 633 | |
| 634 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 635 | { |
| 636 | auto& texinfo = uid_data->texMtxInfo[i]; |
| 637 | |
| 638 | out.Write("\t{{\n"); |
| 639 | out.Write("\t\tvec4 coord = vec4(0.0, 0.0, 1.0, 1.0);\n"); |
| 640 | switch (texinfo.sourcerow) |
| 641 | { |
| 642 | case SourceRow::Geom: |
| 643 | out.Write("\t\tcoord.xyz = rawpos.xyz;\n"); |
| 644 | break; |
| 645 | case SourceRow::Normal: |
| 646 | if ((uid_data->components & VB_HAS_NORMAL) != 0) |
| 647 | { |
| 648 | out.Write("\t\tcoord.xyz = rawnormal.xyz;\n"); |
| 649 | } |
| 650 | break; |
| 651 | case SourceRow::Colors: |
| 652 | ASSERT(texinfo.texgentype == TexGenType::Color0 || texinfo.texgentype == TexGenType::Color1); |
| 653 | break; |
| 654 | case SourceRow::BinormalT: |
| 655 | if ((uid_data->components & VB_HAS_TANGENT) != 0) |
| 656 | { |
| 657 | out.Write("\t\tcoord.xyz = rawtangent.xyz;\n"); |
| 658 | } |
| 659 | break; |
| 660 | case SourceRow::BinormalB: |
| 661 | if ((uid_data->components & VB_HAS_BINORMAL) != 0) |
| 662 | { |
| 663 | out.Write("\t\tcoord.xyz = rawbinormal.xyz;\n"); |
| 664 | } |
| 665 | break; |
| 666 | default: |
| 667 | ASSERT(texinfo.sourcerow >= SourceRow::Tex0 && texinfo.sourcerow <= SourceRow::Tex7); |
| 668 | u32 texnum = static_cast<u32>(texinfo.sourcerow) - static_cast<u32>(SourceRow::Tex0); |
| 669 | if ((uid_data->components & (VB_HAS_UV0 << (texnum))) != 0) |
| 670 | { |
| 671 | out.Write("\t\tcoord = vec4(rawtex{}.x, rawtex{}.y, 1.0, 1.0);\n", texnum, texnum); |
| 672 | } |
| 673 | break; |
| 674 | } |
| 675 | // Input form of AB11 sets z element to 1.0 |
| 676 | |
| 677 | if (texinfo.inputform == TexInputForm::AB11) |
| 678 | out.Write("\t\tcoord.z = 1.0;\n"); |
| 679 | |
| 680 | // Convert NaNs to 1 - needed to fix eyelids in Shadow the Hedgehog during cutscenes |
| 681 | // See https://bugs.dolphin-emu.org/issues/11458 |
| 682 | out.Write("\t\t// Convert NaN to 1\n"); |
| 683 | out.Write("\t\tif (dolphin_isnan(coord.x)) coord.x = 1.0;\n"); |
| 684 | out.Write("\t\tif (dolphin_isnan(coord.y)) coord.y = 1.0;\n"); |
| 685 | out.Write("\t\tif (dolphin_isnan(coord.z)) coord.z = 1.0;\n"); |
| 686 | |
| 687 | out.Write("\t\tvertex_input.texture_coord_{0} = coord;\n", i); |
| 688 | out.Write("\t}}\n"); |
| 689 | } |
| 690 | |
| 691 | // Initialize other texture coordinates that are unused |
| 692 | for (u32 i = uid_data->numTexGens; i < 8; i++) |
| 693 | { |
| 694 | out.Write("\tvertex_input.texture_coord_{0} = vec4(0, 0, 0, 0);\n", i); |
| 695 | } |
| 696 | |
| 697 | out.Write("\tDolphinVertexOutput vertex_output;\n"); |
| 698 | out.Write("\tprocess_vertex(vertex_input, vertex_output);\n"); |
| 699 | |
| 700 | out.Write("\to.pos = vec4(dot(" I_PROJECTION "[0], vertex_output.position), dot(" I_PROJECTION |
| 701 | "[1], vertex_output.position), dot(" I_PROJECTION |
| 702 | "[2], vertex_output.position), dot(" I_PROJECTION "[3], vertex_output.position));\n"); |
| 703 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 704 | { |
| 705 | out.Write("\to.tex{0} = vertex_output.texture_coord_{0};\n", i); |
| 706 | } |
| 707 | |
| 708 | out.Write("\to.colors_0 = vertex_output.color_0;\n"); |
| 709 | out.Write("\to.colors_1 = vertex_output.color_1;\n"); |
| 710 | if (per_pixel_lighting) |
| 711 | { |
| 712 | out.Write("\to.Normal = vertex_output.normal;\n"); |
| 713 | |
| 714 | // TODO: Rename, this is actually in Viewspace... |
| 715 | out.Write("\to.WorldPos = vertex_output.position.xyz;\n"); |
| 716 | } |
| 717 | |
| 718 | if (uid_data->vs_expand == VSExpand::Line) |
| 719 | { |
| 720 | out.Write("bool is_bottom = (gl_VertexID & 2) != 0;\n"); |
| 721 | out.Write("// Line expansion\n" |
| 722 | "int id_offset = 0;\n" |
| 723 | "if (is_bottom) {{\n" |
| 724 | " id_offset -= 1;\n" |
| 725 | "}} else {{\n" |
| 726 | " id_offset += 1;\n" |
| 727 | "}}\n" |
| 728 | "InputData other = dolphin_primitive_expand_data(id_offset);\n"); |
| 729 | if (uid_data->position_has_3_elems) |
| 730 | out.Write("float4 other_pos = float4(other.pos0, other.pos1, other.pos2, 1.0f);\n"); |
| 731 | else |
| 732 | out.Write("float4 other_pos = float4(other.pos0, other.pos1, 0.0f, 1.0f);\n"); |
| 733 | if (uid_data->components & VB_HAS_POSMTXIDX) |
| 734 | { |
| 735 | out.Write("uint other_posidx = other.posmtx & 0xff;\n" |
| 736 | "float4 other_p0 = " I_TRANSFORMMATRICES "[other_posidx];\n" |
| 737 | "float4 other_p1 = " I_TRANSFORMMATRICES "[other_posidx + 1];\n" |
| 738 | "float4 other_p2 = " I_TRANSFORMMATRICES "[other_posidx + 2];\n" |
| 739 | "other_pos = float4(dot(other_p0, other_pos), dot(other_p1, other_pos), " |
| 740 | "dot(other_p2, other_pos), 1.0f);\n"); |
| 741 | } |
| 742 | else |
| 743 | { |
| 744 | out.Write("other_pos = vec4(other_pos * dolphin_position_matrix(), 1.0);\n"); |
| 745 | } |
| 746 | |
| 747 | // Variable needed by GenerateVSLineExpansion |
| 748 | out.Write("bool is_right = (gl_VertexID & 1) != 0;\n"); |
| 749 | GenerateVSLineExpansion(out, "", uid_data->numTexGens); |
| 750 | } |
| 751 | else if (uid_data->vs_expand == VSExpand::Point) |
| 752 | { |
| 753 | // Variables needed by GenerateVSPointExpansion |
| 754 | out.Write("bool is_bottom = (gl_VertexID & 2) != 0;\n"); |
| 755 | out.Write("bool is_right = (gl_VertexID & 1) != 0;\n"); |
| 756 | out.Write("// Point expansion\n"); |
| 757 | GenerateVSPointExpansion(out, "", uid_data->numTexGens); |
| 758 | } |
| 759 | |
| 760 | // clipPos/w needs to be done in pixel shader, not here |
| 761 | if (!host_config.fast_depth_calc) |
| 762 | out.Write("o.clipPos = o.pos;\n"); |
| 763 | |
| 764 | // If we can disable the incorrect depth clipping planes using depth clamping, then we can do |
| 765 | // our own depth clipping and calculate the depth range before the perspective divide if |
| 766 | // necessary. |
| 767 | if (host_config.backend_depth_clamp) |
| 768 | { |
| 769 | // Since we're adjusting z for the depth range before the perspective divide, we have to do our |
| 770 | // own clipping. We want to clip so that -w <= z <= 0, which matches the console -1..0 range. |
| 771 | // We adjust our depth value for clipping purposes to match the perspective projection in the |
| 772 | // software backend, which is a hack to fix Sonic Adventure and Unleashed games. |
| 773 | out.Write("float clipDepth = o.pos.z * (1.0 - 1e-7);\n" |
| 774 | "float clipDist0 = clipDepth + o.pos.w;\n" // Near: z < -w |
| 775 | "float clipDist1 = -clipDepth;\n"); // Far: z > 0 |
| 776 | |
| 777 | if (host_config.backend_geometry_shaders) |
| 778 | { |
| 779 | out.Write("o.clipDist0 = clipDist0;\n" |
| 780 | "o.clipDist1 = clipDist1;\n"); |
| 781 | } |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | // Same depth adjustment for Sonic. Without depth clamping, it unfortunately |
| 786 | // affects non-clipping uses of depth too. |
| 787 | out.Write("o.pos.z = o.pos.z * (1.0 - 1e-7);\n"); |
| 788 | } |
| 789 | |
| 790 | // Write the true depth value. If the game uses depth textures, then the pixel shader will |
| 791 | // override it with the correct values if not then early z culling will improve speed. |
| 792 | // There are two different ways to do this, when the depth range is oversized, we process |
| 793 | // the depth range in the vertex shader, if not we let the host driver handle it. |
| 794 | // |
| 795 | // Adjust z for the depth range. We're using an equation which incorporates a depth inversion, |
| 796 | // so we can map the console -1..0 range to the 0..1 range used in the depth buffer. |
| 797 | // We have to handle the depth range in the vertex shader instead of after the perspective |
| 798 | // divide, because some games will use a depth range larger than what is allowed by the |
| 799 | // graphics API. These large depth ranges will still be clipped to the 0..1 range, so these |
| 800 | // games effectively add a depth bias to the values written to the depth buffer. |
| 801 | out.Write("o.pos.z = o.pos.w * " I_PIXELCENTERCORRECTION ".w - " |
| 802 | "o.pos.z * " I_PIXELCENTERCORRECTION ".z;\n"); |
| 803 | |
| 804 | if (!host_config.backend_clip_control) |
| 805 | { |
| 806 | // If the graphics API doesn't support a depth range of 0..1, then we need to map z to |
| 807 | // the -1..1 range. Unfortunately we have to use a subtraction, which is a lossy floating-point |
| 808 | // operation that can introduce a round-trip error. |
| 809 | out.Write("o.pos.z = o.pos.z * 2.0 - o.pos.w;\n"); |
| 810 | } |
| 811 | |
| 812 | // Correct for negative viewports by mirroring all vertices. We need to negate the height here, |
| 813 | // since the viewport height is already negated by the render backend. |
| 814 | out.Write("o.pos.xy *= sign(" I_PIXELCENTERCORRECTION ".xy * float2(1.0, -1.0));\n"); |
| 815 | |
| 816 | // The console GPU places the pixel center at 7/12 in screen space unless |
| 817 | // antialiasing is enabled, while D3D and OpenGL place it at 0.5. This results |
| 818 | // in some primitives being placed one pixel too far to the bottom-right, |
| 819 | // which in turn can be critical if it happens for clear quads. |
| 820 | // Hence, we compensate for this pixel center difference so that primitives |
| 821 | // get rasterized correctly. |
| 822 | out.Write("o.pos.xy = o.pos.xy - o.pos.w * " I_PIXELCENTERCORRECTION ".xy;\n"); |
| 823 | |
| 824 | if (vertex_rounding) |
| 825 | { |
| 826 | // By now our position is in clip space |
| 827 | // however, higher resolutions than the Wii outputs |
| 828 | // cause an additional pixel offset |
| 829 | // due to a higher pixel density |
| 830 | // we need to correct this by converting our |
| 831 | // clip-space position into the Wii's screen-space |
| 832 | // acquire the right pixel and then convert it back |
| 833 | out.Write("if (o.pos.w == 1.0f)\n" |
| 834 | "{{\n" |
| 835 | |
| 836 | "\tfloat ss_pixel_x = ((o.pos.x + 1.0f) * (" I_VIEWPORT_SIZE ".x * 0.5f));\n" |
| 837 | "\tfloat ss_pixel_y = ((o.pos.y + 1.0f) * (" I_VIEWPORT_SIZE ".y * 0.5f));\n" |
| 838 | |
| 839 | "\tss_pixel_x = round(ss_pixel_x);\n" |
| 840 | "\tss_pixel_y = round(ss_pixel_y);\n" |
| 841 | |
| 842 | "\to.pos.x = ((ss_pixel_x / (" I_VIEWPORT_SIZE ".x * 0.5f)) - 1.0f);\n" |
| 843 | "\to.pos.y = ((ss_pixel_y / (" I_VIEWPORT_SIZE ".y * 0.5f)) - 1.0f);\n" |
| 844 | "}}\n"); |
| 845 | } |
| 846 | |
| 847 | if (host_config.backend_geometry_shaders) |
| 848 | { |
| 849 | AssignVSOutputMembers(out, "vs", "o", uid_data->numTexGens, host_config); |
| 850 | } |
| 851 | else |
| 852 | { |
| 853 | // TODO: Pass interface blocks between shader stages even if geometry shaders |
| 854 | // are not supported, however that will require at least OpenGL 3.2 support. |
| 855 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 856 | out.Write("tex{}.xyz = o.tex{};\n", i, i); |
| 857 | if (!host_config.fast_depth_calc) |
| 858 | out.Write("clipPos = o.clipPos;\n"); |
| 859 | if (per_pixel_lighting) |
| 860 | { |
| 861 | out.Write("Normal = o.Normal;\n" |
| 862 | "WorldPos = o.WorldPos;\n"); |
| 863 | } |
| 864 | out.Write("colors_0 = o.colors_0;\n" |
| 865 | "colors_1 = o.colors_1;\n"); |
| 866 | } |
| 867 | |
| 868 | if (host_config.backend_depth_clamp) |
| 869 | { |
| 870 | out.Write("gl_ClipDistance[0] = clipDist0;\n" |
| 871 | "gl_ClipDistance[1] = clipDist1;\n"); |
| 872 | } |
| 873 | |
| 874 | // Vulkan NDC space has Y pointing down (right-handed NDC space). |
| 875 | if (api_type == APIType::Vulkan) |
| 876 | out.Write("gl_Position = float4(o.pos.x, -o.pos.y, o.pos.z, o.pos.w);\n"); |
| 877 | else |
| 878 | out.Write("gl_Position = o.pos;\n"); |
| 879 | out.Write("}}\n"); |
| 880 | |
| 881 | return out; |
| 882 | } |
| 883 | |
| 884 | void WriteVertexBody(APIType api_type, const ShaderHostConfig& host_config, |
| 885 | const vertex_shader_uid_data* uid_data, ShaderCode& out) |
| 886 | { |
| 887 | out.Write( |
| 888 | "\tvertex_output.position = vec4(vertex_input.position * dolphin_position_matrix(), 1.0);\n"); |
| 889 | |
| 890 | out.Write("\tvertex_output.normal = normalize(vertex_input.normal * dolphin_normal_matrix());\n"); |
| 891 | |
| 892 | for (u32 chan = 0; chan < NUM_XF_COLOR_CHANNELS; chan++) |
| 893 | { |
| 894 | out.Write( |
| 895 | "\tvec4 vertex_lighting_{0} = dolphin_calculate_lighting_chn{0}(vertex_input.color_{0}, " |
| 896 | "vertex_output.position.xyz, vertex_output.normal);\n", |
| 897 | chan); |
| 898 | out.Write("\tvertex_output.color_{0} = vertex_lighting_{0};\n", chan); |
| 899 | } |
| 900 | |
| 901 | if (host_config.per_pixel_lighting) |
| 902 | { |
| 903 | // When per-pixel lighting is enabled, the vertex colors are passed through |
| 904 | // unmodified so we can evaluate the lighting in the pixel shader. |
| 905 | out.Write("\tvertex_output.color_0 = vertex_input.color_0;\n"); |
| 906 | out.Write("\tvertex_output.color_1 = vertex_input.color_1;\n"); |
| 907 | } |
| 908 | else |
| 909 | { |
| 910 | // The number of colors available to TEV is determined by numColorChans. |
| 911 | // We have to provide the fields to match the interface, so set to zero if it's not enabled. |
| 912 | if (uid_data->numColorChans == 0) |
| 913 | out.Write("\tvertex_output.color_0 = vec4(0.0, 0.0, 0.0, 0.0);\n"); |
| 914 | if (uid_data->numColorChans <= 1) |
| 915 | out.Write("\tvertex_output.color_1 = vec4(0.0, 0.0, 0.0, 0.0);\n"); |
| 916 | } |
| 917 | |
| 918 | for (u32 i = 0; i < uid_data->numTexGens; ++i) |
| 919 | { |
| 920 | auto& texinfo = uid_data->texMtxInfo[i]; |
| 921 | |
| 922 | switch (texinfo.texgentype) |
| 923 | { |
| 924 | case TexGenType::EmbossMap: // calculate tex coords into bump map |
| 925 | |
| 926 | out.Write("\t{{\n"); |
| 927 | // transform the light dir into tangent space |
| 928 | out.Write("\t\tvec3 ldir = normalize(" LIGHT_POS ".xyz - vertex_output.position.xyz);\n", |
| 929 | LIGHT_POS_PARAMS(texinfo.embosslightshift)); |
| 930 | |
| 931 | out.Write("\t\tvec3 tangent = vertex_input.tangent * dolphin_normal_matrix();\n"); |
| 932 | out.Write("\t\tvec3 binormal = vertex_input.binormal * dolphin_normal_matrix();\n"); |
| 933 | out.Write("\t\tvertex_output.texture_coord_{}.xyz = vertex_output.texture_coord_{}.xyz + " |
| 934 | "vec3(dot(ldir, tangent), " |
| 935 | "dot(ldir, binormal), 0.0);\n", |
| 936 | i, texinfo.embosssourceshift); |
| 937 | out.Write("\t}}\n"); |
| 938 | break; |
| 939 | case TexGenType::Color0: |
| 940 | out.Write("\tvertex_output.texture_coord_{}.xyz = vec3(vertex_lighting_0.x, " |
| 941 | "vertex_lighting_0.y, 1);\n", |
| 942 | i); |
| 943 | break; |
| 944 | case TexGenType::Color1: |
| 945 | out.Write("\tvertex_output.texture_coord_{}.xyz = vec3(vertex_lighting_1.x, " |
| 946 | "vertex_lighting_1.y, 1);\n", |
| 947 | i); |
| 948 | break; |
| 949 | case TexGenType::Regular: |
| 950 | out.Write("\tvertex_output.texture_coord_{0} = " |
| 951 | "dolphin_transform_texcoord{0}(vertex_input.texture_coord_{0});\n", |
| 952 | i); |
| 953 | break; |
| 954 | }; |
| 955 | } |
| 956 | |
| 957 | // Fill out output that is unused |
| 958 | for (u32 i = uid_data->numTexGens; i < 8; i++) |
| 959 | { |
| 960 | out.Write("\tvertex_output.texture_coord_{0} = vec3(0, 0, 0);\n", i); |
| 961 | } |
| 962 | } |